pot_api.c revision a90ba9d3
185b528e0SShwetha/*
285b528e0SShwetha * Copyright (c) 2016 Cisco and/or its affiliates.
385b528e0SShwetha * Licensed under the Apache License, Version 2.0 (the "License");
485b528e0SShwetha * you may not use this file except in compliance with the License.
585b528e0SShwetha * You may obtain a copy of the License at:
685b528e0SShwetha *
785b528e0SShwetha *     http://www.apache.org/licenses/LICENSE-2.0
885b528e0SShwetha *
985b528e0SShwetha * Unless required by applicable law or agreed to in writing, software
1085b528e0SShwetha * distributed under the License is distributed on an "AS IS" BASIS,
1185b528e0SShwetha * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1285b528e0SShwetha * See the License for the specific language governing permissions and
1385b528e0SShwetha * limitations under the License.
1485b528e0SShwetha */
1585b528e0SShwetha/*
1685b528e0SShwetha *------------------------------------------------------------------
1785b528e0SShwetha * pot_api.c - Proof of Transit related APIs to create
1885b528e0SShwetha *             and maintain profiles
1985b528e0SShwetha *------------------------------------------------------------------
2085b528e0SShwetha */
2185b528e0SShwetha
2285b528e0SShwetha#include <vnet/vnet.h>
2385b528e0SShwetha#include <vnet/plugin/plugin.h>
24ea3e1fc8SOle Troan#include <ioam/lib-pot/pot_util.h>
2585b528e0SShwetha
2685b528e0SShwetha#include <vlibapi/api.h>
2785b528e0SShwetha#include <vlibmemory/api.h>
2885b528e0SShwetha#include <vlibsocket/api.h>
2985b528e0SShwetha
3085b528e0SShwetha/* define message IDs */
31ea3e1fc8SOle Troan#include <ioam/lib-pot/pot_msg_enum.h>
3285b528e0SShwetha
3385b528e0SShwetha/* define message structures */
3485b528e0SShwetha#define vl_typedefs
35ea3e1fc8SOle Troan#include <ioam/lib-pot/pot_all_api_h.h>
3685b528e0SShwetha#undef vl_typedefs
3785b528e0SShwetha
3885b528e0SShwetha/* define generated endian-swappers */
3985b528e0SShwetha#define vl_endianfun
40ea3e1fc8SOle Troan#include <ioam/lib-pot/pot_all_api_h.h>
4185b528e0SShwetha#undef vl_endianfun
4285b528e0SShwetha
4385b528e0SShwetha/* instantiate all the print functions we know about */
4485b528e0SShwetha#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
4585b528e0SShwetha#define vl_printfun
46ea3e1fc8SOle Troan#include <ioam/lib-pot/pot_all_api_h.h>
4785b528e0SShwetha#undef vl_printfun
4885b528e0SShwetha
4985b528e0SShwetha/* Get the API version number */
5085b528e0SShwetha#define vl_api_version(n,v) static u32 api_version=(v);
51ea3e1fc8SOle Troan#include <ioam/lib-pot/pot_all_api_h.h>
5285b528e0SShwetha#undef vl_api_version
5385b528e0SShwetha
5485b528e0SShwetha/*
5585b528e0SShwetha * A handy macro to set up a message reply.
5685b528e0SShwetha * Assumes that the following variables are available:
5785b528e0SShwetha * mp - pointer to request message
5885b528e0SShwetha * rmp - pointer to reply message type
5985b528e0SShwetha * rv - return value
6085b528e0SShwetha */
6185b528e0SShwetha
6285b528e0SShwetha#define REPLY_MACRO(t)                                          \
6385b528e0SShwethado {                                                            \
6485b528e0SShwetha    unix_shared_memory_queue_t * q =                            \
6585b528e0SShwetha    vl_api_client_index_to_input_queue (mp->client_index);      \
6685b528e0SShwetha    if (!q)                                                     \
6785b528e0SShwetha        return;                                                 \
6885b528e0SShwetha                                                                \
6985b528e0SShwetha    rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
7085b528e0SShwetha    rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base);               \
7185b528e0SShwetha    rmp->context = mp->context;                                 \
7285b528e0SShwetha    rmp->retval = ntohl(rv);                                    \
7385b528e0SShwetha                                                                \
7485b528e0SShwetha    vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
7585b528e0SShwetha} while(0);
7685b528e0SShwetha
7785b528e0SShwetha#define REPLY_MACRO2(t, body)                                   \
7885b528e0SShwethado {                                                            \
7985b528e0SShwetha    unix_shared_memory_queue_t * q;                             \
8085b528e0SShwetha    rv = vl_msg_api_pd_handler (mp, rv);                        \
8185b528e0SShwetha    q = vl_api_client_index_to_input_queue (mp->client_index);  \
8285b528e0SShwetha    if (!q)                                                     \
8385b528e0SShwetha        return;                                                 \
8485b528e0SShwetha                                                                \
8585b528e0SShwetha    rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
86b4bc55b9SSagar Srivastav    rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base);               \
8785b528e0SShwetha    rmp->context = mp->context;                                 \
8885b528e0SShwetha    rmp->retval = ntohl(rv);                                    \
8985b528e0SShwetha    do {body;} while (0);                                       \
9085b528e0SShwetha    vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
9185b528e0SShwetha} while(0);
9285b528e0SShwetha
9385b528e0SShwetha/* List of message types that this plugin understands */
9485b528e0SShwetha
9585b528e0SShwetha#define foreach_pot_plugin_api_msg                                      \
9685b528e0SShwetha_(POT_PROFILE_ADD, pot_profile_add)                                     \
9785b528e0SShwetha_(POT_PROFILE_ACTIVATE, pot_profile_activate)                           \
9885b528e0SShwetha_(POT_PROFILE_DEL, pot_profile_del)                                     \
99b4bc55b9SSagar Srivastav_(POT_PROFILE_SHOW_CONFIG_DUMP, pot_profile_show_config_dump)                                     \
10085b528e0SShwetha
10185b528e0SShwethastatic void vl_api_pot_profile_add_t_handler
10285b528e0SShwetha(vl_api_pot_profile_add_t *mp)
10385b528e0SShwetha{
10485b528e0SShwetha    pot_main_t * sm = &pot_main;
10585b528e0SShwetha    int rv = 0;
10685b528e0SShwetha    vl_api_pot_profile_add_reply_t * rmp;
10785b528e0SShwetha    u8 id;
10885b528e0SShwetha    pot_profile *profile = NULL;
10985b528e0SShwetha    u8 *name = 0;
11085b528e0SShwetha
11185b528e0SShwetha    if (mp->list_name_len)
11285b528e0SShwetha        name = format(0, "%s", mp->list_name);
11385b528e0SShwetha
11485b528e0SShwetha    pot_profile_list_init(name);
11585b528e0SShwetha    id = mp->id;
11685b528e0SShwetha    profile = pot_profile_find(id);
11785b528e0SShwetha    if (profile) {
11885b528e0SShwetha	rv = pot_profile_create(profile,
11985b528e0SShwetha				clib_net_to_host_u64(mp->prime),
12085b528e0SShwetha				clib_net_to_host_u64(mp->polynomial_public),
12185b528e0SShwetha				clib_net_to_host_u64(mp->lpc),
12285b528e0SShwetha				clib_net_to_host_u64(mp->secret_share));
12385b528e0SShwetha	if (rv != 0)
12485b528e0SShwetha            goto ERROROUT;
12585b528e0SShwetha	if (1 == mp->validator)
12685b528e0SShwetha	  (void)pot_set_validator(profile, clib_net_to_host_u64(mp->secret_key));
12785b528e0SShwetha        (void)pot_profile_set_bit_mask(profile, mp->max_bits);
12885b528e0SShwetha    } else {
12985b528e0SShwetha        rv = -3;
13085b528e0SShwetha    }
13185b528e0SShwetha ERROROUT:
13285b528e0SShwetha    vec_free(name);
13385b528e0SShwetha    REPLY_MACRO(VL_API_POT_PROFILE_ADD_REPLY);
13485b528e0SShwetha}
13585b528e0SShwetha
136b4bc55b9SSagar Srivastavstatic void send_pot_profile_details(vl_api_pot_profile_show_config_dump_t *mp, u8 id)
137b4bc55b9SSagar Srivastav{
138b4bc55b9SSagar Srivastav    vl_api_pot_profile_show_config_details_t * rmp;
139b4bc55b9SSagar Srivastav    pot_main_t * sm = &pot_main;
140b4bc55b9SSagar Srivastav    pot_profile *profile = pot_profile_find(id);
141b4bc55b9SSagar Srivastav    int rv = 0;
142b4bc55b9SSagar Srivastav    if(profile){
143b4bc55b9SSagar Srivastav        REPLY_MACRO2(VL_API_POT_PROFILE_SHOW_CONFIG_DETAILS,
144b4bc55b9SSagar Srivastav			rmp->id=id;
145b4bc55b9SSagar Srivastav			rmp->validator=profile->validator;
146b4bc55b9SSagar Srivastav			rmp->secret_key=clib_host_to_net_u64(profile->secret_key);
147b4bc55b9SSagar Srivastav			rmp->secret_share=clib_host_to_net_u64(profile->secret_share);
148b4bc55b9SSagar Srivastav			rmp->prime=clib_host_to_net_u64(profile->prime);
149b4bc55b9SSagar Srivastav			rmp->bit_mask=clib_host_to_net_u64(profile->bit_mask);
150b4bc55b9SSagar Srivastav			rmp->lpc=clib_host_to_net_u64(profile->lpc);
151b4bc55b9SSagar Srivastav			rmp->polynomial_public=clib_host_to_net_u64(profile->poly_pre_eval);
152b4bc55b9SSagar Srivastav			);
153b4bc55b9SSagar Srivastav    }
154b4bc55b9SSagar Srivastav    else{
155b4bc55b9SSagar Srivastav        REPLY_MACRO2(VL_API_POT_PROFILE_SHOW_CONFIG_DETAILS,
156b4bc55b9SSagar Srivastav			rmp->id=id;
157b4bc55b9SSagar Srivastav			rmp->validator=0;
158b4bc55b9SSagar Srivastav			rmp->secret_key=0;
159b4bc55b9SSagar Srivastav			rmp->secret_share=0;
160b4bc55b9SSagar Srivastav			rmp->prime=0;
161b4bc55b9SSagar Srivastav			rmp->bit_mask=0;
162b4bc55b9SSagar Srivastav			rmp->lpc=0;
163b4bc55b9SSagar Srivastav			rmp->polynomial_public=0;
164b4bc55b9SSagar Srivastav			);
165b4bc55b9SSagar Srivastav    }
166b4bc55b9SSagar Srivastav}
167b4bc55b9SSagar Srivastav
168b4bc55b9SSagar Srivastavstatic void vl_api_pot_profile_show_config_dump_t_handler
169b4bc55b9SSagar Srivastav(vl_api_pot_profile_show_config_dump_t *mp)
170b4bc55b9SSagar Srivastav{
171b4bc55b9SSagar Srivastav    u8 id = mp->id;
172b4bc55b9SSagar Srivastav    u8 dump_call_id = ~0;
173b4bc55b9SSagar Srivastav    if(dump_call_id==id){
174b4bc55b9SSagar Srivastav        for(id=0;id<MAX_POT_PROFILES;id++)
175b4bc55b9SSagar Srivastav	    send_pot_profile_details(mp,id);
176b4bc55b9SSagar Srivastav    }
177b4bc55b9SSagar Srivastav    else
178b4bc55b9SSagar Srivastav        send_pot_profile_details(mp,id);
179b4bc55b9SSagar Srivastav}
180b4bc55b9SSagar Srivastav
18185b528e0SShwethastatic void vl_api_pot_profile_activate_t_handler
18285b528e0SShwetha(vl_api_pot_profile_activate_t *mp)
18385b528e0SShwetha{
18485b528e0SShwetha    pot_main_t * sm = &pot_main;
18585b528e0SShwetha    int rv = 0;
18685b528e0SShwetha    vl_api_pot_profile_add_reply_t * rmp;
18785b528e0SShwetha    u8 id;
18885b528e0SShwetha    u8 *name = NULL;
18985b528e0SShwetha
19085b528e0SShwetha    if (mp->list_name_len)
19185b528e0SShwetha        name = format(0, "%s", mp->list_name);
19285b528e0SShwetha    if (!pot_profile_list_is_enabled(name)) {
19385b528e0SShwetha        rv = -1;
19485b528e0SShwetha    } else {
19585b528e0SShwetha        id = mp->id;
19685b528e0SShwetha	rv = pot_profile_set_active(id);
19785b528e0SShwetha    }
19885b528e0SShwetha
19985b528e0SShwetha    vec_free(name);
20085b528e0SShwetha    REPLY_MACRO(VL_API_POT_PROFILE_ACTIVATE_REPLY);
20185b528e0SShwetha}
20285b528e0SShwetha
20385b528e0SShwetha
20485b528e0SShwethastatic void vl_api_pot_profile_del_t_handler
20585b528e0SShwetha(vl_api_pot_profile_del_t *mp)
20685b528e0SShwetha{
20785b528e0SShwetha    pot_main_t * sm = &pot_main;
20885b528e0SShwetha    int rv = 0;
20985b528e0SShwetha    vl_api_pot_profile_del_reply_t * rmp;
21085b528e0SShwetha
21185b528e0SShwetha    clear_pot_profiles();
21285b528e0SShwetha
21385b528e0SShwetha    REPLY_MACRO(VL_API_POT_PROFILE_DEL_REPLY);
21485b528e0SShwetha}
21585b528e0SShwetha
21685b528e0SShwetha/* Set up the API message handling tables */
21785b528e0SShwethastatic clib_error_t *
21885b528e0SShwethapot_plugin_api_hookup (vlib_main_t *vm)
21985b528e0SShwetha{
22085b528e0SShwetha  pot_main_t * sm = &pot_main;
22185b528e0SShwetha#define _(N,n)                                                  \
22285b528e0SShwetha    vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
22385b528e0SShwetha                           #n,					\
22485b528e0SShwetha                           vl_api_##n##_t_handler,              \
22585b528e0SShwetha                           vl_noop_handler,                     \
22685b528e0SShwetha                           vl_api_##n##_t_endian,               \
22785b528e0SShwetha                           vl_api_##n##_t_print,                \
22885b528e0SShwetha                           sizeof(vl_api_##n##_t), 1);
22985b528e0SShwetha    foreach_pot_plugin_api_msg;
23085b528e0SShwetha#undef _
23185b528e0SShwetha
23285b528e0SShwetha    return 0;
23385b528e0SShwetha}
23485b528e0SShwetha
235557d128bSDave Barach#define vl_msg_name_crc_list
236557d128bSDave Barach#include <ioam/lib-pot/pot_all_api_h.h>
237557d128bSDave Barach#undef vl_msg_name_crc_list
238557d128bSDave Barach
239557d128bSDave Barachstatic void
240557d128bSDave Barachsetup_message_id_table (pot_main_t * sm, api_main_t * am)
241557d128bSDave Barach{
242557d128bSDave Barach#define _(id,n,crc) \
243557d128bSDave Barach  vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
244557d128bSDave Barach  foreach_vl_msg_name_crc_pot;
245557d128bSDave Barach#undef _
246557d128bSDave Barach}
247557d128bSDave Barach
24885b528e0SShwethastatic clib_error_t * pot_init (vlib_main_t * vm)
24985b528e0SShwetha{
25085b528e0SShwetha  pot_main_t * sm = &pot_main;
25185b528e0SShwetha  clib_error_t * error = 0;
25285b528e0SShwetha  u8 * name;
25385b528e0SShwetha
25485b528e0SShwetha  bzero(sm, sizeof(pot_main));
25585b528e0SShwetha  (void)pot_util_init();
256a90ba9d3SAkshayaNadahalli
257a90ba9d3SAkshayaNadahalli  sm->vlib_main = vm;
258a90ba9d3SAkshayaNadahalli  sm->vnet_main = vnet_get_main();
259a90ba9d3SAkshayaNadahalli
2605c3bfab7SSagar Srivastav  name = format (0, "ioam_pot_%08x%c", api_version, 0);
26185b528e0SShwetha
26285b528e0SShwetha  /* Ask for a correctly-sized block of API message decode slots */
26385b528e0SShwetha  sm->msg_id_base = vl_msg_api_get_msg_ids
26485b528e0SShwetha      ((char *) name, VL_MSG_FIRST_AVAILABLE);
26585b528e0SShwetha
26685b528e0SShwetha  error = pot_plugin_api_hookup (vm);
26785b528e0SShwetha
268557d128bSDave Barach  /* Add our API messages to the global name_crc hash table */
269557d128bSDave Barach  setup_message_id_table (sm, &api_main);
270557d128bSDave Barach
27185b528e0SShwetha  vec_free(name);
27285b528e0SShwetha
27385b528e0SShwetha  return error;
27485b528e0SShwetha}
27585b528e0SShwetha
27685b528e0SShwethaVLIB_INIT_FUNCTION (pot_init);
277