Skip to contentSkip to conversation

selftests: rds: ksft cleanups

PATCH Applied Allison Henderson 4 participants 9 messages Mar 8 – Mar 16

Subsystems (5)
lore
Share & tools
lore

Allison Henderson

author maintainer
#0 lore raw

cover selftests: rds: ksft cleanups

Hi all,

This set addresses a few rds selftests clean ups and bugs encountered when running in the ksft framework. The first patch is a clean up patch that addresses pylint warnings, but otherwise no functional changes. The next patch moves the test time out to a ksft settings file so that the time out is set appropriately. And lastly we fix a tcpdump segfault caused by deprecated a os.fork() call.

Questions, comments and feed back appreciated!

Thanks!
Allison

Allison Henderson (3):
selftests: rds: Fix pylint warnings
selftests: rds: Add ksft timeout
selftests: rds: Fix tcpdump segfault in rds selftests

 tools/testing/selftests/net/rds/Makefile |   1 +
 tools/testing/selftests/net/rds/run.sh   |   7 +-
 tools/testing/selftests/net/rds/settings |   1 +
 tools/testing/selftests/net/rds/test.py  | 108 +++++++++++++----------
 4 files changed, 66 insertions(+), 51 deletions(-)
 create mode 100644 tools/testing/selftests/net/rds/settings
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH 1/3 selftests: rds: Fix pylint warnings

Tidy up all exiting pylint errors in test.py. No functional changes are introduced in this patch

---
 tools/testing/selftests/net/rds/test.py | 84 +++++++++++++------------
 1 file changed, 45 insertions(+), 39 deletions(-)
tools/testing/selftests/net/rds/test.py +45 -39
index 4a7178d11193..8256afe6ad6f 100755
@@ -11,7 +11,6 @@ import signal
 import socket
 import subprocess
 import sys
-import atexit
 from pwd import getpwuid
 from os import stat
 
@@ -23,45 +22,54 @@ from lib.py.utils import ip
 libc = ctypes.cdll.LoadLibrary('libc.so.6')
 setns = libc.setns
 
-net0 = 'net0'
-net1 = 'net1'
+NET0 = 'net0'
+NET1 = 'net1'
 
-veth0 = 'veth0'
-veth1 = 'veth1'
+VETH0 = 'veth0'
+VETH1 = 'veth1'
 
 # Helper function for creating a socket inside a network namespace.
 # We need this because otherwise RDS will detect that the two TCP
 # sockets are on the same interface and use the loop transport instead
 # of the TCP transport.
-def netns_socket(netns, *args):
+def netns_socket(netns, *sock_args):
+    """
+    Creates sockets inside of network namespace
+
+    :param netns: the name of the network namespace
+    :param sock_args: socket family and type
+    """
     u0, u1 = socket.socketpair(socket.AF_UNIX, socket.SOCK_SEQPACKET)
 
     child = os.fork()
     if child == 0:
         # change network namespace
-        with open(f'/var/run/netns/{netns}') as f:
+        with open(f'/var/run/netns/{netns}', encoding='utf-8') as f:
             try:
-                ret = setns(f.fileno(), 0)
+                setns(f.fileno(), 0)
             except IOError as e:
                 print(e.errno)
                 print(e)
 
         # create socket in target namespace
-        s = socket.socket(*args)
+        sock = socket.socket(*sock_args)
 
         # send resulting socket to parent
-        socket.send_fds(u0, [], [s.fileno()])
+        socket.send_fds(u0, [], [sock.fileno()])
 
         sys.exit(0)
 
     # receive socket from child
-    _, s, _, _ = socket.recv_fds(u1, 0, 1)
+    _, fds, _, _ = socket.recv_fds(u1, 0, 1)
     os.waitpid(child, 0)
     u0.close()
     u1.close()
-    return socket.fromfd(s[0], *args)
+    return socket.fromfd(fds[0], *sock_args)
 
-def signal_handler(sig, frame):
+def signal_handler(_sig, _frame):
+    """
+    Test timed out signal handler
+    """
     print('Test timed out')
     sys.exit(1)
 
@@ -81,13 +89,13 @@ parser.add_argument('-u', '--duplicate', help="Simulate tcp packet duplication",
                     type=int, default=0)
 args = parser.parse_args()
 logdir=args.logdir
-packet_loss=str(args.loss)+'%'
-packet_corruption=str(args.corruption)+'%'
-packet_duplicate=str(args.duplicate)+'%'
+PACKET_LOSS=str(args.loss)+'%'
+PACKET_CORRUPTION=str(args.corruption)+'%'
+PACKET_DUPLICATE=str(args.duplicate)+'%'
 
-ip(f"netns add {net0}")
-ip(f"netns add {net1}")
-ip(f"link add type veth")
+ip(f"netns add {NET0}")
+ip(f"netns add {NET1}")
+ip("link add type veth")
 
 addrs = [
     # we technically don't need different port numbers, but this will
@@ -99,25 +107,25 @@ addrs = [
 # move interfaces to separate namespaces so they can no longer be
 # bound directly; this prevents rds from switching over from the tcp
 # transport to the loop transport.
-ip(f"link set {veth0} netns {net0} up")
-ip(f"link set {veth1} netns {net1} up")
+ip(f"link set {VETH0} netns {NET0} up")
+ip(f"link set {VETH1} netns {NET1} up")
 
 
 
 # add addresses
-ip(f"-n {net0} addr add {addrs[0][0]}/32 dev {veth0}")
-ip(f"-n {net1} addr add {addrs[1][0]}/32 dev {veth1}")
+ip(f"-n {NET0} addr add {addrs[0][0]}/32 dev {VETH0}")
+ip(f"-n {NET1} addr add {addrs[1][0]}/32 dev {VETH1}")
 
 # add routes
-ip(f"-n {net0} route add {addrs[1][0]}/32 dev {veth0}")
-ip(f"-n {net1} route add {addrs[0][0]}/32 dev {veth1}")
+ip(f"-n {NET0} route add {addrs[1][0]}/32 dev {VETH0}")
+ip(f"-n {NET1} route add {addrs[0][0]}/32 dev {VETH1}")
 
 # sanity check that our two interfaces/addresses are correctly set up
 # and communicating by doing a single ping
-ip(f"netns exec {net0} ping -c 1 {addrs[1][0]}")
+ip(f"netns exec {NET0} ping -c 1 {addrs[1][0]}")
 
 # Start a packet capture on each network
-for net in [net0, net1]:
+for net in [NET0, NET1]:
     tcpdump_pid = os.fork()
     if tcpdump_pid == 0:
         pcap = logdir+'/'+net+'.pcap'
@@ -127,10 +135,10 @@ for net in [net0, net1]:
         sys.exit(0)
 
 # simulate packet loss, duplication and corruption
-for net, iface in [(net0, veth0), (net1, veth1)]:
+for net, iface in [(NET0, VETH0), (NET1, VETH1)]:
     ip(f"netns exec {net} /usr/sbin/tc qdisc add dev {iface} root netem  \
-         corrupt {packet_corruption} loss {packet_loss} duplicate  \
-         {packet_duplicate}")
+         corrupt {PACKET_CORRUPTION} loss {PACKET_LOSS} duplicate  \
+         {PACKET_DUPLICATE}")
 
 # add a timeout
 if args.timeout > 0:
@@ -138,8 +146,8 @@ if args.timeout > 0:
     signal.signal(signal.SIGALRM, signal_handler)
 
 sockets = [
-    netns_socket(net0, socket.AF_RDS, socket.SOCK_SEQPACKET),
-    netns_socket(net1, socket.AF_RDS, socket.SOCK_SEQPACKET),
+    netns_socket(NET0, socket.AF_RDS, socket.SOCK_SEQPACKET),
+    netns_socket(NET1, socket.AF_RDS, socket.SOCK_SEQPACKET),
 ]
 
 for s, addr in zip(sockets, addrs):
@@ -150,9 +158,7 @@ fileno_to_socket = {
     s.fileno(): s for s in sockets
 }
 
-addr_to_socket = {
-    addr: s for addr, s in zip(addrs, sockets)
-}
+addr_to_socket = dict(zip(addrs, sockets))
 
 socket_to_addr = {
     s: addr for addr, s in zip(addrs, sockets)
@@ -166,14 +172,14 @@ ep = select.epoll()
 for s in sockets:
     ep.register(s, select.EPOLLRDNORM)
 
-n = 50000
+NUM_PACKETS = 50000
 nr_send = 0
 nr_recv = 0
 
-while nr_send < n:
+while nr_send < NUM_PACKETS:
     # Send as much as we can without blocking
     print("sending...", nr_send, nr_recv)
-    while nr_send < n:
+    while nr_send < NUM_PACKETS:
         send_data = hashlib.sha256(
             f'packet {nr_send}'.encode('utf-8')).hexdigest().encode('utf-8')
 
@@ -212,7 +218,7 @@ while nr_send < n:
                         break
 
     # exercise net/rds/tcp.c:rds_tcp_sysctl_reset()
-    for net in [net0, net1]:
+    for net in [NET0, NET1]:
         ip(f"netns exec {net} /usr/sbin/sysctl net.rds.tcp.rds_tcp_rcvbuf=10000")
         ip(f"netns exec {net} /usr/sbin/sysctl net.rds.tcp.rds_tcp_sndbuf=10000")
 
Signature · 1 line
2.43.0
author maintainer
#2 lore raw

PATCH 2/3 selftests: rds: Add ksft timeout

In reply to Allison Henderson

rds/run.sh sets a timer of 400s when calling test.py. However when tests are run through ksft, a default 45s timer is applied. Fix this by adding a ksft timeout in tools/testing/selftests/net/rds/settings

---
 tools/testing/selftests/net/rds/Makefile | 1 +
 tools/testing/selftests/net/rds/run.sh   | 7 +++++--
 tools/testing/selftests/net/rds/settings | 1 +
 3 files changed, 7 insertions(+), 2 deletions(-)
tools/testing/selftests/net/rds/Makefile +1 -0
index 762845cc973c..8f9d81a18a1b 100644
@@ -8,6 +8,7 @@ TEST_PROGS := run.sh
 TEST_FILES := \
 	include.sh \
 	test.py \
+	settings \
 # end of TEST_FILES
 
 EXTRA_CLEAN := \
tools/testing/selftests/net/rds/run.sh +5 -2
index 8aee244f582a..897d17d1b8db 100755
@@ -19,6 +19,9 @@ if test -f "$build_include"; then
 	build_dir="$mk_build_dir"
 fi
 
+# Source settings for timeout value (also used by ksft runner)
+source "$current_dir"/settings
+
 # This test requires kernel source and the *.gcda data therein
 # Locate the top level of the kernel source, and the net/rds
 # subfolder with the appropriate *.gcno object files
@@ -194,8 +197,8 @@ set +e
 echo running RDS tests...
 echo Traces will be logged to "$TRACE_FILE"
 rm -f "$TRACE_FILE"
-strace -T -tt -o "$TRACE_FILE" python3 "$(dirname "$0")/test.py" --timeout 400 -d "$LOG_DIR" \
-       -l "$PLOSS" -c "$PCORRUPT" -u "$PDUP"
+strace -T -tt -o "$TRACE_FILE" python3 "$(dirname "$0")/test.py" \
+	--timeout "$timeout" -d "$LOG_DIR" -l "$PLOSS" -c "$PCORRUPT" -u "$PDUP"
 
 test_rc=$?
 dmesg > "${LOG_DIR}/dmesg.out"
tools/testing/selftests/net/rds/settings new file +1 -0
new file mode 100644
index 000000000000..d2009a64589c
@@ -0,0 +1 @@
+timeout=400
Signature · 1 line
2.43.0
author maintainer
#3 lore raw

PATCH 3/3 selftests: rds: Fix tcpdump segfault in rds selftests

In reply to Allison Henderson

net/rds/test.py sees a segfault in tcpdump when executed through the ksft runner.

[   21.903713] tcpdump[1469]: segfault at 0 ip 000072100e99126d
sp 00007ffccf740fd0 error 4
[   21.903721]  in libc.so.6[16a26d,7798b149a000+188000]
[   21.905074]  in libc.so.6[16a26d,72100e84f000+188000] likely on
CPU 5 (core 5, socket 0)
[   21.905084] Code: 00 0f 85 a0 00 00 00 48 83 c4 38 89 d8 5b 41 5c
41 5d 41 5e 41 5f 5d c3 0f 1f 44 00 00 48 8b 05 91 8b 09 00 8b 4d ac
64 89 08 <41> 0f b6 07 83 e8 2b a8 fd 0f 84 54 ff ff ff 49 8b 36 4c 89
ff e8
[   21.906760]  likely on CPU 9 (core 9, socket 0)
[   21.913469] Code: 00 0f 85 a0 00 00 00 48 83 c4 38 89 d8 5b 41 5c 41

5d 41 5e 41 5f 5d c3 0f 1f 44 00 00 48 8b 05 91 8b 09 00 8b 4d ac 64 89 08 <41> 0f b6 07 83 e8 2b a8 fd 0f 84 54 ff ff ff 49 8b 36 4c 89 ff e8

The os.fork() call creates extra complexity because it forks the entire process including the python interpreter. ip() then calls cmd() which creates a subprocess.Popen. We can avoid the extra layering by simply calling subprocess.Popen directly. Track the process handles directly and terminate them at cleanup rather than relying on killall. Further tcpdump's -Z flag attempts to change savefile ownership, which is not supported by the 9p protocol. Fix this by writing pcap captures to "/tmp" during the test and move them to the log directory after tcpdump exits.

---
 tools/testing/selftests/net/rds/test.py | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
tools/testing/selftests/net/rds/test.py +14 -10
index 8256afe6ad6f..93e23e8b256c 100755
@@ -11,8 +11,8 @@ import signal
 import socket
 import subprocess
 import sys
-from pwd import getpwuid
-from os import stat
+import tempfile
+import shutil
 
 # Allow utils module to be imported from different directory
 this_dir = os.path.dirname(os.path.realpath(__file__))
@@ -125,14 +125,14 @@ ip(f"-n {NET1} route add {addrs[0][0]}/32 dev {VETH1}")
 ip(f"netns exec {NET0} ping -c 1 {addrs[1][0]}")
 
 # Start a packet capture on each network
+tcpdump_procs = []
 for net in [NET0, NET1]:
-    tcpdump_pid = os.fork()
-    if tcpdump_pid == 0:
-        pcap = logdir+'/'+net+'.pcap'
-        subprocess.check_call(['touch', pcap])
-        user = getpwuid(stat(pcap).st_uid).pw_name
-        ip(f"netns exec {net} /usr/sbin/tcpdump -Z {user} -i any -w {pcap}")
-        sys.exit(0)
+    pcap = logdir+'/'+net+'.pcap'
+    fd, pcap_tmp = tempfile.mkstemp(suffix=".pcap", prefix=f"{net}-", dir="/tmp")
+    p = subprocess.Popen(
+        ['ip', 'netns', 'exec', net,
+         '/usr/sbin/tcpdump', '-i', 'any', '-w', pcap_tmp])
+    tcpdump_procs.append((p, pcap_tmp, pcap, fd))
 
 # simulate packet loss, duplication and corruption
 for net, iface in [(NET0, VETH0), (NET1, VETH1)]:
@@ -248,7 +248,11 @@ for s in sockets:
 print(f"getsockopt(): {nr_success}/{nr_error}")
 
 print("Stopping network packet captures")
-subprocess.check_call(['killall', '-q', 'tcpdump'])
+for p, pcap_tmp, pcap, fd in tcpdump_procs:
+    p.terminate()
+    p.wait()
+    os.close(fd)
+    shutil.move(pcap_tmp, pcap)
 
 # We're done sending and receiving stuff, now let's check if what
 # we received is what we sent.
Signature · 1 line
2.43.0
maintainer
#4 lore raw
In reply to Allison Henderson
Quoted 6 lines · On Sat, 7 Mar 2026 22:58:32 -0700 Allison Henderson wrote:

This set addresses a few rds selftests clean ups and bugs encountered when running in the ksft framework. The first patch is a clean up patch that addresses pylint warnings, but otherwise no functional changes. The next patch moves the test time out to a ksft settings file so that the time out is set appropriately. And lastly we fix a tcpdump segfault caused by deprecated a os.fork() call.

Looks good! So this is enough to make ksft work? Or just the first batch of obvious fixes? :)

#5 lore raw
Quoted 10 lines · Jakub Kicinski
Quoted 6 lines · On Sat, 7 Mar 2026 22:58:32 -0700 Allison Henderson wrote:

This set addresses a few rds selftests clean ups and bugs encountered when running in the ksft framework. The first patch is a clean up patch that addresses pylint warnings, but otherwise no functional changes. The next patch moves the test time out to a ksft settings file so that the time out is set appropriately. And lastly we fix a tcpdump segfault caused by deprecated a os.fork() call.

Looks good! So this is enough to make ksft work? Or just the first batch of obvious fixes? :)

Those were all the ksft bugs I encountered on my end. Let me know if you run into any trouble with it.

Thank you!
Allison

maintainer
#6 lore raw
Quoted 9 lines · On Wed, 11 Mar 2026 04:56:59 +0000 Allison Henderson wrote:
Quoted 8 lines · Jakub Kicinski
Quoted 6 lines · On Sat, 7 Mar 2026 22:58:32 -0700 Allison Henderson wrote:

This set addresses a few rds selftests clean ups and bugs encountered when running in the ksft framework. The first patch is a clean up patch that addresses pylint warnings, but otherwise no functional changes. The next patch moves the test time out to a ksft settings file so that the time out is set appropriately. And lastly we fix a tcpdump segfault caused by deprecated a os.fork() call.

Quoted 5 lines
Quoted 2 lines

Looks good! So this is enough to make ksft work? Or just the first batch of obvious fixes? :)

Those were all the ksft bugs I encountered on my end. Let me know if you run into any trouble with it.

SG! I'll retry over the weekend

maintainer
#7 lore raw
Quoted 7 lines · On Wed, 11 Mar 2026 17:05:57 -0700 Jakub Kicinski wrote:
Quoted 5 lines
Quoted 2 lines

Looks good! So this is enough to make ksft work? Or just the first batch of obvious fixes? :)

Those were all the ksft bugs I encountered on my end. Let me know if you run into any trouble with it.

SG! I'll retry over the weekend

Didn't get far. We're missing a config file. TAL at other targets there has to be a config file listing all the dependencies. We're building a minimal config for the tests.

I think I already linked but FWIW:
https://github.com/linux-netdev/nipa/wiki/How-to-run-netdev-selftests-CI-style

#8 lore raw
In reply to Allison Henderson
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 10 lines · On Sat, 7 Mar 2026 22:58:32 -0700 you wrote:

Hi all,

This set addresses a few rds selftests clean ups and bugs encountered when running in the ksft framework. The first patch is a clean up patch that addresses pylint warnings, but otherwise no functional changes. The next patch moves the test time out to a ksft settings file so that the time out is set appropriately. And lastly we fix a tcpdump segfault caused by deprecated a os.fork() call.

[...]

Here is the summary with links:
- [net-next,v1,1/3] selftests: rds: Fix pylint warnings

    https://git.kernel.org/netdev/net-next/c/5a0c5702bd00
  - [net-next,v1,2/3] selftests: rds: Add ksft timeout
    https://git.kernel.org/netdev/net-next/c/b873b4e16042
  - [net-next,v1,3/3] selftests: rds: Fix tcpdump segfault in rds selftests
    https://git.kernel.org/netdev/net-next/c/87fdf57ded3d

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html