blob: ad3e025a0ff001056f10a7687afa2038e3309f64 [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
30typedef enum {
31#define gre_error(n,s) GRE_ERROR_##n,
32#include <vnet/gre/error.def>
33#undef gre_error
34 GRE_N_ERROR,
35} gre_error_t;
36
Neale Ranns177bbdc2016-11-15 09:46:51 +000037/**
38 * A GRE payload protocol registration
39 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070040typedef struct {
Neale Ranns177bbdc2016-11-15 09:46:51 +000041 /** Name (a c string). */
Ed Warnickecb9cada2015-12-08 15:45:58 -070042 char * name;
43
Neale Ranns177bbdc2016-11-15 09:46:51 +000044 /** GRE protocol type in host byte order. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070045 gre_protocol_t protocol;
46
Neale Ranns177bbdc2016-11-15 09:46:51 +000047 /** Node which handles this type. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070048 u32 node_index;
49
Neale Ranns177bbdc2016-11-15 09:46:51 +000050 /** Next index for this type. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070051 u32 next_index;
52} gre_protocol_info_t;
53
Neale Ranns177bbdc2016-11-15 09:46:51 +000054/**
55 * @brief The GRE tunnel type
56 */
57typedef enum gre_tunnel_tyoe_t_
58{
59 /**
60 * L3 GRE (i.e. this tunnel is in L3 mode)
61 */
62 GRE_TUNNEL_TYPE_L3,
63 /**
64 * Transparent Ethernet Bridging - the tunnel is in L2 mode
65 */
66 GRE_TUNNEL_TYPE_TEB,
67} gre_tunnel_type_t;
68
69#define GRE_TUNNEL_TYPE_NAMES { \
70 [GRE_TUNNEL_TYPE_L3] = "L3", \
71 [GRE_TUNNEL_TYPE_TEB] = "TEB", \
72}
73
74#define GRE_TUNNEL_N_TYPES ((gre_tunnel_type_t)GRE_TUNNEL_TYPE_TEB+1)
75
76/**
77 * @brief A representation of a GRE tunnel
78 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070079typedef struct {
Neale Ranns0bfe5d82016-08-25 15:29:12 +010080 /**
81 * Linkage into the FIB object graph
82 */
83 fib_node_t node;
84
85 /**
86 * The tunnel's source/local address
87 */
Ciara Loftus7eac9162016-09-30 15:47:03 +010088 ip46_address_t tunnel_src;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010089 /**
90 * The tunnel's destination/remote address
91 */
Ciara Loftus7eac9162016-09-30 15:47:03 +010092 fib_prefix_t tunnel_dst;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010093 /**
94 * The FIB in which the src.dst address are present
95 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070096 u32 outer_fib_index;
97 u32 hw_if_index;
Chris Luke27fe48f2016-04-28 13:44:38 -040098 u32 sw_if_index;
Neale Ranns177bbdc2016-11-15 09:46:51 +000099 gre_tunnel_type_t type;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100100
101 /**
102 * The FIB entry sourced by the tunnel for its destination prefix
103 */
104 fib_node_index_t fib_entry_index;
105
106 /**
107 * The tunnel is a child of the FIB entry for its desintion. This is
108 * so it receives updates when the forwarding information for that entry
109 * changes.
110 * The tunnels sibling index on the FIB entry's dependency list.
111 */
112 u32 sibling_index;
113
114 /**
Neale Ranns5e575b12016-10-03 09:40:25 +0100115 * on a L2 tunnel this is the VLIB arc from the L2-tx to the l2-midchain
116 */
117 u32 l2_tx_arc;
Neale Rannsb80c5362016-10-08 13:03:40 +0100118
119 /**
120 * an L2 tunnel always rquires an L2 midchain. cache here for DP.
121 */
122 adj_index_t l2_adj_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123} gre_tunnel_t;
124
Neale Ranns177bbdc2016-11-15 09:46:51 +0000125/**
126 * @brief GRE related global data
127 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700128typedef struct {
Neale Ranns177bbdc2016-11-15 09:46:51 +0000129 /**
130 * pool of tunnel instances
131 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132 gre_tunnel_t *tunnels;
133
Neale Ranns177bbdc2016-11-15 09:46:51 +0000134 /**
135 * GRE payload protocol registrations
136 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137 gre_protocol_info_t * protocol_infos;
138
Neale Ranns177bbdc2016-11-15 09:46:51 +0000139 /**
140 * Hash tables mapping name/protocol to protocol info index.
141 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142 uword * protocol_info_by_name, * protocol_info_by_protocol;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100143
Neale Ranns177bbdc2016-11-15 09:46:51 +0000144 /**
Ciara Loftus7eac9162016-09-30 15:47:03 +0100145 * Hash mapping ipv4 src/dst addr pair to tunnel
Neale Ranns177bbdc2016-11-15 09:46:51 +0000146 */
Ciara Loftus7eac9162016-09-30 15:47:03 +0100147 uword * tunnel_by_key4;
148
149 /**
150 * Hash mapping ipv6 src/dst addr pair to tunnel
151 */
152 uword * tunnel_by_key6;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153
Neale Ranns177bbdc2016-11-15 09:46:51 +0000154 /**
155 * Free vlib hw_if_indices.
156 * A free list per-tunnel type since the interfaces ctreated are fo different
157 * types and we cannot change the type.
158 */
159 u32 * free_gre_tunnel_hw_if_indices[GRE_TUNNEL_N_TYPES];
Chris Luke27fe48f2016-04-28 13:44:38 -0400160
Neale Ranns177bbdc2016-11-15 09:46:51 +0000161 /**
162 * Mapping from sw_if_index to tunnel index
163 */
Chris Luke27fe48f2016-04-28 13:44:38 -0400164 u32 * tunnel_index_by_sw_if_index;
165
Damjan Marion63d5bae2017-04-04 01:28:26 +0200166 /* Sparse vector mapping gre protocol in network byte order
167 to next index. */
168 u16 * next_by_protocol;
169
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170 /* convenience */
171 vlib_main_t * vlib_main;
172 vnet_main_t * vnet_main;
173} gre_main_t;
174
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100175/**
176 * @brief IPv4 and GRE header.
Neale Ranns177bbdc2016-11-15 09:46:51 +0000177 */
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100178typedef CLIB_PACKED (struct {
179 ip4_header_t ip4;
180 gre_header_t gre;
181}) ip4_and_gre_header_t;
182
Ciara Loftus7eac9162016-09-30 15:47:03 +0100183/**
184 * @brief IPv6 and GRE header.
185 */
186typedef CLIB_PACKED (struct {
187 ip6_header_t ip6;
188 gre_header_t gre;
189}) ip6_and_gre_header_t;
190
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191always_inline gre_protocol_info_t *
192gre_get_protocol_info (gre_main_t * em, gre_protocol_t protocol)
193{
194 uword * p = hash_get (em->protocol_info_by_protocol, protocol);
195 return p ? vec_elt_at_index (em->protocol_infos, p[0]) : 0;
196}
197
198gre_main_t gre_main;
199
200/* Register given node index to take input for given gre type. */
201void
202gre_register_input_type (vlib_main_t * vm,
203 gre_protocol_t protocol,
204 u32 node_index);
205
Neale Rannsb80c5362016-10-08 13:03:40 +0100206extern clib_error_t * gre_interface_admin_up_down (vnet_main_t * vnm,
207 u32 hw_if_index,
208 u32 flags);
209
210extern void gre_tunnel_stack (adj_index_t ai);
211extern void gre_update_adj (vnet_main_t * vnm,
Neale Ranns177bbdc2016-11-15 09:46:51 +0000212 u32 sw_if_index,
213 adj_index_t ai);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214
215format_function_t format_gre_protocol;
216format_function_t format_gre_header;
217format_function_t format_gre_header_with_length;
218
Ciara Loftus7eac9162016-09-30 15:47:03 +0100219extern vlib_node_registration_t gre4_input_node;
220extern vlib_node_registration_t gre6_input_node;
Damjan Marionb8abf872016-03-14 20:02:35 +0100221extern vnet_device_class_t gre_device_class;
Neale Ranns177bbdc2016-11-15 09:46:51 +0000222extern vnet_device_class_t gre_device_teb_class;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223
224/* Parse gre protocol as 0xXXXX or protocol name.
225 In either host or network byte order. */
226unformat_function_t unformat_gre_protocol_host_byte_order;
227unformat_function_t unformat_gre_protocol_net_byte_order;
228
229/* Parse gre header. */
230unformat_function_t unformat_gre_header;
231unformat_function_t unformat_pg_gre_header;
232
233void
234gre_register_input_protocol (vlib_main_t * vm,
235 gre_protocol_t protocol,
236 u32 node_index);
237
238/* manually added to the interface output node in gre.c */
239#define GRE_OUTPUT_NEXT_LOOKUP 1
240
Chris Luke27fe48f2016-04-28 13:44:38 -0400241typedef struct {
242 u8 is_add;
243
Ciara Loftus7eac9162016-09-30 15:47:03 +0100244 ip46_address_t src, dst;
245 u8 is_ipv6;
Hongjun Ni11bfc2f2016-07-22 18:19:19 +0800246 u32 outer_fib_id;
David Hothama8cd3092016-09-19 09:55:07 -0700247 u8 teb;
Chris Luke27fe48f2016-04-28 13:44:38 -0400248} vnet_gre_add_del_tunnel_args_t;
249
250int vnet_gre_add_del_tunnel
251 (vnet_gre_add_del_tunnel_args_t *a, u32 * sw_if_indexp);
252
Ed Warnickecb9cada2015-12-08 15:45:58 -0700253#endif /* included_gre_h */