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