selftest: net: Add selftest for netpoll
Subsystems (4)
v4 cover selftest: net: Add selftest for netpoll
I am submitting a new selftest for the netpoll subsystem specifically targeting the case where the RX is polling in the TX path, which is a case that we don't have any test in the tree today. This is done when netpoll_poll_dev() called, and this test creates a scenario when that is probably.
The test does the following:
1) Configuring a single RX/TX queue to increase contention on the interface.
2) Generating background traffic to saturate the network, mimicking real-world congestion.
3) Sending netconsole messages to trigger netpoll polling and monitor its behavior.
4) Using dynamic netconsole targets via configfs, with the ability to delete and recreate targets during the test.
5) Running bpftrace in parallel to verify that netpoll_poll_dev() is called when expected. If it is called, then the test passes, otherwise the test is marked as skipped.
In order to achieve it, I stole Jakub's bpftrace helper from [1], and did some small changes that I found useful to use the helper.
So, this patchset basically contains:
1) The code stolen from Jakub
2) Improvements on bpftrace() helper
3) The selftest itself
---
Changes in v4:
- Make the test XFail if it doesn't hit the function we are looking for
- Toggle the interface while the traffic is flowing.
- Bumped the number of messages from 10 to 40 per iterations.
* This is hitting ~15 times per run on my vng test.
- Decreased the time from 15 seconds to 10 seconds, given that if
it didn't hit the function in 10 seconds, 5 seconds extra will not
help.
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- Make pylint happy (Simon)
- Remove the unnecessary patch in bpftrace to raise an exception when it fails. (Jakub)
- Improved the bpftrace code (Willem)
- Stop sending messages if bpftrace is not alive anymore.
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Stole Jakub's helper to run bpftrace
- Removed the DEBUG option and moved logs to logging
- Change the code to have a higher chance of calling netpoll_poll_dev().
In my current configuration, it is hitting multiple times during the test.
- Save and restore TX/RX queue size (Jakub)
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Breno Leitao (2):
selftests: drv-net: Strip '@' prefix from bpftrace map keys
selftests: net: add netpoll basic functionality test
Jakub Kicinski (1):
selftests: drv-net: add helper/wrapper for bpftrace
tools/testing/selftests/drivers/net/Makefile | 1 + .../selftests/drivers/net/lib/py/__init__.py | 3 +- .../testing/selftests/drivers/net/netpoll_basic.py | 365 +++++++++++++++++++++ tools/testing/selftests/net/lib/py/utils.py | 35 ++ 4 files changed, 403 insertions(+), 1 deletion(-)
---
base-commit: 22e1cfda0f612556f116560d2b7e9c3315636bfb
change-id: 20250612-netpoll_test-a1324d2057c8
Best regards,
Signature · 1 line
Breno Leitao <[email protected]>
PATCH v4 1/3 selftests: drv-net: add helper/wrapper for bpftrace
From: Jakub Kicinski <[email protected]>
bpftrace is very useful for low level driver testing. perf or trace-cmd would also do for collecting data from tracepoints, but they require much more post-processing.
Add a wrapper for running bpftrace and sanitizing its output. bpftrace has JSON output, which is great, but it prints loose objects and in a slightly inconvenient format. We have to read the objects line by line, and while at it return them indexed by the map name.
--- .../selftests/drivers/net/lib/py/__init__.py | 3 +- tools/testing/selftests/net/lib/py/utils.py | 33 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-)
tools/testing/selftests/drivers/net/lib/py/__init__.py +2 -1
@@ -14,7 +14,8 @@ try: from net.lib.py import EthtoolFamily, NetdevFamily, NetshaperFamily, \ NlError, RtnlFamily from net.lib.py import CmdExitFailure - from net.lib.py import bkg, cmd, defer, ethtool, fd_read_timeout, ip, \ + from net.lib.py import bkg, cmd, bpftrace, defer, ethtool, \ + fd_read_timeout, ip, \ rand_port, tool, wait_port_listen from net.lib.py import fd_read_timeout from net.lib.py import KsftSkipEx, KsftFailEx, KsftXfailEx
tools/testing/selftests/net/lib/py/utils.py +33 -0
@@ -185,6 +185,39 @@ def ethtool(args, json=None, ns=None, host=None): return tool('ethtool', args, json=json, ns=ns, host=host) +def bpftrace(expr, json=None, ns=None, host=None, timeout=None): + """ + Run bpftrace and return map data (if json=True). + The output of bpftrace is inconvenient, so the helper converts + to a dict indexed by map name, e.g.: + { + "@": { ... }, + "@map2": { ... }, + } + """ + cmd_arr = ['bpftrace'] + # Throw in --quiet if json, otherwise the output has two objects + if json: + cmd_arr += ['-f', 'json', '-q'] + if timeout: + expr += ' interval:s:' + str(timeout) + ' { exit(); }' + cmd_arr += ['-e', expr] + cmd_obj = cmd(cmd_arr, ns=ns, host=host, shell=False) + if json: + # bpftrace prints objects as lines + ret = {} + for l in cmd_obj.stdout.split('\n'): + if not l.strip(): + continue + one = _json.loads(l) + if one.get('type') != 'map': + continue + for k, v in one["data"].items(): + ret[k] = v + return ret + return cmd_obj + + def rand_port(type=socket.SOCK_STREAM): """ Get a random unprivileged port.
Signature · 1 line
2.47.1
PATCH v4 2/3 selftests: drv-net: Strip '@' prefix from bpftrace map keys
In reply to Breno LeitaoThe '@' prefix in bpftrace map keys is specific to bpftrace and can be safely removed when processing results. This patch modifies the bpftrace utility to strip the '@' from map keys before storing them in the result dictionary, making the keys more consistent with Python conventions.
--- tools/testing/selftests/net/lib/py/utils.py | 2 ++ 1 file changed, 2 insertions(+)
tools/testing/selftests/net/lib/py/utils.py +2 -0
@@ -213,6 +213,8 @@ def bpftrace(expr, json=None, ns=None, host=None, timeout=None): if one.get('type') != 'map': continue for k, v in one["data"].items(): + if k.startswith('@'): + k = k.lstrip('@') ret[k] = v return ret return cmd_obj
Signature · 1 line
2.47.1
PATCH v4 3/3 selftests: net: add netpoll basic functionality test
In reply to Breno LeitaoAdd a basic selftest for the netpoll polling mechanism, specifically targeting the netpoll poll() side.
The test creates a scenario where network transmission is running at maximum speed, and netpoll needs to poll the NIC. This is achieved by:
1. Configuring a single RX/TX queue to create contention
2. Generating background traffic to saturate the interface
3. Sending netconsole messages to trigger netpoll polling
4. Using dynamic netconsole targets via configfs
5. Delete and create new netconsole targets after some messages
6. Start a bpftrace in parallel to make sure netpoll_poll_dev() is called
7. If bpftrace exists and netpoll_poll_dev() was called, stop.
The test validates a critical netpoll code path by monitoring traffic flow and ensuring netpoll_poll_dev() is called when the normal TX path is blocked.
This addresses a gap in netpoll test coverage for a path that is tricky for the network stack.
--- tools/testing/selftests/drivers/net/Makefile | 1 + .../testing/selftests/drivers/net/netpoll_basic.py | 365 +++++++++++++++++++++ 2 files changed, 366 insertions(+)
tools/testing/selftests/drivers/net/Makefile +1 -0
@@ -16,6 +16,7 @@ TEST_PROGS := \ netcons_fragmented_msg.sh \ netcons_overflow.sh \ netcons_sysdata.sh \ + netpoll_basic.py \ ping.py \ queues.py \ stats.py \
tools/testing/selftests/drivers/net/netpoll_basic.py new file +365 -0
365 lines not on this page · raw message
Signature · 1 line
2.47.1
Re: [PATCH net-next v4 3/3] selftests: net: add netpoll basic functionality test
Quoted 15 lines · Breno Leitao
Add a basic selftest for the netpoll polling mechanism, specifically targeting the netpoll
poll()side.The test creates a scenario where network transmission is running at maximum speed, and netpoll needs to poll the NIC. This is achieved by:
1. Configuring a single RX/TX queue to create contention
2. Generating background traffic to saturate the interface
3. Sending netconsole messages to trigger netpoll polling
4. Using dynamic netconsole targets via configfs
5. Delete and create new netconsole targets after some messages
6. Start a bpftrace in parallel to make surenetpoll_poll_dev()is called
7. If bpftrace exists andnetpoll_poll_dev()was called, stop.
Quoted 8 lines
The test validates a critical netpoll code path by monitoring traffic flow and ensuring
netpoll_poll_dev()is called when the normal TX path is blocked.This addresses a gap in netpoll test coverage for a path that is tricky for the network stack.
Signed-off-by Breno Leitao <[email protected]>
Quoted 15 lines
tools/testing/selftests/drivers/net/netpoll_basic.py new file +15
-0+def test_netpoll(cfg: NetDrvEpEnv) -> None: + """ + Test netpoll by sending traffic to the interface and then sending + netconsole messages to trigger a poll + """ + + target_name = netcons_generate_random_target_name() + ifname = cfg.dev["ifname"] + traffic = None + original_queues = ethtool_read_rx_tx_queue(ifname) + + try: + # Set RX/TX queues to 1 to force congestion + ethtool_set_rx_tx_queue(ifname, 1, 1) +
tools/testing/selftests/drivers/net/netpoll_basic.py line 341
Quoted 10 lines
tools/testing/selftests/drivers/net/netpoll_basic.py new file +10
-0+ traffic = GenerateTraffic(cfg) + do_netpoll_flush_monitored(cfg, ifname, target_name) + finally: + if traffic: + traffic.stop() + + # Revert RX/TX queues + ethtool_set_rx_tx_queue(ifname, original_queues[0], original_queues[1]) + netcons_delete_target(target_name) + bpftrace_stop()
One risk with stateful tests is that the state is not reset if the test exists (or crashes) before reaching the cleanup logic. There are ways around it. Jakub added defer for this purpose, for one.
Paolo Abeni
maintainerRe: [PATCH net-next v4 1/3] selftests: drv-net: add helper/wrapper for bpftrace
In reply to Breno LeitaoQuoted 6 lines · Breno Leitao
From: Jakub Kicinski <[email protected]>
bpftrace is very useful for low level driver testing. perf or trace-cmd would also do for collecting data from tracepoints, but they require much more post-processing.
Quoted 8 lines
Add a wrapper for running bpftrace and sanitizing its output. bpftrace has JSON output, which is great, but it prints loose objects and in a slightly inconvenient format. We have to read the objects line by line, and while at it return them indexed by the map name.
Signed-off-by Jakub Kicinski <[email protected]>Reviewed-by Breno Leitao <[email protected]>Signed-off-by Breno Leitao <[email protected]>
Does not apply cleanly anymore. Please rebase and repost, thanks!
/P
Re: [PATCH net-next v4 1/3] selftests: drv-net: add helper/wrapper for bpftrace
Quoted 12 lines · Paolo Abeni
Quoted 11 lines · Breno Leitao
From: Jakub Kicinski <[email protected]>
bpftrace is very useful for low level driver testing. perf or trace-cmd would also do for collecting data from tracepoints, but they require much more post-processing.
Add a wrapper for running bpftrace and sanitizing its output. bpftrace has JSON output, which is great, but it prints loose objects and in a slightly inconvenient format. We have to read the objects line by line, and while at it return them indexed by the map name.
Quoted 5 lines
Quoted 3 lines
Signed-off-by Jakub Kicinski <[email protected]>Reviewed-by Breno Leitao <[email protected]>Signed-off-by Breno Leitao <[email protected]>Does not apply cleanly anymore. Please rebase and repost, thanks!
ack!
Re: [PATCH net-next v4 3/3] selftests: net: add netpoll basic functionality test
In reply to Willem de BruijnHello Willem,
Quoted 1 line · Willem de Bruijn
Quoted 1 line
+def
test_netpoll(cfg: NetDrvEpEnv)-> None:
<snip>
Quoted 5 lines
Quoted 1 line
+
bpftrace_stop()One risk with stateful tests is that the state is not reset if the test exists (or crashes) before reaching the cleanup logic. There are ways around it. Jakub added defer for this purpose, for one.
Agree. Jakub suggested about "defer" a while ago, but I didn't realize we had a "defer" helper in our test framework. For a second I though Jakub was talking calling the stop later, my bad.
Now that you raised it, I found that we have "defer" as a helper, and we definitely should use it. How about something as simple as:
diff --git a/tools/testing/selftests/drivers/net/netpoll_basic.py b/tools/testing/selftests/drivers/net/netpoll_basic.py index 398ac959151b3..6017b71f154b2 100755 --- a/tools/testing/selftests/drivers/net/netpoll_basic.py +++ b/tools/testing/selftests/drivers/net/netpoll_basic.py @@ -27,6 +27,7 @@ from typing import Optional from lib.py import ( bpftrace, + defer, ip, ethtool, GenerateTraffic, @@ -251,6 +252,7 @@ def do_netpoll_flush_monitored(cfg: NetDrvEpEnv, ifname: str, target_name: str) # Start bpftrace in parallel, so, it is watching # netpoll_poll_dev() while we are sending netconsole messages bpftrace_start() + defer(bpftrace_stop) do_netpoll_flush(cfg, ifname, target_name) @@ -338,7 +340,6 @@ def test_netpoll(cfg: NetDrvEpEnv) -> None: # Revert RX/TX queues ethtool_set_rx_tx_queue(ifname, original_queues[0], original_queues[1]) netcons_delete_target(target_name)
Thanks for th heads-up and review,
--breno