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