Linux Kernel Selftest development
 help / color / mirror / Atom feed
From: Mohsin Bashir <mohsin.bashr@gmail.com>
To: netdev@vger.kernel.org
Cc: andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@kernel.org,
	kuba@kernel.org, pabeni@redhat.com, shuah@kernel.org,
	linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH net-next 04/11] selftests: drv-net: tc_flower_police: Test the policed rate and its scope
Date: Thu,  3 Dec 2026 16:05:14 -0800	[thread overview]
Message-ID: <20261204000521.3396802-5-mohsin.bashr@gmail.com> (raw)
In-Reply-To: <20261204000521.3396802-1-mohsin.bashr@gmail.com>

From: Mohsin Bashir <hmohsin@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 <kuba@kernel.org>
Signed-off-by: Mohsin Bashir <hmohsin@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


  parent reply	other threads:[~2026-12-04  0:05 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  0:05 [PATCH net-next 00/11] selftests: drv-net: Test tc-flower police offload Mohsin Bashir
2026-12-04  0:05 ` [PATCH net-next 01/11] selftests: drv-net: Iperf3Runner: Support UDP and server side results Mohsin Bashir
2026-12-04  0:05 ` [PATCH net-next 02/11] selftests: drv-net: Add tc-flower police offload test Mohsin Bashir
2026-12-04  0:05 ` [PATCH net-next 03/11] selftests: drv-net: tc_flower_police: Test that rules do not leak Mohsin Bashir
2026-12-04  0:05 ` Mohsin Bashir [this message]
2026-12-04  0:05 ` [PATCH net-next 05/11] selftests: drv-net: tc_flower_police: Test destination prefix rules Mohsin Bashir
2026-12-04  0:05 ` [PATCH net-next 06/11] selftests: drv-net: tc_flower_police: Test police parameters Mohsin Bashir
2026-12-04  0:05 ` [PATCH net-next 07/11] selftests: drv-net: tc_flower_police: Test multiple policers Mohsin Bashir
2026-12-04  0:05 ` [PATCH net-next 08/11] selftests: drv-net: tc_flower_police: Test rule removal Mohsin Bashir
2026-12-04  0:05 ` [PATCH net-next 09/11] selftests: drv-net: tc_flower_police: Test policing across reconfiguration Mohsin Bashir
2026-12-04  0:05 ` [PATCH net-next 10/11] selftests: drv-net: tc_flower_police: Test policing with ntuple steering Mohsin Bashir
2026-12-04  0:05 ` [PATCH net-next 11/11] selftests: drv-net: tc_flower_police: Test policing with RSS contexts Mohsin Bashir

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204000521.3396802-5-mohsin.bashr@gmail.com \
    --to=mohsin.bashr@gmail.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=shuah@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox