From ee0202e2e731d074639461b3db2296bf44d847ce Mon Sep 17 00:00:00 2001 From: Jakub Kicinski Date: Mon, 5 Jun 2023 12:01:08 -0700 Subject: tools: ynl: add sample for netdev Add a sample application using the C library. My main goal is to make writing selftests easier but until I have some of those ready I think it's useful to show off the functionality and let people poke and tinker. Sample outputs - dump: $ ./netdev Select ifc ($ifindex; or 0 = dump; or -2 ntf check): 0 lo[1] 0: enp1s0[2] 23: basic redirect rx-sg Notifications (watching veth pair getting added and deleted): $ ./netdev Select ifc ($ifindex; or 0 = dump; or -2 ntf check): -2 [53] 0: (ntf: dev-add-ntf) [54] 0: (ntf: dev-add-ntf) [54] 23: basic redirect rx-sg (ntf: dev-change-ntf) [53] 23: basic redirect rx-sg (ntf: dev-change-ntf) [53] 23: basic redirect rx-sg (ntf: dev-del-ntf) [54] 23: basic redirect rx-sg (ntf: dev-del-ntf) Reviewed-by: Willem de Bruijn Signed-off-by: Jakub Kicinski --- tools/net/ynl/samples/.gitignore | 1 + tools/net/ynl/samples/Makefile | 28 ++++++++++ tools/net/ynl/samples/netdev.c | 108 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 tools/net/ynl/samples/.gitignore create mode 100644 tools/net/ynl/samples/Makefile create mode 100644 tools/net/ynl/samples/netdev.c (limited to 'tools/net/ynl/samples') diff --git a/tools/net/ynl/samples/.gitignore b/tools/net/ynl/samples/.gitignore new file mode 100644 index 000000000000..7b1f5179cb54 --- /dev/null +++ b/tools/net/ynl/samples/.gitignore @@ -0,0 +1 @@ +netdev diff --git a/tools/net/ynl/samples/Makefile b/tools/net/ynl/samples/Makefile new file mode 100644 index 000000000000..714316cad45f --- /dev/null +++ b/tools/net/ynl/samples/Makefile @@ -0,0 +1,28 @@ +# SPDX-License-Identifier: GPL-2.0 + +CC=gcc +CFLAGS=-std=gnu11 -O2 -W -Wall -Wextra -Wno-unused-parameter -Wshadow \ + -I../lib/ -I../generated/ +ifeq ("$(DEBUG)","1") + CFLAGS += -g -fsanitize=address -fsanitize=leak -static-libasan +endif + +LDLIBS=-lmnl ../lib/ynl.a ../generated/protos.a + +SRCS=$(wildcard *.c) +BINS=$(patsubst %.c,%,${SRCS}) + +include $(wildcard *.d) + +all: $(BINS) + +$(BINS): ../lib/ynl.a ../generated/protos.a + +clean: + rm -f *.o *.d *~ + +hardclean: clean + rm -f $(BINS) + +.PHONY: all clean +.DEFAULT_GOAL=all diff --git a/tools/net/ynl/samples/netdev.c b/tools/net/ynl/samples/netdev.c new file mode 100644 index 000000000000..d31268aa47c5 --- /dev/null +++ b/tools/net/ynl/samples/netdev.c @@ -0,0 +1,108 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include + +#include + +#include + +#include "netdev-user.h" + +/* netdev genetlink family code sample + * This sample shows off basics of the netdev family but also notification + * handling, hence the somewhat odd UI. We subscribe to notifications first + * then wait for ifc selection, so the socket may already accumulate + * notifications as we wait. This allows us to test that YNL can handle + * requests and notifications getting interleaved. + */ + +static void netdev_print_device(struct netdev_dev_get_rsp *d, unsigned int op) +{ + char ifname[IF_NAMESIZE]; + const char *name; + + if (!d->_present.ifindex) + return; + + name = if_indextoname(d->ifindex, ifname); + if (name) + printf("%8s", name); + printf("[%d]\t", d->ifindex); + + if (!d->_present.xdp_features) + return; + + printf("%llx:", d->xdp_features); + for (int i = 0; d->xdp_features > 1U << i; i++) { + if (d->xdp_features & (1U << i)) + printf(" %s", netdev_xdp_act_str(1 << i)); + } + + name = netdev_op_str(op); + if (name) + printf(" (ntf: %s)", name); + printf("\n"); +} + +int main(int argc, char **argv) +{ + struct netdev_dev_get_list *devs; + struct ynl_ntf_base_type *ntf; + struct ynl_error yerr; + struct ynl_sock *ys; + int ifindex = 0; + + if (argc > 1) + ifindex = strtol(argv[1], NULL, 0); + + ys = ynl_sock_create(&ynl_netdev_family, &yerr); + if (!ys) { + fprintf(stderr, "YNL: %s\n", yerr.msg); + return 1; + } + + if (ynl_subscribe(ys, "mgmt")) + goto err_close; + + printf("Select ifc ($ifindex; or 0 = dump; or -2 ntf check): "); + scanf("%d", &ifindex); + + if (ifindex > 0) { + struct netdev_dev_get_req *req; + struct netdev_dev_get_rsp *d; + + req = netdev_dev_get_req_alloc(); + netdev_dev_get_req_set_ifindex(req, ifindex); + + d = netdev_dev_get(ys, req); + netdev_dev_get_req_free(req); + if (!d) + goto err_close; + + netdev_print_device(d, 0); + netdev_dev_get_rsp_free(d); + } else if (!ifindex) { + devs = netdev_dev_get_dump(ys); + if (!devs) + goto err_close; + + ynl_dump_foreach(devs, d) + netdev_print_device(d, 0); + netdev_dev_get_list_free(devs); + } else if (ifindex == -2) { + ynl_ntf_check(ys); + } + while ((ntf = ynl_ntf_dequeue(ys))) { + netdev_print_device((struct netdev_dev_get_rsp *)&ntf->data, + ntf->cmd); + ynl_ntf_free(ntf); + } + + ynl_sock_destroy(ys); + return 0; + +err_close: + fprintf(stderr, "YNL: %s\n", ys->err.msg); + ynl_sock_destroy(ys); + return 2; +} -- cgit v1.2.3 From fff8660b5425bb2f441d67758926df62e61a69aa Mon Sep 17 00:00:00 2001 From: Jakub Kicinski Date: Wed, 7 Jun 2023 13:24:03 -0700 Subject: tools: ynl: add sample for devlink Add a sample to show off how to issue basic devlink requests. For added testing issue get requests while walking a dump. $ ./devlink netdevsim/netdevsim1: driver: netdevsim running fw: fw.mgmt: 10.20.30 ... netdevsim/netdevsim2: driver: netdevsim running fw: fw.mgmt: 10.20.30 ... Signed-off-by: Jakub Kicinski --- tools/net/ynl/samples/.gitignore | 1 + tools/net/ynl/samples/devlink.c | 60 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 tools/net/ynl/samples/devlink.c (limited to 'tools/net/ynl/samples') diff --git a/tools/net/ynl/samples/.gitignore b/tools/net/ynl/samples/.gitignore index 7b1f5179cb54..a24678b67557 100644 --- a/tools/net/ynl/samples/.gitignore +++ b/tools/net/ynl/samples/.gitignore @@ -1 +1,2 @@ +devlink netdev diff --git a/tools/net/ynl/samples/devlink.c b/tools/net/ynl/samples/devlink.c new file mode 100644 index 000000000000..d2611d7ebab4 --- /dev/null +++ b/tools/net/ynl/samples/devlink.c @@ -0,0 +1,60 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include + +#include + +#include "devlink-user.h" + +int main(int argc, char **argv) +{ + struct devlink_get_list *devs; + struct ynl_sock *ys; + + ys = ynl_sock_create(&ynl_devlink_family, NULL); + if (!ys) + return 1; + + devs = devlink_get_dump(ys); + if (!devs) + goto err_close; + + ynl_dump_foreach(devs, d) { + struct devlink_info_get_req *info_req; + struct devlink_info_get_rsp *info_rsp; + + printf("%s/%s:\n", d->bus_name, d->dev_name); + + info_req = devlink_info_get_req_alloc(); + devlink_info_get_req_set_bus_name(info_req, d->bus_name); + devlink_info_get_req_set_dev_name(info_req, d->dev_name); + + info_rsp = devlink_info_get(ys, info_req); + devlink_info_get_req_free(info_req); + if (!info_rsp) + goto err_free_devs; + + if (info_rsp->_present.info_driver_name_len) + printf(" driver: %s\n", info_rsp->info_driver_name); + if (info_rsp->n_info_version_running) + printf(" running fw:\n"); + for (unsigned i = 0; i < info_rsp->n_info_version_running; i++) + printf(" %s: %s\n", + info_rsp->info_version_running[i].info_version_name, + info_rsp->info_version_running[i].info_version_value); + printf(" ...\n"); + devlink_info_get_rsp_free(info_rsp); + } + devlink_get_list_free(devs); + + ynl_sock_destroy(ys); + + return 0; + +err_free_devs: + devlink_get_list_free(devs); +err_close: + fprintf(stderr, "YNL: %s\n", ys->err.msg); + ynl_sock_destroy(ys); + return 2; +} -- cgit v1.2.3 From f561ff232a6b502bda020ac47200e88f0bc5f98a Mon Sep 17 00:00:00 2001 From: Jakub Kicinski Date: Fri, 9 Jun 2023 14:43:46 -0700 Subject: tools: ynl: add sample for ethtool Configuring / reading ring sizes and counts is a fairly common operation for ethtool netlink. Present a sample doing that with YNL: $ ./ethtool Channels: enp1s0: combined 1 eni1np1: combined 1 eni2np1: combined 1 Rings: enp1s0: rx 256 tx 256 eni1np1: rx 0 tx 0 eni2np1: rx 0 tx 0 Signed-off-by: Jakub Kicinski Signed-off-by: David S. Miller --- tools/net/ynl/samples/.gitignore | 1 + tools/net/ynl/samples/ethtool.c | 65 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 tools/net/ynl/samples/ethtool.c (limited to 'tools/net/ynl/samples') diff --git a/tools/net/ynl/samples/.gitignore b/tools/net/ynl/samples/.gitignore index a24678b67557..2aae60c4829f 100644 --- a/tools/net/ynl/samples/.gitignore +++ b/tools/net/ynl/samples/.gitignore @@ -1,2 +1,3 @@ +ethtool devlink netdev diff --git a/tools/net/ynl/samples/ethtool.c b/tools/net/ynl/samples/ethtool.c new file mode 100644 index 000000000000..a7ebbd1b98db --- /dev/null +++ b/tools/net/ynl/samples/ethtool.c @@ -0,0 +1,65 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include + +#include + +#include + +#include "ethtool-user.h" + +int main(int argc, char **argv) +{ + struct ethtool_channels_get_req_dump creq = {}; + struct ethtool_rings_get_req_dump rreq = {}; + struct ethtool_channels_get_list *channels; + struct ethtool_rings_get_list *rings; + struct ynl_sock *ys; + + ys = ynl_sock_create(&ynl_ethtool_family, NULL); + if (!ys) + return 1; + + creq._present.header = 1; /* ethtool needs an empty nest, sigh */ + channels = ethtool_channels_get_dump(ys, &creq); + if (!channels) + goto err_close; + + printf("Channels:\n"); + ynl_dump_foreach(channels, dev) { + printf(" %8s: ", dev->header.dev_name); + if (dev->_present.rx_count) + printf("rx %d ", dev->rx_count); + if (dev->_present.tx_count) + printf("tx %d ", dev->tx_count); + if (dev->_present.combined_count) + printf("combined %d ", dev->combined_count); + printf("\n"); + } + ethtool_channels_get_list_free(channels); + + rreq._present.header = 1; /* ethtool needs an empty nest.. */ + rings = ethtool_rings_get_dump(ys, &rreq); + if (!rings) + goto err_close; + + printf("Rings:\n"); + ynl_dump_foreach(rings, dev) { + printf(" %8s: ", dev->header.dev_name); + if (dev->_present.rx) + printf("rx %d ", dev->rx); + if (dev->_present.tx) + printf("tx %d ", dev->tx); + printf("\n"); + } + ethtool_rings_get_list_free(rings); + + ynl_sock_destroy(ys); + + return 0; + +err_close: + fprintf(stderr, "YNL (%d): %s\n", ys->err.code, ys->err.msg); + ynl_sock_destroy(ys); + return 2; +} -- cgit v1.2.3 From f0ec58d557d65838974035ffd8a8862cf8166a81 Mon Sep 17 00:00:00 2001 From: Jakub Kicinski Date: Tue, 13 Jun 2023 17:28:00 -0700 Subject: tools: ynl: work around stale system headers The inability to include the uAPI headers directly in tools/ is one of the bigger annoyances of compiling user space code. Most projects trade the pain for smaller inconvenience of having to copy the headers under tools/include. In case of netlink headers I think that we can avoid both. Netlink family headers are simple and should be self-contained. We can try to twiddle the Makefile a little to force-include just the family header, and use system headers for the rest. This works fairly well. There are two warts - for some reason if we specify -include $path/family.h as a compilation flag, the #ifdef header guard does not seem to work. So we need to throw the guard in on the command line as well. Seems like GCC detects that the header is different and tries to include both. Second problem is that make wants hash sign to be escaped or not depending on the version. Sigh. Signed-off-by: Jakub Kicinski Signed-off-by: David S. Miller --- tools/net/ynl/Makefile.deps | 28 ++++++++++++++++++++++++++++ tools/net/ynl/generated/Makefile | 6 ++++-- tools/net/ynl/samples/Makefile | 4 +++- 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 tools/net/ynl/Makefile.deps (limited to 'tools/net/ynl/samples') diff --git a/tools/net/ynl/Makefile.deps b/tools/net/ynl/Makefile.deps new file mode 100644 index 000000000000..524fc4bb586b --- /dev/null +++ b/tools/net/ynl/Makefile.deps @@ -0,0 +1,28 @@ +# SPDX-License-Identifier: GPL-2.0 + +# Try to include uAPI headers from the kernel uapi/ path. +# Most code under tools/ requires the respective kernel uAPI headers +# to be copied to tools/include. The duplication is annoying. +# All the family headers should be self-contained. We avoid the copying +# by selectively including just the uAPI header of the family directly +# from the kernel sources. + +UAPI_PATH:=../../../../include/uapi/ + +# If the header does not exist at all in the system path - let the +# compiler fall back to the kernel header via -Idirafter. +# GCC seems to ignore header guard if the header is different, so we need +# to specify the -D$(hdr_guard). +# And we need to define HASH indirectly because GNU Make 4.2 wants it escaped +# and Gnu Make 4.4 wants it without escaping. + +HASH := \# + +get_hdr_inc=$(if $(shell echo "$(HASH)include " | \ + cpp >>/dev/null 2>/dev/null && echo yes),\ + -D$(1) -include $(UAPI_PATH)/linux/$(2)) + +CFLAGS_devlink:=$(call get_hdr_inc,_UAPI_LINUX_DEVLINK_H_,devlink.h) +CFLAGS_ethtool:=$(call get_hdr_inc,_LINUX_ETHTOOL_NETLINK_H_,ethtool_netlink.h) +CFLAGS_handshake:=$(call get_hdr_inc,_UAPI_LINUX_HANDSHAKE_H,handshake.h) +CFLAGS_netdev:=$(call get_hdr_inc,_UAPI_LINUX_NETDEV_H,netdev.h) diff --git a/tools/net/ynl/generated/Makefile b/tools/net/ynl/generated/Makefile index f15c24893296..f8817d2e56e4 100644 --- a/tools/net/ynl/generated/Makefile +++ b/tools/net/ynl/generated/Makefile @@ -2,11 +2,13 @@ CC=gcc CFLAGS=-std=gnu11 -O2 -W -Wall -Wextra -Wno-unused-parameter -Wshadow \ - -I../lib/ + -I../lib/ -idirafter $(UAPI_PATH) ifeq ("$(DEBUG)","1") CFLAGS += -g -fsanitize=address -fsanitize=leak -static-libasan endif +include ../Makefile.deps + YNL_GEN_ARG_ethtool:=--user-header linux/ethtool_netlink.h \ --exclude-op stats-get @@ -33,7 +35,7 @@ protos.a: $(OBJS) %-user.o: %-user.c %-user.h @echo -e "\tCC $@" - @$(COMPILE.c) -c -o $@ $< + @$(COMPILE.c) $(CFLAGS_$*) -o $@ $< clean: rm -f *.o diff --git a/tools/net/ynl/samples/Makefile b/tools/net/ynl/samples/Makefile index 714316cad45f..f2db8bb78309 100644 --- a/tools/net/ynl/samples/Makefile +++ b/tools/net/ynl/samples/Makefile @@ -1,8 +1,10 @@ # SPDX-License-Identifier: GPL-2.0 +include ../Makefile.deps + CC=gcc CFLAGS=-std=gnu11 -O2 -W -Wall -Wextra -Wno-unused-parameter -Wshadow \ - -I../lib/ -I../generated/ + -I../lib/ -I../generated/ -idirafter $(UAPI_PATH) ifeq ("$(DEBUG)","1") CFLAGS += -g -fsanitize=address -fsanitize=leak -static-libasan endif -- cgit v1.2.3