move unix_file_* code to vppinfra
[vpp.git] / src / vlibapi / api_common.h
1 /*
2  *------------------------------------------------------------------
3  * api_common.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_common_h
21 #define included_api_common_h
22
23 #include <vppinfra/clib_error.h>
24 #include <svm/svm_common.h>
25 #include <vlibmemory/unix_shared_memory_queue.h>
26
27 typedef enum
28 {
29   REGISTRATION_TYPE_FREE = 0,
30   REGISTRATION_TYPE_SHMEM,
31   REGISTRATION_TYPE_SOCKET_LISTEN,
32   REGISTRATION_TYPE_SOCKET_SERVER,
33   REGISTRATION_TYPE_SOCKET_CLIENT,
34 } vl_registration_type_t;
35
36 typedef struct vl_api_registration_
37 {
38   vl_registration_type_t registration_type;
39
40   /* Index in VLIB's brain (not shared memory). */
41   u32 vl_api_registration_pool_index;
42
43   u8 *name;
44
45   /*
46    * The following groups of data could be unioned, but my fingers are
47    * going to be sore enough.
48    */
49
50   /* shared memory only */
51   unix_shared_memory_queue_t *vl_input_queue;
52
53   /* socket server and client */
54   u32 clib_file_index;
55   i8 *unprocessed_input;
56   u32 unprocessed_msg_length;
57   u8 *output_vector;
58
59   /* socket client only */
60   u32 server_handle;
61   u32 server_index;
62
63 } vl_api_registration_t;
64
65
66 /* Trace configuration for a single message */
67 typedef struct
68 {
69   int size;
70   int trace_enable;
71   int replay_enable;
72 } trace_cfg_t;
73
74 /*
75  * API recording
76  */
77 typedef struct
78 {
79   u8 endian;
80   u8 enabled;
81   u8 wrapped;
82   u8 pad;
83   u32 nitems;
84   u32 curindex;
85   u8 **traces;
86 } vl_api_trace_t;
87
88 typedef enum
89 {
90   VL_API_TRACE_TX,
91   VL_API_TRACE_RX,
92 } vl_api_trace_which_t;
93
94 #define VL_API_LITTLE_ENDIAN 0x00
95 #define VL_API_BIG_ENDIAN 0x01
96
97 typedef struct
98 {
99   u8 *name;
100   u16 first_msg_id;
101   u16 last_msg_id;
102 } vl_api_msg_range_t;
103
104 typedef struct
105 {
106   int id;
107   char *name;
108   u32 crc;
109   void *handler;
110   void *cleanup;
111   void *endian;
112   void *print;
113   int size;
114   int traced;
115   int replay;
116   int message_bounce;
117   int is_mp_safe;
118 } vl_msg_api_msg_config_t;
119
120 typedef struct msgbuf_
121 {
122   unix_shared_memory_queue_t *q;
123   u32 data_len;
124   u32 gc_mark_timestamp;
125   u8 data[0];
126 } msgbuf_t;
127
128 /* api_shared.c prototypes */
129 void vl_msg_api_handler (void *the_msg);
130 void vl_msg_api_handler_no_free (void *the_msg);
131 void vl_msg_api_handler_no_trace_no_free (void *the_msg);
132 void vl_msg_api_trace_only (void *the_msg);
133 void vl_msg_api_cleanup_handler (void *the_msg);
134 void vl_msg_api_replay_handler (void *the_msg);
135 void vl_msg_api_socket_handler (void *the_msg);
136 void vl_msg_api_set_handlers (int msg_id, char *msg_name,
137                               void *handler,
138                               void *cleanup,
139                               void *endian,
140                               void *print, int msg_size, int traced);
141 void vl_msg_api_config (vl_msg_api_msg_config_t *);
142 void vl_msg_api_set_cleanup_handler (int msg_id, void *fp);
143 void vl_msg_api_queue_handler (unix_shared_memory_queue_t * q);
144
145 void vl_msg_api_barrier_sync (void) __attribute__ ((weak));
146 void vl_msg_api_barrier_release (void) __attribute__ ((weak));
147 void vl_msg_api_free (void *);
148 void vl_noop_handler (void *mp);
149 void vl_msg_api_increment_missing_client_counter (void);
150 void vl_msg_api_post_mortem_dump (void);
151 void vl_msg_api_post_mortem_dump_enable_disable (int enable);
152 void vl_msg_api_register_pd_handler (void *handler,
153                                      u16 msg_id_host_byte_order);
154 int vl_msg_api_pd_handler (void *mp, int rv);
155
156 void vl_msg_api_set_first_available_msg_id (u16 first_avail);
157 u16 vl_msg_api_get_msg_ids (const char *name, int n);
158 u32 vl_api_get_msg_index (u8 * name_and_crc);
159
160 typedef clib_error_t *(vl_msg_api_init_function_t) (u32 client_index);
161
162 typedef struct _vl_msg_api_init_function_list_elt
163 {
164   struct _vl_msg_api_init_function_list_elt *next_init_function;
165   vl_msg_api_init_function_t *f;
166 } _vl_msg_api_function_list_elt_t;
167
168 typedef struct
169 {
170   void (**msg_handlers) (void *);
171   int (**pd_msg_handlers) (void *, int);
172   void (**msg_cleanup_handlers) (void *);
173   void (**msg_endian_handlers) (void *);
174   void (**msg_print_handlers) (void *, void *);
175   const char **msg_names;
176   u8 *message_bounce;
177   u8 *is_mp_safe;
178   struct ring_alloc_ *arings;
179   u32 ring_misses;
180   u32 garbage_collects;
181   u32 missing_clients;
182   vl_api_trace_t *rx_trace;
183   vl_api_trace_t *tx_trace;
184   int msg_print_flag;
185   trace_cfg_t *api_trace_cfg;
186   int our_pid;
187   svm_region_t *vlib_rp;
188   svm_region_t **mapped_shmem_regions;
189   struct vl_shmem_hdr_ *shmem_hdr;
190   vl_api_registration_t **vl_clients;
191
192   u8 *serialized_message_table_in_shmem;
193
194   /* For plugin msg allocator */
195   u16 first_available_msg_id;
196
197   /* message range by name hash */
198   uword *msg_range_by_name;
199
200   /* vector of message ranges */
201   vl_api_msg_range_t *msg_ranges;
202
203   /* uid for the api shared memory region */
204   int api_uid;
205   /* gid for the api shared memory region */
206   int api_gid;
207
208   /* base virtual address for global VM region */
209   u64 global_baseva;
210
211   /* size of the global VM region */
212   u64 global_size;
213
214   /* size of the API region */
215   u64 api_size;
216
217   /* size of the global VM private mheap */
218   u64 global_pvt_heap_size;
219
220   /* size of the api private mheap */
221   u64 api_pvt_heap_size;
222
223   /* Client-only data structures */
224   unix_shared_memory_queue_t *vl_input_queue;
225
226   /*
227    * All VLIB-side message handlers use my_client_index to identify
228    * the queue / client. This works in sim replay.
229    */
230   int my_client_index;
231   /*
232    * This is the (shared VM) address of the registration,
233    * don't use it to id the connection since it can't possibly
234    * work in simulator replay.
235    */
236   vl_api_registration_t *my_registration;
237
238   i32 vlib_signal;
239
240   /* vlib input queue length */
241   u32 vlib_input_queue_length;
242
243   /* client side message index hash table */
244   uword *msg_index_by_name_and_crc;
245
246   const char *region_name;
247   const char *root_path;
248
249   /* Replay in progress? */
250   int replay_in_progress;
251
252   /* List of API client reaper functions */
253   _vl_msg_api_function_list_elt_t *reaper_function_registrations;
254
255 } api_main_t;
256
257 extern api_main_t api_main;
258
259
260 #endif /* included_api_common_h */
261
262 /*
263  * fd.io coding-style-patch-verification: ON
264  *
265  * Local Variables:
266  * eval: (c-set-style "gnu")
267  * End:
268  */