blob: d1c5b05090d2bc7333c21c4919afd9dfd34a1535 [file] [log] [blame]
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001/*
2 *------------------------------------------------------------------
3 * Copyright (c) 2017 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *------------------------------------------------------------------
16 */
17
Dave Wallace6cd396c2018-01-23 17:47:02 -050018/** @file
Paul Vinciguerrab5a575b2019-11-01 13:00:58 -040019 * @defgroup libmemif Example libmemif App
Dave Wallace6cd396c2018-01-23 17:47:02 -050020 */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020021
22#ifndef _LIBMEMIF_H_
23#define _LIBMEMIF_H_
24
25/** Libmemif version. */
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +020026#define LIBMEMIF_VERSION "3.1"
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020027/** Default name of application using libmemif. */
28#define MEMIF_DEFAULT_APP_NAME "libmemif-app"
29
30#include <inttypes.h>
Andrew Yourtchenko085e8d42021-03-09 19:02:06 +000031#include <sys/types.h>
Jakub Grajciar84b83772019-03-04 12:42:19 +010032#include <sys/timerfd.h>
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020033
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020034/*! Error codes */
35typedef enum
36{
37 MEMIF_ERR_SUCCESS = 0, /*!< success */
38/* SYSCALL ERRORS */
39 MEMIF_ERR_SYSCALL, /*!< other syscall error */
Jakub Grajciar568cc462018-09-05 12:11:35 +020040 MEMIF_ERR_CONNREFUSED, /*!< connection refused */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020041 MEMIF_ERR_ACCES, /*!< permission denied */
42 MEMIF_ERR_NO_FILE, /*!< file does not exist */
43 MEMIF_ERR_FILE_LIMIT, /*!< system open file limit */
44 MEMIF_ERR_PROC_FILE_LIMIT, /*!< process open file limit */
45 MEMIF_ERR_ALREADY, /*!< connection already requested */
46 MEMIF_ERR_AGAIN, /*!< fd is not socket, or operation would block */
47 MEMIF_ERR_BAD_FD, /*!< invalid fd */
48 MEMIF_ERR_NOMEM, /*!< out of memory */
49/* LIBMEMIF ERRORS */
50 MEMIF_ERR_INVAL_ARG, /*!< invalid argument */
51 MEMIF_ERR_NOCONN, /*!< handle points to no connection */
52 MEMIF_ERR_CONN, /*!< handle points to existing connection */
53 MEMIF_ERR_CB_FDUPDATE, /*!< user defined callback memif_control_fd_update_t error */
Jakub Grajciar84b83772019-03-04 12:42:19 +010054 MEMIF_ERR_FILE_NOT_SOCK, /*!< file specified by socket filename
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020055 exists, but it's not socket */
56 MEMIF_ERR_NO_SHMFD, /*!< missing shm fd */
57 MEMIF_ERR_COOKIE, /*!< wrong cookie on ring */
58 MEMIF_ERR_NOBUF_RING, /*!< ring buffer full */
59 MEMIF_ERR_NOBUF, /*!< not enough memif buffers */
60 MEMIF_ERR_NOBUF_DET, /*!< memif details needs larger buffer */
61 MEMIF_ERR_INT_WRITE, /*!< send interrupt error */
62 MEMIF_ERR_MFMSG, /*!< malformed msg received */
63 MEMIF_ERR_QID, /*!< invalid queue id */
64/* MEMIF PROTO ERRORS */
65 MEMIF_ERR_PROTO, /*!< incompatible protocol version */
66 MEMIF_ERR_ID, /*!< unmatched interface id */
67 MEMIF_ERR_ACCSLAVE, /*!< slave cannot accept connection requests */
68 MEMIF_ERR_ALRCONN, /*!< memif is already connected */
69 MEMIF_ERR_MODE, /*!< mode mismatch */
70 MEMIF_ERR_SECRET, /*!< secret mismatch */
71 MEMIF_ERR_NOSECRET, /*!< secret required */
72 MEMIF_ERR_MAXREG, /*!< max region limit reached */
73 MEMIF_ERR_MAXRING, /*!< max ring limit reached */
74 MEMIF_ERR_NO_INTFD, /*!< missing interrupt fd */
Paul Vinciguerraf4fbfd62020-05-15 23:13:36 -040075 MEMIF_ERR_DISCONNECT, /*!< disconnect received */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020076 MEMIF_ERR_DISCONNECTED, /*!< peer interface disconnected */
77 MEMIF_ERR_UNKNOWN_MSG, /*!< unknown message type */
Milan Lenco0a47c992017-10-12 14:19:31 +020078 MEMIF_ERR_POLL_CANCEL, /*!< memif_poll_event() was cancelled */
Damjan Marion6d56fa42017-11-03 12:24:37 +010079 MEMIF_ERR_MAX_RING, /*!< too large ring size */
Jakub Grajciarab7c2b02018-03-28 10:21:05 +020080 MEMIF_ERR_PRIVHDR, /*!< private hdrs not supported */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020081} memif_err_t;
82
83/**
84 * @defgroup MEMIF_FD_EVENT Types of events that need to be watched for specific fd.
Dave Wallace6cd396c2018-01-23 17:47:02 -050085 * @ingroup libmemif
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020086 * @{
87 */
88
Paul Vinciguerra62237662020-05-29 23:03:06 -040089/** user needs to set events that occurred on fd and pass them to memif_control_fd_handler */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020090#define MEMIF_FD_EVENT_READ (1 << 0)
91#define MEMIF_FD_EVENT_WRITE (1 << 1)
Paul Vinciguerra62237662020-05-29 23:03:06 -040092/** inform libmemif that error occurred on fd */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020093#define MEMIF_FD_EVENT_ERROR (1 << 2)
94/** if set, informs that fd is going to be closed (user may want to stop watching for events on this fd) */
95#define MEMIF_FD_EVENT_DEL (1 << 3)
96/** update events */
97#define MEMIF_FD_EVENT_MOD (1 << 4)
98/** @} */
99
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200100/** \brief Memif per thread main handle
101 Pointer of type void, pointing to internal structure.
102 Used to identify internal per thread database.
103*/
104typedef void *memif_per_thread_main_handle_t;
105
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200106/** \brief Memif connection handle
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200107 pointer of type void, pointing to internal structure
108*/
109typedef void *memif_conn_handle_t;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200110
Jakub Grajciar12df4972019-07-01 14:24:48 +0200111/** \brief Memif socket handle
112 pointer of type void, pointing to internal structure
113*/
114typedef void *memif_socket_handle_t;
115
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200116/** \brief Memif allocator alloc
117 @param size - requested allocation size
118
119 custom memory allocator: alloc function template
120*/
121typedef void *(memif_alloc_t) (size_t size);
122
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200123
124/** \brief Memif realloc
125 @param ptr - pointer to memory block
126 @param size - requested allocation size
127
128 custom memory reallocation
129*/
130typedef void *(memif_realloc_t) (void *ptr, size_t size);
131
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200132/** \brief Memif allocator free
133 @param size - requested allocation size
134
135 custom memory allocator: free function template
136*/
137typedef void (memif_free_t) (void *ptr);
138
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200139/**
140 * @defgroup CALLBACKS Callback functions definitions
Dave Wallace6cd396c2018-01-23 17:47:02 -0500141 * @ingroup libmemif
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200142 *
143 * @{
144 */
145
146/** \brief Memif control file descriptor update (callback function)
147 @param fd - new file descriptor to watch
148 @param events - event type(s) to watch for
Jakub Grajciar6f090fa2019-11-14 10:47:25 +0100149 @param private_ctx - libmemif main private context. Is NULL for
150 libmemif main created by memif_init()
151
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200152
153 This callback is called when there is new fd to watch for events on
154 or if fd is about to be closed (user mey want to stop watching for events on this fd).
Jakub Grajciar6f090fa2019-11-14 10:47:25 +0100155 Private context is taken from libmemif_main, 'private_ctx' passed to memif_per_thread_init()
156 or NULL in case of memif_init()
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200157*/
Jakub Grajciar12df4972019-07-01 14:24:48 +0200158typedef int (memif_control_fd_update_t) (int fd, uint8_t events,
159 void *private_ctx);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200160
161/** \brief Memif connection status update (callback function)
162 @param conn - memif connection handle
163 @param private_ctx - private context
164
165 Informs user about connection status connected/disconnected.
166 On connected -> start watching for events on interrupt fd (optional).
167*/
168typedef int (memif_connection_update_t) (memif_conn_handle_t conn,
169 void *private_ctx);
170
Paul Vinciguerra62237662020-05-29 23:03:06 -0400171/** \brief Memif interrupt occurred (callback function)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200172 @param conn - memif connection handle
173 @param private_ctx - private context
Paul Vinciguerra62237662020-05-29 23:03:06 -0400174 @param qid - queue id on which interrupt occurred
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200175
176 Called when event is received on interrupt fd.
177*/
178typedef int (memif_interrupt_t) (memif_conn_handle_t conn, void *private_ctx,
179 uint16_t qid);
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200180
181/** @} */
182
183/**
184 * @defgroup EXTERNAL_REGION External region APIs
185 * @ingroup libmemif
186 *
187 * @{
188 */
189
190/** \brief Get external buffer offset (optional)
191 @param private_ctx - private context
192
193 Find unallocated external buffer and return its offset.
194*/
195typedef uint32_t (memif_get_external_buffer_offset_t) (void *private_ctx);
196
197/** \brief Add external region
198 @param[out] addr - region address
199 @param size - requested region size
200 @param fd[out] - file descriptor
201 @param private_ctx - private context
202
203 Called by slave. Add external region created by client.
204*/
205typedef int (memif_add_external_region_t) (void * *addr, uint32_t size,
206 int *fd, void *private_ctx);
207
208/** \brief Get external region address
209 @param size - requested region size
210 @param fd - file descriptor
211 @param private_ctx - private context
212
213 Called by master. Get region address from client.
214
215 \return region address
216*/
217typedef void *(memif_get_external_region_addr_t) (uint32_t size, int fd,
218 void *private_ctx);
219
220/** \brief Delete external region
221 @param addr - region address
222 @param size - region size
223 @param fd - file descriptor
224 @param private_ctx - private context
225
226 Delete external region.
227*/
228typedef int (memif_del_external_region_t) (void *addr, uint32_t size, int fd,
229 void *private_ctx);
230
231/** \brief Register external region
232 @param ar - add external region callback
233 @param gr - get external region addr callback
234 @param dr - delete external region callback
235 @param go - get external buffer offset callback (optional)
236*/
237void memif_register_external_region (memif_add_external_region_t * ar,
238 memif_get_external_region_addr_t * gr,
239 memif_del_external_region_t * dr,
240 memif_get_external_buffer_offset_t * go);
241
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200242/** \brief Register external region
243 @param pt_main - per thread main handle
244 @param ar - add external region callback
245 @param gr - get external region addr callback
246 @param dr - delete external region callback
247 @param go - get external buffer offset callback (optional)
Paul Vinciguerra62237662020-05-29 23:03:06 -0400248
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200249void memif_per_thread_register_external_region (memif_per_thread_main_handle_t
250 pt_main,
251 memif_add_external_region_t *
252 ar,
253 memif_get_external_region_addr_t
254 * gr,
255 memif_del_external_region_t *
256 dr,
257 memif_get_external_buffer_offset_t
258 * go);
259
Paul Vinciguerra62237662020-05-29 23:03:06 -0400260 @} */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200261
262/**
263 * @defgroup ARGS_N_BUFS Connection arguments and buffers
Dave Wallace6cd396c2018-01-23 17:47:02 -0500264 * @ingroup libmemif
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200265 *
266 * @{
267 */
268
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200269#ifndef _MEMIF_H_
270typedef enum
271{
272 MEMIF_INTERFACE_MODE_ETHERNET = 0,
273 MEMIF_INTERFACE_MODE_IP = 1,
274 MEMIF_INTERFACE_MODE_PUNT_INJECT = 2,
275} memif_interface_mode_t;
276#endif /* _MEMIF_H_ */
277
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200278/** \brief Memif connection arguments
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200279 @param socket - Memif socket handle, if NULL default socket will be used.
280 Default socket is only supported in global database (see memif_init).
281 Custom database does not create a default socket
282 (see memif_per_thread_init).
283 Memif connection is stored in the same database as the socket.
Paul Vinciguerraf4fbfd62020-05-15 23:13:36 -0400284 @param secret - optional parameter used as interface authentication
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200285 @param num_s2m_rings - number of slave to master rings
286 @param num_m2s_rings - number of master to slave rings
287 @param buffer_size - size of buffer in shared memory
288 @param log2_ring_size - logarithm base 2 of ring size
289 @param is_master - 0 == master, 1 == slave
290 @param interface_id - id used to identify peer connection
291 @param interface_name - interface name
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200292 @param mode - 0 == ethernet, 1 == ip , 2 == punt/inject
293*/
294typedef struct
295{
Jakub Grajciar12df4972019-07-01 14:24:48 +0200296 memif_socket_handle_t socket; /*!< default = /run/vpp/memif.sock */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200297 uint8_t secret[24]; /*!< optional (interface authentication) */
298
299 uint8_t num_s2m_rings; /*!< default = 1 */
300 uint8_t num_m2s_rings; /*!< default = 1 */
301 uint16_t buffer_size; /*!< default = 2048 */
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200302 uint8_t log2_ring_size; /*!< default = 10 (1024) */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200303 uint8_t is_master;
304
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200305 uint32_t interface_id;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200306 uint8_t interface_name[32];
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200307 memif_interface_mode_t mode:8;
308} memif_conn_args_t;
309
310/*! memif receive mode */
311typedef enum
312{
313 MEMIF_RX_MODE_INTERRUPT = 0, /*!< interrupt mode */
314 MEMIF_RX_MODE_POLLING /*!< polling mode */
315} memif_rx_mode_t;
316
317/** \brief Memif buffer
318 @param desc_index - ring descriptor index
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200319 @param ring - pointer to ring containing descriptor for this buffer
320 @param len - available length
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200321 @param flags - memif buffer flags
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200322 @param data - pointer to shared memory data
323*/
324typedef struct
325{
326 uint16_t desc_index;
Jakub Grajciarf35fef22021-01-08 15:01:13 +0100327 void *queue;
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200328 uint32_t len;
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200329/** next buffer present (chained buffers) */
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200330#define MEMIF_BUFFER_FLAG_NEXT (1 << 0)
331 uint8_t flags;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200332 void *data;
333} memif_buffer_t;
334/** @} */
335
336/**
337 * @defgroup MEMIF_DETAILS Memif details structs
Dave Wallace6cd396c2018-01-23 17:47:02 -0500338 * @ingroup libmemif
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200339 *
340 * @{
341 */
342
343/** \brief Memif queue details
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200344 @param region - region index
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200345 @param qid - queue id
Paul Vinciguerraf4fbfd62020-05-15 23:13:36 -0400346 @param ring_size - size of ring buffer in shared memory
Jakub Grajciar84197552017-11-16 14:02:49 +0100347 @param flags - ring flags
348 @param head - ring head pointer
349 @param tail - ring tail pointer
Paul Vinciguerraf4fbfd62020-05-15 23:13:36 -0400350 @param buffer_size - buffer size on shared memory
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200351*/
352typedef struct
353{
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200354 uint8_t region;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200355 uint8_t qid;
356 uint32_t ring_size;
Jakub Grajciar84197552017-11-16 14:02:49 +0100357/** if set queue is in polling mode, else in interrupt mode */
358#define MEMIF_QUEUE_FLAG_POLLING 1
359 uint16_t flags;
360 uint16_t head;
361 uint16_t tail;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200362 uint16_t buffer_size;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200363} memif_queue_details_t;
364
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200365/** \brief Memif region details
366 @param index - region index
367 @param addr - region address
368 @param size - region size
369 @param fd - file descriptor
370 @param is_external - if not zero then region is defined by client
371*/
372typedef struct
373{
374 uint8_t index;
375 void *addr;
376 uint32_t size;
377 int fd;
378 uint8_t is_external;
379} memif_region_details_t;
380
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200381/** \brief Memif details
382 @param if_name - interface name
383 @param inst_name - application name
384 @param remote_if_name - peer interface name
385 @param remote_inst_name - peer application name
386 @param id - connection id
387 @param secret - secret
388 @param role - 0 = master, 1 = slave
389 @param mode - 0 = ethernet, 1 = ip , 2 = punt/inject
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200390 @param socket_filename - socket filename
391 @param regions_num - number of regions
392 @param regions - struct containing region details
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200393 @param rx_queues_num - number of receive queues
394 @param tx_queues_num - number of transmit queues
395 @param rx_queues - struct containing receive queue details
396 @param tx_queues - struct containing transmit queue details
Jakub Grajciar568cc462018-09-05 12:11:35 +0200397 @param error - error string
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200398 @param link_up_down - 1 = up (connected), 2 = down (disconnected)
399*/
400typedef struct
401{
402 uint8_t *if_name;
403 uint8_t *inst_name;
404 uint8_t *remote_if_name;
405 uint8_t *remote_inst_name;
406
407 uint32_t id;
408 uint8_t *secret; /* optional */
409 uint8_t role; /* 0 = master, 1 = slave */
410 uint8_t mode; /* 0 = ethernet, 1 = ip, 2 = punt/inject */
411 uint8_t *socket_filename;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200412 uint8_t regions_num;
413 memif_region_details_t *regions;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200414 uint8_t rx_queues_num;
415 uint8_t tx_queues_num;
416 memif_queue_details_t *rx_queues;
417 memif_queue_details_t *tx_queues;
418
Jakub Grajciar568cc462018-09-05 12:11:35 +0200419 uint8_t *error;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200420 uint8_t link_up_down; /* 1 = up, 0 = down */
421} memif_details_t;
422/** @} */
423
424/**
425 * @defgroup API_CALLS Api calls
Dave Wallace6cd396c2018-01-23 17:47:02 -0500426 * @ingroup libmemif
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200427 *
428 * @{
429 */
430
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200431/** \brief Memif get version
432
433 \return ((MEMIF_VERSION_MAJOR << 8) | MEMIF_VERSION_MINOR)
434*/
435uint16_t memif_get_version ();
436
Paul Vinciguerraf4fbfd62020-05-15 23:13:36 -0400437/** \brief Memif get queue event file descriptor
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200438 @param conn - memif connection handle
439 @param qid - queue id
440 @param[out] fd - returns event file descriptor
441
442 \return memif_err_t
443*/
444
445int memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *fd);
446
447/** \brief Memif set rx mode
448 @param conn - memif connection handle
449 @param rx_mode - receive mode
450 @param qid - queue id
451
452 \return memif_err_t
453*/
454int memif_set_rx_mode (memif_conn_handle_t conn, memif_rx_mode_t rx_mode,
455 uint16_t qid);
456
457/** \brief Memif strerror
458 @param err_code - error code
459
460 Converts error code to error message.
Jakub Grajciar84b83772019-03-04 12:42:19 +0100461
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200462 \return Error string
463*/
464char *memif_strerror (int err_code);
465
466/** \brief Memif get details
Paul Vinciguerraf4fbfd62020-05-15 23:13:36 -0400467 @param conn - memif connection handle
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200468 @param md - pointer to memif details struct
469 @param buf - buffer containing details strings
470 @param buflen - length of buffer
471
472 \return memif_err_t
473*/
474int memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
475 char *buf, ssize_t buflen);
476
477/** \brief Memif initialization
478 @param on_control_fd_update - if control fd updates inform user to watch new fd
Jakub Grajciar19418712018-03-13 13:57:50 +0100479 @param app_name - application name (will be truncated to 32 chars)
Paul Vinciguerraf4fbfd62020-05-15 23:13:36 -0400480 @param memif_alloc - custom memory allocator, NULL = default
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200481 @param memif_realloc - custom memory reallocation, NULL = default
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200482 @param memif_free - custom memory free, NULL = default
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200483
484 if param on_control_fd_update is set to NULL,
485 libmemif will handle file descriptor event polling
486 if a valid callback is set, file descriptor event polling needs to be done by
487 user application, all file descriptors and event types will be passed in
488 this callback to user application
489
490 Initialize internal libmemif structures. Create timerfd (used to periodically request connection by
491 disconnected memifs in slave mode, with no additional API call). This fd is passed to user with memif_control_fd_update_t
492 timer is inactive at this state. It activates with if there is at least one memif in slave mode.
493
494 \return memif_err_t
495*/
496int memif_init (memif_control_fd_update_t * on_control_fd_update,
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200497 char *app_name, memif_alloc_t * memif_alloc,
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200498 memif_realloc_t * memif_realloc, memif_free_t * memif_free);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200499
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200500/** \brief Memif per thread initialization
501 @param pt_main - per thread main handle
502 @param private_ctx - private context
503 @param on_control_fd_update - if control fd updates inform user to watch new fd
504 @param app_name - application name (will be truncated to 32 chars)
Paul Vinciguerraf4fbfd62020-05-15 23:13:36 -0400505 @param memif_alloc - custom memory allocator, NULL = default
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200506 @param memif_realloc - custom memory reallocation, NULL = default
507 @param memif_free - custom memory free, NULL = default
508
509 Per thread version of memif_init ().
510 Instead of using global database, creates and initializes unique database,
511 identified by 'memif_per_thread_main_handle_t'.
512
513 \return memif_err_t
514*/
515int memif_per_thread_init (memif_per_thread_main_handle_t * pt_main,
516 void *private_ctx,
517 memif_control_fd_update_t * on_control_fd_update,
518 char *app_name, memif_alloc_t * memif_alloc,
519 memif_realloc_t * memif_realloc,
520 memif_free_t * memif_free);
521
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200522/** \brief Memif cleanup
523
524 Free libmemif internal allocations.
525
526 \return 0
527*/
528int memif_cleanup ();
529
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200530/** \brief Memif per thread cleanup
531 @param pt_main - per thread main handle
532
533 Free libmemif internal allocations and sets the handle to NULL.
534
535 \return memif_err_t
536*/
537int memif_per_thread_cleanup (memif_per_thread_main_handle_t * pt_main);
538
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200539/** \brief Memory interface create function
Jakub Grajciar12df4972019-07-01 14:24:48 +0200540 @param conn - connection handle for client app
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200541 @param args - memory interface connection arguments
542 @param on_connect - inform user about connected status
543 @param on_disconnect - inform user about disconnected status
544 @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
Paul Vinciguerraf4fbfd62020-05-15 23:13:36 -0400545 @param private_ctx - private context passed back to user with callback
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200546
547 Creates memory interface.
Jakub Grajciar84b83772019-03-04 12:42:19 +0100548
549 SLAVE-MODE -
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200550 Start timer that will send events to timerfd. If this fd is passed to memif_control_fd_handler
551 every disconnected memif in slave mode will send connection request.
552 On success new fd is passed to user with memif_control_fd_update_t.
553
Jakub Grajciar84b83772019-03-04 12:42:19 +0100554 MASTER-MODE -
Paul Vinciguerraf4fbfd62020-05-15 23:13:36 -0400555 Create listener socket and pass fd to user with memif_control_fd_update_t.
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200556 If this fd is passed to memif_control_fd_handler accept will be called and
557 new fd will be passed to user with memif_control_fd_update_t.
558
559
560 \return memif_err_t
561*/
562int memif_create (memif_conn_handle_t * conn, memif_conn_args_t * args,
563 memif_connection_update_t * on_connect,
564 memif_connection_update_t * on_disconnect,
565 memif_interrupt_t * on_interrupt, void *private_ctx);
566
567/** \brief Memif control file descriptor handler
Paul Vinciguerraf4fbfd62020-05-15 23:13:36 -0400568 @param fd - file descriptor on which the event occurred
569 @param events - event type(s) that occurred
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200570
Paul Vinciguerraf4fbfd62020-05-15 23:13:36 -0400571 If event occurs on any control fd, call memif_control_fd_handler.
572 Internal - lib will "identify" fd (timerfd, listener, control) and handle event accordingly.
Jakub Grajciar84b83772019-03-04 12:42:19 +0100573
574 FD-TYPE -
575 TIMERFD -
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200576 Every disconnected memif in slave mode will request connection.
Jakub Grajciar84b83772019-03-04 12:42:19 +0100577 LISTENER or CONTROL -
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200578 Handle socket messaging (internal connection establishment).
Jakub Grajciar84b83772019-03-04 12:42:19 +0100579 INTERRUPT -
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200580 Call on_interrupt callback (if set).
Jakub Grajciar84b83772019-03-04 12:42:19 +0100581
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200582 \return memif_err_t
583
584*/
585int memif_control_fd_handler (int fd, uint8_t events);
586
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200587/** \brief Memif per thread control file descriptor handler
588 @param pt_main - per thread main handle
Paul Vinciguerraf4fbfd62020-05-15 23:13:36 -0400589 @param fd - file descriptor on which the event occurred
590 @param events - event type(s) that occurred
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200591
592 Per thread version of memif_control_fd_handler.
593
594 \return memif_err_t
595
596*/
597int memif_per_thread_control_fd_handler (memif_per_thread_main_handle_t
598 pt_main, int fd, uint8_t events);
599
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200600/** \brief Memif delete
601 @param conn - pointer to memif connection handle
602
603
604 disconnect session (free queues and regions, close file descriptors, unmap shared memory)
605 set connection handle to NULL, to avoid possible double free
606
607 \return memif_err_t
608*/
609int memif_delete (memif_conn_handle_t * conn);
610
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200611/** \brief Memif buffer enq tx
Paul Vinciguerraf4fbfd62020-05-15 23:13:36 -0400612 @param conn - memif connection handle
613 @param qid - number identifying queue
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200614 @param bufs - memif buffers
Paul Vinciguerraf4fbfd62020-05-15 23:13:36 -0400615 @param count - number of memif buffers to enqueue
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200616 @param count_out - returns number of allocated buffers
617
Jakub Grajciarf35fef22021-01-08 15:01:13 +0100618 Enqueue buffers to specified tx queue. Can only be used by slave.
619 Updates desc_index field for each memif buffer.
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200620 If connection handle points to master returns MEMIF_ERR_INVAL_ARG.
621
622 \return memif_err_t
623*/
624int memif_buffer_enq_tx (memif_conn_handle_t conn, uint16_t qid,
625 memif_buffer_t * bufs, uint16_t count,
626 uint16_t * count_out);
627
Jakub Grajciarf35fef22021-01-08 15:01:13 +0100628/** \brief Memif buffer enq tx at idx
629 @param conn - memif connection handle
630 @param buf_a - memif buffer
631 @param buf_b - memif buffer
632
633 Swap descriptors for provided buffers and update the buffers
634*/
635int memif_buffer_requeue (memif_conn_handle_t conn, memif_buffer_t *buf_a,
636 memif_buffer_t *buf_b);
637
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200638/** \brief Memif buffer alloc
Paul Vinciguerraf4fbfd62020-05-15 23:13:36 -0400639 @param conn - memif connection handle
640 @param qid - number identifying queue
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200641 @param bufs - memif buffers
642 @param count - number of memif buffers to allocate
643 @param count_out - returns number of allocated buffers
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200644 @param size - buffer size, may return chained buffers if size > buffer_size
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200645
646 \return memif_err_t
647*/
648int memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
649 memif_buffer_t * bufs, uint16_t count,
Jakub Grajciarb467b2a2017-09-14 14:12:10 +0200650 uint16_t * count_out, uint16_t size);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200651
Jakub Grajciar47e68de2021-01-08 15:32:43 +0100652/** \brief Memif set next free buffer
653 @param conn - memif connection handle
654 @param qid - number identifying queue
655 @param buf - next free buffer
656
657 Sets next free descriptor pointer for specified tx queue.
658 The next allocation will happen at this buffer.
659*/
660int memif_set_next_free_buffer (memif_conn_handle_t conn, uint16_t qid,
661 memif_buffer_t *buf);
662
Jakub Grajciarf35fef22021-01-08 15:01:13 +0100663/** \brief Memif refill queue
Paul Vinciguerraf4fbfd62020-05-15 23:13:36 -0400664 @param conn - memif connection handle
665 @param qid - number identifying queue
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200666 @param count - number of buffers to be placed on ring
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200667 @param headroom - offset the buffer by headroom
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200668
669 \return memif_err_t
670*/
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200671int memif_refill_queue (memif_conn_handle_t conn, uint16_t qid,
Jakub Grajciar3744fc72018-03-29 13:15:10 +0200672 uint16_t count, uint16_t headroom);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200673
674/** \brief Memif transmit buffer burst
Paul Vinciguerraf4fbfd62020-05-15 23:13:36 -0400675 @param conn - memif connection handle
676 @param qid - number identifying queue
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200677 @param bufs - memif buffers
678 @param count - number of memif buffers to transmit
679 @param tx - returns number of transmitted buffers
680
681 \return memif_err_t
682*/
683int memif_tx_burst (memif_conn_handle_t conn, uint16_t qid,
684 memif_buffer_t * bufs, uint16_t count, uint16_t * tx);
685
686/** \brief Memif receive buffer burst
Paul Vinciguerraf4fbfd62020-05-15 23:13:36 -0400687 @param conn - memif connection handle
688 @param qid - number identifying queue
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200689 @param bufs - memif buffers
690 @param count - number of memif buffers to receive
691 @param rx - returns number of received buffers
692
Jan Cavojsky77a95cd2020-03-03 16:25:58 +0100693 Consume interrupt event for receive queue.
694 The event is not consumed, if memif_rx_burst fails.
695
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200696 \return memif_err_t
697*/
698int memif_rx_burst (memif_conn_handle_t conn, uint16_t qid,
699 memif_buffer_t * bufs, uint16_t count, uint16_t * rx);
700
701/** \brief Memif poll event
702 @param timeout - timeout in seconds
703
Jakub Grajciar84b83772019-03-04 12:42:19 +0100704 Passive event polling -
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200705 timeout = 0 - dont wait for event, check event queue if there is an event and return.
706 timeout = -1 - wait until event
707
708 \return memif_err_t
709*/
710int memif_poll_event (int timeout);
Milan Lenco0a47c992017-10-12 14:19:31 +0200711
Paul Vinciguerra62237662020-05-29 23:03:06 -0400712/** \brief Memif per thread poll event
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200713 @param pt_main - per thread main handle
714 @param timeout - timeout in seconds
715
716 Per thread version of memif_poll_event.
717
718 \return memif_err_t
719*/
720int memif_per_thread_poll_event (memif_per_thread_main_handle_t pt_main,
721 int timeout);
722
Milan Lenco0a47c992017-10-12 14:19:31 +0200723/** \brief Send signal to stop concurrently running memif_poll_event().
724
725 The function, however, does not wait for memif_poll_event() to stop.
Paul Vinciguerraf4fbfd62020-05-15 23:13:36 -0400726 memif_poll_event() may still return simply because an event has occurred
Milan Lenco0a47c992017-10-12 14:19:31 +0200727 or the timeout has elapsed, but if called repeatedly in an infinite loop,
Paul Vinciguerraf4fbfd62020-05-15 23:13:36 -0400728 a canceled memif_poll_event() is guaranteed to return MEMIF_ERR_POLL_CANCEL
Milan Lenco0a47c992017-10-12 14:19:31 +0200729 in the shortest possible time.
730 This feature was not available in the first release.
731 Use macro MEMIF_HAVE_CANCEL_POLL_EVENT to check if the feature is present.
732
733 \return memif_err_t
734*/
735#define MEMIF_HAVE_CANCEL_POLL_EVENT 1
736int memif_cancel_poll_event ();
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200737/** \brief Send signal to stop concurrently running memif_poll_event().
738 @param pt_main - per thread main handle
739
740 Per thread version of memif_cancel_poll_event.
741
742 \return memif_err_t
743*/
744int memif_per_thread_cancel_poll_event (memif_per_thread_main_handle_t
745 pt_main);
Jakub Grajciar84b83772019-03-04 12:42:19 +0100746
747/** \brief Set connection request timer value
748 @param timer - new timer value
749
750 Timer on which all disconnected slaves request connection.
751 See system call 'timer_settime' man-page.
752
753 \return memif_err_t
754*/
Jakub Grajciar12df4972019-07-01 14:24:48 +0200755int memif_set_connection_request_timer (struct itimerspec timer);
Jakub Grajciar84b83772019-03-04 12:42:19 +0100756
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200757/** \brief Set connection request timer value
758 @param pt_main - per thread main handle
759 @param timer - new timer value
760
761 Per thread version of memif_set_connection_request_timer
762
763 \return memif_err_t
764*/
765int
766memif_per_thread_set_connection_request_timer (memif_per_thread_main_handle_t
767 pt_main,
768 struct itimerspec timer);
769
Jakub Grajciar84b83772019-03-04 12:42:19 +0100770/** \brief Send connection request
771 @param conn - memif connection handle
772
773 Only slave interface can request connection.
774
775 \return memif_err_t
776*/
Jakub Grajciar12df4972019-07-01 14:24:48 +0200777int memif_request_connection (memif_conn_handle_t conn);
778
779/** \brief Create memif socket
780 @param sock - socket handle for client app
781 @param filename - path to socket file
782 @param private_ctx - private context
783
784 The first time an interface is assigned a socket, its type is determined.
785 For master role it's 'listener', for slave role it's 'client'. Each interface
Paul Vinciguerraf4fbfd62020-05-15 23:13:36 -0400786 requires socket of its respective type. Default socket is created if no
Jakub Grajciar12df4972019-07-01 14:24:48 +0200787 socket handle is passed to memif_create(). It's private context is NULL.
788 If all interfaces using this socket are deleted, the socket returns
789 to its default state.
790
791 \return memif_err_t
792*/
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200793int memif_create_socket (memif_socket_handle_t * sock, const char *filename,
794 void *private_ctx);
795
796/** \brief Create memif socket
797 @param pt_main - per thread main handle
798 @param sock - socket handle for client app
799 @param filename - path to socket file
800 @param private_ctx - private context
801
Paul Vinciguerraf4fbfd62020-05-15 23:13:36 -0400802 Per thread version of memif_create_socket.
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200803
804 \return memif_err_t
805*/
806int memif_per_thread_create_socket (memif_per_thread_main_handle_t pt_main,
807 memif_socket_handle_t * sock,
808 const char *filename, void *private_ctx);
Jakub Grajciar12df4972019-07-01 14:24:48 +0200809
810/** \brief Delete memif socket
811 @param sock - socket handle for client app
812
813 When trying to free socket in use, socket will not be freed and
814 MEMIF_ERR_INVAL_ARG is returned.
815
816 \return memif_err_t
817*/
818int memif_delete_socket (memif_socket_handle_t * sock);
819
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200820/** \brief Get socket filename
821 @param sock - socket handle for client app
822
823 Return constant pointer to socket filename.
824
Paul Vinciguerraf4fbfd62020-05-15 23:13:36 -0400825 \return const char *
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200826*/
827const char *memif_get_socket_filename (memif_socket_handle_t sock);
828
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200829/** @} */
830
831#endif /* _LIBMEMIF_H_ */