329083af631ac696e7b3285272538f3e76643ddd
[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 typedef CLIB_PACKED (struct {
31   ip4_header_t ip4;             /* 20 bytes */
32   udp_header_t udp;             /* 8 bytes */
33   lisp_gpe_header_t lisp;       /* 8 bytes */
34 }) ip4_udp_lisp_gpe_header_t;
35
36 typedef CLIB_PACKED (struct {
37   ip6_header_t ip6;             /* 40 bytes */
38   udp_header_t udp;             /* 8 bytes */
39   lisp_gpe_header_t lisp;       /* 8 bytes */
40 }) ip6_udp_lisp_gpe_header_t;
41
42 typedef struct
43 {
44   union
45   {
46     struct
47     {
48       ip_prefix_t eid;          /* within the dp only ip and mac can be eids */
49       ip_address_t dst_loc;
50       u32 iid;
51     };
52     u8 as_u8[40];
53   };
54 } lisp_gpe_tunnel_key_t;
55
56 typedef struct
57 {
58   /* Rewrite string. $$$$ embed vnet_rewrite header */
59   u8 * rewrite;
60
61   /* decap next index */
62   u32 decap_next_index;
63
64   /* tunnel src and dst addresses */
65   ip_address_t src;
66   ip_address_t dst;
67
68   /* FIB indices */
69   u32 encap_fib_index;          /* tunnel partner lookup here */
70   u32 decap_fib_index;          /* inner IP lookup here */
71
72   /* vnet intfc hw/sw_if_index */
73   u32 hw_if_index;
74   u32 sw_if_index;
75
76   /* LISP header fields in HOST byte order */
77   u8 flags;
78   u8 ver_res;
79   u8 res;
80   u8 next_protocol;
81   u32 vni;
82 } lisp_gpe_tunnel_t;
83
84 #define foreach_lisp_gpe_ip_input_next          \
85 _(DROP, "error-drop")                           \
86 _(IP4_INPUT, "ip4-input")                       \
87 _(IP6_INPUT, "ip6-input")                       \
88 _(ETHERNET_INPUT, "ethernet-input")
89
90 typedef enum {
91 #define _(s,n) LISP_GPE_INPUT_NEXT_##s,
92   foreach_lisp_gpe_ip_input_next
93 #undef _
94   LISP_GPE_INPUT_N_NEXT,
95 } lisp_gpe_input_next_t;
96
97 typedef enum {
98 #define lisp_gpe_error(n,s) LISP_GPE_ERROR_##n,
99 #include <vnet/lisp-gpe/lisp_gpe_error.def>
100 #undef lisp_gpe_error
101   LISP_GPE_N_ERROR,
102 } lisp_gpe_error_t;
103
104 /* As a first step, reuse v4 fib. The goal of the typedef is to shield
105  * consumers from future updates that may result in the lisp ip4 fib diverging
106  * from ip4 fib */
107 typedef ip4_fib_t ip4_src_fib_t;
108
109 typedef struct ip6_src_fib
110 {
111   BVT(clib_bihash) ip6_lookup_table;
112
113   /* bitmap/vector of mask widths to search */
114   uword * non_empty_dst_address_length_bitmap;
115   u8 * prefix_lengths_in_search_order;
116   ip6_address_t fib_masks[129];
117   i32 dst_address_length_refcounts[129];
118
119   /* ip6 lookup table config parameters */
120   u32 lookup_table_nbuckets;
121   uword lookup_table_size;
122 } ip6_src_fib_t;
123
124 typedef struct lisp_gpe_main
125 {
126   /* Pool of src fibs that are paired with dst fibs */
127   ip4_src_fib_t * ip4_src_fibs;
128   ip6_src_fib_t * ip6_src_fibs;
129
130   /* vector of encap tunnel instances */
131   lisp_gpe_tunnel_t * tunnels;
132
133   /* lookup tunnel by key */
134   mhash_t lisp_gpe_tunnel_by_key;
135
136   /* lookup decap tunnel termination sw_if_index by vni and vice versa */
137   uword * tunnel_term_sw_if_index_by_vni;
138   uword * vni_by_tunnel_term_sw_if_index;
139
140   /* Free vlib hw_if_indices */
141   u32 * free_lisp_gpe_tunnel_hw_if_indices;
142
143   /* Lookup lisp-gpe interfaces by vrf */
144   uword * lisp_gpe_hw_if_index_by_table_id;
145
146   /* Lookup lgpe_ipX_lookup_next by vrf */
147   uword * lgpe_ip4_lookup_next_index_by_table_id;
148   uword * lgpe_ip6_lookup_next_index_by_table_id;
149
150   /* next node indexes that point ip4/6 lookup to lisp gpe ip lookup */
151   u32 ip4_lookup_next_lgpe_ip4_lookup;
152   u32 ip6_lookup_next_lgpe_ip6_lookup;
153
154   /* convenience */
155   vlib_main_t * vlib_main;
156   vnet_main_t * vnet_main;
157   ip4_main_t * im4;
158   ip6_main_t * im6;
159   ip_lookup_main_t * lm4;
160   ip_lookup_main_t * lm6;
161 } lisp_gpe_main_t;
162
163 lisp_gpe_main_t lisp_gpe_main;
164
165 extern vlib_node_registration_t lgpe_ip4_lookup_node;
166 extern vlib_node_registration_t lgpe_ip6_lookup_node;
167 extern vlib_node_registration_t lisp_gpe_ip4_input_node;
168 extern vlib_node_registration_t lisp_gpe_ip6_input_node;
169
170 u8 *
171 format_lisp_gpe_header_with_length (u8 * s, va_list * args);
172
173 typedef struct
174 {
175   u8 is_add;
176   ip4_address_t src, dst;
177   u32 encap_fib_index;
178   u32 decap_fib_index;
179   u32 decap_next_index;
180   u8 flags;
181   u8 ver_res;
182   u8 res;
183   u8 next_protocol;
184   u32 vni; /* host byte order */
185 } vnet_lisp_gpe_add_del_tunnel_args_t;
186
187 int
188 vnet_lisp_gpe_add_del_tunnel (vnet_lisp_gpe_add_del_tunnel_args_t *a,
189                               u32 * sw_if_indexp);
190
191 typedef struct
192 {
193   u8 is_add;
194   u32 table_id; /* vrf */
195   u32 vni;      /* host byte order */
196 } vnet_lisp_gpe_add_del_iface_args_t;
197
198 void
199 vnet_lisp_gpe_add_del_iface (vnet_lisp_gpe_add_del_iface_args_t *a,
200                              u32 * hw_if_indexp);
201
202 typedef struct
203 {
204   u8 is_en;
205 } vnet_lisp_gpe_enable_disable_args_t;
206
207 clib_error_t *
208 vnet_lisp_gpe_enable_disable (vnet_lisp_gpe_enable_disable_args_t *a);
209
210 typedef enum
211 {
212   NO_ACTION,
213   FORWARD_NATIVE,
214   SEND_MAP_REQUEST,
215   DROP
216 } negative_fwd_actions_e;
217
218 typedef struct
219 {
220   u8 is_add;
221   u8 is_negative;
222   negative_fwd_actions_e action;
223   gid_address_t seid; /* TODO convert to ip4, ip6, mac ? */
224   gid_address_t deid;
225   ip_address_t slocator;
226   ip_address_t dlocator;
227   u32 encap_fib_index;
228   u32 decap_fib_index;
229   u32 decap_next_index; /* TODO is this really needed? */
230   u8 flags;
231   u8 ver_res;
232   u8 res;
233   u8 next_protocol;
234   u32 vni; /* host byte order */
235   u32 table_id;
236 } vnet_lisp_gpe_add_del_fwd_entry_args_t;
237
238 int
239 vnet_lisp_gpe_add_del_fwd_entry (vnet_lisp_gpe_add_del_fwd_entry_args_t *a,
240                                  u32 * hw_if_indexp);
241
242 int
243 ip_sd_fib_add_del_route (lisp_gpe_main_t * lgm, ip_prefix_t * dst_prefix,
244                          ip_prefix_t * src_prefix, u32 table_id,
245                          ip_adjacency_t * add_adj, u8 is_add);
246 u32
247 ip_sd_fib_get_route (lisp_gpe_main_t * lgm, ip_prefix_t * dst_prefix,
248                      ip_prefix_t * src_prefix, u32 table_id);
249
250 #define foreach_lgpe_ip4_lookup_next    \
251   _(DROP, "error-drop")                 \
252   _(LISP_CP_LOOKUP, "lisp-cp-lookup")
253
254 typedef enum lgpe_ip4_lookup_next
255 {
256 #define _(sym,str) LGPE_IP4_LOOKUP_NEXT_##sym,
257   foreach_lgpe_ip4_lookup_next
258 #undef _
259   LGPE_IP4_LOOKUP_N_NEXT,
260 } lgpe_ip4_lookup_next_t;
261
262 #define foreach_lgpe_ip6_lookup_next     \
263   _(DROP, "error-drop")                 \
264   _(LISP_CP_LOOKUP, "lisp-cp-lookup")
265
266 typedef enum lgpe_ip6_lookup_next
267 {
268 #define _(sym,str) LGPE_IP6_LOOKUP_NEXT_##sym,
269   foreach_lgpe_ip6_lookup_next
270 #undef _
271   LGPE_IP6_LOOKUP_N_NEXT,
272 } lgpe_ip6_lookup_next_t;
273
274 #endif /* included_vnet_lisp_gpe_h */