Skip to contentSkip to conversation

selftests/bpf: Test bpf_skb_adjust_room on CHECKSUM_PARTIAL

PATCH Superseded Martin KaFai Lau 2 participants 3 messages Dec 21, 2022

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Martin KaFai Lau

author reviewer
#0 lore raw

PATCH v2 1/1 selftests/bpf: Test bpf_skb_adjust_room on CHECKSUM_PARTIAL

From: Martin KaFai Lau <[email protected]>

When the bpf_skb_adjust_room() shrinks the skb such that its csum_start is invalid, the skb->ip_summed should be reset from CHECKSUM_PARTIAL to CHECKSUM_NONE.

The commit 54c3f1a81421 ("bpf: pull before calling skb_postpull_rcsum()") fixed it.

This patch adds a test to ensure the skb->ip_summed changed from CHECKSUM_PARTIAL to CHECKSUM_NONE after bpf_skb_adjust_room().

---
v2: Add test to DENYLIST.s390x due to kfunc usage

 tools/testing/selftests/bpf/DENYLIST.s390x    |  1 +
 .../selftests/bpf/prog_tests/decap_sanity.c   | 83 +++++++++++++++++++
 .../selftests/bpf/progs/bpf_tracing_net.h     |  6 ++
 .../selftests/bpf/progs/decap_sanity.c        | 68 +++++++++++++++
 4 files changed, 158 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/decap_sanity.c
 create mode 100644 tools/testing/selftests/bpf/progs/decap_sanity.c
tools/testing/selftests/bpf/DENYLIST.s390x +1 -0
index 585fcf73c731..dba6c027920d 100644
@@ -86,3 +86,4 @@ xdp_bpf2bpf                              # failed to auto-attach program 'trace_
 xdp_do_redirect                          # prog_run_max_size unexpected error: -22 (errno 22)
 xdp_synproxy                             # JIT does not support calling kernel function                                (kfunc)
 xfrm_info                                # JIT does not support calling kernel function                                (kfunc)
+decap_sanity                             # JIT does not support calling kernel function                                (kfunc)
\ No newline at end of file
tools/testing/selftests/bpf/prog_tests/decap_sanity.c new file +83 -0

83 lines not on this page · raw message

tools/testing/selftests/bpf/progs/bpf_tracing_net.h +6 -0
index b394817126cf..cfed4df490f3 100644
@@ -50,6 +50,12 @@
 #define ICSK_TIME_LOSS_PROBE	5
 #define ICSK_TIME_REO_TIMEOUT	6
 
+#define ETH_HLEN		14
+#define ETH_P_IPV6		0x86DD
+
+#define CHECKSUM_NONE		0
+#define CHECKSUM_PARTIAL	3
+
 #define IFNAMSIZ		16
 
 #define RTF_GATEWAY		0x0002
tools/testing/selftests/bpf/progs/decap_sanity.c new file +68 -0

68 lines not on this page · raw message

Signature · 1 line
2.30.2
#1 lore raw
Quoted 31 lines · Martin KaFai Lau

From: Martin KaFai Lau <[email protected]>

When the bpf_skb_adjust_room() shrinks the skb such that its csum_start is invalid, the skb->ip_summed should be reset from CHECKSUM_PARTIAL to CHECKSUM_NONE.

The commit 54c3f1a81421 ("bpf: pull before calling skb_postpull_rcsum()") fixed it.

This patch adds a test to ensure the skb->ip_summed changed from CHECKSUM_PARTIAL to CHECKSUM_NONE after bpf_skb_adjust_room().

---
v2: Add test to DENYLIST.s390x due to kfunc usage

 tools/testing/selftests/bpf/DENYLIST.s390x    |  1 +
 .../selftests/bpf/prog_tests/decap_sanity.c   | 83 +++++++++++++++++++
 .../selftests/bpf/progs/bpf_tracing_net.h     |  6 ++
 .../selftests/bpf/progs/decap_sanity.c        | 68 +++++++++++++++
 4 files changed, 158 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/decap_sanity.c
 create mode 100644 tools/testing/selftests/bpf/progs/decap_sanity.c
tools/testing/selftests/bpf/DENYLIST.s390x +0 -0
index 585fcf73c731..dba6c027920d 100644

tools/testing/selftests/bpf/DENYLIST.s390x xdp_bpf2bpf # failed to auto-attach program 'trace_ · line 89

Quoted 4 lines
tools/testing/selftests/bpf/DENYLIST.s390x +1 -0
@@ -86,3 +86,4 @@ xdp_bpf2bpf                              # failed to auto-attach program 'trace_
 xdp_do_redirect                          # prog_run_max_size unexpected error: -22 (errno 22)
 xdp_synproxy                             # JIT does not support calling kernel function                                (kfunc)
 xfrm_info                                # JIT does not support calling kernel function                                (kfunc)
+decap_sanity                             # JIT does not support calling kernel function                                (kfunc)

let's keep this list sorted?

Quoted 51 lines

\ No newline at end of file

tools/testing/selftests/bpf/prog_tests/decap_sanity.c new file +44 -0
new file mode 100644
index 000000000000..2fbb3017b740
@@ -0,0 +1,83 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <net/if.h>
+#include <linux/in6.h>
+
+#include "test_progs.h"
+#include "network_helpers.h"
+#include "decap_sanity.skel.h"
+
+#define SYS(fmt, ...)                                          \
+       ({                                                      \
+               char cmd[1024];                                 \
+               snprintf(cmd, sizeof(cmd), fmt, ##__VA_ARGS__); \
+               if (!ASSERT_OK(system(cmd), cmd))               \
+                       goto fail;                              \
+       })
+
+#define NS_TEST "decap_sanity_ns"
+#define IPV6_IFACE_ADDR "face::1"
+#define UDP_TEST_PORT 7777
+
+void test_decap_sanity(void)
+{
+       LIBBPF_OPTS(bpf_tc_hook, qdisc_hook, .attach_point = BPF_TC_EGRESS);
+       LIBBPF_OPTS(bpf_tc_opts, tc_attach);
+       struct nstoken *nstoken = NULL;
+       struct decap_sanity *skel;
+       struct sockaddr_in6 addr;
+       socklen_t addrlen;
+       char buf[128] = {};
+       int sockfd, err;
+
+       skel = decap_sanity__open_and_load();
+       if (!ASSERT_OK_PTR(skel, "skel open_and_load"))
+               return;
+
+       SYS("ip netns add %s", NS_TEST);
+       SYS("ip -net %s -6 addr add %s/128 dev lo nodad", NS_TEST, IPV6_IFACE_ADDR);
+       SYS("ip -net %s link set dev lo up", NS_TEST);
+
+       nstoken = open_netns(NS_TEST);

tools/testing/selftests/bpf/prog_tests/decap_sanity.c line 54

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/decap_sanity.c new file +10 -0
+       if (!ASSERT_OK_PTR(nstoken, "open_netns"))
+               goto fail;
+
+       qdisc_hook.ifindex = if_nametoindex("lo");
+       if (!ASSERT_GT(qdisc_hook.ifindex, 0, "if_nametoindex lo"))
+               goto fail;
+
+       err = bpf_tc_hook_create(&qdisc_hook);
+       if (!ASSERT_OK(err, "create qdisc hook"))
+               goto fail;

you seem to be missing bpf_tc_hook_destroy() for clean up

Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/decap_sanity.c new file +12 -0
+
+       tc_attach.prog_fd = bpf_program__fd(skel->progs.decap_sanity);
+       err = bpf_tc_attach(&qdisc_hook, &tc_attach);
+       if (!ASSERT_OK(err, "attach filter"))
+               goto fail;
+
+       addrlen = sizeof(addr);
+       err = make_sockaddr(AF_INET6, IPV6_IFACE_ADDR, UDP_TEST_PORT,
+                           (void *)&addr, &addrlen);
+       if (!ASSERT_OK(err, "make_sockaddr"))
+               goto fail;
+       sockfd = socket(AF_INET6, SOCK_DGRAM, 0);

tools/testing/selftests/bpf/prog_tests/decap_sanity.c line 76

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/decap_sanity.c new file +10 -0
+       if (!ASSERT_NEQ(sockfd, -1, "socket"))
+               goto fail;
+       err = sendto(sockfd, buf, sizeof(buf), 0, (void *)&addr, addrlen);
+       close(sockfd);
+       if (!ASSERT_EQ(err, sizeof(buf), "send"))
+               goto fail;
+
+       ASSERT_EQ(skel->bss->init_csum_partial, true, "init_csum_partial");
+       ASSERT_EQ(skel->bss->final_csum_none, true, "final_csum_none");
+       ASSERT_EQ(skel->bss->broken_csum_start, false, "broken_csum_start");

ASSERT_TRUE and ASSERT_FALSE ?

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/decap_sanity.c new file +7 -0
+
+fail:
+       if (nstoken)
+               close_netns(nstoken);
+       system("ip netns del " NS_TEST " >& /dev/null");
+       decap_sanity__destroy(skel);
+}

[...]

author reviewer
#2 lore raw
Quoted 27 lines · Andrii Nakryiko
Quoted 26 lines · Martin KaFai Lau

From: Martin KaFai Lau <[email protected]>

When the bpf_skb_adjust_room() shrinks the skb such that its csum_start is invalid, the skb->ip_summed should be reset from CHECKSUM_PARTIAL to CHECKSUM_NONE.

The commit 54c3f1a81421 ("bpf: pull before calling skb_postpull_rcsum()") fixed it.

This patch adds a test to ensure the skb->ip_summed changed from CHECKSUM_PARTIAL to CHECKSUM_NONE after bpf_skb_adjust_room().

---
v2: Add test to DENYLIST.s390x due to kfunc usage

  tools/testing/selftests/bpf/DENYLIST.s390x    |  1 +
  .../selftests/bpf/prog_tests/decap_sanity.c   | 83 +++++++++++++++++++
  .../selftests/bpf/progs/bpf_tracing_net.h     |  6 ++
  .../selftests/bpf/progs/decap_sanity.c        | 68 +++++++++++++++
  4 files changed, 158 insertions(+)

create mode 100644 tools/testing/selftests/bpf/prog_tests/decap_sanity.c create mode 100644 tools/testing/selftests/bpf/progs/decap_sanity.c

Quoted 11 lines

tools/testing/selftests/bpf/DENYLIST.s390x xdp_bpf2bpf # failed to auto-attach program 'trace_ · line 89

Quoted 9 lines
tools/testing/selftests/bpf/DENYLIST.s390x +1 -0
index 585fcf73c731..dba6c027920d 100644
@@ -86,3 +86,4 @@ xdp_bpf2bpf                              # failed to auto-attach program 'trace_
  xdp_do_redirect                          # prog_run_max_size unexpected error: -22 (errno 22)
  xdp_synproxy                             # JIT does not support calling kernel function                                (kfunc)
  xfrm_info                                # JIT does not support calling kernel function                                (kfunc)
+decap_sanity                             # JIT does not support calling kernel function                                (kfunc)

let's keep this list sorted?

Ack.

Quoted 56 lines
Quoted 56 lines

\ No newline at end of file

tools/testing/selftests/bpf/prog_tests/decap_sanity.c new file +48 -0
new file mode 100644
index 000000000000..2fbb3017b740
@@ -0,0 +1,83 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <net/if.h>
+#include <linux/in6.h>
+
+#include "test_progs.h"
+#include "network_helpers.h"
+#include "decap_sanity.skel.h"
+
+#define SYS(fmt, ...)                                          \
+       ({                                                      \
+               char cmd[1024];                                 \
+               snprintf(cmd, sizeof(cmd), fmt, ##__VA_ARGS__); \
+               if (!ASSERT_OK(system(cmd), cmd))               \
+                       goto fail;                              \
+       })
+
+#define NS_TEST "decap_sanity_ns"
+#define IPV6_IFACE_ADDR "face::1"
+#define UDP_TEST_PORT 7777
+
+void test_decap_sanity(void)
+{
+       LIBBPF_OPTS(bpf_tc_hook, qdisc_hook, .attach_point = BPF_TC_EGRESS);
+       LIBBPF_OPTS(bpf_tc_opts, tc_attach);
+       struct nstoken *nstoken = NULL;
+       struct decap_sanity *skel;
+       struct sockaddr_in6 addr;
+       socklen_t addrlen;
+       char buf[128] = {};
+       int sockfd, err;
+
+       skel = decap_sanity__open_and_load();
+       if (!ASSERT_OK_PTR(skel, "skel open_and_load"))
+               return;
+
+       SYS("ip netns add %s", NS_TEST);
+       SYS("ip -net %s -6 addr add %s/128 dev lo nodad", NS_TEST, IPV6_IFACE_ADDR);
+       SYS("ip -net %s link set dev lo up", NS_TEST);
+
+       nstoken = open_netns(NS_TEST);
+       if (!ASSERT_OK_PTR(nstoken, "open_netns"))
+               goto fail;
+
+       qdisc_hook.ifindex = if_nametoindex("lo");
Quoted 8 lines

tools/testing/selftests/bpf/prog_tests/decap_sanity.c line 54

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/decap_sanity.c new file +6 -0
+       if (!ASSERT_GT(qdisc_hook.ifindex, 0, "if_nametoindex lo"))
+               goto fail;
+
+       err = bpf_tc_hook_create(&qdisc_hook);
+       if (!ASSERT_OK(err, "create qdisc hook"))
+               goto fail;

you seem to be missing bpf_tc_hook_destroy() for clean up

It will go away with the 'ip netns del' at the end of the test, so not needed. I will add a comment at the end of this function to make it clear.

Quoted 17 lines
Quoted 17 lines
tools/testing/selftests/bpf/prog_tests/decap_sanity.c new file +16 -0
+
+       tc_attach.prog_fd = bpf_program__fd(skel->progs.decap_sanity);
+       err = bpf_tc_attach(&qdisc_hook, &tc_attach);
+       if (!ASSERT_OK(err, "attach filter"))
+               goto fail;
+
+       addrlen = sizeof(addr);
+       err = make_sockaddr(AF_INET6, IPV6_IFACE_ADDR, UDP_TEST_PORT,
+                           (void *)&addr, &addrlen);
+       if (!ASSERT_OK(err, "make_sockaddr"))
+               goto fail;
+       sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
+       if (!ASSERT_NEQ(sockfd, -1, "socket"))
+               goto fail;
+       err = sendto(sockfd, buf, sizeof(buf), 0, (void *)&addr, addrlen);
+       close(sockfd);
Quoted 8 lines

tools/testing/selftests/bpf/prog_tests/decap_sanity.c line 76

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/decap_sanity.c new file +6 -0
+       if (!ASSERT_EQ(err, sizeof(buf), "send"))
+               goto fail;
+
+       ASSERT_EQ(skel->bss->init_csum_partial, true, "init_csum_partial");
+       ASSERT_EQ(skel->bss->final_csum_none, true, "final_csum_none");
+       ASSERT_EQ(skel->bss->broken_csum_start, false, "broken_csum_start");

ASSERT_TRUE and ASSERT_FALSE ?

Ack.

Thanks for the review.

Quoted 10 lines
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/decap_sanity.c new file +7 -0
+
+fail:
+       if (nstoken)
+               close_netns(nstoken);
+       system("ip netns del " NS_TEST " >& /dev/null");
+       decap_sanity__destroy(skel);
+}

[...]