]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/iplink_vxlan.c
vxlan: add support to set flow label
[mirror_iproute2.git] / ip / iplink_vxlan.c
1 /*
2 * iplink_vxlan.c VXLAN device support
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Stephen Hemminger <shemminger@vyatta.com
10 */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <net/if.h>
16 #include <linux/ip.h>
17 #include <linux/if_link.h>
18 #include <arpa/inet.h>
19
20 #include "rt_names.h"
21 #include "utils.h"
22 #include "ip_common.h"
23
24 static void print_explain(FILE *f)
25 {
26 fprintf(f, "Usage: ... vxlan id VNI [ { group | remote } IP_ADDRESS ] [ local ADDR ]\n");
27 fprintf(f, " [ ttl TTL ] [ tos TOS ] [ flowlabel LABEL ] [ dev PHYS_DEV ]\n");
28 fprintf(f, " [ dstport PORT ] [ srcport MIN MAX ]\n");
29 fprintf(f, " [ [no]learning ] [ [no]proxy ] [ [no]rsc ]\n");
30 fprintf(f, " [ [no]l2miss ] [ [no]l3miss ]\n");
31 fprintf(f, " [ ageing SECONDS ] [ maxaddress NUMBER ]\n");
32 fprintf(f, " [ [no]udpcsum ] [ [no]udp6zerocsumtx ] [ [no]udp6zerocsumrx ]\n");
33 fprintf(f, " [ [no]remcsumtx ] [ [no]remcsumrx ]\n");
34 fprintf(f, " [ [no]external ] [ gbp ]\n");
35 fprintf(f, "\n");
36 fprintf(f, "Where: VNI := 0-16777215\n");
37 fprintf(f, " ADDR := { IP_ADDRESS | any }\n");
38 fprintf(f, " TOS := { NUMBER | inherit }\n");
39 fprintf(f, " TTL := { 1..255 | inherit }\n");
40 fprintf(f, " LABEL := 0-1048575\n");
41 }
42
43 static void explain(void)
44 {
45 print_explain(stderr);
46 }
47
48 static int vxlan_parse_opt(struct link_util *lu, int argc, char **argv,
49 struct nlmsghdr *n)
50 {
51 __u32 vni = 0;
52 int vni_set = 0;
53 __u32 saddr = 0;
54 __u32 gaddr = 0;
55 __u32 daddr = 0;
56 struct in6_addr saddr6 = IN6ADDR_ANY_INIT;
57 struct in6_addr gaddr6 = IN6ADDR_ANY_INIT;
58 struct in6_addr daddr6 = IN6ADDR_ANY_INIT;
59 unsigned int link = 0;
60 __u8 tos = 0;
61 __u8 ttl = 0;
62 __u32 label = 0;
63 __u8 learning = 1;
64 __u8 proxy = 0;
65 __u8 rsc = 0;
66 __u8 l2miss = 0;
67 __u8 l3miss = 0;
68 __u8 noage = 0;
69 __u32 age = 0;
70 __u32 maxaddr = 0;
71 __u16 dstport = 0;
72 __u8 udpcsum = 0;
73 bool udpcsum_set = false;
74 __u8 udp6zerocsumtx = 0;
75 bool udp6zerocsumtx_set = false;
76 __u8 udp6zerocsumrx = 0;
77 bool udp6zerocsumrx_set = false;
78 __u8 remcsumtx = 0;
79 __u8 remcsumrx = 0;
80 __u8 metadata = 0;
81 __u8 gbp = 0;
82 int dst_port_set = 0;
83 struct ifla_vxlan_port_range range = { 0, 0 };
84
85 while (argc > 0) {
86 if (!matches(*argv, "id") ||
87 !matches(*argv, "vni")) {
88 NEXT_ARG();
89 if (get_u32(&vni, *argv, 0) ||
90 vni >= 1u << 24)
91 invarg("invalid id", *argv);
92 vni_set = 1;
93 } else if (!matches(*argv, "group")) {
94 NEXT_ARG();
95 if (!inet_get_addr(*argv, &gaddr, &gaddr6)) {
96 fprintf(stderr, "Invalid address \"%s\"\n", *argv);
97 return -1;
98 }
99 if (!IN6_IS_ADDR_MULTICAST(&gaddr6) && !IN_MULTICAST(ntohl(gaddr)))
100 invarg("invalid group address", *argv);
101 } else if (!matches(*argv, "remote")) {
102 NEXT_ARG();
103 if (!inet_get_addr(*argv, &daddr, &daddr6)) {
104 fprintf(stderr, "Invalid address \"%s\"\n", *argv);
105 return -1;
106 }
107 if (IN6_IS_ADDR_MULTICAST(&daddr6) || IN_MULTICAST(ntohl(daddr)))
108 invarg("invalid remote address", *argv);
109 } else if (!matches(*argv, "local")) {
110 NEXT_ARG();
111 if (strcmp(*argv, "any")) {
112 if (!inet_get_addr(*argv, &saddr, &saddr6)) {
113 fprintf(stderr, "Invalid address \"%s\"\n", *argv);
114 return -1;
115 }
116 }
117
118 if (IN_MULTICAST(ntohl(saddr)) || IN6_IS_ADDR_MULTICAST(&saddr6))
119 invarg("invalid local address", *argv);
120 } else if (!matches(*argv, "dev")) {
121 NEXT_ARG();
122 link = if_nametoindex(*argv);
123 if (link == 0) {
124 fprintf(stderr, "Cannot find device \"%s\"\n",
125 *argv);
126 exit(-1);
127 }
128 } else if (!matches(*argv, "ttl") ||
129 !matches(*argv, "hoplimit")) {
130 unsigned int uval;
131
132 NEXT_ARG();
133 if (strcmp(*argv, "inherit") != 0) {
134 if (get_unsigned(&uval, *argv, 0))
135 invarg("invalid TTL", *argv);
136 if (uval > 255)
137 invarg("TTL must be <= 255", *argv);
138 ttl = uval;
139 }
140 } else if (!matches(*argv, "tos") ||
141 !matches(*argv, "dsfield")) {
142 __u32 uval;
143
144 NEXT_ARG();
145 if (strcmp(*argv, "inherit") != 0) {
146 if (rtnl_dsfield_a2n(&uval, *argv))
147 invarg("bad TOS value", *argv);
148 tos = uval;
149 } else
150 tos = 1;
151 } else if (!matches(*argv, "label") ||
152 !matches(*argv, "flowlabel")) {
153 __u32 uval;
154
155 NEXT_ARG();
156 if (get_u32(&uval, *argv, 0) ||
157 (uval & ~LABEL_MAX_MASK))
158 invarg("invalid flowlabel", *argv);
159 label = htonl(uval);
160 } else if (!matches(*argv, "ageing")) {
161 NEXT_ARG();
162 if (strcmp(*argv, "none") == 0)
163 noage = 1;
164 else if (get_u32(&age, *argv, 0))
165 invarg("ageing timer", *argv);
166 } else if (!matches(*argv, "maxaddress")) {
167 NEXT_ARG();
168 if (strcmp(*argv, "unlimited") == 0)
169 maxaddr = 0;
170 else if (get_u32(&maxaddr, *argv, 0))
171 invarg("max addresses", *argv);
172 } else if (!matches(*argv, "port") ||
173 !matches(*argv, "srcport")) {
174 __u16 minport, maxport;
175
176 NEXT_ARG();
177 if (get_u16(&minport, *argv, 0))
178 invarg("min port", *argv);
179 NEXT_ARG();
180 if (get_u16(&maxport, *argv, 0))
181 invarg("max port", *argv);
182 range.low = htons(minport);
183 range.high = htons(maxport);
184 } else if (!matches(*argv, "dstport")) {
185 NEXT_ARG();
186 if (get_u16(&dstport, *argv, 0))
187 invarg("dst port", *argv);
188 dst_port_set = 1;
189 } else if (!matches(*argv, "nolearning")) {
190 learning = 0;
191 } else if (!matches(*argv, "learning")) {
192 learning = 1;
193 } else if (!matches(*argv, "noproxy")) {
194 proxy = 0;
195 } else if (!matches(*argv, "proxy")) {
196 proxy = 1;
197 } else if (!matches(*argv, "norsc")) {
198 rsc = 0;
199 } else if (!matches(*argv, "rsc")) {
200 rsc = 1;
201 } else if (!matches(*argv, "nol2miss")) {
202 l2miss = 0;
203 } else if (!matches(*argv, "l2miss")) {
204 l2miss = 1;
205 } else if (!matches(*argv, "nol3miss")) {
206 l3miss = 0;
207 } else if (!matches(*argv, "l3miss")) {
208 l3miss = 1;
209 } else if (!matches(*argv, "udpcsum")) {
210 udpcsum = 1;
211 udpcsum_set = true;
212 } else if (!matches(*argv, "noudpcsum")) {
213 udpcsum = 0;
214 udpcsum_set = true;
215 } else if (!matches(*argv, "udp6zerocsumtx")) {
216 udp6zerocsumtx = 1;
217 udp6zerocsumtx_set = true;
218 } else if (!matches(*argv, "noudp6zerocsumtx")) {
219 udp6zerocsumtx = 0;
220 udp6zerocsumtx_set = true;
221 } else if (!matches(*argv, "udp6zerocsumrx")) {
222 udp6zerocsumrx = 1;
223 udp6zerocsumrx_set = true;
224 } else if (!matches(*argv, "noudp6zerocsumrx")) {
225 udp6zerocsumrx = 0;
226 udp6zerocsumrx_set = true;
227 } else if (!matches(*argv, "remcsumtx")) {
228 remcsumtx = 1;
229 } else if (!matches(*argv, "noremcsumtx")) {
230 remcsumtx = 0;
231 } else if (!matches(*argv, "remcsumrx")) {
232 remcsumrx = 1;
233 } else if (!matches(*argv, "noremcsumrx")) {
234 remcsumrx = 0;
235 } else if (!matches(*argv, "external")) {
236 metadata = 1;
237 } else if (!matches(*argv, "noexternal")) {
238 metadata = 0;
239 } else if (!matches(*argv, "gbp")) {
240 gbp = 1;
241 } else if (matches(*argv, "help") == 0) {
242 explain();
243 return -1;
244 } else {
245 fprintf(stderr, "vxlan: unknown command \"%s\"?\n", *argv);
246 explain();
247 return -1;
248 }
249 argc--, argv++;
250 }
251
252 if (metadata && vni_set) {
253 fprintf(stderr, "vxlan: both 'external' and vni cannot be specified\n");
254 return -1;
255 }
256
257 if (!metadata && !vni_set) {
258 fprintf(stderr, "vxlan: missing virtual network identifier\n");
259 return -1;
260 }
261
262 if ((gaddr && daddr) ||
263 (memcmp(&gaddr6, &in6addr_any, sizeof(gaddr6)) &&
264 memcmp(&daddr6, &in6addr_any, sizeof(daddr6)))) {
265 fprintf(stderr, "vxlan: both group and remote cannot be specified\n");
266 return -1;
267 }
268
269 if (!dst_port_set) {
270 fprintf(stderr, "vxlan: destination port not specified\n"
271 "Will use Linux kernel default (non-standard value)\n");
272 fprintf(stderr,
273 "Use 'dstport 4789' to get the IANA assigned value\n"
274 "Use 'dstport 0' to get default and quiet this message\n");
275 }
276
277 addattr32(n, 1024, IFLA_VXLAN_ID, vni);
278 if (gaddr)
279 addattr_l(n, 1024, IFLA_VXLAN_GROUP, &gaddr, 4);
280 else if (daddr)
281 addattr_l(n, 1024, IFLA_VXLAN_GROUP, &daddr, 4);
282 if (memcmp(&gaddr6, &in6addr_any, sizeof(gaddr6)) != 0)
283 addattr_l(n, 1024, IFLA_VXLAN_GROUP6, &gaddr6, sizeof(struct in6_addr));
284 else if (memcmp(&daddr6, &in6addr_any, sizeof(daddr6)) != 0)
285 addattr_l(n, 1024, IFLA_VXLAN_GROUP6, &daddr6, sizeof(struct in6_addr));
286
287 if (saddr)
288 addattr_l(n, 1024, IFLA_VXLAN_LOCAL, &saddr, 4);
289 else if (memcmp(&saddr6, &in6addr_any, sizeof(saddr6)) != 0)
290 addattr_l(n, 1024, IFLA_VXLAN_LOCAL6, &saddr6, sizeof(struct in6_addr));
291
292 if (link)
293 addattr32(n, 1024, IFLA_VXLAN_LINK, link);
294 addattr32(n, 1024, IFLA_VXLAN_LABEL, label);
295 addattr8(n, 1024, IFLA_VXLAN_TTL, ttl);
296 addattr8(n, 1024, IFLA_VXLAN_TOS, tos);
297 addattr8(n, 1024, IFLA_VXLAN_LEARNING, learning);
298 addattr8(n, 1024, IFLA_VXLAN_PROXY, proxy);
299 addattr8(n, 1024, IFLA_VXLAN_RSC, rsc);
300 addattr8(n, 1024, IFLA_VXLAN_L2MISS, l2miss);
301 addattr8(n, 1024, IFLA_VXLAN_L3MISS, l3miss);
302 addattr8(n, 1024, IFLA_VXLAN_REMCSUM_TX, remcsumtx);
303 addattr8(n, 1024, IFLA_VXLAN_REMCSUM_RX, remcsumrx);
304 addattr8(n, 1024, IFLA_VXLAN_COLLECT_METADATA, metadata);
305
306 if (udpcsum_set)
307 addattr8(n, 1024, IFLA_VXLAN_UDP_CSUM, udpcsum);
308 if (udp6zerocsumtx_set)
309 addattr8(n, 1024, IFLA_VXLAN_UDP_ZERO_CSUM6_TX, udp6zerocsumtx);
310 if (udp6zerocsumrx_set)
311 addattr8(n, 1024, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, udp6zerocsumrx);
312 if (noage)
313 addattr32(n, 1024, IFLA_VXLAN_AGEING, 0);
314 else if (age)
315 addattr32(n, 1024, IFLA_VXLAN_AGEING, age);
316 if (maxaddr)
317 addattr32(n, 1024, IFLA_VXLAN_LIMIT, maxaddr);
318 if (range.low || range.high)
319 addattr_l(n, 1024, IFLA_VXLAN_PORT_RANGE,
320 &range, sizeof(range));
321 if (dstport)
322 addattr16(n, 1024, IFLA_VXLAN_PORT, htons(dstport));
323
324 if (gbp)
325 addattr_l(n, 1024, IFLA_VXLAN_GBP, NULL, 0);
326
327
328 return 0;
329 }
330
331 static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
332 {
333 __u32 vni;
334 unsigned int link;
335 __u8 tos;
336 __u32 maxaddr;
337 char s2[64];
338
339 if (!tb)
340 return;
341
342 if (!tb[IFLA_VXLAN_ID] ||
343 RTA_PAYLOAD(tb[IFLA_VXLAN_ID]) < sizeof(__u32))
344 return;
345
346 vni = rta_getattr_u32(tb[IFLA_VXLAN_ID]);
347 fprintf(f, "id %u ", vni);
348
349 if (tb[IFLA_VXLAN_GROUP]) {
350 __be32 addr = rta_getattr_u32(tb[IFLA_VXLAN_GROUP]);
351
352 if (addr) {
353 if (IN_MULTICAST(ntohl(addr)))
354 fprintf(f, "group %s ",
355 format_host(AF_INET, 4, &addr));
356 else
357 fprintf(f, "remote %s ",
358 format_host(AF_INET, 4, &addr));
359 }
360 } else if (tb[IFLA_VXLAN_GROUP6]) {
361 struct in6_addr addr;
362
363 memcpy(&addr, RTA_DATA(tb[IFLA_VXLAN_GROUP6]), sizeof(struct in6_addr));
364 if (memcmp(&addr, &in6addr_any, sizeof(addr)) != 0) {
365 if (IN6_IS_ADDR_MULTICAST(&addr))
366 fprintf(f, "group %s ",
367 format_host(AF_INET6, sizeof(struct in6_addr), &addr));
368 else
369 fprintf(f, "remote %s ",
370 format_host(AF_INET6, sizeof(struct in6_addr), &addr));
371 }
372 }
373
374 if (tb[IFLA_VXLAN_LOCAL]) {
375 __be32 addr = rta_getattr_u32(tb[IFLA_VXLAN_LOCAL]);
376
377 if (addr)
378 fprintf(f, "local %s ",
379 format_host(AF_INET, 4, &addr));
380 } else if (tb[IFLA_VXLAN_LOCAL6]) {
381 struct in6_addr addr;
382
383 memcpy(&addr, RTA_DATA(tb[IFLA_VXLAN_LOCAL6]), sizeof(struct in6_addr));
384 if (memcmp(&addr, &in6addr_any, sizeof(addr)) != 0)
385 fprintf(f, "local %s ",
386 format_host(AF_INET6, sizeof(struct in6_addr), &addr));
387 }
388
389 if (tb[IFLA_VXLAN_LINK] &&
390 (link = rta_getattr_u32(tb[IFLA_VXLAN_LINK]))) {
391 const char *n = if_indextoname(link, s2);
392
393 if (n)
394 fprintf(f, "dev %s ", n);
395 else
396 fprintf(f, "dev %u ", link);
397 }
398
399 if (tb[IFLA_VXLAN_PORT_RANGE]) {
400 const struct ifla_vxlan_port_range *r
401 = RTA_DATA(tb[IFLA_VXLAN_PORT_RANGE]);
402 fprintf(f, "srcport %u %u ", ntohs(r->low), ntohs(r->high));
403 }
404
405 if (tb[IFLA_VXLAN_PORT])
406 fprintf(f, "dstport %u ",
407 ntohs(rta_getattr_u16(tb[IFLA_VXLAN_PORT])));
408
409 if (tb[IFLA_VXLAN_LEARNING] &&
410 !rta_getattr_u8(tb[IFLA_VXLAN_LEARNING]))
411 fputs("nolearning ", f);
412
413 if (tb[IFLA_VXLAN_PROXY] && rta_getattr_u8(tb[IFLA_VXLAN_PROXY]))
414 fputs("proxy ", f);
415
416 if (tb[IFLA_VXLAN_RSC] && rta_getattr_u8(tb[IFLA_VXLAN_RSC]))
417 fputs("rsc ", f);
418
419 if (tb[IFLA_VXLAN_L2MISS] && rta_getattr_u8(tb[IFLA_VXLAN_L2MISS]))
420 fputs("l2miss ", f);
421
422 if (tb[IFLA_VXLAN_L3MISS] && rta_getattr_u8(tb[IFLA_VXLAN_L3MISS]))
423 fputs("l3miss ", f);
424
425 if (tb[IFLA_VXLAN_TOS] &&
426 (tos = rta_getattr_u8(tb[IFLA_VXLAN_TOS]))) {
427 if (tos == 1)
428 fprintf(f, "tos inherit ");
429 else
430 fprintf(f, "tos %#x ", tos);
431 }
432
433 if (tb[IFLA_VXLAN_TTL]) {
434 __u8 ttl = rta_getattr_u8(tb[IFLA_VXLAN_TTL]);
435
436 if (ttl)
437 fprintf(f, "ttl %d ", ttl);
438 }
439
440 if (tb[IFLA_VXLAN_LABEL]) {
441 __u32 label = rta_getattr_u32(tb[IFLA_VXLAN_LABEL]);
442
443 if (label)
444 fprintf(f, "flowlabel %#x ", ntohl(label));
445 }
446
447 if (tb[IFLA_VXLAN_AGEING]) {
448 __u32 age = rta_getattr_u32(tb[IFLA_VXLAN_AGEING]);
449
450 if (age == 0)
451 fprintf(f, "ageing none ");
452 else
453 fprintf(f, "ageing %u ", age);
454 }
455
456 if (tb[IFLA_VXLAN_LIMIT] &&
457 ((maxaddr = rta_getattr_u32(tb[IFLA_VXLAN_LIMIT])) != 0))
458 fprintf(f, "maxaddr %u ", maxaddr);
459
460 if (tb[IFLA_VXLAN_UDP_CSUM]) {
461 if (!rta_getattr_u8(tb[IFLA_VXLAN_UDP_CSUM]))
462 fputs("no", f);
463 fputs("udpcsum ", f);
464 }
465
466 if (tb[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) {
467 if (!rta_getattr_u8(tb[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
468 fputs("no", f);
469 fputs("udp6zerocsumtx ", f);
470 }
471
472 if (tb[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) {
473 if (!rta_getattr_u8(tb[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
474 fputs("no", f);
475 fputs("udp6zerocsumrx ", f);
476 }
477
478 if (tb[IFLA_VXLAN_REMCSUM_TX] &&
479 rta_getattr_u8(tb[IFLA_VXLAN_REMCSUM_TX]))
480 fputs("remcsumtx ", f);
481
482 if (tb[IFLA_VXLAN_REMCSUM_RX] &&
483 rta_getattr_u8(tb[IFLA_VXLAN_REMCSUM_RX]))
484 fputs("remcsumrx ", f);
485
486 if (tb[IFLA_VXLAN_COLLECT_METADATA] &&
487 rta_getattr_u8(tb[IFLA_VXLAN_COLLECT_METADATA]))
488 fputs("external ", f);
489
490 if (tb[IFLA_VXLAN_GBP])
491 fputs("gbp ", f);
492 }
493
494 static void vxlan_print_help(struct link_util *lu, int argc, char **argv,
495 FILE *f)
496 {
497 print_explain(f);
498 }
499
500 struct link_util vxlan_link_util = {
501 .id = "vxlan",
502 .maxattr = IFLA_VXLAN_MAX,
503 .parse_opt = vxlan_parse_opt,
504 .print_opt = vxlan_print_opt,
505 .print_help = vxlan_print_help,
506 };