blob: 6e07a5c700042b9e38b80123d936e59e4829ac6d [file] [log] [blame]
Pavel Kotucekcfb7be82016-12-21 14:15:37 +01001/*
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 <vnet/lisp-cp/control.h>
26#include <vnet/lisp-gpe/lisp_gpe.h>
27
28#include <vnet/vnet_msg_enum.h>
29
30#define vl_typedefs /* define message structures */
31#include <vnet/vnet_all_api_h.h>
32#undef vl_typedefs
33
34#define vl_endianfun /* define message structures */
35#include <vnet/vnet_all_api_h.h>
36#undef vl_endianfun
37
38/* instantiate all the print functions we know about */
39#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
40#define vl_printfun
41#include <vnet/vnet_all_api_h.h>
42#undef vl_printfun
43
44#include <vlibapi/api_helper_macros.h>
45
46#define foreach_vpe_api_msg \
47_(LISP_ADD_DEL_LOCATOR_SET, lisp_add_del_locator_set) \
48_(LISP_ADD_DEL_LOCATOR, lisp_add_del_locator) \
49_(LISP_ADD_DEL_LOCAL_EID, lisp_add_del_local_eid) \
50_(LISP_ADD_DEL_MAP_RESOLVER, lisp_add_del_map_resolver) \
51_(LISP_ADD_DEL_MAP_SERVER, lisp_add_del_map_server) \
52_(LISP_ENABLE_DISABLE, lisp_enable_disable) \
53_(LISP_RLOC_PROBE_ENABLE_DISABLE, lisp_rloc_probe_enable_disable) \
54_(LISP_MAP_REGISTER_ENABLE_DISABLE, lisp_map_register_enable_disable) \
55_(LISP_ADD_DEL_REMOTE_MAPPING, lisp_add_del_remote_mapping) \
56_(LISP_ADD_DEL_ADJACENCY, lisp_add_del_adjacency) \
57_(LISP_PITR_SET_LOCATOR_SET, lisp_pitr_set_locator_set) \
58_(LISP_MAP_REQUEST_MODE, lisp_map_request_mode) \
59_(LISP_EID_TABLE_ADD_DEL_MAP, lisp_eid_table_add_del_map) \
60_(LISP_LOCATOR_SET_DUMP, lisp_locator_set_dump) \
61_(LISP_LOCATOR_DUMP, lisp_locator_dump) \
62_(LISP_EID_TABLE_DUMP, lisp_eid_table_dump) \
63_(LISP_MAP_RESOLVER_DUMP, lisp_map_resolver_dump) \
64_(LISP_MAP_SERVER_DUMP, lisp_map_server_dump) \
65_(LISP_EID_TABLE_MAP_DUMP, lisp_eid_table_map_dump) \
66_(LISP_EID_TABLE_VNI_DUMP, lisp_eid_table_vni_dump) \
67_(LISP_ADJACENCIES_GET, lisp_adjacencies_get) \
68_(SHOW_LISP_RLOC_PROBE_STATE, show_lisp_rloc_probe_state) \
69_(SHOW_LISP_MAP_REGISTER_STATE, show_lisp_map_register_state) \
70_(SHOW_LISP_STATUS, show_lisp_status) \
71_(LISP_ADD_DEL_MAP_REQUEST_ITR_RLOCS, \
72 lisp_add_del_map_request_itr_rlocs) \
73_(LISP_GET_MAP_REQUEST_ITR_RLOCS, lisp_get_map_request_itr_rlocs) \
74_(SHOW_LISP_PITR, show_lisp_pitr) \
75_(SHOW_LISP_MAP_REQUEST_MODE, show_lisp_map_request_mode) \
Florin Corasba888e42017-01-24 11:38:18 -080076_(LISP_USE_PETR, lisp_use_petr) \
77_(SHOW_LISP_USE_PETR, show_lisp_use_petr) \
Pavel Kotucekcfb7be82016-12-21 14:15:37 +010078
79/** Used for transferring locators via VPP API */
80/* *INDENT-OFF* */
81typedef CLIB_PACKED (struct {
82 u8 is_ip4; /**< is locator an IPv4 address */
83 u8 priority; /**< locator priority */
84 u8 weight; /**< locator weight */
85 u8 addr[16]; /**< IPv4/IPv6 address */
86}) rloc_t;
87/* *INDENT-ON* */
88
89/** Used for transferring locators via VPP API */
90/* *INDENT-OFF* */
91typedef CLIB_PACKED (struct {
92 u32 sw_if_index; /**< locator sw_if_index */
93 u8 priority; /**< locator priority */
94 u8 weight; /**< locator weight */
95}) ls_locator_t;
96/* *INDENT-ON* */
97
98static locator_t *
99unformat_lisp_locs (void *rmt_locs, u32 rloc_num)
100{
101 u32 i;
102 locator_t *locs = 0, loc;
103 rloc_t *r;
104
105 for (i = 0; i < rloc_num; i++)
106 {
107 /* remote locators */
108 r = &((rloc_t *) rmt_locs)[i];
109 memset (&loc, 0, sizeof (loc));
110 gid_address_ip_set (&loc.address, &r->addr, r->is_ip4 ? IP4 : IP6);
111
112 loc.priority = r->priority;
113 loc.weight = r->weight;
114
115 vec_add1 (locs, loc);
116 }
117 return locs;
118}
119
120static void
121vl_api_lisp_add_del_locator_set_t_handler (vl_api_lisp_add_del_locator_set_t *
122 mp)
123{
124 vl_api_lisp_add_del_locator_set_reply_t *rmp;
125 int rv = 0;
126 vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
127 locator_t locator;
128 ls_locator_t *ls_loc;
129 u32 ls_index = ~0, locator_num;
130 u8 *locator_name = NULL;
131 int i;
132
133 memset (a, 0, sizeof (a[0]));
134
135 locator_name = format (0, "%s", mp->locator_set_name);
136
137 a->name = locator_name;
138 a->is_add = mp->is_add;
139 a->local = 1;
140 locator_num = clib_net_to_host_u32 (mp->locator_num);
141
142 memset (&locator, 0, sizeof (locator));
143 for (i = 0; i < locator_num; i++)
144 {
145 ls_loc = &((ls_locator_t *) mp->locators)[i];
146 VALIDATE_SW_IF_INDEX (ls_loc);
147
148 locator.sw_if_index = htonl (ls_loc->sw_if_index);
149 locator.priority = ls_loc->priority;
150 locator.weight = ls_loc->weight;
151 locator.local = 1;
152 vec_add1 (a->locators, locator);
153 }
154
155 rv = vnet_lisp_add_del_locator_set (a, &ls_index);
156
157 BAD_SW_IF_INDEX_LABEL;
158
159 vec_free (locator_name);
160 vec_free (a->locators);
161
162 /* *INDENT-OFF* */
163 REPLY_MACRO2 (VL_API_LISP_ADD_DEL_LOCATOR_SET_REPLY,
164 ({
165 rmp->ls_index = clib_host_to_net_u32 (ls_index);
166 }));
167 /* *INDENT-ON* */
168}
169
170static void
171vl_api_lisp_add_del_locator_t_handler (vl_api_lisp_add_del_locator_t * mp)
172{
173 vl_api_lisp_add_del_locator_reply_t *rmp;
174 int rv = 0;
175 locator_t locator, *locators = NULL;
176 vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
177 u32 ls_index = ~0;
178 u8 *locator_name = NULL;
179
180 memset (&locator, 0, sizeof (locator));
181 memset (a, 0, sizeof (a[0]));
182
183 locator.sw_if_index = ntohl (mp->sw_if_index);
184 locator.priority = mp->priority;
185 locator.weight = mp->weight;
186 locator.local = 1;
187 vec_add1 (locators, locator);
188
189 locator_name = format (0, "%s", mp->locator_set_name);
190
191 a->name = locator_name;
192 a->locators = locators;
193 a->is_add = mp->is_add;
194 a->local = 1;
195
196 rv = vnet_lisp_add_del_locator (a, NULL, &ls_index);
197
198 vec_free (locators);
199 vec_free (locator_name);
200
201 REPLY_MACRO (VL_API_LISP_ADD_DEL_LOCATOR_REPLY);
202}
203
204static int
205unformat_lisp_eid_api (gid_address_t * dst, u32 vni, u8 type, void *src,
206 u8 len)
207{
208 switch (type)
209 {
210 case 0: /* ipv4 */
211 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
212 gid_address_ip_set (dst, src, IP4);
213 gid_address_ippref_len (dst) = len;
214 ip_prefix_normalize (&gid_address_ippref (dst));
215 break;
216 case 1: /* ipv6 */
217 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
218 gid_address_ip_set (dst, src, IP6);
219 gid_address_ippref_len (dst) = len;
220 ip_prefix_normalize (&gid_address_ippref (dst));
221 break;
222 case 2: /* l2 mac */
223 gid_address_type (dst) = GID_ADDR_MAC;
224 clib_memcpy (&gid_address_mac (dst), src, 6);
225 break;
226 default:
227 /* unknown type */
228 return VNET_API_ERROR_INVALID_VALUE;
229 }
230
231 gid_address_vni (dst) = vni;
232
233 return 0;
234}
235
236static void
237vl_api_lisp_add_del_local_eid_t_handler (vl_api_lisp_add_del_local_eid_t * mp)
238{
239 vl_api_lisp_add_del_local_eid_reply_t *rmp;
240 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
241 int rv = 0;
242 gid_address_t _eid, *eid = &_eid;
243 uword *p = NULL;
244 u32 locator_set_index = ~0, map_index = ~0;
245 vnet_lisp_add_del_mapping_args_t _a, *a = &_a;
246 u8 *name = NULL, *key = NULL;
247 memset (a, 0, sizeof (a[0]));
248 memset (eid, 0, sizeof (eid[0]));
249
250 rv = unformat_lisp_eid_api (eid, clib_net_to_host_u32 (mp->vni),
251 mp->eid_type, mp->eid, mp->prefix_len);
252 if (rv)
253 goto out;
254
255 name = format (0, "%s", mp->locator_set_name);
256 p = hash_get_mem (lcm->locator_set_index_by_name, name);
257 if (!p)
258 {
259 rv = VNET_API_ERROR_INVALID_VALUE;
260 goto out;
261 }
262 locator_set_index = p[0];
263
264 if (*mp->key)
265 key = format (0, "%s", mp->key);
266
267 /* XXX treat batch configuration */
268 a->is_add = mp->is_add;
269 gid_address_copy (&a->eid, eid);
270 a->locator_set_index = locator_set_index;
271 a->local = 1;
272 a->key = key;
273 a->key_id = clib_net_to_host_u16 (mp->key_id);
274
275 rv = vnet_lisp_add_del_local_mapping (a, &map_index);
276
277out:
278 vec_free (name);
279 vec_free (key);
280 gid_address_free (&a->eid);
281
282 REPLY_MACRO (VL_API_LISP_ADD_DEL_LOCAL_EID_REPLY);
283}
284
285static void
286 vl_api_lisp_eid_table_add_del_map_t_handler
287 (vl_api_lisp_eid_table_add_del_map_t * mp)
288{
289 vl_api_lisp_eid_table_add_del_map_reply_t *rmp;
290 int rv = 0;
291 rv = vnet_lisp_eid_table_map (clib_net_to_host_u32 (mp->vni),
292 clib_net_to_host_u32 (mp->dp_table),
293 mp->is_l2, mp->is_add);
294REPLY_MACRO (VL_API_LISP_EID_TABLE_ADD_DEL_MAP_REPLY)}
295
296static void
297vl_api_lisp_add_del_map_server_t_handler (vl_api_lisp_add_del_map_server_t
298 * mp)
299{
300 vl_api_lisp_add_del_map_server_reply_t *rmp;
301 int rv = 0;
302 ip_address_t addr;
303
304 memset (&addr, 0, sizeof (addr));
305
306 ip_address_set (&addr, mp->ip_address, mp->is_ipv6 ? IP6 : IP4);
307 rv = vnet_lisp_add_del_map_server (&addr, mp->is_add);
308
309 REPLY_MACRO (VL_API_LISP_ADD_DEL_MAP_SERVER_REPLY);
310}
311
312static void
313vl_api_lisp_add_del_map_resolver_t_handler (vl_api_lisp_add_del_map_resolver_t
314 * mp)
315{
316 vl_api_lisp_add_del_map_resolver_reply_t *rmp;
317 int rv = 0;
318 vnet_lisp_add_del_map_resolver_args_t _a, *a = &_a;
319
320 memset (a, 0, sizeof (a[0]));
321
322 a->is_add = mp->is_add;
323 ip_address_set (&a->address, mp->ip_address, mp->is_ipv6 ? IP6 : IP4);
324
325 rv = vnet_lisp_add_del_map_resolver (a);
326
327 REPLY_MACRO (VL_API_LISP_ADD_DEL_MAP_RESOLVER_REPLY);
328}
329
330static void
331 vl_api_lisp_map_register_enable_disable_t_handler
332 (vl_api_lisp_map_register_enable_disable_t * mp)
333{
334 vl_api_lisp_map_register_enable_disable_reply_t *rmp;
335 int rv = 0;
336
337 vnet_lisp_map_register_enable_disable (mp->is_enabled);
338 REPLY_MACRO (VL_API_LISP_ENABLE_DISABLE_REPLY);
339}
340
341static void
342 vl_api_lisp_rloc_probe_enable_disable_t_handler
343 (vl_api_lisp_rloc_probe_enable_disable_t * mp)
344{
345 vl_api_lisp_rloc_probe_enable_disable_reply_t *rmp;
346 int rv = 0;
347
348 vnet_lisp_rloc_probe_enable_disable (mp->is_enabled);
349 REPLY_MACRO (VL_API_LISP_ENABLE_DISABLE_REPLY);
350}
351
352static void
353vl_api_lisp_enable_disable_t_handler (vl_api_lisp_enable_disable_t * mp)
354{
355 vl_api_lisp_enable_disable_reply_t *rmp;
356 int rv = 0;
357
358 vnet_lisp_enable_disable (mp->is_en);
359 REPLY_MACRO (VL_API_LISP_ENABLE_DISABLE_REPLY);
360}
361
362static void
363 vl_api_show_lisp_map_request_mode_t_handler
364 (vl_api_show_lisp_map_request_mode_t * mp)
365{
366 int rv = 0;
367 vl_api_show_lisp_map_request_mode_reply_t *rmp;
368
369 /* *INDENT-OFF* */
370 REPLY_MACRO2(VL_API_SHOW_LISP_MAP_REQUEST_MODE_REPLY,
371 ({
372 rmp->mode = vnet_lisp_get_map_request_mode ();
373 }));
374 /* *INDENT-ON* */
375}
376
377static void
378vl_api_lisp_map_request_mode_t_handler (vl_api_lisp_map_request_mode_t * mp)
379{
380 vl_api_lisp_map_request_mode_reply_t *rmp;
381 int rv = 0;
382
383 rv = vnet_lisp_set_map_request_mode (mp->mode);
384
385 REPLY_MACRO (VL_API_LISP_MAP_REQUEST_MODE_REPLY);
386}
387
388static void
389vl_api_lisp_pitr_set_locator_set_t_handler (vl_api_lisp_pitr_set_locator_set_t
390 * mp)
391{
392 vl_api_lisp_pitr_set_locator_set_reply_t *rmp;
393 int rv = 0;
394 u8 *ls_name = 0;
395
396 ls_name = format (0, "%s", mp->ls_name);
397 rv = vnet_lisp_pitr_set_locator_set (ls_name, mp->is_add);
398 vec_free (ls_name);
399
400 REPLY_MACRO (VL_API_LISP_PITR_SET_LOCATOR_SET_REPLY);
401}
402
403static void
Florin Corasba888e42017-01-24 11:38:18 -0800404vl_api_lisp_use_petr_t_handler (vl_api_lisp_use_petr_t * mp)
405{
406 vl_api_lisp_use_petr_reply_t *rmp;
407 int rv = 0;
408 ip_address_t addr;
409
410 ip_address_set (&addr, &mp->address, mp->is_ip4 ? IP4 : IP6);
411 rv = vnet_lisp_use_petr (&addr, mp->is_add);
412
413 REPLY_MACRO (VL_API_LISP_USE_PETR_REPLY);
414}
415
416static void
417vl_api_show_lisp_use_petr_t_handler (vl_api_show_lisp_use_petr_t * mp)
418{
419 unix_shared_memory_queue_t *q = NULL;
420 vl_api_show_lisp_use_petr_reply_t *rmp = NULL;
421 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
422 mapping_t *m;
423 locator_set_t *ls = 0;
424 int rv = 0;
425 locator_t *loc;
426
427 q = vl_api_client_index_to_input_queue (mp->client_index);
428 if (q == 0)
429 {
430 return;
431 }
432
433 rmp->status = lcm->flags & LISP_FLAG_USE_PETR;
434 if (rmp->status)
435 {
436 m = pool_elt_at_index (lcm->mapping_pool, lcm->petr_map_index);
437 if (~0 != m->locator_set_index)
438 {
439 ls =
440 pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
441 loc = pool_elt_at_index (lcm->locator_pool, ls->locator_indices[0]);
442 gid_address_put (rmp->address, &loc->address);
443 rmp->is_ip4 = (gid_address_ip_version (&loc->address) == IP4);
444 }
445 }
446
447 REPLY_MACRO (VL_API_SHOW_LISP_USE_PETR_REPLY);
448}
449
450static void
Pavel Kotucekcfb7be82016-12-21 14:15:37 +0100451 vl_api_lisp_add_del_map_request_itr_rlocs_t_handler
452 (vl_api_lisp_add_del_map_request_itr_rlocs_t * mp)
453{
454 vl_api_lisp_add_del_map_request_itr_rlocs_reply_t *rmp;
455 int rv = 0;
456 u8 *locator_set_name = NULL;
457 vnet_lisp_add_del_mreq_itr_rloc_args_t _a, *a = &_a;
458
459 locator_set_name = format (0, "%s", mp->locator_set_name);
460
461 a->is_add = mp->is_add;
462 a->locator_set_name = locator_set_name;
463
464 rv = vnet_lisp_add_del_mreq_itr_rlocs (a);
465
466 vec_free (locator_set_name);
467
468 REPLY_MACRO (VL_API_LISP_ADD_DEL_MAP_REQUEST_ITR_RLOCS_REPLY);
469}
470
471static void
472 vl_api_lisp_add_del_remote_mapping_t_handler
473 (vl_api_lisp_add_del_remote_mapping_t * mp)
474{
475 locator_t *rlocs = 0;
476 vl_api_lisp_add_del_remote_mapping_reply_t *rmp;
477 int rv = 0;
478 gid_address_t _eid, *eid = &_eid;
479 u32 rloc_num = clib_net_to_host_u32 (mp->rloc_num);
480
481 memset (eid, 0, sizeof (eid[0]));
482
483 rv = unformat_lisp_eid_api (eid, clib_net_to_host_u32 (mp->vni),
484 mp->eid_type, mp->eid, mp->eid_len);
485 if (rv)
486 goto send_reply;
487
488 rlocs = unformat_lisp_locs (mp->rlocs, rloc_num);
489
490 if (!mp->is_add)
491 {
492 vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
493 gid_address_copy (&a->reid, eid);
494 a->is_add = 0;
495 rv = vnet_lisp_add_del_adjacency (a);
496 if (rv)
497 {
498 goto out;
499 }
500 }
501
502 /* NOTE: for now this works as a static remote mapping, i.e.,
503 * not authoritative and ttl infinite. */
504 rv = vnet_lisp_add_del_mapping (eid, rlocs, mp->action, 0, ~0,
505 mp->is_add, 1 /* is_static */ , 0);
506
507 if (mp->del_all)
508 vnet_lisp_clear_all_remote_adjacencies ();
509
510out:
511 vec_free (rlocs);
512send_reply:
513 REPLY_MACRO (VL_API_LISP_ADD_DEL_REMOTE_MAPPING_REPLY);
514}
515
516static void
517vl_api_lisp_add_del_adjacency_t_handler (vl_api_lisp_add_del_adjacency_t * mp)
518{
519 vl_api_lisp_add_del_adjacency_reply_t *rmp;
520 vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
521
522 int rv = 0;
523 memset (a, 0, sizeof (a[0]));
524
525 rv = unformat_lisp_eid_api (&a->leid, clib_net_to_host_u32 (mp->vni),
526 mp->eid_type, mp->leid, mp->leid_len);
527 rv |= unformat_lisp_eid_api (&a->reid, clib_net_to_host_u32 (mp->vni),
528 mp->eid_type, mp->reid, mp->reid_len);
529
530 if (rv)
531 goto send_reply;
532
533 a->is_add = mp->is_add;
534 rv = vnet_lisp_add_del_adjacency (a);
535
536send_reply:
537 REPLY_MACRO (VL_API_LISP_ADD_DEL_ADJACENCY_REPLY);
538}
539
540static void
541send_lisp_locator_details (lisp_cp_main_t * lcm,
542 locator_t * loc,
543 unix_shared_memory_queue_t * q, u32 context)
544{
545 vl_api_lisp_locator_details_t *rmp;
546
547 rmp = vl_msg_api_alloc (sizeof (*rmp));
548 memset (rmp, 0, sizeof (*rmp));
549 rmp->_vl_msg_id = ntohs (VL_API_LISP_LOCATOR_DETAILS);
550 rmp->context = context;
551
552 rmp->local = loc->local;
553 if (loc->local)
554 {
555 rmp->sw_if_index = ntohl (loc->sw_if_index);
556 }
557 else
558 {
559 rmp->is_ipv6 = gid_address_ip_version (&loc->address);
560 ip_address_copy_addr (rmp->ip_address, &gid_address_ip (&loc->address));
561 }
562 rmp->priority = loc->priority;
563 rmp->weight = loc->weight;
564
565 vl_msg_api_send_shmem (q, (u8 *) & rmp);
566}
567
568static void
569vl_api_lisp_locator_dump_t_handler (vl_api_lisp_locator_dump_t * mp)
570{
571 u8 *ls_name = 0;
572 unix_shared_memory_queue_t *q = 0;
573 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
574 locator_set_t *lsit = 0;
575 locator_t *loc = 0;
576 u32 ls_index = ~0, *locit = 0;
577 uword *p = 0;
578
579 q = vl_api_client_index_to_input_queue (mp->client_index);
580 if (q == 0)
581 {
582 return;
583 }
584
585 if (mp->is_index_set)
586 ls_index = htonl (mp->ls_index);
587 else
588 {
589 /* make sure we get a proper C-string */
590 mp->ls_name[sizeof (mp->ls_name) - 1] = 0;
591 ls_name = format (0, "%s", mp->ls_name);
592 p = hash_get_mem (lcm->locator_set_index_by_name, ls_name);
593 if (!p)
594 goto out;
595 ls_index = p[0];
596 }
597
598 if (pool_is_free_index (lcm->locator_set_pool, ls_index))
599 return;
600
601 lsit = pool_elt_at_index (lcm->locator_set_pool, ls_index);
602
603 vec_foreach (locit, lsit->locator_indices)
604 {
605 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
606 send_lisp_locator_details (lcm, loc, q, mp->context);
607 };
608out:
609 vec_free (ls_name);
610}
611
612static void
613send_lisp_locator_set_details (lisp_cp_main_t * lcm,
614 locator_set_t * lsit,
615 unix_shared_memory_queue_t * q,
616 u32 context, u32 ls_index)
617{
618 vl_api_lisp_locator_set_details_t *rmp;
619 u8 *str = 0;
620
621 rmp = vl_msg_api_alloc (sizeof (*rmp));
622 memset (rmp, 0, sizeof (*rmp));
623 rmp->_vl_msg_id = ntohs (VL_API_LISP_LOCATOR_SET_DETAILS);
624 rmp->context = context;
625
626 rmp->ls_index = htonl (ls_index);
627 if (lsit->local)
628 {
629 ASSERT (lsit->name != NULL);
630 strncpy ((char *) rmp->ls_name, (char *) lsit->name,
631 vec_len (lsit->name));
632 }
633 else
634 {
635 str = format (0, "<remote-%d>", ls_index);
636 strncpy ((char *) rmp->ls_name, (char *) str, vec_len (str));
637 vec_free (str);
638 }
639
640 vl_msg_api_send_shmem (q, (u8 *) & rmp);
641}
642
643static void
644vl_api_lisp_locator_set_dump_t_handler (vl_api_lisp_locator_set_dump_t * mp)
645{
646 unix_shared_memory_queue_t *q = NULL;
647 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
648 locator_set_t *lsit = NULL;
649 u8 filter;
650
651 q = vl_api_client_index_to_input_queue (mp->client_index);
652 if (q == 0)
653 {
654 return;
655 }
656
657 filter = mp->filter;
658 /* *INDENT-OFF* */
659 pool_foreach (lsit, lcm->locator_set_pool,
660 ({
661 if (filter && !((1 == filter && lsit->local) ||
662 (2 == filter && !lsit->local)))
663 {
664 continue;
665 }
666 send_lisp_locator_set_details (lcm, lsit, q, mp->context,
667 lsit - lcm->locator_set_pool);
668 }));
669 /* *INDENT-ON* */
670}
671
672static void
673lisp_fid_put_api (u8 * dst, fid_address_t * src, u8 * prefix_length)
674{
675 ASSERT (prefix_length);
676 ip_prefix_t *ippref = &fid_addr_ippref (src);
677
678 switch (fid_addr_type (src))
679 {
680 case FID_ADDR_IP_PREF:
681 if (ip_prefix_version (ippref) == IP4)
682 clib_memcpy (dst, &ip_prefix_v4 (ippref), 4);
683 else
684 clib_memcpy (dst, &ip_prefix_v6 (ippref), 16);
685 prefix_length[0] = ip_prefix_len (ippref);
686 break;
687
688 case FID_ADDR_MAC:
689 prefix_length[0] = 0;
690 clib_memcpy (dst, fid_addr_mac (src), 6);
691 break;
692
693 default:
694 clib_warning ("Unknown FID type %d!", fid_addr_type (src));
695 break;
696 }
697}
698
699static u8
700fid_type_to_api_type (fid_address_t * fid)
701{
702 ip_prefix_t *ippref;
703
704 switch (fid_addr_type (fid))
705 {
706 case FID_ADDR_IP_PREF:
707 ippref = &fid_addr_ippref (fid);
708 if (ip_prefix_version (ippref) == IP4)
709 return 0;
710 else if (ip_prefix_version (ippref) == IP6)
711 return 1;
712 else
713 return ~0;
714
715 case FID_ADDR_MAC:
716 return 2;
717 }
718
719 return ~0;
720}
721
722static void
723send_lisp_eid_table_details (mapping_t * mapit,
724 unix_shared_memory_queue_t * q,
725 u32 context, u8 filter)
726{
727 fid_address_t *fid;
728 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
729 locator_set_t *ls = 0;
730 vl_api_lisp_eid_table_details_t *rmp = NULL;
731 gid_address_t *gid = NULL;
732 u8 *mac = 0;
733 ip_prefix_t *ip_prefix = NULL;
734
735 switch (filter)
736 {
737 case 0: /* all mappings */
738 break;
739
740 case 1: /* local only */
741 if (!mapit->local)
742 return;
743 break;
744 case 2: /* remote only */
745 if (mapit->local)
746 return;
747 break;
748 default:
749 clib_warning ("Filter error, unknown filter: %d", filter);
750 return;
751 }
752
753 gid = &mapit->eid;
754 ip_prefix = &gid_address_ippref (gid);
755 mac = gid_address_mac (gid);
756
757 rmp = vl_msg_api_alloc (sizeof (*rmp));
758 memset (rmp, 0, sizeof (*rmp));
759 rmp->_vl_msg_id = ntohs (VL_API_LISP_EID_TABLE_DETAILS);
760
761 ls = pool_elt_at_index (lcm->locator_set_pool, mapit->locator_set_index);
762 if (vec_len (ls->locator_indices) == 0)
763 rmp->locator_set_index = ~0;
764 else
765 rmp->locator_set_index = clib_host_to_net_u32 (mapit->locator_set_index);
766
767 rmp->is_local = mapit->local;
768 rmp->ttl = clib_host_to_net_u32 (mapit->ttl);
769 rmp->action = mapit->action;
770 rmp->authoritative = mapit->authoritative;
771
772 switch (gid_address_type (gid))
773 {
774 case GID_ADDR_SRC_DST:
775 rmp->is_src_dst = 1;
776 fid = &gid_address_sd_src (gid);
777 rmp->eid_type = fid_type_to_api_type (fid);
778 lisp_fid_put_api (rmp->seid, &gid_address_sd_src (gid),
779 &rmp->seid_prefix_len);
780 lisp_fid_put_api (rmp->eid, &gid_address_sd_dst (gid),
781 &rmp->eid_prefix_len);
782 break;
783 case GID_ADDR_IP_PREFIX:
784 rmp->eid_prefix_len = ip_prefix_len (ip_prefix);
785 if (ip_prefix_version (ip_prefix) == IP4)
786 {
787 rmp->eid_type = 0; /* ipv4 type */
788 clib_memcpy (rmp->eid, &ip_prefix_v4 (ip_prefix),
789 sizeof (ip_prefix_v4 (ip_prefix)));
790 }
791 else
792 {
793 rmp->eid_type = 1; /* ipv6 type */
794 clib_memcpy (rmp->eid, &ip_prefix_v6 (ip_prefix),
795 sizeof (ip_prefix_v6 (ip_prefix)));
796 }
797 break;
798 case GID_ADDR_MAC:
799 rmp->eid_type = 2; /* l2 mac type */
800 clib_memcpy (rmp->eid, mac, 6);
801 break;
802 default:
803 ASSERT (0);
804 }
805 rmp->context = context;
806 rmp->vni = clib_host_to_net_u32 (gid_address_vni (gid));
807 rmp->key_id = clib_host_to_net_u16 (mapit->key_id);
808 memcpy (rmp->key, mapit->key, vec_len (mapit->key));
809 vl_msg_api_send_shmem (q, (u8 *) & rmp);
810}
811
812static void
813vl_api_lisp_eid_table_dump_t_handler (vl_api_lisp_eid_table_dump_t * mp)
814{
815 u32 mi;
816 unix_shared_memory_queue_t *q = NULL;
817 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
818 mapping_t *mapit = NULL;
819 gid_address_t _eid, *eid = &_eid;
820
821 q = vl_api_client_index_to_input_queue (mp->client_index);
822 if (q == 0)
823 {
824 return;
825 }
826
827 if (mp->eid_set)
828 {
829 memset (eid, 0, sizeof (*eid));
830
831 unformat_lisp_eid_api (eid, clib_net_to_host_u32 (mp->vni),
832 mp->eid_type, mp->eid, mp->prefix_length);
833
834 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
835 if ((u32) ~ 0 == mi)
836 return;
837
838 mapit = pool_elt_at_index (lcm->mapping_pool, mi);
839 send_lisp_eid_table_details (mapit, q, mp->context,
840 0 /* ignore filter */ );
841 }
842 else
843 {
844 /* *INDENT-OFF* */
845 pool_foreach (mapit, lcm->mapping_pool,
846 ({
847 send_lisp_eid_table_details(mapit, q, mp->context,
848 mp->filter);
849 }));
850 /* *INDENT-ON* */
851 }
852}
853
854static void
855send_lisp_map_server_details (ip_address_t * ip,
856 unix_shared_memory_queue_t * q, u32 context)
857{
858 vl_api_lisp_map_server_details_t *rmp = NULL;
859
860 rmp = vl_msg_api_alloc (sizeof (*rmp));
861 memset (rmp, 0, sizeof (*rmp));
862 rmp->_vl_msg_id = ntohs (VL_API_LISP_MAP_SERVER_DETAILS);
863
864 switch (ip_addr_version (ip))
865 {
866 case IP4:
867 rmp->is_ipv6 = 0;
868 clib_memcpy (rmp->ip_address, &ip_addr_v4 (ip),
869 sizeof (ip_addr_v4 (ip)));
870 break;
871
872 case IP6:
873 rmp->is_ipv6 = 1;
874 clib_memcpy (rmp->ip_address, &ip_addr_v6 (ip),
875 sizeof (ip_addr_v6 (ip)));
876 break;
877
878 default:
879 ASSERT (0);
880 }
881 rmp->context = context;
882
883 vl_msg_api_send_shmem (q, (u8 *) & rmp);
884}
885
886static void
887vl_api_lisp_map_server_dump_t_handler (vl_api_lisp_map_server_dump_t * mp)
888{
889 unix_shared_memory_queue_t *q = NULL;
890 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
891 lisp_msmr_t *mr;
892
893 q = vl_api_client_index_to_input_queue (mp->client_index);
894 if (q == 0)
895 {
896 return;
897 }
898
899 vec_foreach (mr, lcm->map_servers)
900 {
901 send_lisp_map_server_details (&mr->address, q, mp->context);
902 }
903}
904
905static void
906send_lisp_map_resolver_details (ip_address_t * ip,
907 unix_shared_memory_queue_t * q, u32 context)
908{
909 vl_api_lisp_map_resolver_details_t *rmp = NULL;
910
911 rmp = vl_msg_api_alloc (sizeof (*rmp));
912 memset (rmp, 0, sizeof (*rmp));
913 rmp->_vl_msg_id = ntohs (VL_API_LISP_MAP_RESOLVER_DETAILS);
914
915 switch (ip_addr_version (ip))
916 {
917 case IP4:
918 rmp->is_ipv6 = 0;
919 clib_memcpy (rmp->ip_address, &ip_addr_v4 (ip),
920 sizeof (ip_addr_v4 (ip)));
921 break;
922
923 case IP6:
924 rmp->is_ipv6 = 1;
925 clib_memcpy (rmp->ip_address, &ip_addr_v6 (ip),
926 sizeof (ip_addr_v6 (ip)));
927 break;
928
929 default:
930 ASSERT (0);
931 }
932 rmp->context = context;
933
934 vl_msg_api_send_shmem (q, (u8 *) & rmp);
935}
936
937static void
938vl_api_lisp_map_resolver_dump_t_handler (vl_api_lisp_map_resolver_dump_t * mp)
939{
940 unix_shared_memory_queue_t *q = NULL;
941 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
942 lisp_msmr_t *mr;
943
944 q = vl_api_client_index_to_input_queue (mp->client_index);
945 if (q == 0)
946 {
947 return;
948 }
949
950 vec_foreach (mr, lcm->map_resolvers)
951 {
952 send_lisp_map_resolver_details (&mr->address, q, mp->context);
953 }
954}
955
956static void
957send_eid_table_map_pair (hash_pair_t * p,
958 unix_shared_memory_queue_t * q, u32 context)
959{
960 vl_api_lisp_eid_table_map_details_t *rmp = NULL;
961
962 rmp = vl_msg_api_alloc (sizeof (*rmp));
963 memset (rmp, 0, sizeof (*rmp));
964 rmp->_vl_msg_id = ntohs (VL_API_LISP_EID_TABLE_MAP_DETAILS);
965
966 rmp->vni = clib_host_to_net_u32 (p->key);
967 rmp->dp_table = clib_host_to_net_u32 (p->value[0]);
968 rmp->context = context;
969 vl_msg_api_send_shmem (q, (u8 *) & rmp);
970}
971
972static void
973vl_api_lisp_eid_table_map_dump_t_handler (vl_api_lisp_eid_table_map_dump_t *
974 mp)
975{
976 unix_shared_memory_queue_t *q = NULL;
977 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
978 hash_pair_t *p;
979 uword *vni_table = 0;
980
981 q = vl_api_client_index_to_input_queue (mp->client_index);
982 if (q == 0)
983 {
984 return;
985 }
986
987 if (mp->is_l2)
988 {
989 vni_table = lcm->bd_id_by_vni;
990 }
991 else
992 {
993 vni_table = lcm->table_id_by_vni;
994 }
995
996 /* *INDENT-OFF* */
997 hash_foreach_pair (p, vni_table,
998 ({
999 send_eid_table_map_pair (p, q, mp->context);
1000 }));
1001 /* *INDENT-ON* */
1002}
1003
1004static void
1005send_eid_table_vni (u32 vni, unix_shared_memory_queue_t * q, u32 context)
1006{
1007 vl_api_lisp_eid_table_vni_details_t *rmp = 0;
1008
1009 rmp = vl_msg_api_alloc (sizeof (*rmp));
1010 memset (rmp, 0, sizeof (*rmp));
1011 rmp->_vl_msg_id = ntohs (VL_API_LISP_EID_TABLE_VNI_DETAILS);
1012 rmp->context = context;
1013 rmp->vni = clib_host_to_net_u32 (vni);
1014 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1015}
1016
1017static void
1018lisp_adjacency_copy (vl_api_lisp_adjacency_t * dst, lisp_adjacency_t * adjs)
1019{
1020 lisp_adjacency_t *adj;
1021 vl_api_lisp_adjacency_t a;
1022 u32 i, n = vec_len (adjs);
1023
1024 for (i = 0; i < n; i++)
1025 {
1026 adj = vec_elt_at_index (adjs, i);
1027 memset (&a, 0, sizeof (a));
1028
1029 switch (gid_address_type (&adj->reid))
1030 {
1031 case GID_ADDR_IP_PREFIX:
1032 a.reid_prefix_len = gid_address_ippref_len (&adj->reid);
1033 a.leid_prefix_len = gid_address_ippref_len (&adj->leid);
1034 if (gid_address_ip_version (&adj->reid) == IP4)
1035 {
1036 a.eid_type = 0; /* ipv4 type */
1037 clib_memcpy (a.reid, &gid_address_ip (&adj->reid), 4);
1038 clib_memcpy (a.leid, &gid_address_ip (&adj->leid), 4);
1039 }
1040 else
1041 {
1042 a.eid_type = 1; /* ipv6 type */
1043 clib_memcpy (a.reid, &gid_address_ip (&adj->reid), 16);
1044 clib_memcpy (a.leid, &gid_address_ip (&adj->leid), 16);
1045 }
1046 break;
1047 case GID_ADDR_MAC:
1048 a.eid_type = 2; /* l2 mac type */
1049 mac_copy (a.reid, gid_address_mac (&adj->reid));
1050 mac_copy (a.leid, gid_address_mac (&adj->leid));
1051 break;
1052 default:
1053 ASSERT (0);
1054 }
1055 dst[i] = a;
1056 }
1057}
1058
1059static void
1060 vl_api_show_lisp_rloc_probe_state_t_handler
1061 (vl_api_show_lisp_rloc_probe_state_t * mp)
1062{
1063 vl_api_show_lisp_rloc_probe_state_reply_t *rmp = 0;
1064 int rv = 0;
1065
1066 /* *INDENT-OFF* */
1067 REPLY_MACRO2 (VL_API_SHOW_LISP_RLOC_PROBE_STATE_REPLY,
1068 {
1069 rmp->is_enabled = vnet_lisp_rloc_probe_state_get ();
1070 });
1071 /* *INDENT-ON* */
1072}
1073
1074static void
1075 vl_api_show_lisp_map_register_state_t_handler
1076 (vl_api_show_lisp_map_register_state_t * mp)
1077{
1078 vl_api_show_lisp_map_register_state_reply_t *rmp = 0;
1079 int rv = 0;
1080
1081 /* *INDENT-OFF* */
1082 REPLY_MACRO2 (VL_API_SHOW_LISP_MAP_REGISTER_STATE_REPLY,
1083 {
1084 rmp->is_enabled = vnet_lisp_map_register_state_get ();
1085 });
1086 /* *INDENT-ON* */
1087}
1088
1089static void
1090vl_api_lisp_adjacencies_get_t_handler (vl_api_lisp_adjacencies_get_t * mp)
1091{
1092 vl_api_lisp_adjacencies_get_reply_t *rmp = 0;
1093 lisp_adjacency_t *adjs = 0;
1094 int rv = 0;
1095 vl_api_lisp_adjacency_t a;
1096 u32 size = ~0;
1097 u32 vni = clib_net_to_host_u32 (mp->vni);
1098
1099 adjs = vnet_lisp_adjacencies_get_by_vni (vni);
1100 size = vec_len (adjs) * sizeof (a);
1101
1102 /* *INDENT-OFF* */
1103 REPLY_MACRO4 (VL_API_LISP_ADJACENCIES_GET_REPLY, size,
1104 {
1105 rmp->count = clib_host_to_net_u32 (vec_len (adjs));
1106 lisp_adjacency_copy (rmp->adjacencies, adjs);
1107 });
1108 /* *INDENT-ON* */
1109
1110 vec_free (adjs);
1111}
1112
1113static void
1114vl_api_lisp_eid_table_vni_dump_t_handler (vl_api_lisp_eid_table_vni_dump_t *
1115 mp)
1116{
1117 hash_pair_t *p;
1118 u32 *vnis = 0;
1119 unix_shared_memory_queue_t *q = 0;
1120 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1121
1122 q = vl_api_client_index_to_input_queue (mp->client_index);
1123 if (q == 0)
1124 {
1125 return;
1126 }
1127
1128 /* *INDENT-OFF* */
1129 hash_foreach_pair (p, lcm->table_id_by_vni,
1130 ({
1131 hash_set (vnis, p->key, 0);
1132 }));
1133
1134 hash_foreach_pair (p, lcm->bd_id_by_vni,
1135 ({
1136 hash_set (vnis, p->key, 0);
1137 }));
1138
1139 hash_foreach_pair (p, vnis,
1140 ({
1141 send_eid_table_vni (p->key, q, mp->context);
1142 }));
1143 /* *INDENT-ON* */
1144
1145 hash_free (vnis);
1146}
1147
1148static void
1149vl_api_show_lisp_status_t_handler (vl_api_show_lisp_status_t * mp)
1150{
1151 unix_shared_memory_queue_t *q = NULL;
1152 vl_api_show_lisp_status_reply_t *rmp = NULL;
1153 int rv = 0;
1154
1155 q = vl_api_client_index_to_input_queue (mp->client_index);
1156 if (q == 0)
1157 {
1158 return;
1159 }
1160
1161 /* *INDENT-OFF* */
1162 REPLY_MACRO2(VL_API_SHOW_LISP_STATUS_REPLY,
1163 ({
1164 rmp->gpe_status = vnet_lisp_gpe_enable_disable_status ();
1165 rmp->feature_status = vnet_lisp_enable_disable_status ();
1166 }));
1167 /* *INDENT-ON* */
1168}
1169
1170static void
1171 vl_api_lisp_get_map_request_itr_rlocs_t_handler
1172 (vl_api_lisp_get_map_request_itr_rlocs_t * mp)
1173{
1174 unix_shared_memory_queue_t *q = NULL;
1175 vl_api_lisp_get_map_request_itr_rlocs_reply_t *rmp = NULL;
1176 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1177 locator_set_t *loc_set = 0;
1178 u8 *tmp_str = 0;
1179 int rv = 0;
1180
1181 q = vl_api_client_index_to_input_queue (mp->client_index);
1182 if (q == 0)
1183 {
1184 return;
1185 }
1186
1187 if (~0 == lcm->mreq_itr_rlocs)
1188 {
1189 tmp_str = format (0, " ");
1190 }
1191 else
1192 {
1193 loc_set =
1194 pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
1195 tmp_str = format (0, "%s", loc_set->name);
1196 }
1197
1198 /* *INDENT-OFF* */
1199 REPLY_MACRO2(VL_API_LISP_GET_MAP_REQUEST_ITR_RLOCS_REPLY,
1200 ({
1201 strncpy((char *) rmp->locator_set_name, (char *) tmp_str,
1202 ARRAY_LEN(rmp->locator_set_name) - 1);
1203 }));
1204 /* *INDENT-ON* */
1205
1206 vec_free (tmp_str);
1207}
1208
1209static void
1210vl_api_show_lisp_pitr_t_handler (vl_api_show_lisp_pitr_t * mp)
1211{
1212 unix_shared_memory_queue_t *q = NULL;
1213 vl_api_show_lisp_pitr_reply_t *rmp = NULL;
1214 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1215 mapping_t *m;
1216 locator_set_t *ls = 0;
1217 u8 *tmp_str = 0;
1218 int rv = 0;
1219
1220 q = vl_api_client_index_to_input_queue (mp->client_index);
1221 if (q == 0)
1222 {
1223 return;
1224 }
1225
1226 if (!lcm->lisp_pitr)
1227 {
1228 tmp_str = format (0, "N/A");
1229 }
1230 else
1231 {
1232 m = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
1233 if (~0 != m->locator_set_index)
1234 {
1235 ls =
1236 pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1237 tmp_str = format (0, "%s", ls->name);
1238 }
1239 else
1240 {
1241 tmp_str = format (0, "N/A");
1242 }
1243 }
1244 vec_add1 (tmp_str, 0);
1245
1246 /* *INDENT-OFF* */
1247 REPLY_MACRO2(VL_API_SHOW_LISP_PITR_REPLY,
1248 ({
1249 rmp->status = lcm->lisp_pitr;
1250 strncpy((char *) rmp->locator_set_name, (char *) tmp_str,
1251 ARRAY_LEN(rmp->locator_set_name) - 1);
1252 }));
1253 /* *INDENT-ON* */
1254}
1255
1256/*
1257 * lisp_api_hookup
1258 * Add vpe's API message handlers to the table.
1259 * vlib has alread mapped shared memory and
1260 * added the client registration handlers.
1261 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1262 */
1263#define vl_msg_name_crc_list
1264#include <vnet/vnet_all_api_h.h>
1265#undef vl_msg_name_crc_list
1266
1267static void
1268setup_message_id_table (api_main_t * am)
1269{
1270#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1271 foreach_vl_msg_name_crc_lisp;
1272#undef _
1273}
1274
1275static clib_error_t *
1276lisp_api_hookup (vlib_main_t * vm)
1277{
1278 api_main_t *am = &api_main;
1279
1280#define _(N,n) \
1281 vl_msg_api_set_handlers(VL_API_##N, #n, \
1282 vl_api_##n##_t_handler, \
1283 vl_noop_handler, \
1284 vl_api_##n##_t_endian, \
1285 vl_api_##n##_t_print, \
1286 sizeof(vl_api_##n##_t), 1);
1287 foreach_vpe_api_msg;
1288#undef _
1289
1290 /*
1291 * Set up the (msg_name, crc, message-id) table
1292 */
1293 setup_message_id_table (am);
1294
1295 return 0;
1296}
1297
1298VLIB_API_INIT_FUNCTION (lisp_api_hookup);
1299
1300/*
1301 * fd.io coding-style-patch-verification: ON
1302 *
1303 * Local Variables:
1304 * eval: (c-set-style "gnu")
1305 * End:
1306 */