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