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/mpls/mpls.h>
27 gre_mk_key (const ip4_address_t *src,
28 const ip4_address_t *dst,
31 // FIXME. the fib index should be part of the key
32 return ((u64)src->as_u32 << 32 | (u64)dst->as_u32);
36 format_gre_tunnel (u8 * s, va_list * args)
38 gre_tunnel_t * t = va_arg (*args, gre_tunnel_t *);
39 int detail = va_arg (*args, int);
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"),
51 s = format (s, "\n fib-entry:%d adj-ip4:%d adj-ip6:%d adj-mpls:%d",
53 t->adj_index[FIB_LINK_IP4],
54 t->adj_index[FIB_LINK_IP6],
55 t->adj_index[FIB_LINK_MPLS]);
62 gre_tunnel_db_find (const ip4_address_t *src,
63 const ip4_address_t *dst,
66 gre_main_t * gm = &gre_main;
70 key = gre_mk_key(src, dst, out_fib_index);
72 p = hash_get (gm->tunnel_by_key, key);
77 return (pool_elt_at_index (gm->tunnels, p[0]));
81 gre_tunnel_db_add (const gre_tunnel_t *t)
83 gre_main_t * gm = &gre_main;
86 key = gre_mk_key(&t->tunnel_src, &t->tunnel_dst, t->outer_fib_index);
87 hash_set (gm->tunnel_by_key, key, t - gm->tunnels);
91 gre_tunnel_db_remove (const gre_tunnel_t *t)
93 gre_main_t * gm = &gre_main;
96 key = gre_mk_key(&t->tunnel_src, &t->tunnel_dst, t->outer_fib_index);
97 hash_unset (gm->tunnel_by_key, key);
100 static gre_tunnel_t *
101 gre_tunnel_from_fib_node (fib_node_t *node)
104 ASSERT(FIB_NODE_TYPE_GRE_TUNNEL == node->fn_type);
106 return ((gre_tunnel_t*) (((char*)node) -
107 STRUCT_OFFSET_OF(gre_tunnel_t, node)));
113 * 'stack' (resolve the recursion for) the tunnel's midchain adjacency
116 gre_tunnel_stack (gre_tunnel_t *gt)
121 * find the adjacency that is contributed by the FIB entry
122 * that this tunnel resovles via, and use it as the next adj
125 FOR_EACH_FIB_LINK(linkt)
127 if (ADJ_INDEX_INVALID != gt->adj_index[linkt])
129 if (vnet_hw_interface_get_flags(vnet_get_main(),
131 VNET_HW_INTERFACE_FLAG_LINK_UP)
133 adj_nbr_midchain_stack(
134 gt->adj_index[linkt],
135 fib_entry_contribute_ip_forwarding(gt->fib_entry_index));
139 adj_nbr_midchain_unstack(gt->adj_index[linkt]);
146 * Function definition to backwalk a FIB node
148 static fib_node_back_walk_rc_t
149 gre_tunnel_back_walk (fib_node_t *node,
150 fib_node_back_walk_ctx_t *ctx)
152 gre_tunnel_stack(gre_tunnel_from_fib_node(node));
154 return (FIB_NODE_BACK_WALK_CONTINUE);
158 * Function definition to get a FIB node from its index
161 gre_tunnel_fib_node_get (fib_node_index_t index)
167 gt = pool_elt_at_index(gm->tunnels, index);
173 * Function definition to inform the FIB node that its last lock has gone.
176 gre_tunnel_last_lock_gone (fib_node_t *node)
179 * The MPLS GRE tunnel is a root of the graph. As such
180 * it never has children and thus is never locked.
186 * Virtual function table registered by MPLS GRE tunnels
187 * for participation in the FIB object graph.
189 const static fib_node_vft_t gre_vft = {
190 .fnv_get = gre_tunnel_fib_node_get,
191 .fnv_last_lock = gre_tunnel_last_lock_gone,
192 .fnv_back_walk = gre_tunnel_back_walk,
196 gre_proto_from_fib_link (fib_link_t link)
201 return (GRE_PROTOCOL_ip4);
203 return (GRE_PROTOCOL_ip6);
205 return (GRE_PROTOCOL_mpls_unicast);
206 case FIB_LINK_ETHERNET:
207 return (GRE_PROTOCOL_teb);
210 return (GRE_PROTOCOL_ip4);
214 gre_rewrite (gre_tunnel_t * t,
217 ip4_and_gre_header_t * h0;
218 u8 * rewrite_data = 0;
220 vec_validate_init_empty (rewrite_data, sizeof (*h0) - 1, 0);
222 h0 = (ip4_and_gre_header_t *) rewrite_data;
224 h0->gre.protocol = clib_host_to_net_u16(gre_proto_from_fib_link(link));
226 h0->ip4.ip_version_and_header_length = 0x45;
228 h0->ip4.protocol = IP_PROTOCOL_GRE;
229 /* $$$ fixup ip4 header length and checksum after-the-fact */
230 h0->ip4.src_address.as_u32 = t->tunnel_src.as_u32;
231 h0->ip4.dst_address.as_u32 = t->tunnel_dst.as_u32;
232 h0->ip4.checksum = ip4_header_checksum (&h0->ip4);
234 return (rewrite_data);
238 gre_fixup (vlib_main_t *vm,
244 ip0 = vlib_buffer_get_current (b0);
246 /* Fixup the checksum and len fields in the GRE tunnel encap
247 * that was applied at the midchain node */
248 ip0->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
249 ip0->checksum = ip4_header_checksum (ip0);
253 vnet_gre_tunnel_add (vnet_gre_add_del_tunnel_args_t *a,
256 gre_main_t * gm = &gre_main;
257 vnet_main_t * vnm = gm->vnet_main;
258 ip4_main_t * im = &ip4_main;
260 vnet_hw_interface_t * hi;
261 u32 hw_if_index, sw_if_index;
268 outer_fib_index = ip4_fib_index_from_table_id(a->outer_fib_id);
270 if (~0 == outer_fib_index)
271 return VNET_API_ERROR_NO_SUCH_FIB;
273 t = gre_tunnel_db_find(&a->src, &a->dst, a->outer_fib_id);
276 return VNET_API_ERROR_INVALID_VALUE;
278 pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
279 memset (t, 0, sizeof (*t));
280 fib_node_init(&t->node, FIB_NODE_TYPE_GRE_TUNNEL);
281 FOR_EACH_FIB_LINK(linkt)
283 t->adj_index[linkt] = ADJ_INDEX_INVALID;
286 if (vec_len (gm->free_gre_tunnel_hw_if_indices) > 0) {
287 vnet_interface_main_t * im = &vnm->interface_main;
289 hw_if_index = gm->free_gre_tunnel_hw_if_indices
290 [vec_len (gm->free_gre_tunnel_hw_if_indices)-1];
291 _vec_len (gm->free_gre_tunnel_hw_if_indices) -= 1;
293 hi = vnet_get_hw_interface (vnm, hw_if_index);
294 hi->dev_instance = t - gm->tunnels;
295 hi->hw_instance = hi->dev_instance;
297 /* clear old stats of freed tunnel before reuse */
298 sw_if_index = hi->sw_if_index;
299 vnet_interface_counter_lock(im);
300 vlib_zero_combined_counter
301 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], sw_if_index);
302 vlib_zero_combined_counter
303 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], sw_if_index);
304 vlib_zero_simple_counter
305 (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index);
306 vnet_interface_counter_unlock(im);
309 t->l2_tx_arc = vlib_node_add_named_next(vlib_get_main(),
316 /* Default MAC address (d00b:eed0:0000 + sw_if_index) */
317 memset (address, 0, sizeof (address));
322 address[4] = t - gm->tunnels;
324 error = ethernet_register_interface
326 gre_l2_device_class.index, t - gm->tunnels, address, &hw_if_index,
331 clib_error_report (error);
332 return VNET_API_ERROR_INVALID_REGISTRATION;
334 hi = vnet_get_hw_interface (vnm, hw_if_index);
336 t->l2_tx_arc = vlib_node_add_named_next(vlib_get_main(),
340 hw_if_index = vnet_register_interface
341 (vnm, gre_device_class.index, t - gm->tunnels,
342 gre_hw_interface_class.index,
345 hi = vnet_get_hw_interface (vnm, hw_if_index);
346 sw_if_index = hi->sw_if_index;
349 t->hw_if_index = hw_if_index;
350 t->outer_fib_index = outer_fib_index;
351 t->sw_if_index = sw_if_index;
354 vec_validate_init_empty (gm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
355 gm->tunnel_index_by_sw_if_index[sw_if_index] = t - gm->tunnels;
357 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
358 im->fib_index_by_sw_if_index[sw_if_index] = t->outer_fib_index;
359 ip4_sw_interface_enable_disable(sw_if_index, 1);
361 hi->min_packet_bytes = 64 + sizeof (gre_header_t) + sizeof (ip4_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;
368 clib_memcpy (&t->tunnel_src, &a->src, sizeof (t->tunnel_src));
369 clib_memcpy (&t->tunnel_dst, &a->dst, sizeof (t->tunnel_dst));
371 gre_tunnel_db_add(t);
374 * source the FIB entry for the tunnel's destination
375 * and become a child thereof. The tunnel will then get poked
376 * when the forwarding for the entry updates, and the tunnel can
377 * re-stack accordingly
379 const fib_prefix_t tun_dst_pfx = {
381 .fp_proto = FIB_PROTOCOL_IP4,
383 .ip4 = t->tunnel_dst,
388 fib_table_entry_special_add(outer_fib_index,
394 fib_entry_child_add(t->fib_entry_index,
395 FIB_NODE_TYPE_GRE_TUNNEL,
399 * create and update the midchain adj this tunnel sources.
400 * We could be smarter here and trigger this on an interface proto enable,
401 * like we do for MPLS.
405 t->adj_index[FIB_LINK_ETHERNET] = adj_nbr_add_or_lock(FIB_PROTOCOL_IP4,
410 rewrite = gre_rewrite(t, FIB_LINK_ETHERNET);
411 adj_nbr_midchain_update_rewrite(t->adj_index[FIB_LINK_ETHERNET],
413 ADJ_MIDCHAIN_FLAG_NO_COUNT,
419 FOR_EACH_FIB_IP_LINK (linkt)
421 t->adj_index[linkt] = adj_nbr_add_or_lock(FIB_PROTOCOL_IP4,
426 rewrite = gre_rewrite(t, linkt);
427 adj_nbr_midchain_update_rewrite(t->adj_index[linkt],
429 ADJ_MIDCHAIN_FLAG_NONE,
435 t->adj_index[FIB_LINK_MPLS] = ADJ_INDEX_INVALID;
437 clib_memcpy (&t->tunnel_src, &a->src, sizeof (t->tunnel_src));
438 clib_memcpy (&t->tunnel_dst, &a->dst, sizeof (t->tunnel_dst));
442 *sw_if_indexp = sw_if_index;
448 vnet_gre_tunnel_delete (vnet_gre_add_del_tunnel_args_t *a,
451 gre_main_t * gm = &gre_main;
452 vnet_main_t * vnm = gm->vnet_main;
457 t = gre_tunnel_db_find(&a->src, &a->dst, a->outer_fib_id);
460 return VNET_API_ERROR_NO_SUCH_ENTRY;
462 sw_if_index = t->sw_if_index;
463 vnet_sw_interface_set_flags (vnm, sw_if_index, 0 /* down */);
464 /* make sure tunnel is removed from l2 bd or xconnect */
465 set_int_l2_mode(gm->vlib_main, vnm, MODE_L3, sw_if_index, 0, 0, 0, 0);
466 vec_add1 (gm->free_gre_tunnel_hw_if_indices, t->hw_if_index);
467 gm->tunnel_index_by_sw_if_index[sw_if_index] = ~0;
468 ip4_sw_interface_enable_disable(sw_if_index, 0);
470 fib_entry_child_remove(t->fib_entry_index,
472 fib_table_entry_delete_index(t->fib_entry_index,
475 FOR_EACH_FIB_LINK(linkt)
477 adj_unlock(t->adj_index[linkt]);
480 gre_tunnel_db_remove(t);
481 fib_node_deinit(&t->node);
482 pool_put (gm->tunnels, t);
485 *sw_if_indexp = sw_if_index;
491 vnet_gre_add_del_tunnel (vnet_gre_add_del_tunnel_args_t *a,
495 return (vnet_gre_tunnel_add(a, sw_if_indexp));
497 return (vnet_gre_tunnel_delete(a, sw_if_indexp));
501 gre_sw_interface_mpls_state_change (u32 sw_if_index,
504 gre_main_t *gm = &gre_main;
508 if ((vec_len(gm->tunnel_index_by_sw_if_index) < sw_if_index) ||
509 (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index]))
512 t = pool_elt_at_index(gm->tunnels,
513 gm->tunnel_index_by_sw_if_index[sw_if_index]);
517 t->adj_index[FIB_LINK_MPLS] =
518 adj_nbr_add_or_lock(FIB_PROTOCOL_IP4,
523 rewrite = gre_rewrite(t, FIB_LINK_MPLS);
524 adj_nbr_midchain_update_rewrite(t->adj_index[FIB_LINK_MPLS],
526 ADJ_MIDCHAIN_FLAG_NONE,
532 adj_unlock(t->adj_index[FIB_LINK_MPLS]);
533 t->adj_index[FIB_LINK_MPLS] = ADJ_INDEX_INVALID;
539 static clib_error_t *
540 create_gre_tunnel_command_fn (vlib_main_t * vm,
541 unformat_input_t * input,
542 vlib_cli_command_t * cmd)
544 unformat_input_t _line_input, * line_input = &_line_input;
545 vnet_gre_add_del_tunnel_args_t _a, * a = &_a;
546 ip4_address_t src, dst;
547 u32 outer_fib_id = 0;
554 /* Get a line of input. */
555 if (! unformat_user (input, unformat_line_input, line_input))
558 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
559 if (unformat (line_input, "del"))
561 else if (unformat (line_input, "src %U", unformat_ip4_address, &src))
563 else if (unformat (line_input, "dst %U", unformat_ip4_address, &dst))
565 else if (unformat (line_input, "outer-fib-id %d", &outer_fib_id))
567 else if (unformat (line_input, "teb"))
570 return clib_error_return (0, "unknown input `%U'",
571 format_unformat_error, input);
573 unformat_free (line_input);
576 return clib_error_return (0, "mandatory argument(s) missing");
578 if (memcmp (&src, &dst, sizeof(src)) == 0)
579 return clib_error_return (0, "src and dst are identical");
581 memset (a, 0, sizeof (*a));
582 a->outer_fib_id = outer_fib_id;
584 clib_memcpy(&a->src, &src, sizeof(src));
585 clib_memcpy(&a->dst, &dst, sizeof(dst));
588 rv = vnet_gre_tunnel_add(a, &sw_if_index);
590 rv = vnet_gre_tunnel_delete(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_VALUE:
598 return clib_error_return (0, "GRE tunnel already exists...");
599 case VNET_API_ERROR_NO_SUCH_FIB:
600 return clib_error_return (0, "outer fib ID %d doesn't exist\n",
603 return clib_error_return (0, "vnet_gre_add_del_tunnel returned %d", rv);
609 VLIB_CLI_COMMAND (create_gre_tunnel_command, static) = {
610 .path = "create gre tunnel",
611 .short_help = "create gre tunnel src <addr> dst <addr> "
612 "[outer-fib-id <fib>] [teb] [del]",
613 .function = create_gre_tunnel_command_fn,
616 static clib_error_t *
617 show_gre_tunnel_command_fn (vlib_main_t * vm,
618 unformat_input_t * input,
619 vlib_cli_command_t * cmd)
621 gre_main_t * gm = &gre_main;
625 if (pool_elts (gm->tunnels) == 0)
626 vlib_cli_output (vm, "No GRE tunnels configured...");
628 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
630 if (unformat (input, "%d", &ti))
638 pool_foreach (t, gm->tunnels,
640 vlib_cli_output (vm, "%U", format_gre_tunnel, t, 0);
645 t = pool_elt_at_index(gm->tunnels, ti);
647 vlib_cli_output (vm, "%U", format_gre_tunnel, t, 1);
653 VLIB_CLI_COMMAND (show_gre_tunnel_command, static) = {
654 .path = "show gre tunnel",
655 .function = show_gre_tunnel_command_fn,
658 /* force inclusion from application's main.c */
659 clib_error_t *gre_interface_init (vlib_main_t *vm)
661 vec_add1(mpls_main.mpls_interface_state_change_callbacks,
662 gre_sw_interface_mpls_state_change);
664 fib_node_register_type(FIB_NODE_TYPE_GRE_TUNNEL, &gre_vft);
668 VLIB_INIT_FUNCTION(gre_interface_init);