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 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 16 | #include <svm/svm_fifo.h> |
Florin Coras | f6359c8 | 2017-06-19 12:26:09 -0400 | [diff] [blame] | 17 | #include <vppinfra/cpu.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 18 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 19 | static inline u8 |
| 20 | position_lt (svm_fifo_t * f, u32 a, u32 b) |
| 21 | { |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 22 | return (ooo_segment_distance_from_tail (f, a) |
| 23 | < ooo_segment_distance_from_tail (f, b)); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | static inline u8 |
| 27 | position_leq (svm_fifo_t * f, u32 a, u32 b) |
| 28 | { |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 29 | return (ooo_segment_distance_from_tail (f, a) |
| 30 | <= ooo_segment_distance_from_tail (f, b)); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | static inline u8 |
| 34 | position_gt (svm_fifo_t * f, u32 a, u32 b) |
| 35 | { |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 36 | return (ooo_segment_distance_from_tail (f, a) |
| 37 | > ooo_segment_distance_from_tail (f, b)); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static inline u32 |
| 41 | position_diff (svm_fifo_t * f, u32 posa, u32 posb) |
| 42 | { |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 43 | return ooo_segment_distance_from_tail (f, posa) |
| 44 | - ooo_segment_distance_from_tail (f, posb); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static inline u32 |
| 48 | ooo_segment_end_pos (svm_fifo_t * f, ooo_segment_t * s) |
| 49 | { |
| 50 | return (s->start + s->length) % f->nitems; |
| 51 | } |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 52 | |
| 53 | u8 * |
| 54 | format_ooo_segment (u8 * s, va_list * args) |
| 55 | { |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 56 | svm_fifo_t *f = va_arg (*args, svm_fifo_t *); |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 57 | ooo_segment_t *seg = va_arg (*args, ooo_segment_t *); |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 58 | u32 normalized_start = (seg->start + f->nitems - f->tail) % f->nitems; |
| 59 | s = format (s, "[%u, %u], len %u, next %d, prev %d", normalized_start, |
| 60 | (normalized_start + seg->length) % f->nitems, seg->length, |
| 61 | seg->next, seg->prev); |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 62 | return s; |
| 63 | } |
| 64 | |
| 65 | u8 * |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 66 | svm_fifo_dump_trace (u8 * s, svm_fifo_t * f) |
| 67 | { |
| 68 | #if SVM_FIFO_TRACE |
| 69 | svm_fifo_trace_elem_t *seg = 0; |
| 70 | int i = 0; |
| 71 | |
| 72 | if (f->trace) |
| 73 | { |
| 74 | vec_foreach (seg, f->trace) |
| 75 | { |
| 76 | s = format (s, "{%u, %u, %u}, ", seg->offset, seg->len, seg->action); |
| 77 | i++; |
| 78 | if (i % 5 == 0) |
| 79 | s = format (s, "\n"); |
| 80 | } |
| 81 | s = format (s, "\n"); |
| 82 | } |
| 83 | return s; |
| 84 | #else |
| 85 | return 0; |
| 86 | #endif |
| 87 | } |
| 88 | |
| 89 | u8 * |
| 90 | svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose) |
| 91 | { |
| 92 | int i, trace_len; |
| 93 | u8 *data = 0; |
| 94 | svm_fifo_trace_elem_t *trace; |
| 95 | u32 offset; |
| 96 | svm_fifo_t *dummy_fifo; |
| 97 | |
| 98 | if (!f) |
| 99 | return s; |
| 100 | |
| 101 | #if SVM_FIFO_TRACE |
| 102 | trace = f->trace; |
| 103 | trace_len = vec_len (trace); |
| 104 | #else |
| 105 | trace = 0; |
| 106 | trace_len = 0; |
| 107 | #endif |
| 108 | |
| 109 | dummy_fifo = svm_fifo_create (f->nitems); |
| 110 | memset (f->data, 0xFF, f->nitems); |
| 111 | |
| 112 | vec_validate (data, f->nitems); |
| 113 | for (i = 0; i < vec_len (data); i++) |
| 114 | data[i] = i; |
| 115 | |
| 116 | for (i = 0; i < trace_len; i++) |
| 117 | { |
| 118 | offset = trace[i].offset; |
| 119 | if (trace[i].action == 1) |
| 120 | { |
| 121 | if (verbose) |
| 122 | s = format (s, "adding [%u, %u]:", trace[i].offset, |
| 123 | (trace[i].offset + |
| 124 | trace[i].len) % dummy_fifo->nitems); |
| 125 | svm_fifo_enqueue_with_offset (dummy_fifo, trace[i].offset, |
| 126 | trace[i].len, &data[offset]); |
| 127 | } |
| 128 | else if (trace[i].action == 2) |
| 129 | { |
| 130 | if (verbose) |
| 131 | s = format (s, "adding [%u, %u]:", 0, trace[i].len); |
| 132 | svm_fifo_enqueue_nowait (dummy_fifo, trace[i].len, &data[offset]); |
| 133 | } |
| 134 | else if (!no_read) |
| 135 | { |
| 136 | if (verbose) |
| 137 | s = format (s, "read: %u", trace[i].len); |
| 138 | svm_fifo_dequeue_drop (dummy_fifo, trace[i].len); |
| 139 | } |
| 140 | if (verbose) |
| 141 | s = format (s, "%U", format_svm_fifo, dummy_fifo, 1); |
| 142 | } |
| 143 | |
| 144 | s = format (s, "result: %U", format_svm_fifo, dummy_fifo, 1); |
| 145 | |
| 146 | return s; |
| 147 | } |
| 148 | |
| 149 | u8 * |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 150 | format_ooo_list (u8 * s, va_list * args) |
| 151 | { |
| 152 | svm_fifo_t *f = va_arg (*args, svm_fifo_t *); |
| 153 | u32 ooo_segment_index = f->ooos_list_head; |
| 154 | ooo_segment_t *seg; |
| 155 | |
| 156 | while (ooo_segment_index != OOO_SEGMENT_INVALID_INDEX) |
| 157 | { |
| 158 | seg = pool_elt_at_index (f->ooo_segments, ooo_segment_index); |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 159 | s = format (s, " %U\n", format_ooo_segment, f, seg); |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 160 | ooo_segment_index = seg->next; |
| 161 | } |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 162 | |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 163 | return s; |
| 164 | } |
| 165 | |
Florin Coras | b59a705 | 2017-04-18 22:07:29 -0700 | [diff] [blame] | 166 | u8 * |
| 167 | format_svm_fifo (u8 * s, va_list * args) |
| 168 | { |
| 169 | svm_fifo_t *f = va_arg (*args, svm_fifo_t *); |
| 170 | int verbose = va_arg (*args, int); |
| 171 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 172 | if (!s) |
| 173 | return s; |
| 174 | |
Florin Coras | b59a705 | 2017-04-18 22:07:29 -0700 | [diff] [blame] | 175 | s = format (s, "cursize %u nitems %u has_event %d\n", |
| 176 | f->cursize, f->nitems, f->has_event); |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 177 | s = format (s, " head %d tail %d\n", f->head, f->tail); |
Florin Coras | b59a705 | 2017-04-18 22:07:29 -0700 | [diff] [blame] | 178 | |
| 179 | if (verbose > 1) |
| 180 | s = format |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 181 | (s, " server session %d thread %d client session %d thread %d\n", |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 182 | f->master_session_index, f->master_thread_index, |
Florin Coras | b59a705 | 2017-04-18 22:07:29 -0700 | [diff] [blame] | 183 | f->client_session_index, f->client_thread_index); |
| 184 | |
| 185 | if (verbose) |
| 186 | { |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 187 | s = format (s, " ooo pool %d active elts newest %u\n", |
| 188 | pool_elts (f->ooo_segments), f->ooos_newest); |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 189 | if (svm_fifo_has_ooo_data (f)) |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 190 | s = format (s, " %U", format_ooo_list, f, verbose); |
Florin Coras | b59a705 | 2017-04-18 22:07:29 -0700 | [diff] [blame] | 191 | } |
| 192 | return s; |
| 193 | } |
| 194 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 195 | /** create an svm fifo, in the current heap. Fails vs blow up the process */ |
| 196 | svm_fifo_t * |
| 197 | svm_fifo_create (u32 data_size_in_bytes) |
| 198 | { |
| 199 | svm_fifo_t *f; |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 200 | u32 rounded_data_size; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 201 | |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 202 | /* always round fifo data size to the next highest power-of-two */ |
| 203 | rounded_data_size = (1 << (max_log2 (data_size_in_bytes))); |
| 204 | f = clib_mem_alloc_aligned_or_null (sizeof (*f) + rounded_data_size, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 205 | CLIB_CACHE_LINE_BYTES); |
| 206 | if (f == 0) |
| 207 | return 0; |
| 208 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 209 | memset (f, 0, sizeof (*f)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 210 | f->nitems = data_size_in_bytes; |
| 211 | f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 212 | f->refcnt = 1; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 213 | return (f); |
| 214 | } |
| 215 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 216 | void |
| 217 | svm_fifo_free (svm_fifo_t * f) |
| 218 | { |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 219 | ASSERT (f->refcnt > 0); |
| 220 | |
| 221 | if (--f->refcnt == 0) |
| 222 | { |
| 223 | pool_free (f->ooo_segments); |
| 224 | clib_mem_free (f); |
| 225 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 228 | always_inline ooo_segment_t * |
| 229 | ooo_segment_new (svm_fifo_t * f, u32 start, u32 length) |
| 230 | { |
| 231 | ooo_segment_t *s; |
| 232 | |
| 233 | pool_get (f->ooo_segments, s); |
| 234 | |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 235 | s->start = start; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 236 | s->length = length; |
| 237 | |
| 238 | s->prev = s->next = OOO_SEGMENT_INVALID_INDEX; |
| 239 | |
| 240 | return s; |
| 241 | } |
| 242 | |
| 243 | always_inline void |
| 244 | ooo_segment_del (svm_fifo_t * f, u32 index) |
| 245 | { |
| 246 | ooo_segment_t *cur, *prev = 0, *next = 0; |
| 247 | cur = pool_elt_at_index (f->ooo_segments, index); |
| 248 | |
| 249 | if (cur->next != OOO_SEGMENT_INVALID_INDEX) |
| 250 | { |
| 251 | next = pool_elt_at_index (f->ooo_segments, cur->next); |
| 252 | next->prev = cur->prev; |
| 253 | } |
| 254 | |
| 255 | if (cur->prev != OOO_SEGMENT_INVALID_INDEX) |
| 256 | { |
| 257 | prev = pool_elt_at_index (f->ooo_segments, cur->prev); |
| 258 | prev->next = cur->next; |
| 259 | } |
| 260 | else |
| 261 | { |
| 262 | f->ooos_list_head = cur->next; |
| 263 | } |
| 264 | |
| 265 | pool_put (f->ooo_segments, cur); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Add segment to fifo's out-of-order segment list. Takes care of merging |
| 270 | * adjacent segments and removing overlapping ones. |
| 271 | */ |
| 272 | static void |
| 273 | ooo_segment_add (svm_fifo_t * f, u32 offset, u32 length) |
| 274 | { |
| 275 | ooo_segment_t *s, *new_s, *prev, *next, *it; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 276 | u32 new_index, s_end_pos, s_index; |
| 277 | u32 normalized_position, normalized_end_position; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 278 | |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 279 | ASSERT (offset + length <= ooo_segment_distance_from_tail (f, f->head)); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 280 | normalized_position = (f->tail + offset) % f->nitems; |
| 281 | normalized_end_position = (f->tail + offset + length) % f->nitems; |
| 282 | |
| 283 | f->ooos_newest = OOO_SEGMENT_INVALID_INDEX; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 284 | |
| 285 | if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX) |
| 286 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 287 | s = ooo_segment_new (f, normalized_position, length); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 288 | f->ooos_list_head = s - f->ooo_segments; |
| 289 | f->ooos_newest = f->ooos_list_head; |
| 290 | return; |
| 291 | } |
| 292 | |
| 293 | /* Find first segment that starts after new segment */ |
| 294 | s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head); |
| 295 | while (s->next != OOO_SEGMENT_INVALID_INDEX |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 296 | && position_lt (f, s->start, normalized_position)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 297 | s = pool_elt_at_index (f->ooo_segments, s->next); |
| 298 | |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 299 | /* If we have a previous and we overlap it, use it as starting point */ |
| 300 | prev = ooo_segment_get_prev (f, s); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 301 | if (prev |
| 302 | && position_leq (f, normalized_position, ooo_segment_end_pos (f, prev))) |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 303 | { |
| 304 | s = prev; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 305 | s_end_pos = ooo_segment_end_pos (f, s); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 306 | |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 307 | /* Since we have previous, normalized start position cannot be smaller |
| 308 | * than prev->start. Check tail */ |
| 309 | ASSERT (position_lt (f, s->start, normalized_position)); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 310 | goto check_tail; |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 313 | s_index = s - f->ooo_segments; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 314 | s_end_pos = ooo_segment_end_pos (f, s); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 315 | |
| 316 | /* No overlap, add before current segment */ |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 317 | if (position_lt (f, normalized_end_position, s->start)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 318 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 319 | new_s = ooo_segment_new (f, normalized_position, length); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 320 | new_index = new_s - f->ooo_segments; |
| 321 | |
| 322 | /* Pool might've moved, get segment again */ |
| 323 | s = pool_elt_at_index (f->ooo_segments, s_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 324 | if (s->prev != OOO_SEGMENT_INVALID_INDEX) |
| 325 | { |
| 326 | new_s->prev = s->prev; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 327 | prev = pool_elt_at_index (f->ooo_segments, new_s->prev); |
| 328 | prev->next = new_index; |
| 329 | } |
| 330 | else |
| 331 | { |
| 332 | /* New head */ |
| 333 | f->ooos_list_head = new_index; |
| 334 | } |
| 335 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 336 | new_s->next = s_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 337 | s->prev = new_index; |
| 338 | f->ooos_newest = new_index; |
| 339 | return; |
| 340 | } |
| 341 | /* No overlap, add after current segment */ |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 342 | else if (position_gt (f, normalized_position, s_end_pos)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 343 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 344 | new_s = ooo_segment_new (f, normalized_position, length); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 345 | new_index = new_s - f->ooo_segments; |
| 346 | |
| 347 | /* Pool might've moved, get segment again */ |
| 348 | s = pool_elt_at_index (f->ooo_segments, s_index); |
| 349 | |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 350 | /* Needs to be last */ |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 351 | ASSERT (s->next == OOO_SEGMENT_INVALID_INDEX); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 352 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 353 | new_s->prev = s_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 354 | s->next = new_index; |
| 355 | f->ooos_newest = new_index; |
| 356 | |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | /* |
| 361 | * Merge needed |
| 362 | */ |
| 363 | |
| 364 | /* Merge at head */ |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 365 | if (position_lt (f, normalized_position, s->start)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 366 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 367 | s->start = normalized_position; |
| 368 | s->length = position_diff (f, s_end_pos, s->start); |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 369 | f->ooos_newest = s - f->ooo_segments; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 370 | } |
| 371 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 372 | check_tail: |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 373 | |
| 374 | /* Overlapping tail */ |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 375 | if (position_gt (f, normalized_end_position, s_end_pos)) |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 376 | { |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 377 | s->length = position_diff (f, normalized_end_position, s->start); |
| 378 | |
| 379 | /* Remove the completely overlapped segments in the tail */ |
| 380 | it = ooo_segment_next (f, s); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 381 | while (it && position_leq (f, ooo_segment_end_pos (f, it), |
| 382 | normalized_end_position)) |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 383 | { |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 384 | next = ooo_segment_next (f, it); |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 385 | ooo_segment_del (f, it - f->ooo_segments); |
| 386 | it = next; |
| 387 | } |
| 388 | |
| 389 | /* If partial overlap with last, merge */ |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 390 | if (it && position_leq (f, it->start, normalized_end_position)) |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 391 | { |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 392 | s->length = position_diff (f, ooo_segment_end_pos (f, it), |
| 393 | s->start); |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 394 | ooo_segment_del (f, it - f->ooo_segments); |
| 395 | } |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 396 | f->ooos_newest = s - f->ooo_segments; |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 397 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Removes segments that can now be enqueued because the fifo's tail has |
| 402 | * advanced. Returns the number of bytes added to tail. |
| 403 | */ |
| 404 | static int |
| 405 | ooo_segment_try_collect (svm_fifo_t * f, u32 n_bytes_enqueued) |
| 406 | { |
| 407 | ooo_segment_t *s; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 408 | u32 index, bytes = 0; |
| 409 | i32 diff; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 410 | |
| 411 | s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 412 | diff = ooo_segment_distance_to_tail (f, s->start); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 413 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 414 | ASSERT (diff != n_bytes_enqueued); |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 415 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 416 | if (diff > n_bytes_enqueued) |
Florin Coras | b59a705 | 2017-04-18 22:07:29 -0700 | [diff] [blame] | 417 | return 0; |
| 418 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 419 | /* If last tail update overlaps one/multiple ooo segments, remove them */ |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 420 | while (0 <= diff && diff < n_bytes_enqueued) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 421 | { |
Florin Coras | b59a705 | 2017-04-18 22:07:29 -0700 | [diff] [blame] | 422 | index = s - f->ooo_segments; |
| 423 | |
| 424 | /* Segment end is beyond the tail. Advance tail and remove segment */ |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 425 | if (s->length > diff) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 426 | { |
Florin Coras | b59a705 | 2017-04-18 22:07:29 -0700 | [diff] [blame] | 427 | bytes = s->length - diff; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 428 | f->tail += bytes; |
| 429 | f->tail %= f->nitems; |
Florin Coras | b59a705 | 2017-04-18 22:07:29 -0700 | [diff] [blame] | 430 | ooo_segment_del (f, index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 431 | break; |
| 432 | } |
Florin Coras | b59a705 | 2017-04-18 22:07:29 -0700 | [diff] [blame] | 433 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 434 | /* If we have next go on */ |
Florin Coras | b59a705 | 2017-04-18 22:07:29 -0700 | [diff] [blame] | 435 | if (s->next != OOO_SEGMENT_INVALID_INDEX) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 436 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 437 | s = pool_elt_at_index (f->ooo_segments, s->next); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 438 | diff = ooo_segment_distance_to_tail (f, s->start); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 439 | ooo_segment_del (f, index); |
| 440 | } |
| 441 | /* End of search */ |
| 442 | else |
| 443 | { |
Florin Coras | b59a705 | 2017-04-18 22:07:29 -0700 | [diff] [blame] | 444 | ooo_segment_del (f, index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 445 | break; |
| 446 | } |
| 447 | } |
| 448 | |
Chris Luke | ab7b8d9 | 2017-09-07 07:40:13 -0400 | [diff] [blame] | 449 | ASSERT (bytes <= f->nitems); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 450 | return bytes; |
| 451 | } |
| 452 | |
| 453 | static int |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 454 | svm_fifo_enqueue_internal (svm_fifo_t * f, u32 max_bytes, |
| 455 | const u8 * copy_from_here) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 456 | { |
| 457 | u32 total_copy_bytes, first_copy_bytes, second_copy_bytes; |
| 458 | u32 cursize, nitems; |
| 459 | |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 460 | /* read cursize, which can only increase while we're working */ |
| 461 | cursize = svm_fifo_max_dequeue (f); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 462 | f->ooos_newest = OOO_SEGMENT_INVALID_INDEX; |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 463 | |
| 464 | if (PREDICT_FALSE (cursize == f->nitems)) |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 465 | return SVM_FIFO_FULL; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 466 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 467 | nitems = f->nitems; |
| 468 | |
| 469 | /* Number of bytes we're going to copy */ |
| 470 | total_copy_bytes = (nitems - cursize) < max_bytes ? |
| 471 | (nitems - cursize) : max_bytes; |
| 472 | |
| 473 | if (PREDICT_TRUE (copy_from_here != 0)) |
| 474 | { |
| 475 | /* Number of bytes in first copy segment */ |
| 476 | first_copy_bytes = ((nitems - f->tail) < total_copy_bytes) |
| 477 | ? (nitems - f->tail) : total_copy_bytes; |
| 478 | |
| 479 | clib_memcpy (&f->data[f->tail], copy_from_here, first_copy_bytes); |
| 480 | f->tail += first_copy_bytes; |
| 481 | f->tail = (f->tail == nitems) ? 0 : f->tail; |
| 482 | |
| 483 | /* Number of bytes in second copy segment, if any */ |
| 484 | second_copy_bytes = total_copy_bytes - first_copy_bytes; |
| 485 | if (second_copy_bytes) |
| 486 | { |
| 487 | clib_memcpy (&f->data[f->tail], copy_from_here + first_copy_bytes, |
| 488 | second_copy_bytes); |
| 489 | f->tail += second_copy_bytes; |
| 490 | f->tail = (f->tail == nitems) ? 0 : f->tail; |
| 491 | } |
| 492 | } |
| 493 | else |
| 494 | { |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 495 | ASSERT (0); |
| 496 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 497 | /* Account for a zero-copy enqueue done elsewhere */ |
| 498 | ASSERT (max_bytes <= (nitems - cursize)); |
| 499 | f->tail += max_bytes; |
| 500 | f->tail = f->tail % nitems; |
| 501 | total_copy_bytes = max_bytes; |
| 502 | } |
| 503 | |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 504 | svm_fifo_trace_add (f, f->head, total_copy_bytes, 2); |
| 505 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 506 | /* Any out-of-order segments to collect? */ |
| 507 | if (PREDICT_FALSE (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX)) |
| 508 | total_copy_bytes += ooo_segment_try_collect (f, total_copy_bytes); |
| 509 | |
| 510 | /* Atomically increase the queue length */ |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 511 | ASSERT (cursize + total_copy_bytes <= nitems); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 512 | __sync_fetch_and_add (&f->cursize, total_copy_bytes); |
| 513 | |
| 514 | return (total_copy_bytes); |
| 515 | } |
| 516 | |
Florin Coras | f6359c8 | 2017-06-19 12:26:09 -0400 | [diff] [blame] | 517 | #define SVM_ENQUEUE_CLONE_TEMPLATE(arch, fn, tgt) \ |
| 518 | uword \ |
| 519 | __attribute__ ((flatten)) \ |
| 520 | __attribute__ ((target (tgt))) \ |
| 521 | CLIB_CPU_OPTIMIZED \ |
| 522 | fn ## _ ## arch ( svm_fifo_t * f, u32 max_bytes, u8 * copy_from_here) \ |
| 523 | { return fn (f, max_bytes, copy_from_here);} |
| 524 | |
| 525 | static int |
| 526 | svm_fifo_enqueue_nowait_ma (svm_fifo_t * f, u32 max_bytes, |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 527 | const u8 * copy_from_here) |
Florin Coras | f6359c8 | 2017-06-19 12:26:09 -0400 | [diff] [blame] | 528 | { |
| 529 | return svm_fifo_enqueue_internal (f, max_bytes, copy_from_here); |
| 530 | } |
| 531 | |
| 532 | foreach_march_variant (SVM_ENQUEUE_CLONE_TEMPLATE, |
| 533 | svm_fifo_enqueue_nowait_ma); |
| 534 | CLIB_MULTIARCH_SELECT_FN (svm_fifo_enqueue_nowait_ma); |
| 535 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 536 | int |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 537 | svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 max_bytes, |
| 538 | const u8 * copy_from_here) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 539 | { |
Florin Coras | f6359c8 | 2017-06-19 12:26:09 -0400 | [diff] [blame] | 540 | #if CLIB_DEBUG > 0 |
| 541 | return svm_fifo_enqueue_nowait_ma (f, max_bytes, copy_from_here); |
| 542 | #else |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 543 | static int (*fp) (svm_fifo_t *, u32, const u8 *); |
Florin Coras | f6359c8 | 2017-06-19 12:26:09 -0400 | [diff] [blame] | 544 | |
| 545 | if (PREDICT_FALSE (fp == 0)) |
| 546 | fp = (void *) svm_fifo_enqueue_nowait_ma_multiarch_select (); |
| 547 | |
| 548 | return (*fp) (f, max_bytes, copy_from_here); |
| 549 | #endif |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 550 | } |
| 551 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 552 | /** |
| 553 | * Enqueue a future segment. |
| 554 | * |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 555 | * Two choices: either copies the entire segment, or copies nothing |
| 556 | * Returns 0 of the entire segment was copied |
| 557 | * Returns -1 if none of the segment was copied due to lack of space |
| 558 | */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 559 | static int |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 560 | svm_fifo_enqueue_with_offset_internal (svm_fifo_t * f, |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 561 | u32 offset, |
| 562 | u32 required_bytes, |
| 563 | u8 * copy_from_here) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 564 | { |
| 565 | u32 total_copy_bytes, first_copy_bytes, second_copy_bytes; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 566 | u32 cursize, nitems, normalized_offset; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 567 | |
| 568 | f->ooos_newest = OOO_SEGMENT_INVALID_INDEX; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 569 | |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 570 | /* read cursize, which can only increase while we're working */ |
| 571 | cursize = svm_fifo_max_dequeue (f); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 572 | nitems = f->nitems; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 573 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 574 | ASSERT (required_bytes < nitems); |
| 575 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 576 | normalized_offset = (f->tail + offset) % nitems; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 577 | |
| 578 | /* Will this request fit? */ |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 579 | if ((required_bytes + offset) > (nitems - cursize)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 580 | return -1; |
| 581 | |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 582 | svm_fifo_trace_add (f, offset, required_bytes, 1); |
| 583 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 584 | ooo_segment_add (f, offset, required_bytes); |
| 585 | |
| 586 | /* Number of bytes we're going to copy */ |
| 587 | total_copy_bytes = required_bytes; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 588 | |
| 589 | /* Number of bytes in first copy segment */ |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 590 | first_copy_bytes = ((nitems - normalized_offset) < total_copy_bytes) |
| 591 | ? (nitems - normalized_offset) : total_copy_bytes; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 592 | |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 593 | clib_memcpy (&f->data[normalized_offset], copy_from_here, first_copy_bytes); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 594 | |
| 595 | /* Number of bytes in second copy segment, if any */ |
| 596 | second_copy_bytes = total_copy_bytes - first_copy_bytes; |
| 597 | if (second_copy_bytes) |
| 598 | { |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 599 | normalized_offset += first_copy_bytes; |
| 600 | normalized_offset %= nitems; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 601 | |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 602 | ASSERT (normalized_offset == 0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 603 | |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 604 | clib_memcpy (&f->data[normalized_offset], |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 605 | copy_from_here + first_copy_bytes, second_copy_bytes); |
| 606 | } |
| 607 | |
| 608 | return (0); |
| 609 | } |
| 610 | |
| 611 | |
| 612 | int |
| 613 | svm_fifo_enqueue_with_offset (svm_fifo_t * f, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 614 | u32 offset, |
| 615 | u32 required_bytes, u8 * copy_from_here) |
| 616 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 617 | return svm_fifo_enqueue_with_offset_internal (f, offset, required_bytes, |
| 618 | copy_from_here); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 619 | } |
| 620 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 621 | void |
| 622 | svm_fifo_overwrite_head (svm_fifo_t * f, u8 * data, u32 len) |
| 623 | { |
| 624 | u32 first_chunk; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 625 | first_chunk = f->nitems - f->head; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 626 | ASSERT (len <= f->nitems); |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 627 | if (len <= first_chunk) |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 628 | clib_memcpy (&f->data[f->head], data, len); |
| 629 | else |
| 630 | { |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 631 | clib_memcpy (&f->data[f->head], data, first_chunk); |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 632 | clib_memcpy (&f->data[0], data + first_chunk, len - first_chunk); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 633 | } |
| 634 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 635 | |
| 636 | static int |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 637 | svm_fifo_dequeue_internal (svm_fifo_t * f, u32 max_bytes, u8 * copy_here) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 638 | { |
| 639 | u32 total_copy_bytes, first_copy_bytes, second_copy_bytes; |
| 640 | u32 cursize, nitems; |
| 641 | |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 642 | /* read cursize, which can only increase while we're working */ |
| 643 | cursize = svm_fifo_max_dequeue (f); |
| 644 | if (PREDICT_FALSE (cursize == 0)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 645 | return -2; /* nothing in the fifo */ |
| 646 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 647 | nitems = f->nitems; |
| 648 | |
| 649 | /* Number of bytes we're going to copy */ |
| 650 | total_copy_bytes = (cursize < max_bytes) ? cursize : max_bytes; |
| 651 | |
| 652 | if (PREDICT_TRUE (copy_here != 0)) |
| 653 | { |
| 654 | /* Number of bytes in first copy segment */ |
| 655 | first_copy_bytes = ((nitems - f->head) < total_copy_bytes) |
| 656 | ? (nitems - f->head) : total_copy_bytes; |
| 657 | clib_memcpy (copy_here, &f->data[f->head], first_copy_bytes); |
| 658 | f->head += first_copy_bytes; |
| 659 | f->head = (f->head == nitems) ? 0 : f->head; |
| 660 | |
| 661 | /* Number of bytes in second copy segment, if any */ |
| 662 | second_copy_bytes = total_copy_bytes - first_copy_bytes; |
| 663 | if (second_copy_bytes) |
| 664 | { |
| 665 | clib_memcpy (copy_here + first_copy_bytes, |
| 666 | &f->data[f->head], second_copy_bytes); |
| 667 | f->head += second_copy_bytes; |
| 668 | f->head = (f->head == nitems) ? 0 : f->head; |
| 669 | } |
| 670 | } |
| 671 | else |
| 672 | { |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 673 | ASSERT (0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 674 | /* Account for a zero-copy dequeue done elsewhere */ |
| 675 | ASSERT (max_bytes <= cursize); |
| 676 | f->head += max_bytes; |
| 677 | f->head = f->head % nitems; |
| 678 | cursize -= max_bytes; |
| 679 | total_copy_bytes = max_bytes; |
| 680 | } |
| 681 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 682 | ASSERT (f->head <= nitems); |
| 683 | ASSERT (cursize >= total_copy_bytes); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 684 | __sync_fetch_and_sub (&f->cursize, total_copy_bytes); |
| 685 | |
| 686 | return (total_copy_bytes); |
| 687 | } |
| 688 | |
Florin Coras | f6359c8 | 2017-06-19 12:26:09 -0400 | [diff] [blame] | 689 | static int |
| 690 | svm_fifo_dequeue_nowait_ma (svm_fifo_t * f, u32 max_bytes, u8 * copy_here) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 691 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 692 | return svm_fifo_dequeue_internal (f, max_bytes, copy_here); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 693 | } |
| 694 | |
Florin Coras | f6359c8 | 2017-06-19 12:26:09 -0400 | [diff] [blame] | 695 | #define SVM_FIFO_DEQUEUE_CLONE_TEMPLATE(arch, fn, tgt) \ |
| 696 | uword \ |
| 697 | __attribute__ ((flatten)) \ |
| 698 | __attribute__ ((target (tgt))) \ |
| 699 | CLIB_CPU_OPTIMIZED \ |
| 700 | fn ## _ ## arch ( svm_fifo_t * f, u32 max_bytes, \ |
| 701 | u8 * copy_here) \ |
| 702 | { return fn (f, max_bytes, copy_here);} |
| 703 | |
| 704 | foreach_march_variant (SVM_FIFO_DEQUEUE_CLONE_TEMPLATE, |
| 705 | svm_fifo_dequeue_nowait_ma); |
| 706 | CLIB_MULTIARCH_SELECT_FN (svm_fifo_dequeue_nowait_ma); |
| 707 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 708 | int |
Florin Coras | f6359c8 | 2017-06-19 12:26:09 -0400 | [diff] [blame] | 709 | svm_fifo_dequeue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_here) |
| 710 | { |
| 711 | #if CLIB_DEBUG > 0 |
| 712 | return svm_fifo_dequeue_nowait_ma (f, max_bytes, copy_here); |
| 713 | #else |
| 714 | static int (*fp) (svm_fifo_t *, u32, u8 *); |
| 715 | |
| 716 | if (PREDICT_FALSE (fp == 0)) |
| 717 | fp = (void *) svm_fifo_dequeue_nowait_ma_multiarch_select (); |
| 718 | |
| 719 | return (*fp) (f, max_bytes, copy_here); |
| 720 | #endif |
| 721 | } |
| 722 | |
| 723 | static int |
| 724 | svm_fifo_peek_ma (svm_fifo_t * f, u32 relative_offset, u32 max_bytes, |
| 725 | u8 * copy_here) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 726 | { |
| 727 | u32 total_copy_bytes, first_copy_bytes, second_copy_bytes; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 728 | u32 cursize, nitems, real_head; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 729 | |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 730 | /* read cursize, which can only increase while we're working */ |
| 731 | cursize = svm_fifo_max_dequeue (f); |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 732 | if (PREDICT_FALSE (cursize < relative_offset)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 733 | return -2; /* nothing in the fifo */ |
| 734 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 735 | nitems = f->nitems; |
Florin Coras | b59a705 | 2017-04-18 22:07:29 -0700 | [diff] [blame] | 736 | real_head = f->head + relative_offset; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 737 | real_head = real_head >= nitems ? real_head - nitems : real_head; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 738 | |
| 739 | /* Number of bytes we're going to copy */ |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 740 | total_copy_bytes = (cursize - relative_offset < max_bytes) ? |
| 741 | cursize - relative_offset : max_bytes; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 742 | |
| 743 | if (PREDICT_TRUE (copy_here != 0)) |
| 744 | { |
| 745 | /* Number of bytes in first copy segment */ |
| 746 | first_copy_bytes = |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 747 | ((nitems - real_head) < total_copy_bytes) ? |
| 748 | (nitems - real_head) : total_copy_bytes; |
| 749 | clib_memcpy (copy_here, &f->data[real_head], first_copy_bytes); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 750 | |
| 751 | /* Number of bytes in second copy segment, if any */ |
| 752 | second_copy_bytes = total_copy_bytes - first_copy_bytes; |
| 753 | if (second_copy_bytes) |
| 754 | { |
| 755 | clib_memcpy (copy_here + first_copy_bytes, &f->data[0], |
| 756 | second_copy_bytes); |
| 757 | } |
| 758 | } |
| 759 | return total_copy_bytes; |
| 760 | } |
| 761 | |
Florin Coras | f6359c8 | 2017-06-19 12:26:09 -0400 | [diff] [blame] | 762 | #define SVM_FIFO_PEEK_CLONE_TEMPLATE(arch, fn, tgt) \ |
| 763 | uword \ |
| 764 | __attribute__ ((flatten)) \ |
| 765 | __attribute__ ((target (tgt))) \ |
| 766 | CLIB_CPU_OPTIMIZED \ |
| 767 | fn ## _ ## arch ( svm_fifo_t * f, u32 relative_offset, u32 max_bytes, \ |
| 768 | u8 * copy_here) \ |
| 769 | { return fn (f, relative_offset, max_bytes, copy_here);} |
| 770 | |
| 771 | foreach_march_variant (SVM_FIFO_PEEK_CLONE_TEMPLATE, svm_fifo_peek_ma); |
| 772 | CLIB_MULTIARCH_SELECT_FN (svm_fifo_peek_ma); |
| 773 | |
| 774 | int |
| 775 | svm_fifo_peek (svm_fifo_t * f, u32 relative_offset, u32 max_bytes, |
| 776 | u8 * copy_here) |
| 777 | { |
| 778 | #if CLIB_DEBUG > 0 |
| 779 | return svm_fifo_peek_ma (f, relative_offset, max_bytes, copy_here); |
| 780 | #else |
| 781 | static int (*fp) (svm_fifo_t *, u32, u32, u8 *); |
| 782 | |
| 783 | if (PREDICT_FALSE (fp == 0)) |
| 784 | fp = (void *) svm_fifo_peek_ma_multiarch_select (); |
| 785 | |
| 786 | return (*fp) (f, relative_offset, max_bytes, copy_here); |
| 787 | #endif |
| 788 | } |
| 789 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 790 | int |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 791 | svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 792 | { |
| 793 | u32 total_drop_bytes, first_drop_bytes, second_drop_bytes; |
| 794 | u32 cursize, nitems; |
| 795 | |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 796 | /* read cursize, which can only increase while we're working */ |
| 797 | cursize = svm_fifo_max_dequeue (f); |
| 798 | if (PREDICT_FALSE (cursize == 0)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 799 | return -2; /* nothing in the fifo */ |
| 800 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 801 | nitems = f->nitems; |
| 802 | |
| 803 | /* Number of bytes we're going to drop */ |
| 804 | total_drop_bytes = (cursize < max_bytes) ? cursize : max_bytes; |
| 805 | |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 806 | svm_fifo_trace_add (f, f->tail, total_drop_bytes, 3); |
| 807 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 808 | /* Number of bytes in first copy segment */ |
| 809 | first_drop_bytes = |
| 810 | ((nitems - f->head) < total_drop_bytes) ? |
| 811 | (nitems - f->head) : total_drop_bytes; |
| 812 | f->head += first_drop_bytes; |
| 813 | f->head = (f->head == nitems) ? 0 : f->head; |
| 814 | |
| 815 | /* Number of bytes in second drop segment, if any */ |
| 816 | second_drop_bytes = total_drop_bytes - first_drop_bytes; |
| 817 | if (second_drop_bytes) |
| 818 | { |
| 819 | f->head += second_drop_bytes; |
| 820 | f->head = (f->head == nitems) ? 0 : f->head; |
| 821 | } |
| 822 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 823 | ASSERT (f->head <= nitems); |
| 824 | ASSERT (cursize >= total_drop_bytes); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 825 | __sync_fetch_and_sub (&f->cursize, total_drop_bytes); |
| 826 | |
| 827 | return total_drop_bytes; |
| 828 | } |
| 829 | |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 830 | u32 |
| 831 | svm_fifo_number_ooo_segments (svm_fifo_t * f) |
| 832 | { |
| 833 | return pool_elts (f->ooo_segments); |
| 834 | } |
| 835 | |
| 836 | ooo_segment_t * |
| 837 | svm_fifo_first_ooo_segment (svm_fifo_t * f) |
| 838 | { |
| 839 | return pool_elt_at_index (f->ooo_segments, f->ooos_list_head); |
| 840 | } |
| 841 | |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 842 | /** |
| 843 | * Set fifo pointers to requested offset |
| 844 | */ |
| 845 | void |
| 846 | svm_fifo_init_pointers (svm_fifo_t * f, u32 pointer) |
| 847 | { |
| 848 | f->head = f->tail = pointer % f->nitems; |
| 849 | } |
| 850 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 851 | /* |
| 852 | * fd.io coding-style-patch-verification: ON |
| 853 | * |
| 854 | * Local Variables: |
| 855 | * eval: (c-set-style "gnu") |
| 856 | * End: |
| 857 | */ |