Merge branch 'tools-ynl-add-install-target'

Jan Stancek says:

====================
tools: ynl: add install target

This series adds an install target for ynl. The python code
is moved to a subdirectory, so it can be used as a package
with flat layout, as well as directly from the tree.

To try the install as a non-root user you can run:
  $ mkdir /tmp/myroot
  $ make DESTDIR=/tmp/myroot install

  $ PATH="/tmp/myroot/usr/bin:$PATH" PYTHONPATH="$(ls -1d /tmp/myroot/usr/lib/python*/site-packages)" ynl --help

Proposed install layout is described in last patch.
====================

Link: https://patch.msgid.link/cover.1736343575.git.jstancek@redhat.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
Jakub Kicinski
2025-01-09 12:53:29 -08:00
23 changed files with 113 additions and 25 deletions

View File

@@ -104,7 +104,7 @@ quiet_cmd_sphinx = SPHINX $@ --> file://$(abspath $(BUILDDIR)/$3/$4)
YNL_INDEX:=$(srctree)/Documentation/networking/netlink_spec/index.rst
YNL_RST_DIR:=$(srctree)/Documentation/networking/netlink_spec
YNL_YAML_DIR:=$(srctree)/Documentation/netlink/specs
YNL_TOOL:=$(srctree)/tools/net/ynl/ynl-gen-rst.py
YNL_TOOL:=$(srctree)/tools/net/ynl/pyynl/ynl_gen_rst.py
YNL_RST_FILES_TMP := $(patsubst %.yaml,%.rst,$(wildcard $(YNL_YAML_DIR)/*.yaml))
YNL_RST_FILES := $(patsubst $(YNL_YAML_DIR)%,$(YNL_RST_DIR)%, $(YNL_RST_FILES_TMP))

View File

@@ -89,7 +89,7 @@ Observability
=============
The relation between PF, irq, napi, and queue can be observed via netlink spec::
$ ./tools/net/ynl/cli.py --spec Documentation/netlink/specs/netdev.yaml --dump queue-get --json='{"ifindex": 13}'
$ ./tools/net/ynl/pyynl/cli.py --spec Documentation/netlink/specs/netdev.yaml --dump queue-get --json='{"ifindex": 13}'
[{'id': 0, 'ifindex': 13, 'napi-id': 539, 'type': 'rx'},
{'id': 1, 'ifindex': 13, 'napi-id': 540, 'type': 'rx'},
{'id': 2, 'ifindex': 13, 'napi-id': 541, 'type': 'rx'},
@@ -101,7 +101,7 @@ The relation between PF, irq, napi, and queue can be observed via netlink spec::
{'id': 3, 'ifindex': 13, 'napi-id': 542, 'type': 'tx'},
{'id': 4, 'ifindex': 13, 'napi-id': 543, 'type': 'tx'}]
$ ./tools/net/ynl/cli.py --spec Documentation/netlink/specs/netdev.yaml --dump napi-get --json='{"ifindex": 13}'
$ ./tools/net/ynl/pyynl/cli.py --spec Documentation/netlink/specs/netdev.yaml --dump napi-get --json='{"ifindex": 13}'
[{'id': 543, 'ifindex': 13, 'irq': 42},
{'id': 542, 'ifindex': 13, 'irq': 41},
{'id': 541, 'ifindex': 13, 'irq': 40},

View File

@@ -199,13 +199,13 @@ parameters mentioned above use hyphens instead of underscores:
Per-NAPI configuration can be done programmatically in a user application
or by using a script included in the kernel source tree:
``tools/net/ynl/cli.py``.
``tools/net/ynl/pyynl/cli.py``.
For example, using the script:
.. code-block:: bash
$ kernel-source/tools/net/ynl/cli.py \
$ kernel-source/tools/net/ynl/pyynl/cli.py \
--spec Documentation/netlink/specs/netdev.yaml \
--do napi-set \
--json='{"id": 345,

View File

@@ -1,4 +1,4 @@
SPDX-License-Identifier: GPL-2.0
This file is populated during the build of the documentation (htmldocs) by the
tools/net/ynl/ynl-gen-rst.py script.
tools/net/ynl/pyynl/ynl_gen_rst.py script.

View File

@@ -15,7 +15,7 @@ developing Netlink related code. The tool is implemented in Python
and can use a YAML specification to issue Netlink requests
to the kernel. Only Generic Netlink is supported.
The tool is located at ``tools/net/ynl/cli.py``. It accepts
The tool is located at ``tools/net/ynl/pyynl/cli.py``. It accepts
a handul of arguments, the most important ones are:
- ``--spec`` - point to the spec file
@@ -27,7 +27,7 @@ YAML specs can be found under ``Documentation/netlink/specs/``.
Example use::
$ ./tools/net/ynl/cli.py --spec Documentation/netlink/specs/ethtool.yaml \
$ ./tools/net/ynl/pyynl/cli.py --spec Documentation/netlink/specs/ethtool.yaml \
--do rings-get \
--json '{"header":{"dev-index": 18}}'
{'header': {'dev-index': 18, 'dev-name': 'eni1np1'},
@@ -75,7 +75,7 @@ the two marker lines like above to a file, add that file to git,
and run the regeneration tool. Grep the tree for ``YNL-GEN``
to see other examples.
The code generation itself is performed by ``tools/net/ynl/ynl-gen-c.py``
The code generation itself is performed by ``tools/net/ynl/pyynl/ynl_gen_c.py``
but it takes a few arguments so calling it directly for each file
quickly becomes tedious.
@@ -84,7 +84,7 @@ YNL lib
``tools/net/ynl/lib/`` contains an implementation of a C library
(based on libmnl) which integrates with code generated by
``tools/net/ynl/ynl-gen-c.py`` to create easy to use netlink wrappers.
``tools/net/ynl/pyynl/ynl_gen_c.py`` to create easy to use netlink wrappers.
YNL basics
----------

View File

@@ -1,5 +1,17 @@
# SPDX-License-Identifier: GPL-2.0
include ../../scripts/Makefile.arch
INSTALL ?= install
prefix ?= /usr
ifeq ($(LP64), 1)
libdir_relative = lib64
else
libdir_relative = lib
endif
libdir ?= $(prefix)/$(libdir_relative)
includedir ?= $(prefix)/include
SUBDIRS = lib generated samples
all: $(SUBDIRS) libynl.a
@@ -21,5 +33,20 @@ clean distclean:
fi \
done
rm -f libynl.a
rm -rf pyynl/__pycache__
rm -rf pyynl/lib/__pycache__
rm -rf pyynl.egg-info
rm -rf build
.PHONY: all clean distclean $(SUBDIRS)
install: libynl.a lib/*.h
@echo -e "\tINSTALL libynl.a"
@$(INSTALL) -d $(DESTDIR)$(libdir)
@$(INSTALL) -m 0644 libynl.a $(DESTDIR)$(libdir)/libynl.a
@echo -e "\tINSTALL libynl headers"
@$(INSTALL) -d $(DESTDIR)$(includedir)/ynl
@$(INSTALL) -m 0644 lib/*.h $(DESTDIR)$(includedir)/ynl/
@echo -e "\tINSTALL pyynl"
@pip install --prefix=$(DESTDIR)$(prefix) .
@make -C generated install
.PHONY: all clean distclean install $(SUBDIRS)

View File

@@ -1,2 +1,3 @@
*-user.c
*-user.h
*.rst

View File

@@ -7,32 +7,44 @@ ifeq ("$(DEBUG)","1")
CFLAGS += -g -fsanitize=address -fsanitize=leak -static-libasan
endif
INSTALL ?= install
prefix ?= /usr
datarootdir ?= $(prefix)/share
docdir ?= $(datarootdir)/doc
includedir ?= $(prefix)/include
include ../Makefile.deps
YNL_GEN_ARG_ethtool:=--user-header linux/ethtool_netlink.h \
--exclude-op stats-get
TOOL:=../ynl-gen-c.py
TOOL:=../pyynl/ynl_gen_c.py
TOOL_RST:=../pyynl/ynl_gen_rst.py
SPECS_DIR:=../../../../Documentation/netlink/specs
GENS_PATHS=$(shell grep -nrI --files-without-match \
'protocol: netlink' \
../../../../Documentation/netlink/specs/)
GENS=$(patsubst ../../../../Documentation/netlink/specs/%.yaml,%,${GENS_PATHS})
$(SPECS_DIR))
GENS=$(patsubst $(SPECS_DIR)/%.yaml,%,${GENS_PATHS})
SRCS=$(patsubst %,%-user.c,${GENS})
HDRS=$(patsubst %,%-user.h,${GENS})
OBJS=$(patsubst %,%-user.o,${GENS})
all: protos.a $(HDRS) $(SRCS) $(KHDRS) $(KSRCS) $(UAPI)
SPECS_PATHS=$(wildcard $(SPECS_DIR)/*.yaml)
SPECS=$(patsubst $(SPECS_DIR)/%.yaml,%,${SPECS_PATHS})
RSTS=$(patsubst %,%.rst,${SPECS})
all: protos.a $(HDRS) $(SRCS) $(KHDRS) $(KSRCS) $(UAPI) $(RSTS)
protos.a: $(OBJS)
@echo -e "\tAR $@"
@ar rcs $@ $(OBJS)
%-user.h: ../../../../Documentation/netlink/specs/%.yaml $(TOOL)
%-user.h: $(SPECS_DIR)/%.yaml $(TOOL)
@echo -e "\tGEN $@"
@$(TOOL) --mode user --header --spec $< -o $@ $(YNL_GEN_ARG_$*)
%-user.c: ../../../../Documentation/netlink/specs/%.yaml $(TOOL)
%-user.c: $(SPECS_DIR)/%.yaml $(TOOL)
@echo -e "\tGEN $@"
@$(TOOL) --mode user --source --spec $< -o $@ $(YNL_GEN_ARG_$*)
@@ -40,14 +52,37 @@ protos.a: $(OBJS)
@echo -e "\tCC $@"
@$(COMPILE.c) $(CFLAGS_$*) -o $@ $<
%.rst: $(SPECS_DIR)/%.yaml $(TOOL_RST)
@echo -e "\tGEN_RST $@"
@$(TOOL_RST) -o $@ -i $<
clean:
rm -f *.o
distclean: clean
rm -f *.c *.h *.a
rm -f *.c *.h *.a *.rst
regen:
@../ynl-regen.sh
.PHONY: all clean distclean regen
install-headers: $(HDRS)
@echo -e "\tINSTALL generated headers"
@$(INSTALL) -d $(DESTDIR)$(includedir)/ynl
@$(INSTALL) -m 0644 *.h $(DESTDIR)$(includedir)/ynl/
install-rsts: $(RSTS)
@echo -e "\tINSTALL generated docs"
@$(INSTALL) -d $(DESTDIR)$(docdir)/ynl
@$(INSTALL) -m 0644 $(RSTS) $(DESTDIR)$(docdir)/ynl/
install-specs:
@echo -e "\tINSTALL specs"
@$(INSTALL) -d $(DESTDIR)$(datarootdir)/ynl
@$(INSTALL) -m 0644 ../../../../Documentation/netlink/*.yaml $(DESTDIR)$(datarootdir)/ynl/
@$(INSTALL) -d $(DESTDIR)$(datarootdir)/ynl/specs
@$(INSTALL) -m 0644 $(SPECS_DIR)/*.yaml $(DESTDIR)$(datarootdir)/ynl/specs/
install: install-headers install-rsts install-specs
.PHONY: all clean distclean regen install install-headers install-rsts install-specs
.DEFAULT_GOAL: all

View File

@@ -1,2 +1 @@
__pycache__/
*.d

View File

@@ -19,7 +19,6 @@ ynl.a: $(OBJS)
clean:
rm -f *.o *.d *~
rm -rf __pycache__
distclean: clean
rm -f *.a

View File

@@ -0,0 +1,24 @@
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"
[project]
name = "pyynl"
authors = [
{name = "Donald Hunter", email = "donald.hunter@gmail.com"},
{name = "Jakub Kicinski", email = "kuba@kernel.org"},
]
description = "yaml netlink (ynl)"
version = "0.0.1"
requires-python = ">=3.9"
dependencies = [
"pyyaml==6.*",
"jsonschema==4.*"
]
[tool.setuptools.packages.find]
include = ["pyynl", "pyynl.lib"]
[project.scripts]
ynl = "pyynl.cli:main"
ynl-ethtool = "pyynl.ethtool:main"

2
tools/net/ynl/pyynl/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
__pycache__/
lib/__pycache__/

View File

View File

@@ -1,7 +1,7 @@
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
TOOL=$(dirname $(realpath $0))/ynl-gen-c.py
TOOL=$(dirname $(realpath $0))/pyynl/ynl_gen_c.py
force=
search=

View File

@@ -13,14 +13,14 @@ try:
SPEC_PATH = KSFT_DIR / "net/lib/specs"
sys.path.append(tools_full_path.as_posix())
from net.lib.ynl.lib import YnlFamily, NlError
from net.lib.ynl.pyynl.lib import YnlFamily, NlError
else:
# Running in tree
tools_full_path = KSRC / "tools"
SPEC_PATH = KSRC / "Documentation/netlink/specs"
sys.path.append(tools_full_path.as_posix())
from net.ynl.lib import YnlFamily, NlError
from net.ynl.pyynl.lib import YnlFamily, NlError
except ModuleNotFoundError as e:
ksft_pr("Failed importing `ynl` library from kernel sources")
ksft_pr(str(e))

View File

@@ -31,7 +31,8 @@ $(OUTPUT)/libynl.a: $(YNL_SPECS) $(OUTPUT)/.libynl-$(YNL_GENS_HASH).sig
$(Q)cp $(top_srcdir)/tools/net/ynl/libynl.a $(OUTPUT)/libynl.a
EXTRA_CLEAN += \
$(top_srcdir)/tools/net/ynl/lib/__pycache__ \
$(top_srcdir)/tools/net/ynl/pyynl/__pycache__ \
$(top_srcdir)/tools/net/ynl/pyynl/lib/__pycache__ \
$(top_srcdir)/tools/net/ynl/lib/*.[ado] \
$(OUTPUT)/.libynl-*.sig \
$(OUTPUT)/libynl.a