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 <lisp/lisp-cp/lisp_types.h>
25 /* define message IDs */
26 #include <lisp/lisp-gpe/lisp_gpe.api_enum.h>
27 #include <lisp/lisp-gpe/lisp_gpe.api_types.h>
28 #include <vpp/api/vpe.api_types.h>
32 /* API message ID base */
36 } lisp_gpe_test_main_t;
38 lisp_gpe_test_main_t lisp_gpe_test_main;
40 #define __plugin_msg_base lisp_gpe_test_main.msg_id_base
41 #include <vlibapi/vat_helper_macros.h>
43 /* Macro to finish up custom dump fns */
44 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
47 vl_print (handle, (char *)s); \
51 #define LISP_PING(_lm, mp_ping) \
52 if (!(_lm)->ping_id) \
53 (_lm)->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC)); \
54 mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping)); \
55 mp_ping->_vl_msg_id = htons ((_lm)->ping_id); \
56 mp_ping->client_index = vam->my_client_index; \
57 fformat (vam->ofp, "Sending ping id=%d\n", (_lm)->ping_id); \
58 vam->result_ready = 0; \
64 } __attribute__ ((__packed__)) lisp_nsh_api_t;
67 unformat_nsh_address (unformat_input_t * input, va_list * args)
69 lisp_nsh_api_t *nsh = va_arg (*args, lisp_nsh_api_t *);
70 return unformat (input, "SPI:%d SI:%d", &nsh->spi, &nsh->si);
74 format_nsh_address_vat (u8 * s, va_list * args)
76 nsh_t *a = va_arg (*args, nsh_t *);
77 return format (s, "SPI:%d SI:%d", clib_net_to_host_u32 (a->spi), a->si);
81 format_lisp_flat_eid (u8 * s, va_list * args)
83 vl_api_eid_t *eid = va_arg (*args, vl_api_eid_t *);
87 case EID_TYPE_API_PREFIX:
88 if (eid->address.prefix.address.af)
89 return format (s, "%U/%d", format_ip6_address,
90 eid->address.prefix.address.un.ip6,
91 eid->address.prefix.len);
92 return format (s, "%U/%d", format_ip4_address,
93 eid->address.prefix.address.un.ip4,
94 eid->address.prefix.len);
95 case EID_TYPE_API_MAC:
96 return format (s, "%U", format_ethernet_address, eid->address.mac);
97 case EID_TYPE_API_NSH:
98 return format (s, "%U", format_nsh_address_vat, eid->address.nsh);
103 static void vl_api_gpe_add_del_fwd_entry_reply_t_handler
104 (vl_api_gpe_add_del_fwd_entry_reply_t * mp)
106 vat_main_t *vam = &vat_main;
107 i32 retval = ntohl (mp->retval);
110 vam->async_errors += (retval < 0);
114 vam->retval = retval;
115 vam->result_ready = 1;
120 api_gpe_fwd_entry_net_to_host (vl_api_gpe_fwd_entry_t * e)
122 e->dp_table = clib_net_to_host_u32 (e->dp_table);
123 e->fwd_entry_index = clib_net_to_host_u32 (e->fwd_entry_index);
124 e->vni = clib_net_to_host_u32 (e->vni);
128 gpe_fwd_entries_get_reply_t_net_to_host
129 (vl_api_gpe_fwd_entries_get_reply_t * mp)
133 mp->count = clib_net_to_host_u32 (mp->count);
134 for (i = 0; i < mp->count; i++)
136 api_gpe_fwd_entry_net_to_host (&mp->entries[i]);
141 format_gpe_encap_mode (u8 * s, va_list * args)
143 u32 mode = va_arg (*args, u32);
148 return format (s, "lisp");
150 return format (s, "vxlan");
156 vl_api_gpe_get_encap_mode_reply_t_handler
157 (vl_api_gpe_get_encap_mode_reply_t * mp)
159 vat_main_t *vam = &vat_main;
161 print (vam->ofp, "gpe mode: %U", format_gpe_encap_mode, mp->encap_mode);
162 vam->retval = ntohl (mp->retval);
163 vam->result_ready = 1;
167 vl_api_gpe_fwd_entry_path_details_t_handler
168 (vl_api_gpe_fwd_entry_path_details_t * mp)
170 vat_main_t *vam = &vat_main;
171 u8 *(*format_ip_address_fcn) (u8 *, va_list *) = 0;
173 if (mp->lcl_loc.addr.af)
174 format_ip_address_fcn = format_ip6_address;
176 format_ip_address_fcn = format_ip4_address;
178 print (vam->ofp, "w:%d %30U %30U", mp->rmt_loc.weight,
179 format_ip_address_fcn, &mp->lcl_loc.addr.un,
180 format_ip_address_fcn, &mp->rmt_loc.addr.un);
184 vl_api_gpe_fwd_entries_get_reply_t_handler
185 (vl_api_gpe_fwd_entries_get_reply_t * mp)
187 vat_main_t *vam = &vat_main;
189 int retval = clib_net_to_host_u32 (mp->retval);
190 vl_api_gpe_fwd_entry_t *e;
195 gpe_fwd_entries_get_reply_t_net_to_host (mp);
197 for (i = 0; i < mp->count; i++)
200 print (vam->ofp, "%10d %10d %U %40U", e->fwd_entry_index, e->dp_table,
201 format_lisp_flat_eid, e->leid, format_lisp_flat_eid, e->reid);
205 vam->retval = retval;
206 vam->result_ready = 1;
210 vl_api_gpe_native_fwd_rpaths_get_reply_t_handler
211 (vl_api_gpe_native_fwd_rpaths_get_reply_t * mp)
213 vat_main_t *vam = &vat_main;
215 int retval = clib_net_to_host_u32 (mp->retval);
216 vl_api_gpe_native_fwd_rpath_t *r;
221 n = clib_net_to_host_u32 (mp->count);
223 for (i = 0; i < n; i++)
226 print (vam->ofp, "fib_index: %d sw_if_index %d nh %U",
227 clib_net_to_host_u32 (r->fib_index),
228 clib_net_to_host_u32 (r->nh_sw_if_index),
229 r->nh_addr.af ? format_ip6_address : format_ip4_address,
234 vam->retval = retval;
235 vam->result_ready = 1;
239 vl_api_gpe_fwd_entry_vnis_get_reply_t_handler
240 (vl_api_gpe_fwd_entry_vnis_get_reply_t * mp)
242 vat_main_t *vam = &vat_main;
244 int retval = clib_net_to_host_u32 (mp->retval);
249 n = clib_net_to_host_u32 (mp->count);
251 for (i = 0; i < n; i++)
252 print (vam->ofp, "%d", clib_net_to_host_u32 (mp->vnis[i]));
255 vam->retval = retval;
256 vam->result_ready = 1;
261 /** Used for parsing LISP eids */
262 typedef CLIB_PACKED(struct{
268 u32 len; /**< prefix length if IP */
269 u8 type; /**< type of eid */
274 unformat_lisp_eid_vat (unformat_input_t * input, va_list * args)
276 lisp_eid_vat_t *a = va_arg (*args, lisp_eid_vat_t *);
278 clib_memset (a, 0, sizeof (a[0]));
280 if (unformat (input, "%U/%d", unformat_ip46_address, a->addr.ip, &a->len))
282 a->type = 0; /* ip prefix type */
284 else if (unformat (input, "%U", unformat_ethernet_address, &a->addr.mac))
286 a->type = 1; /* mac type */
288 else if (unformat (input, "%U", unformat_nsh_address, a->addr.nsh))
290 a->type = 2; /* NSH type */
291 a->addr.nsh.spi = clib_host_to_net_u32 (a->addr.nsh.spi);
300 if (ip46_address_is_ip4 (&a->addr.ip))
301 return a->len > 32 ? 1 : 0;
303 return a->len > 128 ? 1 : 0;
310 lisp_eid_put_vat (vl_api_eid_t * eid, const lisp_eid_vat_t * vat_eid)
312 eid->type = vat_eid->type;
315 case EID_TYPE_API_PREFIX:
316 if (ip46_address_is_ip4 (&vat_eid->addr.ip))
318 clib_memcpy (&eid->address.prefix.address.un.ip4,
319 &vat_eid->addr.ip.ip4, 4);
320 eid->address.prefix.address.af = ADDRESS_IP4;
321 eid->address.prefix.len = vat_eid->len;
325 clib_memcpy (&eid->address.prefix.address.un.ip6,
326 &vat_eid->addr.ip.ip6, 16);
327 eid->address.prefix.address.af = ADDRESS_IP6;
328 eid->address.prefix.len = vat_eid->len;
331 case EID_TYPE_API_MAC:
332 clib_memcpy (&eid->address.mac, &vat_eid->addr.mac,
333 sizeof (eid->address.mac));
335 case EID_TYPE_API_NSH:
336 clib_memcpy (&eid->address.nsh, &vat_eid->addr.nsh,
337 sizeof (eid->address.nsh));
346 api_gpe_add_del_fwd_entry (vat_main_t * vam)
348 u32 dp_table = 0, vni = 0;;
349 unformat_input_t *input = vam->input;
350 vl_api_gpe_add_del_fwd_entry_t *mp;
352 lisp_eid_vat_t _rmt_eid, *rmt_eid = &_rmt_eid;
353 lisp_eid_vat_t _lcl_eid, *lcl_eid = &_lcl_eid;
354 u8 rmt_eid_set = 0, lcl_eid_set = 0;
356 ip4_address_t rmt_rloc4, lcl_rloc4;
357 ip6_address_t rmt_rloc6, lcl_rloc6;
358 vl_api_gpe_locator_t *rmt_locs = 0, *lcl_locs = 0, rloc, *curr_rloc = 0;
361 clib_memset (&rloc, 0, sizeof (rloc));
363 /* Parse args required to build the message */
364 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
366 if (unformat (input, "del"))
368 else if (unformat (input, "add"))
370 else if (unformat (input, "reid %U", unformat_lisp_eid_vat, rmt_eid))
374 else if (unformat (input, "leid %U", unformat_lisp_eid_vat, lcl_eid))
378 else if (unformat (input, "vrf %d", &dp_table))
380 else if (unformat (input, "bd %d", &dp_table))
382 else if (unformat (input, "vni %d", &vni))
384 else if (unformat (input, "w %d", &w))
388 errmsg ("No RLOC configured for setting priority/weight!");
391 curr_rloc->weight = w;
393 else if (unformat (input, "loc-pair %U %U", unformat_ip4_address,
394 &lcl_rloc4, unformat_ip4_address, &rmt_rloc4))
397 clib_memcpy (&rloc.addr.un.ip4, &lcl_rloc4, sizeof (lcl_rloc4));
399 vec_add1 (lcl_locs, rloc);
401 clib_memcpy (&rloc.addr.un.ip4, &rmt_rloc4, sizeof (rmt_rloc4));
402 vec_add1 (rmt_locs, rloc);
403 /* weight saved in rmt loc */
404 curr_rloc = &rmt_locs[vec_len (rmt_locs) - 1];
406 else if (unformat (input, "loc-pair %U %U", unformat_ip6_address,
407 &lcl_rloc6, unformat_ip6_address, &rmt_rloc6))
410 clib_memcpy (&rloc.addr.un.ip6, &lcl_rloc6, sizeof (lcl_rloc6));
412 vec_add1 (lcl_locs, rloc);
414 clib_memcpy (&rloc.addr.un.ip6, &rmt_rloc6, sizeof (rmt_rloc6));
415 vec_add1 (rmt_locs, rloc);
416 /* weight saved in rmt loc */
417 curr_rloc = &rmt_locs[vec_len (rmt_locs) - 1];
419 else if (unformat (input, "action %d", &action))
425 clib_warning ("parse error '%U'", format_unformat_error, input);
432 errmsg ("remote eid addresses not set");
436 if (lcl_eid_set && rmt_eid->type != lcl_eid->type)
438 errmsg ("eid types don't match");
442 if (0 == rmt_locs && (u32) ~ 0 == action)
444 errmsg ("action not set for negative mapping");
448 /* Construct the API message */
449 M2 (GPE_ADD_DEL_FWD_ENTRY, mp,
450 sizeof (vl_api_gpe_locator_t) * vec_len (rmt_locs) * 2);
453 lisp_eid_put_vat (&mp->rmt_eid, rmt_eid);
454 lisp_eid_put_vat (&mp->lcl_eid, lcl_eid);
455 mp->dp_table = clib_host_to_net_u32 (dp_table);
456 mp->vni = clib_host_to_net_u32 (vni);
459 if (0 != rmt_locs && 0 != lcl_locs)
461 mp->loc_num = clib_host_to_net_u32 (vec_len (rmt_locs) * 2);
462 clib_memcpy (mp->locs, lcl_locs,
463 (sizeof (vl_api_gpe_locator_t) * vec_len (lcl_locs)));
465 u32 offset = sizeof (vl_api_gpe_locator_t) * vec_len (lcl_locs);
466 clib_memcpy (((u8 *) mp->locs) + offset, rmt_locs,
467 (sizeof (vl_api_gpe_locator_t) * vec_len (rmt_locs)));
475 /* Wait for a reply... */
481 api_gpe_enable_disable (vat_main_t * vam)
483 unformat_input_t *input = vam->input;
484 vl_api_gpe_enable_disable_t *mp;
489 /* Parse args required to build the message */
490 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
492 if (unformat (input, "enable"))
497 else if (unformat (input, "disable"))
508 errmsg ("Value not set");
512 /* Construct the API message */
513 M (GPE_ENABLE_DISABLE, mp);
515 mp->is_enable = is_enable;
520 /* Wait for a reply... */
526 unformat_gpe_encap_mode (unformat_input_t * input, va_list * args)
528 u32 *mode = va_arg (*args, u32 *);
530 if (unformat (input, "lisp"))
532 else if (unformat (input, "vxlan"))
541 api_gpe_get_encap_mode (vat_main_t * vam)
543 vl_api_gpe_get_encap_mode_t *mp;
546 /* Construct the API message */
547 M (GPE_GET_ENCAP_MODE, mp);
552 /* Wait for a reply... */
558 api_gpe_set_encap_mode (vat_main_t * vam)
560 unformat_input_t *input = vam->input;
561 vl_api_gpe_set_encap_mode_t *mp;
565 /* Parse args required to build the message */
566 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
568 if (unformat (input, "%U", unformat_gpe_encap_mode, &mode))
574 /* Construct the API message */
575 M (GPE_SET_ENCAP_MODE, mp);
582 /* Wait for a reply... */
588 api_gpe_add_del_iface (vat_main_t * vam)
590 unformat_input_t *input = vam->input;
591 vl_api_gpe_add_del_iface_t *mp;
592 u8 action_set = 0, is_add = 1, is_l2 = 0, dp_table_set = 0, vni_set = 0;
593 u32 dp_table = 0, vni = 0;
596 /* Parse args required to build the message */
597 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
599 if (unformat (input, "up"))
604 else if (unformat (input, "down"))
609 else if (unformat (input, "table_id %d", &dp_table))
613 else if (unformat (input, "bd_id %d", &dp_table))
618 else if (unformat (input, "vni %d", &vni))
628 errmsg ("Action not set");
631 if (dp_table_set == 0 || vni_set == 0)
633 errmsg ("vni and dp_table must be set");
637 /* Construct the API message */
638 M (GPE_ADD_DEL_IFACE, mp);
641 mp->dp_table = clib_host_to_net_u32 (dp_table);
643 mp->vni = clib_host_to_net_u32 (vni);
648 /* Wait for a reply... */
654 api_gpe_fwd_entries_get (vat_main_t * vam)
656 unformat_input_t *i = vam->input;
657 vl_api_gpe_fwd_entries_get_t *mp;
662 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
664 if (unformat (i, "vni %d", &vni))
670 errmsg ("parse error '%U'", format_unformat_error, i);
677 errmsg ("vni not set!");
681 if (!vam->json_output)
683 print (vam->ofp, "%10s %10s %s %40s", "fwd_index", "dp_table",
687 M (GPE_FWD_ENTRIES_GET, mp);
688 mp->vni = clib_host_to_net_u32 (vni);
693 /* Wait for a reply... */
699 api_gpe_native_fwd_rpaths_get (vat_main_t * vam)
701 unformat_input_t *i = vam->input;
702 vl_api_gpe_native_fwd_rpaths_get_t *mp;
704 u8 ip_family_set = 0, is_ip4 = 1;
706 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
708 if (unformat (i, "ip4"))
713 else if (unformat (i, "ip6"))
720 errmsg ("parse error '%U'", format_unformat_error, i);
727 errmsg ("ip family not set!");
731 M (GPE_NATIVE_FWD_RPATHS_GET, mp);
737 /* Wait for a reply... */
743 api_gpe_fwd_entry_vnis_get (vat_main_t * vam)
745 vl_api_gpe_fwd_entry_vnis_get_t *mp;
748 if (!vam->json_output)
750 print (vam->ofp, "VNIs");
753 M (GPE_FWD_ENTRY_VNIS_GET, mp);
758 /* Wait for a reply... */
764 api_gpe_add_del_native_fwd_rpath (vat_main_t * vam)
766 unformat_input_t *i = vam->input;
767 vl_api_gpe_add_del_native_fwd_rpath_t *mp;
769 u8 is_add = 1, ip_set = 0, is_ip4 = 1;
772 u32 table_id = 0, nh_sw_if_index = ~0;
774 clib_memset (&ip4, 0, sizeof (ip4));
775 clib_memset (&ip6, 0, sizeof (ip6));
777 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
779 if (unformat (i, "del"))
781 else if (unformat (i, "via %U %U", unformat_ip4_address, &ip4,
782 unformat_sw_if_index, vam, &nh_sw_if_index))
787 else if (unformat (i, "via %U %U", unformat_ip6_address, &ip6,
788 unformat_sw_if_index, vam, &nh_sw_if_index))
793 else if (unformat (i, "via %U", unformat_ip4_address, &ip4))
799 else if (unformat (i, "via %U", unformat_ip6_address, &ip6))
805 else if (unformat (i, "table %d", &table_id))
809 errmsg ("parse error '%U'", format_unformat_error, i);
816 errmsg ("nh addr not set!");
820 M (GPE_ADD_DEL_NATIVE_FWD_RPATH, mp);
822 mp->table_id = clib_host_to_net_u32 (table_id);
823 mp->nh_sw_if_index = clib_host_to_net_u32 (nh_sw_if_index);
824 mp->nh_addr.af = is_ip4 ? 0 : 1;
826 clib_memcpy (mp->nh_addr.un.ip4, &ip4, sizeof (ip4));
828 clib_memcpy (mp->nh_addr.un.ip6, &ip6, sizeof (ip6));
833 /* Wait for a reply... */
839 api_gpe_fwd_entry_path_dump (vat_main_t * vam)
841 vl_api_gpe_fwd_entry_path_dump_t *mp;
842 vl_api_control_ping_t *mp_ping;
843 unformat_input_t *i = vam->input;
844 u32 fwd_entry_index = ~0;
847 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
849 if (unformat (i, "index %d", &fwd_entry_index))
855 if (~0 == fwd_entry_index)
857 errmsg ("no index specified!");
861 if (!vam->json_output)
863 print (vam->ofp, "first line");
866 M (GPE_FWD_ENTRY_PATH_DUMP, mp);
870 /* Use a control ping for synchronization */
871 LISP_PING (&lisp_gpe_test_main, mp_ping);
874 /* Wait for a reply... */
879 #define vat_plugin_register vat_plugin_register_gpe
880 #include <lisp/lisp-gpe/lisp_gpe.api_test.c>
883 * fd.io coding-style-patch-verification: ON
886 * eval: (c-set-style "gnu")