2e87023896e0877a8f6b70537464a7249c6a63a2
[vpp.git] / plugins / ioam-plugin / ioam / lib-pot / pot_test.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 /*
16  *------------------------------------------------------------------
17  * pot_test.c - test harness for pot plugin
18  *------------------------------------------------------------------
19  */
20
21 #include <vat/vat.h>
22 #include <vlibapi/api.h>
23 #include <vlibmemory/api.h>
24 #include <vlibsocket/api.h>
25 #include <vppinfra/error.h>
26
27 /* Declare message IDs */
28 #include <ioam/lib-pot/pot_msg_enum.h>
29
30 /* define message structures */
31 #define vl_typedefs
32 #include <ioam/lib-pot/pot_all_api_h.h>
33 #undef vl_typedefs
34
35 /* declare message handlers for each api */
36
37 #define vl_endianfun             /* define message structures */
38 #include <ioam/lib-pot/pot_all_api_h.h>
39 #undef vl_endianfun
40
41 /* instantiate all the print functions we know about */
42 #define vl_print(handle, ...)
43 #define vl_printfun
44 #include <ioam/lib-pot/pot_all_api_h.h>
45 #undef vl_printfun
46
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>
50 #undef vl_api_version
51
52
53 typedef struct {
54     /* API message ID base */
55     u16 msg_id_base;
56     vat_main_t *vat_main;
57 } pot_test_main_t;
58
59 pot_test_main_t pot_test_main;
60
61 #define foreach_standard_reply_retval_handler     \
62 _(pot_profile_add_reply)                          \
63 _(pot_profile_activate_reply)                     \
64 _(pot_profile_del_reply)
65
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)); \
76                 )
77
78 #define _(n)                                            \
79     static void vl_api_##n##_t_handler                  \
80     (vl_api_##n##_t * mp)                               \
81     {                                                   \
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);          \
86         } else {                                        \
87             vam->retval = retval;                       \
88             vam->result_ready = 1;                      \
89         }                                               \
90     }
91 foreach_standard_reply_retval_handler;
92 #undef _
93
94 #define _(n,body)                                       \
95     static void vl_api_##n##_t_handler                  \
96     (vl_api_##n##_t * mp)                               \
97     {                                                   \
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);          \
102         } else {                                        \
103             vam->retval = retval;                       \
104             vam->result_ready = 1;                      \
105         }                                               \
106         do{body;}while(0);                              \
107     }
108 foreach_custom_reply_retval_handler;
109 #undef _
110
111 /* 
112  * Table of message reply handlers, must include boilerplate handlers
113  * we just generated
114  */
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)
120
121
122 /* M: construct, but don't yet send a message */
123
124 #define M(T,t)                                                  \
125 do {                                                            \
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;                    \
131 } while(0);
132
133 #define M2(T,t,n)                                               \
134 do {                                                            \
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;                    \
140 } while(0);
141
142 /* S: send a message */
143 #define S (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp))
144
145 /* W: wait for results, with timeout */
146 #define W                                       \
147 do {                                            \
148     timeout = vat_time_now (vam) + 1.0;         \
149                                                 \
150     while (vat_time_now (vam) < timeout) {      \
151         if (vam->result_ready == 1) {           \
152             return (vam->retval);               \
153         }                                       \
154     }                                           \
155     return -99;                                 \
156 } while(0);
157
158
159 static int api_pot_profile_add (vat_main_t *vam)
160 {
161 #define MAX_BITS 64
162     pot_test_main_t * sm = &pot_test_main;
163     unformat_input_t *input = vam->input;
164     vl_api_pot_profile_add_t *mp;
165     u8 *name = NULL;
166     u64 prime = 0;
167     u64 secret_share = 0;
168     u64 secret_key = 0;
169     u32  bits = MAX_BITS;
170     u64 lpc = 0, poly2 = 0;
171     f64 timeout;
172     u8 id = 0;
173     int rv = 0;
174
175     while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
176       {
177         if (unformat(input, "name %s", &name))
178           ;
179         else if(unformat(input, "id %d", &id))
180           ;
181         else if (unformat(input, "validator-key 0x%Lx", &secret_key))
182           ;
183         else if (unformat(input, "prime-number 0x%Lx", &prime))
184           ;
185         else if (unformat(input, "secret-share 0x%Lx", &secret_share))
186           ;
187         else if (unformat(input, "polynomial-public 0x%Lx", &poly2))
188           ;
189         else if (unformat(input, "lpc 0x%Lx", &lpc))
190           ;
191         else if (unformat(input, "bits-in-random %u", &bits))
192           {
193             if (bits > MAX_BITS)
194               bits = MAX_BITS;
195           }
196         else
197         break;
198       }
199
200     if (!name)
201       {
202         errmsg ("name required\n");
203         rv = -99;
204         goto OUT;
205       }
206     
207     M2(POT_PROFILE_ADD, pot_profile_add, vec_len(name));
208
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);
215     if (secret_key != 0)
216       {
217         mp->secret_key = clib_host_to_net_u64(secret_key);
218         mp->validator = 1;
219       }
220     else
221       {
222         mp->validator = 0;
223       }
224     mp->id = id;
225     mp->max_bits = bits;
226       
227     S; W;
228   
229 OUT:
230     vec_free(name);
231     return(rv);
232 }
233
234 static int api_pot_profile_activate (vat_main_t *vam)
235 {
236 #define MAX_BITS 64
237     pot_test_main_t * sm = &pot_test_main;  
238     unformat_input_t *input = vam->input;
239     vl_api_pot_profile_activate_t *mp;
240     u8 *name = NULL;
241     u8 id = 0;
242     int rv = 0;
243     f64 timeout;
244     
245     while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
246       {
247         if (unformat(input, "name %s", &name))
248           ;
249         else if(unformat(input, "id %d", &id))
250           ;
251         else
252         break;
253       }
254
255     if (!name)
256       {
257         errmsg ("name required\n");
258         rv = -99;
259         goto OUT;
260       }
261     
262     M2(POT_PROFILE_ACTIVATE, pot_profile_activate, vec_len(name));
263
264     mp->list_name_len = vec_len(name);
265     clib_memcpy(mp->list_name, name, mp->list_name_len);
266     mp->id = id;
267       
268     S; W;
269   
270 OUT:
271     vec_free(name);
272     return(rv);
273 }
274
275
276 static int api_pot_profile_del (vat_main_t *vam)
277 {
278     pot_test_main_t * sm = &pot_test_main;
279     vl_api_pot_profile_del_t *mp;
280     f64 timeout;
281    
282     M(POT_PROFILE_DEL, pot_profile_del);
283     mp->list_name_len = 0;
284     S; W;
285     return 0;
286 }
287
288 static int api_pot_profile_show_config_dump (vat_main_t *vam)
289 {
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;
293     f64 timeout;
294     u8 id = 0;
295
296     while(unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
297     {
298       if(unformat(input,"id %d",&id));
299       else
300         break;
301     }
302     M(POT_PROFILE_SHOW_CONFIG_DUMP, pot_profile_show_config_dump);
303
304     mp->id = id;
305
306     S; W;
307     return 0;
308 }
309
310 /* 
311  * List of messages that the api test plugin sends,
312  * and that the data plane plugin processes
313  */
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]")
322
323 void vat_api_hookup (vat_main_t *vam)
324 {
325     pot_test_main_t * sm = &pot_test_main;
326     /* Hook up handlers for replies from the data plane plug-in */
327 #define _(N,n)                                                  \
328     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
329                            #n,                                  \
330                            vl_api_##n##_t_handler,              \
331                            vl_noop_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;
336 #undef _
337
338     /* API messages we can send */
339 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
340     foreach_vpe_api_msg;
341 #undef _    
342     
343     /* Help strings */
344 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
345     foreach_vpe_api_msg;
346 #undef _
347 }
348
349 clib_error_t * vat_plugin_register (vat_main_t *vam)
350 {
351   pot_test_main_t * sm = &pot_test_main;
352   u8 * name;
353
354   sm->vat_main = vam;
355
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);
358
359   if (sm->msg_id_base != (u16) ~0)
360     vat_api_hookup (vam);
361   
362   vec_free(name);
363   
364   return 0;
365 }