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