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