FD.io VPP  v18.01.2-1-g9b554f3
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  * A GRE payload protocol registration
40  */
41 typedef struct
42 {
43  /** Name (a c string). */
44  char *name;
45 
46  /** GRE protocol type in host byte order. */
48 
49  /** Node which handles this type. */
51 
52  /** Next index for this type. */
55 
56 /**
57  * @brief The GRE tunnel type
58  */
59 typedef enum gre_tunnel_tyoe_t_
60 {
61  /**
62  * L3 GRE (i.e. this tunnel is in L3 mode)
63  */
65  /**
66  * Transparent Ethernet Bridging - the tunnel is in L2 mode
67  */
70 
71 #define GRE_TUNNEL_TYPE_NAMES { \
72  [GRE_TUNNEL_TYPE_L3] = "L3", \
73  [GRE_TUNNEL_TYPE_TEB] = "TEB", \
74 }
75 
76 #define GRE_TUNNEL_N_TYPES ((gre_tunnel_type_t)GRE_TUNNEL_TYPE_TEB+1)
77 
78 /**
79  * @brief Key for a IPv4 GRE Tunnel
80  */
81 typedef struct gre_tunnel_key4_t_
82 {
83  /**
84  * Source and destination IP addresses
85  */
86  union
87  {
88  struct
89  {
92  };
94  };
95 
96  /**
97  * The FIB table the src,dst addresses are in.
98  * tunnels with the same IP addresses in different FIBs are not
99  * the same tunnel
100  */
102 } __attribute__ ((packed)) gre_tunnel_key4_t;
103 
104 /**
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
107  */
108 typedef struct gre_tunnel_key6_t_
109 {
110  /**
111  * Source and destination IP addresses
112  */
115 
116  /**
117  * The FIB table the src,dst addresses are in.
118  * tunnels with the same IP addresses in different FIBs are not
119  * the same tunnel
120  */
122 } __attribute__ ((packed)) gre_tunnel_key6_t;
123 
124 /**
125  * Union of the two possible key types
126  */
127 typedef union gre_tunnel_key_t_
128 {
130  gre_tunnel_key6_t gtk_v6;
132 
133 /**
134  * @brief A representation of a GRE tunnel
135  */
136 typedef struct
137 {
138  /**
139  * Linkage into the FIB object graph
140  */
142 
143  /**
144  * The hash table's key stored in separate memory since the tunnel_t
145  * memory can realloc.
146  */
148 
149  /**
150  * The tunnel's source/local address
151  */
152  ip46_address_t tunnel_src;
153  /**
154  * The tunnel's destination/remote address
155  */
157  /**
158  * The FIB in which the src.dst address are present
159  */
163  gre_tunnel_type_t type;
164 
165  /**
166  * The FIB entry sourced by the tunnel for its destination prefix
167  */
169 
170  /**
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
173  * changes.
174  * The tunnels sibling index on the FIB entry's dependency list.
175  */
177 
178  /**
179  * on a L2 tunnel this is the VLIB arc from the L2-tx to the l2-midchain
180  */
182 
183  /**
184  * an L2 tunnel always rquires an L2 midchain. cache here for DP.
185  */
187 } gre_tunnel_t;
188 
189 /**
190  * @brief GRE related global data
191  */
192 typedef struct
193 {
194  /**
195  * pool of tunnel instances
196  */
198 
199  /**
200  * GRE payload protocol registrations
201  */
203 
204  /**
205  * Hash tables mapping name/protocol to protocol info index.
206  */
207  uword *protocol_info_by_name, *protocol_info_by_protocol;
208 
209  /**
210  * Hash mapping ipv4 src/dst addr pair to tunnel
211  */
213 
214  /**
215  * Hash mapping ipv6 src/dst addr pair to tunnel
216  */
218 
219  /**
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.
223  */
224  u32 *free_gre_tunnel_hw_if_indices[GRE_TUNNEL_N_TYPES];
225 
226  /**
227  * Mapping from sw_if_index to tunnel index
228  */
230 
231  /* Sparse vector mapping gre protocol in network byte order
232  to next index. */
234 
235  /* convenience */
238 } gre_main_t;
239 
240 /**
241  * @brief IPv4 and GRE header.
242  */
243 /* *INDENT-OFF* */
244 typedef CLIB_PACKED (struct {
245  ip4_header_t ip4;
246  gre_header_t gre;
247 }) ip4_and_gre_header_t;
248 /* *INDENT-ON* */
249 
250 /**
251  * @brief IPv6 and GRE header.
252  */
253 /* *INDENT-OFF* */
254 typedef CLIB_PACKED (struct {
255  ip6_header_t ip6;
256  gre_header_t gre;
257 }) ip6_and_gre_header_t;
258 /* *INDENT-ON* */
259 
262 {
263  uword *p = hash_get (em->protocol_info_by_protocol, protocol);
264  return p ? vec_elt_at_index (em->protocol_infos, p[0]) : 0;
265 }
266 
267 extern gre_main_t gre_main;
268 
269 /* Register given node index to take input for given gre type. */
270 void
272  gre_protocol_t protocol, u32 node_index);
273 
275  u32 hw_if_index, u32 flags);
276 
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);
280 
284 
289 
290 /* Parse gre protocol as 0xXXXX or protocol name.
291  In either host or network byte order. */
294 
295 /* Parse gre header. */
298 
299 void
301  gre_protocol_t protocol, u32 node_index);
302 
303 /* manually added to the interface output node in gre.c */
304 #define GRE_OUTPUT_NEXT_LOOKUP 1
305 
306 typedef struct
307 {
309 
310  ip46_address_t src, dst;
315 
317  (vnet_gre_add_del_tunnel_args_t * a, u32 * sw_if_indexp);
318 
319 static inline void
321  const ip4_address_t * dst,
322  u32 fib_index, gre_tunnel_key4_t * key)
323 {
324  key->gtk_src = *src;
325  key->gtk_dst = *dst;
326  key->gtk_fib_index = fib_index;
327 }
328 
329 static inline int
331  const gre_tunnel_key4_t * key2)
332 {
333  return ((key1->gtk_as_u64 == key2->gtk_as_u64) &&
334  (key1->gtk_fib_index == key2->gtk_fib_index));
335 }
336 
337 static inline void
339  const ip6_address_t * dst,
340  u32 fib_index, gre_tunnel_key6_t * key)
341 {
342  key->gtk_src = *src;
343  key->gtk_dst = *dst;
344  key->gtk_fib_index = fib_index;
345 }
346 
347 static inline int
349  const gre_tunnel_key6_t * key2)
350 {
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));
356 }
357 
358 #endif /* included_gre_h */
359 
360 /*
361  * fd.io coding-style-patch-verification: ON
362  *
363  * Local Variables:
364  * eval: (c-set-style "gnu")
365  * End:
366  */
vnet_main_t * vnet_main
Definition: gre.h:237
uword( unformat_function_t)(unformat_input_t *input, va_list *args)
Definition: format.h:231
vlib_node_registration_t gre4_input_node
(constructor) VLIB_REGISTER_NODE (gre4_input_node)
Definition: node.c:575
uword * tunnel_by_key6
Hash mapping ipv6 src/dst addr pair to tunnel.
Definition: gre.h:217
GRE related global data.
Definition: gre.h:192
format_function_t format_gre_header_with_length
Definition: gre.h:283
ip4_address_t gtk_dst
Definition: gre.h:91
fib_node_index_t fib_entry_index
The FIB entry sourced by the tunnel for its destination prefix.
Definition: gre.h:168
u32 hw_if_index
Definition: gre.h:161
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:108
gre_tunnel_tyoe_t_
The GRE tunnel type.
Definition: gre.h:59
typedef CLIB_PACKED(struct{ip4_header_t ip4;gre_header_t gre;}) ip4_and_gre_header_t
IPv4 and GRE header.
a
Definition: bitmap.h:516
uword * protocol_info_by_protocol
Definition: gre.h:207
static void gre_mk_key4(const ip4_address_t *src, const ip4_address_t *dst, u32 fib_index, gre_tunnel_key4_t *key)
Definition: gre.h:320
u64 as_u64[2]
Definition: ip6_packet.h:51
gre_protocol_t protocol
GRE protocol type in host byte order.
Definition: gre.h:47
char * name
Name (a c string).
Definition: gre.h:44
A GRE payload protocol registration.
Definition: gre.h:41
u32 outer_fib_index
The FIB in which the src.dst address are present.
Definition: gre.h:160
#define GRE_TUNNEL_N_TYPES
Definition: gre.h:76
static void gre_mk_key6(const ip6_address_t *src, const ip6_address_t *dst, u32 fib_index, gre_tunnel_key6_t *key)
Definition: gre.h:338
struct _vlib_node_registration vlib_node_registration_t
u8 *( format_function_t)(u8 *s, va_list *args)
Definition: format.h:48
struct _vnet_device_class vnet_device_class_t
ip4_address_t gtk_src
Definition: gre.h:90
u32 * tunnel_index_by_sw_if_index
Mapping from sw_if_index to tunnel index.
Definition: gre.h:229
u64 gtk_as_u64
Definition: gre.h:93
u32 sibling_index
The tunnel is a child of the FIB entry for its desintion.
Definition: gre.h:176
L3 GRE (i.e.
Definition: gre.h:64
u32 sw_if_index
Definition: gre.h:162
ip6_address_t gtk_dst
Definition: gre.h:114
#define always_inline
Definition: clib.h:92
void gre_tunnel_stack(adj_index_t ai)
gre_tunnel_stack
Definition: interface.c:138
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
Aggregrate type for a prefix.
Definition: fib_types.h:172
unsigned long u64
Definition: types.h:89
enum gre_tunnel_tyoe_t_ gre_tunnel_type_t
The GRE tunnel type.
adj_index_t l2_adj_index
an L2 tunnel always rquires an L2 midchain.
Definition: gre.h:186
unformat_function_t unformat_gre_header
Definition: gre.h:296
ip46_address_t tunnel_src
The tunnel&#39;s source/local address.
Definition: gre.h:152
#define hash_get(h, key)
Definition: hash.h:248
uword * tunnel_by_key4
Hash mapping ipv4 src/dst addr pair to tunnel.
Definition: gre.h:212
A representation of a GRE tunnel.
Definition: gre.h:136
ip6_address_t gtk_src
Source and destination IP addresses.
Definition: gre.h:113
unformat_function_t unformat_gre_protocol_net_byte_order
Definition: gre.h:293
static int gre_match_key4(const gre_tunnel_key4_t *key1, const gre_tunnel_key4_t *key2)
Definition: gre.h:330
gre_tunnel_key4_t gtk_v4
Definition: gre.h:129
u32 node_index
Node which handles this type.
Definition: gre.h:50
vnet_device_class_t gre_device_teb_class
static int gre_match_key6(const gre_tunnel_key6_t *key1, const gre_tunnel_key6_t *key2)
Definition: gre.h:348
An node in the FIB graph.
Definition: fib_node.h:286
Key for a IPv4 GRE Tunnel.
Definition: gre.h:81
union gre_tunnel_key_t_ gre_tunnel_key_t
Union of the two possible key types.
gre_tunnel_type_t type
Definition: gre.h:163
gre_tunnel_key6_t gtk_v6
Definition: gre.h:130
format_function_t format_gre_protocol
Definition: gre.h:281
gre_protocol_t
Definition: packet.h:29
u32 fib_node_index_t
A typedef of a node index.
Definition: fib_types.h:29
u32 adj_index_t
An index for adjacencies.
Definition: adj_types.h:30
u32 gtk_fib_index
The FIB table the src,dst addresses are in.
Definition: gre.h:101
unsigned int u32
Definition: types.h:88
u16 * next_by_protocol
Definition: gre.h:233
u32 next_index
Next index for this type.
Definition: gre.h:53
void gre_update_adj(vnet_main_t *vnm, u32 sw_if_index, adj_index_t ai)
Definition: gre.c:292
gre_tunnel_t * tunnels
pool of tunnel instances
Definition: gre.h:197
gre_main_t gre_main
Definition: gre.c:22
Union of the two possible key types.
Definition: gre.h:127
vnet_hw_interface_class_t gre_hw_interface_class
u64 uword
Definition: types.h:112
u32 l2_tx_arc
on a L2 tunnel this is the VLIB arc from the L2-tx to the l2-midchain
Definition: gre.h:181
fib_node_t node
Linkage into the FIB object graph.
Definition: gre.h:141
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:482
unsigned short u16
Definition: types.h:57
int vnet_gre_add_del_tunnel(vnet_gre_add_del_tunnel_args_t *a, u32 *sw_if_indexp)
Definition: interface.c:472
u32 gtk_fib_index
The FIB table the src,dst addresses are in.
Definition: gre.h:121
struct _vnet_hw_interface_class vnet_hw_interface_class_t
unsigned char u8
Definition: types.h:56
Transparent Ethernet Bridging - the tunnel is in L2 mode.
Definition: gre.h:68
void gre_register_input_type(vlib_main_t *vm, gre_protocol_t protocol, u32 node_index)
unformat_function_t unformat_pg_gre_header
Definition: gre.h:297
void gre_register_input_protocol(vlib_main_t *vm, gre_protocol_t protocol, u32 node_index)
Definition: node.c:625
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:147
vlib_node_registration_t gre6_input_node
(constructor) VLIB_REGISTER_NODE (gre6_input_node)
Definition: node.c:598
unformat_function_t unformat_gre_protocol_host_byte_order
Definition: gre.h:292
fib_prefix_t tunnel_dst
The tunnel&#39;s destination/remote address.
Definition: gre.h:156
static gre_protocol_info_t * gre_get_protocol_info(gre_main_t *em, gre_protocol_t protocol)
Definition: gre.h:261
gre_protocol_info_t * protocol_infos
GRE payload protocol registrations.
Definition: gre.h:202
vlib_main_t * vlib_main
Definition: gre.h:236
format_function_t format_gre_header
Definition: gre.h:282
ip46_address_t src
Definition: gre.h:310
gre_error_t
Definition: gre.h:30