blob: 0e2c2101ebb39d95074b74c16a8daa8ea06e024d [file] [log] [blame]
Damjan Marion7cd468a2016-12-19 23:05:39 +01001/*
2 *------------------------------------------------------------------
3 * api.h
4 *
5 * Copyright (c) 2009-2015 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
18 */
19
20#ifndef included_api_h
21#define included_api_h
22
23#include <vppinfra/error.h>
24#include <svm/svm.h>
25#include <vlib/vlib.h>
26#include <vlibmemory/unix_shared_memory_queue.h>
27#include <vlib/unix/unix.h>
Dave Barachbfd92272017-05-12 11:59:25 -040028#include <stddef.h>
Damjan Marion7cd468a2016-12-19 23:05:39 +010029
30typedef enum
31{
32 REGISTRATION_TYPE_FREE = 0,
33 REGISTRATION_TYPE_SHMEM,
34 REGISTRATION_TYPE_SOCKET_LISTEN,
35 REGISTRATION_TYPE_SOCKET_SERVER,
36 REGISTRATION_TYPE_SOCKET_CLIENT,
37} vl_registration_type_t;
38
39typedef struct vl_api_registration_
40{
41 vl_registration_type_t registration_type;
42
43 /* Index in VLIB's brain (not shared memory). */
44 u32 vl_api_registration_pool_index;
45
46 u8 *name;
47
48 /*
49 * The following groups of data could be unioned, but my fingers are
50 * going to be sore enough.
51 */
52
53 /* shared memory only */
54 unix_shared_memory_queue_t *vl_input_queue;
55
56 /* socket server and client */
57 u32 unix_file_index;
58 i8 *unprocessed_input;
59 u32 unprocessed_msg_length;
60 u8 *output_vector;
61
62 /* socket client only */
63 u32 server_handle;
64 u32 server_index;
65
66} vl_api_registration_t;
67
68
69/* Trace configuration for a single message */
70typedef struct
71{
72 int size;
73 int trace_enable;
74 int replay_enable;
75} trace_cfg_t;
76
77/*
78 * API recording
79 */
80typedef struct
81{
82 u8 endian;
83 u8 enabled;
84 u8 wrapped;
85 u8 pad;
86 u32 nitems;
87 u32 curindex;
88 u8 **traces;
89} vl_api_trace_t;
90
91/* *INDENT-OFF* */
92typedef CLIB_PACKED
93(struct
94 {
95 u8 endian; u8 wrapped;
96 u32 nitems;
97}) vl_api_trace_file_header_t;
98/* *INDENT-ON* */
99
100typedef enum
101{
102 VL_API_TRACE_TX,
103 VL_API_TRACE_RX,
104} vl_api_trace_which_t;
105
106#define VL_API_LITTLE_ENDIAN 0x00
107#define VL_API_BIG_ENDIAN 0x01
108
109typedef struct
110{
111 u8 *name;
112 u16 first_msg_id;
113 u16 last_msg_id;
114} vl_api_msg_range_t;
115
Dave Barachb64e4e22017-03-14 09:10:56 -0400116typedef clib_error_t *(vl_msg_api_init_function_t) (u32 client_index);
117
118typedef struct _vl_msg_api_init_function_list_elt
119{
120 struct _vl_msg_api_init_function_list_elt *next_init_function;
121 vl_msg_api_init_function_t *f;
122} _vl_msg_api_function_list_elt_t;
123
Damjan Marion7cd468a2016-12-19 23:05:39 +0100124typedef struct
125{
126 void (**msg_handlers) (void *);
127 int (**pd_msg_handlers) (void *, int);
128 void (**msg_cleanup_handlers) (void *);
129 void (**msg_endian_handlers) (void *);
130 void (**msg_print_handlers) (void *, void *);
Neale Rannse72be392017-04-26 13:59:20 -0700131 const char **msg_names;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100132 u8 *message_bounce;
133 u8 *is_mp_safe;
134 struct ring_alloc_ *arings;
135 u32 ring_misses;
Dave Barach842b9c52017-01-09 15:54:00 -0500136 u32 garbage_collects;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100137 u32 missing_clients;
138 vl_api_trace_t *rx_trace;
139 vl_api_trace_t *tx_trace;
140 int msg_print_flag;
141 trace_cfg_t *api_trace_cfg;
142 int our_pid;
143 svm_region_t *vlib_rp;
144 svm_region_t **mapped_shmem_regions;
145 struct vl_shmem_hdr_ *shmem_hdr;
146 vl_api_registration_t **vl_clients;
147
148 u8 *serialized_message_table_in_shmem;
149
150 /* For plugin msg allocator */
151 u16 first_available_msg_id;
152
153 /* message range by name hash */
154 uword *msg_range_by_name;
155
156 /* vector of message ranges */
157 vl_api_msg_range_t *msg_ranges;
158
159 /* uid for the api shared memory region */
160 int api_uid;
161 /* gid for the api shared memory region */
162 int api_gid;
163
164 /* base virtual address for global VM region */
165 u64 global_baseva;
166
167 /* size of the global VM region */
168 u64 global_size;
169
170 /* size of the API region */
171 u64 api_size;
172
173 /* size of the global VM private mheap */
174 u64 global_pvt_heap_size;
175
176 /* size of the api private mheap */
177 u64 api_pvt_heap_size;
178
179 /* Client-only data structures */
180 unix_shared_memory_queue_t *vl_input_queue;
181
182 /*
183 * All VLIB-side message handlers use my_client_index to identify
184 * the queue / client. This works in sim replay.
185 */
186 int my_client_index;
187 /*
188 * This is the (shared VM) address of the registration,
189 * don't use it to id the connection since it can't possibly
190 * work in simulator replay.
191 */
192 vl_api_registration_t *my_registration;
193
194 i32 vlib_signal;
195
Dave Barach10d8cc62017-05-30 09:30:07 -0400196 /* vlib input queue length */
197 u32 vlib_input_queue_length;
198
Damjan Marion7cd468a2016-12-19 23:05:39 +0100199 /* client side message index hash table */
200 uword *msg_index_by_name_and_crc;
201
Neale Rannse72be392017-04-26 13:59:20 -0700202 const char *region_name;
203 const char *root_path;
Dave Barachdfbee412017-03-02 18:24:10 -0500204
205 /* Replay in progress? */
206 int replay_in_progress;
Dave Barachb64e4e22017-03-14 09:10:56 -0400207
208 /* List of API client reaper functions */
209 _vl_msg_api_function_list_elt_t *reaper_function_registrations;
210
Damjan Marion7cd468a2016-12-19 23:05:39 +0100211} api_main_t;
212
213extern api_main_t api_main;
214
215typedef struct
216{
217 int id;
218 char *name;
219 u32 crc;
220 void *handler;
221 void *cleanup;
222 void *endian;
223 void *print;
224 int size;
225 int traced;
226 int replay;
227 int message_bounce;
228 int is_mp_safe;
229} vl_msg_api_msg_config_t;
230
231typedef struct msgbuf_
232{
233 unix_shared_memory_queue_t *q;
234 u32 data_len;
Dave Barach842b9c52017-01-09 15:54:00 -0500235 u32 gc_mark_timestamp;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100236 u8 data[0];
237} msgbuf_t;
238
239/* api_shared.c prototypes */
240int vl_msg_api_rx_trace_enabled (api_main_t * am);
241int vl_msg_api_tx_trace_enabled (api_main_t * am);
242void vl_msg_api_trace (api_main_t * am, vl_api_trace_t * tp, void *msg);
243int vl_msg_api_trace_onoff (api_main_t * am, vl_api_trace_which_t which,
244 int onoff);
245int vl_msg_api_trace_free (api_main_t * am, vl_api_trace_which_t which);
246int vl_msg_api_trace_save (api_main_t * am,
247 vl_api_trace_which_t which, FILE * fp);
248int vl_msg_api_trace_configure (api_main_t * am, vl_api_trace_which_t which,
249 u32 nitems);
250void vl_msg_api_handler_with_vm_node (api_main_t * am,
251 void *the_msg, vlib_main_t * vm,
252 vlib_node_runtime_t * node);
253void vl_msg_api_handler (void *the_msg);
254void vl_msg_api_handler_no_free (void *the_msg);
255void vl_msg_api_handler_no_trace_no_free (void *the_msg);
256void vl_msg_api_trace_only (void *the_msg);
257void vl_msg_api_cleanup_handler (void *the_msg);
258void vl_msg_api_replay_handler (void *the_msg);
259void vl_msg_api_socket_handler (void *the_msg);
260void vl_msg_api_set_handlers (int msg_id, char *msg_name,
261 void *handler,
262 void *cleanup,
263 void *endian,
264 void *print, int msg_size, int traced);
265void vl_msg_api_config (vl_msg_api_msg_config_t *);
266void vl_msg_api_set_cleanup_handler (int msg_id, void *fp);
267void vl_msg_api_queue_handler (unix_shared_memory_queue_t * q);
268vl_api_trace_t *vl_msg_api_trace_get (api_main_t * am,
269 vl_api_trace_which_t which);
270
Dave Barach80f54e22017-03-08 19:08:56 -0500271void vl_msg_api_barrier_sync (void) __attribute__ ((weak));
272void vl_msg_api_barrier_release (void) __attribute__ ((weak));
Damjan Marion7cd468a2016-12-19 23:05:39 +0100273void vl_msg_api_free (void *);
274void vl_noop_handler (void *mp);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100275void vl_msg_api_increment_missing_client_counter (void);
276void vl_msg_api_post_mortem_dump (void);
Dave Barach80f54e22017-03-08 19:08:56 -0500277void vl_msg_api_post_mortem_dump_enable_disable (int enable);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100278void vl_msg_api_register_pd_handler (void *handler,
279 u16 msg_id_host_byte_order);
280int vl_msg_api_pd_handler (void *mp, int rv);
281
282void vl_msg_api_set_first_available_msg_id (u16 first_avail);
Neale Rannse72be392017-04-26 13:59:20 -0700283u16 vl_msg_api_get_msg_ids (const char *name, int n);
284void vl_msg_api_add_msg_name_crc (api_main_t * am, const char *string,
285 u32 id);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100286u32 vl_api_get_msg_index (u8 * name_and_crc);
Dave Barachbfd92272017-05-12 11:59:25 -0400287u32 vl_msg_api_get_msg_length (void *msg_arg);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100288
289/* node_serialize.c prototypes */
290u8 *vlib_node_serialize (vlib_node_main_t * nm, u8 * vector,
291 u32 max_threads, int include_nexts,
292 int include_stats);
293vlib_node_t **vlib_node_unserialize (u8 * vector);
294
295#define VLIB_API_INIT_FUNCTION(x) VLIB_DECLARE_INIT_FUNCTION(x,api_init)
296
Dave Baracha1a093d2017-03-02 13:13:23 -0500297/* Call given init function: used for init function dependencies. */
298#define vlib_call_api_init_function(vm, x) \
299 ({ \
300 extern vlib_init_function_t * _VLIB_INIT_FUNCTION_SYMBOL (x,api_init); \
301 vlib_init_function_t * _f = _VLIB_INIT_FUNCTION_SYMBOL (x,api_init); \
302 clib_error_t * _error = 0; \
303 if (! hash_get (vm->init_functions_called, _f)) \
304 { \
305 hash_set1 (vm->init_functions_called, _f); \
306 _error = _f (vm); \
307 } \
308 _error; \
309 })
310
311
Dave Barachb64e4e22017-03-14 09:10:56 -0400312#define _VL_MSG_API_FUNCTION_SYMBOL(x, type) \
313 _vl_msg_api_##type##_function_##x
314
315#define VL_MSG_API_FUNCTION_SYMBOL(x) \
316 _VL_MSG_API_FUNCTION_SYMBOL(x, reaper)
317
318#define VLIB_DECLARE_REAPER_FUNCTION(x, tag) \
319vl_msg_api_init_function_t * _VL_MSG_API_FUNCTION_SYMBOL (x, tag) = x; \
320static void __vl_msg_api_add_##tag##_function_##x (void) \
321 __attribute__((__constructor__)) ; \
322 \
323static void __vl_msg_api_add_##tag##_function_##x (void) \
324{ \
325 api_main_t * am = &api_main; \
326 static _vl_msg_api_function_list_elt_t _vl_msg_api_function; \
327 _vl_msg_api_function.next_init_function \
328 = am->tag##_function_registrations; \
329 am->tag##_function_registrations = &_vl_msg_api_function; \
330 _vl_msg_api_function.f = &x; \
331}
332
333#define VL_MSG_API_REAPER_FUNCTION(x) VLIB_DECLARE_REAPER_FUNCTION(x,reaper)
334
335/* Call reaper function with client index */
336#define vl_msg_api_call_reaper_function(ci) \
337 ({ \
338 extern vlib_init_function_t * VLIB_INIT_FUNCTION_SYMBOL (reaper); \
339 vlib_init_function_t * _f = VLIB_INIT_FUNCTION_SYMBOL (reaper); \
340 clib_error_t * _error = 0; \
341 _error = _f (ci); \
342 })
343
Dave Barachbfd92272017-05-12 11:59:25 -0400344static inline u32
345vl_msg_api_get_msg_length_inline (void *msg_arg)
346{
347 u8 *msg = (u8 *) msg_arg;
348
349 msgbuf_t *header = (msgbuf_t *) (msg - offsetof (msgbuf_t, data));
350
351 return clib_net_to_host_u32 (header->data_len);
352}
Damjan Marion7cd468a2016-12-19 23:05:39 +0100353#endif /* included_api_h */
354
355/*
356 * fd.io coding-style-patch-verification: ON
357 *
358 * Local Variables:
359 * eval: (c-set-style "gnu")
360 * End:
361 */