History

net: sched: cls_u32 add bit to specify software only rules

In the initial implementation the only way to stop a rule from being inserted into the hardware table was via the device feature flag. However this doesn't work well when working on an end host system where packets are expect to hit both the hardware and software datapaths. For example we can imagine a rule that will match an IP address and increment a field. If we install this rule in both hardware and software we may increment the field twice. To date we have only added support for the drop action so we have been able to ignore these cases. But as we extend the action support we will hit this example plus more such cases. Arguably these are not even corner cases in many working systems these cases will be common. To avoid forcing the driver to always abort (i.e. the above example) this patch adds a flag to add a rule in software only. A careful user can use this flag to build software and hardware datapaths that work together. One example we have found particularly useful is to use hardware resources to set the skb->mark on the skb when the match may be expensive to run in software but a mark lookup in a hash table is cheap. The idea here is hardware can do in one lookup what the u32 classifier may need to traverse multiple lists and hash tables to compute. The flag is only passed down on inserts. On deletion to avoid stale references in hardware we always try to remove a rule if it exists. The flags field is part of the classifier specific options. Although it is tempting to lift this into the generic structure doing this proves difficult do to how the tc netlink attributes are implemented along with how the dump/change routines are called. There is also precedence for putting seemingly generic pieces in the specific classifier options such as TCA_U32_POLICE, TCA_U32_ACT, etc. So although not ideal I've left FLAGS in the u32 options as well as it simplifies the code greatly and user space has already learned how to manage these bits ala 'tc' tool. Another thing if trying to update a rule we require the flags to be unchanged. This is to force user space, software u32 and the hardware u32 to keep in sync. Thanks to Simon Horman for catching this case.

John Fastabend committed 10 years ago

Commit 9e8ce791 parent 2b6ab0d3 files · +39 −12Browse files

Unified · Split

3 files
  1. include/net/pkt_cls.h +11 −2
  2. include/uapi/linux/pkt_cls.h +1 −0
  3. net/sched/cls_u32.c +27 −10
include/net/pkt_cls.h+11−2
@@ -392,12 +392,21 @@ struct tc_cls_u32_offload { · 391 unmodified lines
392 };392 };
393};393};
394 394
395static inline bool tc_should_offload(struct net_device *dev)395/* tca flags definitions */
396#define TCA_CLS_FLAGS_SKIP_HW 1
397
398static inline bool tc_should_offload(struct net_device *dev, u32 flags)
396{399{
397 if (!(dev->features & NETIF_F_HW_TC))400 if (!(dev->features & NETIF_F_HW_TC))
398 return false;401 return false;
399 402
400 return dev->netdev_ops->ndo_setup_tc;403 if (flags & TCA_CLS_FLAGS_SKIP_HW)
404 return false;
405
406 if (!dev->netdev_ops->ndo_setup_tc)
407 return false;
408
409 return true;
401}410}
402 411
403#endif412#endif
include/uapi/linux/pkt_cls.h+1−0
@@ -172,6 +172,7 @@ enum { · 171 unmodified lines
172 TCA_U32_INDEV,172 TCA_U32_INDEV,
173 TCA_U32_PCNT,173 TCA_U32_PCNT,
174 TCA_U32_MARK,174 TCA_U32_MARK,
175 TCA_U32_FLAGS,
175 __TCA_U32_MAX176 __TCA_U32_MAX
176};177};
177 178
net/sched/cls_u32.c+27−10
@@ -59,6 +59,7 @@ struct tc_u_knode { · 58 unmodified lines
59#ifdef CONFIG_CLS_U32_PERF59#ifdef CONFIG_CLS_U32_PERF
60 struct tc_u32_pcnt __percpu *pf;60 struct tc_u32_pcnt __percpu *pf;
61#endif61#endif
62 u32 flags;
62#ifdef CONFIG_CLS_U32_MARK63#ifdef CONFIG_CLS_U32_MARK
63 u32 val;64 u32 val;
64 u32 mask;65 u32 mask;
@@ -434,7 +435,7 @@ static void u32_remove_hw_knode(struct tcf_proto *tp, u32 handle) · 369 unmodified lines
434 offload.type = TC_SETUP_CLSU32;435 offload.type = TC_SETUP_CLSU32;
435 offload.cls_u32 = &u32_offload;436 offload.cls_u32 = &u32_offload;
436 437
437 if (tc_should_offload(dev)) {438 if (tc_should_offload(dev, 0)) {
438 offload.cls_u32->command = TC_CLSU32_DELETE_KNODE;439 offload.cls_u32->command = TC_CLSU32_DELETE_KNODE;
439 offload.cls_u32->knode.handle = handle;440 offload.cls_u32->knode.handle = handle;
440 dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle,441 dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle,
@@ -442,7 +443,9 @@ static void u32_remove_hw_knode(struct tcf_proto *tp, u32 handle) · 1 unmodified line
442 }443 }
443}444}
444 445
445static void u32_replace_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h)446static void u32_replace_hw_hnode(struct tcf_proto *tp,
447 struct tc_u_hnode *h,
448 u32 flags)
446{449{
447 struct net_device *dev = tp->q->dev_queue->dev;450 struct net_device *dev = tp->q->dev_queue->dev;
448 struct tc_cls_u32_offload u32_offload = {0};451 struct tc_cls_u32_offload u32_offload = {0};
@@ -451,7 +454,7 @@ static void u32_replace_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h) · 2 unmodified lines
451 offload.type = TC_SETUP_CLSU32;454 offload.type = TC_SETUP_CLSU32;
452 offload.cls_u32 = &u32_offload;455 offload.cls_u32 = &u32_offload;
453 456
454 if (tc_should_offload(dev)) {457 if (tc_should_offload(dev, flags)) {
455 offload.cls_u32->command = TC_CLSU32_NEW_HNODE;458 offload.cls_u32->command = TC_CLSU32_NEW_HNODE;
456 offload.cls_u32->hnode.divisor = h->divisor;459 offload.cls_u32->hnode.divisor = h->divisor;
457 offload.cls_u32->hnode.handle = h->handle;460 offload.cls_u32->hnode.handle = h->handle;
@@ -471,7 +474,7 @@ static void u32_clear_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h) · 13 unmodified lines
471 offload.type = TC_SETUP_CLSU32;474 offload.type = TC_SETUP_CLSU32;
472 offload.cls_u32 = &u32_offload;475 offload.cls_u32 = &u32_offload;
473 476
474 if (tc_should_offload(dev)) {477 if (tc_should_offload(dev, 0)) {
475 offload.cls_u32->command = TC_CLSU32_DELETE_HNODE;478 offload.cls_u32->command = TC_CLSU32_DELETE_HNODE;
476 offload.cls_u32->hnode.divisor = h->divisor;479 offload.cls_u32->hnode.divisor = h->divisor;
477 offload.cls_u32->hnode.handle = h->handle;480 offload.cls_u32->hnode.handle = h->handle;
@@ -482,7 +485,9 @@ static void u32_clear_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h) · 4 unmodified lines
482 }485 }
483}486}
484 487
485static void u32_replace_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n)488static void u32_replace_hw_knode(struct tcf_proto *tp,
489 struct tc_u_knode *n,
490 u32 flags)
486{491{
487 struct net_device *dev = tp->q->dev_queue->dev;492 struct net_device *dev = tp->q->dev_queue->dev;
488 struct tc_cls_u32_offload u32_offload = {0};493 struct tc_cls_u32_offload u32_offload = {0};
@@ -491,7 +496,7 @@ static void u32_replace_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n) · 2 unmodified lines
491 offload.type = TC_SETUP_CLSU32;496 offload.type = TC_SETUP_CLSU32;
492 offload.cls_u32 = &u32_offload;497 offload.cls_u32 = &u32_offload;
493 498
494 if (tc_should_offload(dev)) {499 if (tc_should_offload(dev, flags)) {
495 offload.cls_u32->command = TC_CLSU32_REPLACE_KNODE;500 offload.cls_u32->command = TC_CLSU32_REPLACE_KNODE;
496 offload.cls_u32->knode.handle = n->handle;501 offload.cls_u32->knode.handle = n->handle;
497 offload.cls_u32->knode.fshift = n->fshift;502 offload.cls_u32->knode.fshift = n->fshift;
@@ -679,6 +684,7 @@ static const struct nla_policy u32_policy[TCA_U32_MAX + 1] = { · 181 unmodified lines
679 [TCA_U32_SEL] = { .len = sizeof(struct tc_u32_sel) },684 [TCA_U32_SEL] = { .len = sizeof(struct tc_u32_sel) },
680 [TCA_U32_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ },685 [TCA_U32_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ },
681 [TCA_U32_MARK] = { .len = sizeof(struct tc_u32_mark) },686 [TCA_U32_MARK] = { .len = sizeof(struct tc_u32_mark) },
687 [TCA_U32_FLAGS] = { .type = NLA_U32 },
682};688};
683 689
684static int u32_set_parms(struct net *net, struct tcf_proto *tp,690static int u32_set_parms(struct net *net, struct tcf_proto *tp,
@@ -786,6 +792,7 @@ static struct tc_u_knode *u32_init_knode(struct tcf_proto *tp, · 101 unmodified lines
786#endif792#endif
787 new->fshift = n->fshift;793 new->fshift = n->fshift;
788 new->res = n->res;794 new->res = n->res;
795 new->flags = n->flags;
789 RCU_INIT_POINTER(new->ht_down, n->ht_down);796 RCU_INIT_POINTER(new->ht_down, n->ht_down);
790 797
791 /* bump reference count as long as we hold pointer to structure */798 /* bump reference count as long as we hold pointer to structure */
@@ -825,7 +832,7 @@ static int u32_change(struct net *net, struct sk_buff *in_skb, · 33 unmodified lines
825 struct tc_u32_sel *s;832 struct tc_u32_sel *s;
826 struct nlattr *opt = tca[TCA_OPTIONS];833 struct nlattr *opt = tca[TCA_OPTIONS];
827 struct nlattr *tb[TCA_U32_MAX + 1];834 struct nlattr *tb[TCA_U32_MAX + 1];
828 u32 htid;835 u32 htid, flags = 0;
829 int err;836 int err;
830#ifdef CONFIG_CLS_U32_PERF837#ifdef CONFIG_CLS_U32_PERF
831 size_t size;838 size_t size;
@@ -838,6 +845,9 @@ static int u32_change(struct net *net, struct sk_buff *in_skb, · 6 unmodified lines
838 if (err < 0)845 if (err < 0)
839 return err;846 return err;
840 847
848 if (tb[TCA_U32_FLAGS])
849 flags = nla_get_u32(tb[TCA_U32_FLAGS]);
850
841 n = (struct tc_u_knode *)*arg;851 n = (struct tc_u_knode *)*arg;
842 if (n) {852 if (n) {
843 struct tc_u_knode *new;853 struct tc_u_knode *new;
@@ -845,6 +855,9 @@ static int u32_change(struct net *net, struct sk_buff *in_skb, · 1 unmodified line
845 if (TC_U32_KEY(n->handle) == 0)855 if (TC_U32_KEY(n->handle) == 0)
846 return -EINVAL;856 return -EINVAL;
847 857
858 if (n->flags != flags)
859 return -EINVAL;
860
848 new = u32_init_knode(tp, n);861 new = u32_init_knode(tp, n);
849 if (!new)862 if (!new)
850 return -ENOMEM;863 return -ENOMEM;
@@ -861,7 +874,7 @@ static int u32_change(struct net *net, struct sk_buff *in_skb, · 10 unmodified lines
861 u32_replace_knode(tp, tp_c, new);874 u32_replace_knode(tp, tp_c, new);
862 tcf_unbind_filter(tp, &n->res);875 tcf_unbind_filter(tp, &n->res);
863 call_rcu(&n->rcu, u32_delete_key_rcu);876 call_rcu(&n->rcu, u32_delete_key_rcu);
864 u32_replace_hw_knode(tp, new);877 u32_replace_hw_knode(tp, new, flags);
865 return 0;878 return 0;
866 }879 }
867 880
@@ -889,7 +902,7 @@ static int u32_change(struct net *net, struct sk_buff *in_skb, · 21 unmodified lines
889 rcu_assign_pointer(tp_c->hlist, ht);902 rcu_assign_pointer(tp_c->hlist, ht);
890 *arg = (unsigned long)ht;903 *arg = (unsigned long)ht;
891 904
892 u32_replace_hw_hnode(tp, ht);905 u32_replace_hw_hnode(tp, ht, flags);
893 return 0;906 return 0;
894 }907 }
895 908
@@ -940,6 +953,7 @@ static int u32_change(struct net *net, struct sk_buff *in_skb, · 44 unmodified lines
940 RCU_INIT_POINTER(n->ht_up, ht);953 RCU_INIT_POINTER(n->ht_up, ht);
941 n->handle = handle;954 n->handle = handle;
942 n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0;955 n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0;
956 n->flags = flags;
943 tcf_exts_init(&n->exts, TCA_U32_ACT, TCA_U32_POLICE);957 tcf_exts_init(&n->exts, TCA_U32_ACT, TCA_U32_POLICE);
944 n->tp = tp;958 n->tp = tp;
945 959
@@ -972,7 +986,7 @@ static int u32_change(struct net *net, struct sk_buff *in_skb, · 26 unmodified lines
972 986
973 RCU_INIT_POINTER(n->next, pins);987 RCU_INIT_POINTER(n->next, pins);
974 rcu_assign_pointer(*ins, n);988 rcu_assign_pointer(*ins, n);
975 u32_replace_hw_knode(tp, n);989 u32_replace_hw_knode(tp, n, flags);
976 *arg = (unsigned long)n;990 *arg = (unsigned long)n;
977 return 0;991 return 0;
978 }992 }
@@ -1077,6 +1091,9 @@ static int u32_dump(struct net *net, struct tcf_proto *tp, unsigned long fh, · 98 unmodified lines
1077 nla_put_u32(skb, TCA_U32_LINK, ht_down->handle))1091 nla_put_u32(skb, TCA_U32_LINK, ht_down->handle))
1078 goto nla_put_failure;1092 goto nla_put_failure;
1079 1093
1094 if (n->flags && nla_put_u32(skb, TCA_U32_FLAGS, n->flags))
1095 goto nla_put_failure;
1096
1080#ifdef CONFIG_CLS_U32_MARK1097#ifdef CONFIG_CLS_U32_MARK
1081 if ((n->val || n->mask)) {1098 if ((n->val || n->mask)) {
1082 struct tc_u32_mark mark = {.val = n->val,1099 struct tc_u32_mark mark = {.val = n->val,

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t