FD.io VPP  v20.01-48-g3e0dafb74
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 
30 
31 typedef enum
32 {
33 #define gre_error(n,s) GRE_ERROR_##n,
34 #include <vnet/gre/error.def>
35 #undef gre_error
37 } gre_error_t;
38 
39 /**
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
46  */
47 #define foreach_gre_tunnel_type \
48  _(L3, "L3") \
49  _(TEB, "TEB") \
50  _(ERSPAN, "ERSPAN") \
51 
52 /**
53  * @brief The GRE tunnel type
54  */
55 typedef enum gre_tunnel_type_t_
56 {
57 #define _(n, s) GRE_TUNNEL_TYPE_##n,
59 #undef _
60 } __clib_packed gre_tunnel_type_t;
61 
62 extern u8 *format_gre_tunnel_type (u8 * s, va_list * args);
63 
64 #define foreach_gre_tunnel_mode \
65  _(P2P, "point-to-point") \
66  _(MP, "multi-point") \
67 
68 typedef enum gre_tunnel_mode_t_
69 {
70 #define _(n, s) GRE_TUNNEL_MODE_##n,
72 #undef _
73 } __clib_packed gre_tunnel_mode_t;
74 
75 extern u8 *format_gre_tunnel_mode (u8 * s, va_list * args);
76 
77 /**
78  * A GRE payload protocol registration
79  */
80 typedef struct
81 {
82  /** Name (a c string). */
83  char *name;
84 
85  /** GRE protocol type in host byte order. */
87 
88  /** GRE tunnel type */
89  gre_tunnel_type_t tunnel_type;
90 
91  /** Node which handles this type. */
93 
94  /** Next index for this type. */
97 
98 /**
99  * Elements of the GRE key that are common for v6 and v6 addresses
100  */
102 {
103  union
104  {
105  struct
106  {
109  gre_tunnel_type_t type;
110  gre_tunnel_mode_t mode;
111  };
113  };
115 
117 
118 /**
119  * @brief Key for a IPv4 GRE Tunnel
120  */
121 typedef struct gre_tunnel_key4_t_
122 {
123  /**
124  * Source and destination IP addresses
125  */
126  union
127  {
128  struct
129  {
132  };
134  };
135 
136  /** address independent attributes */
138 } __attribute__ ((packed)) gre_tunnel_key4_t;
139 
141 
142 /**
143  * @brief Key for a IPv6 GRE Tunnel
144  * We use a different type so that the V4 key hash is as small as possible
145  */
146 typedef struct gre_tunnel_key6_t_
147 {
148  /**
149  * Source and destination IP addresses
150  */
153 
154  /** address independent attributes */
156 } __attribute__ ((packed)) gre_tunnel_key6_t;
157 
159 
160 /**
161  * Union of the two possible key types
162  */
163 typedef union gre_tunnel_key_t_
164 {
168 
169 /**
170  * The session ID is only a 10 bit value
171  */
172 #define GTK_SESSION_ID_MAX (0x3ff)
173 
174 /**
175  * Used for GRE header seq number generation for ERSPAN encap
176  */
177 typedef struct
178 {
181 } gre_sn_t;
182 
183 /**
184  * Hash key for GRE header seq number generation for ERSPAN encap
185  */
186 typedef struct
187 {
188  ip46_address_t src;
189  ip46_address_t dst;
191 } gre_sn_key_t;
192 
193 /**
194  * @brief A representation of a GRE tunnel
195  */
196 typedef struct
197 {
198  /**
199  * Required for pool_get_aligned
200  */
201  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
202 
203  /**
204  * The tunnel's source/local address
205  */
206  ip46_address_t tunnel_src;
207  /**
208  * The tunnel's destination/remote address
209  */
211  /**
212  * The FIB in which the src.dst address are present
213  */
217  gre_tunnel_type_t type;
218  gre_tunnel_mode_t mode;
219 
220  /**
221  * an L2 tunnel always rquires an L2 midchain. cache here for DP.
222  */
224 
225  /**
226  * ERSPAN type 2 session ID, least significant 10 bits of u16
227  */
229 
230  /**
231  * GRE header sequence number (SN) used for ERSPAN type 2 header, must be
232  * bumped automically to be thread safe. As multiple GRE tunnels are created
233  * for the same fib-idx/DIP/SIP with different ERSPAN session number, they all
234  * share the same SN which is kept per FIB/DIP/SIP, as specified by RFC2890.
235  */
237 
238 
239  u32 dev_instance; /* Real device instance in tunnel vector */
240  u32 user_instance; /* Instance name being shown to user */
241 } gre_tunnel_t;
242 
243 typedef struct
244 {
247 } next_info_t;
248 
249 /**
250  * @brief GRE related global data
251  */
252 typedef struct
253 {
254  /**
255  * pool of tunnel instances
256  */
258 
259  /**
260  * GRE payload protocol registrations
261  */
263 
264  /**
265  * Hash tables mapping name/protocol to protocol info index.
266  */
267  uword *protocol_info_by_name, *protocol_info_by_protocol;
268 
269  /**
270  * Hash mapping to tunnels with ipv4 src/dst addr
271  */
273 
274  /**
275  * Hash mapping to tunnels with ipv6 src/dst addr
276  */
278 
279  /**
280  * Hash mapping tunnel src/dst addr and fib-idx to sequence number
281  */
283 
284  /**
285  * Mapping from sw_if_index to tunnel index
286  */
288 
289  /* Sparse vector mapping gre protocol in network byte order
290  to next index. */
292 
293  /* convenience */
296 
297  /* Record used instances */
299 } gre_main_t;
300 
301 /**
302  * @brief IPv4 and GRE header.
303  */
304 /* *INDENT-OFF* */
305 typedef CLIB_PACKED (struct {
307  gre_header_t gre;
308 }) ip4_and_gre_header_t;
309 /* *INDENT-ON* */
310 
311 /**
312  * @brief IPv6 and GRE header.
313  */
314 /* *INDENT-OFF* */
315 typedef CLIB_PACKED (struct {
317  gre_header_t gre;
318 }) ip6_and_gre_header_t;
319 /* *INDENT-ON* */
320 
323 {
324  uword *p = hash_get (em->protocol_info_by_protocol, protocol);
325  return p ? vec_elt_at_index (em->protocol_infos, p[0]) : 0;
326 }
327 
328 extern gre_main_t gre_main;
329 
331  u32 hw_if_index, u32 flags);
332 
333 extern void gre_tunnel_stack (adj_index_t ai);
334 extern void gre_update_adj (vnet_main_t * vnm,
335  u32 sw_if_index, adj_index_t ai);
336 
340 
345 
346 /* Parse gre protocol as 0xXXXX or protocol name.
347  In either host or network byte order. */
350 
351 /* Parse gre header. */
354 
355 void
357  u32 node_index, gre_tunnel_type_t tunnel_type);
358 
359 /* manually added to the interface output node in gre.c */
360 #define GRE_OUTPUT_NEXT_LOOKUP 1
361 
362 typedef struct
363 {
365  gre_tunnel_type_t type;
366  gre_tunnel_mode_t mode;
369  ip46_address_t src, dst;
373 
375  u32 * sw_if_indexp);
376 
377 static inline void
380  u32 fib_index,
381  gre_tunnel_type_t ttype,
382  gre_tunnel_mode_t tmode, u16 session_id, gre_tunnel_key4_t * key)
383 {
384  key->gtk_src = src;
385  key->gtk_dst = dst;
386  key->gtk_common.type = ttype;
387  key->gtk_common.mode = tmode;
390 }
391 
392 static inline int
394  const gre_tunnel_key4_t * key2)
395 {
396  return ((key1->gtk_as_u64 == key2->gtk_as_u64) &&
397  (key1->gtk_common.as_u64 == key2->gtk_common.as_u64));
398 }
399 
400 static inline void
402  const ip6_address_t * dst,
403  u32 fib_index,
404  gre_tunnel_type_t ttype,
405  gre_tunnel_mode_t tmode, u16 session_id, gre_tunnel_key6_t * key)
406 {
407  key->gtk_src = *src;
408  key->gtk_dst = *dst;
409  key->gtk_common.type = ttype;
410  key->gtk_common.mode = tmode;
413 }
414 
415 static inline int
417  const gre_tunnel_key6_t * key2)
418 {
419  return (ip6_address_is_equal (&key1->gtk_src, &key2->gtk_src) &&
420  ip6_address_is_equal (&key1->gtk_dst, &key2->gtk_dst) &&
421  (key1->gtk_common.as_u64 == key2->gtk_common.as_u64));
422 }
423 
424 static inline void
426 {
427  key->src = gt->tunnel_src;
428  key->dst = gt->tunnel_dst.fp_addr;
429  key->fib_index = gt->outer_fib_index;
430 }
431 
432 #endif /* included_gre_h */
433 
434 /*
435  * fd.io coding-style-patch-verification: ON
436  *
437  * Local Variables:
438  * eval: (c-set-style "gnu")
439  * End:
440  */
vnet_main_t * vnet_main
Definition: gre.h:295
uword * seq_num_by_key
Hash mapping tunnel src/dst addr and fib-idx to sequence number.
Definition: gre.h:282
vlib_node_registration_t gre_encap_node
(constructor) VLIB_REGISTER_NODE (gre_encap_node)
Definition: gre.c:537
vlib_node_registration_t gre4_input_node
(constructor) VLIB_REGISTER_NODE (gre4_input_node)
Definition: node.c:454
uword * tunnel_by_key6
Hash mapping to tunnels with ipv6 src/dst addr.
Definition: gre.h:277
GRE related global data.
Definition: gre.h:252
#define CLIB_CACHE_LINE_ALIGN_MARK(mark)
Definition: cache.h:60
format_function_t format_gre_header_with_length
Definition: gre.h:339
ip4_address_t gtk_dst
Definition: gre.h:131
u32 hw_if_index
Definition: gre.h:215
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:146
a
Definition: bitmap.h:538
uword * protocol_info_by_protocol
Definition: gre.h:267
unsigned long u64
Definition: types.h:89
struct gre_tunnel_key_common_t_ gre_tunnel_key_common_t
Elements of the GRE key that are common for v6 and v6 addresses.
gre_protocol_t protocol
GRE protocol type in host byte order.
Definition: gre.h:86
char * name
Name (a c string).
Definition: gre.h:83
A GRE payload protocol registration.
Definition: gre.h:80
int vnet_gre_tunnel_add_del(vnet_gre_tunnel_add_del_args_t *a, u32 *sw_if_indexp)
Definition: interface.c:505
u32 outer_fib_index
The FIB in which the src.dst address are present.
Definition: gre.h:214
vl_api_address_t src
Definition: gre.api:60
STATIC_ASSERT_SIZEOF(gre_tunnel_key_common_t, sizeof(u64))
#define foreach_gre_tunnel_mode
Definition: gre.h:64
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:55
struct _vnet_device_class vnet_device_class_t
unsigned char u8
Definition: types.h:56
ip4_address_t gtk_src
Definition: gre.h:130
u32 * tunnel_index_by_sw_if_index
Mapping from sw_if_index to tunnel index.
Definition: gre.h:287
u64 gtk_as_u64
Definition: gre.h:133
u32 seq_num
Definition: gre.h:179
static void gre_mk_key4(ip4_address_t src, ip4_address_t dst, u32 fib_index, gre_tunnel_type_t ttype, gre_tunnel_mode_t tmode, u16 session_id, gre_tunnel_key4_t *key)
Definition: gre.h:378
vnet_hw_interface_class_t mgre_hw_interface_class
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:186
vl_api_ip_proto_t protocol
Definition: lb_types.api:71
u32 sw_if_index
Definition: gre.h:216
u32 dev_instance
Definition: gre.h:239
ip6_address_t gtk_dst
Definition: gre.h:152
void gre_tunnel_stack(adj_index_t ai)
gre_tunnel_stack
Definition: interface.c:149
gre_tunnel_mode_t mode
Definition: gre.h:218
gre_tunnel_type_t tunnel_type
GRE tunnel type.
Definition: gre.h:89
#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:369
unsigned int u32
Definition: types.h:88
adj_index_t l2_adj_index
an L2 tunnel always rquires an L2 midchain.
Definition: gre.h:223
unformat_function_t unformat_gre_header
Definition: gre.h:352
ip46_address_t tunnel_src
The tunnel&#39;s source/local address.
Definition: gre.h:206
#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:272
A representation of a GRE tunnel.
Definition: gre.h:196
uword * instance_used
Definition: gre.h:298
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:151
unformat_function_t unformat_gre_protocol_net_byte_order
Definition: gre.h:349
static void gre_mk_key6(const ip6_address_t *src, const ip6_address_t *dst, u32 fib_index, gre_tunnel_type_t ttype, gre_tunnel_mode_t tmode, u16 session_id, gre_tunnel_key6_t *key)
Definition: gre.h:401
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:393
gre_tunnel_key4_t gtk_v4
Definition: gre.h:165
u32 node_index
Node which handles this type.
Definition: gre.h:92
#define always_inline
Definition: ipsec.h:28
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:499
gre_tunnel_type_t type
Definition: gre.h:109
static int gre_match_key6(const gre_tunnel_key6_t *key1, const gre_tunnel_key6_t *key2)
Definition: gre.h:416
uword() unformat_function_t(unformat_input_t *input, va_list *args)
Definition: format.h:233
vl_api_address_t dst
Definition: gre.api:61
gre_tunnel_mode_t mode
Definition: gre.h:366
vlib_main_t * vm
Definition: in2out_ed.c:1810
u8 ip6[16]
Definition: one.api:477
Key for a IPv4 GRE Tunnel.
Definition: gre.h:121
union gre_tunnel_key_t_ gre_tunnel_key_t
Union of the two possible key types.
gre_tunnel_type_t type
Definition: gre.h:217
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:236
gre_tunnel_key6_t gtk_v6
Definition: gre.h:166
u8 tunnel_type
Definition: gre.h:246
format_function_t format_gre_protocol
Definition: gre.h:337
ip46_address_t src
Definition: gre.h:188
gre_protocol_t
Definition: packet.h:30
ip46_address_t dst
Definition: gre.h:189
gre_tunnel_mode_t mode
Definition: gre.h:110
Used for GRE header seq number generation for ERSPAN encap.
Definition: gre.h:177
u32 adj_index_t
An index for adjacencies.
Definition: adj_types.h:30
gre_tunnel_type_t type
Definition: gre.h:365
u32 next_index
Next index for this type.
Definition: gre.h:95
Elements of the GRE key that are common for v6 and v6 addresses.
Definition: gre.h:101
void gre_update_adj(vnet_main_t *vnm, u32 sw_if_index, adj_index_t ai)
Definition: gre.c:310
gre_tunnel_t * tunnels
pool of tunnel instances
Definition: gre.h:257
u32 fib_index
Definition: gre.h:190
gre_main_t gre_main
static uword ip6_address_is_equal(const ip6_address_t *a, const ip6_address_t *b)
Definition: ip6_packet.h:164
next_info_t * next_by_protocol
Definition: gre.h:291
Union of the two possible key types.
Definition: gre.h:163
vnet_hw_interface_class_t gre_hw_interface_class
u32 user_instance
Definition: gre.h:240
typedef key
Definition: ipsec_types.api:83
struct _vlib_node_registration vlib_node_registration_t
enum gre_tunnel_mode_t_ gre_tunnel_mode_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:531
gre_tunnel_mode_t_
Definition: gre.h:68
struct _vnet_hw_interface_class vnet_hw_interface_class_t
u8 * format_gre_tunnel_type(u8 *s, va_list *args)
Definition: interface.c:31
u64 uword
Definition: types.h:112
u32 ref_count
Definition: gre.h:180
u8 * format_gre_tunnel_mode(u8 *s, va_list *args)
Definition: interface.c:48
#define foreach_gre_tunnel_type
L3: GRE (i.e.
Definition: gre.h:47
unformat_function_t unformat_pg_gre_header
Definition: gre.h:353
u8 next_index
Definition: gre.h:245
gre_tunnel_key_common_t gtk_common
address independent attributes
Definition: gre.h:155
vlib_node_registration_t gre6_input_node
(constructor) VLIB_REGISTER_NODE (gre6_input_node)
Definition: node.c:474
unformat_function_t unformat_gre_protocol_host_byte_order
Definition: gre.h:348
enum gre_tunnel_type_t_ gre_tunnel_type_t
The GRE tunnel type.
fib_prefix_t tunnel_dst
The tunnel&#39;s destination/remote address.
Definition: gre.h:210
static gre_protocol_info_t * gre_get_protocol_info(gre_main_t *em, gre_protocol_t protocol)
Definition: gre.h:322
gre_protocol_info_t * protocol_infos
GRE payload protocol registrations.
Definition: gre.h:262
vlib_main_t * vlib_main
Definition: gre.h:294
u32 ip4
Definition: one.api:440
format_function_t format_gre_header
Definition: gre.h:338
u16 session_id
ERSPAN type 2 session ID, least significant 10 bits of u16.
Definition: gre.h:228
static void gre_mk_sn_key(const gre_tunnel_t *gt, gre_sn_key_t *key)
Definition: gre.h:425
gre_tunnel_key_common_t gtk_common
address independent attributes
Definition: gre.h:137
struct gre_tunnel_key4_t_ gre_tunnel_key4_t
Key for a IPv4 GRE Tunnel.
gre_error_t
Definition: gre.h:31