1#!/usr/bin/env python3
2# SPDX-License-Identifier: GPL-2.0
3
4import re
5from os import path
6from lib.py import ksft_run, ksft_exit
7from lib.py import NetDrvEpEnv
8from lib.py import bkg, cmd, ethtool, wait_port_listen
9
10
11def _get_rx_ring_entries(cfg):
12    output = ethtool(f"-g {cfg.ifname}", host=cfg.remote).stdout
13    values = re.findall(r'RX:\s+(\d+)', output)
14    return int(values[1])
15
16
17def _get_combined_channels(cfg):
18    output = ethtool(f"-l {cfg.ifname}", host=cfg.remote).stdout
19    values = re.findall(r'Combined:\s+(\d+)', output)
20    return int(values[1])
21
22
23def _set_flow_rule(cfg, chan):
24    output = ethtool(f"-N {cfg.ifname} flow-type tcp6 dst-port 9999 action {chan}", host=cfg.remote).stdout
25    values = re.search(r'ID (\d+)', output).group(1)
26    return int(values)
27
28
29def test_zcrx(cfg) -> None:
30    cfg.require_ipver('6')
31
32    combined_chans = _get_combined_channels(cfg)
33    if combined_chans < 2:
34        raise KsftSkipEx('at least 2 combined channels required')
35    rx_ring = _get_rx_ring_entries(cfg)
36
37    try:
38        ethtool(f"-G {cfg.ifname} tcp-data-split on", host=cfg.remote)
39        ethtool(f"-G {cfg.ifname} rx 64", host=cfg.remote)
40        ethtool(f"-X {cfg.ifname} equal {combined_chans - 1}", host=cfg.remote)
41        flow_rule_id = _set_flow_rule(cfg, combined_chans - 1)
42
43        rx_cmd = f"{cfg.bin_remote} -s -p 9999 -i {cfg.ifname} -q {combined_chans - 1}"
44        tx_cmd = f"{cfg.bin_local} -c -h {cfg.remote_addr_v['6']} -p 9999 -l 12840"
45        with bkg(rx_cmd, host=cfg.remote, exit_wait=True):
46            wait_port_listen(9999, proto="tcp", host=cfg.remote)
47            cmd(tx_cmd)
48    finally:
49        ethtool(f"-N {cfg.ifname} delete {flow_rule_id}", host=cfg.remote)
50        ethtool(f"-X {cfg.ifname} default", host=cfg.remote)
51        ethtool(f"-G {cfg.ifname} rx {rx_ring}", host=cfg.remote)
52        ethtool(f"-G {cfg.ifname} tcp-data-split auto", host=cfg.remote)
53
54
55def test_zcrx_oneshot(cfg) -> None:
56    cfg.require_ipver('6')
57
58    combined_chans = _get_combined_channels(cfg)
59    if combined_chans < 2:
60        raise KsftSkipEx('at least 2 combined channels required')
61    rx_ring = _get_rx_ring_entries(cfg)
62
63    try:
64        ethtool(f"-G {cfg.ifname} tcp-data-split on", host=cfg.remote)
65        ethtool(f"-G {cfg.ifname} rx 64", host=cfg.remote)
66        ethtool(f"-X {cfg.ifname} equal {combined_chans - 1}", host=cfg.remote)
67        flow_rule_id = _set_flow_rule(cfg, combined_chans - 1)
68
69        rx_cmd = f"{cfg.bin_remote} -s -p 9999 -i {cfg.ifname} -q {combined_chans - 1} -o 4"
70        tx_cmd = f"{cfg.bin_local} -c -h {cfg.remote_addr_v['6']} -p 9999 -l 4096 -z 16384"
71        with bkg(rx_cmd, host=cfg.remote, exit_wait=True):
72            wait_port_listen(9999, proto="tcp", host=cfg.remote)
73            cmd(tx_cmd)
74    finally:
75        ethtool(f"-N {cfg.ifname} delete {flow_rule_id}", host=cfg.remote)
76        ethtool(f"-X {cfg.ifname} default", host=cfg.remote)
77        ethtool(f"-G {cfg.ifname} rx {rx_ring}", host=cfg.remote)
78        ethtool(f"-G {cfg.ifname} tcp-data-split auto", host=cfg.remote)
79
80
81def main() -> None:
82    with NetDrvEpEnv(__file__) as cfg:
83        cfg.bin_local = path.abspath(path.dirname(__file__) + "/../../../drivers/net/hw/iou-zcrx")
84        cfg.bin_remote = cfg.remote.deploy(cfg.bin_local)
85
86        ksft_run(globs=globals(), case_pfx={"test_"}, args=(cfg, ))
87    ksft_exit()
88
89
90if __name__ == "__main__":
91    main()
92