Skip to content

Commit 54d7908

Browse files
committed
tools: ynl: generate code for rt-route and add a sample
YNL C can now generate code for simple classic netlink families. Include rt-route in the Makefile for generation and add a sample. $ ./tools/net/ynl/samples/rt-route oif: wlp0s20f3 gateway: 192.168.1.1 oif: wlp0s20f3 dst: 192.168.1.0/24 oif: vpn0 dst: fe80::/64 oif: wlp0s20f3 dst: fe80::/64 oif: wlp0s20f3 gateway: fe80::200:5eff:fe00:201 Reviewed-by: Jacob Keller <[email protected]> Reviewed-by: Donald Hunter <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 29d34a4 commit 54d7908

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

tools/net/ynl/Makefile.deps

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ CFLAGS_ovs_datapath:=$(call get_hdr_inc,__LINUX_OPENVSWITCH_H,openvswitch.h)
3030
CFLAGS_ovs_flow:=$(call get_hdr_inc,__LINUX_OPENVSWITCH_H,openvswitch.h)
3131
CFLAGS_ovs_vport:=$(call get_hdr_inc,__LINUX_OPENVSWITCH_H,openvswitch.h)
3232
CFLAGS_rt-addr:=$(call get_hdr_inc,__LINUX_RTNETLINK_H,rtnetlink.h)
33+
CFLAGS_rt-route:=$(call get_hdr_inc,__LINUX_RTNETLINK_H,rtnetlink.h)
3334
CFLAGS_tcp_metrics:=$(call get_hdr_inc,_LINUX_TCP_METRICS_H,tcp_metrics.h)

tools/net/ynl/generated/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ SPECS_DIR:=../../../../Documentation/netlink/specs
2525
GENS_PATHS=$(shell grep -nrI --files-without-match \
2626
'protocol: netlink' \
2727
$(SPECS_DIR))
28-
GENS=$(patsubst $(SPECS_DIR)/%.yaml,%,${GENS_PATHS}) rt-addr
28+
GENS=$(patsubst $(SPECS_DIR)/%.yaml,%,${GENS_PATHS}) rt-addr rt-route
2929
SRCS=$(patsubst %,%-user.c,${GENS})
3030
HDRS=$(patsubst %,%-user.h,${GENS})
3131
OBJS=$(patsubst %,%-user.o,${GENS})

tools/net/ynl/samples/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ netdev
44
ovs
55
page-pool
66
rt-addr
7+
rt-route

tools/net/ynl/samples/rt-route.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <stdio.h>
3+
#include <string.h>
4+
5+
#include <ynl.h>
6+
7+
#include <arpa/inet.h>
8+
#include <net/if.h>
9+
10+
#include "rt-route-user.h"
11+
12+
static void rt_route_print(struct rt_route_getroute_rsp *r)
13+
{
14+
char ifname[IF_NAMESIZE];
15+
char route_str[64];
16+
const char *route;
17+
const char *name;
18+
19+
/* Ignore local */
20+
if (r->_hdr.rtm_table == RT_TABLE_LOCAL)
21+
return;
22+
23+
if (r->_present.oif) {
24+
name = if_indextoname(r->oif, ifname);
25+
if (name)
26+
printf("oif: %-16s ", name);
27+
}
28+
29+
if (r->_present.dst_len) {
30+
route = inet_ntop(r->_hdr.rtm_family, r->dst,
31+
route_str, sizeof(route_str));
32+
printf("dst: %s/%d", route, r->_hdr.rtm_dst_len);
33+
}
34+
35+
if (r->_present.gateway_len) {
36+
route = inet_ntop(r->_hdr.rtm_family, r->gateway,
37+
route_str, sizeof(route_str));
38+
printf("gateway: %s ", route);
39+
}
40+
41+
printf("\n");
42+
}
43+
44+
int main(int argc, char **argv)
45+
{
46+
struct rt_route_getroute_req_dump *req;
47+
struct rt_route_getroute_list *rsp;
48+
struct ynl_error yerr;
49+
struct ynl_sock *ys;
50+
51+
ys = ynl_sock_create(&ynl_rt_route_family, &yerr);
52+
if (!ys) {
53+
fprintf(stderr, "YNL: %s\n", yerr.msg);
54+
return 1;
55+
}
56+
57+
req = rt_route_getroute_req_dump_alloc();
58+
if (!req)
59+
goto err_destroy;
60+
61+
rsp = rt_route_getroute_dump(ys, req);
62+
rt_route_getroute_req_dump_free(req);
63+
if (!rsp)
64+
goto err_close;
65+
66+
if (ynl_dump_empty(rsp))
67+
fprintf(stderr, "Error: no routeesses reported\n");
68+
ynl_dump_foreach(rsp, route)
69+
rt_route_print(route);
70+
rt_route_getroute_list_free(rsp);
71+
72+
ynl_sock_destroy(ys);
73+
return 0;
74+
75+
err_close:
76+
fprintf(stderr, "YNL: %s\n", ys->err.msg);
77+
err_destroy:
78+
ynl_sock_destroy(ys);
79+
return 2;
80+
}

0 commit comments

Comments
 (0)