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 adj_nbr_midchain_stack(
130 gt->adj_index[linkt],
131 fib_entry_contribute_ip_forwarding(gt->fib_entry_index));
137 * Function definition to backwalk a FIB node
139 static fib_node_back_walk_rc_t
140 gre_tunnel_back_walk (fib_node_t *node,
141 fib_node_back_walk_ctx_t *ctx)
143 gre_tunnel_stack(gre_tunnel_from_fib_node(node));
145 return (FIB_NODE_BACK_WALK_CONTINUE);
149 * Function definition to get a FIB node from its index
152 gre_tunnel_fib_node_get (fib_node_index_t index)
158 gt = pool_elt_at_index(gm->tunnels, index);
164 * Function definition to inform the FIB node that its last lock has gone.
167 gre_tunnel_last_lock_gone (fib_node_t *node)
170 * The MPLS GRE tunnel is a root of the graph. As such
171 * it never has children and thus is never locked.
177 * Virtual function table registered by MPLS GRE tunnels
178 * for participation in the FIB object graph.
180 const static fib_node_vft_t gre_vft = {
181 .fnv_get = gre_tunnel_fib_node_get,
182 .fnv_last_lock = gre_tunnel_last_lock_gone,
183 .fnv_back_walk = gre_tunnel_back_walk,
187 gre_proto_from_fib_link (fib_link_t link)
192 return (GRE_PROTOCOL_ip4);
194 return (GRE_PROTOCOL_ip6);
196 return (GRE_PROTOCOL_mpls_unicast);
199 return (GRE_PROTOCOL_ip4);
203 gre_rewrite (gre_tunnel_t * t,
206 ip4_and_gre_header_t * h0;
207 u8 * rewrite_data = 0;
209 vec_validate_init_empty (rewrite_data, sizeof (*h0) - 1, 0);
211 h0 = (ip4_and_gre_header_t *) rewrite_data;
215 h0->gre.protocol = clib_net_to_host_u16(GRE_PROTOCOL_teb);
219 h0->gre.protocol = clib_host_to_net_u16(gre_proto_from_fib_link(link));
222 h0->ip4.ip_version_and_header_length = 0x45;
224 h0->ip4.protocol = IP_PROTOCOL_GRE;
225 /* $$$ fixup ip4 header length and checksum after-the-fact */
226 h0->ip4.src_address.as_u32 = t->tunnel_src.as_u32;
227 h0->ip4.dst_address.as_u32 = t->tunnel_dst.as_u32;
228 h0->ip4.checksum = ip4_header_checksum (&h0->ip4);
230 return (rewrite_data);
234 vnet_gre_tunnel_add (vnet_gre_add_del_tunnel_args_t *a,
237 gre_main_t * gm = &gre_main;
238 vnet_main_t * vnm = gm->vnet_main;
239 ip4_main_t * im = &ip4_main;
241 vnet_hw_interface_t * hi;
242 u32 hw_if_index, sw_if_index;
249 outer_fib_index = ip4_fib_index_from_table_id(a->outer_fib_id);
251 if (~0 == outer_fib_index)
252 return VNET_API_ERROR_NO_SUCH_FIB;
254 t = gre_tunnel_db_find(&a->src, &a->dst, a->outer_fib_id);
257 return VNET_API_ERROR_INVALID_VALUE;
259 pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
260 memset (t, 0, sizeof (*t));
261 fib_node_init(&t->node, FIB_NODE_TYPE_GRE_TUNNEL);
263 if (vec_len (gm->free_gre_tunnel_hw_if_indices) > 0) {
264 vnet_interface_main_t * im = &vnm->interface_main;
266 hw_if_index = gm->free_gre_tunnel_hw_if_indices
267 [vec_len (gm->free_gre_tunnel_hw_if_indices)-1];
268 _vec_len (gm->free_gre_tunnel_hw_if_indices) -= 1;
270 hi = vnet_get_hw_interface (vnm, hw_if_index);
271 hi->dev_instance = t - gm->tunnels;
272 hi->hw_instance = hi->dev_instance;
274 /* clear old stats of freed tunnel before reuse */
275 sw_if_index = hi->sw_if_index;
276 vnet_interface_counter_lock(im);
277 vlib_zero_combined_counter
278 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], sw_if_index);
279 vlib_zero_combined_counter
280 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], sw_if_index);
281 vlib_zero_simple_counter
282 (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index);
283 vnet_interface_counter_unlock(im);
287 /* Default MAC address (d00b:eed0:0000 + sw_if_index) */
288 memset (address, 0, sizeof (address));
293 address[4] = t - gm->tunnels;
295 error = ethernet_register_interface
297 gre_device_class.index, t - gm->tunnels, address, &hw_if_index,
302 clib_error_report (error);
303 return VNET_API_ERROR_INVALID_REGISTRATION;
306 hw_if_index = vnet_register_interface
307 (vnm, gre_device_class.index, t - gm->tunnels,
308 gre_hw_interface_class.index,
311 hi = vnet_get_hw_interface (vnm, hw_if_index);
312 sw_if_index = hi->sw_if_index;
315 t->hw_if_index = hw_if_index;
316 t->outer_fib_index = outer_fib_index;
317 t->sw_if_index = sw_if_index;
319 vec_validate_init_empty (gm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
320 gm->tunnel_index_by_sw_if_index[sw_if_index] = t - gm->tunnels;
322 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
323 im->fib_index_by_sw_if_index[sw_if_index] = t->outer_fib_index;
324 ip4_sw_interface_enable_disable(sw_if_index, 1);
326 hi->min_packet_bytes = 64 + sizeof (gre_header_t) + sizeof (ip4_header_t);
327 hi->per_packet_overhead_bytes =
328 /* preamble */ 8 + /* inter frame gap */ 12;
330 /* Standard default gre MTU. */
331 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
333 clib_memcpy (&t->tunnel_src, &a->src, sizeof (t->tunnel_src));
334 clib_memcpy (&t->tunnel_dst, &a->dst, sizeof (t->tunnel_dst));
336 gre_tunnel_db_add(t);
339 * source the FIB entry for the tunnel's destination
340 * and become a child thereof. The tunnel will then get poked
341 * when the forwarding for the entry updates, and the tunnel can
342 * re-stack accordingly
344 const fib_prefix_t tun_dst_pfx = {
346 .fp_proto = FIB_PROTOCOL_IP4,
348 .ip4 = t->tunnel_dst,
353 fib_table_entry_special_add(outer_fib_index,
359 fib_entry_child_add(t->fib_entry_index,
360 FIB_NODE_TYPE_GRE_TUNNEL,
364 * create and update the midchain adj this tunnel sources.
365 * We could be smarter here and trigger this on an interface proto enable,
366 * like we do for MPLS.
368 for (linkt = FIB_LINK_IP4; linkt <= FIB_LINK_IP6; linkt++)
370 t->adj_index[linkt] = adj_nbr_add_or_lock(FIB_PROTOCOL_IP4,
375 rewrite = gre_rewrite(t, linkt);
376 adj_nbr_midchain_update_rewrite(t->adj_index[linkt],
381 t->adj_index[FIB_LINK_MPLS] = ADJ_INDEX_INVALID;
384 clib_memcpy (&t->tunnel_src, &a->src, sizeof (t->tunnel_src));
385 clib_memcpy (&t->tunnel_dst, &a->dst, sizeof (t->tunnel_dst));
389 *sw_if_indexp = sw_if_index;
395 vnet_gre_tunnel_delete (vnet_gre_add_del_tunnel_args_t *a,
398 gre_main_t * gm = &gre_main;
399 vnet_main_t * vnm = gm->vnet_main;
404 t = gre_tunnel_db_find(&a->src, &a->dst, a->outer_fib_id);
407 return VNET_API_ERROR_NO_SUCH_ENTRY;
409 sw_if_index = t->sw_if_index;
410 vnet_sw_interface_set_flags (vnm, sw_if_index, 0 /* down */);
411 /* make sure tunnel is removed from l2 bd or xconnect */
412 set_int_l2_mode(gm->vlib_main, vnm, MODE_L3, sw_if_index, 0, 0, 0, 0);
413 vec_add1 (gm->free_gre_tunnel_hw_if_indices, t->hw_if_index);
414 gm->tunnel_index_by_sw_if_index[sw_if_index] = ~0;
415 ip4_sw_interface_enable_disable(sw_if_index, 0);
417 fib_entry_child_remove(t->fib_entry_index,
419 fib_table_entry_delete_index(t->fib_entry_index,
422 FOR_EACH_FIB_LINK(linkt)
424 adj_unlock(t->adj_index[linkt]);
427 gre_tunnel_db_remove(t);
428 fib_node_deinit(&t->node);
429 pool_put (gm->tunnels, t);
432 *sw_if_indexp = sw_if_index;
438 vnet_gre_add_del_tunnel (vnet_gre_add_del_tunnel_args_t *a,
442 return (vnet_gre_tunnel_add(a, sw_if_indexp));
444 return (vnet_gre_tunnel_delete(a, sw_if_indexp));
448 gre_sw_interface_mpls_state_change (u32 sw_if_index,
451 gre_main_t *gm = &gre_main;
452 vnet_hw_interface_t * hi;
456 if ((vec_len(gm->tunnel_index_by_sw_if_index) < sw_if_index) ||
457 (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index]))
460 t = pool_elt_at_index(gm->tunnels,
461 gm->tunnel_index_by_sw_if_index[sw_if_index]);
465 hi = vnet_get_hw_interface (vnet_get_main(), t->hw_if_index);
466 t->adj_index[FIB_LINK_MPLS] =
467 adj_nbr_add_or_lock(FIB_PROTOCOL_IP4,
472 rewrite = gre_rewrite(t, FIB_LINK_MPLS);
473 adj_nbr_midchain_update_rewrite(t->adj_index[FIB_LINK_MPLS],
480 adj_unlock(t->adj_index[FIB_LINK_MPLS]);
481 t->adj_index[FIB_LINK_MPLS] = ADJ_INDEX_INVALID;
487 static clib_error_t *
488 create_gre_tunnel_command_fn (vlib_main_t * vm,
489 unformat_input_t * input,
490 vlib_cli_command_t * cmd)
492 unformat_input_t _line_input, * line_input = &_line_input;
493 vnet_gre_add_del_tunnel_args_t _a, * a = &_a;
494 ip4_address_t src, dst;
495 u32 outer_fib_id = 0;
502 /* Get a line of input. */
503 if (! unformat_user (input, unformat_line_input, line_input))
506 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
507 if (unformat (line_input, "del"))
509 else if (unformat (line_input, "src %U", unformat_ip4_address, &src))
511 else if (unformat (line_input, "dst %U", unformat_ip4_address, &dst))
513 else if (unformat (line_input, "outer-fib-id %d", &outer_fib_id))
515 else if (unformat (line_input, "teb"))
518 return clib_error_return (0, "unknown input `%U'",
519 format_unformat_error, input);
521 unformat_free (line_input);
524 return clib_error_return (0, "mandatory argument(s) missing");
526 if (memcmp (&src, &dst, sizeof(src)) == 0)
527 return clib_error_return (0, "src and dst are identical");
529 memset (a, 0, sizeof (*a));
530 a->outer_fib_id = outer_fib_id;
532 clib_memcpy(&a->src, &src, sizeof(src));
533 clib_memcpy(&a->dst, &dst, sizeof(dst));
536 rv = vnet_gre_tunnel_add(a, &sw_if_index);
538 rv = vnet_gre_tunnel_delete(a, &sw_if_index);
543 vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index);
545 case VNET_API_ERROR_INVALID_VALUE:
546 return clib_error_return (0, "GRE tunnel already exists...");
547 case VNET_API_ERROR_NO_SUCH_FIB:
548 return clib_error_return (0, "outer fib ID %d doesn't exist\n",
551 return clib_error_return (0, "vnet_gre_add_del_tunnel returned %d", rv);
557 VLIB_CLI_COMMAND (create_gre_tunnel_command, static) = {
558 .path = "create gre tunnel",
559 .short_help = "create gre tunnel src <addr> dst <addr> "
560 "[outer-fib-id <fib>] [teb] [del]",
561 .function = create_gre_tunnel_command_fn,
564 static clib_error_t *
565 show_gre_tunnel_command_fn (vlib_main_t * vm,
566 unformat_input_t * input,
567 vlib_cli_command_t * cmd)
569 gre_main_t * gm = &gre_main;
573 if (pool_elts (gm->tunnels) == 0)
574 vlib_cli_output (vm, "No GRE tunnels configured...");
576 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
578 if (unformat (input, "%d", &ti))
586 pool_foreach (t, gm->tunnels,
588 vlib_cli_output (vm, "%U", format_gre_tunnel, t, 0);
593 t = pool_elt_at_index(gm->tunnels, ti);
595 vlib_cli_output (vm, "%U", format_gre_tunnel, t, 1);
601 VLIB_CLI_COMMAND (show_gre_tunnel_command, static) = {
602 .path = "show gre tunnel",
603 .function = show_gre_tunnel_command_fn,
606 /* force inclusion from application's main.c */
607 clib_error_t *gre_interface_init (vlib_main_t *vm)
609 vec_add1(mpls_main.mpls_interface_state_change_callbacks,
610 gre_sw_interface_mpls_state_change);
612 fib_node_register_type(FIB_NODE_TYPE_GRE_TUNNEL, &gre_vft);
616 VLIB_INIT_FUNCTION(gre_interface_init);