[PATCH mptcp-next v14 05/12] selftests: mptcp: sockopt: add is_mptcp_socket helper

Geliang Tang <[email protected]> Thu, 30 Jul 2026 11:15:16 +0800
Newsgroups dev.linux.lists.mptcp
Message-ID <c62aa1281f35a9cdee1f8eba4a6e0859615633a8.1785380422.git.tanggeliang@kylinos.cn>
From: Geliang Tang <[email protected]>

Add a small is_mptcp_socket() helper that issues getsockopt(TCP_IS_MPTCP)
and returns whether the fd belongs to an MPTCP socket.

Use it to gate the MPTCP-specific assertions: skip do_getsockopts()
entirely for plain TCP or fallback MPTCP sockets, and skip mptcpi_rcv_delta
checks in connect_one_server()/process_one_client() when the socket is not
an MPTCP socket.

Signed-off-by: Geliang Tang <[email protected]>
---
 .../selftests/net/mptcp/mptcp_sockopt.c       | 31 +++++++++++++++++--
 1 file changed, 28 insertions(+), 3 deletions(-)

diff --git a/tools/testing/selftests/net/mptcp/mptcp_sockopt.c b/tools/testing/selftests/net/mptcp/mptcp_sockopt.c
index 3a7fcef5aa54..822cbf3de3e6 100644
--- a/tools/testing/selftests/net/mptcp/mptcp_sockopt.c
+++ b/tools/testing/selftests/net/mptcp/mptcp_sockopt.c
@@ -35,6 +35,9 @@ static int pf = AF_INET;
 #ifndef SOL_MPTCP
 #define SOL_MPTCP 284
 #endif
+#ifndef TCP_IS_MPTCP
+#define TCP_IS_MPTCP 76
+#endif
 
 #ifndef MPTCP_INFO
 struct mptcp_info {
@@ -557,8 +560,28 @@ static void do_getsockopt_mptcp_full_info(struct so_state *s, int fd)
 	assert(!memcmp(&sfinfo->addrs, &s->addrs, sizeof(struct mptcp_subflow_addrs)));
 }
 
+static int is_mptcp_socket(int fd)
+{
+	int is_mptcp = 0;
+	socklen_t optlen;
+
+	optlen = sizeof(is_mptcp);
+	if (getsockopt(fd, IPPROTO_TCP, TCP_IS_MPTCP, &is_mptcp, &optlen) < 0) {
+		if (errno == ENOPROTOOPT) {
+			perror("TCP_IS_MPTCP test skipped");
+			return 0;
+		}
+		xerror("getsockopt TCP_IS_MPTCP");
+	}
+
+	return is_mptcp;
+}
+
 static void do_getsockopts(struct so_state *s, int fd, size_t r, size_t w)
 {
+	if (!is_mptcp_socket(fd))
+		return;
+
 	do_getsockopt_mptcp_info(s, fd, w);
 
 	do_getsockopt_tcp_info(s, fd, r, w);
@@ -634,7 +657,8 @@ static void connect_one_server(int fd, int pipefd)
 	if (eof)
 		total += 1; /* sequence advances due to FIN */
 
-	assert(s.mptcpi_rcv_delta == (uint64_t)total);
+	if (is_mptcp_socket(fd))
+		assert(s.mptcpi_rcv_delta == (uint64_t)total);
 	close(fd);
 }
 
@@ -683,8 +707,9 @@ static void process_one_client(int fd, int pipefd)
 	r += ret;
 
 	do_getsockopts(&s, fd, r, w);
-	check_stat_equal("mptcpi_rcv_delta", s.mptcpi_rcv_delta,
-			 (uint64_t)r + 1); /* +1 for FIN */
+	if (is_mptcp_socket(fd))
+		check_stat_equal("mptcpi_rcv_delta", s.mptcpi_rcv_delta,
+				 (uint64_t)r + 1); /* +1 for FIN */
 
 	/* be nice when running on top of older kernel */
 	if (s.pkt_stats_avail) {
-- 
2.53.0