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