Repair Doxygen build infrastructure
[vpp.git] / src / plugins / ioam / lib-pot / pot_api.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_api.c - Proof of Transit related APIs to create 
18  *             and maintain profiles
19  *------------------------------------------------------------------
20  */
21
22 #include <vnet/vnet.h>
23 #include <vnet/plugin/plugin.h>
24 #include <ioam/lib-pot/pot_util.h>
25
26 #include <vlibapi/api.h>
27 #include <vlibmemory/api.h>
28 #include <vlibsocket/api.h>
29
30 /* define 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 /* define generated endian-swappers */
39 #define vl_endianfun
40 #include <ioam/lib-pot/pot_all_api_h.h>
41 #undef vl_endianfun
42
43 /* instantiate all the print functions we know about */
44 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45 #define vl_printfun
46 #include <ioam/lib-pot/pot_all_api_h.h>
47 #undef vl_printfun
48
49 /* Get the API version number */
50 #define vl_api_version(n,v) static u32 api_version=(v);
51 #include <ioam/lib-pot/pot_all_api_h.h>
52 #undef vl_api_version
53
54 /* 
55  * A handy macro to set up a message reply.
56  * Assumes that the following variables are available:
57  * mp - pointer to request message
58  * rmp - pointer to reply message type
59  * rv - return value
60  */
61
62 #define REPLY_MACRO(t)                                          \
63 do {                                                            \
64     unix_shared_memory_queue_t * q =                            \
65     vl_api_client_index_to_input_queue (mp->client_index);      \
66     if (!q)                                                     \
67         return;                                                 \
68                                                                 \
69     rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
70     rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base);               \
71     rmp->context = mp->context;                                 \
72     rmp->retval = ntohl(rv);                                    \
73                                                                 \
74     vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
75 } while(0);
76
77 #define REPLY_MACRO2(t, body)                                   \
78 do {                                                            \
79     unix_shared_memory_queue_t * q;                             \
80     rv = vl_msg_api_pd_handler (mp, rv);                        \
81     q = vl_api_client_index_to_input_queue (mp->client_index);  \
82     if (!q)                                                     \
83         return;                                                 \
84                                                                 \
85     rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
86     rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base);               \
87     rmp->context = mp->context;                                 \
88     rmp->retval = ntohl(rv);                                    \
89     do {body;} while (0);                                       \
90     vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
91 } while(0);
92
93 /* List of message types that this plugin understands */
94
95 #define foreach_pot_plugin_api_msg                                      \
96 _(POT_PROFILE_ADD, pot_profile_add)                                     \
97 _(POT_PROFILE_ACTIVATE, pot_profile_activate)                           \
98 _(POT_PROFILE_DEL, pot_profile_del)                                     \
99 _(POT_PROFILE_SHOW_CONFIG_DUMP, pot_profile_show_config_dump)                                     \
100
101 static void vl_api_pot_profile_add_t_handler
102 (vl_api_pot_profile_add_t *mp)
103 {
104     pot_main_t * sm = &pot_main;
105     int rv = 0;
106     vl_api_pot_profile_add_reply_t * rmp;
107     u8 id;
108     pot_profile *profile = NULL;
109     u8 *name = 0;
110
111     if (mp->list_name_len)
112         name = format(0, "%s", mp->list_name);
113
114     pot_profile_list_init(name);
115     id = mp->id;
116     profile = pot_profile_find(id);
117     if (profile) {
118         rv = pot_profile_create(profile,
119                                 clib_net_to_host_u64(mp->prime),
120                                 clib_net_to_host_u64(mp->polynomial_public),
121                                 clib_net_to_host_u64(mp->lpc),
122                                 clib_net_to_host_u64(mp->secret_share));
123         if (rv != 0)
124             goto ERROROUT;
125         if (1 == mp->validator)
126           (void)pot_set_validator(profile, clib_net_to_host_u64(mp->secret_key));
127         (void)pot_profile_set_bit_mask(profile, mp->max_bits);
128     } else {
129         rv = -3;
130     }  
131  ERROROUT:
132     vec_free(name);
133     REPLY_MACRO(VL_API_POT_PROFILE_ADD_REPLY);
134 }
135
136 static void send_pot_profile_details(vl_api_pot_profile_show_config_dump_t *mp, u8 id)
137 {
138     vl_api_pot_profile_show_config_details_t * rmp;
139     pot_main_t * sm = &pot_main;
140     pot_profile *profile = pot_profile_find(id);
141     int rv = 0;
142     if(profile){
143         REPLY_MACRO2(VL_API_POT_PROFILE_SHOW_CONFIG_DETAILS,
144                         rmp->id=id;
145                         rmp->validator=profile->validator;
146                         rmp->secret_key=clib_host_to_net_u64(profile->secret_key);
147                         rmp->secret_share=clib_host_to_net_u64(profile->secret_share);
148                         rmp->prime=clib_host_to_net_u64(profile->prime);
149                         rmp->bit_mask=clib_host_to_net_u64(profile->bit_mask);
150                         rmp->lpc=clib_host_to_net_u64(profile->lpc);
151                         rmp->polynomial_public=clib_host_to_net_u64(profile->poly_pre_eval);
152                         );
153     }
154     else{
155         REPLY_MACRO2(VL_API_POT_PROFILE_SHOW_CONFIG_DETAILS,
156                         rmp->id=id;
157                         rmp->validator=0;
158                         rmp->secret_key=0;
159                         rmp->secret_share=0;
160                         rmp->prime=0;
161                         rmp->bit_mask=0;
162                         rmp->lpc=0;
163                         rmp->polynomial_public=0;
164                         );
165     }
166 }
167
168 static void vl_api_pot_profile_show_config_dump_t_handler
169 (vl_api_pot_profile_show_config_dump_t *mp)
170 {
171     u8 id = mp->id;
172     u8 dump_call_id = ~0;
173     if(dump_call_id==id){
174         for(id=0;id<MAX_POT_PROFILES;id++)
175             send_pot_profile_details(mp,id);
176     }
177     else
178         send_pot_profile_details(mp,id);
179 }
180
181 static void vl_api_pot_profile_activate_t_handler
182 (vl_api_pot_profile_activate_t *mp)
183 {
184     pot_main_t * sm = &pot_main;
185     int rv = 0;
186     vl_api_pot_profile_add_reply_t * rmp;
187     u8 id;
188     u8 *name = NULL;
189
190     if (mp->list_name_len)
191         name = format(0, "%s", mp->list_name);
192     if (!pot_profile_list_is_enabled(name)) {
193         rv = -1;
194     } else {
195         id = mp->id;
196         rv = pot_profile_set_active(id);
197     }
198      
199     vec_free(name);
200     REPLY_MACRO(VL_API_POT_PROFILE_ACTIVATE_REPLY);
201 }
202
203
204 static void vl_api_pot_profile_del_t_handler
205 (vl_api_pot_profile_del_t *mp)
206 {
207     pot_main_t * sm = &pot_main;
208     int rv = 0;
209     vl_api_pot_profile_del_reply_t * rmp;
210
211     clear_pot_profiles();
212
213     REPLY_MACRO(VL_API_POT_PROFILE_DEL_REPLY);
214 }
215
216
217 /* 
218  * This routine exists to convince the vlib plugin framework that
219  * we haven't accidentally copied a random .dll into the plugin directory.
220  *
221  * Also collects global variable pointers passed from the vpp engine
222  */
223
224 clib_error_t * 
225 vlib_plugin_register (vlib_main_t * vm, vnet_plugin_handoff_t * h,
226                       int from_early_init)
227 {
228   pot_main_t * sm = &pot_main;
229   clib_error_t * error = 0;
230
231   sm->vlib_main = vm;
232   sm->vnet_main = h->vnet_main;
233   return error;
234 }
235
236 /* Set up the API message handling tables */
237 static clib_error_t *
238 pot_plugin_api_hookup (vlib_main_t *vm)
239 {
240   pot_main_t * sm = &pot_main;
241 #define _(N,n)                                                  \
242     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
243                            #n,                                  \
244                            vl_api_##n##_t_handler,              \
245                            vl_noop_handler,                     \
246                            vl_api_##n##_t_endian,               \
247                            vl_api_##n##_t_print,                \
248                            sizeof(vl_api_##n##_t), 1); 
249     foreach_pot_plugin_api_msg;
250 #undef _
251
252     return 0;
253 }
254
255 #define vl_msg_name_crc_list
256 #include <ioam/lib-pot/pot_all_api_h.h>
257 #undef vl_msg_name_crc_list
258
259 static void
260 setup_message_id_table (pot_main_t * sm, api_main_t * am)
261 {
262 #define _(id,n,crc) \
263   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
264   foreach_vl_msg_name_crc_pot;
265 #undef _
266 }
267
268 static clib_error_t * pot_init (vlib_main_t * vm)
269 {
270   pot_main_t * sm = &pot_main;
271   clib_error_t * error = 0;
272   u8 * name;
273
274   bzero(sm, sizeof(pot_main));
275   (void)pot_util_init();
276   name = format (0, "ioam_pot_%08x%c", api_version, 0);
277
278   /* Ask for a correctly-sized block of API message decode slots */
279   sm->msg_id_base = vl_msg_api_get_msg_ids 
280       ((char *) name, VL_MSG_FIRST_AVAILABLE);
281
282   error = pot_plugin_api_hookup (vm);
283
284   /* Add our API messages to the global name_crc hash table */
285   setup_message_id_table (sm, &api_main);
286
287   vec_free(name);
288
289   return error;
290 }
291
292 VLIB_INIT_FUNCTION (pot_init);