api: API trace improvements
[vpp.git] / src / vlibapi / api_common.h
index 8e6ab0f..a955636 100644 (file)
 #ifndef included_api_common_h
 #define included_api_common_h
 
-/** \file API common definitions
+/** \file api_common.h
+ *  API common definitions
  * See api_doc.md for more info
  */
 
 #include <vppinfra/clib_error.h>
+#include <vppinfra/elog.h>
+#include <vppinfra/cJSON.h>
+#include <vlibapi/api_types.h>
 #include <svm/svm_common.h>
-#include <vlibmemory/unix_shared_memory_queue.h>
+#include <svm/queue.h>
 
 /** API registration types
  */
@@ -50,20 +54,30 @@ typedef struct vl_api_registration_
 
   u8 *name;                    /**< Client name */
 
+  /* Zombie apocalypse checking */
+  f64 last_heard;
+  int last_queue_head;
+  int unanswered_pings;
+  int is_being_removed;
+
   /** shared memory only: pointer to client input queue */
-  unix_shared_memory_queue_t *vl_input_queue;
+  svm_queue_t *vl_input_queue;
+  svm_region_t *vlib_rp;
+  void *shmem_hdr;
 
   /* socket server and client */
   u32 clib_file_index;         /**< Socket only: file index */
   i8 *unprocessed_input;       /**< Socket only: pending input */
   u32 unprocessed_msg_length;  /**< Socket only: unprocssed length */
-  u8 *output_vector;           /**< Socket only: output vecto  */
+  u8 *output_vector;           /**< Socket only: output vector */
+  int *additional_fds_to_close;
 
   /* socket client only */
   u32 server_handle;           /**< Socket client only: server handle */
   u32 server_index;            /**< Socket client only: server index */
 } vl_api_registration_t;
 
+#define VL_API_INVALID_FI ((u32)~0)
 
 /** Trace configuration for a single message */
 typedef struct
@@ -115,22 +129,46 @@ typedef struct
   void *cleanup;               /**< non-default message cleanup handler */
   void *endian;                        /**< message endian function  */
   void *print;                 /**< message print function  */
+  void *print_json;            /**< message print function (JSON format)  */
+  void *tojson;                        /**< binary to JSON convert function */
+  void *fromjson;              /**< JSON to binary convert function */
   int size;                    /**< message size  */
   int traced;                  /**< is this message to be traced?  */
   int replay;                  /**< is this message to be replayed?  */
   int message_bounce;          /**< do not free message after processing */
   int is_mp_safe;              /**< worker thread barrier required?  */
+  int is_autoendian;           /**< endian conversion required?  */
 } vl_msg_api_msg_config_t;
 
 /** Message header structure */
 typedef struct msgbuf_
 {
-  unix_shared_memory_queue_t *q; /**< message allocated in this shmem ring  */
+  svm_queue_t *q; /**< message allocated in this shmem ring  */
   u32 data_len;                         /**< message length not including header  */
   u32 gc_mark_timestamp;        /**< message garbage collector mark TS  */
   u8 data[0];                   /**< actual message begins here  */
 } msgbuf_t;
 
+CLIB_NOSANITIZE_ADDR static inline void
+VL_MSG_API_UNPOISON (const void *a)
+{
+  const msgbuf_t *m = &((const msgbuf_t *) a)[-1];
+  CLIB_MEM_UNPOISON (m, sizeof (*m) + ntohl (m->data_len));
+}
+
+CLIB_NOSANITIZE_ADDR static inline void
+VL_MSG_API_SVM_QUEUE_UNPOISON (const svm_queue_t * q)
+{
+  CLIB_MEM_UNPOISON (q, sizeof (*q) + q->elsize * q->maxsize);
+}
+
+static inline void
+VL_MSG_API_POISON (const void *a)
+{
+  const msgbuf_t *m = &((const msgbuf_t *) a)[-1];
+  CLIB_MEM_POISON (m, sizeof (*m) + ntohl (m->data_len));
+}
+
 /* api_shared.c prototypes */
 void vl_msg_api_handler (void *the_msg);
 void vl_msg_api_handler_no_free (void *the_msg);
@@ -139,15 +177,14 @@ void vl_msg_api_trace_only (void *the_msg);
 void vl_msg_api_cleanup_handler (void *the_msg);
 void vl_msg_api_replay_handler (void *the_msg);
 void vl_msg_api_socket_handler (void *the_msg);
-void vl_msg_api_set_handlers (int msg_id, char *msg_name,
-                             void *handler,
-                             void *cleanup,
-                             void *endian,
-                             void *print, int msg_size, int traced);
+void vl_msg_api_set_handlers (int msg_id, char *msg_name, void *handler,
+                             void *cleanup, void *endian, void *print,
+                             int msg_size, int traced, void *print_json,
+                             void *tojson, void *fromjson);
 void vl_msg_api_clean_handlers (int msg_id);
 void vl_msg_api_config (vl_msg_api_msg_config_t *);
 void vl_msg_api_set_cleanup_handler (int msg_id, void *fp);
-void vl_msg_api_queue_handler (unix_shared_memory_queue_t * q);
+void vl_msg_api_queue_handler (svm_queue_t * q);
 
 void vl_msg_api_barrier_sync (void) __attribute__ ((weak));
 void vl_msg_api_barrier_release (void) __attribute__ ((weak));
@@ -168,7 +205,11 @@ int vl_msg_api_pd_handler (void *mp, int rv);
 
 void vl_msg_api_set_first_available_msg_id (u16 first_avail);
 u16 vl_msg_api_get_msg_ids (const char *name, int n);
-u32 vl_api_get_msg_index (u8 * name_and_crc);
+u32 vl_msg_api_get_msg_index (u8 * name_and_crc);
+void *vl_msg_push_heap (void);
+void *vl_msg_push_heap_w_region (svm_region_t * vlib_rp);
+void vl_msg_pop_heap (void *oldheap);
+void vl_msg_pop_heap_w_region (svm_region_t * vlib_rp, void *oldheap);
 
 typedef clib_error_t *(vl_msg_api_init_function_t) (u32 client_index);
 
@@ -178,8 +219,16 @@ typedef struct _vl_msg_api_init_function_list_elt
   vl_msg_api_init_function_t *f;
 } _vl_msg_api_function_list_elt_t;
 
-/** API main structure, used by both vpp and binary API clients */
 typedef struct
+{
+  u32 major;
+  u32 minor;
+  u32 patch;
+  char name[64];
+} api_version_t;
+
+/** API main structure, used by both vpp and binary API clients */
+typedef struct api_main_t
 {
   /** Message handler vector  */
   void (**msg_handlers) (void *);
@@ -195,15 +244,30 @@ typedef struct
   /** Message print function vector */
   void (**msg_print_handlers) (void *, void *);
 
+  /** Message print function vector in JSON */
+  void (**msg_print_json_handlers) (void *, void *);
+
+  /** Message convert function vector */
+  cJSON *(**msg_tojson_handlers) (void *);
+
+  /** Message convert function vector */
+  void *(**msg_fromjson_handlers) (cJSON *, int *);
+
   /** Message name vector */
   const char **msg_names;
 
+  /** API message ID by name hash table */
+  uword *msg_id_by_name;
+
   /** Don't automatically free message buffer vetor */
   u8 *message_bounce;
 
   /** Message is mp safe vector */
   u8 *is_mp_safe;
 
+  /** Message requires us to do endian conversion */
+  u8 *is_autoendian;
+
   /** Allocator ring vectors (in shared memory) */
   struct ring_alloc_ *arings;
 
@@ -231,10 +295,14 @@ typedef struct
   /** Current process PID */
   int our_pid;
 
-  /** Binary api segment descriptor */
+  /** Current binary api segment descriptor */
   svm_region_t *vlib_rp;
 
+  /** Primary api segment descriptor */
+  svm_region_t *vlib_primary_rp;
+
   /** Vector of all mapped shared-VM segments */
+  svm_region_t **vlib_private_rps;
   svm_region_t **mapped_shmem_regions;
 
   /** Binary API shared-memory segment header pointer */
@@ -277,7 +345,7 @@ typedef struct
   u64 api_pvt_heap_size;
 
   /** Peer input queue pointer */
-  unix_shared_memory_queue_t *vl_input_queue;
+  svm_queue_t *vl_input_queue;
 
   /**
    * All VLIB-side message handlers use my_client_index to identify
@@ -291,15 +359,15 @@ typedef struct
    */
   vl_api_registration_t *my_registration;
 
-  /** (Historical) signal-based queue non-empty signal, to be removed */
-  i32 vlib_signal;
-
   /** vpp/vlib input queue length */
   u32 vlib_input_queue_length;
 
   /** client message index hash table */
   uword *msg_index_by_name_and_crc;
 
+  /** api version list */
+  api_version_t *api_version_list;
+
   /** Shared VM binary API region name */
   const char *region_name;
 
@@ -315,9 +383,35 @@ typedef struct
   /** List of API client reaper functions */
   _vl_msg_api_function_list_elt_t *reaper_function_registrations;
 
+  /** Bin API thread handle */
+  pthread_t rx_thread_handle;
+
+  /** event log */
+  elog_main_t *elog_main;
+  int elog_trace_api_messages;
+
+  /** performance counter callback **/
+  void (**perf_counter_cbs)
+    (struct api_main_t *, u32 id, int before_or_after);
+  void (**perf_counter_cbs_tmp)
+    (struct api_main_t *, u32 id, int before_or_after);
+
 } api_main_t;
 
-extern api_main_t api_main;
+extern __thread api_main_t *my_api_main;
+extern api_main_t api_global_main;
+
+always_inline api_main_t *
+vlibapi_get_main (void)
+{
+  return my_api_main;
+}
+
+always_inline void
+vlibapi_set_main (api_main_t * am)
+{
+  my_api_main = am;
+}
 
 #endif /* included_api_common_h */