d3d320abb3e3417e53f60ddfd62660de85ce673f
[vpp.git] / src / vnet / lisp-cp / control.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 VNET_CONTROL_H_
17 #define VNET_CONTROL_H_
18
19 #include <vnet/vnet.h>
20 #include <vnet/lisp-cp/gid_dictionary.h>
21 #include <vnet/lisp-cp/lisp_types.h>
22 #include <vppinfra/timing_wheel.h>
23
24 #define NUMBER_OF_RETRIES                   1
25 #define PENDING_MREQ_EXPIRATION_TIME        3.0 /* seconds */
26 #define PENDING_MREQ_QUEUE_LEN              5
27
28 #define RLOC_PROBING_INTERVAL               60.0
29
30 /* when map-registration is enabled "quick registration" takes place first.
31    In this mode ETR sends map-register messages at an increased frequency
32    until specified message count is reached */
33 #define QUICK_MAP_REGISTER_MSG_COUNT        5
34 #define QUICK_MAP_REGISTER_INTERVAL         3.0
35
36 /* normal map-register period */
37 #define MAP_REGISTER_INTERVAL               60.0
38
39 /* how many tries until next map-server election */
40 #define MAX_EXPIRED_MAP_REGISTERS_DEFAULT   3
41
42 #define PENDING_MREG_EXPIRATION_TIME        3.0 /* seconds */
43
44 /* 24 hours */
45 #define MAP_REGISTER_DEFAULT_TTL            86400
46
47 typedef struct
48 {
49   gid_address_t src;
50   gid_address_t dst;
51   u32 retries_num;
52   f64 time_to_expire;
53   u8 is_smr_invoked;
54   u64 *nonces;
55   u8 to_be_removed;
56 } pending_map_request_t;
57
58 typedef struct
59 {
60   f64 time_to_expire;
61 } pending_map_register_t;
62
63 typedef struct
64 {
65   gid_address_t leid;
66   gid_address_t reid;
67   u8 is_src_dst;
68   locator_pair_t *locator_pairs;
69 } fwd_entry_t;
70
71 typedef struct
72 {
73   gid_address_t leid;
74   gid_address_t reid;
75 } lisp_adjacency_t;
76
77 typedef enum
78 {
79   IP4_MISS_PACKET,
80   IP6_MISS_PACKET
81 } miss_packet_type_t;
82
83 /* map-server/map-resolver structure */
84 typedef struct
85 {
86   u8 is_down;
87   f64 last_update;
88   ip_address_t address;
89   char *key;
90 } lisp_msmr_t;
91
92 typedef struct
93 {
94   /* headers */
95   u8 data[100];
96   u32 length;
97   miss_packet_type_t type;
98 } miss_packet_t;
99
100 typedef struct
101 {
102   u8 mac[6];
103   u32 ip4;
104 } lisp_api_l2_arp_entry_t;
105
106 typedef struct
107 {
108   u8 mac[6];
109   u8 ip6[16];
110 } lisp_api_ndp_entry_t;
111
112 typedef enum
113 {
114   MR_MODE_DST_ONLY = 0,
115   MR_MODE_SRC_DST,
116   _MR_MODE_MAX
117 } map_request_mode_t;
118
119 #define foreach_lisp_flag_bit       \
120   _(USE_PETR, "Use Proxy-ETR")                    \
121   _(XTR_MODE, "ITR/ETR mode")                     \
122   _(PETR_MODE, "Proxy-ETR mode")                   \
123   _(PITR_MODE, "Proxy-ITR mode")                  \
124   _(STATS_ENABLED, "Statistics enabled")
125
126 typedef enum lisp_flag_bits
127 {
128 #define _(sym, str) LISP_FLAG_BIT_##sym,
129   foreach_lisp_flag_bit
130 #undef _
131 } lisp_flag_bits_e;
132
133 typedef enum lisp_flags
134 {
135 #define _(sym, str) LISP_FLAG_##sym = 1 << LISP_FLAG_BIT_##sym,
136   foreach_lisp_flag_bit
137 #undef _
138 } lisp_flags_e;
139
140 typedef struct
141 {
142   ip_address_t addr;
143   u32 bd;
144 } lisp_l2_arp_key_t;
145
146 typedef enum
147 {
148   LISP_TRANSPORT_PROTOCOL_UDP = 1,
149   LISP_TRANSPORT_PROTOCOL_API
150 } lisp_transport_protocol_t;
151
152 typedef struct
153 {
154   u64 nonce;
155   u8 is_rloc_probe;
156   mapping_t *mappings;
157   volatile u8 is_free;
158 } map_records_arg_t;
159
160 typedef struct
161 {
162   u32 flags;
163
164   /* LISP feature status */
165   u8 is_enabled;
166
167   /* eid table */
168   gid_dictionary_t mapping_index_by_gid;
169
170   /* pool of mappings */
171   mapping_t *mapping_pool;
172
173   /* hash map of secret keys by mapping index */
174   u8 *key_by_mapping_index;
175
176   /* pool of locators */
177   locator_t *locator_pool;
178
179   /* pool of locator-sets */
180   locator_set_t *locator_set_pool;
181
182   /* vector of locator-set vectors composed of and indexed by locator index */
183   u32 **locator_to_locator_sets;
184
185   /* hash map of locators by name */
186   uword *locator_set_index_by_name;
187
188   /* vector of eid index vectors supported and indexed by locator-set index */
189   u32 **locator_set_to_eids;
190
191   /* vectors of indexes for local locator-sets and mappings */
192   u32 *local_mappings_indexes;
193   u32 *local_locator_set_indexes;
194
195   /* hash map of forwarding entries by mapping index */
196   u32 *fwd_entry_by_mapping_index;
197
198   /* pool of vectors of rmts per lcl mapping in adjacencies */
199   u32 **lcl_to_rmt_adjacencies;
200
201   /* hash of pool positions of vectors of rmts by lcl mapping index */
202   u32 *lcl_to_rmt_adjs_by_lcl_idx;
203
204   /* forwarding entries pool */
205   fwd_entry_t *fwd_entry_pool;
206
207   /* hash map keyed by nonce of pending map-requests */
208   uword *pending_map_requests_by_nonce;
209
210   /* pool of pending map requests */
211   pending_map_request_t *pending_map_requests_pool;
212
213   /* pool of pending map registers */
214   pending_map_register_t *pending_map_registers_pool;
215
216   /* hash map of sent map register messages */
217   uword *map_register_messages_by_nonce;
218
219   /* vector of map-resolvers */
220   lisp_msmr_t *map_resolvers;
221
222   /* vector of map-servers */
223   lisp_msmr_t *map_servers;
224
225   /* map resolver address currently being used for sending requests.
226    * This has to be an actual address and not an index to map_resolvers vector
227    * since the vector may be modified during request resend/retry procedure
228    * and break things :-) */
229   ip_address_t active_map_resolver;
230   ip_address_t active_map_server;
231
232   u8 do_map_resolver_election;
233   u8 do_map_server_election;
234
235   /* map-request  locator set index */
236   u32 mreq_itr_rlocs;
237
238   /* vni to vrf hash tables */
239   uword *table_id_by_vni;
240   uword *vni_by_table_id;
241
242   /* vni to bd-index hash tables */
243   uword *bd_id_by_vni;
244   uword *vni_by_bd_id;
245
246   /* track l2 and l3 interfaces that have been created for vni */
247   uword *l2_dp_intf_by_vni;
248
249   /* Proxy ITR map index */
250   u32 pitr_map_index;
251
252   /** Proxy ETR map index used for 'use-petr'.
253    *  Not related to PETR tunnel mode  */
254   u32 petr_map_index;
255
256   /* mapping index for NSH */
257   u32 nsh_map_index;
258
259   /* map request mode */
260   u8 map_request_mode;
261
262   /* enable/disable map registering */
263   u8 map_registering;
264
265   /* enable/disable rloc-probing */
266   u8 rloc_probing;
267
268   /* timing wheel for mapping timeouts */
269   timing_wheel_t wheel;
270
271   /** Per thread pool of records shared with thread0 */
272   map_records_arg_t **map_records_args_pool;
273
274   /* TTL used for all mappings when registering */
275   u32 map_register_ttl;
276
277   /* control variables for map server election */
278   u32 max_expired_map_registers;
279   u32 expired_map_registers;
280
281   /** either UDP based or binary API. Default is UDP */
282   lisp_transport_protocol_t transport_protocol;
283
284   /** Retry service node index */
285   u32 retry_service_index;
286
287   /* commodity */
288   ip4_main_t *im4;
289   ip6_main_t *im6;
290   vlib_main_t *vlib_main;
291   vnet_main_t *vnet_main;
292 } lisp_cp_main_t;
293
294 /* lisp-gpe control plane */
295 extern lisp_cp_main_t lisp_control_main;
296
297 extern vlib_node_registration_t lisp_cp_input_node;
298 extern vlib_node_registration_t lisp_cp_lookup_ip4_node;
299 extern vlib_node_registration_t lisp_cp_lookup_ip6_node;
300
301 clib_error_t *lisp_cp_init ();
302
303 always_inline lisp_cp_main_t *
304 vnet_lisp_cp_get_main ()
305 {
306   return &lisp_control_main;
307 }
308
309 void
310 get_src_and_dst_eids_from_buffer (lisp_cp_main_t * lcm, vlib_buffer_t * b,
311                                   gid_address_t * src, gid_address_t * dst,
312                                   u16 type);
313
314 typedef struct
315 {
316   u8 is_add;
317   union
318   {
319     u8 *name;
320     u32 index;
321   };
322   locator_t *locators;
323   u8 local;
324 } vnet_lisp_add_del_locator_set_args_t;
325
326 int
327 vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
328                                u32 * ls_index);
329 int
330 vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
331                            locator_set_t * ls, u32 * ls_index);
332
333 typedef struct
334 {
335   u8 is_add;
336   gid_address_t eid;
337   u32 locator_set_index;
338
339   u32 ttl;
340   u8 action;
341   u8 authoritative;
342
343   u8 local;
344   u8 is_static;
345   u8 *key;
346   u8 key_id;
347 } vnet_lisp_add_del_mapping_args_t;
348
349 int
350 vnet_lisp_map_cache_add_del (vnet_lisp_add_del_mapping_args_t * a,
351                              u32 * map_index);
352 int
353 vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a,
354                                  u32 * map_index_result);
355
356 int
357 vnet_lisp_add_mapping (vnet_lisp_add_del_mapping_args_t * a,
358                        locator_t * rlocs, u32 * res_map_index,
359                        u8 * is_changed);
360
361 int vnet_lisp_del_mapping (gid_address_t * eid, u32 * res_map_index);
362
363 typedef struct
364 {
365   gid_address_t reid;
366   gid_address_t leid;
367   u8 is_add;
368 } vnet_lisp_add_del_adjacency_args_t;
369
370 int vnet_lisp_add_del_adjacency (vnet_lisp_add_del_adjacency_args_t * a);
371
372 typedef struct
373 {
374   u8 is_add;
375   ip_address_t address;
376 } vnet_lisp_add_del_map_resolver_args_t;
377
378 int
379 vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a);
380 int vnet_lisp_add_del_map_server (ip_address_t * addr, u8 is_add);
381
382 clib_error_t *vnet_lisp_enable_disable (u8 is_enabled);
383 u8 vnet_lisp_enable_disable_status (void);
384 void vnet_lisp_create_retry_process (lisp_cp_main_t * lcm);
385
386 int vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add);
387 int vnet_lisp_use_petr (ip_address_t * ip, u8 is_add);
388
389 typedef struct
390 {
391   u8 is_add;
392   u8 *locator_set_name;
393 } vnet_lisp_add_del_mreq_itr_rloc_args_t;
394
395 int
396 vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a);
397
398 int vnet_lisp_clear_all_remote_adjacencies (void);
399
400 int vnet_lisp_eid_table_map (u32 vni, u32 vrf, u8 is_l2, u8 is_add);
401 int vnet_lisp_add_del_map_table_key (gid_address_t * eid, char *key,
402                                      u8 is_add);
403 int vnet_lisp_set_map_request_mode (u8 mode);
404 u8 vnet_lisp_get_map_request_mode (void);
405 lisp_adjacency_t *vnet_lisp_adjacencies_get_by_vni (u32 vni);
406 int vnet_lisp_rloc_probe_enable_disable (u8 is_enable);
407 int vnet_lisp_map_register_enable_disable (u8 is_enable);
408 u8 vnet_lisp_map_register_state_get (void);
409 u8 vnet_lisp_rloc_probe_state_get (void);
410 int vnet_lisp_add_del_l2_arp_ndp_entry (gid_address_t * key, u8 * mac,
411                                         u8 is_add);
412 u32 *vnet_lisp_l2_arp_bds_get (void);
413 lisp_api_l2_arp_entry_t *vnet_lisp_l2_arp_entries_get_by_bd (u32 bd);
414 int vnet_lisp_nsh_set_locator_set (u8 * locator_set_name, u8 is_add);
415 int vnet_lisp_map_register_set_ttl (u32 ttl);
416 u32 vnet_lisp_map_register_get_ttl (void);
417 int vnet_lisp_map_register_fallback_threshold_set (u32 value);
418 u32 vnet_lisp_map_register_fallback_threshold_get (void);
419 u32 *vnet_lisp_ndp_bds_get (void);
420 lisp_api_ndp_entry_t *vnet_lisp_ndp_entries_get_by_bd (u32 bd);
421 u32 vnet_lisp_set_transport_protocol (u8 protocol);
422 lisp_transport_protocol_t vnet_lisp_get_transport_protocol (void);
423
424 extern int vnet_lisp_enable_disable_xtr_mode (u8 is_enabled);
425 extern int vnet_lisp_enable_disable_pitr_mode (u8 is_enabled);
426 extern int vnet_lisp_enable_disable_petr_mode (u8 is_enabled);
427 extern u8 vnet_lisp_get_xtr_mode (void);
428 extern u8 vnet_lisp_get_pitr_mode (void);
429 extern u8 vnet_lisp_get_petr_mode (void);
430
431 map_records_arg_t *parse_map_reply (vlib_buffer_t * b);
432
433 always_inline mapping_t *
434 lisp_get_petr_mapping (lisp_cp_main_t * lcm)
435 {
436   return pool_elt_at_index (lcm->mapping_pool, lcm->petr_map_index);
437 }
438
439 #endif /* VNET_CONTROL_H_ */
440
441 /*
442  * fd.io coding-style-patch-verification: ON
443  *
444  * Local Variables:
445  * eval: (c-set-style "gnu")
446  * End:
447  */