Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1 | /* |
Florin Coras | c5df8c7 | 2019-04-08 07:42:30 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016-2019 Cisco and/or its affiliates. |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 3 | * Copyright (c) 2019 Arm Limited |
| 4 | * Copyright (c) 2010-2017 Intel Corporation and/or its affiliates. |
| 5 | * Copyright (c) 2007-2009 Kip Macy kmacy@freebsd.org |
| 6 | * Inspired from DPDK rte_ring.h (SPSC only) (derived from freebsd bufring.h). |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at: |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | */ |
| 19 | #ifndef __included_ssvm_fifo_h__ |
| 20 | #define __included_ssvm_fifo_h__ |
| 21 | |
| 22 | #include <vppinfra/clib.h> |
| 23 | #include <vppinfra/vec.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 24 | #include <vppinfra/pool.h> |
| 25 | #include <vppinfra/format.h> |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 26 | #include <svm/fifo_types.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 27 | |
Florin Coras | 326b81e | 2018-10-04 19:03:05 -0700 | [diff] [blame] | 28 | #define OOO_SEGMENT_INVALID_INDEX ((u32)~0) |
| 29 | #define SVM_FIFO_INVALID_SESSION_INDEX ((u32)~0) |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 30 | #define SVM_FIFO_INVALID_INDEX ((u32)~0) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 31 | |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 32 | typedef enum svm_fifo_deq_ntf_ |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 33 | { |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 34 | SVM_FIFO_NO_DEQ_NOTIF = 0, /**< No notification requested */ |
| 35 | SVM_FIFO_WANT_DEQ_NOTIF = 1, /**< Notify on dequeue */ |
| 36 | SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL = 2, /**< Notify on transition from full */ |
| 37 | SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY = 4, /**< Notify on transition to empty */ |
| 38 | } svm_fifo_deq_ntf_t; |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 39 | |
Florin Coras | 2309e7a | 2019-04-18 18:58:10 -0700 | [diff] [blame] | 40 | typedef enum svm_fifo_flag_ |
| 41 | { |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 42 | SVM_FIFO_F_LL_TRACKED = 1 << 0, |
Florin Coras | 2309e7a | 2019-04-18 18:58:10 -0700 | [diff] [blame] | 43 | } svm_fifo_flag_t; |
| 44 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 45 | typedef enum |
| 46 | { |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 47 | SVM_FIFO_EFULL = -2, |
| 48 | SVM_FIFO_EEMPTY = -3, |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 49 | SVM_FIFO_EGROW = -4, |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 50 | } svm_fifo_err_t; |
| 51 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 52 | typedef struct svm_fifo_seg_ |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 53 | { |
| 54 | u8 *data; |
| 55 | u32 len; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 56 | } svm_fifo_seg_t; |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 57 | |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 58 | #if SVM_FIFO_TRACE |
| 59 | #define svm_fifo_trace_add(_f, _s, _l, _t) \ |
| 60 | { \ |
| 61 | svm_fifo_trace_elem_t *trace_elt; \ |
| 62 | vec_add2(_f->trace, trace_elt, 1); \ |
| 63 | trace_elt->offset = _s; \ |
| 64 | trace_elt->len = _l; \ |
| 65 | trace_elt->action = _t; \ |
| 66 | } |
| 67 | #else |
| 68 | #define svm_fifo_trace_add(_f, _s, _l, _t) |
| 69 | #endif |
| 70 | |
| 71 | u8 *svm_fifo_dump_trace (u8 * s, svm_fifo_t * f); |
| 72 | u8 *svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose); |
| 73 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 74 | /** |
| 75 | * Load head and tail optimized for consumer |
| 76 | * |
| 77 | * Internal function. |
| 78 | */ |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 79 | static inline void |
| 80 | f_load_head_tail_cons (svm_fifo_t * f, u32 * head, u32 * tail) |
| 81 | { |
| 82 | /* load-relaxed: consumer owned index */ |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 83 | *head = f->shr->head; |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 84 | /* load-acq: consumer foreign index (paired with store-rel in producer) */ |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 85 | *tail = clib_atomic_load_acq_n (&f->shr->tail); |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 86 | } |
| 87 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 88 | /** Load head and tail optimized for producer |
| 89 | * |
| 90 | * Internal function |
| 91 | */ |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 92 | static inline void |
| 93 | f_load_head_tail_prod (svm_fifo_t * f, u32 * head, u32 * tail) |
| 94 | { |
| 95 | /* load relaxed: producer owned index */ |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 96 | *tail = f->shr->tail; |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 97 | /* load-acq: producer foreign index (paired with store-rel in consumer) */ |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 98 | *head = clib_atomic_load_acq_n (&f->shr->head); |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 99 | } |
| 100 | |
Florin Coras | a7570b0 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 101 | /** |
| 102 | * Load head and tail independent of producer/consumer role |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 103 | * |
| 104 | * Internal function. |
| 105 | */ |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 106 | static inline void |
| 107 | f_load_head_tail_all_acq (svm_fifo_t * f, u32 * head, u32 * tail) |
| 108 | { |
| 109 | /* load-acq : consumer foreign index (paired with store-rel) */ |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 110 | *tail = clib_atomic_load_acq_n (&f->shr->tail); |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 111 | /* load-acq : producer foriegn index (paired with store-rel) */ |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 112 | *head = clib_atomic_load_acq_n (&f->shr->head); |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 113 | } |
| 114 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 115 | /** |
Florin Coras | 29a59c3 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 116 | * Fifo current size, i.e., number of bytes enqueued |
| 117 | * |
| 118 | * Internal function. |
| 119 | */ |
| 120 | static inline u32 |
| 121 | f_cursize (svm_fifo_t * f, u32 head, u32 tail) |
| 122 | { |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 123 | return tail - head; |
Florin Coras | 29a59c3 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Fifo free bytes, i.e., number of free bytes |
| 128 | * |
| 129 | * Internal function |
| 130 | */ |
| 131 | static inline u32 |
| 132 | f_free_count (svm_fifo_t * f, u32 head, u32 tail) |
| 133 | { |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 134 | return (f->shr->size - f_cursize (f, head, tail)); |
Florin Coras | 29a59c3 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 137 | always_inline u32 |
| 138 | f_chunk_end (svm_fifo_chunk_t * c) |
| 139 | { |
| 140 | return c->start_byte + c->length; |
| 141 | } |
| 142 | |
| 143 | always_inline int |
| 144 | f_pos_lt (u32 a, u32 b) |
| 145 | { |
| 146 | return ((i32) (a - b) < 0); |
| 147 | } |
| 148 | |
| 149 | always_inline int |
| 150 | f_pos_leq (u32 a, u32 b) |
| 151 | { |
| 152 | return ((i32) (a - b) <= 0); |
| 153 | } |
| 154 | |
| 155 | always_inline int |
| 156 | f_pos_gt (u32 a, u32 b) |
| 157 | { |
| 158 | return ((i32) (a - b) > 0); |
| 159 | } |
| 160 | |
| 161 | always_inline int |
| 162 | f_pos_geq (u32 a, u32 b) |
| 163 | { |
| 164 | return ((i32) (a - b) >= 0); |
| 165 | } |
| 166 | |
| 167 | always_inline u8 |
| 168 | f_chunk_includes_pos (svm_fifo_chunk_t * c, u32 pos) |
| 169 | { |
| 170 | return (f_pos_geq (pos, c->start_byte) |
| 171 | && f_pos_lt (pos, c->start_byte + c->length)); |
| 172 | } |
Florin Coras | a7570b0 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 173 | |
Florin Coras | af58882 | 2020-12-09 12:51:13 -0800 | [diff] [blame] | 174 | always_inline svm_fifo_chunk_t * |
| 175 | f_start_cptr (svm_fifo_t *f) |
| 176 | { |
| 177 | return fs_chunk_ptr (f->fs_hdr, f->shr->start_chunk); |
| 178 | } |
| 179 | |
| 180 | always_inline svm_fifo_chunk_t * |
| 181 | f_end_cptr (svm_fifo_t *f) |
| 182 | { |
| 183 | return fs_chunk_ptr (f->fs_hdr, f->shr->end_chunk); |
| 184 | } |
| 185 | |
| 186 | always_inline svm_fifo_chunk_t * |
| 187 | f_head_cptr (svm_fifo_t *f) |
| 188 | { |
| 189 | return fs_chunk_ptr (f->fs_hdr, f->shr->head_chunk); |
| 190 | } |
| 191 | |
| 192 | always_inline svm_fifo_chunk_t * |
| 193 | f_tail_cptr (svm_fifo_t *f) |
| 194 | { |
| 195 | return fs_chunk_ptr (f->fs_hdr, f->shr->tail_chunk); |
| 196 | } |
| 197 | |
| 198 | always_inline svm_fifo_chunk_t * |
Florin Coras | 17672aa | 2020-12-29 16:55:32 -0800 | [diff] [blame] | 199 | f_cptr (svm_fifo_t *f, fs_sptr_t cp) |
Florin Coras | af58882 | 2020-12-09 12:51:13 -0800 | [diff] [blame] | 200 | { |
| 201 | return fs_chunk_ptr (f->fs_hdr, cp); |
| 202 | } |
| 203 | |
Florin Coras | 17672aa | 2020-12-29 16:55:32 -0800 | [diff] [blame] | 204 | always_inline fs_sptr_t |
Florin Coras | af58882 | 2020-12-09 12:51:13 -0800 | [diff] [blame] | 205 | f_csptr (svm_fifo_t *f, svm_fifo_chunk_t *c) |
| 206 | { |
| 207 | return fs_chunk_sptr (f->fs_hdr, c); |
| 208 | } |
| 209 | |
| 210 | always_inline void |
Florin Coras | 17672aa | 2020-12-29 16:55:32 -0800 | [diff] [blame] | 211 | f_csptr_link (svm_fifo_t *f, fs_sptr_t cp, svm_fifo_chunk_t *c) |
Florin Coras | af58882 | 2020-12-09 12:51:13 -0800 | [diff] [blame] | 212 | { |
| 213 | fs_chunk_ptr (f->fs_hdr, cp)->next = fs_chunk_sptr (f->fs_hdr, c); |
| 214 | } |
| 215 | |
Florin Coras | a7570b0 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 216 | /** |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 217 | * Create fifo of requested size |
Florin Coras | 41c9e04 | 2018-09-11 00:10:41 -0700 | [diff] [blame] | 218 | * |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 219 | * Allocates fifo on current heap. |
| 220 | * |
| 221 | * @param size data size in bytes for fifo to be allocated. Will be |
| 222 | * rounded to the next highest power-of-two value. |
| 223 | * @return pointer to new fifo |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 224 | */ |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 225 | svm_fifo_t *svm_fifo_alloc (u32 size); |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 226 | /** |
| 227 | * Initialize fifo |
| 228 | * |
Florin Coras | eaacce4 | 2019-07-02 13:07:37 -0700 | [diff] [blame] | 229 | * @param f fifo |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 230 | * @param size size for fifo |
| 231 | */ |
Florin Coras | 0a84680 | 2019-04-09 18:29:14 -0700 | [diff] [blame] | 232 | void svm_fifo_init (svm_fifo_t * f, u32 size); |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 233 | /** |
| 234 | * Allocate a fifo chunk on heap |
| 235 | * |
| 236 | * If the chunk is allocated on a fifo segment, this should be called |
| 237 | * with the segment's heap pushed. |
| 238 | * |
| 239 | * @param size chunk size in bytes. Will be rounded to the next highest |
| 240 | * power-of-two |
| 241 | * @return new chunk or 0 if alloc failed |
| 242 | */ |
| 243 | svm_fifo_chunk_t *svm_fifo_chunk_alloc (u32 size); |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 244 | /** |
Florin Coras | 9e61d9a | 2020-02-05 21:13:18 +0000 | [diff] [blame] | 245 | * Ensure the whole fifo size is writeable |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 246 | * |
Florin Coras | 9e61d9a | 2020-02-05 21:13:18 +0000 | [diff] [blame] | 247 | * Allocates enough chunks to cover the whole fifo size. |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 248 | * |
Florin Coras | 9e61d9a | 2020-02-05 21:13:18 +0000 | [diff] [blame] | 249 | * @param f fifo |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 250 | */ |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 251 | int svm_fifo_fill_chunk_list (svm_fifo_t * f); |
Florin Coras | 9e61d9a | 2020-02-05 21:13:18 +0000 | [diff] [blame] | 252 | /** |
Florin Coras | 40a5da8 | 2020-12-18 09:19:18 -0800 | [diff] [blame] | 253 | * Provision and return chunks for number of bytes requested |
| 254 | * |
| 255 | * Allocates enough chunks to cover the bytes requested and returns them |
| 256 | * in the fifo segment array. The number of bytes provisioned may be less |
| 257 | * than requested if not enough segments were provided. |
| 258 | * |
| 259 | * @param f fifo |
| 260 | * @param fs array of fifo segments |
| 261 | * @param n_segs length of fifo segments array |
| 262 | * @param len number of bytes to preallocate |
| 263 | * @return number of fifo segments provisioned or error |
| 264 | */ |
| 265 | int svm_fifo_provision_chunks (svm_fifo_t *f, svm_fifo_seg_t *fs, u32 n_segs, |
| 266 | u32 len); |
| 267 | /** |
Florin Coras | 9e61d9a | 2020-02-05 21:13:18 +0000 | [diff] [blame] | 268 | * Initialize rbtrees used for ooo lookups |
| 269 | * |
| 270 | * @param f fifo |
| 271 | * @param ooo_type type of ooo operation (0 enqueue, 1 dequeue) |
| 272 | */ |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 273 | void svm_fifo_init_ooo_lookup (svm_fifo_t * f, u8 ooo_type); |
Florin Coras | a7570b0 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 274 | /** |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 275 | * Free fifo and associated state |
| 276 | * |
| 277 | * @param f fifo |
| 278 | */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 279 | void svm_fifo_free (svm_fifo_t * f); |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 280 | /** |
| 281 | * Cleanup fifo chunk lookup rb tree |
| 282 | * |
| 283 | * The rb tree is allocated in segment heap so this should be called |
| 284 | * with it pushed. |
| 285 | * |
| 286 | * @param f fifo to cleanup |
| 287 | */ |
| 288 | void svm_fifo_free_chunk_lookup (svm_fifo_t * f); |
| 289 | /** |
| 290 | * Cleanup fifo ooo data |
| 291 | * |
| 292 | * The ooo data is allocated in producer process memory. The fifo |
| 293 | * segment heap should not be pushed. |
| 294 | * |
| 295 | * @param f fifo to cleanup |
| 296 | */ |
| 297 | void svm_fifo_free_ooo_data (svm_fifo_t * f); |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 298 | /** |
| 299 | * Init fifo head and tail |
| 300 | * |
| 301 | * @param f fifo |
| 302 | * @param head head value that will be matched to a chunk |
| 303 | * @param tail tail value that will be matched to a chunk |
| 304 | */ |
| 305 | void svm_fifo_init_pointers (svm_fifo_t * f, u32 head, u32 tail); |
| 306 | /** |
| 307 | * Clone fifo |
| 308 | * |
| 309 | * Clones single/default chunk fifo. It does not work for fifos with |
| 310 | * multiple chunks. |
| 311 | */ |
| 312 | void svm_fifo_clone (svm_fifo_t * df, svm_fifo_t * sf); |
| 313 | /** |
| 314 | * Enqueue data to fifo |
| 315 | * |
| 316 | * Data is enqueued and tail pointer is updated atomically. If the new data |
| 317 | * enqueued partly overlaps or "touches" an out-of-order segment, said segment |
| 318 | * is "consumed" and the number of bytes returned is appropriately updated. |
| 319 | * |
| 320 | * @param f fifo |
| 321 | * @param len length of data to copy |
| 322 | * @param src buffer from where to copy the data |
| 323 | * @return number of contiguous bytes that can be consumed or error |
| 324 | */ |
| 325 | int svm_fifo_enqueue (svm_fifo_t * f, u32 len, const u8 * src); |
| 326 | /** |
| 327 | * Enqueue data to fifo with offset |
| 328 | * |
| 329 | * Data is enqueued without updating tail pointer. Instead, an out-of-order |
| 330 | * list of segments is generated and maintained. Fifo takes care of coalescing |
| 331 | * contiguous or overlapping segments. |
| 332 | * |
| 333 | * @param f fifo |
| 334 | * @param offset offset at which to copy the data |
| 335 | * @param len len of data to copy |
| 336 | * @param src buffer from where to copy the data |
| 337 | * @return 0 if enqueue was successful, error otherwise |
| 338 | */ |
| 339 | int svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 len, |
| 340 | u8 * src); |
Florin Coras | a7570b0 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 341 | |
| 342 | /** |
| 343 | * Advance tail pointer |
| 344 | * |
| 345 | * Useful for moving tail pointer after external enqueue. |
| 346 | * |
| 347 | * @param f fifo |
| 348 | * @param len number of bytes to add to tail |
| 349 | */ |
| 350 | void svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 len); |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 351 | /** |
Florin Coras | c95cfa2 | 2020-11-24 08:41:17 -0800 | [diff] [blame] | 352 | * Enqueue array of @ref svm_fifo_seg_t in order |
| 353 | * |
| 354 | * @param f fifo |
| 355 | * @param segs array of segments to enqueue |
| 356 | * @param n_segs number of segments |
| 357 | * @param allow_partial if set partial enqueues are allowed |
| 358 | * @return len if enqueue was successful, error otherwise |
| 359 | */ |
| 360 | int svm_fifo_enqueue_segments (svm_fifo_t * f, const svm_fifo_seg_t segs[], |
| 361 | u32 n_segs, u8 allow_partial); |
| 362 | /** |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 363 | * Overwrite fifo head with new data |
| 364 | * |
| 365 | * This should be typically used by dgram transport protocols that need |
Florin Coras | 97d39e3 | 2020-09-04 08:57:27 -0700 | [diff] [blame] | 366 | * to update the dgram header after dequeuing a chunk of data. It assumes |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 367 | * that the dgram header is at most spread over two chunks. |
| 368 | * |
| 369 | * @param f fifo |
| 370 | * @param src src of new data |
| 371 | * @param len length of new data |
| 372 | */ |
| 373 | void svm_fifo_overwrite_head (svm_fifo_t * f, u8 * src, u32 len); |
| 374 | /** |
| 375 | * Dequeue data from fifo |
| 376 | * |
| 377 | * Data is dequeued to consumer provided buffer and head is atomically |
Florin Coras | 97d39e3 | 2020-09-04 08:57:27 -0700 | [diff] [blame] | 378 | * updated. This should not be used in combination with ooo lookups. If |
| 379 | * ooo peeking of data is needed in combination with dequeuing use @ref |
| 380 | * svm_fifo_dequeue_drop. |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 381 | * |
| 382 | * @param f fifo |
| 383 | * @param len length of data to dequeue |
| 384 | * @param dst buffer to where to dequeue the data |
| 385 | * @return number of bytes dequeued or error |
| 386 | */ |
| 387 | int svm_fifo_dequeue (svm_fifo_t * f, u32 len, u8 * dst); |
| 388 | /** |
| 389 | * Peek data from fifo |
| 390 | * |
| 391 | * Data is copied from requested offset into provided dst buffer. Head is |
| 392 | * not updated. |
| 393 | * |
| 394 | * @param f fifo |
| 395 | * @param offset offset from which to copy the data |
| 396 | * @param len length of data to copy |
| 397 | * @param dst buffer to where to dequeue the data |
| 398 | * @return number of bytes peeked |
| 399 | */ |
| 400 | int svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 len, u8 * dst); |
| 401 | /** |
| 402 | * Dequeue and drop bytes from fifo |
| 403 | * |
| 404 | * Advances fifo head by requested amount of bytes. |
| 405 | * |
| 406 | * @param f fifo |
| 407 | * @param len number of bytes to drop |
| 408 | * @return number of bytes dropped |
| 409 | */ |
| 410 | int svm_fifo_dequeue_drop (svm_fifo_t * f, u32 len); |
| 411 | /** |
| 412 | * Dequeue and drop all bytes from fifo |
| 413 | * |
| 414 | * Advances head to tail position. |
| 415 | * |
| 416 | * @param f fifo |
| 417 | */ |
Florin Coras | 25579b4 | 2018-06-06 17:55:02 -0700 | [diff] [blame] | 418 | void svm_fifo_dequeue_drop_all (svm_fifo_t * f); |
Florin Coras | d68faf8 | 2020-09-25 15:18:13 -0700 | [diff] [blame] | 419 | /** |
| 420 | * Get pointers to fifo chunks data in @ref svm_fifo_seg_t array |
| 421 | * |
| 422 | * Populates fifo segment array with pointers to fifo chunk data and lengths. |
| 423 | * Because this returns pointers to data, it must be paired with |
| 424 | * @ref svm_fifo_dequeue_drop to actually release the fifo chunks after the |
| 425 | * data is consumed. |
| 426 | * |
| 427 | * @param f fifo |
Florin Coras | d1cc38d | 2020-10-11 11:05:04 -0700 | [diff] [blame] | 428 | * @param offset offset from where to retrieve segments |
Florin Coras | d68faf8 | 2020-09-25 15:18:13 -0700 | [diff] [blame] | 429 | * @param fs array of fifo segments allocated by caller |
| 430 | * @param n_segs number of fifo segments in array |
| 431 | * @param max_bytes max bytes to be mapped to fifo segments |
| 432 | * @return number of bytes in fifo segments or SVM_FIFO_EEMPTY |
| 433 | */ |
Florin Coras | d1cc38d | 2020-10-11 11:05:04 -0700 | [diff] [blame] | 434 | int svm_fifo_segments (svm_fifo_t * f, u32 offset, svm_fifo_seg_t * fs, |
| 435 | u32 n_segs, u32 max_bytes); |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 436 | /** |
| 437 | * Add io events subscriber to list |
| 438 | * |
| 439 | * @param f fifo |
| 440 | * @param sub subscriber opaque index (typically app worker index) |
| 441 | */ |
| 442 | void svm_fifo_add_subscriber (svm_fifo_t * f, u8 sub); |
| 443 | /** |
| 444 | * Remove io events subscriber form list |
| 445 | * |
| 446 | * @param f fifo |
| 447 | * @param sub subscriber index to be removed |
| 448 | */ |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 449 | void svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber); |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 450 | /** |
| 451 | * Number of out-of-order segments for fifo |
| 452 | * |
| 453 | * @param f fifo |
| 454 | * @return number of out of order segments |
| 455 | */ |
| 456 | u32 svm_fifo_n_ooo_segments (svm_fifo_t * f); |
Florin Coras | eaacce4 | 2019-07-02 13:07:37 -0700 | [diff] [blame] | 457 | /** |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 458 | * First out-of-order segment for fifo |
| 459 | * |
| 460 | * @param f fifo |
| 461 | * @return first out-of-order segment for fifo |
| 462 | */ |
| 463 | ooo_segment_t *svm_fifo_first_ooo_segment (svm_fifo_t * f); |
Florin Coras | eaacce4 | 2019-07-02 13:07:37 -0700 | [diff] [blame] | 464 | /** |
| 465 | * Check if fifo is sane. Debug only. |
| 466 | * |
| 467 | * @param f fifo |
| 468 | * @return 1 if sane, 0 otherwise |
| 469 | */ |
| 470 | u8 svm_fifo_is_sane (svm_fifo_t * f); |
Florin Coras | 9e61d9a | 2020-02-05 21:13:18 +0000 | [diff] [blame] | 471 | /** |
| 472 | * Number of chunks linked into the fifo |
| 473 | * |
| 474 | * @param f fifo |
| 475 | * @return number of chunks in fifo linked list |
| 476 | */ |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 477 | u32 svm_fifo_n_chunks (svm_fifo_t * f); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 478 | format_function_t format_svm_fifo; |
| 479 | |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 480 | /** |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 481 | * Fifo max bytes to dequeue optimized for consumer |
| 482 | * |
| 483 | * @param f fifo |
| 484 | * @return max number of bytes that can be dequeued |
| 485 | */ |
| 486 | static inline u32 |
| 487 | svm_fifo_max_dequeue_cons (svm_fifo_t * f) |
| 488 | { |
| 489 | u32 tail, head; |
| 490 | f_load_head_tail_cons (f, &head, &tail); |
| 491 | return f_cursize (f, head, tail); |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * Fifo max bytes to dequeue optimized for producer |
| 496 | * |
| 497 | * @param f fifo |
| 498 | * @return max number of bytes that can be dequeued |
| 499 | */ |
| 500 | static inline u32 |
| 501 | svm_fifo_max_dequeue_prod (svm_fifo_t * f) |
| 502 | { |
| 503 | u32 tail, head; |
| 504 | f_load_head_tail_prod (f, &head, &tail); |
| 505 | return f_cursize (f, head, tail); |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * Fifo max bytes to dequeue |
| 510 | * |
| 511 | * Note: use producer or consumer specific functions for performance: |
| 512 | * @ref svm_fifo_max_dequeue_cons (svm_fifo_t *f) |
| 513 | * @ref svm_fifo_max_dequeue_prod (svm_fifo_t *f) |
| 514 | */ |
| 515 | static inline u32 |
| 516 | svm_fifo_max_dequeue (svm_fifo_t * f) |
| 517 | { |
| 518 | u32 tail, head; |
| 519 | f_load_head_tail_all_acq (f, &head, &tail); |
| 520 | return f_cursize (f, head, tail); |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Check if fifo is full optimized for producer |
| 525 | * |
| 526 | * @param f fifo |
| 527 | * @return 1 if fifo is full 0 otherwise |
| 528 | */ |
| 529 | static inline int |
| 530 | svm_fifo_is_full_prod (svm_fifo_t * f) |
| 531 | { |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 532 | return (svm_fifo_max_dequeue_prod (f) == f->shr->size); |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | /* Check if fifo is full. |
| 536 | * |
| 537 | * Note: use producer or consumer specific functions for performance. |
| 538 | * @ref svm_fifo_is_full_prod (svm_fifo_t * f) |
| 539 | * add cons version if needed |
| 540 | */ |
| 541 | static inline int |
| 542 | svm_fifo_is_full (svm_fifo_t * f) |
| 543 | { |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 544 | return (svm_fifo_max_dequeue (f) == f->shr->size); |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | /** |
| 548 | * Check if fifo is empty optimized for consumer |
| 549 | * |
| 550 | * @param f fifo |
| 551 | * @return 1 if fifo is empty 0 otherwise |
| 552 | */ |
| 553 | static inline int |
| 554 | svm_fifo_is_empty_cons (svm_fifo_t * f) |
| 555 | { |
| 556 | return (svm_fifo_max_dequeue_cons (f) == 0); |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * Check if fifo is empty optimized for producer |
| 561 | * |
| 562 | * @param f fifo |
| 563 | * @return 1 if fifo is empty 0 otherwise |
| 564 | */ |
| 565 | static inline int |
| 566 | svm_fifo_is_empty_prod (svm_fifo_t * f) |
| 567 | { |
| 568 | return (svm_fifo_max_dequeue_prod (f) == 0); |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * Check if fifo is empty |
| 573 | * |
| 574 | * Note: use producer or consumer specific functions for perfomance. |
| 575 | * @ref svm_fifo_is_empty_cons (svm_fifo_t * f) |
| 576 | * @ref svm_fifo_is_empty_prod (svm_fifo_t * f) |
| 577 | */ |
| 578 | static inline int |
| 579 | svm_fifo_is_empty (svm_fifo_t * f) |
| 580 | { |
| 581 | return (svm_fifo_max_dequeue (f) == 0); |
| 582 | } |
| 583 | |
| 584 | /** |
| 585 | * Check if fifo is wrapped |
| 586 | * |
| 587 | * @param f fifo |
| 588 | * @return 1 if 'normalized' head is ahead of tail |
| 589 | */ |
| 590 | static inline u8 |
| 591 | svm_fifo_is_wrapped (svm_fifo_t * f) |
| 592 | { |
| 593 | u32 head, tail; |
| 594 | f_load_head_tail_all_acq (f, &head, &tail); |
Florin Coras | 29a59c3 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 595 | return head > tail; |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | /** |
| 599 | * Maximum number of bytes that can be enqueued into fifo |
| 600 | * |
| 601 | * Optimized for producer |
| 602 | * |
| 603 | * @param f fifo |
| 604 | * @return max number of bytes that can be enqueued into fifo |
| 605 | */ |
| 606 | static inline u32 |
| 607 | svm_fifo_max_enqueue_prod (svm_fifo_t * f) |
| 608 | { |
| 609 | u32 head, tail; |
| 610 | f_load_head_tail_prod (f, &head, &tail); |
| 611 | return f_free_count (f, head, tail); |
| 612 | } |
| 613 | |
| 614 | /* Maximum number of bytes that can be enqueued into fifo |
| 615 | * |
| 616 | * Note: use producer or consumer specific functions for performance. |
| 617 | * @ref svm_fifo_max_enqueue_prod (svm_fifo_t *f) |
| 618 | * add consumer specific version if needed. |
| 619 | */ |
| 620 | static inline u32 |
| 621 | svm_fifo_max_enqueue (svm_fifo_t * f) |
| 622 | { |
| 623 | u32 head, tail; |
| 624 | f_load_head_tail_all_acq (f, &head, &tail); |
| 625 | return f_free_count (f, head, tail); |
| 626 | } |
| 627 | |
| 628 | /** |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 629 | * Max contiguous chunk of data that can be read. |
| 630 | * |
| 631 | * Should only be called by consumers. |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 632 | */ |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 633 | u32 svm_fifo_max_read_chunk (svm_fifo_t * f); |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 634 | |
| 635 | /** |
| 636 | * Max contiguous chunk of data that can be written |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 637 | * |
| 638 | * Should only be called by producers |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 639 | */ |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 640 | u32 svm_fifo_max_write_chunk (svm_fifo_t * f); |
| 641 | |
Florin Coras | 9e61d9a | 2020-02-05 21:13:18 +0000 | [diff] [blame] | 642 | /** |
| 643 | * Fifo head chunk getter |
| 644 | * |
| 645 | * @param f fifo |
| 646 | * @return head chunk pointer |
| 647 | */ |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 648 | static inline svm_fifo_chunk_t * |
| 649 | svm_fifo_head_chunk (svm_fifo_t * f) |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 650 | { |
Florin Coras | af58882 | 2020-12-09 12:51:13 -0800 | [diff] [blame] | 651 | return f_head_cptr (f); |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 652 | } |
| 653 | |
Florin Coras | 9e61d9a | 2020-02-05 21:13:18 +0000 | [diff] [blame] | 654 | /** |
| 655 | * Fifo head pointer getter |
| 656 | * |
| 657 | * @param f fifo |
| 658 | * @return head pointer |
| 659 | */ |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 660 | static inline u8 * |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 661 | svm_fifo_head (svm_fifo_t * f) |
| 662 | { |
Florin Coras | af58882 | 2020-12-09 12:51:13 -0800 | [diff] [blame] | 663 | svm_fifo_chunk_t *head_chunk; |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 664 | if (!f->shr->head_chunk) |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 665 | return 0; |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 666 | /* load-relaxed: consumer owned index */ |
Florin Coras | af58882 | 2020-12-09 12:51:13 -0800 | [diff] [blame] | 667 | head_chunk = f_head_cptr (f); |
| 668 | return (head_chunk->data + (f->shr->head - head_chunk->start_byte)); |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 669 | } |
| 670 | |
Florin Coras | 9e61d9a | 2020-02-05 21:13:18 +0000 | [diff] [blame] | 671 | /** |
| 672 | * Fifo tail chunk getter |
| 673 | * |
| 674 | * @param f fifo |
| 675 | * @return tail chunk pointer |
| 676 | */ |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 677 | static inline svm_fifo_chunk_t * |
| 678 | svm_fifo_tail_chunk (svm_fifo_t * f) |
| 679 | { |
Florin Coras | af58882 | 2020-12-09 12:51:13 -0800 | [diff] [blame] | 680 | return f_tail_cptr (f); |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 681 | } |
| 682 | |
Florin Coras | 9e61d9a | 2020-02-05 21:13:18 +0000 | [diff] [blame] | 683 | /** |
| 684 | * Fifo tail pointer getter |
| 685 | * |
| 686 | * @param f fifo |
| 687 | * @return tail pointer |
| 688 | */ |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 689 | static inline u8 * |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 690 | svm_fifo_tail (svm_fifo_t * f) |
| 691 | { |
Florin Coras | af58882 | 2020-12-09 12:51:13 -0800 | [diff] [blame] | 692 | svm_fifo_chunk_t *tail_chunk; |
| 693 | |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 694 | /* load-relaxed: producer owned index */ |
Florin Coras | af58882 | 2020-12-09 12:51:13 -0800 | [diff] [blame] | 695 | tail_chunk = f_tail_cptr (f); |
| 696 | return (tail_chunk->data + (f->shr->tail - tail_chunk->start_byte)); |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 697 | } |
| 698 | |
Florin Coras | 9e61d9a | 2020-02-05 21:13:18 +0000 | [diff] [blame] | 699 | /** |
| 700 | * Fifo number of subscribers getter |
| 701 | * |
| 702 | * @param f fifo |
| 703 | * @return number of subscribers |
| 704 | */ |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 705 | static inline u8 |
| 706 | svm_fifo_n_subscribers (svm_fifo_t * f) |
| 707 | { |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 708 | return f->shr->n_subscribers; |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | /** |
| 712 | * Check if fifo has out-of-order data |
| 713 | * |
| 714 | * @param f fifo |
| 715 | * @return 1 if fifo has ooo data, 0 otherwise |
| 716 | */ |
| 717 | static inline u8 |
| 718 | svm_fifo_has_ooo_data (svm_fifo_t * f) |
| 719 | { |
| 720 | return f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX; |
| 721 | } |
| 722 | |
| 723 | static inline ooo_segment_t * |
| 724 | svm_fifo_newest_ooo_segment (svm_fifo_t * f) |
| 725 | { |
| 726 | if (f->ooos_newest == OOO_SEGMENT_INVALID_INDEX) |
| 727 | return 0; |
| 728 | return pool_elt_at_index (f->ooo_segments, f->ooos_newest); |
| 729 | } |
| 730 | |
| 731 | static inline void |
| 732 | svm_fifo_newest_ooo_segment_reset (svm_fifo_t * f) |
| 733 | { |
| 734 | f->ooos_newest = OOO_SEGMENT_INVALID_INDEX; |
| 735 | } |
| 736 | |
| 737 | static inline u32 |
| 738 | ooo_segment_offset_prod (svm_fifo_t * f, ooo_segment_t * s) |
| 739 | { |
| 740 | u32 tail; |
| 741 | /* load-relaxed: producer owned index */ |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 742 | tail = f->shr->tail; |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 743 | |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 744 | return (s->start - tail); |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | static inline u32 |
| 748 | ooo_segment_length (svm_fifo_t * f, ooo_segment_t * s) |
| 749 | { |
| 750 | return s->length; |
| 751 | } |
| 752 | |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 753 | static inline u32 |
| 754 | svm_fifo_size (svm_fifo_t * f) |
| 755 | { |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 756 | return f->shr->size; |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | static inline void |
| 760 | svm_fifo_set_size (svm_fifo_t * f, u32 size) |
| 761 | { |
Florin Coras | 4b8011e | 2020-12-07 19:38:23 -0800 | [diff] [blame] | 762 | if (size > (1 << f->fs_hdr->max_log2_fifo_size)) |
Florin Coras | 0e6199d | 2020-04-17 20:15:22 +0000 | [diff] [blame] | 763 | return; |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 764 | fsh_virtual_mem_update (f->fs_hdr, f->shr->slice_index, |
| 765 | (int) f->shr->size - size); |
| 766 | f->shr->size = size; |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 767 | } |
| 768 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 769 | /** |
| 770 | * Check if fifo has io event |
| 771 | * |
| 772 | * @param f fifo |
| 773 | * @return 1 if fifo has event, 0 otherwise |
| 774 | */ |
| 775 | static inline int |
| 776 | svm_fifo_has_event (svm_fifo_t * f) |
| 777 | { |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 778 | return f->shr->has_event; |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | /** |
| 782 | * Set fifo event flag. |
| 783 | * |
| 784 | * Forces release semantics. |
| 785 | * |
| 786 | * @param f fifo |
| 787 | * @return 1 if flag was not set, 0 otherwise |
| 788 | */ |
| 789 | always_inline u8 |
| 790 | svm_fifo_set_event (svm_fifo_t * f) |
| 791 | { |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 792 | return !clib_atomic_swap_rel_n (&f->shr->has_event, 1); |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | /** |
| 796 | * Unset fifo event flag. |
| 797 | * |
| 798 | * Forces acquire semantics |
| 799 | * |
| 800 | * @param f fifo |
| 801 | */ |
| 802 | always_inline void |
| 803 | svm_fifo_unset_event (svm_fifo_t * f) |
| 804 | { |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 805 | clib_atomic_swap_acq_n (&f->shr->has_event, 0); |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | /** |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 809 | * Set specific want notification flag |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 810 | * |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 811 | * For list of flags see @ref svm_fifo_deq_ntf_t |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 812 | * |
| 813 | * @param f fifo |
| 814 | * @param ntf_type type of notification requested |
| 815 | */ |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 816 | static inline void |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 817 | svm_fifo_add_want_deq_ntf (svm_fifo_t * f, u8 ntf_type) |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 818 | { |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 819 | f->shr->want_deq_ntf |= ntf_type; |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 820 | } |
| 821 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 822 | /** |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 823 | * Clear specific want notification flag |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 824 | * |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 825 | * For list of flags see @ref svm_fifo_ntf_t |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 826 | * |
| 827 | * @param f fifo |
| 828 | * @param ntf_type type of notification to be cleared |
| 829 | */ |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 830 | static inline void |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 831 | svm_fifo_del_want_deq_ntf (svm_fifo_t * f, u8 ntf_type) |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 832 | { |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 833 | f->shr->want_deq_ntf &= ~ntf_type; |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 834 | } |
| 835 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 836 | /** |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 837 | * Clear the want notification flag and set has notification |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 838 | * |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 839 | * Should be used after enqueuing an event. This clears the |
| 840 | * SVM_FIFO_WANT_NOTIF flag but it does not clear |
| 841 | * SVM_FIFO_WANT_NOTIF_IF_FULL. If the latter was set, has_ntf is |
| 842 | * set to avoid enqueueing events for for all dequeue operations until |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 843 | * it is manually cleared. |
| 844 | * |
| 845 | * @param f fifo |
| 846 | */ |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 847 | static inline void |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 848 | svm_fifo_clear_deq_ntf (svm_fifo_t * f) |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 849 | { |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 850 | /* Set the flag if want_notif_if_full was the only ntf requested */ |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 851 | f->shr->has_deq_ntf = |
| 852 | f->shr->want_deq_ntf == SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL; |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 853 | svm_fifo_del_want_deq_ntf (f, SVM_FIFO_WANT_DEQ_NOTIF); |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 854 | } |
| 855 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 856 | /** |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 857 | * Clear has notification flag |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 858 | * |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 859 | * The fifo generates only one event per SVM_FIFO_WANT_NOTIF_IF_FULL |
| 860 | * request and sets has_ntf. To received new events the flag must be |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 861 | * cleared using this function. |
| 862 | * |
| 863 | * @param f fifo |
| 864 | */ |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 865 | static inline void |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 866 | svm_fifo_reset_has_deq_ntf (svm_fifo_t * f) |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 867 | { |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 868 | f->shr->has_deq_ntf = 0; |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 869 | } |
| 870 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 871 | /** |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 872 | * Check if fifo needs dequeue notification |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 873 | * |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 874 | * Determines based on notification request flags and state of the fifo if |
| 875 | * an event should be generated. |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 876 | * |
| 877 | * @param f fifo |
| 878 | * @param n_last_deq number of bytes last dequeued |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 879 | * @return 1 if event should be generated, 0 otherwise |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 880 | */ |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 881 | static inline u8 |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 882 | svm_fifo_needs_deq_ntf (svm_fifo_t * f, u32 n_last_deq) |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 883 | { |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 884 | u8 want_ntf = f->shr->want_deq_ntf; |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 885 | |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 886 | if (PREDICT_TRUE (want_ntf == SVM_FIFO_NO_DEQ_NOTIF)) |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 887 | return 0; |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 888 | else if (want_ntf & SVM_FIFO_WANT_DEQ_NOTIF) |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 889 | return 1; |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 890 | if (want_ntf & SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL) |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 891 | { |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 892 | u32 max_deq = svm_fifo_max_dequeue_cons (f); |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 893 | u32 size = f->shr->size; |
| 894 | if (!f->shr->has_deq_ntf && max_deq < size && |
| 895 | max_deq + n_last_deq >= size) |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 896 | return 1; |
Nathan Skrzypczak | e82a7ad | 2019-05-22 18:41:50 +0200 | [diff] [blame] | 897 | } |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 898 | if (want_ntf & SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY) |
Nathan Skrzypczak | e82a7ad | 2019-05-22 18:41:50 +0200 | [diff] [blame] | 899 | { |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 900 | if (!f->shr->has_deq_ntf && svm_fifo_is_empty (f)) |
Nathan Skrzypczak | e82a7ad | 2019-05-22 18:41:50 +0200 | [diff] [blame] | 901 | return 1; |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 902 | } |
| 903 | return 0; |
| 904 | } |
| 905 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 906 | #endif /* __included_ssvm_fifo_h__ */ |
| 907 | |
| 908 | /* |
| 909 | * fd.io coding-style-patch-verification: ON |
| 910 | * |
| 911 | * Local Variables: |
| 912 | * eval: (c-set-style "gnu") |
| 913 | * End: |
| 914 | */ |