FD.io VPP  v17.01.1-3-gc6833f8
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)+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); \
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 _(POT_PROFILE_SHOW_CONFIG_DUMP, pot_profile_show_config_dump) \
100 
103 {
104  pot_main_t * sm = &pot_main;
105  int rv = 0;
107  u8 id;
108  pot_profile *profile = NULL;
109  u8 *name = 0;
110 
111  if (mp->list_name_len)
112  name = format(0, "%s", mp->list_name);
113 
114  pot_profile_list_init(name);
115  id = mp->id;
116  profile = pot_profile_find(id);
117  if (profile) {
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));
123  if (rv != 0)
124  goto ERROROUT;
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);
128  } else {
129  rv = -3;
130  }
131  ERROROUT:
132  vec_free(name);
133  REPLY_MACRO(VL_API_POT_PROFILE_ADD_REPLY);
134 }
135 
137 {
139  pot_main_t * sm = &pot_main;
140  pot_profile *profile = pot_profile_find(id);
141  int rv = 0;
142  if(profile){
143  REPLY_MACRO2(VL_API_POT_PROFILE_SHOW_CONFIG_DETAILS,
144  rmp->id=id;
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);
152  );
153  }
154  else{
155  REPLY_MACRO2(VL_API_POT_PROFILE_SHOW_CONFIG_DETAILS,
156  rmp->id=id;
157  rmp->validator=0;
158  rmp->secret_key=0;
159  rmp->secret_share=0;
160  rmp->prime=0;
161  rmp->bit_mask=0;
162  rmp->lpc=0;
163  rmp->polynomial_public=0;
164  );
165  }
166 }
167 
170 {
171  u8 id = mp->id;
172  u8 dump_call_id = ~0;
173  if(dump_call_id==id){
174  for(id=0;id<MAX_POT_PROFILES;id++)
176  }
177  else
179 }
180 
183 {
184  pot_main_t * sm = &pot_main;
185  int rv = 0;
187  u8 id;
188  u8 *name = NULL;
189 
190  if (mp->list_name_len)
191  name = format(0, "%s", mp->list_name);
192  if (!pot_profile_list_is_enabled(name)) {
193  rv = -1;
194  } else {
195  id = mp->id;
196  rv = pot_profile_set_active(id);
197  }
198 
199  vec_free(name);
200  REPLY_MACRO(VL_API_POT_PROFILE_ACTIVATE_REPLY);
201 }
202 
203 
206 {
207  pot_main_t * sm = &pot_main;
208  int rv = 0;
210 
212 
213  REPLY_MACRO(VL_API_POT_PROFILE_DEL_REPLY);
214 }
215 
216 /* Set up the API message handling tables */
217 static clib_error_t *
219 {
220  pot_main_t * sm = &pot_main;
221 #define _(N,n) \
222  vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
223  #n, \
224  vl_api_##n##_t_handler, \
225  vl_noop_handler, \
226  vl_api_##n##_t_endian, \
227  vl_api_##n##_t_print, \
228  sizeof(vl_api_##n##_t), 1);
230 #undef _
231 
232  return 0;
233 }
234 
235 #define vl_msg_name_crc_list
237 #undef vl_msg_name_crc_list
238 
239 static void
241 {
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;
245 #undef _
246 }
247 
249 {
250  pot_main_t * sm = &pot_main;
251  clib_error_t * error = 0;
252  u8 * name;
253 
254  bzero(sm, sizeof(pot_main));
255  (void)pot_util_init();
256 
257  sm->vlib_main = vm;
258  sm->vnet_main = vnet_get_main();
259 
260  name = format (0, "ioam_pot_%08x%c", api_version, 0);
261 
262  /* Ask for a correctly-sized block of API message decode slots */
264  ((char *) name, VL_MSG_FIRST_AVAILABLE);
265 
266  error = pot_plugin_api_hookup (vm);
267 
268  /* Add our API messages to the global name_crc hash table */
270 
271  vec_free(name);
272 
273  return error;
274 }
275 
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
static clib_error_t * pot_plugin_api_hookup(vlib_main_t *vm)
Definition: pot_api.c:218
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:248
vnet_main_t * vnet_main
Definition: pot_util.h:84
u64 secret_share
Definition: pot_util.h:62
static int pot_profile_set_active(u8 id)
Definition: pot_util.h:148
#define MAX_POT_PROFILES
Definition: pot_util.h:28
api_main_t api_main
Definition: api_shared.c:39
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
u64 poly_pre_eval
Definition: pot_util.h:65
Show POT profile reply.
Definition: pot.api:122
static void vl_api_pot_profile_del_t_handler(vl_api_pot_profile_del_t *mp)
Definition: pot_api.c:205
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
static void vl_api_pot_profile_show_config_dump_t_handler(vl_api_pot_profile_show_config_dump_t *mp)
Definition: pot_api.c:169
#define foreach_pot_plugin_api_msg
Definition: pot_api.c:95
u64 secret_key
Definition: pot_util.h:61
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
#define REPLY_MACRO2(t, body)
Definition: pot_api.c:77
u8 validator
Definition: pot_util.h:60
static void vl_api_pot_profile_activate_t_handler(vl_api_pot_profile_activate_t *mp)
Definition: pot_api.c:182
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
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
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:1309
static void setup_message_id_table(pot_main_t *sm, api_main_t *am)
Definition: pot_api.c:240
static void send_pot_profile_details(vl_api_pot_profile_show_config_dump_t *mp, u8 id)
Definition: pot_api.c:136
u64 bit_mask
Definition: pot_util.h:66
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:102