blob: f597d42d428bfdb88090c2d6d951f9db2b8d937a [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/**
79 * @brief A representation of a GRE tunnel
80 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +053081typedef struct
82{
Neale Ranns0bfe5d82016-08-25 15:29:12 +010083 /**
84 * Linkage into the FIB object graph
85 */
86 fib_node_t node;
87
88 /**
89 * The tunnel's source/local address
90 */
Ciara Loftus7eac9162016-09-30 15:47:03 +010091 ip46_address_t tunnel_src;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010092 /**
93 * The tunnel's destination/remote address
94 */
Ciara Loftus7eac9162016-09-30 15:47:03 +010095 fib_prefix_t tunnel_dst;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010096 /**
97 * The FIB in which the src.dst address are present
98 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070099 u32 outer_fib_index;
100 u32 hw_if_index;
Chris Luke27fe48f2016-04-28 13:44:38 -0400101 u32 sw_if_index;
Neale Ranns177bbdc2016-11-15 09:46:51 +0000102 gre_tunnel_type_t type;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100103
104 /**
105 * The FIB entry sourced by the tunnel for its destination prefix
106 */
107 fib_node_index_t fib_entry_index;
108
109 /**
110 * The tunnel is a child of the FIB entry for its desintion. This is
111 * so it receives updates when the forwarding information for that entry
112 * changes.
113 * The tunnels sibling index on the FIB entry's dependency list.
114 */
115 u32 sibling_index;
116
117 /**
Neale Ranns5e575b12016-10-03 09:40:25 +0100118 * on a L2 tunnel this is the VLIB arc from the L2-tx to the l2-midchain
119 */
120 u32 l2_tx_arc;
Neale Rannsb80c5362016-10-08 13:03:40 +0100121
122 /**
123 * an L2 tunnel always rquires an L2 midchain. cache here for DP.
124 */
125 adj_index_t l2_adj_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126} gre_tunnel_t;
127
Neale Ranns177bbdc2016-11-15 09:46:51 +0000128/**
129 * @brief GRE related global data
130 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530131typedef struct
132{
Neale Ranns177bbdc2016-11-15 09:46:51 +0000133 /**
134 * pool of tunnel instances
135 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136 gre_tunnel_t *tunnels;
137
Neale Ranns177bbdc2016-11-15 09:46:51 +0000138 /**
139 * GRE payload protocol registrations
140 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530141 gre_protocol_info_t *protocol_infos;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142
Neale Ranns177bbdc2016-11-15 09:46:51 +0000143 /**
144 * Hash tables mapping name/protocol to protocol info index.
145 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530146 uword *protocol_info_by_name, *protocol_info_by_protocol;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100147
Neale Ranns177bbdc2016-11-15 09:46:51 +0000148 /**
Ciara Loftus7eac9162016-09-30 15:47:03 +0100149 * Hash mapping ipv4 src/dst addr pair to tunnel
Neale Ranns177bbdc2016-11-15 09:46:51 +0000150 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530151 uword *tunnel_by_key4;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100152
153 /**
154 * Hash mapping ipv6 src/dst addr pair to tunnel
155 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530156 uword *tunnel_by_key6;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157
Neale Ranns177bbdc2016-11-15 09:46:51 +0000158 /**
159 * Free vlib hw_if_indices.
160 * A free list per-tunnel type since the interfaces ctreated are fo different
161 * types and we cannot change the type.
162 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530163 u32 *free_gre_tunnel_hw_if_indices[GRE_TUNNEL_N_TYPES];
Chris Luke27fe48f2016-04-28 13:44:38 -0400164
Neale Ranns177bbdc2016-11-15 09:46:51 +0000165 /**
166 * Mapping from sw_if_index to tunnel index
167 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530168 u32 *tunnel_index_by_sw_if_index;
Chris Luke27fe48f2016-04-28 13:44:38 -0400169
Damjan Marion63d5bae2017-04-04 01:28:26 +0200170 /* Sparse vector mapping gre protocol in network byte order
171 to next index. */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530172 u16 *next_by_protocol;
Damjan Marion63d5bae2017-04-04 01:28:26 +0200173
Ed Warnickecb9cada2015-12-08 15:45:58 -0700174 /* convenience */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530175 vlib_main_t *vlib_main;
176 vnet_main_t *vnet_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177} gre_main_t;
178
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100179/**
180 * @brief IPv4 and GRE header.
Neale Ranns177bbdc2016-11-15 09:46:51 +0000181 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530182/* *INDENT-OFF* */
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100183typedef CLIB_PACKED (struct {
184 ip4_header_t ip4;
185 gre_header_t gre;
186}) ip4_and_gre_header_t;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530187/* *INDENT-ON* */
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100188
Ciara Loftus7eac9162016-09-30 15:47:03 +0100189/**
190 * @brief IPv6 and GRE header.
191 */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530192/* *INDENT-OFF* */
Ciara Loftus7eac9162016-09-30 15:47:03 +0100193typedef CLIB_PACKED (struct {
194 ip6_header_t ip6;
195 gre_header_t gre;
196}) ip6_and_gre_header_t;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530197/* *INDENT-ON* */
Ciara Loftus7eac9162016-09-30 15:47:03 +0100198
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199always_inline gre_protocol_info_t *
200gre_get_protocol_info (gre_main_t * em, gre_protocol_t protocol)
201{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530202 uword *p = hash_get (em->protocol_info_by_protocol, protocol);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203 return p ? vec_elt_at_index (em->protocol_infos, p[0]) : 0;
204}
205
Dave Wallace71612d62017-10-24 01:32:41 -0400206extern gre_main_t gre_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207
208/* Register given node index to take input for given gre type. */
209void
210gre_register_input_type (vlib_main_t * vm,
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530211 gre_protocol_t protocol, u32 node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530213extern clib_error_t *gre_interface_admin_up_down (vnet_main_t * vnm,
214 u32 hw_if_index, u32 flags);
Neale Rannsb80c5362016-10-08 13:03:40 +0100215
216extern void gre_tunnel_stack (adj_index_t ai);
217extern void gre_update_adj (vnet_main_t * vnm,
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530218 u32 sw_if_index, adj_index_t ai);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700219
220format_function_t format_gre_protocol;
221format_function_t format_gre_header;
222format_function_t format_gre_header_with_length;
223
Ciara Loftus7eac9162016-09-30 15:47:03 +0100224extern vlib_node_registration_t gre4_input_node;
225extern vlib_node_registration_t gre6_input_node;
Damjan Marionb8abf872016-03-14 20:02:35 +0100226extern vnet_device_class_t gre_device_class;
Neale Ranns177bbdc2016-11-15 09:46:51 +0000227extern vnet_device_class_t gre_device_teb_class;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228
229/* Parse gre protocol as 0xXXXX or protocol name.
230 In either host or network byte order. */
231unformat_function_t unformat_gre_protocol_host_byte_order;
232unformat_function_t unformat_gre_protocol_net_byte_order;
233
234/* Parse gre header. */
235unformat_function_t unformat_gre_header;
236unformat_function_t unformat_pg_gre_header;
237
238void
239gre_register_input_protocol (vlib_main_t * vm,
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530240 gre_protocol_t protocol, u32 node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241
242/* manually added to the interface output node in gre.c */
243#define GRE_OUTPUT_NEXT_LOOKUP 1
244
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530245typedef struct
246{
Chris Luke27fe48f2016-04-28 13:44:38 -0400247 u8 is_add;
248
Ciara Loftus7eac9162016-09-30 15:47:03 +0100249 ip46_address_t src, dst;
250 u8 is_ipv6;
Hongjun Ni11bfc2f2016-07-22 18:19:19 +0800251 u32 outer_fib_id;
David Hothama8cd3092016-09-19 09:55:07 -0700252 u8 teb;
Chris Luke27fe48f2016-04-28 13:44:38 -0400253} vnet_gre_add_del_tunnel_args_t;
254
255int vnet_gre_add_del_tunnel
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530256 (vnet_gre_add_del_tunnel_args_t * a, u32 * sw_if_indexp);
Chris Luke27fe48f2016-04-28 13:44:38 -0400257
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258#endif /* included_gre_h */
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530259
260/*
261 * fd.io coding-style-patch-verification: ON
262 *
263 * Local Variables:
264 * eval: (c-set-style "gnu")
265 * End:
266 */