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