Skip to contentSkip to conversation

bpf: Reject TCP_NODELAY in TCP header option callbacks

PATCH Superseded KaFai Wan 3 participants 9 messages Apr 16 – Apr 17

Subsystems (6)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

KaFai Wan

author
#0 lore raw

v2 cover bpf: Reject TCP_NODELAY in TCP header option callbacks

This small patchset is about avoid infinite recursion in TCP header option callbacks via TCP_NODELAY setsockopt.

v2:
- Reject TCP_NODELAY in bpf_sock_ops_setsockopt() (AI and Martin)

v1:
https://lore.kernel.org/bpf/[email protected]/

---
KaFai Wan (2):
bpf: Reject TCP_NODELAY in TCP header option callbacks selftests/bpf: Test TCP_NODELAY in TCP hdr opt callbacks

 net/core/filter.c                             |  5 ++
 .../bpf/prog_tests/tcp_hdr_options.c          | 54 +++++++++++++++++++
 .../bpf/progs/test_misc_tcp_hdr_options.c     | 40 ++++++++++++++
 3 files changed, 99 insertions(+)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v2 1/2 bpf: Reject TCP_NODELAY in TCP header option callbacks

A BPF_SOCK_OPS program can enable BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG and then call bpf_setsockopt(TCP_NODELAY) from BPF_SOCK_OPS_HDR_OPT_LEN_CB or BPF_SOCK_OPS_WRITE_HDR_OPT_CB.

In these callbacks, bpf_setsockopt(TCP_NODELAY) can reach __tcp_sock_set_nodelay(), which can call tcp_push_pending_frames().

From BPF_SOCK_OPS_HDR_OPT_LEN_CB, tcp_push_pending_frames() can call tcp_current_mss(), which calls tcp_established_options() and re-enters bpf_skops_hdr_opt_len().

BPF_SOCK_OPS_HDR_OPT_LEN_CB
-> bpf_setsockopt(TCP_NODELAY)

    -> tcp_push_pending_frames()
      -> tcp_current_mss()
        -> tcp_established_options()
          -> bpf_skops_hdr_opt_len()
            -> BPF_SOCK_OPS_HDR_OPT_LEN_CB

From BPF_SOCK_OPS_WRITE_HDR_OPT_CB, tcp_push_pending_frames() can call tcp_write_xmit(), which calls tcp_transmit_skb(). That path recomputes header option length through tcp_established_options() and bpf_skops_hdr_opt_len() before re-entering bpf_skops_write_hdr_opt().

BPF_SOCK_OPS_WRITE_HDR_OPT_CB
-> bpf_setsockopt(TCP_NODELAY)

    -> tcp_push_pending_frames()
      -> tcp_write_xmit()
        -> tcp_transmit_skb()
          -> tcp_established_options()
            -> bpf_skops_hdr_opt_len()
          -> bpf_skops_write_hdr_opt()
            -> BPF_SOCK_OPS_WRITE_HDR_OPT_CB

This leads to unbounded recursion and can overflow the kernel stack.

Reject TCP_NODELAY with -EOPNOTSUPP in bpf_sock_ops_setsockopt() when bpf_setsockopt() is called from BPF_SOCK_OPS_HDR_OPT_LEN_CB or BPF_SOCK_OPS_WRITE_HDR_OPT_CB.

Fixes 7e41df5dbba2 ("bpf: Add a few optnames to bpf_setsockopt")
---
 net/core/filter.c | 5 +++++
 1 file changed, 5 insertions(+)
net/core/filter.c +5 -0
index fcfcb72663ca..911ff04bca5a 100644
@@ -5833,6 +5833,11 @@ BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
 	if (!is_locked_tcp_sock_ops(bpf_sock))
 		return -EOPNOTSUPP;
 
+	if ((bpf_sock->op == BPF_SOCK_OPS_HDR_OPT_LEN_CB ||
+	     bpf_sock->op == BPF_SOCK_OPS_WRITE_HDR_OPT_CB) &&
+	    IS_ENABLED(CONFIG_INET) && level == SOL_TCP && optname == TCP_NODELAY)
+		return -EOPNOTSUPP;
+
 	return _bpf_setsockopt(bpf_sock->sk, level, optname, optval, optlen);
 }
 
Signature · 1 line
2.43.0
#2 lore raw

net/core/filter.c BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock, · line 5838

Quoted 11 lines · KaFai Wan
net/core/filter.c +3 -0
index fcfcb72663ca..911ff04bca5a 100644
@@ -5833,6 +5833,11 @@ BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
 	if (!is_locked_tcp_sock_ops(bpf_sock))
 		return -EOPNOTSUPP;
 
+	if ((bpf_sock->op == BPF_SOCK_OPS_HDR_OPT_LEN_CB ||
+	     bpf_sock->op == BPF_SOCK_OPS_WRITE_HDR_OPT_CB) &&
+	    IS_ENABLED(CONFIG_INET) && level == SOL_TCP && optname == TCP_NODELAY)

IS_ENABLED(CONFIG_INET) is unnecessary.

pw-bot: cr

Quoted 8 lines
net/core/filter.c +2 -0
+		return -EOPNOTSUPP;
+
 	return _bpf_setsockopt(bpf_sock->sk, level, optname, optval, optlen);
 }
 
Signature · 1 line
2.43.0
author
#3 lore raw
Quoted 14 lines · Martin KaFai Lau

net/core/filter.c BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock, · line 5838

Quoted 11 lines · KaFai Wan
net/core/filter.c +0 -0
index fcfcb72663ca..911ff04bca5a 100644
@@ -5833,6 +5833,11 @@ BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
  	if (!is_locked_tcp_sock_ops(bpf_sock))
  		return -EOPNOTSUPP;

net/core/filter.c +3 -0
+	if ((bpf_sock->op == BPF_SOCK_OPS_HDR_OPT_LEN_CB ||
+	     bpf_sock->op == BPF_SOCK_OPS_WRITE_HDR_OPT_CB) &&
+	    IS_ENABLED(CONFIG_INET) && level == SOL_TCP && optname == TCP_NODELAY)

IS_ENABLED(CONFIG_INET) is unnecessary.

ok, will remove in next version.

Quoted 11 lines

pw-bot: cr

Quoted 8 lines
net/core/filter.c +2 -0
+		return -EOPNOTSUPP;
+

  return _bpf_setsockopt(bpf_sock->sk, level, optname, optval, optlen); }

Signature · 1 line
2.43.0
Signature · 2 lines
Thanks,
KaFai
#4 lore raw
In reply to KaFai Wan
Quoted 57 lines · KaFai Wan

A BPF_SOCK_OPS program can enable BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG and then call bpf_setsockopt(TCP_NODELAY) from BPF_SOCK_OPS_HDR_OPT_LEN_CB or BPF_SOCK_OPS_WRITE_HDR_OPT_CB.

In these callbacks, bpf_setsockopt(TCP_NODELAY) can reach __tcp_sock_set_nodelay(), which can call tcp_push_pending_frames().

From BPF_SOCK_OPS_HDR_OPT_LEN_CB, tcp_push_pending_frames() can call
tcp_current_mss(), which calls tcp_established_options() and re-enters bpf_skops_hdr_opt_len().

BPF_SOCK_OPS_HDR_OPT_LEN_CB
-> bpf_setsockopt(TCP_NODELAY)

     -> tcp_push_pending_frames()
       -> tcp_current_mss()
         -> tcp_established_options()
           -> bpf_skops_hdr_opt_len()
             -> BPF_SOCK_OPS_HDR_OPT_LEN_CB

From BPF_SOCK_OPS_WRITE_HDR_OPT_CB, tcp_push_pending_frames() can call
tcp_write_xmit(), which calls tcp_transmit_skb(). That path recomputes header option length through tcp_established_options() and bpf_skops_hdr_opt_len() before re-entering bpf_skops_write_hdr_opt().

BPF_SOCK_OPS_WRITE_HDR_OPT_CB
-> bpf_setsockopt(TCP_NODELAY)

     -> tcp_push_pending_frames()
       -> tcp_write_xmit()
         -> tcp_transmit_skb()
           -> tcp_established_options()
             -> bpf_skops_hdr_opt_len()
           -> bpf_skops_write_hdr_opt()
             -> BPF_SOCK_OPS_WRITE_HDR_OPT_CB

This leads to unbounded recursion and can overflow the kernel stack.

Reject TCP_NODELAY with -EOPNOTSUPP in bpf_sock_ops_setsockopt() when bpf_setsockopt() is called from BPF_SOCK_OPS_HDR_OPT_LEN_CB or BPF_SOCK_OPS_WRITE_HDR_OPT_CB.

Fixes 7e41df5dbba2 ("bpf: Add a few optnames to bpf_setsockopt")
---
  net/core/filter.c | 5 +++++
  1 file changed, 5 insertions(+)
net/core/filter.c +0 -0
index fcfcb72663ca..911ff04bca5a 100644
@@ -5833,6 +5833,11 @@ BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
  	if (!is_locked_tcp_sock_ops(bpf_sock))

net/core/filter.c BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock, · line 5843

Quoted 10 lines
net/core/filter.c +5 -0
  		return -EOPNOTSUPP;
 
+	if ((bpf_sock->op == BPF_SOCK_OPS_HDR_OPT_LEN_CB ||
+	     bpf_sock->op == BPF_SOCK_OPS_WRITE_HDR_OPT_CB) &&
+	    IS_ENABLED(CONFIG_INET) && level == SOL_TCP && optname == TCP_NODELAY)
+		return -EOPNOTSUPP;
+
  	return _bpf_setsockopt(bpf_sock->sk, level, optname, optval, optlen);
  }
 

A simple comment is recommended:

/* TCP_NODELAY triggers tcp_push_pending_frames() and re-enters these callbacks. */

Also like Martin pointed before, BPF_SOCK_OPS_HDR_OPT_LEN_CB / BPF_SOCK_OPS_WRITE_HDR_OPT_CB

can only be produced under CONFIG_INET so IS_ENABLED(CONFIG_INET) is dead.

author
#5 lore raw
Quoted 72 lines · Jiayuan Chen
Quoted 64 lines · KaFai Wan

A BPF_SOCK_OPS program can enable BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG and then call bpf_setsockopt(TCP_NODELAY) from BPF_SOCK_OPS_HDR_OPT_LEN_CB or BPF_SOCK_OPS_WRITE_HDR_OPT_CB.

In these callbacks, bpf_setsockopt(TCP_NODELAY) can reach __tcp_sock_set_nodelay(), which can call tcp_push_pending_frames().

 From BPF_SOCK_OPS_HDR_OPT_LEN_CB, tcp_push_pending_frames() can call tcp_current_mss(), which calls tcp_established_options() and re-enters bpf_skops_hdr_opt_len().

BPF_SOCK_OPS_HDR_OPT_LEN_CB
   -> bpf_setsockopt(TCP_NODELAY)
     -> tcp_push_pending_frames()
       -> tcp_current_mss()
         -> tcp_established_options()
           -> bpf_skops_hdr_opt_len()
             -> BPF_SOCK_OPS_HDR_OPT_LEN_CB

 From BPF_SOCK_OPS_WRITE_HDR_OPT_CB, tcp_push_pending_frames() can call tcp_write_xmit(), which calls tcp_transmit_skb().  That path recomputes header option length through tcp_established_options() and bpf_skops_hdr_opt_len() before re-entering bpf_skops_write_hdr_opt().

BPF_SOCK_OPS_WRITE_HDR_OPT_CB
   -> bpf_setsockopt(TCP_NODELAY)
     -> tcp_push_pending_frames()
       -> tcp_write_xmit()
         -> tcp_transmit_skb()
           -> tcp_established_options()
             -> bpf_skops_hdr_opt_len()
           -> bpf_skops_write_hdr_opt()
             -> BPF_SOCK_OPS_WRITE_HDR_OPT_CB

This leads to unbounded recursion and can overflow the kernel stack.

Reject TCP_NODELAY with -EOPNOTSUPP in bpf_sock_ops_setsockopt() when bpf_setsockopt() is called from BPF_SOCK_OPS_HDR_OPT_LEN_CB or BPF_SOCK_OPS_WRITE_HDR_OPT_CB.

Fixes 7e41df5dbba2 ("bpf: Add a few optnames to bpf_setsockopt")

---
  net/core/filter.c | 5 +++++

  1 file changed, 5 insertions(+)
net/core/filter.c +0 -0
index fcfcb72663ca..911ff04bca5a 100644
@@ -5833,6 +5833,11 @@ BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
   	if (!is_locked_tcp_sock_ops(bpf_sock))
   		return -EOPNOTSUPP;

net/core/filter.c +5 -0
+	if ((bpf_sock->op == BPF_SOCK_OPS_HDR_OPT_LEN_CB ||
+	     bpf_sock->op == BPF_SOCK_OPS_WRITE_HDR_OPT_CB) &&
+	    IS_ENABLED(CONFIG_INET) && level == SOL_TCP && optname == TCP_NODELAY)
+		return -EOPNOTSUPP;
+
Quoted 3 lines

   return _bpf_setsockopt(bpf_sock->sk, level, optname, optval, optlen); }

A simple comment is recommended:

Quoted 3 lines

/* TCP_NODELAY triggers tcp_push_pending_frames() and re-enters these callbacks. */

ok. Added in new version.

Quoted 6 lines

Also like Martin pointed before, BPF_SOCK_OPS_HDR_OPT_LEN_CB / BPF_SOCK_OPS_WRITE_HDR_OPT_CB

can only be produced under CONFIG_INET so IS_ENABLED(CONFIG_INET) is dead.

Signature · 2 lines
Thanks,
KaFai
author
#6 lore raw

PATCH v2 2/2 selftests/bpf: Test TCP_NODELAY in TCP hdr opt callbacks

In reply to KaFai Wan

Add a sockops selftest for the TCP_NODELAY restriction in BPF_SOCK_OPS_HDR_OPT_LEN_CB and BPF_SOCK_OPS_WRITE_HDR_OPT_CB.

The test program calls bpf_setsockopt(TCP_NODELAY) from ACTIVE_ESTABLISHED_CB and PASSIVE_ESTABLISHED_CB to verify that it is still allowed outside the TCP header option callbacks.

It then enables BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG, sends data to exercise the TCP header option path, and checks that bpf_setsockopt(TCP_NODELAY) returns -EOPNOTSUPP from both BPF_SOCK_OPS_HDR_OPT_LEN_CB and BPF_SOCK_OPS_WRITE_HDR_OPT_CB while the connection continues to make forward progress.

---
 .../bpf/prog_tests/tcp_hdr_options.c          | 54 +++++++++++++++++++
 .../bpf/progs/test_misc_tcp_hdr_options.c     | 40 ++++++++++++++
 2 files changed, 94 insertions(+)
tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c +54 -0
index 56685fc03c7e..2d738c0c4259 100644
@@ -513,6 +513,59 @@ static void misc(void)
 	bpf_link__destroy(link);
 }
 
+static void hdr_sockopt(void)
+{
+	const char send_msg[] = "MISC!!!";
+	char recv_msg[sizeof(send_msg)];
+	const unsigned int nr_data = 2;
+	struct bpf_link *link;
+	struct sk_fds sk_fds;
+	int i, ret, true_val = 1;
+
+	lport_linum_map_fd = bpf_map__fd(misc_skel->maps.lport_linum_map);
+
+	link = bpf_program__attach_cgroup(misc_skel->progs.misc_hdr_sockopt, cg_fd);
+	if (!ASSERT_OK_PTR(link, "attach_cgroup(misc_hdr_sockopt)"))
+		return;
+
+	if (sk_fds_connect(&sk_fds, false)) {
+		bpf_link__destroy(link);
+		return;
+	}
+
+	ret = setsockopt(sk_fds.active_fd, SOL_TCP, TCP_NODELAY, &true_val, sizeof(true_val));
+	if (!ASSERT_OK(ret, "setsockopt(TCP_NODELAY) active"))
+		goto check_linum;
+
+	ret = setsockopt(sk_fds.passive_fd, SOL_TCP, TCP_NODELAY, &true_val, sizeof(true_val));
+	if (!ASSERT_OK(ret, "setsockopt(TCP_NODELAY) passive"))
+		goto check_linum;
+
+	for (i = 0; i < nr_data; i++) {
+		ret = send(sk_fds.active_fd, send_msg, sizeof(send_msg), 0);
+		if (!ASSERT_EQ(ret, sizeof(send_msg), "send(msg)"))
+			goto check_linum;
+
+		ret = read(sk_fds.passive_fd, recv_msg, sizeof(recv_msg));
+		if (!ASSERT_EQ(ret, sizeof(send_msg), "read(msg)"))
+			goto check_linum;
+	}
+
+	ASSERT_NEQ(misc_skel->bss->nr_hdr_sockopt_estab, 0, "nr_hdr_sockopt_estab");
+	ASSERT_EQ(misc_skel->bss->nr_hdr_sockopt_estab_err, 0, "nr_hdr_sockopt_estab_err");
+
+	ASSERT_NEQ(misc_skel->bss->nr_hdr_sockopt_len, 0, "nr_hdr_sockopt_len");
+	ASSERT_EQ(misc_skel->bss->nr_hdr_sockopt_len_err, 0, "nr_hdr_sockopt_len_err");
+
+	ASSERT_NEQ(misc_skel->bss->nr_hdr_sockopt_write, 0, "nr_hdr_sockopt_write");
+	ASSERT_EQ(misc_skel->bss->nr_hdr_sockopt_write_err, 0, "nr_hdr_sockopt_write_err");
+
+check_linum:
+	ASSERT_FALSE(check_error_linum(&sk_fds), "check_error_linum");
+	sk_fds_close(&sk_fds);
+	bpf_link__destroy(link);
+}
+
 struct test {
 	const char *desc;
 	void (*run)(void);
@@ -526,6 +579,7 @@ static struct test tests[] = {
 	DEF_TEST(fastopen_estab),
 	DEF_TEST(fin),
 	DEF_TEST(misc),
+	DEF_TEST(hdr_sockopt),
 };
 
 void test_tcp_hdr_options(void)
tools/testing/selftests/bpf/progs/test_misc_tcp_hdr_options.c +40 -0
index d487153a839d..a8cf7c4e7ed2 100644
@@ -28,6 +28,12 @@ unsigned int nr_data = 0;
 unsigned int nr_syn = 0;
 unsigned int nr_fin = 0;
 unsigned int nr_hwtstamp = 0;
+unsigned int nr_hdr_sockopt_estab = 0;
+unsigned int nr_hdr_sockopt_estab_err = 0;
+unsigned int nr_hdr_sockopt_len = 0;
+unsigned int nr_hdr_sockopt_len_err = 0;
+unsigned int nr_hdr_sockopt_write = 0;
+unsigned int nr_hdr_sockopt_write_err = 0;
 
 /* Check the header received from the active side */
 static int __check_active_hdr_in(struct bpf_sock_ops *skops, bool check_syn)
@@ -326,4 +332,38 @@ int misc_estab(struct bpf_sock_ops *skops)
 	return CG_OK;
 }
 
+SEC("sockops")
+int misc_hdr_sockopt(struct bpf_sock_ops *skops)
+{
+	int true_val = 1;
+	int ret;
+
+	switch (skops->op) {
+	case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
+	case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
+		nr_hdr_sockopt_estab++;
+		set_hdr_cb_flags(skops, 0);
+		ret = bpf_setsockopt(skops, SOL_TCP, TCP_NODELAY, &true_val, sizeof(true_val));
+		if (ret)
+			nr_hdr_sockopt_estab_err++;
+		break;
+	case BPF_SOCK_OPS_HDR_OPT_LEN_CB:
+		nr_hdr_sockopt_len++;
+		ret = bpf_setsockopt(skops, SOL_TCP, TCP_NODELAY, &true_val, sizeof(true_val));
+		if (ret != -EOPNOTSUPP)
+			nr_hdr_sockopt_len_err++;
+		/* just trigger BPF_SOCK_OPS_WRITE_HDR_OPT_CB */
+		bpf_reserve_hdr_opt(skops, 12, 0);
+		break;
+	case BPF_SOCK_OPS_WRITE_HDR_OPT_CB:
+		nr_hdr_sockopt_write++;
+		ret = bpf_setsockopt(skops, SOL_TCP, TCP_NODELAY, &true_val, sizeof(true_val));
+		if (ret != -EOPNOTSUPP)
+			nr_hdr_sockopt_write_err++;
+		break;
+	}
+
+	return CG_OK;
+}
+
 char _license[] SEC("license") = "GPL";
Signature · 1 line
2.43.0
#7 lore raw

Re: [PATCH bpf v2 2/2] selftests/bpf: Test TCP_NODELAY in TCP hdr opt callbacks

Quoted 22 lines · KaFai Wan

index 56685fc03c7e..2d738c0c4259 100644

tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c +15 -0
@@ -513,6 +513,59 @@ static void misc(void)
 	bpf_link__destroy(link);
 }
 
+static void hdr_sockopt(void)
+{
+	const char send_msg[] = "MISC!!!";
+	char recv_msg[sizeof(send_msg)];
+	const unsigned int nr_data = 2;
+	struct bpf_link *link;
+	struct sk_fds sk_fds;
+	int i, ret, true_val = 1;
+
+	lport_linum_map_fd = bpf_map__fd(misc_skel->maps.lport_linum_map);
+
+	link = bpf_program__attach_cgroup(misc_skel->progs.misc_hdr_sockopt, cg_fd);
+	if (!ASSERT_OK_PTR(link, "attach_cgroup(misc_hdr_sockopt)"))
+		return;
+

tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c static void misc(void) · line 540

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c +10 -0
+	if (sk_fds_connect(&sk_fds, false)) {
+		bpf_link__destroy(link);
+		return;
+	}
+
+	ret = setsockopt(sk_fds.active_fd, SOL_TCP, TCP_NODELAY, &true_val, sizeof(true_val));
+	if (!ASSERT_OK(ret, "setsockopt(TCP_NODELAY) active"))
+		goto check_linum;
+
+	ret = setsockopt(sk_fds.passive_fd, SOL_TCP, TCP_NODELAY, &true_val, sizeof(true_val));

Why are these two setsockopt(TCP_NODELAY) calls needed?

Instead of creating a new "void hdr_sockopt(void)", can the test be done in the existing "void misc(void)" by doing bpf_setsockopt(TCP_NODELAY) in the misc_estab() bpf prog?

The PASSIVE_ESTABLISHED_CB can do the bpf_setsockopt(TCP_NODELAY, 0) if it wants to keep the same expectation on Nagle. The BPF_SOCK_OPS_HDR_OPT_LEN_CB and BPF_SOCK_OPS_WRITE_HDR_OPT_CB can do bpf_setsockopt(TCP_NODELAY, 1) to test recursion and the error return value.

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c +0 -0
 void test_tcp_hdr_options(void)
tools/testing/selftests/bpf/progs/test_misc_tcp_hdr_options.c +0 -0
index d487153a839d..a8cf7c4e7ed2 100644

tools/testing/selftests/bpf/progs/test_misc_tcp_hdr_options.c unsigned int nr_data = 0; · line 36

Quoted 9 lines
tools/testing/selftests/bpf/progs/test_misc_tcp_hdr_options.c +6 -0
@@ -28,6 +28,12 @@ unsigned int nr_data = 0;
 unsigned int nr_syn = 0;
 unsigned int nr_fin = 0;
 unsigned int nr_hwtstamp = 0;
+unsigned int nr_hdr_sockopt_estab = 0;
+unsigned int nr_hdr_sockopt_estab_err = 0;
+unsigned int nr_hdr_sockopt_len = 0;
+unsigned int nr_hdr_sockopt_len_err = 0;
+unsigned int nr_hdr_sockopt_write = 0;
+unsigned int nr_hdr_sockopt_write_err = 0;

nr_hdr_sockopt_estab, nr_hdr_sockopt_len, and nr_hdr_sockopt_write are unnecessary. These tests have already been covered in some ways.

Mostly a nit. The new counters are used in both connections. Note the existing nr_xxx is exclusively used in either active or passive, so there is no parallel counting in practice.

Instead of counting, just use a bool nodelay_est_ok, nodelay_hdr_len_err, nodelay_write_err and assert them to be true in userspace.

author
#8 lore raw

Re: [PATCH bpf v2 2/2] selftests/bpf: Test TCP_NODELAY in TCP hdr opt callbacks

Quoted 36 lines · Martin KaFai Lau
Quoted 22 lines · KaFai Wan

index 56685fc03c7e..2d738c0c4259 100644

tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c +0 -0
@@ -513,6 +513,59 @@ static void misc(void)
  	bpf_link__destroy(link);
  }

tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c +15 -0
+static void hdr_sockopt(void)
+{
+	const char send_msg[] = "MISC!!!";
+	char recv_msg[sizeof(send_msg)];
+	const unsigned int nr_data = 2;
+	struct bpf_link *link;
+	struct sk_fds sk_fds;
+	int i, ret, true_val = 1;
+
+	lport_linum_map_fd = bpf_map__fd(misc_skel->maps.lport_linum_map);
+
+	link = bpf_program__attach_cgroup(misc_skel->progs.misc_hdr_sockopt, cg_fd);
+	if (!ASSERT_OK_PTR(link, "attach_cgroup(misc_hdr_sockopt)"))
+		return;
+

tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c static void misc(void) · line 540

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c +10 -0
+	if (sk_fds_connect(&sk_fds, false)) {
+		bpf_link__destroy(link);
+		return;
+	}
+
+	ret = setsockopt(sk_fds.active_fd, SOL_TCP, TCP_NODELAY, &true_val, sizeof(true_val));
+	if (!ASSERT_OK(ret, "setsockopt(TCP_NODELAY) active"))
+		goto check_linum;
+
+	ret = setsockopt(sk_fds.passive_fd, SOL_TCP, TCP_NODELAY, &true_val, sizeof(true_val));

Why are these two setsockopt(TCP_NODELAY) calls needed?

Quoted 3 lines

Instead of creating a new "void hdr_sockopt(void)", can the test be done in the existing "void misc(void)" by doing bpf_setsockopt(TCP_NODELAY) in the misc_estab() bpf prog?

Oh, I see. I meant to test on both active and passive side. We can only test on active side in the existing "void misc(void)".

Quoted 18 lines

The PASSIVE_ESTABLISHED_CB can do the bpf_setsockopt(TCP_NODELAY, 0) if it wants to keep the same expectation on Nagle. The BPF_SOCK_OPS_HDR_OPT_LEN_CB and BPF_SOCK_OPS_WRITE_HDR_OPT_CB can do bpf_setsockopt(TCP_NODELAY, 1) to test recursion and the error return value.

Quoted 11 lines

 void test_tcp_hdr_options(void)

tools/testing/selftests/bpf/progs/test_misc_tcp_hdr_options.c +1 -0
index d487153a839d..a8cf7c4e7ed2 100644
@@ -28,6 +28,12 @@ unsigned int nr_data = 0;
  unsigned int nr_syn = 0;
  unsigned int nr_fin = 0;
  unsigned int nr_hwtstamp = 0;
+unsigned int nr_hdr_sockopt_estab = 0;
Quoted 8 lines

tools/testing/selftests/bpf/progs/test_misc_tcp_hdr_options.c unsigned int nr_data = 0; · line 36

Quoted 5 lines
tools/testing/selftests/bpf/progs/test_misc_tcp_hdr_options.c +5 -0
+unsigned int nr_hdr_sockopt_estab_err = 0;
+unsigned int nr_hdr_sockopt_len = 0;
+unsigned int nr_hdr_sockopt_len_err = 0;
+unsigned int nr_hdr_sockopt_write = 0;
+unsigned int nr_hdr_sockopt_write_err = 0;

nr_hdr_sockopt_estab, nr_hdr_sockopt_len, and nr_hdr_sockopt_write are unnecessary. These tests have already been covered in some ways.

yes, they are unnecessary in existing misc_estab()

Quoted 8 lines

Mostly a nit. The new counters are used in both connections. Note the existing nr_xxx is exclusively used in either active or passive, so there is no parallel counting in practice.

Instead of counting, just use a bool nodelay_est_ok, nodelay_hdr_len_err, nodelay_write_err and assert them to be true in userspace.

indeed. will fix these in next version.

Signature · 2 lines
Thanks,
KaFai