Skip to contentSkip to conversation

net: introduce skb_postpush_rcsum() helper

PATCH In review Alexei Starovoitov 3 participants 6 messages Apr 10, 2015 – Apr 13, 2015

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH v4 1/2 net: introduce skb_postpush_rcsum() helper

similar to skb_postpull_rcsum() introduce skb_postpush_rcsum() helper

---

next patch is also using skb_postpush_rcsum()

 include/linux/skbuff.h |   13 +++++++++++++
 net/core/filter.c      |    4 ++--
 2 files changed, 15 insertions(+), 2 deletions(-)
include/linux/skbuff.h +13 -0
index 0991259643d6..049c2db872e0 100644
@@ -2592,6 +2592,19 @@ static inline void skb_postpull_rcsum(struct sk_buff *skb,
 		skb->csum = csum_sub(skb->csum, csum_partial(start, len, 0));
 }
 
+/**
+ *	skb_postpush_rcsum - update checksum for skb after push
+ *	@skb: buffer to update
+ *	@start: start of data after push
+ *	@len: length of data pushed
+ */
+static inline void skb_postpush_rcsum(struct sk_buff *skb,
+				      const void *start, unsigned int len)
+{
+	if (skb->ip_summed == CHECKSUM_COMPLETE)
+		skb->csum = csum_add(skb->csum, csum_partial(start, len, 0));
+}
+
 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len);
 
 /**
net/core/filter.c +2 -2
index b669e75d2b36..e5872704c28b 100644
@@ -1213,8 +1213,8 @@ static u64 bpf_skb_store_bytes(u64 r1, u64 r2, u64 r3, u64 r4, u64 flags)
 		/* skb_store_bits cannot return -EFAULT here */
 		skb_store_bits(skb, offset, ptr, len);
 
-	if (BPF_RECOMPUTE_CSUM(flags) && skb->ip_summed == CHECKSUM_COMPLETE)
-		skb->csum = csum_add(skb->csum, csum_partial(ptr, len, 0));
+	if (BPF_RECOMPUTE_CSUM(flags))
+		skb_postpush_rcsum(skb, ptr, len);
 	return 0;
 }
 
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH v4 2/2 tc: add 'needs_l2' flag to ingress qdisc

TC classifers and actions attached to ingress and egress qdiscs see inconsistent skb->data. For ingress L2 header is already pulled, whereas for egress it's present. Introduce an optional flag for ingress qdisc which if set will cause ingress to push L2 header before calling into classifiers/actions and pull L2 back afterwards.

The cls_bpf/act_bpf are now marked as 'needs_l2'. The users can use them on ingress qdisc created with 'needs_l2' flag and on any egress qdisc. The use of them with vanilla ingress is disallowed.

The ingress_l2 qdisc can only be attached to devices that provide headers_ops.

When ingress is not enabled static_key avoids *(skb->dev->ingress_queue)

When ingress is enabled the difference old vs new to reach qdisc spinlock:
old:
*(skb->dev->ingress_queue), if, *(rxq->qdisc), if, *(rxq->qdisc), if new:
*(skb->dev->ingress_queue), if, *(rxq->qdisc), if, if

This patch provides a foundation to use ingress_l2+cls_bpf to filter interesting traffic and mirror small part of it to a different netdev for capturing. This approach is significantly faster than traditional af_packet, since skb_clone is called after filtering. dhclient and other tap-based tools may consider switching to this style.

---

This patch is on top of Daniel's static_key patch:
http://patchwork.ozlabs.org/patch/460228/

V3->V4:
. drop skb_share_check() for ingress_l2, since it's called before taps . drop 'needs_l2' marks in other cls/acts (only cls_bpf/act_bpf are marked) . use static_key
. add dev->header_ops check to avoid attaching ingress_l2 to devices that
don't support pushing of L2

 include/net/act_api.h          |    9 ++++---
 include/net/sch_generic.h      |    3 +++
 include/uapi/linux/pkt_sched.h |   10 +++++++
 net/core/dev.c                 |   57 +++++++++++++++++++++++++++++-----------
 net/sched/act_api.c            |   21 ++++++++++-----
 net/sched/act_bpf.c            |    1 +
 net/sched/cls_api.c            |   20 ++++++++++++--
 net/sched/cls_bpf.c            |    1 +
 net/sched/sch_api.c            |    5 ++++
 net/sched/sch_ingress.c        |   21 +++++++++++++++
 samples/bpf/tcbpf1_kern.c      |    6 -----
 11 files changed, 121 insertions(+), 33 deletions(-)
include/net/act_api.h +5 -4
index 3ee4c92afd1b..c52bd98b7dd9 100644
@@ -95,6 +95,8 @@ struct tc_action_ops {
 			struct nlattr *est, struct tc_action *act, int ovr,
 			int bind);
 	int     (*walk)(struct sk_buff *, struct netlink_callback *, int, struct tc_action *);
+	u32	flags;
+#define TCA_NEEDS_L2	1
 };
 
 int tcf_hash_search(struct tc_action *a, u32 index);
@@ -112,12 +114,11 @@ int tcf_unregister_action(struct tc_action_ops *a);
 int tcf_action_destroy(struct list_head *actions, int bind);
 int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
 		    struct tcf_result *res);
-int tcf_action_init(struct net *net, struct nlattr *nla,
-				  struct nlattr *est, char *n, int ovr,
-				  int bind, struct list_head *);
+int tcf_action_init(struct net *net, struct nlattr *nla, struct nlattr *est,
+		    char *n, int ovr, int bind, struct list_head *, bool);
 struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
 				    struct nlattr *est, char *n, int ovr,
-				    int bind);
+				    int bind, bool qdisc_has_l2);
 int tcf_action_dump(struct sk_buff *skb, struct list_head *, int, int);
 int tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int, int);
 int tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int, int);
include/net/sch_generic.h +3 -0
index 6d778efcfdfd..e755cc50a037 100644
@@ -61,6 +61,7 @@ struct Qdisc {
 				      */
 #define TCQ_F_WARN_NONWC	(1 << 16)
 #define TCQ_F_CPUSTATS		0x20 /* run using percpu statistics */
+#define TCQ_F_EARLY_INGRESS_L2	0x40 /* ingress qdisc with L2 header */
 	u32			limit;
 	const struct Qdisc_ops	*ops;
 	struct qdisc_size_table	__rcu *stab;
@@ -228,6 +229,8 @@ struct tcf_proto_ops {
 					struct sk_buff *skb, struct tcmsg*);
 
 	struct module		*owner;
+	u32			flags;
+#define TCF_NEEDS_L2	1
 };
 
 struct tcf_proto {
include/uapi/linux/pkt_sched.h +10 -0
index 534b84710745..1076c56e6458 100644
@@ -845,4 +845,14 @@ struct tc_pie_xstats {
 	__u32 maxq;             /* maximum queue size */
 	__u32 ecn_mark;         /* packets marked with ecn*/
 };
+
+/* INGRESS section */
+
+enum {
+	TCA_INGRESS_UNSPEC,
+	TCA_INGRESS_NEEDS_L2,
+	__TCA_INGRESS_MAX,
+};
+
+#define TCA_INGRESS_MAX (__TCA_INGRESS_MAX - 1)
 #endif
net/core/dev.c +42 -15

110 lines not on this page · raw message

net/sched/act_api.c +15 -6
index 3d43e4979f27..0fe93a60c59b 100644
@@ -484,7 +484,7 @@ errout:
 
 struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
 				    struct nlattr *est, char *name, int ovr,
-				    int bind)
+				    int bind, bool qdisc_has_l2)
 {
 	struct tc_action *a;
 	struct tc_action_ops *a_o;
@@ -533,6 +533,14 @@ struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
 		goto err_out;
 	}
 
+	if ((a_o->flags & TCA_NEEDS_L2) && !qdisc_has_l2) {
+		/* actions that require L2 cannot be attached
+		 * to vanilla ingress qdisc
+		 */
+		err = -EINVAL;
+		goto err_mod;
+	}
+
 	err = -ENOMEM;
 	a = kzalloc(sizeof(*a), GFP_KERNEL);
 	if (a == NULL)
@@ -565,9 +573,9 @@ err_out:
 	return ERR_PTR(err);
 }
 
-int tcf_action_init(struct net *net, struct nlattr *nla,
-				  struct nlattr *est, char *name, int ovr,
-				  int bind, struct list_head *actions)
+int tcf_action_init(struct net *net, struct nlattr *nla, struct nlattr *est,
+		    char *name, int ovr, int bind, struct list_head *actions,
+		    bool qdisc_has_l2)
 {
 	struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
 	struct tc_action *act;
@@ -579,7 +587,8 @@ int tcf_action_init(struct net *net, struct nlattr *nla,
 		return err;
 
 	for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
-		act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
+		act = tcf_action_init_1(net, tb[i], est, name, ovr, bind,
+					qdisc_has_l2);
 		if (IS_ERR(act)) {
 			err = PTR_ERR(act);
 			goto err;
@@ -931,7 +940,7 @@ tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
 	int ret = 0;
 	LIST_HEAD(actions);
 
-	ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
+	ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions, true);
 	if (ret)
 		goto done;
 
net/sched/act_bpf.c +1 -0
index 4d2cede17468..8f89d97dfa4a 100644
@@ -339,6 +339,7 @@ static struct tc_action_ops act_bpf_ops __read_mostly = {
 	.dump		=	tcf_bpf_dump,
 	.cleanup	=	tcf_bpf_cleanup,
 	.init		=	tcf_bpf_init,
+	.flags		=	TCA_NEEDS_L2,
 };
 
 static int __init bpf_init_module(void)
net/sched/cls_api.c +18 -2
index 8b0470e418dc..3bbb033af864 100644
@@ -111,6 +111,11 @@ static inline u32 tcf_auto_prio(struct tcf_proto *tp)
 	return first;
 }
 
+static bool qdisc_has_l2(const struct Qdisc *q)
+{
+	return !(q->flags & TCQ_F_INGRESS) || (q->flags & TCQ_F_EARLY_INGRESS_L2);
+}
+
 /* Add/change/delete/get a filter node */
 
 static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n)
@@ -257,6 +262,15 @@ replay:
 			kfree(tp);
 			goto errout;
 		}
+		if ((tp_ops->flags & TCF_NEEDS_L2) && !qdisc_has_l2(q)) {
+			/* classifiers that need L2 header cannot be
+			 * attached to vanilla ingress qdisc
+			 */
+			err = -EINVAL;
+			module_put(tp_ops->owner);
+			kfree(tp);
+			goto errout;
+		}
 		tp->ops = tp_ops;
 		tp->protocol = protocol;
 		tp->prio = nprio ? :
@@ -522,7 +536,8 @@ int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
 		if (exts->police && tb[exts->police]) {
 			act = tcf_action_init_1(net, tb[exts->police], rate_tlv,
 						"police", ovr,
-						TCA_ACT_BIND);
+						TCA_ACT_BIND,
+						qdisc_has_l2(tp->q));
 			if (IS_ERR(act))
 				return PTR_ERR(act);
 
@@ -532,7 +547,8 @@ int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
 			int err;
 			err = tcf_action_init(net, tb[exts->action], rate_tlv,
 					      NULL, ovr,
-					      TCA_ACT_BIND, &exts->actions);
+					      TCA_ACT_BIND, &exts->actions,
+					      qdisc_has_l2(tp->q));
 			if (err)
 				return err;
 		}
net/sched/cls_bpf.c +1 -0
index 5c4171c5d2bd..b99e677e2800 100644
@@ -476,6 +476,7 @@ static struct tcf_proto_ops cls_bpf_ops __read_mostly = {
 	.delete		=	cls_bpf_delete,
 	.walk		=	cls_bpf_walk,
 	.dump		=	cls_bpf_dump,
+	.flags		=	TCF_NEEDS_L2,
 };
 
 static int __init cls_bpf_init_mod(void)
net/sched/sch_api.c +5 -0
index ad9eed70bc8f..521499bf610a 100644
@@ -987,6 +987,11 @@ qdisc_create(struct net_device *dev, struct netdev_queue *dev_queue,
 				goto err_out4;
 		}
 
+		if ((sch->flags & TCQ_F_EARLY_INGRESS_L2) && !dev->header_ops) {
+			err = -EINVAL;
+			goto err_out4;
+		}
+
 		qdisc_list_add(sch);
 
 		return sch;
net/sched/sch_ingress.c +21 -0
index 4cdbfb85686a..6bdbb6cc3f49 100644
@@ -88,8 +88,29 @@ static int ingress_enqueue(struct sk_buff *skb, struct Qdisc *sch)
 
 /* ------------------------------------------------------------- */
 
+static const struct nla_policy ingress_policy[TCA_INGRESS_MAX + 1] = {
+	[TCA_INGRESS_NEEDS_L2]	= { .type = NLA_U32 },
+};
+
 static int ingress_init(struct Qdisc *sch, struct nlattr *opt)
 {
+	struct nlattr *tb[TCA_INGRESS_MAX + 1];
+	int err;
+
+	if (!opt)
+		goto out;
+
+	err = nla_parse_nested(tb, TCA_INGRESS_MAX, opt, ingress_policy);
+	if (err < 0)
+		return err;
+
+	if (tb[TCA_INGRESS_NEEDS_L2]) {
+		if (nla_get_u32(tb[TCA_INGRESS_NEEDS_L2]))
+			sch->flags |= TCQ_F_EARLY_INGRESS_L2;
+		else
+			sch->flags &= ~TCQ_F_EARLY_INGRESS_L2;
+	}
+out:
 	net_inc_ingress_queue();
 
 	return 0;
samples/bpf/tcbpf1_kern.c +0 -6
index 7cf3f42a6e39..76cdaab63058 100644
@@ -14,12 +14,6 @@ static inline void set_dst_mac(struct __sk_buff *skb, char *mac)
 	bpf_skb_store_bytes(skb, 0, mac, ETH_ALEN, 1);
 }
 
-/* use 1 below for ingress qdisc and 0 for egress */
-#if 0
-#undef ETH_HLEN
-#define ETH_HLEN 0
-#endif
-
 #define IP_CSUM_OFF (ETH_HLEN + offsetof(struct iphdr, check))
 #define TOS_OFF (ETH_HLEN + offsetof(struct iphdr, tos))
 
Signature · 1 line
1.7.9.5
maintainer Acked-by on v4 2/2
#2 lore raw

Re: [PATCH v4 net-next 2/2] tc: add 'needs_l2' flag to ingress qdisc

In reply to Alexei Starovoitov
Quoted 20 lines · Alexei Starovoitov

TC classifers and actions attached to ingress and egress qdiscs see inconsistent skb->data. For ingress L2 header is already pulled, whereas for egress it's present. Introduce an optional flag for ingress qdisc which if set will cause ingress to push L2 header before calling into classifiers/actions and pull L2 back afterwards.

The cls_bpf/act_bpf are now marked as 'needs_l2'. The users can use them on ingress qdisc created with 'needs_l2' flag and on any egress qdisc. The use of them with vanilla ingress is disallowed.

The ingress_l2 qdisc can only be attached to devices that provide headers_ops.

When ingress is not enabled static_key avoids *(skb->dev->ingress_queue)

When ingress is enabled the difference old vs new to reach qdisc spinlock:
old:
*(skb->dev->ingress_queue), if, *(rxq->qdisc), if, *(rxq->qdisc), if new:
*(skb->dev->ingress_queue), if, *(rxq->qdisc), if, if

Quoted 7 lines

This patch provides a foundation to use ingress_l2+cls_bpf to filter interesting traffic and mirror small part of it to a different netdev for capturing. This approach is significantly faster than traditional af_packet, since skb_clone is called after filtering. dhclient and other tap-based tools may consider switching to this style.

Yes, that's the suggested alternative for the constraints we're having. Looks good to me, thanks!

maintainer
#3 lore raw

Re: [PATCH v4 net-next 2/2] tc: add 'needs_l2' flag to ingress qdisc

In reply to Alexei Starovoitov
Quoted 20 lines · Alexei Starovoitov

TC classifers and actions attached to ingress and egress qdiscs see inconsistent skb->data. For ingress L2 header is already pulled, whereas for egress it's present. Introduce an optional flag for ingress qdisc which if set will cause ingress to push L2 header before calling into classifiers/actions and pull L2 back afterwards.

The cls_bpf/act_bpf are now marked as 'needs_l2'. The users can use them on ingress qdisc created with 'needs_l2' flag and on any egress qdisc. The use of them with vanilla ingress is disallowed.

The ingress_l2 qdisc can only be attached to devices that provide headers_ops.

When ingress is not enabled static_key avoids *(skb->dev->ingress_queue)

When ingress is enabled the difference old vs new to reach qdisc spinlock:
old:
*(skb->dev->ingress_queue), if, *(rxq->qdisc), if, *(rxq->qdisc), if new:
*(skb->dev->ingress_queue), if, *(rxq->qdisc), if, if

Quoted 6 lines

This patch provides a foundation to use ingress_l2+cls_bpf to filter interesting traffic and mirror small part of it to a different netdev for capturing. This approach is significantly faster than traditional af_packet, since skb_clone is called after filtering. dhclient and other tap-based tools may consider switching to this style.

Alexei,
I want to support this work but i am having difficulties. I see your point as i hope you see mine. In my opinion, it is a stalemate. We need Dave to make the call.

To repeat what i said earlier:
The only known user at this point is bpf. cls_bpf and cls_act could both look at the AT field, find where they are being invoked from and react accordingly. This is not very hard for a coder to do and the user injecting the policy doesnt need to know about it.
If you do that then i think you need to also inform users downstream from bpf that they should expect to see the packet at the Link header and not the network header.

cheers,
jamal

PS:- note that __netif_receive_skb_core() at the beginning is what sets all these headers.

#4 lore raw

Re: [PATCH v4 net-next 2/2] tc: add 'needs_l2' flag to ingress qdisc

Quoted 6 lines · Jamal Hadi Salim

To repeat what i said earlier:
The only known user at this point is bpf. cls_bpf and cls_act could both look at the AT field, find where they are being invoked from and react accordingly. This is not very hard for a coder to do and the user injecting the policy doesnt need to know about it.

It is hard. That's my experience coding in bpf clearly tells me. Burdening users will have long term consequences.
If Dave doesn't like ingress_l2 approach, my only choice left is to abandon ld_abs/ld_ind instructions for tc+bpf programs (since ld_abs must use skb->data) and introduce new helpers that always use skb->mac_header. That is the ugly choice and the deeper you look the uglier it becomes. Like I cannot reject programs with ld_abs in verifier, since ld_abs with skf_ll_off negative offset is still usable, but slower than new helpers, but regular ld_abs shouldn't be used and user will be finding bugs the hard way. I cannot recode ld_abs offsets on the fly either, since the same program can be attached to both ingress and egress. I cannot pass l3-l2 delta into the program, since negative offsets in ld_abs have different meaning, so ld_abs -14 cannot work. I cannot push/pull/skb_share_check when program is already running. Etc, etc.
ingress_l2 cleanly solves it and it also makes ingress and egress _consistent_ for all existing and future classifiers and actions. I don't see it as being bpf specific. imo it should have been this way from the beginning. That's why I tried to do it unconditionally in v2 patch, but ingress was this way for too long, so we have to add a flag.

Quoted 3 lines

If you do that then i think you need to also inform users downstream from bpf that they should expect to see the packet at the Link header and not the network header.

I've analyzed all classifiers and actions. Few assume starting at l2 and I marked them as such in v3. In this v4 patch I dropped the marking, since my reading of Dave's recommendation was to fix them to work with/without l2. That's my plan. So nothing will be affected by this ingress_l2. imo it's a good generic feature. I can see future non-bpf actions that would also want to use TCA_NEEDS_L2 to avoid dealing with ingress/egress differences.