2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 #include <vnet/vxlan/vxlan.h>
16 #include <vnet/ip/format.h>
18 vxlan_main_t vxlan_main;
20 static u8 * format_decap_next (u8 * s, va_list * args)
22 u32 next_index = va_arg (*args, u32);
26 case VXLAN_INPUT_NEXT_DROP:
27 return format (s, "drop");
28 case VXLAN_INPUT_NEXT_L2_INPUT:
29 return format (s, "l2");
30 case VXLAN_INPUT_NEXT_IP4_INPUT:
31 return format (s, "ip4");
32 case VXLAN_INPUT_NEXT_IP6_INPUT:
33 return format (s, "ip6");
35 return format (s, "unknown %d", next_index);
40 u8 * format_vxlan_tunnel (u8 * s, va_list * args)
42 vxlan_tunnel_t * t = va_arg (*args, vxlan_tunnel_t *);
43 vxlan_main_t * ngm = &vxlan_main;
46 "[%d] %U (src) %U (dst) vni %d encap_fib_index %d",
48 format_ip46_address, &t->src,
49 format_ip46_address, &t->dst,
52 s = format (s, " decap_next %U\n", format_decap_next, t->decap_next_index);
56 static u8 * format_vxlan_name (u8 * s, va_list * args)
58 u32 dev_instance = va_arg (*args, u32);
59 return format (s, "vxlan_tunnel%d", dev_instance);
62 static uword dummy_interface_tx (vlib_main_t * vm,
63 vlib_node_runtime_t * node,
66 clib_warning ("you shouldn't be here, leaking buffers...");
67 return frame->n_vectors;
71 vxlan_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
73 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
74 vnet_hw_interface_set_flags (vnm, hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP);
76 vnet_hw_interface_set_flags (vnm, hw_if_index, 0);
78 return /* no error */ 0;
81 VNET_DEVICE_CLASS (vxlan_device_class,static) = {
83 .format_device_name = format_vxlan_name,
84 .format_tx_trace = format_vxlan_encap_trace,
85 .tx_function = dummy_interface_tx,
86 .admin_up_down_function = vxlan_interface_admin_up_down,
89 static uword dummy_set_rewrite (vnet_main_t * vnm,
94 uword max_rewrite_bytes)
99 static u8 * format_vxlan_header_with_length (u8 * s, va_list * args)
101 u32 dev_instance = va_arg (*args, u32);
102 s = format (s, "unimplemented dev %u", dev_instance);
106 VNET_HW_INTERFACE_CLASS (vxlan_hw_class) = {
108 .format_header = format_vxlan_header_with_length,
109 .set_rewrite = dummy_set_rewrite,
112 #define foreach_copy_field \
117 #define foreach_copy_ipv4 { \
122 #define foreach_copy_ipv6 { \
123 _(src.ip6.as_u64[0]) \
124 _(src.ip6.as_u64[1]) \
125 _(dst.ip6.as_u64[0]) \
126 _(dst.ip6.as_u64[1]) \
129 static int vxlan4_rewrite (vxlan_tunnel_t * t)
133 ip4_vxlan_header_t * h0;
134 int len = sizeof (*h0);
136 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
138 h0 = (ip4_vxlan_header_t *) rw;
140 /* Fixed portion of the (outer) ip4 header */
142 ip0->ip_version_and_header_length = 0x45;
144 ip0->protocol = IP_PROTOCOL_UDP;
146 /* we fix up the ip4 header length and checksum after-the-fact */
147 ip0->src_address.as_u32 = t->src.ip4.as_u32;
148 ip0->dst_address.as_u32 = t->dst.ip4.as_u32;
149 ip0->checksum = ip4_header_checksum (ip0);
151 /* UDP header, randomize src port on something, maybe? */
152 h0->udp.src_port = clib_host_to_net_u16 (4789);
153 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan);
156 vnet_set_vni_and_flags(&h0->vxlan, t->vni);
162 static int vxlan6_rewrite (vxlan_tunnel_t * t)
166 ip6_vxlan_header_t * h0;
167 int len = sizeof (*h0);
169 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
171 h0 = (ip6_vxlan_header_t *) rw;
173 /* Fixed portion of the (outer) ip6 header */
175 ip0->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32(6 << 28);
176 ip0->hop_limit = 255;
177 ip0->protocol = IP_PROTOCOL_UDP;
179 ip0->src_address.as_u64[0] = t->src.ip6.as_u64[0];
180 ip0->src_address.as_u64[1] = t->src.ip6.as_u64[1];
181 ip0->dst_address.as_u64[0] = t->dst.ip6.as_u64[0];
182 ip0->dst_address.as_u64[1] = t->dst.ip6.as_u64[1];
184 /* UDP header, randomize src port on something, maybe? */
185 h0->udp.src_port = clib_host_to_net_u16 (4789);
186 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan);
189 vnet_set_vni_and_flags(&h0->vxlan, t->vni);
195 int vnet_vxlan_add_del_tunnel
196 (vnet_vxlan_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
198 vxlan_main_t * vxm = &vxlan_main;
199 vxlan_tunnel_t *t = 0;
200 vnet_main_t * vnm = vxm->vnet_main;
201 ip4_main_t * im4 = &ip4_main;
202 ip6_main_t * im6 = &ip6_main;
203 vnet_hw_interface_t * hi;
205 u32 hw_if_index = ~0;
206 u32 sw_if_index = ~0;
208 vxlan4_tunnel_key_t key4;
209 vxlan6_tunnel_key_t key6;
212 key4.src = a->dst.ip4.as_u32; /* decap src in key is encap dst in config */
213 key4.vni = clib_host_to_net_u32 (a->vni << 8);
215 p = hash_get (vxm->vxlan4_tunnel_by_key, key4.as_u64);
217 key6.src.as_u64[0] = a->dst.ip6.as_u64[0];
218 key6.src.as_u64[1] = a->dst.ip6.as_u64[1];
219 key6.vni = clib_host_to_net_u32 (a->vni << 8);
221 p = hash_get (vxm->vxlan6_tunnel_by_key, pointer_to_uword(&key6));
226 /* adding a tunnel: tunnel must not already exist */
228 return VNET_API_ERROR_TUNNEL_EXIST;
230 if (a->decap_next_index == ~0)
231 a->decap_next_index = VXLAN_INPUT_NEXT_L2_INPUT;
233 if (a->decap_next_index >= VXLAN_INPUT_N_NEXT)
234 return VNET_API_ERROR_INVALID_DECAP_NEXT;
236 pool_get_aligned (vxm->tunnels, t, CLIB_CACHE_LINE_BYTES);
237 memset (t, 0, sizeof (*t));
239 /* copy from arg structure */
240 #define _(x) t->x = a->x;
242 if (!a->is_ip6) foreach_copy_ipv4
243 else foreach_copy_ipv6
251 if (!a->is_ip6) t->flags |= VXLAN_TUNNEL_IS_IPV4;
254 rv = vxlan4_rewrite (t);
256 rv = vxlan6_rewrite (t);
261 pool_put (vxm->tunnels, t);
266 hash_set (vxm->vxlan4_tunnel_by_key, key4.as_u64, t - vxm->tunnels);
268 hash_set (vxm->vxlan6_tunnel_by_key, pointer_to_uword(&t->key6), t - vxm->tunnels);
270 if (vec_len (vxm->free_vxlan_tunnel_hw_if_indices) > 0)
272 vnet_interface_main_t * im = &vnm->interface_main;
273 hw_if_index = vxm->free_vxlan_tunnel_hw_if_indices
274 [vec_len (vxm->free_vxlan_tunnel_hw_if_indices)-1];
275 _vec_len (vxm->free_vxlan_tunnel_hw_if_indices) -= 1;
277 hi = vnet_get_hw_interface (vnm, hw_if_index);
278 hi->dev_instance = t - vxm->tunnels;
279 hi->hw_instance = hi->dev_instance;
281 /* clear old stats of freed tunnel before reuse */
282 sw_if_index = hi->sw_if_index;
283 vnet_interface_counter_lock(im);
284 vlib_zero_combined_counter
285 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], sw_if_index);
286 vlib_zero_combined_counter
287 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], sw_if_index);
288 vlib_zero_simple_counter
289 (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index);
290 vnet_interface_counter_unlock(im);
294 hw_if_index = vnet_register_interface
295 (vnm, vxlan_device_class.index, t - vxm->tunnels,
296 vxlan_hw_class.index, t - vxm->tunnels);
297 hi = vnet_get_hw_interface (vnm, hw_if_index);
298 hi->output_node_index = vxlan_encap_node.index;
301 t->hw_if_index = hw_if_index;
302 t->sw_if_index = sw_if_index = hi->sw_if_index;
304 vec_validate_init_empty (vxm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
305 vxm->tunnel_index_by_sw_if_index[sw_if_index] = t - vxm->tunnels;
307 if (a->decap_next_index == VXLAN_INPUT_NEXT_L2_INPUT)
309 l2input_main_t * l2im = &l2input_main;
310 /* setup l2 input config with l2 feature and bd 0 to drop packet */
311 vec_validate (l2im->configs, sw_if_index);
312 l2im->configs[sw_if_index].feature_bitmap = L2INPUT_FEAT_DROP;
313 l2im->configs[sw_if_index].bd_index = 0;
315 vnet_sw_interface_set_flags (vnm, sw_if_index,
316 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
318 vec_validate (im4->fib_index_by_sw_if_index, sw_if_index);
319 im4->fib_index_by_sw_if_index[sw_if_index] = t->encap_fib_index;
321 vec_validate (im6->fib_index_by_sw_if_index, sw_if_index);
322 im6->fib_index_by_sw_if_index[sw_if_index] = t->encap_fib_index;
327 /* deleting a tunnel: tunnel must exist */
329 return VNET_API_ERROR_NO_SUCH_ENTRY;
331 t = pool_elt_at_index (vxm->tunnels, p[0]);
333 vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
334 /* make sure tunnel is removed from l2 bd or xconnect */
335 set_int_l2_mode(vxm->vlib_main, vnm, MODE_L3, t->sw_if_index, 0, 0, 0, 0);
336 vec_add1 (vxm->free_vxlan_tunnel_hw_if_indices, t->hw_if_index);
338 vxm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0;
341 hash_unset (vxm->vxlan4_tunnel_by_key, key4.as_u64);
343 hash_unset (vxm->vxlan6_tunnel_by_key, pointer_to_uword(&key6));
345 vec_free (t->rewrite);
347 t->rewrite = vxlan4_dummy_rewrite;
349 t->rewrite = vxlan6_dummy_rewrite;
351 pool_put (vxm->tunnels, t);
355 *sw_if_indexp = sw_if_index;
360 static u32 fib4_index_from_fib_id (u32 fib_id)
362 ip4_main_t * im = &ip4_main;
365 p = hash_get (im->fib_index_by_table_id, fib_id);
372 static u32 fib6_index_from_fib_id (u32 fib_id)
374 ip6_main_t * im = &ip6_main;
377 p = hash_get (im->fib_index_by_table_id, fib_id);
384 static uword unformat_decap_next (unformat_input_t * input, va_list * args)
386 u32 * result = va_arg (*args, u32 *);
389 if (unformat (input, "l2"))
390 *result = VXLAN_INPUT_NEXT_L2_INPUT;
391 else if (unformat (input, "drop"))
392 *result = VXLAN_INPUT_NEXT_DROP;
393 else if (unformat (input, "ip4"))
394 *result = VXLAN_INPUT_NEXT_IP4_INPUT;
395 else if (unformat (input, "ip6"))
396 *result = VXLAN_INPUT_NEXT_IP6_INPUT;
397 else if (unformat (input, "%d", &tmp))
404 static clib_error_t *
405 vxlan_add_del_tunnel_command_fn (vlib_main_t * vm,
406 unformat_input_t * input,
407 vlib_cli_command_t * cmd)
409 unformat_input_t _line_input, * line_input = &_line_input;
410 ip46_address_t src, dst;
416 u32 encap_fib_index = 0;
417 u32 decap_next_index = ~0;
421 vnet_vxlan_add_del_tunnel_args_t _a, * a = &_a;
424 /* Get a line of input. */
425 if (! unformat_user (input, unformat_line_input, line_input))
428 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
429 if (unformat (line_input, "del"))
433 else if (unformat (line_input, "src %U",
434 unformat_ip4_address, &src.ip4))
439 else if (unformat (line_input, "dst %U",
440 unformat_ip4_address, &dst.ip4))
445 else if (unformat (line_input, "src %U",
446 unformat_ip6_address, &src.ip6))
451 else if (unformat (line_input, "dst %U",
452 unformat_ip6_address, &dst.ip6))
457 else if (unformat (line_input, "encap-vrf-id %d", &tmp))
460 encap_fib_index = fib6_index_from_fib_id (tmp);
462 encap_fib_index = fib4_index_from_fib_id (tmp);
463 if (encap_fib_index == ~0)
464 return clib_error_return (0, "nonexistent encap-vrf-id %d", tmp);
466 else if (unformat (line_input, "decap-next %U", unformat_decap_next,
469 else if (unformat (line_input, "vni %d", &vni))
472 return clib_error_return (0, "vni %d out of range", vni);
475 return clib_error_return (0, "parse error: '%U'",
476 format_unformat_error, line_input);
479 unformat_free (line_input);
482 return clib_error_return (0, "tunnel src address not specified");
485 return clib_error_return (0, "tunnel dst address not specified");
487 if (ipv4_set && ipv6_set)
488 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
490 if ((ipv4_set && memcmp(&src.ip4, &dst.ip4, sizeof(src.ip4)) == 0) ||
491 (ipv6_set && memcmp(&src.ip6, &dst.ip6, sizeof(src.ip6)) == 0))
492 return clib_error_return (0, "src and dst addresses are identical");
495 return clib_error_return (0, "vni not specified");
497 memset (a, 0, sizeof (*a));
500 a->is_ip6 = ipv6_set;
502 #define _(x) a->x = x;
504 if (ipv4_set) foreach_copy_ipv4
505 else foreach_copy_ipv6
508 rv = vnet_vxlan_add_del_tunnel (a, &sw_if_index);
514 vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index);
516 case VNET_API_ERROR_INVALID_DECAP_NEXT:
517 return clib_error_return (0, "invalid decap-next...");
519 case VNET_API_ERROR_TUNNEL_EXIST:
520 return clib_error_return (0, "tunnel already exists...");
522 case VNET_API_ERROR_NO_SUCH_ENTRY:
523 return clib_error_return (0, "tunnel does not exist...");
526 return clib_error_return
527 (0, "vnet_vxlan_add_del_tunnel returned %d", rv);
533 VLIB_CLI_COMMAND (create_vxlan_tunnel_command, static) = {
534 .path = "create vxlan tunnel",
536 "create vxlan tunnel src <local-vtep-addr> dst <remote-vtep-addr> vni <nn>"
537 " [encap-vrf-id <nn>] [decap-next [l2|ip4|ip6] [del]\n",
538 .function = vxlan_add_del_tunnel_command_fn,
541 static clib_error_t *
542 show_vxlan_tunnel_command_fn (vlib_main_t * vm,
543 unformat_input_t * input,
544 vlib_cli_command_t * cmd)
546 vxlan_main_t * vxm = &vxlan_main;
549 if (pool_elts (vxm->tunnels) == 0)
550 vlib_cli_output (vm, "No vxlan tunnels configured...");
552 pool_foreach (t, vxm->tunnels,
554 vlib_cli_output (vm, "%U", format_vxlan_tunnel, t);
560 VLIB_CLI_COMMAND (show_vxlan_tunnel_command, static) = {
561 .path = "show vxlan tunnel",
562 .function = show_vxlan_tunnel_command_fn,
566 clib_error_t *vxlan_init (vlib_main_t *vm)
568 vxlan_main_t * vxm = &vxlan_main;
569 ip4_vxlan_header_t * hdr4;
571 ip6_vxlan_header_t * hdr6;
574 vxm->vnet_main = vnet_get_main();
577 /* initialize the ip6 hash */
578 vxm->vxlan6_tunnel_by_key = hash_create_mem(0,
579 sizeof(vxlan6_tunnel_key_t),
582 /* init dummy rewrite string for deleted vxlan tunnels */
583 _vec_len(vxlan4_dummy_rewrite) = sizeof(ip4_vxlan_header_t);
584 hdr4 = (ip4_vxlan_header_t *) vxlan4_dummy_rewrite;
586 /* minimal rewrite setup, see vxlan_rewite() above as reference */
587 ip4->ip_version_and_header_length = 0x45;
588 ip4->checksum = ip4_header_checksum (ip4);
590 /* Same again for IPv6 */
591 _vec_len(vxlan6_dummy_rewrite) = sizeof(ip6_vxlan_header_t);
592 hdr6 = (ip6_vxlan_header_t *) vxlan6_dummy_rewrite;
594 /* minimal rewrite setup, see vxlan_rewite() above as reference */
595 ip6->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32(6 << 28);
597 udp_register_dst_port (vm, UDP_DST_PORT_vxlan,
598 vxlan4_input_node.index, /* is_ip4 */ 1);
599 udp_register_dst_port (vm, UDP_DST_PORT_vxlan6,
600 vxlan6_input_node.index, /* is_ip4 */ 0);
604 VLIB_INIT_FUNCTION(vxlan_init);