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