summaryrefslogtreecommitdiff
path: root/tools/net/ynl/samples/rt-link.c
blob: acdd4b4a0f7433ec8b0394513b798d2c4345a33b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// SPDX-License-Identifier: GPL-2.0
#include <stdio.h>
#include <string.h>

#include <ynl.h>

#include <arpa/inet.h>
#include <net/if.h>

#include "rt-link-user.h"

static void rt_link_print(struct rt_link_getlink_rsp *r)
{
	unsigned int i;

	printf("%3d: ", r->_hdr.ifi_index);

	if (r->_len.ifname)
		printf("%16s: ", r->ifname);

	if (r->_present.mtu)
		printf("mtu %5d  ", r->mtu);

	if (r->linkinfo._len.kind)
		printf("kind %-8s  ", r->linkinfo.kind);
	else
		printf("     %8s  ", "");

	if (r->prop_list._count.alt_ifname) {
		printf("altname ");
		for (i = 0; i < r->prop_list._count.alt_ifname; i++)
			printf("%s ", r->prop_list.alt_ifname[i]->str);
		printf(" ");
	}

	if (r->linkinfo._present.data && r->linkinfo.data._present.netkit) {
		struct rt_link_linkinfo_netkit_attrs *netkit;
		const char *name;

		netkit = &r->linkinfo.data.netkit;
		printf("primary %d  ", netkit->primary);

		name = NULL;
		if (netkit->_present.policy)
			name = rt_link_netkit_policy_str(netkit->policy);
		if (name)
			printf("policy %s  ", name);
	}

	printf("\n");
}

static int rt_link_create_netkit(struct ynl_sock *ys)
{
	struct rt_link_getlink_ntf *ntf_gl;
	struct rt_link_newlink_req *req;
	struct ynl_ntf_base_type *ntf;
	int ret;

	req = rt_link_newlink_req_alloc();
	if (!req) {
		fprintf(stderr, "Can't alloc req\n");
		return -1;
	}

	/* rtnetlink doesn't provide info about the created object.
	 * It expects us to set the ECHO flag and the dig the info out
	 * of the notifications...
	 */
	rt_link_newlink_req_set_nlflags(req, NLM_F_CREATE | NLM_F_ECHO);

	rt_link_newlink_req_set_linkinfo_kind(req, "netkit");

	/* Test error messages */
	rt_link_newlink_req_set_linkinfo_data_netkit_policy(req, 10);
	ret = rt_link_newlink(ys, req);
	if (ret) {
		printf("Testing error message for policy being bad:\n\t%s\n", ys->err.msg);
	} else {
		fprintf(stderr,	"Warning: unexpected success creating netkit with bad attrs\n");
		goto created;
	}

	rt_link_newlink_req_set_linkinfo_data_netkit_policy(req, NETKIT_DROP);

	ret = rt_link_newlink(ys, req);
created:
	rt_link_newlink_req_free(req);
	if (ret) {
		fprintf(stderr, "YNL: %s\n", ys->err.msg);
		return -1;
	}

	if (!ynl_has_ntf(ys)) {
		fprintf(stderr,
			"Warning: interface created but received no notification, won't delete the interface\n");
		return 0;
	}

	ntf = ynl_ntf_dequeue(ys);
	if (ntf->cmd !=	RTM_NEWLINK) {
		fprintf(stderr,
			"Warning: unexpected notification type, won't delete the interface\n");
		return 0;
	}
	ntf_gl = (void *)ntf;
	ret = ntf_gl->obj._hdr.ifi_index;
	ynl_ntf_free(ntf);

	return ret;
}

static void rt_link_del(struct ynl_sock *ys, int ifindex)
{
	struct rt_link_dellink_req *req;

	req = rt_link_dellink_req_alloc();
	if (!req) {
		fprintf(stderr, "Can't alloc req\n");
		return;
	}

	req->_hdr.ifi_index = ifindex;
	if (rt_link_dellink(ys, req))
		fprintf(stderr, "YNL: %s\n", ys->err.msg);
	else
		fprintf(stderr,
			"Trying to delete a Netkit interface (ifindex %d)\n",
			ifindex);

	rt_link_dellink_req_free(req);
}

int main(int argc, char **argv)
{
	struct rt_link_getlink_req_dump *req;
	struct rt_link_getlink_list *rsp;
	struct ynl_error yerr;
	struct ynl_sock *ys;
	int created = 0;

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

	if (argc > 1) {
		fprintf(stderr, "Trying to create a Netkit interface\n");
		created = rt_link_create_netkit(ys);
		if (created < 0)
			goto err_destroy;
	}

	req = rt_link_getlink_req_dump_alloc();
	if (!req)
		goto err_del_ifc;

	rsp = rt_link_getlink_dump(ys, req);
	rt_link_getlink_req_dump_free(req);
	if (!rsp)
		goto err_close;

	if (ynl_dump_empty(rsp))
		fprintf(stderr, "Error: no links reported\n");
	ynl_dump_foreach(rsp, link)
		rt_link_print(link);
	rt_link_getlink_list_free(rsp);

	if (created)
		rt_link_del(ys, created);

	ynl_sock_destroy(ys);
	return 0;

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