blob: 7538050e7b22d3ac5485d7341778734e99584904 [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>
28
29typedef enum
30{
31 REGISTRATION_TYPE_FREE = 0,
32 REGISTRATION_TYPE_SHMEM,
33 REGISTRATION_TYPE_SOCKET_LISTEN,
34 REGISTRATION_TYPE_SOCKET_SERVER,
35 REGISTRATION_TYPE_SOCKET_CLIENT,
36} vl_registration_type_t;
37
38typedef struct vl_api_registration_
39{
40 vl_registration_type_t registration_type;
41
42 /* Index in VLIB's brain (not shared memory). */
43 u32 vl_api_registration_pool_index;
44
45 u8 *name;
46
47 /*
48 * The following groups of data could be unioned, but my fingers are
49 * going to be sore enough.
50 */
51
52 /* shared memory only */
53 unix_shared_memory_queue_t *vl_input_queue;
54
55 /* socket server and client */
56 u32 unix_file_index;
57 i8 *unprocessed_input;
58 u32 unprocessed_msg_length;
59 u8 *output_vector;
60
61 /* socket client only */
62 u32 server_handle;
63 u32 server_index;
64
65} vl_api_registration_t;
66
67
68/* Trace configuration for a single message */
69typedef struct
70{
71 int size;
72 int trace_enable;
73 int replay_enable;
74} trace_cfg_t;
75
76/*
77 * API recording
78 */
79typedef struct
80{
81 u8 endian;
82 u8 enabled;
83 u8 wrapped;
84 u8 pad;
85 u32 nitems;
86 u32 curindex;
87 u8 **traces;
88} vl_api_trace_t;
89
90/* *INDENT-OFF* */
91typedef CLIB_PACKED
92(struct
93 {
94 u8 endian; u8 wrapped;
95 u32 nitems;
96}) vl_api_trace_file_header_t;
97/* *INDENT-ON* */
98
99typedef enum
100{
101 VL_API_TRACE_TX,
102 VL_API_TRACE_RX,
103} vl_api_trace_which_t;
104
105#define VL_API_LITTLE_ENDIAN 0x00
106#define VL_API_BIG_ENDIAN 0x01
107
108typedef struct
109{
110 u8 *name;
111 u16 first_msg_id;
112 u16 last_msg_id;
113} vl_api_msg_range_t;
114
Dave Barachb64e4e22017-03-14 09:10:56 -0400115typedef clib_error_t *(vl_msg_api_init_function_t) (u32 client_index);
116
117typedef struct _vl_msg_api_init_function_list_elt
118{
119 struct _vl_msg_api_init_function_list_elt *next_init_function;
120 vl_msg_api_init_function_t *f;
121} _vl_msg_api_function_list_elt_t;
122
Damjan Marion7cd468a2016-12-19 23:05:39 +0100123typedef struct
124{
125 void (**msg_handlers) (void *);
126 int (**pd_msg_handlers) (void *, int);
127 void (**msg_cleanup_handlers) (void *);
128 void (**msg_endian_handlers) (void *);
129 void (**msg_print_handlers) (void *, void *);
Neale Rannse72be392017-04-26 13:59:20 -0700130 const char **msg_names;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100131 u8 *message_bounce;
132 u8 *is_mp_safe;
133 struct ring_alloc_ *arings;
134 u32 ring_misses;
Dave Barach842b9c52017-01-09 15:54:00 -0500135 u32 garbage_collects;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100136 u32 missing_clients;
137 vl_api_trace_t *rx_trace;
138 vl_api_trace_t *tx_trace;
139 int msg_print_flag;
140 trace_cfg_t *api_trace_cfg;
141 int our_pid;
142 svm_region_t *vlib_rp;
143 svm_region_t **mapped_shmem_regions;
144 struct vl_shmem_hdr_ *shmem_hdr;
145 vl_api_registration_t **vl_clients;
146
147 u8 *serialized_message_table_in_shmem;
148
149 /* For plugin msg allocator */
150 u16 first_available_msg_id;
151
152 /* message range by name hash */
153 uword *msg_range_by_name;
154
155 /* vector of message ranges */
156 vl_api_msg_range_t *msg_ranges;
157
158 /* uid for the api shared memory region */
159 int api_uid;
160 /* gid for the api shared memory region */
161 int api_gid;
162
163 /* base virtual address for global VM region */
164 u64 global_baseva;
165
166 /* size of the global VM region */
167 u64 global_size;
168
169 /* size of the API region */
170 u64 api_size;
171
172 /* size of the global VM private mheap */
173 u64 global_pvt_heap_size;
174
175 /* size of the api private mheap */
176 u64 api_pvt_heap_size;
177
178 /* Client-only data structures */
179 unix_shared_memory_queue_t *vl_input_queue;
180
181 /*
182 * All VLIB-side message handlers use my_client_index to identify
183 * the queue / client. This works in sim replay.
184 */
185 int my_client_index;
186 /*
187 * This is the (shared VM) address of the registration,
188 * don't use it to id the connection since it can't possibly
189 * work in simulator replay.
190 */
191 vl_api_registration_t *my_registration;
192
193 i32 vlib_signal;
194
195 /* client side message index hash table */
196 uword *msg_index_by_name_and_crc;
197
Neale Rannse72be392017-04-26 13:59:20 -0700198 const char *region_name;
199 const char *root_path;
Dave Barachdfbee412017-03-02 18:24:10 -0500200
201 /* Replay in progress? */
202 int replay_in_progress;
Dave Barachb64e4e22017-03-14 09:10:56 -0400203
204 /* List of API client reaper functions */
205 _vl_msg_api_function_list_elt_t *reaper_function_registrations;
206
Damjan Marion7cd468a2016-12-19 23:05:39 +0100207} api_main_t;
208
209extern api_main_t api_main;
210
211typedef struct
212{
213 int id;
214 char *name;
215 u32 crc;
216 void *handler;
217 void *cleanup;
218 void *endian;
219 void *print;
220 int size;
221 int traced;
222 int replay;
223 int message_bounce;
224 int is_mp_safe;
225} vl_msg_api_msg_config_t;
226
227typedef struct msgbuf_
228{
229 unix_shared_memory_queue_t *q;
230 u32 data_len;
Dave Barach842b9c52017-01-09 15:54:00 -0500231 u32 gc_mark_timestamp;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100232 u8 data[0];
233} msgbuf_t;
234
235/* api_shared.c prototypes */
236int vl_msg_api_rx_trace_enabled (api_main_t * am);
237int vl_msg_api_tx_trace_enabled (api_main_t * am);
238void vl_msg_api_trace (api_main_t * am, vl_api_trace_t * tp, void *msg);
239int vl_msg_api_trace_onoff (api_main_t * am, vl_api_trace_which_t which,
240 int onoff);
241int vl_msg_api_trace_free (api_main_t * am, vl_api_trace_which_t which);
242int vl_msg_api_trace_save (api_main_t * am,
243 vl_api_trace_which_t which, FILE * fp);
244int vl_msg_api_trace_configure (api_main_t * am, vl_api_trace_which_t which,
245 u32 nitems);
246void vl_msg_api_handler_with_vm_node (api_main_t * am,
247 void *the_msg, vlib_main_t * vm,
248 vlib_node_runtime_t * node);
249void vl_msg_api_handler (void *the_msg);
250void vl_msg_api_handler_no_free (void *the_msg);
251void vl_msg_api_handler_no_trace_no_free (void *the_msg);
252void vl_msg_api_trace_only (void *the_msg);
253void vl_msg_api_cleanup_handler (void *the_msg);
254void vl_msg_api_replay_handler (void *the_msg);
255void vl_msg_api_socket_handler (void *the_msg);
256void vl_msg_api_set_handlers (int msg_id, char *msg_name,
257 void *handler,
258 void *cleanup,
259 void *endian,
260 void *print, int msg_size, int traced);
261void vl_msg_api_config (vl_msg_api_msg_config_t *);
262void vl_msg_api_set_cleanup_handler (int msg_id, void *fp);
263void vl_msg_api_queue_handler (unix_shared_memory_queue_t * q);
264vl_api_trace_t *vl_msg_api_trace_get (api_main_t * am,
265 vl_api_trace_which_t which);
266
Dave Barach80f54e22017-03-08 19:08:56 -0500267void vl_msg_api_barrier_sync (void) __attribute__ ((weak));
268void vl_msg_api_barrier_release (void) __attribute__ ((weak));
Damjan Marion7cd468a2016-12-19 23:05:39 +0100269void vl_msg_api_free (void *);
270void vl_noop_handler (void *mp);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100271void vl_msg_api_increment_missing_client_counter (void);
272void vl_msg_api_post_mortem_dump (void);
Dave Barach80f54e22017-03-08 19:08:56 -0500273void vl_msg_api_post_mortem_dump_enable_disable (int enable);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100274void vl_msg_api_register_pd_handler (void *handler,
275 u16 msg_id_host_byte_order);
276int vl_msg_api_pd_handler (void *mp, int rv);
277
278void vl_msg_api_set_first_available_msg_id (u16 first_avail);
Neale Rannse72be392017-04-26 13:59:20 -0700279u16 vl_msg_api_get_msg_ids (const char *name, int n);
280void vl_msg_api_add_msg_name_crc (api_main_t * am, const char *string,
281 u32 id);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100282u32 vl_api_get_msg_index (u8 * name_and_crc);
283
284/* node_serialize.c prototypes */
285u8 *vlib_node_serialize (vlib_node_main_t * nm, u8 * vector,
286 u32 max_threads, int include_nexts,
287 int include_stats);
288vlib_node_t **vlib_node_unserialize (u8 * vector);
289
290#define VLIB_API_INIT_FUNCTION(x) VLIB_DECLARE_INIT_FUNCTION(x,api_init)
291
Dave Baracha1a093d2017-03-02 13:13:23 -0500292/* Call given init function: used for init function dependencies. */
293#define vlib_call_api_init_function(vm, x) \
294 ({ \
295 extern vlib_init_function_t * _VLIB_INIT_FUNCTION_SYMBOL (x,api_init); \
296 vlib_init_function_t * _f = _VLIB_INIT_FUNCTION_SYMBOL (x,api_init); \
297 clib_error_t * _error = 0; \
298 if (! hash_get (vm->init_functions_called, _f)) \
299 { \
300 hash_set1 (vm->init_functions_called, _f); \
301 _error = _f (vm); \
302 } \
303 _error; \
304 })
305
306
Dave Barachb64e4e22017-03-14 09:10:56 -0400307#define _VL_MSG_API_FUNCTION_SYMBOL(x, type) \
308 _vl_msg_api_##type##_function_##x
309
310#define VL_MSG_API_FUNCTION_SYMBOL(x) \
311 _VL_MSG_API_FUNCTION_SYMBOL(x, reaper)
312
313#define VLIB_DECLARE_REAPER_FUNCTION(x, tag) \
314vl_msg_api_init_function_t * _VL_MSG_API_FUNCTION_SYMBOL (x, tag) = x; \
315static void __vl_msg_api_add_##tag##_function_##x (void) \
316 __attribute__((__constructor__)) ; \
317 \
318static void __vl_msg_api_add_##tag##_function_##x (void) \
319{ \
320 api_main_t * am = &api_main; \
321 static _vl_msg_api_function_list_elt_t _vl_msg_api_function; \
322 _vl_msg_api_function.next_init_function \
323 = am->tag##_function_registrations; \
324 am->tag##_function_registrations = &_vl_msg_api_function; \
325 _vl_msg_api_function.f = &x; \
326}
327
328#define VL_MSG_API_REAPER_FUNCTION(x) VLIB_DECLARE_REAPER_FUNCTION(x,reaper)
329
330/* Call reaper function with client index */
331#define vl_msg_api_call_reaper_function(ci) \
332 ({ \
333 extern vlib_init_function_t * VLIB_INIT_FUNCTION_SYMBOL (reaper); \
334 vlib_init_function_t * _f = VLIB_INIT_FUNCTION_SYMBOL (reaper); \
335 clib_error_t * _error = 0; \
336 _error = _f (ci); \
337 })
338
Damjan Marion7cd468a2016-12-19 23:05:39 +0100339#endif /* included_api_h */
340
341/*
342 * fd.io coding-style-patch-verification: ON
343 *
344 * Local Variables:
345 * eval: (c-set-style "gnu")
346 * End:
347 */