4a8bdfe7f935b9a3fbb66abca33ce129789f1665
[vpp.git] / vnet / vnet / lisp-gpe / lisp_gpe.h
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /**
16  * @file
17  * @brief LISP-GPE definitions.
18  */
19
20 #ifndef included_vnet_lisp_gpe_h
21 #define included_vnet_lisp_gpe_h
22
23 #include <vppinfra/error.h>
24 #include <vppinfra/mhash.h>
25 #include <vnet/vnet.h>
26 #include <vnet/ip/ip.h>
27 #include <vnet/l2/l2_input.h>
28 #include <vnet/ethernet/ethernet.h>
29 #include <vnet/ip/ip4_packet.h>
30 #include <vnet/ip/udp.h>
31 #include <vnet/lisp-cp/lisp_types.h>
32 #include <vnet/lisp-gpe/lisp_gpe_packet.h>
33
34 /** IP4-UDP-LISP encap header */
35 /* *INDENT-OFF* */
36 typedef CLIB_PACKED (struct {
37   ip4_header_t ip4;             /* 20 bytes */
38   udp_header_t udp;             /* 8 bytes */
39   lisp_gpe_header_t lisp;       /* 8 bytes */
40 }) ip4_udp_lisp_gpe_header_t;
41 /* *INDENT-ON* */
42
43 /** IP6-UDP-LISP encap header */
44 /* *INDENT-OFF* */
45 typedef CLIB_PACKED (struct {
46   ip6_header_t ip6;             /* 40 bytes */
47   udp_header_t udp;             /* 8 bytes */
48   lisp_gpe_header_t lisp;       /* 8 bytes */
49 }) ip6_udp_lisp_gpe_header_t;
50 /* *INDENT-ON* */
51
52 /** LISP-GPE tunnel key */
53 typedef struct
54 {
55   union
56   {
57     struct
58     {
59       dp_address_t rmt;
60       dp_address_t lcl;
61       u32 vni;
62     };
63     u8 as_u8[40];
64   };
65 } lisp_gpe_tunnel_key_t;
66
67 typedef struct lisp_gpe_sub_tunnel
68 {
69   /** Rewrite string. $$$$ embed vnet_rewrite header */
70   u8 *rewrite;
71   u32 parent_index;
72   u32 locator_pair_index;
73   u8 weight;
74   u8 is_ip4;
75 } lisp_gpe_sub_tunnel_t;
76
77 typedef struct nomalized_sub_tunnel
78 {
79   u32 sub_tunnel_index;
80   u8 weight;
81 } normalized_sub_tunnel_weights_t;
82
83 /** LISP-GPE tunnel structure */
84 typedef struct
85 {
86   /** tunnel src and dst addresses */
87   locator_pair_t *locator_pairs;
88
89   /** locator-pairs with best priority become sub-tunnels */
90   lisp_gpe_sub_tunnel_t *sub_tunnels;
91
92   /** sub-tunnels load balancing vector: contains list of sub-tunnel
93    * indexes replicated according to weight */
94   u32 *sub_tunnels_lbv;
95
96   /** number of entries in load balancing vector */
97   u32 sub_tunnels_lbv_count;
98
99   /** normalized sub tunnel weights */
100   normalized_sub_tunnel_weights_t *norm_sub_tunnel_weights;
101
102   /** decap next index */
103   u32 decap_next_index;
104
105   /* TODO remove */
106   ip_address_t src, dst;
107
108   /** FIB indices */
109   u32 encap_fib_index;          /* tunnel partner lookup here */
110   u32 decap_fib_index;          /* inner IP lookup here */
111
112   /** vnet intfc hw/sw_if_index */
113   u32 hw_if_index;
114   u32 sw_if_index;
115
116   /** action for 'negative' tunnels */
117   u8 action;
118
119   /** LISP header fields in HOST byte order */
120   u8 flags;
121   u8 ver_res;
122   u8 res;
123   u8 next_protocol;
124   u32 vni;
125 } lisp_gpe_tunnel_t;
126
127 #define foreach_lisp_gpe_ip_input_next          \
128 _(DROP, "error-drop")                           \
129 _(IP4_INPUT, "ip4-input")                       \
130 _(IP6_INPUT, "ip6-input")                       \
131 _(L2_INPUT, "l2-input")
132
133 /** Enum of possible next nodes post LISP-GPE decap */
134 typedef enum
135 {
136 #define _(s,n) LISP_GPE_INPUT_NEXT_##s,
137   foreach_lisp_gpe_ip_input_next
138 #undef _
139     LISP_GPE_INPUT_N_NEXT,
140 } lisp_gpe_input_next_t;
141
142 typedef enum
143 {
144 #define lisp_gpe_error(n,s) LISP_GPE_ERROR_##n,
145 #include <vnet/lisp-gpe/lisp_gpe_error.def>
146 #undef lisp_gpe_error
147   LISP_GPE_N_ERROR,
148 } lisp_gpe_error_t;
149
150 /** IP4 source FIB.
151  * As a first step, reuse v4 fib. The goal of the typedef is
152  * to shield consumers from future updates that may result in the lisp ip4 fib
153  * diverging from ip4 fib
154  */
155 typedef ip4_fib_t ip4_src_fib_t;
156
157 /** IP6 source FIB */
158 typedef struct ip6_src_fib
159 {
160   BVT (clib_bihash) ip6_lookup_table;
161
162   /** bitmap/vector of mask widths to search */
163   uword *non_empty_dst_address_length_bitmap;
164   u8 *prefix_lengths_in_search_order;
165   ip6_address_t fib_masks[129];
166   i32 dst_address_length_refcounts[129];
167
168   /** ip6 lookup table config parameters */
169   u32 lookup_table_nbuckets;
170   uword lookup_table_size;
171 } ip6_src_fib_t;
172
173 /** Tunnel lookup structure for L2 and L3 tunnels */
174 typedef struct tunnel_lookup
175 {
176   /** Lookup lisp-gpe interfaces by dp table (eg. vrf/bridge index) */
177   uword *hw_if_index_by_dp_table;
178
179   /** lookup decap tunnel termination sw_if_index by vni and vice versa */
180   uword *sw_if_index_by_vni;
181   uword *vni_by_sw_if_index;
182 } tunnel_lookup_t;
183
184 /** LISP-GPE global state*/
185 typedef struct lisp_gpe_main
186 {
187   /** pool of encap tunnel instances */
188   lisp_gpe_tunnel_t *tunnels;
189
190   /** lookup tunnel by key */
191   mhash_t lisp_gpe_tunnel_by_key;
192
193   /** Free vlib hw_if_indices */
194   u32 *free_tunnel_hw_if_indices;
195
196   u8 is_en;
197
198   /* L3 data structures
199    * ================== */
200
201   /** Pool of src fibs that are paired with dst fibs */
202   ip4_src_fib_t *ip4_src_fibs;
203   ip6_src_fib_t *ip6_src_fibs;
204
205   tunnel_lookup_t l3_ifaces;
206
207   /** Lookup lgpe_ipX_lookup_next by vrf */
208   uword *lgpe_ip4_lookup_next_index_by_table_id;
209   uword *lgpe_ip6_lookup_next_index_by_table_id;
210
211   /** next node indexes that point ip4/6 lookup to lisp gpe ip lookup */
212   u32 ip4_lookup_next_lgpe_ip4_lookup;
213   u32 ip6_lookup_next_lgpe_ip6_lookup;
214
215   /* L2 data structures
216    * ================== */
217
218   /** L2 LISP FIB */
219     BVT (clib_bihash) l2_fib;
220
221   tunnel_lookup_t l2_ifaces;
222
223   /** convenience */
224   vlib_main_t *vlib_main;
225   vnet_main_t *vnet_main;
226   ip4_main_t *im4;
227   ip6_main_t *im6;
228   ip_lookup_main_t *lm4;
229   ip_lookup_main_t *lm6;
230 } lisp_gpe_main_t;
231
232 /** LISP-GPE global state*/
233 lisp_gpe_main_t lisp_gpe_main;
234
235 always_inline lisp_gpe_main_t *
236 vnet_lisp_gpe_get_main ()
237 {
238   return &lisp_gpe_main;
239 }
240
241 extern vlib_node_registration_t lgpe_ip4_lookup_node;
242 extern vlib_node_registration_t lgpe_ip6_lookup_node;
243 extern vlib_node_registration_t lisp_gpe_ip4_input_node;
244 extern vlib_node_registration_t lisp_gpe_ip6_input_node;
245
246 u8 *format_lisp_gpe_header_with_length (u8 * s, va_list * args);
247
248 /** Arguments to add an L2/L3 LISP-GPE interface*/
249 typedef struct
250 {
251   u8 is_add;
252   union
253   {
254     /** vrf */
255     u32 table_id;
256
257     /** bridge domain */
258     u16 bd_id;
259
260     /** generic access */
261     u32 dp_table;
262   };
263   u8 is_l2;
264
265   /** virtual network identifier in host byte order */
266   u32 vni;
267 } vnet_lisp_gpe_add_del_iface_args_t;
268
269 /** Read LISP-GPE status */
270 u8 vnet_lisp_gpe_enable_disable_status (void);
271
272 /** Add/del LISP-GPE interface. */
273 int
274 vnet_lisp_gpe_add_del_iface (vnet_lisp_gpe_add_del_iface_args_t * a,
275                              u32 * hw_if_indexp);
276
277 typedef struct
278 {
279   u8 is_en;
280 } vnet_lisp_gpe_enable_disable_args_t;
281
282 clib_error_t
283   * vnet_lisp_gpe_enable_disable (vnet_lisp_gpe_enable_disable_args_t * a);
284
285 /** */
286 typedef struct
287 {
288   u8 is_add;
289
290   /** type of mapping */
291   u8 is_negative;
292
293   /** action for negative mappings */
294   u8 action;
295
296   /** local eid */
297   gid_address_t lcl_eid;
298
299   /** remote eid */
300   gid_address_t rmt_eid;
301
302   /** vector of locator pairs */
303   locator_pair_t *locator_pairs;
304
305   /** FIB index to lookup remote locator at encap */
306   u32 encap_fib_index;
307
308   /** FIB index to lookup inner IP at decap */
309   u32 decap_fib_index;
310
311   /* TODO remove */
312   u32 decap_next_index;
313
314   /** VNI/tenant id in HOST byte order */
315   u32 vni;
316
317   /** vrf or bd where fwd entry should be inserted */
318   union
319   {
320     /** table (vrf) id */
321     u32 table_id;
322
323     /** bridge domain id */
324     u16 bd_id;
325
326     /** generic access */
327     u32 dp_table;
328   };
329 } vnet_lisp_gpe_add_del_fwd_entry_args_t;
330
331 int
332 vnet_lisp_gpe_add_del_fwd_entry (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
333                                  u32 * hw_if_indexp);
334
335 int
336 ip_sd_fib_add_del_route (lisp_gpe_main_t * lgm, ip_prefix_t * dst_prefix,
337                          ip_prefix_t * src_prefix, u32 table_id,
338                          ip_adjacency_t * add_adj, u8 is_add);
339 u32
340 ip_sd_fib_get_route (lisp_gpe_main_t * lgm, ip_prefix_t * dst_prefix,
341                      ip_prefix_t * src_prefix, u32 table_id);
342
343 #define foreach_lgpe_ip4_lookup_next    \
344   _(DROP, "error-drop")                 \
345   _(LISP_CP_LOOKUP, "lisp-cp-lookup")
346
347 typedef enum lgpe_ip4_lookup_next
348 {
349 #define _(sym,str) LGPE_IP4_LOOKUP_NEXT_##sym,
350   foreach_lgpe_ip4_lookup_next
351 #undef _
352     LGPE_IP4_LOOKUP_N_NEXT,
353 } lgpe_ip4_lookup_next_t;
354
355 #define foreach_lgpe_ip6_lookup_next    \
356   _(DROP, "error-drop")                 \
357   _(LISP_CP_LOOKUP, "lisp-cp-lookup")
358
359 typedef enum lgpe_ip6_lookup_next
360 {
361 #define _(sym,str) LGPE_IP6_LOOKUP_NEXT_##sym,
362   foreach_lgpe_ip6_lookup_next
363 #undef _
364     LGPE_IP6_LOOKUP_N_NEXT,
365 } lgpe_ip6_lookup_next_t;
366
367 u8 *format_vnet_lisp_gpe_status (u8 * s, va_list * args);
368
369 #define L2_FIB_DEFAULT_HASH_NUM_BUCKETS (64 * 1024)
370 #define L2_FIB_DEFAULT_HASH_MEMORY_SIZE (32<<20)
371
372 u32
373 lisp_l2_fib_lookup (lisp_gpe_main_t * lgm, u16 bd_index, u8 src_mac[8],
374                     u8 dst_mac[8]);
375
376 #endif /* included_vnet_lisp_gpe_h */
377
378 /*
379  * fd.io coding-style-patch-verification: ON
380  *
381  * Local Variables:
382  * eval: (c-set-style "gnu")
383  * End:
384  */