Skip to contentSkip to conversation

tc: add BPF based action

PATCH Superseded Jiri Pirko 3 participants 6 messages Jan 14, 2015

Subsystems (5)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Jiri Pirko

author maintainer
#0 lore raw

PATCH v3 1/2 tc: add BPF based action

This action provides a possibility to exec custom BPF code.

---
v2->v3:
- s/bpf_len/bpf_num_ops/ per DaveM's suggestion
v1->v2:
- fixed error path in _init
- added cleanup function to kill filter prog

---
 include/net/tc_act/tc_bpf.h        |  25 +++++
 include/uapi/linux/tc_act/Kbuild   |   1 +
 include/uapi/linux/tc_act/tc_bpf.h |  31 ++++++
 net/sched/Kconfig                  |  11 ++
 net/sched/Makefile                 |   1 +
 net/sched/act_bpf.c                | 206 +++++++++++++++++++++++++++++++++++++
 6 files changed, 275 insertions(+)
 create mode 100644 include/net/tc_act/tc_bpf.h
 create mode 100644 include/uapi/linux/tc_act/tc_bpf.h
 create mode 100644 net/sched/act_bpf.c
include/net/tc_act/tc_bpf.h new file +25 -0
new file mode 100644
index 0000000..86a070f
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2015 Jiri Pirko <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#ifndef __NET_TC_BPF_H
+#define __NET_TC_BPF_H
+
+#include <linux/filter.h>
+#include <net/act_api.h>
+
+struct tcf_bpf {
+	struct tcf_common	common;
+	struct bpf_prog		*filter;
+	struct sock_filter	*bpf_ops;
+	u16			bpf_num_ops;
+};
+#define to_bpf(a) \
+	container_of(a->priv, struct tcf_bpf, common)
+
+#endif /* __NET_TC_BPF_H */
include/uapi/linux/tc_act/Kbuild +1 -0
index b057da2..19d5219 100644
@@ -8,3 +8,4 @@ header-y += tc_nat.h
 header-y += tc_pedit.h
 header-y += tc_skbedit.h
 header-y += tc_vlan.h
+header-y += tc_bpf.h
include/uapi/linux/tc_act/tc_bpf.h new file +31 -0
new file mode 100644
index 0000000..5288bd77
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2015 Jiri Pirko <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#ifndef __LINUX_TC_BPF_H
+#define __LINUX_TC_BPF_H
+
+#include <linux/pkt_cls.h>
+
+#define TCA_ACT_BPF 13
+
+struct tc_act_bpf {
+	tc_gen;
+};
+
+enum {
+	TCA_ACT_BPF_UNSPEC,
+	TCA_ACT_BPF_TM,
+	TCA_ACT_BPF_PARMS,
+	TCA_ACT_BPF_OPS_LEN,
+	TCA_ACT_BPF_OPS,
+	__TCA_ACT_BPF_MAX,
+};
+#define TCA_ACT_BPF_MAX (__TCA_ACT_BPF_MAX - 1)
+
+#endif
net/sched/Kconfig +11 -0
index c54c9d9..cc311e9 100644
@@ -698,6 +698,17 @@ config NET_ACT_VLAN
 	  To compile this code as a module, choose M here: the
 	  module will be called act_vlan.
 
+config NET_ACT_BPF
+        tristate "BPF based action"
+        depends on NET_CLS_ACT
+        ---help---
+	  Say Y here to execute BFP code on packets.
+
+	  If unsure, say N.
+
+	  To compile this code as a module, choose M here: the
+	  module will be called act_bpf.
+
 config NET_CLS_IND
 	bool "Incoming device classification"
 	depends on NET_CLS_U32 || NET_CLS_FW
net/sched/Makefile +1 -0
index 679f24a..7ca2b4e 100644
@@ -17,6 +17,7 @@ obj-$(CONFIG_NET_ACT_SIMP)	+= act_simple.o
 obj-$(CONFIG_NET_ACT_SKBEDIT)	+= act_skbedit.o
 obj-$(CONFIG_NET_ACT_CSUM)	+= act_csum.o
 obj-$(CONFIG_NET_ACT_VLAN)	+= act_vlan.o
+obj-$(CONFIG_NET_ACT_BPF)	+= act_bpf.o
 obj-$(CONFIG_NET_SCH_FIFO)	+= sch_fifo.o
 obj-$(CONFIG_NET_SCH_CBQ)	+= sch_cbq.o
 obj-$(CONFIG_NET_SCH_HTB)	+= sch_htb.o
net/sched/act_bpf.c new file +206 -0

206 lines not on this page · raw message

Signature · 1 line
1.9.3
author maintainer
#1 lore raw

PATCH 2/2 tc: cls_bpf: rename bpf_len to bpf_num_ops

It was suggested by DaveM to change the name as "len" might indicate unit bytes.

---
 net/sched/cls_bpf.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
net/sched/cls_bpf.c +9 -9
index 84c8219..1029923 100644
@@ -37,7 +37,7 @@ struct cls_bpf_prog {
 	struct tcf_result res;
 	struct list_head link;
 	u32 handle;
-	u16 bpf_len;
+	u16 bpf_num_ops;
 	struct tcf_proto *tp;
 	struct rcu_head rcu;
 };
@@ -160,7 +160,7 @@ static int cls_bpf_modify_existing(struct net *net, struct tcf_proto *tp,
 	struct tcf_exts exts;
 	struct sock_fprog_kern tmp;
 	struct bpf_prog *fp;
-	u16 bpf_size, bpf_len;
+	u16 bpf_size, bpf_num_ops;
 	u32 classid;
 	int ret;
 
@@ -173,13 +173,13 @@ static int cls_bpf_modify_existing(struct net *net, struct tcf_proto *tp,
 		return ret;
 
 	classid = nla_get_u32(tb[TCA_BPF_CLASSID]);
-	bpf_len = nla_get_u16(tb[TCA_BPF_OPS_LEN]);
-	if (bpf_len > BPF_MAXINSNS || bpf_len == 0) {
+	bpf_num_ops = nla_get_u16(tb[TCA_BPF_OPS_LEN]);
+	if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0) {
 		ret = -EINVAL;
 		goto errout;
 	}
 
-	bpf_size = bpf_len * sizeof(*bpf_ops);
+	bpf_size = bpf_num_ops * sizeof(*bpf_ops);
 	bpf_ops = kzalloc(bpf_size, GFP_KERNEL);
 	if (bpf_ops == NULL) {
 		ret = -ENOMEM;
@@ -188,14 +188,14 @@ static int cls_bpf_modify_existing(struct net *net, struct tcf_proto *tp,
 
 	memcpy(bpf_ops, nla_data(tb[TCA_BPF_OPS]), bpf_size);
 
-	tmp.len = bpf_len;
+	tmp.len = bpf_num_ops;
 	tmp.filter = bpf_ops;
 
 	ret = bpf_prog_create(&fp, &tmp);
 	if (ret)
 		goto errout_free;
 
-	prog->bpf_len = bpf_len;
+	prog->bpf_num_ops = bpf_num_ops;
 	prog->bpf_ops = bpf_ops;
 	prog->filter = fp;
 	prog->res.classid = classid;
@@ -303,10 +303,10 @@ static int cls_bpf_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
 
 	if (nla_put_u32(skb, TCA_BPF_CLASSID, prog->res.classid))
 		goto nla_put_failure;
-	if (nla_put_u16(skb, TCA_BPF_OPS_LEN, prog->bpf_len))
+	if (nla_put_u16(skb, TCA_BPF_OPS_LEN, prog->bpf_num_ops))
 		goto nla_put_failure;
 
-	nla = nla_reserve(skb, TCA_BPF_OPS, prog->bpf_len *
+	nla = nla_reserve(skb, TCA_BPF_OPS, prog->bpf_num_ops *
 			  sizeof(struct sock_filter));
 	if (nla == NULL)
 		goto nla_put_failure;
Signature · 1 line
1.9.3
#3 lore raw
In reply to Jiri Pirko
Quoted 3 lines · Jiri Pirko

This action provides a possibility to exec custom BPF code.

...

net/sched/Kconfig config NET_ACT_VLAN · line 705

Quoted 13 lines
net/sched/Kconfig +5 -0
index c54c9d9..cc311e9 100644
@@ -698,6 +698,17 @@ config NET_ACT_VLAN
  	  To compile this code as a module, choose M here: the
  	  module will be called act_vlan.
 
+config NET_ACT_BPF
+        tristate "BPF based action"
+        depends on NET_CLS_ACT
+        ---help---
+	  Say Y here to execute BFP code on packets.
                                ^^^
                               (typo)

Technically correct, but I'd be a bit more precise. When we add eBPF support one day, this description should be extended to better explain what it can do, for now it would be good to mention that it can filter + drop packets.

Quoted 5 lines
net/sched/Kconfig +5 -0
+	  If unsure, say N.
+
+	  To compile this code as a module, choose M here: the
+	  module will be called act_bpf.
+

...

Quoted 6 lines
net/sched/act_bpf.c new file +0 -0
new file mode 100644
index 0000000..0e2a912
@@ -0,0 +1,206 @@

...

Quoted 8 lines
net/sched/act_bpf.c new file +8 -0
+static int tcf_bpf(struct sk_buff *skb, const struct tc_action *a,
+		   struct tcf_result *res)
+{
+	struct tcf_bpf *b = a->priv;
+	int action;
+	int filter_res;
+
+	spin_lock(&b->tcf_lock);

net/sched/act_bpf.c line 41

Quoted 10 lines
net/sched/act_bpf.c new file +10 -0
+	b->tcf_tm.lastuse = jiffies;
+	bstats_update(&b->tcf_bstats, skb);
+	action = b->tcf_action;
+
+	filter_res = BPF_PROG_RUN(b->filter, skb);
+	if (filter_res == -1)
+		goto drop;
+
+	goto unlock;
+

Why this double goto stuff? Wouldn't it be easier to just write it as:

	filter_res = BPF_PROG_RUN(b->filter, skb);
	if (filter_res == -1) {
		/* #-1 return code from the BPF program in act_bpf
		 * is being interpreted as a drop.
		 */
		action = TC_ACT_SHOT;
		b->tcf_qstats.drops++;
	}

	spin_unlock(&b->tcf_lock);
	return action;

I'm still wondering about the drop semantics ... wouldn't it be more intuitive to use 0 for drops in this context?

net/sched/act_bpf.c line 48

Quoted 7 lines
net/sched/act_bpf.c new file +7 -0
+drop:
+	action = TC_ACT_SHOT;
+	b->tcf_qstats.drops++;
+unlock:
+	spin_unlock(&b->tcf_lock);
+	return action;
+}

...

Thanks,
Daniel

#4 lore raw
Quoted 3 lines · Daniel Borkmann

I'm still wondering about the drop semantics ... wouldn't it be more intuitive to use 0 for drops in this context?

good point.
I think it must be 0 to match behavior of socket filters, etc. If program tries to access beyond packet size or does divide by zero if will be terminated and will return 0.
So zero should be the safest action from caller point of view.

author maintainer
#5 lore raw
Quoted 10 lines · Wed, Jan 14, 2015 at 04:39:34PM CET, [email protected] wrote:
Quoted 3 lines · Daniel Borkmann

I'm still wondering about the drop semantics ... wouldn't it be more intuitive to use 0 for drops in this context?

good point.
I think it must be 0 to match behavior of socket filters, etc. If program tries to access beyond packet size or does divide by zero if will be terminated and will return 0.
So zero should be the safest action from caller point of view.

Will do. Thanks!