1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/sched/cls_u32.c Ugly (or Universal) 32bit key Packet Classifier.
5 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
7 * The filters are packed to hash tables of key nodes
8 * with a set of 32bit key/mask pairs at every node.
9 * Nodes reference next level hash tables etc.
11 * This scheme is the best universal classifier I managed to
12 * invent; it is not super-fast, but it is not slow (provided you
13 * program it correctly), and general enough. And its relative
14 * speed grows as the number of rules becomes larger.
16 * It seems that it represents the best middle point between
17 * speed and manageability both by human and by machine.
19 * It is especially useful for link sharing combined with QoS;
20 * pure RSVP doesn't need such a general approach and can use
21 * much simpler (and faster) schemes, sort of cls_rsvp.c.
23 * nfmark match added by Catalin(ux aka Dino) BOIE <catab at umbrella.ro>
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/types.h>
29 #include <linux/kernel.h>
30 #include <linux/string.h>
31 #include <linux/errno.h>
32 #include <linux/percpu.h>
33 #include <linux/rtnetlink.h>
34 #include <linux/skbuff.h>
35 #include <linux/bitmap.h>
36 #include <linux/netdevice.h>
37 #include <linux/hash.h>
38 #include <net/netlink.h>
39 #include <net/act_api.h>
40 #include <net/pkt_cls.h>
41 #include <linux/idr.h>
42 #include <net/tc_wrapper.h>
45 struct tc_u_knode __rcu *next;
47 struct tc_u_hnode __rcu *ht_up;
51 struct tcf_result res;
52 struct tc_u_hnode __rcu *ht_down;
53 #ifdef CONFIG_CLS_U32_PERF
54 struct tc_u32_pcnt __percpu *pf;
57 unsigned int in_hw_count;
58 #ifdef CONFIG_CLS_U32_MARK
61 u32 __percpu *pcpu_success;
63 struct rcu_work rwork;
64 /* The 'sel' field MUST be the last field in structure to allow for
65 * tc_u32_keys allocated at end of structure.
67 struct tc_u32_sel sel;
71 struct tc_u_hnode __rcu *next;
76 struct idr handle_idr;
80 /* The 'ht' field MUST be the last field in structure to allow for
81 * more entries allocated at end of structure.
83 struct tc_u_knode __rcu *ht[];
87 struct tc_u_hnode __rcu *hlist;
90 struct idr handle_idr;
91 struct hlist_node hnode;
95 static inline unsigned int u32_hash_fold(__be32 key,
96 const struct tc_u32_sel *sel,
99 unsigned int h = ntohl(key & sel->hmask) >> fshift;
104 TC_INDIRECT_SCOPE int u32_classify(struct sk_buff *skb,
105 const struct tcf_proto *tp,
106 struct tcf_result *res)
109 struct tc_u_knode *knode;
111 } stack[TC_U32_MAXDEPTH];
113 struct tc_u_hnode *ht = rcu_dereference_bh(tp->root);
114 unsigned int off = skb_network_offset(skb);
115 struct tc_u_knode *n;
119 #ifdef CONFIG_CLS_U32_PERF
125 n = rcu_dereference_bh(ht->ht[sel]);
129 struct tc_u32_key *key = n->sel.keys;
131 #ifdef CONFIG_CLS_U32_PERF
132 __this_cpu_inc(n->pf->rcnt);
136 if (tc_skip_sw(n->flags)) {
137 n = rcu_dereference_bh(n->next);
141 #ifdef CONFIG_CLS_U32_MARK
142 if ((skb->mark & n->mask) != n->val) {
143 n = rcu_dereference_bh(n->next);
146 __this_cpu_inc(*n->pcpu_success);
150 for (i = n->sel.nkeys; i > 0; i--, key++) {
151 int toff = off + key->off + (off2 & key->offmask);
154 if (skb_headroom(skb) + toff > INT_MAX)
157 data = skb_header_pointer(skb, toff, 4, &hdata);
160 if ((*data ^ key->val) & key->mask) {
161 n = rcu_dereference_bh(n->next);
164 #ifdef CONFIG_CLS_U32_PERF
165 __this_cpu_inc(n->pf->kcnts[j]);
170 ht = rcu_dereference_bh(n->ht_down);
173 if (n->sel.flags & TC_U32_TERMINAL) {
176 if (!tcf_match_indev(skb, n->ifindex)) {
177 n = rcu_dereference_bh(n->next);
180 #ifdef CONFIG_CLS_U32_PERF
181 __this_cpu_inc(n->pf->rhit);
183 r = tcf_exts_exec(skb, &n->exts, res);
185 n = rcu_dereference_bh(n->next);
191 n = rcu_dereference_bh(n->next);
196 if (sdepth >= TC_U32_MAXDEPTH)
198 stack[sdepth].knode = n;
199 stack[sdepth].off = off;
202 ht = rcu_dereference_bh(n->ht_down);
207 data = skb_header_pointer(skb, off + n->sel.hoff, 4,
211 sel = ht->divisor & u32_hash_fold(*data, &n->sel,
214 if (!(n->sel.flags & (TC_U32_VAROFFSET | TC_U32_OFFSET | TC_U32_EAT)))
217 if (n->sel.flags & (TC_U32_OFFSET | TC_U32_VAROFFSET)) {
218 off2 = n->sel.off + 3;
219 if (n->sel.flags & TC_U32_VAROFFSET) {
222 data = skb_header_pointer(skb,
227 off2 += ntohs(n->sel.offmask & *data) >>
232 if (n->sel.flags & TC_U32_EAT) {
243 n = stack[sdepth].knode;
244 ht = rcu_dereference_bh(n->ht_up);
245 off = stack[sdepth].off;
252 net_warn_ratelimited("cls_u32: dead loop\n");
256 static struct tc_u_hnode *u32_lookup_ht(struct tc_u_common *tp_c, u32 handle)
258 struct tc_u_hnode *ht;
260 for (ht = rtnl_dereference(tp_c->hlist);
262 ht = rtnl_dereference(ht->next))
263 if (ht->handle == handle)
269 static struct tc_u_knode *u32_lookup_key(struct tc_u_hnode *ht, u32 handle)
272 struct tc_u_knode *n = NULL;
274 sel = TC_U32_HASH(handle);
275 if (sel > ht->divisor)
278 for (n = rtnl_dereference(ht->ht[sel]);
280 n = rtnl_dereference(n->next))
281 if (n->handle == handle)
288 static void *u32_get(struct tcf_proto *tp, u32 handle)
290 struct tc_u_hnode *ht;
291 struct tc_u_common *tp_c = tp->data;
293 if (TC_U32_HTID(handle) == TC_U32_ROOT)
294 ht = rtnl_dereference(tp->root);
296 ht = u32_lookup_ht(tp_c, TC_U32_HTID(handle));
301 if (TC_U32_KEY(handle) == 0)
304 return u32_lookup_key(ht, handle);
307 /* Protected by rtnl lock */
308 static u32 gen_new_htid(struct tc_u_common *tp_c, struct tc_u_hnode *ptr)
310 int id = idr_alloc_cyclic(&tp_c->handle_idr, ptr, 1, 0x7FF, GFP_KERNEL);
313 return (id | 0x800U) << 20;
316 static struct hlist_head *tc_u_common_hash;
318 #define U32_HASH_SHIFT 10
319 #define U32_HASH_SIZE (1 << U32_HASH_SHIFT)
321 static void *tc_u_common_ptr(const struct tcf_proto *tp)
323 struct tcf_block *block = tp->chain->block;
325 /* The block sharing is currently supported only
326 * for classless qdiscs. In that case we use block
327 * for tc_u_common identification. In case the
328 * block is not shared, block->q is a valid pointer
329 * and we can use that. That works for classful qdiscs.
331 if (tcf_block_shared(block))
337 static struct hlist_head *tc_u_hash(void *key)
339 return tc_u_common_hash + hash_ptr(key, U32_HASH_SHIFT);
342 static struct tc_u_common *tc_u_common_find(void *key)
344 struct tc_u_common *tc;
345 hlist_for_each_entry(tc, tc_u_hash(key), hnode) {
352 static int u32_init(struct tcf_proto *tp)
354 struct tc_u_hnode *root_ht;
355 void *key = tc_u_common_ptr(tp);
356 struct tc_u_common *tp_c = tc_u_common_find(key);
358 root_ht = kzalloc(struct_size(root_ht, ht, 1), GFP_KERNEL);
363 root_ht->handle = tp_c ? gen_new_htid(tp_c, root_ht) : 0x80000000;
364 root_ht->prio = tp->prio;
365 root_ht->is_root = true;
366 idr_init(&root_ht->handle_idr);
369 tp_c = kzalloc(sizeof(*tp_c), GFP_KERNEL);
375 INIT_HLIST_NODE(&tp_c->hnode);
376 idr_init(&tp_c->handle_idr);
378 hlist_add_head(&tp_c->hnode, tc_u_hash(key));
382 RCU_INIT_POINTER(root_ht->next, tp_c->hlist);
383 rcu_assign_pointer(tp_c->hlist, root_ht);
386 rcu_assign_pointer(tp->root, root_ht);
391 static void __u32_destroy_key(struct tc_u_knode *n)
393 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
395 tcf_exts_destroy(&n->exts);
396 if (ht && --ht->refcnt == 0)
401 static void u32_destroy_key(struct tc_u_knode *n, bool free_pf)
403 tcf_exts_put_net(&n->exts);
404 #ifdef CONFIG_CLS_U32_PERF
408 #ifdef CONFIG_CLS_U32_MARK
410 free_percpu(n->pcpu_success);
412 __u32_destroy_key(n);
415 /* u32_delete_key_rcu should be called when free'ing a copied
416 * version of a tc_u_knode obtained from u32_init_knode(). When
417 * copies are obtained from u32_init_knode() the statistics are
418 * shared between the old and new copies to allow readers to
419 * continue to update the statistics during the copy. To support
420 * this the u32_delete_key_rcu variant does not free the percpu
423 static void u32_delete_key_work(struct work_struct *work)
425 struct tc_u_knode *key = container_of(to_rcu_work(work),
429 u32_destroy_key(key, false);
433 /* u32_delete_key_freepf_rcu is the rcu callback variant
434 * that free's the entire structure including the statistics
435 * percpu variables. Only use this if the key is not a copy
436 * returned by u32_init_knode(). See u32_delete_key_rcu()
437 * for the variant that should be used with keys return from
440 static void u32_delete_key_freepf_work(struct work_struct *work)
442 struct tc_u_knode *key = container_of(to_rcu_work(work),
446 u32_destroy_key(key, true);
450 static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode *key)
452 struct tc_u_common *tp_c = tp->data;
453 struct tc_u_knode __rcu **kp;
454 struct tc_u_knode *pkp;
455 struct tc_u_hnode *ht = rtnl_dereference(key->ht_up);
458 kp = &ht->ht[TC_U32_HASH(key->handle)];
459 for (pkp = rtnl_dereference(*kp); pkp;
460 kp = &pkp->next, pkp = rtnl_dereference(*kp)) {
462 RCU_INIT_POINTER(*kp, key->next);
465 tcf_unbind_filter(tp, &key->res);
466 idr_remove(&ht->handle_idr, key->handle);
467 tcf_exts_get_net(&key->exts);
468 tcf_queue_work(&key->rwork, u32_delete_key_freepf_work);
477 static void u32_clear_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h,
478 struct netlink_ext_ack *extack)
480 struct tcf_block *block = tp->chain->block;
481 struct tc_cls_u32_offload cls_u32 = {};
483 tc_cls_common_offload_init(&cls_u32.common, tp, h->flags, extack);
484 cls_u32.command = TC_CLSU32_DELETE_HNODE;
485 cls_u32.hnode.divisor = h->divisor;
486 cls_u32.hnode.handle = h->handle;
487 cls_u32.hnode.prio = h->prio;
489 tc_setup_cb_call(block, TC_SETUP_CLSU32, &cls_u32, false, true);
492 static int u32_replace_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h,
493 u32 flags, struct netlink_ext_ack *extack)
495 struct tcf_block *block = tp->chain->block;
496 struct tc_cls_u32_offload cls_u32 = {};
497 bool skip_sw = tc_skip_sw(flags);
498 bool offloaded = false;
501 tc_cls_common_offload_init(&cls_u32.common, tp, flags, extack);
502 cls_u32.command = TC_CLSU32_NEW_HNODE;
503 cls_u32.hnode.divisor = h->divisor;
504 cls_u32.hnode.handle = h->handle;
505 cls_u32.hnode.prio = h->prio;
507 err = tc_setup_cb_call(block, TC_SETUP_CLSU32, &cls_u32, skip_sw, true);
509 u32_clear_hw_hnode(tp, h, NULL);
511 } else if (err > 0) {
515 if (skip_sw && !offloaded)
521 static void u32_remove_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n,
522 struct netlink_ext_ack *extack)
524 struct tcf_block *block = tp->chain->block;
525 struct tc_cls_u32_offload cls_u32 = {};
527 tc_cls_common_offload_init(&cls_u32.common, tp, n->flags, extack);
528 cls_u32.command = TC_CLSU32_DELETE_KNODE;
529 cls_u32.knode.handle = n->handle;
531 tc_setup_cb_destroy(block, tp, TC_SETUP_CLSU32, &cls_u32, false,
532 &n->flags, &n->in_hw_count, true);
535 static int u32_replace_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n,
536 u32 flags, struct netlink_ext_ack *extack)
538 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
539 struct tcf_block *block = tp->chain->block;
540 struct tc_cls_u32_offload cls_u32 = {};
541 bool skip_sw = tc_skip_sw(flags);
544 tc_cls_common_offload_init(&cls_u32.common, tp, flags, extack);
545 cls_u32.command = TC_CLSU32_REPLACE_KNODE;
546 cls_u32.knode.handle = n->handle;
547 cls_u32.knode.fshift = n->fshift;
548 #ifdef CONFIG_CLS_U32_MARK
549 cls_u32.knode.val = n->val;
550 cls_u32.knode.mask = n->mask;
552 cls_u32.knode.val = 0;
553 cls_u32.knode.mask = 0;
555 cls_u32.knode.sel = &n->sel;
556 cls_u32.knode.res = &n->res;
557 cls_u32.knode.exts = &n->exts;
559 cls_u32.knode.link_handle = ht->handle;
561 err = tc_setup_cb_add(block, tp, TC_SETUP_CLSU32, &cls_u32, skip_sw,
562 &n->flags, &n->in_hw_count, true);
564 u32_remove_hw_knode(tp, n, NULL);
568 if (skip_sw && !(n->flags & TCA_CLS_FLAGS_IN_HW))
574 static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,
575 struct netlink_ext_ack *extack)
577 struct tc_u_common *tp_c = tp->data;
578 struct tc_u_knode *n;
581 for (h = 0; h <= ht->divisor; h++) {
582 while ((n = rtnl_dereference(ht->ht[h])) != NULL) {
583 RCU_INIT_POINTER(ht->ht[h],
584 rtnl_dereference(n->next));
586 tcf_unbind_filter(tp, &n->res);
587 u32_remove_hw_knode(tp, n, extack);
588 idr_remove(&ht->handle_idr, n->handle);
589 if (tcf_exts_get_net(&n->exts))
590 tcf_queue_work(&n->rwork, u32_delete_key_freepf_work);
592 u32_destroy_key(n, true);
597 static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,
598 struct netlink_ext_ack *extack)
600 struct tc_u_common *tp_c = tp->data;
601 struct tc_u_hnode __rcu **hn;
602 struct tc_u_hnode *phn;
604 WARN_ON(--ht->refcnt);
606 u32_clear_hnode(tp, ht, extack);
609 for (phn = rtnl_dereference(*hn);
611 hn = &phn->next, phn = rtnl_dereference(*hn)) {
613 u32_clear_hw_hnode(tp, ht, extack);
614 idr_destroy(&ht->handle_idr);
615 idr_remove(&tp_c->handle_idr, ht->handle);
616 RCU_INIT_POINTER(*hn, ht->next);
625 static void u32_destroy(struct tcf_proto *tp, bool rtnl_held,
626 struct netlink_ext_ack *extack)
628 struct tc_u_common *tp_c = tp->data;
629 struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
631 WARN_ON(root_ht == NULL);
633 if (root_ht && --root_ht->refcnt == 1)
634 u32_destroy_hnode(tp, root_ht, extack);
636 if (--tp_c->refcnt == 0) {
637 struct tc_u_hnode *ht;
639 hlist_del(&tp_c->hnode);
641 while ((ht = rtnl_dereference(tp_c->hlist)) != NULL) {
642 u32_clear_hnode(tp, ht, extack);
643 RCU_INIT_POINTER(tp_c->hlist, ht->next);
645 /* u32_destroy_key() will later free ht for us, if it's
646 * still referenced by some knode
648 if (--ht->refcnt == 0)
652 idr_destroy(&tp_c->handle_idr);
659 static int u32_delete(struct tcf_proto *tp, void *arg, bool *last,
660 bool rtnl_held, struct netlink_ext_ack *extack)
662 struct tc_u_hnode *ht = arg;
663 struct tc_u_common *tp_c = tp->data;
666 if (TC_U32_KEY(ht->handle)) {
667 u32_remove_hw_knode(tp, (struct tc_u_knode *)ht, extack);
668 ret = u32_delete_key(tp, (struct tc_u_knode *)ht);
673 NL_SET_ERR_MSG_MOD(extack, "Not allowed to delete root node");
677 if (ht->refcnt == 1) {
678 u32_destroy_hnode(tp, ht, extack);
680 NL_SET_ERR_MSG_MOD(extack, "Can not delete in-use filter");
685 *last = tp_c->refcnt == 1 && tp_c->knodes == 0;
689 static u32 gen_new_kid(struct tc_u_hnode *ht, u32 htid)
691 u32 index = htid | 0x800;
692 u32 max = htid | 0xFFF;
694 if (idr_alloc_u32(&ht->handle_idr, NULL, &index, max, GFP_KERNEL)) {
696 if (idr_alloc_u32(&ht->handle_idr, NULL, &index, max,
704 static const struct nla_policy u32_policy[TCA_U32_MAX + 1] = {
705 [TCA_U32_CLASSID] = { .type = NLA_U32 },
706 [TCA_U32_HASH] = { .type = NLA_U32 },
707 [TCA_U32_LINK] = { .type = NLA_U32 },
708 [TCA_U32_DIVISOR] = { .type = NLA_U32 },
709 [TCA_U32_SEL] = { .len = sizeof(struct tc_u32_sel) },
710 [TCA_U32_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ },
711 [TCA_U32_MARK] = { .len = sizeof(struct tc_u32_mark) },
712 [TCA_U32_FLAGS] = { .type = NLA_U32 },
715 static void u32_unbind_filter(struct tcf_proto *tp, struct tc_u_knode *n,
718 if (tb[TCA_U32_CLASSID])
719 tcf_unbind_filter(tp, &n->res);
722 static void u32_bind_filter(struct tcf_proto *tp, struct tc_u_knode *n,
723 unsigned long base, struct nlattr **tb)
725 if (tb[TCA_U32_CLASSID]) {
726 n->res.classid = nla_get_u32(tb[TCA_U32_CLASSID]);
727 tcf_bind_filter(tp, &n->res, base);
731 static int u32_set_parms(struct net *net, struct tcf_proto *tp,
732 struct tc_u_knode *n, struct nlattr **tb,
733 struct nlattr *est, u32 flags, u32 fl_flags,
734 struct netlink_ext_ack *extack)
736 int err, ifindex = -1;
738 err = tcf_exts_validate_ex(net, tp, tb, est, &n->exts, flags,
743 if (tb[TCA_U32_INDEV]) {
744 ifindex = tcf_change_indev(net, tb[TCA_U32_INDEV], extack);
749 if (tb[TCA_U32_LINK]) {
750 u32 handle = nla_get_u32(tb[TCA_U32_LINK]);
751 struct tc_u_hnode *ht_down = NULL, *ht_old;
753 if (TC_U32_KEY(handle)) {
754 NL_SET_ERR_MSG_MOD(extack, "u32 Link handle must be a hash table");
759 ht_down = u32_lookup_ht(tp->data, handle);
762 NL_SET_ERR_MSG_MOD(extack, "Link hash table not found");
765 if (ht_down->is_root) {
766 NL_SET_ERR_MSG_MOD(extack, "Not linking to root node");
772 ht_old = rtnl_dereference(n->ht_down);
773 rcu_assign_pointer(n->ht_down, ht_down);
780 n->ifindex = ifindex;
785 static void u32_replace_knode(struct tcf_proto *tp, struct tc_u_common *tp_c,
786 struct tc_u_knode *n)
788 struct tc_u_knode __rcu **ins;
789 struct tc_u_knode *pins;
790 struct tc_u_hnode *ht;
792 if (TC_U32_HTID(n->handle) == TC_U32_ROOT)
793 ht = rtnl_dereference(tp->root);
795 ht = u32_lookup_ht(tp_c, TC_U32_HTID(n->handle));
797 ins = &ht->ht[TC_U32_HASH(n->handle)];
799 /* The node must always exist for it to be replaced if this is not the
800 * case then something went very wrong elsewhere.
802 for (pins = rtnl_dereference(*ins); ;
803 ins = &pins->next, pins = rtnl_dereference(*ins))
804 if (pins->handle == n->handle)
807 idr_replace(&ht->handle_idr, n, n->handle);
808 RCU_INIT_POINTER(n->next, pins->next);
809 rcu_assign_pointer(*ins, n);
812 static struct tc_u_knode *u32_init_knode(struct net *net, struct tcf_proto *tp,
813 struct tc_u_knode *n)
815 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
816 struct tc_u32_sel *s = &n->sel;
817 struct tc_u_knode *new;
819 new = kzalloc(struct_size(new, sel.keys, s->nkeys), GFP_KERNEL);
823 RCU_INIT_POINTER(new->next, n->next);
824 new->handle = n->handle;
825 RCU_INIT_POINTER(new->ht_up, n->ht_up);
827 new->ifindex = n->ifindex;
828 new->fshift = n->fshift;
829 new->flags = n->flags;
830 RCU_INIT_POINTER(new->ht_down, ht);
832 #ifdef CONFIG_CLS_U32_PERF
833 /* Statistics may be incremented by readers during update
834 * so we must keep them in tact. When the node is later destroyed
835 * a special destroy call must be made to not free the pf memory.
840 #ifdef CONFIG_CLS_U32_MARK
843 /* Similarly success statistics must be moved as pointers */
844 new->pcpu_success = n->pcpu_success;
846 memcpy(&new->sel, s, struct_size(s, keys, s->nkeys));
848 if (tcf_exts_init(&new->exts, net, TCA_U32_ACT, TCA_U32_POLICE)) {
853 /* bump reference count as long as we hold pointer to structure */
860 static int u32_change(struct net *net, struct sk_buff *in_skb,
861 struct tcf_proto *tp, unsigned long base, u32 handle,
862 struct nlattr **tca, void **arg, u32 flags,
863 struct netlink_ext_ack *extack)
865 struct tc_u_common *tp_c = tp->data;
866 struct tc_u_hnode *ht;
867 struct tc_u_knode *n;
868 struct tc_u32_sel *s;
869 struct nlattr *opt = tca[TCA_OPTIONS];
870 struct nlattr *tb[TCA_U32_MAX + 1];
871 u32 htid, userflags = 0;
877 NL_SET_ERR_MSG_MOD(extack, "Filter handle requires options");
884 err = nla_parse_nested_deprecated(tb, TCA_U32_MAX, opt, u32_policy,
889 if (tb[TCA_U32_FLAGS]) {
890 userflags = nla_get_u32(tb[TCA_U32_FLAGS]);
891 if (!tc_flags_valid(userflags)) {
892 NL_SET_ERR_MSG_MOD(extack, "Invalid filter flags");
899 struct tc_u_knode *new;
901 if (TC_U32_KEY(n->handle) == 0) {
902 NL_SET_ERR_MSG_MOD(extack, "Key node id cannot be zero");
906 if ((n->flags ^ userflags) &
907 ~(TCA_CLS_FLAGS_IN_HW | TCA_CLS_FLAGS_NOT_IN_HW)) {
908 NL_SET_ERR_MSG_MOD(extack, "Key node flags do not match passed flags");
912 new = u32_init_knode(net, tp, n);
916 err = u32_set_parms(net, tp, new, tb, tca[TCA_RATE],
917 flags, new->flags, extack);
920 __u32_destroy_key(new);
924 u32_bind_filter(tp, new, base, tb);
926 err = u32_replace_hw_knode(tp, new, flags, extack);
928 u32_unbind_filter(tp, new, tb);
930 if (tb[TCA_U32_LINK]) {
931 struct tc_u_hnode *ht_old;
933 ht_old = rtnl_dereference(n->ht_down);
937 __u32_destroy_key(new);
941 if (!tc_in_hw(new->flags))
942 new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
944 u32_replace_knode(tp, tp_c, new);
945 tcf_unbind_filter(tp, &n->res);
946 tcf_exts_get_net(&n->exts);
947 tcf_queue_work(&n->rwork, u32_delete_key_work);
951 if (tb[TCA_U32_DIVISOR]) {
952 unsigned int divisor = nla_get_u32(tb[TCA_U32_DIVISOR]);
954 if (!is_power_of_2(divisor)) {
955 NL_SET_ERR_MSG_MOD(extack, "Divisor is not a power of 2");
958 if (divisor-- > 0x100) {
959 NL_SET_ERR_MSG_MOD(extack, "Exceeded maximum 256 hash buckets");
962 if (TC_U32_KEY(handle)) {
963 NL_SET_ERR_MSG_MOD(extack, "Divisor can only be used on a hash table");
966 ht = kzalloc(struct_size(ht, ht, divisor + 1), GFP_KERNEL);
970 handle = gen_new_htid(tp->data, ht);
976 err = idr_alloc_u32(&tp_c->handle_idr, ht, &handle,
984 ht->divisor = divisor;
987 idr_init(&ht->handle_idr);
988 ht->flags = userflags;
990 err = u32_replace_hw_hnode(tp, ht, userflags, extack);
992 idr_remove(&tp_c->handle_idr, handle);
997 RCU_INIT_POINTER(ht->next, tp_c->hlist);
998 rcu_assign_pointer(tp_c->hlist, ht);
1004 if (tb[TCA_U32_HASH]) {
1005 htid = nla_get_u32(tb[TCA_U32_HASH]);
1006 if (TC_U32_HTID(htid) == TC_U32_ROOT) {
1007 ht = rtnl_dereference(tp->root);
1010 ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
1012 NL_SET_ERR_MSG_MOD(extack, "Specified hash table not found");
1017 ht = rtnl_dereference(tp->root);
1021 if (ht->divisor < TC_U32_HASH(htid)) {
1022 NL_SET_ERR_MSG_MOD(extack, "Specified hash table buckets exceed configured value");
1026 /* At this point, we need to derive the new handle that will be used to
1027 * uniquely map the identity of this table match entry. The
1028 * identity of the entry that we need to construct is 32 bits made of:
1029 * htid(12b):bucketid(8b):node/entryid(12b)
1031 * At this point _we have the table(ht)_ in which we will insert this
1032 * entry. We carry the table's id in variable "htid".
1033 * Note that earlier code picked the ht selection either by a) the user
1034 * providing the htid specified via TCA_U32_HASH attribute or b) when
1035 * no such attribute is passed then the root ht, is default to at ID
1036 * 0x[800][00][000]. Rule: the root table has a single bucket with ID 0.
1037 * If OTOH the user passed us the htid, they may also pass a bucketid of
1038 * choice. 0 is fine. For example a user htid is 0x[600][01][000] it is
1039 * indicating hash bucketid of 1. Rule: the entry/node ID _cannot_ be
1040 * passed via the htid, so even if it was non-zero it will be ignored.
1042 * We may also have a handle, if the user passed one. The handle also
1043 * carries the same addressing of htid(12b):bucketid(8b):node/entryid(12b).
1044 * Rule: the bucketid on the handle is ignored even if one was passed;
1045 * rather the value on "htid" is always assumed to be the bucketid.
1048 /* Rule: The htid from handle and tableid from htid must match */
1049 if (TC_U32_HTID(handle) && TC_U32_HTID(handle ^ htid)) {
1050 NL_SET_ERR_MSG_MOD(extack, "Handle specified hash table address mismatch");
1053 /* Ok, so far we have a valid htid(12b):bucketid(8b) but we
1054 * need to finalize the table entry identification with the last
1055 * part - the node/entryid(12b)). Rule: Nodeid _cannot be 0_ for
1056 * entries. Rule: nodeid of 0 is reserved only for tables(see
1057 * earlier code which processes TC_U32_DIVISOR attribute).
1058 * Rule: The nodeid can only be derived from the handle (and not
1060 * Rule: if the handle specified zero for the node id example
1061 * 0x60000000, then pick a new nodeid from the pool of IDs
1062 * this hash table has been allocating from.
1063 * If OTOH it is specified (i.e for example the user passed a
1064 * handle such as 0x60000123), then we use it generate our final
1065 * handle which is used to uniquely identify the match entry.
1067 if (!TC_U32_NODE(handle)) {
1068 handle = gen_new_kid(ht, htid);
1070 handle = htid | TC_U32_NODE(handle);
1071 err = idr_alloc_u32(&ht->handle_idr, NULL, &handle,
1072 handle, GFP_KERNEL);
1077 /* The user did not give us a handle; lets just generate one
1078 * from the table's pool of nodeids.
1080 handle = gen_new_kid(ht, htid);
1083 if (tb[TCA_U32_SEL] == NULL) {
1084 NL_SET_ERR_MSG_MOD(extack, "Selector not specified");
1089 s = nla_data(tb[TCA_U32_SEL]);
1090 sel_size = struct_size(s, keys, s->nkeys);
1091 if (nla_len(tb[TCA_U32_SEL]) < sel_size) {
1096 n = kzalloc(struct_size(n, sel.keys, s->nkeys), GFP_KERNEL);
1102 #ifdef CONFIG_CLS_U32_PERF
1103 n->pf = __alloc_percpu(struct_size(n->pf, kcnts, s->nkeys),
1104 __alignof__(struct tc_u32_pcnt));
1111 unsafe_memcpy(&n->sel, s, sel_size,
1112 /* A composite flex-array structure destination,
1113 * which was correctly sized with struct_size(),
1114 * bounds-checked against nla_len(), and allocated
1116 RCU_INIT_POINTER(n->ht_up, ht);
1118 n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0;
1119 n->flags = userflags;
1121 err = tcf_exts_init(&n->exts, net, TCA_U32_ACT, TCA_U32_POLICE);
1125 #ifdef CONFIG_CLS_U32_MARK
1126 n->pcpu_success = alloc_percpu(u32);
1127 if (!n->pcpu_success) {
1132 if (tb[TCA_U32_MARK]) {
1133 struct tc_u32_mark *mark;
1135 mark = nla_data(tb[TCA_U32_MARK]);
1137 n->mask = mark->mask;
1141 err = u32_set_parms(net, tp, n, tb, tca[TCA_RATE],
1142 flags, n->flags, extack);
1144 u32_bind_filter(tp, n, base, tb);
1147 struct tc_u_knode __rcu **ins;
1148 struct tc_u_knode *pins;
1150 err = u32_replace_hw_knode(tp, n, flags, extack);
1154 if (!tc_in_hw(n->flags))
1155 n->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
1157 ins = &ht->ht[TC_U32_HASH(handle)];
1158 for (pins = rtnl_dereference(*ins); pins;
1159 ins = &pins->next, pins = rtnl_dereference(*ins))
1160 if (TC_U32_NODE(handle) < TC_U32_NODE(pins->handle))
1163 RCU_INIT_POINTER(n->next, pins);
1164 rcu_assign_pointer(*ins, n);
1171 u32_unbind_filter(tp, n, tb);
1173 #ifdef CONFIG_CLS_U32_MARK
1174 free_percpu(n->pcpu_success);
1178 tcf_exts_destroy(&n->exts);
1179 #ifdef CONFIG_CLS_U32_PERF
1185 idr_remove(&ht->handle_idr, handle);
1189 static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg,
1192 struct tc_u_common *tp_c = tp->data;
1193 struct tc_u_hnode *ht;
1194 struct tc_u_knode *n;
1200 for (ht = rtnl_dereference(tp_c->hlist);
1202 ht = rtnl_dereference(ht->next)) {
1203 if (ht->prio != tp->prio)
1206 if (!tc_cls_stats_dump(tp, arg, ht))
1209 for (h = 0; h <= ht->divisor; h++) {
1210 for (n = rtnl_dereference(ht->ht[h]);
1212 n = rtnl_dereference(n->next)) {
1213 if (!tc_cls_stats_dump(tp, arg, n))
1220 static int u32_reoffload_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,
1221 bool add, flow_setup_cb_t *cb, void *cb_priv,
1222 struct netlink_ext_ack *extack)
1224 struct tc_cls_u32_offload cls_u32 = {};
1227 tc_cls_common_offload_init(&cls_u32.common, tp, ht->flags, extack);
1228 cls_u32.command = add ? TC_CLSU32_NEW_HNODE : TC_CLSU32_DELETE_HNODE;
1229 cls_u32.hnode.divisor = ht->divisor;
1230 cls_u32.hnode.handle = ht->handle;
1231 cls_u32.hnode.prio = ht->prio;
1233 err = cb(TC_SETUP_CLSU32, &cls_u32, cb_priv);
1234 if (err && add && tc_skip_sw(ht->flags))
1240 static int u32_reoffload_knode(struct tcf_proto *tp, struct tc_u_knode *n,
1241 bool add, flow_setup_cb_t *cb, void *cb_priv,
1242 struct netlink_ext_ack *extack)
1244 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
1245 struct tcf_block *block = tp->chain->block;
1246 struct tc_cls_u32_offload cls_u32 = {};
1248 tc_cls_common_offload_init(&cls_u32.common, tp, n->flags, extack);
1249 cls_u32.command = add ?
1250 TC_CLSU32_REPLACE_KNODE : TC_CLSU32_DELETE_KNODE;
1251 cls_u32.knode.handle = n->handle;
1254 cls_u32.knode.fshift = n->fshift;
1255 #ifdef CONFIG_CLS_U32_MARK
1256 cls_u32.knode.val = n->val;
1257 cls_u32.knode.mask = n->mask;
1259 cls_u32.knode.val = 0;
1260 cls_u32.knode.mask = 0;
1262 cls_u32.knode.sel = &n->sel;
1263 cls_u32.knode.res = &n->res;
1264 cls_u32.knode.exts = &n->exts;
1266 cls_u32.knode.link_handle = ht->handle;
1269 return tc_setup_cb_reoffload(block, tp, add, cb, TC_SETUP_CLSU32,
1270 &cls_u32, cb_priv, &n->flags,
1274 static int u32_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb,
1275 void *cb_priv, struct netlink_ext_ack *extack)
1277 struct tc_u_common *tp_c = tp->data;
1278 struct tc_u_hnode *ht;
1279 struct tc_u_knode *n;
1283 for (ht = rtnl_dereference(tp_c->hlist);
1285 ht = rtnl_dereference(ht->next)) {
1286 if (ht->prio != tp->prio)
1289 /* When adding filters to a new dev, try to offload the
1290 * hashtable first. When removing, do the filters before the
1293 if (add && !tc_skip_hw(ht->flags)) {
1294 err = u32_reoffload_hnode(tp, ht, add, cb, cb_priv,
1300 for (h = 0; h <= ht->divisor; h++) {
1301 for (n = rtnl_dereference(ht->ht[h]);
1303 n = rtnl_dereference(n->next)) {
1304 if (tc_skip_hw(n->flags))
1307 err = u32_reoffload_knode(tp, n, add, cb,
1314 if (!add && !tc_skip_hw(ht->flags))
1315 u32_reoffload_hnode(tp, ht, add, cb, cb_priv, extack);
1321 static void u32_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
1324 struct tc_u_knode *n = fh;
1326 tc_cls_bind_class(classid, cl, q, &n->res, base);
1329 static int u32_dump(struct net *net, struct tcf_proto *tp, void *fh,
1330 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
1332 struct tc_u_knode *n = fh;
1333 struct tc_u_hnode *ht_up, *ht_down;
1334 struct nlattr *nest;
1339 t->tcm_handle = n->handle;
1341 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
1343 goto nla_put_failure;
1345 if (TC_U32_KEY(n->handle) == 0) {
1346 struct tc_u_hnode *ht = fh;
1347 u32 divisor = ht->divisor + 1;
1349 if (nla_put_u32(skb, TCA_U32_DIVISOR, divisor))
1350 goto nla_put_failure;
1352 #ifdef CONFIG_CLS_U32_PERF
1353 struct tc_u32_pcnt *gpf;
1357 if (nla_put(skb, TCA_U32_SEL, struct_size(&n->sel, keys, n->sel.nkeys),
1359 goto nla_put_failure;
1361 ht_up = rtnl_dereference(n->ht_up);
1363 u32 htid = n->handle & 0xFFFFF000;
1364 if (nla_put_u32(skb, TCA_U32_HASH, htid))
1365 goto nla_put_failure;
1367 if (n->res.classid &&
1368 nla_put_u32(skb, TCA_U32_CLASSID, n->res.classid))
1369 goto nla_put_failure;
1371 ht_down = rtnl_dereference(n->ht_down);
1373 nla_put_u32(skb, TCA_U32_LINK, ht_down->handle))
1374 goto nla_put_failure;
1376 if (n->flags && nla_put_u32(skb, TCA_U32_FLAGS, n->flags))
1377 goto nla_put_failure;
1379 #ifdef CONFIG_CLS_U32_MARK
1380 if ((n->val || n->mask)) {
1381 struct tc_u32_mark mark = {.val = n->val,
1386 for_each_possible_cpu(cpum) {
1387 __u32 cnt = *per_cpu_ptr(n->pcpu_success, cpum);
1389 mark.success += cnt;
1392 if (nla_put(skb, TCA_U32_MARK, sizeof(mark), &mark))
1393 goto nla_put_failure;
1397 if (tcf_exts_dump(skb, &n->exts) < 0)
1398 goto nla_put_failure;
1401 struct net_device *dev;
1402 dev = __dev_get_by_index(net, n->ifindex);
1403 if (dev && nla_put_string(skb, TCA_U32_INDEV, dev->name))
1404 goto nla_put_failure;
1406 #ifdef CONFIG_CLS_U32_PERF
1407 gpf = kzalloc(struct_size(gpf, kcnts, n->sel.nkeys), GFP_KERNEL);
1409 goto nla_put_failure;
1411 for_each_possible_cpu(cpu) {
1413 struct tc_u32_pcnt *pf = per_cpu_ptr(n->pf, cpu);
1415 gpf->rcnt += pf->rcnt;
1416 gpf->rhit += pf->rhit;
1417 for (i = 0; i < n->sel.nkeys; i++)
1418 gpf->kcnts[i] += pf->kcnts[i];
1421 if (nla_put_64bit(skb, TCA_U32_PCNT, struct_size(gpf, kcnts, n->sel.nkeys),
1422 gpf, TCA_U32_PAD)) {
1424 goto nla_put_failure;
1430 nla_nest_end(skb, nest);
1432 if (TC_U32_KEY(n->handle))
1433 if (tcf_exts_dump_stats(skb, &n->exts) < 0)
1434 goto nla_put_failure;
1438 nla_nest_cancel(skb, nest);
1442 static struct tcf_proto_ops cls_u32_ops __read_mostly = {
1444 .classify = u32_classify,
1446 .destroy = u32_destroy,
1448 .change = u32_change,
1449 .delete = u32_delete,
1451 .reoffload = u32_reoffload,
1453 .bind_class = u32_bind_class,
1454 .owner = THIS_MODULE,
1457 static int __init init_u32(void)
1461 pr_info("u32 classifier\n");
1462 #ifdef CONFIG_CLS_U32_PERF
1463 pr_info(" Performance counters on\n");
1465 pr_info(" input device check on\n");
1466 #ifdef CONFIG_NET_CLS_ACT
1467 pr_info(" Actions configured\n");
1469 tc_u_common_hash = kvmalloc_array(U32_HASH_SIZE,
1470 sizeof(struct hlist_head),
1472 if (!tc_u_common_hash)
1475 for (i = 0; i < U32_HASH_SIZE; i++)
1476 INIT_HLIST_HEAD(&tc_u_common_hash[i]);
1478 ret = register_tcf_proto_ops(&cls_u32_ops);
1480 kvfree(tc_u_common_hash);
1484 static void __exit exit_u32(void)
1486 unregister_tcf_proto_ops(&cls_u32_ops);
1487 kvfree(tc_u_common_hash);
1490 module_init(init_u32)
1491 module_exit(exit_u32)
1492 MODULE_LICENSE("GPL");