blob: 3403e1c60d744ac3e1d029a24a206997219d5046 [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
196 /* client side message index hash table */
197 uword *msg_index_by_name_and_crc;
198
Neale Rannse72be392017-04-26 13:59:20 -0700199 const char *region_name;
200 const char *root_path;
Dave Barachdfbee412017-03-02 18:24:10 -0500201
202 /* Replay in progress? */
203 int replay_in_progress;
Dave Barachb64e4e22017-03-14 09:10:56 -0400204
205 /* List of API client reaper functions */
206 _vl_msg_api_function_list_elt_t *reaper_function_registrations;
207
Damjan Marion7cd468a2016-12-19 23:05:39 +0100208} api_main_t;
209
210extern api_main_t api_main;
211
212typedef struct
213{
214 int id;
215 char *name;
216 u32 crc;
217 void *handler;
218 void *cleanup;
219 void *endian;
220 void *print;
221 int size;
222 int traced;
223 int replay;
224 int message_bounce;
225 int is_mp_safe;
226} vl_msg_api_msg_config_t;
227
228typedef struct msgbuf_
229{
230 unix_shared_memory_queue_t *q;
231 u32 data_len;
Dave Barach842b9c52017-01-09 15:54:00 -0500232 u32 gc_mark_timestamp;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100233 u8 data[0];
234} msgbuf_t;
235
236/* api_shared.c prototypes */
237int vl_msg_api_rx_trace_enabled (api_main_t * am);
238int vl_msg_api_tx_trace_enabled (api_main_t * am);
239void vl_msg_api_trace (api_main_t * am, vl_api_trace_t * tp, void *msg);
240int vl_msg_api_trace_onoff (api_main_t * am, vl_api_trace_which_t which,
241 int onoff);
242int vl_msg_api_trace_free (api_main_t * am, vl_api_trace_which_t which);
243int vl_msg_api_trace_save (api_main_t * am,
244 vl_api_trace_which_t which, FILE * fp);
245int vl_msg_api_trace_configure (api_main_t * am, vl_api_trace_which_t which,
246 u32 nitems);
247void vl_msg_api_handler_with_vm_node (api_main_t * am,
248 void *the_msg, vlib_main_t * vm,
249 vlib_node_runtime_t * node);
250void vl_msg_api_handler (void *the_msg);
251void vl_msg_api_handler_no_free (void *the_msg);
252void vl_msg_api_handler_no_trace_no_free (void *the_msg);
253void vl_msg_api_trace_only (void *the_msg);
254void vl_msg_api_cleanup_handler (void *the_msg);
255void vl_msg_api_replay_handler (void *the_msg);
256void vl_msg_api_socket_handler (void *the_msg);
257void vl_msg_api_set_handlers (int msg_id, char *msg_name,
258 void *handler,
259 void *cleanup,
260 void *endian,
261 void *print, int msg_size, int traced);
262void vl_msg_api_config (vl_msg_api_msg_config_t *);
263void vl_msg_api_set_cleanup_handler (int msg_id, void *fp);
264void vl_msg_api_queue_handler (unix_shared_memory_queue_t * q);
265vl_api_trace_t *vl_msg_api_trace_get (api_main_t * am,
266 vl_api_trace_which_t which);
267
Dave Barach80f54e22017-03-08 19:08:56 -0500268void vl_msg_api_barrier_sync (void) __attribute__ ((weak));
269void vl_msg_api_barrier_release (void) __attribute__ ((weak));
Damjan Marion7cd468a2016-12-19 23:05:39 +0100270void vl_msg_api_free (void *);
271void vl_noop_handler (void *mp);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100272void vl_msg_api_increment_missing_client_counter (void);
273void vl_msg_api_post_mortem_dump (void);
Dave Barach80f54e22017-03-08 19:08:56 -0500274void vl_msg_api_post_mortem_dump_enable_disable (int enable);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100275void vl_msg_api_register_pd_handler (void *handler,
276 u16 msg_id_host_byte_order);
277int vl_msg_api_pd_handler (void *mp, int rv);
278
279void vl_msg_api_set_first_available_msg_id (u16 first_avail);
Neale Rannse72be392017-04-26 13:59:20 -0700280u16 vl_msg_api_get_msg_ids (const char *name, int n);
281void vl_msg_api_add_msg_name_crc (api_main_t * am, const char *string,
282 u32 id);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100283u32 vl_api_get_msg_index (u8 * name_and_crc);
Dave Barachbfd92272017-05-12 11:59:25 -0400284u32 vl_msg_api_get_msg_length (void *msg_arg);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100285
286/* node_serialize.c prototypes */
287u8 *vlib_node_serialize (vlib_node_main_t * nm, u8 * vector,
288 u32 max_threads, int include_nexts,
289 int include_stats);
290vlib_node_t **vlib_node_unserialize (u8 * vector);
291
292#define VLIB_API_INIT_FUNCTION(x) VLIB_DECLARE_INIT_FUNCTION(x,api_init)
293
Dave Baracha1a093d2017-03-02 13:13:23 -0500294/* Call given init function: used for init function dependencies. */
295#define vlib_call_api_init_function(vm, x) \
296 ({ \
297 extern vlib_init_function_t * _VLIB_INIT_FUNCTION_SYMBOL (x,api_init); \
298 vlib_init_function_t * _f = _VLIB_INIT_FUNCTION_SYMBOL (x,api_init); \
299 clib_error_t * _error = 0; \
300 if (! hash_get (vm->init_functions_called, _f)) \
301 { \
302 hash_set1 (vm->init_functions_called, _f); \
303 _error = _f (vm); \
304 } \
305 _error; \
306 })
307
308
Dave Barachb64e4e22017-03-14 09:10:56 -0400309#define _VL_MSG_API_FUNCTION_SYMBOL(x, type) \
310 _vl_msg_api_##type##_function_##x
311
312#define VL_MSG_API_FUNCTION_SYMBOL(x) \
313 _VL_MSG_API_FUNCTION_SYMBOL(x, reaper)
314
315#define VLIB_DECLARE_REAPER_FUNCTION(x, tag) \
316vl_msg_api_init_function_t * _VL_MSG_API_FUNCTION_SYMBOL (x, tag) = x; \
317static void __vl_msg_api_add_##tag##_function_##x (void) \
318 __attribute__((__constructor__)) ; \
319 \
320static void __vl_msg_api_add_##tag##_function_##x (void) \
321{ \
322 api_main_t * am = &api_main; \
323 static _vl_msg_api_function_list_elt_t _vl_msg_api_function; \
324 _vl_msg_api_function.next_init_function \
325 = am->tag##_function_registrations; \
326 am->tag##_function_registrations = &_vl_msg_api_function; \
327 _vl_msg_api_function.f = &x; \
328}
329
330#define VL_MSG_API_REAPER_FUNCTION(x) VLIB_DECLARE_REAPER_FUNCTION(x,reaper)
331
332/* Call reaper function with client index */
333#define vl_msg_api_call_reaper_function(ci) \
334 ({ \
335 extern vlib_init_function_t * VLIB_INIT_FUNCTION_SYMBOL (reaper); \
336 vlib_init_function_t * _f = VLIB_INIT_FUNCTION_SYMBOL (reaper); \
337 clib_error_t * _error = 0; \
338 _error = _f (ci); \
339 })
340
Dave Barachbfd92272017-05-12 11:59:25 -0400341static inline u32
342vl_msg_api_get_msg_length_inline (void *msg_arg)
343{
344 u8 *msg = (u8 *) msg_arg;
345
346 msgbuf_t *header = (msgbuf_t *) (msg - offsetof (msgbuf_t, data));
347
348 return clib_net_to_host_u32 (header->data_len);
349}
Damjan Marion7cd468a2016-12-19 23:05:39 +0100350#endif /* included_api_h */
351
352/*
353 * fd.io coding-style-patch-verification: ON
354 *
355 * Local Variables:
356 * eval: (c-set-style "gnu")
357 * End:
358 */