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;
29 extern vnet_hw_interface_class_t mgre_hw_interface_class;
33 #define gre_error(n,s) GRE_ERROR_##n,
34 #include <vnet/gre/error.def>
40 * L3: GRE (i.e. this tunnel is in L3 mode)
41 * TEB: Transparent Ethernet Bridging - the tunnel is in L2 mode
42 * ERSPAN: type 2 - the tunnel is for port mirror SPAN output. Each tunnel is
43 * associated with a session ID and expected to be used for encap
44 * and output of mirrored packet from a L2 network only. There is
45 * no support for receiving ERSPAN packets from a GRE ERSPAN tunnel
47 #define foreach_gre_tunnel_type \
53 * @brief The GRE tunnel type
55 typedef enum gre_tunnel_type_t_
57 #define _(n, s) GRE_TUNNEL_TYPE_##n,
58 foreach_gre_tunnel_type
60 } __clib_packed gre_tunnel_type_t;
62 extern u8 *format_gre_tunnel_type (u8 * s, va_list * args);
64 #define foreach_gre_tunnel_mode \
65 _(P2P, "point-to-point") \
66 _(MP, "multi-point") \
68 typedef enum gre_tunnel_mode_t_
70 #define _(n, s) GRE_TUNNEL_MODE_##n,
71 foreach_gre_tunnel_mode
73 } __clib_packed gre_tunnel_mode_t;
75 extern u8 *format_gre_tunnel_mode (u8 * s, va_list * args);
78 * A GRE payload protocol registration
82 /** Name (a c string). */
85 /** GRE protocol type in host byte order. */
86 gre_protocol_t protocol;
88 /** GRE tunnel type */
89 gre_tunnel_type_t tunnel_type;
91 /** Node which handles this type. */
94 /** Next index for this type. */
96 } gre_protocol_info_t;
99 * @brief Key for a IPv4 GRE Tunnel
101 typedef struct gre_tunnel_key4_t_
104 * Source and destination IP addresses
110 ip4_address_t gtk_src;
111 ip4_address_t gtk_dst;
117 * FIB table index, ERSPAN session ID and tunnel type in u32 bit fields:
118 * - The FIB table index the src,dst addresses are in, top 20 bits
119 * - The Session ID for ERSPAN tunnel type and 0 otherwise, next 10 bits
120 * - Tunnel type, bottom 2 bits
122 u32 gtk_fidx_ssid_type;
123 } __attribute__ ((packed)) gre_tunnel_key4_t;
126 * @brief Key for a IPv6 GRE Tunnel
127 * We use a different type so that the V4 key hash is as small as possible
129 typedef struct gre_tunnel_key6_t_
132 * Source and destination IP addresses
134 ip6_address_t gtk_src;
135 ip6_address_t gtk_dst;
138 * FIB table index, ERSPAN session ID and tunnel type in u32 bit fields:
139 * - The FIB table index the src,dst addresses are in, top 20 bits
140 * - The Session ID for ERSPAN tunnel type and 0 otherwise, next 10 bits
141 * - Tunnel type, bottom 2 bits
143 u32 gtk_fidx_ssid_type;
144 } __attribute__ ((packed)) gre_tunnel_key6_t;
146 #define GTK_FIB_INDEX_SHIFT 12
147 #define GTK_FIB_INDEX_MASK 0xfffff000
148 #define GTK_TYPE_SHIFT 0
149 #define GTK_TYPE_MASK 0x3
150 #define GTK_SESSION_ID_SHIFT 2
151 #define GTK_SESSION_ID_MASK 0xffc
152 #define GTK_SESSION_ID_MAX (GTK_SESSION_ID_MASK >> GTK_SESSION_ID_SHIFT)
155 * Union of the two possible key types
157 typedef union gre_tunnel_key_t_
159 gre_tunnel_key4_t gtk_v4;
160 gre_tunnel_key6_t gtk_v6;
164 * Used for GRE header seq number generation for ERSPAN encap
173 * Hash key for GRE header seq number generation for ERSPAN encap
183 * @brief A representation of a GRE tunnel
188 * Required for pool_get_aligned
190 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
193 * The hash table's key stored in separate memory since the tunnel_t
194 * memory can realloc.
196 gre_tunnel_key_t *key;
199 * The tunnel's source/local address
201 ip46_address_t tunnel_src;
203 * The tunnel's destination/remote address
205 fib_prefix_t tunnel_dst;
207 * The FIB in which the src.dst address are present
212 gre_tunnel_type_t type;
213 gre_tunnel_mode_t mode;
216 * an L2 tunnel always rquires an L2 midchain. cache here for DP.
218 adj_index_t l2_adj_index;
221 * ERSPAN type 2 session ID, least significant 10 bits of u16
226 * GRE header sequence number (SN) used for ERSPAN type 2 header, must be
227 * bumped automically to be thread safe. As multiple GRE tunnels are created
228 * for the same fib-idx/DIP/SIP with different ERSPAN session number, they all
229 * share the same SN which is kept per FIB/DIP/SIP, as specified by RFC2890.
234 u32 dev_instance; /* Real device instance in tunnel vector */
235 u32 user_instance; /* Instance name being shown to user */
245 * @brief GRE related global data
250 * pool of tunnel instances
252 gre_tunnel_t *tunnels;
255 * GRE payload protocol registrations
257 gre_protocol_info_t *protocol_infos;
260 * Hash tables mapping name/protocol to protocol info index.
262 uword *protocol_info_by_name, *protocol_info_by_protocol;
265 * Hash mapping to tunnels with ipv4 src/dst addr
267 uword *tunnel_by_key4;
270 * Hash mapping to tunnels with ipv6 src/dst addr
272 uword *tunnel_by_key6;
275 * Hash mapping tunnel src/dst addr and fib-idx to sequence number
277 uword *seq_num_by_key;
280 * Mapping from sw_if_index to tunnel index
282 u32 *tunnel_index_by_sw_if_index;
284 /* Sparse vector mapping gre protocol in network byte order
286 next_info_t *next_by_protocol;
289 vlib_main_t *vlib_main;
290 vnet_main_t *vnet_main;
292 /* Record used instances */
293 uword *instance_used;
297 * @brief IPv4 and GRE header.
300 typedef CLIB_PACKED (struct {
303 }) ip4_and_gre_header_t;
307 * @brief IPv6 and GRE header.
310 typedef CLIB_PACKED (struct {
313 }) ip6_and_gre_header_t;
316 always_inline gre_protocol_info_t *
317 gre_get_protocol_info (gre_main_t * em, gre_protocol_t protocol)
319 uword *p = hash_get (em->protocol_info_by_protocol, protocol);
320 return p ? vec_elt_at_index (em->protocol_infos, p[0]) : 0;
323 extern gre_main_t gre_main;
325 extern clib_error_t *gre_interface_admin_up_down (vnet_main_t * vnm,
326 u32 hw_if_index, u32 flags);
328 extern void gre_tunnel_stack (adj_index_t ai);
329 extern void gre_update_adj (vnet_main_t * vnm,
330 u32 sw_if_index, adj_index_t ai);
332 format_function_t format_gre_protocol;
333 format_function_t format_gre_header;
334 format_function_t format_gre_header_with_length;
336 extern vlib_node_registration_t gre4_input_node;
337 extern vlib_node_registration_t gre6_input_node;
338 extern vlib_node_registration_t gre_encap_node;
339 extern vnet_device_class_t gre_device_class;
341 /* Parse gre protocol as 0xXXXX or protocol name.
342 In either host or network byte order. */
343 unformat_function_t unformat_gre_protocol_host_byte_order;
344 unformat_function_t unformat_gre_protocol_net_byte_order;
346 /* Parse gre header. */
347 unformat_function_t unformat_gre_header;
348 unformat_function_t unformat_pg_gre_header;
351 gre_register_input_protocol (vlib_main_t * vm, gre_protocol_t protocol,
352 u32 node_index, gre_tunnel_type_t tunnel_type);
354 /* manually added to the interface output node in gre.c */
355 #define GRE_OUTPUT_NEXT_LOOKUP 1
360 gre_tunnel_type_t type;
361 gre_tunnel_mode_t mode;
364 ip46_address_t src, dst;
367 } vnet_gre_tunnel_add_del_args_t;
369 extern int vnet_gre_tunnel_add_del (vnet_gre_tunnel_add_del_args_t * a,
373 gre_mk_key4 (ip4_address_t src,
375 u32 fib_index, u8 ttype, u16 session_id, gre_tunnel_key4_t * key)
379 key->gtk_fidx_ssid_type = ttype |
380 (fib_index << GTK_FIB_INDEX_SHIFT) | (session_id << GTK_SESSION_ID_SHIFT);
384 gre_match_key4 (const gre_tunnel_key4_t * key1,
385 const gre_tunnel_key4_t * key2)
387 return ((key1->gtk_as_u64 == key2->gtk_as_u64) &&
388 (key1->gtk_fidx_ssid_type == key2->gtk_fidx_ssid_type));
392 gre_mk_key6 (const ip6_address_t * src,
393 const ip6_address_t * dst,
394 u32 fib_index, u8 ttype, u16 session_id, gre_tunnel_key6_t * key)
398 key->gtk_fidx_ssid_type = ttype |
399 (fib_index << GTK_FIB_INDEX_SHIFT) | (session_id << GTK_SESSION_ID_SHIFT);
403 gre_match_key6 (const gre_tunnel_key6_t * key1,
404 const gre_tunnel_key6_t * key2)
406 return ((key1->gtk_src.as_u64[0] == key2->gtk_src.as_u64[0]) &&
407 (key1->gtk_src.as_u64[1] == key2->gtk_src.as_u64[1]) &&
408 (key1->gtk_dst.as_u64[0] == key2->gtk_dst.as_u64[0]) &&
409 (key1->gtk_dst.as_u64[1] == key2->gtk_dst.as_u64[1]) &&
410 (key1->gtk_fidx_ssid_type == key2->gtk_fidx_ssid_type));
414 gre_mk_sn_key (const gre_tunnel_t * gt, gre_sn_key_t * key)
416 key->src = gt->tunnel_src;
417 key->dst = gt->tunnel_dst.fp_addr;
418 key->fib_index = gt->outer_fib_index;
421 #endif /* included_gre_h */
424 * fd.io coding-style-patch-verification: ON
427 * eval: (c-set-style "gnu")