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> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 17 | |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 18 | #define offset_lt(_a, _b) ((i32)((_a)-(_b)) < 0) |
| 19 | #define offset_leq(_a, _b) ((i32)((_a)-(_b)) <= 0) |
| 20 | |
| 21 | u8 * |
| 22 | format_ooo_segment (u8 * s, va_list * args) |
| 23 | { |
| 24 | ooo_segment_t *seg = va_arg (*args, ooo_segment_t *); |
| 25 | |
| 26 | s = format (s, "pos %u, len %u, next %d, prev %d", |
| 27 | seg->start, seg->length, seg->next, seg->prev); |
| 28 | return s; |
| 29 | } |
| 30 | |
| 31 | u8 * |
| 32 | format_ooo_list (u8 * s, va_list * args) |
| 33 | { |
| 34 | svm_fifo_t *f = va_arg (*args, svm_fifo_t *); |
| 35 | u32 ooo_segment_index = f->ooos_list_head; |
| 36 | ooo_segment_t *seg; |
| 37 | |
| 38 | while (ooo_segment_index != OOO_SEGMENT_INVALID_INDEX) |
| 39 | { |
| 40 | seg = pool_elt_at_index (f->ooo_segments, ooo_segment_index); |
Florin Coras | b59a705 | 2017-04-18 22:07:29 -0700 | [diff] [blame] | 41 | s = format (s, " %U\n", format_ooo_segment, seg); |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 42 | ooo_segment_index = seg->next; |
| 43 | } |
| 44 | return s; |
| 45 | } |
| 46 | |
Florin Coras | b59a705 | 2017-04-18 22:07:29 -0700 | [diff] [blame] | 47 | u8 * |
| 48 | format_svm_fifo (u8 * s, va_list * args) |
| 49 | { |
| 50 | svm_fifo_t *f = va_arg (*args, svm_fifo_t *); |
| 51 | int verbose = va_arg (*args, int); |
| 52 | |
| 53 | s = format (s, "cursize %u nitems %u has_event %d\n", |
| 54 | f->cursize, f->nitems, f->has_event); |
| 55 | s = format (s, "head %d tail %d\n", f->head, f->tail); |
| 56 | |
| 57 | if (verbose > 1) |
| 58 | s = format |
| 59 | (s, "server session %d thread %d client session %d thread %d\n", |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 60 | f->master_session_index, f->master_thread_index, |
Florin Coras | b59a705 | 2017-04-18 22:07:29 -0700 | [diff] [blame] | 61 | f->client_session_index, f->client_thread_index); |
| 62 | |
| 63 | if (verbose) |
| 64 | { |
| 65 | s = format (s, "ooo pool %d active elts\n", |
| 66 | pool_elts (f->ooo_segments)); |
| 67 | s = format (s, "%U", format_ooo_list, f); |
| 68 | } |
| 69 | return s; |
| 70 | } |
| 71 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 72 | /** create an svm fifo, in the current heap. Fails vs blow up the process */ |
| 73 | svm_fifo_t * |
| 74 | svm_fifo_create (u32 data_size_in_bytes) |
| 75 | { |
| 76 | svm_fifo_t *f; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 77 | |
| 78 | f = clib_mem_alloc_aligned_or_null (sizeof (*f) + data_size_in_bytes, |
| 79 | CLIB_CACHE_LINE_BYTES); |
| 80 | if (f == 0) |
| 81 | return 0; |
| 82 | |
| 83 | memset (f, 0, sizeof (*f) + data_size_in_bytes); |
| 84 | f->nitems = data_size_in_bytes; |
| 85 | f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX; |
| 86 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 87 | return (f); |
| 88 | } |
| 89 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 90 | void |
| 91 | svm_fifo_free (svm_fifo_t * f) |
| 92 | { |
| 93 | pool_free (f->ooo_segments); |
| 94 | clib_mem_free (f); |
| 95 | } |
| 96 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 97 | always_inline ooo_segment_t * |
| 98 | ooo_segment_new (svm_fifo_t * f, u32 start, u32 length) |
| 99 | { |
| 100 | ooo_segment_t *s; |
| 101 | |
| 102 | pool_get (f->ooo_segments, s); |
| 103 | |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 104 | s->start = start; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 105 | s->length = length; |
| 106 | |
| 107 | s->prev = s->next = OOO_SEGMENT_INVALID_INDEX; |
| 108 | |
| 109 | return s; |
| 110 | } |
| 111 | |
| 112 | always_inline void |
| 113 | ooo_segment_del (svm_fifo_t * f, u32 index) |
| 114 | { |
| 115 | ooo_segment_t *cur, *prev = 0, *next = 0; |
| 116 | cur = pool_elt_at_index (f->ooo_segments, index); |
| 117 | |
| 118 | if (cur->next != OOO_SEGMENT_INVALID_INDEX) |
| 119 | { |
| 120 | next = pool_elt_at_index (f->ooo_segments, cur->next); |
| 121 | next->prev = cur->prev; |
| 122 | } |
| 123 | |
| 124 | if (cur->prev != OOO_SEGMENT_INVALID_INDEX) |
| 125 | { |
| 126 | prev = pool_elt_at_index (f->ooo_segments, cur->prev); |
| 127 | prev->next = cur->next; |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | f->ooos_list_head = cur->next; |
| 132 | } |
| 133 | |
| 134 | pool_put (f->ooo_segments, cur); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Add segment to fifo's out-of-order segment list. Takes care of merging |
| 139 | * adjacent segments and removing overlapping ones. |
| 140 | */ |
| 141 | static void |
| 142 | ooo_segment_add (svm_fifo_t * f, u32 offset, u32 length) |
| 143 | { |
| 144 | ooo_segment_t *s, *new_s, *prev, *next, *it; |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 145 | u32 new_index, end_offset, s_sof, s_eof, s_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 146 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 147 | end_offset = offset + length; |
| 148 | |
| 149 | if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX) |
| 150 | { |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 151 | s = ooo_segment_new (f, offset, length); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 152 | f->ooos_list_head = s - f->ooo_segments; |
| 153 | f->ooos_newest = f->ooos_list_head; |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | /* Find first segment that starts after new segment */ |
| 158 | s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head); |
| 159 | while (s->next != OOO_SEGMENT_INVALID_INDEX |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 160 | && offset_leq (ooo_segment_offset (f, s), offset)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 161 | s = pool_elt_at_index (f->ooo_segments, s->next); |
| 162 | |
| 163 | s_index = s - f->ooo_segments; |
| 164 | s_sof = ooo_segment_offset (f, s); |
| 165 | s_eof = ooo_segment_end_offset (f, s); |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 166 | prev = ooo_segment_get_prev (f, s); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 167 | |
| 168 | /* No overlap, add before current segment */ |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 169 | if (offset_lt (end_offset, s_sof) |
| 170 | && (!prev || offset_lt (prev->start + prev->length, offset))) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 171 | { |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 172 | new_s = ooo_segment_new (f, offset, length); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 173 | new_index = new_s - f->ooo_segments; |
| 174 | |
| 175 | /* Pool might've moved, get segment again */ |
| 176 | s = pool_elt_at_index (f->ooo_segments, s_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 177 | if (s->prev != OOO_SEGMENT_INVALID_INDEX) |
| 178 | { |
| 179 | new_s->prev = s->prev; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 180 | prev = pool_elt_at_index (f->ooo_segments, new_s->prev); |
| 181 | prev->next = new_index; |
| 182 | } |
| 183 | else |
| 184 | { |
| 185 | /* New head */ |
| 186 | f->ooos_list_head = new_index; |
| 187 | } |
| 188 | |
| 189 | new_s->next = s - f->ooo_segments; |
| 190 | s->prev = new_index; |
| 191 | f->ooos_newest = new_index; |
| 192 | return; |
| 193 | } |
| 194 | /* No overlap, add after current segment */ |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 195 | else if (offset_lt (s_eof, offset)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 196 | { |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 197 | new_s = ooo_segment_new (f, offset, length); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 198 | new_index = new_s - f->ooo_segments; |
| 199 | |
| 200 | /* Pool might've moved, get segment again */ |
| 201 | s = pool_elt_at_index (f->ooo_segments, s_index); |
| 202 | |
| 203 | if (s->next != OOO_SEGMENT_INVALID_INDEX) |
| 204 | { |
| 205 | new_s->next = s->next; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 206 | next = pool_elt_at_index (f->ooo_segments, new_s->next); |
| 207 | next->prev = new_index; |
| 208 | } |
| 209 | |
| 210 | new_s->prev = s - f->ooo_segments; |
| 211 | s->next = new_index; |
| 212 | f->ooos_newest = new_index; |
| 213 | |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Merge needed |
| 219 | */ |
| 220 | |
| 221 | /* Merge at head */ |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 222 | if (offset_leq (offset, s_sof)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 223 | { |
| 224 | /* If we have a previous, check if we overlap */ |
| 225 | if (s->prev != OOO_SEGMENT_INVALID_INDEX) |
| 226 | { |
| 227 | prev = pool_elt_at_index (f->ooo_segments, s->prev); |
| 228 | |
| 229 | /* New segment merges prev and current. Remove previous and |
| 230 | * update position of current. */ |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 231 | if (offset_leq (offset, ooo_segment_end_offset (f, prev))) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 232 | { |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 233 | s->start = prev->start; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 234 | s->length = s_eof - ooo_segment_offset (f, prev); |
| 235 | ooo_segment_del (f, s->prev); |
| 236 | } |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 237 | else |
| 238 | { |
| 239 | s->start = offset; |
| 240 | s->length = s_eof - ooo_segment_offset (f, s); |
| 241 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 242 | } |
| 243 | else |
| 244 | { |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 245 | s->start = offset; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 246 | s->length = s_eof - ooo_segment_offset (f, s); |
| 247 | } |
| 248 | |
| 249 | /* The new segment's tail may cover multiple smaller ones */ |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 250 | if (offset_lt (s_eof, end_offset)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 251 | { |
| 252 | /* Remove segments completely covered */ |
| 253 | it = (s->next != OOO_SEGMENT_INVALID_INDEX) ? |
| 254 | pool_elt_at_index (f->ooo_segments, s->next) : 0; |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 255 | while (it && offset_lt (ooo_segment_end_offset (f, it), end_offset)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 256 | { |
| 257 | next = (it->next != OOO_SEGMENT_INVALID_INDEX) ? |
| 258 | pool_elt_at_index (f->ooo_segments, it->next) : 0; |
| 259 | ooo_segment_del (f, it - f->ooo_segments); |
| 260 | it = next; |
| 261 | } |
| 262 | |
| 263 | /* Update length. Segment's start might have changed. */ |
| 264 | s->length = end_offset - ooo_segment_offset (f, s); |
| 265 | |
| 266 | /* If partial overlap with last, merge */ |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 267 | if (it && offset_lt (ooo_segment_offset (f, it), end_offset)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 268 | { |
| 269 | s->length += |
| 270 | it->length - (ooo_segment_offset (f, it) - end_offset); |
| 271 | ooo_segment_del (f, it - f->ooo_segments); |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | /* Last but overlapping previous */ |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 276 | else if (offset_leq (s_eof, end_offset)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 277 | { |
| 278 | s->length = end_offset - ooo_segment_offset (f, s); |
| 279 | } |
| 280 | /* New segment completely covered by current one */ |
| 281 | else |
| 282 | { |
| 283 | /* Do Nothing */ |
| 284 | } |
| 285 | |
| 286 | /* Most recently updated segment */ |
| 287 | f->ooos_newest = s - f->ooo_segments; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Removes segments that can now be enqueued because the fifo's tail has |
| 292 | * advanced. Returns the number of bytes added to tail. |
| 293 | */ |
| 294 | static int |
| 295 | ooo_segment_try_collect (svm_fifo_t * f, u32 n_bytes_enqueued) |
| 296 | { |
| 297 | ooo_segment_t *s; |
| 298 | u32 index, bytes = 0, diff; |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 299 | u32 cursize; |
| 300 | |
Florin Coras | b59a705 | 2017-04-18 22:07:29 -0700 | [diff] [blame] | 301 | /* current size has not yet been updated */ |
| 302 | cursize = svm_fifo_max_dequeue (f) + n_bytes_enqueued; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 303 | |
| 304 | s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head); |
| 305 | |
Florin Coras | b59a705 | 2017-04-18 22:07:29 -0700 | [diff] [blame] | 306 | diff = (f->nitems + (i32) (f->tail - s->start)) % f->nitems; |
| 307 | if (diff > cursize) |
| 308 | return 0; |
| 309 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 310 | /* If last tail update overlaps one/multiple ooo segments, remove them */ |
Florin Coras | b59a705 | 2017-04-18 22:07:29 -0700 | [diff] [blame] | 311 | while (0 < diff && diff < cursize) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 312 | { |
Florin Coras | b59a705 | 2017-04-18 22:07:29 -0700 | [diff] [blame] | 313 | index = s - f->ooo_segments; |
| 314 | |
| 315 | /* Segment end is beyond the tail. Advance tail and remove segment */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 316 | if (diff < s->length) |
| 317 | { |
| 318 | f->tail += s->length - diff; |
| 319 | f->tail %= f->nitems; |
Florin Coras | b59a705 | 2017-04-18 22:07:29 -0700 | [diff] [blame] | 320 | bytes = s->length - diff; |
| 321 | ooo_segment_del (f, index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 322 | break; |
| 323 | } |
Florin Coras | b59a705 | 2017-04-18 22:07:29 -0700 | [diff] [blame] | 324 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 325 | /* If we have next go on */ |
Florin Coras | b59a705 | 2017-04-18 22:07:29 -0700 | [diff] [blame] | 326 | if (s->next != OOO_SEGMENT_INVALID_INDEX) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 327 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 328 | s = pool_elt_at_index (f->ooo_segments, s->next); |
Florin Coras | b59a705 | 2017-04-18 22:07:29 -0700 | [diff] [blame] | 329 | diff = (f->nitems + (i32) (f->tail - s->start)) % f->nitems; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 330 | ooo_segment_del (f, index); |
| 331 | } |
| 332 | /* End of search */ |
| 333 | else |
| 334 | { |
Florin Coras | b59a705 | 2017-04-18 22:07:29 -0700 | [diff] [blame] | 335 | ooo_segment_del (f, index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 336 | break; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | /* If tail is adjacent to an ooo segment, 'consume' it */ |
| 341 | if (diff == 0) |
| 342 | { |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 343 | bytes = ((f->nitems - cursize) >= s->length) ? s->length : |
| 344 | f->nitems - cursize; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 345 | |
| 346 | f->tail += bytes; |
| 347 | f->tail %= f->nitems; |
| 348 | |
| 349 | ooo_segment_del (f, s - f->ooo_segments); |
| 350 | } |
| 351 | |
| 352 | return bytes; |
| 353 | } |
| 354 | |
| 355 | static int |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 356 | svm_fifo_enqueue_internal (svm_fifo_t * f, u32 max_bytes, u8 * copy_from_here) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 357 | { |
| 358 | u32 total_copy_bytes, first_copy_bytes, second_copy_bytes; |
| 359 | u32 cursize, nitems; |
| 360 | |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 361 | /* read cursize, which can only increase while we're working */ |
| 362 | cursize = svm_fifo_max_dequeue (f); |
| 363 | |
| 364 | if (PREDICT_FALSE (cursize == f->nitems)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 365 | return -2; /* fifo stuffed */ |
| 366 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 367 | nitems = f->nitems; |
| 368 | |
| 369 | /* Number of bytes we're going to copy */ |
| 370 | total_copy_bytes = (nitems - cursize) < max_bytes ? |
| 371 | (nitems - cursize) : max_bytes; |
| 372 | |
| 373 | if (PREDICT_TRUE (copy_from_here != 0)) |
| 374 | { |
| 375 | /* Number of bytes in first copy segment */ |
| 376 | first_copy_bytes = ((nitems - f->tail) < total_copy_bytes) |
| 377 | ? (nitems - f->tail) : total_copy_bytes; |
| 378 | |
| 379 | clib_memcpy (&f->data[f->tail], copy_from_here, first_copy_bytes); |
| 380 | f->tail += first_copy_bytes; |
| 381 | f->tail = (f->tail == nitems) ? 0 : f->tail; |
| 382 | |
| 383 | /* Number of bytes in second copy segment, if any */ |
| 384 | second_copy_bytes = total_copy_bytes - first_copy_bytes; |
| 385 | if (second_copy_bytes) |
| 386 | { |
| 387 | clib_memcpy (&f->data[f->tail], copy_from_here + first_copy_bytes, |
| 388 | second_copy_bytes); |
| 389 | f->tail += second_copy_bytes; |
| 390 | f->tail = (f->tail == nitems) ? 0 : f->tail; |
| 391 | } |
| 392 | } |
| 393 | else |
| 394 | { |
| 395 | /* Account for a zero-copy enqueue done elsewhere */ |
| 396 | ASSERT (max_bytes <= (nitems - cursize)); |
| 397 | f->tail += max_bytes; |
| 398 | f->tail = f->tail % nitems; |
| 399 | total_copy_bytes = max_bytes; |
| 400 | } |
| 401 | |
| 402 | /* Any out-of-order segments to collect? */ |
| 403 | if (PREDICT_FALSE (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX)) |
| 404 | total_copy_bytes += ooo_segment_try_collect (f, total_copy_bytes); |
| 405 | |
| 406 | /* Atomically increase the queue length */ |
| 407 | __sync_fetch_and_add (&f->cursize, total_copy_bytes); |
| 408 | |
| 409 | return (total_copy_bytes); |
| 410 | } |
| 411 | |
| 412 | int |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 413 | svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_from_here) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 414 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 415 | return svm_fifo_enqueue_internal (f, max_bytes, copy_from_here); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 416 | } |
| 417 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 418 | /** |
| 419 | * Enqueue a future segment. |
| 420 | * |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 421 | * Two choices: either copies the entire segment, or copies nothing |
| 422 | * Returns 0 of the entire segment was copied |
| 423 | * Returns -1 if none of the segment was copied due to lack of space |
| 424 | */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 425 | static int |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 426 | svm_fifo_enqueue_with_offset_internal (svm_fifo_t * f, |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 427 | u32 offset, |
| 428 | u32 required_bytes, |
| 429 | u8 * copy_from_here) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 430 | { |
| 431 | u32 total_copy_bytes, first_copy_bytes, second_copy_bytes; |
| 432 | u32 cursize, nitems; |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 433 | u32 normalized_offset; |
| 434 | int rv; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 435 | |
Florin Coras | b59a705 | 2017-04-18 22:07:29 -0700 | [diff] [blame] | 436 | /* Users would do well to avoid this */ |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 437 | if (PREDICT_FALSE (f->tail == (offset % f->nitems))) |
| 438 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 439 | rv = svm_fifo_enqueue_internal (f, required_bytes, copy_from_here); |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 440 | if (rv > 0) |
| 441 | return 0; |
| 442 | return -1; |
| 443 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 444 | |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 445 | /* read cursize, which can only increase while we're working */ |
| 446 | cursize = svm_fifo_max_dequeue (f); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 447 | nitems = f->nitems; |
| 448 | |
| 449 | /* Will this request fit? */ |
Florin Coras | b59a705 | 2017-04-18 22:07:29 -0700 | [diff] [blame] | 450 | if ((required_bytes + (offset - f->tail) % nitems) > (nitems - cursize)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 451 | return -1; |
| 452 | |
| 453 | ooo_segment_add (f, offset, required_bytes); |
| 454 | |
| 455 | /* Number of bytes we're going to copy */ |
| 456 | total_copy_bytes = required_bytes; |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 457 | normalized_offset = offset % nitems; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 458 | |
| 459 | /* Number of bytes in first copy segment */ |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 460 | first_copy_bytes = ((nitems - normalized_offset) < total_copy_bytes) |
| 461 | ? (nitems - normalized_offset) : total_copy_bytes; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 462 | |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 463 | clib_memcpy (&f->data[normalized_offset], copy_from_here, first_copy_bytes); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 464 | |
| 465 | /* Number of bytes in second copy segment, if any */ |
| 466 | second_copy_bytes = total_copy_bytes - first_copy_bytes; |
| 467 | if (second_copy_bytes) |
| 468 | { |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 469 | normalized_offset += first_copy_bytes; |
| 470 | normalized_offset %= nitems; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 471 | |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 472 | ASSERT (normalized_offset == 0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 473 | |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 474 | clib_memcpy (&f->data[normalized_offset], |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 475 | copy_from_here + first_copy_bytes, second_copy_bytes); |
| 476 | } |
| 477 | |
| 478 | return (0); |
| 479 | } |
| 480 | |
| 481 | |
| 482 | int |
| 483 | svm_fifo_enqueue_with_offset (svm_fifo_t * f, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 484 | u32 offset, |
| 485 | u32 required_bytes, u8 * copy_from_here) |
| 486 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 487 | return svm_fifo_enqueue_with_offset_internal (f, offset, required_bytes, |
| 488 | copy_from_here); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | |
| 492 | static int |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 493 | 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] | 494 | { |
| 495 | u32 total_copy_bytes, first_copy_bytes, second_copy_bytes; |
| 496 | u32 cursize, nitems; |
| 497 | |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 498 | /* read cursize, which can only increase while we're working */ |
| 499 | cursize = svm_fifo_max_dequeue (f); |
| 500 | if (PREDICT_FALSE (cursize == 0)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 501 | return -2; /* nothing in the fifo */ |
| 502 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 503 | nitems = f->nitems; |
| 504 | |
| 505 | /* Number of bytes we're going to copy */ |
| 506 | total_copy_bytes = (cursize < max_bytes) ? cursize : max_bytes; |
| 507 | |
| 508 | if (PREDICT_TRUE (copy_here != 0)) |
| 509 | { |
| 510 | /* Number of bytes in first copy segment */ |
| 511 | first_copy_bytes = ((nitems - f->head) < total_copy_bytes) |
| 512 | ? (nitems - f->head) : total_copy_bytes; |
| 513 | clib_memcpy (copy_here, &f->data[f->head], first_copy_bytes); |
| 514 | f->head += first_copy_bytes; |
| 515 | f->head = (f->head == nitems) ? 0 : f->head; |
| 516 | |
| 517 | /* Number of bytes in second copy segment, if any */ |
| 518 | second_copy_bytes = total_copy_bytes - first_copy_bytes; |
| 519 | if (second_copy_bytes) |
| 520 | { |
| 521 | clib_memcpy (copy_here + first_copy_bytes, |
| 522 | &f->data[f->head], second_copy_bytes); |
| 523 | f->head += second_copy_bytes; |
| 524 | f->head = (f->head == nitems) ? 0 : f->head; |
| 525 | } |
| 526 | } |
| 527 | else |
| 528 | { |
| 529 | /* Account for a zero-copy dequeue done elsewhere */ |
| 530 | ASSERT (max_bytes <= cursize); |
| 531 | f->head += max_bytes; |
| 532 | f->head = f->head % nitems; |
| 533 | cursize -= max_bytes; |
| 534 | total_copy_bytes = max_bytes; |
| 535 | } |
| 536 | |
| 537 | __sync_fetch_and_sub (&f->cursize, total_copy_bytes); |
| 538 | |
| 539 | return (total_copy_bytes); |
| 540 | } |
| 541 | |
| 542 | int |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 543 | 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] | 544 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 545 | return svm_fifo_dequeue_internal (f, max_bytes, copy_here); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | int |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 549 | svm_fifo_peek (svm_fifo_t * f, u32 relative_offset, u32 max_bytes, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 550 | u8 * copy_here) |
| 551 | { |
| 552 | u32 total_copy_bytes, first_copy_bytes, second_copy_bytes; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 553 | u32 cursize, nitems, real_head; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 554 | |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 555 | /* read cursize, which can only increase while we're working */ |
| 556 | cursize = svm_fifo_max_dequeue (f); |
| 557 | if (PREDICT_FALSE (cursize == 0)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 558 | return -2; /* nothing in the fifo */ |
| 559 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 560 | nitems = f->nitems; |
Florin Coras | b59a705 | 2017-04-18 22:07:29 -0700 | [diff] [blame] | 561 | real_head = f->head + relative_offset; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 562 | real_head = real_head >= nitems ? real_head - nitems : real_head; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 563 | |
| 564 | /* Number of bytes we're going to copy */ |
| 565 | total_copy_bytes = (cursize < max_bytes) ? cursize : max_bytes; |
| 566 | |
| 567 | if (PREDICT_TRUE (copy_here != 0)) |
| 568 | { |
| 569 | /* Number of bytes in first copy segment */ |
| 570 | first_copy_bytes = |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 571 | ((nitems - real_head) < total_copy_bytes) ? |
| 572 | (nitems - real_head) : total_copy_bytes; |
| 573 | clib_memcpy (copy_here, &f->data[real_head], first_copy_bytes); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 574 | |
| 575 | /* Number of bytes in second copy segment, if any */ |
| 576 | second_copy_bytes = total_copy_bytes - first_copy_bytes; |
| 577 | if (second_copy_bytes) |
| 578 | { |
| 579 | clib_memcpy (copy_here + first_copy_bytes, &f->data[0], |
| 580 | second_copy_bytes); |
| 581 | } |
| 582 | } |
| 583 | return total_copy_bytes; |
| 584 | } |
| 585 | |
| 586 | int |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 587 | svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 588 | { |
| 589 | u32 total_drop_bytes, first_drop_bytes, second_drop_bytes; |
| 590 | u32 cursize, nitems; |
| 591 | |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 592 | /* read cursize, which can only increase while we're working */ |
| 593 | cursize = svm_fifo_max_dequeue (f); |
| 594 | if (PREDICT_FALSE (cursize == 0)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 595 | return -2; /* nothing in the fifo */ |
| 596 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 597 | nitems = f->nitems; |
| 598 | |
| 599 | /* Number of bytes we're going to drop */ |
| 600 | total_drop_bytes = (cursize < max_bytes) ? cursize : max_bytes; |
| 601 | |
| 602 | /* Number of bytes in first copy segment */ |
| 603 | first_drop_bytes = |
| 604 | ((nitems - f->head) < total_drop_bytes) ? |
| 605 | (nitems - f->head) : total_drop_bytes; |
| 606 | f->head += first_drop_bytes; |
| 607 | f->head = (f->head == nitems) ? 0 : f->head; |
| 608 | |
| 609 | /* Number of bytes in second drop segment, if any */ |
| 610 | second_drop_bytes = total_drop_bytes - first_drop_bytes; |
| 611 | if (second_drop_bytes) |
| 612 | { |
| 613 | f->head += second_drop_bytes; |
| 614 | f->head = (f->head == nitems) ? 0 : f->head; |
| 615 | } |
| 616 | |
| 617 | __sync_fetch_and_sub (&f->cursize, total_drop_bytes); |
| 618 | |
| 619 | return total_drop_bytes; |
| 620 | } |
| 621 | |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 622 | u32 |
| 623 | svm_fifo_number_ooo_segments (svm_fifo_t * f) |
| 624 | { |
| 625 | return pool_elts (f->ooo_segments); |
| 626 | } |
| 627 | |
| 628 | ooo_segment_t * |
| 629 | svm_fifo_first_ooo_segment (svm_fifo_t * f) |
| 630 | { |
| 631 | return pool_elt_at_index (f->ooo_segments, f->ooos_list_head); |
| 632 | } |
| 633 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 634 | /* |
| 635 | * fd.io coding-style-patch-verification: ON |
| 636 | * |
| 637 | * Local Variables: |
| 638 | * eval: (c-set-style "gnu") |
| 639 | * End: |
| 640 | */ |