Commit bb72159c authored by Jakub Kicinski's avatar Jakub Kicinski

Merge branch 'selftests-net-exercise-page-pool-reporting-via-netlink'

Jakub Kicinski says:

====================
selftests: net: exercise page pool reporting via netlink

Add a basic test for page pool netlink reporting.

v1: https://lore.kernel.org/all/20240411012815.174400-1-kuba@kernel.org/
====================

Link: https://lore.kernel.org/r/20240412141436.828666-1-kuba@kernel.orgSigned-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parents 77c842ca 05fa5c31
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include <linux/netdevice.h> #include <linux/netdevice.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <net/netdev_queues.h> #include <net/netdev_queues.h>
#include <net/page_pool/helpers.h>
#include <net/netlink.h> #include <net/netlink.h>
#include <net/pkt_cls.h> #include <net/pkt_cls.h>
#include <net/rtnetlink.h> #include <net/rtnetlink.h>
...@@ -299,6 +300,29 @@ static int nsim_get_iflink(const struct net_device *dev) ...@@ -299,6 +300,29 @@ static int nsim_get_iflink(const struct net_device *dev)
return iflink; 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 = { static const struct net_device_ops nsim_netdev_ops = {
.ndo_start_xmit = nsim_start_xmit, .ndo_start_xmit = nsim_start_xmit,
.ndo_set_rx_mode = nsim_set_rx_mode, .ndo_set_rx_mode = nsim_set_rx_mode,
...@@ -318,6 +342,8 @@ static const struct net_device_ops nsim_netdev_ops = { ...@@ -318,6 +342,8 @@ static const struct net_device_ops nsim_netdev_ops = {
.ndo_set_features = nsim_set_features, .ndo_set_features = nsim_set_features,
.ndo_get_iflink = nsim_get_iflink, .ndo_get_iflink = nsim_get_iflink,
.ndo_bpf = nsim_bpf, .ndo_bpf = nsim_bpf,
.ndo_open = nsim_open,
.ndo_stop = nsim_stop,
}; };
static const struct net_device_ops nsim_vf_netdev_ops = { static const struct net_device_ops nsim_vf_netdev_ops = {
...@@ -378,6 +404,60 @@ static const struct netdev_stat_ops nsim_stat_ops = { ...@@ -378,6 +404,60 @@ static const struct netdev_stat_ops nsim_stat_ops = {
.get_base_stats = nsim_get_base_stats, .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) static void nsim_setup(struct net_device *dev)
{ {
ether_setup(dev); ether_setup(dev);
...@@ -485,6 +565,10 @@ nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port) ...@@ -485,6 +565,10 @@ nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port)
err = nsim_init_netdevsim_vf(ns); err = nsim_init_netdevsim_vf(ns);
if (err) if (err)
goto err_free_netdev; goto err_free_netdev;
ns->pp_dfs = debugfs_create_file("pp_hold", 0600, nsim_dev_port->ddir,
ns, &nsim_pp_hold_fops);
return ns; return ns;
err_free_netdev: err_free_netdev:
...@@ -497,6 +581,8 @@ void nsim_destroy(struct netdevsim *ns) ...@@ -497,6 +581,8 @@ void nsim_destroy(struct netdevsim *ns)
struct net_device *dev = ns->netdev; struct net_device *dev = ns->netdev;
struct netdevsim *peer; struct netdevsim *peer;
debugfs_remove(ns->pp_dfs);
rtnl_lock(); rtnl_lock();
peer = rtnl_dereference(ns->peer); peer = rtnl_dereference(ns->peer);
if (peer) if (peer)
...@@ -511,6 +597,13 @@ void nsim_destroy(struct netdevsim *ns) ...@@ -511,6 +597,13 @@ void nsim_destroy(struct netdevsim *ns)
rtnl_unlock(); rtnl_unlock();
if (nsim_dev_port_is_pf(ns->nsim_dev_port)) if (nsim_dev_port_is_pf(ns->nsim_dev_port))
nsim_exit_netdevsim(ns); 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); free_netdev(dev);
} }
......
...@@ -125,6 +125,10 @@ struct netdevsim { ...@@ -125,6 +125,10 @@ struct netdevsim {
struct debugfs_u32_array dfs_ports[2]; struct debugfs_u32_array dfs_ports[2];
} udp_ports; } udp_ports;
struct page_pool *pp;
struct page *page;
struct dentry *pp_dfs;
struct nsim_ethtool ethtool; struct nsim_ethtool ethtool;
struct netdevsim __rcu *peer; struct netdevsim __rcu *peer;
}; };
......
...@@ -995,9 +995,11 @@ class YnlFamily(SpecFamily): ...@@ -995,9 +995,11 @@ class YnlFamily(SpecFamily):
rsp_msg.update(self._decode_struct(decoded.raw, op.fixed_header)) rsp_msg.update(self._decode_struct(decoded.raw, op.fixed_header))
rsp.append(rsp_msg) rsp.append(rsp_msg)
if dump:
return rsp
if not rsp: if not rsp:
return None return None
if not dump and len(rsp) == 1: if len(rsp) == 1:
return rsp[0] return rsp[0]
return rsp return rsp
......
# SPDX-License-Identifier: GPL-2.0 # SPDX-License-Identifier: GPL-2.0
import builtins import builtins
import inspect
import time
import traceback
from .consts import KSFT_MAIN_NAME from .consts import KSFT_MAIN_NAME
KSFT_RESULT = None KSFT_RESULT = None
...@@ -18,32 +21,45 @@ def ksft_pr(*objs, **kwargs): ...@@ -18,32 +21,45 @@ def ksft_pr(*objs, **kwargs):
print("#", *objs, **kwargs) 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=""): def ksft_eq(a, b, comment=""):
global KSFT_RESULT global KSFT_RESULT
if a != b: if a != b:
KSFT_RESULT = False _fail("Check failed", a, "!=", b, comment)
ksft_pr("Check failed", a, "!=", b, comment)
def ksft_true(a, comment=""): def ksft_true(a, comment=""):
global KSFT_RESULT
if not a: if not a:
KSFT_RESULT = False _fail("Check failed", a, "does not eval to True", comment)
ksft_pr("Check failed", a, "does not eval to True", comment)
def ksft_in(a, b, comment=""): def ksft_in(a, b, comment=""):
global KSFT_RESULT
if a not in b: if a not in b:
KSFT_RESULT = False _fail("Check failed", a, "not in", b, comment)
ksft_pr("Check failed", a, "not in", b, comment)
def ksft_ge(a, b, comment=""): def ksft_ge(a, b, comment=""):
global KSFT_RESULT
if a < b: if a < b:
KSFT_RESULT = False _fail("Check failed", a, "<", b, comment)
ksft_pr("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=""): def ktap_result(ok, cnt=1, case="", comment=""):
...@@ -82,7 +98,8 @@ def ksft_run(cases, args=()): ...@@ -82,7 +98,8 @@ def ksft_run(cases, args=()):
totals['xfail'] += 1 totals['xfail'] += 1
continue continue
except Exception as e: 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) ksft_pr("Exception|", line)
ktap_result(False, cnt, case) ktap_result(False, cnt, case)
totals['fail'] += 1 totals['fail'] += 1
......
...@@ -28,6 +28,7 @@ class NetdevSim: ...@@ -28,6 +28,7 @@ class NetdevSim:
self.dfs_dir = "%s/ports/%u/" % (nsimdev.dfs_dir, port_index) self.dfs_dir = "%s/ports/%u/" % (nsimdev.dfs_dir, port_index)
ret = ip("-j link show dev %s" % ifname, ns=ns) ret = ip("-j link show dev %s" % ifname, ns=ns)
self.dev = json.loads(ret.stdout)[0] self.dev = json.loads(ret.stdout)[0]
self.ifindex = self.dev["ifindex"]
def dfs_write(self, path, val): def dfs_write(self, path, val):
self.nsimdev.dfs_write(f'ports/{self.port_index}/' + path, val) self.nsimdev.dfs_write(f'ports/{self.port_index}/' + path, val)
...@@ -84,6 +85,17 @@ class NetdevSimDev: ...@@ -84,6 +85,17 @@ class NetdevSimDev:
for port_index in range(port_count): for port_index in range(port_count):
self.nsims.append(self._make_port(port_index, ifnames[port_index])) 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): def _make_port(self, port_index, ifname):
return NetdevSim(self, port_index, ifname, self.ns) return NetdevSim(self, port_index, ifname, self.ns)
...@@ -112,7 +124,9 @@ class NetdevSimDev: ...@@ -112,7 +124,9 @@ class NetdevSimDev:
raise Exception("netdevices did not appear within timeout") raise Exception("netdevices did not appear within timeout")
def remove(self): def remove(self):
if not self.removed:
self.ctrl_write("del_device", "%u" % (self.addr, )) self.ctrl_write("del_device", "%u" % (self.addr, ))
self.removed = True
def remove_nsim(self, nsim): def remove_nsim(self, nsim):
self.nsims.remove(nsim) self.nsims.remove(nsim)
......
#!/usr/bin/env python3 #!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0 # 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: def empty_check(nf) -> None:
...@@ -15,9 +17,79 @@ def lo_check(nf) -> None: ...@@ -15,9 +17,79 @@ def lo_check(nf) -> None:
ksft_eq(len(lo_info['xdp-rx-metadata-features']), 0) 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: def main() -> None:
nf = NetdevFamily() nf = NetdevFamily()
ksft_run([empty_check, lo_check], args=(nf, )) ksft_run([empty_check, lo_check, page_pool_check],
args=(nf, ))
if __name__ == "__main__": if __name__ == "__main__":
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment