2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 #include <vlibapi/api.h>
18 #include <vlibmemory/api.h>
19 #include <vppinfra/error.h>
21 #include <vnet/ip/ip_format_fns.h>
22 #include <vnet/ethernet/ethernet_format_fns.h>
23 #include <vnet/ethernet/mac_address.h>
24 #include <lisp/lisp-cp/lisp_types.h>
26 /* define message IDs */
27 #include <lisp/lisp-gpe/lisp_gpe.api_enum.h>
28 #include <lisp/lisp-gpe/lisp_gpe.api_types.h>
29 #include <vlibmemory/vlib.api_types.h>
33 /* API message ID base */
37 } lisp_gpe_test_main_t;
39 lisp_gpe_test_main_t lisp_gpe_test_main;
41 #define __plugin_msg_base lisp_gpe_test_main.msg_id_base
42 #include <vlibapi/vat_helper_macros.h>
44 /* Macro to finish up custom dump fns */
45 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
48 vl_print (handle, (char *)s); \
52 #define LISP_PING(_lm, mp_ping) \
53 if (!(_lm)->ping_id) \
54 (_lm)->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC)); \
55 mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping)); \
56 mp_ping->_vl_msg_id = htons ((_lm)->ping_id); \
57 mp_ping->client_index = vam->my_client_index; \
58 fformat (vam->ofp, "Sending ping id=%d\n", (_lm)->ping_id); \
59 vam->result_ready = 0; \
65 } __attribute__ ((__packed__)) lisp_nsh_api_t;
68 unformat_nsh_address (unformat_input_t * input, va_list * args)
70 lisp_nsh_api_t *nsh = va_arg (*args, lisp_nsh_api_t *);
71 return unformat (input, "SPI:%d SI:%d", &nsh->spi, &nsh->si);
75 format_nsh_address_vat (u8 * s, va_list * args)
77 nsh_t *a = va_arg (*args, nsh_t *);
78 return format (s, "SPI:%d SI:%d", clib_net_to_host_u32 (a->spi), a->si);
82 format_lisp_flat_eid (u8 * s, va_list * args)
84 vl_api_eid_t *eid = va_arg (*args, vl_api_eid_t *);
88 case EID_TYPE_API_PREFIX:
89 if (eid->address.prefix.address.af)
90 return format (s, "%U/%d", format_ip6_address,
91 eid->address.prefix.address.un.ip6,
92 eid->address.prefix.len);
93 return format (s, "%U/%d", format_ip4_address,
94 eid->address.prefix.address.un.ip4,
95 eid->address.prefix.len);
96 case EID_TYPE_API_MAC:
97 return format (s, "%U", format_ethernet_address, eid->address.mac);
98 case EID_TYPE_API_NSH:
99 return format (s, "%U", format_nsh_address_vat, eid->address.nsh);
104 static void vl_api_gpe_add_del_fwd_entry_reply_t_handler
105 (vl_api_gpe_add_del_fwd_entry_reply_t * mp)
107 vat_main_t *vam = &vat_main;
108 i32 retval = ntohl (mp->retval);
111 vam->async_errors += (retval < 0);
115 vam->retval = retval;
116 vam->result_ready = 1;
121 api_gpe_fwd_entry_net_to_host (vl_api_gpe_fwd_entry_t * e)
123 e->dp_table = clib_net_to_host_u32 (e->dp_table);
124 e->fwd_entry_index = clib_net_to_host_u32 (e->fwd_entry_index);
125 e->vni = clib_net_to_host_u32 (e->vni);
129 gpe_fwd_entries_get_reply_t_net_to_host
130 (vl_api_gpe_fwd_entries_get_reply_t * mp)
134 mp->count = clib_net_to_host_u32 (mp->count);
135 for (i = 0; i < mp->count; i++)
137 api_gpe_fwd_entry_net_to_host (&mp->entries[i]);
142 format_gpe_encap_mode (u8 * s, va_list * args)
144 u32 mode = va_arg (*args, u32);
149 return format (s, "lisp");
151 return format (s, "vxlan");
157 vl_api_gpe_get_encap_mode_reply_t_handler
158 (vl_api_gpe_get_encap_mode_reply_t * mp)
160 vat_main_t *vam = &vat_main;
162 print (vam->ofp, "gpe mode: %U", format_gpe_encap_mode, mp->encap_mode);
163 vam->retval = ntohl (mp->retval);
164 vam->result_ready = 1;
168 vl_api_gpe_fwd_entry_path_details_t_handler
169 (vl_api_gpe_fwd_entry_path_details_t * mp)
171 vat_main_t *vam = &vat_main;
172 u8 *(*format_ip_address_fcn) (u8 *, va_list *) = 0;
174 if (mp->lcl_loc.addr.af)
175 format_ip_address_fcn = format_ip6_address;
177 format_ip_address_fcn = format_ip4_address;
179 print (vam->ofp, "w:%d %30U %30U", mp->rmt_loc.weight,
180 format_ip_address_fcn, &mp->lcl_loc.addr.un,
181 format_ip_address_fcn, &mp->rmt_loc.addr.un);
185 vl_api_gpe_fwd_entries_get_reply_t_handler
186 (vl_api_gpe_fwd_entries_get_reply_t * mp)
188 vat_main_t *vam = &vat_main;
190 int retval = clib_net_to_host_u32 (mp->retval);
191 vl_api_gpe_fwd_entry_t *e;
196 gpe_fwd_entries_get_reply_t_net_to_host (mp);
198 for (i = 0; i < mp->count; i++)
201 print (vam->ofp, "%10d %10d %U %40U", e->fwd_entry_index, e->dp_table,
202 format_lisp_flat_eid, e->leid, format_lisp_flat_eid, e->reid);
206 vam->retval = retval;
207 vam->result_ready = 1;
211 vl_api_gpe_native_fwd_rpaths_get_reply_t_handler
212 (vl_api_gpe_native_fwd_rpaths_get_reply_t * mp)
214 vat_main_t *vam = &vat_main;
216 int retval = clib_net_to_host_u32 (mp->retval);
217 vl_api_gpe_native_fwd_rpath_t *r;
222 n = clib_net_to_host_u32 (mp->count);
224 for (i = 0; i < n; i++)
227 print (vam->ofp, "fib_index: %d sw_if_index %d nh %U",
228 clib_net_to_host_u32 (r->fib_index),
229 clib_net_to_host_u32 (r->nh_sw_if_index),
230 r->nh_addr.af ? format_ip6_address : format_ip4_address,
235 vam->retval = retval;
236 vam->result_ready = 1;
240 vl_api_gpe_fwd_entry_vnis_get_reply_t_handler
241 (vl_api_gpe_fwd_entry_vnis_get_reply_t * mp)
243 vat_main_t *vam = &vat_main;
245 int retval = clib_net_to_host_u32 (mp->retval);
250 n = clib_net_to_host_u32 (mp->count);
252 for (i = 0; i < n; i++)
253 print (vam->ofp, "%d", clib_net_to_host_u32 (mp->vnis[i]));
256 vam->retval = retval;
257 vam->result_ready = 1;
262 /** Used for parsing LISP eids */
263 typedef CLIB_PACKED(struct{
269 u32 len; /**< prefix length if IP */
270 u8 type; /**< type of eid */
275 unformat_lisp_eid_vat (unformat_input_t * input, va_list * args)
277 lisp_eid_vat_t *a = va_arg (*args, lisp_eid_vat_t *);
279 clib_memset (a, 0, sizeof (a[0]));
281 if (unformat (input, "%U/%d", unformat_ip46_address, a->addr.ip, &a->len))
283 a->type = 0; /* ip prefix type */
285 else if (unformat (input, "%U", unformat_ethernet_address, &a->addr.mac))
287 a->type = 1; /* mac type */
289 else if (unformat (input, "%U", unformat_nsh_address, a->addr.nsh))
291 a->type = 2; /* NSH type */
292 a->addr.nsh.spi = clib_host_to_net_u32 (a->addr.nsh.spi);
301 if (ip46_address_is_ip4 (&a->addr.ip))
302 return a->len > 32 ? 1 : 0;
304 return a->len > 128 ? 1 : 0;
311 lisp_eid_put_vat (vl_api_eid_t * eid, const lisp_eid_vat_t * vat_eid)
313 eid->type = vat_eid->type;
316 case EID_TYPE_API_PREFIX:
317 if (ip46_address_is_ip4 (&vat_eid->addr.ip))
319 clib_memcpy (&eid->address.prefix.address.un.ip4,
320 &vat_eid->addr.ip.ip4, 4);
321 eid->address.prefix.address.af = ADDRESS_IP4;
322 eid->address.prefix.len = vat_eid->len;
326 clib_memcpy (&eid->address.prefix.address.un.ip6,
327 &vat_eid->addr.ip.ip6, 16);
328 eid->address.prefix.address.af = ADDRESS_IP6;
329 eid->address.prefix.len = vat_eid->len;
332 case EID_TYPE_API_MAC:
333 clib_memcpy (&eid->address.mac, &vat_eid->addr.mac,
334 sizeof (eid->address.mac));
336 case EID_TYPE_API_NSH:
337 clib_memcpy (&eid->address.nsh, &vat_eid->addr.nsh,
338 sizeof (eid->address.nsh));
347 api_gpe_add_del_fwd_entry (vat_main_t * vam)
349 u32 dp_table = 0, vni = 0;;
350 unformat_input_t *input = vam->input;
351 vl_api_gpe_add_del_fwd_entry_t *mp;
353 lisp_eid_vat_t _rmt_eid, *rmt_eid = &_rmt_eid;
354 lisp_eid_vat_t _lcl_eid, *lcl_eid = &_lcl_eid;
355 u8 rmt_eid_set = 0, lcl_eid_set = 0;
357 ip4_address_t rmt_rloc4, lcl_rloc4;
358 ip6_address_t rmt_rloc6, lcl_rloc6;
359 vl_api_gpe_locator_t *rmt_locs = 0, *lcl_locs = 0, rloc, *curr_rloc = 0;
362 clib_memset (&rloc, 0, sizeof (rloc));
364 /* Parse args required to build the message */
365 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
367 if (unformat (input, "del"))
369 else if (unformat (input, "add"))
371 else if (unformat (input, "reid %U", unformat_lisp_eid_vat, rmt_eid))
375 else if (unformat (input, "leid %U", unformat_lisp_eid_vat, lcl_eid))
379 else if (unformat (input, "vrf %d", &dp_table))
381 else if (unformat (input, "bd %d", &dp_table))
383 else if (unformat (input, "vni %d", &vni))
385 else if (unformat (input, "w %d", &w))
389 errmsg ("No RLOC configured for setting priority/weight!");
392 curr_rloc->weight = w;
394 else if (unformat (input, "loc-pair %U %U", unformat_ip4_address,
395 &lcl_rloc4, unformat_ip4_address, &rmt_rloc4))
398 clib_memcpy (&rloc.addr.un.ip4, &lcl_rloc4, sizeof (lcl_rloc4));
400 vec_add1 (lcl_locs, rloc);
402 clib_memcpy (&rloc.addr.un.ip4, &rmt_rloc4, sizeof (rmt_rloc4));
403 vec_add1 (rmt_locs, rloc);
404 /* weight saved in rmt loc */
405 curr_rloc = &rmt_locs[vec_len (rmt_locs) - 1];
407 else if (unformat (input, "loc-pair %U %U", unformat_ip6_address,
408 &lcl_rloc6, unformat_ip6_address, &rmt_rloc6))
411 clib_memcpy (&rloc.addr.un.ip6, &lcl_rloc6, sizeof (lcl_rloc6));
413 vec_add1 (lcl_locs, rloc);
415 clib_memcpy (&rloc.addr.un.ip6, &rmt_rloc6, sizeof (rmt_rloc6));
416 vec_add1 (rmt_locs, rloc);
417 /* weight saved in rmt loc */
418 curr_rloc = &rmt_locs[vec_len (rmt_locs) - 1];
420 else if (unformat (input, "action %d", &action))
426 clib_warning ("parse error '%U'", format_unformat_error, input);
433 errmsg ("remote eid addresses not set");
437 if (lcl_eid_set && rmt_eid->type != lcl_eid->type)
439 errmsg ("eid types don't match");
443 if (0 == rmt_locs && (u32) ~ 0 == action)
445 errmsg ("action not set for negative mapping");
449 /* Construct the API message */
450 M2 (GPE_ADD_DEL_FWD_ENTRY, mp,
451 sizeof (vl_api_gpe_locator_t) * vec_len (rmt_locs) * 2);
454 lisp_eid_put_vat (&mp->rmt_eid, rmt_eid);
455 lisp_eid_put_vat (&mp->lcl_eid, lcl_eid);
456 mp->dp_table = clib_host_to_net_u32 (dp_table);
457 mp->vni = clib_host_to_net_u32 (vni);
460 if (0 != rmt_locs && 0 != lcl_locs)
462 mp->loc_num = clib_host_to_net_u32 (vec_len (rmt_locs) * 2);
463 clib_memcpy (mp->locs, lcl_locs,
464 (sizeof (vl_api_gpe_locator_t) * vec_len (lcl_locs)));
466 u32 offset = sizeof (vl_api_gpe_locator_t) * vec_len (lcl_locs);
467 clib_memcpy (((u8 *) mp->locs) + offset, rmt_locs,
468 (sizeof (vl_api_gpe_locator_t) * vec_len (rmt_locs)));
476 /* Wait for a reply... */
482 api_gpe_enable_disable (vat_main_t * vam)
484 unformat_input_t *input = vam->input;
485 vl_api_gpe_enable_disable_t *mp;
490 /* Parse args required to build the message */
491 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
493 if (unformat (input, "enable"))
498 else if (unformat (input, "disable"))
509 errmsg ("Value not set");
513 /* Construct the API message */
514 M (GPE_ENABLE_DISABLE, mp);
516 mp->is_enable = is_enable;
521 /* Wait for a reply... */
527 unformat_gpe_encap_mode (unformat_input_t * input, va_list * args)
529 u32 *mode = va_arg (*args, u32 *);
531 if (unformat (input, "lisp"))
533 else if (unformat (input, "vxlan"))
542 api_gpe_get_encap_mode (vat_main_t * vam)
544 vl_api_gpe_get_encap_mode_t *mp;
547 /* Construct the API message */
548 M (GPE_GET_ENCAP_MODE, mp);
553 /* Wait for a reply... */
559 api_gpe_set_encap_mode (vat_main_t * vam)
561 unformat_input_t *input = vam->input;
562 vl_api_gpe_set_encap_mode_t *mp;
566 /* Parse args required to build the message */
567 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
569 if (unformat (input, "%U", unformat_gpe_encap_mode, &mode))
575 /* Construct the API message */
576 M (GPE_SET_ENCAP_MODE, mp);
583 /* Wait for a reply... */
589 api_gpe_add_del_iface (vat_main_t * vam)
591 unformat_input_t *input = vam->input;
592 vl_api_gpe_add_del_iface_t *mp;
593 u8 action_set = 0, is_add = 1, is_l2 = 0, dp_table_set = 0, vni_set = 0;
594 u32 dp_table = 0, vni = 0;
597 /* Parse args required to build the message */
598 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
600 if (unformat (input, "up"))
605 else if (unformat (input, "down"))
610 else if (unformat (input, "table_id %d", &dp_table))
614 else if (unformat (input, "bd_id %d", &dp_table))
619 else if (unformat (input, "vni %d", &vni))
629 errmsg ("Action not set");
632 if (dp_table_set == 0 || vni_set == 0)
634 errmsg ("vni and dp_table must be set");
638 /* Construct the API message */
639 M (GPE_ADD_DEL_IFACE, mp);
642 mp->dp_table = clib_host_to_net_u32 (dp_table);
644 mp->vni = clib_host_to_net_u32 (vni);
649 /* Wait for a reply... */
655 api_gpe_fwd_entries_get (vat_main_t * vam)
657 unformat_input_t *i = vam->input;
658 vl_api_gpe_fwd_entries_get_t *mp;
663 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
665 if (unformat (i, "vni %d", &vni))
671 errmsg ("parse error '%U'", format_unformat_error, i);
678 errmsg ("vni not set!");
682 if (!vam->json_output)
684 print (vam->ofp, "%10s %10s %s %40s", "fwd_index", "dp_table",
688 M (GPE_FWD_ENTRIES_GET, mp);
689 mp->vni = clib_host_to_net_u32 (vni);
694 /* Wait for a reply... */
700 api_gpe_native_fwd_rpaths_get (vat_main_t * vam)
702 unformat_input_t *i = vam->input;
703 vl_api_gpe_native_fwd_rpaths_get_t *mp;
705 u8 ip_family_set = 0, is_ip4 = 1;
707 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
709 if (unformat (i, "ip4"))
714 else if (unformat (i, "ip6"))
721 errmsg ("parse error '%U'", format_unformat_error, i);
728 errmsg ("ip family not set!");
732 M (GPE_NATIVE_FWD_RPATHS_GET, mp);
738 /* Wait for a reply... */
744 api_gpe_fwd_entry_vnis_get (vat_main_t * vam)
746 vl_api_gpe_fwd_entry_vnis_get_t *mp;
749 if (!vam->json_output)
751 print (vam->ofp, "VNIs");
754 M (GPE_FWD_ENTRY_VNIS_GET, mp);
759 /* Wait for a reply... */
765 api_gpe_add_del_native_fwd_rpath (vat_main_t * vam)
767 unformat_input_t *i = vam->input;
768 vl_api_gpe_add_del_native_fwd_rpath_t *mp;
770 u8 is_add = 1, ip_set = 0, is_ip4 = 1;
773 u32 table_id = 0, nh_sw_if_index = ~0;
775 clib_memset (&ip4, 0, sizeof (ip4));
776 clib_memset (&ip6, 0, sizeof (ip6));
778 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
780 if (unformat (i, "del"))
782 else if (unformat (i, "via %U %U", unformat_ip4_address, &ip4,
783 unformat_sw_if_index, vam, &nh_sw_if_index))
788 else if (unformat (i, "via %U %U", unformat_ip6_address, &ip6,
789 unformat_sw_if_index, vam, &nh_sw_if_index))
794 else if (unformat (i, "via %U", unformat_ip4_address, &ip4))
800 else if (unformat (i, "via %U", unformat_ip6_address, &ip6))
806 else if (unformat (i, "table %d", &table_id))
810 errmsg ("parse error '%U'", format_unformat_error, i);
817 errmsg ("nh addr not set!");
821 M (GPE_ADD_DEL_NATIVE_FWD_RPATH, mp);
823 mp->table_id = clib_host_to_net_u32 (table_id);
824 mp->nh_sw_if_index = clib_host_to_net_u32 (nh_sw_if_index);
825 mp->nh_addr.af = is_ip4 ? 0 : 1;
827 clib_memcpy (mp->nh_addr.un.ip4, &ip4, sizeof (ip4));
829 clib_memcpy (mp->nh_addr.un.ip6, &ip6, sizeof (ip6));
834 /* Wait for a reply... */
840 api_gpe_fwd_entry_path_dump (vat_main_t * vam)
842 vl_api_gpe_fwd_entry_path_dump_t *mp;
843 vl_api_control_ping_t *mp_ping;
844 unformat_input_t *i = vam->input;
845 u32 fwd_entry_index = ~0;
848 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
850 if (unformat (i, "index %d", &fwd_entry_index))
856 if (~0 == fwd_entry_index)
858 errmsg ("no index specified!");
862 if (!vam->json_output)
864 print (vam->ofp, "first line");
867 M (GPE_FWD_ENTRY_PATH_DUMP, mp);
871 /* Use a control ping for synchronization */
872 LISP_PING (&lisp_gpe_test_main, mp_ping);
875 /* Wait for a reply... */
880 #define vat_plugin_register vat_plugin_register_gpe
881 #include <lisp/lisp-gpe/lisp_gpe.api_test.c>
884 * fd.io coding-style-patch-verification: ON
887 * eval: (c-set-style "gnu")