LISP multihoming API changes and cleanup
[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 #ifndef included_vnet_lisp_gpe_h
17 #define included_vnet_lisp_gpe_h
18
19 #include <vppinfra/error.h>
20 #include <vppinfra/mhash.h>
21 #include <vnet/vnet.h>
22 #include <vnet/ip/ip.h>
23 #include <vnet/l2/l2_input.h>
24 #include <vnet/ethernet/ethernet.h>
25 #include <vnet/ip/ip4_packet.h>
26 #include <vnet/ip/udp.h>
27 #include <vnet/lisp-cp/lisp_types.h>
28 #include <vnet/lisp-gpe/lisp_gpe_packet.h>
29
30 /* encap headers */
31 typedef CLIB_PACKED (struct {
32   ip4_header_t ip4;             /* 20 bytes */
33   udp_header_t udp;             /* 8 bytes */
34   lisp_gpe_header_t lisp;       /* 8 bytes */
35 }) ip4_udp_lisp_gpe_header_t;
36
37 typedef CLIB_PACKED (struct {
38   ip6_header_t ip6;             /* 40 bytes */
39   udp_header_t udp;             /* 8 bytes */
40   lisp_gpe_header_t lisp;       /* 8 bytes */
41 }) ip6_udp_lisp_gpe_header_t;
42
43 typedef struct
44 {
45   union
46   {
47     struct
48     {
49       dp_address_t rmt;
50       dp_address_t lcl;
51       u32 vni;
52     };
53     u8 as_u8[40];
54   };
55 } lisp_gpe_tunnel_key_t;
56
57 typedef struct
58 {
59   /* Rewrite string. $$$$ embed vnet_rewrite header */
60   u8 * rewrite;
61
62   /* decap next index */
63   u32 decap_next_index;
64
65   /* tunnel src and dst addresses */
66   ip_address_t src;
67   ip_address_t dst;
68
69   /* FIB indices */
70   u32 encap_fib_index;          /* tunnel partner lookup here */
71   u32 decap_fib_index;          /* inner IP lookup here */
72
73   /* vnet intfc hw/sw_if_index */
74   u32 hw_if_index;
75   u32 sw_if_index;
76
77   /* action for 'negative' tunnels */
78   u8 action;
79
80   /* LISP header fields in HOST byte order */
81   u8 flags;
82   u8 ver_res;
83   u8 res;
84   u8 next_protocol;
85   u32 vni;
86 } lisp_gpe_tunnel_t;
87
88 #define foreach_lisp_gpe_ip_input_next          \
89 _(DROP, "error-drop")                           \
90 _(IP4_INPUT, "ip4-input")                       \
91 _(IP6_INPUT, "ip6-input")                       \
92 _(L2_INPUT, "l2-input")
93
94 typedef enum {
95 #define _(s,n) LISP_GPE_INPUT_NEXT_##s,
96   foreach_lisp_gpe_ip_input_next
97 #undef _
98   LISP_GPE_INPUT_N_NEXT,
99 } lisp_gpe_input_next_t;
100
101 typedef enum {
102 #define lisp_gpe_error(n,s) LISP_GPE_ERROR_##n,
103 #include <vnet/lisp-gpe/lisp_gpe_error.def>
104 #undef lisp_gpe_error
105   LISP_GPE_N_ERROR,
106 } lisp_gpe_error_t;
107
108 /* As a first step, reuse v4 fib. The goal of the typedef is to shield
109  * consumers from future updates that may result in the lisp ip4 fib diverging
110  * from ip4 fib */
111 typedef ip4_fib_t ip4_src_fib_t;
112
113 typedef struct ip6_src_fib
114 {
115   BVT(clib_bihash) ip6_lookup_table;
116
117   /* bitmap/vector of mask widths to search */
118   uword * non_empty_dst_address_length_bitmap;
119   u8 * prefix_lengths_in_search_order;
120   ip6_address_t fib_masks[129];
121   i32 dst_address_length_refcounts[129];
122
123   /* ip6 lookup table config parameters */
124   u32 lookup_table_nbuckets;
125   uword lookup_table_size;
126 } ip6_src_fib_t;
127
128 typedef struct tunnel_lookup
129 {
130   /* Lookup lisp-gpe interfaces by dp table (eg. vrf/bridge index) */
131   uword * hw_if_index_by_dp_table;
132
133   /* lookup decap tunnel termination sw_if_index by vni and vice versa */
134   uword * sw_if_index_by_vni;
135   uword * vni_by_sw_if_index;
136 } tunnel_lookup_t;
137
138 typedef struct lisp_gpe_main
139 {
140   /* pool of encap tunnel instances */
141   lisp_gpe_tunnel_t * tunnels;
142
143   /* lookup tunnel by key */
144   mhash_t lisp_gpe_tunnel_by_key;
145
146   /* Free vlib hw_if_indices */
147   u32 * free_tunnel_hw_if_indices;
148
149   u8 is_en;
150
151   /* L3 data structures
152    * ================== */
153
154   /* Pool of src fibs that are paired with dst fibs */
155   ip4_src_fib_t * ip4_src_fibs;
156   ip6_src_fib_t * ip6_src_fibs;
157
158   tunnel_lookup_t l3_ifaces;
159
160   /* Lookup lgpe_ipX_lookup_next by vrf */
161   uword * lgpe_ip4_lookup_next_index_by_table_id;
162   uword * lgpe_ip6_lookup_next_index_by_table_id;
163
164   /* next node indexes that point ip4/6 lookup to lisp gpe ip lookup */
165   u32 ip4_lookup_next_lgpe_ip4_lookup;
166   u32 ip6_lookup_next_lgpe_ip6_lookup;
167
168   /* L2 data structures
169    * ================== */
170
171   /* l2 lisp fib */
172   BVT(clib_bihash) l2_fib;
173
174   tunnel_lookup_t l2_ifaces;
175
176   /* convenience */
177   vlib_main_t * vlib_main;
178   vnet_main_t * vnet_main;
179   ip4_main_t * im4;
180   ip6_main_t * im6;
181   ip_lookup_main_t * lm4;
182   ip_lookup_main_t * lm6;
183 } lisp_gpe_main_t;
184
185 lisp_gpe_main_t lisp_gpe_main;
186
187 always_inline lisp_gpe_main_t *
188 vnet_lisp_gpe_get_main() {
189   return &lisp_gpe_main;
190 }
191
192 extern vlib_node_registration_t lgpe_ip4_lookup_node;
193 extern vlib_node_registration_t lgpe_ip6_lookup_node;
194 extern vlib_node_registration_t lisp_gpe_ip4_input_node;
195 extern vlib_node_registration_t lisp_gpe_ip6_input_node;
196
197 u8 *
198 format_lisp_gpe_header_with_length (u8 * s, va_list * args);
199
200 typedef struct
201 {
202   u8 is_add;
203   union
204   {
205     /* vrf */
206     u32 table_id;
207
208     /* bridge domain */
209     u16 bd_id;
210
211     /* generic access */
212     u32 dp_table;
213   };
214   u8 is_l2;
215   u32 vni; /* host byte order */
216 } vnet_lisp_gpe_add_del_iface_args_t;
217
218 u8
219 vnet_lisp_gpe_enable_disable_status(void);
220 int
221 vnet_lisp_gpe_add_del_iface (vnet_lisp_gpe_add_del_iface_args_t *a,
222                              u32 * hw_if_indexp);
223
224 typedef struct
225 {
226   u8 is_en;
227 } vnet_lisp_gpe_enable_disable_args_t;
228
229 clib_error_t *
230 vnet_lisp_gpe_enable_disable (vnet_lisp_gpe_enable_disable_args_t *a);
231
232 typedef struct
233 {
234   u8 is_add;
235
236   /* type of mapping */
237   u8 is_negative;
238   u8 action;
239
240   /* local and remote eids */
241   gid_address_t lcl_eid;
242   gid_address_t rmt_eid;
243
244   /* vector of locator pairs */
245   locator_pair_t * locator_pairs;
246
247   /* FIB indices to lookup remote locator at encap and inner IP at decap */
248   u32 encap_fib_index;
249   u32 decap_fib_index;
250
251   u32 decap_next_index; /* TODO is this really needed? */
252
253   /* VNI/tenant id in HOST byte order */
254   u32 vni;
255
256   /* vrf or bd where fwd entry should be inserted */
257   union
258   {
259     u32 table_id;
260     u16 bd_id;
261
262     /* generic access */
263     u32 dp_table;
264   };
265 } vnet_lisp_gpe_add_del_fwd_entry_args_t;
266
267 int
268 vnet_lisp_gpe_add_del_fwd_entry (vnet_lisp_gpe_add_del_fwd_entry_args_t *a,
269                                  u32 * hw_if_indexp);
270
271 int
272 ip_sd_fib_add_del_route (lisp_gpe_main_t * lgm, ip_prefix_t * dst_prefix,
273                          ip_prefix_t * src_prefix, u32 table_id,
274                          ip_adjacency_t * add_adj, u8 is_add);
275 u32
276 ip_sd_fib_get_route (lisp_gpe_main_t * lgm, ip_prefix_t * dst_prefix,
277                      ip_prefix_t * src_prefix, u32 table_id);
278
279 #define foreach_lgpe_ip4_lookup_next    \
280   _(DROP, "error-drop")                 \
281   _(LISP_CP_LOOKUP, "lisp-cp-lookup")
282
283 typedef enum lgpe_ip4_lookup_next
284 {
285 #define _(sym,str) LGPE_IP4_LOOKUP_NEXT_##sym,
286   foreach_lgpe_ip4_lookup_next
287 #undef _
288   LGPE_IP4_LOOKUP_N_NEXT,
289 } lgpe_ip4_lookup_next_t;
290
291 #define foreach_lgpe_ip6_lookup_next     \
292   _(DROP, "error-drop")                 \
293   _(LISP_CP_LOOKUP, "lisp-cp-lookup")
294
295 typedef enum lgpe_ip6_lookup_next
296 {
297 #define _(sym,str) LGPE_IP6_LOOKUP_NEXT_##sym,
298   foreach_lgpe_ip6_lookup_next
299 #undef _
300   LGPE_IP6_LOOKUP_N_NEXT,
301 } lgpe_ip6_lookup_next_t;
302
303 u8 * format_vnet_lisp_gpe_status (u8 * s, va_list * args);
304
305 #define L2_FIB_DEFAULT_HASH_NUM_BUCKETS (64 * 1024)
306 #define L2_FIB_DEFAULT_HASH_MEMORY_SIZE (32<<20)
307
308 u32
309 lisp_l2_fib_lookup (lisp_gpe_main_t * lgm, u16 bd_index, u8 src_mac[8],
310                     u8 dst_mac[8]);
311
312 #endif /* included_vnet_lisp_gpe_h */