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