Skip to contentSkip to conversation

support flow dissector in BPF_PROG_TEST_RUN

PATCH In review Stanislav Fomichev 3 participants 6 messages Jan 22, 2019 – Jan 24, 2019

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

cover support flow dissector in BPF_PROG_TEST_RUN

This patch series adds support for testing flow dissector BPF programs by extending already existing BPF_PROG_TEST_RUN. The goal is to have a packet as an input and `struct bpf_flow_key' as an output. That way we can easily test flow dissector programs' behavior.
I've also modified existing test_progs.c test to do a simple flow dissector run as well.

* first patch introduces new __skb_flow_bpf_dissect to simplify sharing between __skb_flow_bpf_dissect and BPF_PROG_TEST_RUN
* second patch adds actual BPF_PROG_TEST_RUN support
* third patch adds example usage to the selftests

Stanislav Fomichev (3):
net/flow_dissector: move bpf case into __skb_flow_bpf_dissect
bpf: add BPF_PROG_TEST_RUN support for flow dissector selftests/bpf: add simple BPF_PROG_TEST_RUN examples for flow
dissector

 include/linux/bpf.h                           |  3 +
 include/linux/skbuff.h                        |  5 +
 net/bpf/test_run.c                            | 75 ++++++++++++++-
 net/core/filter.c                             |  1 +
 net/core/flow_dissector.c                     | 92 +++++++++++--------
 tools/testing/selftests/bpf/Makefile          |  3 +
 .../selftests/bpf/flow_dissector_load.c       | 43 +--------
 .../selftests/bpf/flow_dissector_load.h       | 55 +++++++++++
 tools/testing/selftests/bpf/test_progs.c      | 78 +++++++++++++++-
 9 files changed, 276 insertions(+), 79 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/flow_dissector_load.h
Signature · 1 line
2.20.1.321.g9e740568ce-goog
#1 lore raw

PATCH 1/3 net/flow_dissector: move bpf case into __skb_flow_bpf_dissect

This way, we can reuse it for flow dissector in BPF_PROG_TEST_RUN.

No functional changes.

---
 include/linux/skbuff.h    |  5 +++
 net/core/flow_dissector.c | 92 +++++++++++++++++++++++----------------
 2 files changed, 59 insertions(+), 38 deletions(-)
include/linux/skbuff.h +5 -0
index 93f56fddd92a..be762fc34ff3 100644
@@ -1221,6 +1221,11 @@ static inline int skb_flow_dissector_bpf_prog_detach(const union bpf_attr *attr)
 }
 #endif
 
+struct bpf_flow_keys;
+bool __skb_flow_bpf_dissect(struct bpf_prog *prog,
+			    const struct sk_buff *skb,
+			    struct flow_dissector *flow_dissector,
+			    struct bpf_flow_keys *flow_keys);
 bool __skb_flow_dissect(const struct sk_buff *skb,
 			struct flow_dissector *flow_dissector,
 			void *target_container,
net/core/flow_dissector.c +54 -38

120 lines not on this page · raw message

Signature · 1 line
2.20.1.321.g9e740568ce-goog
#2 lore raw

PATCH 2/3 bpf: add BPF_PROG_TEST_RUN support for flow dissector

In reply to Stanislav Fomichev

The input is packet data, the output is struct bpf_flow_key. This should make it easy to test flow dissector programs without elaborate setup.

---
 include/linux/bpf.h |  3 ++
 net/bpf/test_run.c  | 75 ++++++++++++++++++++++++++++++++++++++++++++-
 net/core/filter.c   |  1 +
 3 files changed, 78 insertions(+), 1 deletion(-)
include/linux/bpf.h +3 -0
index e734f163bd0b..701ef954a258 100644
@@ -397,6 +397,9 @@ 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);
+int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
+				     const union bpf_attr *kattr,
+				     union bpf_attr __user *uattr);
 
 /* an array of programs to be executed under rcu_lock.
  *
net/bpf/test_run.c +74 -1

89 lines not on this page · raw message

net/core/filter.c +1 -0
index 2b3b436ef545..ff4641dae2be 100644
@@ -7690,6 +7690,7 @@ const struct bpf_verifier_ops flow_dissector_verifier_ops = {
 };
 
 const struct bpf_prog_ops flow_dissector_prog_ops = {
+	.test_run		= bpf_prog_test_run_flow_dissector,
 };
 
 int sk_detach_filter(struct sock *sk)
Signature · 1 line
2.20.1.321.g9e740568ce-goog
#3 lore raw

Re: [PATCH bpf-next 2/3] bpf: add BPF_PROG_TEST_RUN support for flow dissector

Quoted 43 lines · Stanislav Fomichev

The input is packet data, the output is struct bpf_flow_key. This should make it easy to test flow dissector programs without elaborate setup.

---
 include/linux/bpf.h |  3 ++
 net/bpf/test_run.c  | 75 ++++++++++++++++++++++++++++++++++++++++++++-
 net/core/filter.c   |  1 +
 3 files changed, 78 insertions(+), 1 deletion(-)
include/linux/bpf.h +3 -0
index e734f163bd0b..701ef954a258 100644
@@ -397,6 +397,9 @@ 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);
+int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
+				     const union bpf_attr *kattr,
+				     union bpf_attr __user *uattr);
 
 /* an array of programs to be executed under rcu_lock.
  *
net/bpf/test_run.c +4 -1
index fa2644d276ef..ecad72885f23 100644
@@ -16,12 +16,26 @@
 static __always_inline u32 bpf_test_run_one(struct bpf_prog *prog, void *ctx,
 		struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE])
 {
+	struct bpf_skb_data_end *cb;
+	struct sk_buff *skb;
 	u32 ret;
 
 	preempt_disable();
 	rcu_read_lock();
 	bpf_cgroup_storage_set(storage);
-	ret = BPF_PROG_RUN(prog, ctx);
+
+	switch (prog->type) {

net/bpf/test_run.c line 37

Quoted 10 lines
net/bpf/test_run.c +10 -0
+	case BPF_PROG_TYPE_FLOW_DISSECTOR:
+		skb = (struct sk_buff *)ctx;
+		cb = (struct bpf_skb_data_end *)skb->cb;
+		ret = __skb_flow_bpf_dissect(prog, ctx, &flow_keys_dissector,
+					     cb->qdisc_cb.flow_keys);
+		break;
+	default:
+		ret = BPF_PROG_RUN(prog, ctx);
+		break;
+	}

What is the benefit of this minimal code reuse?
It seems to me bpf_test_run_one() gets slower for all,
since prog type needs to be checked before every prog run. The point of bpf_prog_ops->test_run was to avoid this overhead. Are you somehow expecting flow_dissector prog using cgroup local storage? I don't think that's possible.

#4 lore raw

Re: [PATCH bpf-next 2/3] bpf: add BPF_PROG_TEST_RUN support for flow dissector

Quoted 53 lines · Alexei Starovoitov
Quoted 52 lines · Stanislav Fomichev

The input is packet data, the output is struct bpf_flow_key. This should make it easy to test flow dissector programs without elaborate setup.

---
 include/linux/bpf.h |  3 ++
 net/bpf/test_run.c  | 75 ++++++++++++++++++++++++++++++++++++++++++++-
 net/core/filter.c   |  1 +
 3 files changed, 78 insertions(+), 1 deletion(-)
include/linux/bpf.h +3 -0
index e734f163bd0b..701ef954a258 100644
@@ -397,6 +397,9 @@ 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);
+int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
+				     const union bpf_attr *kattr,
+				     union bpf_attr __user *uattr);
 
 /* an array of programs to be executed under rcu_lock.
  *
net/bpf/test_run.c +13 -1
index fa2644d276ef..ecad72885f23 100644
@@ -16,12 +16,26 @@
 static __always_inline u32 bpf_test_run_one(struct bpf_prog *prog, void *ctx,
 		struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE])
 {
+	struct bpf_skb_data_end *cb;
+	struct sk_buff *skb;
 	u32 ret;
 
 	preempt_disable();
 	rcu_read_lock();
 	bpf_cgroup_storage_set(storage);
-	ret = BPF_PROG_RUN(prog, ctx);
+
+	switch (prog->type) {
+	case BPF_PROG_TYPE_FLOW_DISSECTOR:
+		skb = (struct sk_buff *)ctx;
+		cb = (struct bpf_skb_data_end *)skb->cb;
+		ret = __skb_flow_bpf_dissect(prog, ctx, &flow_keys_dissector,
+					     cb->qdisc_cb.flow_keys);
+		break;
+	default:
+		ret = BPF_PROG_RUN(prog, ctx);
+		break;
Quoted 8 lines

net/bpf/test_run.c line 37

Quoted 1 line
net/bpf/test_run.c +1 -0
+	}

What is the benefit of this minimal code reuse?
It seems to me bpf_test_run_one() gets slower for all,
since prog type needs to be checked before every prog run. The point of bpf_prog_ops->test_run was to avoid this overhead. Are you somehow expecting flow_dissector prog using cgroup local storage? I don't think that's possible.

Agreed. Initially I didn't want to re-implement the logic of bpf_test_run. But now that you mention that it's mostly about cgroup local storage, I think I can do a simple loop inside of bpf_prog_test_run_flow_dissector. Thanks, will follow up with another series!

#5 lore raw

PATCH 3/3 selftests/bpf: add simple BPF_PROG_TEST_RUN examples for flow dissector

In reply to Stanislav Fomichev

Use existing pkt_v4 and pkt_v6 to make sure flow_keys are what we want.

Also, add new bpf_flow_load routine (and flow_dissector_load.h header) that loads bpf_flow.o program and does all required setup.

---
 tools/testing/selftests/bpf/Makefile          |  3 +
 .../selftests/bpf/flow_dissector_load.c       | 43 ++--------
 .../selftests/bpf/flow_dissector_load.h       | 55 +++++++++++++
 tools/testing/selftests/bpf/test_progs.c      | 78 ++++++++++++++++++-
 4 files changed, 139 insertions(+), 40 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/flow_dissector_load.h
tools/testing/selftests/bpf/Makefile +3 -0
index 70229de510f5..ade67470a152 100644
@@ -19,6 +19,9 @@ all: $(TEST_CUSTOM_PROGS)
 $(TEST_CUSTOM_PROGS): $(OUTPUT)/%: %.c
 	$(CC) -o $(TEST_CUSTOM_PROGS) -static $< -Wl,--build-id
 
+flow_dissector_load.c: flow_dissector_load.h
+test_run.c: flow_dissector_load.h
+
 # Order correspond to 'make run_tests' order
 TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \
 	test_align test_verifier_log test_dev_cgroup test_tcpbpf_user \
tools/testing/selftests/bpf/flow_dissector_load.c +5 -38

64 lines not on this page · raw message

tools/testing/selftests/bpf/flow_dissector_load.h new file +55 -0

55 lines not on this page · raw message

tools/testing/selftests/bpf/test_progs.c +76 -2

112 lines not on this page · raw message

Signature · 1 line
2.20.1.321.g9e740568ce-goog