2 * gre_interface.c: gre interfaces
4 * Copyright (c) 2012 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <vnet/vnet.h>
19 #include <vnet/pg/pg.h>
20 #include <vnet/gre/gre.h>
21 #include <vnet/ip/format.h>
22 #include <vnet/fib/ip4_fib.h>
23 #include <vnet/fib/ip6_fib.h>
24 #include <vnet/adj/adj_midchain.h>
25 #include <vnet/adj/adj_nbr.h>
26 #include <vnet/mpls/mpls.h>
28 static const char *gre_tunnel_type_names[] = GRE_TUNNEL_TYPE_NAMES;
31 format_gre_tunnel (u8 * s, va_list * args)
33 gre_tunnel_t *t = va_arg (*args, gre_tunnel_t *);
34 gre_main_t *gm = &gre_main;
36 s = format (s, "[%d] src %U dst %U fib-idx %d sw-if-idx %d ",
38 format_ip46_address, &t->tunnel_src, IP46_TYPE_ANY,
39 format_ip46_address, &t->tunnel_dst.fp_addr, IP46_TYPE_ANY,
40 t->outer_fib_index, t->sw_if_index);
42 s = format (s, "payload %s", gre_tunnel_type_names[t->type]);
48 gre_tunnel_db_find (const ip46_address_t * src,
49 const ip46_address_t * dst,
50 u32 out_fib_index, u8 is_ipv6, gre_tunnel_key_t * key)
52 gre_main_t *gm = &gre_main;
57 gre_mk_key4 (&src->ip4, &dst->ip4, out_fib_index, &key->gtk_v4);
58 p = hash_get_mem (gm->tunnel_by_key4, &key->gtk_v4);
62 gre_mk_key6 (&src->ip6, &dst->ip6, out_fib_index, &key->gtk_v6);
63 p = hash_get_mem (gm->tunnel_by_key6, &key->gtk_v6);
69 return (pool_elt_at_index (gm->tunnels, p[0]));
73 gre_tunnel_db_add (gre_tunnel_t * t, gre_tunnel_key_t * key)
75 gre_main_t *gm = &gre_main;
77 t->key = clib_mem_alloc (sizeof (*t->key));
78 clib_memcpy (t->key, key, sizeof (*key));
80 if (t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6)
82 hash_set_mem (gm->tunnel_by_key6, &t->key->gtk_v6, t - gm->tunnels);
86 hash_set_mem (gm->tunnel_by_key4, &t->key->gtk_v4, t - gm->tunnels);
91 gre_tunnel_db_remove (gre_tunnel_t * t)
93 gre_main_t *gm = &gre_main;
95 if (t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6)
97 hash_unset_mem (gm->tunnel_by_key6, &t->key->gtk_v6);
101 hash_unset_mem (gm->tunnel_by_key4, &t->key->gtk_v4);
104 clib_mem_free (t->key);
108 static gre_tunnel_t *
109 gre_tunnel_from_fib_node (fib_node_t * node)
111 ASSERT (FIB_NODE_TYPE_GRE_TUNNEL == node->fn_type);
112 return ((gre_tunnel_t *) (((char *) node) -
113 STRUCT_OFFSET_OF (gre_tunnel_t, node)));
119 * 'stack' (resolve the recursion for) the tunnel's midchain adjacency
122 gre_tunnel_stack (adj_index_t ai)
124 gre_main_t *gm = &gre_main;
130 sw_if_index = adj->rewrite_header.sw_if_index;
132 if ((vec_len (gm->tunnel_index_by_sw_if_index) < sw_if_index) ||
133 (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index]))
136 gt = pool_elt_at_index (gm->tunnels,
137 gm->tunnel_index_by_sw_if_index[sw_if_index]);
140 * find the adjacency that is contributed by the FIB entry
141 * that this tunnel resovles via, and use it as the next adj
144 if (vnet_hw_interface_get_flags (vnet_get_main (),
146 VNET_HW_INTERFACE_FLAG_LINK_UP)
148 adj_nbr_midchain_stack (ai,
149 fib_entry_contribute_ip_forwarding
150 (gt->fib_entry_index));
154 adj_nbr_midchain_unstack (ai);
159 * @brief Call back when restacking all adjacencies on a GRE interface
162 gre_adj_walk_cb (adj_index_t ai, void *ctx)
164 gre_tunnel_stack (ai);
166 return (ADJ_WALK_RC_CONTINUE);
170 gre_tunnel_restack (gre_tunnel_t * gt)
172 fib_protocol_t proto;
175 * walk all the adjacencies on th GRE interface and restack them
177 FOR_EACH_FIB_IP_PROTOCOL (proto)
179 adj_nbr_walk (gt->sw_if_index, proto, gre_adj_walk_cb, NULL);
184 * Function definition to backwalk a FIB node
186 static fib_node_back_walk_rc_t
187 gre_tunnel_back_walk (fib_node_t * node, fib_node_back_walk_ctx_t * ctx)
189 gre_tunnel_restack (gre_tunnel_from_fib_node (node));
191 return (FIB_NODE_BACK_WALK_CONTINUE);
195 * Function definition to get a FIB node from its index
198 gre_tunnel_fib_node_get (fib_node_index_t index)
204 gt = pool_elt_at_index (gm->tunnels, index);
210 * Function definition to inform the FIB node that its last lock has gone.
213 gre_tunnel_last_lock_gone (fib_node_t * node)
216 * The MPLS GRE tunnel is a root of the graph. As such
217 * it never has children and thus is never locked.
223 * Virtual function table registered by MPLS GRE tunnels
224 * for participation in the FIB object graph.
226 const static fib_node_vft_t gre_vft = {
227 .fnv_get = gre_tunnel_fib_node_get,
228 .fnv_last_lock = gre_tunnel_last_lock_gone,
229 .fnv_back_walk = gre_tunnel_back_walk,
233 vnet_gre_tunnel_add (vnet_gre_add_del_tunnel_args_t * a, u32 * sw_if_indexp)
235 gre_main_t *gm = &gre_main;
236 vnet_main_t *vnm = gm->vnet_main;
237 ip4_main_t *im4 = &ip4_main;
238 ip6_main_t *im6 = &ip6_main;
240 vnet_hw_interface_t *hi;
241 u32 hw_if_index, sw_if_index;
245 u8 is_ipv6 = a->is_ipv6;
246 gre_tunnel_key_t key;
249 outer_fib_index = ip4_fib_index_from_table_id (a->outer_fib_id);
251 outer_fib_index = ip6_fib_index_from_table_id (a->outer_fib_id);
253 if (~0 == outer_fib_index)
254 return VNET_API_ERROR_NO_SUCH_FIB;
257 gre_tunnel_db_find (&a->src, &a->dst, outer_fib_index, a->is_ipv6, &key);
260 return VNET_API_ERROR_INVALID_VALUE;
262 pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
263 memset (t, 0, sizeof (*t));
264 fib_node_init (&t->node, FIB_NODE_TYPE_GRE_TUNNEL);
267 t->type = GRE_TUNNEL_TYPE_TEB;
269 t->type = GRE_TUNNEL_TYPE_L3;
271 if (vec_len (gm->free_gre_tunnel_hw_if_indices[t->type]) > 0)
273 vnet_interface_main_t *im = &vnm->interface_main;
275 hw_if_index = gm->free_gre_tunnel_hw_if_indices[t->type]
276 [vec_len (gm->free_gre_tunnel_hw_if_indices[t->type]) - 1];
277 _vec_len (gm->free_gre_tunnel_hw_if_indices[t->type]) -= 1;
279 hi = vnet_get_hw_interface (vnm, hw_if_index);
280 hi->dev_instance = t - gm->tunnels;
281 hi->hw_instance = hi->dev_instance;
283 /* clear old stats of freed tunnel before reuse */
284 sw_if_index = hi->sw_if_index;
285 vnet_interface_counter_lock (im);
286 vlib_zero_combined_counter
287 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX],
289 vlib_zero_combined_counter (&im->combined_sw_if_counters
290 [VNET_INTERFACE_COUNTER_RX], sw_if_index);
291 vlib_zero_simple_counter (&im->sw_if_counters
292 [VNET_INTERFACE_COUNTER_DROP], sw_if_index);
293 vnet_interface_counter_unlock (im);
294 if (GRE_TUNNEL_TYPE_TEB == t->type)
296 t->l2_tx_arc = vlib_node_add_named_next (vlib_get_main (),
303 if (GRE_TUNNEL_TYPE_TEB == t->type)
305 /* Default MAC address (d00b:eed0:0000 + sw_if_index) */
306 memset (address, 0, sizeof (address));
311 address[4] = t - gm->tunnels;
313 error = ethernet_register_interface (vnm,
314 gre_device_teb_class.index,
315 t - gm->tunnels, address,
320 clib_error_report (error);
321 return VNET_API_ERROR_INVALID_REGISTRATION;
323 hi = vnet_get_hw_interface (vnm, hw_if_index);
325 t->l2_tx_arc = vlib_node_add_named_next (vlib_get_main (),
331 hw_if_index = vnet_register_interface (vnm,
332 gre_device_class.index,
334 gre_hw_interface_class.index,
337 hi = vnet_get_hw_interface (vnm, hw_if_index);
338 sw_if_index = hi->sw_if_index;
341 t->hw_if_index = hw_if_index;
342 t->outer_fib_index = outer_fib_index;
343 t->sw_if_index = sw_if_index;
344 t->l2_adj_index = ADJ_INDEX_INVALID;
346 vec_validate_init_empty (gm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
347 gm->tunnel_index_by_sw_if_index[sw_if_index] = t - gm->tunnels;
351 vec_validate (im4->fib_index_by_sw_if_index, sw_if_index);
352 hi->min_packet_bytes =
353 64 + sizeof (gre_header_t) + sizeof (ip4_header_t);
357 vec_validate (im6->fib_index_by_sw_if_index, sw_if_index);
358 hi->min_packet_bytes =
359 64 + sizeof (gre_header_t) + sizeof (ip6_header_t);
362 hi->per_packet_overhead_bytes =
363 /* preamble */ 8 + /* inter frame gap */ 12;
365 /* Standard default gre MTU. */
366 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
369 * source the FIB entry for the tunnel's destination
370 * and become a child thereof. The tunnel will then get poked
371 * when the forwarding for the entry updates, and the tunnel can
372 * re-stack accordingly
375 clib_memcpy (&t->tunnel_src, &a->src, sizeof (t->tunnel_src));
376 t->tunnel_dst.fp_len = !is_ipv6 ? 32 : 128;
377 t->tunnel_dst.fp_proto = !is_ipv6 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
378 t->tunnel_dst.fp_addr = a->dst;
380 gre_tunnel_db_add (t, &key);
383 fib_table_entry_special_add (outer_fib_index,
385 FIB_SOURCE_RR, FIB_ENTRY_FLAG_NONE);
387 fib_entry_child_add (t->fib_entry_index,
388 FIB_NODE_TYPE_GRE_TUNNEL, t - gm->tunnels);
390 if (GRE_TUNNEL_TYPE_TEB == t->type)
392 t->l2_adj_index = adj_nbr_add_or_lock (t->tunnel_dst.fp_proto,
394 &zero_addr, sw_if_index);
395 gre_update_adj (vnm, t->sw_if_index, t->l2_adj_index);
399 *sw_if_indexp = sw_if_index;
405 vnet_gre_tunnel_delete (vnet_gre_add_del_tunnel_args_t * a,
408 gre_main_t *gm = &gre_main;
409 vnet_main_t *vnm = gm->vnet_main;
411 gre_tunnel_key_t key;
416 outer_fib_index = ip4_fib_index_from_table_id (a->outer_fib_id);
418 outer_fib_index = ip6_fib_index_from_table_id (a->outer_fib_id);
420 if (~0 == outer_fib_index)
421 return VNET_API_ERROR_NO_SUCH_FIB;
424 gre_tunnel_db_find (&a->src, &a->dst, outer_fib_index, a->is_ipv6, &key);
427 return VNET_API_ERROR_NO_SUCH_ENTRY;
429 sw_if_index = t->sw_if_index;
430 vnet_sw_interface_set_flags (vnm, sw_if_index, 0 /* down */ );
431 /* make sure tunnel is removed from l2 bd or xconnect */
432 set_int_l2_mode (gm->vlib_main, vnm, MODE_L3, sw_if_index, 0, 0, 0, 0);
433 vec_add1 (gm->free_gre_tunnel_hw_if_indices[t->type], t->hw_if_index);
434 gm->tunnel_index_by_sw_if_index[sw_if_index] = ~0;
436 if (GRE_TUNNEL_TYPE_TEB == t->type)
437 adj_unlock (t->l2_adj_index);
439 if (t->l2_adj_index != ADJ_INDEX_INVALID)
440 adj_unlock (t->l2_adj_index);
442 fib_entry_child_remove (t->fib_entry_index, t->sibling_index);
443 fib_table_entry_delete_index (t->fib_entry_index, FIB_SOURCE_RR);
445 gre_tunnel_db_remove (t);
446 fib_node_deinit (&t->node);
447 pool_put (gm->tunnels, t);
450 *sw_if_indexp = sw_if_index;
456 vnet_gre_add_del_tunnel (vnet_gre_add_del_tunnel_args_t * a,
460 return (vnet_gre_tunnel_add (a, sw_if_indexp));
462 return (vnet_gre_tunnel_delete (a, sw_if_indexp));
466 gre_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
468 gre_main_t *gm = &gre_main;
469 vnet_hw_interface_t *hi;
473 hi = vnet_get_hw_interface (vnm, hw_if_index);
475 if (NULL == gm->tunnel_index_by_sw_if_index ||
476 hi->sw_if_index >= vec_len (gm->tunnel_index_by_sw_if_index))
479 ti = gm->tunnel_index_by_sw_if_index[hi->sw_if_index];
482 /* not one of ours */
485 t = pool_elt_at_index (gm->tunnels, ti);
487 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
488 vnet_hw_interface_set_flags (vnm, hw_if_index,
489 VNET_HW_INTERFACE_FLAG_LINK_UP);
491 vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );
493 gre_tunnel_restack (t);
495 return /* no error */ 0;
498 static clib_error_t *
499 create_gre_tunnel_command_fn (vlib_main_t * vm,
500 unformat_input_t * input,
501 vlib_cli_command_t * cmd)
503 unformat_input_t _line_input, *line_input = &_line_input;
504 vnet_gre_add_del_tunnel_args_t _a, *a = &_a;
505 ip46_address_t src, dst;
506 u32 outer_fib_id = 0;
512 clib_error_t *error = NULL;
516 /* Get a line of input. */
517 if (!unformat_user (input, unformat_line_input, line_input))
520 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
522 if (unformat (line_input, "del"))
525 if (unformat (line_input, "src %U", unformat_ip4_address, &src.ip4))
531 if (unformat (line_input, "dst %U", unformat_ip4_address, &dst.ip4))
537 if (unformat (line_input, "src %U", unformat_ip6_address, &src.ip6))
543 if (unformat (line_input, "dst %U", unformat_ip6_address, &dst.ip6))
548 else if (unformat (line_input, "outer-fib-id %d", &outer_fib_id))
550 else if (unformat (line_input, "teb"))
554 error = clib_error_return (0, "unknown input `%U'",
555 format_unformat_error, line_input);
562 error = clib_error_return (0, "mandatory argument(s) missing");
566 if ((ipv4_set && memcmp (&src.ip4, &dst.ip4, sizeof (src.ip4)) == 0) ||
567 (ipv6_set && memcmp (&src.ip6, &dst.ip6, sizeof (src.ip6)) == 0))
569 error = clib_error_return (0, "src and dst are identical");
573 if (ipv4_set && ipv6_set)
574 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
576 if ((ipv4_set && memcmp (&dst.ip4, &zero_addr.ip4, sizeof (dst.ip4)) == 0)
578 && memcmp (&dst.ip6, &zero_addr.ip6, sizeof (dst.ip6)) == 0))
580 error = clib_error_return (0, "dst address cannot be zero");
584 memset (a, 0, sizeof (*a));
585 a->outer_fib_id = outer_fib_id;
587 a->is_ipv6 = ipv6_set;
590 clib_memcpy (&a->src.ip4, &src.ip4, sizeof (src.ip4));
591 clib_memcpy (&a->dst.ip4, &dst.ip4, sizeof (dst.ip4));
595 clib_memcpy (&a->src.ip6, &src.ip6, sizeof (src.ip6));
596 clib_memcpy (&a->dst.ip6, &dst.ip6, sizeof (dst.ip6));
600 rv = vnet_gre_tunnel_add (a, &sw_if_index);
602 rv = vnet_gre_tunnel_delete (a, &sw_if_index);
607 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
608 vnet_get_main (), sw_if_index);
610 case VNET_API_ERROR_INVALID_VALUE:
611 error = clib_error_return (0, "GRE tunnel already exists...");
613 case VNET_API_ERROR_NO_SUCH_FIB:
614 error = clib_error_return (0, "outer fib ID %d doesn't exist\n",
619 clib_error_return (0, "vnet_gre_add_del_tunnel returned %d", rv);
624 unformat_free (line_input);
630 VLIB_CLI_COMMAND (create_gre_tunnel_command, static) = {
631 .path = "create gre tunnel",
632 .short_help = "create gre tunnel src <addr> dst <addr> "
633 "[outer-fib-id <fib>] [teb] [del]",
634 .function = create_gre_tunnel_command_fn,
638 static clib_error_t *
639 show_gre_tunnel_command_fn (vlib_main_t * vm,
640 unformat_input_t * input,
641 vlib_cli_command_t * cmd)
643 gre_main_t *gm = &gre_main;
647 if (pool_elts (gm->tunnels) == 0)
648 vlib_cli_output (vm, "No GRE tunnels configured...");
650 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
652 if (unformat (input, "%d", &ti))
661 pool_foreach (t, gm->tunnels,
663 vlib_cli_output (vm, "%U", format_gre_tunnel, t);
669 t = pool_elt_at_index (gm->tunnels, ti);
671 vlib_cli_output (vm, "%U", format_gre_tunnel, t);
678 VLIB_CLI_COMMAND (show_gre_tunnel_command, static) = {
679 .path = "show gre tunnel",
680 .function = show_gre_tunnel_command_fn,
684 /* force inclusion from application's main.c */
686 gre_interface_init (vlib_main_t * vm)
688 fib_node_register_type (FIB_NODE_TYPE_GRE_TUNNEL, &gre_vft);
693 VLIB_INIT_FUNCTION (gre_interface_init);
696 * fd.io coding-style-patch-verification: ON
699 * eval: (c-set-style "gnu")