Linux Documentation
 help / color / mirror / Atom feed
From: Willem Ferguson <wferguson@cloudflare.com>
To: netdev@vger.kernel.org
Cc: davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org,
	pabeni@redhat.com, ncardwell@google.com, bpf@vger.kernel.org,
	ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
	eddyz87@gmail.com, memxor@gmail.com, linux-doc@vger.kernel.org,
	corbet@lwn.net, linux-kselftest@vger.kernel.org,
	shuah@kernel.org, linux-kernel@vger.kernel.org,
	kernel-team@cloudflare.com
Subject: [RFC PATCH net-next 4/4] selftests/bpf: add tcp_ts_id test
Date: Tue, 13 Oct 2026 10:39:06 +0000	[thread overview]
Message-ID: <20261013103906.2660384-5-wferguson@cloudflare.com> (raw)
In-Reply-To: <20261013103906.2660384-1-wferguson@cloudflare.com>

Exercise BPF_SOCK_OPS_TCP_TS_ID_CB end to end over IPv6 loopback.

A cgroup sock_ops program sets BPF_SOCK_OPS_TCP_TS_ID_CB_FLAG on the
listener and answers the callback. With tcp_timestamps=1 and
tcp_ts_id_bits=4 the test checks that:

 - the callback runs exactly once, on the SYN, with args[1] equal to
   the configured width;
 - the accepted socket reports the identifier and width through
   TCP_TS_ID;
 - the connecting socket reports no identifier, as only passive opens
   are covered;
 - a reply that does not fit in the width ("decline" subtest) leaves the
   accepted socket without an identifier.

The test skips when tcp_ts_id_bits does not exist and restores both
sysctls afterwards. It defines TCP_TS_ID locally when the libc headers
do not have it yet.

Signed-off-by: Willem Ferguson <wferguson@cloudflare.com>
---
 .../selftests/bpf/prog_tests/tcp_ts_id.c      | 150 ++++++++++++++++++
 .../selftests/bpf/progs/test_tcp_ts_id_kern.c |  32 ++++
 2 files changed, 182 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/tcp_ts_id.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_tcp_ts_id_kern.c

diff --git a/tools/testing/selftests/bpf/prog_tests/tcp_ts_id.c b/tools/testing/selftests/bpf/prog_tests/tcp_ts_id.c
new file mode 100644
index 000000000000..25c1f6d8dd43
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/tcp_ts_id.c
@@ -0,0 +1,150 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <errno.h>
+
+#include <test_progs.h>
+#include <network_helpers.h>
+
+#include "test_tcp_ts_id_kern.skel.h"
+
+#define LO_ADDR6		"::1"
+#define CG_NAME			"/tcp-ts-id-test"
+
+#ifndef TCP_TS_ID
+#define TCP_TS_ID		47
+#define TCP_TS_ID_BITS_SHIFT	8
+#endif
+
+#define TS_ID_BITS_SYSCTL	"/proc/sys/net/ipv4/tcp_ts_id_bits"
+#define TIMESTAMPS_SYSCTL	"/proc/sys/net/ipv4/tcp_timestamps"
+#define EXPECTED_TS_ID		7
+#define EXPECTED_TS_ID_BITS	4
+#define EXPECTED_TS_ID_BITS_STR	"4"
+
+static char saved_ts_id_bits[16];
+static char saved_timestamps[16];
+
+static int save_sysctl(const char *path, char *buf)
+{
+	int err = 0;
+	FILE *f;
+
+	f = fopen(path, "r");
+	if (!f)
+		return -errno;
+
+	if (fscanf(f, "%15s", buf) != 1)
+		err = -EINVAL;
+	fclose(f);
+
+	return err;
+}
+
+static void restore_sysctl(const char *path, const char *buf)
+{
+	if (buf[0])
+		write_sysctl(path, buf);
+}
+
+static void run_ts_id_test(int cg_fd, bool decline)
+{
+	int listen_fd = -1, cli_fd = -1, accept_fd = -1;
+	struct test_tcp_ts_id_kern *skel;
+	socklen_t optlen;
+	int ts_id;
+
+	skel = test_tcp_ts_id_kern__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+
+	skel->bss->ts_id_reply = EXPECTED_TS_ID;
+	skel->bss->ts_id_decline = decline;
+
+	skel->links.tcp_ts_id_sockops =
+		bpf_program__attach_cgroup(skel->progs.tcp_ts_id_sockops,
+					   cg_fd);
+	if (!ASSERT_OK_PTR(skel->links.tcp_ts_id_sockops, "attach_cgroup"))
+		goto out;
+
+	/* The listener has to be created after the attach to get LISTEN_CB. */
+	listen_fd = start_server(AF_INET6, SOCK_STREAM, LO_ADDR6, 0, 0);
+	if (!ASSERT_NEQ(listen_fd, -1, "start_server"))
+		goto out;
+
+	cli_fd = connect_to_fd(listen_fd, 0);
+	if (!ASSERT_NEQ(cli_fd, -1, "connect_to_fd"))
+		goto out;
+
+	accept_fd = accept(listen_fd, NULL, NULL);
+	if (!ASSERT_NEQ(accept_fd, -1, "accept"))
+		goto out;
+
+	ASSERT_EQ(skel->bss->ts_id_cb_count, 1, "cb_count");
+	ASSERT_EQ(skel->bss->ts_id_cb_syn, 1, "cb_on_syn");
+	ASSERT_EQ(skel->bss->ts_id_cb_bits, EXPECTED_TS_ID_BITS, "cb_bits");
+
+	/* The identifier must reach the child socket created by the SYN. */
+	optlen = sizeof(ts_id);
+	if (ASSERT_OK(getsockopt(accept_fd, IPPROTO_TCP, TCP_TS_ID, &ts_id,
+				 &optlen), "getsockopt(accept_fd, TCP_TS_ID)"))
+		ASSERT_EQ(ts_id, decline ? 0 :
+			  (EXPECTED_TS_ID_BITS << TCP_TS_ID_BITS_SHIFT) |
+			  EXPECTED_TS_ID, "server_ts_id");
+
+	/* Identifiers are passive-open only, so the opener must have none. */
+	optlen = sizeof(ts_id);
+	if (ASSERT_OK(getsockopt(cli_fd, IPPROTO_TCP, TCP_TS_ID, &ts_id,
+				 &optlen), "getsockopt(cli_fd, TCP_TS_ID)"))
+		ASSERT_EQ(ts_id, 0, "client_ts_id");
+out:
+	if (accept_fd != -1)
+		close(accept_fd);
+	if (cli_fd != -1)
+		close(cli_fd);
+	if (listen_fd != -1)
+		close(listen_fd);
+	test_tcp_ts_id_kern__destroy(skel);
+}
+
+void test_tcp_ts_id(void)
+{
+	int cg_fd = -1;
+	int err;
+
+	err = save_sysctl(TS_ID_BITS_SYSCTL, saved_ts_id_bits);
+	if (err == -ENOENT) {
+		test__skip();
+		return;
+	}
+	if (!ASSERT_OK(err, "read " TS_ID_BITS_SYSCTL))
+		return;
+
+	if (!ASSERT_OK(save_sysctl(TIMESTAMPS_SYSCTL, saved_timestamps),
+		       "read " TIMESTAMPS_SYSCTL))
+		goto out;
+
+	/* The callback only runs when timestamps are negotiated. */
+	if (!ASSERT_OK(write_sysctl(TIMESTAMPS_SYSCTL, "1"),
+		       "write " TIMESTAMPS_SYSCTL))
+		goto out;
+
+	/* Identifiers are disabled by default, so the test has to opt in. */
+	if (!ASSERT_OK(write_sysctl(TS_ID_BITS_SYSCTL, EXPECTED_TS_ID_BITS_STR),
+		       "write " TS_ID_BITS_SYSCTL))
+		goto out;
+
+	cg_fd = test__join_cgroup(CG_NAME);
+	if (!ASSERT_GE(cg_fd, 0, "test__join_cgroup(" CG_NAME ")"))
+		goto out;
+
+	if (test__start_subtest("assign"))
+		run_ts_id_test(cg_fd, false);
+
+	/* Only one sockops program may be attached to the cgroup at a time. */
+	if (test__start_subtest("decline"))
+		run_ts_id_test(cg_fd, true);
+out:
+	if (cg_fd != -1)
+		close(cg_fd);
+	restore_sysctl(TIMESTAMPS_SYSCTL, saved_timestamps);
+	restore_sysctl(TS_ID_BITS_SYSCTL, saved_ts_id_bits);
+}
diff --git a/tools/testing/selftests/bpf/progs/test_tcp_ts_id_kern.c b/tools/testing/selftests/bpf/progs/test_tcp_ts_id_kern.c
new file mode 100644
index 000000000000..07e4e56c4fb1
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_tcp_ts_id_kern.c
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+__u32 ts_id_cb_count;
+__u32 ts_id_cb_syn;
+__u32 ts_id_cb_bits;
+__u32 ts_id_reply;
+__u32 ts_id_decline;	/* set by userspace to exercise the decline path */
+
+SEC("sockops")
+int tcp_ts_id_sockops(struct bpf_sock_ops *skops)
+{
+	if (skops->op == BPF_SOCK_OPS_TCP_LISTEN_CB) {
+		bpf_sock_ops_cb_flags_set(skops,
+					  BPF_SOCK_OPS_TCP_TS_ID_CB_FLAG);
+		return 1;
+	}
+
+	if (skops->op != BPF_SOCK_OPS_TCP_TS_ID_CB)
+		return 1;
+
+	ts_id_cb_count++;
+	ts_id_cb_syn = !!(skops->skb_tcp_flags & (1U << 1));
+	ts_id_cb_bits = skops->args[1];
+	/* -1 does not fit in Arg2 bits, so the kernel assigns no ID. */
+	skops->reply = ts_id_decline ? -1 : ts_id_reply;
+
+	return 1;
+}
+
+char _license[] SEC("license") = "GPL";
-- 
2.47.3


      parent reply	other threads:[~2026-10-13 10:39 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-13 10:39 [RFC PATCH net-next 0/4] tcp: carry a BPF-chosen identifier in TSval low bits Willem Ferguson
2026-10-13 10:39 ` [RFC PATCH net-next 1/4] tcp: add plumbing to carry an " Willem Ferguson
2026-10-13 10:39 ` [RFC PATCH net-next 2/4] tcp: add BPF_SOCK_OPS_TCP_TS_ID_CB to assign a TSval identifier Willem Ferguson
2026-10-13 10:39 ` [RFC PATCH net-next 3/4] tcp: add TCP_TS_ID socket option Willem Ferguson
2026-10-13 10:39 ` Willem Ferguson [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261013103906.2660384-5-wferguson@cloudflare.com \
    --to=wferguson@cloudflare.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=corbet@lwn.net \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=eddyz87@gmail.com \
    --cc=edumazet@kernel.org \
    --cc=kernel-team@cloudflare.com \
    --cc=kuba@kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=memxor@gmail.com \
    --cc=ncardwell@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=shuah@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox