selftests: rds: Register network teardown via atexit

This patch adds a teardown_tcp() helper that removes net0/net1.
The cmd calls here use fail=False so they can be called from
completed or partially-setup states on error. Also call
teardown_tcp() at the top of setup_tcp() so a previous
interrupted run does not leave net0/net1 lingering and break a
subsequent ip netns add.  Register teardown_tcp() with atexit
before setup_tcp() is invoked.

Likewise, we can simpliy stop_pcaps() handling by registering it
with atexit instead of calling it from the signal handler.

atexit handlers run on any exit path - normal completion, raised
exception, and sys.exit() from the timeout signal handler.  This
guarantees cleanup are called without further wrapping the test
body in a try/finally blocks.

atexit LIFO ordering keeps stop_pcaps before teardown_tcp so
tcpdumps are killed cleanly before their namespaces go away.

This is a preparatory cleanup for the upcoming ROCE patch which
will also register a teardown_rdma() alongside teardown_tcp()

Signed-off-by: Allison Henderson <achender@kernel.org>
Link: https://patch.msgid.link/20260518012443.2629206-10-achender@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
Allison Henderson 2026-05-17 18:24:41 -07:00 committed by Jakub Kicinski
parent 0c4d043f61
commit a887620348

View File

@ -5,6 +5,7 @@ This module provides functional testing for the net/rds component.
"""
import argparse
import atexit
import ctypes
import errno
import hashlib
@ -19,7 +20,7 @@ import sys
this_dir = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(this_dir, "../"))
# pylint: disable-next=wrong-import-position,import-error,no-name-in-module
from lib.py.utils import ip # noqa: E402
from lib.py.utils import ip, cmd # noqa: E402
# pylint: disable-next=wrong-import-position,import-error,no-name-in-module
from lib.py.ksft import ksft_pr # noqa: E402
@ -247,7 +248,6 @@ def signal_handler(_sig, _frame):
Test timed out signal handler
"""
ksft_pr("Test timed out")
stop_pcaps()
print("not ok 1 rds selftest")
sys.exit(1)
@ -256,6 +256,9 @@ def setup_tcp():
Configure tcp network
"""
# clean up any leftovers from a previously interrupted run
teardown_tcp()
ip(f"netns add {NET0}")
ip(f"netns add {NET1}")
ip("link add type veth")
@ -300,6 +303,17 @@ def setup_tcp():
corrupt {PACKET_CORRUPTION} loss {PACKET_LOSS} duplicate \
{PACKET_DUPLICATE}")
def teardown_tcp():
"""
Tear down the tcp network configured by setup_tcp().
Removing the namespaces also removes the veth pair, addresses,
routes, and netem qdisc that live inside them. fail=False so
this is safe to call in error paths after a partial or complete setup.
"""
cmd(f"ip netns del {NET0}", fail=False)
cmd(f"ip netns del {NET1}", fail=False)
#Parse out command line arguments. We take an optional
# timeout parameter and an optional log output folder
parser = argparse.ArgumentParser(description="init script args",
@ -320,6 +334,11 @@ PACKET_LOSS=str(args.loss)+'%'
PACKET_CORRUPTION=str(args.corruption)+'%'
PACKET_DUPLICATE=str(args.duplicate)+'%'
# Register cleanup before setup so a partial-setup crash still tears down
# whatever state did get created.
atexit.register(teardown_tcp)
atexit.register(stop_pcaps)
setup_tcp()
print("TAP version 13")
@ -335,8 +354,6 @@ ret = snd_rcv_packets(tcp_addrs, [NET0, NET1])
# cancel timeout
signal.alarm(0)
stop_pcaps()
if ret == 0:
ksft_pr("Success")
print("ok 1 rds selftest")