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 * @brief The GRE tunnel type
41 typedef enum gre_tunnel_type_t_
44 * L3 GRE (i.e. this tunnel is in L3 mode)
46 GRE_TUNNEL_TYPE_L3 = 0,
48 * Transparent Ethernet Bridging - the tunnel is in L2 mode
50 GRE_TUNNEL_TYPE_TEB = 1,
52 * ERSPAN type 2 - the tunnel is for port mirror SPAN output. Each tunnel is
53 * associated with a session ID and expected to be used for encap and output
54 * of mirrored packet from a L2 network only. There is no support for
55 * receiving ERSPAN packets from a GRE ERSPAN tunnel in VPP.
57 GRE_TUNNEL_TYPE_ERSPAN = 2,
62 #define GRE_TUNNEL_TYPE_NAMES { \
63 [GRE_TUNNEL_TYPE_L3] = "L3", \
64 [GRE_TUNNEL_TYPE_TEB] = "TEB", \
65 [GRE_TUNNEL_TYPE_ERSPAN] = "ERSPAN", \
69 * A GRE payload protocol registration
73 /** Name (a c string). */
76 /** GRE protocol type in host byte order. */
77 gre_protocol_t protocol;
79 /** GRE tunnel type */
80 gre_tunnel_type_t tunnel_type;
82 /** Node which handles this type. */
85 /** Next index for this type. */
87 } gre_protocol_info_t;
90 * @brief Key for a IPv4 GRE Tunnel
92 typedef struct gre_tunnel_key4_t_
95 * Source and destination IP addresses
101 ip4_address_t gtk_src;
102 ip4_address_t gtk_dst;
108 * FIB table index, ERSPAN session ID and tunnel type in u32 bit fields:
109 * - The FIB table index the src,dst addresses are in, top 20 bits
110 * - The Session ID for ERSPAN tunnel type and 0 otherwise, next 10 bits
111 * - Tunnel type, bottom 2 bits
113 u32 gtk_fidx_ssid_type;
114 } __attribute__ ((packed)) gre_tunnel_key4_t;
117 * @brief Key for a IPv6 GRE Tunnel
118 * We use a different type so that the V4 key hash is as small as possible
120 typedef struct gre_tunnel_key6_t_
123 * Source and destination IP addresses
125 ip6_address_t gtk_src;
126 ip6_address_t gtk_dst;
129 * FIB table index, ERSPAN session ID and tunnel type in u32 bit fields:
130 * - The FIB table index the src,dst addresses are in, top 20 bits
131 * - The Session ID for ERSPAN tunnel type and 0 otherwise, next 10 bits
132 * - Tunnel type, bottom 2 bits
134 u32 gtk_fidx_ssid_type;
135 } __attribute__ ((packed)) gre_tunnel_key6_t;
137 #define GTK_FIB_INDEX_SHIFT 12
138 #define GTK_FIB_INDEX_MASK 0xfffff000
139 #define GTK_TYPE_SHIFT 0
140 #define GTK_TYPE_MASK 0x3
141 #define GTK_SESSION_ID_SHIFT 2
142 #define GTK_SESSION_ID_MASK 0xffc
143 #define GTK_SESSION_ID_MAX (GTK_SESSION_ID_MASK >> GTK_SESSION_ID_SHIFT)
146 * Union of the two possible key types
148 typedef union gre_tunnel_key_t_
150 gre_tunnel_key4_t gtk_v4;
151 gre_tunnel_key6_t gtk_v6;
155 * Used for GRE header seq number generation for ERSPAN encap
164 * Hash key for GRE header seq number generation for ERSPAN encap
174 * @brief A representation of a GRE tunnel
179 * Required for pool_get_aligned
181 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
184 * Linkage into the FIB object graph
189 * The hash table's key stored in separate memory since the tunnel_t
190 * memory can realloc.
192 gre_tunnel_key_t *key;
195 * The tunnel's source/local address
197 ip46_address_t tunnel_src;
199 * The tunnel's destination/remote address
201 fib_prefix_t tunnel_dst;
203 * The FIB in which the src.dst address are present
208 gre_tunnel_type_t type;
211 * The FIB entry sourced by the tunnel for its destination prefix
213 fib_node_index_t fib_entry_index;
216 * The tunnel is a child of the FIB entry for its desintion. This is
217 * so it receives updates when the forwarding information for that entry
219 * The tunnels sibling index on the FIB entry's dependency list.
224 * an L2 tunnel always rquires an L2 midchain. cache here for DP.
226 adj_index_t l2_adj_index;
229 * ERSPAN type 2 session ID, least significant 10 bits of u16
234 * GRE header sequence number (SN) used for ERSPAN type 2 header, must be
235 * bumped automically to be thread safe. As multiple GRE tunnels are created
236 * for the same fib-idx/DIP/SIP with different ERSPAN session number, they all
237 * share the same SN which is kept per FIB/DIP/SIP, as specified by RFC2890.
242 u32 dev_instance; /* Real device instance in tunnel vector */
243 u32 user_instance; /* Instance name being shown to user */
253 * @brief GRE related global data
258 * pool of tunnel instances
260 gre_tunnel_t *tunnels;
263 * GRE payload protocol registrations
265 gre_protocol_info_t *protocol_infos;
268 * Hash tables mapping name/protocol to protocol info index.
270 uword *protocol_info_by_name, *protocol_info_by_protocol;
273 * Hash mapping to tunnels with ipv4 src/dst addr
275 uword *tunnel_by_key4;
278 * Hash mapping to tunnels with ipv6 src/dst addr
280 uword *tunnel_by_key6;
283 * Hash mapping tunnel src/dst addr and fib-idx to sequence number
285 uword *seq_num_by_key;
288 * Mapping from sw_if_index to tunnel index
290 u32 *tunnel_index_by_sw_if_index;
292 /* Sparse vector mapping gre protocol in network byte order
294 next_info_t *next_by_protocol;
297 vlib_main_t *vlib_main;
298 vnet_main_t *vnet_main;
300 /* Record used instances */
301 uword *instance_used;
305 * @brief IPv4 and GRE header.
308 typedef CLIB_PACKED (struct {
311 }) ip4_and_gre_header_t;
315 * @brief IPv6 and GRE header.
318 typedef CLIB_PACKED (struct {
321 }) ip6_and_gre_header_t;
324 always_inline gre_protocol_info_t *
325 gre_get_protocol_info (gre_main_t * em, gre_protocol_t protocol)
327 uword *p = hash_get (em->protocol_info_by_protocol, protocol);
328 return p ? vec_elt_at_index (em->protocol_infos, p[0]) : 0;
331 extern gre_main_t gre_main;
333 extern clib_error_t *gre_interface_admin_up_down (vnet_main_t * vnm,
334 u32 hw_if_index, u32 flags);
336 extern void gre_tunnel_stack (adj_index_t ai);
337 extern void gre_update_adj (vnet_main_t * vnm,
338 u32 sw_if_index, adj_index_t ai);
340 format_function_t format_gre_protocol;
341 format_function_t format_gre_header;
342 format_function_t format_gre_header_with_length;
344 extern vlib_node_registration_t gre4_input_node;
345 extern vlib_node_registration_t gre6_input_node;
346 extern vlib_node_registration_t gre_encap_node;
347 extern vnet_device_class_t gre_device_class;
349 /* Parse gre protocol as 0xXXXX or protocol name.
350 In either host or network byte order. */
351 unformat_function_t unformat_gre_protocol_host_byte_order;
352 unformat_function_t unformat_gre_protocol_net_byte_order;
354 /* Parse gre header. */
355 unformat_function_t unformat_gre_header;
356 unformat_function_t unformat_pg_gre_header;
359 gre_register_input_protocol (vlib_main_t * vm, gre_protocol_t protocol,
360 u32 node_index, gre_tunnel_type_t tunnel_type);
362 /* manually added to the interface output node in gre.c */
363 #define GRE_OUTPUT_NEXT_LOOKUP 1
371 ip46_address_t src, dst;
374 } vnet_gre_add_del_tunnel_args_t;
376 int vnet_gre_add_del_tunnel
377 (vnet_gre_add_del_tunnel_args_t * a, u32 * sw_if_indexp);
380 gre_mk_key4 (ip4_address_t src,
382 u32 fib_index, u8 ttype, u16 session_id, gre_tunnel_key4_t * key)
386 key->gtk_fidx_ssid_type = ttype |
387 (fib_index << GTK_FIB_INDEX_SHIFT) | (session_id << GTK_SESSION_ID_SHIFT);
391 gre_match_key4 (const gre_tunnel_key4_t * key1,
392 const gre_tunnel_key4_t * key2)
394 return ((key1->gtk_as_u64 == key2->gtk_as_u64) &&
395 (key1->gtk_fidx_ssid_type == key2->gtk_fidx_ssid_type));
399 gre_mk_key6 (const ip6_address_t * src,
400 const ip6_address_t * dst,
401 u32 fib_index, u8 ttype, u16 session_id, gre_tunnel_key6_t * key)
405 key->gtk_fidx_ssid_type = ttype |
406 (fib_index << GTK_FIB_INDEX_SHIFT) | (session_id << GTK_SESSION_ID_SHIFT);
410 gre_match_key6 (const gre_tunnel_key6_t * key1,
411 const gre_tunnel_key6_t * key2)
413 return ((key1->gtk_src.as_u64[0] == key2->gtk_src.as_u64[0]) &&
414 (key1->gtk_src.as_u64[1] == key2->gtk_src.as_u64[1]) &&
415 (key1->gtk_dst.as_u64[0] == key2->gtk_dst.as_u64[0]) &&
416 (key1->gtk_dst.as_u64[1] == key2->gtk_dst.as_u64[1]) &&
417 (key1->gtk_fidx_ssid_type == key2->gtk_fidx_ssid_type));
421 gre_mk_sn_key (const gre_tunnel_t * gt, gre_sn_key_t * key)
423 key->src = gt->tunnel_src;
424 key->dst = gt->tunnel_dst.fp_addr;
425 key->fib_index = gt->outer_fib_index;
428 #endif /* included_gre_h */
431 * fd.io coding-style-patch-verification: ON
434 * eval: (c-set-style "gnu")