cc1b7b76a786c966494250b8cd9e77b38ad1f85d
[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 #define REPLY_MSG_ID_BASE sm->msg_id_base
55 #include <vlibapi/api_helper_macros.h>
56
57 /* List of message types that this plugin understands */
58 #define foreach_pot_plugin_api_msg                                      \
59 _(POT_PROFILE_ADD, pot_profile_add)                                     \
60 _(POT_PROFILE_ACTIVATE, pot_profile_activate)                           \
61 _(POT_PROFILE_DEL, pot_profile_del)                                     \
62 _(POT_PROFILE_SHOW_CONFIG_DUMP, pot_profile_show_config_dump)                                     \
63
64 static void vl_api_pot_profile_add_t_handler
65 (vl_api_pot_profile_add_t *mp)
66 {
67     pot_main_t * sm = &pot_main;
68     int rv = 0;
69     vl_api_pot_profile_add_reply_t * rmp;
70     u8 id;
71     pot_profile *profile = NULL;
72     u8 *name = 0;
73
74     if (mp->list_name_len)
75         name = format(0, "%s", mp->list_name);
76
77     pot_profile_list_init(name);
78     id = mp->id;
79     profile = pot_profile_find(id);
80     if (profile) {
81         rv = pot_profile_create(profile,
82                                 clib_net_to_host_u64(mp->prime),
83                                 clib_net_to_host_u64(mp->polynomial_public),
84                                 clib_net_to_host_u64(mp->lpc),
85                                 clib_net_to_host_u64(mp->secret_share));
86         if (rv != 0)
87             goto ERROROUT;
88         if (1 == mp->validator)
89           (void)pot_set_validator(profile, clib_net_to_host_u64(mp->secret_key));
90         (void)pot_profile_set_bit_mask(profile, mp->max_bits);
91     } else {
92         rv = -3;
93     }  
94  ERROROUT:
95     vec_free(name);
96     REPLY_MACRO(VL_API_POT_PROFILE_ADD_REPLY);
97 }
98
99 static void send_pot_profile_details(vl_api_pot_profile_show_config_dump_t *mp, u8 id)
100 {
101     vl_api_pot_profile_show_config_details_t * rmp;
102     pot_main_t * sm = &pot_main;
103     pot_profile *profile = pot_profile_find(id);
104     int rv = 0;
105     if(profile){
106         REPLY_MACRO2(VL_API_POT_PROFILE_SHOW_CONFIG_DETAILS,
107                         rmp->id=id;
108                         rmp->validator=profile->validator;
109                         rmp->secret_key=clib_host_to_net_u64(profile->secret_key);
110                         rmp->secret_share=clib_host_to_net_u64(profile->secret_share);
111                         rmp->prime=clib_host_to_net_u64(profile->prime);
112                         rmp->bit_mask=clib_host_to_net_u64(profile->bit_mask);
113                         rmp->lpc=clib_host_to_net_u64(profile->lpc);
114                         rmp->polynomial_public=clib_host_to_net_u64(profile->poly_pre_eval);
115                         );
116     }
117     else{
118         REPLY_MACRO2(VL_API_POT_PROFILE_SHOW_CONFIG_DETAILS,
119                         rmp->id=id;
120                         rmp->validator=0;
121                         rmp->secret_key=0;
122                         rmp->secret_share=0;
123                         rmp->prime=0;
124                         rmp->bit_mask=0;
125                         rmp->lpc=0;
126                         rmp->polynomial_public=0;
127                         );
128     }
129 }
130
131 static void vl_api_pot_profile_show_config_dump_t_handler
132 (vl_api_pot_profile_show_config_dump_t *mp)
133 {
134     u8 id = mp->id;
135     u8 dump_call_id = ~0;
136     if(dump_call_id==id){
137         for(id=0;id<MAX_POT_PROFILES;id++)
138             send_pot_profile_details(mp,id);
139     }
140     else
141         send_pot_profile_details(mp,id);
142 }
143
144 static void vl_api_pot_profile_activate_t_handler
145 (vl_api_pot_profile_activate_t *mp)
146 {
147     pot_main_t * sm = &pot_main;
148     int rv = 0;
149     vl_api_pot_profile_add_reply_t * rmp;
150     u8 id;
151     u8 *name = NULL;
152
153     if (mp->list_name_len)
154         name = format(0, "%s", mp->list_name);
155     if (!pot_profile_list_is_enabled(name)) {
156         rv = -1;
157     } else {
158         id = mp->id;
159         rv = pot_profile_set_active(id);
160     }
161      
162     vec_free(name);
163     REPLY_MACRO(VL_API_POT_PROFILE_ACTIVATE_REPLY);
164 }
165
166
167 static void vl_api_pot_profile_del_t_handler
168 (vl_api_pot_profile_del_t *mp)
169 {
170     pot_main_t * sm = &pot_main;
171     int rv = 0;
172     vl_api_pot_profile_del_reply_t * rmp;
173
174     clear_pot_profiles();
175
176     REPLY_MACRO(VL_API_POT_PROFILE_DEL_REPLY);
177 }
178
179 /* Set up the API message handling tables */
180 static clib_error_t *
181 pot_plugin_api_hookup (vlib_main_t *vm)
182 {
183   pot_main_t * sm = &pot_main;
184 #define _(N,n)                                                  \
185     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
186                            #n,                                  \
187                            vl_api_##n##_t_handler,              \
188                            vl_noop_handler,                     \
189                            vl_api_##n##_t_endian,               \
190                            vl_api_##n##_t_print,                \
191                            sizeof(vl_api_##n##_t), 1); 
192     foreach_pot_plugin_api_msg;
193 #undef _
194
195     return 0;
196 }
197
198 #define vl_msg_name_crc_list
199 #include <ioam/lib-pot/pot_all_api_h.h>
200 #undef vl_msg_name_crc_list
201
202 static void
203 setup_message_id_table (pot_main_t * sm, api_main_t * am)
204 {
205 #define _(id,n,crc) \
206   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
207   foreach_vl_msg_name_crc_pot;
208 #undef _
209 }
210
211 static clib_error_t * pot_init (vlib_main_t * vm)
212 {
213   pot_main_t * sm = &pot_main;
214   clib_error_t * error = 0;
215   u8 * name;
216
217   bzero(sm, sizeof(pot_main));
218   (void)pot_util_init();
219
220   sm->vlib_main = vm;
221   sm->vnet_main = vnet_get_main();
222
223   name = format (0, "ioam_pot_%08x%c", api_version, 0);
224
225   /* Ask for a correctly-sized block of API message decode slots */
226   sm->msg_id_base = vl_msg_api_get_msg_ids 
227       ((char *) name, VL_MSG_FIRST_AVAILABLE);
228
229   error = pot_plugin_api_hookup (vm);
230
231   /* Add our API messages to the global name_crc hash table */
232   setup_message_id_table (sm, &api_main);
233
234   vec_free(name);
235
236   return error;
237 }
238
239 VLIB_INIT_FUNCTION (pot_init);