Skip to contentSkip to conversation

support flow dissector in BPF_PROG_TEST_RUN

PATCH Superseded Stanislav Fomichev 3 participants 9 messages Dec 3, 2018 – Dec 4, 2018

Subsystems (7)

v1 of 3 Superseded Latest is v3 →

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                            | 76 +++++++++++++++--
 net/core/filter.c                             |  1 +
 net/core/flow_dissector.c                     | 83 +++++++++++--------
 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      | 76 ++++++++++++++++-
 9 files changed, 265 insertions(+), 80 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/flow_dissector_load.h
Signature · 1 line
2.20.0.rc1.387.gf8505762e3-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 | 83 +++++++++++++++++++++++----------------
 2 files changed, 54 insertions(+), 34 deletions(-)
include/linux/skbuff.h +5 -0
index 73902acf2b71..c10b27bb0cab 100644
@@ -1215,6 +1215,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 +49 -34

111 lines not on this page · raw message

Signature · 1 line
2.20.0.rc1.387.gf8505762e3-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  | 76 +++++++++++++++++++++++++++++++++++++++++----
 net/core/filter.c   |  1 +
 3 files changed, 74 insertions(+), 6 deletions(-)
include/linux/bpf.h +3 -0
index e82b7039fc66..7a572d15d5dd 100644
@@ -373,6 +373,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 +70 -6

115 lines not on this page · raw message

net/core/filter.c +1 -0
index bd0df75dc7b6..4eae6102399d 100644
@@ -7657,6 +7657,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.0.rc1.387.gf8505762e3-goog
#8 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      | 76 ++++++++++++++++++-
 4 files changed, 137 insertions(+), 40 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/flow_dissector_load.h
tools/testing/selftests/bpf/Makefile +3 -0
index 73aa6d8f4a2f..387763e6d137 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 +74 -2

106 lines not on this page · raw message

Signature · 1 line
2.20.0.rc1.387.gf8505762e3-goog
#3 lore raw

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

In reply to Stanislav Fomichev
Quoted 45 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  | 76 +++++++++++++++++++++++++++++++++++++++++----
 net/core/filter.c   |  1 +
 3 files changed, 74 insertions(+), 6 deletions(-)
include/linux/bpf.h +3 -0
index e82b7039fc66..7a572d15d5dd 100644
@@ -373,6 +373,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 +3 -2
index c89c22c49015..bfa05d31c6e3 100644
@@ -14,21 +14,33 @@
 #include <net/tcp.h>
 
 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_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE],
+               struct bpf_flow_keys *flow_keys)
 {
        u32 ret;
 
        preempt_disable();
        rcu_read_lock();
        bpf_cgroup_storage_set(storage);
-       ret = BPF_PROG_RUN(prog, ctx);
+

net/bpf/test_run.c line 35

Quoted 10 lines
net/bpf/test_run.c +10 -0
+       switch (prog->type) {
+       case BPF_PROG_TYPE_FLOW_DISSECTOR:
+               ret = __skb_flow_bpf_dissect(prog, ctx, &flow_keys_dissector,
+                                            flow_keys);
+               break;
+       default:
+               ret = BPF_PROG_RUN(prog, ctx);
+               break;
+       }
+

Is it possible to fold the logic above into bpf_prog_test_run_flow_dissector()? In that way, the logic flow is similar to other bpf_prog_test_run_XXX() functions.

Thanks,
Song

Quoted 110 lines
net/bpf/test_run.c +57 -4
        rcu_read_unlock();
        preempt_enable();
 
        return ret;
 }
 
-static u32 bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, u32 *time)
+static u32 bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, u32 *time,
+                       struct bpf_flow_keys *flow_keys)
 {
        struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] = { 0 };
        enum bpf_cgroup_storage_type stype;
@@ -49,7 +61,7 @@ static u32 bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, u32 *time)
                repeat = 1;
        time_start = ktime_get_ns();
        for (i = 0; i < repeat; i++) {
-               ret = bpf_test_run_one(prog, ctx, storage);
+               ret = bpf_test_run_one(prog, ctx, storage, flow_keys);
                if (need_resched()) {
                        if (signal_pending(current))
                                break;
@@ -165,7 +177,7 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
                __skb_push(skb, hh_len);
        if (is_direct_pkt_access)
                bpf_compute_data_pointers(skb);
-       retval = bpf_test_run(prog, skb, repeat, &duration);
+       retval = bpf_test_run(prog, skb, repeat, &duration, NULL);
        if (!is_l2) {
                if (skb_headroom(skb) < hh_len) {
                        int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb));
@@ -212,7 +224,7 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
        rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0);
        xdp.rxq = &rxqueue->xdp_rxq;
 
-       retval = bpf_test_run(prog, &xdp, repeat, &duration);
+       retval = bpf_test_run(prog, &xdp, repeat, &duration, NULL);
        if (xdp.data != data + XDP_PACKET_HEADROOM + NET_IP_ALIGN ||
            xdp.data_end != xdp.data + size)
                size = xdp.data_end - xdp.data;
@@ -220,3 +232,55 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
        kfree(data);
        return ret;
 }
+
+int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
+                                    const union bpf_attr *kattr,
+                                    union bpf_attr __user *uattr)
+{
+       struct bpf_flow_keys flow_keys = {};
+       u32 size = kattr->test.data_size_in;
+       u32 repeat = kattr->test.repeat;
+       u32 retval, duration;
+       struct sk_buff *skb;
+       struct sock *sk;
+       void *data;
+       int ret;
+
+       if (prog->type != BPF_PROG_TYPE_FLOW_DISSECTOR)
+               return -EINVAL;
+
+       data = bpf_test_init(kattr, size, NET_SKB_PAD + NET_IP_ALIGN,
+                            SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
+       if (IS_ERR(data))
+               return PTR_ERR(data);
+
+       sk = kzalloc(sizeof(*sk), GFP_USER);
+       if (!sk) {
+               kfree(data);
+               return -ENOMEM;
+       }
+       sock_net_set(sk, current->nsproxy->net_ns);
+       sock_init_data(NULL, sk);
+
+       skb = build_skb(data, 0);
+       if (!skb) {
+               kfree(data);
+               kfree(sk);
+               return -ENOMEM;
+       }
+       skb->sk = sk;
+
+       skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
+       __skb_put(skb, size);
+       skb->protocol = eth_type_trans(skb,
+                                      current->nsproxy->net_ns->loopback_dev);
+       skb_reset_network_header(skb);
+
+       retval = bpf_test_run(prog, skb, repeat, &duration, &flow_keys);
+
+       ret = bpf_test_finish(kattr, uattr, &flow_keys, sizeof(flow_keys),
+                             retval, duration);
+       kfree_skb(skb);
+       kfree(sk);
+       return ret;
+}
net/core/filter.c +1 -1
index bd0df75dc7b6..4eae6102399d 100644
@@ -7657,6 +7657,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)
--

2.20.0.rc1.387.gf8505762e3-goog

#4 lore raw

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

Quoted 52 lines · Song Liu
Quoted 51 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  | 76 +++++++++++++++++++++++++++++++++++++++++----
 net/core/filter.c   |  1 +
 3 files changed, 74 insertions(+), 6 deletions(-)
include/linux/bpf.h +3 -0
index e82b7039fc66..7a572d15d5dd 100644
@@ -373,6 +373,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 +9 -2
index c89c22c49015..bfa05d31c6e3 100644
@@ -14,21 +14,33 @@
 #include <net/tcp.h>
 
 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_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE],
+               struct bpf_flow_keys *flow_keys)
 {
        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:
+               ret = __skb_flow_bpf_dissect(prog, ctx, &flow_keys_dissector,
+                                            flow_keys);
+               break;
+       default:
Quoted 8 lines

net/bpf/test_run.c line 35

Quoted 4 lines
net/bpf/test_run.c +4 -0
+               ret = BPF_PROG_RUN(prog, ctx);
+               break;
+       }
+

Is it possible to fold the logic above into bpf_prog_test_run_flow_dissector()? In that way, the logic flow is similar to other bpf_prog_test_run_XXX() functions.

I can probably do everything that __skb_flow_bpf_dissect does inside of bpf_prog_test_run_flow_dissector (pass flow_keys in cb); that would remove this ugly program type check down the stack. But my additional goal here was to test __skb_flow_bpf_dissect itself as well.

Attached some possible prototype below. The only issue I see with that approach is that we need to clear flow_keys on each test iteration. I think in my current patch I'm actually missing a memset(&flow_keys, 0, ...) for each iteration of __skb_flow_bpf_dissect;
I haven't tested multiple runs :-/

So I'd prefer to continue doing this prog type check (plus, add a missing memset for flow_keys on each iteration in v2). WDYT?

---

include/linux/bpf.h +3 -0
index e82b7039fc66..7a572d15d5dd 100644
@@ -373,6 +373,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 +57 -0

60 lines not on this page · raw message

net/core/filter.c +1 -0
index bd0df75dc7b6..4eae6102399d 100644
@@ -7657,6 +7657,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)
Quoted 114 lines

Thanks,
Song

Quoted 110 lines
net/bpf/test_run.c +57 -4
        rcu_read_unlock();
        preempt_enable();
 
        return ret;
 }
 
-static u32 bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, u32 *time)
+static u32 bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, u32 *time,
+                       struct bpf_flow_keys *flow_keys)
 {
        struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] = { 0 };
        enum bpf_cgroup_storage_type stype;
@@ -49,7 +61,7 @@ static u32 bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, u32 *time)
                repeat = 1;
        time_start = ktime_get_ns();
        for (i = 0; i < repeat; i++) {
-               ret = bpf_test_run_one(prog, ctx, storage);
+               ret = bpf_test_run_one(prog, ctx, storage, flow_keys);
                if (need_resched()) {
                        if (signal_pending(current))
                                break;
@@ -165,7 +177,7 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
                __skb_push(skb, hh_len);
        if (is_direct_pkt_access)
                bpf_compute_data_pointers(skb);
-       retval = bpf_test_run(prog, skb, repeat, &duration);
+       retval = bpf_test_run(prog, skb, repeat, &duration, NULL);
        if (!is_l2) {
                if (skb_headroom(skb) < hh_len) {
                        int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb));
@@ -212,7 +224,7 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
        rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0);
        xdp.rxq = &rxqueue->xdp_rxq;
 
-       retval = bpf_test_run(prog, &xdp, repeat, &duration);
+       retval = bpf_test_run(prog, &xdp, repeat, &duration, NULL);
        if (xdp.data != data + XDP_PACKET_HEADROOM + NET_IP_ALIGN ||
            xdp.data_end != xdp.data + size)
                size = xdp.data_end - xdp.data;
@@ -220,3 +232,55 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
        kfree(data);
        return ret;
 }
+
+int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
+                                    const union bpf_attr *kattr,
+                                    union bpf_attr __user *uattr)
+{
+       struct bpf_flow_keys flow_keys = {};
+       u32 size = kattr->test.data_size_in;
+       u32 repeat = kattr->test.repeat;
+       u32 retval, duration;
+       struct sk_buff *skb;
+       struct sock *sk;
+       void *data;
+       int ret;
+
+       if (prog->type != BPF_PROG_TYPE_FLOW_DISSECTOR)
+               return -EINVAL;
+
+       data = bpf_test_init(kattr, size, NET_SKB_PAD + NET_IP_ALIGN,
+                            SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
+       if (IS_ERR(data))
+               return PTR_ERR(data);
+
+       sk = kzalloc(sizeof(*sk), GFP_USER);
+       if (!sk) {
+               kfree(data);
+               return -ENOMEM;
+       }
+       sock_net_set(sk, current->nsproxy->net_ns);
+       sock_init_data(NULL, sk);
+
+       skb = build_skb(data, 0);
+       if (!skb) {
+               kfree(data);
+               kfree(sk);
+               return -ENOMEM;
+       }
+       skb->sk = sk;
+
+       skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
+       __skb_put(skb, size);
+       skb->protocol = eth_type_trans(skb,
+                                      current->nsproxy->net_ns->loopback_dev);
+       skb_reset_network_header(skb);
+
+       retval = bpf_test_run(prog, skb, repeat, &duration, &flow_keys);
+
+       ret = bpf_test_finish(kattr, uattr, &flow_keys, sizeof(flow_keys),
+                             retval, duration);
+       kfree_skb(skb);
+       kfree(sk);
+       return ret;
+}
net/core/filter.c +1 -1
index bd0df75dc7b6..4eae6102399d 100644
@@ -7657,6 +7657,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)
--

2.20.0.rc1.387.gf8505762e3-goog

#5 lore raw

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

Quoted 66 lines · Stanislav Fomichev
Quoted 52 lines · Song Liu
Quoted 51 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  | 76 +++++++++++++++++++++++++++++++++++++++++----
 net/core/filter.c   |  1 +
 3 files changed, 74 insertions(+), 6 deletions(-)
include/linux/bpf.h +3 -0
index e82b7039fc66..7a572d15d5dd 100644
@@ -373,6 +373,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 +9 -2
index c89c22c49015..bfa05d31c6e3 100644
@@ -14,21 +14,33 @@
 #include <net/tcp.h>
 
 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_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE],
+               struct bpf_flow_keys *flow_keys)
 {
        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:
+               ret = __skb_flow_bpf_dissect(prog, ctx, &flow_keys_dissector,
+                                            flow_keys);
+               break;
+       default:
Quoted 8 lines

net/bpf/test_run.c int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr, · line 253

Quoted 4 lines
net/bpf/test_run.c +4 -0
+               ret = BPF_PROG_RUN(prog, ctx);
+               break;
+       }
+

Is it possible to fold the logic above into bpf_prog_test_run_flow_dissector()? In that way, the logic flow is similar to other bpf_prog_test_run_XXX() functions.

I can probably do everything that __skb_flow_bpf_dissect does inside of bpf_prog_test_run_flow_dissector (pass flow_keys in cb); that would remove this ugly program type check down the stack. But my additional goal here was to test __skb_flow_bpf_dissect itself as well.

Quoted 8 lines

Attached some possible prototype below. The only issue I see with that approach is that we need to clear flow_keys on each test iteration. I think in my current patch I'm actually missing a memset(&flow_keys, 0, ...) for each iteration of __skb_flow_bpf_dissect;
I haven't tested multiple runs :-/

So I'd prefer to continue doing this prog type check (plus, add a missing memset for flow_keys on each iteration in v2). WDYT?

I think I found another problem with not using __skb_flow_bpf_dissect:
I want to add some clamping to the thoff/nhoff returned by the bpf program (to be safe); it's probably better to keep all this logic in one place.

I can probably pass flow_keys in the skb->cb, so I don't have to add flow_keys to the bpf_test_run_one/bpf_test_run signatures. I'll send v2 shortly, let me know if it addresses your point.

Quoted 208 lines

---

include/linux/bpf.h +3 -0
index e82b7039fc66..7a572d15d5dd 100644
@@ -373,6 +373,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 +57 -0
index c89c22c49015..04387ef1f95a 100644
@@ -220,3 +220,60 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
 	kfree(data);
 	return ret;
 }
+
+int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
+				     const union bpf_attr *kattr,
+				     union bpf_attr __user *uattr)
+{
+	struct bpf_flow_keys flow_keys = {};
+	u32 size = kattr->test.data_size_in;
+	u32 repeat = kattr->test.repeat;
+	struct bpf_skb_data_end *cb;
+	u32 retval, duration;
+	struct sk_buff *skb;
+	struct sock *sk;
+	void *data;
+	int ret;
+
+	if (prog->type != BPF_PROG_TYPE_FLOW_DISSECTOR)
+		return -EINVAL;
+
+	data = bpf_test_init(kattr, size, NET_SKB_PAD + NET_IP_ALIGN,
+			     SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
+	if (IS_ERR(data))
+		return PTR_ERR(data);
+
+	sk = kzalloc(sizeof(*sk), GFP_USER);
+	if (!sk) {
+		kfree(data);
+		return -ENOMEM;
+	}
+	sock_net_set(sk, current->nsproxy->net_ns);
+	sock_init_data(NULL, sk);
+
+	skb = build_skb(data, 0);
+	if (!skb) {
+		kfree(data);
+		kfree(sk);
+		return -ENOMEM;
+	}
+	skb->sk = sk;
+
+	skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
+	__skb_put(skb, size);
+	skb->protocol = eth_type_trans(skb,
+				       current->nsproxy->net_ns->loopback_dev);
+	skb_reset_network_header(skb);
+
+	cb = (struct bpf_skb_data_end *)skb->cb;
+	memset(cb, 0, sizeof(cb));
+	cb->qdisc_cb.flow_keys = &flow_keys;
+
+	retval = bpf_test_run(prog, skb, repeat, &duration);
+
+	ret = bpf_test_finish(kattr, uattr, &flow_keys, sizeof(flow_keys),
+			      retval, duration);
+	kfree_skb(skb);
+	kfree(sk);
+	return ret;
+}
net/core/filter.c +1 -0
index bd0df75dc7b6..4eae6102399d 100644
@@ -7657,6 +7657,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)
Quoted 115 lines

Thanks,
Song

Quoted 110 lines
net/bpf/test_run.c +57 -4
        rcu_read_unlock();
        preempt_enable();
 
        return ret;
 }
 
-static u32 bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, u32 *time)
+static u32 bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, u32 *time,
+                       struct bpf_flow_keys *flow_keys)
 {
        struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] = { 0 };
        enum bpf_cgroup_storage_type stype;
@@ -49,7 +61,7 @@ static u32 bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, u32 *time)
                repeat = 1;
        time_start = ktime_get_ns();
        for (i = 0; i < repeat; i++) {
-               ret = bpf_test_run_one(prog, ctx, storage);
+               ret = bpf_test_run_one(prog, ctx, storage, flow_keys);
                if (need_resched()) {
                        if (signal_pending(current))
                                break;
@@ -165,7 +177,7 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
                __skb_push(skb, hh_len);
        if (is_direct_pkt_access)
                bpf_compute_data_pointers(skb);
-       retval = bpf_test_run(prog, skb, repeat, &duration);
+       retval = bpf_test_run(prog, skb, repeat, &duration, NULL);
        if (!is_l2) {
                if (skb_headroom(skb) < hh_len) {
                        int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb));
@@ -212,7 +224,7 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
        rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0);
        xdp.rxq = &rxqueue->xdp_rxq;
 
-       retval = bpf_test_run(prog, &xdp, repeat, &duration);
+       retval = bpf_test_run(prog, &xdp, repeat, &duration, NULL);
        if (xdp.data != data + XDP_PACKET_HEADROOM + NET_IP_ALIGN ||
            xdp.data_end != xdp.data + size)
                size = xdp.data_end - xdp.data;
@@ -220,3 +232,55 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
        kfree(data);
        return ret;
 }
+
+int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
+                                    const union bpf_attr *kattr,
+                                    union bpf_attr __user *uattr)
+{
+       struct bpf_flow_keys flow_keys = {};
+       u32 size = kattr->test.data_size_in;
+       u32 repeat = kattr->test.repeat;
+       u32 retval, duration;
+       struct sk_buff *skb;
+       struct sock *sk;
+       void *data;
+       int ret;
+
+       if (prog->type != BPF_PROG_TYPE_FLOW_DISSECTOR)
+               return -EINVAL;
+
+       data = bpf_test_init(kattr, size, NET_SKB_PAD + NET_IP_ALIGN,
+                            SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
+       if (IS_ERR(data))
+               return PTR_ERR(data);
+
+       sk = kzalloc(sizeof(*sk), GFP_USER);
+       if (!sk) {
+               kfree(data);
+               return -ENOMEM;
+       }
+       sock_net_set(sk, current->nsproxy->net_ns);
+       sock_init_data(NULL, sk);
+
+       skb = build_skb(data, 0);
+       if (!skb) {
+               kfree(data);
+               kfree(sk);
+               return -ENOMEM;
+       }
+       skb->sk = sk;
+
+       skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
+       __skb_put(skb, size);
+       skb->protocol = eth_type_trans(skb,
+                                      current->nsproxy->net_ns->loopback_dev);
+       skb_reset_network_header(skb);
+
+       retval = bpf_test_run(prog, skb, repeat, &duration, &flow_keys);
+
+       ret = bpf_test_finish(kattr, uattr, &flow_keys, sizeof(flow_keys),
+                             retval, duration);
+       kfree_skb(skb);
+       kfree(sk);
+       return ret;
+}
net/core/filter.c +1 -1
index bd0df75dc7b6..4eae6102399d 100644
@@ -7657,6 +7657,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)
--

2.20.0.rc1.387.gf8505762e3-goog

#6 lore raw

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

In reply to Stanislav Fomichev
Quoted 67 lines · Stanislav Fomichev
Quoted 52 lines · Song Liu
Quoted 51 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  | 76 +++++++++++++++++++++++++++++++++++++++++----
 net/core/filter.c   |  1 +
 3 files changed, 74 insertions(+), 6 deletions(-)
include/linux/bpf.h +3 -0
index e82b7039fc66..7a572d15d5dd 100644
@@ -373,6 +373,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 +9 -2
index c89c22c49015..bfa05d31c6e3 100644
@@ -14,21 +14,33 @@
 #include <net/tcp.h>
 
 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_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE],
+               struct bpf_flow_keys *flow_keys)
 {
        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:
+               ret = __skb_flow_bpf_dissect(prog, ctx, &flow_keys_dissector,
+                                            flow_keys);
+               break;
+       default:
Quoted 8 lines

net/bpf/test_run.c int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr, · line 253

Quoted 4 lines
net/bpf/test_run.c +4 -0
+               ret = BPF_PROG_RUN(prog, ctx);
+               break;
+       }
+

Is it possible to fold the logic above into bpf_prog_test_run_flow_dissector()? In that way, the logic flow is similar to other bpf_prog_test_run_XXX() functions.

I can probably do everything that __skb_flow_bpf_dissect does inside of bpf_prog_test_run_flow_dissector (pass flow_keys in cb); that would remove this ugly program type check down the stack. But my additional goal here was to test __skb_flow_bpf_dissect itself as well.

Quoted 8 lines

Attached some possible prototype below. The only issue I see with that approach is that we need to clear flow_keys on each test iteration. I think in my current patch I'm actually missing a memset(&flow_keys, 0, ...) for each iteration of __skb_flow_bpf_dissect;
I haven't tested multiple runs :-/

So I'd prefer to continue doing this prog type check (plus, add a missing memset for flow_keys on each iteration in v2). WDYT?

I think v2 is still using original logic? Actually, after a second thought, I think
it is OK to change bpf_test_run_one() (unless Alexei and/or Daniel has better suggestions.

Thanks,
Song

Quoted 209 lines

---

include/linux/bpf.h +3 -0
index e82b7039fc66..7a572d15d5dd 100644
@@ -373,6 +373,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 +57 -0
index c89c22c49015..04387ef1f95a 100644
@@ -220,3 +220,60 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
        kfree(data);
        return ret;
 }
+
+int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
+                                    const union bpf_attr *kattr,
+                                    union bpf_attr __user *uattr)
+{
+       struct bpf_flow_keys flow_keys = {};
+       u32 size = kattr->test.data_size_in;
+       u32 repeat = kattr->test.repeat;
+       struct bpf_skb_data_end *cb;
+       u32 retval, duration;
+       struct sk_buff *skb;
+       struct sock *sk;
+       void *data;
+       int ret;
+
+       if (prog->type != BPF_PROG_TYPE_FLOW_DISSECTOR)
+               return -EINVAL;
+
+       data = bpf_test_init(kattr, size, NET_SKB_PAD + NET_IP_ALIGN,
+                            SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
+       if (IS_ERR(data))
+               return PTR_ERR(data);
+
+       sk = kzalloc(sizeof(*sk), GFP_USER);
+       if (!sk) {
+               kfree(data);
+               return -ENOMEM;
+       }
+       sock_net_set(sk, current->nsproxy->net_ns);
+       sock_init_data(NULL, sk);
+
+       skb = build_skb(data, 0);
+       if (!skb) {
+               kfree(data);
+               kfree(sk);
+               return -ENOMEM;
+       }
+       skb->sk = sk;
+
+       skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
+       __skb_put(skb, size);
+       skb->protocol = eth_type_trans(skb,
+                                      current->nsproxy->net_ns->loopback_dev);
+       skb_reset_network_header(skb);
+
+       cb = (struct bpf_skb_data_end *)skb->cb;
+       memset(cb, 0, sizeof(cb));
+       cb->qdisc_cb.flow_keys = &flow_keys;
+
+       retval = bpf_test_run(prog, skb, repeat, &duration);
+
+       ret = bpf_test_finish(kattr, uattr, &flow_keys, sizeof(flow_keys),
+                             retval, duration);
+       kfree_skb(skb);
+       kfree(sk);
+       return ret;
+}
net/core/filter.c +1 -0
index bd0df75dc7b6..4eae6102399d 100644
@@ -7657,6 +7657,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)
Quoted 115 lines

Thanks,
Song

Quoted 110 lines
net/bpf/test_run.c +57 -4
        rcu_read_unlock();
        preempt_enable();
 
        return ret;
 }
 
-static u32 bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, u32 *time)
+static u32 bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, u32 *time,
+                       struct bpf_flow_keys *flow_keys)
 {
        struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] = { 0 };
        enum bpf_cgroup_storage_type stype;
@@ -49,7 +61,7 @@ static u32 bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, u32 *time)
                repeat = 1;
        time_start = ktime_get_ns();
        for (i = 0; i < repeat; i++) {
-               ret = bpf_test_run_one(prog, ctx, storage);
+               ret = bpf_test_run_one(prog, ctx, storage, flow_keys);
                if (need_resched()) {
                        if (signal_pending(current))
                                break;
@@ -165,7 +177,7 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
                __skb_push(skb, hh_len);
        if (is_direct_pkt_access)
                bpf_compute_data_pointers(skb);
-       retval = bpf_test_run(prog, skb, repeat, &duration);
+       retval = bpf_test_run(prog, skb, repeat, &duration, NULL);
        if (!is_l2) {
                if (skb_headroom(skb) < hh_len) {
                        int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb));
@@ -212,7 +224,7 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
        rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0);
        xdp.rxq = &rxqueue->xdp_rxq;
 
-       retval = bpf_test_run(prog, &xdp, repeat, &duration);
+       retval = bpf_test_run(prog, &xdp, repeat, &duration, NULL);
        if (xdp.data != data + XDP_PACKET_HEADROOM + NET_IP_ALIGN ||
            xdp.data_end != xdp.data + size)
                size = xdp.data_end - xdp.data;
@@ -220,3 +232,55 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
        kfree(data);
        return ret;
 }
+
+int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
+                                    const union bpf_attr *kattr,
+                                    union bpf_attr __user *uattr)
+{
+       struct bpf_flow_keys flow_keys = {};
+       u32 size = kattr->test.data_size_in;
+       u32 repeat = kattr->test.repeat;
+       u32 retval, duration;
+       struct sk_buff *skb;
+       struct sock *sk;
+       void *data;
+       int ret;
+
+       if (prog->type != BPF_PROG_TYPE_FLOW_DISSECTOR)
+               return -EINVAL;
+
+       data = bpf_test_init(kattr, size, NET_SKB_PAD + NET_IP_ALIGN,
+                            SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
+       if (IS_ERR(data))
+               return PTR_ERR(data);
+
+       sk = kzalloc(sizeof(*sk), GFP_USER);
+       if (!sk) {
+               kfree(data);
+               return -ENOMEM;
+       }
+       sock_net_set(sk, current->nsproxy->net_ns);
+       sock_init_data(NULL, sk);
+
+       skb = build_skb(data, 0);
+       if (!skb) {
+               kfree(data);
+               kfree(sk);
+               return -ENOMEM;
+       }
+       skb->sk = sk;
+
+       skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
+       __skb_put(skb, size);
+       skb->protocol = eth_type_trans(skb,
+                                      current->nsproxy->net_ns->loopback_dev);
+       skb_reset_network_header(skb);
+
+       retval = bpf_test_run(prog, skb, repeat, &duration, &flow_keys);
+
+       ret = bpf_test_finish(kattr, uattr, &flow_keys, sizeof(flow_keys),
+                             retval, duration);
+       kfree_skb(skb);
+       kfree(sk);
+       return ret;
+}
net/core/filter.c +1 -1
index bd0df75dc7b6..4eae6102399d 100644
@@ -7657,6 +7657,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)
--

2.20.0.rc1.387.gf8505762e3-goog

#7 lore raw

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

Quoted 74 lines · Song Liu
Quoted 73 lines · Stanislav Fomichev
Quoted 52 lines · Song Liu
Quoted 51 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  | 76 +++++++++++++++++++++++++++++++++++++++++----
 net/core/filter.c   |  1 +
 3 files changed, 74 insertions(+), 6 deletions(-)
include/linux/bpf.h +3 -0
index e82b7039fc66..7a572d15d5dd 100644
@@ -373,6 +373,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 +9 -2
index c89c22c49015..bfa05d31c6e3 100644
@@ -14,21 +14,33 @@
 #include <net/tcp.h>
 
 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_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE],
+               struct bpf_flow_keys *flow_keys)
 {
        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:
+               ret = __skb_flow_bpf_dissect(prog, ctx, &flow_keys_dissector,
+                                            flow_keys);
+               break;
+       default:
Quoted 8 lines

net/bpf/test_run.c int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr, · line 253

Quoted 4 lines
net/bpf/test_run.c +4 -0
+               ret = BPF_PROG_RUN(prog, ctx);
+               break;
+       }
+

Is it possible to fold the logic above into bpf_prog_test_run_flow_dissector()? In that way, the logic flow is similar to other bpf_prog_test_run_XXX() functions.

I can probably do everything that __skb_flow_bpf_dissect does inside of bpf_prog_test_run_flow_dissector (pass flow_keys in cb); that would remove this ugly program type check down the stack. But my additional goal here was to test __skb_flow_bpf_dissect itself as well.

Attached some possible prototype below. The only issue I see with that approach is that we need to clear flow_keys on each test iteration. I think in my current patch I'm actually missing a memset(&flow_keys, 0, ...) for each iteration of __skb_flow_bpf_dissect;
I haven't tested multiple runs :-/

Quoted 5 lines
Quoted 2 lines

So I'd prefer to continue doing this prog type check (plus, add a missing memset for flow_keys on each iteration in v2). WDYT?

I think v2 is still using original logic? Actually, after a second thought, I think

In a sense, it does. v2 still passes flow_dissector to the lower level bpf_test_run_one routine, it just passes it in the skb->cb, not explicitly in the function arguments.

Quoted 2 lines

it is OK to change bpf_test_run_one() (unless Alexei and/or Daniel has better suggestions.

The only preference I have is that I want to call __skb_flow_bpf_dissect from bpf_test_run_one (i.e. do NOT do elaborate setup in bpf_prog_test_run_flow_dissector so BPF_PROG_RUN in bpf_test_run_one just magically works).

I don't really care how we pass flow_keys there (skb->cb vs plumbing the argument) :-)

Quoted 213 lines

Thanks,
Song

Quoted 209 lines

---

include/linux/bpf.h +3 -0
index e82b7039fc66..7a572d15d5dd 100644
@@ -373,6 +373,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 +57 -0
index c89c22c49015..04387ef1f95a 100644
@@ -220,3 +220,60 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
        kfree(data);
        return ret;
 }
+
+int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
+                                    const union bpf_attr *kattr,
+                                    union bpf_attr __user *uattr)
+{
+       struct bpf_flow_keys flow_keys = {};
+       u32 size = kattr->test.data_size_in;
+       u32 repeat = kattr->test.repeat;
+       struct bpf_skb_data_end *cb;
+       u32 retval, duration;
+       struct sk_buff *skb;
+       struct sock *sk;
+       void *data;
+       int ret;
+
+       if (prog->type != BPF_PROG_TYPE_FLOW_DISSECTOR)
+               return -EINVAL;
+
+       data = bpf_test_init(kattr, size, NET_SKB_PAD + NET_IP_ALIGN,
+                            SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
+       if (IS_ERR(data))
+               return PTR_ERR(data);
+
+       sk = kzalloc(sizeof(*sk), GFP_USER);
+       if (!sk) {
+               kfree(data);
+               return -ENOMEM;
+       }
+       sock_net_set(sk, current->nsproxy->net_ns);
+       sock_init_data(NULL, sk);
+
+       skb = build_skb(data, 0);
+       if (!skb) {
+               kfree(data);
+               kfree(sk);
+               return -ENOMEM;
+       }
+       skb->sk = sk;
+
+       skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
+       __skb_put(skb, size);
+       skb->protocol = eth_type_trans(skb,
+                                      current->nsproxy->net_ns->loopback_dev);
+       skb_reset_network_header(skb);
+
+       cb = (struct bpf_skb_data_end *)skb->cb;
+       memset(cb, 0, sizeof(cb));
+       cb->qdisc_cb.flow_keys = &flow_keys;
+
+       retval = bpf_test_run(prog, skb, repeat, &duration);
+
+       ret = bpf_test_finish(kattr, uattr, &flow_keys, sizeof(flow_keys),
+                             retval, duration);
+       kfree_skb(skb);
+       kfree(sk);
+       return ret;
+}
net/core/filter.c +1 -0
index bd0df75dc7b6..4eae6102399d 100644
@@ -7657,6 +7657,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)
Quoted 115 lines

Thanks,
Song

Quoted 110 lines
net/bpf/test_run.c +57 -4
        rcu_read_unlock();
        preempt_enable();
 
        return ret;
 }
 
-static u32 bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, u32 *time)
+static u32 bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, u32 *time,
+                       struct bpf_flow_keys *flow_keys)
 {
        struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] = { 0 };
        enum bpf_cgroup_storage_type stype;
@@ -49,7 +61,7 @@ static u32 bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, u32 *time)
                repeat = 1;
        time_start = ktime_get_ns();
        for (i = 0; i < repeat; i++) {
-               ret = bpf_test_run_one(prog, ctx, storage);
+               ret = bpf_test_run_one(prog, ctx, storage, flow_keys);
                if (need_resched()) {
                        if (signal_pending(current))
                                break;
@@ -165,7 +177,7 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
                __skb_push(skb, hh_len);
        if (is_direct_pkt_access)
                bpf_compute_data_pointers(skb);
-       retval = bpf_test_run(prog, skb, repeat, &duration);
+       retval = bpf_test_run(prog, skb, repeat, &duration, NULL);
        if (!is_l2) {
                if (skb_headroom(skb) < hh_len) {
                        int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb));
@@ -212,7 +224,7 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
        rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0);
        xdp.rxq = &rxqueue->xdp_rxq;
 
-       retval = bpf_test_run(prog, &xdp, repeat, &duration);
+       retval = bpf_test_run(prog, &xdp, repeat, &duration, NULL);
        if (xdp.data != data + XDP_PACKET_HEADROOM + NET_IP_ALIGN ||
            xdp.data_end != xdp.data + size)
                size = xdp.data_end - xdp.data;
@@ -220,3 +232,55 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
        kfree(data);
        return ret;
 }
+
+int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
+                                    const union bpf_attr *kattr,
+                                    union bpf_attr __user *uattr)
+{
+       struct bpf_flow_keys flow_keys = {};
+       u32 size = kattr->test.data_size_in;
+       u32 repeat = kattr->test.repeat;
+       u32 retval, duration;
+       struct sk_buff *skb;
+       struct sock *sk;
+       void *data;
+       int ret;
+
+       if (prog->type != BPF_PROG_TYPE_FLOW_DISSECTOR)
+               return -EINVAL;
+
+       data = bpf_test_init(kattr, size, NET_SKB_PAD + NET_IP_ALIGN,
+                            SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
+       if (IS_ERR(data))
+               return PTR_ERR(data);
+
+       sk = kzalloc(sizeof(*sk), GFP_USER);
+       if (!sk) {
+               kfree(data);
+               return -ENOMEM;
+       }
+       sock_net_set(sk, current->nsproxy->net_ns);
+       sock_init_data(NULL, sk);
+
+       skb = build_skb(data, 0);
+       if (!skb) {
+               kfree(data);
+               kfree(sk);
+               return -ENOMEM;
+       }
+       skb->sk = sk;
+
+       skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
+       __skb_put(skb, size);
+       skb->protocol = eth_type_trans(skb,
+                                      current->nsproxy->net_ns->loopback_dev);
+       skb_reset_network_header(skb);
+
+       retval = bpf_test_run(prog, skb, repeat, &duration, &flow_keys);
+
+       ret = bpf_test_finish(kattr, uattr, &flow_keys, sizeof(flow_keys),
+                             retval, duration);
+       kfree_skb(skb);
+       kfree(sk);
+       return ret;
+}
net/core/filter.c +1 -1
index bd0df75dc7b6..4eae6102399d 100644
@@ -7657,6 +7657,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)
--

2.20.0.rc1.387.gf8505762e3-goog