Improve fifo allocator performance
[vpp.git] / src / vlibapi / api.h
1 /*
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 #include <stddef.h>
29
30 typedef 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
39 typedef 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 */
70 typedef struct
71 {
72   int size;
73   int trace_enable;
74   int replay_enable;
75 } trace_cfg_t;
76
77 /*
78  * API recording
79  */
80 typedef 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* */
92 typedef CLIB_PACKED
93 (struct
94  {
95    u8 endian; u8 wrapped;
96    u32 nitems;
97 }) vl_api_trace_file_header_t;
98 /* *INDENT-ON* */
99
100 typedef 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
109 typedef struct
110 {
111   u8 *name;
112   u16 first_msg_id;
113   u16 last_msg_id;
114 } vl_api_msg_range_t;
115
116 typedef clib_error_t *(vl_msg_api_init_function_t) (u32 client_index);
117
118 typedef 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
124 typedef 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 *);
131   const char **msg_names;
132   u8 *message_bounce;
133   u8 *is_mp_safe;
134   struct ring_alloc_ *arings;
135   u32 ring_misses;
136   u32 garbage_collects;
137   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   /* vlib input queue length */
197   u32 vlib_input_queue_length;
198
199   /* client side message index hash table */
200   uword *msg_index_by_name_and_crc;
201
202   const char *region_name;
203   const char *root_path;
204
205   /* Replay in progress? */
206   int replay_in_progress;
207
208   /* List of API client reaper functions */
209   _vl_msg_api_function_list_elt_t *reaper_function_registrations;
210
211 } api_main_t;
212
213 extern api_main_t api_main;
214
215 typedef 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
231 typedef struct msgbuf_
232 {
233   unix_shared_memory_queue_t *q;
234   u32 data_len;
235   u32 gc_mark_timestamp;
236   u8 data[0];
237 } msgbuf_t;
238
239 /* api_shared.c prototypes */
240 int vl_msg_api_rx_trace_enabled (api_main_t * am);
241 int vl_msg_api_tx_trace_enabled (api_main_t * am);
242 void vl_msg_api_trace (api_main_t * am, vl_api_trace_t * tp, void *msg);
243 int vl_msg_api_trace_onoff (api_main_t * am, vl_api_trace_which_t which,
244                             int onoff);
245 int vl_msg_api_trace_free (api_main_t * am, vl_api_trace_which_t which);
246 int vl_msg_api_trace_save (api_main_t * am,
247                            vl_api_trace_which_t which, FILE * fp);
248 int vl_msg_api_trace_configure (api_main_t * am, vl_api_trace_which_t which,
249                                 u32 nitems);
250 void 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);
253 void vl_msg_api_handler (void *the_msg);
254 void vl_msg_api_handler_no_free (void *the_msg);
255 void vl_msg_api_handler_no_trace_no_free (void *the_msg);
256 void vl_msg_api_trace_only (void *the_msg);
257 void vl_msg_api_cleanup_handler (void *the_msg);
258 void vl_msg_api_replay_handler (void *the_msg);
259 void vl_msg_api_socket_handler (void *the_msg);
260 void 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);
265 void vl_msg_api_config (vl_msg_api_msg_config_t *);
266 void vl_msg_api_set_cleanup_handler (int msg_id, void *fp);
267 void vl_msg_api_queue_handler (unix_shared_memory_queue_t * q);
268 vl_api_trace_t *vl_msg_api_trace_get (api_main_t * am,
269                                       vl_api_trace_which_t which);
270
271 void vl_msg_api_barrier_sync (void) __attribute__ ((weak));
272 void vl_msg_api_barrier_release (void) __attribute__ ((weak));
273 void vl_msg_api_free (void *);
274 void vl_noop_handler (void *mp);
275 void vl_msg_api_increment_missing_client_counter (void);
276 void vl_msg_api_post_mortem_dump (void);
277 void vl_msg_api_post_mortem_dump_enable_disable (int enable);
278 void vl_msg_api_register_pd_handler (void *handler,
279                                      u16 msg_id_host_byte_order);
280 int vl_msg_api_pd_handler (void *mp, int rv);
281
282 void vl_msg_api_set_first_available_msg_id (u16 first_avail);
283 u16 vl_msg_api_get_msg_ids (const char *name, int n);
284 void vl_msg_api_add_msg_name_crc (api_main_t * am, const char *string,
285                                   u32 id);
286 u32 vl_api_get_msg_index (u8 * name_and_crc);
287 u32 vl_msg_api_get_msg_length (void *msg_arg);
288
289 /* node_serialize.c prototypes */
290 u8 *vlib_node_serialize (vlib_node_main_t * nm, u8 * vector,
291                          u32 max_threads, int include_nexts,
292                          int include_stats);
293 vlib_node_t **vlib_node_unserialize (u8 * vector);
294
295 #define VLIB_API_INIT_FUNCTION(x) VLIB_DECLARE_INIT_FUNCTION(x,api_init)
296
297 /* 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
312 #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)                            \
319 vl_msg_api_init_function_t * _VL_MSG_API_FUNCTION_SYMBOL (x, tag) = x;  \
320 static void __vl_msg_api_add_##tag##_function_##x (void)                \
321     __attribute__((__constructor__)) ;                                  \
322                                                                         \
323 static 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
344 static inline u32
345 vl_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 }
353 #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  */