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 <plugins/ioam/lib-pot/pot_msg_enum.h>
30 /* define message structures */
32 #include <plugins/ioam/lib-pot/pot_all_api_h.h>
35 /* declare message handlers for each api */
37 #define vl_endianfun /* define message structures */
38 #include <plugins/ioam/lib-pot/pot_all_api_h.h>
41 /* instantiate all the print functions we know about */
42 #define vl_print(handle, ...)
44 #include <plugins/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 <plugins/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)
67 static void vl_api_##n##_t_handler \
68 (vl_api_##n##_t * mp) \
70 vat_main_t * vam = pot_test_main.vat_main; \
71 i32 retval = ntohl(mp->retval); \
72 if (vam->async_mode) { \
73 vam->async_errors += (retval < 0); \
75 vam->retval = retval; \
76 vam->result_ready = 1; \
79 foreach_standard_reply_retval_handler;
83 * Table of message reply handlers, must include boilerplate handlers
86 #define foreach_vpe_api_reply_msg \
87 _(POT_PROFILE_ADD_REPLY, pot_profile_add_reply) \
88 _(POT_PROFILE_ACTIVATE_REPLY, pot_profile_activate_reply) \
89 _(POT_PROFILE_DEL_REPLY, pot_profile_del_reply) \
92 /* M: construct, but don't yet send a message */
96 vam->result_ready = 0; \
97 mp = vl_msg_api_alloc(sizeof(*mp)); \
98 memset (mp, 0, sizeof (*mp)); \
99 mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base); \
100 mp->client_index = vam->my_client_index; \
105 vam->result_ready = 0; \
106 mp = vl_msg_api_alloc(sizeof(*mp)+(n)); \
107 memset (mp, 0, sizeof (*mp)); \
108 mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base); \
109 mp->client_index = vam->my_client_index; \
112 /* S: send a message */
113 #define S (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp))
115 /* W: wait for results, with timeout */
118 timeout = vat_time_now (vam) + 1.0; \
120 while (vat_time_now (vam) < timeout) { \
121 if (vam->result_ready == 1) { \
122 return (vam->retval); \
129 static int api_pot_profile_add (vat_main_t *vam)
132 pot_test_main_t * sm = &pot_test_main;
133 unformat_input_t *input = vam->input;
134 vl_api_pot_profile_add_t *mp;
137 u64 secret_share = 0;
140 u64 lpc = 0, poly2 = 0;
145 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
147 if (unformat(input, "name %s", &name))
149 else if(unformat(input, "id %d", &id))
151 else if (unformat(input, "validator-key 0x%Lx", &secret_key))
153 else if (unformat(input, "prime-number 0x%Lx", &prime))
155 else if (unformat(input, "secret-share 0x%Lx", &secret_share))
157 else if (unformat(input, "polynomial-public 0x%Lx", &poly2))
159 else if (unformat(input, "lpc 0x%Lx", &lpc))
161 else if (unformat(input, "bits-in-random %u", &bits))
172 errmsg ("name required\n");
177 M2(POT_PROFILE_ADD, pot_profile_add, vec_len(name));
179 mp->list_name_len = vec_len(name);
180 clib_memcpy(mp->list_name, name, mp->list_name_len);
181 mp->secret_share = clib_host_to_net_u64(secret_share);
182 mp->polynomial_public = clib_host_to_net_u64(poly2);
183 mp->lpc = clib_host_to_net_u64(lpc);
184 mp->prime = clib_host_to_net_u64(prime);
187 mp->secret_key = clib_host_to_net_u64(secret_key);
204 static int api_pot_profile_activate (vat_main_t *vam)
207 pot_test_main_t * sm = &pot_test_main;
208 unformat_input_t *input = vam->input;
209 vl_api_pot_profile_activate_t *mp;
215 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
217 if (unformat(input, "name %s", &name))
219 else if(unformat(input, "id %d", &id))
227 errmsg ("name required\n");
232 M2(POT_PROFILE_ACTIVATE, pot_profile_activate, vec_len(name));
234 mp->list_name_len = vec_len(name);
235 clib_memcpy(mp->list_name, name, mp->list_name_len);
246 static int api_pot_profile_del (vat_main_t *vam)
248 pot_test_main_t * sm = &pot_test_main;
249 vl_api_pot_profile_del_t *mp;
252 M(POT_PROFILE_DEL, pot_profile_del);
253 mp->list_name_len = 0;
259 * List of messages that the api test plugin sends,
260 * and that the data plane plugin processes
262 #define foreach_vpe_api_msg \
263 _(pot_profile_add, "name <name> id [0-1] " \
264 "prime-number <0xu64> bits-in-random [0-64] " \
265 "secret-share <0xu64> lpc <0xu64> polynomial-public <0xu64> " \
266 "[validator-key <0xu64>] [validity <0xu64>]") \
267 _(pot_profile_activate, "name <name> id [0-1] ") \
268 _(pot_profile_del, "[id <nn>]") \
271 void vat_api_hookup (vat_main_t *vam)
273 pot_test_main_t * sm = &pot_test_main;
274 /* Hook up handlers for replies from the data plane plug-in */
276 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
278 vl_api_##n##_t_handler, \
280 vl_api_##n##_t_endian, \
281 vl_api_##n##_t_print, \
282 sizeof(vl_api_##n##_t), 1);
283 foreach_vpe_api_reply_msg;
286 /* API messages we can send */
287 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
292 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
297 clib_error_t * vat_plugin_register (vat_main_t *vam)
299 pot_test_main_t * sm = &pot_test_main;
304 name = format (0, "pot_%08x%c", api_version, 0);
305 sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
307 if (sm->msg_id_base != (u16) ~0)
308 vat_api_hookup (vam);