e5af1faec1d03d84b572324ea1e927f05930667d
[vpp.git] / plugins / 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 <plugins/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 <plugins/ioam/lib-pot/pot_msg_enum.h>
32
33 /* define message structures */
34 #define vl_typedefs
35 #include <plugins/ioam/lib-pot/pot_all_api_h.h>
36 #undef vl_typedefs
37
38 /* define generated endian-swappers */
39 #define vl_endianfun
40 #include <plugins/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 <plugins/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 <plugins/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));                               \
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
100 static void vl_api_pot_profile_add_t_handler
101 (vl_api_pot_profile_add_t *mp)
102 {
103     pot_main_t * sm = &pot_main;
104     int rv = 0;
105     vl_api_pot_profile_add_reply_t * rmp;
106     u8 id;
107     pot_profile *profile = NULL;
108     u8 *name = 0;
109
110     if (mp->list_name_len)
111         name = format(0, "%s", mp->list_name);
112
113     pot_profile_list_init(name);
114     id = mp->id;
115     profile = pot_profile_find(id);
116     if (profile) {
117         rv = pot_profile_create(profile,
118                                 clib_net_to_host_u64(mp->prime),
119                                 clib_net_to_host_u64(mp->polynomial_public),
120                                 clib_net_to_host_u64(mp->lpc),
121                                 clib_net_to_host_u64(mp->secret_share));
122         if (rv != 0)
123             goto ERROROUT;
124         if (1 == mp->validator)
125           (void)pot_set_validator(profile, clib_net_to_host_u64(mp->secret_key));
126         (void)pot_profile_set_bit_mask(profile, mp->max_bits);
127     } else {
128         rv = -3;
129     }  
130  ERROROUT:
131     vec_free(name);
132     REPLY_MACRO(VL_API_POT_PROFILE_ADD_REPLY);
133 }
134
135 static void vl_api_pot_profile_activate_t_handler
136 (vl_api_pot_profile_activate_t *mp)
137 {
138     pot_main_t * sm = &pot_main;
139     int rv = 0;
140     vl_api_pot_profile_add_reply_t * rmp;
141     u8 id;
142     u8 *name = NULL;
143
144     if (mp->list_name_len)
145         name = format(0, "%s", mp->list_name);
146     if (!pot_profile_list_is_enabled(name)) {
147         rv = -1;
148     } else {
149         id = mp->id;
150         rv = pot_profile_set_active(id);
151     }
152      
153     vec_free(name);
154     REPLY_MACRO(VL_API_POT_PROFILE_ACTIVATE_REPLY);
155 }
156
157
158 static void vl_api_pot_profile_del_t_handler
159 (vl_api_pot_profile_del_t *mp)
160 {
161     pot_main_t * sm = &pot_main;
162     int rv = 0;
163     vl_api_pot_profile_del_reply_t * rmp;
164
165     clear_pot_profiles();
166
167     REPLY_MACRO(VL_API_POT_PROFILE_DEL_REPLY);
168 }
169
170
171 /* 
172  * This routine exists to convince the vlib plugin framework that
173  * we haven't accidentally copied a random .dll into the plugin directory.
174  *
175  * Also collects global variable pointers passed from the vpp engine
176  */
177
178 clib_error_t * 
179 vlib_plugin_register (vlib_main_t * vm, vnet_plugin_handoff_t * h,
180                       int from_early_init)
181 {
182   pot_main_t * sm = &pot_main;
183   clib_error_t * error = 0;
184
185   sm->vlib_main = vm;
186   sm->vnet_main = h->vnet_main;
187   return error;
188 }
189
190 /* Set up the API message handling tables */
191 static clib_error_t *
192 pot_plugin_api_hookup (vlib_main_t *vm)
193 {
194   pot_main_t * sm = &pot_main;
195 #define _(N,n)                                                  \
196     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
197                            #n,                                  \
198                            vl_api_##n##_t_handler,              \
199                            vl_noop_handler,                     \
200                            vl_api_##n##_t_endian,               \
201                            vl_api_##n##_t_print,                \
202                            sizeof(vl_api_##n##_t), 1); 
203     foreach_pot_plugin_api_msg;
204 #undef _
205
206     return 0;
207 }
208
209 static clib_error_t * pot_init (vlib_main_t * vm)
210 {
211   pot_main_t * sm = &pot_main;
212   clib_error_t * error = 0;
213   u8 * name;
214
215   bzero(sm, sizeof(pot_main));
216   (void)pot_util_init();
217   name = format (0, "pot_%08x%c", api_version, 0);
218
219   /* Ask for a correctly-sized block of API message decode slots */
220   sm->msg_id_base = vl_msg_api_get_msg_ids 
221       ((char *) name, VL_MSG_FIRST_AVAILABLE);
222
223   error = pot_plugin_api_hookup (vm);
224
225   vec_free(name);
226
227   return error;
228 }
229
230 VLIB_INIT_FUNCTION (pot_init);