selftests: consolidate TCP_INQ testing into sockopt
Subsystems (3)
Geliang Tang
reviewerv13 cover selftests: consolidate TCP_INQ testing into sockopt
From: Geliang Tang <[email protected]>
This series consolidates the TCP_INQ test functionality from mptcp_inq into mptcp_sockopt, simplifying the test suite while maintaining full test coverage.
The first version of this series was sent out a year ago. Recently, I refactored it and addressed all the review comments from Sashiko. During this process, I fixed several bugs in the original code, so this is a much better version.
All CI tests are currently passing, including Sashiko and nipa/ai-review. The two checkpatch warnings are false positives. In addition, I've also run extensive loop stability tests, and they all passed as well.
v13:
- Patch 1: seed RNG per child in init_rng fallback. The previous fallback 'srand(time(NULL))' collided for two children forked in the same second (1-second resolution); mixing in getpid() (per-process unique) gives each child an independent seed even when /dev/urandom is unavailable.
- Patch 5: fail loud on legacy kernels lacking TCP_IS_MPTCP. Previously, getsockopt() returning -1 left is_mptcp at 0 and do_getsockopts() saw 0 and silently skipped every MPTCP-specific assert. Initialize is_mptcp to -1 and die_perror() on getsockopt failure, and update call sites to handle the tri-state return ('<= 0' / '> 0').
v12:
- Patch 6 is the only one updated. It no longer uses
proto_tx == IPPROTO_MPTCP && proto_rx == IPPROTO_MPTCP
to check for MPTCP sockets - now it uses TCP_IS_MPTCP via a new helper
in patch 5, which is more accurate. This also makes it work for future TCP_MD5SIG tests that use fallback MPTCP sockets.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
v11:
- Drop "replace /dev/urandom with getrandom".
- Add check_stat_equal() helper to centralize and simplify MPTCP stats validation.
- Fix cmsg_len format specifier in get_tcp_inq() (%u -> %zu) to match
size_t type.
- Fix format specifier in EOF error message (%lu -> %zd) for ssize_t
type.
- Add poll() with POLLRDHUP in client_huge_transfer() to wait for FIN
arrival before reading the final byte, preventing flaky EOF detection.
- Pass total to server_huge_transfer(), instead of len.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
v10:
- Patch 1, a new fix.
- Patch 3, change the type of r/w as size_t.
- Patch 12, in client_huge_transfer(), do a blocking read for EOF on
unixfd instead of sleeping.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
v9:
- No code changes compared to v8.
- To address Sashiko's complaints that the implementation of server_huge_transfer() was split across several patches, the patch
order has been rearranged, some patches have been squashed into
fewer ones, and the commit logs have been updated.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
v8:
- patch 2, a new patch to replace /dev/urandom with getrandom().
- patch 4, check against the accumulated total r instead of ret in process_one_client().
- patch 7 and patch 9, update commit logs.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
v7:
- Add a new patch to reseed RNG in child after fork.
- Add a new patch to fix bytes_acked validation against sent bytes.
- Add a new patch to use r/w for byte counts in process_one_client().
- Split v6-0004 into two separate patches: read exact payload length,
and pass len over unixfd.
- Change socketpair type from SOCK_DGRAM to SOCK_STREAM to avoid
indefinite blocking when peer exits.
- Fully initialize buf in server_huge_transfer() to silence MSAN/Valgrind warnings.
- Use a union containing struct cmsghdr for control message buffers to
prevent unaligned access on strict-alignment architectures.
- Reorder and renumber patches accordingly.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
v6:
- A new patch to use recvmsg instead of read.
- Squash the four original patches into two.
- Reset msg.msg_controllen.
- Check cmsg_len in get_tcp_inq.
- Check return value of client_huge_transfer.
- Address other comments from Sashiko.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
v5:
- Continue using the approach from v3 and fix the instability issues.
This version has undergone extensive loop testing.
- The new two separate functions, server_huge_transfer() and client_huge_transfer(), improve code structure.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
v4:
- v3 changed the behavior of process_one_client and connect_one_server,
causing intermittent failures during loop testing. Revert to using the
v2 approach, along with some cleanups.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
v3:
- No longer using process_one_client_inq() and connect_one_server_inq()
for switching; instead, the inq-related code has been merged into process_one_client() and connect_one_server().
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
v2:
- patch 4, a new patch, add do_setsockopt_inq helper.
- cleanups.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
v1:
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
Geliang Tang (12):
selftests: mptcp: sockopt: reseed RNG after fork
selftests: mptcp: sockopt: use exact length in connect_one_server
selftests: mptcp: sockopt: add check_stat_equal helper
selftests: mptcp: sockopt: use size_t for byte counters
selftests: mptcp: sockopt: add is_mptcp_socket helper
selftests: mptcp: sockopt: add tx/rx protocol options
selftests: mptcp: sockopt: add TCP_INQ test option
selftests: mptcp: sockopt: use unix socket instead of pipe
selftests: mptcp: sockopt: use recvmsg instead of read
selftests: mptcp: sockopt: get and verify TCP_INQ
selftests: mptcp: sockopt: add huge data transfer tests
selftests: mptcp: sockopt: replace mptcp_inq with sockopt
tools/testing/selftests/net/mptcp/.gitignore | 1 - tools/testing/selftests/net/mptcp/Makefile | 1 - tools/testing/selftests/net/mptcp/mptcp_inq.c | 614 ------------------ .../selftests/net/mptcp/mptcp_sockopt.c | 463 +++++++++++-- .../selftests/net/mptcp/mptcp_sockopt.sh | 8 +- 5 files changed, 407 insertions(+), 680 deletions(-) delete mode 100644 tools/testing/selftests/net/mptcp/mptcp_inq.c
Signature · 1 line
2.53.0
Geliang Tang
reviewerPATCH v13 1/12 selftests: mptcp: sockopt: reseed RNG after fork
From: Geliang Tang <[email protected]>
xfork() does not reseed the RNG in the child, so server and client processes use the same rand() sequence. Restore the per-fork reseed from mptcp_inq.c by calling init_rng() in the child, ensuring independent random values for buffer contents and transfer sizes.
init_rng() is now called from each forked child (server, client) so that rand() sequences don't diverge from a single parent seed. But the fallback path did 'srand(time(NULL))' - and time(NULL) has 1-second resolution, so the two children spawned back-to-back in main() almost always land in the same second and end up with the same seed, defeating the per-child re-seed.
Mix in getpid() (per-process unique, kernel-guaranteed) with time(NULL) (cross-second entropy) so the fallback produces an independent seed for each child even when /dev/urandom is unavailable.
Also hoist the 'foo' local to function scope and collapse the two srand() callsites into a single one at the end, so both branches consistently write the seed into 'foo' first.
--- tools/testing/selftests/net/mptcp/mptcp_sockopt.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
tools/testing/selftests/net/mptcp/mptcp_sockopt.c +7 -3
@@ -128,6 +128,8 @@ struct so_state {#define MIN (a, b) ((a) < (b) ? (a) : (b))#endif +static void init_rng (void ); +static void __noreturn die_perror (const char *msg) {perror (msg);
@@ -799,6 +801,8 @@ static pid_t xfork(void)if (p <0 )die_perror ("fork" ); +else if (p ==0 ) +init_rng ();return p; }
@@ -822,9 +826,9 @@ static int rcheck(int wstatus, const char *what)static void init_rng (void ) {int fd =open ("/dev/urandom" ,O_RDONLY ); +unsigned int foo;if (fd >=0 ) { -unsigned int foo;ssize_t ret;/* can't fail */
@@ -832,10 +836,10 @@ static void init_rng(void)assert (ret ==sizeof (foo));close (fd); -srand (foo); }else { -srand (time (NULL )); + foo =getpid () ^time (NULL ); } +srand (foo); }int main (int argc,char *argv[])
Signature · 1 line
2.53.0
Geliang Tang
reviewerPATCH v13 2/12 selftests: mptcp: sockopt: use exact length in connect_one_server
In reply to Geliang TangFrom: Geliang Tang <[email protected]>
Use the exact remaining length ('len - total') when reading the echoed data from the socket, instead of the full buffer size.
This silences a false positive -Wstringop-overflow warning from GCC, as the compiler can now prove that the destination buffer is large enough for the specified read size.
connect_one_server() passes per-chunk 'ret' to do_getsockopts() instead of cumulative 'total', causing mismatches when the echo is fragmented. Fix by passing 'total' to match the semantics used in process_one_client().
--- tools/testing/selftests/net/mptcp/mptcp_sockopt.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/net/mptcp/mptcp_sockopt.c +2 -2
@@ -609,7 +609,7 @@ static void connect_one_server(int fd, int pipefd) total =0 ; do { - ret = read(fd, buf2 + total, sizeof(buf2) - total); + ret = read(fd, buf2 + total, len - total); if (ret < 0) die_perror("read"); if (ret == 0) {
@@ -629,7 +629,7 @@ static void connect_one_server(int fd, int pipefd)if (s.tcpi_rcv_delta)assert (s.tcpi_rcv_delta <= total); -do_getsockopts (&s, fd, ret, ret); +do_getsockopts (&s, fd, total, total);if (eof) total +=1 ;/* sequence advances due to FIN */
Signature · 1 line
2.53.0
Geliang Tang
reviewerPATCH v13 3/12 selftests: mptcp: sockopt: add check_stat_equal helper
In reply to Geliang TangFrom: Geliang Tang <[email protected]>
Add check_stat_equal() helper to reduce repetitive error reporting code when validating MPTCP statistics values in process_one_client().
Also fix bytes_acked validation: compare it against the amount of data written (ret2) instead of the amount read (ret), as bytes_acked tracks sent and acknowledged data.
--- .../selftests/net/mptcp/mptcp_sockopt.c | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-)
tools/testing/selftests/net/mptcp/mptcp_sockopt.c +21 -18
@@ -638,6 +638,16 @@ static void connect_one_server(int fd, int pipefd)close (fd); } +static void check_stat_equal (const char *name,uint64_t actual, +uint64_t expected) +{ +if (actual == expected) +return ; + +xerror ("%s %" PRIu64", expect %" PRIu64", diff %" PRId64, + name, actual, expected, (int64_t )(actual - expected)); +} +static void process_one_client (int fd,int pipefd) {ssize_t ret, ret2, ret3;
@@ -669,27 +679,20 @@ static void process_one_client(int fd, int pipefd)xerror ("expected EOF, got %lu" , ret3);do_getsockopts (&s, fd, ret, ret2); -if (s.mptcpi_rcv_delta != (uint64_t )ret +1 ) -xerror ("mptcpi_rcv_delta %" PRIu64", expect %" PRIu64", diff %" PRId64, - s.mptcpi_rcv_delta, ret +1 , s.mptcpi_rcv_delta - (ret +1 )); +check_stat_equal ("mptcpi_rcv_delta" , s.mptcpi_rcv_delta, + (uint64_t )ret +1 );/* +1 for FIN */ /* be nice when running on top of older kernel */ if (s.pkt_stats_avail) { -if (s.last_sample.mptcpi_bytes_sent != ret2) -xerror ("mptcpi_bytes_sent %" PRIu64", expect %" PRIu64 -", diff %" PRId64, - s.last_sample.mptcpi_bytes_sent, ret2, - s.last_sample.mptcpi_bytes_sent - ret2); -if (s.last_sample.mptcpi_bytes_received != ret) -xerror ("mptcpi_bytes_received %" PRIu64", expect %" PRIu64 -", diff %" PRId64, - s.last_sample.mptcpi_bytes_received, ret, - s.last_sample.mptcpi_bytes_received - ret); -if (s.last_sample.mptcpi_bytes_acked != ret) -xerror ("mptcpi_bytes_acked %" PRIu64", expect %" PRIu64 -", diff %" PRId64, - s.last_sample.mptcpi_bytes_acked, ret, - s.last_sample.mptcpi_bytes_acked - ret); +check_stat_equal ("mptcpi_bytes_sent" , + s.last_sample.mptcpi_bytes_sent, + (uint64_t )ret2); +check_stat_equal ("mptcpi_bytes_received" , + s.last_sample.mptcpi_bytes_received, + (uint64_t )ret); +check_stat_equal ("mptcpi_bytes_acked" , + s.last_sample.mptcpi_bytes_acked, + (uint64_t )ret2); }close (fd);
Signature · 1 line
2.53.0
Geliang Tang
reviewerPATCH v13 4/12 selftests: mptcp: sockopt: use size_t for byte counters
In reply to Geliang TangFrom: Geliang Tang <[email protected]>
Rename ret/ret2/ret3 to ret (ssize_t, for syscall return values) and r/w (size_t, for byte counters) for better readability.
The byte counters were previously stored in ssize_t but never hold negative values; size_t is the natural type and matches the parameter types of do_getsockopts(s, fd, size_t r, size_t w). This also prepares for future updates where r will be modified via '+='.
--- .../selftests/net/mptcp/mptcp_sockopt.c | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-)
tools/testing/selftests/net/mptcp/mptcp_sockopt.c +17 -13
@@ -650,9 +650,10 @@ static void check_stat_equal(const char *name, uint64_t actual,static void process_one_client (int fd,int pipefd) { -ssize_t ret, ret2, ret3;struct so_state s;char buf[4096 ]; +ssize_t ret; +size_t r, w;memset (&s,0 ,sizeof (s));do_getsockopts (&s, fd,0 ,0 );
@@ -663,36 +664,39 @@ static void process_one_client(int fd, int pipefd) ret =read (fd, buf,sizeof (buf));if (ret <0 )die_perror ("read" ); + r = ret; -assert (s.mptcpi_rcv_delta <= (uint64_t )ret); +assert (s.mptcpi_rcv_delta <= (uint64_t )r);if (s.tcpi_rcv_delta) -assert (s.tcpi_rcv_delta == (uint64_t )ret); +assert (s.tcpi_rcv_delta == (uint64_t )r); - ret2 =write (fd, buf, ret); -if (ret2 <0 ) + ret =write (fd, buf, r); +if (ret <0 )die_perror ("write" ); + w = ret;/* wait for hangup */ - ret3 =read (fd, buf,1 ); -if (ret3 !=0 ) -xerror ("expected EOF, got %lu" , ret3); + ret =read (fd, buf,1 ); +if (ret !=0 ) +xerror ("expected EOF, got %zd" , ret); + r += ret; -do_getsockopts (&s, fd, ret, ret2); +do_getsockopts (&s, fd, r, w);check_stat_equal ("mptcpi_rcv_delta" , s.mptcpi_rcv_delta, - (uint64_t )ret +1 );/* +1 for FIN */ + (uint64_t )r +1 );/* +1 for FIN */ /* be nice when running on top of older kernel */ if (s.pkt_stats_avail) {check_stat_equal ("mptcpi_bytes_sent" , s.last_sample.mptcpi_bytes_sent, - (uint64_t )ret2); + (uint64_t )w);check_stat_equal ("mptcpi_bytes_received" , s.last_sample.mptcpi_bytes_received, - (uint64_t )ret); + (uint64_t )r);check_stat_equal ("mptcpi_bytes_acked" , s.last_sample.mptcpi_bytes_acked, - (uint64_t )ret2); + (uint64_t )w); }close (fd);
Signature · 1 line
2.53.0
Geliang Tang
reviewerPATCH v13 5/12 selftests: mptcp: sockopt: add is_mptcp_socket helper
In reply to Geliang TangFrom: 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.
--- .../selftests/net/mptcp/mptcp_sockopt.c | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-)
tools/testing/selftests/net/mptcp/mptcp_sockopt.c +20 -3
@@ -557,8 +557,23 @@ 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 =-1 ; +socklen_t optlen; + + optlen =sizeof (is_mptcp); +if (getsockopt (fd,IPPROTO_TCP ,TCP_IS_MPTCP , &is_mptcp, &optlen) ==-1 ) +die_perror ("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) <=0 ) +return ; +do_getsockopt_mptcp_info (s, fd, w);do_getsockopt_tcp_info (s, fd, r, w);
@@ -634,7 +649,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) >0 ) +assert (s.mptcpi_rcv_delta == (uint64_t )total);close (fd); }
@@ -683,8 +699,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) >0 ) +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) {
Signature · 1 line
2.53.0
Geliang Tang
reviewerPATCH v13 6/12 selftests: mptcp: sockopt: add tx/rx protocol options
In reply to Geliang TangFrom: Geliang Tang <[email protected]>
Add -t and -r options to specify tx/rx protocols (TCP/MPTCP). This increases testing flexibility by allowing explicit protocol selection for both transmission and reception paths.
These codes are from mptcp_inq.c.
--- .../selftests/net/mptcp/mptcp_sockopt.c | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-)
tools/testing/selftests/net/mptcp/mptcp_sockopt.c +24 -4
@@ -28,6 +28,8 @@#include <linux/compiler.h> static int pf =AF_INET ; +static int proto_tx =IPPROTO_MPTCP ; +static int proto_rx =IPPROTO_MPTCP ;#ifndef IPPROTO_MPTCP #define IPPROTO_MPTCP 262
@@ -139,6 +141,7 @@ static void __noreturn die_perror(const char *msg)static void die_usage (int r) {fprintf (stderr,"Usage: mptcp_sockopt [-6]\n" ); +fprintf (stderr," [-t tcp|mptcp] [-r tcp|mptcp]\n" );exit (r); }
@@ -204,7 +207,7 @@ static int sock_listen_mptcp(const char * const listenaddr, hints.ai_family = pf;for (a = addr; a; a = a->ai_next) { - sock =socket (a->ai_family, a->ai_socktype,IPPROTO_MPTCP ); + sock =socket (a->ai_family, a->ai_socktype, proto_rx);if (sock <0 )continue ;
@@ -262,11 +265,22 @@ static int sock_connect_mptcp(const char * const remoteaddr,return sock; } +static int protostr_to_num (const char *s) +{ +if (strcasecmp (s,"tcp" ) ==0 ) +return IPPROTO_TCP ; +if (strcasecmp (s,"mptcp" ) ==0 ) +return IPPROTO_MPTCP ; + +die_usage (1 ); +return 0 ; +} +static void parse_opts (int argc,char **argv) {int c; -while ((c =getopt (argc, argv,"h6" )) !=-1 ) { +while ((c =getopt (argc, argv,"h6t:r:" )) !=-1 ) {switch (c) {case 'h' :die_usage (0 );
@@ -274,6 +288,12 @@ static void parse_opts(int argc, char **argv)case '6' : pf =AF_INET6 ;break ; +case 't' : + proto_tx =protostr_to_num (optarg); +break ; +case 'r' : + proto_rx =protostr_to_num (optarg); +break ;default :die_usage (1 );break ;
@@ -803,10 +823,10 @@ static int client(int pipefd)switch (pf) {case AF_INET : - fd =sock_connect_mptcp ("127.0.0.1" ,"15432" ,IPPROTO_MPTCP ); + fd =sock_connect_mptcp ("127.0.0.1" ,"15432" , proto_tx);break ;case AF_INET6 : - fd =sock_connect_mptcp ("::1" ,"15432" ,IPPROTO_MPTCP ); + fd =sock_connect_mptcp ("::1" ,"15432" , proto_tx);break ;default :xerror ("Unknown pf %d\n" , pf);
Signature · 1 line
2.53.0
Geliang Tang
reviewerPATCH v13 7/12 selftests: mptcp: sockopt: add TCP_INQ test option
In reply to Geliang TangFrom: Geliang Tang <[email protected]>
Add -i option to enable TCP_INQ testing. This prepares for consolidating TCP_INQ tests into a single binary by adding the necessary command-line interface.
Add helper functions to conditionally set TCP_INQ sockopt on accepted sockets when running in inq mode. This ensures proper testing of TCP_CM_INQ functionality by enabling the required socket option on the incoming server connection.
These codes are from mptcp_inq.c.
--- .../selftests/net/mptcp/mptcp_sockopt.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-)
tools/testing/selftests/net/mptcp/mptcp_sockopt.c +17 -1
@@ -30,6 +30,7 @@static int pf =AF_INET ;static int proto_tx =IPPROTO_MPTCP ;static int proto_rx =IPPROTO_MPTCP ; +static bool inq;#ifndef IPPROTO_MPTCP #define IPPROTO_MPTCP 262
@@ -142,6 +143,7 @@ static void die_usage(int r) {fprintf (stderr,"Usage: mptcp_sockopt [-6]\n" );fprintf (stderr," [-t tcp|mptcp] [-r tcp|mptcp]\n" ); +fprintf (stderr," [-i]\n" );exit (r); }
@@ -280,7 +282,7 @@ static void parse_opts(int argc, char **argv) {int c; -while ((c =getopt (argc, argv,"h6t:r:" )) !=-1 ) { +while ((c =getopt (argc, argv,"h6t:r:i" )) !=-1 ) {switch (c) {case 'h' :die_usage (0 );
@@ -294,6 +296,9 @@ static void parse_opts(int argc, char **argv)case 'r' : proto_rx =protostr_to_num (optarg);break ; +case 'i' : + inq = true; +break ;default :die_usage (1 );break ;
@@ -739,6 +744,14 @@ static void process_one_client(int fd, int pipefd)close (fd); } +static void do_setsockopt_inq (int fd) +{ +int on =1 ; + +if (setsockopt (fd,IPPROTO_TCP ,TCP_INQ , &on,sizeof (on))) +die_perror ("setsockopt(TCP_INQ)" ); +} +static int xaccept (int s) {int fd =accept (s,NULL ,0 );
@@ -746,6 +759,9 @@ static int xaccept(int s)if (fd <0 )die_perror ("accept" ); +if (inq) +do_setsockopt_inq (fd); +return fd; }
Signature · 1 line
2.53.0
Geliang Tang
reviewerPATCH v13 8/12 selftests: mptcp: sockopt: use unix socket instead of pipe
In reply to Geliang TangFrom: Geliang Tang <[email protected]>
Replace the pipe() with socketpair(AF_UNIX, SOCK_STREAM) for inter-process communication. This prepares for upcoming patches that will require bidirectional communication and reliable EOF behavior, which a pipe does not fully support.
Use SOCK_STREAM over SOCK_DGRAM (used in the original mptcp_inq.c) to reliably detect peer closure via EOF, avoiding indefinite hangs.
--- .../selftests/net/mptcp/mptcp_sockopt.c | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-)
tools/testing/selftests/net/mptcp/mptcp_sockopt.c +20 -20
@@ -609,7 +609,7 @@ static void do_getsockopts(struct so_state *s, int fd, size_t r, size_t w)do_getsockopt_mptcp_full_info (s, fd); } -static void connect_one_server (int fd,int pipefd) +static void connect_one_server (int fd,int unixfd) {char buf[4096 ], buf2[4096 ];size_t len, i, total;
@@ -634,9 +634,9 @@ static void connect_one_server(int fd, int pipefd)do_getsockopts (&s, fd,0 ,0 );/* un-block server */ - ret =read (pipefd, buf2,4 ); + ret =read (unixfd, buf2,4 );assert (ret ==4 ); -close (pipefd); +close (unixfd);assert (strncmp (buf2,"xmit" ,4 ) ==0 );
@@ -689,7 +689,7 @@ static void check_stat_equal(const char *name, uint64_t actual, name, actual, expected, (int64_t)(actual - expected)); } -static void process_one_client (int fd,int pipefd) +static void process_one_client (int fd,int unixfd) {struct so_state s;char buf[4096 ];
@@ -699,7 +699,7 @@ static void process_one_client(int fd, int pipefd)memset (&s,0 ,sizeof (s));do_getsockopts (&s, fd,0 ,0 ); - ret =write (pipefd,"xmit" ,4 ); + ret =write (unixfd,"xmit" ,4 );assert (ret ==4 ); ret =read (fd, buf,sizeof (buf));
@@ -765,7 +765,7 @@ static int xaccept(int s)return fd; } -static int server (int pipefd) +static int server (int unixfd) {int fd =-1 , r;
@@ -781,13 +781,13 @@ static int server(int pipefd)break ; } - r =write (pipefd,"conn" ,4 ); + r =write (unixfd,"conn" ,4 );assert (r ==4 );alarm (15 ); r =xaccept (fd); -process_one_client (r, pipefd); +process_one_client (r, unixfd);close (fd);return 0 ;
@@ -831,7 +831,7 @@ static void test_ip_tos_sockopt(int fd)xerror ("expect socklen_t == -1" ); } -static int client (int pipefd) +static int client (int unixfd) {int fd =-1 ;
@@ -850,7 +850,7 @@ static int client(int pipefd)test_ip_tos_sockopt (fd); -connect_one_server (fd, pipefd); +connect_one_server (fd, unixfd);return 0 ; }
@@ -906,35 +906,35 @@ int main(int argc, char *argv[]) {int e1, e2, wstatus;pid_t s, c, ret; -int pipefds[2 ]; +int unixfds[2 ];parse_opts (argc, argv);init_rng (); - e1 =pipe (pipefds); + e1 =socketpair (AF_UNIX ,SOCK_STREAM ,0 , unixfds);if (e1 <0 ) -die_perror ("pipe" ); +die_perror ("socketpair" ); s =xfork ();if (s ==0 ) { -close (pipefds[0 ]); - ret =server (pipefds[1 ]); -close (pipefds[1 ]); +close (unixfds[0 ]); + ret =server (unixfds[1 ]); +close (unixfds[1 ]);return ret; } -close (pipefds[1 ]); +close (unixfds[1 ]);/* wait until server bound a socket */ - e1 =read (pipefds[0 ], &e1,4 ); + e1 =read (unixfds[0 ], &e1,4 );assert (e1 ==4 ); c =xfork ();if (c ==0 ) -return client (pipefds[0 ]); +return client (unixfds[0 ]); -close (pipefds[0 ]); +close (unixfds[0 ]); ret =waitpid (s, &wstatus,0 );if (ret ==-1 )
Signature · 1 line
2.53.0
Geliang Tang
reviewerPATCH v13 9/12 selftests: mptcp: sockopt: use recvmsg instead of read
In reply to Geliang TangFrom: Geliang Tang <[email protected]>
Replace read() with recvmsg() in process_one_client() to enable control message (CMSG) handling. The original read(fd, buf, sizeof(buf)) is split into two recvmsg() calls: the first reads exactly one byte, and the second reads the remaining bytes. This allows the code to access CMSG data for each received segment, facilitating the upcoming TCP_INQ validation.
Introduce inq_msg_init() and inq_msg_reset() helpers to manage the struct inq_msg control buffer and iovec, enabling reuse of the same msghdr across multiple recvmsg() calls with different payload buffers.
--- .../selftests/net/mptcp/mptcp_sockopt.c | 48 +++++++++++++++++-- 1 file changed, 43 insertions(+), 5 deletions(-)
tools/testing/selftests/net/mptcp/mptcp_sockopt.c +43 -5
@@ -689,36 +689,74 @@ static void check_stat_equal(const char *name, uint64_t actual, name, actual, expected, (int64_t)(actual - expected)); } +struct inq_msg { +char buf[4096 ]; +union { +struct cmsghdr cmsg; +char msg_buf[4096 ]; + } control; +struct iovec iov; +struct msghdr hdr; +}; + +static void inq_msg_init (struct inq_msg *m,size_t iov_len) +{ +memset (m,0 ,sizeof (*m)); + m->iov.iov_base = m->buf; + m->iov.iov_len = iov_len; + m->hdr.msg_iov = &m->iov; + m->hdr.msg_iovlen =1 ; + m->hdr.msg_control = m->control.msg_buf; + m->hdr.msg_controllen =sizeof (m->control.msg_buf); +} + +static void inq_msg_reset (struct inq_msg *m,size_t iov_len,void *base) +{ + m->iov.iov_base = base; + m->iov.iov_len = iov_len; + m->hdr.msg_controllen =sizeof (m->control.msg_buf); +} +static void process_one_client (int fd,int unixfd) {struct so_state s; -char buf[4096 ]; +struct inq_msg m;ssize_t ret;size_t r, w; +inq_msg_init (&m,1 ); +memset (&s,0 ,sizeof (s));do_getsockopts (&s, fd,0 ,0 ); ret =write (unixfd,"xmit" ,4 );assert (ret ==4 ); - ret =read (fd, buf,sizeof (buf)); +/* read one byte */ + ret =recvmsg (fd, &m.hdr,0 );if (ret <0 ) -die_perror ("read" ); +die_perror ("recvmsg" ); r = ret; +inq_msg_reset (&m,sizeof (m.buf) -1 , m.buf +1 ); + ret =recvmsg (fd, &m.hdr,0 ); +if (ret <0 ) +die_perror ("recvmsg" ); + r += ret; +assert (s.mptcpi_rcv_delta <= (uint64_t )r);if (s.tcpi_rcv_delta)assert (s.tcpi_rcv_delta == (uint64_t )r); - ret =write (fd, buf, r); + ret =write (fd, m.buf, r);if (ret <0 )die_perror ("write" ); w = ret;/* wait for hangup */ - ret =read (fd, buf,1 ); +inq_msg_reset (&m,1 , m.buf); + ret =recvmsg (fd, &m.hdr,0 );if (ret !=0 )xerror ("expected EOF, got %zd" , ret); r += ret;
Signature · 1 line
2.53.0
Geliang Tang
reviewerPATCH v13 10/12 selftests: mptcp: sockopt: get and verify TCP_INQ
In reply to Geliang TangFrom: Geliang Tang <[email protected]>
Implement FIONREAD ioctl checks to validate available data size, ensuring synchronization between write and read operations with non-blocking waits.
Pass the payload length ('len') from the client to the server via the unixfd control channel. This allows the server to know the exact amount of data to expect, enabling precise FIONREAD loop and later TCP_CM_INQ validation.
Add get_tcp_inq() helper to extract and verify TCP_CM_INQ values from received control messages. Integrate it into the echo test to validate in-queue byte counts at key stages, ensuring correct TCP_INQ behavior with MPTCP.
Code adapted from mptcp_inq.c.
--- .../selftests/net/mptcp/mptcp_sockopt.c | 72 ++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-)
tools/testing/selftests/net/mptcp/mptcp_sockopt.c +71 -1
@@ -20,6 +20,7 @@#include <sys/socket.h> #include <sys/types.h> #include <sys/wait.h> +#include <sys/ioctl.h> #include <netdb.h> #include <netinet/in.h>
@@ -636,10 +637,12 @@ static void connect_one_server(int fd, int unixfd)/* un-block server */ ret =read (unixfd, buf2,4 );assert (ret ==4 ); -close (unixfd);assert (strncmp (buf2,"xmit" ,4 ) ==0 ); + ret =write (unixfd, &len,sizeof (len)); +assert (ret == (ssize_t )sizeof (len)); + ret =write (fd, buf, len);if (ret <0 )die_perror ("write" );
@@ -677,6 +680,7 @@ static void connect_one_server(int fd, int unixfd)if (is_mptcp_socket (fd) >0 )assert (s.mptcpi_rcv_delta == (uint64_t )total);close (fd); +close (unixfd); } static void check_stat_equal(const char *name, uint64_t actual,
@@ -717,9 +721,30 @@ static void inq_msg_reset(struct inq_msg *m, size_t iov_len, void *base) m->hdr.msg_controllen =sizeof (m->control.msg_buf); } +static void get_tcp_inq (struct msghdr *msgh,unsigned int *inqv) +{ +struct cmsghdr *cmsg; + +for (cmsg =CMSG_FIRSTHDR (msgh); cmsg; + cmsg =CMSG_NXTHDR (msgh, cmsg)) { +if (cmsg->cmsg_level ==IPPROTO_TCP && + cmsg->cmsg_type ==TCP_CM_INQ ) { +if (cmsg->cmsg_len <CMSG_LEN (sizeof (*inqv))) +xerror ("TCP_CM_INQ cmsg_len too small: %zu" , + cmsg->cmsg_len); +memcpy (inqv,CMSG_DATA (cmsg),sizeof (*inqv)); +return ; + } + } + +xerror ("could not find TCP_CM_INQ cmsg type" ); +} +static void process_one_client (int fd,int unixfd) { +unsigned int tcp_inq;struct so_state s; +size_t expect_len;struct inq_msg m;ssize_t ret;size_t r, w;
@@ -732,16 +757,57 @@ static void process_one_client(int fd, int unixfd) ret =write (unixfd,"xmit" ,4 );assert (ret ==4 ); + ret =read (unixfd, &expect_len,sizeof (expect_len)); +assert (ret == (ssize_t )sizeof (expect_len)); + +if (expect_len >sizeof (m.buf)) +xerror ("expect len %zu exceeds buffer size" , expect_len); + +for (;;) { +struct timespec req; +unsigned int queued; + + ret =ioctl (fd,FIONREAD , &queued); +if (ret <0 ) +die_perror ("FIONREAD" ); +if (queued > expect_len) +xerror ("FIONREAD returned %u, but only %zu expected\n" , + queued, expect_len); +if (queued == expect_len) +break ; + + req.tv_sec =0 ; + req.tv_nsec =1000 *1000ul ; +nanosleep (&req,NULL ); + } +/* read one byte */ ret =recvmsg (fd, &m.hdr,0 );if (ret <0 )die_perror ("recvmsg" ); +if (inq) { +if (m.hdr.msg_controllen ==0 ) +xerror ("msg_controllen is 0" ); + +get_tcp_inq (&m.hdr, &tcp_inq); + +/* expect cmsg to return expected - 1 */ +assert ((size_t )tcp_inq == (expect_len -1 )); + } r = ret;inq_msg_reset (&m,sizeof (m.buf) -1 , m.buf +1 ); ret =recvmsg (fd, &m.hdr,0 );if (ret <0 )die_perror ("recvmsg" ); +if (inq) { +/* should have gotten exact remainder of all pending data */ +assert (ret == (ssize_t )tcp_inq); + +/* should be 0, all drained */ +get_tcp_inq (&m.hdr, &tcp_inq); +assert (tcp_inq ==0 ); + } r += ret;assert (s.mptcpi_rcv_delta <= (uint64_t )r);
@@ -759,6 +825,10 @@ static void process_one_client(int fd, int unixfd) ret =recvmsg (fd, &m.hdr,0 );if (ret !=0 )xerror ("expected EOF, got %zd" , ret); +if (inq) { +get_tcp_inq (&m.hdr, &tcp_inq); +assert (tcp_inq ==1 ); + } r += ret;do_getsockopts (&s, fd, r, w);
Signature · 1 line
2.53.0
Geliang Tang
reviewerPATCH v13 11/12 selftests: mptcp: sockopt: add huge data transfer tests
In reply to Geliang TangFrom: Geliang Tang <[email protected]>
Introduce huge data transfer tests (1-17 MB) to validate MPTCP socket options under high-volume conditions, including proper EOF handling, preparing for TCP_INQ support in subsequent patches.
Add wait_for_ack() helper to synchronize test steps by waiting until the send queue is drained (all data ACKed). Used in server_huge_transfer() to ensure reliable TCP_INQ validation.
These codes are from mptcp_inq.c, but here they have been placed into two separate functions. And in client_huge_transfer(), do a blocking read for EOF on unixfd instead of sleeping, and poll with POLLRDHUP to wait for FIN arrival before reading the final byte, preventing flaky EOF detection.
--- .../selftests/net/mptcp/mptcp_sockopt.c | 171 ++++++++++++++++++ 1 file changed, 171 insertions(+)
tools/testing/selftests/net/mptcp/mptcp_sockopt.c +171 -0
@@ -16,6 +16,7 @@#include <strings.h> #include <time.h> #include <unistd.h> +#include <poll.h> #include <sys/socket.h> #include <sys/types.h>
@@ -26,6 +27,7 @@#include <netinet/in.h> #include <linux/tcp.h> +#include <linux/sockios.h> #include <linux/compiler.h> static int pf =AF_INET ;
@@ -610,6 +612,95 @@ static void do_getsockopts(struct so_state *s, int fd, size_t r, size_t w)do_getsockopt_mptcp_full_info (s, fd); } +/* wait up to timeout milliseconds */ +static void wait_for_ack (int fd,int timeout,size_t total) +{ +int i; + +for (i =0 ; i < timeout; i++) { +int nsd, ret, queued =-1 ; +struct timespec req; + + ret =ioctl (fd,TIOCOUTQ , &queued); +if (ret <0 ) +die_perror ("TIOCOUTQ" ); + + ret =ioctl (fd,SIOCOUTQNSD , &nsd); +if (ret <0 ) +die_perror ("SIOCOUTQNSD" ); + +if ((size_t )queued > total) +xerror ("TIOCOUTQ %u, but only %zu expected\n" , + queued, total); +assert (nsd <= queued); + +if (queued ==0 ) +return ; + +/* wait for peer to ack rx of all data */ + req.tv_sec =0 ; + req.tv_nsec =1 *1000 *1000ul ;/* 1ms */ +nanosleep (&req,NULL ); + } + +xerror ("still tx data queued after %u ms\n" , timeout); +} + +static void server_huge_transfer (int fd,int unixfd,size_t echo) +{ +char buf[4096 ], buf2[4 ]; +size_t len, i, total; +size_t sent; +ssize_t ret; + +for (i =0 ; i <sizeof (buf) -1 ; i++) { + buf[i] =rand () %26 ; + buf[i] +='A' ; + } + + buf[i] ='\n' ; + + ret =read (unixfd, buf2,4 ); +assert (ret ==4 ); +assert (strncmp (buf2,"huge" ,4 ) ==0 ); + + total =rand () % (16 *1024 *1024 ); + total += (1 *1024 *1024 ); + sent = total; + + ret =write (unixfd, &total,sizeof (total)); +assert (ret == (ssize_t )sizeof (total)); + +wait_for_ack (fd,5000 , echo); + +while (total >0 ) { +if (total >sizeof (buf)) + len =sizeof (buf); +else + len = total; + + ret =write (fd, buf, len); +if (ret <0 ) +die_perror ("write" ); + total -= ret; + +/* we don't have to care about buf content, only +* number of total bytes sent +*/ + } + + ret =read (unixfd, buf2,4 ); +assert (ret ==4 ); +assert (strncmp (buf2,"shut" ,4 ) ==0 ); + +wait_for_ack (fd,5000 , sent); + + ret =write (fd, buf,1 ); +assert (ret ==1 ); + ret =write (unixfd,"closed" ,6 ); +assert (ret ==6 ); +} +static void connect_one_server (int fd,int unixfd) {char buf[4096 ], buf2[4096 ];
@@ -679,6 +770,10 @@ static void connect_one_server(int fd, int unixfd)if (is_mptcp_socket (fd) >0 )assert (s.mptcpi_rcv_delta == (uint64_t )total); + +if (inq) +server_huge_transfer (fd, unixfd, total); +close (fd);close (unixfd); }
@@ -740,6 +835,79 @@ static void get_tcp_inq(struct msghdr *msgh, unsigned int *inqv)xerror ("could not find TCP_CM_INQ cmsg type" ); } +static size_t client_huge_transfer (int fd,int unixfd) +{ +struct pollfd pfd = { .fd = fd, .events =POLLRDHUP }; +unsigned int tcp_inq; +size_t expect_len; +ssize_t ret, tot; +struct inq_msg m; +char tmp[16 ]; + +inq_msg_init (&m,1 ); + +/* request a large swath of data. */ + ret =write (unixfd,"huge" ,4 ); +assert (ret ==4 ); + + ret =read (unixfd, &expect_len,sizeof (expect_len)); +assert (ret == (ssize_t )sizeof (expect_len)); + +/* peer should send us a few mb of data */ +if (expect_len <=sizeof (m.buf)) +xerror ("expect len %zu too small\n" , expect_len); + + tot =0 ; +do { +inq_msg_reset (&m,sizeof (m.buf), m.buf); + ret =recvmsg (fd, &m.hdr,0 ); +if (ret <0 ) +die_perror ("recvmsg" ); +if (ret ==0 ) +xerror ("unexpected EOF in huge recv" ); + + tot += ret; + +get_tcp_inq (&m.hdr, &tcp_inq); + +if (tcp_inq > expect_len - tot) +xerror ("inq %d, remaining %zu total_len %d\n" , + tcp_inq, expect_len - tot, (int )expect_len); + +assert (tcp_inq <= expect_len - tot); + }while ((size_t )tot < expect_len); + + ret =write (unixfd,"shut" ,4 ); +assert (ret ==4 ); + +/* wait for "closed"; server did final write on fd */ + ret =read (unixfd, tmp,sizeof (tmp)); +assert (ret ==6 ); +assert (strncmp (tmp,"closed" ,6 ) ==0 ); + +/* block on EOF: server close(fd) precedes close(unixfd) */ + ret =read (unixfd, tmp,sizeof (tmp)); +assert (ret ==0 ); + +/* Wait for the TCP FIN to actually arrive. */ + ret =poll (&pfd,1 ,5000 ); +assert (ret ==1 ); +assert (pfd.revents &POLLRDHUP ); + +inq_msg_reset (&m,1 , m.buf); + ret =recvmsg (fd, &m.hdr,0 ); +if (ret <0 ) +die_perror ("recvmsg" ); +assert (ret ==1 ); + +get_tcp_inq (&m.hdr, &tcp_inq); + +/* tcp_inq should be 1 due to received fin. */ +assert (tcp_inq ==1 ); + +return tot +1 ; +} +static void process_one_client (int fd,int unixfd) {unsigned int tcp_inq;
@@ -820,6 +988,9 @@ static void process_one_client(int fd, int unixfd)die_perror ("write" ); w = ret; +if (inq) + r +=client_huge_transfer (fd, unixfd); +/* wait for hangup */ inq_msg_reset (&m,1 , m.buf); ret =recvmsg (fd, &m.hdr,0 );
Signature · 1 line
2.53.0
Geliang Tang
reviewerPATCH v13 12/12 selftests: mptcp: sockopt: replace mptcp_inq with sockopt
In reply to Geliang TangFrom: Geliang Tang <[email protected]>
Update mptcp_sockopt.sh to use mptcp_sockopt binary instead of mptcp_inq. This consolidates TCP_INQ testing into a single test binary while maintaining equivalent test coverage.
Delete mptcp_inq binary and references since its functionality has been fully integrated into mptcp_sockopt. This simplifies the test suite structure and reduces maintenance overhead.
--- tools/testing/selftests/net/mptcp/.gitignore | 1 - tools/testing/selftests/net/mptcp/Makefile | 1 - tools/testing/selftests/net/mptcp/mptcp_inq.c | 614 ------------------ .../selftests/net/mptcp/mptcp_sockopt.sh | 8 +- 4 files changed, 4 insertions(+), 620 deletions(-) delete mode 100644 tools/testing/selftests/net/mptcp/mptcp_inq.c
tools/testing/selftests/net/mptcp/.gitignore +0 -1
@@ -1,7 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-only mptcp_connect mptcp_diag -mptcp_inq mptcp_sockopt pm_nl_ctl *.pcap
tools/testing/selftests/net/mptcp/Makefile +0 -1
@@ -22,7 +22,6 @@ TEST_PROGS := \ TEST_GEN_FILES := \ mptcp_connect \ mptcp_diag \ - mptcp_inq \ mptcp_sockopt \ pm_nl_ctl \ # end of TEST_GEN_FILES
tools/testing/selftests/net/mptcp/mptcp_inq.c deleted +0 -614
614 lines not on this page · raw message
tools/testing/selftests/net/mptcp/mptcp_sockopt.sh +4 -4
@@ -308,7 +308,7 @@ run_tests() do_tcpinq_test() { print_title "TCP_INQ cmsg/ioctl $*" - ip netns exec "$ns_sbox" ./mptcp_inq "$@" + ip netns exec "$ns_sbox" ./mptcp_sockopt "$@" local lret=$? if [ $lret -ne 0 ];then ret=$lret
@@ -334,19 +334,19 @@ do_tcpinq_tests() local args for args in "-t tcp" "-r tcp"; do - do_tcpinq_test $args + do_tcpinq_test $args -i lret=$? if [ $lret -ne 0 ] ; then return $lret fi - do_tcpinq_test -6 $args + do_tcpinq_test -6 $args -i lret=$? if [ $lret -ne 0 ] ; then return $lret fi done - do_tcpinq_test -r tcp -t tcp + do_tcpinq_test -r tcp -t tcp -i return $? }
Signature · 1 line
2.53.0
Hi Geliang,
Thank you for your modifications, that's great!
Our CI did some validations and here is its report:
- KVM Validation: normal (except selftest_mptcp_join): Success! ✅
- KVM Validation: normal (only selftest_mptcp_join): Success! ✅
- KVM Validation: debug (except selftest_mptcp_join): Success! ✅
- KVM Validation: debug (only selftest_mptcp_join): Success! ✅
- KVM Validation: btf-normal (only bpftest_all): Success! ✅
- KVM Validation: btf-debug (only bpftest_all): Success! ✅
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/30195203951
Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/f69067f3be5d
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=1134601
If there are some issues, you can reproduce them using the same environment as the one used by the CI thanks to a docker image, e.g.:
$ cd [kernel source code]
$ docker run -v "${PWD}:${PWD}:rw" -w "${PWD}" --privileged --rm -it \
--pull always mptcp/mptcp-upstream-virtme-docker:latest \
auto-normalFor more details:
https://github.com/multipath-tcp/mptcp-upstream-virtme-docker
Please note that despite all the efforts that have been already done to have a stable tests suite when executed on a public CI like here, it is possible some reported issues are not due to your modifications. Still, do not hesitate to help us improve that ;-)
Cheers,
MPTCP GH Action bot
Bot operated by Matthieu Baerts (NGI0 Core)