vppinfra: Improve code portability
[vpp.git] / src / vpp-api / vapi / vapi.h
index 245bf65..8f092ee 100644 (file)
@@ -21,7 +21,9 @@
 #include <string.h>
 #include <stdbool.h>
 #include <vppinfra/types.h>
+#include <vlibapi/api_types.h>
 #include <vapi/vapi_common.h>
+#include <svm/queue.h>
 
 #ifdef __cplusplus
 extern "C"
@@ -42,7 +44,7 @@ extern "C"
  * process). It's not recommended to mix the higher and lower level APIs. Due
  * to version issues, the higher-level APIs are not part of the shared library.
  */
-  typedef struct vapi_ctx_s *vapi_ctx_t;
+typedef struct vapi_ctx_s *vapi_ctx_t;
 
 /**
  * @brief allocate vapi message of given size
@@ -54,7 +56,7 @@ extern "C"
  *
  * @return pointer to message or NULL if out of memory
  */
-  void *vapi_msg_alloc (vapi_ctx_t ctx, size_t size);
+void *vapi_msg_alloc (vapi_ctx_t ctx, size_t size);
 
 /**
  * @brief free a vapi message
@@ -64,7 +66,7 @@ extern "C"
  * @param ctx opaque vapi context
  * @param msg message to be freed
  */
-  void vapi_msg_free (vapi_ctx_t ctx, void *msg);
+void vapi_msg_free (vapi_ctx_t ctx, void *msg);
 
 /**
  * @brief allocate vapi context
@@ -73,18 +75,18 @@ extern "C"
  *
  * @return VAPI_OK on success, other error code on error
  */
-  vapi_error_e vapi_ctx_alloc (vapi_ctx_t * result);
+vapi_error_e vapi_ctx_alloc (vapi_ctx_t *result);
 
 /**
  * @brief free vapi context
  */
-  void vapi_ctx_free (vapi_ctx_t ctx);
+void vapi_ctx_free (vapi_ctx_t ctx);
 
 /**
  * @brief check if message identified by it's message id is known by the vpp to
  * which the connection is open
  */
-  bool vapi_is_msg_available (vapi_ctx_t ctx, vapi_msg_id_t type);
+bool vapi_is_msg_available (vapi_ctx_t ctx, vapi_msg_id_t type);
 
 /**
  * @brief connect to vpp
@@ -95,13 +97,53 @@ extern "C"
  * @param max_outstanding_requests max number of outstanding requests queued
  * @param response_queue_size size of the response queue
  * @param mode mode of operation - blocking or nonblocking
+ * @param handle_keepalives - if true, automatically handle memclnt_keepalive
  *
  * @return VAPI_OK on success, other error code on error
  */
-  vapi_error_e vapi_connect (vapi_ctx_t ctx, const char *name,
-                            const char *chroot_prefix,
-                            int max_outstanding_requests,
-                            int response_queue_size, vapi_mode_e mode);
+vapi_error_e vapi_connect (vapi_ctx_t ctx, const char *name,
+                          const char *chroot_prefix,
+                          int max_outstanding_requests,
+                          int response_queue_size, vapi_mode_e mode,
+                          bool handle_keepalives);
+
+/**
+ * @brief connect to vpp
+ *
+ * @param ctx opaque vapi context, must be allocated using vapi_ctx_alloc first
+ * @param name application name
+ * @param path shared memory prefix or path to unix socket
+ * @param max_outstanding_requests max number of outstanding requests queued
+ * @param response_queue_size size of the response queue
+ * @param mode mode of operation - blocking or nonblocking
+ * @param handle_keepalives - if true, automatically handle memclnt_keepalive
+ * @param use_uds - if true, use unix domain socket transport
+ *
+ * @return VAPI_OK on success, other error code on error
+ */
+vapi_error_e vapi_connect_ex (vapi_ctx_t ctx, const char *name,
+                             const char *path, int max_outstanding_requests,
+                             int response_queue_size, vapi_mode_e mode,
+                             bool handle_keepalives, bool use_uds);
+
+/**
+ * @brief connect to vpp from a client in same process
+ * @remark This MUST be called from a separate thread. If called
+ *         from the main thread, it will deadlock.
+ *
+ * @param ctx opaque vapi context, must be allocated using vapi_ctx_alloc first
+ * @param name application name
+ * @param max_outstanding_requests max number of outstanding requests queued
+ * @param response_queue_size size of the response queue
+ * @param mode mode of operation - blocking or nonblocking
+ * @param handle_keepalives - if true, automatically handle memclnt_keepalive
+ *
+ * @return VAPI_OK on success, other error code on error
+ */
+vapi_error_e vapi_connect_from_vpp (vapi_ctx_t ctx, const char *name,
+                                   int max_outstanding_requests,
+                                   int response_queue_size, vapi_mode_e mode,
+                                   bool handle_keepalives);
 
 /**
  * @brief disconnect from vpp
@@ -110,7 +152,8 @@ extern "C"
  *
  * @return VAPI_OK on success, other error code on error
  */
-  vapi_error_e vapi_disconnect (vapi_ctx_t ctx);
+vapi_error_e vapi_disconnect (vapi_ctx_t ctx);
+vapi_error_e vapi_disconnect_from_vpp (vapi_ctx_t ctx);
 
 /**
  * @brief get event file descriptor
@@ -123,7 +166,7 @@ extern "C"
  *
  * @return VAPI_OK on success, other error code on error
  */
-  vapi_error_e vapi_get_fd (vapi_ctx_t ctx, int *fd);
+vapi_error_e vapi_get_fd (vapi_ctx_t ctx, int *fd);
 
 /**
  * @brief low-level api for sending messages to vpp
@@ -136,7 +179,7 @@ extern "C"
  *
  * @return VAPI_OK on success, other error code on error
  */
-  vapi_error_e vapi_send (vapi_ctx_t ctx, void *msg);
+vapi_error_e vapi_send (vapi_ctx_t ctx, void *msg);
 
 /**
  * @brief low-level api for atomically sending two messages to vpp - either
@@ -151,7 +194,7 @@ extern "C"
  *
  * @return VAPI_OK on success, other error code on error
  */
-  vapi_error_e vapi_send2 (vapi_ctx_t ctx, void *msg1, void *msg2);
+vapi_error_e vapi_send2 (vapi_ctx_t ctx, void *msg1, void *msg2);
 
 /**
  * @brief low-level api for reading messages from vpp
@@ -162,27 +205,41 @@ extern "C"
  * @param ctx opaque vapi context
  * @param[out] msg pointer to result variable containing message
  * @param[out] msg_size pointer to result variable containing message size
+ * @param cond enum type for blocking, non-blocking or timed wait call
+ * @param time in sec for timed wait
  *
  * @return VAPI_OK on success, other error code on error
  */
-  vapi_error_e vapi_recv (vapi_ctx_t ctx, void **msg, size_t * msg_size);
+vapi_error_e vapi_recv (vapi_ctx_t ctx, void **msg, size_t *msg_size,
+                       svm_q_conditional_wait_t cond, u32 time);
 
 /**
- * @brief wait for connection to become readable or writable
+ * @brief wait for connection to become readable
  *
  * @param ctx opaque vapi context
- * @param mode type of property to wait for - readability, writability or both
  *
  * @return VAPI_OK on success, other error code on error
  */
-  vapi_error_e vapi_wait (vapi_ctx_t ctx, vapi_wait_mode_e mode);
+vapi_error_e vapi_wait (vapi_ctx_t ctx);
+
+/**
+ * @brief pick next message sent by vpp and call the appropriate callback
+ *
+ * @note if using block mode, it will be blocked indefinitely until the next
+ * msg available. If using non-blocking mode, it will block for time_wait
+ * seconds until the next msg available if time_wait > 0, or does not block if
+ * time_wait == 0.
+ *
+ * @return VAPI_OK on success, other error code on error
+ */
+vapi_error_e vapi_dispatch_one_timedwait (vapi_ctx_t ctx, u32 wait_time);
 
 /**
  * @brief pick next message sent by vpp and call the appropriate callback
  *
  * @return VAPI_OK on success, other error code on error
  */
-  vapi_error_e vapi_dispatch_one (vapi_ctx_t ctx);
+vapi_error_e vapi_dispatch_one (vapi_ctx_t ctx);
 
 /**
  * @brief loop vapi_dispatch_one until responses to all currently outstanding
@@ -190,19 +247,19 @@ extern "C"
  *
  * @note the dispatch loop is interrupted if any error is encountered or
  * returned from the callback, in which case this error is returned as the
- * result of vapi_dispatch. In this case it might be necessary to call dispatch
- * again to process the remaining messages. Returning VAPI_EUSER from
- * a callback allows the user to break the dispatch loop (and distinguish
- * this case in the calling code from other failures). VAPI never returns
- * VAPI_EUSER on its own.
+ * result of vapi_dispatch. In this case it might be necessary to call
+ * dispatch again to process the remaining messages. Returning VAPI_EUSER
+ * from a callback allows the user to break the dispatch loop (and
+ * distinguish this case in the calling code from other failures). VAPI never
+ * returns VAPI_EUSER on its own.
  *
  * @return VAPI_OK on success, other error code on error
  */
-  vapi_error_e vapi_dispatch (vapi_ctx_t ctx);
+vapi_error_e vapi_dispatch (vapi_ctx_t ctx);
 
 /** generic vapi event callback */
-  typedef vapi_error_e (*vapi_event_cb) (vapi_ctx_t ctx, void *callback_ctx,
-                                        void *payload);
+typedef vapi_error_e (*vapi_event_cb) (vapi_ctx_t ctx, void *callback_ctx,
+                                      void *payload);
 
 /**
  * @brief set event callback to call when message with given id is dispatched
@@ -212,8 +269,8 @@ extern "C"
  * @param callback callback
  * @param callback_ctx context pointer stored and passed to callback
  */
-  void vapi_set_event_cb (vapi_ctx_t ctx, vapi_msg_id_t id,
-                         vapi_event_cb callback, void *callback_ctx);
+void vapi_set_event_cb (vapi_ctx_t ctx, vapi_msg_id_t id,
+                       vapi_event_cb callback, void *callback_ctx);
 
 /**
  * @brief clear event callback for given message id
@@ -221,12 +278,12 @@ extern "C"
  * @param ctx opaque vapi context
  * @param id message id
  */
-  void vapi_clear_event_cb (vapi_ctx_t ctx, vapi_msg_id_t id);
+void vapi_clear_event_cb (vapi_ctx_t ctx, vapi_msg_id_t id);
 
 /** generic vapi event callback */
-  typedef vapi_error_e (*vapi_generic_event_cb) (vapi_ctx_t ctx,
-                                                void *callback_ctx,
-                                                vapi_msg_id_t id, void *msg);
+typedef vapi_error_e (*vapi_generic_event_cb) (vapi_ctx_t ctx,
+                                              void *callback_ctx,
+                                              vapi_msg_id_t id, void *msg);
 /**
  * @brief set generic event callback
  *
@@ -237,16 +294,29 @@ extern "C"
  * @param callback callback
  * @param callback_ctx context pointer stored and passed to callback
  */
-  void vapi_set_generic_event_cb (vapi_ctx_t ctx,
-                                 vapi_generic_event_cb callback,
-                                 void *callback_ctx);
+void vapi_set_generic_event_cb (vapi_ctx_t ctx, vapi_generic_event_cb callback,
+                               void *callback_ctx);
 
 /**
  * @brief clear generic event callback
  *
  * @param ctx opaque vapi context
  */
-  void vapi_clear_generic_event_cb (vapi_ctx_t ctx);
+void vapi_clear_generic_event_cb (vapi_ctx_t ctx);
+
+/**
+ * @brief signal RX thread to exit
+ *
+ * @note This adds a message to the client input queue that indicates that
+ * an RX thread should stop processing incoming messages and exit. If an
+ * application has an RX thread which sleeps while waiting for incoming
+ * messages using vapi_wait(), this call will allow the application to
+ * wake up from the vapi_wait() call and figure out that it should stop
+ * running.
+ *
+ * @param ctx opaque vapi context
+ */
+void vapi_stop_rx_thread (vapi_ctx_t ctx);
 
 #ifdef __cplusplus
 }