FD.io VPP  v16.09
Vector Packet Processing
pot_api.c
Go to the documentation of this file.
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 <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 */
32 
33 /* define message structures */
34 #define vl_typedefs
36 #undef vl_typedefs
37 
38 /* define generated endian-swappers */
39 #define vl_endianfun
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
47 #undef vl_printfun
48 
49 /* Get the API version number */
50 #define vl_api_version(n,v) static u32 api_version=(v);
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 
102 {
103  pot_main_t * sm = &pot_main;
104  int rv = 0;
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 
137 {
138  pot_main_t * sm = &pot_main;
139  int rv = 0;
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 
160 {
161  pot_main_t * sm = &pot_main;
162  int rv = 0;
164 
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 *
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 *
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);
204 #undef _
205 
206  return 0;
207 }
208 
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 */
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 
pot_main_t pot_main
Definition: pot_util.c:23
#define REPLY_MACRO(t)
Definition: pot_api.c:62
void clear_pot_profiles()
Definition: pot_util.c:264
clib_error_t * vlib_plugin_register(vlib_main_t *vm, vnet_plugin_handoff_t *h, int from_early_init)
Definition: pot_api.c:179
static clib_error_t * pot_plugin_api_hookup(vlib_main_t *vm)
Definition: pot_api.c:192
pot_profile * pot_profile_find(u8 id)
Definition: pot_util.c:58
int pot_profile_create(pot_profile *profile, u64 prime, u64 poly2, u64 lpc, u64 secret_share)
Definition: pot_util.c:117
Proof of Transit(POT): Set POT profile.
Definition: pot.api:30
Proof of Transit profile add / del response.
Definition: pot.api:94
#define NULL
Definition: clib.h:55
static clib_error_t * pot_init(vlib_main_t *vm)
Definition: pot_api.c:209
vnet_main_t * vnet_main
Definition: pot_util.h:84
static int pot_profile_set_active(u8 id)
Definition: pot_util.h:148
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
static void vl_api_pot_profile_del_t_handler(vl_api_pot_profile_del_t *mp)
Definition: pot_api.c:159
vlib_main_t * vlib_main
Definition: pot_util.h:83
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:300
Usage:
Definition: pot_util.h:54
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:418
#define foreach_pot_plugin_api_msg
Definition: pot_api.c:95
int pot_set_validator(pot_profile *profile, u64 key)
Definition: pot_util.c:136
int pot_util_init(void)
Definition: pot_util.c:42
u16 msg_id_base
Definition: pot_util.h:80
unsigned char u8
Definition: types.h:56
Delete POT Profile.
Definition: pot.api:83
static void vl_api_pot_profile_activate_t_handler(vl_api_pot_profile_activate_t *mp)
Definition: pot_api.c:136
vnet_main_t * vnet_main
Definition: plugin.h:26
void pot_profile_list_init(u8 *profile_list_name)
Definition: pot_util.c:85
int pot_profile_list_is_enabled(u8 *name)
Definition: pot_util.c:79
Proof of Transit profile add / del response.
Definition: pot.api:49
Proof of Transit(POT): Activate POT profile in the list.
Definition: pot.api:60
u16 vl_msg_api_get_msg_ids(char *name, int n)
Definition: api_shared.c:1269
int pot_profile_set_bit_mask(pot_profile *profile, u16 bits)
Definition: pot_util.c:240
static void vl_api_pot_profile_add_t_handler(vl_api_pot_profile_add_t *mp)
Definition: pot_api.c:101