Skip to contentSkip to conversation

bpf: add CHECKSUM_COMPLETE to bpf test progs

PATCH Superseded Vadim Fedorenko 3 participants 5 messages May 27, 2024 – Jun 6, 2024

Subsystems (5)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 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             | 18 +++++++++++++++++-
 tools/include/uapi/linux/bpf.h |  2 ++
 3 files changed, 21 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..4c21562ad526 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 = -EBADMSG;
+			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	(1U << 2)
 
 /* type for BPF_ENABLE_STATS */
 enum bpf_stats_type {
Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 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
#2 lore raw
In reply to Vadim Fedorenko
Quoted 71 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             | 18 +++++++++++++++++-
  tools/include/uapi/linux/bpf.h |  2 ++
  3 files changed, 21 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..4c21562ad526 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 = -EBADMSG;
+			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 +0 -0
index 90706a47f6ff..f7d458d88111 100644

tools/include/uapi/linux/bpf.h enum { · line 1432

Quoted 8 lines
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 {

Hi Daniel!

Have you had a chance to look at v3 of this patch? I think I addressed all your comments form v2.

Thanks,
Vadim

maintainer
#3 lore raw
Quoted 77 lines · Vadim Fedorenko
Quoted 66 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             | 18 +++++++++++++++++- tools/include/uapi/linux/bpf.h |  2 ++

  3 files changed, 21 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..4c21562ad526 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 = -EBADMSG;
+            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 +0 -0
index 90706a47f6ff..f7d458d88111 100644

tools/include/uapi/linux/bpf.h enum { · line 1431

Quoted 7 lines
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 {

Hi Daniel!

Quoted 2 lines

Have you had a chance to look at v3 of this patch? I think I addressed all your comments form v2.

Looks good, but I think there is something off given the test on arm64 and s390x fails in skb_pkt_end with EBADMSG. I wonder if we're missing a :

  skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);

right after the eth_type_trans()?

Thanks,
Daniel

#4 lore raw
Quoted 84 lines · Daniel Borkmann
Quoted 80 lines · Vadim Fedorenko
Quoted 69 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             | 18 +++++++++++++++++- tools/include/uapi/linux/bpf.h |  2 ++

  3 files changed, 21 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..4c21562ad526 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 = -EBADMSG;
+            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 +0 -0
index 90706a47f6ff..f7d458d88111 100644

tools/include/uapi/linux/bpf.h enum { · line 1431

Quoted 7 lines
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 {

Hi Daniel!

Quoted 2 lines

Have you had a chance to look at v3 of this patch? I think I addressed all your comments form v2.

Quoted 6 lines

Looks good, but I think there is something off given the test on arm64 and s390x fails in skb_pkt_end with EBADMSG. I wonder if we're missing a :

  skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);

right after the eth_type_trans()?

Oh, thanks for bringing it up. Looks like it's a bit more complex. Apparently, CHECKSUM_COMPLETE covers everything after the ethernet header. That's why the code has to calculate checksum after network and transport offsets are set. And for L2 case we have to skip ethernet header.

Another issue is that correct check should use folded checksums instead of raw values. I believe that was flagged by tests for other archs.

I'll do v4 soon and will be sure to have tests passing on all archs.

Thanks,
Vadim

Quoted 2 lines

Thanks,
Daniel