Checkign in new iproute2
[iproute2.git] / tc / f_u32.c
index 9d527fc..0bb57ce 100644 (file)
 #include <arpa/inet.h>
 #include <string.h>
 #include <linux/if.h>
+#include <linux/if_ether.h>
 
 #include "utils.h"
 #include "tc_util.h"
 
+extern int show_pretty;
+
 static void explain(void)
 {
-       fprintf(stderr, "Usage: ... u32 [ match SELECTOR ... ] [ link HTID ] [ classid CLASSID ]\n");
-       fprintf(stderr, "               [ police POLICE_SPEC ] [ offset OFFSET_SPEC ]\n");
+       fprintf(stderr, "Usage: ... u32 [ match SELECTOR ... ] [ link HTID ]"
+               " [ classid CLASSID ]\n");
+       fprintf(stderr, "               [ police POLICE_SPEC ]"
+               " [ offset OFFSET_SPEC ]\n");
        fprintf(stderr, "               [ ht HTID ] [ hashkey HASHKEY_SPEC ]\n");
        fprintf(stderr, "               [ sample SAMPLE ]\n");
        fprintf(stderr, "or         u32 divisor DIVISOR\n");
        fprintf(stderr, "\n");
        fprintf(stderr, "Where: SELECTOR := SAMPLE SAMPLE ...\n");
-       fprintf(stderr, "       SAMPLE := { ip | ip6 | udp | tcp | icmp | u{32|16|8} | mark } SAMPLE_ARGS [divisor DIVISOR]\n");
+       fprintf(stderr, "       SAMPLE := { ip | ip6 | udp | tcp | icmp |"
+               " u{32|16|8} | mark } SAMPLE_ARGS [divisor DIVISOR]\n");
        fprintf(stderr, "       FILTERID := X:Y:Z\n");
+       fprintf(stderr, "\nNOTE: CLASSID is parsed at hexadecimal input.\n");
 }
 
 #define usage() return(-1)
@@ -109,7 +116,8 @@ char * sprint_u32_handle(__u32 handle, char *buf)
        return buf;
 }
 
-static int pack_key(struct tc_u32_sel *sel, __u32 key, __u32 mask, int off, int offmask)
+static int pack_key(struct tc_u32_sel *sel, __u32 key, __u32 mask,
+                   int off, int offmask)
 {
        int i;
        int hwm = sel->nkeys;
@@ -140,14 +148,16 @@ static int pack_key(struct tc_u32_sel *sel, __u32 key, __u32 mask, int off, int
        return 0;
 }
 
-static int pack_key32(struct tc_u32_sel *sel, __u32 key, __u32 mask, int off, int offmask)
+static int pack_key32(struct tc_u32_sel *sel, __u32 key, __u32 mask,
+                     int off, int offmask)
 {
        key = htonl(key);
        mask = htonl(mask);
        return pack_key(sel, key, mask, off, offmask);
 }
 
-static int pack_key16(struct tc_u32_sel *sel, __u32 key, __u32 mask, int off, int offmask)
+static int pack_key16(struct tc_u32_sel *sel, __u32 key, __u32 mask,
+                     int off, int offmask)
 {
        if (key > 0xFFFF || mask > 0xFFFF)
                return -1;
@@ -215,7 +225,8 @@ int parse_at(int *argc_p, char ***argv_p, int *off, int *offmask)
 }
 
 
-static int parse_u32(int *argc_p, char ***argv_p, struct tc_u32_sel *sel, int off, int offmask)
+static int parse_u32(int *argc_p, char ***argv_p, struct tc_u32_sel *sel,
+                    int off, int offmask)
 {
        int res = -1;
        int argc = *argc_p;
@@ -246,7 +257,8 @@ static int parse_u32(int *argc_p, char ***argv_p, struct tc_u32_sel *sel, int of
        return res;
 }
 
-static int parse_u16(int *argc_p, char ***argv_p, struct tc_u32_sel *sel, int off, int offmask)
+static int parse_u16(int *argc_p, char ***argv_p, struct tc_u32_sel *sel,
+                    int off, int offmask)
 {
        int res = -1;
        int argc = *argc_p;
@@ -276,7 +288,8 @@ static int parse_u16(int *argc_p, char ***argv_p, struct tc_u32_sel *sel, int of
        return res;
 }
 
-static int parse_u8(int *argc_p, char ***argv_p, struct tc_u32_sel *sel, int off, int offmask)
+static int parse_u8(int *argc_p, char ***argv_p, struct tc_u32_sel *sel,
+                   int off, int offmask)
 {
        int res = -1;
        int argc = *argc_p;
@@ -310,7 +323,8 @@ static int parse_u8(int *argc_p, char ***argv_p, struct tc_u32_sel *sel, int off
        return res;
 }
 
-static int parse_ip_addr(int *argc_p, char ***argv_p, struct tc_u32_sel *sel, int off)
+static int parse_ip_addr(int *argc_p, char ***argv_p, struct tc_u32_sel *sel,
+                        int off)
 {
        int res = -1;
        int argc = *argc_p;
@@ -344,7 +358,8 @@ static int parse_ip_addr(int *argc_p, char ***argv_p, struct tc_u32_sel *sel, in
        return res;
 }
 
-static int parse_ip6_addr(int *argc_p, char ***argv_p, struct tc_u32_sel *sel, int off)
+static int parse_ip6_addr(int *argc_p, char ***argv_p,
+                         struct tc_u32_sel *sel, int off)
 {
        int res = -1;
        int argc = *argc_p;
@@ -370,12 +385,16 @@ static int parse_ip6_addr(int *argc_p, char ***argv_p, struct tc_u32_sel *sel, i
        plen = addr.bitlen;
        for (i=0; i<plen; i+=32) {
 //             if (((i+31)&~0x1F)<=plen) {
-               if (((i+31))<=plen) {
-                       if ((res = pack_key(sel, addr.data[i/32], 0xFFFFFFFF, off+4*(i/32), offmask)) < 0)
+               if (i + 31 <= plen) {
+                       res = pack_key(sel, addr.data[i/32],
+                                      0xFFFFFFFF, off+4*(i/32), offmask);
+                       if (res < 0)
                                return -1;
-               } else if (i<plen) {
-                       __u32 mask = htonl(0xFFFFFFFF<<(32-(plen-i)));
-                       if ((res = pack_key(sel, addr.data[i/32], mask, off+4*(i/32), offmask)) < 0)
+               } else if (i < plen) {
+                       __u32 mask = htonl(0xFFFFFFFF << (32 - (plen -i )));
+                       res = pack_key(sel, addr.data[i/32],
+                                      mask, off+4*(i/32), offmask);
+                       if (res < 0)
                                return -1;
                }
        }
@@ -473,7 +492,7 @@ done:
        *argv_p = argv;
        return res;
 }
-
+                               
 static int parse_ip6(int *argc_p, char ***argv_p, struct tc_u32_sel *sel)
 {
        int res = -1;
@@ -564,6 +583,7 @@ done:
        return res;
 }
 
+
 static int parse_icmp(int *argc_p, char ***argv_p, struct tc_u32_sel *sel)
 {
        int res = -1;
@@ -626,7 +646,8 @@ static int parse_mark(int *argc_p, char ***argv_p, struct nlmsghdr *n)
        return res;
 }
 
-static int parse_selector(int *argc_p, char ***argv_p, struct tc_u32_sel *sel, struct nlmsghdr *n)
+static int parse_selector(int *argc_p, char ***argv_p,
+                         struct tc_u32_sel *sel, struct nlmsghdr *n)
 {
        int argc = *argc_p;
        char **argv = *argv_p;
@@ -771,7 +792,101 @@ static int parse_hashkey(int *argc_p, char ***argv_p, struct tc_u32_sel *sel)
        return 0;
 }
 
-static int u32_parse_opt(struct filter_util *qu, char *handle, int argc, char **argv, struct nlmsghdr *n)
+static void print_ipv4(FILE *f, const struct tc_u32_key *key)
+{
+       char abuf[256];
+
+       switch (key->off) {
+       case 0:
+               switch (ntohl(key->mask)) {
+               case 0x0f000000:
+                       fprintf(f, "\n  match IP ihl %u", ntohl(key->val) >> 24);
+                       return;
+               case 0x00ff0000:
+                       fprintf(f, "\n  match IP dsfield %#x", ntohl(key->val) >> 16);
+                       return;
+               }
+               break;
+       case 8:
+               if (ntohl(key->mask) == 0x00ff0000) {
+                       fprintf(f, "\n  match IP protocol %d", ntohl(key->val) >> 16);
+                       return;
+               }
+               break;
+       case 12:
+       case 16: {
+                       int bits = mask2bits(key->mask);
+                       if (bits >= 0) {
+                               fprintf(f, "\n  %s %s/%d", 
+                                       key->off == 12 ? "match IP src" : "match IP dst",
+                                       inet_ntop(AF_INET, &key->val,
+                                                 abuf, sizeof(abuf)),
+                                       bits);
+                               return;
+                       }
+               }
+               break;
+
+       case 20:
+               switch (ntohl(key->mask)) {
+               case 0x0000ffff:
+                       fprintf(f, "\n  match sport %u",
+                               ntohl(key->val) & 0xffff);
+                       return;
+               case 0xffff0000:
+                       fprintf(f, "\n  match dport %u",
+                               ntohl(key->val) >> 16);
+                       return;
+               case 0xffffffff:
+                       fprintf(f, "\n  match sport %u, match dport %u",
+                               ntohl(key->val) & 0xffff,
+                               ntohl(key->val) >> 16);
+
+                       return;
+               }
+               /* XXX: Default print_raw */
+       }
+}
+
+static void print_raw(FILE *f, const struct tc_u32_key *key)
+{
+       fprintf(f, "\n  match %08x/%08x at %s%d", 
+               (unsigned int)ntohl(key->val),
+               (unsigned int)ntohl(key->mask),
+               key->offmask ? "nexthdr+" : "",
+               key->off);
+}
+
+static const struct {
+       __u16 proto;
+       __u16 pad;
+       void (*pprinter)(FILE *f, const struct tc_u32_key *key);
+} u32_pprinters[] = {
+       {0,        0, print_raw},
+       {ETH_P_IP, 0, print_ipv4},
+};
+
+static void show_keys(FILE *f, const struct tc_u32_key *key)
+{
+       int i = 0;
+
+       if (!show_pretty)
+               goto show_k;
+
+       for (i = 0; i < sizeof(u32_pprinters) / sizeof(u32_pprinters[0]); i++) {
+               if (u32_pprinters[i].proto == ntohs(f_proto)) {
+show_k:
+                       u32_pprinters[i].pprinter(f, key);
+                       return;
+               }
+       }
+
+       i = 0;
+       goto show_k;
+}
+
+static int u32_parse_opt(struct filter_util *qu, char *handle,
+                        int argc, char **argv, struct nlmsghdr *n)
 {
        struct {
                struct tc_u32_sel sel;
@@ -779,7 +894,7 @@ static int u32_parse_opt(struct filter_util *qu, char *handle, int argc, char **
        } sel;
        struct tcmsg *t = NLMSG_DATA(n);
        struct rtattr *tail;
-       int sel_ok = 0;
+       int sel_ok = 0, terminal_ok = 0;
        int sample_ok = 0;
        __u32 htid = 0;
        __u32 order = 0;
@@ -833,7 +948,7 @@ static int u32_parse_opt(struct filter_util *qu, char *handle, int argc, char **
                } else if (matches(*argv, "divisor") == 0) {
                        unsigned divisor;
                        NEXT_ARG();
-                       if (get_unsigned(&divisor, *argv, 0) || 
+                       if (get_unsigned(&divisor, *argv, 0) ||
                            divisor == 0 ||
                            divisor > 0x100 || ((divisor - 1) & divisor)) {
                                fprintf(stderr, "Illegal \"divisor\"\n");
@@ -888,7 +1003,8 @@ static int u32_parse_opt(struct filter_util *qu, char *handle, int argc, char **
                                return -1;
                        }
                        if (sel2.sel.nkeys != 1) {
-                               fprintf(stderr, "\"sample\" must contain exactly ONE key.\n");
+                               fprintf(stderr, "\"sample\" must contain"
+                                       " exactly ONE key.\n");
                                return -1;
                        }
                        if (*argv != 0 && strcmp(*argv, "divisor") == 0) {
@@ -924,6 +1040,7 @@ static int u32_parse_opt(struct filter_util *qu, char *handle, int argc, char **
                                fprintf(stderr, "Illegal \"action\"\n");
                                return -1;
                        }
+                       terminal_ok++;
                        continue;
 
                } else if (matches(*argv, "police") == 0) {
@@ -932,6 +1049,7 @@ static int u32_parse_opt(struct filter_util *qu, char *handle, int argc, char **
                                fprintf(stderr, "Illegal \"police\"\n");
                                return -1;
                        }
+                       terminal_ok++;
                        continue;
                } else if (strcmp(*argv, "help") == 0) {
                        explain();
@@ -944,6 +1062,10 @@ static int u32_parse_opt(struct filter_util *qu, char *handle, int argc, char **
                argc--; argv++;
        }
 
+       /* We dont necessarily need class/flowids */
+       if (terminal_ok)
+               sel.sel.flags |= TC_U32_TERMINAL;
+       
        if (order) {
                if (TC_U32_NODE(t->tcm_handle) && order != TC_U32_NODE(t->tcm_handle)) {
                        fprintf(stderr, "\"order\" contradicts \"handle\"\n");
@@ -955,12 +1077,14 @@ static int u32_parse_opt(struct filter_util *qu, char *handle, int argc, char **
        if (htid)
                addattr_l(n, MAX_MSG, TCA_U32_HASH, &htid, 4);
        if (sel_ok)
-               addattr_l(n, MAX_MSG, TCA_U32_SEL, &sel, sizeof(sel.sel)+sel.sel.nkeys*sizeof(struct tc_u32_key));
+               addattr_l(n, MAX_MSG, TCA_U32_SEL, &sel, 
+                         sizeof(sel.sel)+sel.sel.nkeys*sizeof(struct tc_u32_key));
        tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
        return 0;
 }
 
-static int u32_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 handle)
+static int u32_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt,
+                        __u32 handle)
 {
        struct rtattr *tb[TCA_U32_MAX+1];
        struct tc_u32_sel *sel = NULL;
@@ -990,7 +1114,8 @@ static int u32_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt, __
                fprintf(f, "ht divisor %d ", *(__u32*)RTA_DATA(tb[TCA_U32_DIVISOR]));
        } else if (tb[TCA_U32_HASH]) {
                __u32 htid = *(__u32*)RTA_DATA(tb[TCA_U32_HASH]);
-               fprintf(f, "key ht %x bkt %x ", TC_U32_USERHTID(htid), TC_U32_HASH(htid));
+               fprintf(f, "key ht %x bkt %x ", TC_U32_USERHTID(htid),
+                       TC_U32_HASH(htid));
        } else {
                fprintf(f, "??? ");
        }
@@ -1004,7 +1129,8 @@ static int u32_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt, __
        }
        if (tb[TCA_U32_LINK]) {
                SPRINT_BUF(b1);
-               fprintf(f, "link %s ", sprint_u32_handle(*(__u32*)RTA_DATA(tb[TCA_U32_LINK]), b1));
+               fprintf(f, "link %s ",
+                       sprint_u32_handle(*(__u32*)RTA_DATA(tb[TCA_U32_LINK]), b1));
        }
 
        if (tb[TCA_U32_PCNT]) {
@@ -1031,17 +1157,12 @@ static int u32_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt, __
        }
 
        if (sel) {
-               int i;
-               struct tc_u32_key *key = sel->keys;
                if (sel->nkeys) {
-                       for (i=0; i<sel->nkeys; i++, key++) {
-                               fprintf(f, "\n  match %08x/%08x at %s%d",
-                                       (unsigned int)ntohl(key->val),
-                                       (unsigned int)ntohl(key->mask),
-                                       key->offmask ? "nexthdr+" : "",
-                                       key->off);
+                       int i;
+                       for (i=0; i<sel->nkeys; i++) {
+                               show_keys(f, sel->keys + i);
                                if (show_stats && NULL != pf)
-                                       fprintf(f, " (success %lld ) ",
+                                       fprintf(f, " (success %llu ) ",
                                                (unsigned long long) pf->kcnts[i]);
                        }
                }
@@ -1049,7 +1170,9 @@ static int u32_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt, __
                if (sel->flags&(TC_U32_VAROFFSET|TC_U32_OFFSET)) {
                        fprintf(f, "\n    offset ");
                        if (sel->flags&TC_U32_VAROFFSET)
-                               fprintf(f, "%04x>>%d at %d ", ntohs(sel->offmask), sel->offshift,  sel->offoff);
+                               fprintf(f, "%04x>>%d at %d ",
+                                       ntohs(sel->offmask),
+                                       sel->offshift,  sel->offoff);
                        if (sel->off)
                                fprintf(f, "plus %d ", sel->off);
                }