2 * Copyright (c) 2016 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 * pot_test.c - test harness for pot plugin
18 *------------------------------------------------------------------
22 #include <vlibapi/api.h>
23 #include <vlibmemory/api.h>
24 #include <vlibsocket/api.h>
25 #include <vppinfra/error.h>
27 /* Declare message IDs */
28 #include <ioam/lib-pot/pot_msg_enum.h>
30 /* define message structures */
32 #include <ioam/lib-pot/pot_all_api_h.h>
35 /* declare message handlers for each api */
37 #define vl_endianfun /* define message structures */
38 #include <ioam/lib-pot/pot_all_api_h.h>
41 /* instantiate all the print functions we know about */
42 #define vl_print(handle, ...)
44 #include <ioam/lib-pot/pot_all_api_h.h>
47 /* Get the API version number. */
48 #define vl_api_version(n,v) static u32 api_version=(v);
49 #include <ioam/lib-pot/pot_all_api_h.h>
54 /* API message ID base */
59 pot_test_main_t pot_test_main;
61 #define foreach_standard_reply_retval_handler \
62 _(pot_profile_add_reply) \
63 _(pot_profile_activate_reply) \
64 _(pot_profile_del_reply)
66 #define foreach_custom_reply_retval_handler \
67 _(pot_profile_show_config_details, \
68 errmsg(" ID:%d\n",mp->id); \
69 errmsg(" Validator:%d\n",mp->validator); \
70 errmsg(" secret_key:%Lx\n",clib_net_to_host_u64(mp->secret_key)); \
71 errmsg(" secret_share:%Lx\n",clib_net_to_host_u64(mp->secret_share)); \
72 errmsg(" prime:%Lx\n",clib_net_to_host_u64(mp->prime)); \
73 errmsg(" bitmask:%Lx\n",clib_net_to_host_u64(mp->bit_mask)); \
74 errmsg(" lpc:%Lx\n",clib_net_to_host_u64(mp->lpc)); \
75 errmsg(" public poly:%Lx\n",clib_net_to_host_u64(mp->polynomial_public)); \
79 static void vl_api_##n##_t_handler \
80 (vl_api_##n##_t * mp) \
82 vat_main_t * vam = pot_test_main.vat_main; \
83 i32 retval = ntohl(mp->retval); \
84 if (vam->async_mode) { \
85 vam->async_errors += (retval < 0); \
87 vam->retval = retval; \
88 vam->result_ready = 1; \
91 foreach_standard_reply_retval_handler;
95 static void vl_api_##n##_t_handler \
96 (vl_api_##n##_t * mp) \
98 vat_main_t * vam = pot_test_main.vat_main; \
99 i32 retval = ntohl(mp->retval); \
100 if (vam->async_mode) { \
101 vam->async_errors += (retval < 0); \
103 vam->retval = retval; \
104 vam->result_ready = 1; \
108 foreach_custom_reply_retval_handler;
112 * Table of message reply handlers, must include boilerplate handlers
115 #define foreach_vpe_api_reply_msg \
116 _(POT_PROFILE_ADD_REPLY, pot_profile_add_reply) \
117 _(POT_PROFILE_ACTIVATE_REPLY, pot_profile_activate_reply) \
118 _(POT_PROFILE_DEL_REPLY, pot_profile_del_reply) \
119 _(POT_PROFILE_SHOW_CONFIG_DETAILS, pot_profile_show_config_details)
122 /* M: construct, but don't yet send a message */
126 vam->result_ready = 0; \
127 mp = vl_msg_api_alloc(sizeof(*mp)); \
128 memset (mp, 0, sizeof (*mp)); \
129 mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base); \
130 mp->client_index = vam->my_client_index; \
135 vam->result_ready = 0; \
136 mp = vl_msg_api_alloc(sizeof(*mp)+(n)); \
137 memset (mp, 0, sizeof (*mp)); \
138 mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base); \
139 mp->client_index = vam->my_client_index; \
142 /* S: send a message */
143 #define S (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp))
145 /* W: wait for results, with timeout */
148 timeout = vat_time_now (vam) + 1.0; \
150 while (vat_time_now (vam) < timeout) { \
151 if (vam->result_ready == 1) { \
152 return (vam->retval); \
159 static int api_pot_profile_add (vat_main_t *vam)
162 pot_test_main_t * sm = &pot_test_main;
163 unformat_input_t *input = vam->input;
164 vl_api_pot_profile_add_t *mp;
167 u64 secret_share = 0;
170 u64 lpc = 0, poly2 = 0;
175 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
177 if (unformat(input, "name %s", &name))
179 else if(unformat(input, "id %d", &id))
181 else if (unformat(input, "validator-key 0x%Lx", &secret_key))
183 else if (unformat(input, "prime-number 0x%Lx", &prime))
185 else if (unformat(input, "secret-share 0x%Lx", &secret_share))
187 else if (unformat(input, "polynomial-public 0x%Lx", &poly2))
189 else if (unformat(input, "lpc 0x%Lx", &lpc))
191 else if (unformat(input, "bits-in-random %u", &bits))
202 errmsg ("name required\n");
207 M2(POT_PROFILE_ADD, pot_profile_add, vec_len(name));
209 mp->list_name_len = vec_len(name);
210 clib_memcpy(mp->list_name, name, mp->list_name_len);
211 mp->secret_share = clib_host_to_net_u64(secret_share);
212 mp->polynomial_public = clib_host_to_net_u64(poly2);
213 mp->lpc = clib_host_to_net_u64(lpc);
214 mp->prime = clib_host_to_net_u64(prime);
217 mp->secret_key = clib_host_to_net_u64(secret_key);
234 static int api_pot_profile_activate (vat_main_t *vam)
237 pot_test_main_t * sm = &pot_test_main;
238 unformat_input_t *input = vam->input;
239 vl_api_pot_profile_activate_t *mp;
245 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
247 if (unformat(input, "name %s", &name))
249 else if(unformat(input, "id %d", &id))
257 errmsg ("name required\n");
262 M2(POT_PROFILE_ACTIVATE, pot_profile_activate, vec_len(name));
264 mp->list_name_len = vec_len(name);
265 clib_memcpy(mp->list_name, name, mp->list_name_len);
276 static int api_pot_profile_del (vat_main_t *vam)
278 pot_test_main_t * sm = &pot_test_main;
279 vl_api_pot_profile_del_t *mp;
282 M(POT_PROFILE_DEL, pot_profile_del);
283 mp->list_name_len = 0;
288 static int api_pot_profile_show_config_dump (vat_main_t *vam)
290 pot_test_main_t * sm = &pot_test_main;
291 unformat_input_t *input = vam->input;
292 vl_api_pot_profile_show_config_dump_t *mp;
296 while(unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
298 if(unformat(input,"id %d",&id));
302 M(POT_PROFILE_SHOW_CONFIG_DUMP, pot_profile_show_config_dump);
311 * List of messages that the api test plugin sends,
312 * and that the data plane plugin processes
314 #define foreach_vpe_api_msg \
315 _(pot_profile_add, "name <name> id [0-1] " \
316 "prime-number <0xu64> bits-in-random [0-64] " \
317 "secret-share <0xu64> lpc <0xu64> polynomial-public <0xu64> " \
318 "[validator-key <0xu64>] [validity <0xu64>]") \
319 _(pot_profile_activate, "name <name> id [0-1] ") \
320 _(pot_profile_del, "[id <nn>]") \
321 _(pot_profile_show_config_dump, "id [0-1]")
323 void vat_api_hookup (vat_main_t *vam)
325 pot_test_main_t * sm = &pot_test_main;
326 /* Hook up handlers for replies from the data plane plug-in */
328 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
330 vl_api_##n##_t_handler, \
332 vl_api_##n##_t_endian, \
333 vl_api_##n##_t_print, \
334 sizeof(vl_api_##n##_t), 1);
335 foreach_vpe_api_reply_msg;
338 /* API messages we can send */
339 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
344 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
349 clib_error_t * vat_plugin_register (vat_main_t *vam)
351 pot_test_main_t * sm = &pot_test_main;
356 name = format (0, "ioam_pot_%08x%c", api_version, 0);
357 sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
359 if (sm->msg_id_base != (u16) ~0)
360 vat_api_hookup (vam);