Skip to contentSkip to conversation

bpf: program testing framework

PATCH Superseded Alexei Starovoitov 2 participants 18 messages Mar 31, 2017

Subsystems (9)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover bpf: program testing framework

Development and testing of networking bpf programs is quite cumbersome. Especially tricky are XDP programs that attach to real netdevices and program development feels like working on the car engine while the car is in motion.
Another problem is ongoing changes to upstream llvm core that can introduce an optimization that verifier will not recognize. llvm bpf backend tests have no ability to run the programs. To improve this situation introduce BPF_PROG_TEST_RUN command to test and performance benchmark bpf programs.
It achieves several goals:
- development of xdp and skb based bpf programs can be done in a canned environment with unit tests
- program performance optimizations can be benchmarked outside of networking core (without driver and skb costs)
- continuous testing of upstream changes is finally practical

Patches 3,5,6 add C based test cases of various complexity to cover some sched_cls and xdp features. More tests will be added in the future. The tests were run on centos7 only.

For now the framework supports only skb and xdp programs. In the future it can be extended to socket_filter and tracing program types.

More details are in individual patches.

Alexei Starovoitov (6):
bpf: introduce BPF_PROG_TEST_RUN command tools/lib/bpf: add support for BPF_PROG_TEST_RUN command selftests/bpf: add a test for overlapping packet range checks tools/lib/bpf: expose bpf_program__set_type() selftests/bpf: add a test for basic XDP functionality selftests/bpf: add l4 load balancer test based on sched_cls

 include/linux/bpf.h                                |   7 +
 include/uapi/linux/bpf.h                           |  12 +
 kernel/bpf/syscall.c                               |  27 +-
 net/Makefile                                       |   2 +-
 net/bpf/Makefile                                   |   1 +
 net/bpf/test_run.c                                 | 172 ++++++++
 net/core/filter.c                                  |   5 +
 tools/include/uapi/linux/bpf.h                     |  24 ++
 tools/lib/bpf/bpf.c                                |  24 ++
 tools/lib/bpf/bpf.h                                |   4 +-
 tools/lib/bpf/libbpf.c                             |   3 +-
 tools/lib/bpf/libbpf.h                             |   1 +
 tools/testing/selftests/bpf/Makefile               |  18 +-
 tools/testing/selftests/bpf/test_iptunnel_common.h |  37 ++
 tools/testing/selftests/bpf/test_l4lb.c            | 474 +++++++++++++++++++++
 tools/testing/selftests/bpf/test_pkt_access.c      |  64 +++
 tools/testing/selftests/bpf/test_progs.c           | 284 ++++++++++++
 tools/testing/selftests/bpf/test_xdp.c             | 236 ++++++++++
 18 files changed, 1385 insertions(+), 10 deletions(-)
 create mode 100644 net/bpf/Makefile
 create mode 100644 net/bpf/test_run.c
 create mode 100644 tools/testing/selftests/bpf/test_iptunnel_common.h
 create mode 100644 tools/testing/selftests/bpf/test_l4lb.c
 create mode 100644 tools/testing/selftests/bpf/test_pkt_access.c
 create mode 100644 tools/testing/selftests/bpf/test_progs.c
 create mode 100644 tools/testing/selftests/bpf/test_xdp.c
Signature · 1 line
2.9.3
#7 lore raw

PATCH 2/6 tools/lib/bpf: add support for BPF_PROG_TEST_RUN command

add support for BPF_PROG_TEST_RUN command to libbpf.a

---
 tools/include/uapi/linux/bpf.h | 24 ++++++++++++++++++++++++
 tools/lib/bpf/bpf.c            | 24 ++++++++++++++++++++++++
 tools/lib/bpf/bpf.h            |  4 +++-
 3 files changed, 51 insertions(+), 1 deletion(-)
tools/include/uapi/linux/bpf.h +24 -0
index 1ea08ce35567..a1d95386f562 100644
@@ -81,6 +81,7 @@ enum bpf_cmd {
 	BPF_OBJ_GET,
 	BPF_PROG_ATTACH,
 	BPF_PROG_DETACH,
+	BPF_PROG_TEST_RUN,
 };
 
 enum bpf_map_type {
@@ -189,6 +190,17 @@ union bpf_attr {
 		__u32		attach_type;
 		__u32		attach_flags;
 	};
+
+	struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */
+		__u32		prog_fd;
+		__u32		retval;
+		__u32		data_size_in;
+		__u32		data_size_out;
+		__aligned_u64	data_in;
+		__aligned_u64	data_out;
+		__u32		repeat;
+		__u32		duration;
+	} test;
 } __attribute__((aligned(8)));
 
 /* BPF helper function descriptions:
@@ -459,6 +471,18 @@ union bpf_attr {
  *     Return:
  *       > 0 length of the string including the trailing NUL on success
  *       < 0 error
+ *
+ * u64 bpf_bpf_get_socket_cookie(skb)
+ *     Get the cookie for the socket stored inside sk_buff.
+ *     @skb: pointer to skb
+ *     Return: 8 Bytes non-decreasing number on success or 0 if the socket
+ *     field is missing inside sk_buff
+ *
+ * u32 bpf_get_socket_uid(skb)
+ *     Get the owner uid of the socket stored inside sk_buff.
+ *     @skb: pointer to skb
+ *     Return: uid of the socket owner on success or 0 if the socket pointer
+ *     inside sk_buff is NULL
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
tools/lib/bpf/bpf.c +24 -0
index 9b58d20e8c93..b5ca5277e30c 100644
@@ -209,3 +209,27 @@ int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
 
 	return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
 }
+
+int bpf_program_test_run(int prog_fd, int repeat, void *data, __u32 size,
+			 void *data_out, __u32 *size_out, __u32 *retval,
+			 __u32 *duration)
+{
+	union bpf_attr attr;
+	int ret;
+
+	bzero(&attr, sizeof(attr));
+	attr.test.prog_fd = prog_fd;
+	attr.test.data_in = ptr_to_u64(data);
+	attr.test.data_out = ptr_to_u64(data_out);
+	attr.test.data_size_in = size;
+	attr.test.repeat = repeat;
+
+	ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
+	if (size_out)
+		*size_out = attr.test.data_size_out;
+	if (retval)
+		*retval = attr.test.retval;
+	if (duration)
+		*duration = attr.test.duration;
+	return ret;
+}
tools/lib/bpf/bpf.h +3 -1
index 93f021932623..adfb320ff21d 100644
@@ -47,6 +47,8 @@ int bpf_obj_get(const char *pathname);
 int bpf_prog_attach(int prog_fd, int attachable_fd, enum bpf_attach_type type,
 		    unsigned int flags);
 int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
-
+int bpf_program_test_run(int prog_fd, int repeat, void *data, __u32 size,
+			 void *data_out, __u32 *size_out, __u32 *retval,
+			 __u32 *duration);
 
 #endif
Signature · 1 line
2.9.3
#10 lore raw

PATCH 3/6 selftests/bpf: add a test for overlapping packet range checks

In reply to Alexei Starovoitov

add simple C test case for llvm and verifier range check fix from commit b1977682a385 ("bpf: improve verifier packet range checks")

---
 tools/testing/selftests/bpf/Makefile          |  18 +++-
 tools/testing/selftests/bpf/test_pkt_access.c |  64 ++++++++++++
 tools/testing/selftests/bpf/test_progs.c      | 138 ++++++++++++++++++++++++++
 3 files changed, 216 insertions(+), 4 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/test_pkt_access.c
 create mode 100644 tools/testing/selftests/bpf/test_progs.c
tools/testing/selftests/bpf/Makefile +14 -4
index 6a1ad58cb66f..3f76ab6250da 100644
@@ -1,16 +1,18 @@
 LIBDIR := ../../../lib
 BPFDIR := $(LIBDIR)/bpf
 
-CFLAGS += -Wall -O2 -I../../../include/uapi -I$(LIBDIR)
-LDLIBS += -lcap
+CFLAGS += -Wall -O2 -I../../../include/uapi -I$(LIBDIR) -I../../../include
+LDLIBS += -lcap -lelf
 
-TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map
+TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs
+
+TEST_GEN_FILES = test_pkt_access.o
 
 TEST_PROGS := test_kmod.sh
 
 include ../lib.mk
 
-BPFOBJ := $(OUTPUT)/bpf.o
+BPFOBJ := $(OUTPUT)/libbpf.a
 
 $(TEST_GEN_PROGS): $(BPFOBJ)
 
@@ -21,3 +23,11 @@ $(TEST_GEN_PROGS): $(BPFOBJ)
 
 $(BPFOBJ): force
 	$(MAKE) -C $(BPFDIR) OUTPUT=$(OUTPUT)/
+
+CLANG ?= clang
+
+%.o: %.c
+	$(CLANG) -I../../../include/uapi  \
+		-I../../../../samples/bpf/ -D__x86_64__ \
+		-Wno-compare-distinct-pointer-types \
+		-O2 -target bpf -c $< -o $@
tools/testing/selftests/bpf/test_pkt_access.c new file +64 -0

64 lines not on this page · raw message

tools/testing/selftests/bpf/test_progs.c new file +138 -0

138 lines not on this page · raw message

Signature · 1 line
2.9.3
#11 lore raw

PATCH 4/6 tools/lib/bpf: expose bpf_program__set_type()

In reply to Alexei Starovoitov

expose bpf_program__set_type() to set program type

---
 tools/lib/bpf/libbpf.c | 3 +--
 tools/lib/bpf/libbpf.h | 1 +
 2 files changed, 2 insertions(+), 2 deletions(-)
tools/lib/bpf/libbpf.c +1 -2
index ac6eb863b2a4..1a2c07eb7795 100644
@@ -1618,8 +1618,7 @@ int bpf_program__nth_fd(struct bpf_program *prog, int n)
 	return fd;
 }
 
-static void bpf_program__set_type(struct bpf_program *prog,
-				  enum bpf_prog_type type)
+void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
 {
 	prog->type = type;
 }
tools/lib/bpf/libbpf.h +1 -0
index b30394f9947a..82adde30b696 100644
@@ -185,6 +185,7 @@ int bpf_program__set_sched_cls(struct bpf_program *prog);
 int bpf_program__set_sched_act(struct bpf_program *prog);
 int bpf_program__set_xdp(struct bpf_program *prog);
 int bpf_program__set_perf_event(struct bpf_program *prog);
+void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type);
 
 bool bpf_program__is_socket_filter(struct bpf_program *prog);
 bool bpf_program__is_tracepoint(struct bpf_program *prog);
Signature · 1 line
2.9.3
#16 lore raw

PATCH 5/6 selftests/bpf: add a test for basic XDP functionality

In reply to Alexei Starovoitov

add C test for xdp_adjust_head(), packet rewrite and map lookups

---
 tools/testing/selftests/bpf/Makefile               |   2 +-
 tools/testing/selftests/bpf/test_iptunnel_common.h |  37 ++++
 tools/testing/selftests/bpf/test_progs.c           |  58 +++++
 tools/testing/selftests/bpf/test_xdp.c             | 236 +++++++++++++++++++++
 4 files changed, 332 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/test_iptunnel_common.h
 create mode 100644 tools/testing/selftests/bpf/test_xdp.c
tools/testing/selftests/bpf/Makefile +1 -1
index 3f76ab6250da..e4acc5b38f43 100644
@@ -6,7 +6,7 @@ LDLIBS += -lcap -lelf
 
 TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs
 
-TEST_GEN_FILES = test_pkt_access.o
+TEST_GEN_FILES = test_pkt_access.o test_xdp.o
 
 TEST_PROGS := test_kmod.sh
 
tools/testing/selftests/bpf/test_iptunnel_common.h new file +37 -0
new file mode 100644
index 000000000000..e4cd252a1b20
@@ -0,0 +1,37 @@
+/* Copyright (c) 2016 Facebook
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public
+ * License as published by the Free Software Foundation.
+ */
+#ifndef _TEST_IPTNL_COMMON_H
+#define _TEST_IPTNL_COMMON_H
+
+#include <linux/types.h>
+
+#define MAX_IPTNL_ENTRIES 256U
+
+struct vip {
+	union {
+		__u32 v6[4];
+		__u32 v4;
+	} daddr;
+	__u16 dport;
+	__u16 family;
+	__u8 protocol;
+};
+
+struct iptnl_info {
+	union {
+		__u32 v6[4];
+		__u32 v4;
+	} saddr;
+	union {
+		__u32 v6[4];
+		__u32 v4;
+	} daddr;
+	__u16 family;
+	__u8 dmac[6];
+};
+
+#endif
tools/testing/selftests/bpf/test_progs.c +58 -0

82 lines not on this page · raw message

tools/testing/selftests/bpf/test_xdp.c new file +236 -0

236 lines not on this page · raw message

Signature · 1 line
2.9.3
#17 lore raw

PATCH 6/6 selftests/bpf: add l4 load balancer test based on sched_cls

In reply to Alexei Starovoitov

this l4lb demo is a comprehensive test case for LLVM codegen and kernel verifier. It's using fully inlined jhash(), complex packet parsing and multiple map lookups of different types to stress llvm and verifier.
The map sizes, map population and test vectors are artificial to exercise different paths through the bpf program.

---
 tools/testing/selftests/bpf/Makefile     |   2 +-
 tools/testing/selftests/bpf/test_l4lb.c  | 474 +++++++++++++++++++++++++++++++
 tools/testing/selftests/bpf/test_progs.c |  88 ++++++
 3 files changed, 563 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/test_l4lb.c
tools/testing/selftests/bpf/Makefile +1 -1
index e4acc5b38f43..207e91c5780b 100644
@@ -6,7 +6,7 @@ LDLIBS += -lcap -lelf
 
 TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs
 
-TEST_GEN_FILES = test_pkt_access.o test_xdp.o
+TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o
 
 TEST_PROGS := test_kmod.sh
 
tools/testing/selftests/bpf/test_l4lb.c new file +474 -0

474 lines not on this page · raw message

tools/testing/selftests/bpf/test_progs.c +88 -0

123 lines not on this page · raw message

Signature · 1 line
2.9.3
#1 lore raw

PATCH 1/6 bpf: introduce BPF_PROG_TEST_RUN command

In reply to Alexei Starovoitov

development and testing of networking bpf programs is quite cumbersome. Despite availability of user space bpf interpreters the kernel is the ultimate authority and execution environment.
Current test frameworks for TC include creation of netns, veth, qdiscs and use of various packet generators just to test functionality of a bpf program. XDP testing is even more complicated, since qemu needs to be started with gro/gso disabled and precise queue configuration, transferring of xdp program from host into guest, attaching to virtio/eth0 and generating traffic from the host while capturing the results from the guest.

Moreover analyzing performance bottlenecks in XDP program is impossible in virtio environment, since cost of running the program is tiny comparing to the overhead of virtio packet processing, so performance testing can only be done on physical nic with another server generating traffic.

Furthermore ongoing changes to user space control plane of production applications cannot be run on the test servers leaving bpf programs stubbed out for testing.

Last but not least, the upstream llvm changes are validated by the bpf backend testsuite which has no ability to test the code generated.

To improve this situation introduce BPF_PROG_TEST_RUN command to test and performance benchmark bpf programs.

Joint work with Daniel Borkmann.

---
 include/linux/bpf.h      |   7 ++
 include/uapi/linux/bpf.h |  12 ++++
 kernel/bpf/syscall.c     |  27 +++++++-
 net/Makefile             |   2 +-
 net/bpf/Makefile         |   1 +
 net/bpf/test_run.c       | 172 +++++++++++++++++++++++++++++++++++++++++++++++
 net/core/filter.c        |   5 ++
 7 files changed, 223 insertions(+), 3 deletions(-)
 create mode 100644 net/bpf/Makefile
 create mode 100644 net/bpf/test_run.c
include/linux/bpf.h +7 -0
index 2ae39a3e9ead..bbb513da5075 100644
@@ -169,6 +169,8 @@ struct bpf_verifier_ops {
 				  const struct bpf_insn *src,
 				  struct bpf_insn *dst,
 				  struct bpf_prog *prog);
+	int (*test_run)(struct bpf_prog *prog, const union bpf_attr *kattr,
+			union bpf_attr __user *uattr);
 };
 
 struct bpf_prog_type_list {
@@ -233,6 +235,11 @@ typedef unsigned long (*bpf_ctx_copy_t)(void *dst, const void *src,
 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
 		     void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy);
 
+int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
+			  union bpf_attr __user *uattr);
+int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
+			  union bpf_attr __user *uattr);
+
 #ifdef CONFIG_BPF_SYSCALL
 DECLARE_PER_CPU(int, bpf_prog_active);
 
include/uapi/linux/bpf.h +12 -0
index 28317a04c34d..a1d95386f562 100644
@@ -81,6 +81,7 @@ enum bpf_cmd {
 	BPF_OBJ_GET,
 	BPF_PROG_ATTACH,
 	BPF_PROG_DETACH,
+	BPF_PROG_TEST_RUN,
 };
 
 enum bpf_map_type {
@@ -189,6 +190,17 @@ union bpf_attr {
 		__u32		attach_type;
 		__u32		attach_flags;
 	};
+
+	struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */
+		__u32		prog_fd;
+		__u32		retval;
+		__u32		data_size_in;
+		__u32		data_size_out;
+		__aligned_u64	data_in;
+		__aligned_u64	data_out;
+		__u32		repeat;
+		__u32		duration;
+	} test;
 } __attribute__((aligned(8)));
 
 /* BPF helper function descriptions:
kernel/bpf/syscall.c +25 -2
index c35ebfe6d84d..ab0cf4c43690 100644
@@ -973,6 +973,28 @@ static int bpf_prog_detach(const union bpf_attr *attr)
 }
 #endif /* CONFIG_CGROUP_BPF */
 
+#define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
+
+static int bpf_prog_test_run(const union bpf_attr *attr,
+			     union bpf_attr __user *uattr)
+{
+	struct bpf_prog *prog;
+	int ret = -ENOTSUPP;
+
+	if (CHECK_ATTR(BPF_PROG_TEST_RUN))
+		return -EINVAL;
+
+	prog = bpf_prog_get(attr->test.prog_fd);
+	if (IS_ERR(prog))
+		return PTR_ERR(prog);
+
+	if (prog->aux->ops->test_run)
+		ret = prog->aux->ops->test_run(prog, attr, uattr);
+
+	bpf_prog_put(prog);
+	return ret;
+}
+
 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
 {
 	union bpf_attr attr = {};
@@ -1039,7 +1061,6 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, siz
 	case BPF_OBJ_GET:
 		err = bpf_obj_get(&attr);
 		break;
-
 #ifdef CONFIG_CGROUP_BPF
 	case BPF_PROG_ATTACH:
 		err = bpf_prog_attach(&attr);
@@ -1048,7 +1069,9 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, siz
 		err = bpf_prog_detach(&attr);
 		break;
 #endif
-
+	case BPF_PROG_TEST_RUN:
+		err = bpf_prog_test_run(&attr, uattr);
+		break;
 	default:
 		err = -EINVAL;
 		break;
net/Makefile +1 -1
index 9b681550e3a3..9086ffbb5085 100644
@@ -12,7 +12,7 @@ obj-$(CONFIG_NET)		+= $(tmp-y)
 
 # LLC has to be linked before the files in net/802/
 obj-$(CONFIG_LLC)		+= llc/
-obj-$(CONFIG_NET)		+= ethernet/ 802/ sched/ netlink/
+obj-$(CONFIG_NET)		+= ethernet/ 802/ sched/ netlink/ bpf/
 obj-$(CONFIG_NETFILTER)		+= netfilter/
 obj-$(CONFIG_INET)		+= ipv4/
 obj-$(CONFIG_XFRM)		+= xfrm/
net/bpf/Makefile new file +1 -0
new file mode 100644
index 000000000000..27b2992a0692
@@ -0,0 +1 @@
+obj-y	:= test_run.o
net/bpf/test_run.c new file +172 -0

172 lines not on this page · raw message

net/core/filter.c +5 -0
index dfb9f61a2fd5..15e9a81ffebe 100644
@@ -3309,24 +3309,28 @@ static const struct bpf_verifier_ops tc_cls_act_ops = {
 	.is_valid_access	= tc_cls_act_is_valid_access,
 	.convert_ctx_access	= tc_cls_act_convert_ctx_access,
 	.gen_prologue		= tc_cls_act_prologue,
+	.test_run		= bpf_prog_test_run_skb,
 };
 
 static const struct bpf_verifier_ops xdp_ops = {
 	.get_func_proto		= xdp_func_proto,
 	.is_valid_access	= xdp_is_valid_access,
 	.convert_ctx_access	= xdp_convert_ctx_access,
+	.test_run		= bpf_prog_test_run_xdp,
 };
 
 static const struct bpf_verifier_ops cg_skb_ops = {
 	.get_func_proto		= cg_skb_func_proto,
 	.is_valid_access	= sk_filter_is_valid_access,
 	.convert_ctx_access	= bpf_convert_ctx_access,
+	.test_run		= bpf_prog_test_run_skb,
 };
 
 static const struct bpf_verifier_ops lwt_inout_ops = {
 	.get_func_proto		= lwt_inout_func_proto,
 	.is_valid_access	= lwt_is_valid_access,
 	.convert_ctx_access	= bpf_convert_ctx_access,
+	.test_run		= bpf_prog_test_run_skb,
 };
 
 static const struct bpf_verifier_ops lwt_xmit_ops = {
@@ -3334,6 +3338,7 @@ static const struct bpf_verifier_ops lwt_xmit_ops = {
 	.is_valid_access	= lwt_is_valid_access,
 	.convert_ctx_access	= bpf_convert_ctx_access,
 	.gen_prologue		= tc_cls_act_prologue,
+	.test_run		= bpf_prog_test_run_skb,
 };
 
 static const struct bpf_verifier_ops cg_sock_ops = {
Signature · 1 line
2.9.3
#12 lore raw

Re: [PATCH net-next 4/6] tools/lib/bpf: expose bpf_program__set_type()

In reply to Alexei Starovoitov
Quoted 29 lines · Alexei Starovoitov

expose bpf_program__set_type() to set program type

---
  tools/lib/bpf/libbpf.c | 3 +--
  tools/lib/bpf/libbpf.h | 1 +
  2 files changed, 2 insertions(+), 2 deletions(-)
tools/lib/bpf/libbpf.c +1 -2
index ac6eb863b2a4..1a2c07eb7795 100644
@@ -1618,8 +1618,7 @@ int bpf_program__nth_fd(struct bpf_program *prog, int n)
  	return fd;
  }
 
-static void bpf_program__set_type(struct bpf_program *prog,
-				  enum bpf_prog_type type)
+void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
  {
  	prog->type = type;
  }
tools/lib/bpf/libbpf.h +0 -0
index b30394f9947a..82adde30b696 100644

tools/lib/bpf/libbpf.h int bpf_program__set_sched_cls(struct bpf_program *prog); · line 189

Quoted 5 lines
tools/lib/bpf/libbpf.h +1 -0
@@ -185,6 +185,7 @@ int bpf_program__set_sched_cls(struct bpf_program *prog);
  int bpf_program__set_sched_act(struct bpf_program *prog);
  int bpf_program__set_xdp(struct bpf_program *prog);
  int bpf_program__set_perf_event(struct bpf_program *prog);
+void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type);
 

This makes libbpf.h depend on uapi/linux/bpf.h (because of enum bpf_prog_type), which is not always available.

What about defining another enum inside libbpf.h?

Thank you.

#13 lore raw

Re: [PATCH net-next 4/6] tools/lib/bpf: expose bpf_program__set_type()

Quoted 8 lines · Wangnan (F)
Quoted 3 lines

+void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type);

This makes libbpf.h depend on uapi/linux/bpf.h (because of enum bpf_prog_type), which is not always available.

What about defining another enum inside libbpf.h?

how about just including bpf.h? or making it 'int' instead of enum?

#14 lore raw

Re: [PATCH net-next 4/6] tools/lib/bpf: expose bpf_program__set_type()

Quoted 5 lines · Alexei Starovoitov
Quoted 4 lines · Wangnan (F)
Quoted 3 lines

+void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type);

Quoted 7 lines
Quoted 4 lines

This makes libbpf.h depend on uapi/linux/bpf.h (because of enum bpf_prog_type), which is not always available.

What about defining another enum inside libbpf.h?

how about just including bpf.h? or making it 'int' instead of enum?

Including either kernel header into libbpf.h makes a lot of trouble, because kernel header and uapi have many other things we don't need and may conflict with existing code.

Making it 'int' looks like a backdoor. We still need macro to define each program type.

Thank you.

#2 lore raw

Re: [PATCH net-next 1/6] bpf: introduce BPF_PROG_TEST_RUN command

In reply to Alexei Starovoitov
Quoted 44 lines · Alexei Starovoitov

development and testing of networking bpf programs is quite cumbersome. Despite availability of user space bpf interpreters the kernel is the ultimate authority and execution environment.
Current test frameworks for TC include creation of netns, veth, qdiscs and use of various packet generators just to test functionality of a bpf program. XDP testing is even more complicated, since qemu needs to be started with gro/gso disabled and precise queue configuration, transferring of xdp program from host into guest, attaching to virtio/eth0 and generating traffic from the host while capturing the results from the guest.

Moreover analyzing performance bottlenecks in XDP program is impossible in virtio environment, since cost of running the program is tiny comparing to the overhead of virtio packet processing, so performance testing can only be done on physical nic with another server generating traffic.

Furthermore ongoing changes to user space control plane of production applications cannot be run on the test servers leaving bpf programs stubbed out for testing.

Last but not least, the upstream llvm changes are validated by the bpf backend testsuite which has no ability to test the code generated.

To improve this situation introduce BPF_PROG_TEST_RUN command to test and performance benchmark bpf programs.

Joint work with Daniel Borkmann.

---
  include/linux/bpf.h      |   7 ++
  include/uapi/linux/bpf.h |  12 ++++
  kernel/bpf/syscall.c     |  27 +++++++-
  net/Makefile             |   2 +-
  net/bpf/Makefile         |   1 +
  net/bpf/test_run.c       | 172 +++++++++++++++++++++++++++++++++++++++++++++++
  net/core/filter.c        |   5 ++
  7 files changed, 223 insertions(+), 3 deletions(-)

create mode 100644 net/bpf/Makefile
create mode 100644 net/bpf/test_run.c

[SNIP]

Quoted 19 lines
net/Makefile +1 -1
index 9b681550e3a3..9086ffbb5085 100644
@@ -12,7 +12,7 @@ obj-$(CONFIG_NET)		+= $(tmp-y)
 
  # LLC has to be linked before the files in net/802/
  obj-$(CONFIG_LLC)		+= llc/
-obj-$(CONFIG_NET)		+= ethernet/ 802/ sched/ netlink/
+obj-$(CONFIG_NET)		+= ethernet/ 802/ sched/ netlink/ bpf/
  obj-$(CONFIG_NETFILTER)		+= netfilter/
  obj-$(CONFIG_INET)		+= ipv4/
  obj-$(CONFIG_XFRM)		+= xfrm/
net/bpf/Makefile new file +0 -0
new file mode 100644
index 000000000000..27b2992a0692

net/bpf/Makefile line 1

Quoted 1 line
net/bpf/Makefile new file +1 -0
@@ -0,0 +1 @@
+obj-y	:= test_run.o

I suggest using a CONFIG option to enable/disable code in test_run.o to reduce attack plane.

Thank you.

#15 lore raw

Re: [PATCH net-next 4/6] tools/lib/bpf: expose bpf_program__set_type()

In reply to Wangnan (F)
Quoted 11 lines · Wangnan (F)
Quoted 8 lines · Alexei Starovoitov
Quoted 7 lines · Wangnan (F)
Quoted 3 lines

+void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type);

This makes libbpf.h depend on uapi/linux/bpf.h (because of enum bpf_prog_type), which is not always available.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

What about defining another enum inside libbpf.h?

how about just including bpf.h? or making it 'int' instead of enum?

Including either kernel header into libbpf.h makes a lot of trouble, because kernel header and uapi have many other things we don't need and may conflict with existing code.

I'm not proposing to include kernel headers. Regular /usr/include/linux/bpf.h is enough. This library isn't going to be compiled on distros that don't have bpf support anyway.

Quoted 2 lines

Making it 'int' looks like a backdoor. We still need macro to define each program type.

macro for each program wasn't the greatest idea. It always behind new program types and not usable for this use case. See patches 5 and 6.

#3 lore raw

Re: [PATCH net-next 1/6] bpf: introduce BPF_PROG_TEST_RUN command

In reply to Wangnan (F)
Quoted 2 lines · Wangnan (F)

I suggest using a CONFIG option to enable/disable code in test_run.o to reduce attack plane.

attack plane? what attack do you see and how config helps?

#4 lore raw

Re: [PATCH net-next 1/6] bpf: introduce BPF_PROG_TEST_RUN command

Quoted 6 lines · Alexei Starovoitov
Quoted 2 lines · Wangnan (F)

I suggest using a CONFIG option to enable/disable code in test_run.o to reduce attack plane.

attack plane? what attack do you see and how config helps?

I think all testing features are not required to be compiled for a production system. A feature which should never be used looks dangerous to me.

I suggest adding a CONFIG option like CONFIG_BPF_PROGRAM_TEST_RUN to control whether the kernel should be compiled with this feature or not. We can enable by default, and give people a chance to turn it off. At least in my company people tends to turn all unneeded features off. If you don't provide a config option they will make one by themselves.

Thank you.

#8 lore raw

Re: [PATCH net-next 2/6] tools/lib/bpf: add support for BPF_PROG_TEST_RUN command

In reply to Alexei Starovoitov
Quoted 97 lines · Alexei Starovoitov

add support for BPF_PROG_TEST_RUN command to libbpf.a

---
  tools/include/uapi/linux/bpf.h | 24 ++++++++++++++++++++++++
  tools/lib/bpf/bpf.c            | 24 ++++++++++++++++++++++++
  tools/lib/bpf/bpf.h            |  4 +++-
  3 files changed, 51 insertions(+), 1 deletion(-)
tools/include/uapi/linux/bpf.h +24 -0
index 1ea08ce35567..a1d95386f562 100644
@@ -81,6 +81,7 @@ enum bpf_cmd {
  	BPF_OBJ_GET,
  	BPF_PROG_ATTACH,
  	BPF_PROG_DETACH,
+	BPF_PROG_TEST_RUN,
  };
 
  enum bpf_map_type {
@@ -189,6 +190,17 @@ union bpf_attr {
  		__u32		attach_type;
  		__u32		attach_flags;
  	};
+
+	struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */
+		__u32		prog_fd;
+		__u32		retval;
+		__u32		data_size_in;
+		__u32		data_size_out;
+		__aligned_u64	data_in;
+		__aligned_u64	data_out;
+		__u32		repeat;
+		__u32		duration;
+	} test;
  } __attribute__((aligned(8)));
 
  /* BPF helper function descriptions:
@@ -459,6 +471,18 @@ union bpf_attr {
   *     Return:
   *       > 0 length of the string including the trailing NUL on success
   *       < 0 error
+ *
+ * u64 bpf_bpf_get_socket_cookie(skb)
+ *     Get the cookie for the socket stored inside sk_buff.
+ *     @skb: pointer to skb
+ *     Return: 8 Bytes non-decreasing number on success or 0 if the socket
+ *     field is missing inside sk_buff
+ *
+ * u32 bpf_get_socket_uid(skb)
+ *     Get the owner uid of the socket stored inside sk_buff.
+ *     @skb: pointer to skb
+ *     Return: uid of the socket owner on success or 0 if the socket pointer
+ *     inside sk_buff is NULL
   */
  #define __BPF_FUNC_MAPPER(FN)		\
  	FN(unspec),			\
tools/lib/bpf/bpf.c +24 -0
index 9b58d20e8c93..b5ca5277e30c 100644
@@ -209,3 +209,27 @@ int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
 
  	return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
  }
+
+int bpf_program_test_run(int prog_fd, int repeat, void *data, __u32 size,
+			 void *data_out, __u32 *size_out, __u32 *retval,
+			 __u32 *duration)
+{
+	union bpf_attr attr;
+	int ret;
+
+	bzero(&attr, sizeof(attr));
+	attr.test.prog_fd = prog_fd;
+	attr.test.data_in = ptr_to_u64(data);
+	attr.test.data_out = ptr_to_u64(data_out);
+	attr.test.data_size_in = size;
+	attr.test.repeat = repeat;
+
+	ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
+	if (size_out)
+		*size_out = attr.test.data_size_out;
+	if (retval)
+		*retval = attr.test.retval;
+	if (duration)
+		*duration = attr.test.duration;
+	return ret;
+}
tools/lib/bpf/bpf.h +0 -0
index 93f021932623..adfb320ff21d 100644

tools/lib/bpf/bpf.h int bpf_obj_get(const char *pathname); · line 53

Quoted 8 lines
tools/lib/bpf/bpf.h +3 -1
@@ -47,6 +47,8 @@ int bpf_obj_get(const char *pathname);
  int bpf_prog_attach(int prog_fd, int attachable_fd, enum bpf_attach_type type,
  		    unsigned int flags);
  int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
-
+int bpf_program_test_run(int prog_fd, int repeat, void *data, __u32 size,
+			 void *data_out, __u32 *size_out, __u32 *retval,
+			 __u32 *duration);
 

Please call it bpf_prog_test_run() so it looks uniform with others.

Thank you.

#5 lore raw

Re: [PATCH net-next 1/6] bpf: introduce BPF_PROG_TEST_RUN command

In reply to Wangnan (F)
Quoted 7 lines · Wangnan (F)
Quoted 4 lines · Alexei Starovoitov
Quoted 2 lines · Wangnan (F)

I suggest using a CONFIG option to enable/disable code in test_run.o to reduce attack plane.

Quoted 6 lines
Quoted 2 lines

attack plane? what attack do you see and how config helps?

I think all testing features are not required to be compiled for a production system. A feature which should never be used looks dangerous to me.

It is required on production system, since xdp testing and xdp production has to use the same kernel. We cannot keep rebooting the server back and forth to test and then to run. It's not testing the kernel features, it's testing bpf programs which are technically user space components.

Quoted 6 lines

I suggest adding a CONFIG option like CONFIG_BPF_PROGRAM_TEST_RUN to control whether the kernel should be compiled with this feature or not. We can enable by default, and give people a chance to turn it off. At least in my company people tends to turn all unneeded features off. If you don't provide a config option they will make one by themselves.

Using this logic huawei should be turning off xdp as well. Sorry we're not going stub xdp facility out of the core and the drivers just because you don't use it.

#9 lore raw

Re: [PATCH net-next 2/6] tools/lib/bpf: add support for BPF_PROG_TEST_RUN command

In reply to Wangnan (F)
Quoted 8 lines · Wangnan (F)

tools/lib/bpf/bpf.h int bpf_obj_get(const char *pathname); · line 52

Quoted 6 lines
(quoted diff) +1 -1
-
+int bpf_program_test_run(int prog_fd, int repeat, void *data, __u32

size,

tools/lib/bpf/bpf.h +2 -0
+             void *data_out, __u32 *size_out, __u32 *retval,
+             __u32 *duration);

Please call it bpf_prog_test_run() so it looks uniform with others.

sure. will do. good catch.

#6 lore raw

Re: [PATCH net-next 1/6] bpf: introduce BPF_PROG_TEST_RUN command

In reply to Alexei Starovoitov
Quoted 15 lines · Alexei Starovoitov
Quoted 7 lines · Wangnan (F)
Quoted 4 lines · Alexei Starovoitov
Quoted 2 lines · Wangnan (F)

I suggest using a CONFIG option to enable/disable code in test_run.o to reduce attack plane.

Quoted 6 lines
Quoted 2 lines

attack plane? what attack do you see and how config helps?

I think all testing features are not required to be compiled for a production system. A feature which should never be used looks dangerous to me.

Quoted 6 lines

It is required on production system, since xdp testing and xdp production has to use the same kernel. We cannot keep rebooting the server back and forth to test and then to run. It's not testing the kernel features, it's testing bpf programs which are technically user space components.

Okay. Now I understand it is a production feature.

Thank you.