bpf: add CHECKSUM_COMPLETE to bpf test progs
Subsystems (5)
PATCH v4 1/2 bpf: add CHECKSUM_COMPLETE to bpf test progs
Add special flag to validate that TC BPF program properly updates checksum information in skb.
---
v3 -> v4:
- use network header offset as starting point for checksum
- use folded checksum values to compare results
v2 -> v3:
- remove BIT() macro from uapi bpf.h
- change error code to EBADMSG
v1 -> v2:
- clean unused variable
--- include/uapi/linux/bpf.h | 2 ++ net/bpf/test_run.c | 28 +++++++++++++++++++++++++++- tools/include/uapi/linux/bpf.h | 2 ++ 3 files changed, 31 insertions(+), 1 deletion(-)
include/uapi/linux/bpf.h +2 -0
@@ -1425,6 +1425,8 @@ enum {#define BPF_F_TEST_RUN_ON_CPU (1U << 0)/* If set, XDP frames will be transmitted after processing */ #define BPF_F_TEST_XDP_LIVE_FRAMES (1U << 1) +/* If set, apply CHECKSUM_COMPLETE to skb and validate the checksum */ +#define BPF_F_TEST_SKB_CHECKSUM_COMPLETE (1U << 2)/* type for BPF_ENABLE_STATS */ enum bpf_stats_type {
net/bpf/test_run.c +27 -1
@@ -977,7 +977,8 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,void *data;int ret; -if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size) +if ((kattr->test.flags & ~BPF_F_TEST_SKB_CHECKSUM_COMPLETE ) || + kattr->test.cpu || kattr->test.batch_size)return -EINVAL ; data = bpf_test_init(kattr, kattr->test.data_size_in,
@@ -1025,6 +1026,7 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,skb_reserve (skb,NET_SKB_PAD +NET_IP_ALIGN );__skb_put (skb, size); +if (ctx && ctx->ifindex >1 ) { dev =dev_get_by_index (net, ctx->ifindex);if (!dev) {
@@ -1060,9 +1062,19 @@ 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); + ret =convert___skb_to_skb (skb, ctx);if (ret) goto out; + +if (kattr->test.flags &BPF_F_TEST_SKB_CHECKSUM_COMPLETE ) { +const int off =skb_network_offset (skb); +int len = skb->len - off; + + skb->csum =skb_checksum (skb, off, len,0 ); + skb->ip_summed =CHECKSUM_COMPLETE ; + } + ret =bpf_test_run (prog, skb, repeat, &retval, &duration, false);if (ret) goto out;
@@ -1077,6 +1089,20 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr, }memset (__skb_push (skb, hh_len),0 , hh_len); } + +if (kattr->test.flags &BPF_F_TEST_SKB_CHECKSUM_COMPLETE ) { +const int off =skb_network_offset (skb); +int len = skb->len - off; +__wsum csum; + + csum =skb_checksum (skb, off, len,0 ); + +if (csum_fold (skb->csum) !=csum_fold (csum)) { + ret = -EBADMSG ; + goto out; + } + } +convert_skb_to___skb (skb, ctx); size = skb->len;
tools/include/uapi/linux/bpf.h +2 -0
@@ -1425,6 +1425,8 @@ enum {#define BPF_F_TEST_RUN_ON_CPU (1U << 0)/* If set, XDP frames will be transmitted after processing */ #define BPF_F_TEST_XDP_LIVE_FRAMES (1U << 1) +/* If set, apply CHECKSUM_COMPLETE to skb and validate the checksum */ +#define BPF_F_TEST_SKB_CHECKSUM_COMPLETE (1U << 2)/* type for BPF_ENABLE_STATS */ enum bpf_stats_type {
Signature · 1 line
2.43.0
PATCH v4 2/2 selftests: bpf: validate CHECKSUM_COMPLETE option
Adjust skb program test to run with checksum validation.
--- .../selftests/bpf/prog_tests/test_skb_pkt_end.c | 1 + tools/testing/selftests/bpf/progs/skb_pkt_end.c | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/test_skb_pkt_end.c +1 -0
@@ -11,6 +11,7 @@ static int sanity_run(struct bpf_program *prog) .data_in = &pkt_v4, .data_size_in = sizeof(pkt_v4), .repeat = 1, + .flags =BPF_F_TEST_SKB_CHECKSUM_COMPLETE , ); prog_fd =bpf_program__fd (prog);
tools/testing/selftests/bpf/progs/skb_pkt_end.c +10 -1
@@ -33,6 +33,8 @@ int main_prog(struct __sk_buff *skb)struct iphdr *ip =NULL ;struct tcphdr *tcp;__u8 proto =0 ; +int urg_ptr; +u32 offset;if (!(ip =get_iphdr (skb))) goto out;
@@ -48,7 +50,14 @@ int main_prog(struct __sk_buff *skb)if (!tcp) goto out; -return tcp->urg_ptr; + urg_ptr = tcp->urg_ptr; + +/* Checksum validation part */ + proto++; + offset =sizeof (struct ethhdr ) + offsetof(struct iphdr , protocol); +bpf_skb_store_bytes (skb, offset, &proto,sizeof (proto),BPF_F_RECOMPUTE_CSUM ); + +return urg_ptr; out:return -1 ; }
Signature · 1 line
2.43.0
Quoted 6 lines · On Thu, 6 Jun 2024 07:58:50 -0700 Vadim Fedorenko wrote:
net/bpf/test_run.c +1
-0@@ -1060,9 +1062,19 @@ 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); + ret =convert___skb_to_skb (skb, ctx);
net/bpf/test_run.c int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr, · line 1076
Quoted 10 lines
net/bpf/test_run.c +8
-0if (ret) goto out; + +if (kattr->test.flags &BPF_F_TEST_SKB_CHECKSUM_COMPLETE ) { +const int off =skb_network_offset (skb); +int len = skb->len - off; + + skb->csum =skb_checksum (skb, off, len,0 ); + skb->ip_summed =CHECKSUM_COMPLETE ; + }
Looks good, overall, although I'd be tempted to place this before the L2 is pushed, a few lines up, so that we don't need to worry about network offset. Then again, with you approach there is a nice symmetry between the pre- and post- if blocks so either way is fine:
Quoted 23 lines · Jakub Kicinski
Quoted 6 lines · On Thu, 6 Jun 2024 07:58:50 -0700 Vadim Fedorenko wrote:
net/bpf/test_run.c +1
-0@@ -1060,9 +1062,19 @@ 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); + ret =convert___skb_to_skb (skb, ctx);net/bpf/test_run.c int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr, · line 1076
Quoted 10 lines
net/bpf/test_run.c +8
-0if (ret) goto out; + +if (kattr->test.flags &BPF_F_TEST_SKB_CHECKSUM_COMPLETE ) { +const int off =skb_network_offset (skb); +int len = skb->len - off; + + skb->csum =skb_checksum (skb, off, len,0 ); + skb->ip_summed =CHECKSUM_COMPLETE ; + }Looks good, overall, although I'd be tempted to place this before the L2 is pushed, a few lines up, so that we don't need to worry about network offset. Then again, with you approach there is a nice symmetry between the pre- and post- if blocks so either way is fine:
Quoted 1 line
Reviewed-by Jakub Kicinski <[email protected]>
Hi Daniel!
Could you please take a look and merge the series?
Thanks,
Vadim
Daniel Borkmann
maintainerQuoted 24 lines · Vadim Fedorenko
Quoted 23 lines · Jakub Kicinski
Quoted 6 lines · On Thu, 6 Jun 2024 07:58:50 -0700 Vadim Fedorenko wrote:
net/bpf/test_run.c +1
-0@@ -1060,9 +1062,19 @@ 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); + ret = convert___skb_to_skb(skb, ctx);net/bpf/test_run.c int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr, · line 1076
Quoted 10 lines
net/bpf/test_run.c +8
-0if (ret) goto out; + + if (kattr->test.flags & BPF_F_TEST_SKB_CHECKSUM_COMPLETE) { + const int off = skb_network_offset(skb); + int len = skb->len - off; + + skb->csum = skb_checksum(skb, off, len, 0); + skb->ip_summed = CHECKSUM_COMPLETE; + }Looks good, overall, although I'd be tempted to place this before the L2 is pushed, a few lines up, so that we don't need to worry about network offset. Then again, with you approach there is a nice symmetry between the pre- and post- if blocks so either way is fine:
Quoted 3 lines
Quoted 1 line
Reviewed-by Jakub Kicinski <[email protected]>Could you please take a look and merge the series?
Looks good, done now, thanks Vadim!
This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:
Quoted 14 lines · On Thu, 6 Jun 2024 07:58:50 -0700 you wrote:
Add special flag to validate that TC BPF program properly updates checksum information in skb.
Signed-off-by Vadim Fedorenko <[email protected]>---
v3 -> v4:
- use network header offset as starting point for checksum
- use folded checksum values to compare results
v2 -> v3:
- removeBIT()macro from uapibpf.h
- change error code to EBADMSG
v1 -> v2:
- clean unused variable
Quoted 1 line
[...]
Here is the summary with links:
- [bpf-next,v4,1/2] bpf: add CHECKSUM_COMPLETE to bpf test progs
https://git.kernel.org/bpf/bpf-next/c/a3cfe84cca28 - [bpf-next,v4,2/2] selftests: bpf: validate CHECKSUM_COMPLETE option https://git.kernel.org/bpf/bpf-next/c/041c1dc988fd
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html