Skip to contentSkip to conversation

bpf: add CHECKSUM_COMPLETE to bpf test progs

PATCH Superseded Vadim Fedorenko 3 participants 5 messages May 24, 2024

Subsystems (5)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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.

---
 include/uapi/linux/bpf.h       |  2 ++
 net/bpf/test_run.c             | 17 ++++++++++++++++-
 tools/include/uapi/linux/bpf.h |  2 ++
 3 files changed, 20 insertions(+), 1 deletion(-)
include/uapi/linux/bpf.h +2 -0
index 90706a47f6ff..f7d458d88111 100644
@@ -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 +17 -1
index f6aad4ed2ab2..c6189bb9bf67 100644
@@ -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,12 @@ 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 (kattr->test.flags & BPF_F_TEST_SKB_CHECKSUM_COMPLETE) {
+		skb->csum = skb_checksum(skb, 0, skb->len, 0);
+		skb->ip_summed = CHECKSUM_COMPLETE;
+	}
+
 	if (ctx && ctx->ifindex > 1) {
 		dev = dev_get_by_index(net, ctx->ifindex);
 		if (!dev) {
@@ -1079,6 +1086,15 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
 	}
 	convert_skb_to___skb(skb, ctx);
 
+	if (kattr->test.flags & BPF_F_TEST_SKB_CHECKSUM_COMPLETE) {
+		__wsum csum = skb_checksum(skb, 0, skb->len, 0);
+
+		if (skb->csum != csum) {
+			ret = -EINVAL;
+			goto out;
+		}
+	}
+
 	size = skb->len;
 	/* bpf program can never convert linear skb to non-linear */
 	if (WARN_ON_ONCE(skb_is_nonlinear(skb)))
tools/include/uapi/linux/bpf.h +2 -0
index 90706a47f6ff..f7d458d88111 100644
@@ -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	BIT(2)
 
 /* type for BPF_ENABLE_STATS */
 enum bpf_stats_type {
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 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
index ae93411fd582..09ca13bdf6ca 100644
@@ -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
index db4abd2682fc..3bb4451524a1 100644
@@ -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
maintainer
#2 lore raw

Re: [PATCH bpf-next v2 2/2] selftests: bpf: validate CHECKSUM_COMPLETE option

Quoted 3 lines · Vadim Fedorenko

Adjust skb program test to run with checksum validation.

BPF CI complains :

[...]
/tmp/work/bpf/bpf/tools/testing/selftests/bpf/prog_tests/test_skb_pkt_end.c:14:12: error: call to undeclared function 'BIT'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
14 | .flags = BPF_F_TEST_SKB_CHECKSUM_COMPLETE,
| ^
/tmp/work/bpf/bpf/tools/include/uapi/linux/bpf.h:1429:42: note: expanded from macro 'BPF_F_TEST_SKB_CHECKSUM_COMPLETE'

   1429 | #define BPF_F_TEST_SKB_CHECKSUM_COMPLETE        BIT(2)
        |                                                 ^

1 error generated.
make: *** [Makefile:654: /tmp/work/bpf/bpf/tools/testing/selftests/bpf/test_skb_pkt_end.test.o] Error 1
make: *** Waiting for unfinished jobs....
make: Leaving directory '/tmp/work/bpf/bpf/tools/testing/selftests/bpf'
Error: Process completed with exit code 2.

#3 lore raw

Re: [PATCH bpf-next v2 2/2] selftests: bpf: validate CHECKSUM_COMPLETE option

Quoted 9 lines · Daniel Borkmann
Quoted 3 lines · Vadim Fedorenko

Adjust skb program test to run with checksum validation.

BPF CI complains :

  [...]

Quoted 13 lines

/tmp/work/bpf/bpf/tools/testing/selftests/bpf/prog_tests/test_skb_pkt_end.c:14:12: error: call to undeclared function 'BIT'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
     14 |                 .flags = BPF_F_TEST_SKB_CHECKSUM_COMPLETE,
        |                          ^
  /tmp/work/bpf/bpf/tools/include/uapi/linux/bpf.h:1429:42: note:
expanded from macro 'BPF_F_TEST_SKB_CHECKSUM_COMPLETE'
   1429 | #define BPF_F_TEST_SKB_CHECKSUM_COMPLETE        BIT(2)
        |                                                 ^
  1 error generated.
  make: *** [Makefile:654:
/tmp/work/bpf/bpf/tools/testing/selftests/bpf/test_skb_pkt_end.test.o] Error 1
  make: *** Waiting for unfinished jobs....
  make: Leaving directory '/tmp/work/bpf/bpf/tools/testing/selftests/bpf'
  Error: Process completed with exit code 2.

Oops, looks like checkpatch.pl was too smart and replaced original define with BIT() macro, but in one file only. I'll re-send it with fixes.

maintainer
#4 lore raw
In reply to Vadim Fedorenko
Quoted 51 lines · Vadim Fedorenko

Add special flag to validate that TC BPF program properly updates checksum information in skb.

---
  include/uapi/linux/bpf.h       |  2 ++
  net/bpf/test_run.c             | 17 ++++++++++++++++-
  tools/include/uapi/linux/bpf.h |  2 ++
  3 files changed, 20 insertions(+), 1 deletion(-)
include/uapi/linux/bpf.h +2 -0
index 90706a47f6ff..f7d458d88111 100644
@@ -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 +8 -1
index f6aad4ed2ab2..c6189bb9bf67 100644
@@ -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,12 @@ 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 (kattr->test.flags & BPF_F_TEST_SKB_CHECKSUM_COMPLETE) {
+		skb->csum = skb_checksum(skb, 0, skb->len, 0);
+		skb->ip_summed = CHECKSUM_COMPLETE;
+	}
+
  	if (ctx && ctx->ifindex > 1) {
  		dev = dev_get_by_index(net, ctx->ifindex);
  		if (!dev) {

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

Quoted 8 lines
net/bpf/test_run.c +5 -0
@@ -1079,6 +1086,15 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
  	}
  	convert_skb_to___skb(skb, ctx);
 
+	if (kattr->test.flags & BPF_F_TEST_SKB_CHECKSUM_COMPLETE) {
+		__wsum csum = skb_checksum(skb, 0, skb->len, 0);
+
+		if (skb->csum != csum) {
+			ret = -EINVAL;

This is very useful. Maybe just a nit that EINVAL seems too generic and some more concrete signal (e.g. EBADMSG) might be better. Do you also plan to add test infra for other types like CHECKSUM_PARTIAL?

Quoted 21 lines
net/bpf/test_run.c +4 -0
+			goto out;
+		}
+	}
+
  	size = skb->len;
  	/* bpf program can never convert linear skb to non-linear */
  	if (WARN_ON_ONCE(skb_is_nonlinear(skb)))
tools/include/uapi/linux/bpf.h +2 -0
index 90706a47f6ff..f7d458d88111 100644
@@ -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	BIT(2)
 
  /* type for BPF_ENABLE_STATS */
  enum bpf_stats_type {