libmemif: don't consume rx queue interrupt, if memif_rx_burst fails
[vpp.git] / extras / libmemif / src / libmemif.h
index 9fc2a70..66006f3 100644 (file)
@@ -23,7 +23,7 @@
 #define _LIBMEMIF_H_
 
 /** Libmemif version. */
-#define LIBMEMIF_VERSION "2.1"
+#define LIBMEMIF_VERSION "3.1"
 /** Default name of application using libmemif. */
 #define MEMIF_DEFAULT_APP_NAME "libmemif-app"
 
@@ -96,11 +96,22 @@ typedef enum
 #define MEMIF_FD_EVENT_MOD   (1 << 4)
 /** @} */
 
+/** \brief Memif per thread main handle
+    Pointer of type void, pointing to internal structure.
+    Used to identify internal per thread database.
+*/
+typedef void *memif_per_thread_main_handle_t;
+
 /** \brief Memif connection handle
     pointer of type void, pointing to internal structure
 */
 typedef void *memif_conn_handle_t;
 
+/** \brief Memif socket handle
+    pointer of type void, pointing to internal structure
+*/
+typedef void *memif_socket_handle_t;
+
 /** \brief Memif allocator alloc
     @param size - requested allocation size
 
@@ -134,11 +145,17 @@ typedef void (memif_free_t) (void *ptr);
 /** \brief Memif control file descriptor update (callback function)
     @param fd - new file descriptor to watch
     @param events - event type(s) to watch for
+    @param private_ctx - libmemif main private context. Is NULL for
+                         libmemif main created by memif_init()
+
 
     This callback is called when there is new fd to watch for events on
     or if fd is about to be closed (user mey want to stop watching for events on this fd).
+    Private context is taken from libmemif_main, 'private_ctx' passed to memif_per_thread_init()
+    or NULL in case of memif_init()
 */
-typedef int (memif_control_fd_update_t) (int fd, uint8_t events);
+typedef int (memif_control_fd_update_t) (int fd, uint8_t events,
+                                        void *private_ctx);
 
 /** \brief Memif connection status update (callback function)
     @param conn - memif connection handle
@@ -221,6 +238,24 @@ void memif_register_external_region (memif_add_external_region_t * ar,
                                     memif_del_external_region_t * dr,
                                     memif_get_external_buffer_offset_t * go);
 
+/** \brief Register external region
+    @param pt_main - per thread main handle
+    @param ar - add external region callback
+    @param gr - get external region addr callback
+    @param dr - delete external region callback
+    @param go - get external buffer offset callback (optional)
+*/
+void memif_per_thread_register_external_region (memif_per_thread_main_handle_t
+                                               pt_main,
+                                               memif_add_external_region_t *
+                                               ar,
+                                               memif_get_external_region_addr_t
+                                               * gr,
+                                               memif_del_external_region_t *
+                                               dr,
+                                               memif_get_external_buffer_offset_t
+                                               * go);
+
 /** @} */
 
 /**
@@ -240,7 +275,11 @@ typedef enum
 #endif /* _MEMIF_H_ */
 
 /** \brief Memif connection arguments
-    @param socket_filename - socket filename
+    @param socket - Memif socket handle, if NULL default socket will be used.
+                   Default socket is only supported in global database (see memif_init).
+                   Custom database does not create a default socket
+                   (see memif_per_thread_init).
+                   Memif connection is stored in the same database as the socket.
     @param secret - otional parameter used as interface autenthication
     @param num_s2m_rings - number of slave to master rings
     @param num_m2s_rings - number of master to slave rings
@@ -253,7 +292,7 @@ typedef enum
 */
 typedef struct
 {
-  uint8_t *socket_filename;    /*!< default = /run/vpp/memif.sock */
+  memif_socket_handle_t socket;        /*!< default = /run/vpp/memif.sock */
   uint8_t secret[24];          /*!< optional (interface authentication) */
 
   uint8_t num_s2m_rings;       /*!< default = 1 */
@@ -459,6 +498,28 @@ int memif_init (memif_control_fd_update_t * on_control_fd_update,
                char *app_name, memif_alloc_t * memif_alloc,
                memif_realloc_t * memif_realloc, memif_free_t * memif_free);
 
+/** \brief Memif per thread initialization
+    @param pt_main - per thread main handle
+    @param private_ctx - private context
+    @param on_control_fd_update - if control fd updates inform user to watch new fd
+    @param app_name - application name (will be truncated to 32 chars)
+    @param memif_alloc - cutom memory allocator, NULL = default
+    @param memif_realloc - custom memory reallocation, NULL = default
+    @param memif_free - custom memory free, NULL = default
+
+    Per thread version of memif_init ().
+    Instead of using global database, creates and initializes unique database,
+    identified by 'memif_per_thread_main_handle_t'.
+
+    \return memif_err_t
+*/
+int memif_per_thread_init (memif_per_thread_main_handle_t * pt_main,
+                          void *private_ctx,
+                          memif_control_fd_update_t * on_control_fd_update,
+                          char *app_name, memif_alloc_t * memif_alloc,
+                          memif_realloc_t * memif_realloc,
+                          memif_free_t * memif_free);
+
 /** \brief Memif cleanup
 
     Free libmemif internal allocations.
@@ -467,8 +528,17 @@ int memif_init (memif_control_fd_update_t * on_control_fd_update,
 */
 int memif_cleanup ();
 
+/** \brief Memif per thread cleanup
+    @param pt_main - per thread main handle
+
+    Free libmemif internal allocations and sets the handle to NULL.
+
+    \return memif_err_t
+*/
+int memif_per_thread_cleanup (memif_per_thread_main_handle_t * pt_main);
+
 /** \brief Memory interface create function
-    @param conn - connection handle for user app
+    @param conn - connection handle for client app
     @param args - memory interface connection arguments
     @param on_connect - inform user about connected status
     @param on_disconnect - inform user about disconnected status
@@ -515,6 +585,19 @@ int memif_create (memif_conn_handle_t * conn, memif_conn_args_t * args,
 */
 int memif_control_fd_handler (int fd, uint8_t events);
 
+/** \brief Memif per thread control file descriptor handler
+    @param pt_main - per thread main handle
+    @param fd - file descriptor on which the event occured
+    @param events - event type(s) that occured
+
+    Per thread version of memif_control_fd_handler.
+
+    \return memif_err_t
+
+*/
+int memif_per_thread_control_fd_handler (memif_per_thread_main_handle_t
+                                        pt_main, int fd, uint8_t events);
+
 /** \brief Memif delete
     @param conn - pointer to memif connection handle
 
@@ -586,6 +669,9 @@ int memif_tx_burst (memif_conn_handle_t conn, uint16_t qid,
     @param count - number of memif buffers to receive
     @param rx - returns number of received buffers
 
+    Consume interrupt event for receive queue.
+    The event is not consumed, if memif_rx_burst fails.
+
     \return memif_err_t
 */
 int memif_rx_burst (memif_conn_handle_t conn, uint16_t qid,
@@ -602,6 +688,17 @@ int memif_rx_burst (memif_conn_handle_t conn, uint16_t qid,
 */
 int memif_poll_event (int timeout);
 
+/** \brief Memif poll event
+    @param pt_main - per thread main handle
+    @param timeout - timeout in seconds
+
+    Per thread version of memif_poll_event.
+
+    \return memif_err_t
+*/
+int memif_per_thread_poll_event (memif_per_thread_main_handle_t pt_main,
+                                int timeout);
+
 /** \brief Send signal to stop concurrently running memif_poll_event().
 
     The function, however, does not wait for memif_poll_event() to stop.
@@ -616,6 +713,15 @@ int memif_poll_event (int timeout);
 */
 #define MEMIF_HAVE_CANCEL_POLL_EVENT 1
 int memif_cancel_poll_event ();
+/** \brief Send signal to stop concurrently running memif_poll_event().
+    @param pt_main - per thread main handle
+
+    Per thread version of memif_cancel_poll_event.
+
+    \return memif_err_t
+*/
+int memif_per_thread_cancel_poll_event (memif_per_thread_main_handle_t
+                                       pt_main);
 
 /** \brief Set connection request timer value
     @param timer - new timer value
@@ -625,7 +731,20 @@ int memif_cancel_poll_event ();
 
     \return memif_err_t
 */
-int memif_set_connection_request_timer(struct itimerspec timer);
+int memif_set_connection_request_timer (struct itimerspec timer);
+
+/** \brief Set connection request timer value
+    @param pt_main - per thread main handle
+    @param timer - new timer value
+
+    Per thread version of memif_set_connection_request_timer
+
+    \return memif_err_t
+*/
+int
+memif_per_thread_set_connection_request_timer (memif_per_thread_main_handle_t
+                                              pt_main,
+                                              struct itimerspec timer);
 
 /** \brief Send connection request
     @param conn - memif connection handle
@@ -634,7 +753,58 @@ int memif_set_connection_request_timer(struct itimerspec timer);
 
     \return memif_err_t
 */
-int memif_request_connection(memif_conn_handle_t conn);
+int memif_request_connection (memif_conn_handle_t conn);
+
+/** \brief Create memif socket
+    @param sock - socket handle for client app
+    @param filename - path to socket file
+    @param private_ctx - private context
+
+    The first time an interface is assigned a socket, its type is determined.
+    For master role it's 'listener', for slave role it's 'client'. Each interface
+    requires socket of its respective type. Default socket is creted if no
+    socket handle is passed to memif_create(). It's private context is NULL.
+    If all interfaces using this socket are deleted, the socket returns
+    to its default state.
+
+    \return memif_err_t
+*/
+int memif_create_socket (memif_socket_handle_t * sock, const char *filename,
+                        void *private_ctx);
+
+/** \brief Create memif socket
+    @param pt_main - per thread main handle
+    @param sock - socket handle for client app
+    @param filename - path to socket file
+    @param private_ctx - private context
+
+    Per thread version of memif_create_sopcket.
+
+    \return memif_err_t
+*/
+int memif_per_thread_create_socket (memif_per_thread_main_handle_t pt_main,
+                                   memif_socket_handle_t * sock,
+                                   const char *filename, void *private_ctx);
+
+/** \brief Delete memif socket
+    @param sock - socket handle for client app
+
+    When trying to free socket in use, socket will not be freed and
+    MEMIF_ERR_INVAL_ARG is returned.
+
+    \return memif_err_t
+*/
+int memif_delete_socket (memif_socket_handle_t * sock);
+
+/** \brief Get socket filename
+    @param sock - socket handle for client app
+
+    Return constant pointer to socket filename.
+
+    \return cosnt char *
+*/
+const char *memif_get_socket_filename (memif_socket_handle_t sock);
+
 /** @} */
 
 #endif /* _LIBMEMIF_H_ */