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