blob: 0cc60b87eebc1c4a3cec6f134a28cfab4554256d [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * gre.h: types/functions for gre.
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#ifndef included_gre_h
19#define included_gre_h
20
21#include <vnet/vnet.h>
22#include <vnet/gre/packet.h>
23#include <vnet/ip/ip.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070024#include <vnet/pg/pg.h>
25#include <vnet/ip/format.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010026#include <vnet/adj/adj_types.h>
Neale Ranns59ff9182019-12-29 23:55:18 +000027#include <vnet/tunnel/tunnel.h>
Neale Ranns14053c92019-12-29 23:55:18 +000028#include <vnet/nhrp/nhrp.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070029
Damjan Marionb8abf872016-03-14 20:02:35 +010030extern vnet_hw_interface_class_t gre_hw_interface_class;
Neale Ranns5f8f6172019-04-18 10:23:56 +000031extern vnet_hw_interface_class_t mgre_hw_interface_class;
Ed Warnickecb9cada2015-12-08 15:45:58 -070032
Swarup Nayak9ff647a2017-11-27 10:27:43 +053033typedef enum
34{
Ed Warnickecb9cada2015-12-08 15:45:58 -070035#define gre_error(n,s) GRE_ERROR_##n,
36#include <vnet/gre/error.def>
37#undef gre_error
38 GRE_N_ERROR,
39} gre_error_t;
40
Neale Ranns177bbdc2016-11-15 09:46:51 +000041/**
Neale Ranns5f8f6172019-04-18 10:23:56 +000042 * L3: GRE (i.e. this tunnel is in L3 mode)
43 * TEB: Transparent Ethernet Bridging - the tunnel is in L2 mode
44 * ERSPAN: type 2 - the tunnel is for port mirror SPAN output. Each tunnel is
45 * associated with a session ID and expected to be used for encap
46 * and output of mirrored packet from a L2 network only. There is
47 * no support for receiving ERSPAN packets from a GRE ERSPAN tunnel
48 */
49#define foreach_gre_tunnel_type \
50 _(L3, "L3") \
51 _(TEB, "TEB") \
52 _(ERSPAN, "ERSPAN") \
53
54/**
John Loa43ccae2018-02-13 17:15:23 -050055 * @brief The GRE tunnel type
56 */
57typedef enum gre_tunnel_type_t_
58{
Neale Ranns5f8f6172019-04-18 10:23:56 +000059#define _(n, s) GRE_TUNNEL_TYPE_##n,
60 foreach_gre_tunnel_type
61#undef _
62} __clib_packed gre_tunnel_type_t;
John Loa43ccae2018-02-13 17:15:23 -050063
Neale Ranns5f8f6172019-04-18 10:23:56 +000064extern u8 *format_gre_tunnel_type (u8 * s, va_list * args);
Neale Ranns5a8844b2019-04-16 07:15:35 +000065
John Loa43ccae2018-02-13 17:15:23 -050066
67/**
Neale Ranns177bbdc2016-11-15 09:46:51 +000068 * A GRE payload protocol registration
69 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +053070typedef struct
71{
Neale Ranns177bbdc2016-11-15 09:46:51 +000072 /** Name (a c string). */
Swarup Nayak9ff647a2017-11-27 10:27:43 +053073 char *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -070074
Neale Ranns177bbdc2016-11-15 09:46:51 +000075 /** GRE protocol type in host byte order. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070076 gre_protocol_t protocol;
77
John Loa43ccae2018-02-13 17:15:23 -050078 /** GRE tunnel type */
79 gre_tunnel_type_t tunnel_type;
80
Neale Ranns177bbdc2016-11-15 09:46:51 +000081 /** Node which handles this type. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070082 u32 node_index;
83
Neale Ranns177bbdc2016-11-15 09:46:51 +000084 /** Next index for this type. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070085 u32 next_index;
86} gre_protocol_info_t;
87
Neale Ranns177bbdc2016-11-15 09:46:51 +000088/**
Neale Ranns4c16d802019-12-17 20:15:03 +000089 * Elements of the GRE key that are common for v6 and v6 addresses
90 */
91typedef struct gre_tunnel_key_common_t_
92{
93 union
94 {
95 struct
96 {
97 u32 fib_index;
98 u16 session_id;
99 gre_tunnel_type_t type;
Neale Ranns59ff9182019-12-29 23:55:18 +0000100 tunnel_mode_t mode;
Neale Ranns4c16d802019-12-17 20:15:03 +0000101 };
102 u64 as_u64;
103 };
104} gre_tunnel_key_common_t;
105
106STATIC_ASSERT_SIZEOF (gre_tunnel_key_common_t, sizeof (u64));
107
108/**
Neale Ranns33ce60d2017-12-14 08:51:32 -0800109 * @brief Key for a IPv4 GRE Tunnel
110 */
111typedef struct gre_tunnel_key4_t_
112{
113 /**
114 * Source and destination IP addresses
115 */
116 union
117 {
118 struct
119 {
120 ip4_address_t gtk_src;
121 ip4_address_t gtk_dst;
122 };
123 u64 gtk_as_u64;
124 };
125
Neale Ranns4c16d802019-12-17 20:15:03 +0000126 /** address independent attributes */
127 gre_tunnel_key_common_t gtk_common;
Neale Ranns33ce60d2017-12-14 08:51:32 -0800128} __attribute__ ((packed)) gre_tunnel_key4_t;
129
Neale Ranns4c16d802019-12-17 20:15:03 +0000130STATIC_ASSERT_SIZEOF (gre_tunnel_key4_t, 2 * sizeof (u64));
131
Neale Ranns33ce60d2017-12-14 08:51:32 -0800132/**
133 * @brief Key for a IPv6 GRE Tunnel
134 * We use a different type so that the V4 key hash is as small as possible
135 */
136typedef struct gre_tunnel_key6_t_
137{
138 /**
139 * Source and destination IP addresses
140 */
141 ip6_address_t gtk_src;
142 ip6_address_t gtk_dst;
143
Neale Ranns4c16d802019-12-17 20:15:03 +0000144 /** address independent attributes */
145 gre_tunnel_key_common_t gtk_common;
Neale Ranns33ce60d2017-12-14 08:51:32 -0800146} __attribute__ ((packed)) gre_tunnel_key6_t;
147
Neale Ranns4c16d802019-12-17 20:15:03 +0000148STATIC_ASSERT_SIZEOF (gre_tunnel_key6_t, 5 * sizeof (u64));
John Loa43ccae2018-02-13 17:15:23 -0500149
Neale Ranns33ce60d2017-12-14 08:51:32 -0800150/**
151 * Union of the two possible key types
152 */
153typedef union gre_tunnel_key_t_
154{
155 gre_tunnel_key4_t gtk_v4;
156 gre_tunnel_key6_t gtk_v6;
157} gre_tunnel_key_t;
158
159/**
Neale Ranns4c16d802019-12-17 20:15:03 +0000160 * The session ID is only a 10 bit value
161 */
162#define GTK_SESSION_ID_MAX (0x3ff)
163
164/**
John Loa43ccae2018-02-13 17:15:23 -0500165 * Used for GRE header seq number generation for ERSPAN encap
166 */
167typedef struct
168{
169 u32 seq_num;
170 u32 ref_count;
171} gre_sn_t;
172
173/**
174 * Hash key for GRE header seq number generation for ERSPAN encap
175 */
176typedef struct
177{
178 ip46_address_t src;
179 ip46_address_t dst;
180 u32 fib_index;
181} gre_sn_key_t;
182
183/**
Neale Ranns177bbdc2016-11-15 09:46:51 +0000184 * @brief A representation of a GRE tunnel
185 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530186typedef struct
187{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100188 /**
Dave Baracheb987d32018-05-03 08:26:39 -0400189 * Required for pool_get_aligned
190 */
191 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
192
193 /**
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100194 * The tunnel's source/local address
195 */
Ciara Loftus7eac9162016-09-30 15:47:03 +0100196 ip46_address_t tunnel_src;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100197 /**
198 * The tunnel's destination/remote address
199 */
Ciara Loftus7eac9162016-09-30 15:47:03 +0100200 fib_prefix_t tunnel_dst;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100201 /**
202 * The FIB in which the src.dst address are present
203 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204 u32 outer_fib_index;
205 u32 hw_if_index;
Chris Luke27fe48f2016-04-28 13:44:38 -0400206 u32 sw_if_index;
Neale Ranns177bbdc2016-11-15 09:46:51 +0000207 gre_tunnel_type_t type;
Neale Ranns59ff9182019-12-29 23:55:18 +0000208 tunnel_mode_t mode;
Neale Rannse5b94dd2019-12-31 05:13:14 +0000209 tunnel_encap_decap_flags_t flags;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100210
211 /**
Neale Rannsb80c5362016-10-08 13:03:40 +0100212 * an L2 tunnel always rquires an L2 midchain. cache here for DP.
213 */
214 adj_index_t l2_adj_index;
John Loa43ccae2018-02-13 17:15:23 -0500215
216 /**
217 * ERSPAN type 2 session ID, least significant 10 bits of u16
218 */
219 u16 session_id;
220
221 /**
222 * GRE header sequence number (SN) used for ERSPAN type 2 header, must be
223 * bumped automically to be thread safe. As multiple GRE tunnels are created
224 * for the same fib-idx/DIP/SIP with different ERSPAN session number, they all
225 * share the same SN which is kept per FIB/DIP/SIP, as specified by RFC2890.
226 */
227 gre_sn_t *gre_sn;
228
229
230 u32 dev_instance; /* Real device instance in tunnel vector */
231 u32 user_instance; /* Instance name being shown to user */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700232} gre_tunnel_t;
233
John Loa43ccae2018-02-13 17:15:23 -0500234typedef struct
235{
236 u8 next_index;
237 u8 tunnel_type;
238} next_info_t;
239
Neale Ranns177bbdc2016-11-15 09:46:51 +0000240/**
241 * @brief GRE related global data
242 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530243typedef struct
244{
Neale Ranns177bbdc2016-11-15 09:46:51 +0000245 /**
246 * pool of tunnel instances
247 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248 gre_tunnel_t *tunnels;
249
Neale Ranns177bbdc2016-11-15 09:46:51 +0000250 /**
251 * GRE payload protocol registrations
252 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530253 gre_protocol_info_t *protocol_infos;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254
Neale Ranns177bbdc2016-11-15 09:46:51 +0000255 /**
256 * Hash tables mapping name/protocol to protocol info index.
257 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530258 uword *protocol_info_by_name, *protocol_info_by_protocol;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100259
Neale Ranns177bbdc2016-11-15 09:46:51 +0000260 /**
John Loa43ccae2018-02-13 17:15:23 -0500261 * Hash mapping to tunnels with ipv4 src/dst addr
Neale Ranns177bbdc2016-11-15 09:46:51 +0000262 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530263 uword *tunnel_by_key4;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100264
265 /**
John Loa43ccae2018-02-13 17:15:23 -0500266 * Hash mapping to tunnels with ipv6 src/dst addr
Neale Ranns33ce60d2017-12-14 08:51:32 -0800267 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530268 uword *tunnel_by_key6;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700269
Neale Ranns177bbdc2016-11-15 09:46:51 +0000270 /**
John Loa43ccae2018-02-13 17:15:23 -0500271 * Hash mapping tunnel src/dst addr and fib-idx to sequence number
Neale Ranns177bbdc2016-11-15 09:46:51 +0000272 */
John Loa43ccae2018-02-13 17:15:23 -0500273 uword *seq_num_by_key;
Chris Luke27fe48f2016-04-28 13:44:38 -0400274
Neale Ranns177bbdc2016-11-15 09:46:51 +0000275 /**
276 * Mapping from sw_if_index to tunnel index
277 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530278 u32 *tunnel_index_by_sw_if_index;
Chris Luke27fe48f2016-04-28 13:44:38 -0400279
Damjan Marion63d5bae2017-04-04 01:28:26 +0200280 /* Sparse vector mapping gre protocol in network byte order
281 to next index. */
John Loa43ccae2018-02-13 17:15:23 -0500282 next_info_t *next_by_protocol;
Damjan Marion63d5bae2017-04-04 01:28:26 +0200283
Ed Warnickecb9cada2015-12-08 15:45:58 -0700284 /* convenience */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530285 vlib_main_t *vlib_main;
286 vnet_main_t *vnet_main;
John Loa43ccae2018-02-13 17:15:23 -0500287
288 /* Record used instances */
289 uword *instance_used;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290} gre_main_t;
291
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100292/**
293 * @brief IPv4 and GRE header.
Neale Ranns177bbdc2016-11-15 09:46:51 +0000294 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530295/* *INDENT-OFF* */
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100296typedef CLIB_PACKED (struct {
297 ip4_header_t ip4;
298 gre_header_t gre;
299}) ip4_and_gre_header_t;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530300/* *INDENT-ON* */
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100301
Ciara Loftus7eac9162016-09-30 15:47:03 +0100302/**
303 * @brief IPv6 and GRE header.
304 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530305/* *INDENT-OFF* */
Ciara Loftus7eac9162016-09-30 15:47:03 +0100306typedef CLIB_PACKED (struct {
307 ip6_header_t ip6;
308 gre_header_t gre;
309}) ip6_and_gre_header_t;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530310/* *INDENT-ON* */
Ciara Loftus7eac9162016-09-30 15:47:03 +0100311
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312always_inline gre_protocol_info_t *
313gre_get_protocol_info (gre_main_t * em, gre_protocol_t protocol)
314{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530315 uword *p = hash_get (em->protocol_info_by_protocol, protocol);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700316 return p ? vec_elt_at_index (em->protocol_infos, p[0]) : 0;
317}
318
Dave Wallace71612d62017-10-24 01:32:41 -0400319extern gre_main_t gre_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530321extern clib_error_t *gre_interface_admin_up_down (vnet_main_t * vnm,
322 u32 hw_if_index, u32 flags);
Neale Rannsb80c5362016-10-08 13:03:40 +0100323
324extern void gre_tunnel_stack (adj_index_t ai);
325extern void gre_update_adj (vnet_main_t * vnm,
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530326 u32 sw_if_index, adj_index_t ai);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327
Neale Ranns14053c92019-12-29 23:55:18 +0000328typedef struct mgre_walk_ctx_t_
329{
330 const gre_tunnel_t *t;
331 const nhrp_entry_t *ne;
332} mgre_walk_ctx_t;
333
334adj_walk_rc_t mgre_mk_complete_walk (adj_index_t ai, void *data);
335adj_walk_rc_t mgre_mk_incomplete_walk (adj_index_t ai, void *data);
336
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337format_function_t format_gre_protocol;
338format_function_t format_gre_header;
339format_function_t format_gre_header_with_length;
340
Ciara Loftus7eac9162016-09-30 15:47:03 +0100341extern vlib_node_registration_t gre4_input_node;
342extern vlib_node_registration_t gre6_input_node;
John Loa43ccae2018-02-13 17:15:23 -0500343extern vlib_node_registration_t gre_encap_node;
Damjan Marionb8abf872016-03-14 20:02:35 +0100344extern vnet_device_class_t gre_device_class;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345
346/* Parse gre protocol as 0xXXXX or protocol name.
347 In either host or network byte order. */
348unformat_function_t unformat_gre_protocol_host_byte_order;
349unformat_function_t unformat_gre_protocol_net_byte_order;
350
351/* Parse gre header. */
352unformat_function_t unformat_gre_header;
353unformat_function_t unformat_pg_gre_header;
354
355void
John Loa43ccae2018-02-13 17:15:23 -0500356gre_register_input_protocol (vlib_main_t * vm, gre_protocol_t protocol,
357 u32 node_index, gre_tunnel_type_t tunnel_type);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358
359/* manually added to the interface output node in gre.c */
360#define GRE_OUTPUT_NEXT_LOOKUP 1
361
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530362typedef struct
363{
Chris Luke27fe48f2016-04-28 13:44:38 -0400364 u8 is_add;
Neale Ranns5a8844b2019-04-16 07:15:35 +0000365 gre_tunnel_type_t type;
Neale Ranns59ff9182019-12-29 23:55:18 +0000366 tunnel_mode_t mode;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100367 u8 is_ipv6;
John Loa43ccae2018-02-13 17:15:23 -0500368 u32 instance;
369 ip46_address_t src, dst;
Neale Ranns5f8f6172019-04-18 10:23:56 +0000370 u32 outer_table_id;
John Loa43ccae2018-02-13 17:15:23 -0500371 u16 session_id;
Neale Rannse5b94dd2019-12-31 05:13:14 +0000372 tunnel_encap_decap_flags_t flags;
Neale Ranns5a8844b2019-04-16 07:15:35 +0000373} vnet_gre_tunnel_add_del_args_t;
Chris Luke27fe48f2016-04-28 13:44:38 -0400374
Neale Ranns5a8844b2019-04-16 07:15:35 +0000375extern int vnet_gre_tunnel_add_del (vnet_gre_tunnel_add_del_args_t * a,
376 u32 * sw_if_indexp);
Chris Luke27fe48f2016-04-28 13:44:38 -0400377
Neale Ranns33ce60d2017-12-14 08:51:32 -0800378static inline void
John Loa43ccae2018-02-13 17:15:23 -0500379gre_mk_key4 (ip4_address_t src,
380 ip4_address_t dst,
Neale Ranns4c16d802019-12-17 20:15:03 +0000381 u32 fib_index,
382 gre_tunnel_type_t ttype,
Neale Ranns59ff9182019-12-29 23:55:18 +0000383 tunnel_mode_t tmode, u16 session_id, gre_tunnel_key4_t * key)
Neale Ranns33ce60d2017-12-14 08:51:32 -0800384{
John Loa43ccae2018-02-13 17:15:23 -0500385 key->gtk_src = src;
386 key->gtk_dst = dst;
Neale Ranns4c16d802019-12-17 20:15:03 +0000387 key->gtk_common.type = ttype;
388 key->gtk_common.mode = tmode;
389 key->gtk_common.fib_index = fib_index;
390 key->gtk_common.session_id = session_id;
Neale Ranns33ce60d2017-12-14 08:51:32 -0800391}
392
393static inline int
394gre_match_key4 (const gre_tunnel_key4_t * key1,
395 const gre_tunnel_key4_t * key2)
396{
397 return ((key1->gtk_as_u64 == key2->gtk_as_u64) &&
Neale Ranns4c16d802019-12-17 20:15:03 +0000398 (key1->gtk_common.as_u64 == key2->gtk_common.as_u64));
Neale Ranns33ce60d2017-12-14 08:51:32 -0800399}
400
401static inline void
402gre_mk_key6 (const ip6_address_t * src,
403 const ip6_address_t * dst,
Neale Ranns4c16d802019-12-17 20:15:03 +0000404 u32 fib_index,
405 gre_tunnel_type_t ttype,
Neale Ranns59ff9182019-12-29 23:55:18 +0000406 tunnel_mode_t tmode, u16 session_id, gre_tunnel_key6_t * key)
Neale Ranns33ce60d2017-12-14 08:51:32 -0800407{
408 key->gtk_src = *src;
409 key->gtk_dst = *dst;
Neale Ranns4c16d802019-12-17 20:15:03 +0000410 key->gtk_common.type = ttype;
411 key->gtk_common.mode = tmode;
412 key->gtk_common.fib_index = fib_index;
413 key->gtk_common.session_id = session_id;
Neale Ranns33ce60d2017-12-14 08:51:32 -0800414}
415
416static inline int
417gre_match_key6 (const gre_tunnel_key6_t * key1,
418 const gre_tunnel_key6_t * key2)
419{
Neale Ranns4c16d802019-12-17 20:15:03 +0000420 return (ip6_address_is_equal (&key1->gtk_src, &key2->gtk_src) &&
421 ip6_address_is_equal (&key1->gtk_dst, &key2->gtk_dst) &&
422 (key1->gtk_common.as_u64 == key2->gtk_common.as_u64));
John Loa43ccae2018-02-13 17:15:23 -0500423}
424
425static inline void
426gre_mk_sn_key (const gre_tunnel_t * gt, gre_sn_key_t * key)
427{
428 key->src = gt->tunnel_src;
429 key->dst = gt->tunnel_dst.fp_addr;
430 key->fib_index = gt->outer_fib_index;
Neale Ranns33ce60d2017-12-14 08:51:32 -0800431}
432
Ed Warnickecb9cada2015-12-08 15:45:58 -0700433#endif /* included_gre_h */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530434
435/*
436 * fd.io coding-style-patch-verification: ON
437 *
438 * Local Variables:
439 * eval: (c-set-style "gnu")
440 * End:
441 */