Skip to contentSkip to conversation

split BPF out of core networking

PATCH Superseded Alexei Starovoitov 2 participants 4 messages Jun 1, 2014

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover split BPF out of core networking

This patch set splits BPF out of core networking into generic component

patch #1 splits filter.c into two logical pieces: generic BPF core and socket filters. It only moves functions around. No real changes.

patch #2 adds hidden CONFIG_BPF that seccomp/tracing can select

the split was proposed in the previous thread:
http://article.gmane.org/gmane.linux.network/316050

The main value of the patch is not a NET separation, but rather logical boundary between generic BPF core and socket filtering. All socket specific code stays in net/core/filter.c and net/bpf/core.c is for generic BPF infrastructure (both classic and internal).

Note that CONFIG_BPF_JIT is still under NET, so NET-less configs cannot use BPF JITs yet. This can be cleaned up in the future. Also it seems to makes sense to split up filter.h into generic and socket specific as well to cleanup the boundary further.

The patches are on top of Daniel's cleanup to minimize conflicts:
http://patchwork.ozlabs.org/patch/353700/
http://patchwork.ozlabs.org/patch/353699/

Unfortunately they conflict with Chema's patches which in turn conflict with Daniel's. But since Daniel got in the queue first, I've rebased mine on top of Daniel's.

Tested with several NET and NET-less configs on arm and x86

Alexei Starovoitov (2):
net: filter: split filter.c into two files
net: filter: split BPF out of core networking

 arch/Kconfig           |    3 +-
 include/linux/filter.h |    2 +
 net/Kconfig            |    4 +
 net/Makefile           |    1 +
 net/bpf/Makefile       |    5 +
 net/bpf/core.c         | 1063 ++++++++++++++++++++++++++++++++++++++++++++++++
 net/core/filter.c      | 1023 +---------------------------------------------
 7 files changed, 1079 insertions(+), 1022 deletions(-)
 create mode 100644 net/bpf/Makefile
 create mode 100644 net/bpf/core.c
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH 1/2 net: filter: split filter.c into two files

BPF is used in several kernel components. This split creates logical boundary between generic BPF core and specific BPF use cases

net/bpf/core.c:
internal BPF interpreter, classic to internal converter, classic verifier

net/core/filter.c:
classic BPF extensions related to socket filters, socket attach/detach

This patch only moves functions. No other changes.

Next patch introduces hidden Kconfig flag, so seccomp and tracing filters can select BPF core only instead of depending on the whole NET

---
 include/linux/filter.h |    2 +
 net/Makefile           |    1 +
 net/bpf/Makefile       |    5 +
 net/bpf/core.c         | 1042 ++++++++++++++++++++++++++++++++++++++++++++++++
 net/core/filter.c      | 1023 +----------------------------------------------
 5 files changed, 1052 insertions(+), 1021 deletions(-)
 create mode 100644 net/bpf/Makefile
 create mode 100644 net/bpf/core.c
include/linux/filter.h +2 -0
index f0c2ad43b4af..0e463ee77bb2 100644
@@ -341,6 +341,8 @@ void sk_filter_free(struct sk_filter *fp);
 
 int sk_convert_filter(struct sock_filter *prog, int len,
 		      struct sock_filter_int *new_prog, int *new_len);
+bool sk_convert_bpf_extensions(struct sock_filter *fp,
+			       struct sock_filter_int **insnp);
 
 int sk_unattached_filter_create(struct sk_filter **pfp,
 				struct sock_fprog_kern *fprog);
net/Makefile +1 -0
index cbbbe6d657ca..d0e89323aee3 100644
@@ -8,6 +8,7 @@
 obj-y	:= nonet.o
 
 obj-$(CONFIG_NET)		:= socket.o core/
+obj-$(CONFIG_NET)		+= bpf/
 
 tmp-$(CONFIG_COMPAT) 		:= compat.o
 obj-$(CONFIG_NET)		+= $(tmp-y)
net/bpf/Makefile new file +5 -0
new file mode 100644
index 000000000000..2634b2fe5202
@@ -0,0 +1,5 @@
+#
+# Makefile for the BPF core infrastructure
+#
+
+obj-y := core.o
net/bpf/core.c new file +1042 -0

1,042 lines not on this page · raw message

net/core/filter.c +2 -1021

1,053 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#2 lore raw

PATCH 2/2 net: filter: split BPF out of core networking

In reply to Alexei Starovoitov

seccomp selects BPF only instead of whole NET
Other BPF users (like tracing filters) will select BPF only too

---
 arch/Kconfig   |    3 ++-
 net/Kconfig    |    4 ++++
 net/Makefile   |    2 +-
 net/bpf/core.c |   21 +++++++++++++++++++++
 4 files changed, 28 insertions(+), 2 deletions(-)
arch/Kconfig +2 -1
index 97ff872c7acc..92f43a16eccc 100644
@@ -324,7 +324,8 @@ config HAVE_ARCH_SECCOMP_FILTER
 
 config SECCOMP_FILTER
 	def_bool y
-	depends on HAVE_ARCH_SECCOMP_FILTER && SECCOMP && NET
+	depends on HAVE_ARCH_SECCOMP_FILTER && SECCOMP
+	select BPF
 	help
 	  Enable tasks to build secure computing environments defined
 	  in terms of Berkeley Packet Filter programs which implement
net/Kconfig +4 -0
index d92afe4204d9..e8dca9f836d9 100644
@@ -6,6 +6,7 @@ menuconfig NET
 	bool "Networking support"
 	select NLATTR
 	select GENERIC_NET_UTILS
+	select BPF
 	---help---
 	  Unless you really know what you are doing, you should say Y here.
 	  The reason is that some programs need kernel networking support even
@@ -370,6 +371,9 @@ source "net/nfc/Kconfig"
 
 endif   # if NET
 
+config BPF
+	boolean
+
 # Used by archs to tell that they support BPF_JIT
 config HAVE_BPF_JIT
 	bool
net/Makefile +1 -1
index d0e89323aee3..d56447bca1aa 100644
@@ -8,7 +8,7 @@
 obj-y	:= nonet.o
 
 obj-$(CONFIG_NET)		:= socket.o core/
-obj-$(CONFIG_NET)		+= bpf/
+obj-$(CONFIG_BPF)		+= bpf/
 
 tmp-$(CONFIG_COMPAT) 		:= compat.o
 obj-$(CONFIG_NET)		+= $(tmp-y)
net/bpf/core.c +21 -0
index 22c2d99414c0..8ca1b37ddc28 100644
@@ -1040,3 +1040,24 @@ void sk_filter_free(struct sk_filter *fp)
 	bpf_jit_free(fp);
 }
 EXPORT_SYMBOL_GPL(sk_filter_free);
+
+/* kernel configuration that do not enable NET are not using
+ * classic BPF extensions
+ */
+bool __weak sk_convert_bpf_extensions(struct sock_filter *fp,
+				      struct sock_filter_int **insnp)
+{
+	return false;
+}
+
+/* To emulate LD_ABS/LD_IND instructions __sk_run_filter() may call
+ * skb_copy_bits(), so provide a weak definition for it in NET-less config.
+ * seccomp_check_filter() verifies that seccomp filters are not using
+ * LD_ABS/LD_IND instructions. Other BPF users (like tracing filters)
+ * must not use these instructions unless ctx==skb
+ */
+int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,
+			 int len)
+{
+	return -EFAULT;
+}
Signature · 1 line
1.7.9.5
#3 lore raw
In reply to Alexei Starovoitov
Quoted 5 lines · Alexei Starovoitov

This patch set splits BPF out of core networking into generic component

net: filter: split filter.c into two files
net: filter: split BPF out of core networking

Quoted 10 lines
 arch/Kconfig           |    3 +-
 include/linux/filter.h |    2 +
 net/Kconfig            |    4 +
 net/Makefile           |    1 +
 net/bpf/Makefile       |    5 +
 net/bpf/core.c         | 1063 ++++++++++++++++++++++++++++++++++++++++++++++++
 net/core/filter.c      | 1023 +---------------------------------------------
 7 files changed, 1079 insertions(+), 1022 deletions(-)
 create mode 100644 net/bpf/Makefile
 create mode 100644 net/bpf/core.c

I like the splitup, but please put it into lib/bpf/ or kernel/bpf/ to signal its 'generic', 'core' nature, to be used in many subsystems outside of networking.

Thanks,

Ingo