netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net-next v1 1/2] tools/net/ynl: add support for --family and --list-families
@ 2025-01-10 14:41 Donald Hunter
  2025-01-10 14:41 ` [PATCH net-next v1 2/2] tools/net/ynl: ethtool: support spec load from install location Donald Hunter
  2025-01-11  1:08 ` [PATCH net-next v1 1/2] tools/net/ynl: add support for --family and --list-families Jakub Kicinski
  0 siblings, 2 replies; 5+ messages in thread
From: Donald Hunter @ 2025-01-10 14:41 UTC (permalink / raw)
  To: netdev, Jakub Kicinski, David S. Miller, Eric Dumazet,
	Paolo Abeni, Simon Horman, Jan Stancek, Jiri Pirko
  Cc: donald.hunter, Donald Hunter

Add a --family option to ynl to specify the spec by family name instead
of file path, with support for searching in-tree and system install
location and a --list-families option to show the available families.

./tools/net/ynl/pyynl/cli.py --family rt_addr --dump getaddr

Signed-off-by: Donald Hunter <donald.hunter@gmail.com>
---
 tools/net/ynl/pyynl/cli.py | 44 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 42 insertions(+), 2 deletions(-)

diff --git a/tools/net/ynl/pyynl/cli.py b/tools/net/ynl/pyynl/cli.py
index 41d9fa5c818d..70dd8bf7512b 100755
--- a/tools/net/ynl/pyynl/cli.py
+++ b/tools/net/ynl/pyynl/cli.py
@@ -3,6 +3,7 @@
 
 import argparse
 import json
+import os
 import pathlib
 import pprint
 import sys
@@ -10,6 +11,23 @@ import sys
 sys.path.append(pathlib.Path(__file__).resolve().parent.as_posix())
 from lib import YnlFamily, Netlink, NlError
 
+sys_schema_dir='/usr/share/ynl'
+relative_schema_dir='../../../../Documentation/netlink'
+
+def schema_dir():
+    script_dir = os.path.dirname(os.path.abspath(__file__))
+    schema_dir = os.path.abspath(f"{script_dir}/{relative_schema_dir}")
+    if not os.path.isdir(schema_dir):
+        schema_dir = sys_schema_dir
+    if not os.path.isdir(schema_dir):
+        raise Exception(f"Schema directory {schema_dir} does not exist")
+    return schema_dir
+
+def spec_dir():
+    spec_dir = schema_dir() + '/specs'
+    if not os.path.isdir(spec_dir):
+        raise Exception(f"Spec directory {spec_dir} does not exist")
+    return spec_dir
 
 class YnlEncoder(json.JSONEncoder):
     def default(self, obj):
@@ -32,7 +50,14 @@ def main():
 
     parser = argparse.ArgumentParser(description=description,
                                      epilog=epilog)
-    parser.add_argument('--spec', dest='spec', type=str, required=True)
+    spec_group = parser.add_mutually_exclusive_group(required=True)
+    spec_group.add_argument('--family', dest='family', type=str,
+                            help='name of the netlink FAMILY')
+    spec_group.add_argument('--list-families', action='store_true',
+                            help='list all available netlink families')
+    spec_group.add_argument('--spec', dest='spec', type=str,
+                            help='choose the family by SPEC file path')
+
     parser.add_argument('--schema', dest='schema', type=str)
     parser.add_argument('--no-schema', action='store_true')
     parser.add_argument('--json', dest='json_text', type=str)
@@ -70,6 +95,12 @@ def main():
         else:
             pprint.PrettyPrinter().pprint(msg)
 
+    if args.list_families:
+        for filename in sorted(os.listdir(spec_dir())):
+            if filename.endswith('.yaml'):
+                print(filename.removesuffix('.yaml'))
+        return
+
     if args.no_schema:
         args.schema = ''
 
@@ -77,7 +108,16 @@ def main():
     if args.json_text:
         attrs = json.loads(args.json_text)
 
-    ynl = YnlFamily(args.spec, args.schema, args.process_unknown,
+    if args.family:
+        spec = f"{spec_dir()}/{args.family}.yaml"
+        if args.schema is None:
+            args.schema = ''
+    else:
+        spec = args.spec
+    if not os.path.isfile(spec):
+        raise Exception(f"Spec file {spec} does not exist")
+
+    ynl = YnlFamily(spec, args.schema, args.process_unknown,
                     recv_size=args.dbg_small_recv)
     if args.dbg_small_recv:
         ynl.set_recv_dbg(True)
-- 
2.47.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH net-next v1 2/2] tools/net/ynl: ethtool: support spec load from install location
  2025-01-10 14:41 [PATCH net-next v1 1/2] tools/net/ynl: add support for --family and --list-families Donald Hunter
@ 2025-01-10 14:41 ` Donald Hunter
  2025-01-11  1:10   ` Jakub Kicinski
  2025-01-11  1:08 ` [PATCH net-next v1 1/2] tools/net/ynl: add support for --family and --list-families Jakub Kicinski
  1 sibling, 1 reply; 5+ messages in thread
From: Donald Hunter @ 2025-01-10 14:41 UTC (permalink / raw)
  To: netdev, Jakub Kicinski, David S. Miller, Eric Dumazet,
	Paolo Abeni, Simon Horman, Jan Stancek, Jiri Pirko
  Cc: donald.hunter, Donald Hunter

Replace hard-coded paths for spec and schema with lookup functions so
that ethtool.py will work in-tree or when installed.

Signed-off-by: Donald Hunter <donald.hunter@gmail.com>
---
 tools/net/ynl/pyynl/ethtool.py | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/tools/net/ynl/pyynl/ethtool.py b/tools/net/ynl/pyynl/ethtool.py
index ebb0a11f67bf..af7fddd7b085 100755
--- a/tools/net/ynl/pyynl/ethtool.py
+++ b/tools/net/ynl/pyynl/ethtool.py
@@ -11,6 +11,7 @@ import os
 
 sys.path.append(pathlib.Path(__file__).resolve().parent.as_posix())
 from lib import YnlFamily
+from cli import schema_dir, spec_dir
 
 def args_to_req(ynl, op_name, args, req):
     """
@@ -156,10 +157,8 @@ def main():
     args = parser.parse_args()
 
     script_abs_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
-    spec = os.path.join(script_abs_dir,
-                        '../../../Documentation/netlink/specs/ethtool.yaml')
-    schema = os.path.join(script_abs_dir,
-                          '../../../Documentation/netlink/genetlink-legacy.yaml')
+    spec = os.path.join(spec_dir(), 'ethtool.yaml')
+    schema = os.path.join(schema_dir(), 'genetlink-legacy.yaml')
 
     ynl = YnlFamily(spec, schema)
 
-- 
2.47.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH net-next v1 1/2] tools/net/ynl: add support for --family and --list-families
  2025-01-10 14:41 [PATCH net-next v1 1/2] tools/net/ynl: add support for --family and --list-families Donald Hunter
  2025-01-10 14:41 ` [PATCH net-next v1 2/2] tools/net/ynl: ethtool: support spec load from install location Donald Hunter
@ 2025-01-11  1:08 ` Jakub Kicinski
  2025-01-11 14:04   ` Donald Hunter
  1 sibling, 1 reply; 5+ messages in thread
From: Jakub Kicinski @ 2025-01-11  1:08 UTC (permalink / raw)
  To: Donald Hunter
  Cc: netdev, David S. Miller, Eric Dumazet, Paolo Abeni, Simon Horman,
	Jan Stancek, Jiri Pirko, donald.hunter

On Fri, 10 Jan 2025 14:41:44 +0000 Donald Hunter wrote:
> Add a --family option to ynl to specify the spec by family name instead
> of file path, with support for searching in-tree and system install
> location and a --list-families option to show the available families.

Neat!

>  class YnlEncoder(json.JSONEncoder):
>      def default(self, obj):
> @@ -32,7 +50,14 @@ def main():
>  
>      parser = argparse.ArgumentParser(description=description,
>                                       epilog=epilog)
> -    parser.add_argument('--spec', dest='spec', type=str, required=True)
> +    spec_group = parser.add_mutually_exclusive_group(required=True)
> +    spec_group.add_argument('--family', dest='family', type=str,
> +                            help='name of the netlink FAMILY')
> +    spec_group.add_argument('--list-families', action='store_true',
> +                            help='list all available netlink families')

Do we need to indicate that the list families lists the families for
which we found specs in the filesystem? As opposed to listing all
families currently loaded in the kernel?

Some users may be surprised if they run --list-families, see a family,
issue a request and get an exception that family is not found..

I guess OTOH we also list spec ops in --list-ops, so there's precedent.

Up to you.

> +    if args.family:
> +        spec = f"{spec_dir()}/{args.family}.yaml"
> +        if args.schema is None:

Could we only do this if spec_dir() startswith sys_schema_dir ?

We want to make sure schema is always validated during development.

> +            args.schema = ''
> +    else:
> +        spec = args.spec
> +    if not os.path.isfile(spec):
> +        raise Exception(f"Spec file {spec} does not exist")
> +
> +    ynl = YnlFamily(spec, args.schema, args.process_unknown,
>                      recv_size=args.dbg_small_recv)
>      if args.dbg_small_recv:
>          ynl.set_recv_dbg(True)


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net-next v1 2/2] tools/net/ynl: ethtool: support spec load from install location
  2025-01-10 14:41 ` [PATCH net-next v1 2/2] tools/net/ynl: ethtool: support spec load from install location Donald Hunter
@ 2025-01-11  1:10   ` Jakub Kicinski
  0 siblings, 0 replies; 5+ messages in thread
From: Jakub Kicinski @ 2025-01-11  1:10 UTC (permalink / raw)
  To: Donald Hunter
  Cc: netdev, David S. Miller, Eric Dumazet, Paolo Abeni, Simon Horman,
	Jan Stancek, Jiri Pirko, donald.hunter

On Fri, 10 Jan 2025 14:41:45 +0000 Donald Hunter wrote:
> Replace hard-coded paths for spec and schema with lookup functions so
> that ethtool.py will work in-tree or when installed.

Acked-by: Jakub Kicinski <kuba@kernel.org>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net-next v1 1/2] tools/net/ynl: add support for --family and --list-families
  2025-01-11  1:08 ` [PATCH net-next v1 1/2] tools/net/ynl: add support for --family and --list-families Jakub Kicinski
@ 2025-01-11 14:04   ` Donald Hunter
  0 siblings, 0 replies; 5+ messages in thread
From: Donald Hunter @ 2025-01-11 14:04 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: Donald Hunter, netdev, David S. Miller, Eric Dumazet, Paolo Abeni,
	Simon Horman, Jan Stancek, Jiri Pirko

On Sat, 11 Jan 2025 at 01:08, Jakub Kicinski <kuba@kernel.org> wrote:
>
> On Fri, 10 Jan 2025 14:41:44 +0000 Donald Hunter wrote:
> > Add a --family option to ynl to specify the spec by family name instead
> > of file path, with support for searching in-tree and system install
> > location and a --list-families option to show the available families.
>
> Neat!
>
> >  class YnlEncoder(json.JSONEncoder):
> >      def default(self, obj):
> > @@ -32,7 +50,14 @@ def main():
> >
> >      parser = argparse.ArgumentParser(description=description,
> >                                       epilog=epilog)
> > -    parser.add_argument('--spec', dest='spec', type=str, required=True)
> > +    spec_group = parser.add_mutually_exclusive_group(required=True)
> > +    spec_group.add_argument('--family', dest='family', type=str,
> > +                            help='name of the netlink FAMILY')
> > +    spec_group.add_argument('--list-families', action='store_true',
> > +                            help='list all available netlink families')
>
> Do we need to indicate that the list families lists the families for
> which we found specs in the filesystem? As opposed to listing all
> families currently loaded in the kernel?

That's a good suggestion. I'll update the help to say families that we
have specs for. Will also try to add something to the help about
loaded families.

> Some users may be surprised if they run --list-families, see a family,
> issue a request and get an exception that family is not found..
>
> I guess OTOH we also list spec ops in --list-ops, so there's precedent.
>
> Up to you.
>
> > +    if args.family:
> > +        spec = f"{spec_dir()}/{args.family}.yaml"
> > +        if args.schema is None:
>
> Could we only do this if spec_dir() startswith sys_schema_dir ?
>
> We want to make sure schema is always validated during development.

Yep, makes sense.

> > +            args.schema = ''
> > +    else:
> > +        spec = args.spec
> > +    if not os.path.isfile(spec):
> > +        raise Exception(f"Spec file {spec} does not exist")
> > +
> > +    ynl = YnlFamily(spec, args.schema, args.process_unknown,
> >                      recv_size=args.dbg_small_recv)
> >      if args.dbg_small_recv:
> >          ynl.set_recv_dbg(True)
>


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-01-11 14:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-10 14:41 [PATCH net-next v1 1/2] tools/net/ynl: add support for --family and --list-families Donald Hunter
2025-01-10 14:41 ` [PATCH net-next v1 2/2] tools/net/ynl: ethtool: support spec load from install location Donald Hunter
2025-01-11  1:10   ` Jakub Kicinski
2025-01-11  1:08 ` [PATCH net-next v1 1/2] tools/net/ynl: add support for --family and --list-families Jakub Kicinski
2025-01-11 14:04   ` Donald Hunter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).