blob: 3ea13e1676b0c699664da30fdfc14dfe8cd46e63 [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>
Ed Warnickecb9cada2015-12-08 15:45:58 -070019#include <vnet/gre/gre.h>
Chris Luke27fe48f2016-04-28 13:44:38 -040020#include <vnet/ip/format.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010021#include <vnet/fib/ip4_fib.h>
Ciara Loftus7eac9162016-09-30 15:47:03 +010022#include <vnet/fib/ip6_fib.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010023#include <vnet/adj/adj_midchain.h>
Neale Rannsb80c5362016-10-08 13:03:40 +010024#include <vnet/adj/adj_nbr.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010025#include <vnet/mpls/mpls.h>
Neale Ranns3b81a1e2018-09-06 09:50:26 -070026#include <vnet/l2/l2_input.h>
Neale Ranns03ce4622020-02-03 10:55:09 +000027#include <vnet/teib/teib.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070028
Neale Ranns5f8f6172019-04-18 10:23:56 +000029u8 *
30format_gre_tunnel_type (u8 * s, va_list * args)
31{
32 gre_tunnel_type_t type = va_arg (*args, int);
33
34 switch (type)
35 {
36#define _(n, v) case GRE_TUNNEL_TYPE_##n: \
37 s = format (s, "%s", v); \
38 break;
39 foreach_gre_tunnel_type
40#undef _
41 }
42
43 return (s);
44}
45
Neale Ranns0bfe5d82016-08-25 15:29:12 +010046static u8 *
47format_gre_tunnel (u8 * s, va_list * args)
Chris Luke27fe48f2016-04-28 13:44:38 -040048{
Swarup Nayak9ff647a2017-11-27 10:27:43 +053049 gre_tunnel_t *t = va_arg (*args, gre_tunnel_t *);
Chris Luke27fe48f2016-04-28 13:44:38 -040050
John Loa43ccae2018-02-13 17:15:23 -050051 s = format (s, "[%d] instance %d src %U dst %U fib-idx %d sw-if-idx %d ",
52 t->dev_instance, t->user_instance,
John Lo4478d8e2018-01-12 17:15:25 -050053 format_ip46_address, &t->tunnel_src, IP46_TYPE_ANY,
54 format_ip46_address, &t->tunnel_dst.fp_addr, IP46_TYPE_ANY,
55 t->outer_fib_index, t->sw_if_index);
56
Neale Ranns5f8f6172019-04-18 10:23:56 +000057 s = format (s, "payload %U ", format_gre_tunnel_type, t->type);
Neale Ranns59ff9182019-12-29 23:55:18 +000058 s = format (s, "%U ", format_tunnel_mode, t->mode);
John Loa43ccae2018-02-13 17:15:23 -050059
60 if (t->type == GRE_TUNNEL_TYPE_ERSPAN)
61 s = format (s, "session %d ", t->session_id);
62
63 if (t->type != GRE_TUNNEL_TYPE_L3)
64 s = format (s, "l2-adj-idx %d ", t->l2_adj_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010065
Chris Luke27fe48f2016-04-28 13:44:38 -040066 return s;
67}
68
Neale Ranns0bfe5d82016-08-25 15:29:12 +010069static gre_tunnel_t *
Neale Ranns5a8844b2019-04-16 07:15:35 +000070gre_tunnel_db_find (const vnet_gre_tunnel_add_del_args_t * a,
John Loa43ccae2018-02-13 17:15:23 -050071 u32 outer_fib_index, gre_tunnel_key_t * key)
Neale Ranns0bfe5d82016-08-25 15:29:12 +010072{
Swarup Nayak9ff647a2017-11-27 10:27:43 +053073 gre_main_t *gm = &gre_main;
74 uword *p;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010075
John Loa43ccae2018-02-13 17:15:23 -050076 if (!a->is_ipv6)
Ciara Loftus7eac9162016-09-30 15:47:03 +010077 {
John Loa43ccae2018-02-13 17:15:23 -050078 gre_mk_key4 (a->src.ip4, a->dst.ip4, outer_fib_index,
Neale Ranns4c16d802019-12-17 20:15:03 +000079 a->type, a->mode, a->session_id, &key->gtk_v4);
Neale Ranns33ce60d2017-12-14 08:51:32 -080080 p = hash_get_mem (gm->tunnel_by_key4, &key->gtk_v4);
Ciara Loftus7eac9162016-09-30 15:47:03 +010081 }
82 else
83 {
John Loa43ccae2018-02-13 17:15:23 -050084 gre_mk_key6 (&a->src.ip6, &a->dst.ip6, outer_fib_index,
Neale Ranns4c16d802019-12-17 20:15:03 +000085 a->type, a->mode, a->session_id, &key->gtk_v6);
Neale Ranns33ce60d2017-12-14 08:51:32 -080086 p = hash_get_mem (gm->tunnel_by_key6, &key->gtk_v6);
Ciara Loftus7eac9162016-09-30 15:47:03 +010087 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +010088
89 if (NULL == p)
90 return (NULL);
91
92 return (pool_elt_at_index (gm->tunnels, p[0]));
93}
94
95static void
Neale Ranns33ce60d2017-12-14 08:51:32 -080096gre_tunnel_db_add (gre_tunnel_t * t, gre_tunnel_key_t * key)
Neale Ranns0bfe5d82016-08-25 15:29:12 +010097{
Swarup Nayak9ff647a2017-11-27 10:27:43 +053098 gre_main_t *gm = &gre_main;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010099
Neale Ranns33ce60d2017-12-14 08:51:32 -0800100 if (t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6)
Ciara Loftus7eac9162016-09-30 15:47:03 +0100101 {
Neale Ranns4c16d802019-12-17 20:15:03 +0000102 hash_set_mem_alloc (&gm->tunnel_by_key6, &key->gtk_v6, t->dev_instance);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100103 }
104 else
105 {
Neale Ranns4c16d802019-12-17 20:15:03 +0000106 hash_set_mem_alloc (&gm->tunnel_by_key4, &key->gtk_v4, t->dev_instance);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100107 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100108}
109
110static void
Neale Ranns4c16d802019-12-17 20:15:03 +0000111gre_tunnel_db_remove (gre_tunnel_t * t, gre_tunnel_key_t * key)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100112{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530113 gre_main_t *gm = &gre_main;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100114
Neale Ranns33ce60d2017-12-14 08:51:32 -0800115 if (t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6)
Ciara Loftus7eac9162016-09-30 15:47:03 +0100116 {
Neale Ranns4c16d802019-12-17 20:15:03 +0000117 hash_unset_mem_free (&gm->tunnel_by_key6, &key->gtk_v6);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100118 }
119 else
120 {
Neale Ranns4c16d802019-12-17 20:15:03 +0000121 hash_unset_mem_free (&gm->tunnel_by_key4, &key->gtk_v4);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100122 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100123}
124
Neale Ranns5e575b12016-10-03 09:40:25 +0100125/**
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100126 * gre_tunnel_stack
127 *
128 * 'stack' (resolve the recursion for) the tunnel's midchain adjacency
129 */
Neale Ranns5e575b12016-10-03 09:40:25 +0100130void
Neale Rannsb80c5362016-10-08 13:03:40 +0100131gre_tunnel_stack (adj_index_t ai)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100132{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530133 gre_main_t *gm = &gre_main;
134 ip_adjacency_t *adj;
135 gre_tunnel_t *gt;
136 u32 sw_if_index;
Neale Rannsb80c5362016-10-08 13:03:40 +0100137
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530138 adj = adj_get (ai);
139 sw_if_index = adj->rewrite_header.sw_if_index;
Neale Rannsb80c5362016-10-08 13:03:40 +0100140
Eyal Baricd307742018-07-22 12:45:15 +0300141 if ((vec_len (gm->tunnel_index_by_sw_if_index) <= sw_if_index) ||
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530142 (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index]))
143 return;
Neale Rannsb80c5362016-10-08 13:03:40 +0100144
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530145 gt = pool_elt_at_index (gm->tunnels,
146 gm->tunnel_index_by_sw_if_index[sw_if_index]);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100147
John Loa43ccae2018-02-13 17:15:23 -0500148 if ((vnet_hw_interface_get_flags (vnet_get_main (), gt->hw_if_index) &
149 VNET_HW_INTERFACE_FLAG_LINK_UP) == 0)
Neale Rannsb80c5362016-10-08 13:03:40 +0100150 {
Neale Ranns4c3ba812019-03-26 07:02:58 +0000151 adj_midchain_delegate_unstack (ai);
Neale Rannsb80c5362016-10-08 13:03:40 +0100152 }
Neale Ranns521a8d72018-12-06 13:46:49 +0000153 else
John Loa43ccae2018-02-13 17:15:23 -0500154 {
Neale Ranns4c3ba812019-03-26 07:02:58 +0000155 adj_midchain_delegate_stack (ai, gt->outer_fib_index, &gt->tunnel_dst);
John Loa43ccae2018-02-13 17:15:23 -0500156 }
Neale Rannsb80c5362016-10-08 13:03:40 +0100157}
158
159/**
160 * @brief Call back when restacking all adjacencies on a GRE interface
161 */
162static adj_walk_rc_t
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530163gre_adj_walk_cb (adj_index_t ai, void *ctx)
Neale Rannsb80c5362016-10-08 13:03:40 +0100164{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530165 gre_tunnel_stack (ai);
Neale Rannsb80c5362016-10-08 13:03:40 +0100166
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530167 return (ADJ_WALK_RC_CONTINUE);
Neale Rannsb80c5362016-10-08 13:03:40 +0100168}
169
170static void
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530171gre_tunnel_restack (gre_tunnel_t * gt)
Neale Rannsb80c5362016-10-08 13:03:40 +0100172{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530173 fib_protocol_t proto;
Neale Rannsb80c5362016-10-08 13:03:40 +0100174
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530175 /*
176 * walk all the adjacencies on th GRE interface and restack them
177 */
178 FOR_EACH_FIB_IP_PROTOCOL (proto)
179 {
180 adj_nbr_walk (gt->sw_if_index, proto, gre_adj_walk_cb, NULL);
181 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100182}
183
Neale Ranns4c16d802019-12-17 20:15:03 +0000184static void
Neale Ranns03ce4622020-02-03 10:55:09 +0000185gre_teib_mk_key (const gre_tunnel_t * t,
186 const teib_entry_t * ne, gre_tunnel_key_t * key)
Neale Ranns4c16d802019-12-17 20:15:03 +0000187{
188 const fib_prefix_t *nh;
189
Neale Ranns03ce4622020-02-03 10:55:09 +0000190 nh = teib_entry_get_nh (ne);
Neale Ranns4c16d802019-12-17 20:15:03 +0000191
192 /* construct the key using mode P2P so it can be found in the DP */
193 if (FIB_PROTOCOL_IP4 == nh->fp_proto)
194 gre_mk_key4 (t->tunnel_src.ip4,
195 nh->fp_addr.ip4,
Neale Ranns03ce4622020-02-03 10:55:09 +0000196 teib_entry_get_fib_index (ne),
Neale Ranns59ff9182019-12-29 23:55:18 +0000197 t->type, TUNNEL_MODE_P2P, 0, &key->gtk_v4);
Neale Ranns4c16d802019-12-17 20:15:03 +0000198 else
199 gre_mk_key6 (&t->tunnel_src.ip6,
200 &nh->fp_addr.ip6,
Neale Ranns03ce4622020-02-03 10:55:09 +0000201 teib_entry_get_fib_index (ne),
Neale Ranns59ff9182019-12-29 23:55:18 +0000202 t->type, TUNNEL_MODE_P2P, 0, &key->gtk_v6);
Neale Ranns4c16d802019-12-17 20:15:03 +0000203}
204
Neale Ranns14053c92019-12-29 23:55:18 +0000205/**
206 * An NHRP entry has been added
207 */
Neale Ranns4c16d802019-12-17 20:15:03 +0000208static void
Neale Ranns03ce4622020-02-03 10:55:09 +0000209gre_teib_entry_added (const teib_entry_t * ne)
Neale Ranns4c16d802019-12-17 20:15:03 +0000210{
211 gre_main_t *gm = &gre_main;
Neale Rannse6b83052020-09-17 12:56:47 +0000212 const ip_address_t *nh;
Neale Ranns4c16d802019-12-17 20:15:03 +0000213 gre_tunnel_key_t key;
214 gre_tunnel_t *t;
215 u32 sw_if_index;
216 u32 t_idx;
217
Neale Ranns03ce4622020-02-03 10:55:09 +0000218 sw_if_index = teib_entry_get_sw_if_index (ne);
Neale Ranns4c16d802019-12-17 20:15:03 +0000219 if (vec_len (gm->tunnel_index_by_sw_if_index) < sw_if_index)
220 return;
221
222 t_idx = gm->tunnel_index_by_sw_if_index[sw_if_index];
223
224 if (INDEX_INVALID == t_idx)
225 return;
226
Neale Ranns14053c92019-12-29 23:55:18 +0000227 /* entry has been added on an interface for which there is a GRE tunnel */
Neale Ranns4c16d802019-12-17 20:15:03 +0000228 t = pool_elt_at_index (gm->tunnels, t_idx);
229
Neale Ranns14053c92019-12-29 23:55:18 +0000230 if (t->mode != TUNNEL_MODE_MP)
231 return;
232
233 /* the next-hop (underlay) of the NHRP entry will form part of the key for
234 * ingress lookup to match packets to this interface */
Neale Ranns03ce4622020-02-03 10:55:09 +0000235 gre_teib_mk_key (t, ne, &key);
Neale Ranns4c16d802019-12-17 20:15:03 +0000236 gre_tunnel_db_add (t, &key);
Neale Ranns14053c92019-12-29 23:55:18 +0000237
238 /* update the rewrites for each of the adjacencies for this peer (overlay)
239 * using the next-hop (underlay) */
240 mgre_walk_ctx_t ctx = {
241 .t = t,
242 .ne = ne
243 };
Neale Ranns03ce4622020-02-03 10:55:09 +0000244 nh = teib_entry_get_peer (ne);
245 adj_nbr_walk_nh (teib_entry_get_sw_if_index (ne),
Neale Rannse6b83052020-09-17 12:56:47 +0000246 (AF_IP4 == ip_addr_version (nh) ?
Neale Ranns14053c92019-12-29 23:55:18 +0000247 FIB_PROTOCOL_IP4 :
Neale Rannse6b83052020-09-17 12:56:47 +0000248 FIB_PROTOCOL_IP6),
249 &ip_addr_46 (nh), mgre_mk_complete_walk, &ctx);
Neale Ranns4c16d802019-12-17 20:15:03 +0000250}
251
252static void
Neale Ranns03ce4622020-02-03 10:55:09 +0000253gre_teib_entry_deleted (const teib_entry_t * ne)
Neale Ranns4c16d802019-12-17 20:15:03 +0000254{
255 gre_main_t *gm = &gre_main;
Neale Rannse6b83052020-09-17 12:56:47 +0000256 const ip_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
Neale Ranns03ce4622020-02-03 10:55:09 +0000262 sw_if_index = teib_entry_get_sw_if_index (ne);
Neale Ranns4c16d802019-12-17 20:15:03 +0000263 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 Ranns03ce4622020-02-03 10:55:09 +0000274 gre_teib_mk_key (t, ne, &key);
Neale Ranns4c16d802019-12-17 20:15:03 +0000275 gre_tunnel_db_remove (t, &key);
Neale Ranns14053c92019-12-29 23:55:18 +0000276
Neale Ranns03ce4622020-02-03 10:55:09 +0000277 nh = teib_entry_get_peer (ne);
Neale Ranns14053c92019-12-29 23:55:18 +0000278
279 /* make all the adjacencies incomplete */
Neale Ranns03ce4622020-02-03 10:55:09 +0000280 adj_nbr_walk_nh (teib_entry_get_sw_if_index (ne),
Neale Rannse6b83052020-09-17 12:56:47 +0000281 (AF_IP4 == ip_addr_version (nh) ?
Neale Ranns14053c92019-12-29 23:55:18 +0000282 FIB_PROTOCOL_IP4 :
Neale Rannse6b83052020-09-17 12:56:47 +0000283 FIB_PROTOCOL_IP6),
284 &ip_addr_46 (nh), mgre_mk_incomplete_walk, t);
Neale Ranns4c16d802019-12-17 20:15:03 +0000285}
286
287static walk_rc_t
Neale Ranns03ce4622020-02-03 10:55:09 +0000288gre_tunnel_delete_teib_walk (index_t nei, void *ctx)
Neale Ranns4c16d802019-12-17 20:15:03 +0000289{
290 gre_tunnel_t *t = ctx;
291 gre_tunnel_key_t key;
292
Neale Ranns03ce4622020-02-03 10:55:09 +0000293 gre_teib_mk_key (t, teib_entry_get (nei), &key);
Neale Ranns4c16d802019-12-17 20:15:03 +0000294 gre_tunnel_db_remove (t, &key);
295
296 return (WALK_CONTINUE);
297}
298
299static walk_rc_t
Neale Ranns03ce4622020-02-03 10:55:09 +0000300gre_tunnel_add_teib_walk (index_t nei, void *ctx)
Neale Ranns4c16d802019-12-17 20:15:03 +0000301{
302 gre_tunnel_t *t = ctx;
303 gre_tunnel_key_t key;
304
Neale Ranns03ce4622020-02-03 10:55:09 +0000305 gre_teib_mk_key (t, teib_entry_get (nei), &key);
Neale Ranns4c16d802019-12-17 20:15:03 +0000306 gre_tunnel_db_add (t, &key);
307
308 return (WALK_CONTINUE);
309}
310
Ciara Loftus7eac9162016-09-30 15:47:03 +0100311static int
Neale Ranns5a8844b2019-04-16 07:15:35 +0000312vnet_gre_tunnel_add (vnet_gre_tunnel_add_del_args_t * a,
John Loa43ccae2018-02-13 17:15:23 -0500313 u32 outer_fib_index, u32 * sw_if_indexp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530315 gre_main_t *gm = &gre_main;
316 vnet_main_t *vnm = gm->vnet_main;
317 ip4_main_t *im4 = &ip4_main;
318 ip6_main_t *im6 = &ip6_main;
319 gre_tunnel_t *t;
320 vnet_hw_interface_t *hi;
Chris Luke27fe48f2016-04-28 13:44:38 -0400321 u32 hw_if_index, sw_if_index;
David Hothama8cd3092016-09-19 09:55:07 -0700322 clib_error_t *error;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100323 u8 is_ipv6 = a->is_ipv6;
Neale Ranns33ce60d2017-12-14 08:51:32 -0800324 gre_tunnel_key_t key;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700325
John Loa43ccae2018-02-13 17:15:23 -0500326 t = gre_tunnel_db_find (a, outer_fib_index, &key);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100327 if (NULL != t)
John Loa43ccae2018-02-13 17:15:23 -0500328 return VNET_API_ERROR_IF_ALREADY_EXISTS;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100330 pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400331 clib_memset (t, 0, sizeof (*t));
John Loa43ccae2018-02-13 17:15:23 -0500332
333 /* Reconcile the real dev_instance and a possible requested instance */
334 u32 t_idx = t - gm->tunnels; /* tunnel index (or instance) */
335 u32 u_idx = a->instance; /* user specified instance */
336 if (u_idx == ~0)
337 u_idx = t_idx;
338 if (hash_get (gm->instance_used, u_idx))
339 {
340 pool_put (gm->tunnels, t);
341 return VNET_API_ERROR_INSTANCE_IN_USE;
342 }
343 hash_set (gm->instance_used, u_idx, 1);
344
345 t->dev_instance = t_idx; /* actual */
346 t->user_instance = u_idx; /* name */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700347
Neale Ranns5a8844b2019-04-16 07:15:35 +0000348 t->type = a->type;
Neale Ranns5f8f6172019-04-18 10:23:56 +0000349 t->mode = a->mode;
Neale Rannse5b94dd2019-12-31 05:13:14 +0000350 t->flags = a->flags;
John Loa43ccae2018-02-13 17:15:23 -0500351 if (t->type == GRE_TUNNEL_TYPE_ERSPAN)
352 t->session_id = a->session_id;
Neale Ranns177bbdc2016-11-15 09:46:51 +0000353
John Loa43ccae2018-02-13 17:15:23 -0500354 if (t->type == GRE_TUNNEL_TYPE_L3)
Neale Ranns5f8f6172019-04-18 10:23:56 +0000355 {
Neale Ranns59ff9182019-12-29 23:55:18 +0000356 if (t->mode == TUNNEL_MODE_P2P)
Neale Ranns5f8f6172019-04-18 10:23:56 +0000357 hw_if_index =
358 vnet_register_interface (vnm, gre_device_class.index, t_idx,
359 gre_hw_interface_class.index, t_idx);
360 else
361 hw_if_index =
362 vnet_register_interface (vnm, gre_device_class.index, t_idx,
363 mgre_hw_interface_class.index, t_idx);
364 }
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530365 else
366 {
John Loa43ccae2018-02-13 17:15:23 -0500367 /* Default MAC address (d00b:eed0:0000 + sw_if_index) */
John Lo25d417f2018-02-15 15:47:53 -0500368 u8 address[6] =
369 { 0xd0, 0x0b, 0xee, 0xd0, (u8) (t_idx >> 8), (u8) t_idx };
370 error =
371 ethernet_register_interface (vnm, gre_device_class.index, t_idx,
372 address, &hw_if_index, 0);
John Loa43ccae2018-02-13 17:15:23 -0500373 if (error)
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530374 {
John Loa43ccae2018-02-13 17:15:23 -0500375 clib_error_report (error);
376 return VNET_API_ERROR_INVALID_REGISTRATION;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530377 }
Chris Luke27fe48f2016-04-28 13:44:38 -0400378 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700379
John Loa43ccae2018-02-13 17:15:23 -0500380 /* Set GRE tunnel interface output node (not used for L3 payload) */
Neale Ranns07bbaef2020-04-09 07:34:08 -0400381 if (GRE_TUNNEL_TYPE_ERSPAN == t->type)
382 vnet_set_interface_output_node (vnm, hw_if_index,
383 gre_erspan_encap_node.index);
384 else
385 vnet_set_interface_output_node (vnm, hw_if_index,
386 gre_teb_encap_node.index);
John Loa43ccae2018-02-13 17:15:23 -0500387
388 hi = vnet_get_hw_interface (vnm, hw_if_index);
389 sw_if_index = hi->sw_if_index;
390
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100391 t->hw_if_index = hw_if_index;
392 t->outer_fib_index = outer_fib_index;
393 t->sw_if_index = sw_if_index;
Neale Rannsad422ed2016-11-02 14:20:04 +0000394 t->l2_adj_index = ADJ_INDEX_INVALID;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700395
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100396 vec_validate_init_empty (gm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
John Loa43ccae2018-02-13 17:15:23 -0500397 gm->tunnel_index_by_sw_if_index[sw_if_index] = t_idx;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700398
Ciara Loftus7eac9162016-09-30 15:47:03 +0100399 if (!is_ipv6)
400 {
401 vec_validate (im4->fib_index_by_sw_if_index, sw_if_index);
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530402 hi->min_packet_bytes =
403 64 + sizeof (gre_header_t) + sizeof (ip4_header_t);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100404 }
405 else
406 {
407 vec_validate (im6->fib_index_by_sw_if_index, sw_if_index);
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530408 hi->min_packet_bytes =
409 64 + sizeof (gre_header_t) + sizeof (ip6_header_t);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100410 }
Chris Luke71649002016-05-06 11:51:54 -0400411
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100412 /* Standard default gre MTU. */
Ole Troand7231612018-06-07 10:17:57 +0200413 vnet_sw_interface_set_mtu (vnm, sw_if_index, 9000);
Damjan Marionfe7d4a22018-04-13 19:43:39 +0200414
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100415 /*
416 * source the FIB entry for the tunnel's destination
417 * and become a child thereof. The tunnel will then get poked
418 * when the forwarding for the entry updates, and the tunnel can
419 * re-stack accordingly
420 */
Ciara Loftus7eac9162016-09-30 15:47:03 +0100421
422 clib_memcpy (&t->tunnel_src, &a->src, sizeof (t->tunnel_src));
423 t->tunnel_dst.fp_len = !is_ipv6 ? 32 : 128;
424 t->tunnel_dst.fp_proto = !is_ipv6 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
425 t->tunnel_dst.fp_addr = a->dst;
426
Neale Ranns33ce60d2017-12-14 08:51:32 -0800427 gre_tunnel_db_add (t, &key);
Neale Ranns4c16d802019-12-17 20:15:03 +0000428
Neale Ranns59ff9182019-12-29 23:55:18 +0000429 if (t->mode == TUNNEL_MODE_MP)
Neale Ranns03ce4622020-02-03 10:55:09 +0000430 teib_walk_itf (t->sw_if_index, gre_tunnel_add_teib_walk, t);
Neale Ranns4c16d802019-12-17 20:15:03 +0000431
John Loa43ccae2018-02-13 17:15:23 -0500432 if (t->type == GRE_TUNNEL_TYPE_ERSPAN)
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530433 {
John Loa43ccae2018-02-13 17:15:23 -0500434 gre_sn_key_t skey;
435 gre_sn_t *gre_sn;
436
437 gre_mk_sn_key (t, &skey);
438 gre_sn = (gre_sn_t *) hash_get_mem (gm->seq_num_by_key, &skey);
439 if (gre_sn != NULL)
440 {
441 gre_sn->ref_count++;
442 t->gre_sn = gre_sn;
443 }
444 else
445 {
446 gre_sn = clib_mem_alloc (sizeof (gre_sn_t));
447 gre_sn->seq_num = 0;
448 gre_sn->ref_count = 1;
449 t->gre_sn = gre_sn;
450 hash_set_mem_alloc (&gm->seq_num_by_key, &skey, (uword) gre_sn);
451 }
452 }
453
John Loa43ccae2018-02-13 17:15:23 -0500454 if (t->type != GRE_TUNNEL_TYPE_L3)
455 {
456 t->l2_adj_index = adj_nbr_add_or_lock
457 (t->tunnel_dst.fp_proto, VNET_LINK_ETHERNET, &zero_addr, sw_if_index);
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530458 gre_update_adj (vnm, t->sw_if_index, t->l2_adj_index);
459 }
Chris Luke27fe48f2016-04-28 13:44:38 -0400460
461 if (sw_if_indexp)
462 *sw_if_indexp = sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463
Jakub Grajciarf03beca2019-05-24 08:25:03 +0200464 /* register gre46-input nodes */
465 ip4_register_protocol (IP_PROTOCOL_GRE, gre4_input_node.index);
466 ip6_register_protocol (IP_PROTOCOL_GRE, gre6_input_node.index);
467
Ed Warnickecb9cada2015-12-08 15:45:58 -0700468 return 0;
469}
470
Ciara Loftus7eac9162016-09-30 15:47:03 +0100471static int
Neale Ranns5a8844b2019-04-16 07:15:35 +0000472vnet_gre_tunnel_delete (vnet_gre_tunnel_add_del_args_t * a,
John Loa43ccae2018-02-13 17:15:23 -0500473 u32 outer_fib_index, u32 * sw_if_indexp)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100474{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530475 gre_main_t *gm = &gre_main;
476 vnet_main_t *vnm = gm->vnet_main;
477 gre_tunnel_t *t;
Neale Ranns33ce60d2017-12-14 08:51:32 -0800478 gre_tunnel_key_t key;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100479 u32 sw_if_index;
John Lo101e0052018-01-06 00:22:54 -0500480
John Loa43ccae2018-02-13 17:15:23 -0500481 t = gre_tunnel_db_find (a, outer_fib_index, &key);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100482 if (NULL == t)
483 return VNET_API_ERROR_NO_SUCH_ENTRY;
484
Neale Ranns59ff9182019-12-29 23:55:18 +0000485 if (t->mode == TUNNEL_MODE_MP)
Neale Ranns03ce4622020-02-03 10:55:09 +0000486 teib_walk_itf (t->sw_if_index, gre_tunnel_delete_teib_walk, t);
Neale Ranns4c16d802019-12-17 20:15:03 +0000487
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100488 sw_if_index = t->sw_if_index;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530489 vnet_sw_interface_set_flags (vnm, sw_if_index, 0 /* down */ );
John Loa43ccae2018-02-13 17:15:23 -0500490
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100491 /* make sure tunnel is removed from l2 bd or xconnect */
Neale Rannsb4743802018-09-05 09:13:57 -0700492 set_int_l2_mode (gm->vlib_main, vnm, MODE_L3, sw_if_index, 0,
493 L2_BD_PORT_TYPE_NORMAL, 0, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100494 gm->tunnel_index_by_sw_if_index[sw_if_index] = ~0;
Neale Ranns177bbdc2016-11-15 09:46:51 +0000495
John Loa43ccae2018-02-13 17:15:23 -0500496 if (t->type == GRE_TUNNEL_TYPE_L3)
497 vnet_delete_hw_interface (vnm, t->hw_if_index);
498 else
499 ethernet_delete_interface (vnm, t->hw_if_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100500
Neale Rannsad422ed2016-11-02 14:20:04 +0000501 if (t->l2_adj_index != ADJ_INDEX_INVALID)
Neale Ranns4c3ba812019-03-26 07:02:58 +0000502 {
503 adj_midchain_delegate_unstack (t->l2_adj_index);
504 adj_unlock (t->l2_adj_index);
505 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100506
John Loa43ccae2018-02-13 17:15:23 -0500507 ASSERT ((t->type != GRE_TUNNEL_TYPE_ERSPAN) || (t->gre_sn != NULL));
508 if ((t->type == GRE_TUNNEL_TYPE_ERSPAN) && (t->gre_sn->ref_count-- == 1))
509 {
510 gre_sn_key_t skey;
511 gre_mk_sn_key (t, &skey);
512 hash_unset_mem_free (&gm->seq_num_by_key, &skey);
513 clib_mem_free (t->gre_sn);
514 }
515
516 hash_unset (gm->instance_used, t->user_instance);
Neale Ranns4c16d802019-12-17 20:15:03 +0000517 gre_tunnel_db_remove (t, &key);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100518 pool_put (gm->tunnels, t);
519
520 if (sw_if_indexp)
521 *sw_if_indexp = sw_if_index;
522
523 return 0;
524}
525
526int
Neale Ranns5a8844b2019-04-16 07:15:35 +0000527vnet_gre_tunnel_add_del (vnet_gre_tunnel_add_del_args_t * a,
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530528 u32 * sw_if_indexp)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100529{
John Loa43ccae2018-02-13 17:15:23 -0500530 u32 outer_fib_index;
531
532 if (!a->is_ipv6)
Neale Ranns5f8f6172019-04-18 10:23:56 +0000533 outer_fib_index = ip4_fib_index_from_table_id (a->outer_table_id);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100534 else
Neale Ranns5f8f6172019-04-18 10:23:56 +0000535 outer_fib_index = ip6_fib_index_from_table_id (a->outer_table_id);
John Loa43ccae2018-02-13 17:15:23 -0500536
537 if (~0 == outer_fib_index)
538 return VNET_API_ERROR_NO_SUCH_FIB;
539
540 if (a->session_id > GTK_SESSION_ID_MAX)
541 return VNET_API_ERROR_INVALID_SESSION_ID;
542
Neale Ranns59ff9182019-12-29 23:55:18 +0000543 if (a->mode == TUNNEL_MODE_MP && !ip46_address_is_zero (&a->dst))
Neale Ranns4c16d802019-12-17 20:15:03 +0000544 return (VNET_API_ERROR_INVALID_DST_ADDRESS);
545
John Loa43ccae2018-02-13 17:15:23 -0500546 if (a->is_add)
547 return (vnet_gre_tunnel_add (a, outer_fib_index, sw_if_indexp));
548 else
549 return (vnet_gre_tunnel_delete (a, outer_fib_index, sw_if_indexp));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100550}
551
Neale Rannsb80c5362016-10-08 13:03:40 +0100552clib_error_t *
553gre_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100554{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530555 gre_main_t *gm = &gre_main;
556 vnet_hw_interface_t *hi;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100557 gre_tunnel_t *t;
Neale Rannsb80c5362016-10-08 13:03:40 +0100558 u32 ti;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100559
Neale Rannsb80c5362016-10-08 13:03:40 +0100560 hi = vnet_get_hw_interface (vnm, hw_if_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100561
Neale Rannsb80c5362016-10-08 13:03:40 +0100562 if (NULL == gm->tunnel_index_by_sw_if_index ||
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530563 hi->sw_if_index >= vec_len (gm->tunnel_index_by_sw_if_index))
564 return (NULL);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100565
Neale Rannsb80c5362016-10-08 13:03:40 +0100566 ti = gm->tunnel_index_by_sw_if_index[hi->sw_if_index];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100567
Neale Rannsb80c5362016-10-08 13:03:40 +0100568 if (~0 == ti)
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530569 /* not one of ours */
570 return (NULL);
Neale Rannsb80c5362016-10-08 13:03:40 +0100571
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530572 t = pool_elt_at_index (gm->tunnels, ti);
Neale Rannsb80c5362016-10-08 13:03:40 +0100573
574 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530575 vnet_hw_interface_set_flags (vnm, hw_if_index,
576 VNET_HW_INTERFACE_FLAG_LINK_UP);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100577 else
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530578 vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100579
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530580 gre_tunnel_restack (t);
Neale Rannsb80c5362016-10-08 13:03:40 +0100581
582 return /* no error */ 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100583}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700584
585static clib_error_t *
586create_gre_tunnel_command_fn (vlib_main_t * vm,
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530587 unformat_input_t * input,
588 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700589{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530590 unformat_input_t _line_input, *line_input = &_line_input;
Neale Ranns5a8844b2019-04-16 07:15:35 +0000591 vnet_gre_tunnel_add_del_args_t _a, *a = &_a;
Neale Ranns5f8f6172019-04-18 10:23:56 +0000592 ip46_address_t src = ip46_address_initializer, dst =
593 ip46_address_initializer;
John Loa43ccae2018-02-13 17:15:23 -0500594 u32 instance = ~0;
Neale Ranns5f8f6172019-04-18 10:23:56 +0000595 u32 outer_table_id = 0;
John Loa43ccae2018-02-13 17:15:23 -0500596 gre_tunnel_type_t t_type = GRE_TUNNEL_TYPE_L3;
Neale Ranns59ff9182019-12-29 23:55:18 +0000597 tunnel_mode_t t_mode = TUNNEL_MODE_P2P;
John Loa43ccae2018-02-13 17:15:23 -0500598 u32 session_id = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700599 int rv;
Chris Luke27fe48f2016-04-28 13:44:38 -0400600 u8 is_add = 1;
Pierre Pfister78ea9c22016-05-23 12:51:54 +0100601 u32 sw_if_index;
Billy McFalla9a20e72017-02-15 11:39:12 -0500602 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700603
604 /* Get a line of input. */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530605 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700606 return 0;
607
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530608 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
609 {
610 if (unformat (line_input, "del"))
611 is_add = 0;
John Loa43ccae2018-02-13 17:15:23 -0500612 else if (unformat (line_input, "instance %d", &instance))
613 ;
Neale Ranns5f8f6172019-04-18 10:23:56 +0000614 else if (unformat (line_input, "src %U", unformat_ip46_address, &src))
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530615 ;
Neale Ranns5f8f6172019-04-18 10:23:56 +0000616 else if (unformat (line_input, "dst %U", unformat_ip46_address, &dst))
617 ;
618 else if (unformat (line_input, "outer-table-id %d", &outer_table_id))
619 ;
620 else if (unformat (line_input, "multipoint"))
Neale Ranns59ff9182019-12-29 23:55:18 +0000621 t_mode = TUNNEL_MODE_MP;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530622 else if (unformat (line_input, "teb"))
John Loa43ccae2018-02-13 17:15:23 -0500623 t_type = GRE_TUNNEL_TYPE_TEB;
624 else if (unformat (line_input, "erspan %d", &session_id))
625 t_type = GRE_TUNNEL_TYPE_ERSPAN;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530626 else
627 {
628 error = clib_error_return (0, "unknown input `%U'",
629 format_unformat_error, line_input);
630 goto done;
631 }
632 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700633
Neale Ranns5f8f6172019-04-18 10:23:56 +0000634 if (ip46_address_is_equal (&src, &dst))
Billy McFalla9a20e72017-02-15 11:39:12 -0500635 {
636 error = clib_error_return (0, "src and dst are identical");
637 goto done;
638 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700639
Neale Ranns59ff9182019-12-29 23:55:18 +0000640 if (t_mode != TUNNEL_MODE_MP && ip46_address_is_zero (&dst))
Ciara Loftus7eac9162016-09-30 15:47:03 +0100641 {
Neale Ranns5f8f6172019-04-18 10:23:56 +0000642 error = clib_error_return (0, "destination address not specified");
643 goto done;
644 }
645
646 if (ip46_address_is_zero (&src))
647 {
648 error = clib_error_return (0, "source address not specified");
649 goto done;
650 }
651
652 if (ip46_address_is_ip4 (&src) != ip46_address_is_ip4 (&dst))
653 {
654 error =
655 clib_error_return (0, "src and dst address must be the same AF");
Ciara Loftus7eac9162016-09-30 15:47:03 +0100656 goto done;
657 }
658
Dave Barachb7b92992018-10-17 10:38:51 -0400659 clib_memset (a, 0, sizeof (*a));
John Loa43ccae2018-02-13 17:15:23 -0500660 a->is_add = is_add;
Neale Ranns5f8f6172019-04-18 10:23:56 +0000661 a->outer_table_id = outer_table_id;
Neale Ranns5a8844b2019-04-16 07:15:35 +0000662 a->type = t_type;
Neale Ranns5f8f6172019-04-18 10:23:56 +0000663 a->mode = t_mode;
John Loa43ccae2018-02-13 17:15:23 -0500664 a->session_id = session_id;
Neale Ranns5f8f6172019-04-18 10:23:56 +0000665 a->is_ipv6 = !ip46_address_is_ip4 (&src);
John Loa43ccae2018-02-13 17:15:23 -0500666 a->instance = instance;
Neale Ranns5f8f6172019-04-18 10:23:56 +0000667 clib_memcpy (&a->src, &src, sizeof (a->src));
668 clib_memcpy (&a->dst, &dst, sizeof (a->dst));
Chris Luke27fe48f2016-04-28 13:44:38 -0400669
Neale Ranns5a8844b2019-04-16 07:15:35 +0000670 rv = vnet_gre_tunnel_add_del (a, &sw_if_index);
Chris Luke27fe48f2016-04-28 13:44:38 -0400671
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530672 switch (rv)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700673 {
674 case 0:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530675 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
676 vnet_get_main (), sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700677 break;
John Loa43ccae2018-02-13 17:15:23 -0500678 case VNET_API_ERROR_IF_ALREADY_EXISTS:
Billy McFalla9a20e72017-02-15 11:39:12 -0500679 error = clib_error_return (0, "GRE tunnel already exists...");
680 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700681 case VNET_API_ERROR_NO_SUCH_FIB:
Neale Ranns5f8f6172019-04-18 10:23:56 +0000682 error = clib_error_return (0, "outer table ID %d doesn't exist\n",
683 outer_table_id);
Billy McFalla9a20e72017-02-15 11:39:12 -0500684 goto done;
John Loa43ccae2018-02-13 17:15:23 -0500685 case VNET_API_ERROR_NO_SUCH_ENTRY:
686 error = clib_error_return (0, "GRE tunnel doesn't exist");
687 goto done;
688 case VNET_API_ERROR_INVALID_SESSION_ID:
689 error = clib_error_return (0, "session ID %d out of range\n",
690 session_id);
691 goto done;
692 case VNET_API_ERROR_INSTANCE_IN_USE:
693 error = clib_error_return (0, "Instance is in use");
694 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700695 default:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530696 error =
Neale Ranns5a8844b2019-04-16 07:15:35 +0000697 clib_error_return (0, "vnet_gre_tunnel_add_del returned %d", rv);
Billy McFalla9a20e72017-02-15 11:39:12 -0500698 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700699 }
700
Billy McFalla9a20e72017-02-15 11:39:12 -0500701done:
702 unformat_free (line_input);
703
704 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700705}
706
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530707/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700708VLIB_CLI_COMMAND (create_gre_tunnel_command, static) = {
709 .path = "create gre tunnel",
John Loa43ccae2018-02-13 17:15:23 -0500710 .short_help = "create gre tunnel src <addr> dst <addr> [instance <n>] "
Neale Rannsae0d46e2020-09-22 13:20:27 +0000711 "[outer-fib-id <fib>] [teb | erspan <session-id>] [del] "
712 "[multipoint]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700713 .function = create_gre_tunnel_command_fn,
714};
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530715/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700716
Chris Luke27fe48f2016-04-28 13:44:38 -0400717static clib_error_t *
718show_gre_tunnel_command_fn (vlib_main_t * vm,
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530719 unformat_input_t * input,
720 vlib_cli_command_t * cmd)
Chris Luke27fe48f2016-04-28 13:44:38 -0400721{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530722 gre_main_t *gm = &gre_main;
723 gre_tunnel_t *t;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100724 u32 ti = ~0;
Chris Luke27fe48f2016-04-28 13:44:38 -0400725
726 if (pool_elts (gm->tunnels) == 0)
727 vlib_cli_output (vm, "No GRE tunnels configured...");
728
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100729 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
730 {
731 if (unformat (input, "%d", &ti))
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530732 ;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100733 else
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530734 break;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100735 }
736
737 if (~0 == ti)
738 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530739 /* *INDENT-OFF* */
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100740 pool_foreach (t, gm->tunnels,
741 ({
Neale Rannsb80c5362016-10-08 13:03:40 +0100742 vlib_cli_output (vm, "%U", format_gre_tunnel, t);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100743 }));
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530744 /* *INDENT-ON* */
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100745 }
746 else
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530747 {
748 t = pool_elt_at_index (gm->tunnels, ti);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100749
Neale Rannsb80c5362016-10-08 13:03:40 +0100750 vlib_cli_output (vm, "%U", format_gre_tunnel, t);
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530751 }
Chris Luke27fe48f2016-04-28 13:44:38 -0400752
753 return 0;
754}
755
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530756/* *INDENT-OFF* */
Chris Luke27fe48f2016-04-28 13:44:38 -0400757VLIB_CLI_COMMAND (show_gre_tunnel_command, static) = {
758 .path = "show gre tunnel",
759 .function = show_gre_tunnel_command_fn,
760};
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530761/* *INDENT-ON* */
Chris Luke27fe48f2016-04-28 13:44:38 -0400762
Neale Ranns03ce4622020-02-03 10:55:09 +0000763const static teib_vft_t gre_teib_vft = {
764 .nv_added = gre_teib_entry_added,
765 .nv_deleted = gre_teib_entry_deleted,
Neale Ranns4c16d802019-12-17 20:15:03 +0000766};
767
Ed Warnickecb9cada2015-12-08 15:45:58 -0700768/* force inclusion from application's main.c */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530769clib_error_t *
770gre_interface_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700771{
Neale Ranns03ce4622020-02-03 10:55:09 +0000772 teib_register (&gre_teib_vft);
Neale Ranns4c16d802019-12-17 20:15:03 +0000773
Neale Ranns4c3ba812019-03-26 07:02:58 +0000774 return (NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700775}
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530776
777VLIB_INIT_FUNCTION (gre_interface_init);
778
779/*
780 * fd.io coding-style-patch-verification: ON
781 *
782 * Local Variables:
783 * eval: (c-set-style "gnu")
784 * End:
785 */