The test script is provided by Kuniyuki in [1], which is used to test the selection of the TCP reuseport socket problem.
Link: https://lore.kernel.org/netdev/20250801040757.1599996-1-kuniyu@google.com/ [1] Signed-off-by: Menglong Dong dongml2@chinatelecom.cn --- tools/testing/selftests/net/Makefile | 1 + tools/testing/selftests/net/tcp_reuseport.py | 36 ++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100755 tools/testing/selftests/net/tcp_reuseport.py
diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile index b31a71f2b372..0f4c3eea9709 100644 --- a/tools/testing/selftests/net/Makefile +++ b/tools/testing/selftests/net/Makefile @@ -117,6 +117,7 @@ TEST_GEN_FILES += tfo TEST_PROGS += tfo_passive.sh TEST_PROGS += broadcast_pmtu.sh TEST_PROGS += ipv6_force_forwarding.sh +TEST_PROGS += tcp_reuseport.py
# YNL files, must be before "include ..lib.mk" YNL_GEN_FILES := busy_poller netlink-dumps diff --git a/tools/testing/selftests/net/tcp_reuseport.py b/tools/testing/selftests/net/tcp_reuseport.py new file mode 100755 index 000000000000..eaeb7096382e --- /dev/null +++ b/tools/testing/selftests/net/tcp_reuseport.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0 + +import os + +from lib.py import ksft_run, ksft_exit +from socket import * + +def test_reuseport_select() -> None: + s1 = socket() + s1.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1) + s1.setsockopt(SOL_SOCKET, SO_BINDTODEVICE, b'lo') + s1.listen() + s1.setblocking(False) + + s2 = socket() + s2.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1) + s2.bind(s1.getsockname()) + s2.listen() + s2.setblocking(False) + + for i in range(3): + c = socket() + c.connect(s1.getsockname()) + try: + print("SUCCESS: assigned properly:", s1.accept()) + except: + print("FAIL: wrong assignment") + os.sys.exit(1) + +def main() -> None: + ksft_run([test_reuseport_select]) + ksft_exit() + +if __name__ == "__main__": + main()