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>
17 #include <vppinfra/cpu.h>
20 position_lt (svm_fifo_t * f, u32 a, u32 b)
22 return (ooo_segment_distance_from_tail (f, a)
23 < ooo_segment_distance_from_tail (f, b));
27 position_leq (svm_fifo_t * f, u32 a, u32 b)
29 return (ooo_segment_distance_from_tail (f, a)
30 <= ooo_segment_distance_from_tail (f, b));
34 position_gt (svm_fifo_t * f, u32 a, u32 b)
36 return (ooo_segment_distance_from_tail (f, a)
37 > ooo_segment_distance_from_tail (f, b));
41 position_diff (svm_fifo_t * f, u32 posa, u32 posb)
43 return ooo_segment_distance_from_tail (f, posa)
44 - ooo_segment_distance_from_tail (f, posb);
48 ooo_segment_end_pos (svm_fifo_t * f, ooo_segment_t * s)
50 return (s->start + s->length) % f->nitems;
54 format_ooo_segment (u8 * s, va_list * args)
56 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
57 ooo_segment_t *seg = va_arg (*args, ooo_segment_t *);
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);
66 svm_fifo_dump_trace (u8 * s, svm_fifo_t * f)
69 svm_fifo_trace_elem_t *seg = 0;
74 vec_foreach (seg, f->trace)
76 s = format (s, "{%u, %u, %u}, ", seg->offset, seg->len, seg->action);
90 svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose)
94 svm_fifo_trace_elem_t *trace;
96 svm_fifo_t *dummy_fifo;
103 trace_len = vec_len (trace);
109 dummy_fifo = svm_fifo_create (f->nitems);
110 memset (f->data, 0xFF, f->nitems);
112 vec_validate (data, f->nitems);
113 for (i = 0; i < vec_len (data); i++)
116 for (i = 0; i < trace_len; i++)
118 offset = trace[i].offset;
119 if (trace[i].action == 1)
122 s = format (s, "adding [%u, %u]:", 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]);
128 else if (trace[i].action == 2)
131 s = format (s, "adding [%u, %u]:", 0, trace[i].len);
132 svm_fifo_enqueue_nowait (dummy_fifo, trace[i].len, &data[offset]);
137 s = format (s, "read: %u", trace[i].len);
138 svm_fifo_dequeue_drop (dummy_fifo, trace[i].len);
141 s = format (s, "%U", format_svm_fifo, dummy_fifo, 1);
144 s = format (s, "result: %U", format_svm_fifo, dummy_fifo, 1);
150 format_ooo_list (u8 * s, va_list * args)
152 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
153 u32 ooo_segment_index = f->ooos_list_head;
156 while (ooo_segment_index != OOO_SEGMENT_INVALID_INDEX)
158 seg = pool_elt_at_index (f->ooo_segments, ooo_segment_index);
159 s = format (s, " %U\n", format_ooo_segment, f, seg);
160 ooo_segment_index = seg->next;
167 format_svm_fifo (u8 * s, va_list * args)
169 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
170 int verbose = va_arg (*args, int);
172 s = format (s, "cursize %u nitems %u has_event %d\n",
173 f->cursize, f->nitems, f->has_event);
174 s = format (s, " head %d tail %d\n", f->head, f->tail);
178 (s, " server session %d thread %d client session %d thread %d\n",
179 f->master_session_index, f->master_thread_index,
180 f->client_session_index, f->client_thread_index);
184 s = format (s, " ooo pool %d active elts newest %u\n",
185 pool_elts (f->ooo_segments), f->ooos_newest);
186 if (svm_fifo_has_ooo_data (f))
187 s = format (s, " %U", format_ooo_list, f, verbose);
192 /** create an svm fifo, in the current heap. Fails vs blow up the process */
194 svm_fifo_create (u32 data_size_in_bytes)
197 u32 rounded_data_size;
199 /* always round fifo data size to the next highest power-of-two */
200 rounded_data_size = (1 << (max_log2 (data_size_in_bytes)));
201 f = clib_mem_alloc_aligned_or_null (sizeof (*f) + rounded_data_size,
202 CLIB_CACHE_LINE_BYTES);
206 memset (f, 0, sizeof (*f));
207 f->nitems = data_size_in_bytes;
208 f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
214 svm_fifo_free (svm_fifo_t * f)
216 ASSERT (f->refcnt > 0);
218 if (--f->refcnt == 0)
220 pool_free (f->ooo_segments);
225 always_inline ooo_segment_t *
226 ooo_segment_new (svm_fifo_t * f, u32 start, u32 length)
230 pool_get (f->ooo_segments, s);
235 s->prev = s->next = OOO_SEGMENT_INVALID_INDEX;
241 ooo_segment_del (svm_fifo_t * f, u32 index)
243 ooo_segment_t *cur, *prev = 0, *next = 0;
244 cur = pool_elt_at_index (f->ooo_segments, index);
246 if (cur->next != OOO_SEGMENT_INVALID_INDEX)
248 next = pool_elt_at_index (f->ooo_segments, cur->next);
249 next->prev = cur->prev;
252 if (cur->prev != OOO_SEGMENT_INVALID_INDEX)
254 prev = pool_elt_at_index (f->ooo_segments, cur->prev);
255 prev->next = cur->next;
259 f->ooos_list_head = cur->next;
262 pool_put (f->ooo_segments, cur);
266 * Add segment to fifo's out-of-order segment list. Takes care of merging
267 * adjacent segments and removing overlapping ones.
270 ooo_segment_add (svm_fifo_t * f, u32 offset, u32 length)
272 ooo_segment_t *s, *new_s, *prev, *next, *it;
273 u32 new_index, s_end_pos, s_index;
274 u32 normalized_position, normalized_end_position;
276 ASSERT (offset + length <= ooo_segment_distance_from_tail (f, f->head));
277 normalized_position = (f->tail + offset) % f->nitems;
278 normalized_end_position = (f->tail + offset + length) % f->nitems;
280 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
282 if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX)
284 s = ooo_segment_new (f, normalized_position, length);
285 f->ooos_list_head = s - f->ooo_segments;
286 f->ooos_newest = f->ooos_list_head;
290 /* Find first segment that starts after new segment */
291 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
292 while (s->next != OOO_SEGMENT_INVALID_INDEX
293 && position_lt (f, s->start, normalized_position))
294 s = pool_elt_at_index (f->ooo_segments, s->next);
296 /* If we have a previous and we overlap it, use it as starting point */
297 prev = ooo_segment_get_prev (f, s);
299 && position_leq (f, normalized_position, ooo_segment_end_pos (f, prev)))
302 s_end_pos = ooo_segment_end_pos (f, s);
304 /* Since we have previous, normalized start position cannot be smaller
305 * than prev->start. Check tail */
306 ASSERT (position_lt (f, s->start, normalized_position));
310 s_index = s - f->ooo_segments;
311 s_end_pos = ooo_segment_end_pos (f, s);
313 /* No overlap, add before current segment */
314 if (position_lt (f, normalized_end_position, s->start))
316 new_s = ooo_segment_new (f, normalized_position, length);
317 new_index = new_s - f->ooo_segments;
319 /* Pool might've moved, get segment again */
320 s = pool_elt_at_index (f->ooo_segments, s_index);
321 if (s->prev != OOO_SEGMENT_INVALID_INDEX)
323 new_s->prev = s->prev;
324 prev = pool_elt_at_index (f->ooo_segments, new_s->prev);
325 prev->next = new_index;
330 f->ooos_list_head = new_index;
333 new_s->next = s_index;
335 f->ooos_newest = new_index;
338 /* No overlap, add after current segment */
339 else if (position_gt (f, normalized_position, s_end_pos))
341 new_s = ooo_segment_new (f, normalized_position, length);
342 new_index = new_s - f->ooo_segments;
344 /* Pool might've moved, get segment again */
345 s = pool_elt_at_index (f->ooo_segments, s_index);
347 /* Needs to be last */
348 ASSERT (s->next == OOO_SEGMENT_INVALID_INDEX);
350 new_s->prev = s_index;
352 f->ooos_newest = new_index;
362 if (position_lt (f, normalized_position, s->start))
364 s->start = normalized_position;
365 s->length = position_diff (f, s_end_pos, s->start);
366 f->ooos_newest = s - f->ooo_segments;
371 /* Overlapping tail */
372 if (position_gt (f, normalized_end_position, s_end_pos))
374 s->length = position_diff (f, normalized_end_position, s->start);
376 /* Remove the completely overlapped segments in the tail */
377 it = ooo_segment_next (f, s);
378 while (it && position_leq (f, ooo_segment_end_pos (f, it),
379 normalized_end_position))
381 next = ooo_segment_next (f, it);
382 ooo_segment_del (f, it - f->ooo_segments);
386 /* If partial overlap with last, merge */
387 if (it && position_leq (f, it->start, normalized_end_position))
389 s->length = position_diff (f, ooo_segment_end_pos (f, it),
391 ooo_segment_del (f, it - f->ooo_segments);
393 f->ooos_newest = s - f->ooo_segments;
398 * Removes segments that can now be enqueued because the fifo's tail has
399 * advanced. Returns the number of bytes added to tail.
402 ooo_segment_try_collect (svm_fifo_t * f, u32 n_bytes_enqueued)
405 u32 index, bytes = 0;
408 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
409 diff = ooo_segment_distance_to_tail (f, s->start);
411 ASSERT (diff != n_bytes_enqueued);
413 if (diff > n_bytes_enqueued)
416 /* If last tail update overlaps one/multiple ooo segments, remove them */
417 while (0 <= diff && diff < n_bytes_enqueued)
419 index = s - f->ooo_segments;
421 /* Segment end is beyond the tail. Advance tail and remove segment */
422 if (s->length > diff)
424 bytes = s->length - diff;
426 f->tail %= f->nitems;
427 ooo_segment_del (f, index);
431 /* If we have next go on */
432 if (s->next != OOO_SEGMENT_INVALID_INDEX)
434 s = pool_elt_at_index (f->ooo_segments, s->next);
435 diff = ooo_segment_distance_to_tail (f, s->start);
436 ooo_segment_del (f, index);
441 ooo_segment_del (f, index);
446 ASSERT (bytes <= f->nitems);
451 svm_fifo_enqueue_internal (svm_fifo_t * f, u32 max_bytes,
452 const u8 * copy_from_here)
454 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
457 /* read cursize, which can only increase while we're working */
458 cursize = svm_fifo_max_dequeue (f);
459 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
461 if (PREDICT_FALSE (cursize == f->nitems))
462 return -2; /* fifo stuffed */
466 /* Number of bytes we're going to copy */
467 total_copy_bytes = (nitems - cursize) < max_bytes ?
468 (nitems - cursize) : max_bytes;
470 if (PREDICT_TRUE (copy_from_here != 0))
472 /* Number of bytes in first copy segment */
473 first_copy_bytes = ((nitems - f->tail) < total_copy_bytes)
474 ? (nitems - f->tail) : total_copy_bytes;
476 clib_memcpy (&f->data[f->tail], copy_from_here, first_copy_bytes);
477 f->tail += first_copy_bytes;
478 f->tail = (f->tail == nitems) ? 0 : f->tail;
480 /* Number of bytes in second copy segment, if any */
481 second_copy_bytes = total_copy_bytes - first_copy_bytes;
482 if (second_copy_bytes)
484 clib_memcpy (&f->data[f->tail], copy_from_here + first_copy_bytes,
486 f->tail += second_copy_bytes;
487 f->tail = (f->tail == nitems) ? 0 : f->tail;
494 /* Account for a zero-copy enqueue done elsewhere */
495 ASSERT (max_bytes <= (nitems - cursize));
496 f->tail += max_bytes;
497 f->tail = f->tail % nitems;
498 total_copy_bytes = max_bytes;
501 svm_fifo_trace_add (f, f->head, total_copy_bytes, 2);
503 /* Any out-of-order segments to collect? */
504 if (PREDICT_FALSE (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX))
505 total_copy_bytes += ooo_segment_try_collect (f, total_copy_bytes);
507 /* Atomically increase the queue length */
508 ASSERT (cursize + total_copy_bytes <= nitems);
509 __sync_fetch_and_add (&f->cursize, total_copy_bytes);
511 return (total_copy_bytes);
514 #define SVM_ENQUEUE_CLONE_TEMPLATE(arch, fn, tgt) \
516 __attribute__ ((flatten)) \
517 __attribute__ ((target (tgt))) \
519 fn ## _ ## arch ( svm_fifo_t * f, u32 max_bytes, u8 * copy_from_here) \
520 { return fn (f, max_bytes, copy_from_here);}
523 svm_fifo_enqueue_nowait_ma (svm_fifo_t * f, u32 max_bytes,
524 const u8 * copy_from_here)
526 return svm_fifo_enqueue_internal (f, max_bytes, copy_from_here);
529 foreach_march_variant (SVM_ENQUEUE_CLONE_TEMPLATE,
530 svm_fifo_enqueue_nowait_ma);
531 CLIB_MULTIARCH_SELECT_FN (svm_fifo_enqueue_nowait_ma);
534 svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 max_bytes,
535 const u8 * copy_from_here)
538 return svm_fifo_enqueue_nowait_ma (f, max_bytes, copy_from_here);
540 static int (*fp) (svm_fifo_t *, u32, const u8 *);
542 if (PREDICT_FALSE (fp == 0))
543 fp = (void *) svm_fifo_enqueue_nowait_ma_multiarch_select ();
545 return (*fp) (f, max_bytes, copy_from_here);
550 * Enqueue a future segment.
552 * Two choices: either copies the entire segment, or copies nothing
553 * Returns 0 of the entire segment was copied
554 * Returns -1 if none of the segment was copied due to lack of space
557 svm_fifo_enqueue_with_offset_internal (svm_fifo_t * f,
562 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
563 u32 cursize, nitems, normalized_offset;
565 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
567 /* read cursize, which can only increase while we're working */
568 cursize = svm_fifo_max_dequeue (f);
571 ASSERT (required_bytes < nitems);
573 normalized_offset = (f->tail + offset) % nitems;
575 /* Will this request fit? */
576 if ((required_bytes + offset) > (nitems - cursize))
579 svm_fifo_trace_add (f, offset, required_bytes, 1);
581 ooo_segment_add (f, offset, required_bytes);
583 /* Number of bytes we're going to copy */
584 total_copy_bytes = required_bytes;
586 /* Number of bytes in first copy segment */
587 first_copy_bytes = ((nitems - normalized_offset) < total_copy_bytes)
588 ? (nitems - normalized_offset) : total_copy_bytes;
590 clib_memcpy (&f->data[normalized_offset], copy_from_here, first_copy_bytes);
592 /* Number of bytes in second copy segment, if any */
593 second_copy_bytes = total_copy_bytes - first_copy_bytes;
594 if (second_copy_bytes)
596 normalized_offset += first_copy_bytes;
597 normalized_offset %= nitems;
599 ASSERT (normalized_offset == 0);
601 clib_memcpy (&f->data[normalized_offset],
602 copy_from_here + first_copy_bytes, second_copy_bytes);
610 svm_fifo_enqueue_with_offset (svm_fifo_t * f,
612 u32 required_bytes, u8 * copy_from_here)
614 return svm_fifo_enqueue_with_offset_internal (f, offset, required_bytes,
620 svm_fifo_dequeue_internal (svm_fifo_t * f, u32 max_bytes, u8 * copy_here)
622 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
625 /* read cursize, which can only increase while we're working */
626 cursize = svm_fifo_max_dequeue (f);
627 if (PREDICT_FALSE (cursize == 0))
628 return -2; /* nothing in the fifo */
632 /* Number of bytes we're going to copy */
633 total_copy_bytes = (cursize < max_bytes) ? cursize : max_bytes;
635 if (PREDICT_TRUE (copy_here != 0))
637 /* Number of bytes in first copy segment */
638 first_copy_bytes = ((nitems - f->head) < total_copy_bytes)
639 ? (nitems - f->head) : total_copy_bytes;
640 clib_memcpy (copy_here, &f->data[f->head], first_copy_bytes);
641 f->head += first_copy_bytes;
642 f->head = (f->head == nitems) ? 0 : f->head;
644 /* Number of bytes in second copy segment, if any */
645 second_copy_bytes = total_copy_bytes - first_copy_bytes;
646 if (second_copy_bytes)
648 clib_memcpy (copy_here + first_copy_bytes,
649 &f->data[f->head], second_copy_bytes);
650 f->head += second_copy_bytes;
651 f->head = (f->head == nitems) ? 0 : f->head;
657 /* Account for a zero-copy dequeue done elsewhere */
658 ASSERT (max_bytes <= cursize);
659 f->head += max_bytes;
660 f->head = f->head % nitems;
661 cursize -= max_bytes;
662 total_copy_bytes = max_bytes;
665 ASSERT (f->head <= nitems);
666 ASSERT (cursize >= total_copy_bytes);
667 __sync_fetch_and_sub (&f->cursize, total_copy_bytes);
669 return (total_copy_bytes);
673 svm_fifo_dequeue_nowait_ma (svm_fifo_t * f, u32 max_bytes, u8 * copy_here)
675 return svm_fifo_dequeue_internal (f, max_bytes, copy_here);
678 #define SVM_FIFO_DEQUEUE_CLONE_TEMPLATE(arch, fn, tgt) \
680 __attribute__ ((flatten)) \
681 __attribute__ ((target (tgt))) \
683 fn ## _ ## arch ( svm_fifo_t * f, u32 max_bytes, \
685 { return fn (f, max_bytes, copy_here);}
687 foreach_march_variant (SVM_FIFO_DEQUEUE_CLONE_TEMPLATE,
688 svm_fifo_dequeue_nowait_ma);
689 CLIB_MULTIARCH_SELECT_FN (svm_fifo_dequeue_nowait_ma);
692 svm_fifo_dequeue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_here)
695 return svm_fifo_dequeue_nowait_ma (f, max_bytes, copy_here);
697 static int (*fp) (svm_fifo_t *, u32, u8 *);
699 if (PREDICT_FALSE (fp == 0))
700 fp = (void *) svm_fifo_dequeue_nowait_ma_multiarch_select ();
702 return (*fp) (f, max_bytes, copy_here);
707 svm_fifo_peek_ma (svm_fifo_t * f, u32 relative_offset, u32 max_bytes,
710 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
711 u32 cursize, nitems, real_head;
713 /* read cursize, which can only increase while we're working */
714 cursize = svm_fifo_max_dequeue (f);
715 if (PREDICT_FALSE (cursize < relative_offset))
716 return -2; /* nothing in the fifo */
719 real_head = f->head + relative_offset;
720 real_head = real_head >= nitems ? real_head - nitems : real_head;
722 /* Number of bytes we're going to copy */
723 total_copy_bytes = (cursize - relative_offset < max_bytes) ?
724 cursize - relative_offset : max_bytes;
726 if (PREDICT_TRUE (copy_here != 0))
728 /* Number of bytes in first copy segment */
730 ((nitems - real_head) < total_copy_bytes) ?
731 (nitems - real_head) : total_copy_bytes;
732 clib_memcpy (copy_here, &f->data[real_head], first_copy_bytes);
734 /* Number of bytes in second copy segment, if any */
735 second_copy_bytes = total_copy_bytes - first_copy_bytes;
736 if (second_copy_bytes)
738 clib_memcpy (copy_here + first_copy_bytes, &f->data[0],
742 return total_copy_bytes;
745 #define SVM_FIFO_PEEK_CLONE_TEMPLATE(arch, fn, tgt) \
747 __attribute__ ((flatten)) \
748 __attribute__ ((target (tgt))) \
750 fn ## _ ## arch ( svm_fifo_t * f, u32 relative_offset, u32 max_bytes, \
752 { return fn (f, relative_offset, max_bytes, copy_here);}
754 foreach_march_variant (SVM_FIFO_PEEK_CLONE_TEMPLATE, svm_fifo_peek_ma);
755 CLIB_MULTIARCH_SELECT_FN (svm_fifo_peek_ma);
758 svm_fifo_peek (svm_fifo_t * f, u32 relative_offset, u32 max_bytes,
762 return svm_fifo_peek_ma (f, relative_offset, max_bytes, copy_here);
764 static int (*fp) (svm_fifo_t *, u32, u32, u8 *);
766 if (PREDICT_FALSE (fp == 0))
767 fp = (void *) svm_fifo_peek_ma_multiarch_select ();
769 return (*fp) (f, relative_offset, max_bytes, copy_here);
774 svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes)
776 u32 total_drop_bytes, first_drop_bytes, second_drop_bytes;
779 /* read cursize, which can only increase while we're working */
780 cursize = svm_fifo_max_dequeue (f);
781 if (PREDICT_FALSE (cursize == 0))
782 return -2; /* nothing in the fifo */
786 /* Number of bytes we're going to drop */
787 total_drop_bytes = (cursize < max_bytes) ? cursize : max_bytes;
789 svm_fifo_trace_add (f, f->tail, total_drop_bytes, 3);
791 /* Number of bytes in first copy segment */
793 ((nitems - f->head) < total_drop_bytes) ?
794 (nitems - f->head) : total_drop_bytes;
795 f->head += first_drop_bytes;
796 f->head = (f->head == nitems) ? 0 : f->head;
798 /* Number of bytes in second drop segment, if any */
799 second_drop_bytes = total_drop_bytes - first_drop_bytes;
800 if (second_drop_bytes)
802 f->head += second_drop_bytes;
803 f->head = (f->head == nitems) ? 0 : f->head;
806 ASSERT (f->head <= nitems);
807 ASSERT (cursize >= total_drop_bytes);
808 __sync_fetch_and_sub (&f->cursize, total_drop_bytes);
810 return total_drop_bytes;
814 svm_fifo_number_ooo_segments (svm_fifo_t * f)
816 return pool_elts (f->ooo_segments);
820 svm_fifo_first_ooo_segment (svm_fifo_t * f)
822 return pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
826 * Set fifo pointers to requested offset
829 svm_fifo_init_pointers (svm_fifo_t * f, u32 pointer)
831 f->head = f->tail = pointer % f->nitems;
835 * fd.io coding-style-patch-verification: ON
838 * eval: (c-set-style "gnu")