Netdev List
 help / color / mirror / Atom feed
From: Breno Leitao <leitao@debian.org>
To: "D. Wythe" <alibuda@linux.alibaba.com>,
	 Dust Li <dust.li@linux.alibaba.com>,
	 Sidraya Jayagond <sidraya@linux.ibm.com>,
	 Wenjia Zhang <wenjia@linux.ibm.com>,
	 Mahanta Jambigi <mjambigi@linux.ibm.com>,
	 Tony Lu <tonylu@linux.alibaba.com>,
	Wen Gu <guwen@linux.alibaba.com>,
	 "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	 Jakub Kicinski <kuba@kernel.org>,
	Paolo Abeni <pabeni@redhat.com>,  Simon Horman <horms@kernel.org>,
	Shuah Khan <shuah@kernel.org>
Cc: linux-rdma@vger.kernel.org, linux-s390@vger.kernel.org,
	 netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	 linux-kselftest@vger.kernel.org,
	Breno Leitao <leitao@debian.org>,
	 kernel-team@meta.com
Subject: [PATCH net-next 2/2] selftests: net: add SMC getsockopt_iter conversion test
Date: Fri, 05 Jun 2026 05:13:26 -0700	[thread overview]
Message-ID: <20260605-getsockopt_smc-v1-2-65da62fa44c4@debian.org> (raw)
In-Reply-To: <20260605-getsockopt_smc-v1-0-65da62fa44c4@debian.org>

Add a kselftest that exercises the SMC getsockopt() paths converted to
the getsockopt_iter() / sockopt_t callback:

- SOL_SMC options (SMC_LIMIT_HS), handled directly by smc_getsockopt(),
  which returns the int value through copy_to_iter() and reports the
  written length in opt->optlen.

- The CLC pass-through (e.g. SOL_TCP), where smc_getsockopt() forwards to
  the underlying TCP socket: optval is reconstructed from iter_out, the
  optlen pointer is forwarded, and the clamped length is mirrored back
  through opt->optlen. The oversized-buffer case (input optlen differs
  from output) specifically guards against a missing writeback sync.

Signed-off-by: Breno Leitao <leitao@debian.org>
---
 tools/testing/selftests/net/Makefile         |   1 +
 tools/testing/selftests/net/getsockopt_smc.c | 175 +++++++++++++++++++++++++++
 2 files changed, 176 insertions(+)

diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile
index 5ca6c557fc3f..5b50f718dbde 100644
--- a/tools/testing/selftests/net/Makefile
+++ b/tools/testing/selftests/net/Makefile
@@ -177,6 +177,7 @@ TEST_GEN_PROGS := \
 	bind_wildcard \
 	epoll_busy_poll \
 	getsockopt_iter \
+	getsockopt_smc \
 	icmp_rfc4884 \
 	ipv6_fragmentation \
 	proc_net_pktgen \
diff --git a/tools/testing/selftests/net/getsockopt_smc.c b/tools/testing/selftests/net/getsockopt_smc.c
new file mode 100644
index 000000000000..239deefb3187
--- /dev/null
+++ b/tools/testing/selftests/net/getsockopt_smc.c
@@ -0,0 +1,175 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Exercise the SMC getsockopt() paths that were converted to the
+ * getsockopt_iter() / sockopt_t callback.
+ *
+ * Two distinct paths are covered:
+ *
+ *   - SOL_SMC options (SMC_LIMIT_HS) are handled directly by
+ *     smc_getsockopt(), which returns the int value through copy_to_iter()
+ *     and reports the written length in opt->optlen.
+ *
+ *   - Other levels (e.g. SOL_TCP) are forwarded to the underlying CLC (TCP)
+ *     socket, whose getsockopt() still operates on __user buffers. The
+ *     converted smc_getsockopt() reconstructs the userspace optval from
+ *     iter_out, forwards the original optlen pointer, and mirrors the length
+ *     the clcsock reported back into opt->optlen so the core writes the right
+ *     value to userspace.
+ *
+ * The kernel-buffer (kvec) path of the CLC pass-through returns -EOPNOTSUPP
+ * and is not reachable from a userspace getsockopt(), so it is not tested
+ * here.
+ *
+ * Author: Breno Leitao <leitao@debian.org>
+ */
+#include <errno.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <sys/socket.h>
+
+#include "kselftest_harness.h"
+
+#ifndef AF_SMC
+#define AF_SMC 43
+#endif
+#ifndef SMCPROTO_SMC
+#define SMCPROTO_SMC 0
+#endif
+#ifndef SOL_SMC
+#define SOL_SMC 286
+#endif
+#ifndef SMC_LIMIT_HS
+#define SMC_LIMIT_HS 1
+#endif
+
+FIXTURE(smc) {
+	int fd;
+};
+
+FIXTURE_SETUP(smc)
+{
+	self->fd = socket(AF_SMC, SOCK_STREAM, SMCPROTO_SMC);
+	if (self->fd < 0)
+		SKIP(return, "AF_SMC unavailable (errno %d) - load the smc module",
+		     errno);
+}
+
+FIXTURE_TEARDOWN(smc)
+{
+	if (self->fd >= 0)
+		close(self->fd);
+}
+
+/* ---------- SOL_SMC: handled directly by smc_getsockopt() ---------- */
+
+/* SMC_LIMIT_HS is reported back as a 4-byte int via copy_to_iter(). */
+TEST_F(smc, limit_hs_default)
+{
+	socklen_t optlen = sizeof(int);
+	int val = 0xdeadbeef;
+
+	ASSERT_EQ(0, getsockopt(self->fd, SOL_SMC, SMC_LIMIT_HS, &val, &optlen));
+	EXPECT_EQ(sizeof(int), optlen);
+	EXPECT_TRUE(val == 0 || val == 1);
+}
+
+/* A value set via setsockopt() must be readable back unchanged. */
+TEST_F(smc, limit_hs_set_get)
+{
+	socklen_t optlen = sizeof(int);
+	int val = 1;
+
+	ASSERT_EQ(0, setsockopt(self->fd, SOL_SMC, SMC_LIMIT_HS, &val, optlen));
+
+	val = -1;
+	ASSERT_EQ(0, getsockopt(self->fd, SOL_SMC, SMC_LIMIT_HS, &val, &optlen));
+	EXPECT_EQ(sizeof(int), optlen);
+	EXPECT_EQ(1, val);
+}
+
+/* setsockopt() stores !!val, so a non-1 truthy value reads back as 1. */
+TEST_F(smc, limit_hs_set_get_clear)
+{
+	socklen_t optlen = sizeof(int);
+	int val = 0;
+
+	ASSERT_EQ(0, setsockopt(self->fd, SOL_SMC, SMC_LIMIT_HS, &val, optlen));
+
+	val = -1;
+	ASSERT_EQ(0, getsockopt(self->fd, SOL_SMC, SMC_LIMIT_HS, &val, &optlen));
+	EXPECT_EQ(sizeof(int), optlen);
+	EXPECT_EQ(0, val);
+}
+
+/* An oversized buffer is clamped: optlen is reported back as sizeof(int). */
+TEST_F(smc, limit_hs_oversize_clamped)
+{
+	socklen_t optlen;
+	char buf[16] = {};
+
+	optlen = sizeof(buf);
+	ASSERT_EQ(0, getsockopt(self->fd, SOL_SMC, SMC_LIMIT_HS, buf, &optlen));
+	EXPECT_EQ(sizeof(int), optlen);
+}
+
+/* An unknown SOL_SMC option is rejected with -EOPNOTSUPP. */
+TEST_F(smc, bad_optname)
+{
+	socklen_t optlen = sizeof(int);
+	int val;
+
+	ASSERT_EQ(-1, getsockopt(self->fd, SOL_SMC, 0x7fff, &val, &optlen));
+	EXPECT_EQ(EOPNOTSUPP, errno);
+}
+
+/* ---------- CLC pass-through: forwarded to the underlying TCP socket ------ */
+
+/* A TCP option set on the SMC socket is applied to the CLC socket and must be
+ * readable back through the pass-through, exercising optval reconstruction.
+ */
+TEST_F(smc, clc_tcp_nodelay_set_get)
+{
+	socklen_t optlen = sizeof(int);
+	int val = 1;
+
+	ASSERT_EQ(0, setsockopt(self->fd, IPPROTO_TCP, TCP_NODELAY,
+				&val, optlen));
+
+	val = -1;
+	ASSERT_EQ(0, getsockopt(self->fd, IPPROTO_TCP, TCP_NODELAY,
+				&val, &optlen));
+	EXPECT_EQ(sizeof(int), optlen);
+	EXPECT_EQ(1, val);
+}
+
+/* With an oversized buffer the clcsock clamps the reported length to
+ * sizeof(int). That length is produced by the clcsock writing the user optlen
+ * pointer, and must be mirrored back through opt->optlen; since the input
+ * optlen (16) differs from the output (4), this fails if the writeback sync
+ * in smc_getsockopt() is missing.
+ */
+TEST_F(smc, clc_tcp_nodelay_oversize_clamped)
+{
+	socklen_t optlen;
+	char buf[16] = {};
+
+	optlen = sizeof(buf);
+	ASSERT_EQ(0, getsockopt(self->fd, IPPROTO_TCP, TCP_NODELAY,
+				buf, &optlen));
+	EXPECT_EQ(sizeof(int), optlen);
+}
+
+/* An error from the clcsock (unknown TCP option) is propagated unchanged. */
+TEST_F(smc, clc_bad_optname)
+{
+	socklen_t optlen = sizeof(int);
+	int val;
+
+	ASSERT_EQ(-1, getsockopt(self->fd, IPPROTO_TCP, 0x7fff, &val, &optlen));
+	EXPECT_EQ(ENOPROTOOPT, errno);
+}
+
+TEST_HARNESS_MAIN

-- 
2.53.0-Meta


      parent reply	other threads:[~2026-06-05 12:13 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-05 12:13 [PATCH net-next 0/2] net/smc: convert getsockopt to getsockopt_iter Breno Leitao
2026-06-05 12:13 ` [PATCH net-next 1/2] smc: convert " Breno Leitao
2026-06-08 10:48   ` Breno Leitao
2026-06-05 12:13 ` Breno Leitao [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=20260605-getsockopt_smc-v1-2-65da62fa44c4@debian.org \
    --to=leitao@debian.org \
    --cc=alibuda@linux.alibaba.com \
    --cc=davem@davemloft.net \
    --cc=dust.li@linux.alibaba.com \
    --cc=edumazet@google.com \
    --cc=guwen@linux.alibaba.com \
    --cc=horms@kernel.org \
    --cc=kernel-team@meta.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=mjambigi@linux.ibm.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=shuah@kernel.org \
    --cc=sidraya@linux.ibm.com \
    --cc=tonylu@linux.alibaba.com \
    --cc=wenjia@linux.ibm.com \
    /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