From: Mohsin Bashir <hmoh…@meta.com> An ntuple rule and a policer can match the same flow, and a device may implement both with a single classifier lookup. Add police_with_ntuple to verify that, in either install order, the flow is rate limited and lands on the ntuple queue only. A device without ntuple filters, or one that rejects the second rule, is skipped. A rejected policer must not be left behind. Signed-off-by: Jakub Kicinski <ku…@kernel.org> Signed-off-by: Mohsin Bashir <hmoh…@meta.com> --- .../drivers/net/hw/tc_flower_police.py | 118 +++++++++++++++++- 1 file changed, 117 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/drivers/net/hw/tc_flower_police.py b/tools/testing/selftests/drivers/net/hw/tc_flower_police.py index 368882984411..d0315e8bf2b2 100755 --- a/tools/testing/selftests/drivers/net/hw/tc_flower_police.py +++ b/tools/testing/selftests/drivers/net/hw/tc_flower_police.py @@ -12,7 +12,7 @@ from lib.py import ksft_run, ksft_exit, ksft_pr from lib.py import ksft_eq, ksft_ge from lib.py import ksft_disruptive, ksft_variants, KsftNamedVariant from lib.py import CmdExitFailure, KsftSkipEx, KsftXfailEx, KsftFailEx, NlError -from lib.py import Iperf3Runner, NetDrvEpEnv, EthtoolFamily +from lib.py import Iperf3Runner, NetDrvEpEnv, NetdevFamily, EthtoolFamily from lib.py import set_ethtool_feat from lib.py import cmd, ctl_file_write, defer, ethtool, ip, tc from lib.py import rand_port, rand_ports, wait_file @@ -569,6 +569,120 @@ def police_ring_size(cfg): _check_rate(got, RATE_MBIT, "after ring resize") +def _set_rss_indir(cfg, ehdr, indir): + try: + cfg.ethnl.rss_set(ehdr | {"indir": indir}) + except NlError as ex: + raise KsftSkipEx("Cannot change the RSS indirection table") from ex + defer(cfg.ethnl.rss_set, ehdr | {"indir": None}) + + +def _require_ntuple(cfg): + feat = ethtool(f"-k {cfg.ifname}", json=True)[0]["ntuple-filters"] + if not feat["active"]: + if feat["fixed"]: + raise KsftSkipEx("Device does not support ntuple-filters") + defer(ethtool, f"-K {cfg.ifname} ntuple-filters off") + cmd(f"ethtool -K {cfg.ifname} ntuple-filters on", fail=False) + feat = ethtool(f"-k {cfg.ifname}", json=True)[0]["ntuple-filters"] + if not feat["active"]: + raise KsftSkipEx("Cannot turn ntuple-filters on") + + +def _rx_queue_pkts(cfg): + cfg.wait_hw_stats_settle() + try: + stats = cfg.netdevnl.qstats_get({"ifindex": cfg.ifindex, + "scope": ["queue"]}, dump=True) + except NlError as ex: + raise KsftSkipEx("Device does not report queue stats") from ex + pkts = {s["queue-id"]: s["rx-packets"] for s in stats + if s["queue-type"] == "rx" and "rx-packets" in s} + if not pkts: + raise KsftSkipEx("Device does not report RX packets per queue") + return pkts + + +def _expected_pkts(mbit): + # The client sends for -t plus the -O second + return mbit * 1e6 * (MEASURE_SEC + 1) / 8 / IP6_PKT_LEN + + +def _add_ntuple(cfg, flow): + res = cmd(f"ethtool -N {cfg.ifname} {flow}", fail=False) + if res.ret: + ksft_pr(res.stderr.strip()) + raise KsftSkipEx("ntuple rule rejected") + defer(ethtool, f"-N {cfg.ifname} delete {int(res.stdout.split()[-1])}") + + …@ksft_variants([ + KsftNamedVariant("ntuple_first", True), + KsftNamedVariant("police_first", False), +]) +def police_with_ntuple(cfg, ntuple_first): + """ + Check that an ntuple rule and a police rule for the same flow both take + effect, whichever is installed first: the flow is policed and lands on + the ntuple queue only. + """ + _setup_clsact(cfg) + rule, dst = _dst_ip_rule(cfg) + _require_ntuple(cfg) + _rx_queue_pkts(cfg) + + ehdr = {"header": {"dev-index": cfg.ifindex}} + try: + channels = cfg.ethnl.channels_get(ehdr) + except NlError as ex: + raise KsftSkipEx("channel-get not supported by device") from ex + cnt = channels.get("combined-count", 0) + if cnt < 2: + if channels.get("combined-max", 0) < 2: + raise KsftSkipEx("Need at least 2 combined channels") + defer(wait_file, f"/sys/class/net/{cfg.ifname}/carrier", + lambda x: x.strip() == "1", deadline=30) + defer(cfg.ethnl.channels_set, ehdr | {"combined-count": cnt}) + try: + cfg.ethnl.channels_set(ehdr | {"combined-count": 2}) + except NlError as ex: + raise KsftSkipEx("Cannot set 2 combined channels") from ex + wait_file(f"/sys/class/net/{cfg.ifname}/carrier", + lambda x: x.strip() == "1", deadline=30) + cnt = 2 + queue = cnt - 1 + _set_rss_indir(cfg, ehdr, [0]) + + _check_baseline_rate(cfg) + flow = f"flow-type udp6 dst-ip {dst} action {queue}" + if ntuple_first: + _add_ntuple(cfg, flow) + added, err = _try_filter(cfg, rule, _police(RATE_MBIT), pref=10) + if not added: + _skip_rejected(cfg, err, 10, "Policer next to the ntuple rule") + else: + _add_filter(cfg, rule, _police(RATE_MBIT), pref=10) + try: + _add_ntuple(cfg, flow) + except KsftSkipEx: + # Rejecting the ntuple rule must not break the policer + got = _iperf3(cfg, dst, OFFERED_MBIT) + if _check_rate(got, RATE_MBIT, "policer after ntuple rejection"): + raise + return + + before = _rx_queue_pkts(cfg) + got = _iperf3(cfg, dst, OFFERED_MBIT) + after = _rx_queue_pkts(cfg) + _check_rate(got, RATE_MBIT, "ntuple steered flow") + + delta = {q: after[q] - before.get(q, 0) for q in after} + ksft_ge(delta.get(queue, 0), _expected_pkts(got) * 0.9, + f"Policed flow not on ntuple queue {queue}: {delta}") + idle = sum(delta.values()) - delta.get(0, 0) - delta.get(queue, 0) + ksft_eq(idle, 0, f"Traffic on queues other than 0 and {queue}: {delta}") + + def _link_down(cfg): ctl_file_write("/proc/sys/net/ipv6/conf/all/keep_addr_on_down", 1) ip(f"link set dev {cfg.ifname} down") @@ -628,6 +742,7 @@ def main() -> None: """Ksft boilerplate main.""" with NetDrvEpEnv(__file__, nsim_test=False) as cfg: cfg.ethnl = EthtoolFamily() + cfg.netdevnl = NetdevFamily() cfg.baseline = None ksft_run([offload_flags, police_no_leak, @@ -642,6 +757,7 @@ def main() -> None: police_delete, police_channels, police_ring_size, + police_with_ntuple, police_link_flap, police_add_while_down], args=(cfg,))
-- 2.53.1-Meta