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