summaryrefslogtreecommitdiffstats
path: root/src/plugins/tlsopenssl
AgeCommit message (Collapse)AuthorFilesLines
2019-09-02tls: some rework based on TLS openssl C APIPing Yu3-26/+33
Type: fix Change-Id: I5d0ac1fe6a6770ab8b3a9c366d10387718391199 Signed-off-by: Ping Yu <ping.yu@intel.com>
2019-08-20tls: Add C API for TLS openssl to set enginePing Yu9-10/+436
Type: feature Parameters of the engine can be set by C API. After this patch, it is easier to integrate TLS into CSIT test. Change-Id: I063cabf613aabbfad831727551579328705afb41 Signed-off-by: Ping Yu <ping.yu@intel.com>
2019-08-09tls: mark as no lookup transportFlorin Coras1-14/+24
Type:fix Also fix transport close while handshake is ongoing. Change-Id: I004c56d2297d0847c2cb77202f8fba3edaacad29 Signed-off-by: Florin Coras <fcoras@cisco.com>
2019-08-08tls: fix close with dataFlorin Coras1-12/+43
Type:fix Also changes the way the ctx is freed. TLS now waits for tcp delete notification before freeing the ctx. Change-Id: I2f606a9ce7b3755ae9d11d6fe714fe11b65dcb98 Signed-off-by: Florin Coras <fcoras@cisco.com>
2019-06-29svm: rename fifo tx notifications to reflect useFlorin Coras1-1/+1
Type: refactor Change-Id: I651db44acdcb666a9c63e1037352cf88c68795b5 Signed-off-by: Florin Coras <fcoras@cisco.com>
2019-05-16init / exit function orderingDave Barach1-7/+6
The vlib init function subsystem now supports a mix of procedural and formally-specified ordering constraints. We should eliminate procedural knowledge wherever possible. The following schemes are *roughly* equivalent: static clib_error_t *init_runs_first (vlib_main_t *vm) { clib_error_t *error; ... do some stuff... if ((error = vlib_call_init_function (init_runs_next))) return error; ... } VLIB_INIT_FUNCTION (init_runs_first); and static clib_error_t *init_runs_first (vlib_main_t *vm) { ... do some stuff... } VLIB_INIT_FUNCTION (init_runs_first) = { .runs_before = VLIB_INITS("init_runs_next"), }; The first form will [most likely] call "init_runs_next" on the spot. The second form means that "init_runs_first" runs before "init_runs_next," possibly much earlier in the sequence. Please DO NOT construct sets of init functions where A before B actually means A *right before* B. It's not necessary - simply combine A and B - and it leads to hugely annoying debugging exercises when trying to switch from ad-hoc procedural ordering constraints to formal ordering constraints. Change-Id: I5e4353503bf43b4acb11a45fb33c79a5ade8426c Signed-off-by: Dave Barach <dave@barachs.net>
2019-05-08session: send tx events when data is dequeuedFlorin Coras1-0/+3
Change-Id: Ib8cb19361c42e38e3f68d7147358378fff161eb1 Signed-off-by: Florin Coras <fcoras@cisco.com>
2019-05-03plugins: clean up plugin descriptionsDave Wallace1-1/+1
- Make plugin descriptions more consistent so the output of "show plugin" can be used in the wiki. Change-Id: I4c6feb11e7dcc5a4cf0848eed37f1d3b035c7dda Signed-off-by: Dave Wallace <dwallacelf@gmail.com>
2019-04-18tls: allow engines to customize closeFlorin Coras1-0/+24
Change-Id: I11ac3e4f59206902e5dfc326f815c877c5dd6643 Signed-off-by: Florin Coras <fcoras@cisco.com>
2019-04-16svm_fifo rework to avoid contention on cursizeSirshak Das1-7/+7
Problems Addressed: - Contention of cursize by producer and consumer. - Reduce the no of modulo operations. Changes: - Synchronization between producer and consumer changed from cursize to head and tail indexes Implications: reduces the usable size of fifo by 1. - Using weaker memory ordering C++11 atomics to access head and tail based on producer and consumer role. - Head and tail indexes are unsigned 32 bit integers. Additions and subtraction on them are implicit 32 bit Modulo operation. - Adding weaker memory ordering variants of max_enq, max_deq, is_empty and is_full Using them appropriately in all places. Perfomance improvement (iperf3 via Hoststack): iperf3 Server: Marvell ThunderX2(AArch64) - iperf3 Client: Skylake(x86) ~6%(256 rxd/txd) - ~11%(2048 rxd/txd) Change-Id: I1d484e000e437430fdd5a819657d1c6b62443018 Signed-off-by: Sirshak Das <sirshak.das@arm.com> Reviewed-by: Honnappa Nagarahalli <honnappa.nagarahalli@arm.com>
2019-02-18tls: fix openssl/mbedtls use of app_wrk indexFlorin Coras1-2/+7
Change-Id: I7ccc948357d815a1bd4279a7079cf4db2949183c Signed-off-by: Florin Coras <fcoras@cisco.com>
2019-02-04session: cleanup part 1Florin Coras3-22/+19
Rename core data structures. This will break compatibility for out of tree builtin apps. - stream_session_t to session_t - server_rx/tx_fifo to rx/tx_fifo - stream_session.h to session_types.h - update copyright Change-Id: I414097c6e28bcbea866fbf13b8773c7db3f49325 Signed-off-by: Florin Coras <fcoras@cisco.com>
2019-01-28update openssl TLS async to align with openssl master branchPing Yu1-4/+2
Need to align with 3.0.0 version Change-Id: I4e8aec1f1226ce09963a9bbb3a9170d1863059ec Signed-off-by: Ping Yu <ping.yu@intel.com>
2019-01-07Change vpp code to align with openssl interface changePing Yu3-36/+27
PR in openssl community is almost done, and need to change some code in VPP to align with the openssl interface. Change-Id: Ic7da53e507b67b53958760d07738dd774b1c526d Signed-off-by: Ping Yu <ping.yu@intel.com>
2018-11-08tlsopenssl: remove unused #includeKlement Sekera1-1/+0
Change-Id: I294e4f93e925c58765d4692337208fcee7d12886 Signed-off-by: Klement Sekera <ksekera@cisco.com>
2018-10-23c11 safe string handling supportDave Barach2-3/+3
Change-Id: Ied34720ca5a6e6e717eea4e86003e854031b6eab Signed-off-by: Dave Barach <dave@barachs.net>
2018-10-02tls: fix disconnects for sessions with pending dataFlorin Coras1-12/+12
TLS can enqueue events to itself when app session queue cannot be entirely drained. If a pending disconnect is handled before any such event, session layer may try to dequeue data on deallocated sessions. Change-Id: I5bfc4d53ce95bc16b6a01e1b0e644aafa1ca311b Signed-off-by: Florin Coras <fcoras@cisco.com>
2018-09-27add cmake build option to build openssl asyncPing Yu1-0/+11
This code is orignally in automake, but it is missing in cmake. Thus add it to make openssl async work in cmake build system Change-Id: Ie69ee9c2099273e51ce13ccab27bdd2619db4814 Signed-off-by: Ping Yu <ping.yu@intel.com>
2018-09-15tls: fix openssl engine write complete conditionFlorin Coras1-1/+1
Change-Id: Ic1526f6916970ce2b0c4fc0d148d4396fa629b5f Signed-off-by: Florin Coras <fcoras@cisco.com>
2018-09-13Add a polling configure to make thread bind with hardware enginePing Yu1-4/+9
Change-Id: Ib4130098dd9bf45370bdee9a04e4804074df58b1 Signed-off-by: Ping Yu <ping.yu@intel.com>
2018-09-04add option to allow user to set ciphersPing Yu2-6/+33
Orignal code hard code TLS ciphers, and this patch allows user to set ciphers via CLI, so that user can perform the TLS testing without re-building the code. Change-Id: I0d497f6d906af25bc7a33cee5747f9a1d63e0683 Signed-off-by: Ping Yu <ping.yu@intel.com>
2018-08-26cmake: move functions to src/cmakeDamjan Marion1-0/+1
Change-Id: Ibcb7105fa7e3c09efdce01bccd4de235fe33ea99 Signed-off-by: Damjan Marion <damarion@cisco.com>
2018-08-25cmake: improve add_vpp_plugin macroDamjan Marion1-2/+8
Change-Id: Iffd5c45ab242a919592a1f686f7f880936b68a1a Signed-off-by: Damjan Marion <damarion@cisco.com>
2018-08-17CMake as an alternative to autotools (experimental)Damjan Marion1-0/+18
Change-Id: Ibc59323e849810531dd0963e85493efad3b86857 Signed-off-by: Damjan Marion <damarion@cisco.com>
2018-08-17optimize init_server to reduce session overheadPing Yu2-33/+107
move un-necessary session based operation to listener split orignal openssl ctx to be session based ctx and listen ctx Change-Id: Id6c54f47b0e2171fd8924a45efcd5266ce5402d5 Signed-off-by: Ping Yu <ping.yu@intel.com>
2018-08-14reduce polling and resume overhead by checking if inflight request existsPing Yu1-3/+6
Change-Id: I0777a00f0cc082bab3348be8ec0be39faa50ffed Signed-off-by: Ping Yu <ping.yu@intel.com>
2018-07-23tls: avoid possible async handler duplicationPing Yu1-1/+1
One handler is good enough when engine sends out a retry status Thus this patch will just go one branch Change-Id: Id81cb3fa67d2b322b0fe1b2f62cd866cf3491eb4 Signed-off-by: Ping Yu <ping.yu@intel.com>
2018-07-19Add a new communication channel between VPP and openssl enginePing Yu3-19/+59
Thus when engine buffer is full during a burst in performance tesing, this code will help VPP handle retry machansim. Change-Id: I0f9fc05d3dba8a54d34dca4c6137700d6c80f714 Signed-off-by: Ping Yu <ping.yu@intel.com>
2018-07-16Enable openssl TLS async support in client for HW acclerationPing Yu1-0/+15
Change-Id: I003e41786c549c6451a1e9e178f5871d32c20e6e Signed-off-by: Ping Yu <ping.yu@intel.com>
2018-06-15TLS async supportPing Yu3-22/+735
Change-Id: I26194e00dfb85e5cd1c65ff4e6ffd665be2d719b Signed-off-by: Ping Yu <ping.yu@intel.com>
2018-05-31Fix TLS issue to load certification and keyPing Yu1-0/+2
Change-Id: If1ef2d4bc6f90a4d4b6a345c63723117834c6504 Signed-off-by: Ping Yu <ping.yu@intel.com>
2018-03-15tls: add openssl engineFlorin Coras1-0/+675
Change-Id: I6c215858d2c9c620787632b570950b15274c0df2 Signed-off-by: Florin Coras <fcoras@cisco.com>
d-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */ }
/*
 *------------------------------------------------------------------
 * 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 "3.1"
/** Default name of application using libmemif. */
#define MEMIF_DEFAULT_APP_NAME "libmemif-app"

#include <inttypes.h>
#include <sys/timerfd.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 filename
				   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
 * @{
 */

/** user needs to set events that occurred on fd and pass them to memif_control_fd_handler */
#define MEMIF_FD_EVENT_READ  (1 << 0)
#define MEMIF_FD_EVENT_WRITE (1 << 1)
/** inform libmemif that error occurred on fd */
#define MEMIF_FD_EVENT_ERROR (1 << 2)
/** if set, informs that fd is going to be closed (user may want to stop watching for events on this fd) */
#define MEMIF_FD_EVENT_DEL   (1 << 3)
/** update events */
#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

    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 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,
					 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_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_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 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;
  uint8_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_filename - socket filename
    @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_filename;
  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 ();

/** \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 Memif initialization
    @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 - custom memory allocator, NULL = default
    @param memif_realloc - custom memory reallocation, NULL = default
    @param memif_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

    Initialize internal libmemif structures. Create timerfd (used to periodically request connection by
    disconnected memifs in slave mode, with no additional API call). This fd is passed to user with memif_control_fd_update_t
    timer is inactive at this state. It activates with if there is at least one memif in slave mode.

    \return memif_err_t
*/
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 - custom 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.

    \return 0
*/
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 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_interrupt_t * on_interrupt, void *private_ctx);

/** \brief Memif control file descriptor handler
    @param fd - file descriptor on which the event occurred
    @param events - event type(s) that occurred

    If event occurs on any control fd, call memif_control_fd_handler.
    Internal - lib will "identify" fd (timerfd, listener, control) and handle event accordingly.

    FD-TYPE -
        TIMERFD -
            Every disconnected memif in slave mode will request connection.
        LISTENER or CONTROL -
            Handle socket messaging (internal connection establishment).
        INTERRUPT -
            Call on_interrupt callback (if set).

    \return memif_err_t

*/
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 occurred
    @param events - event type(s) that occurred

    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


    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 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 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 (int timeout);

/** \brief Memif per thread 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.
    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 ();
/** \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

    Timer on which all disconnected slaves request connection.
    See system call 'timer_settime' man-page.

    \return memif_err_t
*/
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

    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 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 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, 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_socket.

    \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 const char *
*/
const char *memif_get_socket_filename (memif_socket_handle_t sock);

/** @} */

#endif /* _LIBMEMIF_H_ */