Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1 | /* |
| 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 Wallace | 6cd396c | 2018-01-23 17:47:02 -0500 | [diff] [blame] | 18 | /** @file |
Paul Vinciguerra | b5a575b | 2019-11-01 13:00:58 -0400 | [diff] [blame] | 19 | * @defgroup libmemif Example libmemif App |
Dave Wallace | 6cd396c | 2018-01-23 17:47:02 -0500 | [diff] [blame] | 20 | */ |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 21 | |
| 22 | #ifndef _LIBMEMIF_H_ |
| 23 | #define _LIBMEMIF_H_ |
| 24 | |
| 25 | /** Libmemif version. */ |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 26 | #define LIBMEMIF_VERSION "3.1" |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 27 | /** Default name of application using libmemif. */ |
| 28 | #define MEMIF_DEFAULT_APP_NAME "libmemif-app" |
| 29 | |
| 30 | #include <inttypes.h> |
Andrew Yourtchenko | 085e8d4 | 2021-03-09 19:02:06 +0000 | [diff] [blame^] | 31 | #include <sys/types.h> |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 32 | #include <sys/timerfd.h> |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 33 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 34 | /*! Error codes */ |
| 35 | typedef enum |
| 36 | { |
| 37 | MEMIF_ERR_SUCCESS = 0, /*!< success */ |
| 38 | /* SYSCALL ERRORS */ |
| 39 | MEMIF_ERR_SYSCALL, /*!< other syscall error */ |
Jakub Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 40 | MEMIF_ERR_CONNREFUSED, /*!< connection refused */ |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 41 | 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 Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 54 | MEMIF_ERR_FILE_NOT_SOCK, /*!< file specified by socket filename |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 55 | 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 Vinciguerra | f4fbfd6 | 2020-05-15 23:13:36 -0400 | [diff] [blame] | 75 | MEMIF_ERR_DISCONNECT, /*!< disconnect received */ |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 76 | MEMIF_ERR_DISCONNECTED, /*!< peer interface disconnected */ |
| 77 | MEMIF_ERR_UNKNOWN_MSG, /*!< unknown message type */ |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 78 | MEMIF_ERR_POLL_CANCEL, /*!< memif_poll_event() was cancelled */ |
Damjan Marion | 6d56fa4 | 2017-11-03 12:24:37 +0100 | [diff] [blame] | 79 | MEMIF_ERR_MAX_RING, /*!< too large ring size */ |
Jakub Grajciar | ab7c2b0 | 2018-03-28 10:21:05 +0200 | [diff] [blame] | 80 | MEMIF_ERR_PRIVHDR, /*!< private hdrs not supported */ |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 81 | } memif_err_t; |
| 82 | |
| 83 | /** |
| 84 | * @defgroup MEMIF_FD_EVENT Types of events that need to be watched for specific fd. |
Dave Wallace | 6cd396c | 2018-01-23 17:47:02 -0500 | [diff] [blame] | 85 | * @ingroup libmemif |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 86 | * @{ |
| 87 | */ |
| 88 | |
Paul Vinciguerra | 6223766 | 2020-05-29 23:03:06 -0400 | [diff] [blame] | 89 | /** user needs to set events that occurred on fd and pass them to memif_control_fd_handler */ |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 90 | #define MEMIF_FD_EVENT_READ (1 << 0) |
| 91 | #define MEMIF_FD_EVENT_WRITE (1 << 1) |
Paul Vinciguerra | 6223766 | 2020-05-29 23:03:06 -0400 | [diff] [blame] | 92 | /** inform libmemif that error occurred on fd */ |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 93 | #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 Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 100 | /** \brief Memif per thread main handle |
| 101 | Pointer of type void, pointing to internal structure. |
| 102 | Used to identify internal per thread database. |
| 103 | */ |
| 104 | typedef void *memif_per_thread_main_handle_t; |
| 105 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 106 | /** \brief Memif connection handle |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 107 | pointer of type void, pointing to internal structure |
| 108 | */ |
| 109 | typedef void *memif_conn_handle_t; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 110 | |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 111 | /** \brief Memif socket handle |
| 112 | pointer of type void, pointing to internal structure |
| 113 | */ |
| 114 | typedef void *memif_socket_handle_t; |
| 115 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 116 | /** \brief Memif allocator alloc |
| 117 | @param size - requested allocation size |
| 118 | |
| 119 | custom memory allocator: alloc function template |
| 120 | */ |
| 121 | typedef void *(memif_alloc_t) (size_t size); |
| 122 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 123 | |
| 124 | /** \brief Memif realloc |
| 125 | @param ptr - pointer to memory block |
| 126 | @param size - requested allocation size |
| 127 | |
| 128 | custom memory reallocation |
| 129 | */ |
| 130 | typedef void *(memif_realloc_t) (void *ptr, size_t size); |
| 131 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 132 | /** \brief Memif allocator free |
| 133 | @param size - requested allocation size |
| 134 | |
| 135 | custom memory allocator: free function template |
| 136 | */ |
| 137 | typedef void (memif_free_t) (void *ptr); |
| 138 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 139 | /** |
| 140 | * @defgroup CALLBACKS Callback functions definitions |
Dave Wallace | 6cd396c | 2018-01-23 17:47:02 -0500 | [diff] [blame] | 141 | * @ingroup libmemif |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 142 | * |
| 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 Grajciar | 6f090fa | 2019-11-14 10:47:25 +0100 | [diff] [blame] | 149 | @param private_ctx - libmemif main private context. Is NULL for |
| 150 | libmemif main created by memif_init() |
| 151 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 152 | |
| 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 Grajciar | 6f090fa | 2019-11-14 10:47:25 +0100 | [diff] [blame] | 155 | Private context is taken from libmemif_main, 'private_ctx' passed to memif_per_thread_init() |
| 156 | or NULL in case of memif_init() |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 157 | */ |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 158 | typedef int (memif_control_fd_update_t) (int fd, uint8_t events, |
| 159 | void *private_ctx); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 160 | |
| 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 | */ |
| 168 | typedef int (memif_connection_update_t) (memif_conn_handle_t conn, |
| 169 | void *private_ctx); |
| 170 | |
Paul Vinciguerra | 6223766 | 2020-05-29 23:03:06 -0400 | [diff] [blame] | 171 | /** \brief Memif interrupt occurred (callback function) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 172 | @param conn - memif connection handle |
| 173 | @param private_ctx - private context |
Paul Vinciguerra | 6223766 | 2020-05-29 23:03:06 -0400 | [diff] [blame] | 174 | @param qid - queue id on which interrupt occurred |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 175 | |
| 176 | Called when event is received on interrupt fd. |
| 177 | */ |
| 178 | typedef int (memif_interrupt_t) (memif_conn_handle_t conn, void *private_ctx, |
| 179 | uint16_t qid); |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 180 | |
| 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 | */ |
| 195 | typedef 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 | */ |
| 205 | typedef 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 | */ |
| 217 | typedef 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 | */ |
| 228 | typedef 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 | */ |
| 237 | void 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 Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 242 | /** \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 Vinciguerra | 6223766 | 2020-05-29 23:03:06 -0400 | [diff] [blame] | 248 | |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 249 | void 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 Vinciguerra | 6223766 | 2020-05-29 23:03:06 -0400 | [diff] [blame] | 260 | @} */ |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 261 | |
| 262 | /** |
| 263 | * @defgroup ARGS_N_BUFS Connection arguments and buffers |
Dave Wallace | 6cd396c | 2018-01-23 17:47:02 -0500 | [diff] [blame] | 264 | * @ingroup libmemif |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 265 | * |
| 266 | * @{ |
| 267 | */ |
| 268 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 269 | #ifndef _MEMIF_H_ |
| 270 | typedef 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 Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 278 | /** \brief Memif connection arguments |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 279 | @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 Vinciguerra | f4fbfd6 | 2020-05-15 23:13:36 -0400 | [diff] [blame] | 284 | @param secret - optional parameter used as interface authentication |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 285 | @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 Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 292 | @param mode - 0 == ethernet, 1 == ip , 2 == punt/inject |
| 293 | */ |
| 294 | typedef struct |
| 295 | { |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 296 | memif_socket_handle_t socket; /*!< default = /run/vpp/memif.sock */ |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 297 | 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 Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 302 | uint8_t log2_ring_size; /*!< default = 10 (1024) */ |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 303 | uint8_t is_master; |
| 304 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 305 | uint32_t interface_id; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 306 | uint8_t interface_name[32]; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 307 | memif_interface_mode_t mode:8; |
| 308 | } memif_conn_args_t; |
| 309 | |
| 310 | /*! memif receive mode */ |
| 311 | typedef 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 Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 319 | @param ring - pointer to ring containing descriptor for this buffer |
| 320 | @param len - available length |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 321 | @param flags - memif buffer flags |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 322 | @param data - pointer to shared memory data |
| 323 | */ |
| 324 | typedef struct |
| 325 | { |
| 326 | uint16_t desc_index; |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 327 | void *queue; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 328 | uint32_t len; |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 329 | /** next buffer present (chained buffers) */ |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 330 | #define MEMIF_BUFFER_FLAG_NEXT (1 << 0) |
| 331 | uint8_t flags; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 332 | void *data; |
| 333 | } memif_buffer_t; |
| 334 | /** @} */ |
| 335 | |
| 336 | /** |
| 337 | * @defgroup MEMIF_DETAILS Memif details structs |
Dave Wallace | 6cd396c | 2018-01-23 17:47:02 -0500 | [diff] [blame] | 338 | * @ingroup libmemif |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 339 | * |
| 340 | * @{ |
| 341 | */ |
| 342 | |
| 343 | /** \brief Memif queue details |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 344 | @param region - region index |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 345 | @param qid - queue id |
Paul Vinciguerra | f4fbfd6 | 2020-05-15 23:13:36 -0400 | [diff] [blame] | 346 | @param ring_size - size of ring buffer in shared memory |
Jakub Grajciar | 8419755 | 2017-11-16 14:02:49 +0100 | [diff] [blame] | 347 | @param flags - ring flags |
| 348 | @param head - ring head pointer |
| 349 | @param tail - ring tail pointer |
Paul Vinciguerra | f4fbfd6 | 2020-05-15 23:13:36 -0400 | [diff] [blame] | 350 | @param buffer_size - buffer size on shared memory |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 351 | */ |
| 352 | typedef struct |
| 353 | { |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 354 | uint8_t region; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 355 | uint8_t qid; |
| 356 | uint32_t ring_size; |
Jakub Grajciar | 8419755 | 2017-11-16 14:02:49 +0100 | [diff] [blame] | 357 | /** 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 Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 362 | uint16_t buffer_size; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 363 | } memif_queue_details_t; |
| 364 | |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 365 | /** \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 | */ |
| 372 | typedef 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 Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 381 | /** \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 Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 390 | @param socket_filename - socket filename |
| 391 | @param regions_num - number of regions |
| 392 | @param regions - struct containing region details |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 393 | @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 Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 397 | @param error - error string |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 398 | @param link_up_down - 1 = up (connected), 2 = down (disconnected) |
| 399 | */ |
| 400 | typedef 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 Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 412 | uint8_t regions_num; |
| 413 | memif_region_details_t *regions; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 414 | 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 Grajciar | 568cc46 | 2018-09-05 12:11:35 +0200 | [diff] [blame] | 419 | uint8_t *error; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 420 | uint8_t link_up_down; /* 1 = up, 0 = down */ |
| 421 | } memif_details_t; |
| 422 | /** @} */ |
| 423 | |
| 424 | /** |
| 425 | * @defgroup API_CALLS Api calls |
Dave Wallace | 6cd396c | 2018-01-23 17:47:02 -0500 | [diff] [blame] | 426 | * @ingroup libmemif |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 427 | * |
| 428 | * @{ |
| 429 | */ |
| 430 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 431 | /** \brief Memif get version |
| 432 | |
| 433 | \return ((MEMIF_VERSION_MAJOR << 8) | MEMIF_VERSION_MINOR) |
| 434 | */ |
| 435 | uint16_t memif_get_version (); |
| 436 | |
Paul Vinciguerra | f4fbfd6 | 2020-05-15 23:13:36 -0400 | [diff] [blame] | 437 | /** \brief Memif get queue event file descriptor |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 438 | @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 | |
| 445 | int 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 | */ |
| 454 | int 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 Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 461 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 462 | \return Error string |
| 463 | */ |
| 464 | char *memif_strerror (int err_code); |
| 465 | |
| 466 | /** \brief Memif get details |
Paul Vinciguerra | f4fbfd6 | 2020-05-15 23:13:36 -0400 | [diff] [blame] | 467 | @param conn - memif connection handle |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 468 | @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 | */ |
| 474 | int 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 Grajciar | 1941871 | 2018-03-13 13:57:50 +0100 | [diff] [blame] | 479 | @param app_name - application name (will be truncated to 32 chars) |
Paul Vinciguerra | f4fbfd6 | 2020-05-15 23:13:36 -0400 | [diff] [blame] | 480 | @param memif_alloc - custom memory allocator, NULL = default |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 481 | @param memif_realloc - custom memory reallocation, NULL = default |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 482 | @param memif_free - custom memory free, NULL = default |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 483 | |
| 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 | */ |
| 496 | int memif_init (memif_control_fd_update_t * on_control_fd_update, |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 497 | char *app_name, memif_alloc_t * memif_alloc, |
Jakub Grajciar | 93a5dd1 | 2018-08-20 14:26:32 +0200 | [diff] [blame] | 498 | memif_realloc_t * memif_realloc, memif_free_t * memif_free); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 499 | |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 500 | /** \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 Vinciguerra | f4fbfd6 | 2020-05-15 23:13:36 -0400 | [diff] [blame] | 505 | @param memif_alloc - custom memory allocator, NULL = default |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 506 | @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 | */ |
| 515 | int 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 Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 522 | /** \brief Memif cleanup |
| 523 | |
| 524 | Free libmemif internal allocations. |
| 525 | |
| 526 | \return 0 |
| 527 | */ |
| 528 | int memif_cleanup (); |
| 529 | |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 530 | /** \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 | */ |
| 537 | int memif_per_thread_cleanup (memif_per_thread_main_handle_t * pt_main); |
| 538 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 539 | /** \brief Memory interface create function |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 540 | @param conn - connection handle for client app |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 541 | @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 Vinciguerra | f4fbfd6 | 2020-05-15 23:13:36 -0400 | [diff] [blame] | 545 | @param private_ctx - private context passed back to user with callback |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 546 | |
| 547 | Creates memory interface. |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 548 | |
| 549 | SLAVE-MODE - |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 550 | 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 Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 554 | MASTER-MODE - |
Paul Vinciguerra | f4fbfd6 | 2020-05-15 23:13:36 -0400 | [diff] [blame] | 555 | Create listener socket and pass fd to user with memif_control_fd_update_t. |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 556 | 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 | */ |
| 562 | int 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 Vinciguerra | f4fbfd6 | 2020-05-15 23:13:36 -0400 | [diff] [blame] | 568 | @param fd - file descriptor on which the event occurred |
| 569 | @param events - event type(s) that occurred |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 570 | |
Paul Vinciguerra | f4fbfd6 | 2020-05-15 23:13:36 -0400 | [diff] [blame] | 571 | 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 Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 573 | |
| 574 | FD-TYPE - |
| 575 | TIMERFD - |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 576 | Every disconnected memif in slave mode will request connection. |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 577 | LISTENER or CONTROL - |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 578 | Handle socket messaging (internal connection establishment). |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 579 | INTERRUPT - |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 580 | Call on_interrupt callback (if set). |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 581 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 582 | \return memif_err_t |
| 583 | |
| 584 | */ |
| 585 | int memif_control_fd_handler (int fd, uint8_t events); |
| 586 | |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 587 | /** \brief Memif per thread control file descriptor handler |
| 588 | @param pt_main - per thread main handle |
Paul Vinciguerra | f4fbfd6 | 2020-05-15 23:13:36 -0400 | [diff] [blame] | 589 | @param fd - file descriptor on which the event occurred |
| 590 | @param events - event type(s) that occurred |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 591 | |
| 592 | Per thread version of memif_control_fd_handler. |
| 593 | |
| 594 | \return memif_err_t |
| 595 | |
| 596 | */ |
| 597 | int memif_per_thread_control_fd_handler (memif_per_thread_main_handle_t |
| 598 | pt_main, int fd, uint8_t events); |
| 599 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 600 | /** \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 | */ |
| 609 | int memif_delete (memif_conn_handle_t * conn); |
| 610 | |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 611 | /** \brief Memif buffer enq tx |
Paul Vinciguerra | f4fbfd6 | 2020-05-15 23:13:36 -0400 | [diff] [blame] | 612 | @param conn - memif connection handle |
| 613 | @param qid - number identifying queue |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 614 | @param bufs - memif buffers |
Paul Vinciguerra | f4fbfd6 | 2020-05-15 23:13:36 -0400 | [diff] [blame] | 615 | @param count - number of memif buffers to enqueue |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 616 | @param count_out - returns number of allocated buffers |
| 617 | |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 618 | Enqueue buffers to specified tx queue. Can only be used by slave. |
| 619 | Updates desc_index field for each memif buffer. |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 620 | If connection handle points to master returns MEMIF_ERR_INVAL_ARG. |
| 621 | |
| 622 | \return memif_err_t |
| 623 | */ |
| 624 | int 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 Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 628 | /** \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 | */ |
| 635 | int memif_buffer_requeue (memif_conn_handle_t conn, memif_buffer_t *buf_a, |
| 636 | memif_buffer_t *buf_b); |
| 637 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 638 | /** \brief Memif buffer alloc |
Paul Vinciguerra | f4fbfd6 | 2020-05-15 23:13:36 -0400 | [diff] [blame] | 639 | @param conn - memif connection handle |
| 640 | @param qid - number identifying queue |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 641 | @param bufs - memif buffers |
| 642 | @param count - number of memif buffers to allocate |
| 643 | @param count_out - returns number of allocated buffers |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 644 | @param size - buffer size, may return chained buffers if size > buffer_size |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 645 | |
| 646 | \return memif_err_t |
| 647 | */ |
| 648 | int memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, |
| 649 | memif_buffer_t * bufs, uint16_t count, |
Jakub Grajciar | b467b2a | 2017-09-14 14:12:10 +0200 | [diff] [blame] | 650 | uint16_t * count_out, uint16_t size); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 651 | |
Jakub Grajciar | 47e68de | 2021-01-08 15:32:43 +0100 | [diff] [blame] | 652 | /** \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 | */ |
| 660 | int memif_set_next_free_buffer (memif_conn_handle_t conn, uint16_t qid, |
| 661 | memif_buffer_t *buf); |
| 662 | |
Jakub Grajciar | f35fef2 | 2021-01-08 15:01:13 +0100 | [diff] [blame] | 663 | /** \brief Memif refill queue |
Paul Vinciguerra | f4fbfd6 | 2020-05-15 23:13:36 -0400 | [diff] [blame] | 664 | @param conn - memif connection handle |
| 665 | @param qid - number identifying queue |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 666 | @param count - number of buffers to be placed on ring |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 667 | @param headroom - offset the buffer by headroom |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 668 | |
| 669 | \return memif_err_t |
| 670 | */ |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 671 | int memif_refill_queue (memif_conn_handle_t conn, uint16_t qid, |
Jakub Grajciar | 3744fc7 | 2018-03-29 13:15:10 +0200 | [diff] [blame] | 672 | uint16_t count, uint16_t headroom); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 673 | |
| 674 | /** \brief Memif transmit buffer burst |
Paul Vinciguerra | f4fbfd6 | 2020-05-15 23:13:36 -0400 | [diff] [blame] | 675 | @param conn - memif connection handle |
| 676 | @param qid - number identifying queue |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 677 | @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 | */ |
| 683 | int 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 Vinciguerra | f4fbfd6 | 2020-05-15 23:13:36 -0400 | [diff] [blame] | 687 | @param conn - memif connection handle |
| 688 | @param qid - number identifying queue |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 689 | @param bufs - memif buffers |
| 690 | @param count - number of memif buffers to receive |
| 691 | @param rx - returns number of received buffers |
| 692 | |
Jan Cavojsky | 77a95cd | 2020-03-03 16:25:58 +0100 | [diff] [blame] | 693 | Consume interrupt event for receive queue. |
| 694 | The event is not consumed, if memif_rx_burst fails. |
| 695 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 696 | \return memif_err_t |
| 697 | */ |
| 698 | int 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 Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 704 | Passive event polling - |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 705 | 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 | */ |
| 710 | int memif_poll_event (int timeout); |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 711 | |
Paul Vinciguerra | 6223766 | 2020-05-29 23:03:06 -0400 | [diff] [blame] | 712 | /** \brief Memif per thread poll event |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 713 | @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 | */ |
| 720 | int memif_per_thread_poll_event (memif_per_thread_main_handle_t pt_main, |
| 721 | int timeout); |
| 722 | |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 723 | /** \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 Vinciguerra | f4fbfd6 | 2020-05-15 23:13:36 -0400 | [diff] [blame] | 726 | memif_poll_event() may still return simply because an event has occurred |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 727 | or the timeout has elapsed, but if called repeatedly in an infinite loop, |
Paul Vinciguerra | f4fbfd6 | 2020-05-15 23:13:36 -0400 | [diff] [blame] | 728 | a canceled memif_poll_event() is guaranteed to return MEMIF_ERR_POLL_CANCEL |
Milan Lenco | 0a47c99 | 2017-10-12 14:19:31 +0200 | [diff] [blame] | 729 | 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 |
| 736 | int memif_cancel_poll_event (); |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 737 | /** \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 | */ |
| 744 | int memif_per_thread_cancel_poll_event (memif_per_thread_main_handle_t |
| 745 | pt_main); |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 746 | |
| 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 Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 755 | int memif_set_connection_request_timer (struct itimerspec timer); |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 756 | |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 757 | /** \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 | */ |
| 765 | int |
| 766 | memif_per_thread_set_connection_request_timer (memif_per_thread_main_handle_t |
| 767 | pt_main, |
| 768 | struct itimerspec timer); |
| 769 | |
Jakub Grajciar | 84b8377 | 2019-03-04 12:42:19 +0100 | [diff] [blame] | 770 | /** \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 Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 777 | int 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 Vinciguerra | f4fbfd6 | 2020-05-15 23:13:36 -0400 | [diff] [blame] | 786 | requires socket of its respective type. Default socket is created if no |
Jakub Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 787 | 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 Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 793 | int 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 Vinciguerra | f4fbfd6 | 2020-05-15 23:13:36 -0400 | [diff] [blame] | 802 | Per thread version of memif_create_socket. |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 803 | |
| 804 | \return memif_err_t |
| 805 | */ |
| 806 | int 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 Grajciar | 12df497 | 2019-07-01 14:24:48 +0200 | [diff] [blame] | 809 | |
| 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 | */ |
| 818 | int memif_delete_socket (memif_socket_handle_t * sock); |
| 819 | |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 820 | /** \brief Get socket filename |
| 821 | @param sock - socket handle for client app |
| 822 | |
| 823 | Return constant pointer to socket filename. |
| 824 | |
Paul Vinciguerra | f4fbfd6 | 2020-05-15 23:13:36 -0400 | [diff] [blame] | 825 | \return const char * |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 826 | */ |
| 827 | const char *memif_get_socket_filename (memif_socket_handle_t sock); |
| 828 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 829 | /** @} */ |
| 830 | |
| 831 | #endif /* _LIBMEMIF_H_ */ |