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