blob: 178b080a73b5fc2af2a1ba5399878893a20d0445 [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>
Neale Ranns3b81a1e2018-09-06 09:50:26 -070027#include <vnet/l2/l2_input.h>
Neale Ranns4c16d802019-12-17 20:15:03 +000028#include <vnet/nhrp/nhrp.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070029
Neale Ranns5f8f6172019-04-18 10:23:56 +000030u8 *
31format_gre_tunnel_type (u8 * s, va_list * args)
32{
33 gre_tunnel_type_t type = va_arg (*args, int);
34
35 switch (type)
36 {
37#define _(n, v) case GRE_TUNNEL_TYPE_##n: \
38 s = format (s, "%s", v); \
39 break;
40 foreach_gre_tunnel_type
41#undef _
42 }
43
44 return (s);
45}
46
Neale Ranns0bfe5d82016-08-25 15:29:12 +010047static u8 *
48format_gre_tunnel (u8 * s, va_list * args)
Chris Luke27fe48f2016-04-28 13:44:38 -040049{
Swarup Nayak9ff647a2017-11-27 10:27:43 +053050 gre_tunnel_t *t = va_arg (*args, gre_tunnel_t *);
Chris Luke27fe48f2016-04-28 13:44:38 -040051
John Loa43ccae2018-02-13 17:15:23 -050052 s = format (s, "[%d] instance %d src %U dst %U fib-idx %d sw-if-idx %d ",
53 t->dev_instance, t->user_instance,
John Lo4478d8e2018-01-12 17:15:25 -050054 format_ip46_address, &t->tunnel_src, IP46_TYPE_ANY,
55 format_ip46_address, &t->tunnel_dst.fp_addr, IP46_TYPE_ANY,
56 t->outer_fib_index, t->sw_if_index);
57
Neale Ranns5f8f6172019-04-18 10:23:56 +000058 s = format (s, "payload %U ", format_gre_tunnel_type, t->type);
Neale Ranns59ff9182019-12-29 23:55:18 +000059 s = format (s, "%U ", format_tunnel_mode, t->mode);
John Loa43ccae2018-02-13 17:15:23 -050060
61 if (t->type == GRE_TUNNEL_TYPE_ERSPAN)
62 s = format (s, "session %d ", t->session_id);
63
64 if (t->type != GRE_TUNNEL_TYPE_L3)
65 s = format (s, "l2-adj-idx %d ", t->l2_adj_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010066
Chris Luke27fe48f2016-04-28 13:44:38 -040067 return s;
68}
69
Neale Ranns0bfe5d82016-08-25 15:29:12 +010070static gre_tunnel_t *
Neale Ranns5a8844b2019-04-16 07:15:35 +000071gre_tunnel_db_find (const vnet_gre_tunnel_add_del_args_t * a,
John Loa43ccae2018-02-13 17:15:23 -050072 u32 outer_fib_index, gre_tunnel_key_t * key)
Neale Ranns0bfe5d82016-08-25 15:29:12 +010073{
Swarup Nayak9ff647a2017-11-27 10:27:43 +053074 gre_main_t *gm = &gre_main;
75 uword *p;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010076
John Loa43ccae2018-02-13 17:15:23 -050077 if (!a->is_ipv6)
Ciara Loftus7eac9162016-09-30 15:47:03 +010078 {
John Loa43ccae2018-02-13 17:15:23 -050079 gre_mk_key4 (a->src.ip4, a->dst.ip4, outer_fib_index,
Neale Ranns4c16d802019-12-17 20:15:03 +000080 a->type, a->mode, a->session_id, &key->gtk_v4);
Neale Ranns33ce60d2017-12-14 08:51:32 -080081 p = hash_get_mem (gm->tunnel_by_key4, &key->gtk_v4);
Ciara Loftus7eac9162016-09-30 15:47:03 +010082 }
83 else
84 {
John Loa43ccae2018-02-13 17:15:23 -050085 gre_mk_key6 (&a->src.ip6, &a->dst.ip6, outer_fib_index,
Neale Ranns4c16d802019-12-17 20:15:03 +000086 a->type, a->mode, a->session_id, &key->gtk_v6);
Neale Ranns33ce60d2017-12-14 08:51:32 -080087 p = hash_get_mem (gm->tunnel_by_key6, &key->gtk_v6);
Ciara Loftus7eac9162016-09-30 15:47:03 +010088 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +010089
90 if (NULL == p)
91 return (NULL);
92
93 return (pool_elt_at_index (gm->tunnels, p[0]));
94}
95
96static void
Neale Ranns33ce60d2017-12-14 08:51:32 -080097gre_tunnel_db_add (gre_tunnel_t * t, gre_tunnel_key_t * key)
Neale Ranns0bfe5d82016-08-25 15:29:12 +010098{
Swarup Nayak9ff647a2017-11-27 10:27:43 +053099 gre_main_t *gm = &gre_main;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100100
Neale Ranns33ce60d2017-12-14 08:51:32 -0800101 if (t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6)
Ciara Loftus7eac9162016-09-30 15:47:03 +0100102 {
Neale Ranns4c16d802019-12-17 20:15:03 +0000103 hash_set_mem_alloc (&gm->tunnel_by_key6, &key->gtk_v6, t->dev_instance);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100104 }
105 else
106 {
Neale Ranns4c16d802019-12-17 20:15:03 +0000107 hash_set_mem_alloc (&gm->tunnel_by_key4, &key->gtk_v4, t->dev_instance);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100108 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100109}
110
111static void
Neale Ranns4c16d802019-12-17 20:15:03 +0000112gre_tunnel_db_remove (gre_tunnel_t * t, gre_tunnel_key_t * key)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100113{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530114 gre_main_t *gm = &gre_main;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100115
Neale Ranns33ce60d2017-12-14 08:51:32 -0800116 if (t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6)
Ciara Loftus7eac9162016-09-30 15:47:03 +0100117 {
Neale Ranns4c16d802019-12-17 20:15:03 +0000118 hash_unset_mem_free (&gm->tunnel_by_key6, &key->gtk_v6);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100119 }
120 else
121 {
Neale Ranns4c16d802019-12-17 20:15:03 +0000122 hash_unset_mem_free (&gm->tunnel_by_key4, &key->gtk_v4);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100123 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100124}
125
Neale Ranns5e575b12016-10-03 09:40:25 +0100126/**
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100127 * gre_tunnel_stack
128 *
129 * 'stack' (resolve the recursion for) the tunnel's midchain adjacency
130 */
Neale Ranns5e575b12016-10-03 09:40:25 +0100131void
Neale Rannsb80c5362016-10-08 13:03:40 +0100132gre_tunnel_stack (adj_index_t ai)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100133{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530134 gre_main_t *gm = &gre_main;
135 ip_adjacency_t *adj;
136 gre_tunnel_t *gt;
137 u32 sw_if_index;
Neale Rannsb80c5362016-10-08 13:03:40 +0100138
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530139 adj = adj_get (ai);
140 sw_if_index = adj->rewrite_header.sw_if_index;
Neale Rannsb80c5362016-10-08 13:03:40 +0100141
Eyal Baricd307742018-07-22 12:45:15 +0300142 if ((vec_len (gm->tunnel_index_by_sw_if_index) <= sw_if_index) ||
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530143 (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index]))
144 return;
Neale Rannsb80c5362016-10-08 13:03:40 +0100145
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530146 gt = pool_elt_at_index (gm->tunnels,
147 gm->tunnel_index_by_sw_if_index[sw_if_index]);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100148
John Loa43ccae2018-02-13 17:15:23 -0500149 if ((vnet_hw_interface_get_flags (vnet_get_main (), gt->hw_if_index) &
150 VNET_HW_INTERFACE_FLAG_LINK_UP) == 0)
Neale Rannsb80c5362016-10-08 13:03:40 +0100151 {
Neale Ranns4c3ba812019-03-26 07:02:58 +0000152 adj_midchain_delegate_unstack (ai);
Neale Rannsb80c5362016-10-08 13:03:40 +0100153 }
Neale Ranns521a8d72018-12-06 13:46:49 +0000154 else
John Loa43ccae2018-02-13 17:15:23 -0500155 {
Neale Ranns4c3ba812019-03-26 07:02:58 +0000156 adj_midchain_delegate_stack (ai, gt->outer_fib_index, &gt->tunnel_dst);
John Loa43ccae2018-02-13 17:15:23 -0500157 }
Neale Rannsb80c5362016-10-08 13:03:40 +0100158}
159
160/**
161 * @brief Call back when restacking all adjacencies on a GRE interface
162 */
163static adj_walk_rc_t
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530164gre_adj_walk_cb (adj_index_t ai, void *ctx)
Neale Rannsb80c5362016-10-08 13:03:40 +0100165{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530166 gre_tunnel_stack (ai);
Neale Rannsb80c5362016-10-08 13:03:40 +0100167
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530168 return (ADJ_WALK_RC_CONTINUE);
Neale Rannsb80c5362016-10-08 13:03:40 +0100169}
170
171static void
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530172gre_tunnel_restack (gre_tunnel_t * gt)
Neale Rannsb80c5362016-10-08 13:03:40 +0100173{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530174 fib_protocol_t proto;
Neale Rannsb80c5362016-10-08 13:03:40 +0100175
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530176 /*
177 * walk all the adjacencies on th GRE interface and restack them
178 */
179 FOR_EACH_FIB_IP_PROTOCOL (proto)
180 {
181 adj_nbr_walk (gt->sw_if_index, proto, gre_adj_walk_cb, NULL);
182 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100183}
184
Neale Ranns4c16d802019-12-17 20:15:03 +0000185static void
186gre_nhrp_mk_key (const gre_tunnel_t * t,
187 const nhrp_entry_t * ne, gre_tunnel_key_t * key)
188{
189 const fib_prefix_t *nh;
190
191 nh = nhrp_entry_get_nh (ne);
192
193 /* construct the key using mode P2P so it can be found in the DP */
194 if (FIB_PROTOCOL_IP4 == nh->fp_proto)
195 gre_mk_key4 (t->tunnel_src.ip4,
196 nh->fp_addr.ip4,
197 nhrp_entry_get_fib_index (ne),
Neale Ranns59ff9182019-12-29 23:55:18 +0000198 t->type, TUNNEL_MODE_P2P, 0, &key->gtk_v4);
Neale Ranns4c16d802019-12-17 20:15:03 +0000199 else
200 gre_mk_key6 (&t->tunnel_src.ip6,
201 &nh->fp_addr.ip6,
202 nhrp_entry_get_fib_index (ne),
Neale Ranns59ff9182019-12-29 23:55:18 +0000203 t->type, TUNNEL_MODE_P2P, 0, &key->gtk_v6);
Neale Ranns4c16d802019-12-17 20:15:03 +0000204}
205
Neale Ranns14053c92019-12-29 23:55:18 +0000206/**
207 * An NHRP entry has been added
208 */
Neale Ranns4c16d802019-12-17 20:15:03 +0000209static void
210gre_nhrp_entry_added (const nhrp_entry_t * ne)
211{
212 gre_main_t *gm = &gre_main;
Neale Ranns14053c92019-12-29 23:55:18 +0000213 const ip46_address_t *nh;
Neale Ranns4c16d802019-12-17 20:15:03 +0000214 gre_tunnel_key_t key;
215 gre_tunnel_t *t;
216 u32 sw_if_index;
217 u32 t_idx;
218
219 sw_if_index = nhrp_entry_get_sw_if_index (ne);
220 if (vec_len (gm->tunnel_index_by_sw_if_index) < sw_if_index)
221 return;
222
223 t_idx = gm->tunnel_index_by_sw_if_index[sw_if_index];
224
225 if (INDEX_INVALID == t_idx)
226 return;
227
Neale Ranns14053c92019-12-29 23:55:18 +0000228 /* entry has been added on an interface for which there is a GRE tunnel */
Neale Ranns4c16d802019-12-17 20:15:03 +0000229 t = pool_elt_at_index (gm->tunnels, t_idx);
230
Neale Ranns14053c92019-12-29 23:55:18 +0000231 if (t->mode != TUNNEL_MODE_MP)
232 return;
233
234 /* the next-hop (underlay) of the NHRP entry will form part of the key for
235 * ingress lookup to match packets to this interface */
Neale Ranns4c16d802019-12-17 20:15:03 +0000236 gre_nhrp_mk_key (t, ne, &key);
237 gre_tunnel_db_add (t, &key);
Neale Ranns14053c92019-12-29 23:55:18 +0000238
239 /* update the rewrites for each of the adjacencies for this peer (overlay)
240 * using the next-hop (underlay) */
241 mgre_walk_ctx_t ctx = {
242 .t = t,
243 .ne = ne
244 };
245 nh = nhrp_entry_get_peer (ne);
246 adj_nbr_walk_nh (nhrp_entry_get_sw_if_index (ne),
247 (ip46_address_is_ip4 (nh) ?
248 FIB_PROTOCOL_IP4 :
249 FIB_PROTOCOL_IP6), nh, mgre_mk_complete_walk, &ctx);
Neale Ranns4c16d802019-12-17 20:15:03 +0000250}
251
252static void
253gre_nhrp_entry_deleted (const nhrp_entry_t * ne)
254{
255 gre_main_t *gm = &gre_main;
Neale Ranns14053c92019-12-29 23:55:18 +0000256 const ip46_address_t *nh;
Neale Ranns4c16d802019-12-17 20:15:03 +0000257 gre_tunnel_key_t key;
258 gre_tunnel_t *t;
259 u32 sw_if_index;
260 u32 t_idx;
261
262 sw_if_index = nhrp_entry_get_sw_if_index (ne);
263 if (vec_len (gm->tunnel_index_by_sw_if_index) < sw_if_index)
264 return;
265
266 t_idx = gm->tunnel_index_by_sw_if_index[sw_if_index];
267
268 if (INDEX_INVALID == t_idx)
269 return;
270
271 t = pool_elt_at_index (gm->tunnels, t_idx);
272
Neale Ranns14053c92019-12-29 23:55:18 +0000273 /* remove the next-hop as an ingress lookup key */
Neale Ranns4c16d802019-12-17 20:15:03 +0000274 gre_nhrp_mk_key (t, ne, &key);
275 gre_tunnel_db_remove (t, &key);
Neale Ranns14053c92019-12-29 23:55:18 +0000276
277 nh = nhrp_entry_get_peer (ne);
278
279 /* make all the adjacencies incomplete */
280 adj_nbr_walk_nh (nhrp_entry_get_sw_if_index (ne),
281 (ip46_address_is_ip4 (nh) ?
282 FIB_PROTOCOL_IP4 :
283 FIB_PROTOCOL_IP6), nh, mgre_mk_incomplete_walk, t);
Neale Ranns4c16d802019-12-17 20:15:03 +0000284}
285
286static walk_rc_t
287gre_tunnel_delete_nhrp_walk (index_t nei, void *ctx)
288{
289 gre_tunnel_t *t = ctx;
290 gre_tunnel_key_t key;
291
292 gre_nhrp_mk_key (t, nhrp_entry_get (nei), &key);
293 gre_tunnel_db_remove (t, &key);
294
295 return (WALK_CONTINUE);
296}
297
298static walk_rc_t
299gre_tunnel_add_nhrp_walk (index_t nei, void *ctx)
300{
301 gre_tunnel_t *t = ctx;
302 gre_tunnel_key_t key;
303
304 gre_nhrp_mk_key (t, nhrp_entry_get (nei), &key);
305 gre_tunnel_db_add (t, &key);
306
307 return (WALK_CONTINUE);
308}
309
Ciara Loftus7eac9162016-09-30 15:47:03 +0100310static int
Neale Ranns5a8844b2019-04-16 07:15:35 +0000311vnet_gre_tunnel_add (vnet_gre_tunnel_add_del_args_t * a,
John Loa43ccae2018-02-13 17:15:23 -0500312 u32 outer_fib_index, u32 * sw_if_indexp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700313{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530314 gre_main_t *gm = &gre_main;
315 vnet_main_t *vnm = gm->vnet_main;
316 ip4_main_t *im4 = &ip4_main;
317 ip6_main_t *im6 = &ip6_main;
318 gre_tunnel_t *t;
319 vnet_hw_interface_t *hi;
Chris Luke27fe48f2016-04-28 13:44:38 -0400320 u32 hw_if_index, sw_if_index;
David Hothama8cd3092016-09-19 09:55:07 -0700321 clib_error_t *error;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100322 u8 is_ipv6 = a->is_ipv6;
Neale Ranns33ce60d2017-12-14 08:51:32 -0800323 gre_tunnel_key_t key;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324
John Loa43ccae2018-02-13 17:15:23 -0500325 t = gre_tunnel_db_find (a, outer_fib_index, &key);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100326 if (NULL != t)
John Loa43ccae2018-02-13 17:15:23 -0500327 return VNET_API_ERROR_IF_ALREADY_EXISTS;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700328
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100329 pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400330 clib_memset (t, 0, sizeof (*t));
John Loa43ccae2018-02-13 17:15:23 -0500331
332 /* Reconcile the real dev_instance and a possible requested instance */
333 u32 t_idx = t - gm->tunnels; /* tunnel index (or instance) */
334 u32 u_idx = a->instance; /* user specified instance */
335 if (u_idx == ~0)
336 u_idx = t_idx;
337 if (hash_get (gm->instance_used, u_idx))
338 {
339 pool_put (gm->tunnels, t);
340 return VNET_API_ERROR_INSTANCE_IN_USE;
341 }
342 hash_set (gm->instance_used, u_idx, 1);
343
344 t->dev_instance = t_idx; /* actual */
345 t->user_instance = u_idx; /* name */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346
Neale Ranns5a8844b2019-04-16 07:15:35 +0000347 t->type = a->type;
Neale Ranns5f8f6172019-04-18 10:23:56 +0000348 t->mode = a->mode;
John Loa43ccae2018-02-13 17:15:23 -0500349 if (t->type == GRE_TUNNEL_TYPE_ERSPAN)
350 t->session_id = a->session_id;
Neale Ranns177bbdc2016-11-15 09:46:51 +0000351
John Loa43ccae2018-02-13 17:15:23 -0500352 if (t->type == GRE_TUNNEL_TYPE_L3)
Neale Ranns5f8f6172019-04-18 10:23:56 +0000353 {
Neale Ranns59ff9182019-12-29 23:55:18 +0000354 if (t->mode == TUNNEL_MODE_P2P)
Neale Ranns5f8f6172019-04-18 10:23:56 +0000355 hw_if_index =
356 vnet_register_interface (vnm, gre_device_class.index, t_idx,
357 gre_hw_interface_class.index, t_idx);
358 else
359 hw_if_index =
360 vnet_register_interface (vnm, gre_device_class.index, t_idx,
361 mgre_hw_interface_class.index, t_idx);
362 }
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530363 else
364 {
John Loa43ccae2018-02-13 17:15:23 -0500365 /* Default MAC address (d00b:eed0:0000 + sw_if_index) */
John Lo25d417f2018-02-15 15:47:53 -0500366 u8 address[6] =
367 { 0xd0, 0x0b, 0xee, 0xd0, (u8) (t_idx >> 8), (u8) t_idx };
368 error =
369 ethernet_register_interface (vnm, gre_device_class.index, t_idx,
370 address, &hw_if_index, 0);
John Loa43ccae2018-02-13 17:15:23 -0500371 if (error)
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530372 {
John Loa43ccae2018-02-13 17:15:23 -0500373 clib_error_report (error);
374 return VNET_API_ERROR_INVALID_REGISTRATION;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530375 }
Chris Luke27fe48f2016-04-28 13:44:38 -0400376 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700377
John Loa43ccae2018-02-13 17:15:23 -0500378 /* Set GRE tunnel interface output node (not used for L3 payload) */
379 vnet_set_interface_output_node (vnm, hw_if_index, gre_encap_node.index);
380
381 hi = vnet_get_hw_interface (vnm, hw_if_index);
382 sw_if_index = hi->sw_if_index;
383
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100384 t->hw_if_index = hw_if_index;
385 t->outer_fib_index = outer_fib_index;
386 t->sw_if_index = sw_if_index;
Neale Rannsad422ed2016-11-02 14:20:04 +0000387 t->l2_adj_index = ADJ_INDEX_INVALID;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100389 vec_validate_init_empty (gm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
John Loa43ccae2018-02-13 17:15:23 -0500390 gm->tunnel_index_by_sw_if_index[sw_if_index] = t_idx;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391
Ciara Loftus7eac9162016-09-30 15:47:03 +0100392 if (!is_ipv6)
393 {
394 vec_validate (im4->fib_index_by_sw_if_index, sw_if_index);
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530395 hi->min_packet_bytes =
396 64 + sizeof (gre_header_t) + sizeof (ip4_header_t);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100397 }
398 else
399 {
400 vec_validate (im6->fib_index_by_sw_if_index, sw_if_index);
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530401 hi->min_packet_bytes =
402 64 + sizeof (gre_header_t) + sizeof (ip6_header_t);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100403 }
Chris Luke71649002016-05-06 11:51:54 -0400404
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100405 /* Standard default gre MTU. */
Ole Troand7231612018-06-07 10:17:57 +0200406 vnet_sw_interface_set_mtu (vnm, sw_if_index, 9000);
Damjan Marionfe7d4a22018-04-13 19:43:39 +0200407
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100408 /*
409 * source the FIB entry for the tunnel's destination
410 * and become a child thereof. The tunnel will then get poked
411 * when the forwarding for the entry updates, and the tunnel can
412 * re-stack accordingly
413 */
Ciara Loftus7eac9162016-09-30 15:47:03 +0100414
415 clib_memcpy (&t->tunnel_src, &a->src, sizeof (t->tunnel_src));
416 t->tunnel_dst.fp_len = !is_ipv6 ? 32 : 128;
417 t->tunnel_dst.fp_proto = !is_ipv6 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
418 t->tunnel_dst.fp_addr = a->dst;
419
Neale Ranns33ce60d2017-12-14 08:51:32 -0800420 gre_tunnel_db_add (t, &key);
Neale Ranns4c16d802019-12-17 20:15:03 +0000421
Neale Ranns59ff9182019-12-29 23:55:18 +0000422 if (t->mode == TUNNEL_MODE_MP)
Neale Ranns4c16d802019-12-17 20:15:03 +0000423 nhrp_walk_itf (t->sw_if_index, gre_tunnel_add_nhrp_walk, t);
424
John Loa43ccae2018-02-13 17:15:23 -0500425 if (t->type == GRE_TUNNEL_TYPE_ERSPAN)
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530426 {
John Loa43ccae2018-02-13 17:15:23 -0500427 gre_sn_key_t skey;
428 gre_sn_t *gre_sn;
429
430 gre_mk_sn_key (t, &skey);
431 gre_sn = (gre_sn_t *) hash_get_mem (gm->seq_num_by_key, &skey);
432 if (gre_sn != NULL)
433 {
434 gre_sn->ref_count++;
435 t->gre_sn = gre_sn;
436 }
437 else
438 {
439 gre_sn = clib_mem_alloc (sizeof (gre_sn_t));
440 gre_sn->seq_num = 0;
441 gre_sn->ref_count = 1;
442 t->gre_sn = gre_sn;
443 hash_set_mem_alloc (&gm->seq_num_by_key, &skey, (uword) gre_sn);
444 }
445 }
446
John Loa43ccae2018-02-13 17:15:23 -0500447 if (t->type != GRE_TUNNEL_TYPE_L3)
448 {
449 t->l2_adj_index = adj_nbr_add_or_lock
450 (t->tunnel_dst.fp_proto, VNET_LINK_ETHERNET, &zero_addr, sw_if_index);
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530451 gre_update_adj (vnm, t->sw_if_index, t->l2_adj_index);
452 }
Chris Luke27fe48f2016-04-28 13:44:38 -0400453
454 if (sw_if_indexp)
455 *sw_if_indexp = sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700456
Jakub Grajciarf03beca2019-05-24 08:25:03 +0200457 /* register gre46-input nodes */
458 ip4_register_protocol (IP_PROTOCOL_GRE, gre4_input_node.index);
459 ip6_register_protocol (IP_PROTOCOL_GRE, gre6_input_node.index);
460
Ed Warnickecb9cada2015-12-08 15:45:58 -0700461 return 0;
462}
463
Ciara Loftus7eac9162016-09-30 15:47:03 +0100464static int
Neale Ranns5a8844b2019-04-16 07:15:35 +0000465vnet_gre_tunnel_delete (vnet_gre_tunnel_add_del_args_t * a,
John Loa43ccae2018-02-13 17:15:23 -0500466 u32 outer_fib_index, u32 * sw_if_indexp)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100467{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530468 gre_main_t *gm = &gre_main;
469 vnet_main_t *vnm = gm->vnet_main;
470 gre_tunnel_t *t;
Neale Ranns33ce60d2017-12-14 08:51:32 -0800471 gre_tunnel_key_t key;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100472 u32 sw_if_index;
John Lo101e0052018-01-06 00:22:54 -0500473
John Loa43ccae2018-02-13 17:15:23 -0500474 t = gre_tunnel_db_find (a, outer_fib_index, &key);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100475 if (NULL == t)
476 return VNET_API_ERROR_NO_SUCH_ENTRY;
477
Neale Ranns59ff9182019-12-29 23:55:18 +0000478 if (t->mode == TUNNEL_MODE_MP)
Neale Ranns4c16d802019-12-17 20:15:03 +0000479 nhrp_walk_itf (t->sw_if_index, gre_tunnel_delete_nhrp_walk, t);
480
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100481 sw_if_index = t->sw_if_index;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530482 vnet_sw_interface_set_flags (vnm, sw_if_index, 0 /* down */ );
John Loa43ccae2018-02-13 17:15:23 -0500483
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100484 /* make sure tunnel is removed from l2 bd or xconnect */
Neale Rannsb4743802018-09-05 09:13:57 -0700485 set_int_l2_mode (gm->vlib_main, vnm, MODE_L3, sw_if_index, 0,
486 L2_BD_PORT_TYPE_NORMAL, 0, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100487 gm->tunnel_index_by_sw_if_index[sw_if_index] = ~0;
Neale Ranns177bbdc2016-11-15 09:46:51 +0000488
John Loa43ccae2018-02-13 17:15:23 -0500489 if (t->type == GRE_TUNNEL_TYPE_L3)
490 vnet_delete_hw_interface (vnm, t->hw_if_index);
491 else
492 ethernet_delete_interface (vnm, t->hw_if_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100493
Neale Rannsad422ed2016-11-02 14:20:04 +0000494 if (t->l2_adj_index != ADJ_INDEX_INVALID)
Neale Ranns4c3ba812019-03-26 07:02:58 +0000495 {
496 adj_midchain_delegate_unstack (t->l2_adj_index);
497 adj_unlock (t->l2_adj_index);
498 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100499
John Loa43ccae2018-02-13 17:15:23 -0500500 ASSERT ((t->type != GRE_TUNNEL_TYPE_ERSPAN) || (t->gre_sn != NULL));
501 if ((t->type == GRE_TUNNEL_TYPE_ERSPAN) && (t->gre_sn->ref_count-- == 1))
502 {
503 gre_sn_key_t skey;
504 gre_mk_sn_key (t, &skey);
505 hash_unset_mem_free (&gm->seq_num_by_key, &skey);
506 clib_mem_free (t->gre_sn);
507 }
508
509 hash_unset (gm->instance_used, t->user_instance);
Neale Ranns4c16d802019-12-17 20:15:03 +0000510 gre_tunnel_db_remove (t, &key);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100511 pool_put (gm->tunnels, t);
512
513 if (sw_if_indexp)
514 *sw_if_indexp = sw_if_index;
515
516 return 0;
517}
518
519int
Neale Ranns5a8844b2019-04-16 07:15:35 +0000520vnet_gre_tunnel_add_del (vnet_gre_tunnel_add_del_args_t * a,
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530521 u32 * sw_if_indexp)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100522{
John Loa43ccae2018-02-13 17:15:23 -0500523 u32 outer_fib_index;
524
525 if (!a->is_ipv6)
Neale Ranns5f8f6172019-04-18 10:23:56 +0000526 outer_fib_index = ip4_fib_index_from_table_id (a->outer_table_id);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100527 else
Neale Ranns5f8f6172019-04-18 10:23:56 +0000528 outer_fib_index = ip6_fib_index_from_table_id (a->outer_table_id);
John Loa43ccae2018-02-13 17:15:23 -0500529
530 if (~0 == outer_fib_index)
531 return VNET_API_ERROR_NO_SUCH_FIB;
532
533 if (a->session_id > GTK_SESSION_ID_MAX)
534 return VNET_API_ERROR_INVALID_SESSION_ID;
535
Neale Ranns59ff9182019-12-29 23:55:18 +0000536 if (a->mode == TUNNEL_MODE_MP && !ip46_address_is_zero (&a->dst))
Neale Ranns4c16d802019-12-17 20:15:03 +0000537 return (VNET_API_ERROR_INVALID_DST_ADDRESS);
538
John Loa43ccae2018-02-13 17:15:23 -0500539 if (a->is_add)
540 return (vnet_gre_tunnel_add (a, outer_fib_index, sw_if_indexp));
541 else
542 return (vnet_gre_tunnel_delete (a, outer_fib_index, sw_if_indexp));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100543}
544
Neale Rannsb80c5362016-10-08 13:03:40 +0100545clib_error_t *
546gre_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100547{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530548 gre_main_t *gm = &gre_main;
549 vnet_hw_interface_t *hi;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100550 gre_tunnel_t *t;
Neale Rannsb80c5362016-10-08 13:03:40 +0100551 u32 ti;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100552
Neale Rannsb80c5362016-10-08 13:03:40 +0100553 hi = vnet_get_hw_interface (vnm, hw_if_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100554
Neale Rannsb80c5362016-10-08 13:03:40 +0100555 if (NULL == gm->tunnel_index_by_sw_if_index ||
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530556 hi->sw_if_index >= vec_len (gm->tunnel_index_by_sw_if_index))
557 return (NULL);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100558
Neale Rannsb80c5362016-10-08 13:03:40 +0100559 ti = gm->tunnel_index_by_sw_if_index[hi->sw_if_index];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100560
Neale Rannsb80c5362016-10-08 13:03:40 +0100561 if (~0 == ti)
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530562 /* not one of ours */
563 return (NULL);
Neale Rannsb80c5362016-10-08 13:03:40 +0100564
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530565 t = pool_elt_at_index (gm->tunnels, ti);
Neale Rannsb80c5362016-10-08 13:03:40 +0100566
567 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530568 vnet_hw_interface_set_flags (vnm, hw_if_index,
569 VNET_HW_INTERFACE_FLAG_LINK_UP);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100570 else
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530571 vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100572
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530573 gre_tunnel_restack (t);
Neale Rannsb80c5362016-10-08 13:03:40 +0100574
575 return /* no error */ 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100576}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700577
578static clib_error_t *
579create_gre_tunnel_command_fn (vlib_main_t * vm,
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530580 unformat_input_t * input,
581 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700582{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530583 unformat_input_t _line_input, *line_input = &_line_input;
Neale Ranns5a8844b2019-04-16 07:15:35 +0000584 vnet_gre_tunnel_add_del_args_t _a, *a = &_a;
Neale Ranns5f8f6172019-04-18 10:23:56 +0000585 ip46_address_t src = ip46_address_initializer, dst =
586 ip46_address_initializer;
John Loa43ccae2018-02-13 17:15:23 -0500587 u32 instance = ~0;
Neale Ranns5f8f6172019-04-18 10:23:56 +0000588 u32 outer_table_id = 0;
John Loa43ccae2018-02-13 17:15:23 -0500589 gre_tunnel_type_t t_type = GRE_TUNNEL_TYPE_L3;
Neale Ranns59ff9182019-12-29 23:55:18 +0000590 tunnel_mode_t t_mode = TUNNEL_MODE_P2P;
John Loa43ccae2018-02-13 17:15:23 -0500591 u32 session_id = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700592 int rv;
Chris Luke27fe48f2016-04-28 13:44:38 -0400593 u8 is_add = 1;
Pierre Pfister78ea9c22016-05-23 12:51:54 +0100594 u32 sw_if_index;
Billy McFalla9a20e72017-02-15 11:39:12 -0500595 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700596
597 /* Get a line of input. */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530598 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700599 return 0;
600
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530601 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
602 {
603 if (unformat (line_input, "del"))
604 is_add = 0;
John Loa43ccae2018-02-13 17:15:23 -0500605 else if (unformat (line_input, "instance %d", &instance))
606 ;
Neale Ranns5f8f6172019-04-18 10:23:56 +0000607 else if (unformat (line_input, "src %U", unformat_ip46_address, &src))
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530608 ;
Neale Ranns5f8f6172019-04-18 10:23:56 +0000609 else if (unformat (line_input, "dst %U", unformat_ip46_address, &dst))
610 ;
611 else if (unformat (line_input, "outer-table-id %d", &outer_table_id))
612 ;
613 else if (unformat (line_input, "multipoint"))
Neale Ranns59ff9182019-12-29 23:55:18 +0000614 t_mode = TUNNEL_MODE_MP;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530615 else if (unformat (line_input, "teb"))
John Loa43ccae2018-02-13 17:15:23 -0500616 t_type = GRE_TUNNEL_TYPE_TEB;
617 else if (unformat (line_input, "erspan %d", &session_id))
618 t_type = GRE_TUNNEL_TYPE_ERSPAN;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530619 else
620 {
621 error = clib_error_return (0, "unknown input `%U'",
622 format_unformat_error, line_input);
623 goto done;
624 }
625 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700626
Neale Ranns5f8f6172019-04-18 10:23:56 +0000627 if (ip46_address_is_equal (&src, &dst))
Billy McFalla9a20e72017-02-15 11:39:12 -0500628 {
629 error = clib_error_return (0, "src and dst are identical");
630 goto done;
631 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700632
Neale Ranns59ff9182019-12-29 23:55:18 +0000633 if (t_mode != TUNNEL_MODE_MP && ip46_address_is_zero (&dst))
Ciara Loftus7eac9162016-09-30 15:47:03 +0100634 {
Neale Ranns5f8f6172019-04-18 10:23:56 +0000635 error = clib_error_return (0, "destination address not specified");
636 goto done;
637 }
638
639 if (ip46_address_is_zero (&src))
640 {
641 error = clib_error_return (0, "source address not specified");
642 goto done;
643 }
644
645 if (ip46_address_is_ip4 (&src) != ip46_address_is_ip4 (&dst))
646 {
647 error =
648 clib_error_return (0, "src and dst address must be the same AF");
Ciara Loftus7eac9162016-09-30 15:47:03 +0100649 goto done;
650 }
651
Dave Barachb7b92992018-10-17 10:38:51 -0400652 clib_memset (a, 0, sizeof (*a));
John Loa43ccae2018-02-13 17:15:23 -0500653 a->is_add = is_add;
Neale Ranns5f8f6172019-04-18 10:23:56 +0000654 a->outer_table_id = outer_table_id;
Neale Ranns5a8844b2019-04-16 07:15:35 +0000655 a->type = t_type;
Neale Ranns5f8f6172019-04-18 10:23:56 +0000656 a->mode = t_mode;
John Loa43ccae2018-02-13 17:15:23 -0500657 a->session_id = session_id;
Neale Ranns5f8f6172019-04-18 10:23:56 +0000658 a->is_ipv6 = !ip46_address_is_ip4 (&src);
John Loa43ccae2018-02-13 17:15:23 -0500659 a->instance = instance;
Neale Ranns5f8f6172019-04-18 10:23:56 +0000660 clib_memcpy (&a->src, &src, sizeof (a->src));
661 clib_memcpy (&a->dst, &dst, sizeof (a->dst));
Chris Luke27fe48f2016-04-28 13:44:38 -0400662
Neale Ranns5a8844b2019-04-16 07:15:35 +0000663 rv = vnet_gre_tunnel_add_del (a, &sw_if_index);
Chris Luke27fe48f2016-04-28 13:44:38 -0400664
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530665 switch (rv)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700666 {
667 case 0:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530668 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
669 vnet_get_main (), sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700670 break;
John Loa43ccae2018-02-13 17:15:23 -0500671 case VNET_API_ERROR_IF_ALREADY_EXISTS:
Billy McFalla9a20e72017-02-15 11:39:12 -0500672 error = clib_error_return (0, "GRE tunnel already exists...");
673 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700674 case VNET_API_ERROR_NO_SUCH_FIB:
Neale Ranns5f8f6172019-04-18 10:23:56 +0000675 error = clib_error_return (0, "outer table ID %d doesn't exist\n",
676 outer_table_id);
Billy McFalla9a20e72017-02-15 11:39:12 -0500677 goto done;
John Loa43ccae2018-02-13 17:15:23 -0500678 case VNET_API_ERROR_NO_SUCH_ENTRY:
679 error = clib_error_return (0, "GRE tunnel doesn't exist");
680 goto done;
681 case VNET_API_ERROR_INVALID_SESSION_ID:
682 error = clib_error_return (0, "session ID %d out of range\n",
683 session_id);
684 goto done;
685 case VNET_API_ERROR_INSTANCE_IN_USE:
686 error = clib_error_return (0, "Instance is in use");
687 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700688 default:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530689 error =
Neale Ranns5a8844b2019-04-16 07:15:35 +0000690 clib_error_return (0, "vnet_gre_tunnel_add_del returned %d", rv);
Billy McFalla9a20e72017-02-15 11:39:12 -0500691 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700692 }
693
Billy McFalla9a20e72017-02-15 11:39:12 -0500694done:
695 unformat_free (line_input);
696
697 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700698}
699
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530700/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700701VLIB_CLI_COMMAND (create_gre_tunnel_command, static) = {
702 .path = "create gre tunnel",
John Loa43ccae2018-02-13 17:15:23 -0500703 .short_help = "create gre tunnel src <addr> dst <addr> [instance <n>] "
704 "[outer-fib-id <fib>] [teb | erspan <session-id>] [del]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700705 .function = create_gre_tunnel_command_fn,
706};
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530707/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700708
Chris Luke27fe48f2016-04-28 13:44:38 -0400709static clib_error_t *
710show_gre_tunnel_command_fn (vlib_main_t * vm,
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530711 unformat_input_t * input,
712 vlib_cli_command_t * cmd)
Chris Luke27fe48f2016-04-28 13:44:38 -0400713{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530714 gre_main_t *gm = &gre_main;
715 gre_tunnel_t *t;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100716 u32 ti = ~0;
Chris Luke27fe48f2016-04-28 13:44:38 -0400717
718 if (pool_elts (gm->tunnels) == 0)
719 vlib_cli_output (vm, "No GRE tunnels configured...");
720
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100721 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
722 {
723 if (unformat (input, "%d", &ti))
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530724 ;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100725 else
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530726 break;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100727 }
728
729 if (~0 == ti)
730 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530731 /* *INDENT-OFF* */
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100732 pool_foreach (t, gm->tunnels,
733 ({
Neale Rannsb80c5362016-10-08 13:03:40 +0100734 vlib_cli_output (vm, "%U", format_gre_tunnel, t);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100735 }));
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530736 /* *INDENT-ON* */
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100737 }
738 else
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530739 {
740 t = pool_elt_at_index (gm->tunnels, ti);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100741
Neale Rannsb80c5362016-10-08 13:03:40 +0100742 vlib_cli_output (vm, "%U", format_gre_tunnel, t);
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530743 }
Chris Luke27fe48f2016-04-28 13:44:38 -0400744
745 return 0;
746}
747
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530748/* *INDENT-OFF* */
Chris Luke27fe48f2016-04-28 13:44:38 -0400749VLIB_CLI_COMMAND (show_gre_tunnel_command, static) = {
750 .path = "show gre tunnel",
751 .function = show_gre_tunnel_command_fn,
752};
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530753/* *INDENT-ON* */
Chris Luke27fe48f2016-04-28 13:44:38 -0400754
Neale Ranns4c16d802019-12-17 20:15:03 +0000755const static nhrp_vft_t gre_nhrp_vft = {
756 .nv_added = gre_nhrp_entry_added,
757 .nv_deleted = gre_nhrp_entry_deleted,
758};
759
Ed Warnickecb9cada2015-12-08 15:45:58 -0700760/* force inclusion from application's main.c */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530761clib_error_t *
762gre_interface_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700763{
Neale Ranns4c16d802019-12-17 20:15:03 +0000764 nhrp_register (&gre_nhrp_vft);
765
Neale Ranns4c3ba812019-03-26 07:02:58 +0000766 return (NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700767}
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530768
769VLIB_INIT_FUNCTION (gre_interface_init);
770
771/*
772 * fd.io coding-style-patch-verification: ON
773 *
774 * Local Variables:
775 * eval: (c-set-style "gnu")
776 * End:
777 */