Skip to contentSkip to conversation

tc: add BPF based action

PATCH Superseded Jiri Pirko 2 participants 3 messages Jan 12, 2015 – Jan 14, 2015

Subsystems (5)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Jiri Pirko

author maintainer
#0 lore raw

PATCH v2 1/1 tc: add BPF based action

This action provides a possibility to exec custom BPF code.

---
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                | 205 +++++++++++++++++++++++++++++++++++++
 6 files changed, 274 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..95e11da
@@ -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_len;
+};
+#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 +205 -0

205 lines not on this page · raw message

Signature · 1 line
1.9.3
maintainer
#1 lore raw

From: Jiri Pirko <[email protected]>
Date: Mon, 12 Jan 2015 17:09:38 +0100

net/sched/act_bpf.c line 124

Quoted 5 lines
net/sched/act_bpf.c new file +5 -0
+	bpf_len = nla_get_u16(tb[TCA_ACT_BPF_OPS_LEN]);
+	if (bpf_len > BPF_MAXINSNS || bpf_len == 0)
+		return -EINVAL;
+
+	bpf_size = bpf_len * sizeof(*bpf_ops);

When I see variables named 'len' and 'size', I expect them to be in unit bytes.

I think it's clearer to call bpf_len something like "bpf_num_insns", or "bpf_num_ops", or something like that.

Also, is the OPS_LEN attribute really necessary? Can't you just figure this out using nla_len() on the OPS attribute? Or is that not always accurate due to alignment?

author maintainer
#2 lore raw
Quoted 11 lines · Tue, Jan 13, 2015 at 08:36:33PM CET, [email protected] wrote:

From: Jiri Pirko <[email protected]>
Date: Mon, 12 Jan 2015 17:09:38 +0100

net/sched/act_bpf.c line 124

Quoted 5 lines
net/sched/act_bpf.c new file +5 -0
+	bpf_len = nla_get_u16(tb[TCA_ACT_BPF_OPS_LEN]);
+	if (bpf_len > BPF_MAXINSNS || bpf_len == 0)
+		return -EINVAL;
+
+	bpf_size = bpf_len * sizeof(*bpf_ops);

When I see variables named 'len' and 'size', I expect them to be in unit bytes.

Size is bytes, len is number of instructions. This is the same as in net/sched/cls_bpf.c line 182. I follow the same naming for consistency

Quoted 3 lines

I think it's clearer to call bpf_len something like "bpf_num_insns", or "bpf_num_ops", or something like that.

I understand. I will change it in net/sched/cls_bpf.c as well.

Quoted 4 lines

Also, is the OPS_LEN attribute really necessary? Can't you just figure this out using nla_len() on the OPS attribute? Or is that not always accurate due to alignment?

I guess that you could just use nla_len. Again, I followed cls_bpf for consistency purposes. It has TCA_BPF_OPS_LEN as well.

Thanks for review!

Jiri