string: factorize skip_spaces and export it to be generally available
[safe/jmp/linux-2.6] / net / sched / cls_u32.c
index 8dbe369..07372f6 100644 (file)
@@ -75,23 +75,20 @@ struct tc_u_hnode
 
 struct tc_u_common
 {
-       struct tc_u_common      *next;
        struct tc_u_hnode       *hlist;
        struct Qdisc            *q;
        int                     refcnt;
        u32                     hgenerator;
 };
 
-static struct tcf_ext_map u32_ext_map = {
+static const struct tcf_ext_map u32_ext_map = {
        .action = TCA_U32_ACT,
        .police = TCA_U32_POLICE
 };
 
-static struct tc_u_common *u32_list;
-
-static __inline__ unsigned u32_hash_fold(u32 key, struct tc_u32_sel *sel, u8 fshift)
+static __inline__ unsigned u32_hash_fold(__be32 key, struct tc_u32_sel *sel, u8 fshift)
 {
-       unsigned h = (key & sel->hmask)>>fshift;
+       unsigned h = ntohl(key & sel->hmask)>>fshift;
 
        return h;
 }
@@ -137,7 +134,7 @@ next_knode:
 
                for (i = n->sel.nkeys; i>0; i--, key++) {
 
-                       if ((*(u32*)(ptr+key->off+(off2&key->offmask))^key->val)&key->mask) {
+                       if ((*(__be32*)(ptr+key->off+(off2&key->offmask))^key->val)&key->mask) {
                                n = n->next;
                                goto next_knode;
                        }
@@ -182,7 +179,7 @@ check_terminal:
                ht = n->ht_down;
                sel = 0;
                if (ht->divisor)
-                       sel = ht->divisor&u32_hash_fold(*(u32*)(ptr+n->sel.hoff), &n->sel,n->fshift);
+                       sel = ht->divisor&u32_hash_fold(*(__be32*)(ptr+n->sel.hoff), &n->sel,n->fshift);
 
                if (!(n->sel.flags&(TC_U32_VAROFFSET|TC_U32_OFFSET|TC_U32_EAT)))
                        goto next_ht;
@@ -190,7 +187,7 @@ check_terminal:
                if (n->sel.flags&(TC_U32_OFFSET|TC_U32_VAROFFSET)) {
                        off2 = n->sel.off + 3;
                        if (n->sel.flags&TC_U32_VAROFFSET)
-                               off2 += ntohs(n->sel.offmask & *(u16*)(ptr+n->sel.offoff)) >>n->sel.offshift;
+                               off2 += ntohs(n->sel.offmask & *(__be16*)(ptr+n->sel.offoff)) >>n->sel.offshift;
                        off2 &= ~3;
                }
                if (n->sel.flags&TC_U32_EAT) {
@@ -287,9 +284,7 @@ static int u32_init(struct tcf_proto *tp)
        struct tc_u_hnode *root_ht;
        struct tc_u_common *tp_c;
 
-       for (tp_c = u32_list; tp_c; tp_c = tp_c->next)
-               if (tp_c->q == tp->q)
-                       break;
+       tp_c = tp->q->u32_node;
 
        root_ht = kzalloc(sizeof(*root_ht), GFP_KERNEL);
        if (root_ht == NULL)
@@ -307,8 +302,7 @@ static int u32_init(struct tcf_proto *tp)
                        return -ENOBUFS;
                }
                tp_c->q = tp->q;
-               tp_c->next = u32_list;
-               u32_list = tp_c;
+               tp->q->u32_node = tp_c;
        }
 
        tp_c->refcnt++;
@@ -351,7 +345,7 @@ static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode* key)
                        }
                }
        }
-       BUG_TRAP(0);
+       WARN_ON(1);
        return 0;
 }
 
@@ -374,7 +368,7 @@ static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
        struct tc_u_common *tp_c = tp->data;
        struct tc_u_hnode **hn;
 
-       BUG_TRAP(!ht->refcnt);
+       WARN_ON(ht->refcnt);
 
        u32_clear_hnode(tp, ht);
 
@@ -386,38 +380,34 @@ static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
                }
        }
 
-       BUG_TRAP(0);
+       WARN_ON(1);
        return -ENOENT;
 }
 
 static void u32_destroy(struct tcf_proto *tp)
 {
        struct tc_u_common *tp_c = tp->data;
-       struct tc_u_hnode *root_ht = xchg(&tp->root, NULL);
+       struct tc_u_hnode *root_ht = tp->root;
 
-       BUG_TRAP(root_ht != NULL);
+       WARN_ON(root_ht == NULL);
 
        if (root_ht && --root_ht->refcnt == 0)
                u32_destroy_hnode(tp, root_ht);
 
        if (--tp_c->refcnt == 0) {
                struct tc_u_hnode *ht;
-               struct tc_u_common **tp_cp;
 
-               for (tp_cp = &u32_list; *tp_cp; tp_cp = &(*tp_cp)->next) {
-                       if (*tp_cp == tp_c) {
-                               *tp_cp = tp_c->next;
-                               break;
-                       }
-               }
+               tp->q->u32_node = NULL;
 
-               for (ht=tp_c->hlist; ht; ht = ht->next)
+               for (ht = tp_c->hlist; ht; ht = ht->next) {
+                       ht->refcnt--;
                        u32_clear_hnode(tp, ht);
+               }
 
                while ((ht = tp_c->hlist) != NULL) {
                        tp_c->hlist = ht->next;
 
-                       BUG_TRAP(ht->refcnt == 0);
+                       WARN_ON(ht->refcnt != 0);
 
                        kfree(ht);
                }
@@ -441,8 +431,12 @@ static int u32_delete(struct tcf_proto *tp, unsigned long arg)
        if (tp->root == ht)
                return -EINVAL;
 
-       if (--ht->refcnt == 0)
+       if (ht->refcnt == 1) {
+               ht->refcnt--;
                u32_destroy_hnode(tp, ht);
+       } else {
+               return -EBUSY;
+       }
 
        return 0;
 }
@@ -460,10 +454,20 @@ static u32 gen_new_kid(struct tc_u_hnode *ht, u32 handle)
        return handle|(i>0xFFF ? 0xFFF : i);
 }
 
+static const struct nla_policy u32_policy[TCA_U32_MAX + 1] = {
+       [TCA_U32_CLASSID]       = { .type = NLA_U32 },
+       [TCA_U32_HASH]          = { .type = NLA_U32 },
+       [TCA_U32_LINK]          = { .type = NLA_U32 },
+       [TCA_U32_DIVISOR]       = { .type = NLA_U32 },
+       [TCA_U32_SEL]           = { .len = sizeof(struct tc_u32_sel) },
+       [TCA_U32_INDEV]         = { .type = NLA_STRING, .len = IFNAMSIZ },
+       [TCA_U32_MARK]          = { .len = sizeof(struct tc_u32_mark) },
+};
+
 static int u32_set_parms(struct tcf_proto *tp, unsigned long base,
                         struct tc_u_hnode *ht,
-                        struct tc_u_knode *n, struct rtattr **tb,
-                        struct rtattr *est)
+                        struct tc_u_knode *n, struct nlattr **tb,
+                        struct nlattr *est)
 {
        int err;
        struct tcf_exts e;
@@ -473,9 +477,9 @@ static int u32_set_parms(struct tcf_proto *tp, unsigned long base,
                return err;
 
        err = -EINVAL;
-       if (tb[TCA_U32_LINK-1]) {
-               u32 handle = *(u32*)RTA_DATA(tb[TCA_U32_LINK-1]);
-               struct tc_u_hnode *ht_down = NULL;
+       if (tb[TCA_U32_LINK]) {
+               u32 handle = nla_get_u32(tb[TCA_U32_LINK]);
+               struct tc_u_hnode *ht_down = NULL, *ht_old;
 
                if (TC_U32_KEY(handle))
                        goto errout;
@@ -489,20 +493,21 @@ static int u32_set_parms(struct tcf_proto *tp, unsigned long base,
                }
 
                tcf_tree_lock(tp);
-               ht_down = xchg(&n->ht_down, ht_down);
+               ht_old = n->ht_down;
+               n->ht_down = ht_down;
                tcf_tree_unlock(tp);
 
-               if (ht_down)
-                       ht_down->refcnt--;
+               if (ht_old)
+                       ht_old->refcnt--;
        }
-       if (tb[TCA_U32_CLASSID-1]) {
-               n->res.classid = *(u32*)RTA_DATA(tb[TCA_U32_CLASSID-1]);
+       if (tb[TCA_U32_CLASSID]) {
+               n->res.classid = nla_get_u32(tb[TCA_U32_CLASSID]);
                tcf_bind_filter(tp, &n->res, base);
        }
 
 #ifdef CONFIG_NET_CLS_IND
-       if (tb[TCA_U32_INDEV-1]) {
-               int err = tcf_change_indev(tp, n->indev, tb[TCA_U32_INDEV-1]);
+       if (tb[TCA_U32_INDEV]) {
+               err = tcf_change_indev(tp, n->indev, tb[TCA_U32_INDEV]);
                if (err < 0)
                        goto errout;
        }
@@ -516,33 +521,34 @@ errout:
 }
 
 static int u32_change(struct tcf_proto *tp, unsigned long base, u32 handle,
-                     struct rtattr **tca,
+                     struct nlattr **tca,
                      unsigned long *arg)
 {
        struct tc_u_common *tp_c = tp->data;
        struct tc_u_hnode *ht;
        struct tc_u_knode *n;
        struct tc_u32_sel *s;
-       struct rtattr *opt = tca[TCA_OPTIONS-1];
-       struct rtattr *tb[TCA_U32_MAX];
+       struct nlattr *opt = tca[TCA_OPTIONS];
+       struct nlattr *tb[TCA_U32_MAX + 1];
        u32 htid;
        int err;
 
        if (opt == NULL)
                return handle ? -EINVAL : 0;
 
-       if (rtattr_parse_nested(tb, TCA_U32_MAX, opt) < 0)
-               return -EINVAL;
+       err = nla_parse_nested(tb, TCA_U32_MAX, opt, u32_policy);
+       if (err < 0)
+               return err;
 
        if ((n = (struct tc_u_knode*)*arg) != NULL) {
                if (TC_U32_KEY(n->handle) == 0)
                        return -EINVAL;
 
-               return u32_set_parms(tp, base, n->ht_up, n, tb, tca[TCA_RATE-1]);
+               return u32_set_parms(tp, base, n->ht_up, n, tb, tca[TCA_RATE]);
        }
 
-       if (tb[TCA_U32_DIVISOR-1]) {
-               unsigned divisor = *(unsigned*)RTA_DATA(tb[TCA_U32_DIVISOR-1]);
+       if (tb[TCA_U32_DIVISOR]) {
+               unsigned divisor = nla_get_u32(tb[TCA_U32_DIVISOR]);
 
                if (--divisor > 0x100)
                        return -EINVAL;
@@ -557,7 +563,7 @@ static int u32_change(struct tcf_proto *tp, unsigned long base, u32 handle,
                if (ht == NULL)
                        return -ENOBUFS;
                ht->tp_c = tp_c;
-               ht->refcnt = 0;
+               ht->refcnt = 1;
                ht->divisor = divisor;
                ht->handle = handle;
                ht->prio = tp->prio;
@@ -567,8 +573,8 @@ static int u32_change(struct tcf_proto *tp, unsigned long base, u32 handle,
                return 0;
        }
 
-       if (tb[TCA_U32_HASH-1]) {
-               htid = *(unsigned*)RTA_DATA(tb[TCA_U32_HASH-1]);
+       if (tb[TCA_U32_HASH]) {
+               htid = nla_get_u32(tb[TCA_U32_HASH]);
                if (TC_U32_HTID(htid) == TC_U32_ROOT) {
                        ht = tp->root;
                        htid = ht->handle;
@@ -592,11 +598,10 @@ static int u32_change(struct tcf_proto *tp, unsigned long base, u32 handle,
        } else
                handle = gen_new_kid(ht, htid);
 
-       if (tb[TCA_U32_SEL-1] == 0 ||
-           RTA_PAYLOAD(tb[TCA_U32_SEL-1]) < sizeof(struct tc_u32_sel))
+       if (tb[TCA_U32_SEL] == NULL)
                return -EINVAL;
 
-       s = RTA_DATA(tb[TCA_U32_SEL-1]);
+       s = nla_data(tb[TCA_U32_SEL]);
 
        n = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL);
        if (n == NULL)
@@ -613,36 +618,19 @@ static int u32_change(struct tcf_proto *tp, unsigned long base, u32 handle,
        memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
        n->ht_up = ht;
        n->handle = handle;
-{
-       u8 i = 0;
-       u32 mask = s->hmask;
-       if (mask) {
-               while (!(mask & 1)) {
-                       i++;
-                       mask>>=1;
-               }
-       }
-       n->fshift = i;
-}
+       n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0;
 
 #ifdef CONFIG_CLS_U32_MARK
-       if (tb[TCA_U32_MARK-1]) {
+       if (tb[TCA_U32_MARK]) {
                struct tc_u32_mark *mark;
 
-               if (RTA_PAYLOAD(tb[TCA_U32_MARK-1]) < sizeof(struct tc_u32_mark)) {
-#ifdef CONFIG_CLS_U32_PERF
-                       kfree(n->pf);
-#endif
-                       kfree(n);
-                       return -EINVAL;
-               }
-               mark = RTA_DATA(tb[TCA_U32_MARK-1]);
+               mark = nla_data(tb[TCA_U32_MARK]);
                memcpy(&n->mark, mark, sizeof(struct tc_u32_mark));
                n->mark.success = 0;
        }
 #endif
 
-       err = u32_set_parms(tp, base, ht, n, tb, tca[TCA_RATE-1]);
+       err = u32_set_parms(tp, base, ht, n, tb, tca[TCA_RATE]);
        if (err == 0) {
                struct tc_u_knode **ins;
                for (ins = &ht->ht[TC_U32_HASH(handle)]; *ins; ins = &(*ins)->next)
@@ -650,8 +638,9 @@ static int u32_change(struct tcf_proto *tp, unsigned long base, u32 handle,
                                break;
 
                n->next = *ins;
-               wmb();
+               tcf_tree_lock(tp);
                *ins = n;
+               tcf_tree_unlock(tp);
 
                *arg = (unsigned long)n;
                return 0;
@@ -703,66 +692,66 @@ static int u32_dump(struct tcf_proto *tp, unsigned long fh,
                     struct sk_buff *skb, struct tcmsg *t)
 {
        struct tc_u_knode *n = (struct tc_u_knode*)fh;
-       unsigned char *b = skb_tail_pointer(skb);
-       struct rtattr *rta;
+       struct nlattr *nest;
 
        if (n == NULL)
                return skb->len;
 
        t->tcm_handle = n->handle;
 
-       rta = (struct rtattr*)b;
-       RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
+       nest = nla_nest_start(skb, TCA_OPTIONS);
+       if (nest == NULL)
+               goto nla_put_failure;
 
        if (TC_U32_KEY(n->handle) == 0) {
                struct tc_u_hnode *ht = (struct tc_u_hnode*)fh;
                u32 divisor = ht->divisor+1;
-               RTA_PUT(skb, TCA_U32_DIVISOR, 4, &divisor);
+               NLA_PUT_U32(skb, TCA_U32_DIVISOR, divisor);
        } else {
-               RTA_PUT(skb, TCA_U32_SEL,
+               NLA_PUT(skb, TCA_U32_SEL,
                        sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
                        &n->sel);
                if (n->ht_up) {
                        u32 htid = n->handle & 0xFFFFF000;
-                       RTA_PUT(skb, TCA_U32_HASH, 4, &htid);
+                       NLA_PUT_U32(skb, TCA_U32_HASH, htid);
                }
                if (n->res.classid)
-                       RTA_PUT(skb, TCA_U32_CLASSID, 4, &n->res.classid);
+                       NLA_PUT_U32(skb, TCA_U32_CLASSID, n->res.classid);
                if (n->ht_down)
-                       RTA_PUT(skb, TCA_U32_LINK, 4, &n->ht_down->handle);
+                       NLA_PUT_U32(skb, TCA_U32_LINK, n->ht_down->handle);
 
 #ifdef CONFIG_CLS_U32_MARK
                if (n->mark.val || n->mark.mask)
-                       RTA_PUT(skb, TCA_U32_MARK, sizeof(n->mark), &n->mark);
+                       NLA_PUT(skb, TCA_U32_MARK, sizeof(n->mark), &n->mark);
 #endif
 
                if (tcf_exts_dump(skb, &n->exts, &u32_ext_map) < 0)
-                       goto rtattr_failure;
+                       goto nla_put_failure;
 
 #ifdef CONFIG_NET_CLS_IND
                if(strlen(n->indev))
-                       RTA_PUT(skb, TCA_U32_INDEV, IFNAMSIZ, n->indev);
+                       NLA_PUT_STRING(skb, TCA_U32_INDEV, n->indev);
 #endif
 #ifdef CONFIG_CLS_U32_PERF
-               RTA_PUT(skb, TCA_U32_PCNT,
+               NLA_PUT(skb, TCA_U32_PCNT,
                sizeof(struct tc_u32_pcnt) + n->sel.nkeys*sizeof(u64),
                        n->pf);
 #endif
        }
 
-       rta->rta_len = skb_tail_pointer(skb) - b;
+       nla_nest_end(skb, nest);
+
        if (TC_U32_KEY(n->handle))
                if (tcf_exts_dump_stats(skb, &n->exts, &u32_ext_map) < 0)
-                       goto rtattr_failure;
+                       goto nla_put_failure;
        return skb->len;
 
-rtattr_failure:
-       nlmsg_trim(skb, b);
+nla_put_failure:
+       nla_nest_cancel(skb, nest);
        return -1;
 }
 
-static struct tcf_proto_ops cls_u32_ops = {
-       .next           =       NULL,
+static struct tcf_proto_ops cls_u32_ops __read_mostly = {
        .kind           =       "u32",
        .classify       =       u32_classify,
        .init           =       u32_init,