blob: ce9685d55a3a4533fd162eecd27fbaeaa78b8544 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * gre_interface.c: gre interfaces
3 *
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:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18#include <vnet/vnet.h>
19#include <vnet/pg/pg.h>
20#include <vnet/gre/gre.h>
Chris Luke27fe48f2016-04-28 13:44:38 -040021#include <vnet/ip/format.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010022#include <vnet/fib/ip4_fib.h>
Ciara Loftus7eac9162016-09-30 15:47:03 +010023#include <vnet/fib/ip6_fib.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010024#include <vnet/adj/adj_midchain.h>
Neale Rannsb80c5362016-10-08 13:03:40 +010025#include <vnet/adj/adj_nbr.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010026#include <vnet/mpls/mpls.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070027
Neale Ranns177bbdc2016-11-15 09:46:51 +000028static const char *gre_tunnel_type_names[] = GRE_TUNNEL_TYPE_NAMES;
29
Neale Ranns0bfe5d82016-08-25 15:29:12 +010030static u8 *
31format_gre_tunnel (u8 * s, va_list * args)
Chris Luke27fe48f2016-04-28 13:44:38 -040032{
Swarup Nayak9ff647a2017-11-27 10:27:43 +053033 gre_tunnel_t *t = va_arg (*args, gre_tunnel_t *);
34 gre_main_t *gm = &gre_main;
Chris Luke27fe48f2016-04-28 13:44:38 -040035
John Lo4478d8e2018-01-12 17:15:25 -050036 s = format (s, "[%d] src %U dst %U fib-idx %d sw-if-idx %d ",
37 t - gm->tunnels,
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);
41
42 s = format (s, "payload %s", gre_tunnel_type_names[t->type]);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010043
Chris Luke27fe48f2016-04-28 13:44:38 -040044 return s;
45}
46
Neale Ranns0bfe5d82016-08-25 15:29:12 +010047static gre_tunnel_t *
Swarup Nayak9ff647a2017-11-27 10:27:43 +053048gre_tunnel_db_find (const ip46_address_t * src,
Neale Ranns33ce60d2017-12-14 08:51:32 -080049 const ip46_address_t * dst,
50 u32 out_fib_index, u8 is_ipv6, gre_tunnel_key_t * key)
Neale Ranns0bfe5d82016-08-25 15:29:12 +010051{
Swarup Nayak9ff647a2017-11-27 10:27:43 +053052 gre_main_t *gm = &gre_main;
53 uword *p;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010054
Ciara Loftus7eac9162016-09-30 15:47:03 +010055 if (!is_ipv6)
56 {
Neale Ranns33ce60d2017-12-14 08:51:32 -080057 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);
Ciara Loftus7eac9162016-09-30 15:47:03 +010059 }
60 else
61 {
Neale Ranns33ce60d2017-12-14 08:51:32 -080062 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);
Ciara Loftus7eac9162016-09-30 15:47:03 +010064 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +010065
66 if (NULL == p)
67 return (NULL);
68
69 return (pool_elt_at_index (gm->tunnels, p[0]));
70}
71
72static void
Neale Ranns33ce60d2017-12-14 08:51:32 -080073gre_tunnel_db_add (gre_tunnel_t * t, gre_tunnel_key_t * key)
Neale Ranns0bfe5d82016-08-25 15:29:12 +010074{
Swarup Nayak9ff647a2017-11-27 10:27:43 +053075 gre_main_t *gm = &gre_main;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010076
Neale Ranns33ce60d2017-12-14 08:51:32 -080077 t->key = clib_mem_alloc (sizeof (*t->key));
78 clib_memcpy (t->key, key, sizeof (*key));
79
80 if (t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6)
Ciara Loftus7eac9162016-09-30 15:47:03 +010081 {
Neale Ranns33ce60d2017-12-14 08:51:32 -080082 hash_set_mem (gm->tunnel_by_key6, &t->key->gtk_v6, t - gm->tunnels);
Ciara Loftus7eac9162016-09-30 15:47:03 +010083 }
84 else
85 {
Neale Ranns33ce60d2017-12-14 08:51:32 -080086 hash_set_mem (gm->tunnel_by_key4, &t->key->gtk_v4, t - gm->tunnels);
Ciara Loftus7eac9162016-09-30 15:47:03 +010087 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +010088}
89
90static void
Neale Ranns33ce60d2017-12-14 08:51:32 -080091gre_tunnel_db_remove (gre_tunnel_t * t)
Neale Ranns0bfe5d82016-08-25 15:29:12 +010092{
Swarup Nayak9ff647a2017-11-27 10:27:43 +053093 gre_main_t *gm = &gre_main;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010094
Neale Ranns33ce60d2017-12-14 08:51:32 -080095 if (t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6)
Ciara Loftus7eac9162016-09-30 15:47:03 +010096 {
Neale Ranns33ce60d2017-12-14 08:51:32 -080097 hash_unset_mem (gm->tunnel_by_key6, &t->key->gtk_v6);
Ciara Loftus7eac9162016-09-30 15:47:03 +010098 }
99 else
100 {
Neale Ranns33ce60d2017-12-14 08:51:32 -0800101 hash_unset_mem (gm->tunnel_by_key4, &t->key->gtk_v4);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100102 }
103
Neale Ranns33ce60d2017-12-14 08:51:32 -0800104 clib_mem_free (t->key);
105 t->key = NULL;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100106}
107
108static gre_tunnel_t *
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530109gre_tunnel_from_fib_node (fib_node_t * node)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100110{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530111 ASSERT (FIB_NODE_TYPE_GRE_TUNNEL == node->fn_type);
112 return ((gre_tunnel_t *) (((char *) node) -
113 STRUCT_OFFSET_OF (gre_tunnel_t, node)));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100114}
115
Neale Ranns5e575b12016-10-03 09:40:25 +0100116/**
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100117 * gre_tunnel_stack
118 *
119 * 'stack' (resolve the recursion for) the tunnel's midchain adjacency
120 */
Neale Ranns5e575b12016-10-03 09:40:25 +0100121void
Neale Rannsb80c5362016-10-08 13:03:40 +0100122gre_tunnel_stack (adj_index_t ai)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100123{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530124 gre_main_t *gm = &gre_main;
125 ip_adjacency_t *adj;
126 gre_tunnel_t *gt;
127 u32 sw_if_index;
Neale Rannsb80c5362016-10-08 13:03:40 +0100128
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530129 adj = adj_get (ai);
130 sw_if_index = adj->rewrite_header.sw_if_index;
Neale Rannsb80c5362016-10-08 13:03:40 +0100131
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530132 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]))
134 return;
Neale Rannsb80c5362016-10-08 13:03:40 +0100135
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530136 gt = pool_elt_at_index (gm->tunnels,
137 gm->tunnel_index_by_sw_if_index[sw_if_index]);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100138
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530139 /*
140 * find the adjacency that is contributed by the FIB entry
141 * that this tunnel resovles via, and use it as the next adj
142 * in the midchain
143 */
144 if (vnet_hw_interface_get_flags (vnet_get_main (),
145 gt->hw_if_index) &
146 VNET_HW_INTERFACE_FLAG_LINK_UP)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100147 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530148 adj_nbr_midchain_stack (ai,
149 fib_entry_contribute_ip_forwarding
150 (gt->fib_entry_index));
Neale Rannsb80c5362016-10-08 13:03:40 +0100151 }
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530152 else
Neale Rannsb80c5362016-10-08 13:03:40 +0100153 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530154 adj_nbr_midchain_unstack (ai);
Neale Rannsb80c5362016-10-08 13:03:40 +0100155 }
156}
157
158/**
159 * @brief Call back when restacking all adjacencies on a GRE interface
160 */
161static adj_walk_rc_t
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530162gre_adj_walk_cb (adj_index_t ai, void *ctx)
Neale Rannsb80c5362016-10-08 13:03:40 +0100163{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530164 gre_tunnel_stack (ai);
Neale Rannsb80c5362016-10-08 13:03:40 +0100165
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530166 return (ADJ_WALK_RC_CONTINUE);
Neale Rannsb80c5362016-10-08 13:03:40 +0100167}
168
169static void
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530170gre_tunnel_restack (gre_tunnel_t * gt)
Neale Rannsb80c5362016-10-08 13:03:40 +0100171{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530172 fib_protocol_t proto;
Neale Rannsb80c5362016-10-08 13:03:40 +0100173
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530174 /*
175 * walk all the adjacencies on th GRE interface and restack them
176 */
177 FOR_EACH_FIB_IP_PROTOCOL (proto)
178 {
179 adj_nbr_walk (gt->sw_if_index, proto, gre_adj_walk_cb, NULL);
180 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100181}
182
183/**
184 * Function definition to backwalk a FIB node
185 */
186static fib_node_back_walk_rc_t
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530187gre_tunnel_back_walk (fib_node_t * node, fib_node_back_walk_ctx_t * ctx)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100188{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530189 gre_tunnel_restack (gre_tunnel_from_fib_node (node));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100190
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530191 return (FIB_NODE_BACK_WALK_CONTINUE);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100192}
193
194/**
195 * Function definition to get a FIB node from its index
196 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530197static fib_node_t *
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100198gre_tunnel_fib_node_get (fib_node_index_t index)
199{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530200 gre_tunnel_t *gt;
201 gre_main_t *gm;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100202
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530203 gm = &gre_main;
204 gt = pool_elt_at_index (gm->tunnels, index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100205
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530206 return (&gt->node);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100207}
208
209/**
210 * Function definition to inform the FIB node that its last lock has gone.
211 */
212static void
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530213gre_tunnel_last_lock_gone (fib_node_t * node)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100214{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530215 /*
216 * The MPLS GRE tunnel is a root of the graph. As such
217 * it never has children and thus is never locked.
218 */
219 ASSERT (0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100220}
221
222/*
223 * Virtual function table registered by MPLS GRE tunnels
224 * for participation in the FIB object graph.
225 */
226const static fib_node_vft_t gre_vft = {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530227 .fnv_get = gre_tunnel_fib_node_get,
228 .fnv_last_lock = gre_tunnel_last_lock_gone,
229 .fnv_back_walk = gre_tunnel_back_walk,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100230};
231
Ciara Loftus7eac9162016-09-30 15:47:03 +0100232static int
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530233vnet_gre_tunnel_add (vnet_gre_add_del_tunnel_args_t * a, u32 * sw_if_indexp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530235 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;
239 gre_tunnel_t *t;
240 vnet_hw_interface_t *hi;
Chris Luke27fe48f2016-04-28 13:44:38 -0400241 u32 hw_if_index, sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242 u32 outer_fib_index;
David Hothama8cd3092016-09-19 09:55:07 -0700243 u8 address[6];
244 clib_error_t *error;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100245 u8 is_ipv6 = a->is_ipv6;
Neale Ranns33ce60d2017-12-14 08:51:32 -0800246 gre_tunnel_key_t key;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247
Ciara Loftus7eac9162016-09-30 15:47:03 +0100248 if (!is_ipv6)
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530249 outer_fib_index = ip4_fib_index_from_table_id (a->outer_fib_id);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100250 else
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530251 outer_fib_index = ip6_fib_index_from_table_id (a->outer_fib_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100253 if (~0 == outer_fib_index)
254 return VNET_API_ERROR_NO_SUCH_FIB;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255
Neale Ranns33ce60d2017-12-14 08:51:32 -0800256 t =
John Lo101e0052018-01-06 00:22:54 -0500257 gre_tunnel_db_find (&a->src, &a->dst, outer_fib_index, a->is_ipv6, &key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100259 if (NULL != t)
260 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100262 pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
263 memset (t, 0, sizeof (*t));
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530264 fib_node_init (&t->node, FIB_NODE_TYPE_GRE_TUNNEL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265
Neale Ranns177bbdc2016-11-15 09:46:51 +0000266 if (a->teb)
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530267 t->type = GRE_TUNNEL_TYPE_TEB;
Neale Ranns177bbdc2016-11-15 09:46:51 +0000268 else
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530269 t->type = GRE_TUNNEL_TYPE_L3;
Neale Ranns177bbdc2016-11-15 09:46:51 +0000270
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530271 if (vec_len (gm->free_gre_tunnel_hw_if_indices[t->type]) > 0)
272 {
273 vnet_interface_main_t *im = &vnm->interface_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274
Neale Ranns177bbdc2016-11-15 09:46:51 +0000275 hw_if_index = gm->free_gre_tunnel_hw_if_indices[t->type]
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530276 [vec_len (gm->free_gre_tunnel_hw_if_indices[t->type]) - 1];
Neale Ranns177bbdc2016-11-15 09:46:51 +0000277 _vec_len (gm->free_gre_tunnel_hw_if_indices[t->type]) -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100279 hi = vnet_get_hw_interface (vnm, hw_if_index);
280 hi->dev_instance = t - gm->tunnels;
281 hi->hw_instance = hi->dev_instance;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700282
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100283 /* clear old stats of freed tunnel before reuse */
284 sw_if_index = hi->sw_if_index;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530285 vnet_interface_counter_lock (im);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100286 vlib_zero_combined_counter
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530287 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX],
288 sw_if_index);
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);
Neale Ranns177bbdc2016-11-15 09:46:51 +0000294 if (GRE_TUNNEL_TYPE_TEB == t->type)
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530295 {
296 t->l2_tx_arc = vlib_node_add_named_next (vlib_get_main (),
297 hi->tx_node_index,
298 "adj-l2-midchain");
299 }
300 }
301 else
302 {
Neale Ranns177bbdc2016-11-15 09:46:51 +0000303 if (GRE_TUNNEL_TYPE_TEB == t->type)
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530304 {
305 /* Default MAC address (d00b:eed0:0000 + sw_if_index) */
306 memset (address, 0, sizeof (address));
307 address[0] = 0xd0;
308 address[1] = 0x0b;
309 address[2] = 0xee;
310 address[3] = 0xd0;
311 address[4] = t - gm->tunnels;
David Hothama8cd3092016-09-19 09:55:07 -0700312
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530313 error = ethernet_register_interface (vnm,
314 gre_device_teb_class.index,
315 t - gm->tunnels, address,
316 &hw_if_index, 0);
David Hothama8cd3092016-09-19 09:55:07 -0700317
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530318 if (error)
319 {
320 clib_error_report (error);
321 return VNET_API_ERROR_INVALID_REGISTRATION;
322 }
323 hi = vnet_get_hw_interface (vnm, hw_if_index);
Neale Ranns5e575b12016-10-03 09:40:25 +0100324
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530325 t->l2_tx_arc = vlib_node_add_named_next (vlib_get_main (),
326 hi->tx_node_index,
327 "adj-l2-midchain");
328 }
329 else
330 {
331 hw_if_index = vnet_register_interface (vnm,
332 gre_device_class.index,
333 t - gm->tunnels,
334 gre_hw_interface_class.index,
335 t - gm->tunnels);
336 }
David Hothama8cd3092016-09-19 09:55:07 -0700337 hi = vnet_get_hw_interface (vnm, hw_if_index);
338 sw_if_index = hi->sw_if_index;
Chris Luke27fe48f2016-04-28 13:44:38 -0400339 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100341 t->hw_if_index = hw_if_index;
342 t->outer_fib_index = outer_fib_index;
343 t->sw_if_index = sw_if_index;
Neale Rannsad422ed2016-11-02 14:20:04 +0000344 t->l2_adj_index = ADJ_INDEX_INVALID;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100346 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348
Ciara Loftus7eac9162016-09-30 15:47:03 +0100349 if (!is_ipv6)
350 {
351 vec_validate (im4->fib_index_by_sw_if_index, sw_if_index);
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530352 hi->min_packet_bytes =
353 64 + sizeof (gre_header_t) + sizeof (ip4_header_t);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100354 }
355 else
356 {
357 vec_validate (im6->fib_index_by_sw_if_index, sw_if_index);
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530358 hi->min_packet_bytes =
359 64 + sizeof (gre_header_t) + sizeof (ip6_header_t);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100360 }
Chris Luke71649002016-05-06 11:51:54 -0400361
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100362 hi->per_packet_overhead_bytes =
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530363 /* preamble */ 8 + /* inter frame gap */ 12;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700364
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100365 /* Standard default gre MTU. */
366 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700367
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100368 /*
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
373 */
Ciara Loftus7eac9162016-09-30 15:47:03 +0100374
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;
379
Neale Ranns33ce60d2017-12-14 08:51:32 -0800380 gre_tunnel_db_add (t, &key);
Chris Luke27fe48f2016-04-28 13:44:38 -0400381
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100382 t->fib_entry_index =
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530383 fib_table_entry_special_add (outer_fib_index,
384 &t->tunnel_dst,
385 FIB_SOURCE_RR, FIB_ENTRY_FLAG_NONE);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100386 t->sibling_index =
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530387 fib_entry_child_add (t->fib_entry_index,
388 FIB_NODE_TYPE_GRE_TUNNEL, t - gm->tunnels);
Chris Luke27fe48f2016-04-28 13:44:38 -0400389
Neale Ranns177bbdc2016-11-15 09:46:51 +0000390 if (GRE_TUNNEL_TYPE_TEB == t->type)
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530391 {
392 t->l2_adj_index = adj_nbr_add_or_lock (t->tunnel_dst.fp_proto,
393 VNET_LINK_ETHERNET,
394 &zero_addr, sw_if_index);
395 gre_update_adj (vnm, t->sw_if_index, t->l2_adj_index);
396 }
Chris Luke27fe48f2016-04-28 13:44:38 -0400397
398 if (sw_if_indexp)
399 *sw_if_indexp = sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700400
401 return 0;
402}
403
Ciara Loftus7eac9162016-09-30 15:47:03 +0100404static int
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530405vnet_gre_tunnel_delete (vnet_gre_add_del_tunnel_args_t * a,
406 u32 * sw_if_indexp)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100407{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530408 gre_main_t *gm = &gre_main;
409 vnet_main_t *vnm = gm->vnet_main;
410 gre_tunnel_t *t;
Neale Ranns33ce60d2017-12-14 08:51:32 -0800411 gre_tunnel_key_t key;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100412 u32 sw_if_index;
John Lo101e0052018-01-06 00:22:54 -0500413 u32 outer_fib_index;
414
415 if (!a->is_ipv6)
416 outer_fib_index = ip4_fib_index_from_table_id (a->outer_fib_id);
417 else
418 outer_fib_index = ip6_fib_index_from_table_id (a->outer_fib_id);
419
420 if (~0 == outer_fib_index)
421 return VNET_API_ERROR_NO_SUCH_FIB;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100422
Neale Ranns33ce60d2017-12-14 08:51:32 -0800423 t =
John Lo101e0052018-01-06 00:22:54 -0500424 gre_tunnel_db_find (&a->src, &a->dst, outer_fib_index, a->is_ipv6, &key);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100425
426 if (NULL == t)
427 return VNET_API_ERROR_NO_SUCH_ENTRY;
428
429 sw_if_index = t->sw_if_index;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530430 vnet_sw_interface_set_flags (vnm, sw_if_index, 0 /* down */ );
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100431 /* make sure tunnel is removed from l2 bd or xconnect */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530432 set_int_l2_mode (gm->vlib_main, vnm, MODE_L3, sw_if_index, 0, 0, 0, 0);
Neale Ranns177bbdc2016-11-15 09:46:51 +0000433 vec_add1 (gm->free_gre_tunnel_hw_if_indices[t->type], t->hw_if_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100434 gm->tunnel_index_by_sw_if_index[sw_if_index] = ~0;
Neale Ranns177bbdc2016-11-15 09:46:51 +0000435
436 if (GRE_TUNNEL_TYPE_TEB == t->type)
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530437 adj_unlock (t->l2_adj_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100438
Neale Rannsad422ed2016-11-02 14:20:04 +0000439 if (t->l2_adj_index != ADJ_INDEX_INVALID)
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530440 adj_unlock (t->l2_adj_index);
Neale Rannsad422ed2016-11-02 14:20:04 +0000441
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530442 fib_entry_child_remove (t->fib_entry_index, t->sibling_index);
443 fib_table_entry_delete_index (t->fib_entry_index, FIB_SOURCE_RR);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100444
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530445 gre_tunnel_db_remove (t);
446 fib_node_deinit (&t->node);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100447 pool_put (gm->tunnels, t);
448
449 if (sw_if_indexp)
450 *sw_if_indexp = sw_if_index;
451
452 return 0;
453}
454
455int
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530456vnet_gre_add_del_tunnel (vnet_gre_add_del_tunnel_args_t * a,
457 u32 * sw_if_indexp)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100458{
459 if (a->is_add)
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530460 return (vnet_gre_tunnel_add (a, sw_if_indexp));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100461 else
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530462 return (vnet_gre_tunnel_delete (a, sw_if_indexp));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100463}
464
Neale Rannsb80c5362016-10-08 13:03:40 +0100465clib_error_t *
466gre_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100467{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530468 gre_main_t *gm = &gre_main;
469 vnet_hw_interface_t *hi;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100470 gre_tunnel_t *t;
Neale Rannsb80c5362016-10-08 13:03:40 +0100471 u32 ti;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100472
Neale Rannsb80c5362016-10-08 13:03:40 +0100473 hi = vnet_get_hw_interface (vnm, hw_if_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100474
Neale Rannsb80c5362016-10-08 13:03:40 +0100475 if (NULL == gm->tunnel_index_by_sw_if_index ||
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530476 hi->sw_if_index >= vec_len (gm->tunnel_index_by_sw_if_index))
477 return (NULL);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100478
Neale Rannsb80c5362016-10-08 13:03:40 +0100479 ti = gm->tunnel_index_by_sw_if_index[hi->sw_if_index];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100480
Neale Rannsb80c5362016-10-08 13:03:40 +0100481 if (~0 == ti)
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530482 /* not one of ours */
483 return (NULL);
Neale Rannsb80c5362016-10-08 13:03:40 +0100484
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530485 t = pool_elt_at_index (gm->tunnels, ti);
Neale Rannsb80c5362016-10-08 13:03:40 +0100486
487 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530488 vnet_hw_interface_set_flags (vnm, hw_if_index,
489 VNET_HW_INTERFACE_FLAG_LINK_UP);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100490 else
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530491 vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100492
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530493 gre_tunnel_restack (t);
Neale Rannsb80c5362016-10-08 13:03:40 +0100494
495 return /* no error */ 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100496}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700497
498static clib_error_t *
499create_gre_tunnel_command_fn (vlib_main_t * vm,
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530500 unformat_input_t * input,
501 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700502{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530503 unformat_input_t _line_input, *line_input = &_line_input;
504 vnet_gre_add_del_tunnel_args_t _a, *a = &_a;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100505 ip46_address_t src, dst;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700506 u32 outer_fib_id = 0;
David Hothama8cd3092016-09-19 09:55:07 -0700507 u8 teb = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508 int rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700509 u32 num_m_args = 0;
Chris Luke27fe48f2016-04-28 13:44:38 -0400510 u8 is_add = 1;
Pierre Pfister78ea9c22016-05-23 12:51:54 +0100511 u32 sw_if_index;
Billy McFalla9a20e72017-02-15 11:39:12 -0500512 clib_error_t *error = NULL;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100513 u8 ipv4_set = 0;
514 u8 ipv6_set = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515
516 /* Get a line of input. */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530517 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700518 return 0;
519
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530520 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
521 {
522 if (unformat (line_input, "del"))
523 is_add = 0;
524 else
525 if (unformat (line_input, "src %U", unformat_ip4_address, &src.ip4))
526 {
527 num_m_args++;
528 ipv4_set = 1;
529 }
530 else
531 if (unformat (line_input, "dst %U", unformat_ip4_address, &dst.ip4))
532 {
533 num_m_args++;
534 ipv4_set = 1;
535 }
536 else
537 if (unformat (line_input, "src %U", unformat_ip6_address, &src.ip6))
538 {
539 num_m_args++;
540 ipv6_set = 1;
541 }
542 else
543 if (unformat (line_input, "dst %U", unformat_ip6_address, &dst.ip6))
544 {
545 num_m_args++;
546 ipv6_set = 1;
547 }
548 else if (unformat (line_input, "outer-fib-id %d", &outer_fib_id))
549 ;
550 else if (unformat (line_input, "teb"))
551 teb = 1;
552 else
553 {
554 error = clib_error_return (0, "unknown input `%U'",
555 format_unformat_error, line_input);
556 goto done;
557 }
558 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700559
560 if (num_m_args < 2)
Billy McFalla9a20e72017-02-15 11:39:12 -0500561 {
562 error = clib_error_return (0, "mandatory argument(s) missing");
563 goto done;
564 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700565
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530566 if ((ipv4_set && memcmp (&src.ip4, &dst.ip4, sizeof (src.ip4)) == 0) ||
567 (ipv6_set && memcmp (&src.ip6, &dst.ip6, sizeof (src.ip6)) == 0))
Billy McFalla9a20e72017-02-15 11:39:12 -0500568 {
569 error = clib_error_return (0, "src and dst are identical");
570 goto done;
571 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700572
Ciara Loftus7eac9162016-09-30 15:47:03 +0100573 if (ipv4_set && ipv6_set)
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530574 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
Ciara Loftus7eac9162016-09-30 15:47:03 +0100575
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530576 if ((ipv4_set && memcmp (&dst.ip4, &zero_addr.ip4, sizeof (dst.ip4)) == 0)
577 || (ipv6_set
578 && memcmp (&dst.ip6, &zero_addr.ip6, sizeof (dst.ip6)) == 0))
Ciara Loftus7eac9162016-09-30 15:47:03 +0100579 {
580 error = clib_error_return (0, "dst address cannot be zero");
581 goto done;
582 }
583
Chris Luke27fe48f2016-04-28 13:44:38 -0400584 memset (a, 0, sizeof (*a));
Hongjun Ni11bfc2f2016-07-22 18:19:19 +0800585 a->outer_fib_id = outer_fib_id;
David Hothama8cd3092016-09-19 09:55:07 -0700586 a->teb = teb;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100587 a->is_ipv6 = ipv6_set;
588 if (!ipv6_set)
589 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530590 clib_memcpy (&a->src.ip4, &src.ip4, sizeof (src.ip4));
591 clib_memcpy (&a->dst.ip4, &dst.ip4, sizeof (dst.ip4));
Ciara Loftus7eac9162016-09-30 15:47:03 +0100592 }
593 else
594 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530595 clib_memcpy (&a->src.ip6, &src.ip6, sizeof (src.ip6));
596 clib_memcpy (&a->dst.ip6, &dst.ip6, sizeof (dst.ip6));
Ciara Loftus7eac9162016-09-30 15:47:03 +0100597 }
Chris Luke27fe48f2016-04-28 13:44:38 -0400598
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100599 if (is_add)
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530600 rv = vnet_gre_tunnel_add (a, &sw_if_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100601 else
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530602 rv = vnet_gre_tunnel_delete (a, &sw_if_index);
Chris Luke27fe48f2016-04-28 13:44:38 -0400603
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530604 switch (rv)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700605 {
606 case 0:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530607 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
608 vnet_get_main (), sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700609 break;
610 case VNET_API_ERROR_INVALID_VALUE:
Billy McFalla9a20e72017-02-15 11:39:12 -0500611 error = clib_error_return (0, "GRE tunnel already exists...");
612 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700613 case VNET_API_ERROR_NO_SUCH_FIB:
Billy McFalla9a20e72017-02-15 11:39:12 -0500614 error = clib_error_return (0, "outer fib ID %d doesn't exist\n",
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530615 outer_fib_id);
Billy McFalla9a20e72017-02-15 11:39:12 -0500616 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700617 default:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530618 error =
619 clib_error_return (0, "vnet_gre_add_del_tunnel returned %d", rv);
Billy McFalla9a20e72017-02-15 11:39:12 -0500620 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700621 }
622
Billy McFalla9a20e72017-02-15 11:39:12 -0500623done:
624 unformat_free (line_input);
625
626 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700627}
628
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530629/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700630VLIB_CLI_COMMAND (create_gre_tunnel_command, static) = {
631 .path = "create gre tunnel",
Chris Luke27fe48f2016-04-28 13:44:38 -0400632 .short_help = "create gre tunnel src <addr> dst <addr> "
David Hothama8cd3092016-09-19 09:55:07 -0700633 "[outer-fib-id <fib>] [teb] [del]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700634 .function = create_gre_tunnel_command_fn,
635};
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530636/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700637
Chris Luke27fe48f2016-04-28 13:44:38 -0400638static clib_error_t *
639show_gre_tunnel_command_fn (vlib_main_t * vm,
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530640 unformat_input_t * input,
641 vlib_cli_command_t * cmd)
Chris Luke27fe48f2016-04-28 13:44:38 -0400642{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530643 gre_main_t *gm = &gre_main;
644 gre_tunnel_t *t;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100645 u32 ti = ~0;
Chris Luke27fe48f2016-04-28 13:44:38 -0400646
647 if (pool_elts (gm->tunnels) == 0)
648 vlib_cli_output (vm, "No GRE tunnels configured...");
649
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100650 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
651 {
652 if (unformat (input, "%d", &ti))
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530653 ;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100654 else
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530655 break;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100656 }
657
658 if (~0 == ti)
659 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530660 /* *INDENT-OFF* */
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100661 pool_foreach (t, gm->tunnels,
662 ({
Neale Rannsb80c5362016-10-08 13:03:40 +0100663 vlib_cli_output (vm, "%U", format_gre_tunnel, t);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100664 }));
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530665 /* *INDENT-ON* */
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100666 }
667 else
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530668 {
669 t = pool_elt_at_index (gm->tunnels, ti);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100670
Neale Rannsb80c5362016-10-08 13:03:40 +0100671 vlib_cli_output (vm, "%U", format_gre_tunnel, t);
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530672 }
Chris Luke27fe48f2016-04-28 13:44:38 -0400673
674 return 0;
675}
676
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530677/* *INDENT-OFF* */
Chris Luke27fe48f2016-04-28 13:44:38 -0400678VLIB_CLI_COMMAND (show_gre_tunnel_command, static) = {
679 .path = "show gre tunnel",
680 .function = show_gre_tunnel_command_fn,
681};
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530682/* *INDENT-ON* */
Chris Luke27fe48f2016-04-28 13:44:38 -0400683
Ed Warnickecb9cada2015-12-08 15:45:58 -0700684/* force inclusion from application's main.c */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530685clib_error_t *
686gre_interface_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700687{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530688 fib_node_register_type (FIB_NODE_TYPE_GRE_TUNNEL, &gre_vft);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100689
Ed Warnickecb9cada2015-12-08 15:45:58 -0700690 return 0;
691}
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530692
693VLIB_INIT_FUNCTION (gre_interface_init);
694
695/*
696 * fd.io coding-style-patch-verification: ON
697 *
698 * Local Variables:
699 * eval: (c-set-style "gnu")
700 * End:
701 */