Skip to contentSkip to conversation

Add XDP support for bpf_load_hdr_opt

PATCH Superseded Joanne Koong 4 participants 9 messages Oct 1, 2021

Subsystems (7)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#1 lore raw

PATCH 1/3 bpf/xdp: Add bpf_load_hdr_opt support for xdp

In reply to Joanne Koong

This patch enables XDP programs to use the bpf_load_hdr_opt helper function to load header options.

The upper 16 bits of "flags" is used to denote the offset to the tcp header. No other flags are, at this time, used by XDP programs. In the future, more flags can be included to support other types of header options.

Much of the logic for loading header options can be shared between sockops and xdp programs. In net/core/filter.c, this common shared logic is refactored into a separate function both sockops and xdp use.

---
 include/uapi/linux/bpf.h       | 26 ++++++----
 net/core/filter.c              | 88 ++++++++++++++++++++++++++--------
 tools/include/uapi/linux/bpf.h | 26 ++++++----
 3 files changed, 103 insertions(+), 37 deletions(-)
include/uapi/linux/bpf.h +17 -9
index 6fc59d61937a..2a90e9e5088f 100644
@@ -4272,21 +4272,28 @@ union bpf_attr {
  *		A non-negative value equal to or less than *size* on success,
  *		or a negative error in case of failure.
  *
- * long bpf_load_hdr_opt(struct bpf_sock_ops *skops, void *searchby_res, u32 len, u64 flags)
+ * long bpf_load_hdr_opt(void *ctx, void *searchby_res, u32 len, u64 flags)
  *	Description
- *		Load header option.  Support reading a particular TCP header
- *		option for bpf program (**BPF_PROG_TYPE_SOCK_OPS**).
+ *		Load header option. Support reading a particular TCP header
+ *		option for bpf programs (**BPF_PROG_TYPE_SOCK_OPS** and
+ *		**BPF_PROG_TYPE_XDP**).
  *
- *		If *flags* is 0, it will search the option from the
- *		*skops*\ **->skb_data**.  The comment in **struct bpf_sock_ops**
- *		has details on what skb_data contains under different
- *		*skops*\ **->op**.
+ *		*ctx* is either **struct bpf_sock_ops** for SOCK_OPS programs or
+ *		**struct xdp_md** for XDP programs.
+ *
+ *		For SOCK_OPS programs, if *flags* is 0, it will search the option
+ *		from the *skops*\ **->skb_data**.  The comment in
+ *		**struct bpf_sock_ops** has details on what skb_data contains
+ *		under different *skops*\ **->op**.
+ *
+ *		For XDP programs, the upper 16 bits of **flags** should contain
+ *		the offset to the tcp header.
  *
  *		The first byte of the *searchby_res* specifies the
  *		kind that it wants to search.
  *
  *		If the searching kind is an experimental kind
- *		(i.e. 253 or 254 according to RFC6994).  It also
+ *		(i.e. 253 or 254 according to RFC6994), it also
  *		needs to specify the "magic" which is either
  *		2 bytes or 4 bytes.  It then also needs to
  *		specify the size of the magic by using
@@ -4309,7 +4316,7 @@ union bpf_attr {
  *		*len* must be at least 2 bytes which is the minimal size
  *		of a header option.
  *
- *		Supported flags:
+ *		Supported flags for **SOCK_OPS** programs:
  *
  *		* **BPF_LOAD_HDR_OPT_TCP_SYN** to search from the
  *		  saved_syn packet or the just-received syn packet.
@@ -6018,6 +6025,7 @@ enum {
 
 enum {
 	BPF_LOAD_HDR_OPT_TCP_SYN = (1ULL << 0),
+	BPF_LOAD_HDR_OPT_TCP_OFFSET_SHIFT = 48,
 };
 
 /* args[0] value during BPF_SOCK_OPS_HDR_OPT_LEN_CB and
net/core/filter.c +69 -19

120 lines not on this page · raw message

tools/include/uapi/linux/bpf.h +17 -9
index 6fc59d61937a..2a90e9e5088f 100644
@@ -4272,21 +4272,28 @@ union bpf_attr {
  *		A non-negative value equal to or less than *size* on success,
  *		or a negative error in case of failure.
  *
- * long bpf_load_hdr_opt(struct bpf_sock_ops *skops, void *searchby_res, u32 len, u64 flags)
+ * long bpf_load_hdr_opt(void *ctx, void *searchby_res, u32 len, u64 flags)
  *	Description
- *		Load header option.  Support reading a particular TCP header
- *		option for bpf program (**BPF_PROG_TYPE_SOCK_OPS**).
+ *		Load header option. Support reading a particular TCP header
+ *		option for bpf programs (**BPF_PROG_TYPE_SOCK_OPS** and
+ *		**BPF_PROG_TYPE_XDP**).
  *
- *		If *flags* is 0, it will search the option from the
- *		*skops*\ **->skb_data**.  The comment in **struct bpf_sock_ops**
- *		has details on what skb_data contains under different
- *		*skops*\ **->op**.
+ *		*ctx* is either **struct bpf_sock_ops** for SOCK_OPS programs or
+ *		**struct xdp_md** for XDP programs.
+ *
+ *		For SOCK_OPS programs, if *flags* is 0, it will search the option
+ *		from the *skops*\ **->skb_data**.  The comment in
+ *		**struct bpf_sock_ops** has details on what skb_data contains
+ *		under different *skops*\ **->op**.
+ *
+ *		For XDP programs, the upper 16 bits of **flags** should contain
+ *		the offset to the tcp header.
  *
  *		The first byte of the *searchby_res* specifies the
  *		kind that it wants to search.
  *
  *		If the searching kind is an experimental kind
- *		(i.e. 253 or 254 according to RFC6994).  It also
+ *		(i.e. 253 or 254 according to RFC6994), it also
  *		needs to specify the "magic" which is either
  *		2 bytes or 4 bytes.  It then also needs to
  *		specify the size of the magic by using
@@ -4309,7 +4316,7 @@ union bpf_attr {
  *		*len* must be at least 2 bytes which is the minimal size
  *		of a header option.
  *
- *		Supported flags:
+ *		Supported flags for **SOCK_OPS** programs:
  *
  *		* **BPF_LOAD_HDR_OPT_TCP_SYN** to search from the
  *		  saved_syn packet or the just-received syn packet.
@@ -6018,6 +6025,7 @@ enum {
 
 enum {
 	BPF_LOAD_HDR_OPT_TCP_SYN = (1ULL << 0),
+	BPF_LOAD_HDR_OPT_TCP_OFFSET_SHIFT = 48,
 };
 
 /* args[0] value during BPF_SOCK_OPS_HDR_OPT_LEN_CB and
Signature · 1 line
2.30.2
#4 lore raw

PATCH 2/3 bpf/selftests: Rename test_tcp_hdr_options to test_sockops_tcp_hdr_options

In reply to Joanne Koong

Currently, tcp_hdr_options is only supported for sockops type programs. This patchset adds xdp tcp_hdr_options support. To more easily differentiate between these two tests, this patch does the following renames (with no functional changes):

test_tcp_hdr_options -> test_sockops_tcp_hdr_options test_misc_tcp_hdr_options -> test_sockops_misc_tcp_hdr_options

The next patch will add xdp_test_tcp_hdr_options.

---
 ...hdr_options.c => sockops_tcp_hdr_options.c} | 18 +++++++++---------
 ...s.c => test_sockops_misc_tcp_hdr_options.c} |  0
 ...ptions.c => test_sockops_tcp_hdr_options.c} |  0
 3 files changed, 9 insertions(+), 9 deletions(-)
 rename tools/testing/selftests/bpf/prog_tests/{tcp_hdr_options.c => sockops_tcp_hdr_options.c} (96%)
 rename tools/testing/selftests/bpf/progs/{test_misc_tcp_hdr_options.c => test_sockops_misc_tcp_hdr_options.c} (100%)
 rename tools/testing/selftests/bpf/progs/{test_tcp_hdr_options.c => test_sockops_tcp_hdr_options.c} (100%)
tools/testing/selftests/bpf/prog_tests/sockops_tcp_hdr_options.c renamed from tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c +9 -9
similarity index 96%
rename from tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c
rename to tools/testing/selftests/bpf/prog_tests/sockops_tcp_hdr_options.c
index 1fa772079967..f8fb12f4c1ed 100644
@@ -12,8 +12,8 @@
 #include "cgroup_helpers.h"
 #include "network_helpers.h"
 #include "test_tcp_hdr_options.h"
-#include "test_tcp_hdr_options.skel.h"
-#include "test_misc_tcp_hdr_options.skel.h"
+#include "test_sockops_tcp_hdr_options.skel.h"
+#include "test_sockops_misc_tcp_hdr_options.skel.h"
 
 #define LO_ADDR6 "::1"
 #define CG_NAME "/tcpbpf-hdr-opt-test"
@@ -25,8 +25,8 @@ static struct bpf_test_option exp_active_fin_in;
 static struct hdr_stg exp_passive_hdr_stg;
 static struct hdr_stg exp_active_hdr_stg = { .active = true, };
 
-static struct test_misc_tcp_hdr_options *misc_skel;
-static struct test_tcp_hdr_options *skel;
+static struct test_sockops_misc_tcp_hdr_options *misc_skel;
+static struct test_sockops_tcp_hdr_options *skel;
 static int lport_linum_map_fd;
 static int hdr_stg_map_fd;
 static __u32 duration;
@@ -570,15 +570,15 @@ static struct test tests[] = {
 	DEF_TEST(misc),
 };
 
-void test_tcp_hdr_options(void)
+void test_sockops_tcp_hdr_options(void)
 {
 	int i;
 
-	skel = test_tcp_hdr_options__open_and_load();
+	skel = test_sockops_tcp_hdr_options__open_and_load();
 	if (CHECK(!skel, "open and load skel", "failed"))
 		return;
 
-	misc_skel = test_misc_tcp_hdr_options__open_and_load();
+	misc_skel = test_sockops_misc_tcp_hdr_options__open_and_load();
 	if (CHECK(!misc_skel, "open and load misc test skel", "failed"))
 		goto skel_destroy;
 
@@ -600,6 +600,6 @@ void test_tcp_hdr_options(void)
 
 	close(cg_fd);
 skel_destroy:
-	test_misc_tcp_hdr_options__destroy(misc_skel);
-	test_tcp_hdr_options__destroy(skel);
+	test_sockops_misc_tcp_hdr_options__destroy(misc_skel);
+	test_sockops_tcp_hdr_options__destroy(skel);
 }
tools/testing/selftests/bpf/progs/test_sockops_misc_tcp_hdr_options.c renamed from tools/testing/selftests/bpf/progs/test_misc_tcp_hdr_options.c +0 -0
similarity index 100%
rename from tools/testing/selftests/bpf/progs/test_misc_tcp_hdr_options.c
rename to tools/testing/selftests/bpf/progs/test_sockops_misc_tcp_hdr_options.c
tools/testing/selftests/bpf/progs/test_sockops_tcp_hdr_options.c renamed from tools/testing/selftests/bpf/progs/test_tcp_hdr_options.c +0 -0
similarity index 100%
rename from tools/testing/selftests/bpf/progs/test_tcp_hdr_options.c
rename to tools/testing/selftests/bpf/progs/test_sockops_tcp_hdr_options.c
Signature · 1 line
2.30.2
#0 lore raw

cover Add XDP support for bpf_load_hdr_opt

Currently, bpf_sockops programs have been using bpf_load_hdr_opt() to parse the tcp header option. It will be useful to allow other bpf prog types to have a similar way of handling tcp hdr options.

This series adds XDP support for bpf_load_hdr_opt(). At a high level, these patches are:

1/3 patch - Add functionality for xdp bpf_load_hdr_opt in net/core.

2/3 patch - Rename existing test_tcp_hdr_options to test_sockops_tcp_hdr_options.

3/3 patch - Add tests for xdp bpf_load_hdr_opt (test_xdp_tcp_hdr_options).

Joanne Koong (3):
bpf/xdp: Add bpf_load_hdr_opt support for xdp bpf/selftests: Rename test_tcp_hdr_options to
test_sockops_tcp_hdr_options
bpf/selftests: Add xdp bpf_load_tcp_hdr_options tests

 include/uapi/linux/bpf.h                      |  26 ++-
 net/core/filter.c                             |  88 ++++++--
 tools/include/uapi/linux/bpf.h                |  26 ++-
 ...dr_options.c => sockops_tcp_hdr_options.c} |  18 +-
 .../bpf/prog_tests/xdp_tcp_hdr_options.c      | 158 ++++++++++++++
 ....c => test_sockops_misc_tcp_hdr_options.c} |   0
 ...tions.c => test_sockops_tcp_hdr_options.c} |   0
 .../bpf/progs/test_xdp_tcp_hdr_options.c      | 198 ++++++++++++++++++
 8 files changed, 468 insertions(+), 46 deletions(-)
 rename tools/testing/selftests/bpf/prog_tests/{tcp_hdr_options.c => sockops_tcp_hdr_options.c} (96%)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/xdp_tcp_hdr_options.c
 rename tools/testing/selftests/bpf/progs/{test_misc_tcp_hdr_options.c => test_sockops_misc_tcp_hdr_options.c} (100%)
 rename tools/testing/selftests/bpf/progs/{test_tcp_hdr_options.c => test_sockops_tcp_hdr_options.c} (100%)
 create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_tcp_hdr_options.c
Signature · 1 line
2.30.2
#6 lore raw

PATCH 3/3 bpf/selftests: Add xdp bpf_load_tcp_hdr_options tests

This patch adds tests for bpf_load_tcp_hdr_options used by xdp programs.

test_xdp_tcp_hdr_options.c:
- Tests ipv4 and ipv6 packets with TCPOPT_EXP and non-TCPOPT_EXP tcp options set. Verify that options can be parsed and loaded successfully.
- Tests error paths: TCPOPT_EXP with invalid magic, option with invalid kind_len, non-existent option, invalid flags, option size smaller than kind_len, invalid packet

---
 .../bpf/prog_tests/xdp_tcp_hdr_options.c      | 158 ++++++++++++++
 .../bpf/progs/test_xdp_tcp_hdr_options.c      | 198 ++++++++++++++++++
 2 files changed, 356 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/xdp_tcp_hdr_options.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_tcp_hdr_options.c
tools/testing/selftests/bpf/prog_tests/xdp_tcp_hdr_options.c new file +158 -0
new file mode 100644
index 000000000000..bd77593fb2dd
@@ -0,0 +1,158 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2021 Facebook */
+
+#include "test_progs.h"
+#include "network_helpers.h"
+#include "test_tcp_hdr_options.h"
+#include "test_xdp_tcp_hdr_options.skel.h"
+
+struct xdp_exprm_opt {
+	__u8 kind;
+	__u8 len;
+	__u16 magic;
+	struct bpf_test_option data;
+} __packed;
+
+struct xdp_regular_opt {
+	__u8 kind;
+	__u8 len;
+	struct bpf_test_option data;
+} __packed;
+
+struct xdp_test_opt {
+	struct xdp_exprm_opt exprm_opt;
+	struct xdp_regular_opt regular_opt;
+} __packed;
+
+struct xdp_ipv4_packet {
+	struct ipv4_packet pkt_v4;
+	struct xdp_test_opt test_opt;
+} __packed;
+
+struct xdp_ipv6_packet {
+	struct ipv6_packet pkt_v6;
+	struct xdp_test_opt test_opt;
+} __packed;
+
+static __u8 opt_flags = OPTION_MAX_DELACK_MS | OPTION_RAND;
+static __u8 exprm_max_delack_ms = 12;
+static __u8 regular_max_delack_ms = 21;
+static __u8 exprm_rand = 0xfa;
+static __u8 regular_rand = 0xce;
+
+static void init_test_opt(struct xdp_test_opt *test_opt,
+			  struct test_xdp_tcp_hdr_options *skel)
+{
+	test_opt->exprm_opt.kind = TCPOPT_EXP;
+	/* +1 for kind, +1 for kind-len, +2 for magic, +1 for flags, +1 for
+	 * OPTION_MAX_DELACK_MAX, +1 FOR OPTION_RAND
+	 */
+	test_opt->exprm_opt.len = 3 + TCP_BPF_EXPOPT_BASE_LEN;
+	test_opt->exprm_opt.magic = __bpf_htons(skel->rodata->test_magic);
+	test_opt->exprm_opt.data.flags = opt_flags;
+	test_opt->exprm_opt.data.max_delack_ms = exprm_max_delack_ms;
+	test_opt->exprm_opt.data.rand = exprm_rand;
+
+	test_opt->regular_opt.kind = skel->rodata->test_kind;
+	/* +1 for kind, +1 for kind-len, +1 for flags, +1 FOR
+	 * OPTION_MAX_DELACK_MS, +1 FOR OPTION_RAND
+	 */
+	test_opt->regular_opt.len = 5;
+	test_opt->regular_opt.data.flags = opt_flags;
+	test_opt->regular_opt.data.max_delack_ms = regular_max_delack_ms;
+	test_opt->regular_opt.data.rand = regular_rand;
+}
+
+static void check_opt_out(struct test_xdp_tcp_hdr_options *skel)
+{
+	struct bpf_test_option *opt_out;
+	__u32 duration = 0;
+
+	opt_out = &skel->bss->exprm_opt_out;
+	CHECK(opt_out->flags != opt_flags, "exprm flags",
+	      "flags = 0x%x", opt_out->flags);
+	CHECK(opt_out->max_delack_ms != exprm_max_delack_ms, "exprm max_delack_ms",
+	      "max_delack_ms = 0x%x", opt_out->max_delack_ms);
+	CHECK(opt_out->rand != exprm_rand, "exprm rand",
+	      "rand = 0x%x", opt_out->rand);
+
+	opt_out = &skel->bss->regular_opt_out;
+	CHECK(opt_out->flags != opt_flags, "regular flags",
+	      "flags = 0x%x", opt_out->flags);
+	CHECK(opt_out->max_delack_ms != regular_max_delack_ms, "regular max_delack_ms",
+	      "max_delack_ms = 0x%x", opt_out->max_delack_ms);
+	CHECK(opt_out->rand != regular_rand, "regular rand",
+	      "rand = 0x%x", opt_out->rand);
+}
+
+void test_xdp_tcp_hdr_options(void)
+{
+	int err, prog_fd, prog_err_path_fd, prog_invalid_pkt_fd;
+	struct xdp_ipv6_packet ipv6_pkt, invalid_pkt;
+	struct test_xdp_tcp_hdr_options *skel;
+	struct xdp_ipv4_packet ipv4_pkt;
+	struct xdp_test_opt test_opt;
+	__u32 duration, retval, size;
+	char buf[128];
+
+	/* Load XDP program to introspect */
+	skel = test_xdp_tcp_hdr_options__open_and_load();
+	if (CHECK(!skel, "skel open and load",
+		  "%s skeleton failed\n", __func__))
+		return;
+
+	prog_fd = bpf_program__fd(skel->progs._xdp_load_hdr_opt);
+
+	init_test_opt(&test_opt, skel);
+
+	/* Init the packets */
+	ipv4_pkt.pkt_v4 = pkt_v4;
+	ipv4_pkt.pkt_v4.tcp.doff += 3;
+	ipv4_pkt.test_opt = test_opt;
+
+	ipv6_pkt.pkt_v6 = pkt_v6;
+	ipv6_pkt.pkt_v6.tcp.doff += 3;
+	ipv6_pkt.test_opt = test_opt;
+
+	invalid_pkt.pkt_v6 = pkt_v6;
+	/* Set to an offset that will exceed the xdp data_end */
+	invalid_pkt.pkt_v6.tcp.doff += 4;
+	invalid_pkt.test_opt = test_opt;
+
+	/* Test on ipv4 packet */
+	err = bpf_prog_test_run(prog_fd, 1, &ipv4_pkt, sizeof(ipv4_pkt),
+				buf, &size, &retval, &duration);
+	CHECK(err || retval != XDP_PASS,
+	      "xdp_tcp_hdr_options ipv4", "err val %d, retval %d\n",
+	      skel->bss->err_val, retval);
+	check_opt_out(skel);
+
+	/* Test on ipv6 packet */
+	err = bpf_prog_test_run(prog_fd, 1, &ipv6_pkt, sizeof(ipv6_pkt),
+				buf, &size, &retval, &duration);
+	CHECK(err || retval != XDP_PASS,
+	      "xdp_tcp_hdr_options ipv6", "err val %d, retval %d\n",
+	      skel->bss->err_val, retval);
+	check_opt_out(skel);
+
+	/* Test error paths */
+	prog_err_path_fd =
+		bpf_program__fd(skel->progs._xdp_load_hdr_opt_err_paths);
+	err = bpf_prog_test_run(prog_err_path_fd, 1, &ipv6_pkt, sizeof(ipv6_pkt),
+				buf, &size, &retval, &duration);
+	CHECK(err || retval != XDP_PASS,
+	      "xdp_tcp_hdr_options err_path", "err val %d, retval %d\n",
+	      skel->bss->err_val, retval);
+
+	/* Test invalid packet */
+	prog_invalid_pkt_fd =
+		bpf_program__fd(skel->progs._xdp_load_hdr_opt_invalid_pkt);
+	err = bpf_prog_test_run(prog_invalid_pkt_fd, 1, &invalid_pkt,
+				sizeof(invalid_pkt), buf, &size, &retval,
+				&duration);
+	CHECK(err || retval != XDP_PASS,
+	      "xdp_tcp_hdr_options invalid_pkt", "err val %d, retval %d\n",
+	      skel->bss->err_val, retval);
+
+	test_xdp_tcp_hdr_options__destroy(skel);
+}
tools/testing/selftests/bpf/progs/test_xdp_tcp_hdr_options.c new file +198 -0

198 lines not on this page · raw message

Signature · 1 line
2.30.2
reviewer Acked-by on v1 1/3
#2 lore raw

Re: [PATCH bpf-next v1 1/3] bpf/xdp: Add bpf_load_hdr_opt support for xdp

In reply to Joanne Koong
Quoted 9 lines · Joanne Koong

This patch enables XDP programs to use the bpf_load_hdr_opt helper function to load header options.

The upper 16 bits of "flags" is used to denote the offset to the tcp header. No other flags are, at this time, used by XDP programs. In the future, more flags can be included to support other types of header options.

Quoted 6 lines

Much of the logic for loading header options can be shared between sockops and xdp programs. In net/core/filter.c, this common shared logic is refactored into a separate function both sockops and xdp use.

Looks good over all.

Just a nitpick below.

Quoted 6 lines
---
 include/uapi/linux/bpf.h       | 26 ++++++----
 net/core/filter.c              | 88 ++++++++++++++++++++++++++--------
 tools/include/uapi/linux/bpf.h | 26 ++++++----
 3 files changed, 103 insertions(+), 37 deletions(-)

[...]

net/core/filter.c BPF_CALL_4(bpf_sock_ops_load_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock, · line 6968

Quoted 11 lines
net/core/filter.c +11 -0
+
+BPF_CALL_4(bpf_xdp_load_hdr_opt, struct xdp_buff *, xdp,
+          void *, search_res, u32, len, u64, flags)
+{
+       const void *op, *opend;
+       struct tcphdr *th;
+
+       /* The upper 16 bits of flags contain the offset to the tcp header.
+        * No other bits should be set.
+        */
+       if (flags & 0xffffffffffff)

Maybe use (1ULL << BPF_LOAD_HDR_OPT_TCP_OFFSET_SHIFT) - 1

Quoted 6 lines
net/core/filter.c +6 -0
+               return -EINVAL;
+
+       th = xdp->data + (flags >> BPF_LOAD_HDR_OPT_TCP_OFFSET_SHIFT);
+       op = (void *)th + sizeof(struct tcphdr);
+       if (unlikely(op > xdp->data_end))
+               return -EINVAL;

[...]

reviewer Acked-by on v1 2/3
#5 lore raw

Re: [PATCH bpf-next v1 2/3] bpf/selftests: Rename test_tcp_hdr_options to test_sockops_tcp_hdr_options

In reply to Joanne Koong
Quoted 6 lines · Joanne Koong

Currently, tcp_hdr_options is only supported for sockops type programs. This patchset adds xdp tcp_hdr_options support. To more easily differentiate between these two tests, this patch does the following renames (with no functional changes):

Quoted 6 lines

test_tcp_hdr_options -> test_sockops_tcp_hdr_options test_misc_tcp_hdr_options -> test_sockops_misc_tcp_hdr_options

The next patch will add xdp_test_tcp_hdr_options.

Quoted 77 lines
---
 ...hdr_options.c => sockops_tcp_hdr_options.c} | 18 +++++++++---------
 ...s.c => test_sockops_misc_tcp_hdr_options.c} |  0
 ...ptions.c => test_sockops_tcp_hdr_options.c} |  0
 3 files changed, 9 insertions(+), 9 deletions(-)
 rename tools/testing/selftests/bpf/prog_tests/{tcp_hdr_options.c => sockops_tcp_hdr_options.c} (96%)
 rename tools/testing/selftests/bpf/progs/{test_misc_tcp_hdr_options.c => test_sockops_misc_tcp_hdr_options.c} (100%)
 rename tools/testing/selftests/bpf/progs/{test_tcp_hdr_options.c => test_sockops_tcp_hdr_options.c} (100%)
tools/testing/selftests/bpf/prog_tests/sockops_tcp_hdr_options.c renamed from tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c +9 -9
similarity index 96%
rename from tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c
rename to tools/testing/selftests/bpf/prog_tests/sockops_tcp_hdr_options.c
index 1fa772079967..f8fb12f4c1ed 100644
@@ -12,8 +12,8 @@
 #include "cgroup_helpers.h"
 #include "network_helpers.h"
 #include "test_tcp_hdr_options.h"
-#include "test_tcp_hdr_options.skel.h"
-#include "test_misc_tcp_hdr_options.skel.h"
+#include "test_sockops_tcp_hdr_options.skel.h"
+#include "test_sockops_misc_tcp_hdr_options.skel.h"
 
 #define LO_ADDR6 "::1"
 #define CG_NAME "/tcpbpf-hdr-opt-test"
@@ -25,8 +25,8 @@ static struct bpf_test_option exp_active_fin_in;
 static struct hdr_stg exp_passive_hdr_stg;
 static struct hdr_stg exp_active_hdr_stg = { .active = true, };
 
-static struct test_misc_tcp_hdr_options *misc_skel;
-static struct test_tcp_hdr_options *skel;
+static struct test_sockops_misc_tcp_hdr_options *misc_skel;
+static struct test_sockops_tcp_hdr_options *skel;
 static int lport_linum_map_fd;
 static int hdr_stg_map_fd;
 static __u32 duration;
@@ -570,15 +570,15 @@ static struct test tests[] = {
        DEF_TEST(misc),
 };
 
-void test_tcp_hdr_options(void)
+void test_sockops_tcp_hdr_options(void)
 {
        int i;
 
-       skel = test_tcp_hdr_options__open_and_load();
+       skel = test_sockops_tcp_hdr_options__open_and_load();
        if (CHECK(!skel, "open and load skel", "failed"))
                return;
 
-       misc_skel = test_misc_tcp_hdr_options__open_and_load();
+       misc_skel = test_sockops_misc_tcp_hdr_options__open_and_load();
        if (CHECK(!misc_skel, "open and load misc test skel", "failed"))
                goto skel_destroy;
 
@@ -600,6 +600,6 @@ void test_tcp_hdr_options(void)
 
        close(cg_fd);
 skel_destroy:
-       test_misc_tcp_hdr_options__destroy(misc_skel);
-       test_tcp_hdr_options__destroy(skel);
+       test_sockops_misc_tcp_hdr_options__destroy(misc_skel);
+       test_sockops_tcp_hdr_options__destroy(skel);
 }
tools/testing/selftests/bpf/progs/test_sockops_misc_tcp_hdr_options.c renamed from tools/testing/selftests/bpf/progs/test_misc_tcp_hdr_options.c +0 -0
similarity index 100%
rename from tools/testing/selftests/bpf/progs/test_misc_tcp_hdr_options.c
rename to tools/testing/selftests/bpf/progs/test_sockops_misc_tcp_hdr_options.c
tools/testing/selftests/bpf/progs/test_sockops_tcp_hdr_options.c renamed from tools/testing/selftests/bpf/progs/test_tcp_hdr_options.c +0 -1
similarity index 100%
rename from tools/testing/selftests/bpf/progs/test_tcp_hdr_options.c
rename to tools/testing/selftests/bpf/progs/test_sockops_tcp_hdr_options.c
--

2.30.2

maintainer
#3 lore raw

Re: [PATCH bpf-next v1 1/3] bpf/xdp: Add bpf_load_hdr_opt support for xdp

In reply to Song Liu
Quoted 3 lines · On Fri, 1 Oct 2021 15:47:55 -0700 Song Liu wrote:
Quoted 1 line

+ if (flags & 0xffffffffffff)

Maybe use (1ULL << BPF_LOAD_HDR_OPT_TCP_OFFSET_SHIFT) - 1

Or GENMASK_ULL(47, 0) ?

reviewer
#7 lore raw

Re: [PATCH bpf-next v1 3/3] bpf/selftests: Add xdp bpf_load_tcp_hdr_options tests

In reply to Joanne Koong
Quoted 143 lines · Joanne Koong

This patch adds tests for bpf_load_tcp_hdr_options used by xdp programs.

test_xdp_tcp_hdr_options.c:
- Tests ipv4 and ipv6 packets with TCPOPT_EXP and non-TCPOPT_EXP tcp options set. Verify that options can be parsed and loaded successfully.
- Tests error paths: TCPOPT_EXP with invalid magic, option with invalid kind_len, non-existent option, invalid flags, option size smaller than kind_len, invalid packet

---
 .../bpf/prog_tests/xdp_tcp_hdr_options.c      | 158 ++++++++++++++
 .../bpf/progs/test_xdp_tcp_hdr_options.c      | 198 ++++++++++++++++++
 2 files changed, 356 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/xdp_tcp_hdr_options.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_tcp_hdr_options.c
tools/testing/selftests/bpf/prog_tests/xdp_tcp_hdr_options.c new file +117 -0
new file mode 100644
index 000000000000..bd77593fb2dd
@@ -0,0 +1,158 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2021 Facebook */
+
+#include "test_progs.h"
+#include "network_helpers.h"
+#include "test_tcp_hdr_options.h"
+#include "test_xdp_tcp_hdr_options.skel.h"
+
+struct xdp_exprm_opt {
+       __u8 kind;
+       __u8 len;
+       __u16 magic;
+       struct bpf_test_option data;
+} __packed;
+
+struct xdp_regular_opt {
+       __u8 kind;
+       __u8 len;
+       struct bpf_test_option data;
+} __packed;
+
+struct xdp_test_opt {
+       struct xdp_exprm_opt exprm_opt;
+       struct xdp_regular_opt regular_opt;
+} __packed;
+
+struct xdp_ipv4_packet {
+       struct ipv4_packet pkt_v4;
+       struct xdp_test_opt test_opt;
+} __packed;
+
+struct xdp_ipv6_packet {
+       struct ipv6_packet pkt_v6;
+       struct xdp_test_opt test_opt;
+} __packed;
+
+static __u8 opt_flags = OPTION_MAX_DELACK_MS | OPTION_RAND;
+static __u8 exprm_max_delack_ms = 12;
+static __u8 regular_max_delack_ms = 21;
+static __u8 exprm_rand = 0xfa;
+static __u8 regular_rand = 0xce;
+
+static void init_test_opt(struct xdp_test_opt *test_opt,
+                         struct test_xdp_tcp_hdr_options *skel)
+{
+       test_opt->exprm_opt.kind = TCPOPT_EXP;
+       /* +1 for kind, +1 for kind-len, +2 for magic, +1 for flags, +1 for
+        * OPTION_MAX_DELACK_MAX, +1 FOR OPTION_RAND
+        */
+       test_opt->exprm_opt.len = 3 + TCP_BPF_EXPOPT_BASE_LEN;
+       test_opt->exprm_opt.magic = __bpf_htons(skel->rodata->test_magic);
+       test_opt->exprm_opt.data.flags = opt_flags;
+       test_opt->exprm_opt.data.max_delack_ms = exprm_max_delack_ms;
+       test_opt->exprm_opt.data.rand = exprm_rand;
+
+       test_opt->regular_opt.kind = skel->rodata->test_kind;
+       /* +1 for kind, +1 for kind-len, +1 for flags, +1 FOR
+        * OPTION_MAX_DELACK_MS, +1 FOR OPTION_RAND
+        */
+       test_opt->regular_opt.len = 5;
+       test_opt->regular_opt.data.flags = opt_flags;
+       test_opt->regular_opt.data.max_delack_ms = regular_max_delack_ms;
+       test_opt->regular_opt.data.rand = regular_rand;
+}
+
+static void check_opt_out(struct test_xdp_tcp_hdr_options *skel)
+{
+       struct bpf_test_option *opt_out;
+       __u32 duration = 0;
+
+       opt_out = &skel->bss->exprm_opt_out;
+       CHECK(opt_out->flags != opt_flags, "exprm flags",
+             "flags = 0x%x", opt_out->flags);
+       CHECK(opt_out->max_delack_ms != exprm_max_delack_ms, "exprm max_delack_ms",
+             "max_delack_ms = 0x%x", opt_out->max_delack_ms);
+       CHECK(opt_out->rand != exprm_rand, "exprm rand",
+             "rand = 0x%x", opt_out->rand);
+
+       opt_out = &skel->bss->regular_opt_out;
+       CHECK(opt_out->flags != opt_flags, "regular flags",
+             "flags = 0x%x", opt_out->flags);
+       CHECK(opt_out->max_delack_ms != regular_max_delack_ms, "regular max_delack_ms",
+             "max_delack_ms = 0x%x", opt_out->max_delack_ms);
+       CHECK(opt_out->rand != regular_rand, "regular rand",
+             "rand = 0x%x", opt_out->rand);
+}
+
+void test_xdp_tcp_hdr_options(void)
+{
+       int err, prog_fd, prog_err_path_fd, prog_invalid_pkt_fd;
+       struct xdp_ipv6_packet ipv6_pkt, invalid_pkt;
+       struct test_xdp_tcp_hdr_options *skel;
+       struct xdp_ipv4_packet ipv4_pkt;
+       struct xdp_test_opt test_opt;
+       __u32 duration, retval, size;
+       char buf[128];
+
+       /* Load XDP program to introspect */
+       skel = test_xdp_tcp_hdr_options__open_and_load();
+       if (CHECK(!skel, "skel open and load",
+                 "%s skeleton failed\n", __func__))
+               return;
+
+       prog_fd = bpf_program__fd(skel->progs._xdp_load_hdr_opt);
+
+       init_test_opt(&test_opt, skel);
+
+       /* Init the packets */
+       ipv4_pkt.pkt_v4 = pkt_v4;
+       ipv4_pkt.pkt_v4.tcp.doff += 3;
+       ipv4_pkt.test_opt = test_opt;
+
+       ipv6_pkt.pkt_v6 = pkt_v6;
+       ipv6_pkt.pkt_v6.tcp.doff += 3;
+       ipv6_pkt.test_opt = test_opt;
+
+       invalid_pkt.pkt_v6 = pkt_v6;

tools/testing/selftests/bpf/prog_tests/xdp_tcp_hdr_options.c line 127

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/xdp_tcp_hdr_options.c new file +10 -0
+       /* Set to an offset that will exceed the xdp data_end */
+       invalid_pkt.pkt_v6.tcp.doff += 4;
+       invalid_pkt.test_opt = test_opt;
+
+       /* Test on ipv4 packet */
+       err = bpf_prog_test_run(prog_fd, 1, &ipv4_pkt, sizeof(ipv4_pkt),
+                               buf, &size, &retval, &duration);
+       CHECK(err || retval != XDP_PASS,
+             "xdp_tcp_hdr_options ipv4", "err val %d, retval %d\n",
+             skel->bss->err_val, retval);

Shall we skip the following checks if the test_run fails?

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/xdp_tcp_hdr_options.c new file +8 -0
+       check_opt_out(skel);
+
+       /* Test on ipv6 packet */
+       err = bpf_prog_test_run(prog_fd, 1, &ipv6_pkt, sizeof(ipv6_pkt),
+                               buf, &size, &retval, &duration);
+       CHECK(err || retval != XDP_PASS,
+             "xdp_tcp_hdr_options ipv6", "err val %d, retval %d\n",
+             skel->bss->err_val, retval);

tools/testing/selftests/bpf/prog_tests/xdp_tcp_hdr_options.c line 145

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/xdp_tcp_hdr_options.c new file +10 -0
+       check_opt_out(skel);
+
+       /* Test error paths */
+       prog_err_path_fd =
+               bpf_program__fd(skel->progs._xdp_load_hdr_opt_err_paths);
+       err = bpf_prog_test_run(prog_err_path_fd, 1, &ipv6_pkt, sizeof(ipv6_pkt),
+                               buf, &size, &retval, &duration);
+       CHECK(err || retval != XDP_PASS,
+             "xdp_tcp_hdr_options err_path", "err val %d, retval %d\n",
+             skel->bss->err_val, retval);

Ditto.

Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/xdp_tcp_hdr_options.c new file +13 -0
+
+       /* Test invalid packet */
+       prog_invalid_pkt_fd =
+               bpf_program__fd(skel->progs._xdp_load_hdr_opt_invalid_pkt);
+       err = bpf_prog_test_run(prog_invalid_pkt_fd, 1, &invalid_pkt,
+                               sizeof(invalid_pkt), buf, &size, &retval,
+                               &duration);
+       CHECK(err || retval != XDP_PASS,
+             "xdp_tcp_hdr_options invalid_pkt", "err val %d, retval %d\n",
+             skel->bss->err_val, retval);
+
+       test_xdp_tcp_hdr_options__destroy(skel);
+}

[...]

#8 lore raw

Re: [PATCH bpf-next v1 3/3] bpf/selftests: Add xdp bpf_load_tcp_hdr_options tests

In reply to Joanne Koong
Quoted 20 lines · Joanne Koong

This patch adds tests for bpf_load_tcp_hdr_options used by xdp programs.

test_xdp_tcp_hdr_options.c:
- Tests ipv4 and ipv6 packets with TCPOPT_EXP and non-TCPOPT_EXP tcp options set. Verify that options can be parsed and loaded successfully.
- Tests error paths: TCPOPT_EXP with invalid magic, option with invalid kind_len, non-existent option, invalid flags, option size smaller than kind_len, invalid packet

---
 .../bpf/prog_tests/xdp_tcp_hdr_options.c      | 158 ++++++++++++++
 .../bpf/progs/test_xdp_tcp_hdr_options.c      | 198 ++++++++++++++++++
 2 files changed, 356 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/xdp_tcp_hdr_options.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_tcp_hdr_options.c

[...]

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/xdp_tcp_hdr_options.c new file +10 -0
+static void check_opt_out(struct test_xdp_tcp_hdr_options *skel)
+{
+       struct bpf_test_option *opt_out;
+       __u32 duration = 0;
+
+       opt_out = &skel->bss->exprm_opt_out;
+       CHECK(opt_out->flags != opt_flags, "exprm flags",
+             "flags = 0x%x", opt_out->flags);
+       CHECK(opt_out->max_delack_ms != exprm_max_delack_ms, "exprm max_delack_ms",
+             "max_delack_ms = 0x%x", opt_out->max_delack_ms);

tools/testing/selftests/bpf/prog_tests/xdp_tcp_hdr_options.c line 85

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/xdp_tcp_hdr_options.c new file +10 -0
+       CHECK(opt_out->rand != exprm_rand, "exprm rand",
+             "rand = 0x%x", opt_out->rand);
+
+       opt_out = &skel->bss->regular_opt_out;
+       CHECK(opt_out->flags != opt_flags, "regular flags",
+             "flags = 0x%x", opt_out->flags);
+       CHECK(opt_out->max_delack_ms != regular_max_delack_ms, "regular max_delack_ms",
+             "max_delack_ms = 0x%x", opt_out->max_delack_ms);
+       CHECK(opt_out->rand != regular_rand, "regular rand",
+             "rand = 0x%x", opt_out->rand);

Please use ASSERT_xxx() macros for new tests. CHECK()s are confusing and actually require more typing and work to output actual arguments that failed. In this case, you'd just write

ASSERT_EQ(opt_out->rand, regular_rand, "regular_rand");

And if they are not equal, ASSERT_EQ() will print actual values of both opt_out->rand and regular_rand.

Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/xdp_tcp_hdr_options.c new file +12 -0
+}
+
+void test_xdp_tcp_hdr_options(void)
+{
+       int err, prog_fd, prog_err_path_fd, prog_invalid_pkt_fd;
+       struct xdp_ipv6_packet ipv6_pkt, invalid_pkt;
+       struct test_xdp_tcp_hdr_options *skel;
+       struct xdp_ipv4_packet ipv4_pkt;
+       struct xdp_test_opt test_opt;
+       __u32 duration, retval, size;
+       char buf[128];
+

[...]