FD.io VPP  v19.08.3-2-gbabecb413
Vector Packet Processing
gre.h
Go to the documentation of this file.
1 /*
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/pg/pg.h>
25 #include <vnet/ip/format.h>
26 #include <vnet/adj/adj_types.h>
27 
29 
30 typedef enum
31 {
32 #define gre_error(n,s) GRE_ERROR_##n,
33 #include <vnet/gre/error.def>
34 #undef gre_error
36 } gre_error_t;
37 
38 /**
39  * @brief The GRE tunnel type
40  */
41 typedef enum gre_tunnel_type_t_
42 {
43  /**
44  * L3 GRE (i.e. this tunnel is in L3 mode)
45  */
47  /**
48  * Transparent Ethernet Bridging - the tunnel is in L2 mode
49  */
51  /**
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.
56  */
59 
60 #define GRE_TUNNEL_TYPE_N (GRE_TUNNEL_TYPE_ERSPAN + 1)
61 
62 #define GRE_TUNNEL_TYPE_NAMES { \
63  [GRE_TUNNEL_TYPE_L3] = "L3", \
64  [GRE_TUNNEL_TYPE_TEB] = "TEB", \
65  [GRE_TUNNEL_TYPE_ERSPAN] = "ERSPAN", \
66 }
67 
68 /**
69  * A GRE payload protocol registration
70  */
71 typedef struct
72 {
73  /** Name (a c string). */
74  char *name;
75 
76  /** GRE protocol type in host byte order. */
78 
79  /** GRE tunnel type */
81 
82  /** Node which handles this type. */
84 
85  /** Next index for this type. */
88 
89 /**
90  * @brief Key for a IPv4 GRE Tunnel
91  */
92 typedef struct gre_tunnel_key4_t_
93 {
94  /**
95  * Source and destination IP addresses
96  */
97  union
98  {
99  struct
100  {
103  };
105  };
106 
107  /**
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
112  */
114 } __attribute__ ((packed)) gre_tunnel_key4_t;
115 
116 /**
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
119  */
120 typedef struct gre_tunnel_key6_t_
121 {
122  /**
123  * Source and destination IP addresses
124  */
127 
128  /**
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
133  */
135 } __attribute__ ((packed)) gre_tunnel_key6_t;
136 
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)
144 
145 /**
146  * Union of the two possible key types
147  */
148 typedef union gre_tunnel_key_t_
149 {
153 
154 /**
155  * Used for GRE header seq number generation for ERSPAN encap
156  */
157 typedef struct
158 {
161 } gre_sn_t;
162 
163 /**
164  * Hash key for GRE header seq number generation for ERSPAN encap
165  */
166 typedef struct
167 {
168  ip46_address_t src;
169  ip46_address_t dst;
171 } gre_sn_key_t;
172 
173 /**
174  * @brief A representation of a GRE tunnel
175  */
176 typedef struct
177 {
178  /**
179  * Required for pool_get_aligned
180  */
181  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
182 
183  /**
184  * The hash table's key stored in separate memory since the tunnel_t
185  * memory can realloc.
186  */
188 
189  /**
190  * The tunnel's source/local address
191  */
192  ip46_address_t tunnel_src;
193  /**
194  * The tunnel's destination/remote address
195  */
197  /**
198  * The FIB in which the src.dst address are present
199  */
204 
205  /**
206  * an L2 tunnel always rquires an L2 midchain. cache here for DP.
207  */
209 
210  /**
211  * ERSPAN type 2 session ID, least significant 10 bits of u16
212  */
214 
215  /**
216  * GRE header sequence number (SN) used for ERSPAN type 2 header, must be
217  * bumped automically to be thread safe. As multiple GRE tunnels are created
218  * for the same fib-idx/DIP/SIP with different ERSPAN session number, they all
219  * share the same SN which is kept per FIB/DIP/SIP, as specified by RFC2890.
220  */
222 
223 
224  u32 dev_instance; /* Real device instance in tunnel vector */
225  u32 user_instance; /* Instance name being shown to user */
226 } gre_tunnel_t;
227 
228 typedef struct
229 {
232 } next_info_t;
233 
234 /**
235  * @brief GRE related global data
236  */
237 typedef struct
238 {
239  /**
240  * pool of tunnel instances
241  */
243 
244  /**
245  * GRE payload protocol registrations
246  */
248 
249  /**
250  * Hash tables mapping name/protocol to protocol info index.
251  */
252  uword *protocol_info_by_name, *protocol_info_by_protocol;
253 
254  /**
255  * Hash mapping to tunnels with ipv4 src/dst addr
256  */
258 
259  /**
260  * Hash mapping to tunnels with ipv6 src/dst addr
261  */
263 
264  /**
265  * Hash mapping tunnel src/dst addr and fib-idx to sequence number
266  */
268 
269  /**
270  * Mapping from sw_if_index to tunnel index
271  */
273 
274  /* Sparse vector mapping gre protocol in network byte order
275  to next index. */
277 
278  /* convenience */
281 
282  /* Record used instances */
284 } gre_main_t;
285 
286 /**
287  * @brief IPv4 and GRE header.
288  */
289 /* *INDENT-OFF* */
290 typedef CLIB_PACKED (struct {
291  ip4_header_t ip4;
292  gre_header_t gre;
293 }) ip4_and_gre_header_t;
294 /* *INDENT-ON* */
295 
296 /**
297  * @brief IPv6 and GRE header.
298  */
299 /* *INDENT-OFF* */
300 typedef CLIB_PACKED (struct {
301  ip6_header_t ip6;
302  gre_header_t gre;
303 }) ip6_and_gre_header_t;
304 /* *INDENT-ON* */
305 
308 {
309  uword *p = hash_get (em->protocol_info_by_protocol, protocol);
310  return p ? vec_elt_at_index (em->protocol_infos, p[0]) : 0;
311 }
312 
313 extern gre_main_t gre_main;
314 
316  u32 hw_if_index, u32 flags);
317 
318 extern void gre_tunnel_stack (adj_index_t ai);
319 extern void gre_update_adj (vnet_main_t * vnm,
320  u32 sw_if_index, adj_index_t ai);
321 
325 
330 
331 /* Parse gre protocol as 0xXXXX or protocol name.
332  In either host or network byte order. */
335 
336 /* Parse gre header. */
339 
340 void
342  u32 node_index, gre_tunnel_type_t tunnel_type);
343 
344 /* manually added to the interface output node in gre.c */
345 #define GRE_OUTPUT_NEXT_LOOKUP 1
346 
347 typedef struct
348 {
353  ip46_address_t src, dst;
357 
359  u32 * sw_if_indexp);
360 
361 static inline void
364  u32 fib_index, u8 ttype, u16 session_id, gre_tunnel_key4_t * key)
365 {
366  key->gtk_src = src;
367  key->gtk_dst = dst;
368  key->gtk_fidx_ssid_type = ttype |
369  (fib_index << GTK_FIB_INDEX_SHIFT) | (session_id << GTK_SESSION_ID_SHIFT);
370 }
371 
372 static inline int
374  const gre_tunnel_key4_t * key2)
375 {
376  return ((key1->gtk_as_u64 == key2->gtk_as_u64) &&
377  (key1->gtk_fidx_ssid_type == key2->gtk_fidx_ssid_type));
378 }
379 
380 static inline void
382  const ip6_address_t * dst,
383  u32 fib_index, u8 ttype, u16 session_id, gre_tunnel_key6_t * key)
384 {
385  key->gtk_src = *src;
386  key->gtk_dst = *dst;
387  key->gtk_fidx_ssid_type = ttype |
388  (fib_index << GTK_FIB_INDEX_SHIFT) | (session_id << GTK_SESSION_ID_SHIFT);
389 }
390 
391 static inline int
393  const gre_tunnel_key6_t * key2)
394 {
395  return ((key1->gtk_src.as_u64[0] == key2->gtk_src.as_u64[0]) &&
396  (key1->gtk_src.as_u64[1] == key2->gtk_src.as_u64[1]) &&
397  (key1->gtk_dst.as_u64[0] == key2->gtk_dst.as_u64[0]) &&
398  (key1->gtk_dst.as_u64[1] == key2->gtk_dst.as_u64[1]) &&
399  (key1->gtk_fidx_ssid_type == key2->gtk_fidx_ssid_type));
400 }
401 
402 static inline void
404 {
405  key->src = gt->tunnel_src;
406  key->dst = gt->tunnel_dst.fp_addr;
407  key->fib_index = gt->outer_fib_index;
408 }
409 
410 #endif /* included_gre_h */
411 
412 /*
413  * fd.io coding-style-patch-verification: ON
414  *
415  * Local Variables:
416  * eval: (c-set-style "gnu")
417  * End:
418  */
vnet_main_t * vnet_main
Definition: gre.h:280
uword * seq_num_by_key
Hash mapping tunnel src/dst addr and fib-idx to sequence number.
Definition: gre.h:267
vlib_node_registration_t gre_encap_node
(constructor) VLIB_REGISTER_NODE (gre_encap_node)
Definition: gre.c:493
vlib_node_registration_t gre4_input_node
(constructor) VLIB_REGISTER_NODE (gre4_input_node)
Definition: node.c:453
uword * tunnel_by_key6
Hash mapping to tunnels with ipv6 src/dst addr.
Definition: gre.h:262
GRE related global data.
Definition: gre.h:237
#define CLIB_CACHE_LINE_ALIGN_MARK(mark)
Definition: cache.h:60
format_function_t format_gre_header_with_length
Definition: gre.h:324
ip4_address_t gtk_dst
Definition: gre.h:102
u32 hw_if_index
Definition: gre.h:201
Key for a IPv6 GRE Tunnel We use a different type so that the V4 key hash is as small as possible...
Definition: gre.h:120
a
Definition: bitmap.h:538
Transparent Ethernet Bridging - the tunnel is in L2 mode.
Definition: gre.h:50
uword * protocol_info_by_protocol
Definition: gre.h:252
u64 as_u64[2]
Definition: ip6_packet.h:51
unsigned long u64
Definition: types.h:89
#define GTK_SESSION_ID_SHIFT
Definition: gre.h:141
gre_protocol_t protocol
GRE protocol type in host byte order.
Definition: gre.h:77
char * name
Name (a c string).
Definition: gre.h:74
A GRE payload protocol registration.
Definition: gre.h:71
int vnet_gre_tunnel_add_del(vnet_gre_tunnel_add_del_args_t *a, u32 *sw_if_indexp)
Definition: interface.c:369
u32 outer_fib_index
The FIB in which the src.dst address are present.
Definition: gre.h:200
vl_api_address_t src
Definition: gre.api:51
typedef CLIB_PACKED(struct { ip4_header_t ip4;gre_header_t gre;}) ip4_and_gre_header_t
IPv4 and GRE header.
gre_tunnel_type_t_
The GRE tunnel type.
Definition: gre.h:41
u32 gtk_fidx_ssid_type
FIB table index, ERSPAN session ID and tunnel type in u32 bit fields:
Definition: gre.h:134
struct _vnet_device_class vnet_device_class_t
unsigned char u8
Definition: types.h:56
ip4_address_t gtk_src
Definition: gre.h:101
u32 * tunnel_index_by_sw_if_index
Mapping from sw_if_index to tunnel index.
Definition: gre.h:272
u64 gtk_as_u64
Definition: gre.h:104
u32 seq_num
Definition: gre.h:159
u8 *() format_function_t(u8 *s, va_list *args)
Definition: format.h:48
Hash key for GRE header seq number generation for ERSPAN encap.
Definition: gre.h:166
enum gre_tunnel_type_t_ gre_tunnel_type_t
The GRE tunnel type.
u32 sw_if_index
Definition: gre.h:202
u32 gtk_fidx_ssid_type
FIB table index, ERSPAN session ID and tunnel type in u32 bit fields:
Definition: gre.h:113
u32 dev_instance
Definition: gre.h:224
ip6_address_t gtk_dst
Definition: gre.h:126
#define always_inline
Definition: clib.h:99
void gre_tunnel_stack(adj_index_t ai)
gre_tunnel_stack
Definition: interface.c:121
gre_tunnel_type_t tunnel_type
GRE tunnel type.
Definition: gre.h:80
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
Aggregate type for a prefix.
Definition: fib_types.h:203
ip46_address_t src
Definition: gre.h:353
unsigned int u32
Definition: types.h:88
adj_index_t l2_adj_index
an L2 tunnel always rquires an L2 midchain.
Definition: gre.h:208
unformat_function_t unformat_gre_header
Definition: gre.h:337
ip46_address_t tunnel_src
The tunnel&#39;s source/local address.
Definition: gre.h:192
#define hash_get(h, key)
Definition: hash.h:249
uword * tunnel_by_key4
Hash mapping to tunnels with ipv4 src/dst addr.
Definition: gre.h:257
A representation of a GRE tunnel.
Definition: gre.h:176
uword * instance_used
Definition: gre.h:283
ip46_address_t fp_addr
The address type is not deriveable from the fp_addr member.
Definition: fib_types.h:226
struct gre_tunnel_key6_t_ gre_tunnel_key6_t
Key for a IPv6 GRE Tunnel We use a different type so that the V4 key hash is as small as possible...
ip6_address_t gtk_src
Source and destination IP addresses.
Definition: gre.h:125
vl_api_ip_proto_t protocol
Definition: punt.api:39
unformat_function_t unformat_gre_protocol_net_byte_order
Definition: gre.h:334
unsigned short u16
Definition: types.h:57
static int gre_match_key4(const gre_tunnel_key4_t *key1, const gre_tunnel_key4_t *key2)
Definition: gre.h:373
gre_tunnel_key4_t gtk_v4
Definition: gre.h:150
u32 node_index
Node which handles this type.
Definition: gre.h:83
void gre_register_input_protocol(vlib_main_t *vm, gre_protocol_t protocol, u32 node_index, gre_tunnel_type_t tunnel_type)
Definition: node.c:498
static int gre_match_key6(const gre_tunnel_key6_t *key1, const gre_tunnel_key6_t *key2)
Definition: gre.h:392
uword() unformat_function_t(unformat_input_t *input, va_list *args)
Definition: format.h:233
vl_api_address_t dst
Definition: gre.api:52
Key for a IPv4 GRE Tunnel.
Definition: gre.h:92
union gre_tunnel_key_t_ gre_tunnel_key_t
Union of the two possible key types.
gre_tunnel_type_t type
Definition: gre.h:203
vlib_main_t * vm
Definition: buffer.c:323
gre_sn_t * gre_sn
GRE header sequence number (SN) used for ERSPAN type 2 header, must be bumped automically to be threa...
Definition: gre.h:221
gre_tunnel_key6_t gtk_v6
Definition: gre.h:151
u8 tunnel_type
Definition: gre.h:231
format_function_t format_gre_protocol
Definition: gre.h:322
ip46_address_t src
Definition: gre.h:168
gre_protocol_t
Definition: packet.h:30
#define GTK_FIB_INDEX_SHIFT
Definition: gre.h:137
static void gre_mk_key4(ip4_address_t src, ip4_address_t dst, u32 fib_index, u8 ttype, u16 session_id, gre_tunnel_key4_t *key)
Definition: gre.h:362
ip46_address_t dst
Definition: gre.h:169
Used for GRE header seq number generation for ERSPAN encap.
Definition: gre.h:157
u32 adj_index_t
An index for adjacencies.
Definition: adj_types.h:30
gre_tunnel_type_t type
Definition: gre.h:350
u16 session_id
Definition: gre.api:46
u32 next_index
Next index for this type.
Definition: gre.h:86
void gre_update_adj(vnet_main_t *vnm, u32 sw_if_index, adj_index_t ai)
Definition: gre.c:307
gre_tunnel_t * tunnels
pool of tunnel instances
Definition: gre.h:242
u32 fib_index
Definition: gre.h:170
gre_main_t gre_main
next_info_t * next_by_protocol
Definition: gre.h:276
Union of the two possible key types.
Definition: gre.h:148
vnet_hw_interface_class_t gre_hw_interface_class
u32 user_instance
Definition: gre.h:225
static void gre_mk_key6(const ip6_address_t *src, const ip6_address_t *dst, u32 fib_index, u8 ttype, u16 session_id, gre_tunnel_key6_t *key)
Definition: gre.h:381
struct _vlib_node_registration vlib_node_registration_t
vnet_device_class_t gre_device_class
clib_error_t * gre_interface_admin_up_down(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: interface.c:392
ERSPAN type 2 - the tunnel is for port mirror SPAN output.
Definition: gre.h:57
struct _vnet_hw_interface_class vnet_hw_interface_class_t
L3 GRE (i.e.
Definition: gre.h:46
u64 uword
Definition: types.h:112
u32 ref_count
Definition: gre.h:160
typedef key
Definition: ipsec.api:247
unformat_function_t unformat_pg_gre_header
Definition: gre.h:338
u8 next_index
Definition: gre.h:230
gre_tunnel_key_t * key
The hash table&#39;s key stored in separate memory since the tunnel_t memory can realloc.
Definition: gre.h:187
vlib_node_registration_t gre6_input_node
(constructor) VLIB_REGISTER_NODE (gre6_input_node)
Definition: node.c:473
unformat_function_t unformat_gre_protocol_host_byte_order
Definition: gre.h:333
fib_prefix_t tunnel_dst
The tunnel&#39;s destination/remote address.
Definition: gre.h:196
static gre_protocol_info_t * gre_get_protocol_info(gre_main_t *em, gre_protocol_t protocol)
Definition: gre.h:307
gre_protocol_info_t * protocol_infos
GRE payload protocol registrations.
Definition: gre.h:247
vlib_main_t * vlib_main
Definition: gre.h:279
format_function_t format_gre_header
Definition: gre.h:323
u16 session_id
ERSPAN type 2 session ID, least significant 10 bits of u16.
Definition: gre.h:213
static void gre_mk_sn_key(const gre_tunnel_t *gt, gre_sn_key_t *key)
Definition: gre.h:403
struct gre_tunnel_key4_t_ gre_tunnel_key4_t
Key for a IPv4 GRE Tunnel.
gre_error_t
Definition: gre.h:30