Repair plugin binary API message numbering
[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     f64 timeout;
136     u8 id = 0;
137     int rv = 0;
138
139     while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
140       {
141         if (unformat(input, "name %s", &name))
142           ;
143         else if(unformat(input, "id %d", &id))
144           ;
145         else if (unformat(input, "validator-key 0x%Lx", &secret_key))
146           ;
147         else if (unformat(input, "prime-number 0x%Lx", &prime))
148           ;
149         else if (unformat(input, "secret-share 0x%Lx", &secret_share))
150           ;
151         else if (unformat(input, "polynomial-public 0x%Lx", &poly2))
152           ;
153         else if (unformat(input, "lpc 0x%Lx", &lpc))
154           ;
155         else if (unformat(input, "bits-in-random %u", &bits))
156           {
157             if (bits > MAX_BITS)
158               bits = MAX_BITS;
159           }
160         else
161         break;
162       }
163
164     if (!name)
165       {
166         errmsg ("name required\n");
167         rv = -99;
168         goto OUT;
169       }
170     
171     M2(POT_PROFILE_ADD, pot_profile_add, vec_len(name));
172
173     mp->list_name_len = vec_len(name);
174     clib_memcpy(mp->list_name, name, mp->list_name_len);
175     mp->secret_share = clib_host_to_net_u64(secret_share);
176     mp->polynomial_public = clib_host_to_net_u64(poly2);
177     mp->lpc = clib_host_to_net_u64(lpc);
178     mp->prime = clib_host_to_net_u64(prime);
179     if (secret_key != 0)
180       {
181         mp->secret_key = clib_host_to_net_u64(secret_key);
182         mp->validator = 1;
183       }
184     else
185       {
186         mp->validator = 0;
187       }
188     mp->id = id;
189     mp->max_bits = bits;
190       
191     S; 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     f64 timeout;
207     
208     while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
209       {
210         if (unformat(input, "name %s", &name))
211           ;
212         else if(unformat(input, "id %d", &id))
213           ;
214         else
215         break;
216       }
217
218     if (!name)
219       {
220         errmsg ("name required\n");
221         rv = -99;
222         goto OUT;
223       }
224     
225     M2(POT_PROFILE_ACTIVATE, pot_profile_activate, vec_len(name));
226
227     mp->list_name_len = vec_len(name);
228     clib_memcpy(mp->list_name, name, mp->list_name_len);
229     mp->id = id;
230       
231     S; 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     f64 timeout;
243    
244     M(POT_PROFILE_DEL, pot_profile_del);
245     mp->list_name_len = 0;
246     S; 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     f64 timeout;
255     u8 id = 0;
256
257     while(unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
258     {
259       if(unformat(input,"id %d",&id));
260       else
261         break;
262     }
263     M(POT_PROFILE_SHOW_CONFIG_DUMP, pot_profile_show_config_dump);
264
265     mp->id = id;
266
267     S; 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 }