blob: b6544b9b7373a920216e29ab18bcd0f35dbce40d [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>
24#include <vnet/ip/ip4.h>
25#include <vnet/ip/ip4_packet.h>
26#include <vnet/pg/pg.h>
27#include <vnet/ip/format.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010028#include <vnet/adj/adj_types.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;
Ed Warnickecb9cada2015-12-08 15:45:58 -070031
32typedef enum {
33#define gre_error(n,s) GRE_ERROR_##n,
34#include <vnet/gre/error.def>
35#undef gre_error
36 GRE_N_ERROR,
37} gre_error_t;
38
Neale Ranns177bbdc2016-11-15 09:46:51 +000039/**
40 * A GRE payload protocol registration
41 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070042typedef struct {
Neale Ranns177bbdc2016-11-15 09:46:51 +000043 /** Name (a c string). */
Ed Warnickecb9cada2015-12-08 15:45:58 -070044 char * name;
45
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 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070081typedef struct {
Neale Ranns0bfe5d82016-08-25 15:29:12 +010082 /**
83 * Linkage into the FIB object graph
84 */
85 fib_node_t node;
86
87 /**
88 * The tunnel's source/local address
89 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070090 ip4_address_t tunnel_src;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010091 /**
92 * The tunnel's destination/remote address
93 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070094 ip4_address_t tunnel_dst;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010095 /**
96 * The FIB in which the src.dst address are present
97 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070098 u32 outer_fib_index;
99 u32 hw_if_index;
Chris Luke27fe48f2016-04-28 13:44:38 -0400100 u32 sw_if_index;
Neale Ranns177bbdc2016-11-15 09:46:51 +0000101 gre_tunnel_type_t type;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100102
103 /**
104 * The FIB entry sourced by the tunnel for its destination prefix
105 */
106 fib_node_index_t fib_entry_index;
107
108 /**
109 * The tunnel is a child of the FIB entry for its desintion. This is
110 * so it receives updates when the forwarding information for that entry
111 * changes.
112 * The tunnels sibling index on the FIB entry's dependency list.
113 */
114 u32 sibling_index;
115
116 /**
Neale Ranns5e575b12016-10-03 09:40:25 +0100117 * on a L2 tunnel this is the VLIB arc from the L2-tx to the l2-midchain
118 */
119 u32 l2_tx_arc;
Neale Rannsb80c5362016-10-08 13:03:40 +0100120
121 /**
122 * an L2 tunnel always rquires an L2 midchain. cache here for DP.
123 */
124 adj_index_t l2_adj_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125} gre_tunnel_t;
126
Neale Ranns177bbdc2016-11-15 09:46:51 +0000127/**
128 * @brief GRE related global data
129 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130typedef struct {
Neale Ranns177bbdc2016-11-15 09:46:51 +0000131 /**
132 * pool of tunnel instances
133 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134 gre_tunnel_t *tunnels;
135
Neale Ranns177bbdc2016-11-15 09:46:51 +0000136 /**
137 * GRE payload protocol registrations
138 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139 gre_protocol_info_t * protocol_infos;
140
Neale Ranns177bbdc2016-11-15 09:46:51 +0000141 /**
142 * Hash tables mapping name/protocol to protocol info index.
143 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144 uword * protocol_info_by_name, * protocol_info_by_protocol;
Neale Ranns177bbdc2016-11-15 09:46:51 +0000145 /**
146 * Hash mapping src/dst addr pair to tunnel
147 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148 uword * tunnel_by_key;
149
Neale Ranns177bbdc2016-11-15 09:46:51 +0000150 /**
151 * Free vlib hw_if_indices.
152 * A free list per-tunnel type since the interfaces ctreated are fo different
153 * types and we cannot change the type.
154 */
155 u32 * free_gre_tunnel_hw_if_indices[GRE_TUNNEL_N_TYPES];
Chris Luke27fe48f2016-04-28 13:44:38 -0400156
Neale Ranns177bbdc2016-11-15 09:46:51 +0000157 /**
158 * Mapping from sw_if_index to tunnel index
159 */
Chris Luke27fe48f2016-04-28 13:44:38 -0400160 u32 * tunnel_index_by_sw_if_index;
161
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162 /* convenience */
163 vlib_main_t * vlib_main;
164 vnet_main_t * vnet_main;
165} gre_main_t;
166
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100167/**
168 * @brief IPv4 and GRE header.
Neale Ranns177bbdc2016-11-15 09:46:51 +0000169 */
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100170typedef CLIB_PACKED (struct {
171 ip4_header_t ip4;
172 gre_header_t gre;
173}) ip4_and_gre_header_t;
174
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175always_inline gre_protocol_info_t *
176gre_get_protocol_info (gre_main_t * em, gre_protocol_t protocol)
177{
178 uword * p = hash_get (em->protocol_info_by_protocol, protocol);
179 return p ? vec_elt_at_index (em->protocol_infos, p[0]) : 0;
180}
181
182gre_main_t gre_main;
183
184/* Register given node index to take input for given gre type. */
185void
186gre_register_input_type (vlib_main_t * vm,
187 gre_protocol_t protocol,
188 u32 node_index);
189
Neale Rannsb80c5362016-10-08 13:03:40 +0100190extern clib_error_t * gre_interface_admin_up_down (vnet_main_t * vnm,
191 u32 hw_if_index,
192 u32 flags);
193
194extern void gre_tunnel_stack (adj_index_t ai);
195extern void gre_update_adj (vnet_main_t * vnm,
Neale Ranns177bbdc2016-11-15 09:46:51 +0000196 u32 sw_if_index,
197 adj_index_t ai);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198
199format_function_t format_gre_protocol;
200format_function_t format_gre_header;
201format_function_t format_gre_header_with_length;
202
Jean-Mickael Guerin8941ec22016-03-04 14:14:21 +0100203extern vlib_node_registration_t gre_input_node;
Damjan Marionb8abf872016-03-14 20:02:35 +0100204extern vnet_device_class_t gre_device_class;
Neale Ranns177bbdc2016-11-15 09:46:51 +0000205extern vnet_device_class_t gre_device_teb_class;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206
207/* Parse gre protocol as 0xXXXX or protocol name.
208 In either host or network byte order. */
209unformat_function_t unformat_gre_protocol_host_byte_order;
210unformat_function_t unformat_gre_protocol_net_byte_order;
211
212/* Parse gre header. */
213unformat_function_t unformat_gre_header;
214unformat_function_t unformat_pg_gre_header;
215
216void
217gre_register_input_protocol (vlib_main_t * vm,
218 gre_protocol_t protocol,
219 u32 node_index);
220
221/* manually added to the interface output node in gre.c */
222#define GRE_OUTPUT_NEXT_LOOKUP 1
223
Chris Luke27fe48f2016-04-28 13:44:38 -0400224typedef struct {
225 u8 is_add;
226
227 ip4_address_t src, dst;
Hongjun Ni11bfc2f2016-07-22 18:19:19 +0800228 u32 outer_fib_id;
David Hothama8cd3092016-09-19 09:55:07 -0700229 u8 teb;
Chris Luke27fe48f2016-04-28 13:44:38 -0400230} vnet_gre_add_del_tunnel_args_t;
231
232int vnet_gre_add_del_tunnel
233 (vnet_gre_add_del_tunnel_args_t *a, u32 * sw_if_indexp);
234
Ed Warnickecb9cada2015-12-08 15:45:58 -0700235#endif /* included_gre_h */