lisp: Move to plugin
[vpp.git] / src / plugins / lisp / lisp-cp / lisp_api.c
1 /*
2  *------------------------------------------------------------------
3  * lisp_api.c - lisp api
4  *
5  * Copyright (c) 2016 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 #include <vnet/ip/ip_types_api.h>
28 #include <vnet/ethernet/ethernet_types_api.h>
29 #include <lisp/lisp-cp/lisp_types_api.h>
30
31 /* define message IDs */
32 #include <vnet/format_fns.h>
33 #include <lisp/lisp-cp/lisp.api_enum.h>
34 #include <lisp/lisp-cp/lisp.api_types.h>
35
36 /**
37  * Base message ID fot the plugin
38  */
39 static u32 lisp_base_msg_id;
40 #define REPLY_MSG_ID_BASE lisp_base_msg_id
41
42 #include <vlibapi/api_helper_macros.h>
43
44 static locator_t *
45 unformat_lisp_locs (vl_api_remote_locator_t * rmt_locs, u32 rloc_num)
46 {
47   u32 i;
48   locator_t *locs = 0, loc;
49   vl_api_remote_locator_t *r;
50
51   for (i = 0; i < rloc_num; i++)
52     {
53       /* remote locators */
54       r = &rmt_locs[i];
55       clib_memset (&loc, 0, sizeof (loc));
56       ip_address_decode2 (&r->ip_address, &loc.address.ippref.addr);
57       loc.address.ippref.len =
58         ip_address_max_len (loc.address.ippref.addr.version);
59
60       loc.priority = r->priority;
61       loc.weight = r->weight;
62
63       vec_add1 (locs, loc);
64     }
65   return locs;
66 }
67
68 static void
69 vl_api_lisp_add_del_locator_set_t_handler (vl_api_lisp_add_del_locator_set_t *
70                                            mp)
71 {
72   vl_api_lisp_add_del_locator_set_reply_t *rmp;
73   int rv = 0;
74   vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
75   locator_t locator;
76   vl_api_local_locator_t *ls_loc;
77   u32 ls_index = ~0, locator_num;
78   u8 *locator_name = NULL;
79   int i;
80
81   clib_memset (a, 0, sizeof (a[0]));
82
83   mp->locator_set_name[sizeof (mp->locator_set_name) - 1] = 0;
84   locator_name = format (0, "%s", mp->locator_set_name);
85   vec_terminate_c_string (locator_name);
86
87   a->name = locator_name;
88   a->is_add = mp->is_add;
89   a->local = 1;
90   locator_num = clib_net_to_host_u32 (mp->locator_num);
91
92   clib_memset (&locator, 0, sizeof (locator));
93   for (i = 0; i < locator_num; i++)
94     {
95       ls_loc = &mp->locators[i];
96       VALIDATE_SW_IF_INDEX (ls_loc);
97
98       locator.sw_if_index = htonl (ls_loc->sw_if_index);
99       locator.priority = ls_loc->priority;
100       locator.weight = ls_loc->weight;
101       locator.local = 1;
102       vec_add1 (a->locators, locator);
103     }
104
105   rv = vnet_lisp_add_del_locator_set (a, &ls_index);
106
107   BAD_SW_IF_INDEX_LABEL;
108
109   vec_free (locator_name);
110   vec_free (a->locators);
111
112   /* *INDENT-OFF* */
113   REPLY_MACRO2 (VL_API_LISP_ADD_DEL_LOCATOR_SET_REPLY,
114   ({
115     rmp->ls_index = clib_host_to_net_u32 (ls_index);
116   }));
117   /* *INDENT-ON* */
118 }
119
120 static void
121 vl_api_lisp_add_del_locator_t_handler (vl_api_lisp_add_del_locator_t * mp)
122 {
123   vl_api_lisp_add_del_locator_reply_t *rmp;
124   int rv = 0;
125   locator_t locator, *locators = NULL;
126   vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
127   u32 ls_index = ~0;
128   u8 *locator_name = NULL;
129
130   clib_memset (&locator, 0, sizeof (locator));
131   clib_memset (a, 0, sizeof (a[0]));
132
133   locator.sw_if_index = ntohl (mp->sw_if_index);
134   locator.priority = mp->priority;
135   locator.weight = mp->weight;
136   locator.local = 1;
137   vec_add1 (locators, locator);
138
139   mp->locator_set_name[sizeof (mp->locator_set_name) - 1] = 0;
140   locator_name = format (0, "%s", mp->locator_set_name);
141   vec_terminate_c_string (locator_name);
142
143   a->name = locator_name;
144   a->locators = locators;
145   a->is_add = mp->is_add;
146   a->local = 1;
147
148   rv = vnet_lisp_add_del_locator (a, NULL, &ls_index);
149
150   vec_free (locators);
151   vec_free (locator_name);
152
153   REPLY_MACRO (VL_API_LISP_ADD_DEL_LOCATOR_REPLY);
154 }
155
156 static void
157 vl_api_lisp_add_del_local_eid_t_handler (vl_api_lisp_add_del_local_eid_t * mp)
158 {
159   vl_api_lisp_add_del_local_eid_reply_t *rmp;
160   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
161   int rv = 0;
162   gid_address_t _gid, *gid = &_gid;
163   uword *p = NULL;
164   u32 locator_set_index = ~0, map_index = ~0;
165   vnet_lisp_add_del_mapping_args_t _a, *a = &_a;
166   u8 *name = NULL, *key = NULL;
167   clib_memset (a, 0, sizeof (a[0]));
168   clib_memset (gid, 0, sizeof (gid[0]));
169
170   rv = unformat_lisp_eid_api (gid, mp->vni, &mp->eid);
171   if (rv)
172     goto out;
173
174   mp->locator_set_name[sizeof (mp->locator_set_name) - 1] = 0;
175   name = format (0, "%s", mp->locator_set_name);
176   vec_terminate_c_string (name);
177   p = hash_get_mem (lcm->locator_set_index_by_name, name);
178   if (!p)
179     {
180       rv = VNET_API_ERROR_INVALID_VALUE;
181       goto out;
182     }
183   locator_set_index = p[0];
184
185   if (mp->key.id)
186     key = format (0, "%s", mp->key.key);
187
188   /* XXX treat batch configuration */
189   a->is_add = mp->is_add;
190   gid_address_copy (&a->eid, gid);
191   a->locator_set_index = locator_set_index;
192   a->local = 1;
193   a->key = key;
194   a->key_id = clib_net_to_host_u16 (mp->key.id);
195
196   rv = vnet_lisp_add_del_local_mapping (a, &map_index);
197
198 out:
199   vec_free (name);
200   vec_free (key);
201   gid_address_free (&a->eid);
202
203   REPLY_MACRO (VL_API_LISP_ADD_DEL_LOCAL_EID_REPLY);
204 }
205
206 static void
207   vl_api_lisp_eid_table_add_del_map_t_handler
208   (vl_api_lisp_eid_table_add_del_map_t * mp)
209 {
210   vl_api_lisp_eid_table_add_del_map_reply_t *rmp;
211   int rv = 0;
212   rv = vnet_lisp_eid_table_map (clib_net_to_host_u32 (mp->vni),
213                                 clib_net_to_host_u32 (mp->dp_table),
214                                 mp->is_l2, mp->is_add);
215 REPLY_MACRO (VL_API_LISP_EID_TABLE_ADD_DEL_MAP_REPLY)}
216
217 static void
218 vl_api_lisp_add_del_map_server_t_handler (vl_api_lisp_add_del_map_server_t
219                                           * mp)
220 {
221   vl_api_lisp_add_del_map_server_reply_t *rmp;
222   int rv = 0;
223   ip_address_t addr;
224
225   clib_memset (&addr, 0, sizeof (addr));
226
227   ip_address_decode2 (&mp->ip_address, &addr);
228   rv = vnet_lisp_add_del_map_server (&addr, mp->is_add);
229
230   REPLY_MACRO (VL_API_LISP_ADD_DEL_MAP_SERVER_REPLY);
231 }
232
233 static void
234 vl_api_lisp_add_del_map_resolver_t_handler (vl_api_lisp_add_del_map_resolver_t
235                                             * mp)
236 {
237   vl_api_lisp_add_del_map_resolver_reply_t *rmp;
238   int rv = 0;
239   vnet_lisp_add_del_map_resolver_args_t _a, *a = &_a;
240
241   clib_memset (a, 0, sizeof (a[0]));
242
243   a->is_add = mp->is_add;
244   ip_address_decode2 (&mp->ip_address, &a->address);
245
246   rv = vnet_lisp_add_del_map_resolver (a);
247
248   REPLY_MACRO (VL_API_LISP_ADD_DEL_MAP_RESOLVER_REPLY);
249 }
250
251 static void
252   vl_api_lisp_map_register_enable_disable_t_handler
253   (vl_api_lisp_map_register_enable_disable_t * mp)
254 {
255   vl_api_lisp_map_register_enable_disable_reply_t *rmp;
256   int rv = 0;
257
258   vnet_lisp_map_register_enable_disable (mp->is_enable);
259   REPLY_MACRO (VL_API_LISP_ENABLE_DISABLE_REPLY);
260 }
261
262 static void
263   vl_api_lisp_rloc_probe_enable_disable_t_handler
264   (vl_api_lisp_rloc_probe_enable_disable_t * mp)
265 {
266   vl_api_lisp_rloc_probe_enable_disable_reply_t *rmp;
267   int rv = 0;
268
269   vnet_lisp_rloc_probe_enable_disable (mp->is_enable);
270   REPLY_MACRO (VL_API_LISP_ENABLE_DISABLE_REPLY);
271 }
272
273 static void
274 vl_api_lisp_enable_disable_t_handler (vl_api_lisp_enable_disable_t * mp)
275 {
276   vl_api_lisp_enable_disable_reply_t *rmp;
277   int rv = 0;
278
279   vnet_lisp_enable_disable (mp->is_enable);
280   REPLY_MACRO (VL_API_LISP_ENABLE_DISABLE_REPLY);
281 }
282
283 static void
284   vl_api_show_lisp_map_request_mode_t_handler
285   (vl_api_show_lisp_map_request_mode_t * mp)
286 {
287   int rv = 0;
288   vl_api_show_lisp_map_request_mode_reply_t *rmp;
289
290   /* *INDENT-OFF* */
291   REPLY_MACRO2(VL_API_SHOW_LISP_MAP_REQUEST_MODE_REPLY,
292   ({
293     rmp->is_src_dst = vnet_lisp_get_map_request_mode ();
294   }));
295   /* *INDENT-ON* */
296 }
297
298 static void
299 vl_api_lisp_map_request_mode_t_handler (vl_api_lisp_map_request_mode_t * mp)
300 {
301   vl_api_lisp_map_request_mode_reply_t *rmp;
302   int rv = 0;
303
304   rv = vnet_lisp_set_map_request_mode (mp->is_src_dst);
305
306   REPLY_MACRO (VL_API_LISP_MAP_REQUEST_MODE_REPLY);
307 }
308
309 static void
310 vl_api_lisp_pitr_set_locator_set_t_handler (vl_api_lisp_pitr_set_locator_set_t
311                                             * mp)
312 {
313   vl_api_lisp_pitr_set_locator_set_reply_t *rmp;
314   int rv = 0;
315   u8 *ls_name = 0;
316
317   mp->ls_name[sizeof (mp->ls_name) - 1] = 0;
318   ls_name = format (0, "%s", mp->ls_name);
319   vec_terminate_c_string (ls_name);
320   rv = vnet_lisp_pitr_set_locator_set (ls_name, mp->is_add);
321   vec_free (ls_name);
322
323   REPLY_MACRO (VL_API_LISP_PITR_SET_LOCATOR_SET_REPLY);
324 }
325
326 static void
327 vl_api_lisp_use_petr_t_handler (vl_api_lisp_use_petr_t * mp)
328 {
329   vl_api_lisp_use_petr_reply_t *rmp;
330   int rv = 0;
331   ip_address_t addr;
332
333   ip_address_decode2 (&mp->ip_address, &addr);
334   rv = vnet_lisp_use_petr (&addr, mp->is_add);
335
336   REPLY_MACRO (VL_API_LISP_USE_PETR_REPLY);
337 }
338
339 static void
340 vl_api_show_lisp_use_petr_t_handler (vl_api_show_lisp_use_petr_t * mp)
341 {
342   vl_api_show_lisp_use_petr_reply_t *rmp = NULL;
343   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
344   mapping_t *m;
345   locator_set_t *ls = 0;
346   int rv = 0;
347   locator_t *loc = 0;
348   u8 status = 0;
349   gid_address_t addr;
350
351   clib_memset (&addr, 0, sizeof (addr));
352   status = lcm->flags & LISP_FLAG_USE_PETR;
353   if (status)
354     {
355       m = pool_elt_at_index (lcm->mapping_pool, lcm->petr_map_index);
356       if (~0 != m->locator_set_index)
357         {
358           ls =
359             pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
360           loc = pool_elt_at_index (lcm->locator_pool, ls->locator_indices[0]);
361           gid_address_copy (&addr, &loc->address);
362         }
363     }
364
365   /* *INDENT-OFF* */
366   REPLY_MACRO2 (VL_API_SHOW_LISP_USE_PETR_REPLY,
367   {
368     rmp->is_petr_enable = status;
369     ip_address_encode2 (&gid_address_ip (&addr), &rmp->ip_address);
370   });
371   /* *INDENT-ON* */
372 }
373
374 static void
375   vl_api_lisp_add_del_map_request_itr_rlocs_t_handler
376   (vl_api_lisp_add_del_map_request_itr_rlocs_t * mp)
377 {
378   vl_api_lisp_add_del_map_request_itr_rlocs_reply_t *rmp;
379   int rv = 0;
380   u8 *locator_set_name = NULL;
381   vnet_lisp_add_del_mreq_itr_rloc_args_t _a, *a = &_a;
382
383   mp->locator_set_name[sizeof (mp->locator_set_name) - 1] = 0;
384   locator_set_name = format (0, "%s", mp->locator_set_name);
385   vec_terminate_c_string (locator_set_name);
386
387   a->is_add = mp->is_add;
388   a->locator_set_name = locator_set_name;
389
390   rv = vnet_lisp_add_del_mreq_itr_rlocs (a);
391
392   vec_free (locator_set_name);
393
394   REPLY_MACRO (VL_API_LISP_ADD_DEL_MAP_REQUEST_ITR_RLOCS_REPLY);
395 }
396
397 static void
398   vl_api_lisp_add_del_remote_mapping_t_handler
399   (vl_api_lisp_add_del_remote_mapping_t * mp)
400 {
401   locator_t *rlocs = 0;
402   vl_api_lisp_add_del_remote_mapping_reply_t *rmp;
403   int rv = 0;
404   gid_address_t _eid, *eid = &_eid;
405   u32 rloc_num = clib_net_to_host_u32 (mp->rloc_num);
406
407   clib_memset (eid, 0, sizeof (eid[0]));
408
409   rv = unformat_lisp_eid_api (eid, mp->vni, &mp->deid);
410   if (rv)
411     goto send_reply;
412
413   rlocs = unformat_lisp_locs (mp->rlocs, rloc_num);
414
415   if (!mp->is_add)
416     {
417       vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
418       clib_memset (a, 0, sizeof (*a));
419       gid_address_copy (&a->reid, eid);
420       a->is_add = 0;
421       rv = vnet_lisp_add_del_adjacency (a);
422       if (rv)
423         {
424           goto out;
425         }
426     }
427
428   /* NOTE: for now this works as a static remote mapping, i.e.,
429    * not authoritative and ttl infinite. */
430   if (mp->is_add)
431     {
432       vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args;
433       clib_memset (m_args, 0, sizeof (m_args[0]));
434       gid_address_copy (&m_args->eid, eid);
435       m_args->action = mp->action;
436       m_args->is_static = 1;
437       m_args->ttl = ~0;
438       m_args->authoritative = 0;
439       rv = vnet_lisp_add_mapping (m_args, rlocs, NULL, NULL);
440     }
441   else
442     {
443       rv = vnet_lisp_del_mapping (eid, NULL);
444     }
445
446   if (mp->del_all)
447     vnet_lisp_clear_all_remote_adjacencies ();
448
449 out:
450   vec_free (rlocs);
451 send_reply:
452   REPLY_MACRO (VL_API_LISP_ADD_DEL_REMOTE_MAPPING_REPLY);
453 }
454
455 static void
456 vl_api_lisp_add_del_adjacency_t_handler (vl_api_lisp_add_del_adjacency_t * mp)
457 {
458   vl_api_lisp_add_del_adjacency_reply_t *rmp;
459   vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
460
461   int rv = 0;
462   clib_memset (a, 0, sizeof (a[0]));
463
464   rv = unformat_lisp_eid_api (&a->leid, mp->vni, &mp->leid);
465   rv = unformat_lisp_eid_api (&a->reid, mp->vni, &mp->reid);
466
467   if (rv)
468     goto send_reply;
469
470   a->is_add = mp->is_add;
471   rv = vnet_lisp_add_del_adjacency (a);
472
473 send_reply:
474   REPLY_MACRO (VL_API_LISP_ADD_DEL_ADJACENCY_REPLY);
475 }
476
477 static void
478 send_lisp_locator_details (lisp_cp_main_t * lcm,
479                            locator_t * loc, vl_api_registration_t * reg,
480                            u32 context)
481 {
482   vl_api_lisp_locator_details_t *rmp;
483
484   rmp = vl_msg_api_alloc (sizeof (*rmp));
485   clib_memset (rmp, 0, sizeof (*rmp));
486   rmp->_vl_msg_id = ntohs (VL_API_LISP_LOCATOR_DETAILS + REPLY_MSG_ID_BASE);
487   rmp->context = context;
488
489   rmp->local = loc->local;
490   if (loc->local)
491     {
492       rmp->sw_if_index = ntohl (loc->sw_if_index);
493     }
494   else
495     {
496       ip_address_encode2 (&gid_address_ip (&loc->address), &rmp->ip_address);
497     }
498   rmp->priority = loc->priority;
499   rmp->weight = loc->weight;
500
501   vl_api_send_msg (reg, (u8 *) rmp);
502 }
503
504 static void
505 vl_api_lisp_locator_dump_t_handler (vl_api_lisp_locator_dump_t * mp)
506 {
507   u8 *ls_name = 0;
508   vl_api_registration_t *reg = 0;
509   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
510   locator_set_t *lsit = 0;
511   locator_t *loc = 0;
512   u32 ls_index = ~0, *locit = 0;
513   uword *p = 0;
514
515   reg = vl_api_client_index_to_registration (mp->client_index);
516   if (!reg)
517     return;
518
519   if (mp->is_index_set)
520     ls_index = htonl (mp->ls_index);
521   else
522     {
523       /* make sure we get a proper C-string */
524       mp->ls_name[sizeof (mp->ls_name) - 1] = 0;
525       ls_name = format (0, "%s", mp->ls_name);
526       vec_terminate_c_string (ls_name);
527       p = hash_get_mem (lcm->locator_set_index_by_name, ls_name);
528       if (!p)
529         goto out;
530       ls_index = p[0];
531     }
532
533   if (pool_is_free_index (lcm->locator_set_pool, ls_index))
534     return;
535
536   lsit = pool_elt_at_index (lcm->locator_set_pool, ls_index);
537
538   vec_foreach (locit, lsit->locator_indices)
539   {
540     loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
541     send_lisp_locator_details (lcm, loc, reg, mp->context);
542   };
543 out:
544   vec_free (ls_name);
545 }
546
547 static void
548 send_lisp_locator_set_details (lisp_cp_main_t * lcm,
549                                locator_set_t * lsit,
550                                vl_api_registration_t * reg, u32 context,
551                                u32 ls_index)
552 {
553   vl_api_lisp_locator_set_details_t *rmp;
554   u8 *str = 0;
555
556   rmp = vl_msg_api_alloc (sizeof (*rmp));
557   clib_memset (rmp, 0, sizeof (*rmp));
558   rmp->_vl_msg_id =
559     ntohs (VL_API_LISP_LOCATOR_SET_DETAILS + REPLY_MSG_ID_BASE);
560   rmp->context = context;
561
562   rmp->ls_index = htonl (ls_index);
563   if (lsit->local)
564     {
565       ASSERT (lsit->name != NULL);
566       strncpy ((char *) rmp->ls_name, (char *) lsit->name,
567                vec_len (lsit->name));
568     }
569   else
570     {
571       str = format (0, "<remote-%d>", ls_index);
572       strncpy ((char *) rmp->ls_name, (char *) str, vec_len (str));
573       vec_free (str);
574     }
575
576   vl_api_send_msg (reg, (u8 *) rmp);
577 }
578
579 static void
580 vl_api_lisp_locator_set_dump_t_handler (vl_api_lisp_locator_set_dump_t * mp)
581 {
582   vl_api_registration_t *reg;
583   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
584   locator_set_t *lsit = NULL;
585   u8 filter;
586
587   reg = vl_api_client_index_to_registration (mp->client_index);
588   if (!reg)
589     return;
590
591   filter = mp->filter;
592   /* *INDENT-OFF* */
593   pool_foreach (lsit, lcm->locator_set_pool,
594   ({
595     if (filter && !((1 == filter && lsit->local) ||
596                     (2 == filter && !lsit->local)))
597       {
598         continue;
599       }
600     send_lisp_locator_set_details (lcm, lsit, reg, mp->context,
601                                    lsit - lcm->locator_set_pool);
602   }));
603   /* *INDENT-ON* */
604 }
605
606 static void
607 send_lisp_eid_table_details (mapping_t * mapit,
608                              vl_api_registration_t * reg, u32 context,
609                              u8 filter)
610 {
611   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
612   locator_set_t *ls = 0;
613   vl_api_lisp_eid_table_details_t *rmp = NULL;
614   gid_address_t *gid = NULL;
615
616   switch (filter)
617     {
618     case 0:                     /* all mappings */
619       break;
620
621     case 1:                     /* local only */
622       if (!mapit->local)
623         return;
624       break;
625     case 2:                     /* remote only */
626       if (mapit->local)
627         return;
628       break;
629     default:
630       clib_warning ("Filter error, unknown filter: %d", filter);
631       return;
632     }
633
634   /* don't send PITR generated mapping */
635   if (mapit->pitr_set)
636     return;
637
638   gid = &mapit->eid;
639
640   rmp = vl_msg_api_alloc (sizeof (*rmp));
641   clib_memset (rmp, 0, sizeof (*rmp));
642   rmp->_vl_msg_id = ntohs (VL_API_LISP_EID_TABLE_DETAILS + REPLY_MSG_ID_BASE);
643
644   ls = pool_elt_at_index (lcm->locator_set_pool, mapit->locator_set_index);
645   if (vec_len (ls->locator_indices) == 0)
646     rmp->locator_set_index = ~0;
647   else
648     rmp->locator_set_index = clib_host_to_net_u32 (mapit->locator_set_index);
649
650   rmp->is_local = mapit->local;
651   rmp->ttl = clib_host_to_net_u32 (mapit->ttl);
652   rmp->action = mapit->action;
653   rmp->authoritative = mapit->authoritative;
654   switch (gid_address_type (gid))
655     {
656     case GID_ADDR_SRC_DST:
657       lisp_fid_put_api (&rmp->seid, &gid_address_sd_src (gid));
658       lisp_fid_put_api (&rmp->deid, &gid_address_sd_dst (gid));
659       rmp->is_src_dst = 1;
660       break;
661     case GID_ADDR_IP_PREFIX:
662       lisp_gid_put_api (&rmp->seid, gid);
663       break;
664     case GID_ADDR_MAC:
665       lisp_gid_put_api (&rmp->seid, gid);
666       break;
667     default:
668       ASSERT (0);
669     }
670   rmp->context = context;
671   rmp->vni = clib_host_to_net_u32 (gid_address_vni (gid));
672   rmp->key.id = clib_host_to_net_u16 (mapit->key_id);
673   memcpy (rmp->key.key, mapit->key, vec_len (mapit->key));
674   vl_api_send_msg (reg, (u8 *) rmp);
675 }
676
677 static void
678 vl_api_lisp_eid_table_dump_t_handler (vl_api_lisp_eid_table_dump_t * mp)
679 {
680   u32 mi;
681   vl_api_registration_t *reg;
682   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
683   mapping_t *mapit = NULL;
684   gid_address_t _eid, *eid = &_eid;
685
686   reg = vl_api_client_index_to_registration (mp->client_index);
687   if (!reg)
688     return;
689
690   if (mp->eid_set)
691     {
692       clib_memset (eid, 0, sizeof (*eid));
693
694       unformat_lisp_eid_api (eid, mp->vni, &mp->eid);
695
696       mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
697       if ((u32) ~ 0 == mi)
698         return;
699
700       mapit = pool_elt_at_index (lcm->mapping_pool, mi);
701       send_lisp_eid_table_details (mapit, reg, mp->context,
702                                    0 /* ignore filter */ );
703     }
704   else
705     {
706       /* *INDENT-OFF* */
707       pool_foreach (mapit, lcm->mapping_pool,
708       ({
709         send_lisp_eid_table_details(mapit, reg, mp->context,
710                                     mp->filter);
711       }));
712       /* *INDENT-ON* */
713     }
714 }
715
716 static void
717 send_lisp_map_server_details (ip_address_t * ip, vl_api_registration_t * reg,
718                               u32 context)
719 {
720   vl_api_lisp_map_server_details_t *rmp = NULL;
721
722   rmp = vl_msg_api_alloc (sizeof (*rmp));
723   clib_memset (rmp, 0, sizeof (*rmp));
724   rmp->_vl_msg_id =
725     ntohs (VL_API_LISP_MAP_SERVER_DETAILS + REPLY_MSG_ID_BASE);
726
727   ip_address_encode2 (ip, &rmp->ip_address);
728   rmp->context = context;
729
730   vl_api_send_msg (reg, (u8 *) rmp);
731 }
732
733 static void
734 vl_api_lisp_map_server_dump_t_handler (vl_api_lisp_map_server_dump_t * mp)
735 {
736   vl_api_registration_t *reg;
737   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
738   lisp_msmr_t *mr;
739
740   reg = vl_api_client_index_to_registration (mp->client_index);
741   if (!reg)
742     return;
743
744   vec_foreach (mr, lcm->map_servers)
745   {
746     send_lisp_map_server_details (&mr->address, reg, mp->context);
747   }
748 }
749
750 static void
751 send_lisp_map_resolver_details (ip_address_t * ip,
752                                 vl_api_registration_t * reg, u32 context)
753 {
754   vl_api_lisp_map_resolver_details_t *rmp = NULL;
755
756   rmp = vl_msg_api_alloc (sizeof (*rmp));
757   clib_memset (rmp, 0, sizeof (*rmp));
758   rmp->_vl_msg_id =
759     ntohs (VL_API_LISP_MAP_RESOLVER_DETAILS + REPLY_MSG_ID_BASE);
760
761   ip_address_encode2 (ip, &rmp->ip_address);
762   rmp->context = context;
763
764   vl_api_send_msg (reg, (u8 *) rmp);
765 }
766
767 static void
768 vl_api_lisp_map_resolver_dump_t_handler (vl_api_lisp_map_resolver_dump_t * mp)
769 {
770   vl_api_registration_t *reg;
771   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
772   lisp_msmr_t *mr;
773
774   reg = vl_api_client_index_to_registration (mp->client_index);
775   if (!reg)
776     return;
777
778   vec_foreach (mr, lcm->map_resolvers)
779   {
780     send_lisp_map_resolver_details (&mr->address, reg, mp->context);
781   }
782 }
783
784 static void
785 send_eid_table_map_pair (hash_pair_t * p, vl_api_registration_t * reg,
786                          u32 context)
787 {
788   vl_api_lisp_eid_table_map_details_t *rmp = NULL;
789
790   rmp = vl_msg_api_alloc (sizeof (*rmp));
791   clib_memset (rmp, 0, sizeof (*rmp));
792   rmp->_vl_msg_id =
793     ntohs (VL_API_LISP_EID_TABLE_MAP_DETAILS + REPLY_MSG_ID_BASE);
794
795   rmp->vni = clib_host_to_net_u32 (p->key);
796   rmp->dp_table = clib_host_to_net_u32 (p->value[0]);
797   rmp->context = context;
798   vl_api_send_msg (reg, (u8 *) rmp);
799 }
800
801 static void
802 vl_api_lisp_eid_table_map_dump_t_handler (vl_api_lisp_eid_table_map_dump_t *
803                                           mp)
804 {
805   vl_api_registration_t *reg;
806   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
807   hash_pair_t *p;
808   uword *vni_table = 0;
809
810   reg = vl_api_client_index_to_registration (mp->client_index);
811   if (!reg)
812     return;
813
814   if (mp->is_l2)
815     {
816       vni_table = lcm->bd_id_by_vni;
817     }
818   else
819     {
820       vni_table = lcm->table_id_by_vni;
821     }
822
823   /* *INDENT-OFF* */
824   hash_foreach_pair (p, vni_table,
825   ({
826     send_eid_table_map_pair (p, reg, mp->context);
827   }));
828   /* *INDENT-ON* */
829 }
830
831 static void
832 send_eid_table_vni (u32 vni, vl_api_registration_t * reg, u32 context)
833 {
834   vl_api_lisp_eid_table_vni_details_t *rmp = 0;
835
836   rmp = vl_msg_api_alloc (sizeof (*rmp));
837   clib_memset (rmp, 0, sizeof (*rmp));
838   rmp->_vl_msg_id =
839     ntohs (VL_API_LISP_EID_TABLE_VNI_DETAILS + REPLY_MSG_ID_BASE);
840   rmp->context = context;
841   rmp->vni = clib_host_to_net_u32 (vni);
842   vl_api_send_msg (reg, (u8 *) rmp);
843 }
844
845 static void
846 lisp_adjacency_copy (vl_api_lisp_adjacency_t * dst, lisp_adjacency_t * adjs)
847 {
848   lisp_adjacency_t *adj;
849   vl_api_lisp_adjacency_t a;
850   u32 i, n = vec_len (adjs);
851
852   for (i = 0; i < n; i++)
853     {
854       adj = vec_elt_at_index (adjs, i);
855       clib_memset (&a, 0, sizeof (a));
856
857       lisp_gid_put_api (&a.reid, &adj->reid);
858       lisp_gid_put_api (&a.leid, &adj->leid);
859
860       dst[i] = a;
861     }
862 }
863
864 static void
865   vl_api_show_lisp_rloc_probe_state_t_handler
866   (vl_api_show_lisp_rloc_probe_state_t * mp)
867 {
868   vl_api_show_lisp_rloc_probe_state_reply_t *rmp = 0;
869   int rv = 0;
870
871   /* *INDENT-OFF* */
872   REPLY_MACRO2 (VL_API_SHOW_LISP_RLOC_PROBE_STATE_REPLY,
873   {
874     rmp->is_enabled = vnet_lisp_rloc_probe_state_get ();
875   });
876   /* *INDENT-ON* */
877 }
878
879 static void
880   vl_api_show_lisp_map_register_state_t_handler
881   (vl_api_show_lisp_map_register_state_t * mp)
882 {
883   vl_api_show_lisp_map_register_state_reply_t *rmp = 0;
884   int rv = 0;
885
886   /* *INDENT-OFF* */
887   REPLY_MACRO2 (VL_API_SHOW_LISP_MAP_REGISTER_STATE_REPLY,
888   {
889     rmp->is_enabled = vnet_lisp_map_register_state_get ();
890   });
891   /* *INDENT-ON* */
892 }
893
894 static void
895 vl_api_lisp_adjacencies_get_t_handler (vl_api_lisp_adjacencies_get_t * mp)
896 {
897   vl_api_lisp_adjacencies_get_reply_t *rmp = 0;
898   lisp_adjacency_t *adjs = 0;
899   int rv = 0;
900   u32 size = ~0;
901   u32 vni = clib_net_to_host_u32 (mp->vni);
902
903   adjs = vnet_lisp_adjacencies_get_by_vni (vni);
904   size = vec_len (adjs) * sizeof (vl_api_lisp_adjacency_t);
905
906   /* *INDENT-OFF* */
907   REPLY_MACRO4 (VL_API_LISP_ADJACENCIES_GET_REPLY, size,
908   {
909     rmp->count = clib_host_to_net_u32 (vec_len (adjs));
910     lisp_adjacency_copy (rmp->adjacencies, adjs);
911   });
912   /* *INDENT-ON* */
913
914   vec_free (adjs);
915 }
916
917 static void
918 vl_api_lisp_eid_table_vni_dump_t_handler (vl_api_lisp_eid_table_vni_dump_t *
919                                           mp)
920 {
921   hash_pair_t *p;
922   u32 *vnis = 0;
923   vl_api_registration_t *reg = 0;
924   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
925
926   reg = vl_api_client_index_to_registration (mp->client_index);
927   if (!reg)
928     return;
929
930   /* *INDENT-OFF* */
931   hash_foreach_pair (p, lcm->table_id_by_vni,
932   ({
933     hash_set (vnis, p->key, 0);
934   }));
935
936   hash_foreach_pair (p, lcm->bd_id_by_vni,
937   ({
938     hash_set (vnis, p->key, 0);
939   }));
940
941   hash_foreach_pair (p, vnis,
942   ({
943     send_eid_table_vni (p->key, reg, mp->context);
944   }));
945   /* *INDENT-ON* */
946
947   hash_free (vnis);
948 }
949
950 static void
951 vl_api_show_lisp_status_t_handler (vl_api_show_lisp_status_t * mp)
952 {
953   vl_api_show_lisp_status_reply_t *rmp = NULL;
954   int rv = 0;
955
956   /* *INDENT-OFF* */
957   REPLY_MACRO2(VL_API_SHOW_LISP_STATUS_REPLY,
958   ({
959     rmp->is_gpe_enabled = vnet_lisp_gpe_enable_disable_status ();
960     rmp->is_lisp_enabled = vnet_lisp_enable_disable_status ();
961   }));
962   /* *INDENT-ON* */
963 }
964
965 static void
966   vl_api_lisp_get_map_request_itr_rlocs_t_handler
967   (vl_api_lisp_get_map_request_itr_rlocs_t * mp)
968 {
969   vl_api_lisp_get_map_request_itr_rlocs_reply_t *rmp = NULL;
970   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
971   locator_set_t *loc_set = 0;
972   u8 *tmp_str = 0;
973   int rv = 0;
974
975   if (~0 == lcm->mreq_itr_rlocs)
976     {
977       tmp_str = format (0, " ");
978     }
979   else
980     {
981       loc_set =
982         pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
983       tmp_str = format (0, "%s", loc_set->name);
984     }
985
986   /* *INDENT-OFF* */
987   REPLY_MACRO2(VL_API_LISP_GET_MAP_REQUEST_ITR_RLOCS_REPLY,
988   ({
989     strncpy((char *) rmp->locator_set_name, (char *) tmp_str,
990             ARRAY_LEN(rmp->locator_set_name) - 1);
991   }));
992   /* *INDENT-ON* */
993
994   vec_free (tmp_str);
995 }
996
997 static void
998 vl_api_show_lisp_pitr_t_handler (vl_api_show_lisp_pitr_t * mp)
999 {
1000   vl_api_show_lisp_pitr_reply_t *rmp = NULL;
1001   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1002   mapping_t *m;
1003   locator_set_t *ls = 0;
1004   u8 *tmp_str = 0;
1005   int rv = 0;
1006
1007   u8 is_enabled = (lcm->flags & LISP_FLAG_PITR_MODE)
1008     && lcm->pitr_map_index != ~0;
1009
1010   if (!is_enabled)
1011     {
1012       tmp_str = format (0, "N/A");
1013     }
1014   else
1015     {
1016       m = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
1017       if (~0 != m->locator_set_index)
1018         {
1019           ls =
1020             pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1021           tmp_str = format (0, "%s", ls->name);
1022         }
1023       else
1024         {
1025           tmp_str = format (0, "N/A");
1026         }
1027     }
1028   vec_add1 (tmp_str, 0);
1029
1030   /* *INDENT-OFF* */
1031   REPLY_MACRO2(VL_API_SHOW_LISP_PITR_REPLY,
1032   ({
1033     rmp->is_enabled = lcm->flags & LISP_FLAG_PITR_MODE;
1034     strncpy((char *) rmp->locator_set_name, (char *) tmp_str,
1035             ARRAY_LEN(rmp->locator_set_name) - 1);
1036   }));
1037   /* *INDENT-ON* */
1038 }
1039
1040 /*
1041  * lisp_api_hookup
1042  * Add vpe's API message handlers to the table.
1043  * vlib has already mapped shared memory and
1044  * added the client registration handlers.
1045  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1046  */
1047 #include <lisp/lisp-cp/lisp.api.c>
1048
1049 static clib_error_t *
1050 lisp_api_hookup (vlib_main_t * vm)
1051 {
1052   /*
1053    * Set up the (msg_name, crc, message-id) table
1054    */
1055   lisp_base_msg_id = setup_message_id_table ();
1056
1057   return NULL;
1058 }
1059
1060 VLIB_API_INIT_FUNCTION (lisp_api_hookup);
1061
1062 /*
1063  * fd.io coding-style-patch-verification: ON
1064  *
1065  * Local Variables:
1066  * eval: (c-set-style "gnu")
1067  * End:
1068  */