66a547fb93d1d938431daa59bc35cfdc37935ded
[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 /** \file api_common.h
24  *  API common definitions
25  * See api_doc.md for more info
26  */
27
28 #include <vppinfra/clib_error.h>
29 #include <vppinfra/elog.h>
30 #include <vppinfra/cJSON.h>
31 #include <vlibapi/api_types.h>
32 #include <svm/svm_common.h>
33 #include <svm/queue.h>
34
35 /** API registration types
36  */
37 typedef enum
38 {
39   REGISTRATION_TYPE_FREE = 0,
40   REGISTRATION_TYPE_SHMEM,      /**< Shared memory connection */
41   REGISTRATION_TYPE_SOCKET_LISTEN, /**< Socket listener  */
42   REGISTRATION_TYPE_SOCKET_SERVER, /**< Socket server */
43   REGISTRATION_TYPE_SOCKET_CLIENT, /**< Socket client */
44 } vl_registration_type_t;
45
46 /** An API client registration, only in vpp/vlib */
47
48 typedef struct vl_api_registration_
49 {
50   vl_registration_type_t registration_type; /**< type */
51
52   /** Index in VLIB's brain (not shared memory). */
53   u32 vl_api_registration_pool_index;
54
55   u8 *name;                     /**< Client name */
56
57   /* Zombie apocalypse checking */
58   f64 last_heard;
59   int last_queue_head;
60   int unanswered_pings;
61   int is_being_removed;
62
63   /** shared memory only: pointer to client input queue */
64   svm_queue_t *vl_input_queue;
65   svm_region_t *vlib_rp;
66   void *shmem_hdr;
67
68   /* socket server and client */
69   u32 clib_file_index;          /**< Socket only: file index */
70   i8 *unprocessed_input;        /**< Socket only: pending input */
71   u32 unprocessed_msg_length;   /**< Socket only: unprocssed length */
72   u8 *output_vector;            /**< Socket only: output vector */
73   int *additional_fds_to_close;
74
75   /* socket client only */
76   u32 server_handle;            /**< Socket client only: server handle */
77   u32 server_index;             /**< Socket client only: server index */
78
79   bool keepalive; /**< Dead client scan */
80 } vl_api_registration_t;
81
82 #define VL_API_INVALID_FI ((u32)~0)
83
84 /**
85  * API trace state
86  */
87 typedef struct
88 {
89   u8 endian;                    /**< trace endianness */
90   u8 enabled;                   /**< trace is enabled  */
91   u8 wrapped;                   /**< trace has wrapped */
92   u8 pad;
93   u32 nitems;                   /**< Number of trace records */
94   u32 curindex;                 /**< Current index in circular buffer  */
95   u8 **traces;                  /**< Trace ring */
96 } vl_api_trace_t;
97
98 /** Trace RX / TX enum */
99 typedef 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
108 /** Message range (belonging to a plugin) */
109 typedef struct
110 {
111   u8 *name;                     /**< name of the plugin  */
112   u16 first_msg_id;             /**< first assigned message ID */
113   u16 last_msg_id;              /**< last assigned message ID */
114 } vl_api_msg_range_t;
115
116 /** Message configuration definition */
117 typedef struct
118 {
119   int id;                       /**< the message ID */
120   char *name;                   /**< the message name */
121   u32 crc;                      /**< message definition CRC  */
122   void *handler;                /**< the message handler  */
123   void *cleanup;                /**< non-default message cleanup handler */
124   void *endian;                 /**< message endian function  */
125   void *print;                  /**< message print function  */
126   void *print_json;             /**< message print function (JSON format)  */
127   void *tojson;                 /**< binary to JSON convert function */
128   void *fromjson;               /**< JSON to binary convert function */
129   void *calc_size;              /**< message size calculation */
130   int size;                     /**< message size  */
131   int traced : 1;               /**< is this message to be traced?  */
132   int replay : 1;               /**< is this message to be replayed?  */
133   int message_bounce : 1;       /**< do not free message after processing */
134   int is_mp_safe : 1;           /**< worker thread barrier required?  */
135   int is_autoendian : 1;        /**< endian conversion required?  */
136 } vl_msg_api_msg_config_t;
137
138 /** Message header structure */
139 typedef struct msgbuf_
140 {
141   svm_queue_t *q; /**< message allocated in this shmem ring  */
142   u32 data_len;                  /**< message length not including header  */
143   u32 gc_mark_timestamp;         /**< message garbage collector mark TS  */
144   u8 data[0];                    /**< actual message begins here  */
145 } msgbuf_t;
146
147 __clib_nosanitize_addr static inline void
148 VL_MSG_API_UNPOISON (const void *a)
149 {
150   const msgbuf_t *m = &((const msgbuf_t *) a)[-1];
151   clib_mem_unpoison (m, sizeof (*m) + ntohl (m->data_len));
152 }
153
154 __clib_nosanitize_addr static inline void
155 VL_MSG_API_SVM_QUEUE_UNPOISON (const svm_queue_t *q)
156 {
157   clib_mem_unpoison (q, sizeof (*q) + q->elsize * q->maxsize);
158 }
159
160 static inline void
161 VL_MSG_API_POISON (const void *a)
162 {
163   const msgbuf_t *m = &((const msgbuf_t *) a)[-1];
164   clib_mem_poison (m, sizeof (*m) + ntohl (m->data_len));
165 }
166
167 /* api_shared.c prototypes */
168 void vl_msg_api_handler (void *the_msg, uword msg_len);
169 void vl_msg_api_handler_no_free (void *the_msg, uword msg_len);
170 void vl_msg_api_handler_no_trace_no_free (void *the_msg, uword msg_len);
171 void vl_msg_api_trace_only (void *the_msg, uword msg_len);
172 void vl_msg_api_cleanup_handler (void *the_msg);
173 void vl_msg_api_replay_handler (void *the_msg);
174 void vl_msg_api_socket_handler (void *the_msg, uword msg_len);
175 void vl_msg_api_set_handlers (int msg_id, char *msg_name, void *handler,
176                               void *cleanup, void *endian, void *print,
177                               int msg_size, int traced, void *print_json,
178                               void *tojson, void *fromjson,
179                               void *validate_size);
180 void vl_msg_api_clean_handlers (int msg_id);
181 void vl_msg_api_config (vl_msg_api_msg_config_t *);
182 void vl_msg_api_set_cleanup_handler (int msg_id, void *fp);
183 void vl_msg_api_queue_handler (svm_queue_t * q);
184
185 void vl_msg_api_barrier_sync (void) __attribute__ ((weak));
186 void vl_msg_api_barrier_release (void) __attribute__ ((weak));
187 #ifdef BARRIER_TRACING
188 void vl_msg_api_barrier_trace_context (const char *context)
189   __attribute__ ((weak));
190 #else
191 #define vl_msg_api_barrier_trace_context(X)
192 #endif
193 void vl_msg_api_free (void *);
194 void vl_noop_handler (void *mp);
195 void vl_msg_api_increment_missing_client_counter (void);
196 void vl_msg_api_post_mortem_dump (void);
197 void vl_msg_api_post_mortem_dump_enable_disable (int enable);
198 void vl_msg_api_register_pd_handler (void *handler,
199                                      u16 msg_id_host_byte_order);
200 int vl_msg_api_pd_handler (void *mp, int rv);
201
202 void vl_msg_api_set_first_available_msg_id (u16 first_avail);
203 u16 vl_msg_api_get_msg_ids (const char *name, int n);
204 u32 vl_msg_api_get_msg_index (u8 * name_and_crc);
205 void *vl_msg_push_heap (void);
206 void *vl_msg_push_heap_w_region (svm_region_t * vlib_rp);
207 void vl_msg_pop_heap (void *oldheap);
208 void vl_msg_pop_heap_w_region (svm_region_t * vlib_rp, void *oldheap);
209
210 typedef clib_error_t *(vl_msg_api_init_function_t) (u32 client_index);
211
212 typedef struct _vl_msg_api_init_function_list_elt
213 {
214   struct _vl_msg_api_init_function_list_elt *next_init_function;
215   vl_msg_api_init_function_t *f;
216 } _vl_msg_api_function_list_elt_t;
217
218 typedef struct
219 {
220   u32 major;
221   u32 minor;
222   u32 patch;
223   char name[64];
224 } api_version_t;
225
226 typedef struct
227 {
228   /** Message handler vector  */
229   void (*handler) (void *);
230
231   /** non-default message cleanup handler vector */
232   void (*cleanup_handler) (void *);
233
234   /** Message name vector */
235   const char *name;
236
237   /** Message convert function vector */
238   cJSON *(*tojson_handler) (void *);
239
240   /** Message convert function vector */
241   void *(*fromjson_handler) (cJSON *, int *);
242
243   /** Message endian handler vector */
244   void (*endian_handler) (void *);
245
246   /** Message print function vector */
247   void (*print_handler) (void *, void *);
248
249   /** Message print function vector in JSON */
250   void (*print_json_handler) (void *, void *);
251
252   /** Message calc size function vector */
253   uword (*calc_size_func) (void *);
254
255   /** trace size for sanity checking */
256   int trace_size;
257
258   /** Flags */
259   u8 bounce : 1;         /**> Don't automatically free message buffer vetor */
260   u8 is_mp_safe : 1;     /**< Message is mp safe vector */
261   u8 is_autoendian : 1;  /**< Message requires us to do endian conversion */
262   u8 trace_enable : 1;   /**< trace this message  */
263   u8 replay_allowed : 1; /**< This message can be replayed  */
264
265 } vl_api_msg_data_t;
266
267 /** API main structure, used by both vpp and binary API clients */
268 typedef struct api_main_t
269 {
270   vl_api_msg_data_t *msg_data;
271
272   /** API message ID by name hash table */
273   uword *msg_id_by_name;
274
275   /** Allocator ring vectors (in shared memory) */
276   struct ring_alloc_ *arings;
277
278   /** Number of times that the ring allocator failed */
279   u32 ring_misses;
280
281   /** Number of garbage-collected message buffers */
282   u32 garbage_collects;
283
284   /** Number of missing clients / failed message sends */
285   u32 missing_clients;
286
287   /** Received message trace configuration */
288   vl_api_trace_t *rx_trace;
289
290   /** Sent message trace configuration */
291   vl_api_trace_t *tx_trace;
292
293   /** Print every received message */
294   int msg_print_flag;
295
296   /** Current process PID */
297   int our_pid;
298
299   /** Current binary api segment descriptor */
300   svm_region_t *vlib_rp;
301
302   /** Primary api segment descriptor */
303   svm_region_t *vlib_primary_rp;
304
305   /** Vector of all mapped shared-VM segments */
306   svm_region_t **vlib_private_rps;
307   svm_region_t **mapped_shmem_regions;
308
309   /** Binary API shared-memory segment header pointer */
310   struct vl_shmem_hdr_ *shmem_hdr;
311
312   /** vlib/vpp only: vector of client registrations */
313   vl_api_registration_t **vl_clients;
314
315   /** vlib/vpp only: serialized (message, name, crc) table */
316   u8 *serialized_message_table_in_shmem;
317
318   /** First available message ID, for theplugin msg allocator */
319   u16 first_available_msg_id;
320
321   /** Message range by name hash */
322   uword *msg_range_by_name;
323
324   /** vector of message ranges */
325   vl_api_msg_range_t *msg_ranges;
326
327   /** uid for the api shared memory region */
328   int api_uid;
329
330   /** gid for the api shared memory region */
331   int api_gid;
332
333   /** base virtual address for global VM region */
334   u64 global_baseva;
335
336   /** size of the global VM region */
337   u64 global_size;
338
339   /** size of the API region */
340   u64 api_size;
341
342   /** size of the global VM private mheap */
343   u64 global_pvt_heap_size;
344
345   /** size of the api private mheap */
346   u64 api_pvt_heap_size;
347
348   /** Peer input queue pointer */
349   svm_queue_t *vl_input_queue;
350
351   /**
352    * All VLIB-side message handlers use my_client_index to identify
353    * the queue / client. This works in sim replay.
354    */
355   int my_client_index;
356   /**
357    * This is the (shared VM) address of the registration,
358    * don't use it to id the connection since it can't possibly
359    * work in simulator replay.
360    */
361   vl_api_registration_t *my_registration;
362
363   /** vpp/vlib input queue length */
364   u32 vlib_input_queue_length;
365
366   /** client message index hash table */
367   uword *msg_index_by_name_and_crc;
368
369   /** api version list */
370   api_version_t *api_version_list;
371
372   /** Shared VM binary API region name */
373   const char *region_name;
374
375   /** Chroot path to the shared memory API files */
376   const char *root_path;
377
378   /** Replay in progress? */
379   int replay_in_progress;
380
381   /** Dump (msg-name, crc) snapshot here at startup */
382   u8 *save_msg_table_filename;
383
384   /** List of API client reaper functions */
385   _vl_msg_api_function_list_elt_t *reaper_function_registrations;
386
387   /** Bin API thread handle */
388   pthread_t rx_thread_handle;
389
390   /** event log */
391   elog_main_t *elog_main;
392   int elog_trace_api_messages;
393
394   /** performance counter callback **/
395   void (**perf_counter_cbs)
396     (struct api_main_t *, u32 id, int before_or_after);
397   void (**perf_counter_cbs_tmp)
398     (struct api_main_t *, u32 id, int before_or_after);
399
400 } api_main_t;
401
402 extern __thread api_main_t *my_api_main;
403
404 always_inline api_main_t *
405 vlibapi_get_main (void)
406 {
407   return my_api_main;
408 }
409
410 always_inline vl_api_msg_data_t *
411 vl_api_get_msg_data (api_main_t *am, u32 msg_id)
412 {
413   if (msg_id >= vec_len (am->msg_data))
414     return 0;
415   return am->msg_data + msg_id;
416 }
417
418 always_inline void
419 vlibapi_set_main (api_main_t * am)
420 {
421   my_api_main = am;
422 }
423
424 #endif /* included_api_common_h */
425
426 /*
427  * fd.io coding-style-patch-verification: ON
428  *
429  * Local Variables:
430  * eval: (c-set-style "gnu")
431  * End:
432  */