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/adj/adj_midchain.h>
24 #include <vnet/adj/adj_nbr.h>
25 #include <vnet/mpls/mpls.h>
28 gre_mk_key (const ip4_address_t *src,
29 const ip4_address_t *dst,
32 // FIXME. the fib index should be part of the key
33 return ((u64)src->as_u32 << 32 | (u64)dst->as_u32);
37 format_gre_tunnel (u8 * s, va_list * args)
39 gre_tunnel_t * t = va_arg (*args, gre_tunnel_t *);
40 gre_main_t * gm = &gre_main;
43 "[%d] %U (src) %U (dst) payload %s outer_fib_index %d",
45 format_ip4_address, &t->tunnel_src,
46 format_ip4_address, &t->tunnel_dst,
47 (t->teb ? "teb" : "ip"),
54 gre_tunnel_db_find (const ip4_address_t *src,
55 const ip4_address_t *dst,
58 gre_main_t * gm = &gre_main;
62 key = gre_mk_key(src, dst, out_fib_index);
64 p = hash_get (gm->tunnel_by_key, key);
69 return (pool_elt_at_index (gm->tunnels, p[0]));
73 gre_tunnel_db_add (const gre_tunnel_t *t)
75 gre_main_t * gm = &gre_main;
78 key = gre_mk_key(&t->tunnel_src, &t->tunnel_dst, t->outer_fib_index);
79 hash_set (gm->tunnel_by_key, key, t - gm->tunnels);
83 gre_tunnel_db_remove (const gre_tunnel_t *t)
85 gre_main_t * gm = &gre_main;
88 key = gre_mk_key(&t->tunnel_src, &t->tunnel_dst, t->outer_fib_index);
89 hash_unset (gm->tunnel_by_key, key);
93 gre_tunnel_from_fib_node (fib_node_t *node)
96 ASSERT(FIB_NODE_TYPE_GRE_TUNNEL == node->fn_type);
98 return ((gre_tunnel_t*) (((char*)node) -
99 STRUCT_OFFSET_OF(gre_tunnel_t, node)));
105 * 'stack' (resolve the recursion for) the tunnel's midchain adjacency
108 gre_tunnel_stack (adj_index_t ai)
110 gre_main_t * gm = &gre_main;
116 sw_if_index = adj->rewrite_header.sw_if_index;
118 if ((vec_len(gm->tunnel_index_by_sw_if_index) < sw_if_index) ||
119 (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index]))
122 gt = pool_elt_at_index(gm->tunnels,
123 gm->tunnel_index_by_sw_if_index[sw_if_index]);
126 * find the adjacency that is contributed by the FIB entry
127 * that this tunnel resovles via, and use it as the next adj
130 if (vnet_hw_interface_get_flags(vnet_get_main(),
132 VNET_HW_INTERFACE_FLAG_LINK_UP)
134 adj_nbr_midchain_stack(
136 fib_entry_contribute_ip_forwarding(gt->fib_entry_index));
140 adj_nbr_midchain_unstack(ai);
145 * @brief Call back when restacking all adjacencies on a GRE interface
148 gre_adj_walk_cb (adj_index_t ai,
151 gre_tunnel_stack(ai);
153 return (ADJ_WALK_RC_CONTINUE);
157 gre_tunnel_restack (gre_tunnel_t *gt)
159 fib_protocol_t proto;
162 * walk all the adjacencies on th GRE interface and restack them
164 FOR_EACH_FIB_IP_PROTOCOL(proto)
166 adj_nbr_walk(gt->sw_if_index,
174 * Function definition to backwalk a FIB node
176 static fib_node_back_walk_rc_t
177 gre_tunnel_back_walk (fib_node_t *node,
178 fib_node_back_walk_ctx_t *ctx)
180 gre_tunnel_restack(gre_tunnel_from_fib_node(node));
182 return (FIB_NODE_BACK_WALK_CONTINUE);
186 * Function definition to get a FIB node from its index
189 gre_tunnel_fib_node_get (fib_node_index_t index)
195 gt = pool_elt_at_index(gm->tunnels, index);
201 * Function definition to inform the FIB node that its last lock has gone.
204 gre_tunnel_last_lock_gone (fib_node_t *node)
207 * The MPLS GRE tunnel is a root of the graph. As such
208 * it never has children and thus is never locked.
214 * Virtual function table registered by MPLS GRE tunnels
215 * for participation in the FIB object graph.
217 const static fib_node_vft_t gre_vft = {
218 .fnv_get = gre_tunnel_fib_node_get,
219 .fnv_last_lock = gre_tunnel_last_lock_gone,
220 .fnv_back_walk = gre_tunnel_back_walk,
224 vnet_gre_tunnel_add (vnet_gre_add_del_tunnel_args_t *a,
227 gre_main_t * gm = &gre_main;
228 vnet_main_t * vnm = gm->vnet_main;
229 ip4_main_t * im = &ip4_main;
231 vnet_hw_interface_t * hi;
232 u32 hw_if_index, sw_if_index;
237 outer_fib_index = ip4_fib_index_from_table_id(a->outer_fib_id);
239 if (~0 == outer_fib_index)
240 return VNET_API_ERROR_NO_SUCH_FIB;
242 t = gre_tunnel_db_find(&a->src, &a->dst, a->outer_fib_id);
245 return VNET_API_ERROR_INVALID_VALUE;
247 pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
248 memset (t, 0, sizeof (*t));
249 fib_node_init(&t->node, FIB_NODE_TYPE_GRE_TUNNEL);
251 if (vec_len (gm->free_gre_tunnel_hw_if_indices) > 0) {
252 vnet_interface_main_t * im = &vnm->interface_main;
254 hw_if_index = gm->free_gre_tunnel_hw_if_indices
255 [vec_len (gm->free_gre_tunnel_hw_if_indices)-1];
256 _vec_len (gm->free_gre_tunnel_hw_if_indices) -= 1;
258 hi = vnet_get_hw_interface (vnm, hw_if_index);
259 hi->dev_instance = t - gm->tunnels;
260 hi->hw_instance = hi->dev_instance;
262 /* clear old stats of freed tunnel before reuse */
263 sw_if_index = hi->sw_if_index;
264 vnet_interface_counter_lock(im);
265 vlib_zero_combined_counter
266 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], sw_if_index);
267 vlib_zero_combined_counter
268 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], sw_if_index);
269 vlib_zero_simple_counter
270 (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index);
271 vnet_interface_counter_unlock(im);
274 t->l2_tx_arc = vlib_node_add_named_next(vlib_get_main(),
281 /* Default MAC address (d00b:eed0:0000 + sw_if_index) */
282 memset (address, 0, sizeof (address));
287 address[4] = t - gm->tunnels;
289 error = ethernet_register_interface(vnm,
290 gre_device_class.index,
291 t - gm->tunnels, address,
297 clib_error_report (error);
298 return VNET_API_ERROR_INVALID_REGISTRATION;
300 hi = vnet_get_hw_interface (vnm, hw_if_index);
302 t->l2_tx_arc = vlib_node_add_named_next(vlib_get_main(),
306 hw_if_index = vnet_register_interface(vnm,
307 gre_device_class.index,
309 gre_hw_interface_class.index,
312 hi = vnet_get_hw_interface (vnm, hw_if_index);
313 sw_if_index = hi->sw_if_index;
316 t->hw_if_index = hw_if_index;
317 t->outer_fib_index = outer_fib_index;
318 t->sw_if_index = sw_if_index;
321 vec_validate_init_empty (gm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
322 gm->tunnel_index_by_sw_if_index[sw_if_index] = t - gm->tunnels;
324 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
325 im->fib_index_by_sw_if_index[sw_if_index] = t->outer_fib_index;
326 ip4_sw_interface_enable_disable(sw_if_index, 1);
328 hi->min_packet_bytes = 64 + sizeof (gre_header_t) + sizeof (ip4_header_t);
329 hi->per_packet_overhead_bytes =
330 /* preamble */ 8 + /* inter frame gap */ 12;
332 /* Standard default gre MTU. */
333 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
335 clib_memcpy (&t->tunnel_src, &a->src, sizeof (t->tunnel_src));
336 clib_memcpy (&t->tunnel_dst, &a->dst, sizeof (t->tunnel_dst));
338 gre_tunnel_db_add(t);
341 * source the FIB entry for the tunnel's destination
342 * and become a child thereof. The tunnel will then get poked
343 * when the forwarding for the entry updates, and the tunnel can
344 * re-stack accordingly
346 const fib_prefix_t tun_dst_pfx = {
348 .fp_proto = FIB_PROTOCOL_IP4,
350 .ip4 = t->tunnel_dst,
355 fib_table_entry_special_add(outer_fib_index,
361 fib_entry_child_add(t->fib_entry_index,
362 FIB_NODE_TYPE_GRE_TUNNEL,
365 clib_memcpy (&t->tunnel_src, &a->src, sizeof (t->tunnel_src));
366 clib_memcpy (&t->tunnel_dst, &a->dst, sizeof (t->tunnel_dst));
370 t->l2_adj_index = adj_nbr_add_or_lock(FIB_PROTOCOL_IP4,
375 gre_update_adj(vnm, t->sw_if_index, t->l2_adj_index);
379 *sw_if_indexp = sw_if_index;
385 vnet_gre_tunnel_delete (vnet_gre_add_del_tunnel_args_t *a,
388 gre_main_t * gm = &gre_main;
389 vnet_main_t * vnm = gm->vnet_main;
393 t = gre_tunnel_db_find(&a->src, &a->dst, a->outer_fib_id);
396 return VNET_API_ERROR_NO_SUCH_ENTRY;
398 sw_if_index = t->sw_if_index;
399 vnet_sw_interface_set_flags (vnm, sw_if_index, 0 /* down */);
400 /* make sure tunnel is removed from l2 bd or xconnect */
401 set_int_l2_mode(gm->vlib_main, vnm, MODE_L3, sw_if_index, 0, 0, 0, 0);
402 vec_add1 (gm->free_gre_tunnel_hw_if_indices, t->hw_if_index);
403 gm->tunnel_index_by_sw_if_index[sw_if_index] = ~0;
404 ip4_sw_interface_enable_disable(sw_if_index, 0);
406 fib_entry_child_remove(t->fib_entry_index,
408 fib_table_entry_delete_index(t->fib_entry_index,
411 gre_tunnel_db_remove(t);
412 fib_node_deinit(&t->node);
413 pool_put (gm->tunnels, t);
416 *sw_if_indexp = sw_if_index;
422 vnet_gre_add_del_tunnel (vnet_gre_add_del_tunnel_args_t *a,
426 return (vnet_gre_tunnel_add(a, sw_if_indexp));
428 return (vnet_gre_tunnel_delete(a, sw_if_indexp));
432 gre_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
434 gre_main_t * gm = &gre_main;
435 vnet_hw_interface_t * hi;
439 hi = vnet_get_hw_interface (vnm, hw_if_index);
441 if (NULL == gm->tunnel_index_by_sw_if_index ||
442 hi->sw_if_index >= vec_len(gm->tunnel_index_by_sw_if_index))
445 ti = gm->tunnel_index_by_sw_if_index[hi->sw_if_index];
448 /* not one of ours */
451 t = pool_elt_at_index(gm->tunnels, ti);
453 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
454 vnet_hw_interface_set_flags (vnm, hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP);
456 vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */);
458 gre_tunnel_restack(t);
460 return /* no error */ 0;
463 static clib_error_t *
464 create_gre_tunnel_command_fn (vlib_main_t * vm,
465 unformat_input_t * input,
466 vlib_cli_command_t * cmd)
468 unformat_input_t _line_input, * line_input = &_line_input;
469 vnet_gre_add_del_tunnel_args_t _a, * a = &_a;
470 ip4_address_t src, dst;
471 u32 outer_fib_id = 0;
478 /* Get a line of input. */
479 if (! unformat_user (input, unformat_line_input, line_input))
482 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
483 if (unformat (line_input, "del"))
485 else if (unformat (line_input, "src %U", unformat_ip4_address, &src))
487 else if (unformat (line_input, "dst %U", unformat_ip4_address, &dst))
489 else if (unformat (line_input, "outer-fib-id %d", &outer_fib_id))
491 else if (unformat (line_input, "teb"))
494 return clib_error_return (0, "unknown input `%U'",
495 format_unformat_error, input);
497 unformat_free (line_input);
500 return clib_error_return (0, "mandatory argument(s) missing");
502 if (memcmp (&src, &dst, sizeof(src)) == 0)
503 return clib_error_return (0, "src and dst are identical");
505 memset (a, 0, sizeof (*a));
506 a->outer_fib_id = outer_fib_id;
508 clib_memcpy(&a->src, &src, sizeof(src));
509 clib_memcpy(&a->dst, &dst, sizeof(dst));
512 rv = vnet_gre_tunnel_add(a, &sw_if_index);
514 rv = vnet_gre_tunnel_delete(a, &sw_if_index);
519 vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index);
521 case VNET_API_ERROR_INVALID_VALUE:
522 return clib_error_return (0, "GRE tunnel already exists...");
523 case VNET_API_ERROR_NO_SUCH_FIB:
524 return clib_error_return (0, "outer fib ID %d doesn't exist\n",
527 return clib_error_return (0, "vnet_gre_add_del_tunnel returned %d", rv);
533 VLIB_CLI_COMMAND (create_gre_tunnel_command, static) = {
534 .path = "create gre tunnel",
535 .short_help = "create gre tunnel src <addr> dst <addr> "
536 "[outer-fib-id <fib>] [teb] [del]",
537 .function = create_gre_tunnel_command_fn,
540 static clib_error_t *
541 show_gre_tunnel_command_fn (vlib_main_t * vm,
542 unformat_input_t * input,
543 vlib_cli_command_t * cmd)
545 gre_main_t * gm = &gre_main;
549 if (pool_elts (gm->tunnels) == 0)
550 vlib_cli_output (vm, "No GRE tunnels configured...");
552 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
554 if (unformat (input, "%d", &ti))
562 pool_foreach (t, gm->tunnels,
564 vlib_cli_output (vm, "%U", format_gre_tunnel, t);
569 t = pool_elt_at_index(gm->tunnels, ti);
571 vlib_cli_output (vm, "%U", format_gre_tunnel, t);
577 VLIB_CLI_COMMAND (show_gre_tunnel_command, static) = {
578 .path = "show gre tunnel",
579 .function = show_gre_tunnel_command_fn,
582 /* force inclusion from application's main.c */
583 clib_error_t *gre_interface_init (vlib_main_t *vm)
585 fib_node_register_type(FIB_NODE_TYPE_GRE_TUNNEL, &gre_vft);
589 VLIB_INIT_FUNCTION(gre_interface_init);