LISP: re-fetch mapping before it expires
[vpp.git] / src / vnet / lisp-cp / one_api.c
1 /*
2  *------------------------------------------------------------------
3  * one_api.c - Overlay Network Engine API
4  *
5  * Copyright (c) 2016-2017 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
22
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vnet/lisp-cp/control.h>
26 #include <vnet/lisp-gpe/lisp_gpe.h>
27
28 #include <vnet/vnet_msg_enum.h>
29
30 #define vl_api_one_remote_locator_t_endian vl_noop_handler
31 #define vl_api_one_remote_locator_t_print vl_noop_handler
32 #define vl_api_one_local_locator_t_endian vl_noop_handler
33 #define vl_api_one_local_locator_t_print vl_noop_handler
34
35 #define vl_api_one_add_del_locator_set_t_endian vl_noop_handler
36 #define vl_api_one_add_del_locator_set_t_print vl_noop_handler
37 #define vl_api_one_add_del_remote_mapping_t_endian vl_noop_handler
38 #define vl_api_one_add_del_remote_mapping_t_print vl_noop_handler
39
40 #define vl_api_one_add_del_locator_set_t_endian vl_noop_handler
41 #define vl_api_one_add_del_locator_set_t_print vl_noop_handler
42 #define vl_api_one_add_del_remote_mapping_t_endian vl_noop_handler
43 #define vl_api_one_add_del_remote_mapping_t_print vl_noop_handler
44
45 #define vl_api_one_l2_arp_entry_t_endian vl_noop_handler
46 #define vl_api_one_l2_arp_entry_t_print vl_noop_handler
47 #define vl_api_one_add_del_l2_arp_entry vl_noop_handler
48 #define vl_api_one_l2_arp_bd_get vl_noop_handler
49
50 #define vl_typedefs             /* define message structures */
51 #include <vnet/vnet_all_api_h.h>
52 #undef vl_typedefs
53
54 #define vl_endianfun            /* define message structures */
55 #include <vnet/vnet_all_api_h.h>
56 #undef vl_endianfun
57
58 /* instantiate all the print functions we know about */
59 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
60 #define vl_printfun
61 #include <vnet/vnet_all_api_h.h>
62 #undef vl_printfun
63
64 #include <vlibapi/api_helper_macros.h>
65
66 #define REPLY_DETAILS(t, body)                                  \
67 do {                                                            \
68     unix_shared_memory_queue_t * q;                             \
69     rv = vl_msg_api_pd_handler (mp, rv);                        \
70     q = vl_api_client_index_to_input_queue (mp->client_index);  \
71     if (!q)                                                     \
72         return;                                                 \
73                                                                 \
74     rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
75     rmp->_vl_msg_id = ntohs((t));                               \
76     rmp->context = mp->context;                                 \
77     do {body;} while (0);                                       \
78     vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
79 } while(0);
80
81 #define foreach_vpe_api_msg                             \
82 _(ONE_ADD_DEL_LOCATOR_SET, one_add_del_locator_set)                     \
83 _(ONE_ADD_DEL_LOCATOR, one_add_del_locator)                             \
84 _(ONE_ADD_DEL_LOCAL_EID, one_add_del_local_eid)                         \
85 _(ONE_ADD_DEL_MAP_RESOLVER, one_add_del_map_resolver)                   \
86 _(ONE_ADD_DEL_MAP_SERVER, one_add_del_map_server)                       \
87 _(ONE_ENABLE_DISABLE, one_enable_disable)                               \
88 _(ONE_RLOC_PROBE_ENABLE_DISABLE, one_rloc_probe_enable_disable)         \
89 _(ONE_MAP_REGISTER_ENABLE_DISABLE, one_map_register_enable_disable)     \
90 _(ONE_MAP_REGISTER_FALLBACK_THRESHOLD,                                  \
91   one_map_register_fallback_threshold)                                  \
92 _(ONE_ADD_DEL_REMOTE_MAPPING, one_add_del_remote_mapping)               \
93 _(ONE_ADD_DEL_ADJACENCY, one_add_del_adjacency)                         \
94 _(ONE_PITR_SET_LOCATOR_SET, one_pitr_set_locator_set)                   \
95 _(ONE_NSH_SET_LOCATOR_SET, one_nsh_set_locator_set)                     \
96 _(ONE_MAP_REQUEST_MODE, one_map_request_mode)                           \
97 _(ONE_EID_TABLE_ADD_DEL_MAP, one_eid_table_add_del_map)                 \
98 _(ONE_LOCATOR_SET_DUMP, one_locator_set_dump)                           \
99 _(ONE_LOCATOR_DUMP, one_locator_dump)                                   \
100 _(ONE_EID_TABLE_DUMP, one_eid_table_dump)                               \
101 _(ONE_MAP_RESOLVER_DUMP, one_map_resolver_dump)                         \
102 _(ONE_MAP_SERVER_DUMP, one_map_server_dump)                             \
103 _(ONE_EID_TABLE_MAP_DUMP, one_eid_table_map_dump)                       \
104 _(ONE_EID_TABLE_VNI_DUMP, one_eid_table_vni_dump)                       \
105 _(ONE_ADJACENCIES_GET, one_adjacencies_get)                             \
106 _(ONE_MAP_REGISTER_SET_TTL, one_map_register_set_ttl)                   \
107 _(SHOW_ONE_NSH_MAPPING, show_one_nsh_mapping)                           \
108 _(SHOW_ONE_RLOC_PROBE_STATE, show_one_rloc_probe_state)                 \
109 _(SHOW_ONE_MAP_REGISTER_STATE, show_one_map_register_state)             \
110 _(SHOW_ONE_MAP_REGISTER_TTL, show_one_map_register_ttl)                 \
111 _(SHOW_ONE_MAP_REGISTER_FALLBACK_THRESHOLD,                             \
112   show_one_map_register_fallback_threshold)                             \
113 _(SHOW_ONE_STATUS, show_one_status)                                     \
114 _(ONE_ADD_DEL_MAP_REQUEST_ITR_RLOCS,                                    \
115   one_add_del_map_request_itr_rlocs)                                    \
116 _(ONE_GET_MAP_REQUEST_ITR_RLOCS, one_get_map_request_itr_rlocs)         \
117 _(SHOW_ONE_PITR, show_one_pitr)                                         \
118 _(SHOW_ONE_MAP_REQUEST_MODE, show_one_map_request_mode)                 \
119 _(ONE_USE_PETR, one_use_petr)                                           \
120 _(SHOW_ONE_USE_PETR, show_one_use_petr)                                 \
121 _(SHOW_ONE_STATS_ENABLE_DISABLE, show_one_stats_enable_disable)         \
122 _(ONE_STATS_ENABLE_DISABLE, one_stats_enable_disable)                   \
123 _(ONE_STATS_DUMP, one_stats_dump)                                       \
124 _(ONE_STATS_FLUSH, one_stats_flush)                                     \
125 _(ONE_L2_ARP_BD_GET, one_l2_arp_bd_get)                                 \
126 _(ONE_L2_ARP_ENTRIES_GET, one_l2_arp_entries_get)                       \
127 _(ONE_ADD_DEL_L2_ARP_ENTRY, one_add_del_l2_arp_entry)                   \
128
129
130 static locator_t *
131 unformat_one_locs (vl_api_one_remote_locator_t * rmt_locs, u32 rloc_num)
132 {
133   u32 i;
134   locator_t *locs = 0, loc;
135   vl_api_one_remote_locator_t *r;
136
137   for (i = 0; i < rloc_num; i++)
138     {
139       /* remote locators */
140       r = &rmt_locs[i];
141       memset (&loc, 0, sizeof (loc));
142       gid_address_ip_set (&loc.address, &r->addr, r->is_ip4 ? IP4 : IP6);
143
144       loc.priority = r->priority;
145       loc.weight = r->weight;
146
147       vec_add1 (locs, loc);
148     }
149   return locs;
150 }
151
152 static void
153 vl_api_one_map_register_set_ttl_t_handler (vl_api_one_map_register_set_ttl_t *
154                                            mp)
155 {
156   vl_api_one_map_register_set_ttl_reply_t *rmp;
157   int rv = 0;
158
159   mp->ttl = clib_net_to_host_u32 (mp->ttl);
160   rv = vnet_lisp_map_register_set_ttl (mp->ttl);
161
162   REPLY_MACRO (VL_API_ONE_MAP_REGISTER_SET_TTL_REPLY);
163 }
164
165 static void
166   vl_api_show_one_map_register_ttl_t_handler
167   (vl_api_show_one_map_register_ttl_t * mp)
168 {
169   vl_api_show_one_map_register_ttl_reply_t *rmp;
170   int rv = 0;
171
172   u32 ttl = vnet_lisp_map_register_get_ttl ();
173   /* *INDENT-OFF* */
174   REPLY_MACRO2 (VL_API_SHOW_ONE_MAP_REGISTER_TTL_REPLY,
175   ({
176     rmp->ttl = clib_host_to_net_u32 (ttl);
177   }));
178   /* *INDENT-ON* */
179 }
180
181 static void
182 vl_api_one_add_del_locator_set_t_handler (vl_api_one_add_del_locator_set_t *
183                                           mp)
184 {
185   vl_api_one_add_del_locator_set_reply_t *rmp;
186   int rv = 0;
187   vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
188   locator_t locator;
189   vl_api_one_local_locator_t *ls_loc;
190   u32 ls_index = ~0, locator_num;
191   u8 *locator_name = NULL;
192   int i;
193
194   memset (a, 0, sizeof (a[0]));
195
196   locator_name = format (0, "%s", mp->locator_set_name);
197
198   a->name = locator_name;
199   a->is_add = mp->is_add;
200   a->local = 1;
201   locator_num = clib_net_to_host_u32 (mp->locator_num);
202
203   memset (&locator, 0, sizeof (locator));
204   for (i = 0; i < locator_num; i++)
205     {
206       ls_loc = &mp->locators[i];
207       VALIDATE_SW_IF_INDEX (ls_loc);
208
209       locator.sw_if_index = htonl (ls_loc->sw_if_index);
210       locator.priority = ls_loc->priority;
211       locator.weight = ls_loc->weight;
212       locator.local = 1;
213       vec_add1 (a->locators, locator);
214     }
215
216   rv = vnet_lisp_add_del_locator_set (a, &ls_index);
217
218   BAD_SW_IF_INDEX_LABEL;
219
220   vec_free (locator_name);
221   vec_free (a->locators);
222
223   /* *INDENT-OFF* */
224   REPLY_MACRO2 (VL_API_ONE_ADD_DEL_LOCATOR_SET_REPLY,
225   ({
226     rmp->ls_index = clib_host_to_net_u32 (ls_index);
227   }));
228   /* *INDENT-ON* */
229 }
230
231 static void
232 vl_api_one_add_del_locator_t_handler (vl_api_one_add_del_locator_t * mp)
233 {
234   vl_api_one_add_del_locator_reply_t *rmp;
235   int rv = 0;
236   locator_t locator, *locators = NULL;
237   vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
238   u32 ls_index = ~0;
239   u8 *locator_name = NULL;
240
241   memset (&locator, 0, sizeof (locator));
242   memset (a, 0, sizeof (a[0]));
243
244   locator.sw_if_index = ntohl (mp->sw_if_index);
245   locator.priority = mp->priority;
246   locator.weight = mp->weight;
247   locator.local = 1;
248   vec_add1 (locators, locator);
249
250   locator_name = format (0, "%s", mp->locator_set_name);
251
252   a->name = locator_name;
253   a->locators = locators;
254   a->is_add = mp->is_add;
255   a->local = 1;
256
257   rv = vnet_lisp_add_del_locator (a, NULL, &ls_index);
258
259   vec_free (locators);
260   vec_free (locator_name);
261
262   REPLY_MACRO (VL_API_ONE_ADD_DEL_LOCATOR_REPLY);
263 }
264
265 typedef struct
266 {
267   u32 spi;
268   u8 si;
269 } __attribute__ ((__packed__)) lisp_nsh_api_t;
270
271 static int
272 unformat_one_eid_api (gid_address_t * dst, u32 vni, u8 type, void *src,
273                       u8 len)
274 {
275   lisp_nsh_api_t *nsh;
276
277   switch (type)
278     {
279     case 0:                     /* ipv4 */
280       gid_address_type (dst) = GID_ADDR_IP_PREFIX;
281       gid_address_ip_set (dst, src, IP4);
282       gid_address_ippref_len (dst) = len;
283       ip_prefix_normalize (&gid_address_ippref (dst));
284       break;
285     case 1:                     /* ipv6 */
286       gid_address_type (dst) = GID_ADDR_IP_PREFIX;
287       gid_address_ip_set (dst, src, IP6);
288       gid_address_ippref_len (dst) = len;
289       ip_prefix_normalize (&gid_address_ippref (dst));
290       break;
291     case 2:                     /* l2 mac */
292       gid_address_type (dst) = GID_ADDR_MAC;
293       clib_memcpy (&gid_address_mac (dst), src, 6);
294       break;
295     case 3:                     /* NSH */
296       gid_address_type (dst) = GID_ADDR_NSH;
297       nsh = src;
298       gid_address_nsh_spi (dst) = clib_net_to_host_u32 (nsh->spi);
299       gid_address_nsh_si (dst) = nsh->si;
300       break;
301     default:
302       /* unknown type */
303       return VNET_API_ERROR_INVALID_VALUE;
304     }
305
306   gid_address_vni (dst) = vni;
307
308   return 0;
309 }
310
311 static void
312 vl_api_one_add_del_local_eid_t_handler (vl_api_one_add_del_local_eid_t * mp)
313 {
314   vl_api_one_add_del_local_eid_reply_t *rmp;
315   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
316   int rv = 0;
317   gid_address_t _eid, *eid = &_eid;
318   uword *p = NULL;
319   u32 locator_set_index = ~0, map_index = ~0;
320   vnet_lisp_add_del_mapping_args_t _a, *a = &_a;
321   u8 *name = NULL, *key = NULL;
322   memset (a, 0, sizeof (a[0]));
323   memset (eid, 0, sizeof (eid[0]));
324
325   rv = unformat_one_eid_api (eid, clib_net_to_host_u32 (mp->vni),
326                              mp->eid_type, mp->eid, mp->prefix_len);
327   if (rv)
328     goto out;
329
330   if (gid_address_type (eid) == GID_ADDR_NSH)
331     {
332       rv = VNET_API_ERROR_INVALID_VALUE;
333       goto out;
334     }
335
336   name = format (0, "%s", mp->locator_set_name);
337   p = hash_get_mem (lcm->locator_set_index_by_name, name);
338   if (!p)
339     {
340       rv = VNET_API_ERROR_INVALID_VALUE;
341       goto out;
342     }
343   locator_set_index = p[0];
344
345   if (*mp->key)
346     key = format (0, "%s", mp->key);
347
348   /* XXX treat batch configuration */
349   a->is_add = mp->is_add;
350   gid_address_copy (&a->eid, eid);
351   a->locator_set_index = locator_set_index;
352   a->local = 1;
353   a->key = key;
354   a->key_id = clib_net_to_host_u16 (mp->key_id);
355
356   rv = vnet_lisp_add_del_local_mapping (a, &map_index);
357
358 out:
359   vec_free (name);
360   vec_free (key);
361   gid_address_free (&a->eid);
362
363   REPLY_MACRO (VL_API_ONE_ADD_DEL_LOCAL_EID_REPLY);
364 }
365
366 static void
367   vl_api_one_eid_table_add_del_map_t_handler
368   (vl_api_one_eid_table_add_del_map_t * mp)
369 {
370   vl_api_one_eid_table_add_del_map_reply_t *rmp;
371   int rv = 0;
372   rv = vnet_lisp_eid_table_map (clib_net_to_host_u32 (mp->vni),
373                                 clib_net_to_host_u32 (mp->dp_table),
374                                 mp->is_l2, mp->is_add);
375 REPLY_MACRO (VL_API_ONE_EID_TABLE_ADD_DEL_MAP_REPLY)}
376
377 static void
378 vl_api_one_add_del_map_server_t_handler (vl_api_one_add_del_map_server_t * mp)
379 {
380   vl_api_one_add_del_map_server_reply_t *rmp;
381   int rv = 0;
382   ip_address_t addr;
383
384   memset (&addr, 0, sizeof (addr));
385
386   ip_address_set (&addr, mp->ip_address, mp->is_ipv6 ? IP6 : IP4);
387   rv = vnet_lisp_add_del_map_server (&addr, mp->is_add);
388
389   REPLY_MACRO (VL_API_ONE_ADD_DEL_MAP_SERVER_REPLY);
390 }
391
392 static void
393 vl_api_one_add_del_map_resolver_t_handler (vl_api_one_add_del_map_resolver_t
394                                            * mp)
395 {
396   vl_api_one_add_del_map_resolver_reply_t *rmp;
397   int rv = 0;
398   vnet_lisp_add_del_map_resolver_args_t _a, *a = &_a;
399
400   memset (a, 0, sizeof (a[0]));
401
402   a->is_add = mp->is_add;
403   ip_address_set (&a->address, mp->ip_address, mp->is_ipv6 ? IP6 : IP4);
404
405   rv = vnet_lisp_add_del_map_resolver (a);
406
407   REPLY_MACRO (VL_API_ONE_ADD_DEL_MAP_RESOLVER_REPLY);
408 }
409
410 static void
411   vl_api_one_map_register_enable_disable_t_handler
412   (vl_api_one_map_register_enable_disable_t * mp)
413 {
414   vl_api_one_map_register_enable_disable_reply_t *rmp;
415   int rv = 0;
416
417   vnet_lisp_map_register_enable_disable (mp->is_enabled);
418   REPLY_MACRO (VL_API_ONE_ENABLE_DISABLE_REPLY);
419 }
420
421 static void
422   vl_api_one_rloc_probe_enable_disable_t_handler
423   (vl_api_one_rloc_probe_enable_disable_t * mp)
424 {
425   vl_api_one_rloc_probe_enable_disable_reply_t *rmp;
426   int rv = 0;
427
428   vnet_lisp_rloc_probe_enable_disable (mp->is_enabled);
429   REPLY_MACRO (VL_API_ONE_ENABLE_DISABLE_REPLY);
430 }
431
432 static void
433 vl_api_one_enable_disable_t_handler (vl_api_one_enable_disable_t * mp)
434 {
435   vl_api_one_enable_disable_reply_t *rmp;
436   int rv = 0;
437
438   vnet_lisp_enable_disable (mp->is_en);
439   REPLY_MACRO (VL_API_ONE_ENABLE_DISABLE_REPLY);
440 }
441
442 static void
443   vl_api_show_one_map_request_mode_t_handler
444   (vl_api_show_one_map_request_mode_t * mp)
445 {
446   int rv = 0;
447   vl_api_show_one_map_request_mode_reply_t *rmp;
448
449   /* *INDENT-OFF* */
450   REPLY_MACRO2(VL_API_SHOW_ONE_MAP_REQUEST_MODE_REPLY,
451   ({
452     rmp->mode = vnet_lisp_get_map_request_mode ();
453   }));
454   /* *INDENT-ON* */
455 }
456
457 static void
458 vl_api_one_map_request_mode_t_handler (vl_api_one_map_request_mode_t * mp)
459 {
460   vl_api_one_map_request_mode_reply_t *rmp;
461   int rv = 0;
462
463   rv = vnet_lisp_set_map_request_mode (mp->mode);
464
465   REPLY_MACRO (VL_API_ONE_MAP_REQUEST_MODE_REPLY);
466 }
467
468 static void
469 vl_api_one_nsh_set_locator_set_t_handler (vl_api_one_nsh_set_locator_set_t
470                                           * mp)
471 {
472   vl_api_one_nsh_set_locator_set_reply_t *rmp;
473   int rv = 0;
474   u8 *ls_name = 0;
475
476   ls_name = format (0, "%s", mp->ls_name);
477   rv = vnet_lisp_nsh_set_locator_set (ls_name, mp->is_add);
478   vec_free (ls_name);
479
480   REPLY_MACRO (VL_API_ONE_PITR_SET_LOCATOR_SET_REPLY);
481 }
482
483 static void
484 vl_api_one_pitr_set_locator_set_t_handler (vl_api_one_pitr_set_locator_set_t
485                                            * mp)
486 {
487   vl_api_one_pitr_set_locator_set_reply_t *rmp;
488   int rv = 0;
489   u8 *ls_name = 0;
490
491   ls_name = format (0, "%s", mp->ls_name);
492   rv = vnet_lisp_pitr_set_locator_set (ls_name, mp->is_add);
493   vec_free (ls_name);
494
495   REPLY_MACRO (VL_API_ONE_PITR_SET_LOCATOR_SET_REPLY);
496 }
497
498 static void
499 vl_api_one_use_petr_t_handler (vl_api_one_use_petr_t * mp)
500 {
501   vl_api_one_use_petr_reply_t *rmp;
502   int rv = 0;
503   ip_address_t addr;
504
505   ip_address_set (&addr, &mp->address, mp->is_ip4 ? IP4 : IP6);
506   rv = vnet_lisp_use_petr (&addr, mp->is_add);
507
508   REPLY_MACRO (VL_API_ONE_USE_PETR_REPLY);
509 }
510
511 static void
512 vl_api_show_one_use_petr_t_handler (vl_api_show_one_use_petr_t * mp)
513 {
514   unix_shared_memory_queue_t *q = NULL;
515   vl_api_show_one_use_petr_reply_t *rmp = NULL;
516   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
517   mapping_t *m;
518   locator_set_t *ls = 0;
519   int rv = 0;
520   locator_t *loc = 0;
521   u8 status = 0;
522   gid_address_t addr;
523
524   q = vl_api_client_index_to_input_queue (mp->client_index);
525   if (q == 0)
526     {
527       return;
528     }
529
530   memset (&addr, 0, sizeof (addr));
531   status = lcm->flags & LISP_FLAG_USE_PETR;
532   if (status)
533     {
534       m = pool_elt_at_index (lcm->mapping_pool, lcm->petr_map_index);
535       if (~0 != m->locator_set_index)
536         {
537           ls =
538             pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
539           loc = pool_elt_at_index (lcm->locator_pool, ls->locator_indices[0]);
540           gid_address_copy (&addr, &loc->address);
541         }
542     }
543
544   /* *INDENT-OFF* */
545   REPLY_MACRO2 (VL_API_SHOW_ONE_USE_PETR_REPLY,
546   {
547     rmp->status = status;
548     ip_address_t *ip = &gid_address_ip (&addr);
549     switch (ip_addr_version (ip))
550       {
551       case IP4:
552         clib_memcpy (rmp->address, &ip_addr_v4 (ip),
553                      sizeof (ip_addr_v4 (ip)));
554         break;
555
556       case IP6:
557         clib_memcpy (rmp->address, &ip_addr_v6 (ip),
558                      sizeof (ip_addr_v6 (ip)));
559         break;
560
561       default:
562         ASSERT (0);
563       }
564     rmp->is_ip4 = (gid_address_ip_version (&addr) == IP4);
565   });
566   /* *INDENT-ON* */
567 }
568
569 static void
570   vl_api_one_add_del_map_request_itr_rlocs_t_handler
571   (vl_api_one_add_del_map_request_itr_rlocs_t * mp)
572 {
573   vl_api_one_add_del_map_request_itr_rlocs_reply_t *rmp;
574   int rv = 0;
575   u8 *locator_set_name = NULL;
576   vnet_lisp_add_del_mreq_itr_rloc_args_t _a, *a = &_a;
577
578   locator_set_name = format (0, "%s", mp->locator_set_name);
579
580   a->is_add = mp->is_add;
581   a->locator_set_name = locator_set_name;
582
583   rv = vnet_lisp_add_del_mreq_itr_rlocs (a);
584
585   vec_free (locator_set_name);
586
587   REPLY_MACRO (VL_API_ONE_ADD_DEL_MAP_REQUEST_ITR_RLOCS_REPLY);
588 }
589
590 static void
591   vl_api_one_add_del_remote_mapping_t_handler
592   (vl_api_one_add_del_remote_mapping_t * mp)
593 {
594   locator_t *rlocs = 0;
595   vl_api_one_add_del_remote_mapping_reply_t *rmp;
596   int rv = 0;
597   gid_address_t _eid, *eid = &_eid;
598   u32 rloc_num = clib_net_to_host_u32 (mp->rloc_num);
599
600   memset (eid, 0, sizeof (eid[0]));
601
602   rv = unformat_one_eid_api (eid, clib_net_to_host_u32 (mp->vni),
603                              mp->eid_type, mp->eid, mp->eid_len);
604   if (rv)
605     goto send_reply;
606
607   rlocs = unformat_one_locs (mp->rlocs, rloc_num);
608
609   if (!mp->is_add)
610     {
611       vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
612       gid_address_copy (&a->reid, eid);
613       a->is_add = 0;
614       rv = vnet_lisp_add_del_adjacency (a);
615       if (rv)
616         {
617           goto out;
618         }
619     }
620
621   /* NOTE: for now this works as a static remote mapping, i.e.,
622    * not authoritative and ttl infinite. */
623   if (mp->is_add)
624     {
625       vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args;
626       memset (m_args, 0, sizeof (m_args[0]));
627       gid_address_copy (&m_args->eid, eid);
628       m_args->action = mp->action;
629       m_args->is_static = 1;
630       m_args->ttl = ~0;
631       m_args->authoritative = 0;
632       rv = vnet_lisp_add_mapping (m_args, rlocs, NULL, NULL);
633     }
634   else
635     rv = vnet_lisp_del_mapping (eid, NULL);
636
637   if (mp->del_all)
638     vnet_lisp_clear_all_remote_adjacencies ();
639
640 out:
641   vec_free (rlocs);
642 send_reply:
643   REPLY_MACRO (VL_API_ONE_ADD_DEL_REMOTE_MAPPING_REPLY);
644 }
645
646 static void
647 vl_api_one_add_del_adjacency_t_handler (vl_api_one_add_del_adjacency_t * mp)
648 {
649   vl_api_one_add_del_adjacency_reply_t *rmp;
650   vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
651
652   int rv = 0;
653   memset (a, 0, sizeof (a[0]));
654
655   rv = unformat_one_eid_api (&a->leid, clib_net_to_host_u32 (mp->vni),
656                              mp->eid_type, mp->leid, mp->leid_len);
657   rv |= unformat_one_eid_api (&a->reid, clib_net_to_host_u32 (mp->vni),
658                               mp->eid_type, mp->reid, mp->reid_len);
659
660   if (rv)
661     goto send_reply;
662
663   a->is_add = mp->is_add;
664   rv = vnet_lisp_add_del_adjacency (a);
665
666 send_reply:
667   REPLY_MACRO (VL_API_ONE_ADD_DEL_ADJACENCY_REPLY);
668 }
669
670 static void
671 send_one_locator_details (lisp_cp_main_t * lcm,
672                           locator_t * loc,
673                           unix_shared_memory_queue_t * q, u32 context)
674 {
675   vl_api_one_locator_details_t *rmp;
676
677   rmp = vl_msg_api_alloc (sizeof (*rmp));
678   memset (rmp, 0, sizeof (*rmp));
679   rmp->_vl_msg_id = ntohs (VL_API_ONE_LOCATOR_DETAILS);
680   rmp->context = context;
681
682   rmp->local = loc->local;
683   if (loc->local)
684     {
685       rmp->sw_if_index = ntohl (loc->sw_if_index);
686     }
687   else
688     {
689       rmp->is_ipv6 = gid_address_ip_version (&loc->address);
690       ip_address_copy_addr (rmp->ip_address, &gid_address_ip (&loc->address));
691     }
692   rmp->priority = loc->priority;
693   rmp->weight = loc->weight;
694
695   vl_msg_api_send_shmem (q, (u8 *) & rmp);
696 }
697
698 static void
699 vl_api_one_locator_dump_t_handler (vl_api_one_locator_dump_t * mp)
700 {
701   u8 *ls_name = 0;
702   unix_shared_memory_queue_t *q = 0;
703   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
704   locator_set_t *lsit = 0;
705   locator_t *loc = 0;
706   u32 ls_index = ~0, *locit = 0;
707   uword *p = 0;
708
709   q = vl_api_client_index_to_input_queue (mp->client_index);
710   if (q == 0)
711     {
712       return;
713     }
714
715   if (mp->is_index_set)
716     ls_index = htonl (mp->ls_index);
717   else
718     {
719       /* make sure we get a proper C-string */
720       mp->ls_name[sizeof (mp->ls_name) - 1] = 0;
721       ls_name = format (0, "%s", mp->ls_name);
722       p = hash_get_mem (lcm->locator_set_index_by_name, ls_name);
723       if (!p)
724         goto out;
725       ls_index = p[0];
726     }
727
728   if (pool_is_free_index (lcm->locator_set_pool, ls_index))
729     return;
730
731   lsit = pool_elt_at_index (lcm->locator_set_pool, ls_index);
732
733   vec_foreach (locit, lsit->locator_indices)
734   {
735     loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
736     send_one_locator_details (lcm, loc, q, mp->context);
737   };
738 out:
739   vec_free (ls_name);
740 }
741
742 static void
743 send_one_locator_set_details (lisp_cp_main_t * lcm,
744                               locator_set_t * lsit,
745                               unix_shared_memory_queue_t * q,
746                               u32 context, u32 ls_index)
747 {
748   vl_api_one_locator_set_details_t *rmp;
749   u8 *str = 0;
750
751   rmp = vl_msg_api_alloc (sizeof (*rmp));
752   memset (rmp, 0, sizeof (*rmp));
753   rmp->_vl_msg_id = ntohs (VL_API_ONE_LOCATOR_SET_DETAILS);
754   rmp->context = context;
755
756   rmp->ls_index = htonl (ls_index);
757   if (lsit->local)
758     {
759       ASSERT (lsit->name != NULL);
760       strncpy ((char *) rmp->ls_name, (char *) lsit->name,
761                vec_len (lsit->name));
762     }
763   else
764     {
765       str = format (0, "<remote-%d>", ls_index);
766       strncpy ((char *) rmp->ls_name, (char *) str, vec_len (str));
767       vec_free (str);
768     }
769
770   vl_msg_api_send_shmem (q, (u8 *) & rmp);
771 }
772
773 static void
774 vl_api_one_locator_set_dump_t_handler (vl_api_one_locator_set_dump_t * mp)
775 {
776   unix_shared_memory_queue_t *q = NULL;
777   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
778   locator_set_t *lsit = NULL;
779   u8 filter;
780
781   q = vl_api_client_index_to_input_queue (mp->client_index);
782   if (q == 0)
783     {
784       return;
785     }
786
787   filter = mp->filter;
788   /* *INDENT-OFF* */
789   pool_foreach (lsit, lcm->locator_set_pool,
790   ({
791     if (filter && !((1 == filter && lsit->local) ||
792                     (2 == filter && !lsit->local)))
793       {
794         continue;
795       }
796     send_one_locator_set_details (lcm, lsit, q, mp->context,
797                                    lsit - lcm->locator_set_pool);
798   }));
799   /* *INDENT-ON* */
800 }
801
802 static void
803 one_fid_put_api (u8 * dst, fid_address_t * src, u8 * prefix_length)
804 {
805   ASSERT (prefix_length);
806   ip_prefix_t *ippref = &fid_addr_ippref (src);
807
808   switch (fid_addr_type (src))
809     {
810     case FID_ADDR_IP_PREF:
811       if (ip_prefix_version (ippref) == IP4)
812         clib_memcpy (dst, &ip_prefix_v4 (ippref), 4);
813       else
814         clib_memcpy (dst, &ip_prefix_v6 (ippref), 16);
815       prefix_length[0] = ip_prefix_len (ippref);
816       break;
817
818     case FID_ADDR_MAC:
819       prefix_length[0] = 0;
820       clib_memcpy (dst, fid_addr_mac (src), 6);
821       break;
822
823     default:
824       clib_warning ("Unknown FID type %d!", fid_addr_type (src));
825       break;
826     }
827 }
828
829 static u8
830 fid_type_to_api_type (fid_address_t * fid)
831 {
832   ip_prefix_t *ippref;
833
834   switch (fid_addr_type (fid))
835     {
836     case FID_ADDR_IP_PREF:
837       ippref = &fid_addr_ippref (fid);
838       if (ip_prefix_version (ippref) == IP4)
839         return 0;
840       else if (ip_prefix_version (ippref) == IP6)
841         return 1;
842       else
843         return ~0;
844
845     case FID_ADDR_MAC:
846       return 2;
847     case FID_ADDR_NSH:
848       return 3;
849     }
850
851   return ~0;
852 }
853
854 static void
855 send_one_eid_table_details (mapping_t * mapit,
856                             unix_shared_memory_queue_t * q,
857                             u32 context, u8 filter)
858 {
859   fid_address_t *fid;
860   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
861   locator_set_t *ls = 0;
862   vl_api_one_eid_table_details_t *rmp = NULL;
863   gid_address_t *gid = NULL;
864   u8 *mac = 0;
865   ip_prefix_t *ip_prefix = NULL;
866
867   if (mapit->pitr_set || mapit->nsh_set)
868     return;
869
870   switch (filter)
871     {
872     case 0:                     /* all mappings */
873       break;
874
875     case 1:                     /* local only */
876       if (!mapit->local)
877         return;
878       break;
879     case 2:                     /* remote only */
880       if (mapit->local)
881         return;
882       break;
883     default:
884       clib_warning ("Filter error, unknown filter: %d", filter);
885       return;
886     }
887
888   gid = &mapit->eid;
889   ip_prefix = &gid_address_ippref (gid);
890   mac = gid_address_mac (gid);
891
892   rmp = vl_msg_api_alloc (sizeof (*rmp));
893   memset (rmp, 0, sizeof (*rmp));
894   rmp->_vl_msg_id = ntohs (VL_API_ONE_EID_TABLE_DETAILS);
895
896   ls = pool_elt_at_index (lcm->locator_set_pool, mapit->locator_set_index);
897   if (vec_len (ls->locator_indices) == 0)
898     rmp->locator_set_index = ~0;
899   else
900     rmp->locator_set_index = clib_host_to_net_u32 (mapit->locator_set_index);
901
902   rmp->is_local = mapit->local;
903   rmp->ttl = clib_host_to_net_u32 (mapit->ttl);
904   rmp->action = mapit->action;
905   rmp->authoritative = mapit->authoritative;
906
907   switch (gid_address_type (gid))
908     {
909     case GID_ADDR_SRC_DST:
910       rmp->is_src_dst = 1;
911       fid = &gid_address_sd_src (gid);
912       rmp->eid_type = fid_type_to_api_type (fid);
913       one_fid_put_api (rmp->seid, &gid_address_sd_src (gid),
914                        &rmp->seid_prefix_len);
915       one_fid_put_api (rmp->eid, &gid_address_sd_dst (gid),
916                        &rmp->eid_prefix_len);
917       break;
918     case GID_ADDR_IP_PREFIX:
919       rmp->eid_prefix_len = ip_prefix_len (ip_prefix);
920       if (ip_prefix_version (ip_prefix) == IP4)
921         {
922           rmp->eid_type = 0;    /* ipv4 type */
923           clib_memcpy (rmp->eid, &ip_prefix_v4 (ip_prefix),
924                        sizeof (ip_prefix_v4 (ip_prefix)));
925         }
926       else
927         {
928           rmp->eid_type = 1;    /* ipv6 type */
929           clib_memcpy (rmp->eid, &ip_prefix_v6 (ip_prefix),
930                        sizeof (ip_prefix_v6 (ip_prefix)));
931         }
932       break;
933     case GID_ADDR_MAC:
934       rmp->eid_type = 2;        /* l2 mac type */
935       clib_memcpy (rmp->eid, mac, 6);
936       break;
937     case GID_ADDR_NSH:
938       rmp->eid_type = 3;        /* NSH type */
939       lisp_nsh_api_t nsh;
940       nsh.spi = clib_host_to_net_u32 (gid_address_nsh_spi (gid));
941       nsh.si = gid_address_nsh_si (gid);
942       clib_memcpy (rmp->eid, &nsh, sizeof (nsh));
943       break;
944     default:
945       ASSERT (0);
946     }
947   rmp->context = context;
948   rmp->vni = clib_host_to_net_u32 (gid_address_vni (gid));
949   rmp->key_id = clib_host_to_net_u16 (mapit->key_id);
950   memcpy (rmp->key, mapit->key, vec_len (mapit->key));
951   vl_msg_api_send_shmem (q, (u8 *) & rmp);
952 }
953
954 static void
955 vl_api_one_eid_table_dump_t_handler (vl_api_one_eid_table_dump_t * mp)
956 {
957   u32 mi;
958   unix_shared_memory_queue_t *q = NULL;
959   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
960   mapping_t *mapit = NULL;
961   gid_address_t _eid, *eid = &_eid;
962
963   q = vl_api_client_index_to_input_queue (mp->client_index);
964   if (q == 0)
965     {
966       return;
967     }
968
969   if (mp->eid_set)
970     {
971       memset (eid, 0, sizeof (*eid));
972
973       unformat_one_eid_api (eid, clib_net_to_host_u32 (mp->vni),
974                             mp->eid_type, mp->eid, mp->prefix_length);
975
976       mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
977       if ((u32) ~ 0 == mi)
978         return;
979
980       mapit = pool_elt_at_index (lcm->mapping_pool, mi);
981       send_one_eid_table_details (mapit, q, mp->context,
982                                   0 /* ignore filter */ );
983     }
984   else
985     {
986       /* *INDENT-OFF* */
987       pool_foreach (mapit, lcm->mapping_pool,
988       ({
989         send_one_eid_table_details(mapit, q, mp->context,
990                                     mp->filter);
991       }));
992       /* *INDENT-ON* */
993     }
994 }
995
996 static void
997 send_one_map_server_details (ip_address_t * ip,
998                              unix_shared_memory_queue_t * q, u32 context)
999 {
1000   vl_api_one_map_server_details_t *rmp = NULL;
1001
1002   rmp = vl_msg_api_alloc (sizeof (*rmp));
1003   memset (rmp, 0, sizeof (*rmp));
1004   rmp->_vl_msg_id = ntohs (VL_API_ONE_MAP_SERVER_DETAILS);
1005
1006   switch (ip_addr_version (ip))
1007     {
1008     case IP4:
1009       rmp->is_ipv6 = 0;
1010       clib_memcpy (rmp->ip_address, &ip_addr_v4 (ip),
1011                    sizeof (ip_addr_v4 (ip)));
1012       break;
1013
1014     case IP6:
1015       rmp->is_ipv6 = 1;
1016       clib_memcpy (rmp->ip_address, &ip_addr_v6 (ip),
1017                    sizeof (ip_addr_v6 (ip)));
1018       break;
1019
1020     default:
1021       ASSERT (0);
1022     }
1023   rmp->context = context;
1024
1025   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1026 }
1027
1028 static void
1029 vl_api_one_map_server_dump_t_handler (vl_api_one_map_server_dump_t * mp)
1030 {
1031   unix_shared_memory_queue_t *q = NULL;
1032   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1033   lisp_msmr_t *mr;
1034
1035   q = vl_api_client_index_to_input_queue (mp->client_index);
1036   if (q == 0)
1037     {
1038       return;
1039     }
1040
1041   vec_foreach (mr, lcm->map_servers)
1042   {
1043     send_one_map_server_details (&mr->address, q, mp->context);
1044   }
1045 }
1046
1047 static void
1048 send_one_map_resolver_details (ip_address_t * ip,
1049                                unix_shared_memory_queue_t * q, u32 context)
1050 {
1051   vl_api_one_map_resolver_details_t *rmp = NULL;
1052
1053   rmp = vl_msg_api_alloc (sizeof (*rmp));
1054   memset (rmp, 0, sizeof (*rmp));
1055   rmp->_vl_msg_id = ntohs (VL_API_ONE_MAP_RESOLVER_DETAILS);
1056
1057   switch (ip_addr_version (ip))
1058     {
1059     case IP4:
1060       rmp->is_ipv6 = 0;
1061       clib_memcpy (rmp->ip_address, &ip_addr_v4 (ip),
1062                    sizeof (ip_addr_v4 (ip)));
1063       break;
1064
1065     case IP6:
1066       rmp->is_ipv6 = 1;
1067       clib_memcpy (rmp->ip_address, &ip_addr_v6 (ip),
1068                    sizeof (ip_addr_v6 (ip)));
1069       break;
1070
1071     default:
1072       ASSERT (0);
1073     }
1074   rmp->context = context;
1075
1076   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1077 }
1078
1079 static void
1080 vl_api_one_map_resolver_dump_t_handler (vl_api_one_map_resolver_dump_t * mp)
1081 {
1082   unix_shared_memory_queue_t *q = NULL;
1083   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1084   lisp_msmr_t *mr;
1085
1086   q = vl_api_client_index_to_input_queue (mp->client_index);
1087   if (q == 0)
1088     {
1089       return;
1090     }
1091
1092   vec_foreach (mr, lcm->map_resolvers)
1093   {
1094     send_one_map_resolver_details (&mr->address, q, mp->context);
1095   }
1096 }
1097
1098 static void
1099 send_eid_table_map_pair (hash_pair_t * p,
1100                          unix_shared_memory_queue_t * q, u32 context)
1101 {
1102   vl_api_one_eid_table_map_details_t *rmp = NULL;
1103
1104   rmp = vl_msg_api_alloc (sizeof (*rmp));
1105   memset (rmp, 0, sizeof (*rmp));
1106   rmp->_vl_msg_id = ntohs (VL_API_ONE_EID_TABLE_MAP_DETAILS);
1107
1108   rmp->vni = clib_host_to_net_u32 (p->key);
1109   rmp->dp_table = clib_host_to_net_u32 (p->value[0]);
1110   rmp->context = context;
1111   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1112 }
1113
1114 static void
1115 vl_api_one_eid_table_map_dump_t_handler (vl_api_one_eid_table_map_dump_t * mp)
1116 {
1117   unix_shared_memory_queue_t *q = NULL;
1118   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1119   hash_pair_t *p;
1120   uword *vni_table = 0;
1121
1122   q = vl_api_client_index_to_input_queue (mp->client_index);
1123   if (q == 0)
1124     {
1125       return;
1126     }
1127
1128   if (mp->is_l2)
1129     {
1130       vni_table = lcm->bd_id_by_vni;
1131     }
1132   else
1133     {
1134       vni_table = lcm->table_id_by_vni;
1135     }
1136
1137   /* *INDENT-OFF* */
1138   hash_foreach_pair (p, vni_table,
1139   ({
1140     send_eid_table_map_pair (p, q, mp->context);
1141   }));
1142   /* *INDENT-ON* */
1143 }
1144
1145 static void
1146 send_eid_table_vni (u32 vni, unix_shared_memory_queue_t * q, u32 context)
1147 {
1148   vl_api_one_eid_table_vni_details_t *rmp = 0;
1149
1150   rmp = vl_msg_api_alloc (sizeof (*rmp));
1151   memset (rmp, 0, sizeof (*rmp));
1152   rmp->_vl_msg_id = ntohs (VL_API_ONE_EID_TABLE_VNI_DETAILS);
1153   rmp->context = context;
1154   rmp->vni = clib_host_to_net_u32 (vni);
1155   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1156 }
1157
1158 static void
1159 one_adjacency_copy (vl_api_one_adjacency_t * dst, lisp_adjacency_t * adjs)
1160 {
1161   lisp_adjacency_t *adj;
1162   vl_api_one_adjacency_t a;
1163   u32 i, n = vec_len (adjs);
1164   lisp_nsh_api_t nsh;
1165
1166   for (i = 0; i < n; i++)
1167     {
1168       adj = vec_elt_at_index (adjs, i);
1169       memset (&a, 0, sizeof (a));
1170
1171       switch (gid_address_type (&adj->reid))
1172         {
1173         case GID_ADDR_IP_PREFIX:
1174           a.reid_prefix_len = gid_address_ippref_len (&adj->reid);
1175           a.leid_prefix_len = gid_address_ippref_len (&adj->leid);
1176           if (gid_address_ip_version (&adj->reid) == IP4)
1177             {
1178               a.eid_type = 0;   /* ipv4 type */
1179               clib_memcpy (a.reid, &gid_address_ip (&adj->reid), 4);
1180               clib_memcpy (a.leid, &gid_address_ip (&adj->leid), 4);
1181             }
1182           else
1183             {
1184               a.eid_type = 1;   /* ipv6 type */
1185               clib_memcpy (a.reid, &gid_address_ip (&adj->reid), 16);
1186               clib_memcpy (a.leid, &gid_address_ip (&adj->leid), 16);
1187             }
1188           break;
1189         case GID_ADDR_MAC:
1190           a.eid_type = 2;       /* l2 mac type */
1191           mac_copy (a.reid, gid_address_mac (&adj->reid));
1192           mac_copy (a.leid, gid_address_mac (&adj->leid));
1193           break;
1194         case GID_ADDR_NSH:
1195           a.eid_type = 3;       /* NSH type */
1196           nsh.spi = clib_host_to_net_u32 (gid_address_nsh_spi (&adj->reid));
1197           nsh.si = gid_address_nsh_si (&adj->reid);
1198           clib_memcpy (a.reid, &nsh, sizeof (nsh));
1199
1200           nsh.spi = clib_host_to_net_u32 (gid_address_nsh_spi (&adj->leid));
1201           nsh.si = gid_address_nsh_si (&adj->leid);
1202           clib_memcpy (a.leid, &nsh, sizeof (nsh));
1203           break;
1204         default:
1205           ASSERT (0);
1206         }
1207       dst[i] = a;
1208     }
1209 }
1210
1211 static void
1212   vl_api_show_one_rloc_probe_state_t_handler
1213   (vl_api_show_one_rloc_probe_state_t * mp)
1214 {
1215   vl_api_show_one_rloc_probe_state_reply_t *rmp = 0;
1216   int rv = 0;
1217
1218   /* *INDENT-OFF* */
1219   REPLY_MACRO2 (VL_API_SHOW_ONE_RLOC_PROBE_STATE_REPLY,
1220   {
1221     rmp->is_enabled = vnet_lisp_rloc_probe_state_get ();
1222   });
1223   /* *INDENT-ON* */
1224 }
1225
1226 static void
1227   vl_api_show_one_map_register_state_t_handler
1228   (vl_api_show_one_map_register_state_t * mp)
1229 {
1230   vl_api_show_one_map_register_state_reply_t *rmp = 0;
1231   int rv = 0;
1232
1233   /* *INDENT-OFF* */
1234   REPLY_MACRO2 (VL_API_SHOW_ONE_MAP_REGISTER_STATE_REPLY,
1235   {
1236     rmp->is_enabled = vnet_lisp_map_register_state_get ();
1237   });
1238   /* *INDENT-ON* */
1239 }
1240
1241 static void
1242 vl_api_one_adjacencies_get_t_handler (vl_api_one_adjacencies_get_t * mp)
1243 {
1244   vl_api_one_adjacencies_get_reply_t *rmp = 0;
1245   lisp_adjacency_t *adjs = 0;
1246   int rv = 0;
1247   u32 size = ~0;
1248   u32 vni = clib_net_to_host_u32 (mp->vni);
1249
1250   adjs = vnet_lisp_adjacencies_get_by_vni (vni);
1251   size = vec_len (adjs) * sizeof (vl_api_one_adjacency_t);
1252
1253   /* *INDENT-OFF* */
1254   REPLY_MACRO4 (VL_API_ONE_ADJACENCIES_GET_REPLY, size,
1255   {
1256     rmp->count = clib_host_to_net_u32 (vec_len (adjs));
1257     one_adjacency_copy (rmp->adjacencies, adjs);
1258   });
1259   /* *INDENT-ON* */
1260
1261   vec_free (adjs);
1262 }
1263
1264 static void
1265 vl_api_one_eid_table_vni_dump_t_handler (vl_api_one_eid_table_vni_dump_t * mp)
1266 {
1267   hash_pair_t *p;
1268   u32 *vnis = 0;
1269   unix_shared_memory_queue_t *q = 0;
1270   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1271
1272   q = vl_api_client_index_to_input_queue (mp->client_index);
1273   if (q == 0)
1274     {
1275       return;
1276     }
1277
1278   /* *INDENT-OFF* */
1279   hash_foreach_pair (p, lcm->table_id_by_vni,
1280   ({
1281     hash_set (vnis, p->key, 0);
1282   }));
1283
1284   hash_foreach_pair (p, lcm->bd_id_by_vni,
1285   ({
1286     hash_set (vnis, p->key, 0);
1287   }));
1288
1289   hash_foreach_pair (p, vnis,
1290   ({
1291     send_eid_table_vni (p->key, q, mp->context);
1292   }));
1293   /* *INDENT-ON* */
1294
1295   hash_free (vnis);
1296 }
1297
1298 static void
1299 vl_api_show_one_status_t_handler (vl_api_show_one_status_t * mp)
1300 {
1301   unix_shared_memory_queue_t *q = NULL;
1302   vl_api_show_one_status_reply_t *rmp = NULL;
1303   int rv = 0;
1304
1305   q = vl_api_client_index_to_input_queue (mp->client_index);
1306   if (q == 0)
1307     {
1308       return;
1309     }
1310
1311   /* *INDENT-OFF* */
1312   REPLY_MACRO2(VL_API_SHOW_ONE_STATUS_REPLY,
1313   ({
1314     rmp->gpe_status = vnet_lisp_gpe_enable_disable_status ();
1315     rmp->feature_status = vnet_lisp_enable_disable_status ();
1316   }));
1317   /* *INDENT-ON* */
1318 }
1319
1320 static void
1321   vl_api_one_get_map_request_itr_rlocs_t_handler
1322   (vl_api_one_get_map_request_itr_rlocs_t * mp)
1323 {
1324   unix_shared_memory_queue_t *q = NULL;
1325   vl_api_one_get_map_request_itr_rlocs_reply_t *rmp = NULL;
1326   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1327   locator_set_t *loc_set = 0;
1328   u8 *tmp_str = 0;
1329   int rv = 0;
1330
1331   q = vl_api_client_index_to_input_queue (mp->client_index);
1332   if (q == 0)
1333     {
1334       return;
1335     }
1336
1337   if (~0 == lcm->mreq_itr_rlocs)
1338     {
1339       tmp_str = format (0, " ");
1340     }
1341   else
1342     {
1343       loc_set =
1344         pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
1345       tmp_str = format (0, "%s", loc_set->name);
1346     }
1347
1348   /* *INDENT-OFF* */
1349   REPLY_MACRO2(VL_API_ONE_GET_MAP_REQUEST_ITR_RLOCS_REPLY,
1350   ({
1351     strncpy((char *) rmp->locator_set_name, (char *) tmp_str,
1352             ARRAY_LEN(rmp->locator_set_name) - 1);
1353   }));
1354   /* *INDENT-ON* */
1355
1356   vec_free (tmp_str);
1357 }
1358
1359 static void
1360 vl_api_show_one_nsh_mapping_t_handler (vl_api_show_one_nsh_mapping_t * mp)
1361 {
1362   unix_shared_memory_queue_t *q = NULL;
1363   vl_api_show_one_nsh_mapping_reply_t *rmp = NULL;
1364   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1365   mapping_t *m;
1366   locator_set_t *ls = 0;
1367   u8 *tmp_str = 0;
1368   u8 is_set = 0;
1369   int rv = 0;
1370
1371   q = vl_api_client_index_to_input_queue (mp->client_index);
1372   if (q == 0)
1373     {
1374       return;
1375     }
1376
1377   if (lcm->nsh_map_index == (u32) ~ 0)
1378     {
1379       tmp_str = format (0, "N/A");
1380     }
1381   else
1382     {
1383       m = pool_elt_at_index (lcm->mapping_pool, lcm->nsh_map_index);
1384       if (~0 != m->locator_set_index)
1385         {
1386           ls =
1387             pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1388           tmp_str = format (0, "%s", ls->name);
1389           is_set = 1;
1390         }
1391       else
1392         {
1393           tmp_str = format (0, "N/A");
1394         }
1395     }
1396   vec_add1 (tmp_str, 0);
1397
1398   /* *INDENT-OFF* */
1399   REPLY_MACRO2(VL_API_SHOW_ONE_NSH_MAPPING_REPLY,
1400   ({
1401     rmp->is_set = is_set;
1402     strncpy((char *) rmp->locator_set_name, (char *) tmp_str,
1403             ARRAY_LEN(rmp->locator_set_name) - 1);
1404   }));
1405   /* *INDENT-ON* */
1406 }
1407
1408 static void
1409 vl_api_show_one_pitr_t_handler (vl_api_show_one_pitr_t * mp)
1410 {
1411   unix_shared_memory_queue_t *q = NULL;
1412   vl_api_show_one_pitr_reply_t *rmp = NULL;
1413   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1414   mapping_t *m;
1415   locator_set_t *ls = 0;
1416   u8 *tmp_str = 0;
1417   int rv = 0;
1418
1419   q = vl_api_client_index_to_input_queue (mp->client_index);
1420   if (q == 0)
1421     {
1422       return;
1423     }
1424
1425   if (!lcm->lisp_pitr)
1426     {
1427       tmp_str = format (0, "N/A");
1428     }
1429   else
1430     {
1431       m = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
1432       if (~0 != m->locator_set_index)
1433         {
1434           ls =
1435             pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1436           tmp_str = format (0, "%s", ls->name);
1437         }
1438       else
1439         {
1440           tmp_str = format (0, "N/A");
1441         }
1442     }
1443   vec_add1 (tmp_str, 0);
1444
1445   /* *INDENT-OFF* */
1446   REPLY_MACRO2(VL_API_SHOW_ONE_PITR_REPLY,
1447   ({
1448     rmp->status = lcm->lisp_pitr;
1449     strncpy((char *) rmp->locator_set_name, (char *) tmp_str,
1450             ARRAY_LEN(rmp->locator_set_name) - 1);
1451   }));
1452   /* *INDENT-ON* */
1453 }
1454
1455 static void
1456   vl_api_show_one_stats_enable_disable_t_handler
1457   (vl_api_show_one_stats_enable_disable_t * mp)
1458 {
1459   vl_api_show_one_stats_enable_disable_reply_t *rmp = NULL;
1460   vnet_api_error_t rv = 0;
1461
1462   /* *INDENT-OFF* */
1463   REPLY_MACRO2 (VL_API_SHOW_ONE_STATS_ENABLE_DISABLE_REPLY,
1464   ({
1465     rmp->is_en = vnet_lisp_stats_enable_disable_state ();
1466   }));
1467   /* *INDENT-ON* */
1468 }
1469
1470 static void
1471   vl_api_one_stats_enable_disable_t_handler
1472   (vl_api_one_stats_enable_disable_t * mp)
1473 {
1474   vl_api_one_enable_disable_reply_t *rmp = NULL;
1475
1476   vnet_api_error_t rv = vnet_lisp_stats_enable_disable (mp->is_en);
1477   REPLY_MACRO (VL_API_ONE_ENABLE_DISABLE_REPLY);
1478 }
1479
1480 static void
1481 lisp_fid_addr_to_api (fid_address_t * fid, u8 * dst, u8 * api_eid_type,
1482                       u8 * prefix_length)
1483 {
1484   switch (fid_addr_type (fid))
1485     {
1486     case FID_ADDR_IP_PREF:
1487       *prefix_length = fid_addr_prefix_length (fid);
1488       if (fid_addr_ip_version (fid) == IP4)
1489         {
1490           *api_eid_type = 0;    /* ipv4 type */
1491           clib_memcpy (dst, &fid_addr_ippref (fid), 4);
1492         }
1493       else
1494         {
1495           *api_eid_type = 1;    /* ipv6 type */
1496           clib_memcpy (dst, &fid_addr_ippref (fid), 16);
1497         }
1498       break;
1499     case FID_ADDR_MAC:
1500       *api_eid_type = 2;        /* l2 mac type */
1501       mac_copy (dst, fid_addr_mac (fid));
1502       break;
1503     default:
1504       ASSERT (0);
1505     }
1506 }
1507
1508 static void
1509 vl_api_one_stats_flush_t_handler (vl_api_one_stats_flush_t * mp)
1510 {
1511   vl_api_one_stats_flush_reply_t *rmp;
1512   u8 rv;
1513
1514   rv = vnet_lisp_flush_stats ();
1515   REPLY_MACRO (VL_API_ONE_STATS_FLUSH_REPLY);
1516 }
1517
1518 static void
1519 vl_api_one_stats_dump_t_handler (vl_api_one_stats_dump_t * mp)
1520 {
1521   vl_api_one_stats_details_t *rmp;
1522   lisp_api_stats_t *stats, *stat;
1523   u8 rv = 0;
1524
1525   stats = vnet_lisp_get_stats ();
1526   vec_foreach (stat, stats)
1527   {
1528       /* *INDENT-OFF* */
1529       REPLY_DETAILS (VL_API_ONE_STATS_DETAILS,
1530       ({
1531         lisp_fid_addr_to_api (&stat->deid, rmp->deid, &rmp->eid_type,
1532                               &rmp->deid_pref_len);
1533         lisp_fid_addr_to_api (&stat->seid, rmp->seid, &rmp->eid_type,
1534                               &rmp->seid_pref_len);
1535         rmp->vni = clib_host_to_net_u32 (stat->vni);
1536
1537         rmp->is_ip4 = ip_addr_version (&stat->rmt_rloc) == IP4 ? 1 : 0;
1538         ip_address_copy_addr (rmp->rloc, &stat->rmt_rloc);
1539         ip_address_copy_addr (rmp->lloc, &stat->loc_rloc);
1540
1541         rmp->pkt_count = clib_host_to_net_u32 (stat->counters.packets);
1542         rmp->bytes = clib_host_to_net_u32 (stat->counters.bytes);
1543       }));
1544       /* *INDENT-ON* */
1545   }
1546 }
1547
1548 static void
1549   vl_api_one_add_del_l2_arp_entry_t_handler
1550   (vl_api_one_add_del_l2_arp_entry_t * mp)
1551 {
1552   vl_api_one_add_del_l2_arp_entry_reply_t *rmp;
1553   int rv = 0;
1554   gid_address_t _arp, *arp = &_arp;
1555   memset (arp, 0, sizeof (*arp));
1556
1557   gid_address_type (arp) = GID_ADDR_ARP;
1558   gid_address_arp_bd (arp) = clib_net_to_host_u32 (mp->bd);
1559
1560   /* vpp keeps ip4 addresses in network byte order */
1561   clib_memcpy (&gid_address_arp_ip4 (arp), &mp->ip4, 4);
1562
1563   rv = vnet_lisp_add_del_l2_arp_entry (arp, mp->mac, mp->is_add);
1564
1565   REPLY_MACRO (VL_API_ONE_ADD_DEL_L2_ARP_ENTRY_REPLY);
1566 }
1567
1568 static void
1569 vl_api_one_l2_arp_bd_get_t_handler (vl_api_one_l2_arp_bd_get_t * mp)
1570 {
1571   vl_api_one_l2_arp_bd_get_reply_t *rmp;
1572   int rv = 0;
1573   u32 i = 0;
1574   hash_pair_t *p;
1575
1576   u32 *bds = vnet_lisp_l2_arp_bds_get ();
1577   u32 size = hash_elts (bds) * sizeof (u32);
1578
1579   /* *INDENT-OFF* */
1580   REPLY_MACRO4 (VL_API_ONE_L2_ARP_BD_GET_REPLY, size,
1581   {
1582     rmp->count = clib_host_to_net_u32 (hash_elts (bds));
1583     hash_foreach_pair (p, bds,
1584     ({
1585       rmp->bridge_domains[i++] = clib_host_to_net_u32 (p->key);
1586     }));
1587   });
1588   /* *INDENT-ON* */
1589
1590   hash_free (bds);
1591 }
1592
1593 static void
1594 vl_api_one_l2_arp_entries_get_t_handler (vl_api_one_l2_arp_entries_get_t * mp)
1595 {
1596   vl_api_one_l2_arp_entries_get_reply_t *rmp;
1597   lisp_api_l2_arp_entry_t *entries = 0, *e;
1598   u32 i = 0;
1599   int rv = 0;
1600
1601   u32 bd = clib_net_to_host_u32 (mp->bd);
1602
1603   entries = vnet_lisp_l2_arp_entries_get_by_bd (bd);
1604   u32 size = vec_len (entries) * sizeof (vl_api_one_l2_arp_entry_t);
1605
1606   /* *INDENT-OFF* */
1607   REPLY_MACRO4 (VL_API_ONE_L2_ARP_ENTRIES_GET_REPLY, size,
1608   {
1609     rmp->count = clib_host_to_net_u32 (vec_len (entries));
1610     vec_foreach (e, entries)
1611       {
1612         mac_copy (rmp->entries[i].mac, e->mac);
1613         rmp->entries[i].ip4 = e->ip4;
1614         i++;
1615       }
1616   });
1617   /* *INDENT-ON* */
1618
1619   vec_free (entries);
1620 }
1621
1622 static void
1623   vl_api_one_map_register_fallback_threshold_t_handler
1624   (vl_api_one_map_register_fallback_threshold_t * mp)
1625 {
1626   vl_api_one_map_register_fallback_threshold_reply_t *rmp;
1627   int rv = 0;
1628
1629   mp->value = clib_net_to_host_u32 (mp->value);
1630   rv = vnet_lisp_map_register_fallback_threshold_set (mp->value);
1631   REPLY_MACRO (VL_API_ONE_MAP_REGISTER_FALLBACK_THRESHOLD_REPLY);
1632 }
1633
1634 static void
1635   vl_api_show_one_map_register_fallback_threshold_t_handler
1636   (vl_api_show_one_map_register_fallback_threshold_t * mp)
1637 {
1638   vl_api_show_one_map_register_fallback_threshold_reply_t *rmp;
1639   int rv = 0;
1640
1641   u32 value = vnet_lisp_map_register_fallback_threshold_get ();
1642
1643   /* *INDENT-OFF* */
1644   REPLY_MACRO2 (VL_API_SHOW_ONE_MAP_REGISTER_FALLBACK_THRESHOLD_REPLY,
1645   ({
1646     rmp->value = clib_host_to_net_u32 (value);
1647   }));
1648   /* *INDENT-ON* */
1649 }
1650
1651 /*
1652  * one_api_hookup
1653  * Add vpe's API message handlers to the table.
1654  * vlib has alread mapped shared memory and
1655  * added the client registration handlers.
1656  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1657  */
1658 #define vl_msg_name_crc_list
1659 #include <vnet/vnet_all_api_h.h>
1660 #undef vl_msg_name_crc_list
1661
1662 static void
1663 setup_message_id_table (api_main_t * am)
1664 {
1665 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1666   foreach_vl_msg_name_crc_one;
1667 #undef _
1668 }
1669
1670 static clib_error_t *
1671 one_api_hookup (vlib_main_t * vm)
1672 {
1673   api_main_t *am = &api_main;
1674
1675 #define _(N,n)                                                  \
1676     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
1677                            vl_api_##n##_t_handler,              \
1678                            vl_noop_handler,                     \
1679                            vl_api_##n##_t_endian,               \
1680                            vl_api_##n##_t_print,                \
1681                            sizeof(vl_api_##n##_t), 1);
1682   foreach_vpe_api_msg;
1683 #undef _
1684
1685   /*
1686    * Set up the (msg_name, crc, message-id) table
1687    */
1688   setup_message_id_table (am);
1689
1690   return 0;
1691 }
1692
1693 VLIB_API_INIT_FUNCTION (one_api_hookup);
1694
1695 /*
1696  * fd.io coding-style-patch-verification: ON
1697  *
1698  * Local Variables:
1699  * eval: (c-set-style "gnu")
1700  * End:
1701  */