Skip to contentSkip to conversation

selftests: net: exercise page pool reporting via netlink

PATCH Applied Jakub Kicinski 3 participants 9 messages Apr 12, 2024 – Apr 15, 2024

Subsystems (5)
lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

v2 cover selftests: net: exercise page pool reporting via netlink

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/[email protected]/

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(-)
Signature · 1 line
2.44.0
author maintainer
#1 lore raw

PATCH v2 1/6 net: netdevsim: add some fake page pool use

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.

---
 drivers/net/netdevsim/netdev.c    | 93 +++++++++++++++++++++++++++++++
 drivers/net/netdevsim/netdevsim.h |  4 ++
 2 files changed, 97 insertions(+)
drivers/net/netdevsim/netdev.c +93 -0
index d7ba447db17c..d127856f8f36 100644
@@ -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);
 }
 
drivers/net/netdevsim/netdevsim.h +4 -0
index 553c4b9b4f63..7664ab823e29 100644
@@ -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;
 };
Signature · 1 line
2.44.0
author maintainer
#2 lore raw

PATCH v2 2/6 tools: ynl: don't return None for dumps

In reply to Jakub Kicinski

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
 []

---
CC: [email protected]

---
 tools/net/ynl/lib/ynl.py | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/net/ynl/lib/ynl.py +3 -1
index 0ba5f6fb8747..a67f7b6fef92 100644
@@ -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
 
Signature · 1 line
2.44.0
author maintainer
#3 lore raw

PATCH v2 3/6 selftests: net: print report check location in python tests

In reply to Jakub Kicinski

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

---
v2: - pass *args from fail to pr

---
 tools/testing/selftests/net/lib/py/ksft.py | 25 ++++++++++++----------
 1 file changed, 14 insertions(+), 11 deletions(-)
tools/testing/selftests/net/lib/py/ksft.py +14 -11
index c7210525981c..b4b0bfff68b0 100644
@@ -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=""):
Signature · 1 line
2.44.0
author maintainer
#4 lore raw

PATCH v2 4/6 selftests: net: print full exception on failure

In reply to Jakub Kicinski

Instead of a summary line print the full exception. This makes debugging Python tests much easier.

---
 tools/testing/selftests/net/lib/py/ksft.py | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/testing/selftests/net/lib/py/ksft.py +3 -1
index b4b0bfff68b0..793e4761645e 100644
@@ -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
Signature · 1 line
2.44.0
author maintainer
#5 lore raw

PATCH v2 5/6 selftests: net: support use of NetdevSimDev under "with" in python

In reply to Jakub Kicinski

Using "with" on an entire driver test env is supported already, but it's also useful to use "with" on an individual nsim.

---
 tools/testing/selftests/net/lib/py/nsim.py | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
tools/testing/selftests/net/lib/py/nsim.py +14 -1
index 97457aca7e08..94aa32f59fdb 100644
@@ -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)
Signature · 1 line
2.44.0
author maintainer
#6 lore raw

PATCH v2 6/6 selftests: net: exercise page pool reporting via netlink

In reply to Jakub Kicinski

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

---
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(-)
tools/testing/selftests/net/lib/py/ksft.py +12 -0
index 793e4761645e..3769b9197213 100644
@@ -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:
tools/testing/selftests/net/lib/py/nsim.py +1 -0
index 94aa32f59fdb..06896cdf7c18 100644
@@ -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)
tools/testing/selftests/net/nl_netdev.py +74 -2

90 lines not on this page · raw message

Signature · 1 line
2.44.0
#8 lore raw
In reply to Jakub Kicinski
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 8 lines · 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/[email protected]/

[...]

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!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html