blob: e604c40b20e407ebb3b2c34cf4cf77e83a7d5d97 [file] [log] [blame]
/*
*------------------------------------------------------------------
* Copyright (c) 2017 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*------------------------------------------------------------------
*/
/** @file
* @defgroup libmemif Example libmemif App
*/
#ifndef _LIBMEMIF_H_
#define _LIBMEMIF_H_
/** Libmemif version. */
#define LIBMEMIF_VERSION "4.0"
/** Default name of application using libmemif. */
#define MEMIF_DEFAULT_APP_NAME "libmemif-app"
#include <inttypes.h>
#include <sys/types.h>
#include <sys/timerfd.h>
#include <sys/types.h>
/*! Error codes */
typedef enum
{
MEMIF_ERR_SUCCESS = 0, /*!< success */
/* SYSCALL ERRORS */
MEMIF_ERR_SYSCALL, /*!< other syscall error */
MEMIF_ERR_CONNREFUSED, /*!< connection refused */
MEMIF_ERR_ACCES, /*!< permission denied */
MEMIF_ERR_NO_FILE, /*!< file does not exist */
MEMIF_ERR_FILE_LIMIT, /*!< system open file limit */
MEMIF_ERR_PROC_FILE_LIMIT, /*!< process open file limit */
MEMIF_ERR_ALREADY, /*!< connection already requested */
MEMIF_ERR_AGAIN, /*!< fd is not socket, or operation would block */
MEMIF_ERR_BAD_FD, /*!< invalid fd */
MEMIF_ERR_NOMEM, /*!< out of memory */
/* LIBMEMIF ERRORS */
MEMIF_ERR_INVAL_ARG, /*!< invalid argument */
MEMIF_ERR_NOCONN, /*!< handle points to no connection */
MEMIF_ERR_CONN, /*!< handle points to existing connection */
MEMIF_ERR_CB_FDUPDATE, /*!< user defined callback memif_control_fd_update_t
error */
MEMIF_ERR_FILE_NOT_SOCK, /*!< file specified by socket path
exists, but it's not socket */
MEMIF_ERR_NO_SHMFD, /*!< missing shm fd */
MEMIF_ERR_COOKIE, /*!< wrong cookie on ring */
MEMIF_ERR_NOBUF_RING, /*!< ring buffer full */
MEMIF_ERR_NOBUF, /*!< not enough memif buffers */
MEMIF_ERR_NOBUF_DET, /*!< memif details needs larger buffer */
MEMIF_ERR_INT_WRITE, /*!< send interrupt error */
MEMIF_ERR_MFMSG, /*!< malformed msg received */
MEMIF_ERR_QID, /*!< invalid queue id */
/* MEMIF PROTO ERRORS */
MEMIF_ERR_PROTO, /*!< incompatible protocol version */
MEMIF_ERR_ID, /*!< unmatched interface id */
MEMIF_ERR_ACCSLAVE, /*!< slave cannot accept connection requests */
MEMIF_ERR_ALRCONN, /*!< memif is already connected */
MEMIF_ERR_MODE, /*!< mode mismatch */
MEMIF_ERR_SECRET, /*!< secret mismatch */
MEMIF_ERR_NOSECRET, /*!< secret required */
MEMIF_ERR_MAXREG, /*!< max region limit reached */
MEMIF_ERR_MAXRING, /*!< max ring limit reached */
MEMIF_ERR_NO_INTFD, /*!< missing interrupt fd */
MEMIF_ERR_DISCONNECT, /*!< disconnect received */
MEMIF_ERR_DISCONNECTED, /*!< peer interface disconnected */
MEMIF_ERR_UNKNOWN_MSG, /*!< unknown message type */
MEMIF_ERR_POLL_CANCEL, /*!< memif_poll_event() was cancelled */
MEMIF_ERR_MAX_RING, /*!< too large ring size */
MEMIF_ERR_PRIVHDR, /*!< private hdrs not supported */
} memif_err_t;
/**
* @defgroup MEMIF_FD_EVENT Types of events that need to be watched for specific fd.
* @ingroup libmemif
* @{
*/
/** \brief Memif fd events
* User needs to set events that occurred on fd and pass them to
* memif_control_fd_handler
*/
typedef enum memif_fd_event_type
{
MEMIF_FD_EVENT_READ = 1, /* 00001 */
MEMIF_FD_EVENT_WRITE = 2, /* 00010 */
/** inform libmemif that error occurred on fd */
MEMIF_FD_EVENT_ERROR = 4, /* 00100 */
/** if set, informs that fd is going to be closed (user may want to stop
watching for events on this fd) */
MEMIF_FD_EVENT_DEL = 8, /* 01000 */
/** update events */
MEMIF_FD_EVENT_MOD = 16 /* 10000 */
} memif_fd_event_type_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
custom memory allocator: alloc function template
*/
typedef void *(memif_alloc_t) (size_t size);
/** \brief Memif realloc
@param ptr - pointer to memory block
@param size - requested allocation size
custom memory reallocation
*/
typedef void *(memif_realloc_t) (void *ptr, size_t size);
/** \brief Memif allocator free
@param size - requested allocation size
custom memory allocator: free function template
*/
typedef void (memif_free_t) (void *ptr);
/**
* @defgroup CALLBACKS Callback functions definitions
* @ingroup libmemif
*
* @{
*/
/** \brief Memif fd event
@param fd - interrupt file descriptor
@param type - memif fd event type
@param private_ctx - private event data
*/
typedef struct memif_fd_event
{
int fd;
memif_fd_event_type_t type;
void *private_ctx;
} memif_fd_event_t;
/** \brief Memif control file descriptor update (callback function)
@param fde - memif fd event
@param private_ctx - private context of socket this fd belongs to
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) (memif_fd_event_t fde,
void *private_ctx);
/** \brief Memif connection status update (callback function)
@param conn - memif connection handle
@param private_ctx - private context
Informs user about connection status connected/disconnected.
On connected -> start watching for events on interrupt fd (optional).
*/
typedef int (memif_connection_update_t) (memif_conn_handle_t conn,
void *private_ctx);
/** \brief Memif interrupt occurred (callback function)
@param conn - memif connection handle
@param private_ctx - private context
@param qid - queue id on which interrupt occurred
Called when event is received on interrupt fd.
*/
typedef int (memif_on_interrupt_t) (memif_conn_handle_t conn,
void *private_ctx, uint16_t qid);
/** @} */
/**
* @defgroup EXTERNAL_REGION External region APIs
* @ingroup libmemif
*
* @{
*/
/** \brief Get external buffer offset (optional)
@param private_ctx - private context
Find unallocated external buffer and return its offset.
*/
typedef uint32_t (memif_get_external_buffer_offset_t) (void *private_ctx);
/** \brief Add external region
@param[out] addr - region address
@param size - requested region size
@param fd[out] - file descriptor
@param private_ctx - private context
Called by slave. Add external region created by client.
*/
typedef int (memif_add_external_region_t) (void * *addr, uint32_t size,
int *fd, void *private_ctx);
/** \brief Get external region address
@param size - requested region size
@param fd - file descriptor
@param private_ctx - private context
Called by master. Get region address from client.
\return region address
*/
typedef void *(memif_get_external_region_addr_t) (uint32_t size, int fd,
void *private_ctx);
/** \brief Delete external region
@param addr - region address
@param size - region size
@param fd - file descriptor
@param private_ctx - private context
Delete external region.
*/
typedef int (memif_del_external_region_t) (void *addr, uint32_t size, int fd,
void *private_ctx);
/** \brief Register external region
@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_register_external_region (memif_socket_handle_t sock,
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);
/** \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);
@} */
/**
* @defgroup ARGS_N_BUFS Connection arguments and buffers
* @ingroup libmemif
*
* @{
*/
#ifndef _MEMIF_H_
typedef enum
{
MEMIF_INTERFACE_MODE_ETHERNET = 0,
MEMIF_INTERFACE_MODE_IP = 1,
MEMIF_INTERFACE_MODE_PUNT_INJECT = 2,
} memif_interface_mode_t;
#endif /* _MEMIF_H_ */
/** \brief Memif socket arguments
@param path - UNIX socket path, supports abstract socket (have '\0' or '@'
as the first char of the path)
@param app_name - application name
@param connection_request_timer - automaticaly request connection each time
this timer expires, must be non-zero to enable this feature
@param on_control_fd_update - if control fd updates inform user to watch
new fd
@param alloc - custom memory allocator, NULL = default
@param realloc - custom memory reallocation, NULL = default
@param free - custom memory free, NULL = default
If param on_control_fd_update is set to NULL,
libmemif will handle file descriptor event polling
if a valid callback is set, file descriptor event polling needs to be done
by user application, all file descriptors and event types will be passed in
this callback to user application
*/
typedef struct memif_socket_args
{
char path[108];
char app_name[32];
struct itimerspec connection_request_timer;
memif_control_fd_update_t *on_control_fd_update;
memif_alloc_t *alloc;
memif_realloc_t *realloc;
memif_free_t *free;
} memif_socket_args_t;
/** \brief Memif connection arguments
@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 - optional parameter used as interface authentication
@param num_s2m_rings - number of slave to master rings
@param num_m2s_rings - number of master to slave rings
@param buffer_size - size of buffer in shared memory
@param log2_ring_size - logarithm base 2 of ring size
@param is_master - 0 == master, 1 == slave
@param interface_id - id used to identify peer connection
@param interface_name - interface name
@param mode - 0 == ethernet, 1 == ip , 2 == punt/inject
*/
typedef struct
{
memif_socket_handle_t socket; /*!< default = /run/vpp/memif.sock */
uint8_t secret[24]; /*!< optional (interface authentication) */
uint8_t num_s2m_rings; /*!< default = 1 */
uint8_t num_m2s_rings; /*!< default = 1 */
uint16_t buffer_size; /*!< default = 2048 */
uint8_t log2_ring_size; /*!< default = 10 (1024) */
uint8_t is_master;
uint32_t interface_id;
uint8_t interface_name[32];
memif_interface_mode_t mode:8;
} memif_conn_args_t;
/*! memif receive mode */
typedef enum
{
MEMIF_RX_MODE_INTERRUPT = 0, /*!< interrupt mode */
MEMIF_RX_MODE_POLLING /*!< polling mode */
} memif_rx_mode_t;
/** \brief Memif buffer
@param desc_index - ring descriptor index
@param ring - pointer to ring containing descriptor for this buffer
@param len - available length
@param flags - memif buffer flags
@param data - pointer to shared memory data
*/
typedef struct
{
uint16_t desc_index;
void *queue;
uint32_t len;
/** next buffer present (chained buffers) */
#define MEMIF_BUFFER_FLAG_NEXT (1 << 0)
uint8_t flags;
void *data;
} memif_buffer_t;
/** @} */
/**
* @defgroup MEMIF_DETAILS Memif details structs
* @ingroup libmemif
*
* @{
*/
/** \brief Memif queue details
@param region - region index
@param qid - queue id
@param ring_size - size of ring buffer in shared memory
@param flags - ring flags
@param head - ring head pointer
@param tail - ring tail pointer
@param buffer_size - buffer size on shared memory
*/
typedef struct
{
uint8_t region;
uint16_t qid;
uint32_t ring_size;
/** if set queue is in polling mode, else in interrupt mode */
#define MEMIF_QUEUE_FLAG_POLLING 1
uint16_t flags;
uint16_t head;
uint16_t tail;
uint16_t buffer_size;
} memif_queue_details_t;
/** \brief Memif region details
@param index - region index
@param addr - region address
@param size - region size
@param fd - file descriptor
@param is_external - if not zero then region is defined by client
*/
typedef struct
{
uint8_t index;
void *addr;
uint32_t size;
int fd;
uint8_t is_external;
} memif_region_details_t;
/** \brief Memif details
@param if_name - interface name
@param inst_name - application name
@param remote_if_name - peer interface name
@param remote_inst_name - peer application name
@param id - connection id
@param secret - secret
@param role - 0 = master, 1 = slave
@param mode - 0 = ethernet, 1 = ip , 2 = punt/inject
@param socket_path - socket path
@param regions_num - number of regions
@param regions - struct containing region details
@param rx_queues_num - number of receive queues
@param tx_queues_num - number of transmit queues
@param rx_queues - struct containing receive queue details
@param tx_queues - struct containing transmit queue details
@param error - error string
@param link_up_down - 1 = up (connected), 2 = down (disconnected)
*/
typedef struct
{
uint8_t *if_name;
uint8_t *inst_name;
uint8_t *remote_if_name;
uint8_t *remote_inst_name;
uint32_t id;
uint8_t *secret; /* optional */
uint8_t role; /* 0 = master, 1 = slave */
uint8_t mode; /* 0 = ethernet, 1 = ip, 2 = punt/inject */
uint8_t *socket_path;
uint8_t regions_num;
memif_region_details_t *regions;
uint8_t rx_queues_num;
uint8_t tx_queues_num;
memif_queue_details_t *rx_queues;
memif_queue_details_t *tx_queues;
uint8_t *error;
uint8_t link_up_down; /* 1 = up, 0 = down */
} memif_details_t;
/** @} */
/**
* @defgroup API_CALLS Api calls
* @ingroup libmemif
*
* @{
*/
/** \brief Memif get version
\return ((MEMIF_VERSION_MAJOR << 8) | MEMIF_VERSION_MINOR)
*/
uint16_t memif_get_version (void);
/** \brief Get memif version as string
\return major.minor
*/
const char *memif_get_version_str (void);
/** \brief Memif get queue event file descriptor
@param conn - memif connection handle
@param qid - queue id
@param[out] fd - returns event file descriptor
\return memif_err_t
*/
int memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *fd);
/** \brief Memif set rx mode
@param conn - memif connection handle
@param rx_mode - receive mode
@param qid - queue id
\return memif_err_t
*/
int memif_set_rx_mode (memif_conn_handle_t conn, memif_rx_mode_t rx_mode,
uint16_t qid);
/** \brief Memif strerror
@param err_code - error code
Converts error code to error message.
\return Error string
*/
char *memif_strerror (int err_code);
/** \brief Memif get details
@param conn - memif connection handle
@param md - pointer to memif details struct
@param buf - buffer containing details strings
@param buflen - length of buffer
\return memif_err_t
*/
int memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
char *buf, ssize_t buflen);
/** \brief Memory interface create function
@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
@param on_interrupt - informs user about interrupt, if set to null user will not be notified about interrupt, user can use memif_get_queue_efd call to get interrupt fd to poll for events
@param private_ctx - private context passed back to user with callback
Creates memory interface.
SLAVE-MODE -
Start timer that will send events to timerfd. If this fd is passed to memif_control_fd_handler
every disconnected memif in slave mode will send connection request.
On success new fd is passed to user with memif_control_fd_update_t.
MASTER-MODE -
Create listener socket and pass fd to user with memif_control_fd_update_t.
If this fd is passed to memif_control_fd_handler accept will be called and
new fd will be passed to user with memif_control_fd_update_t.
\return memif_err_t
*/
int memif_create (memif_conn_handle_t *conn, memif_conn_args_t *args,
memif_connection_update_t *on_connect,
memif_connection_update_t *on_disconnect,
memif_on_interrupt_t *on_interrupt, void *private_ctx);
/** \brief Memif control file descriptor handler
@param ptr - pointer to event data
@param events - event type(s) that occurred
\return memif_err_t
*/
int memif_control_fd_handler (void *ptr, memif_fd_event_type_t events);
/** \brief Memif delete
@param conn - pointer to memif connection handle
disconnect session (free queues and regions, close file descriptors, unmap shared memory)
set connection handle to NULL, to avoid possible double free
\return memif_err_t
*/
int memif_delete (memif_conn_handle_t * conn);
/** \brief Memif buffer enq tx
@param conn - memif connection handle
@param qid - number identifying queue
@param bufs - memif buffers
@param count - number of memif buffers to enqueue
@param count_out - returns number of allocated buffers
Enqueue buffers to specified tx queue. Can only be used by slave.
Updates desc_index field for each memif buffer.
If connection handle points to master returns MEMIF_ERR_INVAL_ARG.
\return memif_err_t
*/
int memif_buffer_enq_tx (memif_conn_handle_t conn, uint16_t qid,
memif_buffer_t * bufs, uint16_t count,
uint16_t * count_out);
/** \brief Memif buffer enq tx at idx
@param conn - memif connection handle
@param buf_a - memif buffer
@param buf_b - memif buffer
Swap descriptors for provided buffers and update the buffers
*/
int memif_buffer_requeue (memif_conn_handle_t conn, memif_buffer_t *buf_a,
memif_buffer_t *buf_b);
/** \brief Memif buffer alloc
@param conn - memif connection handle
@param qid - number identifying queue
@param bufs - memif buffers
@param count - number of memif buffers to allocate
@param count_out - returns number of allocated buffers
@param size - buffer size, may return chained buffers if size > buffer_size
\return memif_err_t
*/
int memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
memif_buffer_t * bufs, uint16_t count,
uint16_t * count_out, uint16_t size);
/** \brief Memif set next free buffer
@param conn - memif connection handle
@param qid - number identifying queue
@param buf - next free buffer
Sets next free descriptor pointer for specified tx queue.
The next allocation will happen at this buffer.
*/
int memif_set_next_free_buffer (memif_conn_handle_t conn, uint16_t qid,
memif_buffer_t *buf);
/** \brief Memif refill queue
@param conn - memif connection handle
@param qid - number identifying queue
@param count - number of buffers to be placed on ring
@param headroom - offset the buffer by headroom
\return memif_err_t
*/
int memif_refill_queue (memif_conn_handle_t conn, uint16_t qid,
uint16_t count, uint16_t headroom);
/** \brief Memif transmit buffer burst
@param conn - memif connection handle
@param qid - number identifying queue
@param bufs - memif buffers
@param count - number of memif buffers to transmit
@param tx - returns number of transmitted buffers
\return memif_err_t
*/
int memif_tx_burst (memif_conn_handle_t conn, uint16_t qid,
memif_buffer_t * bufs, uint16_t count, uint16_t * tx);
/** \brief Memif receive buffer burst
@param conn - memif connection handle
@param qid - number identifying queue
@param bufs - memif buffers
@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,
memif_buffer_t * bufs, uint16_t count, uint16_t * rx);
/** \brief Memif poll event
@param sock - socket to poll events on
@param timeout - timeout in seconds
Passive event polling -
timeout = 0 - dont wait for event, check event queue if there is an event
and return. timeout = -1 - wait until event
\return memif_err_t
*/
int memif_poll_event (memif_socket_handle_t sock, int timeout);
/** \brief Send signal to stop concurrently running memif_poll_event().
@param sock - stop polling on this socket
The function, however, does not wait for memif_poll_event() to stop.
memif_poll_event() may still return simply because an event has occurred
or the timeout has elapsed, but if called repeatedly in an infinite loop,
a canceled memif_poll_event() is guaranteed to return MEMIF_ERR_POLL_CANCEL
in the shortest possible time.
This feature was not available in the first release.
Use macro MEMIF_HAVE_CANCEL_POLL_EVENT to check if the feature is present.
\return memif_err_t
*/
#define MEMIF_HAVE_CANCEL_POLL_EVENT 1
int memif_cancel_poll_event (memif_socket_handle_t sock);
/** \brief Send connection request
@param conn - memif connection handle
Only slave interface can request connection.
\return memif_err_t
*/
int memif_request_connection (memif_conn_handle_t conn);
/** \brief Create memif socket
@param sock - socket handle for client app
@param args - memif socket arguments
@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 created
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,
memif_socket_args_t *args, void *private_ctx);
/** \brief Get memif socket handle from connection
@param conn - memif connection handle
\return memif_socket_handle_t
*/
memif_socket_handle_t memif_get_socket_handle (memif_conn_handle_t conn);
/** \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 path
@param sock - socket handle for client app
Return constant pointer to socket path.
\return const char *
*/
const char *memif_get_socket_path (memif_socket_handle_t sock);
/** \brief Get listener file descriptor
@param sock - memif socket handle
\return listener fd
*/
int memif_get_listener_fd (memif_socket_handle_t sock);
/** \brief Set listener file descriptor
@param sock - memif socket handle
@param if - file descriptor
\return memif_err_t
*/
int memif_set_listener_fd (memif_socket_handle_t sock, int fd);
/** \brief Set connection request timer value
@param sock - memif socket handle
@param timer - new timer value
Timer on which all disconnected slaves request connection.
If the timer doesn't exist (timerspec is 0) create new timer.
See system call 'timer_settime' man-page.
\return memif_err_t
*/
int memif_set_connection_request_timer (memif_socket_handle_t sock,
struct itimerspec timer);
/** @} */
#endif /* _LIBMEMIF_H_ */