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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 *------------------------------------------------------------------
17 * pot_api.c - Proof of Transit related APIs to create
18 * and maintain profiles
19 *------------------------------------------------------------------
22 #include <vnet/vnet.h>
23 #include <vnet/plugin/plugin.h>
24 #include <ioam/lib-pot/pot_util.h>
26 #include <vlibapi/api.h>
27 #include <vlibmemory/api.h>
28 #include <vlibsocket/api.h>
30 /* define message IDs */
31 #include <ioam/lib-pot/pot_msg_enum.h>
33 /* define message structures */
35 #include <ioam/lib-pot/pot_all_api_h.h>
38 /* define generated endian-swappers */
40 #include <ioam/lib-pot/pot_all_api_h.h>
43 /* instantiate all the print functions we know about */
44 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
46 #include <ioam/lib-pot/pot_all_api_h.h>
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>
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
62 #define REPLY_MACRO(t) \
64 unix_shared_memory_queue_t * q = \
65 vl_api_client_index_to_input_queue (mp->client_index); \
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); \
74 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
77 #define REPLY_MACRO2(t, body) \
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); \
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); \
93 /* List of message types that this plugin understands */
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) \
101 static void vl_api_pot_profile_add_t_handler
102 (vl_api_pot_profile_add_t *mp)
104 pot_main_t * sm = &pot_main;
106 vl_api_pot_profile_add_reply_t * rmp;
108 pot_profile *profile = NULL;
111 if (mp->list_name_len)
112 name = format(0, "%s", mp->list_name);
114 pot_profile_list_init(name);
116 profile = pot_profile_find(id);
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));
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);
133 REPLY_MACRO(VL_API_POT_PROFILE_ADD_REPLY);
136 static void send_pot_profile_details(vl_api_pot_profile_show_config_dump_t *mp, u8 id)
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);
143 REPLY_MACRO2(VL_API_POT_PROFILE_SHOW_CONFIG_DETAILS,
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);
155 REPLY_MACRO2(VL_API_POT_PROFILE_SHOW_CONFIG_DETAILS,
163 rmp->polynomial_public=0;
168 static void vl_api_pot_profile_show_config_dump_t_handler
169 (vl_api_pot_profile_show_config_dump_t *mp)
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);
178 send_pot_profile_details(mp,id);
181 static void vl_api_pot_profile_activate_t_handler
182 (vl_api_pot_profile_activate_t *mp)
184 pot_main_t * sm = &pot_main;
186 vl_api_pot_profile_add_reply_t * rmp;
190 if (mp->list_name_len)
191 name = format(0, "%s", mp->list_name);
192 if (!pot_profile_list_is_enabled(name)) {
196 rv = pot_profile_set_active(id);
200 REPLY_MACRO(VL_API_POT_PROFILE_ACTIVATE_REPLY);
204 static void vl_api_pot_profile_del_t_handler
205 (vl_api_pot_profile_del_t *mp)
207 pot_main_t * sm = &pot_main;
209 vl_api_pot_profile_del_reply_t * rmp;
211 clear_pot_profiles();
213 REPLY_MACRO(VL_API_POT_PROFILE_DEL_REPLY);
216 /* Set up the API message handling tables */
217 static clib_error_t *
218 pot_plugin_api_hookup (vlib_main_t *vm)
220 pot_main_t * sm = &pot_main;
222 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
224 vl_api_##n##_t_handler, \
226 vl_api_##n##_t_endian, \
227 vl_api_##n##_t_print, \
228 sizeof(vl_api_##n##_t), 1);
229 foreach_pot_plugin_api_msg;
235 #define vl_msg_name_crc_list
236 #include <ioam/lib-pot/pot_all_api_h.h>
237 #undef vl_msg_name_crc_list
240 setup_message_id_table (pot_main_t * sm, api_main_t * am)
242 #define _(id,n,crc) \
243 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
244 foreach_vl_msg_name_crc_pot;
248 static clib_error_t * pot_init (vlib_main_t * vm)
250 pot_main_t * sm = &pot_main;
251 clib_error_t * error = 0;
254 bzero(sm, sizeof(pot_main));
255 (void)pot_util_init();
258 sm->vnet_main = vnet_get_main();
260 name = format (0, "ioam_pot_%08x%c", api_version, 0);
262 /* Ask for a correctly-sized block of API message decode slots */
263 sm->msg_id_base = vl_msg_api_get_msg_ids
264 ((char *) name, VL_MSG_FIRST_AVAILABLE);
266 error = pot_plugin_api_hookup (vm);
268 /* Add our API messages to the global name_crc hash table */
269 setup_message_id_table (sm, &api_main);
276 VLIB_INIT_FUNCTION (pot_init);