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) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 42 | |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 43 | typedef struct |
| 44 | { |
| 45 | u32 offset; |
| 46 | u32 len; |
| 47 | u32 action; |
| 48 | } svm_fifo_trace_elem_t; |
| 49 | |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 50 | typedef struct _svm_fifo |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 51 | { |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 52 | volatile u32 cursize; /**< current fifo size */ |
| 53 | u32 nitems; |
| 54 | CLIB_CACHE_LINE_ALIGN_MARK (end_cursize); |
| 55 | |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 56 | volatile u32 has_event; /**< non-zero if deq event exists */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 57 | |
| 58 | /* Backpointers */ |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 59 | u32 master_session_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 60 | u32 client_session_index; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 61 | u8 master_thread_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 62 | u8 client_thread_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 63 | u32 segment_manager; |
Florin Coras | 326b81e | 2018-10-04 19:03:05 -0700 | [diff] [blame] | 64 | u32 ct_session_index; /**< Local session index for vpp */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 65 | CLIB_CACHE_LINE_ALIGN_MARK (end_shared); |
| 66 | u32 head; |
Florin Coras | 0e88e85 | 2018-09-17 22:09:02 -0700 | [diff] [blame] | 67 | volatile u32 want_tx_evt; /**< producer wants nudge */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 68 | CLIB_CACHE_LINE_ALIGN_MARK (end_consumer); |
| 69 | |
| 70 | /* producer */ |
| 71 | u32 tail; |
| 72 | |
| 73 | ooo_segment_t *ooo_segments; /**< Pool of ooo segments */ |
| 74 | u32 ooos_list_head; /**< Head of out-of-order linked-list */ |
| 75 | u32 ooos_newest; /**< Last segment to have been updated */ |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 76 | struct _svm_fifo *next; /**< next in freelist/active chain */ |
| 77 | struct _svm_fifo *prev; /**< prev in active chain */ |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 78 | #if SVM_FIFO_TRACE |
| 79 | svm_fifo_trace_elem_t *trace; |
| 80 | #endif |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 81 | u32 freelist_index; /**< aka log2(allocated_size) - const. */ |
| 82 | i8 refcnt; /**< reference count */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 83 | CLIB_CACHE_LINE_ALIGN_MARK (data); |
| 84 | } svm_fifo_t; |
| 85 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 86 | typedef enum |
| 87 | { |
| 88 | SVM_FIFO_FULL = -2, |
| 89 | } svm_fifo_err_t; |
| 90 | |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 91 | typedef struct svm_fifo_segment_ |
| 92 | { |
| 93 | u8 *data; |
| 94 | u32 len; |
| 95 | } svm_fifo_segment_t; |
| 96 | |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 97 | #if SVM_FIFO_TRACE |
| 98 | #define svm_fifo_trace_add(_f, _s, _l, _t) \ |
| 99 | { \ |
| 100 | svm_fifo_trace_elem_t *trace_elt; \ |
| 101 | vec_add2(_f->trace, trace_elt, 1); \ |
| 102 | trace_elt->offset = _s; \ |
| 103 | trace_elt->len = _l; \ |
| 104 | trace_elt->action = _t; \ |
| 105 | } |
| 106 | #else |
| 107 | #define svm_fifo_trace_add(_f, _s, _l, _t) |
| 108 | #endif |
| 109 | |
| 110 | u8 *svm_fifo_dump_trace (u8 * s, svm_fifo_t * f); |
| 111 | u8 *svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose); |
| 112 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 113 | static inline u32 |
| 114 | svm_fifo_max_dequeue (svm_fifo_t * f) |
| 115 | { |
| 116 | return f->cursize; |
| 117 | } |
| 118 | |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 119 | static inline int |
| 120 | svm_fifo_is_full (svm_fifo_t * f) |
| 121 | { |
| 122 | return (f->cursize == f->nitems); |
| 123 | } |
| 124 | |
Florin Coras | 8409955 | 2018-07-22 12:59:30 -0700 | [diff] [blame] | 125 | static inline int |
| 126 | svm_fifo_is_empty (svm_fifo_t * f) |
| 127 | { |
| 128 | return (f->cursize == 0); |
| 129 | } |
| 130 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 131 | static inline u32 |
| 132 | svm_fifo_max_enqueue (svm_fifo_t * f) |
| 133 | { |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 134 | return f->nitems - svm_fifo_max_dequeue (f); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 135 | } |
| 136 | |
Florin Coras | 46f001d | 2018-07-11 05:25:06 -0700 | [diff] [blame] | 137 | static inline int |
| 138 | svm_fifo_has_event (svm_fifo_t * f) |
| 139 | { |
| 140 | return f->has_event; |
| 141 | } |
| 142 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 143 | static inline u8 |
| 144 | svm_fifo_has_ooo_data (svm_fifo_t * f) |
| 145 | { |
| 146 | return f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX; |
| 147 | } |
| 148 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 149 | /** |
| 150 | * Sets fifo event flag. |
| 151 | * |
Florin Coras | 41c9e04 | 2018-09-11 00:10:41 -0700 | [diff] [blame] | 152 | * Also acts as a release barrier. |
| 153 | * |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 154 | * @return 1 if flag was not set. |
| 155 | */ |
| 156 | always_inline u8 |
| 157 | svm_fifo_set_event (svm_fifo_t * f) |
| 158 | { |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 159 | /* return __sync_lock_test_and_set (&f->has_event, 1) == 0; |
| 160 | return __sync_bool_compare_and_swap (&f->has_event, 0, 1); */ |
Florin Coras | 41c9e04 | 2018-09-11 00:10:41 -0700 | [diff] [blame] | 161 | return !__atomic_exchange_n (&f->has_event, 1, __ATOMIC_RELEASE); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Unsets fifo event flag. |
Florin Coras | 41c9e04 | 2018-09-11 00:10:41 -0700 | [diff] [blame] | 166 | * |
| 167 | * Also acts as a release barrier. |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 168 | */ |
| 169 | always_inline void |
| 170 | svm_fifo_unset_event (svm_fifo_t * f) |
| 171 | { |
Sirshak Das | 2f6d7bb | 2018-10-03 22:53:51 +0000 | [diff] [blame] | 172 | clib_atomic_release (&f->has_event); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Florin Coras | 0e88e85 | 2018-09-17 22:09:02 -0700 | [diff] [blame] | 175 | static inline void |
| 176 | svm_fifo_set_want_tx_evt (svm_fifo_t * f, u8 want_evt) |
| 177 | { |
| 178 | f->want_tx_evt = want_evt; |
| 179 | } |
| 180 | |
| 181 | static inline u8 |
| 182 | svm_fifo_want_tx_evt (svm_fifo_t * f) |
| 183 | { |
| 184 | return f->want_tx_evt; |
| 185 | } |
| 186 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 187 | svm_fifo_t *svm_fifo_create (u32 data_size_in_bytes); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 188 | void svm_fifo_free (svm_fifo_t * f); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 189 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 190 | int svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 max_bytes, |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 191 | const u8 * copy_from_here); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 192 | int svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, |
| 193 | u32 required_bytes, u8 * copy_from_here); |
| 194 | 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] | 195 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 196 | int svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 max_bytes, u8 * copy_here); |
| 197 | int svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes); |
Florin Coras | 25579b4 | 2018-06-06 17:55:02 -0700 | [diff] [blame] | 198 | void svm_fifo_dequeue_drop_all (svm_fifo_t * f); |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 199 | int svm_fifo_segments (svm_fifo_t * f, svm_fifo_segment_t * fs); |
| 200 | void svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_segment_t * fs); |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 201 | u32 svm_fifo_number_ooo_segments (svm_fifo_t * f); |
| 202 | ooo_segment_t *svm_fifo_first_ooo_segment (svm_fifo_t * f); |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 203 | void svm_fifo_init_pointers (svm_fifo_t * f, u32 pointer); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 204 | void svm_fifo_overwrite_head (svm_fifo_t * f, u8 * data, u32 len); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 205 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 206 | format_function_t format_svm_fifo; |
| 207 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 208 | always_inline ooo_segment_t * |
| 209 | svm_fifo_newest_ooo_segment (svm_fifo_t * f) |
| 210 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 211 | if (f->ooos_newest == OOO_SEGMENT_INVALID_INDEX) |
| 212 | return 0; |
| 213 | return pool_elt_at_index (f->ooo_segments, f->ooos_newest); |
| 214 | } |
| 215 | |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 216 | always_inline void |
| 217 | svm_fifo_newest_ooo_segment_reset (svm_fifo_t * f) |
| 218 | { |
| 219 | f->ooos_newest = OOO_SEGMENT_INVALID_INDEX; |
| 220 | } |
| 221 | |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 222 | /** |
| 223 | * Max contiguous chunk of data that can be read |
| 224 | */ |
| 225 | always_inline u32 |
| 226 | svm_fifo_max_read_chunk (svm_fifo_t * f) |
| 227 | { |
| 228 | return ((f->tail > f->head) ? (f->tail - f->head) : (f->nitems - f->head)); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Max contiguous chunk of data that can be written |
| 233 | */ |
| 234 | always_inline u32 |
| 235 | svm_fifo_max_write_chunk (svm_fifo_t * f) |
| 236 | { |
| 237 | return ((f->tail >= f->head) ? (f->nitems - f->tail) : (f->head - f->tail)); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Advance tail pointer |
| 242 | * |
| 243 | * Useful for moving tail pointer after external enqueue. |
| 244 | */ |
| 245 | always_inline void |
| 246 | svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 bytes) |
| 247 | { |
| 248 | ASSERT (bytes <= svm_fifo_max_enqueue (f)); |
| 249 | f->tail = (f->tail + bytes) % f->nitems; |
| 250 | f->cursize += bytes; |
| 251 | } |
| 252 | |
| 253 | always_inline u8 * |
| 254 | svm_fifo_head (svm_fifo_t * f) |
| 255 | { |
| 256 | return (f->data + f->head); |
| 257 | } |
| 258 | |
| 259 | always_inline u8 * |
| 260 | svm_fifo_tail (svm_fifo_t * f) |
| 261 | { |
| 262 | return (f->data + f->tail); |
| 263 | } |
| 264 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 265 | always_inline u32 |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 266 | ooo_segment_distance_from_tail (svm_fifo_t * f, u32 pos) |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 267 | { |
| 268 | /* Ambiguous. Assumption is that ooo segments don't touch tail */ |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 269 | if (PREDICT_FALSE (pos == f->tail && f->tail == f->head)) |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 270 | return f->nitems; |
| 271 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 272 | return (((f->nitems + pos) - f->tail) % f->nitems); |
| 273 | } |
| 274 | |
| 275 | always_inline u32 |
| 276 | ooo_segment_distance_to_tail (svm_fifo_t * f, u32 pos) |
| 277 | { |
| 278 | return (((f->nitems + f->tail) - pos) % f->nitems); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | always_inline u32 |
| 282 | ooo_segment_offset (svm_fifo_t * f, ooo_segment_t * s) |
| 283 | { |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 284 | return ooo_segment_distance_from_tail (f, s->start); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | always_inline u32 |
| 288 | ooo_segment_end_offset (svm_fifo_t * f, ooo_segment_t * s) |
| 289 | { |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 290 | return ooo_segment_distance_from_tail (f, s->start) + s->length; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | always_inline u32 |
| 294 | ooo_segment_length (svm_fifo_t * f, ooo_segment_t * s) |
| 295 | { |
| 296 | return s->length; |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | always_inline ooo_segment_t * |
| 300 | ooo_segment_get_prev (svm_fifo_t * f, ooo_segment_t * s) |
| 301 | { |
| 302 | if (s->prev == OOO_SEGMENT_INVALID_INDEX) |
| 303 | return 0; |
| 304 | return pool_elt_at_index (f->ooo_segments, s->prev); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 305 | } |
| 306 | |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 307 | always_inline ooo_segment_t * |
| 308 | ooo_segment_next (svm_fifo_t * f, ooo_segment_t * s) |
| 309 | { |
| 310 | if (s->next == OOO_SEGMENT_INVALID_INDEX) |
| 311 | return 0; |
| 312 | return pool_elt_at_index (f->ooo_segments, s->next); |
| 313 | } |
| 314 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 315 | #endif /* __included_ssvm_fifo_h__ */ |
| 316 | |
| 317 | /* |
| 318 | * fd.io coding-style-patch-verification: ON |
| 319 | * |
| 320 | * Local Variables: |
| 321 | * eval: (c-set-style "gnu") |
| 322 | * End: |
| 323 | */ |