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.
17 * @brief Common utility functions for IPv4 and IPv6 VXLAN GPE tunnels
20 #include <vnet/vxlan-gpe/vxlan_gpe.h>
21 #include <vnet/ip/format.h>
23 vxlan_gpe_main_t vxlan_gpe_main;
26 * @brief Tracing function for VXLAN GPE tunnel packets
28 * @param *s formatting string
31 * @return *s formatted string
34 u8 * format_vxlan_gpe_tunnel (u8 * s, va_list * args)
36 vxlan_gpe_tunnel_t * t = va_arg (*args, vxlan_gpe_tunnel_t *);
37 vxlan_gpe_main_t * gm = &vxlan_gpe_main;
39 s = format (s, "[%d] local: %U remote: %U ",
41 format_ip46_address, &t->local, IP46_TYPE_ANY,
42 format_ip46_address, &t->remote, IP46_TYPE_ANY);
44 s = format (s, " vxlan VNI %d ", t->vni);
48 case VXLAN_GPE_PROTOCOL_IP4:
49 s = format (s, "next-protocol ip4");
51 case VXLAN_GPE_PROTOCOL_IP6:
52 s = format (s, "next-protocol ip6");
54 case VXLAN_GPE_PROTOCOL_ETHERNET:
55 s = format (s, "next-protocol ethernet");
57 case VXLAN_GPE_PROTOCOL_NSH:
58 s = format (s, "next-protocol nsh");
61 s = format (s, "next-protocol unknown %d", t->protocol);
64 s = format (s, " fibs: (encap %d, decap %d)",
72 * @brief Naming for VXLAN GPE tunnel
74 * @param *s formatting string
77 * @return *s formatted string
80 static u8 * format_vxlan_gpe_name (u8 * s, va_list * args)
82 u32 dev_instance = va_arg (*args, u32);
83 return format (s, "vxlan_gpe_tunnel%d", dev_instance);
86 static uword dummy_interface_tx (vlib_main_t * vm,
87 vlib_node_runtime_t * node,
90 clib_warning ("you shouldn't be here, leaking buffers...");
91 return frame->n_vectors;
95 * @brief CLI function for VXLAN GPE admin up/down
104 static clib_error_t *
105 vxlan_gpe_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
107 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
108 vnet_hw_interface_set_flags (vnm, hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP);
110 vnet_hw_interface_set_flags (vnm, hw_if_index, 0);
115 VNET_DEVICE_CLASS (vxlan_gpe_device_class,static) = {
117 .format_device_name = format_vxlan_gpe_name,
118 .format_tx_trace = format_vxlan_gpe_encap_trace,
119 .tx_function = dummy_interface_tx,
120 .admin_up_down_function = vxlan_gpe_interface_admin_up_down,
123 static uword dummy_set_rewrite (vnet_main_t * vnm,
128 uword max_rewrite_bytes)
135 * @brief Formatting function for tracing VXLAN GPE with length
143 static u8 * format_vxlan_gpe_header_with_length (u8 * s, va_list * args)
145 u32 dev_instance = va_arg (*args, u32);
146 s = format (s, "unimplemented dev %u", dev_instance);
150 VNET_HW_INTERFACE_CLASS (vxlan_gpe_hw_class) = {
152 .format_header = format_vxlan_gpe_header_with_length,
153 .set_rewrite = dummy_set_rewrite,
157 #define foreach_gpe_copy_field \
163 #define foreach_copy_ipv4 { \
164 _(local.ip4.as_u32) \
165 _(remote.ip4.as_u32) \
168 #define foreach_copy_ipv6 { \
169 _(local.ip6.as_u64[0]) \
170 _(local.ip6.as_u64[1]) \
171 _(remote.ip6.as_u64[0]) \
172 _(remote.ip6.as_u64[1]) \
177 * @brief Calculate IPv4 VXLAN GPE rewrite header
184 static int vxlan4_gpe_rewrite (vxlan_gpe_tunnel_t * t)
188 ip4_vxlan_gpe_header_t * h0;
193 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
195 h0 = (ip4_vxlan_gpe_header_t *) rw;
197 /* Fixed portion of the (outer) ip4 header */
199 ip0->ip_version_and_header_length = 0x45;
201 ip0->protocol = IP_PROTOCOL_UDP;
203 /* we fix up the ip4 header length and checksum after-the-fact */
204 ip0->src_address.as_u32 = t->local.ip4.as_u32;
205 ip0->dst_address.as_u32 = t->remote.ip4.as_u32;
206 ip0->checksum = ip4_header_checksum (ip0);
208 /* UDP header, randomize src port on something, maybe? */
209 h0->udp.src_port = clib_host_to_net_u16 (4790);
210 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan_gpe);
212 /* VXLAN header. Are we having fun yet? */
213 h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
214 h0->vxlan.ver_res = VXLAN_GPE_VERSION;
215 h0->vxlan.protocol = t->protocol;
216 h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni<<8);
223 * @brief Calculate IPv6 VXLAN GPE rewrite header
230 static int vxlan6_gpe_rewrite (vxlan_gpe_tunnel_t * t)
234 ip6_vxlan_gpe_header_t * h0;
239 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
241 h0 = (ip6_vxlan_gpe_header_t *) rw;
243 /* Fixed portion of the (outer) ip4 header */
245 ip0->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32(6 << 28);
246 ip0->hop_limit = 255;
247 ip0->protocol = IP_PROTOCOL_UDP;
249 ip0->src_address.as_u64[0] = t->local.ip6.as_u64[0];
250 ip0->src_address.as_u64[1] = t->local.ip6.as_u64[1];
251 ip0->dst_address.as_u64[0] = t->remote.ip6.as_u64[0];
252 ip0->dst_address.as_u64[1] = t->remote.ip6.as_u64[1];
254 /* UDP header, randomize src port on something, maybe? */
255 h0->udp.src_port = clib_host_to_net_u16 (4790);
256 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan_gpe);
258 /* VXLAN header. Are we having fun yet? */
259 h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
260 h0->vxlan.ver_res = VXLAN_GPE_VERSION;
261 h0->vxlan.protocol = t->protocol;
262 h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni<<8);
269 * @brief Add or Del a VXLAN GPE tunnel
272 * @param *sw_if_index
277 int vnet_vxlan_gpe_add_del_tunnel
278 (vnet_vxlan_gpe_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
280 vxlan_gpe_main_t * gm = &vxlan_gpe_main;
281 vxlan_gpe_tunnel_t *t = 0;
282 vnet_main_t * vnm = gm->vnet_main;
283 vnet_hw_interface_t * hi;
285 u32 hw_if_index = ~0;
286 u32 sw_if_index = ~0;
288 vxlan4_gpe_tunnel_key_t key4, *key4_copy;
289 vxlan6_gpe_tunnel_key_t key6, *key6_copy;
294 key4.local = a->local.ip4.as_u32;
295 key4.remote = a->remote.ip4.as_u32;
296 key4.vni = clib_host_to_net_u32 (a->vni << 8);
299 p = hash_get_mem(gm->vxlan4_gpe_tunnel_by_key, &key4);
303 key6.local.as_u64[0] = a->local.ip6.as_u64[0];
304 key6.local.as_u64[1] = a->local.ip6.as_u64[1];
305 key6.remote.as_u64[0] = a->remote.ip6.as_u64[0];
306 key6.remote.as_u64[1] = a->remote.ip6.as_u64[1];
307 key6.vni = clib_host_to_net_u32 (a->vni << 8);
309 p = hash_get_mem(gm->vxlan6_gpe_tunnel_by_key, &key6);
314 /* adding a tunnel: tunnel must not already exist */
316 return VNET_API_ERROR_INVALID_VALUE;
318 pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
319 memset (t, 0, sizeof (*t));
321 /* copy from arg structure */
322 #define _(x) t->x = a->x;
323 foreach_gpe_copy_field;
324 if (!a->is_ip6) foreach_copy_ipv4
325 else foreach_copy_ipv6
328 if (!a->is_ip6) t->flags |= VXLAN_GPE_TUNNEL_IS_IPV4;
331 rv = vxlan4_gpe_rewrite (t);
333 rv = vxlan6_gpe_rewrite (t);
338 pool_put (gm->tunnels, t);
344 key4_copy = clib_mem_alloc (sizeof (*key4_copy));
345 clib_memcpy (key4_copy, &key4, sizeof (*key4_copy));
346 hash_set_mem (gm->vxlan4_gpe_tunnel_by_key, key4_copy,
351 key6_copy = clib_mem_alloc (sizeof (*key6_copy));
352 clib_memcpy (key6_copy, &key6, sizeof (*key6_copy));
353 hash_set_mem (gm->vxlan6_gpe_tunnel_by_key, key6_copy,
357 if (vec_len (gm->free_vxlan_gpe_tunnel_hw_if_indices) > 0)
359 hw_if_index = gm->free_vxlan_gpe_tunnel_hw_if_indices
360 [vec_len (gm->free_vxlan_gpe_tunnel_hw_if_indices)-1];
361 _vec_len (gm->free_vxlan_gpe_tunnel_hw_if_indices) -= 1;
363 hi = vnet_get_hw_interface (vnm, hw_if_index);
364 hi->dev_instance = t - gm->tunnels;
365 hi->hw_instance = hi->dev_instance;
369 hw_if_index = vnet_register_interface
370 (vnm, vxlan_gpe_device_class.index, t - gm->tunnels,
371 vxlan_gpe_hw_class.index, t - gm->tunnels);
372 hi = vnet_get_hw_interface (vnm, hw_if_index);
373 hi->output_node_index = vxlan_gpe_encap_node.index;
376 t->hw_if_index = hw_if_index;
377 t->sw_if_index = sw_if_index = hi->sw_if_index;
378 vec_validate_init_empty (gm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
379 gm->tunnel_index_by_sw_if_index[sw_if_index] = t - gm->tunnels;
381 vnet_sw_interface_set_flags (vnm, hi->sw_if_index,
382 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
386 /* deleting a tunnel: tunnel must exist */
388 return VNET_API_ERROR_NO_SUCH_ENTRY;
390 t = pool_elt_at_index (gm->tunnels, p[0]);
392 vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
393 vec_add1 (gm->free_vxlan_gpe_tunnel_hw_if_indices, t->hw_if_index);
395 gm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0;
399 hp = hash_get_pair (gm->vxlan4_gpe_tunnel_by_key, &key4);
400 key4_copy = (void *)(hp->key);
401 hash_unset_mem (gm->vxlan4_gpe_tunnel_by_key, &key4);
402 clib_mem_free (key4_copy);
406 hp = hash_get_pair (gm->vxlan6_gpe_tunnel_by_key, &key6);
407 key6_copy = (void *)(hp->key);
408 hash_unset_mem (gm->vxlan4_gpe_tunnel_by_key, &key6);
409 clib_mem_free (key6_copy);
412 vec_free (t->rewrite);
413 pool_put (gm->tunnels, t);
417 *sw_if_indexp = sw_if_index;
423 * @brief Find the IPv4 FIB index from the FIB ID
430 static u32 fib4_index_from_fib_id (u32 fib_id)
432 ip4_main_t * im = &ip4_main;
435 p = hash_get (im->fib_index_by_table_id, fib_id);
443 * @brief Find the IPv4 FIB index from the FIB ID
450 static u32 fib6_index_from_fib_id (u32 fib_id)
452 ip6_main_t * im = &ip6_main;
455 p = hash_get (im->fib_index_by_table_id, fib_id);
463 * @brief CLI function for Add/Del of IPv4/IPv6 VXLAN GPE tunnel
472 static clib_error_t *
473 vxlan_gpe_add_del_tunnel_command_fn (vlib_main_t * vm,
474 unformat_input_t * input,
475 vlib_cli_command_t * cmd)
477 unformat_input_t _line_input, * line_input = &_line_input;
479 ip46_address_t local, remote;
484 u32 encap_fib_index = 0;
485 u32 decap_fib_index = 0;
486 u8 protocol = VXLAN_GPE_PROTOCOL_IP4;
491 vnet_vxlan_gpe_add_del_tunnel_args_t _a, * a = &_a;
494 /* Get a line of input. */
495 if (! unformat_user (input, unformat_line_input, line_input))
498 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
499 if (unformat (line_input, "del"))
501 else if (unformat (line_input, "local %U",
502 unformat_ip4_address, &local.ip4))
507 else if (unformat (line_input, "remote %U",
508 unformat_ip4_address, &remote.ip4))
513 else if (unformat (line_input, "local %U",
514 unformat_ip6_address, &local.ip6))
519 else if (unformat (line_input, "remote %U",
520 unformat_ip6_address, &remote.ip6))
525 else if (unformat (line_input, "encap-vrf-id %d", &tmp))
528 encap_fib_index = fib6_index_from_fib_id (tmp);
530 encap_fib_index = fib4_index_from_fib_id (tmp);
532 if (encap_fib_index == ~0)
533 return clib_error_return (0, "nonexistent encap fib id %d", tmp);
535 else if (unformat (line_input, "decap-vrf-id %d", &tmp))
539 decap_fib_index = fib6_index_from_fib_id (tmp);
541 decap_fib_index = fib4_index_from_fib_id (tmp);
543 if (decap_fib_index == ~0)
544 return clib_error_return (0, "nonexistent decap fib id %d", tmp);
546 else if (unformat (line_input, "vni %d", &vni))
548 else if (unformat(line_input, "next-ip4"))
549 protocol = VXLAN_GPE_PROTOCOL_IP4;
550 else if (unformat(line_input, "next-ip6"))
551 protocol = VXLAN_GPE_PROTOCOL_IP6;
552 else if (unformat(line_input, "next-ethernet"))
553 protocol = VXLAN_GPE_PROTOCOL_ETHERNET;
554 else if (unformat(line_input, "next-nsh"))
555 protocol = VXLAN_GPE_PROTOCOL_NSH;
557 return clib_error_return (0, "parse error: '%U'",
558 format_unformat_error, line_input);
561 unformat_free (line_input);
564 return clib_error_return (0, "tunnel local address not specified");
567 return clib_error_return (0, "tunnel remote address not specified");
569 if (ipv4_set && ipv6_set)
570 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
572 if ((ipv4_set && memcmp(&local.ip4, &remote.ip4, sizeof(local.ip4)) == 0) ||
573 (ipv6_set && memcmp(&local.ip6, &remote.ip6, sizeof(local.ip6)) == 0))
574 return clib_error_return (0, "src and dst addresses are identical");
577 return clib_error_return (0, "vni not specified");
579 memset (a, 0, sizeof (*a));
582 a->is_ip6 = ipv6_set;
584 #define _(x) a->x = x;
585 foreach_gpe_copy_field;
586 if (ipv4_set) foreach_copy_ipv4
587 else foreach_copy_ipv6
590 rv = vnet_vxlan_gpe_add_del_tunnel (a, &sw_if_index);
595 vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index);
597 case VNET_API_ERROR_INVALID_DECAP_NEXT:
598 return clib_error_return (0, "invalid decap-next...");
600 case VNET_API_ERROR_TUNNEL_EXIST:
601 return clib_error_return (0, "tunnel already exists...");
603 case VNET_API_ERROR_NO_SUCH_ENTRY:
604 return clib_error_return (0, "tunnel does not exist...");
607 return clib_error_return
608 (0, "vnet_vxlan_gpe_add_del_tunnel returned %d", rv);
614 VLIB_CLI_COMMAND (create_vxlan_gpe_tunnel_command, static) = {
615 .path = "create vxlan-gpe tunnel",
617 "create vxlan-gpe tunnel local <local-addr> remote <remote-addr>"
618 " vni <nn> [next-ip4][next-ip6][next-ethernet][next-nsh]"
619 " [encap-vrf-id <nn>] [decap-vrf-id <nn>]"
621 .function = vxlan_gpe_add_del_tunnel_command_fn,
625 * @brief CLI function for showing VXLAN GPE tunnels
634 static clib_error_t *
635 show_vxlan_gpe_tunnel_command_fn (vlib_main_t * vm,
636 unformat_input_t * input,
637 vlib_cli_command_t * cmd)
639 vxlan_gpe_main_t * gm = &vxlan_gpe_main;
640 vxlan_gpe_tunnel_t * t;
642 if (pool_elts (gm->tunnels) == 0)
643 vlib_cli_output (vm, "No vxlan-gpe tunnels configured.");
645 pool_foreach (t, gm->tunnels,
647 vlib_cli_output (vm, "%U", format_vxlan_gpe_tunnel, t);
653 VLIB_CLI_COMMAND (show_vxlan_gpe_tunnel_command, static) = {
654 .path = "show vxlan-gpe",
655 .function = show_vxlan_gpe_tunnel_command_fn,
659 * @brief Feature init function for VXLAN GPE
666 clib_error_t *vxlan_gpe_init (vlib_main_t *vm)
668 vxlan_gpe_main_t *gm = &vxlan_gpe_main;
670 gm->vnet_main = vnet_get_main();
673 gm->vxlan4_gpe_tunnel_by_key
674 = hash_create_mem (0, sizeof(vxlan4_gpe_tunnel_key_t), sizeof (uword));
676 gm->vxlan6_gpe_tunnel_by_key
677 = hash_create_mem (0, sizeof(vxlan6_gpe_tunnel_key_t), sizeof (uword));
680 udp_register_dst_port (vm, UDP_DST_PORT_vxlan_gpe,
681 vxlan4_gpe_input_node.index, 1 /* is_ip4 */);
682 udp_register_dst_port (vm, UDP_DST_PORT_vxlan6_gpe,
683 vxlan6_gpe_input_node.index, 0 /* is_ip4 */);
687 VLIB_INIT_FUNCTION(vxlan_gpe_init);