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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Command to manage vnifiltering on a vxlan device
*
* Authors: Roopa Prabhu <roopa@nvidia.com>
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <linux/if_link.h>
#include <linux/if_bridge.h>
#include <linux/if_ether.h>
#include "json_print.h"
#include "libnetlink.h"
#include "br_common.h"
#include "utils.h"
static unsigned int filter_index;
/* max len of "<start>-<end>" */
#define VXLAN_ID_LEN 17
#define __stringify_1(x...) #x
#define __stringify(x...) __stringify_1(x)
static void usage(void)
{
fprintf(stderr,
"Usage: bridge vni { add | del } vni VNI\n"
" [ { group | remote } IP_ADDRESS ]\n"
" dev DEV\n"
" bridge vni { show } [ dev DEV ]\n"
"\n"
"Where: VNI := 0-16777215\n"
);
exit(-1);
}
static int parse_vni_filter(const char *argv, struct nlmsghdr *n, int reqsize,
inet_prefix *group)
{
char *vnilist = strdupa(argv);
char *vni = strtok(vnilist, ",");
int group_type = AF_UNSPEC;
struct rtattr *nlvlist_e;
char *v;
if (group && is_addrtype_inet(group))
group_type = (group->family == AF_INET) ? VXLAN_VNIFILTER_ENTRY_GROUP :
VXLAN_VNIFILTER_ENTRY_GROUP6;
while (vni != NULL) {
__u32 vni_start = 0, vni_end = 0;
v = strchr(vni, '-');
if (v) {
*v = '\0';
v++;
vni_start = atoi(vni);
vni_end = atoi(v);
} else {
vni_start = atoi(vni);
}
nlvlist_e = addattr_nest(n, reqsize, VXLAN_VNIFILTER_ENTRY |
NLA_F_NESTED);
addattr32(n, 1024, VXLAN_VNIFILTER_ENTRY_START, vni_start);
if (vni_end)
addattr32(n, 1024, VXLAN_VNIFILTER_ENTRY_END, vni_end);
if (group)
addattr_l(n, 1024, group_type, group->data, group->bytelen);
addattr_nest_end(n, nlvlist_e);
vni = strtok(NULL, ",");
}
return 0;
}
static int vni_modify(int cmd, int argc, char **argv)
{
struct {
struct nlmsghdr n;
struct tunnel_msg tmsg;
char buf[1024];
} req = {
.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tunnel_msg)),
.n.nlmsg_flags = NLM_F_REQUEST,
.n.nlmsg_type = cmd,
.tmsg.family = PF_BRIDGE,
};
bool daddr_present = false;
inet_prefix daddr;
char *vni = NULL;
char *d = NULL;
while (argc > 0) {
if (strcmp(*argv, "dev") == 0) {
NEXT_ARG();
d = *argv;
} else if (strcmp(*argv, "vni") == 0) {
NEXT_ARG();
if (vni)
duparg("vni", *argv);
vni = *argv;
} else if (strcmp(*argv, "group") == 0) {
NEXT_ARG();
if (daddr_present)
duparg("destination", *argv);
get_addr(&daddr, *argv, AF_UNSPEC);
if (!is_addrtype_inet_multi(&daddr))
invarg("invalid group address", *argv);
daddr_present = true;
} else if (strcmp(*argv, "remote") == 0) {
NEXT_ARG();
if (daddr_present)
duparg("destination", *argv);
get_addr(&daddr, *argv, AF_UNSPEC);
daddr_present = true;
} else {
if (strcmp(*argv, "help") == 0)
usage();
}
argc--; argv++;
}
if (d == NULL || vni == NULL) {
fprintf(stderr, "Device and VNI ID are required arguments.\n");
return -1;
}
parse_vni_filter(vni, &req.n, sizeof(req),
(daddr_present ? &daddr : NULL));
req.tmsg.ifindex = ll_name_to_index(d);
if (req.tmsg.ifindex == 0) {
fprintf(stderr, "Cannot find vxlan device \"%s\"\n", d);
return -1;
}
if (rtnl_talk(&rth, &req.n, NULL) < 0)
return -1;
return 0;
}
static void open_vni_port(int ifi_index)
{
open_json_object(NULL);
print_color_string(PRINT_ANY, COLOR_IFNAME, "ifname",
"%-" __stringify(IFNAMSIZ) "s ",
ll_index_to_name(ifi_index));
open_json_array(PRINT_JSON, "vnis");
}
static void close_vni_port(void)
{
close_json_array(PRINT_JSON, NULL);
close_json_object();
}
static void print_vnifilter_entry_stats(struct rtattr *stats_attr)
{
struct rtattr *stb[VNIFILTER_ENTRY_STATS_MAX+1];
__u64 stat;
open_json_object("stats");
parse_rtattr_flags(stb, VNIFILTER_ENTRY_STATS_MAX, RTA_DATA(stats_attr),
RTA_PAYLOAD(stats_attr), NLA_F_NESTED);
print_nl();
print_string(PRINT_FP, NULL, "%-" __stringify(IFNAMSIZ) "s RX: ",
"");
if (stb[VNIFILTER_ENTRY_STATS_RX_BYTES]) {
stat = rta_getattr_u64(stb[VNIFILTER_ENTRY_STATS_RX_BYTES]);
print_lluint(PRINT_ANY, "rx_bytes", "bytes %llu ", stat);
}
if (stb[VNIFILTER_ENTRY_STATS_RX_PKTS]) {
stat = rta_getattr_u64(stb[VNIFILTER_ENTRY_STATS_RX_PKTS]);
print_lluint(PRINT_ANY, "rx_pkts", "pkts %llu ", stat);
}
if (stb[VNIFILTER_ENTRY_STATS_RX_DROPS]) {
stat = rta_getattr_u64(stb[VNIFILTER_ENTRY_STATS_RX_DROPS]);
print_lluint(PRINT_ANY, "rx_drops", "drops %llu ", stat);
}
if (stb[VNIFILTER_ENTRY_STATS_RX_ERRORS]) {
stat = rta_getattr_u64(stb[VNIFILTER_ENTRY_STATS_RX_ERRORS]);
print_lluint(PRINT_ANY, "rx_errors", "errors %llu ", stat);
}
print_nl();
print_string(PRINT_FP, NULL, "%-" __stringify(IFNAMSIZ) "s TX: ",
"");
if (stb[VNIFILTER_ENTRY_STATS_TX_BYTES]) {
stat = rta_getattr_u64(stb[VNIFILTER_ENTRY_STATS_TX_BYTES]);
print_lluint(PRINT_ANY, "tx_bytes", "bytes %llu ", stat);
}
if (stb[VNIFILTER_ENTRY_STATS_TX_PKTS]) {
stat = rta_getattr_u64(stb[VNIFILTER_ENTRY_STATS_TX_PKTS]);
print_lluint(PRINT_ANY, "tx_pkts", "pkts %llu ", stat);
}
if (stb[VNIFILTER_ENTRY_STATS_TX_DROPS]) {
stat = rta_getattr_u64(stb[VNIFILTER_ENTRY_STATS_TX_DROPS]);
print_lluint(PRINT_ANY, "tx_drops", "drops %llu ", stat);
}
if (stb[VNIFILTER_ENTRY_STATS_TX_ERRORS]) {
stat = rta_getattr_u64(stb[VNIFILTER_ENTRY_STATS_TX_ERRORS]);
print_lluint(PRINT_ANY, "tx_errors", "errors %llu ", stat);
}
close_json_object();
}
static void print_vni(struct rtattr *t, int ifindex)
{
struct rtattr *ttb[VXLAN_VNIFILTER_ENTRY_MAX+1];
__u32 vni_start = 0;
unsigned int width;
__u32 vni_end;
parse_rtattr_flags(ttb, VXLAN_VNIFILTER_ENTRY_MAX, RTA_DATA(t),
RTA_PAYLOAD(t), NLA_F_NESTED);
if (ttb[VXLAN_VNIFILTER_ENTRY_START])
vni_start = rta_getattr_u32(ttb[VXLAN_VNIFILTER_ENTRY_START]);
if (ttb[VXLAN_VNIFILTER_ENTRY_END])
vni_end = rta_getattr_u32(ttb[VXLAN_VNIFILTER_ENTRY_END]);
else
vni_end = vni_start;
open_json_object(NULL);
width = print_range("vni", vni_start, vni_end);
if (!is_json_context())
printf("%-*s ", VXLAN_ID_LEN - width, "");
if (ttb[VXLAN_VNIFILTER_ENTRY_GROUP]) {
__be32 addr = rta_getattr_u32(ttb[VXLAN_VNIFILTER_ENTRY_GROUP]);
if (addr) {
if (IN_MULTICAST(ntohl(addr)))
print_string(PRINT_ANY,
"group",
"%s",
format_host(AF_INET, 4, &addr));
else
print_string(PRINT_ANY,
"remote",
"%s",
format_host(AF_INET, 4, &addr));
}
} else if (ttb[VXLAN_VNIFILTER_ENTRY_GROUP6]) {
struct in6_addr addr;
memcpy(&addr, RTA_DATA(ttb[VXLAN_VNIFILTER_ENTRY_GROUP6]), sizeof(struct in6_addr));
if (!IN6_IS_ADDR_UNSPECIFIED(&addr)) {
if (IN6_IS_ADDR_MULTICAST(&addr))
print_string(PRINT_ANY,
"group",
"%s",
format_host(AF_INET6,
sizeof(struct in6_addr),
&addr));
else
print_string(PRINT_ANY,
"remote",
"%s",
format_host(AF_INET6,
sizeof(struct in6_addr),
&addr));
}
}
if (ttb[VXLAN_VNIFILTER_ENTRY_STATS])
print_vnifilter_entry_stats(ttb[VXLAN_VNIFILTER_ENTRY_STATS]);
close_json_object();
print_nl();
}
int print_vnifilter_rtm(struct nlmsghdr *n, void *arg)
{
struct tunnel_msg *tmsg = NLMSG_DATA(n);
int len = n->nlmsg_len;
bool opened = false;
struct rtattr *t;
FILE *fp = arg;
int rem;
if (n->nlmsg_type != RTM_NEWTUNNEL &&
n->nlmsg_type != RTM_DELTUNNEL &&
n->nlmsg_type != RTM_GETTUNNEL) {
fprintf(stderr, "Unknown vni tunnel rtm msg: %08x %08x %08x\n",
n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
return 0;
}
len -= NLMSG_LENGTH(sizeof(*tmsg));
if (len < 0) {
fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
return -1;
}
if (tmsg->family != AF_BRIDGE)
return 0;
if (filter_index && filter_index != tmsg->ifindex)
return 0;
print_headers(fp, "[TUNNEL]");
if (n->nlmsg_type == RTM_DELTUNNEL)
print_bool(PRINT_ANY, "deleted", "Deleted ", true);
rem = len;
for (t = TUNNEL_RTA(tmsg); RTA_OK(t, rem); t = RTA_NEXT(t, rem)) {
if (rta_type(t) != VXLAN_VNIFILTER_ENTRY)
continue;
if (!opened) {
open_vni_port(tmsg->ifindex);
opened = true;
} else {
print_string(PRINT_FP, NULL, "%-" __stringify(IFNAMSIZ) "s ", "");
}
print_vni(t, tmsg->ifindex);
}
if (opened)
close_vni_port();
fflush(stdout);
return 0;
}
static int vni_show(int argc, char **argv)
{
char *filter_dev = NULL;
__u8 flags = 0;
int ret = 0;
while (argc > 0) {
if (strcmp(*argv, "dev") == 0) {
NEXT_ARG();
if (filter_dev)
duparg("dev", *argv);
filter_dev = *argv;
}
argc--; argv++;
}
if (filter_dev) {
filter_index = ll_name_to_index(filter_dev);
if (!filter_index)
return nodev(filter_dev);
}
new_json_obj(json);
if (show_stats)
flags = TUNNEL_MSG_FLAG_STATS;
if (rtnl_tunneldump_req(&rth, PF_BRIDGE, filter_index, flags) < 0) {
perror("Cannot send dump request");
exit(1);
}
if (!is_json_context())
printf("%-" __stringify(IFNAMSIZ) "s %-"
__stringify(VXLAN_ID_LEN) "s group/remote\n", "dev",
"vni");
ret = rtnl_dump_filter(&rth, print_vnifilter_rtm, NULL);
if (ret < 0) {
fprintf(stderr, "Dump ternminated\n");
exit(1);
}
delete_json_obj();
fflush(stdout);
return 0;
}
int do_vni(int argc, char **argv)
{
ll_init_map(&rth);
timestamp = 0;
if (argc > 0) {
if (strcmp(*argv, "add") == 0)
return vni_modify(RTM_NEWTUNNEL, argc-1, argv+1);
if (strcmp(*argv, "delete") == 0 ||
strcmp(*argv, "del") == 0)
return vni_modify(RTM_DELTUNNEL, argc-1, argv+1);
if (strcmp(*argv, "show") == 0 ||
strcmp(*argv, "lst") == 0 ||
strcmp(*argv, "list") == 0)
return vni_show(argc-1, argv+1);
if (strcmp(*argv, "help") == 0)
usage();
} else {
return vni_show(0, NULL);
}
fprintf(stderr, "Command \"%s\" is unknown, try \"bridge vni help\".\n", *argv);
exit(-1);
}
|