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 <plugins/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 <plugins/ioam/lib-pot/pot_msg_enum.h>
33 /* define message structures */
35 #include <plugins/ioam/lib-pot/pot_all_api_h.h>
38 /* define generated endian-swappers */
40 #include <plugins/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 <plugins/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 <plugins/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)); \
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) \
100 static void vl_api_pot_profile_add_t_handler
101 (vl_api_pot_profile_add_t *mp)
103 pot_main_t * sm = &pot_main;
105 vl_api_pot_profile_add_reply_t * rmp;
107 pot_profile *profile = NULL;
110 if (mp->list_name_len)
111 name = format(0, "%s", mp->list_name);
113 pot_profile_list_init(name);
115 profile = pot_profile_find(id);
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));
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);
132 REPLY_MACRO(VL_API_POT_PROFILE_ADD_REPLY);
135 static void vl_api_pot_profile_activate_t_handler
136 (vl_api_pot_profile_activate_t *mp)
138 pot_main_t * sm = &pot_main;
140 vl_api_pot_profile_add_reply_t * rmp;
144 if (mp->list_name_len)
145 name = format(0, "%s", mp->list_name);
146 if (!pot_profile_list_is_enabled(name)) {
150 rv = pot_profile_set_active(id);
154 REPLY_MACRO(VL_API_POT_PROFILE_ACTIVATE_REPLY);
158 static void vl_api_pot_profile_del_t_handler
159 (vl_api_pot_profile_del_t *mp)
161 pot_main_t * sm = &pot_main;
163 vl_api_pot_profile_del_reply_t * rmp;
165 clear_pot_profiles();
167 REPLY_MACRO(VL_API_POT_PROFILE_DEL_REPLY);
172 * This routine exists to convince the vlib plugin framework that
173 * we haven't accidentally copied a random .dll into the plugin directory.
175 * Also collects global variable pointers passed from the vpp engine
179 vlib_plugin_register (vlib_main_t * vm, vnet_plugin_handoff_t * h,
182 pot_main_t * sm = &pot_main;
183 clib_error_t * error = 0;
186 sm->vnet_main = h->vnet_main;
190 /* Set up the API message handling tables */
191 static clib_error_t *
192 pot_plugin_api_hookup (vlib_main_t *vm)
194 pot_main_t * sm = &pot_main;
196 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
198 vl_api_##n##_t_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;
209 static clib_error_t * pot_init (vlib_main_t * vm)
211 pot_main_t * sm = &pot_main;
212 clib_error_t * error = 0;
215 bzero(sm, sizeof(pot_main));
216 (void)pot_util_init();
217 name = format (0, "pot_%08x%c", api_version, 0);
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);
223 error = pot_plugin_api_hookup (vm);
230 VLIB_INIT_FUNCTION (pot_init);