Thread View: linux.kernel.netdev
3 messages
3 total messages
Started by Breno Leitao
Thu, 12 Nov 2026 17:05
[PATCH net-next 3/3] selftests: net: getsockopt_iter: cover tcp
Author: Breno Leitao
Date: Thu, 12 Nov 2026 17:05
Date: Thu, 12 Nov 2026 17:05
284 lines
17242 bytes
17242 bytes
Add a fixture for do_tcp_getsockopt(). The setup keeps both ends of a
loopback connection, with TCP_SAVE_SYN set on the listener, so the
accepted socket has a saved SYN to report.
TCP_INFO and TCP_CONGESTION cover clamping the reply down to what fits,
TCP_ULP covers reporting zero and writing nothing, and TCP_SAVED_SYN
covers the length-then-EINVAL path, which the second half of that test
uses to read the SYN back.
TCP_ZEROCOPY_RECEIVE reads its struct from optval and writes it back.
With nothing queued it maps nothing and succeeds, so the tests cover
the 16-byte struct glibc still declares, a larger buffer cut down to
the kernel's struct, and one whose extra tail is not zero.
Signed-off-by: Breno Leitao <leitao@debian.org>
---
tools/testing/selftests/net/getsockopt_iter.c | 245 ++++++++++++++++++++++++++
1 file changed, 245 insertions(+)
diff --git a/tools/testing/selftests/net/getsockopt_iter.c b/tools/testing/selftests/net/getsockopt_iter.c
index 2367fce226d4cc..4e51c8aa6a8555 100644
--- a/tools/testing/selftests/net/getsockopt_iter.c
+++ b/tools/testing/selftests/net/getsockopt_iter.c
@@ -55,6 +55,9 @@
#ifndef TCP_ULP
#define TCP_ULP 31
#endif
+#ifndef TCP_CA_NAME_MAX
+#define TCP_CA_NAME_MAX 16
+#endif
/* linux/mroute.h does not coexist with netinet/in.h here. */
#ifndef MRT_VERSION
#define MRT_VERSION 206
@@ -1005,4 +1008,246 @@ TEST_F(tls, bad_optname)
ASSERT_EQ(ENOPROTOOPT, errno);
}
+/* ---------- tcp ---------- */
+
+FIXTURE(tcp)
+{
+ int fd; /* connected client */
+ int sfd; /* accepted server side, its SYN was saved */
+};
+
+FIXTURE_SETUP(tcp)
+{
+ struct sockaddr_in a = {
+ .sin_family = AF_INET,
+ .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
+ };
+ socklen_t alen = sizeof(a);
+ int one = 1;
+ int lfd;
+
+ self->fd = -1;
+ self->sfd = -1;
+
+ lfd = socket(AF_INET, SOCK_STREAM, 0);
+ if (lfd < 0)
+ SKIP(return, "TCP socket: %s", strerror(errno));
+ setsockopt(lfd, IPPROTO_TCP, TCP_SAVE_SYN, &one, sizeof(one));
+ if (bind(lfd, (struct sockaddr *)&a, sizeof(a)) || listen(lfd, 1) ||
+ getsockname(lfd, (struct sockaddr *)&a, &alen)) {
+ close(lfd);
+ SKIP(return, "listener setup: %s", strerror(errno));
+ }
+ self->fd = socket(AF_INET, SOCK_STREAM, 0);
+ if (self->fd < 0) {
+ close(lfd);
+ SKIP(return, "TCP socket: %s", strerror(errno));
+ }
+ if (connect(self->fd, (struct sockaddr *)&a, sizeof(a))) {
+ close(lfd);
+ SKIP(return, "connect: %s", strerror(errno));
+ }
+ self->sfd = accept(lfd, NULL, NULL);
+ close(lfd);
+ if (self->sfd < 0)
+ SKIP(return, "accept: %s", strerror(errno));
+}
+
+FIXTURE_TEARDOWN(tcp)
+{
+ if (self->fd >= 0)
+ close(self->fd);
+ if (self->sfd >= 0)
+ close(self->sfd);
+}
+
+TEST_F(tcp, maxseg_exact)
+{
+ socklen_t optlen = sizeof(int);
+ int val = -1;
+
+ ASSERT_EQ(0, getsockopt(self->fd, IPPROTO_TCP, TCP_MAXSEG, &val,
+ &optlen));
+ ASSERT_EQ(sizeof(int), optlen);
+ ASSERT_GT(val, 0);
+}
+
+/* Unlike IPv4, TCP clamps the reply rather than shrinking it to a byte. */
+TEST_F(tcp, nodelay_undersize_clamped)
+{
+ socklen_t optlen = 1;
+ int val = 0;
+
+ ASSERT_EQ(0, getsockopt(self->fd, IPPROTO_TCP, TCP_NODELAY, &val,
+ &optlen));
+ ASSERT_EQ(1, optlen);
+}
+
+TEST_F(tcp, info_exact)
+{
+ struct tcp_info info;
+ socklen_t optlen = sizeof(info);
+
+ ASSERT_EQ(0, getsockopt(self->fd, IPPROTO_TCP, TCP_INFO, &info,
+ &optlen));
+ ASSERT_EQ(sizeof(info), optlen);
+ ASSERT_EQ(TCP_ESTABLISHED, info.tcpi_state);
+}
+
+TEST_F(tcp, info_short_clamped)
+{
+ struct tcp_info info;
+ socklen_t optlen = 8;
+
+ ASSERT_EQ(0, getsockopt(self->fd, IPPROTO_TCP, TCP_INFO, &info,
+ &optlen));
+ ASSERT_EQ(8, optlen);
+ ASSERT_EQ(TCP_ESTABLISHED, info.tcpi_state);
+}
+
+TEST_F(tcp, congestion_exact)
+{
+ socklen_t optlen = TCP_CA_NAME_MAX;
+ char name[TCP_CA_NAME_MAX] = {};
+
+ ASSERT_EQ(0, getsockopt(self->fd, IPPROTO_TCP, TCP_CONGESTION, name,
+ &optlen));
+ ASSERT_EQ(TCP_CA_NAME_MAX, optlen);
+ ASSERT_NE(0, name[0]);
+}
+
+TEST_F(tcp, congestion_short_clamped)
+{
+ socklen_t optlen = 4;
+ char name[4] = {};
+
+ ASSERT_EQ(0, getsockopt(self->fd, IPPROTO_TCP, TCP_CONGESTION, name,
+ &optlen));
+ ASSERT_EQ(4, optlen);
+ ASSERT_NE(0, name[0]);
+}
+
+/* No ULP attached: length reported as zero, nothing written. */
+TEST_F(tcp, ulp_none)
+{
+ socklen_t optlen = 16;
+ char buf[16];
+
+ memset(buf, 0x5a, sizeof(buf));
+ ASSERT_EQ(0, getsockopt(self->fd, IPPROTO_TCP, TCP_ULP, buf, &optlen));
+ ASSERT_EQ(0, optlen);
+ ASSERT_EQ(0x5a, (unsigned char)buf[0]);
+}
+
+TEST_F(tcp, repair_window_wrong_len)
+{
+ socklen_t optlen = 4;
+ char buf[4];
+
+ ASSERT_EQ(-1, getsockopt(self->fd, IPPROTO_TCP, TCP_REPAIR_WINDOW, buf,
+ &optlen));
+ ASSERT_EQ(EINVAL, errno);
+}
+
+TEST_F(tcp, zerocopy_receive_short)
+{
+ socklen_t optlen = 4;
+ char buf[4] = {};
+
+ ASSERT_EQ(-1, getsockopt(self->fd, IPPROTO_TCP, TCP_ZEROCOPY_RECEIVE,
+ buf, &optlen));
+ ASSERT_EQ(EINVAL, errno);
+}
+
+/* Nothing queued: the call maps nothing and succeeds. */
+TEST_F(tcp, zerocopy_receive_old_struct)
+{
+ struct tcp_zerocopy_receive zc = {};
+ socklen_t optlen = sizeof(zc);
+
+ ASSERT_EQ(0, getsockopt(self->fd, IPPROTO_TCP, TCP_ZEROCOPY_RECEIVE,
+ &zc, &optlen));
+ ASSERT_EQ(sizeof(zc), optlen);
+ ASSERT_EQ(0, zc.length);
+}
+
+/* A larger struct is cut down to the kernel's, if its tail is zero. */
+TEST_F(tcp, zerocopy_receive_oversize_clamped)
+{
+ char buf[256] = {};
+ socklen_t optlen = sizeof(buf);
+
+ ASSERT_EQ(0, getsockopt(self->fd, IPPROTO_TCP, TCP_ZEROCOPY_RECEIVE,
+ buf, &optlen));
+ ASSERT_GE(optlen, sizeof(struct tcp_zerocopy_receive));
+ ASSERT_LT(optlen, sizeof(buf));
+}
+
+TEST_F(tcp, zerocopy_receive_oversize_dirty)
+{
+ char buf[256] = {};
+ socklen_t optlen = sizeof(buf);
+
+ buf[sizeof(buf) - 1] = 1;
+ ASSERT_EQ(-1, getsockopt(self->fd, IPPROTO_TCP, TCP_ZEROCOPY_RECEIVE,
+ buf, &optlen));
+ ASSERT_EQ(EINVAL, errno);
+ ASSERT_EQ(sizeof(buf), optlen);
+}
+
+/* The client never saved a SYN, so there is nothing to report. */
+TEST_F(tcp, saved_syn_none)
+{
+ socklen_t optlen = 128;
+ char buf[128];
+
+ memset(buf, 0x5a, sizeof(buf));
+ ASSERT_EQ(0, getsockopt(self->fd, IPPROTO_TCP, TCP_SAVED_SYN, buf,
+ &optlen));
+ ASSERT_EQ(0, optlen);
+ ASSERT_EQ(0x5a, (unsigned char)buf[0]);
+}
+
+/* A short buffer gets the required length back along with EINVAL, and the
+ * SYN is still there for the retry.
+ */
+TEST_F(tcp, saved_syn_short_reports_len)
+{
+ socklen_t optlen = 1;
+ char buf[256];
+ socklen_t need;
+
+ ASSERT_EQ(-1, getsockopt(self->sfd, IPPROTO_TCP, TCP_SAVED_SYN, buf,
+ &optlen));
+ ASSERT_EQ(EINVAL, errno);
+ ASSERT_GT(optlen, 1);
+ ASSERT_LE(optlen, sizeof(buf));
+ need = optlen;
+
+ ASSERT_EQ(0, getsockopt(self->sfd, IPPROTO_TCP, TCP_SAVED_SYN, buf,
+ &optlen));
+ ASSERT_EQ(need, optlen);
+}
+
+TEST_F(tcp, negative_optlen)
+{
+ socklen_t optlen = (socklen_t)-1;
+ int val;
+
+ ASSERT_EQ(-1, getsockopt(self->fd, IPPROTO_TCP, TCP_MAXSEG, &val,
+ &optlen));
+ ASSERT_EQ(EINVAL, errno);
+}
+
+TEST_F(tcp, bad_optname)
+{
+ socklen_t optlen = sizeof(int);
+ int val;
+
+ ASSERT_EQ(-1, getsockopt(self->fd, IPPROTO_TCP, 0x7fff, &val,
+ &optlen));
+ ASSERT_EQ(ENOPROTOOPT, errno);
+ ASSERT_EQ(sizeof(int), optlen);
+}
+
TEST_HARNESS_MAIN
--
2.53.0-Meta
Re: [PATCH net-next 3/3] selftests: net: getsockopt_iter: cover tcp
Author: Stanislav Fomichev
Date: Fri, 13 Nov 2026 20:22
Date: Fri, 13 Nov 2026 20:22
19 lines
12550 bytes
12550 bytes
On 11/12, Breno Leitao wrote: > Add a fixture for do_tcp_getsockopt(). The setup keeps both ends of a > loopback connection, with TCP_SAVE_SYN set on the listener, so the > accepted socket has a saved SYN to report. > > TCP_INFO and TCP_CONGESTION cover clamping the reply down to what fits, > TCP_ULP covers reporting zero and writing nothing, and TCP_SAVED_SYN > covers the length-then-EINVAL path, which the second half of that test > uses to read the SYN back. > > TCP_ZEROCOPY_RECEIVE reads its struct from optval and writes it back. > With nothing queued it maps nothing and succeeds, so the tests cover > the 16-byte struct glibc still declares, a larger buffer cut down to > the kernel's struct, and one whose extra tail is not zero. > > Signed-off-by: Breno Leitao <leitao@debian.org> Acked-by: Stanislav Fomichev <sdf@fomichev.me>
Re: [PATCH net-next 3/3] selftests: net: getsockopt_iter: cover tcp
Author: netdev-bot+sashiko
Date: Sun, 15 Nov 2026 17:01
Date: Sun, 15 Nov 2026 17:01
111 lines
11845 bytes
11845 bytes
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] In tools/testing/selftests/net/getsockopt_iter.c, the test
tcp/nodelay_undersize_clamped is commented "Unlike IPv4, TCP clamps…
- [Low] In tools/testing/selftests/net/getsockopt_iter.c, the test
tcp/zerocopy_receive_oversize_clamped is commented "A larger struct is…
--
selftests: net: getsockopt_iter: cover tcp
This commit adds a tcp fixture to the getsockopt_iter selftest. The fixture
sets up a loopback connection with TCP_SAVE_SYN on the listener. The new
tests cover do_tcp_getsockopt() clamping, zero-length replies, the
TCP_SAVED_SYN length-then-EINVAL path, and the TCP_ZEROCOPY_RECEIVE struct
size handling.
> diff --git a/tools/testing/selftests/net/getsockopt_iter.c b/tools/testing/selftests/net/getsockopt_iter.c
> index 2367fce226d4c..4e51c8aa6a855 100644
> --- a/tools/testing/selftests/net/getsockopt_iter.c
> +++ b/tools/testing/selftests/net/getsockopt_iter.c
[ ... ]
> +/* Unlike IPv4, TCP clamps the reply rather than shrinking it to a byte. */
> +TEST_F(tcp, nodelay_undersize_clamped)
> +{
> + socklen_t optlen = 1;
> + int val = 0;
> +
> + ASSERT_EQ(0, getsockopt(self->fd, IPPROTO_TCP, TCP_NODELAY, &val,
> + &optlen));
> + ASSERT_EQ(1, optlen);
> +}
[Severity: Low]
Does this test actually show the difference the comment describes?
With optlen = 1, both paths report the same length. do_tcp_getsockopt()
clamps:
net/ipv4/tcp.c:do_tcp_getsockopt() {
...
len = min_t(unsigned int, len, sizeof(int));
...
}
and do_ip_getsockopt() also ends up with optlen = 1:
net/ipv4/ip_sockglue.c:do_ip_getsockopt() {
...
if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
unsigned char ucval = (unsigned char)val;
sopt->optlen = 1;
...
}
So if TCP started shrinking sub-int replies to a single byte the way SOL_IP
does, this test would still pass.
The ip/ttl_single_byte and ipv6/hops_sub_int tests in the same file use a
3-byte buffer, which does tell the two apart. Would optlen = 2 or 3, with a
check that optlen is unchanged, match the intent of the comment better?
[ ... ]
> +/* A larger struct is cut down to the kernel's, if its tail is zero. */
> +TEST_F(tcp, zerocopy_receive_oversize_clamped)
> +{
> + char buf[256] = {};
> + socklen_t optlen = sizeof(buf);
> +
> + ASSERT_EQ(0, getsockopt(self->fd, IPPROTO_TCP, TCP_ZEROCOPY_RECEIVE,
> + buf, &optlen));
> + ASSERT_GE(optlen, sizeof(struct tcp_zerocopy_receive));
> + ASSERT_LT(optlen, sizeof(buf));
> +}
[Severity: Low]
Should this assert the exact clamped length? When the tail is zero, the
kernel sets optlen to the size of its own struct:
net/ipv4/tcp.c:do_tcp_getsockopt() {
...
if (unlikely(len > sizeof(zc))) {
...
len = sizeof(zc);
sopt->optlen = len;
...
}
That is the uapi struct tcp_zerocopy_receive from include/uapi/linux/tcp.h,
which is 64 bytes.
The test includes netinet/tcp.h, so sizeof(struct tcp_zerocopy_receive) here
is the 16-byte glibc struct the commit message mentions. That means any
optlen from 16 to 255 passes. A clamp to the wrong size, such as one of the
older 16 or 40 byte layouts, would not be caught.
raw/icmpfilter_oversize_clamped in the same file uses ASSERT_EQ against the
exact struct size. Could this test do the same with the kernel struct size
(64)?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261112-getsockopt_phase7-v1-0-1eb759dd0929%40debian.org
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads