Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | #ifndef __included_ssvm_fifo_h__ |
| 16 | #define __included_ssvm_fifo_h__ |
| 17 | |
| 18 | #include <vppinfra/clib.h> |
| 19 | #include <vppinfra/vec.h> |
| 20 | #include <vppinfra/mheap.h> |
| 21 | #include <vppinfra/heap.h> |
| 22 | #include <vppinfra/pool.h> |
| 23 | #include <vppinfra/format.h> |
| 24 | #include <pthread.h> |
| 25 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 26 | /** Out-of-order segment */ |
| 27 | typedef struct |
| 28 | { |
| 29 | u32 next; /**< Next linked-list element pool index */ |
| 30 | u32 prev; /**< Previous linked-list element pool index */ |
| 31 | |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 32 | u32 start; /**< Start of segment, normalized*/ |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 33 | u32 length; /**< Length of segment */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 34 | } ooo_segment_t; |
| 35 | |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 36 | format_function_t format_ooo_segment; |
| 37 | format_function_t format_ooo_list; |
| 38 | |
Florin Coras | 326b81e | 2018-10-04 19:03:05 -0700 | [diff] [blame] | 39 | #define SVM_FIFO_TRACE (0) |
| 40 | #define OOO_SEGMENT_INVALID_INDEX ((u32)~0) |
| 41 | #define SVM_FIFO_INVALID_SESSION_INDEX ((u32)~0) |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 42 | #define SVM_FIFO_INVALID_INDEX ((u32)~0) |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 43 | #define SVM_FIFO_MAX_EVT_SUBSCRIBERS 8 |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 44 | |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 45 | enum |
| 46 | { |
| 47 | SVM_FIFO_NO_TX_NOTIF = 0, |
| 48 | SVM_FIFO_WANT_TX_NOTIF = 1, |
| 49 | SVM_FIFO_WANT_TX_NOTIF_IF_FULL = 2, |
| 50 | }; |
| 51 | |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 52 | typedef struct |
| 53 | { |
| 54 | u32 offset; |
| 55 | u32 len; |
| 56 | u32 action; |
| 57 | } svm_fifo_trace_elem_t; |
| 58 | |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 59 | typedef struct _svm_fifo |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 60 | { |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 61 | CLIB_CACHE_LINE_ALIGN_MARK (shared_first); |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 62 | volatile u32 cursize; /**< current fifo size */ |
| 63 | u32 nitems; |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 64 | |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 65 | CLIB_CACHE_LINE_ALIGN_MARK (shared_second); |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 66 | volatile u32 has_event; /**< non-zero if deq event exists */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 67 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 68 | u32 master_session_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 69 | u32 client_session_index; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 70 | u8 master_thread_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 71 | u8 client_thread_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 72 | u32 segment_manager; |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 73 | u32 segment_index; |
Florin Coras | 326b81e | 2018-10-04 19:03:05 -0700 | [diff] [blame] | 74 | u32 ct_session_index; /**< Local session index for vpp */ |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 75 | u32 freelist_index; /**< aka log2(allocated_size) - const. */ |
| 76 | i8 refcnt; /**< reference count */ |
| 77 | |
| 78 | CLIB_CACHE_LINE_ALIGN_MARK (consumer); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 79 | u32 head; |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 80 | volatile u32 want_tx_ntf; /**< producer wants nudge */ |
| 81 | volatile u32 has_tx_ntf; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 82 | |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 83 | CLIB_CACHE_LINE_ALIGN_MARK (producer); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 84 | u32 tail; |
| 85 | |
| 86 | ooo_segment_t *ooo_segments; /**< Pool of ooo segments */ |
| 87 | u32 ooos_list_head; /**< Head of out-of-order linked-list */ |
| 88 | u32 ooos_newest; /**< Last segment to have been updated */ |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 89 | struct _svm_fifo *next; /**< next in freelist/active chain */ |
| 90 | struct _svm_fifo *prev; /**< prev in active chain */ |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 91 | volatile u8 n_subscribers; |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 92 | u8 subscribers[SVM_FIFO_MAX_EVT_SUBSCRIBERS]; |
| 93 | |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 94 | #if SVM_FIFO_TRACE |
| 95 | svm_fifo_trace_elem_t *trace; |
| 96 | #endif |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 97 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 98 | CLIB_CACHE_LINE_ALIGN_MARK (data); |
| 99 | } svm_fifo_t; |
| 100 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 101 | typedef enum |
| 102 | { |
| 103 | SVM_FIFO_FULL = -2, |
| 104 | } svm_fifo_err_t; |
| 105 | |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 106 | typedef struct svm_fifo_segment_ |
| 107 | { |
| 108 | u8 *data; |
| 109 | u32 len; |
| 110 | } svm_fifo_segment_t; |
| 111 | |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 112 | #if SVM_FIFO_TRACE |
| 113 | #define svm_fifo_trace_add(_f, _s, _l, _t) \ |
| 114 | { \ |
| 115 | svm_fifo_trace_elem_t *trace_elt; \ |
| 116 | vec_add2(_f->trace, trace_elt, 1); \ |
| 117 | trace_elt->offset = _s; \ |
| 118 | trace_elt->len = _l; \ |
| 119 | trace_elt->action = _t; \ |
| 120 | } |
| 121 | #else |
| 122 | #define svm_fifo_trace_add(_f, _s, _l, _t) |
| 123 | #endif |
| 124 | |
| 125 | u8 *svm_fifo_dump_trace (u8 * s, svm_fifo_t * f); |
| 126 | u8 *svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose); |
| 127 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 128 | static inline u32 |
| 129 | svm_fifo_max_dequeue (svm_fifo_t * f) |
| 130 | { |
Sirshak Das | 19515ac | 2018-11-07 18:46:42 -0600 | [diff] [blame] | 131 | return clib_atomic_load_acq_n (&f->cursize); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 132 | } |
| 133 | |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 134 | static inline int |
| 135 | svm_fifo_is_full (svm_fifo_t * f) |
| 136 | { |
Sirshak Das | 19515ac | 2018-11-07 18:46:42 -0600 | [diff] [blame] | 137 | return (clib_atomic_load_acq_n (&f->cursize) == f->nitems); |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Florin Coras | 8409955 | 2018-07-22 12:59:30 -0700 | [diff] [blame] | 140 | static inline int |
| 141 | svm_fifo_is_empty (svm_fifo_t * f) |
| 142 | { |
Sirshak Das | 19515ac | 2018-11-07 18:46:42 -0600 | [diff] [blame] | 143 | return (clib_atomic_load_acq_n (&f->cursize) == 0); |
Florin Coras | 8409955 | 2018-07-22 12:59:30 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 146 | static inline u32 |
| 147 | svm_fifo_max_enqueue (svm_fifo_t * f) |
| 148 | { |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 149 | return f->nitems - svm_fifo_max_dequeue (f); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 150 | } |
| 151 | |
Florin Coras | 46f001d | 2018-07-11 05:25:06 -0700 | [diff] [blame] | 152 | static inline int |
| 153 | svm_fifo_has_event (svm_fifo_t * f) |
| 154 | { |
| 155 | return f->has_event; |
| 156 | } |
| 157 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 158 | static inline u8 |
| 159 | svm_fifo_has_ooo_data (svm_fifo_t * f) |
| 160 | { |
| 161 | return f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX; |
| 162 | } |
| 163 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 164 | /** |
| 165 | * Sets fifo event flag. |
| 166 | * |
Florin Coras | 41c9e04 | 2018-09-11 00:10:41 -0700 | [diff] [blame] | 167 | * Also acts as a release barrier. |
| 168 | * |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 169 | * @return 1 if flag was not set. |
| 170 | */ |
| 171 | always_inline u8 |
| 172 | svm_fifo_set_event (svm_fifo_t * f) |
| 173 | { |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 174 | /* return __sync_lock_test_and_set (&f->has_event, 1) == 0; |
| 175 | return __sync_bool_compare_and_swap (&f->has_event, 0, 1); */ |
Florin Coras | 41c9e04 | 2018-09-11 00:10:41 -0700 | [diff] [blame] | 176 | return !__atomic_exchange_n (&f->has_event, 1, __ATOMIC_RELEASE); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Unsets fifo event flag. |
Florin Coras | 41c9e04 | 2018-09-11 00:10:41 -0700 | [diff] [blame] | 181 | * |
| 182 | * Also acts as a release barrier. |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 183 | */ |
| 184 | always_inline void |
| 185 | svm_fifo_unset_event (svm_fifo_t * f) |
| 186 | { |
Sirshak Das | 2f6d7bb | 2018-10-03 22:53:51 +0000 | [diff] [blame] | 187 | clib_atomic_release (&f->has_event); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 190 | svm_fifo_t *svm_fifo_create (u32 data_size_in_bytes); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 191 | void svm_fifo_free (svm_fifo_t * f); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 192 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 193 | int svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 max_bytes, |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 194 | const u8 * copy_from_here); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 195 | int svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, |
| 196 | u32 required_bytes, u8 * copy_from_here); |
| 197 | int svm_fifo_dequeue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_here); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 198 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 199 | int svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 max_bytes, u8 * copy_here); |
| 200 | int svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes); |
Florin Coras | 25579b4 | 2018-06-06 17:55:02 -0700 | [diff] [blame] | 201 | void svm_fifo_dequeue_drop_all (svm_fifo_t * f); |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 202 | int svm_fifo_segments (svm_fifo_t * f, svm_fifo_segment_t * fs); |
| 203 | void svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_segment_t * fs); |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 204 | void svm_fifo_init_pointers (svm_fifo_t * f, u32 pointer); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 205 | void svm_fifo_overwrite_head (svm_fifo_t * f, u8 * data, u32 len); |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 206 | void svm_fifo_add_subscriber (svm_fifo_t * f, u8 subscriber); |
| 207 | void svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 208 | format_function_t format_svm_fifo; |
| 209 | |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 210 | /** |
| 211 | * Max contiguous chunk of data that can be read |
| 212 | */ |
| 213 | always_inline u32 |
| 214 | svm_fifo_max_read_chunk (svm_fifo_t * f) |
| 215 | { |
| 216 | return ((f->tail > f->head) ? (f->tail - f->head) : (f->nitems - f->head)); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Max contiguous chunk of data that can be written |
| 221 | */ |
| 222 | always_inline u32 |
| 223 | svm_fifo_max_write_chunk (svm_fifo_t * f) |
| 224 | { |
| 225 | return ((f->tail >= f->head) ? (f->nitems - f->tail) : (f->head - f->tail)); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Advance tail pointer |
| 230 | * |
| 231 | * Useful for moving tail pointer after external enqueue. |
| 232 | */ |
| 233 | always_inline void |
| 234 | svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 bytes) |
| 235 | { |
| 236 | ASSERT (bytes <= svm_fifo_max_enqueue (f)); |
| 237 | f->tail = (f->tail + bytes) % f->nitems; |
Florin Coras | 0964985 | 2019-02-20 23:13:43 -0800 | [diff] [blame] | 238 | clib_atomic_fetch_add_rel (&f->cursize, bytes); |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | always_inline u8 * |
| 242 | svm_fifo_head (svm_fifo_t * f) |
| 243 | { |
| 244 | return (f->data + f->head); |
| 245 | } |
| 246 | |
| 247 | always_inline u8 * |
| 248 | svm_fifo_tail (svm_fifo_t * f) |
| 249 | { |
| 250 | return (f->data + f->tail); |
| 251 | } |
| 252 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 253 | always_inline u32 |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 254 | svm_fifo_nitems (svm_fifo_t * f) |
| 255 | { |
| 256 | return f->nitems; |
| 257 | } |
| 258 | |
| 259 | static inline void |
| 260 | svm_fifo_add_want_tx_ntf (svm_fifo_t * f, u8 ntf_type) |
| 261 | { |
| 262 | f->want_tx_ntf |= ntf_type; |
| 263 | } |
| 264 | |
| 265 | static inline void |
| 266 | svm_fifo_del_want_tx_ntf (svm_fifo_t * f, u8 ntf_type) |
| 267 | { |
| 268 | f->want_tx_ntf &= ~ntf_type; |
| 269 | } |
| 270 | |
| 271 | static inline void |
| 272 | svm_fifo_clear_tx_ntf (svm_fifo_t * f) |
| 273 | { |
| 274 | /* Set the flag if want_tx_notif_if_full was the only ntf requested */ |
| 275 | f->has_tx_ntf = f->want_tx_ntf == SVM_FIFO_WANT_TX_NOTIF_IF_FULL; |
| 276 | svm_fifo_del_want_tx_ntf (f, SVM_FIFO_WANT_TX_NOTIF); |
| 277 | } |
| 278 | |
| 279 | static inline void |
| 280 | svm_fifo_reset_tx_ntf (svm_fifo_t * f) |
| 281 | { |
| 282 | f->has_tx_ntf = 0; |
| 283 | } |
| 284 | |
| 285 | static inline u8 |
| 286 | svm_fifo_needs_tx_ntf (svm_fifo_t * f, u32 n_last_deq) |
| 287 | { |
| 288 | u8 want_ntf = f->want_tx_ntf; |
| 289 | |
| 290 | if (PREDICT_TRUE (want_ntf == SVM_FIFO_NO_TX_NOTIF)) |
| 291 | return 0; |
| 292 | else if (want_ntf & SVM_FIFO_WANT_TX_NOTIF) |
| 293 | return 1; |
| 294 | else if (want_ntf & SVM_FIFO_WANT_TX_NOTIF_IF_FULL) |
| 295 | { |
| 296 | u32 max_deq = svm_fifo_max_dequeue (f); |
| 297 | u32 nitems = svm_fifo_nitems (f); |
| 298 | if (!f->has_tx_ntf && max_deq < nitems |
| 299 | && max_deq + n_last_deq >= nitems) |
| 300 | return 1; |
| 301 | |
| 302 | return 0; |
| 303 | } |
| 304 | return 0; |
| 305 | } |
| 306 | |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 307 | always_inline u8 |
| 308 | svm_fifo_n_subscribers (svm_fifo_t * f) |
| 309 | { |
| 310 | return f->n_subscribers; |
| 311 | } |
| 312 | |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 313 | u32 svm_fifo_number_ooo_segments (svm_fifo_t * f); |
| 314 | ooo_segment_t *svm_fifo_first_ooo_segment (svm_fifo_t * f); |
| 315 | |
| 316 | always_inline ooo_segment_t * |
| 317 | svm_fifo_newest_ooo_segment (svm_fifo_t * f) |
| 318 | { |
| 319 | if (f->ooos_newest == OOO_SEGMENT_INVALID_INDEX) |
| 320 | return 0; |
| 321 | return pool_elt_at_index (f->ooo_segments, f->ooos_newest); |
| 322 | } |
| 323 | |
| 324 | always_inline void |
| 325 | svm_fifo_newest_ooo_segment_reset (svm_fifo_t * f) |
| 326 | { |
| 327 | f->ooos_newest = OOO_SEGMENT_INVALID_INDEX; |
| 328 | } |
| 329 | |
| 330 | always_inline u32 |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 331 | ooo_segment_distance_from_tail (svm_fifo_t * f, u32 pos) |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 332 | { |
| 333 | /* Ambiguous. Assumption is that ooo segments don't touch tail */ |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 334 | if (PREDICT_FALSE (pos == f->tail && f->tail == f->head)) |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 335 | return f->nitems; |
| 336 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 337 | return (((f->nitems + pos) - f->tail) % f->nitems); |
| 338 | } |
| 339 | |
| 340 | always_inline u32 |
| 341 | ooo_segment_distance_to_tail (svm_fifo_t * f, u32 pos) |
| 342 | { |
| 343 | return (((f->nitems + f->tail) - pos) % f->nitems); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | always_inline u32 |
| 347 | ooo_segment_offset (svm_fifo_t * f, ooo_segment_t * s) |
| 348 | { |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 349 | return ooo_segment_distance_from_tail (f, s->start); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | always_inline u32 |
| 353 | ooo_segment_end_offset (svm_fifo_t * f, ooo_segment_t * s) |
| 354 | { |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 355 | return ooo_segment_distance_from_tail (f, s->start) + s->length; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | always_inline u32 |
| 359 | ooo_segment_length (svm_fifo_t * f, ooo_segment_t * s) |
| 360 | { |
| 361 | return s->length; |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | always_inline ooo_segment_t * |
| 365 | ooo_segment_get_prev (svm_fifo_t * f, ooo_segment_t * s) |
| 366 | { |
| 367 | if (s->prev == OOO_SEGMENT_INVALID_INDEX) |
| 368 | return 0; |
| 369 | return pool_elt_at_index (f->ooo_segments, s->prev); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 370 | } |
| 371 | |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 372 | always_inline ooo_segment_t * |
| 373 | ooo_segment_next (svm_fifo_t * f, ooo_segment_t * s) |
| 374 | { |
| 375 | if (s->next == OOO_SEGMENT_INVALID_INDEX) |
| 376 | return 0; |
| 377 | return pool_elt_at_index (f->ooo_segments, s->next); |
| 378 | } |
| 379 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 380 | #endif /* __included_ssvm_fifo_h__ */ |
| 381 | |
| 382 | /* |
| 383 | * fd.io coding-style-patch-verification: ON |
| 384 | * |
| 385 | * Local Variables: |
| 386 | * eval: (c-set-style "gnu") |
| 387 | * End: |
| 388 | */ |