blob: 83bab76f087c30f91db89d8f48fae472e603d340 [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/**
39 * A GRE payload protocol registration
40 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +053041typedef struct
42{
Neale Ranns177bbdc2016-11-15 09:46:51 +000043 /** Name (a c string). */
Swarup Nayak9ff647a2017-11-27 10:27:43 +053044 char *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -070045
Neale Ranns177bbdc2016-11-15 09:46:51 +000046 /** GRE protocol type in host byte order. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070047 gre_protocol_t protocol;
48
Neale Ranns177bbdc2016-11-15 09:46:51 +000049 /** Node which handles this type. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070050 u32 node_index;
51
Neale Ranns177bbdc2016-11-15 09:46:51 +000052 /** Next index for this type. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070053 u32 next_index;
54} gre_protocol_info_t;
55
Neale Ranns177bbdc2016-11-15 09:46:51 +000056/**
57 * @brief The GRE tunnel type
58 */
59typedef enum gre_tunnel_tyoe_t_
60{
61 /**
62 * L3 GRE (i.e. this tunnel is in L3 mode)
63 */
64 GRE_TUNNEL_TYPE_L3,
65 /**
66 * Transparent Ethernet Bridging - the tunnel is in L2 mode
67 */
68 GRE_TUNNEL_TYPE_TEB,
69} gre_tunnel_type_t;
70
71#define GRE_TUNNEL_TYPE_NAMES { \
72 [GRE_TUNNEL_TYPE_L3] = "L3", \
73 [GRE_TUNNEL_TYPE_TEB] = "TEB", \
74}
75
76#define GRE_TUNNEL_N_TYPES ((gre_tunnel_type_t)GRE_TUNNEL_TYPE_TEB+1)
77
78/**
Neale Ranns33ce60d2017-12-14 08:51:32 -080079 * @brief Key for a IPv4 GRE Tunnel
80 */
81typedef struct gre_tunnel_key4_t_
82{
83 /**
84 * Source and destination IP addresses
85 */
86 union
87 {
88 struct
89 {
90 ip4_address_t gtk_src;
91 ip4_address_t gtk_dst;
92 };
93 u64 gtk_as_u64;
94 };
95
96 /**
97 * The FIB table the src,dst addresses are in.
98 * tunnels with the same IP addresses in different FIBs are not
99 * the same tunnel
100 */
101 u32 gtk_fib_index;
102} __attribute__ ((packed)) gre_tunnel_key4_t;
103
104/**
105 * @brief Key for a IPv6 GRE Tunnel
106 * We use a different type so that the V4 key hash is as small as possible
107 */
108typedef struct gre_tunnel_key6_t_
109{
110 /**
111 * Source and destination IP addresses
112 */
113 ip6_address_t gtk_src;
114 ip6_address_t gtk_dst;
115
116 /**
117 * The FIB table the src,dst addresses are in.
118 * tunnels with the same IP addresses in different FIBs are not
119 * the same tunnel
120 */
121 u32 gtk_fib_index;
122} __attribute__ ((packed)) gre_tunnel_key6_t;
123
124/**
125 * Union of the two possible key types
126 */
127typedef union gre_tunnel_key_t_
128{
129 gre_tunnel_key4_t gtk_v4;
130 gre_tunnel_key6_t gtk_v6;
131} gre_tunnel_key_t;
132
133/**
Neale Ranns177bbdc2016-11-15 09:46:51 +0000134 * @brief A representation of a GRE tunnel
135 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530136typedef struct
137{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100138 /**
139 * Linkage into the FIB object graph
140 */
141 fib_node_t node;
142
143 /**
Neale Ranns33ce60d2017-12-14 08:51:32 -0800144 * The hash table's key stored in separate memory since the tunnel_t
145 * memory can realloc.
146 */
147 gre_tunnel_key_t *key;
148
149 /**
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100150 * The tunnel's source/local address
151 */
Ciara Loftus7eac9162016-09-30 15:47:03 +0100152 ip46_address_t tunnel_src;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100153 /**
154 * The tunnel's destination/remote address
155 */
Ciara Loftus7eac9162016-09-30 15:47:03 +0100156 fib_prefix_t tunnel_dst;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100157 /**
158 * The FIB in which the src.dst address are present
159 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160 u32 outer_fib_index;
161 u32 hw_if_index;
Chris Luke27fe48f2016-04-28 13:44:38 -0400162 u32 sw_if_index;
Neale Ranns177bbdc2016-11-15 09:46:51 +0000163 gre_tunnel_type_t type;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100164
165 /**
166 * The FIB entry sourced by the tunnel for its destination prefix
167 */
168 fib_node_index_t fib_entry_index;
169
170 /**
171 * The tunnel is a child of the FIB entry for its desintion. This is
172 * so it receives updates when the forwarding information for that entry
173 * changes.
174 * The tunnels sibling index on the FIB entry's dependency list.
175 */
176 u32 sibling_index;
177
178 /**
Neale Ranns5e575b12016-10-03 09:40:25 +0100179 * on a L2 tunnel this is the VLIB arc from the L2-tx to the l2-midchain
180 */
181 u32 l2_tx_arc;
Neale Rannsb80c5362016-10-08 13:03:40 +0100182
183 /**
184 * an L2 tunnel always rquires an L2 midchain. cache here for DP.
185 */
186 adj_index_t l2_adj_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187} gre_tunnel_t;
188
Neale Ranns177bbdc2016-11-15 09:46:51 +0000189/**
190 * @brief GRE related global data
191 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530192typedef struct
193{
Neale Ranns177bbdc2016-11-15 09:46:51 +0000194 /**
195 * pool of tunnel instances
196 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700197 gre_tunnel_t *tunnels;
198
Neale Ranns177bbdc2016-11-15 09:46:51 +0000199 /**
200 * GRE payload protocol registrations
201 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530202 gre_protocol_info_t *protocol_infos;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203
Neale Ranns177bbdc2016-11-15 09:46:51 +0000204 /**
205 * Hash tables mapping name/protocol to protocol info index.
206 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530207 uword *protocol_info_by_name, *protocol_info_by_protocol;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100208
Neale Ranns177bbdc2016-11-15 09:46:51 +0000209 /**
Ciara Loftus7eac9162016-09-30 15:47:03 +0100210 * Hash mapping ipv4 src/dst addr pair to tunnel
Neale Ranns177bbdc2016-11-15 09:46:51 +0000211 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530212 uword *tunnel_by_key4;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100213
214 /**
Neale Ranns33ce60d2017-12-14 08:51:32 -0800215 * Hash mapping ipv6 src/dst addr pair to tunnel
216 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530217 uword *tunnel_by_key6;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218
Neale Ranns177bbdc2016-11-15 09:46:51 +0000219 /**
220 * Free vlib hw_if_indices.
221 * A free list per-tunnel type since the interfaces ctreated are fo different
222 * types and we cannot change the type.
223 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530224 u32 *free_gre_tunnel_hw_if_indices[GRE_TUNNEL_N_TYPES];
Chris Luke27fe48f2016-04-28 13:44:38 -0400225
Neale Ranns177bbdc2016-11-15 09:46:51 +0000226 /**
227 * Mapping from sw_if_index to tunnel index
228 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530229 u32 *tunnel_index_by_sw_if_index;
Chris Luke27fe48f2016-04-28 13:44:38 -0400230
Damjan Marion63d5bae2017-04-04 01:28:26 +0200231 /* Sparse vector mapping gre protocol in network byte order
232 to next index. */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530233 u16 *next_by_protocol;
Damjan Marion63d5bae2017-04-04 01:28:26 +0200234
Ed Warnickecb9cada2015-12-08 15:45:58 -0700235 /* convenience */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530236 vlib_main_t *vlib_main;
237 vnet_main_t *vnet_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238} gre_main_t;
239
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100240/**
241 * @brief IPv4 and GRE header.
Neale Ranns177bbdc2016-11-15 09:46:51 +0000242 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530243/* *INDENT-OFF* */
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100244typedef CLIB_PACKED (struct {
245 ip4_header_t ip4;
246 gre_header_t gre;
247}) ip4_and_gre_header_t;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530248/* *INDENT-ON* */
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100249
Ciara Loftus7eac9162016-09-30 15:47:03 +0100250/**
251 * @brief IPv6 and GRE header.
252 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530253/* *INDENT-OFF* */
Ciara Loftus7eac9162016-09-30 15:47:03 +0100254typedef CLIB_PACKED (struct {
255 ip6_header_t ip6;
256 gre_header_t gre;
257}) ip6_and_gre_header_t;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530258/* *INDENT-ON* */
Ciara Loftus7eac9162016-09-30 15:47:03 +0100259
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260always_inline gre_protocol_info_t *
261gre_get_protocol_info (gre_main_t * em, gre_protocol_t protocol)
262{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530263 uword *p = hash_get (em->protocol_info_by_protocol, protocol);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264 return p ? vec_elt_at_index (em->protocol_infos, p[0]) : 0;
265}
266
Dave Wallace71612d62017-10-24 01:32:41 -0400267extern gre_main_t gre_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700268
269/* Register given node index to take input for given gre type. */
270void
271gre_register_input_type (vlib_main_t * vm,
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530272 gre_protocol_t protocol, u32 node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700273
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530274extern clib_error_t *gre_interface_admin_up_down (vnet_main_t * vnm,
275 u32 hw_if_index, u32 flags);
Neale Rannsb80c5362016-10-08 13:03:40 +0100276
277extern void gre_tunnel_stack (adj_index_t ai);
278extern void gre_update_adj (vnet_main_t * vnm,
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530279 u32 sw_if_index, adj_index_t ai);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280
281format_function_t format_gre_protocol;
282format_function_t format_gre_header;
283format_function_t format_gre_header_with_length;
284
Ciara Loftus7eac9162016-09-30 15:47:03 +0100285extern vlib_node_registration_t gre4_input_node;
286extern vlib_node_registration_t gre6_input_node;
Damjan Marionb8abf872016-03-14 20:02:35 +0100287extern vnet_device_class_t gre_device_class;
Neale Ranns177bbdc2016-11-15 09:46:51 +0000288extern vnet_device_class_t gre_device_teb_class;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700289
290/* Parse gre protocol as 0xXXXX or protocol name.
291 In either host or network byte order. */
292unformat_function_t unformat_gre_protocol_host_byte_order;
293unformat_function_t unformat_gre_protocol_net_byte_order;
294
295/* Parse gre header. */
296unformat_function_t unformat_gre_header;
297unformat_function_t unformat_pg_gre_header;
298
299void
300gre_register_input_protocol (vlib_main_t * vm,
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530301 gre_protocol_t protocol, u32 node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700302
303/* manually added to the interface output node in gre.c */
304#define GRE_OUTPUT_NEXT_LOOKUP 1
305
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530306typedef struct
307{
Chris Luke27fe48f2016-04-28 13:44:38 -0400308 u8 is_add;
309
Ciara Loftus7eac9162016-09-30 15:47:03 +0100310 ip46_address_t src, dst;
311 u8 is_ipv6;
Hongjun Ni11bfc2f2016-07-22 18:19:19 +0800312 u32 outer_fib_id;
David Hothama8cd3092016-09-19 09:55:07 -0700313 u8 teb;
Chris Luke27fe48f2016-04-28 13:44:38 -0400314} vnet_gre_add_del_tunnel_args_t;
315
316int vnet_gre_add_del_tunnel
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530317 (vnet_gre_add_del_tunnel_args_t * a, u32 * sw_if_indexp);
Chris Luke27fe48f2016-04-28 13:44:38 -0400318
Neale Ranns33ce60d2017-12-14 08:51:32 -0800319static inline void
320gre_mk_key4 (const ip4_address_t * src,
321 const ip4_address_t * dst,
322 u32 fib_index, gre_tunnel_key4_t * key)
323{
324 key->gtk_src = *src;
325 key->gtk_dst = *dst;
326 key->gtk_fib_index = fib_index;
327}
328
329static inline int
330gre_match_key4 (const gre_tunnel_key4_t * key1,
331 const gre_tunnel_key4_t * key2)
332{
333 return ((key1->gtk_as_u64 == key2->gtk_as_u64) &&
334 (key1->gtk_fib_index == key2->gtk_fib_index));
335}
336
337static inline void
338gre_mk_key6 (const ip6_address_t * src,
339 const ip6_address_t * dst,
340 u32 fib_index, gre_tunnel_key6_t * key)
341{
342 key->gtk_src = *src;
343 key->gtk_dst = *dst;
344 key->gtk_fib_index = fib_index;
345}
346
347static inline int
348gre_match_key6 (const gre_tunnel_key6_t * key1,
349 const gre_tunnel_key6_t * key2)
350{
351 return ((key1->gtk_src.as_u64[0] == key2->gtk_src.as_u64[0]) &&
352 (key1->gtk_src.as_u64[1] == key2->gtk_src.as_u64[1]) &&
353 (key1->gtk_dst.as_u64[0] == key2->gtk_dst.as_u64[0]) &&
354 (key1->gtk_dst.as_u64[1] == key2->gtk_dst.as_u64[1]) &&
355 (key1->gtk_fib_index == key2->gtk_fib_index));
356}
357
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358#endif /* included_gre_h */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530359
360/*
361 * fd.io coding-style-patch-verification: ON
362 *
363 * Local Variables:
364 * eval: (c-set-style "gnu")
365 * End:
366 */