Commit 86020d7d authored by Jakub Kicinski's avatar Jakub Kicinski
Browse files

Merge branch 'tools-ynl-convert-samples-into-selftests'

Jakub Kicinski says:

====================
tools: ynl: convert samples into selftests

The "samples" were always poor man's tests, used to manually
confirm that C YNL works as expected. Since a proper tests/
directory now exists move the samples and use the kselftest
harness to turn them into selftests outputting KTAP.
====================

Link: https://patch.msgid.link/20260307033630.1396085-1-kuba@kernel.org


Signed-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parents 0bcac7b1 aa234faa
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -14,12 +14,12 @@ includedir ?= $(prefix)/include

SPECDIR=../../../Documentation/netlink/specs

SUBDIRS = lib generated samples ynltool tests
SUBDIRS = lib generated ynltool tests

all: $(SUBDIRS) libynl.a

tests: | lib generated libynl.a
ynltool: | lib generated libynl.a
samples: | lib generated
libynl.a: | lib generated
	@echo -e "\tAR $@"
	@ar rcs $@ lib/ynl.o generated/*-user.o

tools/net/ynl/samples/Makefile

deleted100644 → 0
+0 −36
Original line number Diff line number Diff line
# 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/ -idirafter $(UAPI_PATH)
ifeq ("$(DEBUG)","1")
  CFLAGS += -g -fsanitize=address -fsanitize=leak -static-libasan
endif

LDLIBS=../lib/ynl.a ../generated/protos.a

SRCS=$(wildcard *.c)
BINS=$(patsubst %.c,%,${SRCS})

include $(wildcard *.d)

all: $(BINS)

CFLAGS_page-pool=$(CFLAGS_netdev)
CFLAGS_tc-filter-add:=$(CFLAGS_tc)

$(BINS): ../lib/ynl.a ../generated/protos.a $(SRCS)
	@echo -e '\tCC sample $@'
	@$(COMPILE.c) $(CFLAGS_$@) $@.c -o $@.o
	@$(LINK.c) $@.o -o $@  $(LDLIBS)

clean:
	rm -f *.o *.d *~

distclean: clean
	rm -f $(BINS)

.PHONY: all clean distclean
.DEFAULT_GOAL=all

tools/net/ynl/samples/netdev.c

deleted100644 → 0
+0 −128
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0
#include <stdio.h>
#include <string.h>

#include <ynl.h>

#include <net/if.h>

#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("xdp-features (%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));
	}

	printf(" xdp-rx-metadata-features (%llx):", d->xdp_rx_metadata_features);
	for (int i = 0; d->xdp_rx_metadata_features >= 1U << i; i++) {
		if (d->xdp_rx_metadata_features & (1U << i))
			printf(" %s", netdev_xdp_rx_metadata_str(1 << i));
	}

	printf(" xsk-features (%llx):", d->xsk_features);
	for (int i = 0; d->xsk_features >= 1U << i; i++) {
		if (d->xsk_features & (1U << i))
			printf(" %s", netdev_xsk_flags_str(1 << i));
	}

	printf(" xdp-zc-max-segs=%u", d->xdp_zc_max_segs);

	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): ");
	if (scanf("%d", &ifindex) != 1) {
		fprintf(stderr, "Error: unable to parse input\n");
		goto err_destroy;
	}

	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;

		if (ynl_dump_empty(devs))
			fprintf(stderr, "Error: no devices reported\n");
		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);
err_destroy:
	ynl_sock_destroy(ys);
	return 2;
}

tools/net/ynl/samples/ovs.c

deleted100644 → 0
+0 −60
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0
#include <stdio.h>
#include <string.h>

#include <ynl.h>

#include "ovs_datapath-user.h"

int main(int argc, char **argv)
{
	struct ynl_sock *ys;
	int err;

	ys = ynl_sock_create(&ynl_ovs_datapath_family, NULL);
	if (!ys)
		return 1;

	if (argc > 1) {
		struct ovs_datapath_new_req *req;

		req = ovs_datapath_new_req_alloc();
		if (!req)
			goto err_close;

		ovs_datapath_new_req_set_upcall_pid(req, 1);
		ovs_datapath_new_req_set_name(req, argv[1]);

		err = ovs_datapath_new(ys, req);
		ovs_datapath_new_req_free(req);
		if (err)
			goto err_close;
	} else {
		struct ovs_datapath_get_req_dump *req;
		struct ovs_datapath_get_list *dps;

		printf("Dump:\n");
		req = ovs_datapath_get_req_dump_alloc();

		dps = ovs_datapath_get_dump(ys, req);
		ovs_datapath_get_req_dump_free(req);
		if (!dps)
			goto err_close;

		ynl_dump_foreach(dps, dp) {
			printf("  %s(%d): pid:%u cache:%u\n",
			       dp->name, dp->_hdr.dp_ifindex,
			       dp->upcall_pid, dp->masks_cache_size);
		}
		ovs_datapath_get_list_free(dps);
	}

	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;
}

tools/net/ynl/samples/tc.c

deleted100644 → 0
+0 −80
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0
#include <stdio.h>
#include <string.h>

#include <ynl.h>

#include <net/if.h>

#include "tc-user.h"

static void tc_qdisc_print(struct tc_getqdisc_rsp *q)
{
	char ifname[IF_NAMESIZE];
	const char *name;

	name = if_indextoname(q->_hdr.tcm_ifindex, ifname);
	if (name)
		printf("%16s: ", name);

	if (q->_len.kind) {
		printf("%s  ", q->kind);

		if (q->options._present.fq_codel) {
			struct tc_fq_codel_attrs *fq_codel;
			struct tc_fq_codel_xstats *stats;

			fq_codel = &q->options.fq_codel;
			stats = q->stats2.app.fq_codel;

			if (fq_codel->_present.limit)
				printf("limit: %dp ", fq_codel->limit);
			if (fq_codel->_present.target)
				printf("target: %dms ",
				       (fq_codel->target + 500) / 1000);
			if (q->stats2.app._len.fq_codel)
				printf("new_flow_cnt: %d ",
				       stats->qdisc_stats.new_flow_count);
		}
	}

	printf("\n");
}

int main(int argc, char **argv)
{
	struct tc_getqdisc_req_dump *req;
	struct tc_getqdisc_list *rsp;
	struct ynl_error yerr;
	struct ynl_sock *ys;

	ys = ynl_sock_create(&ynl_tc_family, &yerr);
	if (!ys) {
		fprintf(stderr, "YNL: %s\n", yerr.msg);
		return 1;
	}

	req = tc_getqdisc_req_dump_alloc();
	if (!req)
		goto err_destroy;

	rsp = tc_getqdisc_dump(ys, req);
	tc_getqdisc_req_dump_free(req);
	if (!rsp)
		goto err_close;

	if (ynl_dump_empty(rsp))
		fprintf(stderr, "Error: no addresses reported\n");
	ynl_dump_foreach(rsp, qdisc)
		tc_qdisc_print(qdisc);
	tc_getqdisc_list_free(rsp);

	ynl_sock_destroy(ys);
	return 0;

err_close:
	fprintf(stderr, "YNL: %s\n", ys->err.msg);
err_destroy:
	ynl_sock_destroy(ys);
	return 2;
}
Loading