Repair vlib API socket server
[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
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     int ret;
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, mp, 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(mp);
192     W (ret);
193     return ret;
194   
195 OUT:
196     vec_free(name);
197     return(rv);
198 }
199
200 static int api_pot_profile_activate (vat_main_t *vam)
201 {
202 #define MAX_BITS 64
203     unformat_input_t *input = vam->input;
204     vl_api_pot_profile_activate_t *mp;
205     u8 *name = NULL;
206     u8 id = 0;
207     int rv = 0;
208     int ret;
209     
210     while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
211       {
212         if (unformat(input, "name %s", &name))
213           ;
214         else if(unformat(input, "id %d", &id))
215           ;
216         else
217         break;
218       }
219
220     if (!name)
221       {
222         errmsg ("name required\n");
223         rv = -99;
224         goto OUT;
225       }
226     
227     M2(POT_PROFILE_ACTIVATE, mp, vec_len(name));
228
229     mp->list_name_len = vec_len(name);
230     clib_memcpy(mp->list_name, name, mp->list_name_len);
231     mp->id = id;
232       
233     S(mp);
234     W (ret);
235     return ret;
236   
237 OUT:
238     vec_free(name);
239     return(rv);
240 }
241
242
243 static int api_pot_profile_del (vat_main_t *vam)
244 {
245     vl_api_pot_profile_del_t *mp;
246     int ret;
247    
248     M(POT_PROFILE_DEL, mp);
249     mp->list_name_len = 0;
250     S(mp);
251     W (ret);
252     return ret;
253 }
254
255 static int api_pot_profile_show_config_dump (vat_main_t *vam)
256 {
257     unformat_input_t *input = vam->input;
258     vl_api_pot_profile_show_config_dump_t *mp;
259     u8 id = 0;
260     int ret;
261
262     while(unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
263     {
264       if(unformat(input,"id %d",&id));
265       else
266         break;
267     }
268     M(POT_PROFILE_SHOW_CONFIG_DUMP, mp);
269
270     mp->id = id;
271
272     S(mp);
273     W (ret);
274     return ret;
275 }
276
277 /* 
278  * List of messages that the api test plugin sends,
279  * and that the data plane plugin processes
280  */
281 #define foreach_vpe_api_msg \
282 _(pot_profile_add, "name <name> id [0-1] "                              \
283   "prime-number <0xu64> bits-in-random [0-64] "                         \
284   "secret-share <0xu64> lpc <0xu64> polynomial-public <0xu64> "         \
285   "[validator-key <0xu64>] [validity <0xu64>]")                         \
286 _(pot_profile_activate, "name <name> id [0-1] ")                        \
287 _(pot_profile_del, "[id <nn>]")                                         \
288 _(pot_profile_show_config_dump, "id [0-1]")
289
290 static void 
291 pot_vat_api_hookup (vat_main_t *vam)
292 {
293     pot_test_main_t * sm = &pot_test_main;
294     /* Hook up handlers for replies from the data plane plug-in */
295 #define _(N,n)                                                  \
296     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
297                            #n,                                  \
298                            vl_api_##n##_t_handler,              \
299                            vl_noop_handler,                     \
300                            vl_api_##n##_t_endian,               \
301                            vl_api_##n##_t_print,                \
302                            sizeof(vl_api_##n##_t), 1); 
303     foreach_vpe_api_reply_msg;
304 #undef _
305
306     /* API messages we can send */
307 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
308     foreach_vpe_api_msg;
309 #undef _    
310     
311     /* Help strings */
312 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
313     foreach_vpe_api_msg;
314 #undef _
315 }
316
317 clib_error_t * vat_plugin_register (vat_main_t *vam)
318 {
319   pot_test_main_t * sm = &pot_test_main;
320   u8 * name;
321
322   sm->vat_main = vam;
323
324   name = format (0, "ioam_pot_%08x%c", api_version, 0);
325   sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
326
327   if (sm->msg_id_base != (u16) ~0)
328     pot_vat_api_hookup (vam);
329   
330   vec_free(name);
331   
332   return 0;
333 }