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