hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/net/sched/cls_u32.c
....@@ -1,10 +1,6 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * net/sched/cls_u32.c Ugly (or Universal) 32bit key Packet Classifier.
3
- *
4
- * This program is free software; you can redistribute it and/or
5
- * modify it under the terms of the GNU General Public License
6
- * as published by the Free Software Foundation; either version
7
- * 2 of the License, or (at your option) any later version.
84 *
95 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
106 *
....@@ -23,9 +19,6 @@
2319 * It is especially useful for link sharing combined with QoS;
2420 * pure RSVP doesn't need such a general approach and can use
2521 * much simpler (and faster) schemes, sort of cls_rsvp.c.
26
- *
27
- * JHS: We should remove the CONFIG_NET_CLS_IND from here
28
- * eventually when the meta match extension is made available
2922 *
3023 * nfmark match added by Catalin(ux aka Dino) BOIE <catab at umbrella.ro>
3124 */
....@@ -52,9 +45,7 @@
5245 u32 handle;
5346 struct tc_u_hnode __rcu *ht_up;
5447 struct tcf_exts exts;
55
-#ifdef CONFIG_NET_CLS_IND
5648 int ifindex;
57
-#endif
5849 u8 fshift;
5950 struct tcf_result res;
6051 struct tc_u_hnode __rcu *ht_down;
....@@ -68,7 +59,6 @@
6859 u32 mask;
6960 u32 __percpu *pcpu_success;
7061 #endif
71
- struct tcf_proto *tp;
7262 struct rcu_work rwork;
7363 /* The 'sel' field MUST be the last field in structure to allow for
7464 * tc_u32_keys allocated at end of structure.
....@@ -80,16 +70,16 @@
8070 struct tc_u_hnode __rcu *next;
8171 u32 handle;
8272 u32 prio;
83
- struct tc_u_common *tp_c;
8473 int refcnt;
8574 unsigned int divisor;
8675 struct idr handle_idr;
76
+ bool is_root;
8777 struct rcu_head rcu;
8878 u32 flags;
8979 /* The 'ht' field MUST be the last field in structure to allow for
9080 * more entries allocated at end of structure.
9181 */
92
- struct tc_u_knode __rcu *ht[1];
82
+ struct tc_u_knode __rcu *ht[];
9383 };
9484
9585 struct tc_u_common {
....@@ -98,7 +88,7 @@
9888 int refcnt;
9989 struct idr handle_idr;
10090 struct hlist_node hnode;
101
- struct rcu_head rcu;
91
+ long knodes;
10292 };
10393
10494 static inline unsigned int u32_hash_fold(__be32 key,
....@@ -181,12 +171,10 @@
181171 if (n->sel.flags & TC_U32_TERMINAL) {
182172
183173 *res = n->res;
184
-#ifdef CONFIG_NET_CLS_IND
185174 if (!tcf_match_indev(skb, n->ifindex)) {
186175 n = rcu_dereference_bh(n->next);
187176 goto next_knode;
188177 }
189
-#endif
190178 #ifdef CONFIG_CLS_U32_PERF
191179 __this_cpu_inc(n->pf->rhit);
192180 #endif
....@@ -344,19 +332,16 @@
344332 return block->q;
345333 }
346334
347
-static unsigned int tc_u_hash(const struct tcf_proto *tp)
335
+static struct hlist_head *tc_u_hash(void *key)
348336 {
349
- return hash_ptr(tc_u_common_ptr(tp), U32_HASH_SHIFT);
337
+ return tc_u_common_hash + hash_ptr(key, U32_HASH_SHIFT);
350338 }
351339
352
-static struct tc_u_common *tc_u_common_find(const struct tcf_proto *tp)
340
+static struct tc_u_common *tc_u_common_find(void *key)
353341 {
354342 struct tc_u_common *tc;
355
- unsigned int h;
356
-
357
- h = tc_u_hash(tp);
358
- hlist_for_each_entry(tc, &tc_u_common_hash[h], hnode) {
359
- if (tc->ptr == tc_u_common_ptr(tp))
343
+ hlist_for_each_entry(tc, tc_u_hash(key), hnode) {
344
+ if (tc->ptr == key)
360345 return tc;
361346 }
362347 return NULL;
....@@ -365,38 +350,35 @@
365350 static int u32_init(struct tcf_proto *tp)
366351 {
367352 struct tc_u_hnode *root_ht;
368
- struct tc_u_common *tp_c;
369
- unsigned int h;
353
+ void *key = tc_u_common_ptr(tp);
354
+ struct tc_u_common *tp_c = tc_u_common_find(key);
370355
371
- tp_c = tc_u_common_find(tp);
372
-
373
- root_ht = kzalloc(sizeof(*root_ht), GFP_KERNEL);
356
+ root_ht = kzalloc(struct_size(root_ht, ht, 1), GFP_KERNEL);
374357 if (root_ht == NULL)
375358 return -ENOBUFS;
376359
377360 root_ht->refcnt++;
378361 root_ht->handle = tp_c ? gen_new_htid(tp_c, root_ht) : 0x80000000;
379362 root_ht->prio = tp->prio;
363
+ root_ht->is_root = true;
380364 idr_init(&root_ht->handle_idr);
381365
382366 if (tp_c == NULL) {
383
- tp_c = kzalloc(sizeof(*tp_c), GFP_KERNEL);
367
+ tp_c = kzalloc(struct_size(tp_c, hlist->ht, 1), GFP_KERNEL);
384368 if (tp_c == NULL) {
385369 kfree(root_ht);
386370 return -ENOBUFS;
387371 }
388
- tp_c->ptr = tc_u_common_ptr(tp);
372
+ tp_c->ptr = key;
389373 INIT_HLIST_NODE(&tp_c->hnode);
390374 idr_init(&tp_c->handle_idr);
391375
392
- h = tc_u_hash(tp);
393
- hlist_add_head(&tp_c->hnode, &tc_u_common_hash[h]);
376
+ hlist_add_head(&tp_c->hnode, tc_u_hash(key));
394377 }
395378
396379 tp_c->refcnt++;
397380 RCU_INIT_POINTER(root_ht->next, tp_c->hlist);
398381 rcu_assign_pointer(tp_c->hlist, root_ht);
399
- root_ht->tp_c = tp_c;
400382
401383 root_ht->refcnt++;
402384 rcu_assign_pointer(tp->root, root_ht);
....@@ -404,15 +386,19 @@
404386 return 0;
405387 }
406388
407
-static int u32_destroy_key(struct tcf_proto *tp, struct tc_u_knode *n,
408
- bool free_pf)
389
+static void __u32_destroy_key(struct tc_u_knode *n)
409390 {
410391 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
411392
412393 tcf_exts_destroy(&n->exts);
413
- tcf_exts_put_net(&n->exts);
414394 if (ht && --ht->refcnt == 0)
415395 kfree(ht);
396
+ kfree(n);
397
+}
398
+
399
+static void u32_destroy_key(struct tc_u_knode *n, bool free_pf)
400
+{
401
+ tcf_exts_put_net(&n->exts);
416402 #ifdef CONFIG_CLS_U32_PERF
417403 if (free_pf)
418404 free_percpu(n->pf);
....@@ -421,8 +407,7 @@
421407 if (free_pf)
422408 free_percpu(n->pcpu_success);
423409 #endif
424
- kfree(n);
425
- return 0;
410
+ __u32_destroy_key(n);
426411 }
427412
428413 /* u32_delete_key_rcu should be called when free'ing a copied
....@@ -439,7 +424,7 @@
439424 struct tc_u_knode,
440425 rwork);
441426 rtnl_lock();
442
- u32_destroy_key(key->tp, key, false);
427
+ u32_destroy_key(key, false);
443428 rtnl_unlock();
444429 }
445430
....@@ -456,12 +441,13 @@
456441 struct tc_u_knode,
457442 rwork);
458443 rtnl_lock();
459
- u32_destroy_key(key->tp, key, true);
444
+ u32_destroy_key(key, true);
460445 rtnl_unlock();
461446 }
462447
463448 static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode *key)
464449 {
450
+ struct tc_u_common *tp_c = tp->data;
465451 struct tc_u_knode __rcu **kp;
466452 struct tc_u_knode *pkp;
467453 struct tc_u_hnode *ht = rtnl_dereference(key->ht_up);
....@@ -472,6 +458,7 @@
472458 kp = &pkp->next, pkp = rtnl_dereference(*kp)) {
473459 if (pkp == key) {
474460 RCU_INIT_POINTER(*kp, key->next);
461
+ tp_c->knodes--;
475462
476463 tcf_unbind_filter(tp, &key->res);
477464 idr_remove(&ht->handle_idr, key->handle);
....@@ -497,7 +484,7 @@
497484 cls_u32.hnode.handle = h->handle;
498485 cls_u32.hnode.prio = h->prio;
499486
500
- tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, false);
487
+ tc_setup_cb_call(block, TC_SETUP_CLSU32, &cls_u32, false, true);
501488 }
502489
503490 static int u32_replace_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h,
....@@ -515,7 +502,7 @@
515502 cls_u32.hnode.handle = h->handle;
516503 cls_u32.hnode.prio = h->prio;
517504
518
- err = tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, skip_sw);
505
+ err = tc_setup_cb_call(block, TC_SETUP_CLSU32, &cls_u32, skip_sw, true);
519506 if (err < 0) {
520507 u32_clear_hw_hnode(tp, h, NULL);
521508 return err;
....@@ -539,8 +526,8 @@
539526 cls_u32.command = TC_CLSU32_DELETE_KNODE;
540527 cls_u32.knode.handle = n->handle;
541528
542
- tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, false);
543
- tcf_block_offload_dec(block, &n->flags);
529
+ tc_setup_cb_destroy(block, tp, TC_SETUP_CLSU32, &cls_u32, false,
530
+ &n->flags, &n->in_hw_count, true);
544531 }
545532
546533 static int u32_replace_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n,
....@@ -564,17 +551,16 @@
564551 cls_u32.knode.mask = 0;
565552 #endif
566553 cls_u32.knode.sel = &n->sel;
554
+ cls_u32.knode.res = &n->res;
567555 cls_u32.knode.exts = &n->exts;
568556 if (n->ht_down)
569557 cls_u32.knode.link_handle = ht->handle;
570558
571
- err = tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, skip_sw);
572
- if (err < 0) {
559
+ err = tc_setup_cb_add(block, tp, TC_SETUP_CLSU32, &cls_u32, skip_sw,
560
+ &n->flags, &n->in_hw_count, true);
561
+ if (err) {
573562 u32_remove_hw_knode(tp, n, NULL);
574563 return err;
575
- } else if (err > 0) {
576
- n->in_hw_count = err;
577
- tcf_block_offload_inc(block, &n->flags);
578564 }
579565
580566 if (skip_sw && !(n->flags & TCA_CLS_FLAGS_IN_HW))
....@@ -586,6 +572,7 @@
586572 static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,
587573 struct netlink_ext_ack *extack)
588574 {
575
+ struct tc_u_common *tp_c = tp->data;
589576 struct tc_u_knode *n;
590577 unsigned int h;
591578
....@@ -593,13 +580,14 @@
593580 while ((n = rtnl_dereference(ht->ht[h])) != NULL) {
594581 RCU_INIT_POINTER(ht->ht[h],
595582 rtnl_dereference(n->next));
583
+ tp_c->knodes--;
596584 tcf_unbind_filter(tp, &n->res);
597585 u32_remove_hw_knode(tp, n, extack);
598586 idr_remove(&ht->handle_idr, n->handle);
599587 if (tcf_exts_get_net(&n->exts))
600588 tcf_queue_work(&n->rwork, u32_delete_key_freepf_work);
601589 else
602
- u32_destroy_key(n->tp, n, true);
590
+ u32_destroy_key(n, true);
603591 }
604592 }
605593 }
....@@ -632,18 +620,8 @@
632620 return -ENOENT;
633621 }
634622
635
-static bool ht_empty(struct tc_u_hnode *ht)
636
-{
637
- unsigned int h;
638
-
639
- for (h = 0; h <= ht->divisor; h++)
640
- if (rcu_access_pointer(ht->ht[h]))
641
- return false;
642
-
643
- return true;
644
-}
645
-
646
-static void u32_destroy(struct tcf_proto *tp, struct netlink_ext_ack *extack)
623
+static void u32_destroy(struct tcf_proto *tp, bool rtnl_held,
624
+ struct netlink_ext_ack *extack)
647625 {
648626 struct tc_u_common *tp_c = tp->data;
649627 struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
....@@ -677,15 +655,11 @@
677655 }
678656
679657 static int u32_delete(struct tcf_proto *tp, void *arg, bool *last,
680
- struct netlink_ext_ack *extack)
658
+ bool rtnl_held, struct netlink_ext_ack *extack)
681659 {
682660 struct tc_u_hnode *ht = arg;
683
- struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
684661 struct tc_u_common *tp_c = tp->data;
685662 int ret = 0;
686
-
687
- if (ht == NULL)
688
- goto out;
689663
690664 if (TC_U32_KEY(ht->handle)) {
691665 u32_remove_hw_knode(tp, (struct tc_u_knode *)ht, extack);
....@@ -693,7 +667,7 @@
693667 goto out;
694668 }
695669
696
- if (root_ht == ht) {
670
+ if (ht->is_root) {
697671 NL_SET_ERR_MSG_MOD(extack, "Not allowed to delete root node");
698672 return -EINVAL;
699673 }
....@@ -706,38 +680,7 @@
706680 }
707681
708682 out:
709
- *last = true;
710
- if (root_ht) {
711
- if (root_ht->refcnt > 2) {
712
- *last = false;
713
- goto ret;
714
- }
715
- if (root_ht->refcnt == 2) {
716
- if (!ht_empty(root_ht)) {
717
- *last = false;
718
- goto ret;
719
- }
720
- }
721
- }
722
-
723
- if (tp_c->refcnt > 1) {
724
- *last = false;
725
- goto ret;
726
- }
727
-
728
- if (tp_c->refcnt == 1) {
729
- struct tc_u_hnode *ht;
730
-
731
- for (ht = rtnl_dereference(tp_c->hlist);
732
- ht;
733
- ht = rtnl_dereference(ht->next))
734
- if (!ht_empty(ht)) {
735
- *last = false;
736
- break;
737
- }
738
- }
739
-
740
-ret:
683
+ *last = tp_c->refcnt == 1 && tp_c->knodes == 0;
741684 return ret;
742685 }
743686
....@@ -768,16 +711,22 @@
768711 };
769712
770713 static int u32_set_parms(struct net *net, struct tcf_proto *tp,
771
- unsigned long base, struct tc_u_hnode *ht,
714
+ unsigned long base,
772715 struct tc_u_knode *n, struct nlattr **tb,
773716 struct nlattr *est, bool ovr,
774717 struct netlink_ext_ack *extack)
775718 {
776
- int err;
719
+ int err, ifindex = -1;
777720
778
- err = tcf_exts_validate(net, tp, tb, est, &n->exts, ovr, extack);
721
+ err = tcf_exts_validate(net, tp, tb, est, &n->exts, ovr, true, extack);
779722 if (err < 0)
780723 return err;
724
+
725
+ if (tb[TCA_U32_INDEV]) {
726
+ ifindex = tcf_change_indev(net, tb[TCA_U32_INDEV], extack);
727
+ if (ifindex < 0)
728
+ return -EINVAL;
729
+ }
781730
782731 if (tb[TCA_U32_LINK]) {
783732 u32 handle = nla_get_u32(tb[TCA_U32_LINK]);
....@@ -789,10 +738,14 @@
789738 }
790739
791740 if (handle) {
792
- ht_down = u32_lookup_ht(ht->tp_c, handle);
741
+ ht_down = u32_lookup_ht(tp->data, handle);
793742
794743 if (!ht_down) {
795744 NL_SET_ERR_MSG_MOD(extack, "Link hash table not found");
745
+ return -EINVAL;
746
+ }
747
+ if (ht_down->is_root) {
748
+ NL_SET_ERR_MSG_MOD(extack, "Not linking to root node");
796749 return -EINVAL;
797750 }
798751 ht_down->refcnt++;
....@@ -809,15 +762,9 @@
809762 tcf_bind_filter(tp, &n->res, base);
810763 }
811764
812
-#ifdef CONFIG_NET_CLS_IND
813
- if (tb[TCA_U32_INDEV]) {
814
- int ret;
815
- ret = tcf_change_indev(net, tb[TCA_U32_INDEV], extack);
816
- if (ret < 0)
817
- return -EINVAL;
818
- n->ifindex = ret;
819
- }
820
-#endif
765
+ if (ifindex >= 0)
766
+ n->ifindex = ifindex;
767
+
821768 return 0;
822769 }
823770
....@@ -848,16 +795,14 @@
848795 rcu_assign_pointer(*ins, n);
849796 }
850797
851
-static struct tc_u_knode *u32_init_knode(struct tcf_proto *tp,
798
+static struct tc_u_knode *u32_init_knode(struct net *net, struct tcf_proto *tp,
852799 struct tc_u_knode *n)
853800 {
854801 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
855802 struct tc_u32_sel *s = &n->sel;
856803 struct tc_u_knode *new;
857804
858
- new = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key),
859
- GFP_KERNEL);
860
-
805
+ new = kzalloc(struct_size(new, sel.keys, s->nkeys), GFP_KERNEL);
861806 if (!new)
862807 return NULL;
863808
....@@ -865,17 +810,10 @@
865810 new->handle = n->handle;
866811 RCU_INIT_POINTER(new->ht_up, n->ht_up);
867812
868
-#ifdef CONFIG_NET_CLS_IND
869813 new->ifindex = n->ifindex;
870
-#endif
871814 new->fshift = n->fshift;
872
- new->res = n->res;
873815 new->flags = n->flags;
874816 RCU_INIT_POINTER(new->ht_down, ht);
875
-
876
- /* bump reference count as long as we hold pointer to structure */
877
- if (ht)
878
- ht->refcnt++;
879817
880818 #ifdef CONFIG_CLS_U32_PERF
881819 /* Statistics may be incremented by readers during update
....@@ -891,20 +829,23 @@
891829 /* Similarly success statistics must be moved as pointers */
892830 new->pcpu_success = n->pcpu_success;
893831 #endif
894
- new->tp = tp;
895
- memcpy(&new->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
832
+ memcpy(&new->sel, s, struct_size(s, keys, s->nkeys));
896833
897
- if (tcf_exts_init(&new->exts, TCA_U32_ACT, TCA_U32_POLICE)) {
834
+ if (tcf_exts_init(&new->exts, net, TCA_U32_ACT, TCA_U32_POLICE)) {
898835 kfree(new);
899836 return NULL;
900837 }
838
+
839
+ /* bump reference count as long as we hold pointer to structure */
840
+ if (ht)
841
+ ht->refcnt++;
901842
902843 return new;
903844 }
904845
905846 static int u32_change(struct net *net, struct sk_buff *in_skb,
906847 struct tcf_proto *tp, unsigned long base, u32 handle,
907
- struct nlattr **tca, void **arg, bool ovr,
848
+ struct nlattr **tca, void **arg, bool ovr, bool rtnl_held,
908849 struct netlink_ext_ack *extack)
909850 {
910851 struct tc_u_common *tp_c = tp->data;
....@@ -916,9 +857,6 @@
916857 u32 htid, flags = 0;
917858 size_t sel_size;
918859 int err;
919
-#ifdef CONFIG_CLS_U32_PERF
920
- size_t size;
921
-#endif
922860
923861 if (!opt) {
924862 if (handle) {
....@@ -929,7 +867,8 @@
929867 }
930868 }
931869
932
- err = nla_parse_nested(tb, TCA_U32_MAX, opt, u32_policy, extack);
870
+ err = nla_parse_nested_deprecated(tb, TCA_U32_MAX, opt, u32_policy,
871
+ extack);
933872 if (err < 0)
934873 return err;
935874
....@@ -956,22 +895,21 @@
956895 return -EINVAL;
957896 }
958897
959
- new = u32_init_knode(tp, n);
898
+ new = u32_init_knode(net, tp, n);
960899 if (!new)
961900 return -ENOMEM;
962901
963
- err = u32_set_parms(net, tp, base,
964
- rtnl_dereference(n->ht_up), new, tb,
902
+ err = u32_set_parms(net, tp, base, new, tb,
965903 tca[TCA_RATE], ovr, extack);
966904
967905 if (err) {
968
- u32_destroy_key(tp, new, false);
906
+ __u32_destroy_key(new);
969907 return err;
970908 }
971909
972910 err = u32_replace_hw_knode(tp, new, flags, extack);
973911 if (err) {
974
- u32_destroy_key(tp, new, false);
912
+ __u32_destroy_key(new);
975913 return err;
976914 }
977915
....@@ -988,7 +926,11 @@
988926 if (tb[TCA_U32_DIVISOR]) {
989927 unsigned int divisor = nla_get_u32(tb[TCA_U32_DIVISOR]);
990928
991
- if (--divisor > 0x100) {
929
+ if (!is_power_of_2(divisor)) {
930
+ NL_SET_ERR_MSG_MOD(extack, "Divisor is not a power of 2");
931
+ return -EINVAL;
932
+ }
933
+ if (divisor-- > 0x100) {
992934 NL_SET_ERR_MSG_MOD(extack, "Exceeded maximum 256 hash buckets");
993935 return -EINVAL;
994936 }
....@@ -996,7 +938,7 @@
996938 NL_SET_ERR_MSG_MOD(extack, "Divisor can only be used on a hash table");
997939 return -EINVAL;
998940 }
999
- ht = kzalloc(sizeof(*ht) + divisor*sizeof(void *), GFP_KERNEL);
941
+ ht = kzalloc(struct_size(ht, ht, divisor + 1), GFP_KERNEL);
1000942 if (ht == NULL)
1001943 return -ENOBUFS;
1002944 if (handle == 0) {
....@@ -1013,7 +955,6 @@
1013955 return err;
1014956 }
1015957 }
1016
- ht->tp_c = tp_c;
1017958 ht->refcnt = 1;
1018959 ht->divisor = divisor;
1019960 ht->handle = handle;
....@@ -1057,18 +998,62 @@
1057998 return -EINVAL;
1058999 }
10591000
1001
+ /* At this point, we need to derive the new handle that will be used to
1002
+ * uniquely map the identity of this table match entry. The
1003
+ * identity of the entry that we need to construct is 32 bits made of:
1004
+ * htid(12b):bucketid(8b):node/entryid(12b)
1005
+ *
1006
+ * At this point _we have the table(ht)_ in which we will insert this
1007
+ * entry. We carry the table's id in variable "htid".
1008
+ * Note that earlier code picked the ht selection either by a) the user
1009
+ * providing the htid specified via TCA_U32_HASH attribute or b) when
1010
+ * no such attribute is passed then the root ht, is default to at ID
1011
+ * 0x[800][00][000]. Rule: the root table has a single bucket with ID 0.
1012
+ * If OTOH the user passed us the htid, they may also pass a bucketid of
1013
+ * choice. 0 is fine. For example a user htid is 0x[600][01][000] it is
1014
+ * indicating hash bucketid of 1. Rule: the entry/node ID _cannot_ be
1015
+ * passed via the htid, so even if it was non-zero it will be ignored.
1016
+ *
1017
+ * We may also have a handle, if the user passed one. The handle also
1018
+ * carries the same addressing of htid(12b):bucketid(8b):node/entryid(12b).
1019
+ * Rule: the bucketid on the handle is ignored even if one was passed;
1020
+ * rather the value on "htid" is always assumed to be the bucketid.
1021
+ */
10601022 if (handle) {
1023
+ /* Rule: The htid from handle and tableid from htid must match */
10611024 if (TC_U32_HTID(handle) && TC_U32_HTID(handle ^ htid)) {
10621025 NL_SET_ERR_MSG_MOD(extack, "Handle specified hash table address mismatch");
10631026 return -EINVAL;
10641027 }
1065
- handle = htid | TC_U32_NODE(handle);
1066
- err = idr_alloc_u32(&ht->handle_idr, NULL, &handle, handle,
1067
- GFP_KERNEL);
1068
- if (err)
1069
- return err;
1070
- } else
1028
+ /* Ok, so far we have a valid htid(12b):bucketid(8b) but we
1029
+ * need to finalize the table entry identification with the last
1030
+ * part - the node/entryid(12b)). Rule: Nodeid _cannot be 0_ for
1031
+ * entries. Rule: nodeid of 0 is reserved only for tables(see
1032
+ * earlier code which processes TC_U32_DIVISOR attribute).
1033
+ * Rule: The nodeid can only be derived from the handle (and not
1034
+ * htid).
1035
+ * Rule: if the handle specified zero for the node id example
1036
+ * 0x60000000, then pick a new nodeid from the pool of IDs
1037
+ * this hash table has been allocating from.
1038
+ * If OTOH it is specified (i.e for example the user passed a
1039
+ * handle such as 0x60000123), then we use it generate our final
1040
+ * handle which is used to uniquely identify the match entry.
1041
+ */
1042
+ if (!TC_U32_NODE(handle)) {
1043
+ handle = gen_new_kid(ht, htid);
1044
+ } else {
1045
+ handle = htid | TC_U32_NODE(handle);
1046
+ err = idr_alloc_u32(&ht->handle_idr, NULL, &handle,
1047
+ handle, GFP_KERNEL);
1048
+ if (err)
1049
+ return err;
1050
+ }
1051
+ } else {
1052
+ /* The user did not give us a handle; lets just generate one
1053
+ * from the table's pool of nodeids.
1054
+ */
10711055 handle = gen_new_kid(ht, htid);
1056
+ }
10721057
10731058 if (tb[TCA_U32_SEL] == NULL) {
10741059 NL_SET_ERR_MSG_MOD(extack, "Selector not specified");
....@@ -1083,15 +1068,15 @@
10831068 goto erridr;
10841069 }
10851070
1086
- n = kzalloc(offsetof(typeof(*n), sel) + sel_size, GFP_KERNEL);
1071
+ n = kzalloc(struct_size(n, sel.keys, s->nkeys), GFP_KERNEL);
10871072 if (n == NULL) {
10881073 err = -ENOBUFS;
10891074 goto erridr;
10901075 }
10911076
10921077 #ifdef CONFIG_CLS_U32_PERF
1093
- size = sizeof(struct tc_u32_pcnt) + s->nkeys * sizeof(u64);
1094
- n->pf = __alloc_percpu(size, __alignof__(struct tc_u32_pcnt));
1078
+ n->pf = __alloc_percpu(struct_size(n->pf, kcnts, s->nkeys),
1079
+ __alignof__(struct tc_u32_pcnt));
10951080 if (!n->pf) {
10961081 err = -ENOBUFS;
10971082 goto errfree;
....@@ -1103,9 +1088,8 @@
11031088 n->handle = handle;
11041089 n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0;
11051090 n->flags = flags;
1106
- n->tp = tp;
11071091
1108
- err = tcf_exts_init(&n->exts, TCA_U32_ACT, TCA_U32_POLICE);
1092
+ err = tcf_exts_init(&n->exts, net, TCA_U32_ACT, TCA_U32_POLICE);
11091093 if (err < 0)
11101094 goto errout;
11111095
....@@ -1125,7 +1109,7 @@
11251109 }
11261110 #endif
11271111
1128
- err = u32_set_parms(net, tp, base, ht, n, tb, tca[TCA_RATE], ovr,
1112
+ err = u32_set_parms(net, tp, base, n, tb, tca[TCA_RATE], ovr,
11291113 extack);
11301114 if (err == 0) {
11311115 struct tc_u_knode __rcu **ins;
....@@ -1146,6 +1130,7 @@
11461130
11471131 RCU_INIT_POINTER(n->next, pins);
11481132 rcu_assign_pointer(*ins, n);
1133
+ tp_c->knodes++;
11491134 *arg = n;
11501135 return 0;
11511136 }
....@@ -1167,7 +1152,8 @@
11671152 return err;
11681153 }
11691154
1170
-static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
1155
+static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg,
1156
+ bool rtnl_held)
11711157 {
11721158 struct tc_u_common *tp_c = tp->data;
11731159 struct tc_u_hnode *ht;
....@@ -1208,7 +1194,7 @@
12081194 }
12091195
12101196 static int u32_reoffload_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,
1211
- bool add, tc_setup_cb_t *cb, void *cb_priv,
1197
+ bool add, flow_setup_cb_t *cb, void *cb_priv,
12121198 struct netlink_ext_ack *extack)
12131199 {
12141200 struct tc_cls_u32_offload cls_u32 = {};
....@@ -1228,7 +1214,7 @@
12281214 }
12291215
12301216 static int u32_reoffload_knode(struct tcf_proto *tp, struct tc_u_knode *n,
1231
- bool add, tc_setup_cb_t *cb, void *cb_priv,
1217
+ bool add, flow_setup_cb_t *cb, void *cb_priv,
12321218 struct netlink_ext_ack *extack)
12331219 {
12341220 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
....@@ -1251,24 +1237,22 @@
12511237 cls_u32.knode.mask = 0;
12521238 #endif
12531239 cls_u32.knode.sel = &n->sel;
1240
+ cls_u32.knode.res = &n->res;
12541241 cls_u32.knode.exts = &n->exts;
12551242 if (n->ht_down)
12561243 cls_u32.knode.link_handle = ht->handle;
12571244 }
12581245
1259
- err = cb(TC_SETUP_CLSU32, &cls_u32, cb_priv);
1260
- if (err) {
1261
- if (add && tc_skip_sw(n->flags))
1262
- return err;
1263
- return 0;
1264
- }
1265
-
1266
- tc_cls_offload_cnt_update(block, &n->in_hw_count, &n->flags, add);
1246
+ err = tc_setup_cb_reoffload(block, tp, add, cb, TC_SETUP_CLSU32,
1247
+ &cls_u32, cb_priv, &n->flags,
1248
+ &n->in_hw_count);
1249
+ if (err)
1250
+ return err;
12671251
12681252 return 0;
12691253 }
12701254
1271
-static int u32_reoffload(struct tcf_proto *tp, bool add, tc_setup_cb_t *cb,
1255
+static int u32_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb,
12721256 void *cb_priv, struct netlink_ext_ack *extack)
12731257 {
12741258 struct tc_u_common *tp_c = tp->data;
....@@ -1329,7 +1313,7 @@
13291313 }
13301314
13311315 static int u32_dump(struct net *net, struct tcf_proto *tp, void *fh,
1332
- struct sk_buff *skb, struct tcmsg *t)
1316
+ struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
13331317 {
13341318 struct tc_u_knode *n = fh;
13351319 struct tc_u_hnode *ht_up, *ht_down;
....@@ -1340,7 +1324,7 @@
13401324
13411325 t->tcm_handle = n->handle;
13421326
1343
- nest = nla_nest_start(skb, TCA_OPTIONS);
1327
+ nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
13441328 if (nest == NULL)
13451329 goto nla_put_failure;
13461330
....@@ -1356,8 +1340,7 @@
13561340 int cpu;
13571341 #endif
13581342
1359
- if (nla_put(skb, TCA_U32_SEL,
1360
- sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
1343
+ if (nla_put(skb, TCA_U32_SEL, struct_size(&n->sel, keys, n->sel.nkeys),
13611344 &n->sel))
13621345 goto nla_put_failure;
13631346
....@@ -1400,18 +1383,14 @@
14001383 if (tcf_exts_dump(skb, &n->exts) < 0)
14011384 goto nla_put_failure;
14021385
1403
-#ifdef CONFIG_NET_CLS_IND
14041386 if (n->ifindex) {
14051387 struct net_device *dev;
14061388 dev = __dev_get_by_index(net, n->ifindex);
14071389 if (dev && nla_put_string(skb, TCA_U32_INDEV, dev->name))
14081390 goto nla_put_failure;
14091391 }
1410
-#endif
14111392 #ifdef CONFIG_CLS_U32_PERF
1412
- gpf = kzalloc(sizeof(struct tc_u32_pcnt) +
1413
- n->sel.nkeys * sizeof(u64),
1414
- GFP_KERNEL);
1393
+ gpf = kzalloc(struct_size(gpf, kcnts, n->sel.nkeys), GFP_KERNEL);
14151394 if (!gpf)
14161395 goto nla_put_failure;
14171396
....@@ -1425,9 +1404,7 @@
14251404 gpf->kcnts[i] += pf->kcnts[i];
14261405 }
14271406
1428
- if (nla_put_64bit(skb, TCA_U32_PCNT,
1429
- sizeof(struct tc_u32_pcnt) +
1430
- n->sel.nkeys * sizeof(u64),
1407
+ if (nla_put_64bit(skb, TCA_U32_PCNT, struct_size(gpf, kcnts, n->sel.nkeys),
14311408 gpf, TCA_U32_PAD)) {
14321409 kfree(gpf);
14331410 goto nla_put_failure;
....@@ -1471,9 +1448,7 @@
14711448 #ifdef CONFIG_CLS_U32_PERF
14721449 pr_info(" Performance counters on\n");
14731450 #endif
1474
-#ifdef CONFIG_NET_CLS_IND
14751451 pr_info(" input device check on\n");
1476
-#endif
14771452 #ifdef CONFIG_NET_CLS_ACT
14781453 pr_info(" Actions configured\n");
14791454 #endif