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