Skip to content

Commit 29d34a4

Browse files
committed
tools: ynl: generate code for rt-addr and add a sample
YNL C can now generate code for simple classic netlink families. Include rt-addr in the Makefile for generation and add a sample. $ ./tools/net/ynl/samples/rt-addr lo: 127.0.0.1 wlp0s20f3: 192.168.1.101 lo: :: wlp0s20f3: fe80::6385:be6:746e:8116 vpn0: fe80::3597:d353:b5a7:66dd 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 882e7b1 commit 29d34a4

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

tools/net/ynl/Makefile.deps

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ CFLAGS_nfsd:=$(call get_hdr_inc,_LINUX_NFSD_NETLINK_H,nfsd_netlink.h)
2929
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)
32+
CFLAGS_rt-addr:=$(call get_hdr_inc,__LINUX_RTNETLINK_H,rtnetlink.h)
3233
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})
28+
GENS=$(patsubst $(SPECS_DIR)/%.yaml,%,${GENS_PATHS}) rt-addr
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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ ethtool
22
devlink
33
netdev
44
ovs
5-
page-pool
5+
page-pool
6+
rt-addr

tools/net/ynl/samples/rt-addr.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-addr-user.h"
11+
12+
static void rt_addr_print(struct rt_addr_getaddr_rsp *a)
13+
{
14+
char ifname[IF_NAMESIZE];
15+
char addr_str[64];
16+
const char *addr;
17+
const char *name;
18+
19+
name = if_indextoname(a->_hdr.ifa_index, ifname);
20+
if (name)
21+
printf("%16s: ", name);
22+
23+
switch (a->_present.address_len) {
24+
case 4:
25+
addr = inet_ntop(AF_INET, a->address,
26+
addr_str, sizeof(addr_str));
27+
break;
28+
case 16:
29+
addr = inet_ntop(AF_INET6, a->address,
30+
addr_str, sizeof(addr_str));
31+
break;
32+
default:
33+
addr = NULL;
34+
break;
35+
}
36+
if (addr)
37+
printf("%s", addr);
38+
else
39+
printf("[%d]", a->_present.address_len);
40+
41+
printf("\n");
42+
}
43+
44+
int main(int argc, char **argv)
45+
{
46+
struct rt_addr_getaddr_list *rsp;
47+
struct rt_addr_getaddr_req *req;
48+
struct ynl_error yerr;
49+
struct ynl_sock *ys;
50+
51+
ys = ynl_sock_create(&ynl_rt_addr_family, &yerr);
52+
if (!ys) {
53+
fprintf(stderr, "YNL: %s\n", yerr.msg);
54+
return 1;
55+
}
56+
57+
req = rt_addr_getaddr_req_alloc();
58+
if (!req)
59+
goto err_destroy;
60+
61+
rsp = rt_addr_getaddr_dump(ys, req);
62+
rt_addr_getaddr_req_free(req);
63+
if (!rsp)
64+
goto err_close;
65+
66+
if (ynl_dump_empty(rsp))
67+
fprintf(stderr, "Error: no addresses reported\n");
68+
ynl_dump_foreach(rsp, addr)
69+
rt_addr_print(addr);
70+
rt_addr_getaddr_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)