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;
259 /** Used for parsing LISP eids */
260 typedef CLIB_PACKED(struct{
266 u32 len; /**< prefix length if IP */
267 u8 type; /**< type of eid */
271 unformat_lisp_eid_vat (unformat_input_t * input, va_list * args)
273 lisp_eid_vat_t *a = va_arg (*args, lisp_eid_vat_t *);
275 clib_memset (a, 0, sizeof (a[0]));
277 if (unformat (input, "%U/%d", unformat_ip46_address, a->addr.ip, &a->len))
279 a->type = 0; /* ip prefix type */
281 else if (unformat (input, "%U", unformat_ethernet_address, &a->addr.mac))
283 a->type = 1; /* mac type */
285 else if (unformat (input, "%U", unformat_nsh_address, a->addr.nsh))
287 a->type = 2; /* NSH type */
288 a->addr.nsh.spi = clib_host_to_net_u32 (a->addr.nsh.spi);
297 if (ip46_address_is_ip4 (&a->addr.ip))
298 return a->len > 32 ? 1 : 0;
300 return a->len > 128 ? 1 : 0;
307 lisp_eid_put_vat (vl_api_eid_t * eid, const lisp_eid_vat_t * vat_eid)
309 eid->type = vat_eid->type;
312 case EID_TYPE_API_PREFIX:
313 if (ip46_address_is_ip4 (&vat_eid->addr.ip))
315 clib_memcpy (&eid->address.prefix.address.un.ip4,
316 &vat_eid->addr.ip.ip4, 4);
317 eid->address.prefix.address.af = ADDRESS_IP4;
318 eid->address.prefix.len = vat_eid->len;
322 clib_memcpy (&eid->address.prefix.address.un.ip6,
323 &vat_eid->addr.ip.ip6, 16);
324 eid->address.prefix.address.af = ADDRESS_IP6;
325 eid->address.prefix.len = vat_eid->len;
328 case EID_TYPE_API_MAC:
329 clib_memcpy (&eid->address.mac, &vat_eid->addr.mac,
330 sizeof (eid->address.mac));
332 case EID_TYPE_API_NSH:
333 clib_memcpy (&eid->address.nsh, &vat_eid->addr.nsh,
334 sizeof (eid->address.nsh));
343 api_gpe_add_del_fwd_entry (vat_main_t * vam)
345 u32 dp_table = 0, vni = 0;;
346 unformat_input_t *input = vam->input;
347 vl_api_gpe_add_del_fwd_entry_t *mp;
349 lisp_eid_vat_t _rmt_eid, *rmt_eid = &_rmt_eid;
350 lisp_eid_vat_t _lcl_eid, *lcl_eid = &_lcl_eid;
351 u8 rmt_eid_set = 0, lcl_eid_set = 0;
353 ip4_address_t rmt_rloc4, lcl_rloc4;
354 ip6_address_t rmt_rloc6, lcl_rloc6;
355 vl_api_gpe_locator_t *rmt_locs = 0, *lcl_locs = 0, rloc, *curr_rloc = 0;
358 clib_memset (&rloc, 0, sizeof (rloc));
360 /* Parse args required to build the message */
361 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
363 if (unformat (input, "del"))
365 else if (unformat (input, "add"))
367 else if (unformat (input, "reid %U", unformat_lisp_eid_vat, rmt_eid))
371 else if (unformat (input, "leid %U", unformat_lisp_eid_vat, lcl_eid))
375 else if (unformat (input, "vrf %d", &dp_table))
377 else if (unformat (input, "bd %d", &dp_table))
379 else if (unformat (input, "vni %d", &vni))
381 else if (unformat (input, "w %d", &w))
385 errmsg ("No RLOC configured for setting priority/weight!");
388 curr_rloc->weight = w;
390 else if (unformat (input, "loc-pair %U %U", unformat_ip4_address,
391 &lcl_rloc4, unformat_ip4_address, &rmt_rloc4))
394 clib_memcpy (&rloc.addr.un.ip4, &lcl_rloc4, sizeof (lcl_rloc4));
396 vec_add1 (lcl_locs, rloc);
398 clib_memcpy (&rloc.addr.un.ip4, &rmt_rloc4, sizeof (rmt_rloc4));
399 vec_add1 (rmt_locs, rloc);
400 /* weight saved in rmt loc */
401 curr_rloc = &rmt_locs[vec_len (rmt_locs) - 1];
403 else if (unformat (input, "loc-pair %U %U", unformat_ip6_address,
404 &lcl_rloc6, unformat_ip6_address, &rmt_rloc6))
407 clib_memcpy (&rloc.addr.un.ip6, &lcl_rloc6, sizeof (lcl_rloc6));
409 vec_add1 (lcl_locs, rloc);
411 clib_memcpy (&rloc.addr.un.ip6, &rmt_rloc6, sizeof (rmt_rloc6));
412 vec_add1 (rmt_locs, rloc);
413 /* weight saved in rmt loc */
414 curr_rloc = &rmt_locs[vec_len (rmt_locs) - 1];
416 else if (unformat (input, "action %d", &action))
422 clib_warning ("parse error '%U'", format_unformat_error, input);
429 errmsg ("remote eid addresses not set");
433 if (lcl_eid_set && rmt_eid->type != lcl_eid->type)
435 errmsg ("eid types don't match");
439 if (0 == rmt_locs && (u32) ~ 0 == action)
441 errmsg ("action not set for negative mapping");
445 /* Construct the API message */
446 M2 (GPE_ADD_DEL_FWD_ENTRY, mp,
447 sizeof (vl_api_gpe_locator_t) * vec_len (rmt_locs) * 2);
450 lisp_eid_put_vat (&mp->rmt_eid, rmt_eid);
451 lisp_eid_put_vat (&mp->lcl_eid, lcl_eid);
452 mp->dp_table = clib_host_to_net_u32 (dp_table);
453 mp->vni = clib_host_to_net_u32 (vni);
456 if (0 != rmt_locs && 0 != lcl_locs)
458 mp->loc_num = clib_host_to_net_u32 (vec_len (rmt_locs) * 2);
459 clib_memcpy (mp->locs, lcl_locs,
460 (sizeof (vl_api_gpe_locator_t) * vec_len (lcl_locs)));
462 u32 offset = sizeof (vl_api_gpe_locator_t) * vec_len (lcl_locs);
463 clib_memcpy (((u8 *) mp->locs) + offset, rmt_locs,
464 (sizeof (vl_api_gpe_locator_t) * vec_len (rmt_locs)));
472 /* Wait for a reply... */
478 api_gpe_enable_disable (vat_main_t * vam)
480 unformat_input_t *input = vam->input;
481 vl_api_gpe_enable_disable_t *mp;
486 /* Parse args required to build the message */
487 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
489 if (unformat (input, "enable"))
494 else if (unformat (input, "disable"))
505 errmsg ("Value not set");
509 /* Construct the API message */
510 M (GPE_ENABLE_DISABLE, mp);
512 mp->is_enable = is_enable;
517 /* Wait for a reply... */
523 unformat_gpe_encap_mode (unformat_input_t * input, va_list * args)
525 u32 *mode = va_arg (*args, u32 *);
527 if (unformat (input, "lisp"))
529 else if (unformat (input, "vxlan"))
538 api_gpe_get_encap_mode (vat_main_t * vam)
540 vl_api_gpe_get_encap_mode_t *mp;
543 /* Construct the API message */
544 M (GPE_GET_ENCAP_MODE, mp);
549 /* Wait for a reply... */
555 api_gpe_set_encap_mode (vat_main_t * vam)
557 unformat_input_t *input = vam->input;
558 vl_api_gpe_set_encap_mode_t *mp;
562 /* Parse args required to build the message */
563 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
565 if (unformat (input, "%U", unformat_gpe_encap_mode, &mode))
571 /* Construct the API message */
572 M (GPE_SET_ENCAP_MODE, mp);
579 /* Wait for a reply... */
585 api_gpe_add_del_iface (vat_main_t * vam)
587 unformat_input_t *input = vam->input;
588 vl_api_gpe_add_del_iface_t *mp;
589 u8 action_set = 0, is_add = 1, is_l2 = 0, dp_table_set = 0, vni_set = 0;
590 u32 dp_table = 0, vni = 0;
593 /* Parse args required to build the message */
594 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
596 if (unformat (input, "up"))
601 else if (unformat (input, "down"))
606 else if (unformat (input, "table_id %d", &dp_table))
610 else if (unformat (input, "bd_id %d", &dp_table))
615 else if (unformat (input, "vni %d", &vni))
625 errmsg ("Action not set");
628 if (dp_table_set == 0 || vni_set == 0)
630 errmsg ("vni and dp_table must be set");
634 /* Construct the API message */
635 M (GPE_ADD_DEL_IFACE, mp);
638 mp->dp_table = clib_host_to_net_u32 (dp_table);
640 mp->vni = clib_host_to_net_u32 (vni);
645 /* Wait for a reply... */
651 api_gpe_fwd_entries_get (vat_main_t * vam)
653 unformat_input_t *i = vam->input;
654 vl_api_gpe_fwd_entries_get_t *mp;
659 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
661 if (unformat (i, "vni %d", &vni))
667 errmsg ("parse error '%U'", format_unformat_error, i);
674 errmsg ("vni not set!");
678 if (!vam->json_output)
680 print (vam->ofp, "%10s %10s %s %40s", "fwd_index", "dp_table",
684 M (GPE_FWD_ENTRIES_GET, mp);
685 mp->vni = clib_host_to_net_u32 (vni);
690 /* Wait for a reply... */
696 api_gpe_native_fwd_rpaths_get (vat_main_t * vam)
698 unformat_input_t *i = vam->input;
699 vl_api_gpe_native_fwd_rpaths_get_t *mp;
701 u8 ip_family_set = 0, is_ip4 = 1;
703 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
705 if (unformat (i, "ip4"))
710 else if (unformat (i, "ip6"))
717 errmsg ("parse error '%U'", format_unformat_error, i);
724 errmsg ("ip family not set!");
728 M (GPE_NATIVE_FWD_RPATHS_GET, mp);
734 /* Wait for a reply... */
740 api_gpe_fwd_entry_vnis_get (vat_main_t * vam)
742 vl_api_gpe_fwd_entry_vnis_get_t *mp;
745 if (!vam->json_output)
747 print (vam->ofp, "VNIs");
750 M (GPE_FWD_ENTRY_VNIS_GET, mp);
755 /* Wait for a reply... */
761 api_gpe_add_del_native_fwd_rpath (vat_main_t * vam)
763 unformat_input_t *i = vam->input;
764 vl_api_gpe_add_del_native_fwd_rpath_t *mp;
766 u8 is_add = 1, ip_set = 0, is_ip4 = 1;
769 u32 table_id = 0, nh_sw_if_index = ~0;
771 clib_memset (&ip4, 0, sizeof (ip4));
772 clib_memset (&ip6, 0, sizeof (ip6));
774 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
776 if (unformat (i, "del"))
778 else if (unformat (i, "via %U %U", unformat_ip4_address, &ip4,
779 unformat_sw_if_index, vam, &nh_sw_if_index))
784 else if (unformat (i, "via %U %U", unformat_ip6_address, &ip6,
785 unformat_sw_if_index, vam, &nh_sw_if_index))
790 else if (unformat (i, "via %U", unformat_ip4_address, &ip4))
796 else if (unformat (i, "via %U", unformat_ip6_address, &ip6))
802 else if (unformat (i, "table %d", &table_id))
806 errmsg ("parse error '%U'", format_unformat_error, i);
813 errmsg ("nh addr not set!");
817 M (GPE_ADD_DEL_NATIVE_FWD_RPATH, mp);
819 mp->table_id = clib_host_to_net_u32 (table_id);
820 mp->nh_sw_if_index = clib_host_to_net_u32 (nh_sw_if_index);
821 mp->nh_addr.af = is_ip4 ? 0 : 1;
823 clib_memcpy (mp->nh_addr.un.ip4, &ip4, sizeof (ip4));
825 clib_memcpy (mp->nh_addr.un.ip6, &ip6, sizeof (ip6));
830 /* Wait for a reply... */
836 api_gpe_fwd_entry_path_dump (vat_main_t * vam)
838 vl_api_gpe_fwd_entry_path_dump_t *mp;
839 vl_api_control_ping_t *mp_ping;
840 unformat_input_t *i = vam->input;
841 u32 fwd_entry_index = ~0;
844 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
846 if (unformat (i, "index %d", &fwd_entry_index))
852 if (~0 == fwd_entry_index)
854 errmsg ("no index specified!");
858 if (!vam->json_output)
860 print (vam->ofp, "first line");
863 M (GPE_FWD_ENTRY_PATH_DUMP, mp);
867 /* Use a control ping for synchronization */
868 LISP_PING (&lisp_gpe_test_main, mp_ping);
871 /* Wait for a reply... */
876 #define vat_plugin_register vat_plugin_register_gpe
877 #include <lisp/lisp-gpe/lisp_gpe.api_test.c>
880 * fd.io coding-style-patch-verification: ON
883 * eval: (c-set-style "gnu")