Skip to contentSkip to conversation

tools: ynl: turn the page-pool sample into a real tool

PATCH Superseded Jakub Kicinski 3 participants 11 messages Nov 4, 2025 – Nov 7, 2025

Subsystems (3)

v1 of 2 Superseded 1 of 5 applied Latest is v2 →

lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

cover tools: ynl: turn the page-pool sample into a real tool

The page-pool YNL sample is quite useful. It's helps calculate recycling rate and memory consumption. Since we still haven't figured out a way to integrate with iproute2 (not for the lack of thinking how to solve it) - create a ynltool command in ynl.

Add page-pool and qstats support.

Most commands can use the Python YNL CLI directly but low level stats often need aggregation or some math on top to be useful. Specifically in this patch set:
- page pool stats are aggregated and recycling rate computed
- per-queue stats are used to compute traffic balance across queues

Jakub Kicinski (5):
netlink: specs: netdev add missing stats to qstat-get
tools: ynltool: create skeleton for the C command
tools: ynltool: add page-pool stats
tools: ynltool: add qstats support
tools: ynltool: add traffic distribution balance

 Documentation/netlink/specs/netdev.yaml |  23 +
 tools/net/ynl/Makefile                  |   3 +-
 tools/net/ynl/ynltool/Makefile          |  49 ++
 tools/net/ynl/ynltool/json_writer.h     |  75 +++
 tools/net/ynl/ynltool/main.h            |  66 +++
 tools/net/ynl/samples/page-pool.c       | 149 ------
 tools/net/ynl/ynltool/json_writer.c     | 288 +++++++++++
 tools/net/ynl/ynltool/main.c            | 242 +++++++++
 tools/net/ynl/ynltool/page-pool.c       | 461 ++++++++++++++++++
 tools/net/ynl/ynltool/qstats.c          | 621 ++++++++++++++++++++++++
 tools/net/ynl/ynltool/.gitignore        |   1 +
 11 files changed, 1828 insertions(+), 150 deletions(-)
 create mode 100644 tools/net/ynl/ynltool/Makefile
 create mode 100644 tools/net/ynl/ynltool/json_writer.h
 create mode 100644 tools/net/ynl/ynltool/main.h
 delete mode 100644 tools/net/ynl/samples/page-pool.c
 create mode 100644 tools/net/ynl/ynltool/json_writer.c
 create mode 100644 tools/net/ynl/ynltool/main.c
 create mode 100644 tools/net/ynl/ynltool/page-pool.c
 create mode 100644 tools/net/ynl/ynltool/qstats.c
 create mode 100644 tools/net/ynl/ynltool/.gitignore
Signature · 1 line
2.51.1
author maintainer
#1 lore raw

PATCH 1/5 netlink: specs: netdev add missing stats to qstat-get

Add missing entries in C attribute list.

---
 Documentation/netlink/specs/netdev.yaml | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
Documentation/netlink/specs/netdev.yaml +23 -0
index 10c412b7433f..82bf5cb2617d 100644
@@ -733,6 +733,29 @@ doc: >-
             - rx-bytes
             - tx-packets
             - tx-bytes
+            - rx-alloc-fail
+            - rx-hw-drops
+            - rx-hw-drop-overruns
+            - rx-csum-complete
+            - rx-csum-unnecessary
+            - rx-csum-none
+            - rx-csum-bad
+            - rx-hw-gro-packets
+            - rx-hw-gro-bytes
+            - rx-hw-gro-wire-packets
+            - rx-hw-gro-wire-bytes
+            - rx-hw-drop-ratelimits
+            - tx-hw-drops
+            - tx-hw-drop-errors
+            - tx-csum-none
+            - tx-needs-csum
+            - tx-hw-gso-packets
+            - tx-hw-gso-bytes
+            - tx-hw-gso-wire-packets
+            - tx-hw-gso-wire-bytes
+            - tx-hw-drop-ratelimits
+            - tx-stop
+            - tx-wake
     -
       name: bind-rx
       doc: Bind dmabuf to netdev
Signature · 1 line
2.51.1
author maintainer
#2 lore raw

PATCH 2/5 tools: ynltool: create skeleton for the C command

In reply to Jakub Kicinski

Based on past discussions it seems like integration of YNL into iproute2 is unlikely. YNL itself is not great as a C library, since it has no backward compat (we routinely change types).

Most of the operations can be performed with the generic Python CLI directly. There is, however, a handful of operations where summarization of kernel output is very useful (mostly related to stats: page-pool, qstat).

Create a command (inspired by bpftool, I think it stood the test of time reasonably well) to be able to plug the subcommands into.

---
 tools/net/ynl/Makefile              |   3 +-
 tools/net/ynl/ynltool/Makefile      |  44 +++++
 tools/net/ynl/ynltool/json_writer.h |  75 ++++++++
 tools/net/ynl/ynltool/main.h        |  62 ++++++
 tools/net/ynl/ynltool/json_writer.c | 288 ++++++++++++++++++++++++++++
 tools/net/ynl/ynltool/main.c        | 240 +++++++++++++++++++++++
 tools/net/ynl/ynltool/.gitignore    |   1 +
 7 files changed, 712 insertions(+), 1 deletion(-)
 create mode 100644 tools/net/ynl/ynltool/Makefile
 create mode 100644 tools/net/ynl/ynltool/json_writer.h
 create mode 100644 tools/net/ynl/ynltool/main.h
 create mode 100644 tools/net/ynl/ynltool/json_writer.c
 create mode 100644 tools/net/ynl/ynltool/main.c
 create mode 100644 tools/net/ynl/ynltool/.gitignore
tools/net/ynl/Makefile +2 -1
index 211df5a93ad9..31ed20c0f3f8 100644
@@ -12,10 +12,11 @@ endif
 libdir  ?= $(prefix)/$(libdir_relative)
 includedir ?= $(prefix)/include
 
-SUBDIRS = lib generated samples
+SUBDIRS = lib generated samples ynltool
 
 all: $(SUBDIRS) libynl.a
 
+ynltool: | lib generated libynl.a
 samples: | lib generated
 libynl.a: | lib generated
 	@echo -e "\tAR $@"
tools/net/ynl/ynltool/Makefile new file +44 -0

44 lines not on this page · raw message

tools/net/ynl/ynltool/json_writer.h new file +75 -0

75 lines not on this page · raw message

tools/net/ynl/ynltool/main.h new file +62 -0

62 lines not on this page · raw message

tools/net/ynl/ynltool/json_writer.c new file +288 -0

288 lines not on this page · raw message

tools/net/ynl/ynltool/main.c new file +240 -0

240 lines not on this page · raw message

tools/net/ynl/ynltool/.gitignore new file +1 -0
new file mode 100644
index 000000000000..f38848dbb0d3
@@ -0,0 +1 @@
+ynltool
Signature · 1 line
2.51.1
author maintainer
#5 lore raw

PATCH 3/5 tools: ynltool: add page-pool stats

In reply to Jakub Kicinski

Replace the page-pool sample with page pool support in ynltool.

 # ynltool page-pool stats
    eth0[2]	page pools: 18 (zombies: 0)
		refs: 171456 bytes: 702283776 (refs: 0 bytes: 0)
		recycling: 97.3% (alloc: 2679:6134966 recycle: 1250981:4719386)
 # ynltool -j page-pool stats | jq
 [
  {
    "ifname": "eth0",
    "ifindex": 2,
    "page_pools": 18,
    "zombies": 0,
    "live": {
      "refs": 171456,
      "bytes": 702283776
    },
    "zombie": {
      "refs": 0,
      "bytes": 0
    },
    "recycling_pct": 97.2746,
    "alloc": {
      "slow": 2679,
      "fast": 6135029
    },
    "recycle": {
      "ring": 1250997,
      "cache": 4719432
    }
  }
 ]
 # ynltool page-pool stats group-by pp
 pool id: 108  dev: eth0[2]  napi: 530
   inflight: 9472 pages 38797312 bytes
   recycling: 95.5% (alloc: 148:208379 recycle: 45386:153842)
 pool id: 107  dev: eth0[2]  napi: 529
   inflight: 9408 pages 38535168 bytes
   recycling: 94.9% (alloc: 147:180178 recycle: 42251:128808)
---
 tools/net/ynl/ynltool/Makefile    |  11 +-
 tools/net/ynl/ynltool/main.h      |   3 +
 tools/net/ynl/samples/page-pool.c | 149 ----------
 tools/net/ynl/ynltool/main.c      |   3 +-
 tools/net/ynl/ynltool/page-pool.c | 461 ++++++++++++++++++++++++++++++
 5 files changed, 474 insertions(+), 153 deletions(-)
 delete mode 100644 tools/net/ynl/samples/page-pool.c
 create mode 100644 tools/net/ynl/ynltool/page-pool.c
tools/net/ynl/ynltool/Makefile +8 -3
index ce27dc691ffe..1e860c63df66 100644
@@ -10,25 +10,30 @@ CFLAGS := -Wall -Wextra -Werror -O2
 ifeq ("$(DEBUG)","1")
   CFLAGS += -g -fsanitize=address -fsanitize=leak -static-libasan
 endif
-CFLAGS += -I../lib
+CFLAGS += -I../lib -I../generated -I../../../include/uapi/
 
 SRCS := $(wildcard *.c)
 OBJS := $(patsubst %.c,$(OUTPUT)%.o,$(SRCS))
 
 YNLTOOL := $(OUTPUT)ynltool
+LIBS := ../lib/ynl.a ../generated/netdev-user.o
 
 include $(wildcard *.d)
 
 all: $(YNLTOOL)
 
-$(YNLTOOL): $(OBJS)
+$(YNLTOOL): $(OBJS) $(LIBS)
 	@echo -e "\tLINK $@"
-	@$(CC) $(CFLAGS) -o $@ $(OBJS)
+	@$(CC) $(CFLAGS) -o $@ $(OBJS) $(LIBS) -lmnl
 
 %.o: %.c main.h json_writer.h
 	@echo -e "\tCC $@"
 	@$(COMPILE.c) -MMD -c -o $@ $<
 
+../generated/netdev-user.o: ../generated/netdev-user.c
+	@echo -e "\tCC $@"
+	@$(CC) $(filter-out -Werror -fsanitize=address -fsanitize=leak -static-libasan,$(CFLAGS)) -MMD -c -o $@ $<
+
 clean:
 	rm -f *.o *.d *~
 
tools/net/ynl/ynltool/main.h +3 -0
index f4a70acf2085..fd05d21451a2 100644
@@ -59,4 +59,7 @@ struct cmd {
 int cmd_select(const struct cmd *cmds, int argc, char **argv,
 	       int (*help)(int argc, char **argv));
 
+/* subcommands */
+int do_page_pool(int argc, char **argv);
+
 #endif /* __YNLTOOL_H */
tools/net/ynl/samples/page-pool.c deleted +0 -149

149 lines not on this page · raw message

tools/net/ynl/ynltool/main.c +2 -1
index c5047fad50cf..ba7420e2a7d5 100644
@@ -47,7 +47,7 @@ static int do_help(int argc __attribute__((unused)),
 		"Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
 		"       %s version\n"
 		"\n"
-		"       OBJECT := { }\n"
+		"       OBJECT := { page-pool }\n"
 		"       " HELP_SPEC_OPTIONS "\n"
 		"",
 		bin_name, bin_name);
@@ -71,6 +71,7 @@ static int do_version(int argc __attribute__((unused)),
 
 static const struct cmd commands[] = {
 	{ "help",	do_help },
+	{ "page-pool",	do_page_pool },
 	{ "version",	do_version },
 	{ 0 }
 };
tools/net/ynl/ynltool/page-pool.c new file +461 -0

461 lines not on this page · raw message

Signature · 1 line
2.51.1
author maintainer
#6 lore raw

PATCH 4/5 tools: ynltool: add qstats support

In reply to Jakub Kicinski
  $ ynltool qstat
  eth0        rx-packets:       493192163        rx-bytes:   1442544543997
              tx-packets:       745999838        tx-bytes:   4574215826482
                 tx-stop:            7033         tx-wake:            7033
  $ ynltool qstat show group-by queue
  eth0  rx-0     packets:        70196880           bytes:    178633973750
  eth0  rx-1     packets:        63623419           bytes:    197274745250
  ...
  eth0  tx-1     packets:        98645810           bytes:    631247647938
                    stop:            1048            wake:            1048
  eth0  tx-2     packets:        86775824           bytes:    563930471952
                    stop:            1126            wake:            1126
  ...
  $ ynltool -j qstat  | jq
  [
   {
    "ifname": "eth0",
    "ifindex": 2,
    "rx": {
      "packets": 493396439,
      "bytes": 1443608198921
    },
    "tx": {
      "packets": 746239978,
      "bytes": 4574333772645,
      "stop": 7072,
      "wake": 7072
    }
   }
  ]
---
 tools/net/ynl/ynltool/main.h   |   1 +
 tools/net/ynl/ynltool/main.c   |   3 +-
 tools/net/ynl/ynltool/qstats.c | 330 +++++++++++++++++++++++++++++++++
 3 files changed, 333 insertions(+), 1 deletion(-)
 create mode 100644 tools/net/ynl/ynltool/qstats.c
tools/net/ynl/ynltool/main.h +1 -0
index fd05d21451a2..c7039f9ac55a 100644
@@ -61,5 +61,6 @@ int cmd_select(const struct cmd *cmds, int argc, char **argv,
 
 /* subcommands */
 int do_page_pool(int argc, char **argv);
+int do_qstats(int argc, char **argv);
 
 #endif /* __YNLTOOL_H */
tools/net/ynl/ynltool/main.c +2 -1
index ba7420e2a7d5..1a69f0ae1647 100644
@@ -47,7 +47,7 @@ static int do_help(int argc __attribute__((unused)),
 		"Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
 		"       %s version\n"
 		"\n"
-		"       OBJECT := { page-pool }\n"
+		"       OBJECT := { page-pool | qstats }\n"
 		"       " HELP_SPEC_OPTIONS "\n"
 		"",
 		bin_name, bin_name);
@@ -72,6 +72,7 @@ static int do_version(int argc __attribute__((unused)),
 static const struct cmd commands[] = {
 	{ "help",	do_help },
 	{ "page-pool",	do_page_pool },
+	{ "qstats",	do_qstats },
 	{ "version",	do_version },
 	{ 0 }
 };
tools/net/ynl/ynltool/qstats.c new file +330 -0

330 lines not on this page · raw message

Signature · 1 line
2.51.1
author maintainer
#7 lore raw

PATCH 5/5 tools: ynltool: add traffic distribution balance

In reply to Jakub Kicinski

The main if not only use case for per-queue stats today is checking for traffic imbalance. Add simple traffic balance analysis to qstats.

 $ ynltool qstat balance
 eth0 rx 44 queues:
  rx-packets  : cv=6.9% ns=24.2% stddev=512006493
                min=6278921110 max=8011570575 mean=7437054644
  rx-bytes    : cv=6.9% ns=24.1% stddev=759670503060
                min=9326315769440 max=11884393670786 mean=11035439201354
  ...
  $ ynltool -j qstat balance | jq
  [
   {
    "ifname": "eth0",
    "ifindex": 2,
    "queue-type": "rx",
    "rx-packets": {
      "queue-count": 44,
      "min": 6278301665,
      "max": 8010780185,
      "mean": 7.43635E+9,
      "stddev": 5.12012E+8,
      "coefficient-of-variation": 6.88525,
      "normalized-spread": 24.249
    },
   ...
---
 tools/net/ynl/ynltool/Makefile |   2 +-
 tools/net/ynl/ynltool/qstats.c | 293 ++++++++++++++++++++++++++++++++-
 2 files changed, 293 insertions(+), 2 deletions(-)
tools/net/ynl/ynltool/Makefile +1 -1
index 1e860c63df66..2fe520f54ebb 100644
@@ -24,7 +24,7 @@ all: $(YNLTOOL)
 
 $(YNLTOOL): $(OBJS) $(LIBS)
 	@echo -e "\tLINK $@"
-	@$(CC) $(CFLAGS) -o $@ $(OBJS) $(LIBS) -lmnl
+	@$(CC) $(CFLAGS) -o $@ $(OBJS) $(LIBS) -lmnl -lm
 
 %.o: %.c main.h json_writer.h
 	@echo -e "\tCC $@"
tools/net/ynl/ynltool/qstats.c +292 -1

330 lines not on this page · raw message

Signature · 1 line
2.51.1
author maintainer
#8 lore raw
In reply to Jakub Kicinski
Quoted 5 lines · On Tue, 4 Nov 2025 15:23:43 -0800 Jakub Kicinski wrote:

The page-pool YNL sample is quite useful. It's helps calculate recycling rate and memory consumption. Since we still haven't figured out a way to integrate with iproute2 (not for the lack of thinking how to solve it) - create a ynltool command in ynl.

Quoted 7 lines

Add page-pool and qstats support.

Most commands can use the Python YNL CLI directly but low level stats often need aggregation or some math on top to be useful. Specifically in this patch set:
- page pool stats are aggregated and recycling rate computed
- per-queue stats are used to compute traffic balance across queues

FWIW I'd appreciate any feedback here. It's hard to draw the line on what to implement in a tool like ynltool and what to leave for the YNL Python directly.

#9 lore raw
Quoted 14 lines · Jakub Kicinski
Quoted 5 lines · On Tue, 4 Nov 2025 15:23:43 -0800 Jakub Kicinski wrote:

The page-pool YNL sample is quite useful. It's helps calculate recycling rate and memory consumption. Since we still haven't figured out a way to integrate with iproute2 (not for the lack of thinking how to solve it) - create a ynltool command in ynl.

Quoted 7 lines

Add page-pool and qstats support.

Most commands can use the Python YNL CLI directly but low level stats often need aggregation or some math on top to be useful. Specifically in this patch set:
- page pool stats are aggregated and recycling rate computed
- per-queue stats are used to compute traffic balance across queues

Quoted 3 lines

FWIW I'd appreciate any feedback here. It's hard to draw the line on what to implement in a tool like ynltool and what to leave for the YNL Python directly.

I like the idea. Python is useful for local development, but shipping it (as of now) is a bit painful. I'm not sure what's the line gonna be between iproute2 vs ethtool vs ynltool, but I think we can figure it out as we go.

#3 lore raw

Re: [PATCH net-next 2/5] tools: ynltool: create skeleton for the C command

In reply to Jakub Kicinski
Quoted 600 lines · Jakub Kicinski

Based on past discussions it seems like integration of YNL into iproute2 is unlikely. YNL itself is not great as a C library, since it has no backward compat (we routinely change types).

Most of the operations can be performed with the generic Python CLI directly. There is, however, a handful of operations where summarization of kernel output is very useful (mostly related to stats: page-pool, qstat).

Create a command (inspired by bpftool, I think it stood the test of time reasonably well) to be able to plug the subcommands into.

---
 tools/net/ynl/Makefile              |   3 +-
 tools/net/ynl/ynltool/Makefile      |  44 +++++
 tools/net/ynl/ynltool/json_writer.h |  75 ++++++++
 tools/net/ynl/ynltool/main.h        |  62 ++++++
 tools/net/ynl/ynltool/json_writer.c | 288 ++++++++++++++++++++++++++++
 tools/net/ynl/ynltool/main.c        | 240 +++++++++++++++++++++++
 tools/net/ynl/ynltool/.gitignore    |   1 +
 7 files changed, 712 insertions(+), 1 deletion(-)
 create mode 100644 tools/net/ynl/ynltool/Makefile
 create mode 100644 tools/net/ynl/ynltool/json_writer.h
 create mode 100644 tools/net/ynl/ynltool/main.h
 create mode 100644 tools/net/ynl/ynltool/json_writer.c
 create mode 100644 tools/net/ynl/ynltool/main.c
 create mode 100644 tools/net/ynl/ynltool/.gitignore
tools/net/ynl/Makefile +2 -1
index 211df5a93ad9..31ed20c0f3f8 100644
@@ -12,10 +12,11 @@ endif
 libdir  ?= $(prefix)/$(libdir_relative)
 includedir ?= $(prefix)/include
 
-SUBDIRS = lib generated samples
+SUBDIRS = lib generated samples ynltool
 
 all: $(SUBDIRS) libynl.a
 
+ynltool: | lib generated libynl.a
 samples: | lib generated
 libynl.a: | lib generated
 	@echo -e "\tAR $@"
tools/net/ynl/ynltool/Makefile new file +44 -0
new file mode 100644
index 000000000000..ce27dc691ffe
@@ -0,0 +1,44 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+include ../Makefile.deps
+
+INSTALL	?= install
+prefix  ?= /usr
+
+CC := gcc
+CFLAGS := -Wall -Wextra -Werror -O2
+ifeq ("$(DEBUG)","1")
+  CFLAGS += -g -fsanitize=address -fsanitize=leak -static-libasan
+endif
+CFLAGS += -I../lib
+
+SRCS := $(wildcard *.c)
+OBJS := $(patsubst %.c,$(OUTPUT)%.o,$(SRCS))
+
+YNLTOOL := $(OUTPUT)ynltool
+
+include $(wildcard *.d)
+
+all: $(YNLTOOL)
+
+$(YNLTOOL): $(OBJS)
+	@echo -e "\tLINK $@"
+	@$(CC) $(CFLAGS) -o $@ $(OBJS)
+
+%.o: %.c main.h json_writer.h
+	@echo -e "\tCC $@"
+	@$(COMPILE.c) -MMD -c -o $@ $<
+
+clean:
+	rm -f *.o *.d *~
+
+distclean: clean
+	rm -f $(YNLTOOL)
+
+bindir ?= /usr/bin
+
+install: $(YNLTOOL)
+	install -m 0755 $(YNLTOOL) $(DESTDIR)$(bindir)/$(YNLTOOL)
+
+.PHONY: all clean distclean
+.DEFAULT_GOAL=all
tools/net/ynl/ynltool/json_writer.h new file +75 -0
new file mode 100644
index 000000000000..0f1e63c88f6a
@@ -0,0 +1,75 @@
+/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
+/*
+ * Simple streaming JSON writer
+ *
+ * This takes care of the annoying bits of JSON syntax like the commas
+ * after elements
+ *
+ * Authors:	Stephen Hemminger <[email protected]>
+ */
+
+#ifndef _JSON_WRITER_H_
+#define _JSON_WRITER_H_
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdarg.h>
+#include <stdio.h>
+
+/* Opaque class structure */
+typedef struct json_writer json_writer_t;
+
+/* Create a new JSON stream */
+json_writer_t *jsonw_new(FILE *f);
+/* End output to JSON stream */
+void jsonw_destroy(json_writer_t **self_p);
+
+/* Cause output to have pretty whitespace */
+void jsonw_pretty(json_writer_t *self, bool on);
+
+/* Reset separator to create new JSON */
+void jsonw_reset(json_writer_t *self);
+
+/* Add property name */
+void jsonw_name(json_writer_t *self, const char *name);
+
+/* Add value  */
+void __attribute__((format(printf, 2, 0))) jsonw_vprintf_enquote(json_writer_t *self,
+								 const char *fmt,
+								 va_list ap);
+void __attribute__((format(printf, 2, 3))) jsonw_printf(json_writer_t *self,
+							const char *fmt, ...);
+void jsonw_string(json_writer_t *self, const char *value);
+void jsonw_bool(json_writer_t *self, bool value);
+void jsonw_float(json_writer_t *self, double number);
+void jsonw_float_fmt(json_writer_t *self, const char *fmt, double num);
+void jsonw_uint(json_writer_t *self, uint64_t number);
+void jsonw_hu(json_writer_t *self, unsigned short number);
+void jsonw_int(json_writer_t *self, int64_t number);
+void jsonw_null(json_writer_t *self);
+void jsonw_lluint(json_writer_t *self, unsigned long long int num);
+
+/* Useful Combinations of name and value */
+void jsonw_string_field(json_writer_t *self, const char *prop, const char *val);
+void jsonw_bool_field(json_writer_t *self, const char *prop, bool value);
+void jsonw_float_field(json_writer_t *self, const char *prop, double num);
+void jsonw_uint_field(json_writer_t *self, const char *prop, uint64_t num);
+void jsonw_hu_field(json_writer_t *self, const char *prop, unsigned short num);
+void jsonw_int_field(json_writer_t *self, const char *prop, int64_t num);
+void jsonw_null_field(json_writer_t *self, const char *prop);
+void jsonw_lluint_field(json_writer_t *self, const char *prop,
+			unsigned long long int num);
+void jsonw_float_field_fmt(json_writer_t *self, const char *prop,
+			   const char *fmt, double val);
+
+/* Collections */
+void jsonw_start_object(json_writer_t *self);
+void jsonw_end_object(json_writer_t *self);
+
+void jsonw_start_array(json_writer_t *self);
+void jsonw_end_array(json_writer_t *self);
+
+/* Override default exception handling */
+typedef void (jsonw_err_handler_fn)(const char *);
+
+#endif /* _JSON_WRITER_H_ */
tools/net/ynl/ynltool/main.h new file +62 -0
new file mode 100644
index 000000000000..f4a70acf2085
@@ -0,0 +1,62 @@
+/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
+/* Copyright (C) 2017-2018 Netronome Systems, Inc. */
+/* Copyright Meta Platforms, Inc. and affiliates */
+
+#ifndef __YNLTOOL_H
+#define __YNLTOOL_H
+
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+
+#include "json_writer.h"
+
+#define NEXT_ARG()	({ argc--; argv++; if (argc < 0) usage(); })
+#define NEXT_ARGP()	({ (*argc)--; (*argv)++; if (*argc < 0) usage(); })
+#define BAD_ARG()	({ p_err("what is '%s'?", *argv); -1; })
+#define GET_ARG()	({ argc--; *argv++; })
+#define REQ_ARGS(cnt)							\
+	({								\
+		int _cnt = (cnt);					\
+		bool _res;						\
+									\
+		if (argc < _cnt) {					\
+			p_err("'%s' needs at least %d arguments, %d found", \
+			      argv[-1], _cnt, argc);			\
+			_res = false;					\
+		} else {						\
+			_res = true;					\
+		}							\
+		_res;							\
+	})
+
+#define HELP_SPEC_OPTIONS						\
+	"OPTIONS := { {-j|--json} [{-p|--pretty}] }"
+
+extern const char *bin_name;
+
+extern json_writer_t *json_wtr;
+extern bool json_output;
+extern bool pretty_output;
+
+void __attribute__((format(printf, 1, 2))) p_err(const char *fmt, ...);
+void __attribute__((format(printf, 1, 2))) p_info(const char *fmt, ...);
+
+bool is_prefix(const char *pfx, const char *str);
+int detect_common_prefix(const char *arg, ...);
+void usage(void) __attribute__((noreturn));
+
+struct cmd {
+	const char *cmd;
+	int (*func)(int argc, char **argv);
+};
+
+int cmd_select(const struct cmd *cmds, int argc, char **argv,
+	       int (*help)(int argc, char **argv));
+
+#endif /* __YNLTOOL_H */
tools/net/ynl/ynltool/json_writer.c new file +288 -0
new file mode 100644
index 000000000000..c8685e592cd3
@@ -0,0 +1,288 @@
+// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
+/*
+ * Simple streaming JSON writer
+ *
+ * This takes care of the annoying bits of JSON syntax like the commas
+ * after elements
+ *
+ * Authors:	Stephen Hemminger <[email protected]>
+ */
+
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdarg.h>
+#include <assert.h>
+#include <malloc.h>
+#include <inttypes.h>
+#include <stdint.h>
+
+#include "json_writer.h"
+
+struct json_writer {
+	FILE		*out;
+	unsigned	depth;
+	bool		pretty;
+	char		sep;
+};
+
+static void jsonw_indent(json_writer_t *self)
+{
+	unsigned i;
+	for (i = 0; i < self->depth; ++i)
+		fputs("    ", self->out);
+}
+
+static void jsonw_eol(json_writer_t *self)
+{
+	if (!self->pretty)
+		return;
+
+	putc('\n', self->out);
+	jsonw_indent(self);
+}
+
+static void jsonw_eor(json_writer_t *self)
+{
+	if (self->sep != '\0')
+		putc(self->sep, self->out);
+	self->sep = ',';
+}
+
+static void jsonw_puts(json_writer_t *self, const char *str)
+{
+	putc('"', self->out);
+	for (; *str; ++str)
+		switch (*str) {
+		case '\t':
+			fputs("\\t", self->out);
+			break;
+		case '\n':
+			fputs("\\n", self->out);
+			break;
+		case '\r':
+			fputs("\\r", self->out);
+			break;
+		case '\f':
+			fputs("\\f", self->out);
+			break;
+		case '\b':
+			fputs("\\b", self->out);
+			break;
+		case '\\':
+			fputs("\\\\", self->out);
+			break;
+		case '"':
+			fputs("\\\"", self->out);
+			break;
+		default:
+			putc(*str, self->out);
+		}
+	putc('"', self->out);
+}
+
+json_writer_t *jsonw_new(FILE *f)
+{
+	json_writer_t *self = malloc(sizeof(*self));
+	if (self) {
+		self->out = f;
+		self->depth = 0;
+		self->pretty = false;
+		self->sep = '\0';
+	}
+	return self;
+}
+
+void jsonw_destroy(json_writer_t **self_p)
+{
+	json_writer_t *self = *self_p;
+
+	assert(self->depth == 0);
+	fputs("\n", self->out);
+	fflush(self->out);
+	free(self);
+	*self_p = NULL;
+}
+
+void jsonw_pretty(json_writer_t *self, bool on)
+{
+	self->pretty = on;
+}
+
+void jsonw_reset(json_writer_t *self)
+{
+	assert(self->depth == 0);
+	self->sep = '\0';
+}
+
+static void jsonw_begin(json_writer_t *self, int c)
+{
+	jsonw_eor(self);
+	putc(c, self->out);
+	++self->depth;
+	self->sep = '\0';
+}
+
+static void jsonw_end(json_writer_t *self, int c)
+{
+	assert(self->depth > 0);
+
+	--self->depth;
+	if (self->sep != '\0')
+		jsonw_eol(self);
+	putc(c, self->out);
+	self->sep = ',';
+}
+
+void jsonw_name(json_writer_t *self, const char *name)
+{
+	jsonw_eor(self);
+	jsonw_eol(self);
+	self->sep = '\0';
+	jsonw_puts(self, name);
+	putc(':', self->out);
+	if (self->pretty)
+		putc(' ', self->out);
+}
+
+void jsonw_vprintf_enquote(json_writer_t *self, const char *fmt, va_list ap)
+{
+	jsonw_eor(self);
+	putc('"', self->out);
+	vfprintf(self->out, fmt, ap);
+	putc('"', self->out);
+}
+
+void jsonw_printf(json_writer_t *self, const char *fmt, ...)
+{
+	va_list ap;
+
+	va_start(ap, fmt);
+	jsonw_eor(self);
+	vfprintf(self->out, fmt, ap);
+	va_end(ap);
+}
+
+void jsonw_start_object(json_writer_t *self)
+{
+	jsonw_begin(self, '{');
+}
+
+void jsonw_end_object(json_writer_t *self)
+{
+	jsonw_end(self, '}');
+}
+
+void jsonw_start_array(json_writer_t *self)
+{
+	jsonw_begin(self, '[');
+}
+
+void jsonw_end_array(json_writer_t *self)
+{
+	jsonw_end(self, ']');
+}
+
+void jsonw_string(json_writer_t *self, const char *value)
+{
+	jsonw_eor(self);
+	jsonw_puts(self, value);
+}
+
+void jsonw_bool(json_writer_t *self, bool val)
+{
+	jsonw_printf(self, "%s", val ? "true" : "false");
+}
+
+void jsonw_null(json_writer_t *self)
+{
+	jsonw_printf(self, "null");
+}
+
+void jsonw_float_fmt(json_writer_t *self, const char *fmt, double num)
+{
+	jsonw_printf(self, fmt, num);
+}
+
+void jsonw_float(json_writer_t *self, double num)
+{
+	jsonw_printf(self, "%g", num);
+}
+
+void jsonw_hu(json_writer_t *self, unsigned short num)
+{
+	jsonw_printf(self, "%hu", num);
+}
+
+void jsonw_uint(json_writer_t *self, uint64_t num)
+{
+	jsonw_printf(self, "%"PRIu64, num);
+}
+
+void jsonw_lluint(json_writer_t *self, unsigned long long int num)
+{
+	jsonw_printf(self, "%llu", num);
+}
+
+void jsonw_int(json_writer_t *self, int64_t num)
+{
+	jsonw_printf(self, "%"PRId64, num);
+}
+
+void jsonw_string_field(json_writer_t *self, const char *prop, const char *val)
+{
+	jsonw_name(self, prop);
+	jsonw_string(self, val);
+}
+
+void jsonw_bool_field(json_writer_t *self, const char *prop, bool val)
+{
+	jsonw_name(self, prop);
+	jsonw_bool(self, val);
+}
+
+void jsonw_float_field(json_writer_t *self, const char *prop, double val)
+{
+	jsonw_name(self, prop);
+	jsonw_float(self, val);
+}
+
+void jsonw_float_field_fmt(json_writer_t *self,
+			   const char *prop,
+			   const char *fmt,
+			   double val)
+{
+	jsonw_name(self, prop);
+	jsonw_float_fmt(self, fmt, val);
+}
+
+void jsonw_uint_field(json_writer_t *self, const char *prop, uint64_t num)
+{
+	jsonw_name(self, prop);
+	jsonw_uint(self, num);
+}
+
+void jsonw_hu_field(json_writer_t *self, const char *prop, unsigned short num)
+{
+	jsonw_name(self, prop);
+	jsonw_hu(self, num);
+}
+
+void jsonw_lluint_field(json_writer_t *self,
+			const char *prop,
+			unsigned long long int num)
+{
+	jsonw_name(self, prop);
+	jsonw_lluint(self, num);
+}
+
+void jsonw_int_field(json_writer_t *self, const char *prop, int64_t num)
+{
+	jsonw_name(self, prop);
+	jsonw_int(self, num);
+}
+
+void jsonw_null_field(json_writer_t *self, const char *prop)
+{
+	jsonw_name(self, prop);
+	jsonw_null(self);
+}
tools/net/ynl/ynltool/main.c new file +54 -0
new file mode 100644
index 000000000000..c5047fad50cf
@@ -0,0 +1,240 @@
+// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+/* Copyright (C) 2017-2018 Netronome Systems, Inc. */
+/* Copyright Meta Platforms, Inc. and affiliates */
+
+#include <ctype.h>
+#include <errno.h>
+#include <getopt.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdarg.h>
+
+#include "main.h"
+
+const char *bin_name;
+static int last_argc;
+static char **last_argv;
+static int (*last_do_help)(int argc, char **argv);
+json_writer_t *json_wtr;
+bool pretty_output;
+bool json_output;
+
+static void __attribute__((noreturn)) clean_and_exit(int i)
+{
+	if (json_output)
+		jsonw_destroy(&json_wtr);
+
+	exit(i);
+}
+
+void usage(void)
+{
+	last_do_help(last_argc - 1, last_argv + 1);
+
+	clean_and_exit(-1);
+}
+
+static int do_help(int argc __attribute__((unused)),
+		   char **argv __attribute__((unused)))
+{
+	if (json_output) {
+		jsonw_null(json_wtr);
+		return 0;
+	}
+
+	fprintf(stderr,
+		"Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
+		"       %s version\n"
+		"\n"
+		"       OBJECT := { }\n"
+		"       " HELP_SPEC_OPTIONS "\n"
+		"",
+		bin_name, bin_name);
+

tools/net/ynl/ynltool/main.c line 64

Quoted 10 lines
tools/net/ynl/ynltool/main.c new file +10 -0
+	return 0;
+}
+
+static int do_version(int argc __attribute__((unused)),
+		      char **argv __attribute__((unused)))
+{
+	if (json_output) {
+		jsonw_start_object(json_wtr);
+		jsonw_name(json_wtr, "version");
+		jsonw_printf(json_wtr, "\"0.1.0\"");

Any reason not to start with something like commit 4bfe3bd3cc35 ("tools/bpftool: use version from the kernel source tree") here? Otherwise I doubt 0.1.0 will be changed ever.

author maintainer
#4 lore raw

Re: [PATCH net-next 2/5] tools: ynltool: create skeleton for the C command

Quoted 11 lines · On Thu, 6 Nov 2025 08:37:07 -0800 Stanislav Fomichev wrote:

tools/net/ynl/ynltool/main.c line 64

Quoted 7 lines
tools/net/ynl/ynltool/main.c new file +7 -0
+static int do_version(int argc __attribute__((unused)),
+		      char **argv __attribute__((unused)))
+{
+	if (json_output) {
+		jsonw_start_object(json_wtr);
+		jsonw_name(json_wtr, "version");
+		jsonw_printf(json_wtr, "\"0.1.0\"");  

Any reason not to start with something like commit 4bfe3bd3cc35 ("tools/bpftool: use version from the kernel source tree") here? Otherwise I doubt 0.1.0 will be changed ever.

Ah, thanks a lot for the pointer! I grepped bpftool for kernelversion but somehow it eluded me..

Signature · 1 line
pw-bot: cr
#10 lore raw
In reply to Jakub Kicinski
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 8 lines · On Tue, 4 Nov 2025 15:23:43 -0800 you wrote:

The page-pool YNL sample is quite useful. It's helps calculate recycling rate and memory consumption. Since we still haven't figured out a way to integrate with iproute2 (not for the lack of thinking how to solve it) - create a ynltool command in ynl.

Add page-pool and qstats support.

[...]

Here is the summary with links:
- [net-next,1/5] netlink: specs: netdev add missing stats to qstat-get

    https://git.kernel.org/netdev/net-next/c/c6934c4e049c

- [net-next,2/5] tools: ynltool: create skeleton for the C command (no matching commit)
- [net-next,3/5] tools: ynltool: add page-pool stats (no matching commit)
- [net-next,4/5] tools: ynltool: add qstats support
(no matching commit)
- [net-next,5/5] tools: ynltool: add traffic distribution balance (no matching commit)

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html