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>
29 /* define message IDs */
30 #include <ioam/lib-pot/pot_msg_enum.h>
32 /* define message structures */
34 #include <ioam/lib-pot/pot_all_api_h.h>
37 /* define generated endian-swappers */
39 #include <ioam/lib-pot/pot_all_api_h.h>
42 /* instantiate all the print functions we know about */
43 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45 #include <ioam/lib-pot/pot_all_api_h.h>
48 /* Get the API version number */
49 #define vl_api_version(n,v) static u32 api_version=(v);
50 #include <ioam/lib-pot/pot_all_api_h.h>
53 #define REPLY_MSG_ID_BASE sm->msg_id_base
54 #include <vlibapi/api_helper_macros.h>
56 /* List of message types that this plugin understands */
57 #define foreach_pot_plugin_api_msg \
58 _(POT_PROFILE_ADD, pot_profile_add) \
59 _(POT_PROFILE_ACTIVATE, pot_profile_activate) \
60 _(POT_PROFILE_DEL, pot_profile_del) \
61 _(POT_PROFILE_SHOW_CONFIG_DUMP, pot_profile_show_config_dump) \
63 static void vl_api_pot_profile_add_t_handler
64 (vl_api_pot_profile_add_t *mp)
66 pot_main_t * sm = &pot_main;
68 vl_api_pot_profile_add_reply_t * rmp;
70 pot_profile *profile = NULL;
73 if (mp->list_name_len)
74 name = format(0, "%s", mp->list_name);
76 pot_profile_list_init(name);
78 profile = pot_profile_find(id);
80 rv = pot_profile_create(profile,
81 clib_net_to_host_u64(mp->prime),
82 clib_net_to_host_u64(mp->polynomial_public),
83 clib_net_to_host_u64(mp->lpc),
84 clib_net_to_host_u64(mp->secret_share));
87 if (1 == mp->validator)
88 (void)pot_set_validator(profile, clib_net_to_host_u64(mp->secret_key));
89 (void)pot_profile_set_bit_mask(profile, mp->max_bits);
95 REPLY_MACRO(VL_API_POT_PROFILE_ADD_REPLY);
98 static void send_pot_profile_details(vl_api_pot_profile_show_config_dump_t *mp, u8 id)
100 vl_api_pot_profile_show_config_details_t * rmp;
101 pot_main_t * sm = &pot_main;
102 pot_profile *profile = pot_profile_find(id);
105 REPLY_MACRO2(VL_API_POT_PROFILE_SHOW_CONFIG_DETAILS,
107 rmp->validator=profile->validator;
108 rmp->secret_key=clib_host_to_net_u64(profile->secret_key);
109 rmp->secret_share=clib_host_to_net_u64(profile->secret_share);
110 rmp->prime=clib_host_to_net_u64(profile->prime);
111 rmp->bit_mask=clib_host_to_net_u64(profile->bit_mask);
112 rmp->lpc=clib_host_to_net_u64(profile->lpc);
113 rmp->polynomial_public=clib_host_to_net_u64(profile->poly_pre_eval);
117 REPLY_MACRO2(VL_API_POT_PROFILE_SHOW_CONFIG_DETAILS,
125 rmp->polynomial_public=0;
130 static void vl_api_pot_profile_show_config_dump_t_handler
131 (vl_api_pot_profile_show_config_dump_t *mp)
134 u8 dump_call_id = ~0;
135 if(dump_call_id==id){
136 for(id=0;id<MAX_POT_PROFILES;id++)
137 send_pot_profile_details(mp,id);
140 send_pot_profile_details(mp,id);
143 static void vl_api_pot_profile_activate_t_handler
144 (vl_api_pot_profile_activate_t *mp)
146 pot_main_t * sm = &pot_main;
148 vl_api_pot_profile_add_reply_t * rmp;
152 if (mp->list_name_len)
153 name = format(0, "%s", mp->list_name);
154 if (!pot_profile_list_is_enabled(name)) {
158 rv = pot_profile_set_active(id);
162 REPLY_MACRO(VL_API_POT_PROFILE_ACTIVATE_REPLY);
166 static void vl_api_pot_profile_del_t_handler
167 (vl_api_pot_profile_del_t *mp)
169 pot_main_t * sm = &pot_main;
171 vl_api_pot_profile_del_reply_t * rmp;
173 clear_pot_profiles();
175 REPLY_MACRO(VL_API_POT_PROFILE_DEL_REPLY);
178 /* Set up the API message handling tables */
179 static clib_error_t *
180 pot_plugin_api_hookup (vlib_main_t *vm)
182 pot_main_t * sm = &pot_main;
184 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
186 vl_api_##n##_t_handler, \
188 vl_api_##n##_t_endian, \
189 vl_api_##n##_t_print, \
190 sizeof(vl_api_##n##_t), 1);
191 foreach_pot_plugin_api_msg;
197 #define vl_msg_name_crc_list
198 #include <ioam/lib-pot/pot_all_api_h.h>
199 #undef vl_msg_name_crc_list
202 setup_message_id_table (pot_main_t * sm, api_main_t * am)
204 #define _(id,n,crc) \
205 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
206 foreach_vl_msg_name_crc_pot;
210 static clib_error_t * pot_init (vlib_main_t * vm)
212 pot_main_t * sm = &pot_main;
213 clib_error_t * error = 0;
216 bzero(sm, sizeof(pot_main));
217 (void)pot_util_init();
220 sm->vnet_main = vnet_get_main();
222 name = format (0, "ioam_pot_%08x%c", api_version, 0);
224 /* Ask for a correctly-sized block of API message decode slots */
225 sm->msg_id_base = vl_msg_api_get_msg_ids
226 ((char *) name, VL_MSG_FIRST_AVAILABLE);
228 error = pot_plugin_api_hookup (vm);
230 /* Add our API messages to the global name_crc hash table */
231 setup_message_id_table (sm, &api_main);
238 VLIB_INIT_FUNCTION (pot_init);