Add a basic test for page pool netlink reporting.
v2: - pass args as *args (patch 3) - improve the test and add busy wait helper (patch 6) v1: https://lore.kernel.org/all/20240411012815.174400-1-kuba@kernel.org/
Jakub Kicinski (6): net: netdevsim: add some fake page pool use tools: ynl: don't return None for dumps selftests: net: print report check location in python tests selftests: net: print full exception on failure selftests: net: support use of NetdevSimDev under "with" in python selftests: net: exercise page pool reporting via netlink
drivers/net/netdevsim/netdev.c | 93 ++++++++++++++++++++++ drivers/net/netdevsim/netdevsim.h | 4 + tools/net/ynl/lib/ynl.py | 4 +- tools/testing/selftests/net/lib/py/ksft.py | 41 +++++++--- tools/testing/selftests/net/lib/py/nsim.py | 16 +++- tools/testing/selftests/net/nl_netdev.py | 76 +++++++++++++++++- 6 files changed, 218 insertions(+), 16 deletions(-)
Add very basic page pool use so that we can exercise the netlink uAPI in a selftest.
Page pool gets created on open, destroyed on close. But we control allocating of a single page thru debugfs. This page may survive past the page pool itself so that we can test orphaned page pools.
Signed-off-by: Jakub Kicinski kuba@kernel.org --- drivers/net/netdevsim/netdev.c | 93 +++++++++++++++++++++++++++++++ drivers/net/netdevsim/netdevsim.h | 4 ++ 2 files changed, 97 insertions(+)
diff --git a/drivers/net/netdevsim/netdev.c b/drivers/net/netdevsim/netdev.c index d7ba447db17c..d127856f8f36 100644 --- a/drivers/net/netdevsim/netdev.c +++ b/drivers/net/netdevsim/netdev.c @@ -20,6 +20,7 @@ #include <linux/netdevice.h> #include <linux/slab.h> #include <net/netdev_queues.h> +#include <net/page_pool/helpers.h> #include <net/netlink.h> #include <net/pkt_cls.h> #include <net/rtnetlink.h> @@ -299,6 +300,29 @@ static int nsim_get_iflink(const struct net_device *dev) return iflink; }
+static int nsim_open(struct net_device *dev) +{ + struct netdevsim *ns = netdev_priv(dev); + struct page_pool_params pp = { 0 }; + + pp.pool_size = 128; + pp.dev = &dev->dev; + pp.dma_dir = DMA_BIDIRECTIONAL; + pp.netdev = dev; + + ns->pp = page_pool_create(&pp); + return PTR_ERR_OR_ZERO(ns->pp); +} + +static int nsim_stop(struct net_device *dev) +{ + struct netdevsim *ns = netdev_priv(dev); + + page_pool_destroy(ns->pp); + + return 0; +} + static const struct net_device_ops nsim_netdev_ops = { .ndo_start_xmit = nsim_start_xmit, .ndo_set_rx_mode = nsim_set_rx_mode, @@ -318,6 +342,8 @@ static const struct net_device_ops nsim_netdev_ops = { .ndo_set_features = nsim_set_features, .ndo_get_iflink = nsim_get_iflink, .ndo_bpf = nsim_bpf, + .ndo_open = nsim_open, + .ndo_stop = nsim_stop, };
static const struct net_device_ops nsim_vf_netdev_ops = { @@ -378,6 +404,60 @@ static const struct netdev_stat_ops nsim_stat_ops = { .get_base_stats = nsim_get_base_stats, };
+static ssize_t +nsim_pp_hold_read(struct file *file, char __user *data, + size_t count, loff_t *ppos) +{ + struct netdevsim *ns = file->private_data; + char buf[3] = "n\n"; + + if (ns->page) + buf[0] = 'y'; + + return simple_read_from_buffer(data, count, ppos, buf, 2); +} + +static ssize_t +nsim_pp_hold_write(struct file *file, const char __user *data, + size_t count, loff_t *ppos) +{ + struct netdevsim *ns = file->private_data; + ssize_t ret; + bool val; + + ret = kstrtobool_from_user(data, count, &val); + if (ret) + return ret; + + rtnl_lock(); + ret = count; + if (val == !!ns->page) + goto exit; + + if (!netif_running(ns->netdev) && val) { + ret = -ENETDOWN; + } else if (val) { + ns->page = page_pool_dev_alloc_pages(ns->pp); + if (!ns->page) + ret = -ENOMEM; + } else { + page_pool_put_full_page(ns->page->pp, ns->page, false); + ns->page = NULL; + } + rtnl_unlock(); + +exit: + return count; +} + +static const struct file_operations nsim_pp_hold_fops = { + .open = simple_open, + .read = nsim_pp_hold_read, + .write = nsim_pp_hold_write, + .llseek = generic_file_llseek, + .owner = THIS_MODULE, +}; + static void nsim_setup(struct net_device *dev) { ether_setup(dev); @@ -485,6 +565,10 @@ nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port) err = nsim_init_netdevsim_vf(ns); if (err) goto err_free_netdev; + + ns->pp_dfs = debugfs_create_file("pp_hold", 0600, nsim_dev_port->ddir, + ns, &nsim_pp_hold_fops); + return ns;
err_free_netdev: @@ -497,6 +581,8 @@ void nsim_destroy(struct netdevsim *ns) struct net_device *dev = ns->netdev; struct netdevsim *peer;
+ debugfs_remove(ns->pp_dfs); + rtnl_lock(); peer = rtnl_dereference(ns->peer); if (peer) @@ -511,6 +597,13 @@ void nsim_destroy(struct netdevsim *ns) rtnl_unlock(); if (nsim_dev_port_is_pf(ns->nsim_dev_port)) nsim_exit_netdevsim(ns); + + /* Put this intentionally late to exercise the orphaning path */ + if (ns->page) { + page_pool_put_full_page(ns->page->pp, ns->page, false); + ns->page = NULL; + } + free_netdev(dev); }
diff --git a/drivers/net/netdevsim/netdevsim.h b/drivers/net/netdevsim/netdevsim.h index 553c4b9b4f63..7664ab823e29 100644 --- a/drivers/net/netdevsim/netdevsim.h +++ b/drivers/net/netdevsim/netdevsim.h @@ -125,6 +125,10 @@ struct netdevsim { struct debugfs_u32_array dfs_ports[2]; } udp_ports;
+ struct page_pool *pp; + struct page *page; + struct dentry *pp_dfs; + struct nsim_ethtool ethtool; struct netdevsim __rcu *peer; };
YNL currently reports None for empty dump:
$ cli.py ...netdev.yaml --dump page-pool-get None
This doesn't matter for the CLI but when writing YNL based tests having to deal with either list or None is annoying. Limit the None conversion to non-dump ops:
$ cli.py ...netdev.yaml --dump page-pool-get []
Reviewed-by: Donald Hunter donald.hunter@gmail.com Signed-off-by: Jakub Kicinski kuba@kernel.org --- CC: jiri@resnulli.us --- tools/net/ynl/lib/ynl.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/tools/net/ynl/lib/ynl.py b/tools/net/ynl/lib/ynl.py index 0ba5f6fb8747..a67f7b6fef92 100644 --- a/tools/net/ynl/lib/ynl.py +++ b/tools/net/ynl/lib/ynl.py @@ -995,9 +995,11 @@ genl_family_name_to_id = None rsp_msg.update(self._decode_struct(decoded.raw, op.fixed_header)) rsp.append(rsp_msg)
+ if dump: + return rsp if not rsp: return None - if not dump and len(rsp) == 1: + if len(rsp) == 1: return rsp[0] return rsp
Developing Python tests is a bit annoying because when test fails we only print the fail message and no info about which exact check led to it. Print the location (the first line of this example is new):
# At /root/ksft-net-drv/./net/nl_netdev.py line 38: # Check failed 0 != 10 not ok 3 nl_netdev.page_pool_check
Reviewed-by: Petr Machata petrm@nvidia.com Signed-off-by: Jakub Kicinski kuba@kernel.org --- v2: - pass *args from fail to pr --- tools/testing/selftests/net/lib/py/ksft.py | 25 ++++++++++++---------- 1 file changed, 14 insertions(+), 11 deletions(-)
diff --git a/tools/testing/selftests/net/lib/py/ksft.py b/tools/testing/selftests/net/lib/py/ksft.py index c7210525981c..b4b0bfff68b0 100644 --- a/tools/testing/selftests/net/lib/py/ksft.py +++ b/tools/testing/selftests/net/lib/py/ksft.py @@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0
import builtins +import inspect from .consts import KSFT_MAIN_NAME
KSFT_RESULT = None @@ -18,32 +19,34 @@ KSFT_RESULT = None print("#", *objs, **kwargs)
+def _fail(*args): + global KSFT_RESULT + KSFT_RESULT = False + + frame = inspect.stack()[2] + ksft_pr("At " + frame.filename + " line " + str(frame.lineno) + ":") + ksft_pr(*args) + + def ksft_eq(a, b, comment=""): global KSFT_RESULT if a != b: - KSFT_RESULT = False - ksft_pr("Check failed", a, "!=", b, comment) + _fail("Check failed", a, "!=", b, comment)
def ksft_true(a, comment=""): - global KSFT_RESULT if not a: - KSFT_RESULT = False - ksft_pr("Check failed", a, "does not eval to True", comment) + _fail("Check failed", a, "does not eval to True", comment)
def ksft_in(a, b, comment=""): - global KSFT_RESULT if a not in b: - KSFT_RESULT = False - ksft_pr("Check failed", a, "not in", b, comment) + _fail("Check failed", a, "not in", b, comment)
def ksft_ge(a, b, comment=""): - global KSFT_RESULT if a < b: - KSFT_RESULT = False - ksft_pr("Check failed", a, "<", b, comment) + _fail("Check failed", a, "<", b, comment)
def ktap_result(ok, cnt=1, case="", comment=""):
Instead of a summary line print the full exception. This makes debugging Python tests much easier.
Reviewed-by: Petr Machata petrm@nvidia.com Signed-off-by: Jakub Kicinski kuba@kernel.org --- tools/testing/selftests/net/lib/py/ksft.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/net/lib/py/ksft.py b/tools/testing/selftests/net/lib/py/ksft.py index b4b0bfff68b0..793e4761645e 100644 --- a/tools/testing/selftests/net/lib/py/ksft.py +++ b/tools/testing/selftests/net/lib/py/ksft.py @@ -2,6 +2,7 @@
import builtins import inspect +import traceback from .consts import KSFT_MAIN_NAME
KSFT_RESULT = None @@ -85,7 +86,8 @@ KSFT_RESULT = None totals['xfail'] += 1 continue except Exception as e: - for line in str(e).split('\n'): + tb = traceback.format_exc() + for line in tb.strip().split('\n'): ksft_pr("Exception|", line) ktap_result(False, cnt, case) totals['fail'] += 1
Using "with" on an entire driver test env is supported already, but it's also useful to use "with" on an individual nsim.
Reviewed-by: Petr Machata petrm@nvidia.com Signed-off-by: Jakub Kicinski kuba@kernel.org --- tools/testing/selftests/net/lib/py/nsim.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/net/lib/py/nsim.py b/tools/testing/selftests/net/lib/py/nsim.py index 97457aca7e08..94aa32f59fdb 100644 --- a/tools/testing/selftests/net/lib/py/nsim.py +++ b/tools/testing/selftests/net/lib/py/nsim.py @@ -84,6 +84,17 @@ from .utils import cmd, ip for port_index in range(port_count): self.nsims.append(self._make_port(port_index, ifnames[port_index]))
+ self.removed = False + + def __enter__(self): + return self + + def __exit__(self, ex_type, ex_value, ex_tb): + """ + __exit__ gets called at the end of a "with" block. + """ + self.remove() + def _make_port(self, port_index, ifname): return NetdevSim(self, port_index, ifname, self.ns)
@@ -112,7 +123,9 @@ from .utils import cmd, ip raise Exception("netdevices did not appear within timeout")
def remove(self): - self.ctrl_write("del_device", "%u" % (self.addr, )) + if not self.removed: + self.ctrl_write("del_device", "%u" % (self.addr, )) + self.removed = True
def remove_nsim(self, nsim): self.nsims.remove(nsim)
Add a Python test for the basic ops.
# ./net/nl_netdev.py KTAP version 1 1..3 ok 1 nl_netdev.empty_check ok 2 nl_netdev.lo_check ok 3 nl_netdev.page_pool_check # Totals: pass:3 fail:0 xfail:0 xpass:0 skip:0 error:0
Signed-off-by: Jakub Kicinski kuba@kernel.org --- v2: - move up / down to the test - add helper for getting pp - add busy_wait helper - rename undeteched --- tools/testing/selftests/net/lib/py/ksft.py | 12 ++++ tools/testing/selftests/net/lib/py/nsim.py | 1 + tools/testing/selftests/net/nl_netdev.py | 76 +++++++++++++++++++++- 3 files changed, 87 insertions(+), 2 deletions(-)
diff --git a/tools/testing/selftests/net/lib/py/ksft.py b/tools/testing/selftests/net/lib/py/ksft.py index 793e4761645e..3769b9197213 100644 --- a/tools/testing/selftests/net/lib/py/ksft.py +++ b/tools/testing/selftests/net/lib/py/ksft.py @@ -2,6 +2,7 @@
import builtins import inspect +import time import traceback from .consts import KSFT_MAIN_NAME
@@ -50,6 +51,17 @@ KSFT_RESULT = None _fail("Check failed", a, "<", b, comment)
+def ksft_busy_wait(cond, sleep=0.005, deadline=1, comment=""): + end = time.monotonic() + deadline + while True: + if cond(): + return + if time.monotonic() > end: + _fail("Waiting for condition timed out", comment) + return + time.sleep(sleep) + + def ktap_result(ok, cnt=1, case="", comment=""): res = "" if not ok: diff --git a/tools/testing/selftests/net/lib/py/nsim.py b/tools/testing/selftests/net/lib/py/nsim.py index 94aa32f59fdb..06896cdf7c18 100644 --- a/tools/testing/selftests/net/lib/py/nsim.py +++ b/tools/testing/selftests/net/lib/py/nsim.py @@ -28,6 +28,7 @@ from .utils import cmd, ip self.dfs_dir = "%s/ports/%u/" % (nsimdev.dfs_dir, port_index) ret = ip("-j link show dev %s" % ifname, ns=ns) self.dev = json.loads(ret.stdout)[0] + self.ifindex = self.dev["ifindex"]
def dfs_write(self, path, val): self.nsimdev.dfs_write(f'ports/{self.port_index}/' + path, val) diff --git a/tools/testing/selftests/net/nl_netdev.py b/tools/testing/selftests/net/nl_netdev.py index 2b8b488fb419..6909b1760739 100755 --- a/tools/testing/selftests/net/nl_netdev.py +++ b/tools/testing/selftests/net/nl_netdev.py @@ -1,7 +1,9 @@ #!/usr/bin/env python3 # SPDX-License-Identifier: GPL-2.0
-from lib.py import ksft_run, ksft_pr, ksft_eq, ksft_ge, NetdevFamily +import time +from lib.py import ksft_run, ksft_pr, ksft_eq, ksft_ge, ksft_busy_wait +from lib.py import NetdevFamily, NetdevSimDev, ip
def empty_check(nf) -> None: @@ -15,9 +17,79 @@ from lib.py import ksft_run, ksft_pr, ksft_eq, ksft_ge, NetdevFamily ksft_eq(len(lo_info['xdp-rx-metadata-features']), 0)
+def page_pool_check(nf) -> None: + with NetdevSimDev() as nsimdev: + nsim = nsimdev.nsims[0] + + def up(): + ip(f"link set dev {nsim.ifname} up") + + def down(): + ip(f"link set dev {nsim.ifname} down") + + def get_pp(): + pp_list = nf.page_pool_get({}, dump=True) + return [pp for pp in pp_list if pp.get("ifindex") == nsim.ifindex] + + # No page pools when down + down() + ksft_eq(len(get_pp()), 0) + + # Up, empty page pool appears + up() + pp_list = get_pp() + ksft_ge(len(pp_list), 0) + refs = sum([pp["inflight"] for pp in pp_list]) + ksft_eq(refs, 0) + + # Down, it disappears, again + down() + pp_list = get_pp() + ksft_eq(len(pp_list), 0) + + # Up, allocate a page + up() + nsim.dfs_write("pp_hold", "y") + pp_list = nf.page_pool_get({}, dump=True) + refs = sum([pp["inflight"] for pp in pp_list if pp.get("ifindex") == nsim.ifindex]) + ksft_ge(refs, 1) + + # Now let's leak a page + down() + pp_list = get_pp() + ksft_eq(len(pp_list), 1) + refs = sum([pp["inflight"] for pp in pp_list]) + ksft_eq(refs, 1) + attached = [pp for pp in pp_list if "detach-time" not in pp] + ksft_eq(len(attached), 0) + + # New pp can get created, and we'll have two + up() + pp_list = get_pp() + attached = [pp for pp in pp_list if "detach-time" not in pp] + detached = [pp for pp in pp_list if "detach-time" in pp] + ksft_eq(len(attached), 1) + ksft_eq(len(detached), 1) + + # Free the old page and the old pp is gone + nsim.dfs_write("pp_hold", "n") + # Freeing check is once a second so we may need to retry + ksft_busy_wait(lambda: len(get_pp()) == 1, deadline=2) + + # And down... + down() + ksft_eq(len(get_pp()), 0) + + # Last, leave the page hanging for destroy, nothing to check + # we're trying to exercise the orphaning path in the kernel + up() + nsim.dfs_write("pp_hold", "y") + + def main() -> None: nf = NetdevFamily() - ksft_run([empty_check, lo_check], args=(nf, )) + ksft_run([empty_check, lo_check, page_pool_check], + args=(nf, ))
if __name__ == "__main__":
Jakub Kicinski kuba@kernel.org writes:
Add a Python test for the basic ops.
# ./net/nl_netdev.py KTAP version 1 1..3 ok 1 nl_netdev.empty_check ok 2 nl_netdev.lo_check ok 3 nl_netdev.page_pool_check # Totals: pass:3 fail:0 xfail:0 xpass:0 skip:0 error:0
Signed-off-by: Jakub Kicinski kuba@kernel.org
LGTM
Reviewed-by: Petr Machata petrm@nvidia.com
Hello:
This series was applied to netdev/net-next.git (main) by Jakub Kicinski kuba@kernel.org:
On Fri, 12 Apr 2024 07:14:30 -0700 you wrote:
Add a basic test for page pool netlink reporting.
v2:
- pass args as *args (patch 3)
- improve the test and add busy wait helper (patch 6)
v1: https://lore.kernel.org/all/20240411012815.174400-1-kuba@kernel.org/
[...]
Here is the summary with links: - [net-next,v2,1/6] net: netdevsim: add some fake page pool use https://git.kernel.org/netdev/net-next/c/1580cbcbfe77 - [net-next,v2,2/6] tools: ynl: don't return None for dumps https://git.kernel.org/netdev/net-next/c/72ba6cba0a6e - [net-next,v2,3/6] selftests: net: print report check location in python tests https://git.kernel.org/netdev/net-next/c/eeb409bde964 - [net-next,v2,4/6] selftests: net: print full exception on failure https://git.kernel.org/netdev/net-next/c/99583b970b90 - [net-next,v2,5/6] selftests: net: support use of NetdevSimDev under "with" in python https://git.kernel.org/netdev/net-next/c/8554d6e39b6a - [net-next,v2,6/6] selftests: net: exercise page pool reporting via netlink https://git.kernel.org/netdev/net-next/c/05fa5c31b988
You are awesome, thank you!
linux-kselftest-mirror@lists.linaro.org