diff mbox series

[net-next,6/6] selftests: net: exercise page pool reporting via netlink

Message ID 20240411012815.174400-7-kuba@kernel.org (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series selftests: net: exercise page pool reporting via netlink | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next, async
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 8 this patch: 8
netdev/build_tools success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers success CCed 6 of 6 maintainers
netdev/build_clang success Errors and warnings before: 8 this patch: 8
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 8 this patch: 8
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 106 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-04-12--06-00 (tests: 962)

Commit Message

Jakub Kicinski April 11, 2024, 1:28 a.m. UTC
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>
---
 tools/testing/selftests/net/lib/py/nsim.py |  7 ++
 tools/testing/selftests/net/nl_netdev.py   | 79 +++++++++++++++++++++-
 2 files changed, 84 insertions(+), 2 deletions(-)

Comments

Petr Machata April 12, 2024, 8:20 a.m. UTC | #1
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>
> ---
>  tools/testing/selftests/net/lib/py/nsim.py |  7 ++
>  tools/testing/selftests/net/nl_netdev.py   | 79 +++++++++++++++++++++-
>  2 files changed, 84 insertions(+), 2 deletions(-)
>
> diff --git a/tools/testing/selftests/net/lib/py/nsim.py b/tools/testing/selftests/net/lib/py/nsim.py
> index 94aa32f59fdb..1fd50a308408 100644
> --- a/tools/testing/selftests/net/lib/py/nsim.py
> +++ b/tools/testing/selftests/net/lib/py/nsim.py
> @@ -28,6 +28,13 @@ 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 up(self):
> +        ip("link set dev {} up".format(self.ifname))
> +
> +    def down(self):
> +        ip("link set dev {} down".format(self.ifname))

Yeah, what I meant by integration pain with LNST the other day was
basically this. You end up rewriting the iproute2 API in Python.
But it is what it is.

>      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..afc510c044ce 100755
> --- a/tools/testing/selftests/net/nl_netdev.py
> +++ b/tools/testing/selftests/net/nl_netdev.py
> @@ -1,7 +1,8 @@
>  #!/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, NetdevFamily, NetdevSimDev, ip
>  
>  
>  def empty_check(nf) -> None:
> @@ -15,9 +16,83 @@ 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]
> +
> +        # No page pools when down
> +        nsim.down()
> +        pp_list = nf.page_pool_get({}, dump=True)
> +        pp_list = [pp for pp in pp_list if pp.get("ifindex") == nsim.ifindex]

This combo of page_pool_get / filter looks like it should be in a helper.

> +        ksft_eq(len(pp_list), 0)
> +
> +        # Up, empty page pool appears
> +        nsim.up()
> +        pp_list = nf.page_pool_get({}, dump=True)
> +        pp_list = [pp for pp in pp_list if pp.get("ifindex") == nsim.ifindex]
> +        ksft_ge(len(pp_list), 0)
> +        refs = sum([pp["inflight"] for pp in pp_list])
> +        ksft_eq(refs, 0)
> +
> +        # Down, it disappears, again
> +        nsim.down()
> +        pp_list = nf.page_pool_get({}, dump=True)
> +        pp_list = [pp for pp in pp_list if pp.get("ifindex") == nsim.ifindex]
> +        ksft_eq(len(pp_list), 0)
> +
> +        # Up, allocate a page
> +        nsim.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
> +        nsim.down()
> +        pp_list = nf.page_pool_get({}, dump=True)
> +        pp_list = [pp for pp in pp_list if pp.get("ifindex") == nsim.ifindex]
> +        ksft_eq(len(pp_list), 1)
> +        refs = sum([pp["inflight"] for pp in pp_list if pp.get("ifindex") == nsim.ifindex])

The second filtering is unnecessary.

> +        ksft_eq(refs, 1)
> +        undetached = [pp for pp in pp_list if "detach-time" not in pp]

Maybe call this attached to be in sync with the next hunk?

> +        ksft_eq(len(undetached), 0)
> +
> +        # New pp can get created, and we'll have two
> +        nsim.up()
> +        pp_list = nf.page_pool_get({}, dump=True)
> +        pp_list = [pp for pp in pp_list if pp.get("ifindex") == nsim.ifindex]
> +        attached = [pp for pp in pp_list if "detach-time" not in pp]
> +        undetached = [pp for pp in pp_list if "detach-time" in pp]

detached.

> +        ksft_eq(len(attached), 1)
> +        ksft_eq(len(undetached), 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
> +        for i in range(50):
> +            pp_list = nf.page_pool_get({}, dump=True)
> +            pp_list = [pp for pp in pp_list if pp.get("ifindex") == nsim.ifindex]
> +            if len(pp_list) == 1:
> +                break
> +            time.sleep(0.05)
> +        ksft_eq(len(pp_list), 1)

Yeah, I don't know if busywait / slowwait sort of a helper is practical
to write in Python. No idea how to go about it. But the bash experience
shows it would useful fairly often.

> +        # And down...
> +        nsim.down()
> +        pp_list = nf.page_pool_get({}, dump=True)
> +        pp_list = [pp for pp in pp_list if pp.get("ifindex") == nsim.ifindex]
> +        ksft_eq(len(pp_list), 0)
> +
> +        # Last, leave the page hanging for destroy, nothing to check
> +        # we're trying to exercise the orphaning path in the kernel
> +        nsim.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 April 12, 2024, 1:48 p.m. UTC | #2
On Fri, 12 Apr 2024 10:20:47 +0200 Petr Machata wrote:
> > +    def up(self):
> > +        ip("link set dev {} up".format(self.ifname))
> > +
> > +    def down(self):
> > +        ip("link set dev {} down".format(self.ifname))  
> 
> Yeah, what I meant by integration pain with LNST the other day was
> basically this. You end up rewriting the iproute2 API in Python.
> But it is what it is.

Yes, I wasn't sure where to make it a local helper in the test or 
a method of the class. I also don't think we should wrap too much but
I was worried someone will give me feedback in the opposite direction :)

Let me make it a local helper in the test.

I should probably move to format strings, too, not sure now what made 
me use .format().

> > +        ksft_eq(len(attached), 1)
> > +        ksft_eq(len(undetached), 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
> > +        for i in range(50):
> > +            pp_list = nf.page_pool_get({}, dump=True)
> > +            pp_list = [pp for pp in pp_list if pp.get("ifindex") == nsim.ifindex]
> > +            if len(pp_list) == 1:
> > +                break
> > +            time.sleep(0.05)
> > +        ksft_eq(len(pp_list), 1)  
> 
> Yeah, I don't know if busywait / slowwait sort of a helper is practical
> to write in Python. No idea how to go about it. But the bash experience
> shows it would useful fairly often.

If I wrap:

            pp_list = nf.page_pool_get({}, dump=True)
            pp_list = [pp for pp in pp_list if pp.get("ifindex") == nsim.ifindex]

into a helper, I can probably pass the condition in as a lambda 
diff mbox series

Patch

diff --git a/tools/testing/selftests/net/lib/py/nsim.py b/tools/testing/selftests/net/lib/py/nsim.py
index 94aa32f59fdb..1fd50a308408 100644
--- a/tools/testing/selftests/net/lib/py/nsim.py
+++ b/tools/testing/selftests/net/lib/py/nsim.py
@@ -28,6 +28,13 @@  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 up(self):
+        ip("link set dev {} up".format(self.ifname))
+
+    def down(self):
+        ip("link set dev {} down".format(self.ifname))
 
     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..afc510c044ce 100755
--- a/tools/testing/selftests/net/nl_netdev.py
+++ b/tools/testing/selftests/net/nl_netdev.py
@@ -1,7 +1,8 @@ 
 #!/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, NetdevFamily, NetdevSimDev, ip
 
 
 def empty_check(nf) -> None:
@@ -15,9 +16,83 @@  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]
+
+        # No page pools when down
+        nsim.down()
+        pp_list = nf.page_pool_get({}, dump=True)
+        pp_list = [pp for pp in pp_list if pp.get("ifindex") == nsim.ifindex]
+        ksft_eq(len(pp_list), 0)
+
+        # Up, empty page pool appears
+        nsim.up()
+        pp_list = nf.page_pool_get({}, dump=True)
+        pp_list = [pp for pp in pp_list if pp.get("ifindex") == nsim.ifindex]
+        ksft_ge(len(pp_list), 0)
+        refs = sum([pp["inflight"] for pp in pp_list])
+        ksft_eq(refs, 0)
+
+        # Down, it disappears, again
+        nsim.down()
+        pp_list = nf.page_pool_get({}, dump=True)
+        pp_list = [pp for pp in pp_list if pp.get("ifindex") == nsim.ifindex]
+        ksft_eq(len(pp_list), 0)
+
+        # Up, allocate a page
+        nsim.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
+        nsim.down()
+        pp_list = nf.page_pool_get({}, dump=True)
+        pp_list = [pp for pp in pp_list if pp.get("ifindex") == nsim.ifindex]
+        ksft_eq(len(pp_list), 1)
+        refs = sum([pp["inflight"] for pp in pp_list if pp.get("ifindex") == nsim.ifindex])
+        ksft_eq(refs, 1)
+        undetached = [pp for pp in pp_list if "detach-time" not in pp]
+        ksft_eq(len(undetached), 0)
+
+        # New pp can get created, and we'll have two
+        nsim.up()
+        pp_list = nf.page_pool_get({}, dump=True)
+        pp_list = [pp for pp in pp_list if pp.get("ifindex") == nsim.ifindex]
+        attached = [pp for pp in pp_list if "detach-time" not in pp]
+        undetached = [pp for pp in pp_list if "detach-time" in pp]
+        ksft_eq(len(attached), 1)
+        ksft_eq(len(undetached), 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
+        for i in range(50):
+            pp_list = nf.page_pool_get({}, dump=True)
+            pp_list = [pp for pp in pp_list if pp.get("ifindex") == nsim.ifindex]
+            if len(pp_list) == 1:
+                break
+            time.sleep(0.05)
+        ksft_eq(len(pp_list), 1)
+
+        # And down...
+        nsim.down()
+        pp_list = nf.page_pool_get({}, dump=True)
+        pp_list = [pp for pp in pp_list if pp.get("ifindex") == nsim.ifindex]
+        ksft_eq(len(pp_list), 0)
+
+        # Last, leave the page hanging for destroy, nothing to check
+        # we're trying to exercise the orphaning path in the kernel
+        nsim.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__":