From: Mohsin Bashir <hmoh…@meta.com>
Police offload is only useful if matching traffic is held to the
configured rate and other traffic is left alone. Add police_rate to
verify that UDP sent at twice the police rate arrives at the police
rate, and police_isolation to verify that traffic to another address of
the device is not limited.
The rate is measured on the receiver, scaled from the UDP payload to the
IP packet size that act_police counts, and must be within 10%.
link_baseline measures the unpoliced rate, and the policing tests skip
if it does not reach the offered rate, so a slow link is not mistaken
for a working policer.
Signed-off-by: Jakub Kicinski <ku…@kernel.org>
Signed-off-by: Mohsin Bashir <hmoh…@meta.com>
---
.../drivers/net/hw/tc_flower_police.py | 158 +++++++++++++++++-
1 file changed, 154 insertions(+), 4 deletions(-)
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 b03a00ad0c0b..b42ed22419ad 100755
--- a/tools/testing/selftests/drivers/net/hw/tc_flower_police.py
+++ b/tools/testing/selftests/drivers/net/hw/tc_flower_police.py
@@ -3,14 +3,25 @@
"""Test HW offload of tc-flower rules with a police action on ingress."""
import ipaddress
+import json
+import random
import tempfile
import time
from lib.py import ksft_run, ksft_exit, ksft_pr
-from lib.py import ksft_eq
+from lib.py import ksft_eq, ksft_ge
from lib.py import CmdExitFailure, KsftSkipEx, KsftXfailEx, KsftFailEx
-from lib.py import NetDrvEpEnv
+from lib.py import Iperf3Runner, NetDrvEpEnv
from lib.py import set_ethtool_feat
-from lib.py import cmd, defer, ethtool, tc
+from lib.py import cmd, defer, ethtool, ip, tc
+from lib.py import rand_port
+
+
+RATE_MBIT = 100
+TOLERANCE = 0.10
+OFFERED_MBIT = 2 * RATE_MBIT
+MEASURE_SEC = 5
+UDP_PAYLOAD = 1200
+IP6_PKT_LEN = UDP_PAYLOAD + 8 + 40
def _setup_clsact(cfg):
@@ -36,6 +47,27 @@ def _local_prefix(cfg):
return net
+def _dev_addrs(cfg):
+ addrs = ip(f"-6 addr show dev {cfg.ifname}", json=True)[0]
+ return {a["local"] for a in addrs.get("addr_info", [])}
+
+
+def _add_secondary_addr(cfg, want=None):
+ net = _local_prefix(cfg)
+
+ taken = _dev_addrs(cfg)
+ hosts = range(1, min(net.num_addresses, 1 << 16))
+ for i in random.sample(hosts, len(hosts)):
+ addr = str(net[i])
+ if addr not in taken and (want is None or want(addr)):
+ break
+ else:
+ raise KsftSkipEx("No free address in LOCAL_PREFIX_V6")
+ ip(f"-6 addr add {addr}/128 dev {cfg.ifname} nodad")
+ defer(ip, f"-6 addr del {addr}/128 dev {cfg.ifname}")
+ return addr
+
+
def _police(rate_mbit):
return (f"action police rate {rate_mbit}mbit burst 1m "
"conform-exceed drop/ok")
@@ -79,6 +111,81 @@ def _ctrl_rule(cfg, i=0):
return f"protocol ipv6 flower dst_ip {net[i + 1]}"
+def _payload_mbit(mbit):
+ # iperf3 rates only count the UDP payload
+ return round(mbit * UDP_PAYLOAD / IP6_PKT_LEN, 3)
+
+
+def _iperf3(cfg, dst_addr, bw_mbit):
+ runner = Iperf3Runner(cfg, port=rand_port(), server_ip=dst_addr)
+ server = runner.start_server(json=True)
+ done = False
+ try:
+ runner.start_client(duration=MEASURE_SEC, omit=1,
+ udp_mbit=_payload_mbit(bw_mbit),
+ length=UDP_PAYLOAD)
+ done = True
+ except CmdExitFailure as ex:
+ out = f"{ex.cmd.stderr} {ex.cmd.stdout}"
+ raise KsftFailEx(f"iperf3 failed: {out}") from ex
+ finally:
+ server.process(terminate=not done, fail=False, timeout=5)
+ return _rx_mbit(server.stdout)
+
+
+def _rx_ivals(out):
+ try:
+ res = json.loads(out)
+ if "error" in res:
+ raise KsftFailEx(f"iperf3 server: {res['error']}")
+ return [(i["sum"]["seconds"],
+ i["sum"]["bytes"] * IP6_PKT_LEN / UDP_PAYLOAD)
+ for i in res["intervals"]
+ if not i["sum"].get("omitted")]
+ except (ValueError, KeyError) as ex:
+ raise KsftFailEx(f"iperf3 server output: {out}") from ex
+
+
+def _rx_mbit(out):
+ ivals = _rx_ivals(out)
+ secs = sum(secs for secs, _ in ivals)
+ rx_bytes = sum(rx_bytes for _, rx_bytes in ivals)
+ return rx_bytes * 8 / secs / 1e6 if secs else 0
+
+
+def _check_rate(got, expect, what):
+ low, high = expect * (1 - TOLERANCE), expect * (1 + TOLERANCE)
+ ksft_ge(high, got, f"{what} above the expected {expect:.0f} Mbit/s")
+ ksft_ge(got, low, f"{what} below the expected {expect:.0f} Mbit/s")
+ return low <= got <= high
+
+
+def _check_unpoliced(got, rate_mbit, what):
+ ksft_ge(got, rate_mbit * (1 + TOLERANCE), f"{what} looks policed")
+
+
+def _get_baseline(cfg):
+ if cfg.baseline is None:
+ cfg.require_ipver("6")
+ cfg.require_cmd("iperf3", local=True, remote=True)
+ cfg.baseline = _iperf3(cfg, cfg.addr_v["6"], OFFERED_MBIT)
+ ksft_pr(f"Unpoliced UDP rate {cfg.baseline:.0f} Mbit/s")
+ return cfg.baseline
+
+
+def _check_baseline_rate(cfg):
+ want = OFFERED_MBIT * (1 - TOLERANCE)
+ baseline = _get_baseline(cfg)
+ if baseline < want:
+ raise KsftSkipEx(f"Baseline rate {baseline:.0f} Mbit/s too low, "
+ f"need {want:.0f} Mbit/s")
+
+
+def _dst_ip_rule(cfg):
+ dst = _add_secondary_addr(cfg)
+ return f"protocol ipv6 flower dst_ip {dst}", dst
+
+
def offload_flags(cfg):
"""
Check that a skip_sw police rule is offloaded and reported in_hw, and
@@ -152,11 +259,54 @@ def police_no_leak(cfg):
ksft_eq(refill, full, "Rules taken after the flush")
+def link_baseline(cfg):
+ """
+ Measure the unpoliced UDP rate, so that the policing tests can skip if
+ the link is too slow to tell policed traffic from unpoliced.
+ """
+ if not _get_baseline(cfg):
+ raise KsftFailEx("No UDP traffic received")
+
+
+def police_rate(cfg):
+ """
+ Check that UDP sent at twice the police rate to the destination IP of
+ a rule is limited to that rate.
+ """
+ _setup_clsact(cfg)
+ rule, dst = _dst_ip_rule(cfg)
+
+ _check_baseline_rate(cfg)
+ _add_filter(cfg, rule, _police(RATE_MBIT), pref=10)
+ _check_in_hw(cfg, 10)
+
+ got = _iperf3(cfg, dst, OFFERED_MBIT)
+ _check_rate(got, RATE_MBIT, "matching flow")
+
+
+def police_isolation(cfg):
+ """
+ Check that traffic to another address of the device is not policed.
+ """
+ _setup_clsact(cfg)
+ rule, _ = _dst_ip_rule(cfg)
+
+ _check_baseline_rate(cfg)
+ _add_filter(cfg, rule, _police(RATE_MBIT), pref=10)
+
+ got = _iperf3(cfg, cfg.addr_v["6"], OFFERED_MBIT)
+ _check_unpoliced(got, RATE_MBIT, "other address")
+
+
def main() -> None:
"""Ksft boilerplate main."""
with NetDrvEpEnv(__file__, nsim_test=False) as cfg:
+ cfg.baseline = None
ksft_run([offload_flags,
- police_no_leak],
+ police_no_leak,
+ link_baseline,
+ police_rate,
+ police_isolation],
args=(cfg,))
ksft_exit()
--
2.53.1-Meta