2 * Copyright (c) 2018 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.
16 *------------------------------------------------------------------
17 * map_test.c - test harness plugin
18 *------------------------------------------------------------------
22 #include <vlibapi/api.h>
23 #include <vlibmemory/api.h>
24 #include <vppinfra/error.h>
25 #include <vnet/ip/ip.h>
27 #define __plugin_msg_base map_test_main.msg_id_base
28 #include <vlibapi/vat_helper_macros.h>
30 /* Declare message IDs */
31 #include <map/map_msg_enum.h>
33 /* Get CRC codes of the messages defined outside of this plugin */
34 #define vl_msg_name_crc_list
35 #include <vpp/api/vpe_all_api_h.h>
36 #undef vl_msg_name_crc_list
38 /* define message structures */
40 #include <vpp/api/vpe_all_api_h.h>
41 #include <map/map_all_api_h.h>
44 /* declare message handlers for each api */
46 #define vl_endianfun /* define message structures */
47 #include <map/map_all_api_h.h>
50 /* instantiate all the print functions we know about */
51 #define vl_print(handle, ...)
53 #include <map/map_all_api_h.h>
56 /* Get the API version number. */
57 #define vl_api_version(n,v) static u32 api_version=(v);
58 #include <map/map_all_api_h.h>
62 /* API message ID base */
68 map_test_main_t map_test_main;
70 #define foreach_standard_reply_retval_handler \
71 _(map_del_domain_reply) \
72 _(map_add_del_rule_reply)
75 static void vl_api_##n##_t_handler \
76 (vl_api_##n##_t * mp) \
78 vat_main_t * vam = map_test_main.vat_main; \
79 i32 retval = ntohl(mp->retval); \
80 if (vam->json_output) { \
81 vat_json_node_t node; \
82 vat_json_init_object (&node); \
83 vat_json_object_add_int (&node, "retval", ntohl (mp->retval)); \
84 vat_json_print (vam->ofp, &node); \
85 vat_json_free (&node); \
88 if (vam->async_mode) { \
89 vam->async_errors += (retval < 0); \
91 vam->retval = retval; \
92 vam->result_ready = 1; \
95 foreach_standard_reply_retval_handler;
99 * Table of message reply handlers, must include boilerplate handlers
102 #define foreach_vpe_api_reply_msg \
103 _(MAP_ADD_DOMAIN_REPLY, map_add_domain_reply) \
104 _(MAP_DEL_DOMAIN_REPLY, map_del_domain_reply) \
105 _(MAP_ADD_DEL_RULE_REPLY, map_add_del_rule_reply) \
106 _(MAP_DOMAIN_DETAILS, map_domain_details)
109 api_map_add_domain (vat_main_t * vam)
111 unformat_input_t *i = vam->input;
112 vl_api_map_add_domain_t *mp;
114 ip4_address_t ip4_prefix;
115 ip6_address_t ip6_prefix;
116 ip6_address_t ip6_src;
118 u32 ip6_prefix_len = 0, ip4_prefix_len = 0, ea_bits_len = 0, psid_offset =
120 u8 is_translation = 0;
122 u32 ip6_src_len = 128;
125 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
127 if (unformat (i, "ip4-pfx %U/%d", unformat_ip4_address,
128 &ip4_prefix, &ip4_prefix_len))
130 else if (unformat (i, "ip6-pfx %U/%d", unformat_ip6_address,
131 &ip6_prefix, &ip6_prefix_len))
135 (i, "ip6-src %U/%d", unformat_ip6_address, &ip6_src,
138 else if (unformat (i, "ip6-src %U", unformat_ip6_address, &ip6_src))
140 else if (unformat (i, "ea-bits-len %d", &ea_bits_len))
142 else if (unformat (i, "psid-offset %d", &psid_offset))
144 else if (unformat (i, "psid-len %d", &psid_length))
146 else if (unformat (i, "mtu %d", &mtu))
148 else if (unformat (i, "map-t"))
152 clib_warning ("parse error '%U'", format_unformat_error, i);
159 errmsg ("mandatory argument(s) missing");
163 /* Construct the API message */
164 M (MAP_ADD_DOMAIN, mp);
166 clib_memcpy (mp->ip4_prefix, &ip4_prefix, sizeof (ip4_prefix));
167 mp->ip4_prefix_len = ip4_prefix_len;
169 clib_memcpy (mp->ip6_prefix, &ip6_prefix, sizeof (ip6_prefix));
170 mp->ip6_prefix_len = ip6_prefix_len;
172 clib_memcpy (mp->ip6_src, &ip6_src, sizeof (ip6_src));
173 mp->ip6_src_prefix_len = ip6_src_len;
175 mp->ea_bits_len = ea_bits_len;
176 mp->psid_offset = psid_offset;
177 mp->psid_length = psid_length;
178 mp->is_translation = is_translation;
179 mp->mtu = htons (mtu);
184 /* Wait for a reply, return good/bad news */
189 api_map_del_domain (vat_main_t * vam)
191 unformat_input_t *i = vam->input;
192 vl_api_map_del_domain_t *mp;
198 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
200 if (unformat (i, "index %d", &index))
204 clib_warning ("parse error '%U'", format_unformat_error, i);
211 errmsg ("mandatory argument(s) missing");
215 /* Construct the API message */
216 M (MAP_DEL_DOMAIN, mp);
218 mp->index = ntohl (index);
223 /* Wait for a reply, return good/bad news */
229 api_map_add_del_rule (vat_main_t * vam)
231 unformat_input_t *i = vam->input;
232 vl_api_map_add_del_rule_t *mp;
234 ip6_address_t ip6_dst;
235 u32 num_m_args = 0, index, psid = 0;
238 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
240 if (unformat (i, "index %d", &index))
242 else if (unformat (i, "psid %d", &psid))
244 else if (unformat (i, "dst %U", unformat_ip6_address, &ip6_dst))
246 else if (unformat (i, "del"))
252 clib_warning ("parse error '%U'", format_unformat_error, i);
257 /* Construct the API message */
258 M (MAP_ADD_DEL_RULE, mp);
260 mp->index = ntohl (index);
262 clib_memcpy (mp->ip6_dst, &ip6_dst, sizeof (ip6_dst));
263 mp->psid = ntohs (psid);
268 /* Wait for a reply, return good/bad news */
273 api_map_domain_dump (vat_main_t * vam)
275 map_test_main_t *mm = &map_test_main;
276 vl_api_map_domain_dump_t *mp;
277 vl_api_control_ping_t *mp_ping;
280 /* Construct the API message */
281 M (MAP_DOMAIN_DUMP, mp);
286 /* Use a control ping for synchronization */
287 mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
288 mp_ping->_vl_msg_id = htons (mm->ping_id);
289 mp_ping->client_index = vam->my_client_index;
291 fformat (vam->ofp, "Sending ping id=%d\n", mm->ping_id);
293 vam->result_ready = 0;
302 api_map_rule_dump (vat_main_t * vam)
304 map_test_main_t *mm = &map_test_main;
305 unformat_input_t *i = vam->input;
306 vl_api_map_rule_dump_t *mp;
307 vl_api_control_ping_t *mp_ping;
308 u32 domain_index = ~0;
311 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
313 if (unformat (i, "index %u", &domain_index))
319 if (domain_index == ~0)
321 clib_warning ("parse error: domain index expected");
325 /* Construct the API message */
326 M (MAP_RULE_DUMP, mp);
328 mp->domain_index = htonl (domain_index);
333 /* Use a control ping for synchronization */
334 /* Use a control ping for synchronization */
335 mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
336 mp_ping->_vl_msg_id = htons (mm->ping_id);
337 mp_ping->client_index = vam->my_client_index;
339 vam->result_ready = 0;
346 static void vl_api_map_add_domain_reply_t_handler
347 (vl_api_map_add_domain_reply_t * mp)
349 vat_main_t *vam = &vat_main;
350 i32 retval = ntohl (mp->retval);
352 if (vam->json_output) {
353 vat_json_node_t node;
354 vat_json_init_object (&node);
355 vat_json_object_add_int (&node, "retval", ntohl (mp->retval));
356 vat_json_object_add_uint (&node, "index", ntohl (mp->index));
357 vat_json_print (vam->ofp, &node);
358 vat_json_free (&node);
363 vam->async_errors += (retval < 0);
367 vam->retval = retval;
368 vam->result_ready = 1;
372 static void vl_api_map_domain_details_t_handler_json
373 (vl_api_map_domain_details_t * mp)
375 vat_json_node_t *node = NULL;
376 vat_main_t *vam = &vat_main;
380 if (VAT_JSON_ARRAY != vam->json_tree.type)
382 ASSERT (VAT_JSON_NONE == vam->json_tree.type);
383 vat_json_init_array (&vam->json_tree);
386 node = vat_json_array_add (&vam->json_tree);
387 vat_json_init_object (node);
389 vat_json_object_add_uint (node, "domain_index",
390 clib_net_to_host_u32 (mp->domain_index));
391 clib_memcpy (&ip6, mp->ip6_prefix, sizeof (ip6));
392 vat_json_object_add_ip6 (node, "ip6_prefix", ip6);
393 clib_memcpy (&ip4, mp->ip4_prefix, sizeof (ip4));
394 vat_json_object_add_ip4 (node, "ip4_prefix", ip4);
395 clib_memcpy (&ip6, mp->ip6_src, sizeof (ip6));
396 vat_json_object_add_ip6 (node, "ip6_src", ip6);
397 vat_json_object_add_int (node, "ip6_prefix_len", mp->ip6_prefix_len);
398 vat_json_object_add_int (node, "ip4_prefix_len", mp->ip4_prefix_len);
399 vat_json_object_add_int (node, "ip6_src_len", mp->ip6_src_len);
400 vat_json_object_add_int (node, "ea_bits_len", mp->ea_bits_len);
401 vat_json_object_add_int (node, "psid_offset", mp->psid_offset);
402 vat_json_object_add_int (node, "psid_length", mp->psid_length);
403 vat_json_object_add_uint (node, "flags", mp->flags);
404 vat_json_object_add_uint (node, "mtu", clib_net_to_host_u16 (mp->mtu));
405 vat_json_object_add_int (node, "is_translation", mp->is_translation);
408 static void vl_api_map_domain_details_t_handler
409 (vl_api_map_domain_details_t * mp)
411 vat_main_t *vam = &vat_main;
413 if (vam->json_output)
414 return vl_api_map_domain_details_t_handler_json (mp);
416 if (mp->is_translation)
419 "* %U/%d (ipv4-prefix) %U/%d (ipv6-prefix) %U/%d (ip6-src) index: %u",
420 format_ip4_address, mp->ip4_prefix, mp->ip4_prefix_len,
421 format_ip6_address, mp->ip6_prefix, mp->ip6_prefix_len,
422 format_ip6_address, mp->ip6_src, mp->ip6_src_len,
423 clib_net_to_host_u32 (mp->domain_index));
428 "* %U/%d (ipv4-prefix) %U/%d (ipv6-prefix) %U (ip6-src) index: %u",
429 format_ip4_address, mp->ip4_prefix, mp->ip4_prefix_len,
430 format_ip6_address, mp->ip6_prefix, mp->ip6_prefix_len,
431 format_ip6_address, mp->ip6_src,
432 clib_net_to_host_u32 (mp->domain_index));
434 print (vam->ofp, " ea-len %d psid-offset %d psid-len %d mtu %d %s",
435 mp->ea_bits_len, mp->psid_offset, mp->psid_length, mp->mtu,
436 mp->is_translation ? "map-t" : "");
440 * List of messages that the api test plugin sends,
441 * and that the data plane plugin processes
443 #define foreach_vpe_api_msg \
445 "ip4-pfx <ip4pfx> ip6-pfx <ip6pfx> " \
446 "ip6-src <ip6addr> " \
447 "ea-bits-len <n> psid-offset <n> psid-len <n>") \
448 _(map_del_domain, "index <n>") \
449 _(map_add_del_rule, \
450 "index <n> psid <n> dst <ip6addr> [del]") \
451 _(map_domain_dump, "") \
452 _(map_rule_dump, "index <map-domain>")
454 static void map_api_hookup (vat_main_t *vam)
456 map_test_main_t * mm = &map_test_main;
457 /* Hook up handlers for replies from the data plane plug-in */
459 vl_msg_api_set_handlers((VL_API_##N + mm->msg_id_base), \
461 vl_api_##n##_t_handler, \
463 vl_api_##n##_t_endian, \
464 vl_api_##n##_t_print, \
465 sizeof(vl_api_##n##_t), 1);
466 foreach_vpe_api_reply_msg;
469 /* API messages we can send */
471 hash_set_mem (vam->function_by_name, #n, api_##n);
476 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
481 clib_error_t * vat_plugin_register (vat_main_t *vam)
483 map_test_main_t * mm = &map_test_main;
488 name = format (0, "map_%08x%c", api_version, 0);
489 mm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
491 /* Get the control ping ID */
492 #define _(id,n,crc) \
493 const char *id ## _CRC __attribute__ ((unused)) = #n "_" #crc;
494 foreach_vl_msg_name_crc_vpe;
496 mm->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
498 if (mm->msg_id_base != (u16) ~0)
499 map_api_hookup (vam);