Localize the timeout variable within the W message macro.
[vpp.git] / src / plugins / 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 #define __plugin_msg_base pot_test_main.msg_id_base
28 #include <vlibapi/vat_helper_macros.h>
29
30 /* Declare message IDs */
31 #include <ioam/lib-pot/pot_msg_enum.h>
32
33 /* define message structures */
34 #define vl_typedefs
35 #include <ioam/lib-pot/pot_all_api_h.h>
36 #undef vl_typedefs
37
38 /* declare message handlers for each api */
39
40 #define vl_endianfun             /* define message structures */
41 #include <ioam/lib-pot/pot_all_api_h.h>
42 #undef vl_endianfun
43
44 /* instantiate all the print functions we know about */
45 #define vl_print(handle, ...)
46 #define vl_printfun
47 #include <ioam/lib-pot/pot_all_api_h.h>
48 #undef vl_printfun
49
50 /* Get the API version number. */
51 #define vl_api_version(n,v) static u32 api_version=(v);
52 #include <ioam/lib-pot/pot_all_api_h.h>
53 #undef vl_api_version
54
55
56 typedef struct {
57     /* API message ID base */
58     u16 msg_id_base;
59     vat_main_t *vat_main;
60 } pot_test_main_t;
61
62 pot_test_main_t pot_test_main;
63
64 #define foreach_standard_reply_retval_handler     \
65 _(pot_profile_add_reply)                          \
66 _(pot_profile_activate_reply)                     \
67 _(pot_profile_del_reply)
68
69 #define foreach_custom_reply_retval_handler                     \
70 _(pot_profile_show_config_details,                              \
71     errmsg("            ID:%d\n",mp->id);                       \
72     errmsg("     Validator:%d\n",mp->validator);                \
73     errmsg("    secret_key:%Lx\n",clib_net_to_host_u64(mp->secret_key));                \
74     errmsg("  secret_share:%Lx\n",clib_net_to_host_u64(mp->secret_share));              \
75     errmsg("         prime:%Lx\n",clib_net_to_host_u64(mp->prime));                     \
76     errmsg("       bitmask:%Lx\n",clib_net_to_host_u64(mp->bit_mask));          \
77     errmsg("           lpc:%Lx\n",clib_net_to_host_u64(mp->lpc));                       \
78     errmsg("   public poly:%Lx\n",clib_net_to_host_u64(mp->polynomial_public)); \
79                 )
80
81 #define _(n)                                            \
82     static void vl_api_##n##_t_handler                  \
83     (vl_api_##n##_t * mp)                               \
84     {                                                   \
85         vat_main_t * vam = pot_test_main.vat_main;   \
86         i32 retval = ntohl(mp->retval);                 \
87         if (vam->async_mode) {                          \
88             vam->async_errors += (retval < 0);          \
89         } else {                                        \
90             vam->retval = retval;                       \
91             vam->result_ready = 1;                      \
92         }                                               \
93     }
94 foreach_standard_reply_retval_handler;
95 #undef _
96
97 #define _(n,body)                                       \
98     static void vl_api_##n##_t_handler                  \
99     (vl_api_##n##_t * mp)                               \
100     {                                                   \
101         vat_main_t * vam = pot_test_main.vat_main;   \
102         i32 retval = ntohl(mp->retval);                 \
103         if (vam->async_mode) {                          \
104             vam->async_errors += (retval < 0);          \
105         } else {                                        \
106             vam->retval = retval;                       \
107             vam->result_ready = 1;                      \
108         }                                               \
109         do{body;}while(0);                              \
110     }
111 foreach_custom_reply_retval_handler;
112 #undef _
113
114 /* 
115  * Table of message reply handlers, must include boilerplate handlers
116  * we just generated
117  */
118 #define foreach_vpe_api_reply_msg                                       \
119 _(POT_PROFILE_ADD_REPLY, pot_profile_add_reply)                         \
120 _(POT_PROFILE_ACTIVATE_REPLY, pot_profile_activate_reply)               \
121 _(POT_PROFILE_DEL_REPLY, pot_profile_del_reply)                         \
122 _(POT_PROFILE_SHOW_CONFIG_DETAILS, pot_profile_show_config_details)
123
124 static int api_pot_profile_add (vat_main_t *vam)
125 {
126 #define MAX_BITS 64
127     unformat_input_t *input = vam->input;
128     vl_api_pot_profile_add_t *mp;
129     u8 *name = NULL;
130     u64 prime = 0;
131     u64 secret_share = 0;
132     u64 secret_key = 0;
133     u32  bits = MAX_BITS;
134     u64 lpc = 0, poly2 = 0;
135     u8 id = 0;
136     int rv = 0;
137
138     while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
139       {
140         if (unformat(input, "name %s", &name))
141           ;
142         else if(unformat(input, "id %d", &id))
143           ;
144         else if (unformat(input, "validator-key 0x%Lx", &secret_key))
145           ;
146         else if (unformat(input, "prime-number 0x%Lx", &prime))
147           ;
148         else if (unformat(input, "secret-share 0x%Lx", &secret_share))
149           ;
150         else if (unformat(input, "polynomial-public 0x%Lx", &poly2))
151           ;
152         else if (unformat(input, "lpc 0x%Lx", &lpc))
153           ;
154         else if (unformat(input, "bits-in-random %u", &bits))
155           {
156             if (bits > MAX_BITS)
157               bits = MAX_BITS;
158           }
159         else
160         break;
161       }
162
163     if (!name)
164       {
165         errmsg ("name required\n");
166         rv = -99;
167         goto OUT;
168       }
169     
170     M2(POT_PROFILE_ADD, mp, vec_len(name));
171
172     mp->list_name_len = vec_len(name);
173     clib_memcpy(mp->list_name, name, mp->list_name_len);
174     mp->secret_share = clib_host_to_net_u64(secret_share);
175     mp->polynomial_public = clib_host_to_net_u64(poly2);
176     mp->lpc = clib_host_to_net_u64(lpc);
177     mp->prime = clib_host_to_net_u64(prime);
178     if (secret_key != 0)
179       {
180         mp->secret_key = clib_host_to_net_u64(secret_key);
181         mp->validator = 1;
182       }
183     else
184       {
185         mp->validator = 0;
186       }
187     mp->id = id;
188     mp->max_bits = bits;
189       
190     S(mp);
191     W;
192   
193 OUT:
194     vec_free(name);
195     return(rv);
196 }
197
198 static int api_pot_profile_activate (vat_main_t *vam)
199 {
200 #define MAX_BITS 64
201     unformat_input_t *input = vam->input;
202     vl_api_pot_profile_activate_t *mp;
203     u8 *name = NULL;
204     u8 id = 0;
205     int rv = 0;
206     
207     while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
208       {
209         if (unformat(input, "name %s", &name))
210           ;
211         else if(unformat(input, "id %d", &id))
212           ;
213         else
214         break;
215       }
216
217     if (!name)
218       {
219         errmsg ("name required\n");
220         rv = -99;
221         goto OUT;
222       }
223     
224     M2(POT_PROFILE_ACTIVATE, mp, vec_len(name));
225
226     mp->list_name_len = vec_len(name);
227     clib_memcpy(mp->list_name, name, mp->list_name_len);
228     mp->id = id;
229       
230     S(mp);
231     W;
232   
233 OUT:
234     vec_free(name);
235     return(rv);
236 }
237
238
239 static int api_pot_profile_del (vat_main_t *vam)
240 {
241     vl_api_pot_profile_del_t *mp;
242    
243     M(POT_PROFILE_DEL, mp);
244     mp->list_name_len = 0;
245     S(mp);
246     W;
247     return 0;
248 }
249
250 static int api_pot_profile_show_config_dump (vat_main_t *vam)
251 {
252     unformat_input_t *input = vam->input;
253     vl_api_pot_profile_show_config_dump_t *mp;
254     u8 id = 0;
255
256     while(unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
257     {
258       if(unformat(input,"id %d",&id));
259       else
260         break;
261     }
262     M(POT_PROFILE_SHOW_CONFIG_DUMP, mp);
263
264     mp->id = id;
265
266     S(mp);
267     W;
268     return 0;
269 }
270
271 /* 
272  * List of messages that the api test plugin sends,
273  * and that the data plane plugin processes
274  */
275 #define foreach_vpe_api_msg \
276 _(pot_profile_add, "name <name> id [0-1] "                              \
277   "prime-number <0xu64> bits-in-random [0-64] "                         \
278   "secret-share <0xu64> lpc <0xu64> polynomial-public <0xu64> "         \
279   "[validator-key <0xu64>] [validity <0xu64>]")                         \
280 _(pot_profile_activate, "name <name> id [0-1] ")                        \
281 _(pot_profile_del, "[id <nn>]")                                         \
282 _(pot_profile_show_config_dump, "id [0-1]")
283
284 static void 
285 pot_vat_api_hookup (vat_main_t *vam)
286 {
287     pot_test_main_t * sm = &pot_test_main;
288     /* Hook up handlers for replies from the data plane plug-in */
289 #define _(N,n)                                                  \
290     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
291                            #n,                                  \
292                            vl_api_##n##_t_handler,              \
293                            vl_noop_handler,                     \
294                            vl_api_##n##_t_endian,               \
295                            vl_api_##n##_t_print,                \
296                            sizeof(vl_api_##n##_t), 1); 
297     foreach_vpe_api_reply_msg;
298 #undef _
299
300     /* API messages we can send */
301 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
302     foreach_vpe_api_msg;
303 #undef _    
304     
305     /* Help strings */
306 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
307     foreach_vpe_api_msg;
308 #undef _
309 }
310
311 clib_error_t * vat_plugin_register (vat_main_t *vam)
312 {
313   pot_test_main_t * sm = &pot_test_main;
314   u8 * name;
315
316   sm->vat_main = vam;
317
318   name = format (0, "ioam_pot_%08x%c", api_version, 0);
319   sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
320
321   if (sm->msg_id_base != (u16) ~0)
322     pot_vat_api_hookup (vam);
323   
324   vec_free(name);
325   
326   return 0;
327 }