tools: ynl: C-based YNL linter
Subsystems (3)
cover tools: ynl: C-based YNL linter
This RFC series adds a C-based linter for checking YNL/C alignment, and as an example fixes the reported issues in rt-link.
The series is organized as follows:
Patch 1-2) Prepares the last families for C code gen.
Patch 3) Introduces the new linter.
Patch 4-8) Fixes the reported issues in the rt-link family.
When run on net-next the linter reports the following:
$ make -C tools/net/ynl/generated lint 2>&1 > /dev/null | grep 'Linter summary' conntrack: Linter summary: 48 errors, 0 warnings devlink: Linter summary: 5 errors, 2 warnings ethtool: Linter summary: 10 errors, 2 warnings mptcp_pm: Linter summary: 0 errors, 4 warnings nftables: Linter summary: 183 errors, 0 warnings nl80211: Linter summary: 193 errors, 0 warnings nlctrl: Linter summary: 23 errors, 0 warnings ovs_flow: Linter summary: 2 errors, 3 warnings rt-link: Linter summary: 173 errors, 8 warnings rt-neigh: Linter summary: 31 errors, 0 warnings rt-rule: Linter summary: 9 errors, 0 warnings tc: Linter summary: 11 errors, 1 warnings
After this series rt-link has 2 false-positive warnings:
$ make -C tools/net/ynl/generated/ rt-link-lint 2>&1 > /dev/null rt-link: WARN: vlan-protocols: Possible missing member before ETH_P_8021AD (33024 -> 34984) rt-link: WARN: netkit-policy: Possible missing member before NETKIT_DROP (0 -> 2) rt-link: Linter summary: 0 errors, 2 warnings
WDYT?
Asbjørn Sloth Tønnesen (8):
netlink: specs: nftables: add uapi-header
netlink: specs: conntrack: add uapi-header
tools: ynl: add C-based YNL linter
netlink: specs: rt-link: add C naming info
netlink: specs: rt-link: re-align IPv4 devconf
netlink: specs: rt-link: re-align ifla-inet6-stats
netlink: specs: rt-link: fix ifinfo-flags names
netlink: specs: rt-link: fix netkit-policy names
Documentation/netlink/specs/conntrack.yaml | 1 + Documentation/netlink/specs/nftables.yaml | 1 + Documentation/netlink/specs/rt-link.yaml | 60 +++++--- include/uapi/linux/snmp.h | 6 +- tools/net/ynl/Makefile.deps | 11 +- tools/net/ynl/generated/.gitignore | 3 + tools/net/ynl/generated/Makefile | 27 +++- tools/net/ynl/linter/gen-h.sh | 23 ++++ tools/net/ynl/linter/linter.h | 152 +++++++++++++++++++++ tools/net/ynl/pyynl/ynl_gen_c.py | 72 ++++++++-- 10 files changed, 316 insertions(+), 40 deletions(-) create mode 100755 tools/net/ynl/linter/gen-h.sh create mode 100644 tools/net/ynl/linter/linter.h
base-commit: 0abe8777490ebc008635ecb20297761d1b317697
Signature · 1 line
2.55.0
RFC PATCH 1/8 netlink: specs: nftables: add uapi-header
Define uAPI header, as needed for C code gen.
--- Documentation/netlink/specs/nftables.yaml | 1 + 1 file changed, 1 insertion(+)
Documentation/netlink/specs/nftables.yaml +1 -0
@@ -2,6 +2,7 @@ --- name: nftables protocol: netlink-raw +uapi-header: linux/netfilter/nf_tables.h protonum: 12 doc: >-
Signature · 1 line
2.55.0
RFC PATCH 2/8 netlink: specs: conntrack: add uapi-header
In reply to Asbjørn Sloth TønnesenDefine uAPI header, as needed for C code gen.
--- Documentation/netlink/specs/conntrack.yaml | 1 + 1 file changed, 1 insertion(+)
Documentation/netlink/specs/conntrack.yaml +1 -0
@@ -2,6 +2,7 @@ --- name: conntrack protocol: netlink-raw +uapi-header: linux/netfilter/nf_conntrack_common.h protonum: 12 doc: >-
Signature · 1 line
2.55.0
Jakub Kicinski
maintainerRe: [RFC PATCH net-next 2/8] netlink: specs: conntrack: add uapi-header
Quoted 1 line · On Thu, 10 Sep 2026 22:17:22 +0000 Asbjørn Sloth Tønnesen wrote:
Define uAPI header, as needed for C code gen.
You mean your own code gen? We don't support real codegen on conntrack
tools/net/ynl/generated/Makefile:27:GENS_UNSUP=conntrack nftables
RFC PATCH 3/8 tools: ynl: add C-based YNL linter
In reply to Asbjørn Sloth TønnesenValidate that structures described in YNL match their C counterpart, currently focused on enums.
The aim is to protect against uAPI regressions, and the linter output should be easy to digest in NIPA.
Building and execution happens in the following steps:
1) ynl_gen_c.py is used to generate C data structures.
Example: make -C tools/net/ynl/generated rt-link-linter.c
2) gen-h.sh generates a header file with LINTER_HAS_* defines.
Example: make -C tools/net/ynl/generated rt-link-linter.h
3) Build linter program.
Example: make -C tools/net/ynl/generated rt-link-linter
4) Execute linter and report findings (if any).
Example: make -C tools/net/ynl/generated rt-link-lint
--- tools/net/ynl/generated/.gitignore | 3 + tools/net/ynl/generated/Makefile | 27 ++++- tools/net/ynl/linter/gen-h.sh | 23 +++++ tools/net/ynl/linter/linter.h | 152 +++++++++++++++++++++++++++++ tools/net/ynl/pyynl/ynl_gen_c.py | 72 +++++++++++--- 5 files changed, 261 insertions(+), 16 deletions(-) create mode 100755 tools/net/ynl/linter/gen-h.sh create mode 100644 tools/net/ynl/linter/linter.h
tools/net/ynl/generated/.gitignore +3 -0
@@ -1,3 +1,6 @@ *-user.c *-user.h +*-linter.c +*-linter.h +*-linter *.rst
tools/net/ynl/generated/Makefile +23 -4
@@ -33,8 +33,9 @@ OBJS=$(patsubst %,%-user.o,${GENS}) SPECS_PATHS=$(wildcard $(SPECS_DIR)/*.yaml) SPECS=$(patsubst $(SPECS_DIR)/%.yaml,%,${SPECS_PATHS}) RSTS=$(patsubst %,%.rst,${SPECS}) +LINTS=$(patsubst %,%-lint,${SPECS}) -all: protos.a $(HDRS) $(SRCS) $(KHDRS) $(KSRCS) $(UAPI) $(RSTS) +all: protos.a $(HDRS) $(SRCS) $(KHDRS) $(KSRCS) $(UAPI) $(RSTS) $(LINTS) protos.a: $(OBJS) @echo -e "\tAR $@"
@@ -56,11 +57,29 @@ protos.a: $(OBJS) @echo -e "\tGEN_RST $@" @$(TOOL_RST) -o $@ -i $< +%-linter.c: $(SPECS_DIR)/%.yaml $(TOOL) ../linter/linter.h + @echo -e "\tGEN $@" + @$(TOOL) --mode user --linter --spec $< -o $@ $(YNL_GEN_ARG_$*) + +%-linter.h: %-linter.c ../linter/gen-h.sh + @echo -e "\tGEN $@" + @../linter/gen-h.sh $(CFLAGS) $(CFLAGS_$*) < $< > $@ + +%-linter: %-linter.c %-linter.h ../linter/linter.h + @echo -e "\tCC $@" + @$(CC) $(CFLAGS) $(CFLAGS_$*) -include $*-linter.h -o $@ $< + +%-lint: %-linter + @echo -e "\tLINT $@" + @./$< $* + +lint: $(LINTS) + clean: rm -f *.o distclean: clean - rm -f *.c *.h *.a *.rst + rm -f *.c *.h *.a *.rst *-linter regen: @../ynl-regen.sh
@@ -68,7 +87,7 @@ regen: install-headers: $(HDRS) @echo -e "\tINSTALL generated headers" @$(INSTALL) -d $(DESTDIR)$(includedir)/ynl - @$(INSTALL) -m 0644 *.h $(DESTDIR)$(includedir)/ynl/ + @$(INSTALL) -m 0644 $(KHDRS) $(DESTDIR)$(includedir)/ynl/ install-rsts: $(RSTS) @echo -e "\tINSTALL generated docs"
@@ -84,5 +103,5 @@ install-specs: install: install-headers install-rsts install-specs -.PHONY: all clean distclean regen install install-headers install-rsts install-specs +.PHONY: all clean distclean regen install install-headers install-rsts install-specs lint .DEFAULT_GOAL: all
tools/net/ynl/linter/gen-h.sh new file +23 -0
@@ -0,0 +1,23 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 + +# Usage: ./gen-h.sh $CFLAGS < in.c > out.h +# +# Extract valid symbols, and define that they are available. This is a +# workaround, to avoid compile failures due to non-existent enum members. +# +# Capitalized keywords found in the preprocessor output, are mostly enum +# members. +# +# As an example, existence of FOO can be checked with: +# #if defined(FOO) || defined(LINTER_HAS_FOO) +# thus checking for FOO either as a macro or as an enum members. + +set -e + +echo '/* This is an auto-generated file */' +grep '^#include <linux/' | + cpp -x c "$@" - | + grep -wo '[A-Z][A-Z0-9_]\+' | + sort | uniq | + sed -e 's/^/#define LINTER_HAS_/g'
tools/net/ynl/linter/linter.h new file +152 -0
152 lines not on this page · raw message
tools/net/ynl/pyynl/ynl_gen_c.py +60 -12
115 lines not on this page · raw message
Signature · 1 line
2.55.0
Jakub Kicinski
maintainerRe: [RFC PATCH net-next 3/8] tools: ynl: add C-based YNL linter
Quoted 2 lines · On Thu, 10 Sep 2026 22:17:23 +0000 Asbjørn Sloth Tønnesen wrote:
Validate that structures described in YNL match their C counterpart, currently focused on enums.
I don't see the point of this. LLMs will catch most of the problems and for the hundredth time classic netlink is not the focus for YNL. Anything genetlink can have YNL generate the code and uAPI so problems do no exist.
Feel free to keep the code OOT and let's see if you can prove my assertion wrong within a couple of release :)
If you have any ready fixes to the spec please post them. I'm also sitting on a bunch of fixes based on LLM scans, I'm just blocked on the "15 outstanding patches" limit :(
Re: [RFC PATCH net-next 3/8] tools: ynl: add C-based YNL linter
Quoted 8 lines · Jakub Kicinski
Quoted 2 lines · On Thu, 10 Sep 2026 22:17:23 +0000 Asbjørn Sloth Tønnesen wrote:
Validate that structures described in YNL match their C counterpart, currently focused on enums.
I don't see the point of this. LLMs will catch most of the problems and for the hundredth time classic netlink is not the focus for YNL. Anything genetlink can have YNL generate the code and uAPI so problems do no exist.
FTR, I didn't intent to imply any "regular" C code gen on classic families, the --linter was a very limited output mode, only for validating enum alignment.
I like to also have deterministic checks, the LLM didn't catch the IPV4_DEVCONF_* alignment issue, while reviewing commit 5e63ade19044 ("netlink: specs: rt-link: update ipv6 devconf doc"), where I claimed that IPV4_DEVCONF_FORWARDING = 1 (while it was 0 in YNL) in patch 1/3.
Now reading the raw log, the closest it got was "... which means there could be an existing bug in ipv4-devconf!", but then it forgot about it, and that was for patch 3/3. I can't find it in the Clashiko log for that series.
https://sashiko.dev/#/log/148492
Quoted 2 lines
Feel free to keep the code OOT and let's see if you can prove my assertion wrong within a couple of release :)
I will keep it OOT for now. Thank you for your review!
Quoted 3 lines
If you have any ready fixes to the spec please post them. I'm also sitting on a bunch of fixes based on LLM scans, I'm just blocked on the "15 outstanding patches" limit :(
I have posted my fixes for most severe issues now, and left out adding extra enum-prefix and header entries. I skimmed your new devlink spec patches, to look for duplicates, but will do a review tomorrow.
RFC PATCH 4/8 netlink: specs: rt-link: add C naming info
In reply to Asbjørn Sloth TønnesenAdd missing naming info needed for C code gen.
Add headers to Makefile.deps, so that the local copy is used, except for if.h which is non-trivial due to a compiler.h include.
Rename snmp.h header guard, for Makefile.deps compatibility, this is opaque once installed by scripts/headers_install.sh.
--- Documentation/netlink/specs/rt-link.yaml | 25 +++++++++++++++++++++--- include/uapi/linux/snmp.h | 6 +++--- tools/net/ynl/Makefile.deps | 11 +++++++++-- 3 files changed, 34 insertions(+), 8 deletions(-)
Documentation/netlink/specs/rt-link.yaml +22 -3
@@ -58,6 +58,7 @@ definitions: name: vlan-protocols type: enum enum-name: + name-prefix: eth-p- entries: - name: 8021q
@@ -305,6 +306,8 @@ definitions: - name: ipv4-devconf enum-name: + name-prefix: ipv4-devconf- + header: linux/ip.h type: enum entries: -
@@ -376,6 +379,8 @@ definitions: - name: ipv6-devconf enum-name: + name-prefix: devconf- + header: linux/ipv6.h type: enum entries: -
@@ -501,6 +506,8 @@ definitions: - name: ifla-icmp6-stats enum-name: + name-prefix: icmp6-mib- + header: linux/snmp.h type: enum entries: -
@@ -520,6 +527,8 @@ definitions: - name: ifla-inet6-stats enum-name: + name-prefix: ipstats-mib- + header: linux/snmp.h type: enum entries: -
@@ -640,6 +649,8 @@ definitions: name: vlan-flags type: flags enum-name: + name-prefix: vlan-flag- + header: linux/if_vlan.h entries: - reorder-hdr - gvrp
@@ -727,6 +738,8 @@ definitions: name: ifla-vf-link-state-enum type: enum enum-name: + name-prefix: ifla-vf-link-state- + header: linux/if_link.h entries: - auto - enable
@@ -794,6 +807,7 @@ definitions: name: rtext-filter type: flags enum-name: + name-prefix: rtext-filter- entries: - vf - brvlan
@@ -806,7 +820,8 @@ definitions: - name: netkit-policy type: enum - enum-name: + enum-name: netkit-action + name-prefix: netkit- entries: - name: forward
@@ -818,6 +833,7 @@ definitions: name: netkit-mode type: enum enum-name: netkit-mode + name-prefix: netkit- entries: - name: l2 - name: l3
@@ -825,7 +841,8 @@ definitions: - name: netkit-scrub type: enum - enum-name: + enum-name: netkit-scrub + name-prefix: netkit-scrub- entries: - name: none - name: default
@@ -833,13 +850,14 @@ definitions: name: netkit-pairing type: enum enum-name: netkit-pairing + name-prefix: netkit-device- entries: - name: pair - name: single - name: ovpn-mode enum-name: ovpn-mode - name-prefix: ovpn-mode + name-prefix: ovpn-mode- type: enum entries: - p2p
@@ -848,6 +866,7 @@ definitions: name: br-stp-mode type: enum enum-name: br-stp-mode + name-prefix: br-stp-mode- entries: - auto - user
include/uapi/linux/snmp.h +3 -3
@@ -5,8 +5,8 @@* Author: Hideaki YOSHIFUJI <[email protected]> */ -#ifndef _LINUX_SNMP_H -#define _LINUX_SNMP_H +#ifndef _UAPI_LINUX_SNMP_H +#define _UAPI_LINUX_SNMP_H/* ipstats mib definitions */ /*
@@ -372,4 +372,4 @@ enum__LINUX_MIB_TLSMAX }; -#endif /* _LINUX_SNMP_H */ +#endif /* _UAPI_LINUX_SNMP_H */
tools/net/ynl/Makefile.deps +9 -2
@@ -40,8 +40,15 @@ CFLAGS_ovs_vport:=$(call get_hdr_inc,__LINUX_OPENVSWITCH_H,openvswitch.h) CFLAGS_psp:=$(call get_hdr_inc,_LINUX_PSP_H,psp.h) CFLAGS_rt-addr:=$(call get_hdr_inc,__LINUX_RTNETLINK_H,rtnetlink.h) \ $(call get_hdr_inc,__LINUX_IF_ADDR_H,if_addr.h) -CFLAGS_rt-link:=$(call get_hdr_inc,__LINUX_RTNETLINK_H,rtnetlink.h) \ - $(call get_hdr_inc,_LINUX_IF_LINK_H,if_link.h) +CFLAGS_rt-link:=$(call get_hdr_inc,_LINUX_IF_LINK_H,if_link.h) \ + $(call get_hdr_inc,__LINUX_RTNETLINK_H,rtnetlink.h) \ + $(call get_hdr_inc,_LINUX_IP_H,ip.h) \ + $(call get_hdr_inc,_IPV6_H,ipv6.h) \ + $(call get_hdr_inc,_LINUX_IF_BRIDGE_H,if_bridge.h) \ + $(call get_hdr_inc,_IF_TUNNEL_H_,if_tunnel.h) \ + $(call get_hdr_inc,_LINUX_IF_VLAN_H_,if_vlan.h) \ + $(call get_hdr_inc,_LINUX_SNMP_H,snmp.h) \ + $(call get_hdr_inc,_LINUX_DPLL_H,dpll.h) CFLAGS_rt-neigh:=$(call get_hdr_inc,__LINUX_RTNETLINK_H,rtnetlink.h) \ $(call get_hdr_inc,__LINUX_NEIGHBOUR_H,neighbour.h) CFLAGS_rt-route:=$(call get_hdr_inc,__LINUX_RTNETLINK_H,rtnetlink.h)
Signature · 1 line
2.55.0
RFC PATCH 5/8 netlink: specs: rt-link: re-align IPv4 devconf
In reply to Asbjørn Sloth TønnesenThe YNL "ipv4-devconf" enum is misaligned with the IPV4_DEVCONF_* enum.
IPV4_DEVCONF_* starts at 1, ipv4-devconf starts at 0.
$ git grep -B3 -A1 IPV4_DEVCONF_FORWARDING include/
inc../uapi/linux/ip.h-/* index values for the variables in ipv4_devconf */
inc../uapi/linux/ip.h-enum
inc../uapi/linux/ip.h-{
inc../uapi/linux/ip.h: IPV4_DEVCONF_FORWARDING=1,
inc../uapi/linux/ip.h- IPV4_DEVCONF_MC_FORWARDING,Re-align the YNL spec by explicitly setting the first value to 1.
--- Documentation/netlink/specs/rt-link.yaml | 1 + 1 file changed, 1 insertion(+)
Documentation/netlink/specs/rt-link.yaml +1 -0
@@ -312,6 +312,7 @@ definitions: entries: - name: forwarding + value: 1 - name: mc-forwarding -
Signature · 1 line
2.55.0
RFC PATCH 6/8 netlink: specs: rt-link: re-align ifla-inet6-stats
In reply to Asbjørn Sloth TønnesenYNL enum ifla-inet6-stats got mis-aligned with C, as two commits reordered the enum.
In commit b4a11b2033b7 ("net: fix IPSTATS_MIB_OUTPKGS increment in OutForwDatagrams."), IPSTATS_MIB_OUTPKTS was renamed to _OUTREQUESTS and a new _OUTPKTS was as the last entry.
In commit 652e2c777862 ("net: reorganize IP MIB values (II)"), the IPSTATS_MIB_* enum was reorganized for data locality.
Neither of these commits updated the YNL spec.
This changed uAPI, as IPSTATS_MIB_* is used as indicies in an u64 array, exported as IFLA_INET6_STATS by inet6_fill_ifla6_stats_attrs().
This patch updates ifla-inet6-stats to reflect both of these commits. Next time we want to reorganize these counters we should properly apply mapping as to maintain the current order on the netlink side.
This pre-existing issue was identified by Sashiko during review of commit 2b0aecb7b2b1 ("netlink: specs: rt-link: add accept-ra-min-lft").
--- Documentation/netlink/specs/rt-link.yaml | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-)
Documentation/netlink/specs/rt-link.yaml +11 -9
@@ -541,11 +541,21 @@ definitions: - name: indelivers - - name: outforwdatagrams + name: noectpkts + - + name: ect1pkts + - + name: ect0pkts + - + name: cepkts + - + name: outrequests - name: outpkts - name: outoctets + - + name: outforwdatagrams - name: inhdrerrors -
@@ -596,14 +606,6 @@ definitions: name: outbcastoctets - name: csumerrors - - - name: noectpkts - - - name: ect1-pkts - - - name: ect0-pkts - - - name: cepkts - name: reasm-overlaps - name: br-boolopt-multi
Signature · 1 line
2.55.0
RFC PATCH 7/8 netlink: specs: rt-link: fix ifinfo-flags names
In reply to Asbjørn Sloth TønnesenCorrect the names of enum net_device_flags constants:
- IFF_POINTOPOINT should be "pointopoint", not "point-to-point".
- IFF_NOTRAILERS should be "notrailers", not "no-trailers".
- IFF_NOARP should be "noarp", not "no-arp".
- IFF_ALLMULTI should be "allmulti", not "all-multi".
- IFF_AUTOMEDIA should be "automedia", not "auto-media".
--- Documentation/netlink/specs/rt-link.yaml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
Documentation/netlink/specs/rt-link.yaml +5 -5
@@ -25,17 +25,17 @@ definitions: - name: loopback - - name: point-to-point + name: pointopoint - - name: no-trailers + name: notrailers - name: running - - name: no-arp + name: noarp - name: promisc - - name: all-multi + name: allmulti - name: master -
@@ -45,7 +45,7 @@ definitions: - name: portsel - - name: auto-media + name: automedia - name: dynamic -
Signature · 1 line
2.55.0
RFC PATCH 8/8 netlink: specs: rt-link: fix netkit-policy names
In reply to Asbjørn Sloth TønnesenCorrect the names of these enum netkit_action constants:
- NETKIT_PASS is 0, so "pass", not "forward".
- NETKIT_DROP is 2, so "drop", not "blackhole".
Note, the other enum members are not used in netlink, and are not allowed by netkit_check_policy().
--- Documentation/netlink/specs/rt-link.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/netlink/specs/rt-link.yaml +2 -2
@@ -827,10 +827,10 @@ definitions: name-prefix: netkit- entries: - - name: forward + name: pass value: 0 - - name: blackhole + name: drop value: 2 - name: netkit-mode
Signature · 1 line
2.55.0