action pedit: stylistic changes

More modern layout.

Signed-off-by: Jamal Hadi Salim <jhs@mojatatu.com>
This commit is contained in:
Jamal Hadi Salim 2016-06-12 17:40:34 -04:00 committed by Stephen Hemminger
parent 8e45e44b79
commit d8694a30a4
1 changed files with 62 additions and 56 deletions

View File

@ -32,8 +32,7 @@
static struct m_pedit_util *pedit_list; static struct m_pedit_util *pedit_list;
static int pedit_debug; static int pedit_debug;
static void static void explain(void)
explain(void)
{ {
fprintf(stderr, "Usage: ... pedit munge <MUNGE> [CONTROL]\n"); fprintf(stderr, "Usage: ... pedit munge <MUNGE> [CONTROL]\n");
fprintf(stderr, fprintf(stderr,
@ -48,22 +47,24 @@ explain(void)
} }
static void static void usage(void)
usage(void)
{ {
explain(); explain();
exit(-1); exit(-1);
} }
static int static int pedit_parse_nopopt(int *argc_p, char ***argv_p,
pedit_parse_nopopt (int *argc_p, char ***argv_p, struct tc_pedit_sel *sel, struct tc_pedit_key *tkey) struct tc_pedit_sel *sel,
struct tc_pedit_key *tkey)
{ {
int argc = *argc_p; int argc = *argc_p;
char **argv = *argv_p; char **argv = *argv_p;
if (argc) { if (argc) {
fprintf(stderr, "Unknown action hence option \"%s\" is unparsable\n", *argv); fprintf(stderr,
return -1; "Unknown action hence option \"%s\" is unparsable\n",
*argv);
return -1;
} }
return 0; return 0;
@ -75,7 +76,7 @@ static struct m_pedit_util *get_pedit_kind(const char *str)
static void *pBODY; static void *pBODY;
void *dlh; void *dlh;
char buf[256]; char buf[256];
struct m_pedit_util *p; struct m_pedit_util *p;
for (p = pedit_list; p; p = p->next) { for (p = pedit_list; p; p = p->next) {
if (strcmp(p->id, str) == 0) if (strcmp(p->id, str) == 0)
@ -107,15 +108,14 @@ noexist:
p = malloc(sizeof(*p)); p = malloc(sizeof(*p));
if (p) { if (p) {
memset(p, 0, sizeof(*p)); memset(p, 0, sizeof(*p));
strncpy(p->id, str, sizeof(p->id)-1); strncpy(p->id, str, sizeof(p->id) - 1);
p->parse_peopt = pedit_parse_nopopt; p->parse_peopt = pedit_parse_nopopt;
goto reg; goto reg;
} }
return p; return p;
} }
int int pack_key(struct tc_pedit_sel *sel, struct tc_pedit_key *tkey)
pack_key(struct tc_pedit_sel *sel, struct tc_pedit_key *tkey)
{ {
int hwm = sel->nkeys; int hwm = sel->nkeys;
@ -137,9 +137,8 @@ pack_key(struct tc_pedit_sel *sel, struct tc_pedit_key *tkey)
return 0; return 0;
} }
int pack_key32(__u32 retain, struct tc_pedit_sel *sel,
int struct tc_pedit_key *tkey)
pack_key32(__u32 retain, struct tc_pedit_sel *sel, struct tc_pedit_key *tkey)
{ {
if (tkey->off > (tkey->off & ~3)) { if (tkey->off > (tkey->off & ~3)) {
fprintf(stderr, fprintf(stderr,
@ -152,11 +151,11 @@ pack_key32(__u32 retain, struct tc_pedit_sel *sel, struct tc_pedit_key *tkey)
return pack_key(sel, tkey); return pack_key(sel, tkey);
} }
int int pack_key16(__u32 retain, struct tc_pedit_sel *sel,
pack_key16(__u32 retain, struct tc_pedit_sel *sel, struct tc_pedit_key *tkey) struct tc_pedit_key *tkey)
{ {
int ind, stride; int ind, stride;
__u32 m[4] = {0x0000FFFF, 0xFF0000FF, 0xFFFF0000}; __u32 m[4] = { 0x0000FFFF, 0xFF0000FF, 0xFFFF0000 };
if (tkey->val > 0xFFFF || tkey->mask > 0xFFFF) { if (tkey->val > 0xFFFF || tkey->mask > 0xFFFF) {
fprintf(stderr, "pack_key16 bad value\n"); fprintf(stderr, "pack_key16 bad value\n");
@ -177,19 +176,20 @@ pack_key16(__u32 retain, struct tc_pedit_sel *sel, struct tc_pedit_key *tkey)
tkey->off &= ~3; tkey->off &= ~3;
if (pedit_debug) if (pedit_debug)
printf("pack_key16: Final val %08x mask %08x\n", tkey->val, tkey->mask); printf("pack_key16: Final val %08x mask %08x\n",
tkey->val, tkey->mask);
return pack_key(sel, tkey); return pack_key(sel, tkey);
} }
int int pack_key8(__u32 retain, struct tc_pedit_sel *sel, struct tc_pedit_key *tkey)
pack_key8(__u32 retain, struct tc_pedit_sel *sel, struct tc_pedit_key *tkey)
{ {
int ind, stride; int ind, stride;
__u32 m[4] = {0x00FFFFFF, 0xFF00FFFF, 0xFFFF00FF, 0xFFFFFF00}; __u32 m[4] = { 0x00FFFFFF, 0xFF00FFFF, 0xFFFF00FF, 0xFFFFFF00 };
if (tkey->val > 0xFF || tkey->mask > 0xFF) { if (tkey->val > 0xFF || tkey->mask > 0xFF) {
fprintf(stderr, "pack_key8 bad value (val %x mask %x\n", tkey->val, tkey->mask); fprintf(stderr, "pack_key8 bad value (val %x mask %x\n",
tkey->val, tkey->mask);
return -1; return -1;
} }
@ -202,12 +202,12 @@ pack_key8(__u32 retain, struct tc_pedit_sel *sel, struct tc_pedit_key *tkey)
tkey->off &= ~3; tkey->off &= ~3;
if (pedit_debug) if (pedit_debug)
printf("pack_key8: Final word off %d val %08x mask %08x\n", tkey->off, tkey->val, tkey->mask); printf("pack_key8: Final word off %d val %08x mask %08x\n",
tkey->off, tkey->val, tkey->mask);
return pack_key(sel, tkey); return pack_key(sel, tkey);
} }
int int parse_val(int *argc_p, char ***argv_p, __u32 * val, int type)
parse_val(int *argc_p, char ***argv_p, __u32 *val, int type)
{ {
int argc = *argc_p; int argc = *argc_p;
char **argv = *argv_p; char **argv = *argv_p;
@ -216,7 +216,7 @@ parse_val(int *argc_p, char ***argv_p, __u32 *val, int type)
return -1; return -1;
if (type == TINT) if (type == TINT)
return get_integer((int *) val, *argv, 0); return get_integer((int *)val, *argv, 0);
if (type == TU32) if (type == TU32)
return get_u32(val, *argv, 0); return get_u32(val, *argv, 0);
@ -238,8 +238,8 @@ parse_val(int *argc_p, char ***argv_p, __u32 *val, int type)
return -1; return -1;
} }
int int parse_cmd(int *argc_p, char ***argv_p, __u32 len, int type, __u32 retain,
parse_cmd(int *argc_p, char ***argv_p, __u32 len, int type, __u32 retain, struct tc_pedit_sel *sel, struct tc_pedit_key *tkey) struct tc_pedit_sel *sel, struct tc_pedit_key *tkey)
{ {
__u32 mask = 0, val = 0; __u32 mask = 0, val = 0;
__u32 o = 0xFF; __u32 o = 0xFF;
@ -251,7 +251,8 @@ parse_cmd(int *argc_p, char ***argv_p, __u32 len, int type, __u32 retain, struct
return -1; return -1;
if (pedit_debug) if (pedit_debug)
printf("parse_cmd argc %d %s offset %d length %d\n", argc, *argv, tkey->off, len); printf("parse_cmd argc %d %s offset %d length %d\n",
argc, *argv, tkey->off, len);
if (len == 2) if (len == 2)
o = 0xFFFF; o = 0xFFFF;
@ -271,13 +272,15 @@ parse_cmd(int *argc_p, char ***argv_p, __u32 len, int type, __u32 retain, struct
return -1; return -1;
} }
argc--; argv++; argc--;
argv++;
if (argc && matches(*argv, "retain") == 0) { if (argc && matches(*argv, "retain") == 0) {
NEXT_ARG(); NEXT_ARG();
if (parse_val(&argc, &argv, &retain, TU32)) if (parse_val(&argc, &argv, &retain, TU32))
return -1; return -1;
argc--; argv++; argc--;
argv++;
} }
tkey->val = val; tkey->val = val;
@ -302,15 +305,16 @@ parse_cmd(int *argc_p, char ***argv_p, __u32 len, int type, __u32 retain, struct
return -1; return -1;
done: done:
if (pedit_debug) if (pedit_debug)
printf("parse_cmd done argc %d %s offset %d length %d\n", argc, *argv, tkey->off, len); printf("parse_cmd done argc %d %s offset %d length %d\n",
argc, *argv, tkey->off, len);
*argc_p = argc; *argc_p = argc;
*argv_p = argv; *argv_p = argv;
return res; return res;
} }
int int parse_offset(int *argc_p, char ***argv_p, struct tc_pedit_sel *sel,
parse_offset(int *argc_p, char ***argv_p, struct tc_pedit_sel *sel, struct tc_pedit_key *tkey) struct tc_pedit_key *tkey)
{ {
int off; int off;
__u32 len, retain; __u32 len, retain;
@ -331,7 +335,6 @@ parse_offset(int *argc_p, char ***argv_p, struct tc_pedit_sel *sel, struct tc_pe
if (argc <= 0) if (argc <= 0)
return -1; return -1;
if (matches(*argv, "u32") == 0) { if (matches(*argv, "u32") == 0) {
len = 4; len = 4;
retain = 0xFFFFFFFF; retain = 0xFFFFFFFF;
@ -386,8 +389,7 @@ done:
return res; return res;
} }
static int static int parse_munge(int *argc_p, char ***argv_p, struct tc_pedit_sel *sel)
parse_munge(int *argc_p, char ***argv_p, struct tc_pedit_sel *sel)
{ {
struct tc_pedit_key tkey; struct tc_pedit_key tkey;
int argc = *argc_p; int argc = *argc_p;
@ -433,8 +435,8 @@ done:
return res; return res;
} }
int int parse_pedit(struct action_util *a, int *argc_p, char ***argv_p, int tca_id,
parse_pedit(struct action_util *a, int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n) struct nlmsghdr *n)
{ {
struct { struct {
struct tc_pedit_sel sel; struct tc_pedit_sel sel;
@ -459,13 +461,15 @@ parse_pedit(struct action_util *a, int *argc_p, char ***argv_p, int tca_id, stru
usage(); usage();
} else if (matches(*argv, "munge") == 0) { } else if (matches(*argv, "munge") == 0) {
if (!ok) { if (!ok) {
fprintf(stderr, "Illegal pedit construct (%s)\n", *argv); fprintf(stderr, "Bad pedit construct (%s)\n",
*argv);
explain(); explain();
return -1; return -1;
} }
NEXT_ARG(); NEXT_ARG();
if (parse_munge(&argc, &argv, &sel.sel)) { if (parse_munge(&argc, &argv, &sel.sel)) {
fprintf(stderr, "Illegal pedit construct (%s)\n", *argv); fprintf(stderr, "Bad pedit construct (%s)\n",
*argv);
explain(); explain();
return -1; return -1;
} }
@ -489,7 +493,7 @@ parse_pedit(struct action_util *a, int *argc_p, char ***argv_p, int tca_id, stru
sel.sel.action = TC_ACT_PIPE; sel.sel.action = TC_ACT_PIPE;
NEXT_ARG(); NEXT_ARG();
} else if (matches(*argv, "drop") == 0 || } else if (matches(*argv, "drop") == 0 ||
matches(*argv, "shot") == 0) { matches(*argv, "shot") == 0) {
sel.sel.action = TC_ACT_SHOT; sel.sel.action = TC_ACT_SHOT;
NEXT_ARG(); NEXT_ARG();
} else if (matches(*argv, "continue") == 0) { } else if (matches(*argv, "continue") == 0) {
@ -517,16 +521,17 @@ parse_pedit(struct action_util *a, int *argc_p, char ***argv_p, int tca_id, stru
tail = NLMSG_TAIL(n); tail = NLMSG_TAIL(n);
addattr_l(n, MAX_MSG, tca_id, NULL, 0); addattr_l(n, MAX_MSG, tca_id, NULL, 0);
addattr_l(n, MAX_MSG, TCA_PEDIT_PARMS, &sel, sizeof(sel.sel)+sel.sel.nkeys*sizeof(struct tc_pedit_key)); addattr_l(n, MAX_MSG, TCA_PEDIT_PARMS, &sel,
tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; sizeof(sel.sel) +
sel.sel.nkeys * sizeof(struct tc_pedit_key));
tail->rta_len = (void *)NLMSG_TAIL(n) - (void *)tail;
*argc_p = argc; *argc_p = argc;
*argv_p = argv; *argv_p = argv;
return 0; return 0;
} }
int int print_pedit(struct action_util *au, FILE *f, struct rtattr *arg)
print_pedit(struct action_util *au, FILE * f, struct rtattr *arg)
{ {
struct tc_pedit_sel *sel; struct tc_pedit_sel *sel;
struct rtattr *tb[TCA_PEDIT_MAX + 1]; struct rtattr *tb[TCA_PEDIT_MAX + 1];
@ -544,8 +549,10 @@ print_pedit(struct action_util *au, FILE * f, struct rtattr *arg)
} }
sel = RTA_DATA(tb[TCA_PEDIT_PARMS]); sel = RTA_DATA(tb[TCA_PEDIT_PARMS]);
fprintf(f, " pedit action %s keys %d\n ", action_n2a(sel->action, b1, sizeof (b1)), sel->nkeys); fprintf(f, " pedit action %s keys %d\n ",
fprintf(f, "\t index %d ref %d bind %d", sel->index, sel->refcnt, sel->bindcnt); action_n2a(sel->action, b1, sizeof(b1)), sel->nkeys);
fprintf(f, "\t index %d ref %d bind %d", sel->index, sel->refcnt,
sel->bindcnt);
if (show_stats) { if (show_stats) {
if (tb[TCA_PEDIT_TM]) { if (tb[TCA_PEDIT_TM]) {
@ -561,21 +568,20 @@ print_pedit(struct action_util *au, FILE * f, struct rtattr *arg)
for (i = 0; i < sel->nkeys; i++, key++) { for (i = 0; i < sel->nkeys; i++, key++) {
fprintf(f, "\n\t key #%d", i); fprintf(f, "\n\t key #%d", i);
fprintf(f, " at %d: val %08x mask %08x", fprintf(f, " at %d: val %08x mask %08x",
(unsigned int)key->off, (unsigned int)key->off,
(unsigned int)ntohl(key->val), (unsigned int)ntohl(key->val),
(unsigned int)ntohl(key->mask)); (unsigned int)ntohl(key->mask));
} }
} else { } else {
fprintf(f, "\npedit %x keys %d is not LEGIT", sel->index, sel->nkeys); fprintf(f, "\npedit %x keys %d is not LEGIT", sel->index,
sel->nkeys);
} }
fprintf(f, "\n "); fprintf(f, "\n ");
return 0; return 0;
} }
int int pedit_print_xstats(struct action_util *au, FILE *f, struct rtattr *xstats)
pedit_print_xstats(struct action_util *au, FILE *f, struct rtattr *xstats)
{ {
return 0; return 0;
} }