1#!/usr/bin/env python3
2# SPDX-License-Identifier: GPL-2.0
3
4import multiprocessing
5import socket
6from lib.py import ksft_run, ksft_exit, ksft_eq, ksft_ge, cmd, fd_read_timeout
7from lib.py import NetDrvEpEnv
8from lib.py import EthtoolFamily, NetdevFamily
9from lib.py import KsftSkipEx, KsftFailEx
10from lib.py import rand_port
11
12
13def traffic(cfg, local_port, remote_port, ipver):
14    af_inet = socket.AF_INET if ipver == "4" else socket.AF_INET6
15    sock = socket.socket(af_inet, socket.SOCK_DGRAM)
16    sock.bind(("", local_port))
17    sock.connect((cfg.remote_addr_v[ipver], remote_port))
18    tgt = f"{ipver}:[{cfg.addr_v[ipver]}]:{local_port},sourceport={remote_port}"
19    cmd("echo a | socat - UDP" + tgt, host=cfg.remote)
20    fd_read_timeout(sock.fileno(), 5)
21    return sock.getsockopt(socket.SOL_SOCKET, socket.SO_INCOMING_CPU)
22
23
24def test_rss_input_xfrm(cfg, ipver):
25    """
26    Test symmetric input_xfrm.
27    If symmetric RSS hash is configured, send traffic twice, swapping the
28    src/dst UDP ports, and verify that the same queue is receiving the traffic
29    in both cases (IPs are constant).
30    """
31
32    if multiprocessing.cpu_count() < 2:
33        raise KsftSkipEx("Need at least two CPUs to test symmetric RSS hash")
34
35    cfg.require_cmd("socat", local=False, remote=True)
36
37    if not hasattr(socket, "SO_INCOMING_CPU"):
38        raise KsftSkipEx("socket.SO_INCOMING_CPU was added in Python 3.11")
39
40    rss = cfg.ethnl.rss_get({'header': {'dev-name': cfg.ifname}})
41    input_xfrm = set(filter(lambda x: 'sym' in x, rss.get('input-xfrm', {})))
42
43    # Check for symmetric xor/or-xor
44    if not input_xfrm:
45        raise KsftSkipEx("Symmetric RSS hash not requested")
46
47    cpus = set()
48    successful = 0
49    for _ in range(100):
50        try:
51            port1 = rand_port(socket.SOCK_DGRAM)
52            port2 = rand_port(socket.SOCK_DGRAM)
53            cpu1 = traffic(cfg, port1, port2, ipver)
54            cpu2 = traffic(cfg, port2, port1, ipver)
55            cpus.update([cpu1, cpu2])
56            ksft_eq(
57                cpu1, cpu2, comment=f"Received traffic on different cpus with ports ({port1 = }, {port2 = }) while symmetric hash is configured")
58
59            successful += 1
60            if successful == 10:
61                break
62        except:
63            continue
64    else:
65        raise KsftFailEx("Failed to run traffic")
66
67    ksft_ge(len(cpus), 2,
68            comment=f"Received traffic on less than two cpus {cpus = }")
69
70
71def test_rss_input_xfrm_ipv4(cfg):
72    cfg.require_ipver("4")
73    test_rss_input_xfrm(cfg, "4")
74
75
76def test_rss_input_xfrm_ipv6(cfg):
77    cfg.require_ipver("6")
78    test_rss_input_xfrm(cfg, "6")
79
80
81def main() -> None:
82    with NetDrvEpEnv(__file__, nsim_test=False) as cfg:
83        cfg.ethnl = EthtoolFamily()
84        cfg.netdevnl = NetdevFamily()
85
86        ksft_run([test_rss_input_xfrm_ipv4, test_rss_input_xfrm_ipv6],
87                 args=(cfg, ))
88    ksft_exit()
89
90
91if __name__ == "__main__":
92    main()
93