blob: d574e5967791dd3e32aa052156a188d2858e5174 [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 inline u64
Ciara Loftus7eac9162016-09-30 15:47:03 +010031gre4_mk_key (const ip4_address_t *src,
Neale Ranns0bfe5d82016-08-25 15:29:12 +010032 const ip4_address_t *dst,
33 u32 out_fib_index)
34{
35 // FIXME. the fib index should be part of the key
36 return ((u64)src->as_u32 << 32 | (u64)dst->as_u32);
37}
38
39static u8 *
Neale Ranns177bbdc2016-11-15 09:46:51 +000040format_gre_tunnel_type (u8 * s, va_list * args)
41{
42 gre_tunnel_type_t type = va_arg (*args, gre_tunnel_type_t);
43
44 return (format(s, "%s", gre_tunnel_type_names[type]));
45}
46
47static u8 *
Neale Ranns0bfe5d82016-08-25 15:29:12 +010048format_gre_tunnel (u8 * s, va_list * args)
Chris Luke27fe48f2016-04-28 13:44:38 -040049{
50 gre_tunnel_t * t = va_arg (*args, gre_tunnel_t *);
51 gre_main_t * gm = &gre_main;
Ciara Loftus7eac9162016-09-30 15:47:03 +010052 u8 is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
Chris Luke27fe48f2016-04-28 13:44:38 -040053
Ciara Loftus7eac9162016-09-30 15:47:03 +010054 if (!is_ipv6)
55 s = format (s,
56 "[%d] %U (src) %U (dst) payload %U outer_fib_index %d",
57 t - gm->tunnels,
58 format_ip4_address, &t->tunnel_src.ip4,
59 format_ip4_address, &t->tunnel_dst.fp_addr.ip4,
60 format_gre_tunnel_type, t->type,
61 t->outer_fib_index);
62 else
63 s = format (s,
64 "[%d] %U (src) %U (dst) payload %U outer_fib_index %d",
65 t - gm->tunnels,
66 format_ip6_address, &t->tunnel_src.ip6,
67 format_ip6_address, &t->tunnel_dst.fp_addr.ip6,
68 format_gre_tunnel_type, t->type,
69 t->outer_fib_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010070
Chris Luke27fe48f2016-04-28 13:44:38 -040071 return s;
72}
73
Neale Ranns0bfe5d82016-08-25 15:29:12 +010074static gre_tunnel_t *
Ciara Loftus7eac9162016-09-30 15:47:03 +010075gre_tunnel_db_find (const ip46_address_t *src,
76 const ip46_address_t *dst,
77 u32 out_fib_index,
78 u8 is_ipv6)
Neale Ranns0bfe5d82016-08-25 15:29:12 +010079{
80 gre_main_t * gm = &gre_main;
81 uword * p;
Ciara Loftus7eac9162016-09-30 15:47:03 +010082 u64 key4, key6[4];
Neale Ranns0bfe5d82016-08-25 15:29:12 +010083
Ciara Loftus7eac9162016-09-30 15:47:03 +010084 if (!is_ipv6)
85 {
86 key4 = gre4_mk_key(&src->ip4, &dst->ip4, out_fib_index);
87 p = hash_get (gm->tunnel_by_key4, key4);
88 }
89 else
90 {
91 key6[0] = src->ip6.as_u64[0];
92 key6[1] = src->ip6.as_u64[1];
93 key6[2] = dst->ip6.as_u64[0];
94 key6[3] = dst->ip6.as_u64[1];
95 p = hash_get_mem (gm->tunnel_by_key6, key6);
96 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +010097
98 if (NULL == p)
99 return (NULL);
100
101 return (pool_elt_at_index (gm->tunnels, p[0]));
102}
103
104static void
105gre_tunnel_db_add (const gre_tunnel_t *t)
106{
107 gre_main_t * gm = &gre_main;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100108 u64 key4, key6[4], *key6_copy;
109 u8 is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100110
Ciara Loftus7eac9162016-09-30 15:47:03 +0100111 if (!is_ipv6)
112 {
113 key4 = gre4_mk_key(&t->tunnel_src.ip4, &t->tunnel_dst.fp_addr.ip4,
114 t->outer_fib_index);
115 hash_set (gm->tunnel_by_key4, key4, t - gm->tunnels);
116 }
117 else
118 {
119 key6[0] = t->tunnel_src.ip6.as_u64[0];
120 key6[1] = t->tunnel_src.ip6.as_u64[1];
121 key6[2] = t->tunnel_dst.fp_addr.ip6.as_u64[0];
122 key6[3] = t->tunnel_dst.fp_addr.ip6.as_u64[1];
123 key6_copy = clib_mem_alloc (sizeof (key6));
124 clib_memcpy (key6_copy, key6, sizeof (key6));
125 hash_set_mem (gm->tunnel_by_key6, key6_copy, t - gm->tunnels);
126 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100127}
128
129static void
130gre_tunnel_db_remove (const gre_tunnel_t *t)
131{
132 gre_main_t * gm = &gre_main;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100133 u64 key4, key6[4];
134 u8 is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100135
Ciara Loftus7eac9162016-09-30 15:47:03 +0100136 if (!is_ipv6)
137 {
138 key4 = gre4_mk_key(&t->tunnel_src.ip4, &t->tunnel_dst.fp_addr.ip4,
139 t->outer_fib_index);
140 hash_unset (gm->tunnel_by_key4, key4);
141 }
142 else
143 {
144 key6[0] = t->tunnel_src.ip6.as_u64[0];
145 key6[1] = t->tunnel_src.ip6.as_u64[1];
146 key6[2] = t->tunnel_dst.fp_addr.ip6.as_u64[0];
147 key6[3] = t->tunnel_dst.fp_addr.ip6.as_u64[1];
148 hash_unset_mem (gm->tunnel_by_key6, key6);
149 }
150
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100151}
152
153static gre_tunnel_t *
154gre_tunnel_from_fib_node (fib_node_t *node)
155{
156#if (CLIB_DEBUG > 0)
157 ASSERT(FIB_NODE_TYPE_GRE_TUNNEL == node->fn_type);
158#endif
159 return ((gre_tunnel_t*) (((char*)node) -
160 STRUCT_OFFSET_OF(gre_tunnel_t, node)));
161}
162
Neale Ranns5e575b12016-10-03 09:40:25 +0100163/**
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100164 * gre_tunnel_stack
165 *
166 * 'stack' (resolve the recursion for) the tunnel's midchain adjacency
167 */
Neale Ranns5e575b12016-10-03 09:40:25 +0100168void
Neale Rannsb80c5362016-10-08 13:03:40 +0100169gre_tunnel_stack (adj_index_t ai)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100170{
Neale Rannsb80c5362016-10-08 13:03:40 +0100171 gre_main_t * gm = &gre_main;
172 ip_adjacency_t *adj;
173 gre_tunnel_t *gt;
174 u32 sw_if_index;
175
176 adj = adj_get(ai);
177 sw_if_index = adj->rewrite_header.sw_if_index;
178
179 if ((vec_len(gm->tunnel_index_by_sw_if_index) < sw_if_index) ||
180 (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index]))
181 return;
182
183 gt = pool_elt_at_index(gm->tunnels,
184 gm->tunnel_index_by_sw_if_index[sw_if_index]);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100185
186 /*
187 * find the adjacency that is contributed by the FIB entry
188 * that this tunnel resovles via, and use it as the next adj
189 * in the midchain
190 */
Neale Rannsb80c5362016-10-08 13:03:40 +0100191 if (vnet_hw_interface_get_flags(vnet_get_main(),
192 gt->hw_if_index) &
193 VNET_HW_INTERFACE_FLAG_LINK_UP)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100194 {
Neale Rannsb80c5362016-10-08 13:03:40 +0100195 adj_nbr_midchain_stack(
196 ai,
197 fib_entry_contribute_ip_forwarding(gt->fib_entry_index));
198 }
199 else
200 {
201 adj_nbr_midchain_unstack(ai);
202 }
203}
204
205/**
206 * @brief Call back when restacking all adjacencies on a GRE interface
207 */
208static adj_walk_rc_t
209gre_adj_walk_cb (adj_index_t ai,
210 void *ctx)
211{
212 gre_tunnel_stack(ai);
213
214 return (ADJ_WALK_RC_CONTINUE);
215}
216
217static void
218gre_tunnel_restack (gre_tunnel_t *gt)
219{
220 fib_protocol_t proto;
221
222 /*
223 * walk all the adjacencies on th GRE interface and restack them
224 */
225 FOR_EACH_FIB_IP_PROTOCOL(proto)
226 {
227 adj_nbr_walk(gt->sw_if_index,
228 proto,
229 gre_adj_walk_cb,
230 NULL);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100231 }
232}
233
234/**
235 * Function definition to backwalk a FIB node
236 */
237static fib_node_back_walk_rc_t
238gre_tunnel_back_walk (fib_node_t *node,
Neale Rannsb80c5362016-10-08 13:03:40 +0100239 fib_node_back_walk_ctx_t *ctx)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100240{
Neale Rannsb80c5362016-10-08 13:03:40 +0100241 gre_tunnel_restack(gre_tunnel_from_fib_node(node));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100242
243 return (FIB_NODE_BACK_WALK_CONTINUE);
244}
245
246/**
247 * Function definition to get a FIB node from its index
248 */
249static fib_node_t*
250gre_tunnel_fib_node_get (fib_node_index_t index)
251{
252 gre_tunnel_t * gt;
253 gre_main_t * gm;
254
255 gm = &gre_main;
256 gt = pool_elt_at_index(gm->tunnels, index);
257
258 return (&gt->node);
259}
260
261/**
262 * Function definition to inform the FIB node that its last lock has gone.
263 */
264static void
265gre_tunnel_last_lock_gone (fib_node_t *node)
266{
267 /*
268 * The MPLS GRE tunnel is a root of the graph. As such
269 * it never has children and thus is never locked.
270 */
271 ASSERT(0);
272}
273
274/*
275 * Virtual function table registered by MPLS GRE tunnels
276 * for participation in the FIB object graph.
277 */
278const static fib_node_vft_t gre_vft = {
279 .fnv_get = gre_tunnel_fib_node_get,
280 .fnv_last_lock = gre_tunnel_last_lock_gone,
281 .fnv_back_walk = gre_tunnel_back_walk,
282};
283
Ciara Loftus7eac9162016-09-30 15:47:03 +0100284static int
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100285vnet_gre_tunnel_add (vnet_gre_add_del_tunnel_args_t *a,
286 u32 * sw_if_indexp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700287{
288 gre_main_t * gm = &gre_main;
Chris Luke27fe48f2016-04-28 13:44:38 -0400289 vnet_main_t * vnm = gm->vnet_main;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100290 ip4_main_t * im4 = &ip4_main;
291 ip6_main_t * im6 = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700292 gre_tunnel_t * t;
293 vnet_hw_interface_t * hi;
Chris Luke27fe48f2016-04-28 13:44:38 -0400294 u32 hw_if_index, sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295 u32 outer_fib_index;
David Hothama8cd3092016-09-19 09:55:07 -0700296 u8 address[6];
297 clib_error_t *error;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100298 u8 is_ipv6 = a->is_ipv6;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700299
Ciara Loftus7eac9162016-09-30 15:47:03 +0100300 if (!is_ipv6)
301 outer_fib_index = ip4_fib_index_from_table_id(a->outer_fib_id);
302 else
303 outer_fib_index = ip6_fib_index_from_table_id(a->outer_fib_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700304
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100305 if (~0 == outer_fib_index)
306 return VNET_API_ERROR_NO_SUCH_FIB;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700307
Ciara Loftus7eac9162016-09-30 15:47:03 +0100308 t = gre_tunnel_db_find(&a->src, &a->dst, a->outer_fib_id, a->is_ipv6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700309
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100310 if (NULL != t)
311 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100313 pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
314 memset (t, 0, sizeof (*t));
315 fib_node_init(&t->node, FIB_NODE_TYPE_GRE_TUNNEL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700316
Neale Ranns177bbdc2016-11-15 09:46:51 +0000317 if (a->teb)
318 t->type = GRE_TUNNEL_TYPE_TEB;
319 else
320 t->type = GRE_TUNNEL_TYPE_L3;
321
322 if (vec_len (gm->free_gre_tunnel_hw_if_indices[t->type]) > 0) {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100323 vnet_interface_main_t * im = &vnm->interface_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324
Neale Ranns177bbdc2016-11-15 09:46:51 +0000325 hw_if_index = gm->free_gre_tunnel_hw_if_indices[t->type]
326 [vec_len (gm->free_gre_tunnel_hw_if_indices[t->type])-1];
327 _vec_len (gm->free_gre_tunnel_hw_if_indices[t->type]) -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700328
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100329 hi = vnet_get_hw_interface (vnm, hw_if_index);
330 hi->dev_instance = t - gm->tunnels;
331 hi->hw_instance = hi->dev_instance;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100333 /* clear old stats of freed tunnel before reuse */
334 sw_if_index = hi->sw_if_index;
335 vnet_interface_counter_lock(im);
336 vlib_zero_combined_counter
Chris Luke27fe48f2016-04-28 13:44:38 -0400337 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], sw_if_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100338 vlib_zero_combined_counter
Chris Luke27fe48f2016-04-28 13:44:38 -0400339 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], sw_if_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100340 vlib_zero_simple_counter
Chris Luke27fe48f2016-04-28 13:44:38 -0400341 (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index);
342 vnet_interface_counter_unlock(im);
Neale Ranns177bbdc2016-11-15 09:46:51 +0000343 if (GRE_TUNNEL_TYPE_TEB == t->type)
Neale Ranns5e575b12016-10-03 09:40:25 +0100344 {
Neale Ranns177bbdc2016-11-15 09:46:51 +0000345 t->l2_tx_arc = vlib_node_add_named_next(vlib_get_main(),
346 hi->tx_node_index,
347 "adj-l2-midchain");
Neale Ranns5e575b12016-10-03 09:40:25 +0100348 }
Chris Luke27fe48f2016-04-28 13:44:38 -0400349 } else {
Neale Ranns177bbdc2016-11-15 09:46:51 +0000350 if (GRE_TUNNEL_TYPE_TEB == t->type)
David Hothama8cd3092016-09-19 09:55:07 -0700351 {
352 /* Default MAC address (d00b:eed0:0000 + sw_if_index) */
353 memset (address, 0, sizeof (address));
354 address[0] = 0xd0;
355 address[1] = 0x0b;
356 address[2] = 0xee;
357 address[3] = 0xd0;
358 address[4] = t - gm->tunnels;
359
Neale Rannsb80c5362016-10-08 13:03:40 +0100360 error = ethernet_register_interface(vnm,
Neale Ranns177bbdc2016-11-15 09:46:51 +0000361 gre_device_teb_class.index,
Neale Rannsb80c5362016-10-08 13:03:40 +0100362 t - gm->tunnels, address,
363 &hw_if_index,
364 0);
David Hothama8cd3092016-09-19 09:55:07 -0700365
366 if (error)
367 {
368 clib_error_report (error);
369 return VNET_API_ERROR_INVALID_REGISTRATION;
370 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100371 hi = vnet_get_hw_interface (vnm, hw_if_index);
372
373 t->l2_tx_arc = vlib_node_add_named_next(vlib_get_main(),
374 hi->tx_node_index,
375 "adj-l2-midchain");
David Hothama8cd3092016-09-19 09:55:07 -0700376 } else {
Neale Rannsb80c5362016-10-08 13:03:40 +0100377 hw_if_index = vnet_register_interface(vnm,
378 gre_device_class.index,
379 t - gm->tunnels,
380 gre_hw_interface_class.index,
381 t - gm->tunnels);
David Hothama8cd3092016-09-19 09:55:07 -0700382 }
383 hi = vnet_get_hw_interface (vnm, hw_if_index);
384 sw_if_index = hi->sw_if_index;
Chris Luke27fe48f2016-04-28 13:44:38 -0400385 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700386
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100387 t->hw_if_index = hw_if_index;
388 t->outer_fib_index = outer_fib_index;
389 t->sw_if_index = sw_if_index;
Neale Rannsad422ed2016-11-02 14:20:04 +0000390 t->l2_adj_index = ADJ_INDEX_INVALID;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100392 vec_validate_init_empty (gm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
393 gm->tunnel_index_by_sw_if_index[sw_if_index] = t - gm->tunnels;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394
Ciara Loftus7eac9162016-09-30 15:47:03 +0100395 if (!is_ipv6)
396 {
397 vec_validate (im4->fib_index_by_sw_if_index, sw_if_index);
398 hi->min_packet_bytes = 64 + sizeof (gre_header_t) + sizeof (ip4_header_t);
399 }
400 else
401 {
402 vec_validate (im6->fib_index_by_sw_if_index, sw_if_index);
403 hi->min_packet_bytes = 64 + sizeof (gre_header_t) + sizeof (ip6_header_t);
404 }
Chris Luke71649002016-05-06 11:51:54 -0400405
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100406 hi->per_packet_overhead_bytes =
Chris Luke27fe48f2016-04-28 13:44:38 -0400407 /* preamble */ 8 + /* inter frame gap */ 12;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100409 /* Standard default gre MTU. */
410 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700411
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100412 /*
413 * source the FIB entry for the tunnel's destination
414 * and become a child thereof. The tunnel will then get poked
415 * when the forwarding for the entry updates, and the tunnel can
416 * re-stack accordingly
417 */
Ciara Loftus7eac9162016-09-30 15:47:03 +0100418
419 clib_memcpy (&t->tunnel_src, &a->src, sizeof (t->tunnel_src));
420 t->tunnel_dst.fp_len = !is_ipv6 ? 32 : 128;
421 t->tunnel_dst.fp_proto = !is_ipv6 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
422 t->tunnel_dst.fp_addr = a->dst;
423
424 gre_tunnel_db_add(t);
Chris Luke27fe48f2016-04-28 13:44:38 -0400425
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100426 t->fib_entry_index =
427 fib_table_entry_special_add(outer_fib_index,
Ciara Loftus7eac9162016-09-30 15:47:03 +0100428 &t->tunnel_dst,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100429 FIB_SOURCE_RR,
Neale Rannsa0558302017-04-13 00:44:52 -0700430 FIB_ENTRY_FLAG_NONE);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100431 t->sibling_index =
432 fib_entry_child_add(t->fib_entry_index,
433 FIB_NODE_TYPE_GRE_TUNNEL,
434 t - gm->tunnels);
Chris Luke27fe48f2016-04-28 13:44:38 -0400435
Neale Ranns177bbdc2016-11-15 09:46:51 +0000436 if (GRE_TUNNEL_TYPE_TEB == t->type)
Neale Rannsb80c5362016-10-08 13:03:40 +0100437 {
Ciara Loftus7eac9162016-09-30 15:47:03 +0100438 t->l2_adj_index = adj_nbr_add_or_lock(t->tunnel_dst.fp_proto,
Neale Ranns924d03a2016-10-19 08:25:46 +0100439 VNET_LINK_ETHERNET,
Neale Rannsb80c5362016-10-08 13:03:40 +0100440 &zero_addr,
441 sw_if_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100442 gre_update_adj(vnm, t->sw_if_index, t->l2_adj_index);
443 }
Chris Luke27fe48f2016-04-28 13:44:38 -0400444
445 if (sw_if_indexp)
446 *sw_if_indexp = sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447
448 return 0;
449}
450
Ciara Loftus7eac9162016-09-30 15:47:03 +0100451static int
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100452vnet_gre_tunnel_delete (vnet_gre_add_del_tunnel_args_t *a,
453 u32 * sw_if_indexp)
454{
455 gre_main_t * gm = &gre_main;
456 vnet_main_t * vnm = gm->vnet_main;
457 gre_tunnel_t * t;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100458 u32 sw_if_index;
459
Ciara Loftus7eac9162016-09-30 15:47:03 +0100460 t = gre_tunnel_db_find(&a->src, &a->dst, a->outer_fib_id, a->is_ipv6);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100461
462 if (NULL == t)
463 return VNET_API_ERROR_NO_SUCH_ENTRY;
464
465 sw_if_index = t->sw_if_index;
466 vnet_sw_interface_set_flags (vnm, sw_if_index, 0 /* down */);
467 /* make sure tunnel is removed from l2 bd or xconnect */
468 set_int_l2_mode(gm->vlib_main, vnm, MODE_L3, sw_if_index, 0, 0, 0, 0);
Neale Ranns177bbdc2016-11-15 09:46:51 +0000469 vec_add1 (gm->free_gre_tunnel_hw_if_indices[t->type], t->hw_if_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100470 gm->tunnel_index_by_sw_if_index[sw_if_index] = ~0;
Neale Ranns177bbdc2016-11-15 09:46:51 +0000471
472 if (GRE_TUNNEL_TYPE_TEB == t->type)
473 adj_unlock(t->l2_adj_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100474
Neale Rannsad422ed2016-11-02 14:20:04 +0000475 if (t->l2_adj_index != ADJ_INDEX_INVALID)
476 adj_unlock(t->l2_adj_index);
477
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100478 fib_entry_child_remove(t->fib_entry_index,
479 t->sibling_index);
480 fib_table_entry_delete_index(t->fib_entry_index,
481 FIB_SOURCE_RR);
482
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100483 gre_tunnel_db_remove(t);
484 fib_node_deinit(&t->node);
485 pool_put (gm->tunnels, t);
486
487 if (sw_if_indexp)
488 *sw_if_indexp = sw_if_index;
489
490 return 0;
491}
492
493int
494vnet_gre_add_del_tunnel (vnet_gre_add_del_tunnel_args_t *a,
495 u32 * sw_if_indexp)
496{
497 if (a->is_add)
498 return (vnet_gre_tunnel_add(a, sw_if_indexp));
499 else
500 return (vnet_gre_tunnel_delete(a, sw_if_indexp));
501}
502
Neale Rannsb80c5362016-10-08 13:03:40 +0100503clib_error_t *
504gre_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100505{
Neale Rannsb80c5362016-10-08 13:03:40 +0100506 gre_main_t * gm = &gre_main;
507 vnet_hw_interface_t * hi;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100508 gre_tunnel_t *t;
Neale Rannsb80c5362016-10-08 13:03:40 +0100509 u32 ti;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100510
Neale Rannsb80c5362016-10-08 13:03:40 +0100511 hi = vnet_get_hw_interface (vnm, hw_if_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100512
Neale Rannsb80c5362016-10-08 13:03:40 +0100513 if (NULL == gm->tunnel_index_by_sw_if_index ||
514 hi->sw_if_index >= vec_len(gm->tunnel_index_by_sw_if_index))
515 return (NULL);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100516
Neale Rannsb80c5362016-10-08 13:03:40 +0100517 ti = gm->tunnel_index_by_sw_if_index[hi->sw_if_index];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100518
Neale Rannsb80c5362016-10-08 13:03:40 +0100519 if (~0 == ti)
520 /* not one of ours */
521 return (NULL);
522
523 t = pool_elt_at_index(gm->tunnels, ti);
524
525 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
526 vnet_hw_interface_set_flags (vnm, hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100527 else
Neale Rannsb80c5362016-10-08 13:03:40 +0100528 vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100529
Neale Rannsb80c5362016-10-08 13:03:40 +0100530 gre_tunnel_restack(t);
531
532 return /* no error */ 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100533}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700534
535static clib_error_t *
536create_gre_tunnel_command_fn (vlib_main_t * vm,
537 unformat_input_t * input,
538 vlib_cli_command_t * cmd)
539{
540 unformat_input_t _line_input, * line_input = &_line_input;
Chris Luke27fe48f2016-04-28 13:44:38 -0400541 vnet_gre_add_del_tunnel_args_t _a, * a = &_a;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100542 ip46_address_t src, dst;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543 u32 outer_fib_id = 0;
David Hothama8cd3092016-09-19 09:55:07 -0700544 u8 teb = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700545 int rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700546 u32 num_m_args = 0;
Chris Luke27fe48f2016-04-28 13:44:38 -0400547 u8 is_add = 1;
Pierre Pfister78ea9c22016-05-23 12:51:54 +0100548 u32 sw_if_index;
Billy McFalla9a20e72017-02-15 11:39:12 -0500549 clib_error_t *error = NULL;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100550 u8 ipv4_set = 0;
551 u8 ipv6_set = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700552
553 /* Get a line of input. */
554 if (! unformat_user (input, unformat_line_input, line_input))
555 return 0;
556
557 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
Chris Luke27fe48f2016-04-28 13:44:38 -0400558 if (unformat (line_input, "del"))
559 is_add = 0;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100560 else if (unformat (line_input, "src %U", unformat_ip4_address, &src.ip4)) {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700561 num_m_args++;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100562 ipv4_set = 1;
563 } else if (unformat (line_input, "dst %U", unformat_ip4_address, &dst.ip4)) {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700564 num_m_args++;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100565 ipv4_set = 1;
566 } else if (unformat (line_input, "src %U", unformat_ip6_address, &src.ip6)) {
567 num_m_args++;
568 ipv6_set = 1;
569 } else if (unformat (line_input, "dst %U", unformat_ip6_address, &dst.ip6)) {
570 num_m_args++;
571 ipv6_set = 1;
572 } else if (unformat (line_input, "outer-fib-id %d", &outer_fib_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700573 ;
David Hothama8cd3092016-09-19 09:55:07 -0700574 else if (unformat (line_input, "teb"))
575 teb = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700576 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500577 {
578 error = clib_error_return (0, "unknown input `%U'",
579 format_unformat_error, line_input);
580 goto done;
581 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700582 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700583
584 if (num_m_args < 2)
Billy McFalla9a20e72017-02-15 11:39:12 -0500585 {
586 error = clib_error_return (0, "mandatory argument(s) missing");
587 goto done;
588 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700589
Ciara Loftus7eac9162016-09-30 15:47:03 +0100590 if ((ipv4_set && memcmp (&src.ip4, &dst.ip4, sizeof(src.ip4)) == 0) ||
591 (ipv6_set && memcmp (&src.ip6, &dst.ip6, sizeof(src.ip6)) == 0))
Billy McFalla9a20e72017-02-15 11:39:12 -0500592 {
593 error = clib_error_return (0, "src and dst are identical");
594 goto done;
595 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700596
Ciara Loftus7eac9162016-09-30 15:47:03 +0100597 if (ipv4_set && ipv6_set)
598 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
599
600 if ((ipv4_set && memcmp (&dst.ip4, &zero_addr.ip4, sizeof(dst.ip4)) == 0) ||
601 (ipv6_set && memcmp (&dst.ip6, &zero_addr.ip6, sizeof(dst.ip6)) == 0))
602 {
603 error = clib_error_return (0, "dst address cannot be zero");
604 goto done;
605 }
606
Chris Luke27fe48f2016-04-28 13:44:38 -0400607 memset (a, 0, sizeof (*a));
Hongjun Ni11bfc2f2016-07-22 18:19:19 +0800608 a->outer_fib_id = outer_fib_id;
David Hothama8cd3092016-09-19 09:55:07 -0700609 a->teb = teb;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100610 a->is_ipv6 = ipv6_set;
611 if (!ipv6_set)
612 {
613 clib_memcpy(&a->src.ip4, &src.ip4, sizeof(src.ip4));
614 clib_memcpy(&a->dst.ip4, &dst.ip4, sizeof(dst.ip4));
615 }
616 else
617 {
618 clib_memcpy(&a->src.ip6, &src.ip6, sizeof(src.ip6));
619 clib_memcpy(&a->dst.ip6, &dst.ip6, sizeof(dst.ip6));
620 }
Chris Luke27fe48f2016-04-28 13:44:38 -0400621
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100622 if (is_add)
623 rv = vnet_gre_tunnel_add(a, &sw_if_index);
624 else
625 rv = vnet_gre_tunnel_delete(a, &sw_if_index);
Chris Luke27fe48f2016-04-28 13:44:38 -0400626
627 switch(rv)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700628 {
629 case 0:
Pierre Pfister78ea9c22016-05-23 12:51:54 +0100630 vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700631 break;
632 case VNET_API_ERROR_INVALID_VALUE:
Billy McFalla9a20e72017-02-15 11:39:12 -0500633 error = clib_error_return (0, "GRE tunnel already exists...");
634 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700635 case VNET_API_ERROR_NO_SUCH_FIB:
Billy McFalla9a20e72017-02-15 11:39:12 -0500636 error = clib_error_return (0, "outer fib ID %d doesn't exist\n",
637 outer_fib_id);
638 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700639 default:
Billy McFalla9a20e72017-02-15 11:39:12 -0500640 error = clib_error_return (0, "vnet_gre_add_del_tunnel returned %d", rv);
641 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700642 }
643
Billy McFalla9a20e72017-02-15 11:39:12 -0500644done:
645 unformat_free (line_input);
646
647 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700648}
649
650VLIB_CLI_COMMAND (create_gre_tunnel_command, static) = {
651 .path = "create gre tunnel",
Chris Luke27fe48f2016-04-28 13:44:38 -0400652 .short_help = "create gre tunnel src <addr> dst <addr> "
David Hothama8cd3092016-09-19 09:55:07 -0700653 "[outer-fib-id <fib>] [teb] [del]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700654 .function = create_gre_tunnel_command_fn,
655};
656
Chris Luke27fe48f2016-04-28 13:44:38 -0400657static clib_error_t *
658show_gre_tunnel_command_fn (vlib_main_t * vm,
659 unformat_input_t * input,
660 vlib_cli_command_t * cmd)
661{
662 gre_main_t * gm = &gre_main;
663 gre_tunnel_t * t;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100664 u32 ti = ~0;
Chris Luke27fe48f2016-04-28 13:44:38 -0400665
666 if (pool_elts (gm->tunnels) == 0)
667 vlib_cli_output (vm, "No GRE tunnels configured...");
668
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100669 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
670 {
671 if (unformat (input, "%d", &ti))
672 ;
673 else
674 break;
675 }
676
677 if (~0 == ti)
678 {
679 pool_foreach (t, gm->tunnels,
680 ({
Neale Rannsb80c5362016-10-08 13:03:40 +0100681 vlib_cli_output (vm, "%U", format_gre_tunnel, t);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100682 }));
683 }
684 else
685 {
686 t = pool_elt_at_index(gm->tunnels, ti);
687
Neale Rannsb80c5362016-10-08 13:03:40 +0100688 vlib_cli_output (vm, "%U", format_gre_tunnel, t);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100689 }
Chris Luke27fe48f2016-04-28 13:44:38 -0400690
691 return 0;
692}
693
694VLIB_CLI_COMMAND (show_gre_tunnel_command, static) = {
695 .path = "show gre tunnel",
696 .function = show_gre_tunnel_command_fn,
697};
698
Ed Warnickecb9cada2015-12-08 15:45:58 -0700699/* force inclusion from application's main.c */
700clib_error_t *gre_interface_init (vlib_main_t *vm)
701{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100702 fib_node_register_type(FIB_NODE_TYPE_GRE_TUNNEL, &gre_vft);
703
Ed Warnickecb9cada2015-12-08 15:45:58 -0700704 return 0;
705}
706VLIB_INIT_FUNCTION(gre_interface_init);