Skip to content

Commit 2ce992a

Browse files
committed
Merge branch 'tools-ynl-add-schema-checking'
Donald Hunter says: ==================== tools: ynl: add schema checking Add schema checking and yaml linting for the YNL specs. Patch 1 adds a schema_check make target using a pyynl --validate option Patch 2 adds a lint make target using yamllint Patches 3,4 fix issues reported by make -C tools/net/ynl lint schema_check ==================== Link: https://patch.msgid.link/20251127123502.89142-1-donald.hunter@gmail.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2 parents 2c80116 + 1adc241 commit 2ce992a

5 files changed

Lines changed: 40 additions & 9 deletions

File tree

Documentation/netlink/specs/conntrack.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ attribute-sets:
457457
name: labels
458458
type: binary
459459
-
460-
name: labels mask
460+
name: labels-mask
461461
type: binary
462462
-
463463
name: synproxy

Documentation/netlink/specs/ethtool.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1269,7 +1269,7 @@ attribute-sets:
12691269
-
12701270
name: hist
12711271
type: nest
1272-
multi-attr: True
1272+
multi-attr: true
12731273
nested-attributes: fec-hist
12741274
-
12751275
name: fec

Documentation/netlink/specs/nftables.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ attribute-sets:
915915
type: string
916916
doc: Name of set to use
917917
-
918-
name: set id
918+
name: set-id
919919
type: u32
920920
byte-order: big-endian
921921
doc: ID of set to use

tools/net/ynl/Makefile

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ endif
1212
libdir ?= $(prefix)/$(libdir_relative)
1313
includedir ?= $(prefix)/include
1414

15+
SPECDIR=../../../Documentation/netlink/specs
16+
1517
SUBDIRS = lib generated samples ynltool tests
1618

1719
all: $(SUBDIRS) libynl.a
@@ -54,4 +56,22 @@ install: libynl.a lib/*.h
5456
run_tests:
5557
@$(MAKE) -C tests run_tests
5658

57-
.PHONY: all clean distclean install run_tests $(SUBDIRS)
59+
lint:
60+
yamllint $(SPECDIR)
61+
62+
schema_check:
63+
@N=1; \
64+
for spec in $(SPECDIR)/*.yaml ; do \
65+
NAME=$$(basename $$spec) ; \
66+
OUTPUT=$$(./pyynl/cli.py --spec $$spec --validate) ; \
67+
if [ $$? -eq 0 ] ; then \
68+
echo "ok $$N $$NAME schema validation" ; \
69+
else \
70+
echo "not ok $$N $$NAME schema validation" ; \
71+
echo "$$OUTPUT" ; \
72+
echo ; \
73+
fi ; \
74+
N=$$((N+1)) ; \
75+
done
76+
77+
.PHONY: all clean distclean install run_tests lint schema_check $(SUBDIRS)

tools/net/ynl/pyynl/cli.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import textwrap
1111

1212
sys.path.append(pathlib.Path(__file__).resolve().parent.as_posix())
13-
from lib import YnlFamily, Netlink, NlError
13+
from lib import YnlFamily, Netlink, NlError, SpecFamily
1414

1515
sys_schema_dir='/usr/share/ynl'
1616
relative_schema_dir='../../../../Documentation/netlink'
@@ -127,6 +127,7 @@ def main():
127127
group.add_argument('--list-msgs', action='store_true')
128128
group.add_argument('--list-attrs', dest='list_attrs', metavar='OPERATION', type=str,
129129
help='List attributes for an operation')
130+
group.add_argument('--validate', action='store_true')
130131

131132
parser.add_argument('--duration', dest='duration', type=int,
132133
help='when subscribed, watch for DURATION seconds')
@@ -168,15 +169,25 @@ def output(msg):
168169

169170
if args.family:
170171
spec = f"{spec_dir()}/{args.family}.yaml"
171-
if args.schema is None and spec.startswith(sys_schema_dir):
172-
args.schema = '' # disable schema validation when installed
173-
if args.process_unknown is None:
174-
args.process_unknown = True
175172
else:
176173
spec = args.spec
177174
if not os.path.isfile(spec):
178175
raise Exception(f"Spec file {spec} does not exist")
179176

177+
if args.validate:
178+
try:
179+
SpecFamily(spec, args.schema)
180+
except Exception as error:
181+
print(error)
182+
exit(1)
183+
return
184+
185+
if args.family: # set behaviour when using installed specs
186+
if args.schema is None and spec.startswith(sys_schema_dir):
187+
args.schema = '' # disable schema validation when installed
188+
if args.process_unknown is None:
189+
args.process_unknown = True
190+
180191
ynl = YnlFamily(spec, args.schema, args.process_unknown,
181192
recv_size=args.dbg_small_recv)
182193
if args.dbg_small_recv:

0 commit comments

Comments
 (0)