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>
46 vlib_cli_output (handle, (char *) s); \
50 #define LISP_PING(_lm, mp_ping) \
51 if (!(_lm)->ping_id) \
52 (_lm)->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC)); \
53 mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping)); \
54 mp_ping->_vl_msg_id = htons ((_lm)->ping_id); \
55 mp_ping->client_index = vam->my_client_index; \
56 fformat (vam->ofp, "Sending ping id=%d\n", (_lm)->ping_id); \
57 vam->result_ready = 0; \
63 } __attribute__ ((__packed__)) lisp_nsh_api_t;
66 unformat_nsh_address (unformat_input_t * input, va_list * args)
68 lisp_nsh_api_t *nsh = va_arg (*args, lisp_nsh_api_t *);
69 return unformat (input, "SPI:%d SI:%d", &nsh->spi, &nsh->si);
73 format_nsh_address_vat (u8 * s, va_list * args)
75 nsh_t *a = va_arg (*args, nsh_t *);
76 return format (s, "SPI:%d SI:%d", clib_net_to_host_u32 (a->spi), a->si);
80 format_lisp_flat_eid (u8 * s, va_list * args)
82 vl_api_eid_t *eid = va_arg (*args, vl_api_eid_t *);
86 case EID_TYPE_API_PREFIX:
87 if (eid->address.prefix.address.af)
88 return format (s, "%U/%d", format_ip6_address,
89 eid->address.prefix.address.un.ip6,
90 eid->address.prefix.len);
91 return format (s, "%U/%d", format_ip4_address,
92 eid->address.prefix.address.un.ip4,
93 eid->address.prefix.len);
94 case EID_TYPE_API_MAC:
95 return format (s, "%U", format_ethernet_address, eid->address.mac);
96 case EID_TYPE_API_NSH:
97 return format (s, "%U", format_nsh_address_vat, eid->address.nsh);
102 static void vl_api_gpe_add_del_fwd_entry_reply_t_handler
103 (vl_api_gpe_add_del_fwd_entry_reply_t * mp)
105 vat_main_t *vam = &vat_main;
106 i32 retval = ntohl (mp->retval);
109 vam->async_errors += (retval < 0);
113 vam->retval = retval;
114 vam->result_ready = 1;
119 api_gpe_fwd_entry_net_to_host (vl_api_gpe_fwd_entry_t * e)
121 e->dp_table = clib_net_to_host_u32 (e->dp_table);
122 e->fwd_entry_index = clib_net_to_host_u32 (e->fwd_entry_index);
123 e->vni = clib_net_to_host_u32 (e->vni);
127 gpe_fwd_entries_get_reply_t_net_to_host
128 (vl_api_gpe_fwd_entries_get_reply_t * mp)
132 mp->count = clib_net_to_host_u32 (mp->count);
133 for (i = 0; i < mp->count; i++)
135 api_gpe_fwd_entry_net_to_host (&mp->entries[i]);
140 format_gpe_encap_mode (u8 * s, va_list * args)
142 u32 mode = va_arg (*args, u32);
147 return format (s, "lisp");
149 return format (s, "vxlan");
155 vl_api_gpe_get_encap_mode_reply_t_handler
156 (vl_api_gpe_get_encap_mode_reply_t * mp)
158 vat_main_t *vam = &vat_main;
160 print (vam->ofp, "gpe mode: %U", format_gpe_encap_mode, mp->encap_mode);
161 vam->retval = ntohl (mp->retval);
162 vam->result_ready = 1;
166 vl_api_gpe_fwd_entry_path_details_t_handler
167 (vl_api_gpe_fwd_entry_path_details_t * mp)
169 vat_main_t *vam = &vat_main;
170 u8 *(*format_ip_address_fcn) (u8 *, va_list *) = 0;
172 if (mp->lcl_loc.addr.af)
173 format_ip_address_fcn = format_ip6_address;
175 format_ip_address_fcn = format_ip4_address;
177 print (vam->ofp, "w:%d %30U %30U", mp->rmt_loc.weight,
178 format_ip_address_fcn, &mp->lcl_loc.addr.un,
179 format_ip_address_fcn, &mp->rmt_loc.addr.un);
183 vl_api_gpe_fwd_entries_get_reply_t_handler
184 (vl_api_gpe_fwd_entries_get_reply_t * mp)
186 vat_main_t *vam = &vat_main;
188 int retval = clib_net_to_host_u32 (mp->retval);
189 vl_api_gpe_fwd_entry_t *e;
194 gpe_fwd_entries_get_reply_t_net_to_host (mp);
196 for (i = 0; i < mp->count; i++)
199 print (vam->ofp, "%10d %10d %U %40U", e->fwd_entry_index, e->dp_table,
200 format_lisp_flat_eid, e->leid, format_lisp_flat_eid, e->reid);
204 vam->retval = retval;
205 vam->result_ready = 1;
209 vl_api_gpe_native_fwd_rpaths_get_reply_t_handler
210 (vl_api_gpe_native_fwd_rpaths_get_reply_t * mp)
212 vat_main_t *vam = &vat_main;
214 int retval = clib_net_to_host_u32 (mp->retval);
215 vl_api_gpe_native_fwd_rpath_t *r;
220 n = clib_net_to_host_u32 (mp->count);
222 for (i = 0; i < n; i++)
225 print (vam->ofp, "fib_index: %d sw_if_index %d nh %U",
226 clib_net_to_host_u32 (r->fib_index),
227 clib_net_to_host_u32 (r->nh_sw_if_index),
228 r->nh_addr.af ? format_ip6_address : format_ip4_address,
233 vam->retval = retval;
234 vam->result_ready = 1;
238 vl_api_gpe_fwd_entry_vnis_get_reply_t_handler
239 (vl_api_gpe_fwd_entry_vnis_get_reply_t * mp)
241 vat_main_t *vam = &vat_main;
243 int retval = clib_net_to_host_u32 (mp->retval);
248 n = clib_net_to_host_u32 (mp->count);
250 for (i = 0; i < n; i++)
251 print (vam->ofp, "%d", clib_net_to_host_u32 (mp->vnis[i]));
254 vam->retval = retval;
255 vam->result_ready = 1;
260 /** Used for parsing LISP eids */
261 typedef CLIB_PACKED(struct{
267 u32 len; /**< prefix length if IP */
268 u8 type; /**< type of eid */
273 unformat_lisp_eid_vat (unformat_input_t * input, va_list * args)
275 lisp_eid_vat_t *a = va_arg (*args, lisp_eid_vat_t *);
277 clib_memset (a, 0, sizeof (a[0]));
279 if (unformat (input, "%U/%d", unformat_ip46_address, a->addr.ip, &a->len))
281 a->type = 0; /* ip prefix type */
283 else if (unformat (input, "%U", unformat_ethernet_address, &a->addr.mac))
285 a->type = 1; /* mac type */
287 else if (unformat (input, "%U", unformat_nsh_address, a->addr.nsh))
289 a->type = 2; /* NSH type */
290 a->addr.nsh.spi = clib_host_to_net_u32 (a->addr.nsh.spi);
299 if (ip46_address_is_ip4 (&a->addr.ip))
300 return a->len > 32 ? 1 : 0;
302 return a->len > 128 ? 1 : 0;
309 lisp_eid_put_vat (vl_api_eid_t * eid, const lisp_eid_vat_t * vat_eid)
311 eid->type = vat_eid->type;
314 case EID_TYPE_API_PREFIX:
315 if (ip46_address_is_ip4 (&vat_eid->addr.ip))
317 clib_memcpy (&eid->address.prefix.address.un.ip4,
318 &vat_eid->addr.ip.ip4, 4);
319 eid->address.prefix.address.af = ADDRESS_IP4;
320 eid->address.prefix.len = vat_eid->len;
324 clib_memcpy (&eid->address.prefix.address.un.ip6,
325 &vat_eid->addr.ip.ip6, 16);
326 eid->address.prefix.address.af = ADDRESS_IP6;
327 eid->address.prefix.len = vat_eid->len;
330 case EID_TYPE_API_MAC:
331 clib_memcpy (&eid->address.mac, &vat_eid->addr.mac,
332 sizeof (eid->address.mac));
334 case EID_TYPE_API_NSH:
335 clib_memcpy (&eid->address.nsh, &vat_eid->addr.nsh,
336 sizeof (eid->address.nsh));
345 api_gpe_add_del_fwd_entry (vat_main_t * vam)
347 u32 dp_table = 0, vni = 0;;
348 unformat_input_t *input = vam->input;
349 vl_api_gpe_add_del_fwd_entry_t *mp;
351 lisp_eid_vat_t _rmt_eid, *rmt_eid = &_rmt_eid;
352 lisp_eid_vat_t _lcl_eid, *lcl_eid = &_lcl_eid;
353 u8 rmt_eid_set = 0, lcl_eid_set = 0;
355 ip4_address_t rmt_rloc4, lcl_rloc4;
356 ip6_address_t rmt_rloc6, lcl_rloc6;
357 vl_api_gpe_locator_t *rmt_locs = 0, *lcl_locs = 0, rloc, *curr_rloc = 0;
360 clib_memset (&rloc, 0, sizeof (rloc));
362 /* Parse args required to build the message */
363 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
365 if (unformat (input, "del"))
367 else if (unformat (input, "add"))
369 else if (unformat (input, "reid %U", unformat_lisp_eid_vat, rmt_eid))
373 else if (unformat (input, "leid %U", unformat_lisp_eid_vat, lcl_eid))
377 else if (unformat (input, "vrf %d", &dp_table))
379 else if (unformat (input, "bd %d", &dp_table))
381 else if (unformat (input, "vni %d", &vni))
383 else if (unformat (input, "w %d", &w))
387 errmsg ("No RLOC configured for setting priority/weight!");
390 curr_rloc->weight = w;
392 else if (unformat (input, "loc-pair %U %U", unformat_ip4_address,
393 &lcl_rloc4, unformat_ip4_address, &rmt_rloc4))
396 clib_memcpy (&rloc.addr.un.ip4, &lcl_rloc4, sizeof (lcl_rloc4));
398 vec_add1 (lcl_locs, rloc);
400 clib_memcpy (&rloc.addr.un.ip4, &rmt_rloc4, sizeof (rmt_rloc4));
401 vec_add1 (rmt_locs, rloc);
402 /* weight saved in rmt loc */
403 curr_rloc = &rmt_locs[vec_len (rmt_locs) - 1];
405 else if (unformat (input, "loc-pair %U %U", unformat_ip6_address,
406 &lcl_rloc6, unformat_ip6_address, &rmt_rloc6))
409 clib_memcpy (&rloc.addr.un.ip6, &lcl_rloc6, sizeof (lcl_rloc6));
411 vec_add1 (lcl_locs, rloc);
413 clib_memcpy (&rloc.addr.un.ip6, &rmt_rloc6, sizeof (rmt_rloc6));
414 vec_add1 (rmt_locs, rloc);
415 /* weight saved in rmt loc */
416 curr_rloc = &rmt_locs[vec_len (rmt_locs) - 1];
418 else if (unformat (input, "action %d", &action))
424 clib_warning ("parse error '%U'", format_unformat_error, input);
431 errmsg ("remote eid addresses not set");
435 if (lcl_eid_set && rmt_eid->type != lcl_eid->type)
437 errmsg ("eid types don't match");
441 if (0 == rmt_locs && (u32) ~ 0 == action)
443 errmsg ("action not set for negative mapping");
447 /* Construct the API message */
448 M2 (GPE_ADD_DEL_FWD_ENTRY, mp,
449 sizeof (vl_api_gpe_locator_t) * vec_len (rmt_locs) * 2);
452 lisp_eid_put_vat (&mp->rmt_eid, rmt_eid);
453 lisp_eid_put_vat (&mp->lcl_eid, lcl_eid);
454 mp->dp_table = clib_host_to_net_u32 (dp_table);
455 mp->vni = clib_host_to_net_u32 (vni);
458 if (0 != rmt_locs && 0 != lcl_locs)
460 mp->loc_num = clib_host_to_net_u32 (vec_len (rmt_locs) * 2);
461 clib_memcpy (mp->locs, lcl_locs,
462 (sizeof (vl_api_gpe_locator_t) * vec_len (lcl_locs)));
464 u32 offset = sizeof (vl_api_gpe_locator_t) * vec_len (lcl_locs);
465 clib_memcpy (((u8 *) mp->locs) + offset, rmt_locs,
466 (sizeof (vl_api_gpe_locator_t) * vec_len (rmt_locs)));
474 /* Wait for a reply... */
480 api_gpe_enable_disable (vat_main_t * vam)
482 unformat_input_t *input = vam->input;
483 vl_api_gpe_enable_disable_t *mp;
488 /* Parse args required to build the message */
489 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
491 if (unformat (input, "enable"))
496 else if (unformat (input, "disable"))
507 errmsg ("Value not set");
511 /* Construct the API message */
512 M (GPE_ENABLE_DISABLE, mp);
514 mp->is_enable = is_enable;
519 /* Wait for a reply... */
525 unformat_gpe_encap_mode (unformat_input_t * input, va_list * args)
527 u32 *mode = va_arg (*args, u32 *);
529 if (unformat (input, "lisp"))
531 else if (unformat (input, "vxlan"))
540 api_gpe_get_encap_mode (vat_main_t * vam)
542 vl_api_gpe_get_encap_mode_t *mp;
545 /* Construct the API message */
546 M (GPE_GET_ENCAP_MODE, mp);
551 /* Wait for a reply... */
557 api_gpe_set_encap_mode (vat_main_t * vam)
559 unformat_input_t *input = vam->input;
560 vl_api_gpe_set_encap_mode_t *mp;
564 /* Parse args required to build the message */
565 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
567 if (unformat (input, "%U", unformat_gpe_encap_mode, &mode))
573 /* Construct the API message */
574 M (GPE_SET_ENCAP_MODE, mp);
581 /* Wait for a reply... */
587 api_gpe_add_del_iface (vat_main_t * vam)
589 unformat_input_t *input = vam->input;
590 vl_api_gpe_add_del_iface_t *mp;
591 u8 action_set = 0, is_add = 1, is_l2 = 0, dp_table_set = 0, vni_set = 0;
592 u32 dp_table = 0, vni = 0;
595 /* Parse args required to build the message */
596 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
598 if (unformat (input, "up"))
603 else if (unformat (input, "down"))
608 else if (unformat (input, "table_id %d", &dp_table))
612 else if (unformat (input, "bd_id %d", &dp_table))
617 else if (unformat (input, "vni %d", &vni))
627 errmsg ("Action not set");
630 if (dp_table_set == 0 || vni_set == 0)
632 errmsg ("vni and dp_table must be set");
636 /* Construct the API message */
637 M (GPE_ADD_DEL_IFACE, mp);
640 mp->dp_table = clib_host_to_net_u32 (dp_table);
642 mp->vni = clib_host_to_net_u32 (vni);
647 /* Wait for a reply... */
653 api_gpe_fwd_entries_get (vat_main_t * vam)
655 unformat_input_t *i = vam->input;
656 vl_api_gpe_fwd_entries_get_t *mp;
661 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
663 if (unformat (i, "vni %d", &vni))
669 errmsg ("parse error '%U'", format_unformat_error, i);
676 errmsg ("vni not set!");
680 if (!vam->json_output)
682 print (vam->ofp, "%10s %10s %s %40s", "fwd_index", "dp_table",
686 M (GPE_FWD_ENTRIES_GET, mp);
687 mp->vni = clib_host_to_net_u32 (vni);
692 /* Wait for a reply... */
698 api_gpe_native_fwd_rpaths_get (vat_main_t * vam)
700 unformat_input_t *i = vam->input;
701 vl_api_gpe_native_fwd_rpaths_get_t *mp;
703 u8 ip_family_set = 0, is_ip4 = 1;
705 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
707 if (unformat (i, "ip4"))
712 else if (unformat (i, "ip6"))
719 errmsg ("parse error '%U'", format_unformat_error, i);
726 errmsg ("ip family not set!");
730 M (GPE_NATIVE_FWD_RPATHS_GET, mp);
736 /* Wait for a reply... */
742 api_gpe_fwd_entry_vnis_get (vat_main_t * vam)
744 vl_api_gpe_fwd_entry_vnis_get_t *mp;
747 if (!vam->json_output)
749 print (vam->ofp, "VNIs");
752 M (GPE_FWD_ENTRY_VNIS_GET, mp);
757 /* Wait for a reply... */
763 api_gpe_add_del_native_fwd_rpath (vat_main_t * vam)
765 unformat_input_t *i = vam->input;
766 vl_api_gpe_add_del_native_fwd_rpath_t *mp;
768 u8 is_add = 1, ip_set = 0, is_ip4 = 1;
771 u32 table_id = 0, nh_sw_if_index = ~0;
773 clib_memset (&ip4, 0, sizeof (ip4));
774 clib_memset (&ip6, 0, sizeof (ip6));
776 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
778 if (unformat (i, "del"))
780 else if (unformat (i, "via %U %U", unformat_ip4_address, &ip4,
781 unformat_sw_if_index, vam, &nh_sw_if_index))
786 else if (unformat (i, "via %U %U", unformat_ip6_address, &ip6,
787 unformat_sw_if_index, vam, &nh_sw_if_index))
792 else if (unformat (i, "via %U", unformat_ip4_address, &ip4))
798 else if (unformat (i, "via %U", unformat_ip6_address, &ip6))
804 else if (unformat (i, "table %d", &table_id))
808 errmsg ("parse error '%U'", format_unformat_error, i);
815 errmsg ("nh addr not set!");
819 M (GPE_ADD_DEL_NATIVE_FWD_RPATH, mp);
821 mp->table_id = clib_host_to_net_u32 (table_id);
822 mp->nh_sw_if_index = clib_host_to_net_u32 (nh_sw_if_index);
823 mp->nh_addr.af = is_ip4 ? 0 : 1;
825 clib_memcpy (mp->nh_addr.un.ip4, &ip4, sizeof (ip4));
827 clib_memcpy (mp->nh_addr.un.ip6, &ip6, sizeof (ip6));
832 /* Wait for a reply... */
838 api_gpe_fwd_entry_path_dump (vat_main_t * vam)
840 vl_api_gpe_fwd_entry_path_dump_t *mp;
841 vl_api_control_ping_t *mp_ping;
842 unformat_input_t *i = vam->input;
843 u32 fwd_entry_index = ~0;
846 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
848 if (unformat (i, "index %d", &fwd_entry_index))
854 if (~0 == fwd_entry_index)
856 errmsg ("no index specified!");
860 if (!vam->json_output)
862 print (vam->ofp, "first line");
865 M (GPE_FWD_ENTRY_PATH_DUMP, mp);
869 /* Use a control ping for synchronization */
870 LISP_PING (&lisp_gpe_test_main, mp_ping);
873 /* Wait for a reply... */
878 #define vat_plugin_register vat_plugin_register_gpe
879 #include <lisp/lisp-gpe/lisp_gpe.api_test.c>
882 * fd.io coding-style-patch-verification: ON
885 * eval: (c-set-style "gnu")