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