2 * iplink_geneve.c GENEVE device support
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.
9 * Authors: John W. Linville <linville@tuxdriver.com>
16 #include "ip_common.h"
18 #define GENEVE_ATTRSET(attrs, type) (((attrs) & (1L << (type))) != 0)
20 static void print_explain(FILE *f
)
23 "Usage: ... geneve id VNI\n"
27 " [ flowlabel LABEL ]\n"
31 " [ [no]udp6zerocsumtx ]\n"
32 " [ [no]udp6zerocsumrx ]\n"
34 "Where: VNI := 0-16777215\n"
35 " ADDR := IP_ADDRESS\n"
36 " TOS := { NUMBER | inherit }\n"
37 " TTL := { 1..255 | inherit }\n"
38 " LABEL := 0-1048575\n"
42 static void explain(void)
44 print_explain(stderr
);
47 static void check_duparg(__u64
*attrs
, int type
, const char *key
,
50 if (!GENEVE_ATTRSET(*attrs
, type
)) {
51 *attrs
|= (1L << type
);
57 static int geneve_parse_opt(struct link_util
*lu
, int argc
, char **argv
,
68 __u8 udp6zerocsumtx
= 0;
69 __u8 udp6zerocsumrx
= 0;
71 bool set_op
= (n
->nlmsg_type
== RTM_NEWLINK
&&
72 !(n
->nlmsg_flags
& NLM_F_CREATE
));
77 if (!matches(*argv
, "id") ||
78 !matches(*argv
, "vni")) {
80 check_duparg(&attrs
, IFLA_GENEVE_ID
, "id", *argv
);
81 if (get_u32(&vni
, *argv
, 0) ||
83 invarg("invalid id", *argv
);
84 } else if (!matches(*argv
, "remote")) {
86 check_duparg(&attrs
, IFLA_GENEVE_REMOTE
, "remote",
88 get_addr(&daddr
, *argv
, AF_UNSPEC
);
89 if (!is_addrtype_inet_not_multi(&daddr
))
90 invarg("invalid remote address", *argv
);
91 } else if (!matches(*argv
, "ttl") ||
92 !matches(*argv
, "hoplimit")) {
96 check_duparg(&attrs
, IFLA_GENEVE_TTL
, "ttl", *argv
);
97 if (strcmp(*argv
, "inherit") != 0) {
98 if (get_unsigned(&uval
, *argv
, 0))
99 invarg("invalid TTL", *argv
);
101 invarg("TTL must be <= 255", *argv
);
104 } else if (!matches(*argv
, "tos") ||
105 !matches(*argv
, "dsfield")) {
109 check_duparg(&attrs
, IFLA_GENEVE_TOS
, "tos", *argv
);
110 if (strcmp(*argv
, "inherit") != 0) {
111 if (rtnl_dsfield_a2n(&uval
, *argv
))
112 invarg("bad TOS value", *argv
);
116 } else if (!matches(*argv
, "label") ||
117 !matches(*argv
, "flowlabel")) {
121 check_duparg(&attrs
, IFLA_GENEVE_LABEL
, "flowlabel",
123 if (get_u32(&uval
, *argv
, 0) ||
124 (uval
& ~LABEL_MAX_MASK
))
125 invarg("invalid flowlabel", *argv
);
127 } else if (!matches(*argv
, "dstport")) {
129 check_duparg(&attrs
, IFLA_GENEVE_PORT
, "dstport",
131 if (get_u16(&dstport
, *argv
, 0))
132 invarg("dstport", *argv
);
133 } else if (!matches(*argv
, "external")) {
134 check_duparg(&attrs
, IFLA_GENEVE_COLLECT_METADATA
,
137 } else if (!matches(*argv
, "noexternal")) {
138 check_duparg(&attrs
, IFLA_GENEVE_COLLECT_METADATA
,
141 } else if (!matches(*argv
, "udpcsum")) {
142 check_duparg(&attrs
, IFLA_GENEVE_UDP_CSUM
, *argv
,
145 } else if (!matches(*argv
, "noudpcsum")) {
146 check_duparg(&attrs
, IFLA_GENEVE_UDP_CSUM
, *argv
,
149 } else if (!matches(*argv
, "udp6zerocsumtx")) {
150 check_duparg(&attrs
, IFLA_GENEVE_UDP_ZERO_CSUM6_TX
,
153 } else if (!matches(*argv
, "noudp6zerocsumtx")) {
154 check_duparg(&attrs
, IFLA_GENEVE_UDP_ZERO_CSUM6_TX
,
157 } else if (!matches(*argv
, "udp6zerocsumrx")) {
158 check_duparg(&attrs
, IFLA_GENEVE_UDP_ZERO_CSUM6_RX
,
161 } else if (!matches(*argv
, "noudp6zerocsumrx")) {
162 check_duparg(&attrs
, IFLA_GENEVE_UDP_ZERO_CSUM6_RX
,
165 } else if (matches(*argv
, "help") == 0) {
169 fprintf(stderr
, "geneve: unknown command \"%s\"?\n", *argv
);
176 if (metadata
&& GENEVE_ATTRSET(attrs
, IFLA_GENEVE_ID
)) {
177 fprintf(stderr
, "geneve: both 'external' and vni cannot be specified\n");
182 /* parameter checking make sense only for full geneve tunnels */
183 if (!GENEVE_ATTRSET(attrs
, IFLA_GENEVE_ID
)) {
184 fprintf(stderr
, "geneve: missing virtual network identifier\n");
188 /* If we are modifying the geneve device, then we only need the
189 * ID (VNI) to identify the geneve device, and we do not need
192 if (!set_op
&& !is_addrtype_inet(&daddr
)) {
193 fprintf(stderr
, "geneve: remote link partner not specified\n");
198 addattr32(n
, 1024, IFLA_GENEVE_ID
, vni
);
199 if (is_addrtype_inet(&daddr
)) {
200 int type
= (daddr
.family
== AF_INET
) ? IFLA_GENEVE_REMOTE
:
202 addattr_l(n
, sizeof(1024), type
, daddr
.data
, daddr
.bytelen
);
204 if (!set_op
|| GENEVE_ATTRSET(attrs
, IFLA_GENEVE_LABEL
))
205 addattr32(n
, 1024, IFLA_GENEVE_LABEL
, label
);
206 if (!set_op
|| GENEVE_ATTRSET(attrs
, IFLA_GENEVE_TTL
))
207 addattr8(n
, 1024, IFLA_GENEVE_TTL
, ttl
);
208 if (!set_op
|| GENEVE_ATTRSET(attrs
, IFLA_GENEVE_TOS
))
209 addattr8(n
, 1024, IFLA_GENEVE_TOS
, tos
);
211 addattr16(n
, 1024, IFLA_GENEVE_PORT
, htons(dstport
));
213 addattr(n
, 1024, IFLA_GENEVE_COLLECT_METADATA
);
214 if (GENEVE_ATTRSET(attrs
, IFLA_GENEVE_UDP_CSUM
))
215 addattr8(n
, 1024, IFLA_GENEVE_UDP_CSUM
, udpcsum
);
216 if (GENEVE_ATTRSET(attrs
, IFLA_GENEVE_UDP_ZERO_CSUM6_TX
))
217 addattr8(n
, 1024, IFLA_GENEVE_UDP_ZERO_CSUM6_TX
, udp6zerocsumtx
);
218 if (GENEVE_ATTRSET(attrs
, IFLA_GENEVE_UDP_ZERO_CSUM6_RX
))
219 addattr8(n
, 1024, IFLA_GENEVE_UDP_ZERO_CSUM6_RX
, udp6zerocsumrx
);
224 static void geneve_print_opt(struct link_util
*lu
, FILE *f
, struct rtattr
*tb
[])
233 if (!tb
[IFLA_GENEVE_ID
] ||
234 RTA_PAYLOAD(tb
[IFLA_GENEVE_ID
]) < sizeof(__u32
))
237 vni
= rta_getattr_u32(tb
[IFLA_GENEVE_ID
]);
238 print_uint(PRINT_ANY
, "id", "id %u ", vni
);
240 if (tb
[IFLA_GENEVE_REMOTE
]) {
241 __be32 addr
= rta_getattr_u32(tb
[IFLA_GENEVE_REMOTE
]);
244 print_string(PRINT_ANY
,
247 format_host(AF_INET
, 4, &addr
));
248 } else if (tb
[IFLA_GENEVE_REMOTE6
]) {
249 struct in6_addr addr
;
251 memcpy(&addr
, RTA_DATA(tb
[IFLA_GENEVE_REMOTE6
]), sizeof(struct in6_addr
));
252 if (!IN6_IS_ADDR_UNSPECIFIED(&addr
)) {
253 if (!IN6_IS_ADDR_MULTICAST(&addr
))
254 print_string(PRINT_ANY
,
257 format_host(AF_INET6
,
258 sizeof(struct in6_addr
),
263 if (tb
[IFLA_GENEVE_TTL
])
264 ttl
= rta_getattr_u8(tb
[IFLA_GENEVE_TTL
]);
265 if (is_json_context() || ttl
)
266 print_uint(PRINT_ANY
, "ttl", "ttl %u ", ttl
);
268 print_string(PRINT_FP
, NULL
, "ttl %s ", "inherit");
270 if (tb
[IFLA_GENEVE_TOS
])
271 tos
= rta_getattr_u8(tb
[IFLA_GENEVE_TOS
]);
273 if (is_json_context() || tos
!= 1)
274 print_0xhex(PRINT_ANY
, "tos", "tos 0x%x ", tos
);
276 print_string(PRINT_FP
, NULL
, "tos %s ", "inherit");
279 if (tb
[IFLA_GENEVE_LABEL
]) {
280 __u32 label
= rta_getattr_u32(tb
[IFLA_GENEVE_LABEL
]);
283 print_0xhex(PRINT_ANY
,
289 if (tb
[IFLA_GENEVE_PORT
])
290 print_uint(PRINT_ANY
,
293 rta_getattr_be16(tb
[IFLA_GENEVE_PORT
]));
295 if (tb
[IFLA_GENEVE_COLLECT_METADATA
])
296 print_bool(PRINT_ANY
, "collect_metadata", "external ", true);
298 if (tb
[IFLA_GENEVE_UDP_CSUM
]) {
299 if (is_json_context()) {
300 print_bool(PRINT_JSON
,
303 rta_getattr_u8(tb
[IFLA_GENEVE_UDP_CSUM
]));
305 if (!rta_getattr_u8(tb
[IFLA_GENEVE_UDP_CSUM
]))
307 fputs("udpcsum ", f
);
311 if (tb
[IFLA_GENEVE_UDP_ZERO_CSUM6_TX
]) {
312 if (is_json_context()) {
313 print_bool(PRINT_JSON
,
316 rta_getattr_u8(tb
[IFLA_GENEVE_UDP_ZERO_CSUM6_TX
]));
318 if (!rta_getattr_u8(tb
[IFLA_GENEVE_UDP_ZERO_CSUM6_TX
]))
320 fputs("udp6zerocsumtx ", f
);
324 if (tb
[IFLA_GENEVE_UDP_ZERO_CSUM6_RX
]) {
325 if (is_json_context()) {
326 print_bool(PRINT_JSON
,
329 rta_getattr_u8(tb
[IFLA_GENEVE_UDP_ZERO_CSUM6_RX
]));
331 if (!rta_getattr_u8(tb
[IFLA_GENEVE_UDP_ZERO_CSUM6_RX
]))
333 fputs("udp6zerocsumrx ", f
);
338 static void geneve_print_help(struct link_util
*lu
, int argc
, char **argv
,
344 struct link_util geneve_link_util
= {
346 .maxattr
= IFLA_GENEVE_MAX
,
347 .parse_opt
= geneve_parse_opt
,
348 .print_opt
= geneve_print_opt
,
349 .print_help
= geneve_print_help
,