support flow dissector in BPF_PROG_TEST_RUN
Subsystems (7)
v2 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 converts BPF flow dissector to thoff
* forth patch correctly caps nhoff and thoff returned from bpf flow dissector
* fifth patch adds example usage to the selftests
v2 changes:
* new patch to use thoff instead of nhoff in bpf flow dissector
* new patch to correctly cap thoff for BPF case
* add missing memset(flow_keys, 0, ...) to __skb_flow_bpf_dissect
* set test iterations to 10
Stanislav Fomichev (5):
net/flow_dissector: move bpf case into __skb_flow_bpf_dissect
bpf: add BPF_PROG_TEST_RUN support for flow dissector selftests/bpf: use thoff instead of nhoff in BPF flow dissector net/flow_dissector: correctly cap nhoff and thoff in case of BPF 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 | 71 ++++++++++++++- net/core/filter.c | 1 + net/core/flow_dissector.c | 88 ++++++++++++------- tools/testing/selftests/bpf/Makefile | 3 + tools/testing/selftests/bpf/bpf_flow.c | 36 ++++---- .../selftests/bpf/flow_dissector_load.c | 43 ++------- .../selftests/bpf/flow_dissector_load.h | 55 ++++++++++++ tools/testing/selftests/bpf/test_progs.c | 78 +++++++++++++++- 10 files changed, 289 insertions(+), 94 deletions(-) create mode 100644 tools/testing/selftests/bpf/flow_dissector_load.h
Signature · 1 line
2.20.0.rc1.387.gf8505762e3-goog
PATCH v2 1/5 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 | 85 +++++++++++++++++++++++---------------- 2 files changed, 56 insertions(+), 34 deletions(-)
include/linux/skbuff.h +5 -0
@@ -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 +51 -34
113 lines not on this page · raw message
Signature · 1 line
2.20.0.rc1.387.gf8505762e3-goog
PATCH v2 2/5 bpf: add BPF_PROG_TEST_RUN support for flow dissector
In reply to Stanislav FomichevThe 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 | 71 ++++++++++++++++++++++++++++++++++++++++++++- net/core/filter.c | 1 + 3 files changed, 74 insertions(+), 1 deletion(-)
include/linux/bpf.h +3 -0
@@ -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 -1
85 lines not on this page · raw message
net/core/filter.c +1 -0
@@ -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
PATCH v2 3/5 selftests/bpf: use thoff instead of nhoff in BPF flow dissector
In reply to Stanislav FomichevWe are returning thoff from the flow dissector, not the nhoff. Pass thoff along with nhoff to the bpf program (initially thoff == nhoff) and expect flow dissector amend/return thoff, not nhoff.
This avoids confusion, when by the time bpf flow dissector exits, nhoff == thoff, which doesn't make much sense (this is relevant in the context of the next patch, where I add simple selftest and manually construct expected flow_keys).
--- net/core/flow_dissector.c | 1 + tools/testing/selftests/bpf/bpf_flow.c | 36 ++++++++++++-------------- 2 files changed, 18 insertions(+), 19 deletions(-)
net/core/flow_dissector.c +1 -0
@@ -708,6 +708,7 @@ bool __skb_flow_bpf_dissect(struct bpf_prog *prog,memset (flow_keys,0 ,sizeof (*flow_keys)); cb->qdisc_cb.flow_keys = flow_keys; flow_keys->nhoff =skb_network_offset (skb); + flow_keys->thoff = flow_keys->nhoff;bpf_compute_data_pointers ((struct sk_buff *)skb); result =BPF_PROG_RUN (prog, skb);
tools/testing/selftests/bpf/bpf_flow.c +17 -19
@@ -70,18 +70,18 @@ static __always_inline void *bpf_flow_dissect_get_header(struct __sk_buff *skb, {void *data_end = (void *)(long )skb->data_end;void *data = (void *)(long )skb->data; -__u16 nhoff = skb->flow_keys->nhoff; +__u16 thoff = skb->flow_keys->thoff;__u8 *hdr;/* Verifies this variable offset does not overflow */ -if (nhoff > (USHRT_MAX - hdr_size)) +if (thoff > (USHRT_MAX - hdr_size))return NULL ; - hdr = data + nhoff; + hdr = data + thoff;if (hdr + hdr_size <= data_end)return hdr; -if (bpf_skb_load_bytes (skb, nhoff, buffer, hdr_size)) +if (bpf_skb_load_bytes (skb, thoff, buffer, hdr_size))return NULL ;return buffer;
@@ -158,13 +158,13 @@ static __always_inline int parse_ip_proto(struct __sk_buff *skb, __u8 proto)/* Only inspect standard GRE packets with version 0 */ return BPF_OK ; - keys->nhoff +=sizeof (*gre);/* Step over GRE Flags and Proto */ + keys->thoff +=sizeof (*gre);/* Step over GRE Flags and Proto */ if (GRE_IS_CSUM (gre->flags)) - keys->nhoff +=4 ;/* Step over chksum and Padding */ + keys->thoff +=4 ;/* Step over chksum and Padding */ if (GRE_IS_KEY (gre->flags)) - keys->nhoff +=4 ;/* Step over key */ + keys->thoff +=4 ;/* Step over key */ if (GRE_IS_SEQ (gre->flags)) - keys->nhoff +=4 ;/* Step over sequence number */ + keys->thoff +=4 ;/* Step over sequence number */ keys->is_encap = true;
@@ -174,7 +174,7 @@ static __always_inline int parse_ip_proto(struct __sk_buff *skb, __u8 proto)if (!eth)return BPF_DROP ; - keys->nhoff +=sizeof (*eth); + keys->thoff +=sizeof (*eth);return parse_eth_proto (skb, eth->h_proto); }else {
@@ -191,7 +191,6 @@ static __always_inline int parse_ip_proto(struct __sk_buff *skb, __u8 proto)if ((__u8 *)tcp + (tcp->doff <<2 ) > data_end)return BPF_DROP ; - keys->thoff = keys->nhoff; keys->sport = tcp->source; keys->dport = tcp->dest;return BPF_OK ;
@@ -201,7 +200,6 @@ static __always_inline int parse_ip_proto(struct __sk_buff *skb, __u8 proto)if (!udp)return BPF_DROP ; - keys->thoff = keys->nhoff; keys->sport = udp->source; keys->dport = udp->dest;return BPF_OK ;
@@ -252,8 +250,8 @@ PROG(IP)(struct __sk_buff *skb) keys->ipv4_src = iph->saddr; keys->ipv4_dst = iph->daddr; - keys->nhoff += iph->ihl <<2 ; -if (data + keys->nhoff > data_end) + keys->thoff += iph->ihl <<2 ; +if (data + keys->thoff > data_end)return BPF_DROP ;if (iph->frag_off &bpf_htons (IP_MF |IP_OFFSET )) {
@@ -285,7 +283,7 @@ PROG(IPV6)(struct __sk_buff *skb) keys->addr_proto =ETH_P_IPV6 ;memcpy (&keys->ipv6_src, &ip6h->saddr,2 *sizeof (ip6h->saddr)); - keys->nhoff +=sizeof (struct ipv6hdr ); + keys->thoff +=sizeof (struct ipv6hdr );return parse_ipv6_proto (skb, ip6h->nexthdr); }
@@ -301,7 +299,7 @@ PROG(IPV6OP)(struct __sk_buff *skb)/* hlen is in 8-octets and does not include the first 8 bytes * of the header */ - skb->flow_keys->nhoff += (1 + ip6h->hdrlen) <<3 ; + skb->flow_keys->thoff += (1 + ip6h->hdrlen) <<3 ;return parse_ipv6_proto (skb, ip6h->nexthdr); }
@@ -315,7 +313,7 @@ PROG(IPV6FR)(struct __sk_buff *skb)if (!fragh)return BPF_DROP ; - keys->nhoff +=sizeof (*fragh); + keys->thoff +=sizeof (*fragh); keys->is_frag = true;if (!(fragh->frag_off &bpf_htons (IP6_OFFSET ))) keys->is_first_frag = true;
@@ -341,7 +339,7 @@ PROG(VLAN)(struct __sk_buff *skb)__be16 proto;/* Peek back to see if single or double-tagging */ -if (bpf_skb_load_bytes (skb, keys->nhoff -sizeof (proto), &proto, +if (bpf_skb_load_bytes (skb, keys->thoff -sizeof (proto), &proto,sizeof (proto)))return BPF_DROP ;
@@ -354,14 +352,14 @@ PROG(VLAN)(struct __sk_buff *skb)if (vlan->h_vlan_encapsulated_proto !=bpf_htons (ETH_P_8021Q ))return BPF_DROP ; - keys->nhoff +=sizeof (*vlan); + keys->thoff +=sizeof (*vlan); } vlan =bpf_flow_dissect_get_header (skb,sizeof (*vlan), &_vlan);if (!vlan)return BPF_DROP ; - keys->nhoff +=sizeof (*vlan); + keys->thoff +=sizeof (*vlan);/* Only allow 8021AD + 8021Q double tagging and no triple tagging.*/ if (vlan->h_vlan_encapsulated_proto ==bpf_htons (ETH_P_8021AD ) || vlan->h_vlan_encapsulated_proto ==bpf_htons (ETH_P_8021Q ))
Signature · 1 line
2.20.0.rc1.387.gf8505762e3-goog
Re: [PATCH bpf-next v2 3/5] selftests/bpf: use thoff instead of nhoff in BPF flow dissector
Quoted 21 lines · Stanislav Fomichev
We are returning thoff from the flow dissector, not the nhoff. Pass thoff along with nhoff to the bpf program (initially thoff == nhoff) and expect flow dissector amend/return thoff, not nhoff.
This avoids confusion, when by the time bpf flow dissector exits, nhoff == thoff, which doesn't make much sense (this is relevant in the context of the next patch, where I add simple selftest and manually construct expected
flow_keys).Signed-off-by Stanislav Fomichev <[email protected]>--- net/core/flow_dissector.c | 1 + tools/testing/selftests/bpf/bpf_flow.c | 36 ++++++++++++-------------- 2 files changed, 18 insertions(+), 19 deletions(-)net/core/flow_dissector.c +0
-0
net/core/flow_dissector.c bool __skb_flow_bpf_dissect(struct bpf_prog *prog, · line 711
Quoted 4 lines
net/core/flow_dissector.c +1
-0@@ -708,6 +708,7 @@ bool __skb_flow_bpf_dissect(struct bpf_prog *prog,memset (flow_keys,0 ,sizeof (*flow_keys)); cb->qdisc_cb.flow_keys = flow_keys; flow_keys->nhoff =skb_network_offset (skb); + flow_keys->thoff = flow_keys->nhoff;
Do we need this fix without this set? If yes, do we need it for bpf tree as well?
Thanks,
Song
Quoted 140 lines
net/core/flow_dissector.c +0
-0bpf_compute_data_pointers ((struct sk_buff *)skb); result =BPF_PROG_RUN (prog, skb);tools/testing/selftests/bpf/bpf_flow.c +17
-20@@ -70,18 +70,18 @@ static __always_inline void *bpf_flow_dissect_get_header(struct __sk_buff *skb, {void *data_end = (void *)(long )skb->data_end;void *data = (void *)(long )skb->data; -__u16 nhoff = skb->flow_keys->nhoff; +__u16 thoff = skb->flow_keys->thoff;__u8 *hdr;/* Verifies this variable offset does not overflow */ -if (nhoff > (USHRT_MAX - hdr_size)) +if (thoff > (USHRT_MAX - hdr_size))return NULL ; - hdr = data + nhoff; + hdr = data + thoff;if (hdr + hdr_size <= data_end)return hdr; -if (bpf_skb_load_bytes (skb, nhoff, buffer, hdr_size)) +if (bpf_skb_load_bytes (skb, thoff, buffer, hdr_size))return NULL ;return buffer;@@ -158,13 +158,13 @@ static __always_inline int parse_ip_proto(struct __sk_buff *skb, __u8 proto)/* Only inspect standard GRE packets with version 0 */ return BPF_OK ; - keys->nhoff +=sizeof (*gre);/* Step over GRE Flags and Proto */ + keys->thoff +=sizeof (*gre);/* Step over GRE Flags and Proto */ if (GRE_IS_CSUM (gre->flags)) - keys->nhoff +=4 ;/* Step over chksum and Padding */ + keys->thoff +=4 ;/* Step over chksum and Padding */ if (GRE_IS_KEY (gre->flags)) - keys->nhoff +=4 ;/* Step over key */ + keys->thoff +=4 ;/* Step over key */ if (GRE_IS_SEQ (gre->flags)) - keys->nhoff +=4 ;/* Step over sequence number */ + keys->thoff +=4 ;/* Step over sequence number */ keys->is_encap = true;@@ -174,7 +174,7 @@ static __always_inline int parse_ip_proto(struct __sk_buff *skb, __u8 proto)if (!eth)return BPF_DROP ; - keys->nhoff +=sizeof (*eth); + keys->thoff +=sizeof (*eth);return parse_eth_proto (skb, eth->h_proto); }else {@@ -191,7 +191,6 @@ static __always_inline int parse_ip_proto(struct __sk_buff *skb, __u8 proto)if ((__u8 *)tcp + (tcp->doff <<2 ) > data_end)return BPF_DROP ; - keys->thoff = keys->nhoff; keys->sport = tcp->source; keys->dport = tcp->dest;return BPF_OK ;@@ -201,7 +200,6 @@ static __always_inline int parse_ip_proto(struct __sk_buff *skb, __u8 proto)if (!udp)return BPF_DROP ; - keys->thoff = keys->nhoff; keys->sport = udp->source; keys->dport = udp->dest;return BPF_OK ;@@ -252,8 +250,8 @@ PROG(IP)(struct __sk_buff *skb) keys->ipv4_src = iph->saddr; keys->ipv4_dst = iph->daddr; - keys->nhoff += iph->ihl <<2 ; -if (data + keys->nhoff > data_end) + keys->thoff += iph->ihl <<2 ; +if (data + keys->thoff > data_end)return BPF_DROP ;if (iph->frag_off &bpf_htons (IP_MF |IP_OFFSET )) {@@ -285,7 +283,7 @@ PROG(IPV6)(struct __sk_buff *skb) keys->addr_proto =ETH_P_IPV6 ;memcpy (&keys->ipv6_src, &ip6h->saddr,2 *sizeof (ip6h->saddr)); - keys->nhoff +=sizeof (struct ipv6hdr ); + keys->thoff +=sizeof (struct ipv6hdr );return parse_ipv6_proto (skb, ip6h->nexthdr); }@@ -301,7 +299,7 @@ PROG(IPV6OP)(struct __sk_buff *skb)/* hlen is in 8-octets and does not include the first 8 bytes * of the header */ - skb->flow_keys->nhoff += (1 + ip6h->hdrlen) <<3 ; + skb->flow_keys->thoff += (1 + ip6h->hdrlen) <<3 ;return parse_ipv6_proto (skb, ip6h->nexthdr); }@@ -315,7 +313,7 @@ PROG(IPV6FR)(struct __sk_buff *skb)if (!fragh)return BPF_DROP ; - keys->nhoff +=sizeof (*fragh); + keys->thoff +=sizeof (*fragh); keys->is_frag = true;if (!(fragh->frag_off &bpf_htons (IP6_OFFSET ))) keys->is_first_frag = true;@@ -341,7 +339,7 @@ PROG(VLAN)(struct __sk_buff *skb)__be16 proto;/* Peek back to see if single or double-tagging */ -if (bpf_skb_load_bytes (skb, keys->nhoff -sizeof (proto), &proto, +if (bpf_skb_load_bytes (skb, keys->thoff -sizeof (proto), &proto,sizeof (proto)))return BPF_DROP ;@@ -354,14 +352,14 @@ PROG(VLAN)(struct __sk_buff *skb)if (vlan->h_vlan_encapsulated_proto !=bpf_htons (ETH_P_8021Q ))return BPF_DROP ; - keys->nhoff +=sizeof (*vlan); + keys->thoff +=sizeof (*vlan); } vlan =bpf_flow_dissect_get_header (skb,sizeof (*vlan), &_vlan);if (!vlan)return BPF_DROP ; - keys->nhoff +=sizeof (*vlan); + keys->thoff +=sizeof (*vlan);/* Only allow 8021AD + 8021Q double tagging and no triple tagging.*/ if (vlan->h_vlan_encapsulated_proto ==bpf_htons (ETH_P_8021AD ) || vlan->h_vlan_encapsulated_proto ==bpf_htons (ETH_P_8021Q )) --2.20.0.rc1.387.gf8505762e3-goog
Stanislav Fomichev
reviewerRe: [PATCH bpf-next v2 3/5] selftests/bpf: use thoff instead of nhoff in BPF flow dissector
Quoted 17 lines · Song Liu
Quoted 16 lines · Stanislav Fomichev
We are returning thoff from the flow dissector, not the nhoff. Pass thoff along with nhoff to the bpf program (initially thoff == nhoff) and expect flow dissector amend/return thoff, not nhoff.
This avoids confusion, when by the time bpf flow dissector exits, nhoff == thoff, which doesn't make much sense (this is relevant in the context of the next patch, where I add simple selftest and manually construct expected
flow_keys).Signed-off-by Stanislav Fomichev <[email protected]>--- net/core/flow_dissector.c | 1 + tools/testing/selftests/bpf/bpf_flow.c | 36 ++++++++++++-------------- 2 files changed, 18 insertions(+), 19 deletions(-)
Quoted 12 lines
net/core/flow_dissector.c bool __skb_flow_bpf_dissect(struct bpf_prog *prog, · line 711
Quoted 9 lines
net/core/flow_dissector.c +1
-0@@ -708,6 +708,7 @@ bool __skb_flow_bpf_dissect(struct bpf_prog *prog,memset (flow_keys,0 ,sizeof (*flow_keys)); cb->qdisc_cb.flow_keys = flow_keys; flow_keys->nhoff =skb_network_offset (skb); + flow_keys->thoff = flow_keys->nhoff;Do we need this fix without this set? If yes, do we need it for bpf tree as well?
No, I don't think so. This just changes input to the flow dissector slightly (going forward).
It used to be nhoff in, thoff out. Now it's thoff in (with nhoff for backwards compatibility) and thoff out.
Quoted 144 lines
Thanks,
SongQuoted 140 lines
net/core/flow_dissector.c +0
-0bpf_compute_data_pointers ((struct sk_buff *)skb); result =BPF_PROG_RUN (prog, skb);tools/testing/selftests/bpf/bpf_flow.c +17
-20@@ -70,18 +70,18 @@ static __always_inline void *bpf_flow_dissect_get_header(struct __sk_buff *skb, {void *data_end = (void *)(long )skb->data_end;void *data = (void *)(long )skb->data; -__u16 nhoff = skb->flow_keys->nhoff; +__u16 thoff = skb->flow_keys->thoff;__u8 *hdr;/* Verifies this variable offset does not overflow */ -if (nhoff > (USHRT_MAX - hdr_size)) +if (thoff > (USHRT_MAX - hdr_size))return NULL ; - hdr = data + nhoff; + hdr = data + thoff;if (hdr + hdr_size <= data_end)return hdr; -if (bpf_skb_load_bytes (skb, nhoff, buffer, hdr_size)) +if (bpf_skb_load_bytes (skb, thoff, buffer, hdr_size))return NULL ;return buffer;@@ -158,13 +158,13 @@ static __always_inline int parse_ip_proto(struct __sk_buff *skb, __u8 proto)/* Only inspect standard GRE packets with version 0 */ return BPF_OK ; - keys->nhoff +=sizeof (*gre);/* Step over GRE Flags and Proto */ + keys->thoff +=sizeof (*gre);/* Step over GRE Flags and Proto */ if (GRE_IS_CSUM (gre->flags)) - keys->nhoff +=4 ;/* Step over chksum and Padding */ + keys->thoff +=4 ;/* Step over chksum and Padding */ if (GRE_IS_KEY (gre->flags)) - keys->nhoff +=4 ;/* Step over key */ + keys->thoff +=4 ;/* Step over key */ if (GRE_IS_SEQ (gre->flags)) - keys->nhoff +=4 ;/* Step over sequence number */ + keys->thoff +=4 ;/* Step over sequence number */ keys->is_encap = true;@@ -174,7 +174,7 @@ static __always_inline int parse_ip_proto(struct __sk_buff *skb, __u8 proto)if (!eth)return BPF_DROP ; - keys->nhoff +=sizeof (*eth); + keys->thoff +=sizeof (*eth);return parse_eth_proto (skb, eth->h_proto); }else {@@ -191,7 +191,6 @@ static __always_inline int parse_ip_proto(struct __sk_buff *skb, __u8 proto)if ((__u8 *)tcp + (tcp->doff <<2 ) > data_end)return BPF_DROP ; - keys->thoff = keys->nhoff; keys->sport = tcp->source; keys->dport = tcp->dest;return BPF_OK ;@@ -201,7 +200,6 @@ static __always_inline int parse_ip_proto(struct __sk_buff *skb, __u8 proto)if (!udp)return BPF_DROP ; - keys->thoff = keys->nhoff; keys->sport = udp->source; keys->dport = udp->dest;return BPF_OK ;@@ -252,8 +250,8 @@ PROG(IP)(struct __sk_buff *skb) keys->ipv4_src = iph->saddr; keys->ipv4_dst = iph->daddr; - keys->nhoff += iph->ihl <<2 ; -if (data + keys->nhoff > data_end) + keys->thoff += iph->ihl <<2 ; +if (data + keys->thoff > data_end)return BPF_DROP ;if (iph->frag_off &bpf_htons (IP_MF |IP_OFFSET )) {@@ -285,7 +283,7 @@ PROG(IPV6)(struct __sk_buff *skb) keys->addr_proto =ETH_P_IPV6 ;memcpy (&keys->ipv6_src, &ip6h->saddr,2 *sizeof (ip6h->saddr)); - keys->nhoff +=sizeof (struct ipv6hdr ); + keys->thoff +=sizeof (struct ipv6hdr );return parse_ipv6_proto (skb, ip6h->nexthdr); }@@ -301,7 +299,7 @@ PROG(IPV6OP)(struct __sk_buff *skb)/* hlen is in 8-octets and does not include the first 8 bytes * of the header */ - skb->flow_keys->nhoff += (1 + ip6h->hdrlen) <<3 ; + skb->flow_keys->thoff += (1 + ip6h->hdrlen) <<3 ;return parse_ipv6_proto (skb, ip6h->nexthdr); }@@ -315,7 +313,7 @@ PROG(IPV6FR)(struct __sk_buff *skb)if (!fragh)return BPF_DROP ; - keys->nhoff +=sizeof (*fragh); + keys->thoff +=sizeof (*fragh); keys->is_frag = true;if (!(fragh->frag_off &bpf_htons (IP6_OFFSET ))) keys->is_first_frag = true;@@ -341,7 +339,7 @@ PROG(VLAN)(struct __sk_buff *skb)__be16 proto;/* Peek back to see if single or double-tagging */ -if (bpf_skb_load_bytes (skb, keys->nhoff -sizeof (proto), &proto, +if (bpf_skb_load_bytes (skb, keys->thoff -sizeof (proto), &proto,sizeof (proto)))return BPF_DROP ;@@ -354,14 +352,14 @@ PROG(VLAN)(struct __sk_buff *skb)if (vlan->h_vlan_encapsulated_proto !=bpf_htons (ETH_P_8021Q ))return BPF_DROP ; - keys->nhoff +=sizeof (*vlan); + keys->thoff +=sizeof (*vlan); } vlan =bpf_flow_dissect_get_header (skb,sizeof (*vlan), &_vlan);if (!vlan)return BPF_DROP ; - keys->nhoff +=sizeof (*vlan); + keys->thoff +=sizeof (*vlan);/* Only allow 8021AD + 8021Q double tagging and no triple tagging.*/ if (vlan->h_vlan_encapsulated_proto ==bpf_htons (ETH_P_8021AD ) || vlan->h_vlan_encapsulated_proto ==bpf_htons (ETH_P_8021Q )) --2.20.0.rc1.387.gf8505762e3-goog
Song Liu
Acked-byRe: [PATCH bpf-next v2 3/5] selftests/bpf: use thoff instead of nhoff in BPF flow dissector
Quoted 31 lines · Stanislav Fomichev
Quoted 17 lines · Song Liu
Quoted 16 lines · Stanislav Fomichev
We are returning thoff from the flow dissector, not the nhoff. Pass thoff along with nhoff to the bpf program (initially thoff == nhoff) and expect flow dissector amend/return thoff, not nhoff.
This avoids confusion, when by the time bpf flow dissector exits, nhoff == thoff, which doesn't make much sense (this is relevant in the context of the next patch, where I add simple selftest and manually construct expected
flow_keys).Signed-off-by Stanislav Fomichev <[email protected]>--- net/core/flow_dissector.c | 1 + tools/testing/selftests/bpf/bpf_flow.c | 36 ++++++++++++-------------- 2 files changed, 18 insertions(+), 19 deletions(-)Quoted 12 lines
net/core/flow_dissector.c bool __skb_flow_bpf_dissect(struct bpf_prog *prog, · line 711
Quoted 9 lines
net/core/flow_dissector.c +1
-0@@ -708,6 +708,7 @@ bool __skb_flow_bpf_dissect(struct bpf_prog *prog,memset (flow_keys,0 ,sizeof (*flow_keys)); cb->qdisc_cb.flow_keys = flow_keys; flow_keys->nhoff =skb_network_offset (skb); + flow_keys->thoff = flow_keys->nhoff;Do we need this fix without this set? If yes, do we need it for bpf tree as well?
Quoted 4 lines
No, I don't think so. This just changes input to the flow dissector slightly (going forward).
It used to be nhoff in, thoff out. Now it's thoff in (with nhoff for backwards compatibility) and thoff out.
Thanks for the explanation.
Quoted 145 lines
Quoted 145 lines
Thanks,
SongQuoted 140 lines
net/core/flow_dissector.c +0
-0bpf_compute_data_pointers ((struct sk_buff *)skb); result =BPF_PROG_RUN (prog, skb);tools/testing/selftests/bpf/bpf_flow.c +17
-20@@ -70,18 +70,18 @@ static __always_inline void *bpf_flow_dissect_get_header(struct __sk_buff *skb, {void *data_end = (void *)(long )skb->data_end;void *data = (void *)(long )skb->data; -__u16 nhoff = skb->flow_keys->nhoff; +__u16 thoff = skb->flow_keys->thoff;__u8 *hdr;/* Verifies this variable offset does not overflow */ -if (nhoff > (USHRT_MAX - hdr_size)) +if (thoff > (USHRT_MAX - hdr_size))return NULL ; - hdr = data + nhoff; + hdr = data + thoff;if (hdr + hdr_size <= data_end)return hdr; -if (bpf_skb_load_bytes (skb, nhoff, buffer, hdr_size)) +if (bpf_skb_load_bytes (skb, thoff, buffer, hdr_size))return NULL ;return buffer;@@ -158,13 +158,13 @@ static __always_inline int parse_ip_proto(struct __sk_buff *skb, __u8 proto)/* Only inspect standard GRE packets with version 0 */ return BPF_OK ; - keys->nhoff +=sizeof (*gre);/* Step over GRE Flags and Proto */ + keys->thoff +=sizeof (*gre);/* Step over GRE Flags and Proto */ if (GRE_IS_CSUM (gre->flags)) - keys->nhoff +=4 ;/* Step over chksum and Padding */ + keys->thoff +=4 ;/* Step over chksum and Padding */ if (GRE_IS_KEY (gre->flags)) - keys->nhoff +=4 ;/* Step over key */ + keys->thoff +=4 ;/* Step over key */ if (GRE_IS_SEQ (gre->flags)) - keys->nhoff +=4 ;/* Step over sequence number */ + keys->thoff +=4 ;/* Step over sequence number */ keys->is_encap = true;@@ -174,7 +174,7 @@ static __always_inline int parse_ip_proto(struct __sk_buff *skb, __u8 proto)if (!eth)return BPF_DROP ; - keys->nhoff +=sizeof (*eth); + keys->thoff +=sizeof (*eth);return parse_eth_proto (skb, eth->h_proto); }else {@@ -191,7 +191,6 @@ static __always_inline int parse_ip_proto(struct __sk_buff *skb, __u8 proto)if ((__u8 *)tcp + (tcp->doff <<2 ) > data_end)return BPF_DROP ; - keys->thoff = keys->nhoff; keys->sport = tcp->source; keys->dport = tcp->dest;return BPF_OK ;@@ -201,7 +200,6 @@ static __always_inline int parse_ip_proto(struct __sk_buff *skb, __u8 proto)if (!udp)return BPF_DROP ; - keys->thoff = keys->nhoff; keys->sport = udp->source; keys->dport = udp->dest;return BPF_OK ;@@ -252,8 +250,8 @@ PROG(IP)(struct __sk_buff *skb) keys->ipv4_src = iph->saddr; keys->ipv4_dst = iph->daddr; - keys->nhoff += iph->ihl <<2 ; -if (data + keys->nhoff > data_end) + keys->thoff += iph->ihl <<2 ; +if (data + keys->thoff > data_end)return BPF_DROP ;if (iph->frag_off &bpf_htons (IP_MF |IP_OFFSET )) {@@ -285,7 +283,7 @@ PROG(IPV6)(struct __sk_buff *skb) keys->addr_proto =ETH_P_IPV6 ;memcpy (&keys->ipv6_src, &ip6h->saddr,2 *sizeof (ip6h->saddr)); - keys->nhoff +=sizeof (struct ipv6hdr ); + keys->thoff +=sizeof (struct ipv6hdr );return parse_ipv6_proto (skb, ip6h->nexthdr); }@@ -301,7 +299,7 @@ PROG(IPV6OP)(struct __sk_buff *skb)/* hlen is in 8-octets and does not include the first 8 bytes * of the header */ - skb->flow_keys->nhoff += (1 + ip6h->hdrlen) <<3 ; + skb->flow_keys->thoff += (1 + ip6h->hdrlen) <<3 ;return parse_ipv6_proto (skb, ip6h->nexthdr); }@@ -315,7 +313,7 @@ PROG(IPV6FR)(struct __sk_buff *skb)if (!fragh)return BPF_DROP ; - keys->nhoff +=sizeof (*fragh); + keys->thoff +=sizeof (*fragh); keys->is_frag = true;if (!(fragh->frag_off &bpf_htons (IP6_OFFSET ))) keys->is_first_frag = true;@@ -341,7 +339,7 @@ PROG(VLAN)(struct __sk_buff *skb)__be16 proto;/* Peek back to see if single or double-tagging */ -if (bpf_skb_load_bytes (skb, keys->nhoff -sizeof (proto), &proto, +if (bpf_skb_load_bytes (skb, keys->thoff -sizeof (proto), &proto,sizeof (proto)))return BPF_DROP ;@@ -354,14 +352,14 @@ PROG(VLAN)(struct __sk_buff *skb)if (vlan->h_vlan_encapsulated_proto !=bpf_htons (ETH_P_8021Q ))return BPF_DROP ; - keys->nhoff +=sizeof (*vlan); + keys->thoff +=sizeof (*vlan); } vlan =bpf_flow_dissect_get_header (skb,sizeof (*vlan), &_vlan);if (!vlan)return BPF_DROP ; - keys->nhoff +=sizeof (*vlan); + keys->thoff +=sizeof (*vlan);/* Only allow 8021AD + 8021Q double tagging and no triple tagging.*/ if (vlan->h_vlan_encapsulated_proto ==bpf_htons (ETH_P_8021AD ) || vlan->h_vlan_encapsulated_proto ==bpf_htons (ETH_P_8021Q )) --2.20.0.rc1.387.gf8505762e3-goog
Re: [PATCH bpf-next v2 3/5] selftests/bpf: use thoff instead of nhoff in BPF flow dissector
In reply to Stanislav FomichevQuoted 30 lines · Stanislav Fomichev
Quoted 17 lines · Song Liu
Quoted 16 lines · Stanislav Fomichev
We are returning thoff from the flow dissector, not the nhoff. Pass thoff along with nhoff to the bpf program (initially thoff == nhoff) and expect flow dissector amend/return thoff, not nhoff.
This avoids confusion, when by the time bpf flow dissector exits, nhoff == thoff, which doesn't make much sense (this is relevant in the context of the next patch, where I add simple selftest and manually construct expected
flow_keys).Signed-off-by Stanislav Fomichev <[email protected]>--- net/core/flow_dissector.c | 1 + tools/testing/selftests/bpf/bpf_flow.c | 36 ++++++++++++-------------- 2 files changed, 18 insertions(+), 19 deletions(-)Quoted 12 lines
net/core/flow_dissector.c bool __skb_flow_bpf_dissect(struct bpf_prog *prog, · line 711
Quoted 9 lines
net/core/flow_dissector.c +1
-0@@ -708,6 +708,7 @@ bool __skb_flow_bpf_dissect(struct bpf_prog *prog,memset (flow_keys,0 ,sizeof (*flow_keys)); cb->qdisc_cb.flow_keys = flow_keys; flow_keys->nhoff =skb_network_offset (skb); + flow_keys->thoff = flow_keys->nhoff;Do we need this fix without this set? If yes, do we need it for bpf tree as well?
Quoted 4 lines
No, I don't think so. This just changes input to the flow dissector slightly (going forward).
It used to be nhoff in, thoff out. Now it's thoff in (with nhoff for backwards compatibility) and thoff out.
That is still an api change.
Also patch 4 is a fix.
I think patches 3 and 4 need to go into bpf tree first. Then wait for them to merge into bpf-next and resubmit the rest.
PATCH v2 4/5 net/flow_dissector: correctly cap nhoff and thoff in case of BPF
In reply to Stanislav FomichevWe want to make sure that the following condition holds:
0 <= nhoff <= thoff <= skb->len
BPF program can set out-of-bounds nhoff and thoff, which is dangerous, see recent commit d0c081b49137 ("flow_dissector: properly cap thoff field")'.
--- net/core/flow_dissector.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
net/core/flow_dissector.c +4 -2
@@ -716,6 +716,10 @@ bool __skb_flow_bpf_dissect(struct bpf_prog *prog,/* Restore state */ memcpy (cb, &cb_saved,sizeof (cb_saved)); + flow_keys->nhoff =clamp_t (u16, flow_keys->nhoff,0 , skb->len); + flow_keys->thoff =clamp_t (u16, flow_keys->thoff, + flow_keys->nhoff, skb->len); +return result ==BPF_OK ; }
@@ -808,8 +812,6 @@ bool __skb_flow_dissect(const struct sk_buff *skb, &flow_keys);__skb_flow_bpf_to_target (&flow_keys, flow_dissector, target_container); - key_control->thoff =min_t (u16, key_control->thoff, - skb->len);rcu_read_unlock ();return ret; }
Signature · 1 line
2.20.0.rc1.387.gf8505762e3-goog
Re: [PATCH bpf-next v2 4/5] net/flow_dissector: correctly cap nhoff and thoff in case of BPF
Quoted 37 lines · Stanislav Fomichev
We want to make sure that the following condition holds:
0 <= nhoff <= thoff <=skb->lenBPF program can set out-of-bounds nhoff and thoff, which is dangerous, see recent commit
d0c081b49137("flow_dissector: properly cap thoff field")'.Signed-off-by Stanislav Fomichev <[email protected]>--- net/core/flow_dissector.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)net/core/flow_dissector.c +4
-3@@ -716,6 +716,10 @@ bool __skb_flow_bpf_dissect(struct bpf_prog *prog,/* Restore state */ memcpy (cb, &cb_saved,sizeof (cb_saved)); + flow_keys->nhoff =clamp_t (u16, flow_keys->nhoff,0 , skb->len); + flow_keys->thoff =clamp_t (u16, flow_keys->thoff, + flow_keys->nhoff, skb->len); +return result ==BPF_OK ; }@@ -808,8 +812,6 @@ bool __skb_flow_dissect(const struct sk_buff *skb, &flow_keys);__skb_flow_bpf_to_target (&flow_keys, flow_dissector, target_container); - key_control->thoff =min_t (u16, key_control->thoff, - skb->len);rcu_read_unlock ();return ret; } --
Quoted 2 lines
2.20.0.rc1.387.gf8505762e3-goog
Same question as 3/5:
Do we need this fix without this set? If yes, do we need it for bpf tree as well?
Thanks,
Song
Stanislav Fomichev
reviewerRe: [PATCH bpf-next v2 4/5] net/flow_dissector: correctly cap nhoff and thoff in case of BPF
Quoted 43 lines · Song Liu
Quoted 37 lines · Stanislav Fomichev
We want to make sure that the following condition holds:
0 <= nhoff <= thoff <=skb->lenBPF program can set out-of-bounds nhoff and thoff, which is dangerous, see recent commit
d0c081b49137("flow_dissector: properly cap thoff field")'.Signed-off-by Stanislav Fomichev <[email protected]>--- net/core/flow_dissector.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)net/core/flow_dissector.c +4
-3@@ -716,6 +716,10 @@ bool __skb_flow_bpf_dissect(struct bpf_prog *prog,/* Restore state */ memcpy (cb, &cb_saved,sizeof (cb_saved)); + flow_keys->nhoff =clamp_t (u16, flow_keys->nhoff,0 , skb->len); + flow_keys->thoff =clamp_t (u16, flow_keys->thoff, + flow_keys->nhoff, skb->len); +return result ==BPF_OK ; }@@ -808,8 +812,6 @@ bool __skb_flow_dissect(const struct sk_buff *skb, &flow_keys);__skb_flow_bpf_to_target (&flow_keys, flow_dissector, target_container); - key_control->thoff =min_t (u16, key_control->thoff, - skb->len);rcu_read_unlock ();return ret; } --Quoted 2 lines
2.20.0.rc1.387.gf8505762e3-goog
Same question as 3/5:
Quoted 2 lines
Do we need this fix without this set? If yes, do we need it for bpf tree as well?
No, for the older versions we do this capping when copying to key_control:
key_control->thoff = min_t(u16, key_control->thoff, skb->len);
I just moved this logic to the flow_keys and made it more approachable (use actual clamping with min/max boundary, not cryptic min).
I think my commit message might be confusing. There is no real issue here, it's done mostly for testing (so we see the result of clamping).
Quoted 3 lines
Thanks,
Song
PATCH v2 5/5 selftests/bpf: add simple BPF_PROG_TEST_RUN examples for flow dissector
In reply to Stanislav FomichevUse 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
@@ -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.0.rc1.387.gf8505762e3-goog
Quoted 44 lines · Stanislav Fomichev
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 `structbpf_flow_key' as an output. That way we can easily test flow dissector programs' behavior.
I've also modified existingtest_progs.ctest to do a simple flow dissector run as well.* first patch introduces new
__skb_flow_bpf_dissectto simplify sharing between__skb_flow_bpf_dissectandBPF_PROG_TEST_RUN
* second patch adds actualBPF_PROG_TEST_RUNsupport
* third patch converts BPF flow dissector to thoff
* forth patch correctly caps nhoff and thoff returned from bpf flow dissector
* fifth patch adds example usage to the selftestsv2 changes:
* new patch to use thoff instead of nhoff in bpf flow dissector
* new patch to correctly cap thoff for BPF case
* add missingmemset(flow_keys, 0, ...)to__skb_flow_bpf_dissect
* set test iterations to 10Stanislav Fomichev (5):
net/flow_dissector: move bpf case into__skb_flow_bpf_dissect
bpf: addBPF_PROG_TEST_RUNsupport for flow dissector selftests/bpf: use thoff instead of nhoff in BPF flow dissectornet/flow_dissector: correctly cap nhoff and thoff in case of BPF selftests/bpf: add simpleBPF_PROG_TEST_RUNexamples for flow
dissectorinclude/linux/bpf.h | 3 + include/linux/skbuff.h | 5 ++ net/bpf/test_run.c | 71 ++++++++++++++- net/core/filter.c | 1 + net/core/flow_dissector.c | 88 ++++++++++++------- tools/testing/selftests/bpf/Makefile | 3 + tools/testing/selftests/bpf/bpf_flow.c | 36 ++++---- .../selftests/bpf/flow_dissector_load.c | 43 ++------- .../selftests/bpf/flow_dissector_load.h | 55 ++++++++++++ tools/testing/selftests/bpf/test_progs.c | 78 +++++++++++++++- 10 files changed, 289 insertions(+), 94 deletions(-) create mode 100644 tools/testing/selftests/bpf/flow_dissector_load.h
Quoted 3 lines
--
2.20.0.rc1.387.gf8505762e3-goog
For the series:
Stanislav Fomichev
reviewerQuoted 45 lines · Song Liu
Quoted 44 lines · Stanislav Fomichev
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 `structbpf_flow_key' as an output. That way we can easily test flow dissector programs' behavior.
I've also modified existingtest_progs.ctest to do a simple flow dissector run as well.* first patch introduces new
__skb_flow_bpf_dissectto simplify sharing between__skb_flow_bpf_dissectandBPF_PROG_TEST_RUN
* second patch adds actualBPF_PROG_TEST_RUNsupport
* third patch converts BPF flow dissector to thoff
* forth patch correctly caps nhoff and thoff returned from bpf flow dissector
* fifth patch adds example usage to the selftestsv2 changes:
* new patch to use thoff instead of nhoff in bpf flow dissector
* new patch to correctly cap thoff for BPF case
* add missingmemset(flow_keys, 0, ...)to__skb_flow_bpf_dissect
* set test iterations to 10Stanislav Fomichev (5):
net/flow_dissector: move bpf case into__skb_flow_bpf_dissect
bpf: addBPF_PROG_TEST_RUNsupport for flow dissector selftests/bpf: use thoff instead of nhoff in BPF flow dissectornet/flow_dissector: correctly cap nhoff and thoff in case of BPF selftests/bpf: add simpleBPF_PROG_TEST_RUNexamples for flow
dissectorinclude/linux/bpf.h | 3 + include/linux/skbuff.h | 5 ++ net/bpf/test_run.c | 71 ++++++++++++++- net/core/filter.c | 1 + net/core/flow_dissector.c | 88 ++++++++++++------- tools/testing/selftests/bpf/Makefile | 3 + tools/testing/selftests/bpf/bpf_flow.c | 36 ++++---- .../selftests/bpf/flow_dissector_load.c | 43 ++------- .../selftests/bpf/flow_dissector_load.h | 55 ++++++++++++ tools/testing/selftests/bpf/test_progs.c | 78 +++++++++++++++- 10 files changed, 289 insertions(+), 94 deletions(-) create mode 100644 tools/testing/selftests/bpf/flow_dissector_load.h
Quoted 7 lines
Quoted 3 lines
--
2.20.0.rc1.387.gf8505762e3-googFor the series:
Acked-by Song Liu <[email protected]>
Thank you for a review!