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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <svm/svm_fifo.h>
18 #define offset_lt(_a, _b) ((i32)((_a)-(_b)) < 0)
19 #define offset_leq(_a, _b) ((i32)((_a)-(_b)) <= 0)
22 format_ooo_segment (u8 * s, va_list * args)
24 ooo_segment_t *seg = va_arg (*args, ooo_segment_t *);
26 s = format (s, "pos %u, len %u, next %d, prev %d",
27 seg->start, seg->length, seg->next, seg->prev);
32 format_ooo_list (u8 * s, va_list * args)
34 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
35 u32 ooo_segment_index = f->ooos_list_head;
38 while (ooo_segment_index != OOO_SEGMENT_INVALID_INDEX)
40 seg = pool_elt_at_index (f->ooo_segments, ooo_segment_index);
41 s = format (s, " %U\n", format_ooo_segment, seg);
42 ooo_segment_index = seg->next;
48 format_svm_fifo (u8 * s, va_list * args)
50 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
51 int verbose = va_arg (*args, int);
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);
59 (s, "server session %d thread %d client session %d thread %d\n",
60 f->server_session_index, f->server_thread_index,
61 f->client_session_index, f->client_thread_index);
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);
72 /** create an svm fifo, in the current heap. Fails vs blow up the process */
74 svm_fifo_create (u32 data_size_in_bytes)
78 f = clib_mem_alloc_aligned_or_null (sizeof (*f) + data_size_in_bytes,
79 CLIB_CACHE_LINE_BYTES);
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;
91 svm_fifo_free (svm_fifo_t * f)
93 pool_free (f->ooo_segments);
97 always_inline ooo_segment_t *
98 ooo_segment_new (svm_fifo_t * f, u32 start, u32 length)
102 pool_get (f->ooo_segments, s);
107 s->prev = s->next = OOO_SEGMENT_INVALID_INDEX;
113 ooo_segment_del (svm_fifo_t * f, u32 index)
115 ooo_segment_t *cur, *prev = 0, *next = 0;
116 cur = pool_elt_at_index (f->ooo_segments, index);
118 if (cur->next != OOO_SEGMENT_INVALID_INDEX)
120 next = pool_elt_at_index (f->ooo_segments, cur->next);
121 next->prev = cur->prev;
124 if (cur->prev != OOO_SEGMENT_INVALID_INDEX)
126 prev = pool_elt_at_index (f->ooo_segments, cur->prev);
127 prev->next = cur->next;
131 f->ooos_list_head = cur->next;
134 pool_put (f->ooo_segments, cur);
138 * Add segment to fifo's out-of-order segment list. Takes care of merging
139 * adjacent segments and removing overlapping ones.
142 ooo_segment_add (svm_fifo_t * f, u32 offset, u32 length)
144 ooo_segment_t *s, *new_s, *prev, *next, *it;
145 u32 new_index, end_offset, s_sof, s_eof, s_index;
147 end_offset = offset + length;
149 if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX)
151 s = ooo_segment_new (f, offset, length);
152 f->ooos_list_head = s - f->ooo_segments;
153 f->ooos_newest = f->ooos_list_head;
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
160 && offset_leq (ooo_segment_offset (f, s), offset))
161 s = pool_elt_at_index (f->ooo_segments, s->next);
163 s_index = s - f->ooo_segments;
164 s_sof = ooo_segment_offset (f, s);
165 s_eof = ooo_segment_end_offset (f, s);
166 prev = ooo_segment_get_prev (f, s);
168 /* No overlap, add before current segment */
169 if (offset_lt (end_offset, s_sof)
170 && (!prev || offset_lt (prev->start + prev->length, offset)))
172 new_s = ooo_segment_new (f, offset, length);
173 new_index = new_s - f->ooo_segments;
175 /* Pool might've moved, get segment again */
176 s = pool_elt_at_index (f->ooo_segments, s_index);
177 if (s->prev != OOO_SEGMENT_INVALID_INDEX)
179 new_s->prev = s->prev;
180 prev = pool_elt_at_index (f->ooo_segments, new_s->prev);
181 prev->next = new_index;
186 f->ooos_list_head = new_index;
189 new_s->next = s - f->ooo_segments;
191 f->ooos_newest = new_index;
194 /* No overlap, add after current segment */
195 else if (offset_lt (s_eof, offset))
197 new_s = ooo_segment_new (f, offset, length);
198 new_index = new_s - f->ooo_segments;
200 /* Pool might've moved, get segment again */
201 s = pool_elt_at_index (f->ooo_segments, s_index);
203 if (s->next != OOO_SEGMENT_INVALID_INDEX)
205 new_s->next = s->next;
206 next = pool_elt_at_index (f->ooo_segments, new_s->next);
207 next->prev = new_index;
210 new_s->prev = s - f->ooo_segments;
212 f->ooos_newest = new_index;
222 if (offset_leq (offset, s_sof))
224 /* If we have a previous, check if we overlap */
225 if (s->prev != OOO_SEGMENT_INVALID_INDEX)
227 prev = pool_elt_at_index (f->ooo_segments, s->prev);
229 /* New segment merges prev and current. Remove previous and
230 * update position of current. */
231 if (offset_leq (offset, ooo_segment_end_offset (f, prev)))
233 s->start = prev->start;
234 s->length = s_eof - ooo_segment_offset (f, prev);
235 ooo_segment_del (f, s->prev);
240 s->length = s_eof - ooo_segment_offset (f, s);
246 s->length = s_eof - ooo_segment_offset (f, s);
249 /* The new segment's tail may cover multiple smaller ones */
250 if (offset_lt (s_eof, end_offset))
252 /* Remove segments completely covered */
253 it = (s->next != OOO_SEGMENT_INVALID_INDEX) ?
254 pool_elt_at_index (f->ooo_segments, s->next) : 0;
255 while (it && offset_lt (ooo_segment_end_offset (f, it), end_offset))
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);
263 /* Update length. Segment's start might have changed. */
264 s->length = end_offset - ooo_segment_offset (f, s);
266 /* If partial overlap with last, merge */
267 if (it && offset_lt (ooo_segment_offset (f, it), end_offset))
270 it->length - (ooo_segment_offset (f, it) - end_offset);
271 ooo_segment_del (f, it - f->ooo_segments);
275 /* Last but overlapping previous */
276 else if (offset_leq (s_eof, end_offset))
278 s->length = end_offset - ooo_segment_offset (f, s);
280 /* New segment completely covered by current one */
286 /* Most recently updated segment */
287 f->ooos_newest = s - f->ooo_segments;
291 * Removes segments that can now be enqueued because the fifo's tail has
292 * advanced. Returns the number of bytes added to tail.
295 ooo_segment_try_collect (svm_fifo_t * f, u32 n_bytes_enqueued)
298 u32 index, bytes = 0, diff;
301 /* current size has not yet been updated */
302 cursize = svm_fifo_max_dequeue (f) + n_bytes_enqueued;
304 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
306 diff = (f->nitems + (i32) (f->tail - s->start)) % f->nitems;
310 /* If last tail update overlaps one/multiple ooo segments, remove them */
311 while (0 < diff && diff < cursize)
313 index = s - f->ooo_segments;
315 /* Segment end is beyond the tail. Advance tail and remove segment */
316 if (diff < s->length)
318 f->tail += s->length - diff;
319 f->tail %= f->nitems;
320 bytes = s->length - diff;
321 ooo_segment_del (f, index);
325 /* If we have next go on */
326 if (s->next != OOO_SEGMENT_INVALID_INDEX)
328 s = pool_elt_at_index (f->ooo_segments, s->next);
329 diff = (f->nitems + (i32) (f->tail - s->start)) % f->nitems;
330 ooo_segment_del (f, index);
335 ooo_segment_del (f, index);
340 /* If tail is adjacent to an ooo segment, 'consume' it */
343 bytes = ((f->nitems - cursize) >= s->length) ? s->length :
347 f->tail %= f->nitems;
349 ooo_segment_del (f, s - f->ooo_segments);
356 svm_fifo_enqueue_internal (svm_fifo_t * f,
357 int pid, u32 max_bytes, u8 * copy_from_here)
359 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
362 /* read cursize, which can only increase while we're working */
363 cursize = svm_fifo_max_dequeue (f);
365 if (PREDICT_FALSE (cursize == f->nitems))
366 return -2; /* fifo stuffed */
370 /* Number of bytes we're going to copy */
371 total_copy_bytes = (nitems - cursize) < max_bytes ?
372 (nitems - cursize) : max_bytes;
374 if (PREDICT_TRUE (copy_from_here != 0))
376 /* Number of bytes in first copy segment */
377 first_copy_bytes = ((nitems - f->tail) < total_copy_bytes)
378 ? (nitems - f->tail) : total_copy_bytes;
380 clib_memcpy (&f->data[f->tail], copy_from_here, first_copy_bytes);
381 f->tail += first_copy_bytes;
382 f->tail = (f->tail == nitems) ? 0 : f->tail;
384 /* Number of bytes in second copy segment, if any */
385 second_copy_bytes = total_copy_bytes - first_copy_bytes;
386 if (second_copy_bytes)
388 clib_memcpy (&f->data[f->tail], copy_from_here + first_copy_bytes,
390 f->tail += second_copy_bytes;
391 f->tail = (f->tail == nitems) ? 0 : f->tail;
396 /* Account for a zero-copy enqueue done elsewhere */
397 ASSERT (max_bytes <= (nitems - cursize));
398 f->tail += max_bytes;
399 f->tail = f->tail % nitems;
400 total_copy_bytes = max_bytes;
403 /* Any out-of-order segments to collect? */
404 if (PREDICT_FALSE (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX))
405 total_copy_bytes += ooo_segment_try_collect (f, total_copy_bytes);
407 /* Atomically increase the queue length */
408 __sync_fetch_and_add (&f->cursize, total_copy_bytes);
410 return (total_copy_bytes);
414 svm_fifo_enqueue_nowait (svm_fifo_t * f,
415 int pid, u32 max_bytes, u8 * copy_from_here)
417 return svm_fifo_enqueue_internal (f, pid, max_bytes, copy_from_here);
421 * Enqueue a future segment.
423 * Two choices: either copies the entire segment, or copies nothing
424 * Returns 0 of the entire segment was copied
425 * Returns -1 if none of the segment was copied due to lack of space
428 svm_fifo_enqueue_with_offset_internal (svm_fifo_t * f,
434 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
436 u32 normalized_offset;
439 /* Users would do well to avoid this */
440 if (PREDICT_FALSE (f->tail == (offset % f->nitems)))
442 rv = svm_fifo_enqueue_internal (f, pid, required_bytes, copy_from_here);
448 /* read cursize, which can only increase while we're working */
449 cursize = svm_fifo_max_dequeue (f);
452 /* Will this request fit? */
453 if ((required_bytes + (offset - f->tail) % nitems) > (nitems - cursize))
456 ooo_segment_add (f, offset, required_bytes);
458 /* Number of bytes we're going to copy */
459 total_copy_bytes = required_bytes;
460 normalized_offset = offset % nitems;
462 /* Number of bytes in first copy segment */
463 first_copy_bytes = ((nitems - normalized_offset) < total_copy_bytes)
464 ? (nitems - normalized_offset) : total_copy_bytes;
466 clib_memcpy (&f->data[normalized_offset], copy_from_here, first_copy_bytes);
468 /* Number of bytes in second copy segment, if any */
469 second_copy_bytes = total_copy_bytes - first_copy_bytes;
470 if (second_copy_bytes)
472 normalized_offset += first_copy_bytes;
473 normalized_offset %= nitems;
475 ASSERT (normalized_offset == 0);
477 clib_memcpy (&f->data[normalized_offset],
478 copy_from_here + first_copy_bytes, second_copy_bytes);
486 svm_fifo_enqueue_with_offset (svm_fifo_t * f,
489 u32 required_bytes, u8 * copy_from_here)
491 return svm_fifo_enqueue_with_offset_internal
492 (f, pid, offset, required_bytes, copy_from_here);
497 svm_fifo_dequeue_internal (svm_fifo_t * f,
498 int pid, u32 max_bytes, u8 * copy_here)
500 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
503 /* read cursize, which can only increase while we're working */
504 cursize = svm_fifo_max_dequeue (f);
505 if (PREDICT_FALSE (cursize == 0))
506 return -2; /* nothing in the fifo */
510 /* Number of bytes we're going to copy */
511 total_copy_bytes = (cursize < max_bytes) ? cursize : max_bytes;
513 if (PREDICT_TRUE (copy_here != 0))
515 /* Number of bytes in first copy segment */
516 first_copy_bytes = ((nitems - f->head) < total_copy_bytes)
517 ? (nitems - f->head) : total_copy_bytes;
518 clib_memcpy (copy_here, &f->data[f->head], first_copy_bytes);
519 f->head += first_copy_bytes;
520 f->head = (f->head == nitems) ? 0 : f->head;
522 /* Number of bytes in second copy segment, if any */
523 second_copy_bytes = total_copy_bytes - first_copy_bytes;
524 if (second_copy_bytes)
526 clib_memcpy (copy_here + first_copy_bytes,
527 &f->data[f->head], second_copy_bytes);
528 f->head += second_copy_bytes;
529 f->head = (f->head == nitems) ? 0 : f->head;
534 /* Account for a zero-copy dequeue done elsewhere */
535 ASSERT (max_bytes <= cursize);
536 f->head += max_bytes;
537 f->head = f->head % nitems;
538 cursize -= max_bytes;
539 total_copy_bytes = max_bytes;
542 __sync_fetch_and_sub (&f->cursize, total_copy_bytes);
544 return (total_copy_bytes);
548 svm_fifo_dequeue_nowait (svm_fifo_t * f,
549 int pid, u32 max_bytes, u8 * copy_here)
551 return svm_fifo_dequeue_internal (f, pid, max_bytes, copy_here);
555 svm_fifo_peek (svm_fifo_t * f, int pid, u32 relative_offset, u32 max_bytes,
558 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
559 u32 cursize, nitems, real_head;
561 /* read cursize, which can only increase while we're working */
562 cursize = svm_fifo_max_dequeue (f);
563 if (PREDICT_FALSE (cursize == 0))
564 return -2; /* nothing in the fifo */
567 real_head = f->head + relative_offset;
568 real_head = real_head >= nitems ? real_head - nitems : real_head;
570 /* Number of bytes we're going to copy */
571 total_copy_bytes = (cursize < max_bytes) ? cursize : max_bytes;
573 if (PREDICT_TRUE (copy_here != 0))
575 /* Number of bytes in first copy segment */
577 ((nitems - real_head) < total_copy_bytes) ?
578 (nitems - real_head) : total_copy_bytes;
579 clib_memcpy (copy_here, &f->data[real_head], first_copy_bytes);
581 /* Number of bytes in second copy segment, if any */
582 second_copy_bytes = total_copy_bytes - first_copy_bytes;
583 if (second_copy_bytes)
585 clib_memcpy (copy_here + first_copy_bytes, &f->data[0],
589 return total_copy_bytes;
593 svm_fifo_dequeue_drop (svm_fifo_t * f, int pid, u32 max_bytes)
595 u32 total_drop_bytes, first_drop_bytes, second_drop_bytes;
598 /* read cursize, which can only increase while we're working */
599 cursize = svm_fifo_max_dequeue (f);
600 if (PREDICT_FALSE (cursize == 0))
601 return -2; /* nothing in the fifo */
605 /* Number of bytes we're going to drop */
606 total_drop_bytes = (cursize < max_bytes) ? cursize : max_bytes;
608 /* Number of bytes in first copy segment */
610 ((nitems - f->head) < total_drop_bytes) ?
611 (nitems - f->head) : total_drop_bytes;
612 f->head += first_drop_bytes;
613 f->head = (f->head == nitems) ? 0 : f->head;
615 /* Number of bytes in second drop segment, if any */
616 second_drop_bytes = total_drop_bytes - first_drop_bytes;
617 if (second_drop_bytes)
619 f->head += second_drop_bytes;
620 f->head = (f->head == nitems) ? 0 : f->head;
623 __sync_fetch_and_sub (&f->cursize, total_drop_bytes);
625 return total_drop_bytes;
629 svm_fifo_number_ooo_segments (svm_fifo_t * f)
631 return pool_elts (f->ooo_segments);
635 svm_fifo_first_ooo_segment (svm_fifo_t * f)
637 return pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
641 * fd.io coding-style-patch-verification: ON
644 * eval: (c-set-style "gnu")