2 * gre.h: types/functions for gre.
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #ifndef included_gre_h
19 #define included_gre_h
21 #include <vnet/vnet.h>
22 #include <vnet/gre/packet.h>
23 #include <vnet/ip/ip.h>
24 #include <vnet/pg/pg.h>
25 #include <vnet/ip/format.h>
26 #include <vnet/adj/adj_types.h>
28 extern vnet_hw_interface_class_t gre_hw_interface_class;
32 #define gre_error(n,s) GRE_ERROR_##n,
33 #include <vnet/gre/error.def>
39 * A GRE payload protocol registration
43 /** Name (a c string). */
46 /** GRE protocol type in host byte order. */
47 gre_protocol_t protocol;
49 /** Node which handles this type. */
52 /** Next index for this type. */
54 } gre_protocol_info_t;
57 * @brief The GRE tunnel type
59 typedef enum gre_tunnel_tyoe_t_
62 * L3 GRE (i.e. this tunnel is in L3 mode)
66 * Transparent Ethernet Bridging - the tunnel is in L2 mode
71 #define GRE_TUNNEL_TYPE_NAMES { \
72 [GRE_TUNNEL_TYPE_L3] = "L3", \
73 [GRE_TUNNEL_TYPE_TEB] = "TEB", \
76 #define GRE_TUNNEL_N_TYPES ((gre_tunnel_type_t)GRE_TUNNEL_TYPE_TEB+1)
79 * @brief Key for a IPv4 GRE Tunnel
81 typedef struct gre_tunnel_key4_t_
84 * Source and destination IP addresses
90 ip4_address_t gtk_src;
91 ip4_address_t gtk_dst;
97 * The FIB table the src,dst addresses are in.
98 * tunnels with the same IP addresses in different FIBs are not
102 } __attribute__ ((packed)) gre_tunnel_key4_t;
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
108 typedef struct gre_tunnel_key6_t_
111 * Source and destination IP addresses
113 ip6_address_t gtk_src;
114 ip6_address_t gtk_dst;
117 * The FIB table the src,dst addresses are in.
118 * tunnels with the same IP addresses in different FIBs are not
122 } __attribute__ ((packed)) gre_tunnel_key6_t;
125 * Union of the two possible key types
127 typedef union gre_tunnel_key_t_
129 gre_tunnel_key4_t gtk_v4;
130 gre_tunnel_key6_t gtk_v6;
134 * @brief A representation of a GRE tunnel
139 * Linkage into the FIB object graph
144 * The hash table's key stored in separate memory since the tunnel_t
145 * memory can realloc.
147 gre_tunnel_key_t *key;
150 * The tunnel's source/local address
152 ip46_address_t tunnel_src;
154 * The tunnel's destination/remote address
156 fib_prefix_t tunnel_dst;
158 * The FIB in which the src.dst address are present
163 gre_tunnel_type_t type;
166 * The FIB entry sourced by the tunnel for its destination prefix
168 fib_node_index_t fib_entry_index;
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
174 * The tunnels sibling index on the FIB entry's dependency list.
179 * on a L2 tunnel this is the VLIB arc from the L2-tx to the l2-midchain
184 * an L2 tunnel always rquires an L2 midchain. cache here for DP.
186 adj_index_t l2_adj_index;
190 * @brief GRE related global data
195 * pool of tunnel instances
197 gre_tunnel_t *tunnels;
200 * GRE payload protocol registrations
202 gre_protocol_info_t *protocol_infos;
205 * Hash tables mapping name/protocol to protocol info index.
207 uword *protocol_info_by_name, *protocol_info_by_protocol;
210 * Hash mapping ipv4 src/dst addr pair to tunnel
212 uword *tunnel_by_key4;
215 * Hash mapping ipv6 src/dst addr pair to tunnel
217 uword *tunnel_by_key6;
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.
224 u32 *free_gre_tunnel_hw_if_indices[GRE_TUNNEL_N_TYPES];
227 * Mapping from sw_if_index to tunnel index
229 u32 *tunnel_index_by_sw_if_index;
231 /* Sparse vector mapping gre protocol in network byte order
233 u16 *next_by_protocol;
236 vlib_main_t *vlib_main;
237 vnet_main_t *vnet_main;
241 * @brief IPv4 and GRE header.
244 typedef CLIB_PACKED (struct {
247 }) ip4_and_gre_header_t;
251 * @brief IPv6 and GRE header.
254 typedef CLIB_PACKED (struct {
257 }) ip6_and_gre_header_t;
260 always_inline gre_protocol_info_t *
261 gre_get_protocol_info (gre_main_t * em, gre_protocol_t protocol)
263 uword *p = hash_get (em->protocol_info_by_protocol, protocol);
264 return p ? vec_elt_at_index (em->protocol_infos, p[0]) : 0;
267 extern gre_main_t gre_main;
269 /* Register given node index to take input for given gre type. */
271 gre_register_input_type (vlib_main_t * vm,
272 gre_protocol_t protocol, u32 node_index);
274 extern clib_error_t *gre_interface_admin_up_down (vnet_main_t * vnm,
275 u32 hw_if_index, u32 flags);
277 extern void gre_tunnel_stack (adj_index_t ai);
278 extern void gre_update_adj (vnet_main_t * vnm,
279 u32 sw_if_index, adj_index_t ai);
281 format_function_t format_gre_protocol;
282 format_function_t format_gre_header;
283 format_function_t format_gre_header_with_length;
285 extern vlib_node_registration_t gre4_input_node;
286 extern vlib_node_registration_t gre6_input_node;
287 extern vnet_device_class_t gre_device_class;
288 extern vnet_device_class_t gre_device_teb_class;
290 /* Parse gre protocol as 0xXXXX or protocol name.
291 In either host or network byte order. */
292 unformat_function_t unformat_gre_protocol_host_byte_order;
293 unformat_function_t unformat_gre_protocol_net_byte_order;
295 /* Parse gre header. */
296 unformat_function_t unformat_gre_header;
297 unformat_function_t unformat_pg_gre_header;
300 gre_register_input_protocol (vlib_main_t * vm,
301 gre_protocol_t protocol, u32 node_index);
303 /* manually added to the interface output node in gre.c */
304 #define GRE_OUTPUT_NEXT_LOOKUP 1
310 ip46_address_t src, dst;
314 } vnet_gre_add_del_tunnel_args_t;
316 int vnet_gre_add_del_tunnel
317 (vnet_gre_add_del_tunnel_args_t * a, u32 * sw_if_indexp);
320 gre_mk_key4 (const ip4_address_t * src,
321 const ip4_address_t * dst,
322 u32 fib_index, gre_tunnel_key4_t * key)
326 key->gtk_fib_index = fib_index;
330 gre_match_key4 (const gre_tunnel_key4_t * key1,
331 const gre_tunnel_key4_t * key2)
333 return ((key1->gtk_as_u64 == key2->gtk_as_u64) &&
334 (key1->gtk_fib_index == key2->gtk_fib_index));
338 gre_mk_key6 (const ip6_address_t * src,
339 const ip6_address_t * dst,
340 u32 fib_index, gre_tunnel_key6_t * key)
344 key->gtk_fib_index = fib_index;
348 gre_match_key6 (const gre_tunnel_key6_t * key1,
349 const gre_tunnel_key6_t * key2)
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));
358 #endif /* included_gre_h */
361 * fd.io coding-style-patch-verification: ON
364 * eval: (c-set-style "gnu")