blob: 416ab300877fab90de5e5f7aee1d4643c8776ad4 [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>
Ed Warnickecb9cada2015-12-08 15:45:58 -070027
Damjan Marionb8abf872016-03-14 20:02:35 +010028extern vnet_hw_interface_class_t gre_hw_interface_class;
Ed Warnickecb9cada2015-12-08 15:45:58 -070029
Swarup Nayak9ff647a2017-11-27 10:27:43 +053030typedef enum
31{
Ed Warnickecb9cada2015-12-08 15:45:58 -070032#define gre_error(n,s) GRE_ERROR_##n,
33#include <vnet/gre/error.def>
34#undef gre_error
35 GRE_N_ERROR,
36} gre_error_t;
37
Neale Ranns177bbdc2016-11-15 09:46:51 +000038/**
John Loa43ccae2018-02-13 17:15:23 -050039 * @brief The GRE tunnel type
40 */
41typedef enum gre_tunnel_type_t_
42{
43 /**
44 * L3 GRE (i.e. this tunnel is in L3 mode)
45 */
46 GRE_TUNNEL_TYPE_L3 = 0,
47 /**
48 * Transparent Ethernet Bridging - the tunnel is in L2 mode
49 */
50 GRE_TUNNEL_TYPE_TEB = 1,
51 /**
52 * ERSPAN type 2 - the tunnel is for port mirror SPAN output. Each tunnel is
53 * associated with a session ID and expected to be used for encap and output
54 * of mirrored packet from a L2 network only. There is no support for
55 * receiving ERSPAN packets from a GRE ERSPAN tunnel in VPP.
56 */
57 GRE_TUNNEL_TYPE_ERSPAN = 2,
58
59 GRE_TUNNEL_TYPE_N
60} gre_tunnel_type_t;
61
62#define GRE_TUNNEL_TYPE_NAMES { \
63 [GRE_TUNNEL_TYPE_L3] = "L3", \
64 [GRE_TUNNEL_TYPE_TEB] = "TEB", \
65 [GRE_TUNNEL_TYPE_ERSPAN] = "ERSPAN", \
66}
67
68/**
Neale Ranns177bbdc2016-11-15 09:46:51 +000069 * A GRE payload protocol registration
70 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +053071typedef struct
72{
Neale Ranns177bbdc2016-11-15 09:46:51 +000073 /** Name (a c string). */
Swarup Nayak9ff647a2017-11-27 10:27:43 +053074 char *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -070075
Neale Ranns177bbdc2016-11-15 09:46:51 +000076 /** GRE protocol type in host byte order. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070077 gre_protocol_t protocol;
78
John Loa43ccae2018-02-13 17:15:23 -050079 /** GRE tunnel type */
80 gre_tunnel_type_t tunnel_type;
81
Neale Ranns177bbdc2016-11-15 09:46:51 +000082 /** Node which handles this type. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070083 u32 node_index;
84
Neale Ranns177bbdc2016-11-15 09:46:51 +000085 /** Next index for this type. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070086 u32 next_index;
87} gre_protocol_info_t;
88
Neale Ranns177bbdc2016-11-15 09:46:51 +000089/**
Neale Ranns33ce60d2017-12-14 08:51:32 -080090 * @brief Key for a IPv4 GRE Tunnel
91 */
92typedef struct gre_tunnel_key4_t_
93{
94 /**
95 * Source and destination IP addresses
96 */
97 union
98 {
99 struct
100 {
101 ip4_address_t gtk_src;
102 ip4_address_t gtk_dst;
103 };
104 u64 gtk_as_u64;
105 };
106
107 /**
John Loa43ccae2018-02-13 17:15:23 -0500108 * FIB table index, ERSPAN session ID and tunnel type in u32 bit fields:
109 * - The FIB table index the src,dst addresses are in, top 20 bits
110 * - The Session ID for ERSPAN tunnel type and 0 otherwise, next 10 bits
111 * - Tunnel type, bottom 2 bits
Neale Ranns33ce60d2017-12-14 08:51:32 -0800112 */
John Loa43ccae2018-02-13 17:15:23 -0500113 u32 gtk_fidx_ssid_type;
Neale Ranns33ce60d2017-12-14 08:51:32 -0800114} __attribute__ ((packed)) gre_tunnel_key4_t;
115
116/**
117 * @brief Key for a IPv6 GRE Tunnel
118 * We use a different type so that the V4 key hash is as small as possible
119 */
120typedef struct gre_tunnel_key6_t_
121{
122 /**
123 * Source and destination IP addresses
124 */
125 ip6_address_t gtk_src;
126 ip6_address_t gtk_dst;
127
128 /**
John Loa43ccae2018-02-13 17:15:23 -0500129 * FIB table index, ERSPAN session ID and tunnel type in u32 bit fields:
130 * - The FIB table index the src,dst addresses are in, top 20 bits
131 * - The Session ID for ERSPAN tunnel type and 0 otherwise, next 10 bits
132 * - Tunnel type, bottom 2 bits
Neale Ranns33ce60d2017-12-14 08:51:32 -0800133 */
John Loa43ccae2018-02-13 17:15:23 -0500134 u32 gtk_fidx_ssid_type;
Neale Ranns33ce60d2017-12-14 08:51:32 -0800135} __attribute__ ((packed)) gre_tunnel_key6_t;
136
John Loa43ccae2018-02-13 17:15:23 -0500137#define GTK_FIB_INDEX_SHIFT 12
138#define GTK_FIB_INDEX_MASK 0xfffff000
139#define GTK_TYPE_SHIFT 0
140#define GTK_TYPE_MASK 0x3
141#define GTK_SESSION_ID_SHIFT 2
142#define GTK_SESSION_ID_MASK 0xffc
143#define GTK_SESSION_ID_MAX (GTK_SESSION_ID_MASK >> GTK_SESSION_ID_SHIFT)
144
Neale Ranns33ce60d2017-12-14 08:51:32 -0800145/**
146 * Union of the two possible key types
147 */
148typedef union gre_tunnel_key_t_
149{
150 gre_tunnel_key4_t gtk_v4;
151 gre_tunnel_key6_t gtk_v6;
152} gre_tunnel_key_t;
153
154/**
John Loa43ccae2018-02-13 17:15:23 -0500155 * Used for GRE header seq number generation for ERSPAN encap
156 */
157typedef struct
158{
159 u32 seq_num;
160 u32 ref_count;
161} gre_sn_t;
162
163/**
164 * Hash key for GRE header seq number generation for ERSPAN encap
165 */
166typedef struct
167{
168 ip46_address_t src;
169 ip46_address_t dst;
170 u32 fib_index;
171} gre_sn_key_t;
172
173/**
Neale Ranns177bbdc2016-11-15 09:46:51 +0000174 * @brief A representation of a GRE tunnel
175 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530176typedef struct
177{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100178 /**
Dave Baracheb987d32018-05-03 08:26:39 -0400179 * Required for pool_get_aligned
180 */
181 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
182
183 /**
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100184 * Linkage into the FIB object graph
185 */
186 fib_node_t node;
187
188 /**
Neale Ranns33ce60d2017-12-14 08:51:32 -0800189 * The hash table's key stored in separate memory since the tunnel_t
190 * memory can realloc.
191 */
192 gre_tunnel_key_t *key;
193
194 /**
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100195 * The tunnel's source/local address
196 */
Ciara Loftus7eac9162016-09-30 15:47:03 +0100197 ip46_address_t tunnel_src;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100198 /**
199 * The tunnel's destination/remote address
200 */
Ciara Loftus7eac9162016-09-30 15:47:03 +0100201 fib_prefix_t tunnel_dst;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100202 /**
203 * The FIB in which the src.dst address are present
204 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205 u32 outer_fib_index;
206 u32 hw_if_index;
Chris Luke27fe48f2016-04-28 13:44:38 -0400207 u32 sw_if_index;
Neale Ranns177bbdc2016-11-15 09:46:51 +0000208 gre_tunnel_type_t type;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100209
210 /**
211 * The FIB entry sourced by the tunnel for its destination prefix
212 */
213 fib_node_index_t fib_entry_index;
214
215 /**
216 * The tunnel is a child of the FIB entry for its desintion. This is
217 * so it receives updates when the forwarding information for that entry
218 * changes.
219 * The tunnels sibling index on the FIB entry's dependency list.
220 */
221 u32 sibling_index;
222
223 /**
Neale Rannsb80c5362016-10-08 13:03:40 +0100224 * an L2 tunnel always rquires an L2 midchain. cache here for DP.
225 */
226 adj_index_t l2_adj_index;
John Loa43ccae2018-02-13 17:15:23 -0500227
228 /**
229 * ERSPAN type 2 session ID, least significant 10 bits of u16
230 */
231 u16 session_id;
232
233 /**
234 * GRE header sequence number (SN) used for ERSPAN type 2 header, must be
235 * bumped automically to be thread safe. As multiple GRE tunnels are created
236 * for the same fib-idx/DIP/SIP with different ERSPAN session number, they all
237 * share the same SN which is kept per FIB/DIP/SIP, as specified by RFC2890.
238 */
239 gre_sn_t *gre_sn;
240
241
242 u32 dev_instance; /* Real device instance in tunnel vector */
243 u32 user_instance; /* Instance name being shown to user */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244} gre_tunnel_t;
245
John Loa43ccae2018-02-13 17:15:23 -0500246typedef struct
247{
248 u8 next_index;
249 u8 tunnel_type;
250} next_info_t;
251
Neale Ranns177bbdc2016-11-15 09:46:51 +0000252/**
253 * @brief GRE related global data
254 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530255typedef struct
256{
Neale Ranns177bbdc2016-11-15 09:46:51 +0000257 /**
258 * pool of tunnel instances
259 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260 gre_tunnel_t *tunnels;
261
Neale Ranns177bbdc2016-11-15 09:46:51 +0000262 /**
263 * GRE payload protocol registrations
264 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530265 gre_protocol_info_t *protocol_infos;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700266
Neale Ranns177bbdc2016-11-15 09:46:51 +0000267 /**
268 * Hash tables mapping name/protocol to protocol info index.
269 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530270 uword *protocol_info_by_name, *protocol_info_by_protocol;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100271
Neale Ranns177bbdc2016-11-15 09:46:51 +0000272 /**
John Loa43ccae2018-02-13 17:15:23 -0500273 * Hash mapping to tunnels with ipv4 src/dst addr
Neale Ranns177bbdc2016-11-15 09:46:51 +0000274 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530275 uword *tunnel_by_key4;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100276
277 /**
John Loa43ccae2018-02-13 17:15:23 -0500278 * Hash mapping to tunnels with ipv6 src/dst addr
Neale Ranns33ce60d2017-12-14 08:51:32 -0800279 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530280 uword *tunnel_by_key6;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700281
Neale Ranns177bbdc2016-11-15 09:46:51 +0000282 /**
John Loa43ccae2018-02-13 17:15:23 -0500283 * Hash mapping tunnel src/dst addr and fib-idx to sequence number
Neale Ranns177bbdc2016-11-15 09:46:51 +0000284 */
John Loa43ccae2018-02-13 17:15:23 -0500285 uword *seq_num_by_key;
Chris Luke27fe48f2016-04-28 13:44:38 -0400286
Neale Ranns177bbdc2016-11-15 09:46:51 +0000287 /**
288 * Mapping from sw_if_index to tunnel index
289 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530290 u32 *tunnel_index_by_sw_if_index;
Chris Luke27fe48f2016-04-28 13:44:38 -0400291
Damjan Marion63d5bae2017-04-04 01:28:26 +0200292 /* Sparse vector mapping gre protocol in network byte order
293 to next index. */
John Loa43ccae2018-02-13 17:15:23 -0500294 next_info_t *next_by_protocol;
Damjan Marion63d5bae2017-04-04 01:28:26 +0200295
Ed Warnickecb9cada2015-12-08 15:45:58 -0700296 /* convenience */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530297 vlib_main_t *vlib_main;
298 vnet_main_t *vnet_main;
John Loa43ccae2018-02-13 17:15:23 -0500299
300 /* Record used instances */
301 uword *instance_used;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700302} gre_main_t;
303
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100304/**
305 * @brief IPv4 and GRE header.
Neale Ranns177bbdc2016-11-15 09:46:51 +0000306 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530307/* *INDENT-OFF* */
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100308typedef CLIB_PACKED (struct {
309 ip4_header_t ip4;
310 gre_header_t gre;
311}) ip4_and_gre_header_t;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530312/* *INDENT-ON* */
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100313
Ciara Loftus7eac9162016-09-30 15:47:03 +0100314/**
315 * @brief IPv6 and GRE header.
316 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530317/* *INDENT-OFF* */
Ciara Loftus7eac9162016-09-30 15:47:03 +0100318typedef CLIB_PACKED (struct {
319 ip6_header_t ip6;
320 gre_header_t gre;
321}) ip6_and_gre_header_t;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530322/* *INDENT-ON* */
Ciara Loftus7eac9162016-09-30 15:47:03 +0100323
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324always_inline gre_protocol_info_t *
325gre_get_protocol_info (gre_main_t * em, gre_protocol_t protocol)
326{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530327 uword *p = hash_get (em->protocol_info_by_protocol, protocol);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700328 return p ? vec_elt_at_index (em->protocol_infos, p[0]) : 0;
329}
330
Dave Wallace71612d62017-10-24 01:32:41 -0400331extern gre_main_t gre_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530333extern clib_error_t *gre_interface_admin_up_down (vnet_main_t * vnm,
334 u32 hw_if_index, u32 flags);
Neale Rannsb80c5362016-10-08 13:03:40 +0100335
336extern void gre_tunnel_stack (adj_index_t ai);
337extern void gre_update_adj (vnet_main_t * vnm,
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530338 u32 sw_if_index, adj_index_t ai);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700339
340format_function_t format_gre_protocol;
341format_function_t format_gre_header;
342format_function_t format_gre_header_with_length;
343
Ciara Loftus7eac9162016-09-30 15:47:03 +0100344extern vlib_node_registration_t gre4_input_node;
345extern vlib_node_registration_t gre6_input_node;
John Loa43ccae2018-02-13 17:15:23 -0500346extern vlib_node_registration_t gre_encap_node;
Damjan Marionb8abf872016-03-14 20:02:35 +0100347extern vnet_device_class_t gre_device_class;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348
349/* Parse gre protocol as 0xXXXX or protocol name.
350 In either host or network byte order. */
351unformat_function_t unformat_gre_protocol_host_byte_order;
352unformat_function_t unformat_gre_protocol_net_byte_order;
353
354/* Parse gre header. */
355unformat_function_t unformat_gre_header;
356unformat_function_t unformat_pg_gre_header;
357
358void
John Loa43ccae2018-02-13 17:15:23 -0500359gre_register_input_protocol (vlib_main_t * vm, gre_protocol_t protocol,
360 u32 node_index, gre_tunnel_type_t tunnel_type);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361
362/* manually added to the interface output node in gre.c */
363#define GRE_OUTPUT_NEXT_LOOKUP 1
364
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530365typedef struct
366{
Chris Luke27fe48f2016-04-28 13:44:38 -0400367 u8 is_add;
John Loa43ccae2018-02-13 17:15:23 -0500368 u8 tunnel_type;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100369 u8 is_ipv6;
John Loa43ccae2018-02-13 17:15:23 -0500370 u32 instance;
371 ip46_address_t src, dst;
Hongjun Ni11bfc2f2016-07-22 18:19:19 +0800372 u32 outer_fib_id;
John Loa43ccae2018-02-13 17:15:23 -0500373 u16 session_id;
Chris Luke27fe48f2016-04-28 13:44:38 -0400374} vnet_gre_add_del_tunnel_args_t;
375
376int vnet_gre_add_del_tunnel
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530377 (vnet_gre_add_del_tunnel_args_t * a, u32 * sw_if_indexp);
Chris Luke27fe48f2016-04-28 13:44:38 -0400378
Neale Ranns33ce60d2017-12-14 08:51:32 -0800379static inline void
John Loa43ccae2018-02-13 17:15:23 -0500380gre_mk_key4 (ip4_address_t src,
381 ip4_address_t dst,
382 u32 fib_index, u8 ttype, u16 session_id, gre_tunnel_key4_t * key)
Neale Ranns33ce60d2017-12-14 08:51:32 -0800383{
John Loa43ccae2018-02-13 17:15:23 -0500384 key->gtk_src = src;
385 key->gtk_dst = dst;
386 key->gtk_fidx_ssid_type = ttype |
387 (fib_index << GTK_FIB_INDEX_SHIFT) | (session_id << GTK_SESSION_ID_SHIFT);
Neale Ranns33ce60d2017-12-14 08:51:32 -0800388}
389
390static inline int
391gre_match_key4 (const gre_tunnel_key4_t * key1,
392 const gre_tunnel_key4_t * key2)
393{
394 return ((key1->gtk_as_u64 == key2->gtk_as_u64) &&
John Loa43ccae2018-02-13 17:15:23 -0500395 (key1->gtk_fidx_ssid_type == key2->gtk_fidx_ssid_type));
Neale Ranns33ce60d2017-12-14 08:51:32 -0800396}
397
398static inline void
399gre_mk_key6 (const ip6_address_t * src,
400 const ip6_address_t * dst,
John Loa43ccae2018-02-13 17:15:23 -0500401 u32 fib_index, u8 ttype, u16 session_id, gre_tunnel_key6_t * key)
Neale Ranns33ce60d2017-12-14 08:51:32 -0800402{
403 key->gtk_src = *src;
404 key->gtk_dst = *dst;
John Loa43ccae2018-02-13 17:15:23 -0500405 key->gtk_fidx_ssid_type = ttype |
406 (fib_index << GTK_FIB_INDEX_SHIFT) | (session_id << GTK_SESSION_ID_SHIFT);
Neale Ranns33ce60d2017-12-14 08:51:32 -0800407}
408
409static inline int
410gre_match_key6 (const gre_tunnel_key6_t * key1,
411 const gre_tunnel_key6_t * key2)
412{
413 return ((key1->gtk_src.as_u64[0] == key2->gtk_src.as_u64[0]) &&
414 (key1->gtk_src.as_u64[1] == key2->gtk_src.as_u64[1]) &&
415 (key1->gtk_dst.as_u64[0] == key2->gtk_dst.as_u64[0]) &&
416 (key1->gtk_dst.as_u64[1] == key2->gtk_dst.as_u64[1]) &&
John Loa43ccae2018-02-13 17:15:23 -0500417 (key1->gtk_fidx_ssid_type == key2->gtk_fidx_ssid_type));
418}
419
420static inline void
421gre_mk_sn_key (const gre_tunnel_t * gt, gre_sn_key_t * key)
422{
423 key->src = gt->tunnel_src;
424 key->dst = gt->tunnel_dst.fp_addr;
425 key->fib_index = gt->outer_fib_index;
Neale Ranns33ce60d2017-12-14 08:51:32 -0800426}
427
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428#endif /* included_gre_h */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530429
430/*
431 * fd.io coding-style-patch-verification: ON
432 *
433 * Local Variables:
434 * eval: (c-set-style "gnu")
435 * End:
436 */