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 ooo_segment_t *seg = va_arg (*args, ooo_segment_t *);
58 s = format (s, "pos %u, len %u, next %d, prev %d",
59 seg->start, seg->length, seg->next, seg->prev);
64 svm_fifo_dump_trace (u8 * s, svm_fifo_t * f)
67 svm_fifo_trace_elem_t *seg = 0;
72 vec_foreach (seg, f->trace)
74 s = format (s, "{%u, %u, %u}, ", seg->offset, seg->len, seg->action);
88 svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose)
92 svm_fifo_trace_elem_t *trace;
94 svm_fifo_t *dummy_fifo;
101 trace_len = vec_len (trace);
107 dummy_fifo = svm_fifo_create (f->nitems);
108 memset (f->data, 0xFF, f->nitems);
110 vec_validate (data, f->nitems);
111 for (i = 0; i < vec_len (data); i++)
114 for (i = 0; i < trace_len; i++)
116 offset = trace[i].offset;
117 if (trace[i].action == 1)
120 s = format (s, "adding [%u, %u]:", trace[i].offset,
122 trace[i].len) % dummy_fifo->nitems);
123 svm_fifo_enqueue_with_offset (dummy_fifo, trace[i].offset,
124 trace[i].len, &data[offset]);
126 else if (trace[i].action == 2)
129 s = format (s, "adding [%u, %u]:", 0, trace[i].len);
130 svm_fifo_enqueue_nowait (dummy_fifo, trace[i].len, &data[offset]);
135 s = format (s, "read: %u", trace[i].len);
136 svm_fifo_dequeue_drop (dummy_fifo, trace[i].len);
139 s = format (s, "%U", format_svm_fifo, dummy_fifo, 1);
142 s = format (s, "result: %U", format_svm_fifo, dummy_fifo, 1);
148 format_ooo_list (u8 * s, va_list * args)
150 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
151 u32 ooo_segment_index = f->ooos_list_head;
154 while (ooo_segment_index != OOO_SEGMENT_INVALID_INDEX)
156 seg = pool_elt_at_index (f->ooo_segments, ooo_segment_index);
157 s = format (s, " %U\n", format_ooo_segment, seg);
158 ooo_segment_index = seg->next;
165 format_svm_fifo (u8 * s, va_list * args)
167 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
168 int verbose = va_arg (*args, int);
170 s = format (s, "cursize %u nitems %u has_event %d\n",
171 f->cursize, f->nitems, f->has_event);
172 s = format (s, " head %d tail %d\n", f->head, f->tail);
176 (s, " server session %d thread %d client session %d thread %d\n",
177 f->master_session_index, f->master_thread_index,
178 f->client_session_index, f->client_thread_index);
182 s = format (s, " ooo pool %d active elts newest %u\n",
183 pool_elts (f->ooo_segments), f->ooos_newest);
184 if (svm_fifo_has_ooo_data (f))
185 s = format (s, " %U", format_ooo_list, f, verbose);
190 /** create an svm fifo, in the current heap. Fails vs blow up the process */
192 svm_fifo_create (u32 data_size_in_bytes)
196 f = clib_mem_alloc_aligned_or_null (sizeof (*f) + data_size_in_bytes,
197 CLIB_CACHE_LINE_BYTES);
201 memset (f, 0, sizeof (*f));
202 f->nitems = data_size_in_bytes;
203 f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
209 svm_fifo_free (svm_fifo_t * f)
211 ASSERT (f->refcnt > 0);
213 if (--f->refcnt == 0)
215 pool_free (f->ooo_segments);
220 always_inline ooo_segment_t *
221 ooo_segment_new (svm_fifo_t * f, u32 start, u32 length)
225 pool_get (f->ooo_segments, s);
230 s->prev = s->next = OOO_SEGMENT_INVALID_INDEX;
236 ooo_segment_del (svm_fifo_t * f, u32 index)
238 ooo_segment_t *cur, *prev = 0, *next = 0;
239 cur = pool_elt_at_index (f->ooo_segments, index);
241 if (cur->next != OOO_SEGMENT_INVALID_INDEX)
243 next = pool_elt_at_index (f->ooo_segments, cur->next);
244 next->prev = cur->prev;
247 if (cur->prev != OOO_SEGMENT_INVALID_INDEX)
249 prev = pool_elt_at_index (f->ooo_segments, cur->prev);
250 prev->next = cur->next;
254 f->ooos_list_head = cur->next;
257 pool_put (f->ooo_segments, cur);
261 * Add segment to fifo's out-of-order segment list. Takes care of merging
262 * adjacent segments and removing overlapping ones.
265 ooo_segment_add (svm_fifo_t * f, u32 offset, u32 length)
267 ooo_segment_t *s, *new_s, *prev, *next, *it;
268 u32 new_index, s_end_pos, s_index;
269 u32 normalized_position, normalized_end_position;
271 ASSERT (offset + length <= ooo_segment_distance_from_tail (f, f->head));
272 normalized_position = (f->tail + offset) % f->nitems;
273 normalized_end_position = (f->tail + offset + length) % f->nitems;
275 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
277 if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX)
279 s = ooo_segment_new (f, normalized_position, length);
280 f->ooos_list_head = s - f->ooo_segments;
281 f->ooos_newest = f->ooos_list_head;
285 /* Find first segment that starts after new segment */
286 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
287 while (s->next != OOO_SEGMENT_INVALID_INDEX
288 && position_lt (f, s->start, normalized_position))
289 s = pool_elt_at_index (f->ooo_segments, s->next);
291 /* If we have a previous and we overlap it, use it as starting point */
292 prev = ooo_segment_get_prev (f, s);
294 && position_leq (f, normalized_position, ooo_segment_end_pos (f, prev)))
297 s_end_pos = ooo_segment_end_pos (f, s);
299 /* Since we have previous, normalized start position cannot be smaller
300 * than prev->start. Check tail */
301 ASSERT (position_lt (f, s->start, normalized_position));
305 s_index = s - f->ooo_segments;
306 s_end_pos = ooo_segment_end_pos (f, s);
308 /* No overlap, add before current segment */
309 if (position_lt (f, normalized_end_position, s->start))
311 new_s = ooo_segment_new (f, normalized_position, length);
312 new_index = new_s - f->ooo_segments;
314 /* Pool might've moved, get segment again */
315 s = pool_elt_at_index (f->ooo_segments, s_index);
316 if (s->prev != OOO_SEGMENT_INVALID_INDEX)
318 new_s->prev = s->prev;
319 prev = pool_elt_at_index (f->ooo_segments, new_s->prev);
320 prev->next = new_index;
325 f->ooos_list_head = new_index;
328 new_s->next = s_index;
330 f->ooos_newest = new_index;
333 /* No overlap, add after current segment */
334 else if (position_gt (f, normalized_position, s_end_pos))
336 new_s = ooo_segment_new (f, normalized_position, length);
337 new_index = new_s - f->ooo_segments;
339 /* Pool might've moved, get segment again */
340 s = pool_elt_at_index (f->ooo_segments, s_index);
342 /* Needs to be last */
343 ASSERT (s->next == OOO_SEGMENT_INVALID_INDEX);
345 new_s->prev = s_index;
347 f->ooos_newest = new_index;
357 if (position_lt (f, normalized_position, s->start))
359 s->start = normalized_position;
360 s->length = position_diff (f, s_end_pos, s->start);
361 f->ooos_newest = s - f->ooo_segments;
366 /* Overlapping tail */
367 if (position_gt (f, normalized_end_position, s_end_pos))
369 s->length = position_diff (f, normalized_end_position, s->start);
371 /* Remove the completely overlapped segments in the tail */
372 it = ooo_segment_next (f, s);
373 while (it && position_leq (f, ooo_segment_end_pos (f, it),
374 normalized_end_position))
376 next = ooo_segment_next (f, it);
377 ooo_segment_del (f, it - f->ooo_segments);
381 /* If partial overlap with last, merge */
382 if (it && position_leq (f, it->start, normalized_end_position))
384 s->length = position_diff (f, ooo_segment_end_pos (f, it),
386 ooo_segment_del (f, it - f->ooo_segments);
388 f->ooos_newest = s - f->ooo_segments;
393 * Removes segments that can now be enqueued because the fifo's tail has
394 * advanced. Returns the number of bytes added to tail.
397 ooo_segment_try_collect (svm_fifo_t * f, u32 n_bytes_enqueued)
400 u32 index, bytes = 0;
403 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
404 diff = ooo_segment_distance_to_tail (f, s->start);
406 ASSERT (diff != n_bytes_enqueued);
408 if (diff > n_bytes_enqueued)
411 /* If last tail update overlaps one/multiple ooo segments, remove them */
412 while (0 <= diff && diff < n_bytes_enqueued)
414 index = s - f->ooo_segments;
416 /* Segment end is beyond the tail. Advance tail and remove segment */
417 if (s->length > diff)
419 bytes = s->length - diff;
421 f->tail %= f->nitems;
422 ooo_segment_del (f, index);
426 /* If we have next go on */
427 if (s->next != OOO_SEGMENT_INVALID_INDEX)
429 s = pool_elt_at_index (f->ooo_segments, s->next);
430 diff = ooo_segment_distance_to_tail (f, s->start);
431 ooo_segment_del (f, index);
436 ooo_segment_del (f, index);
441 ASSERT (bytes >= 0 && bytes <= f->nitems);
446 svm_fifo_enqueue_internal (svm_fifo_t * f, u32 max_bytes, u8 * copy_from_here)
448 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
451 /* read cursize, which can only increase while we're working */
452 cursize = svm_fifo_max_dequeue (f);
453 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
455 if (PREDICT_FALSE (cursize == f->nitems))
456 return -2; /* fifo stuffed */
460 /* Number of bytes we're going to copy */
461 total_copy_bytes = (nitems - cursize) < max_bytes ?
462 (nitems - cursize) : max_bytes;
464 if (PREDICT_TRUE (copy_from_here != 0))
466 /* Number of bytes in first copy segment */
467 first_copy_bytes = ((nitems - f->tail) < total_copy_bytes)
468 ? (nitems - f->tail) : total_copy_bytes;
470 clib_memcpy (&f->data[f->tail], copy_from_here, first_copy_bytes);
471 f->tail += first_copy_bytes;
472 f->tail = (f->tail == nitems) ? 0 : f->tail;
474 /* Number of bytes in second copy segment, if any */
475 second_copy_bytes = total_copy_bytes - first_copy_bytes;
476 if (second_copy_bytes)
478 clib_memcpy (&f->data[f->tail], copy_from_here + first_copy_bytes,
480 f->tail += second_copy_bytes;
481 f->tail = (f->tail == nitems) ? 0 : f->tail;
488 /* Account for a zero-copy enqueue done elsewhere */
489 ASSERT (max_bytes <= (nitems - cursize));
490 f->tail += max_bytes;
491 f->tail = f->tail % nitems;
492 total_copy_bytes = max_bytes;
495 svm_fifo_trace_add (f, f->head, total_copy_bytes, 2);
497 /* Any out-of-order segments to collect? */
498 if (PREDICT_FALSE (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX))
499 total_copy_bytes += ooo_segment_try_collect (f, total_copy_bytes);
501 /* Atomically increase the queue length */
502 ASSERT (cursize + total_copy_bytes <= nitems);
503 __sync_fetch_and_add (&f->cursize, total_copy_bytes);
505 return (total_copy_bytes);
508 #define SVM_ENQUEUE_CLONE_TEMPLATE(arch, fn, tgt) \
510 __attribute__ ((flatten)) \
511 __attribute__ ((target (tgt))) \
513 fn ## _ ## arch ( svm_fifo_t * f, u32 max_bytes, u8 * copy_from_here) \
514 { return fn (f, max_bytes, copy_from_here);}
517 svm_fifo_enqueue_nowait_ma (svm_fifo_t * f, u32 max_bytes,
520 return svm_fifo_enqueue_internal (f, max_bytes, copy_from_here);
523 foreach_march_variant (SVM_ENQUEUE_CLONE_TEMPLATE,
524 svm_fifo_enqueue_nowait_ma);
525 CLIB_MULTIARCH_SELECT_FN (svm_fifo_enqueue_nowait_ma);
528 svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_from_here)
531 return svm_fifo_enqueue_nowait_ma (f, max_bytes, copy_from_here);
533 static int (*fp) (svm_fifo_t *, u32, u8 *);
535 if (PREDICT_FALSE (fp == 0))
536 fp = (void *) svm_fifo_enqueue_nowait_ma_multiarch_select ();
538 return (*fp) (f, max_bytes, copy_from_here);
543 * Enqueue a future segment.
545 * Two choices: either copies the entire segment, or copies nothing
546 * Returns 0 of the entire segment was copied
547 * Returns -1 if none of the segment was copied due to lack of space
550 svm_fifo_enqueue_with_offset_internal (svm_fifo_t * f,
555 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
556 u32 cursize, nitems, normalized_offset;
557 u32 offset_from_tail;
559 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
561 /* read cursize, which can only increase while we're working */
562 cursize = svm_fifo_max_dequeue (f);
565 ASSERT (required_bytes < nitems);
567 normalized_offset = (f->tail + offset) % nitems;
569 /* Will this request fit? */
570 offset_from_tail = (nitems + normalized_offset - f->tail) % nitems;
571 if ((required_bytes + offset_from_tail) > (nitems - cursize))
574 svm_fifo_trace_add (f, offset, required_bytes, 1);
576 ooo_segment_add (f, offset, required_bytes);
578 /* Number of bytes we're going to copy */
579 total_copy_bytes = required_bytes;
581 /* Number of bytes in first copy segment */
582 first_copy_bytes = ((nitems - normalized_offset) < total_copy_bytes)
583 ? (nitems - normalized_offset) : total_copy_bytes;
585 clib_memcpy (&f->data[normalized_offset], copy_from_here, first_copy_bytes);
587 /* Number of bytes in second copy segment, if any */
588 second_copy_bytes = total_copy_bytes - first_copy_bytes;
589 if (second_copy_bytes)
591 normalized_offset += first_copy_bytes;
592 normalized_offset %= nitems;
594 ASSERT (normalized_offset == 0);
596 clib_memcpy (&f->data[normalized_offset],
597 copy_from_here + first_copy_bytes, second_copy_bytes);
605 svm_fifo_enqueue_with_offset (svm_fifo_t * f,
607 u32 required_bytes, u8 * copy_from_here)
609 return svm_fifo_enqueue_with_offset_internal (f, offset, required_bytes,
615 svm_fifo_dequeue_internal (svm_fifo_t * f, u32 max_bytes, u8 * copy_here)
617 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
620 /* read cursize, which can only increase while we're working */
621 cursize = svm_fifo_max_dequeue (f);
622 if (PREDICT_FALSE (cursize == 0))
623 return -2; /* nothing in the fifo */
627 /* Number of bytes we're going to copy */
628 total_copy_bytes = (cursize < max_bytes) ? cursize : max_bytes;
630 if (PREDICT_TRUE (copy_here != 0))
632 /* Number of bytes in first copy segment */
633 first_copy_bytes = ((nitems - f->head) < total_copy_bytes)
634 ? (nitems - f->head) : total_copy_bytes;
635 clib_memcpy (copy_here, &f->data[f->head], first_copy_bytes);
636 f->head += first_copy_bytes;
637 f->head = (f->head == nitems) ? 0 : f->head;
639 /* Number of bytes in second copy segment, if any */
640 second_copy_bytes = total_copy_bytes - first_copy_bytes;
641 if (second_copy_bytes)
643 clib_memcpy (copy_here + first_copy_bytes,
644 &f->data[f->head], second_copy_bytes);
645 f->head += second_copy_bytes;
646 f->head = (f->head == nitems) ? 0 : f->head;
652 /* Account for a zero-copy dequeue done elsewhere */
653 ASSERT (max_bytes <= cursize);
654 f->head += max_bytes;
655 f->head = f->head % nitems;
656 cursize -= max_bytes;
657 total_copy_bytes = max_bytes;
660 ASSERT (f->head <= nitems);
661 ASSERT (cursize >= total_copy_bytes);
662 __sync_fetch_and_sub (&f->cursize, total_copy_bytes);
664 return (total_copy_bytes);
668 svm_fifo_dequeue_nowait_ma (svm_fifo_t * f, u32 max_bytes, u8 * copy_here)
670 return svm_fifo_dequeue_internal (f, max_bytes, copy_here);
673 #define SVM_FIFO_DEQUEUE_CLONE_TEMPLATE(arch, fn, tgt) \
675 __attribute__ ((flatten)) \
676 __attribute__ ((target (tgt))) \
678 fn ## _ ## arch ( svm_fifo_t * f, u32 max_bytes, \
680 { return fn (f, max_bytes, copy_here);}
682 foreach_march_variant (SVM_FIFO_DEQUEUE_CLONE_TEMPLATE,
683 svm_fifo_dequeue_nowait_ma);
684 CLIB_MULTIARCH_SELECT_FN (svm_fifo_dequeue_nowait_ma);
687 svm_fifo_dequeue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_here)
690 return svm_fifo_dequeue_nowait_ma (f, max_bytes, copy_here);
692 static int (*fp) (svm_fifo_t *, u32, u8 *);
694 if (PREDICT_FALSE (fp == 0))
695 fp = (void *) svm_fifo_dequeue_nowait_ma_multiarch_select ();
697 return (*fp) (f, max_bytes, copy_here);
702 svm_fifo_peek_ma (svm_fifo_t * f, u32 relative_offset, u32 max_bytes,
705 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
706 u32 cursize, nitems, real_head;
708 /* read cursize, which can only increase while we're working */
709 cursize = svm_fifo_max_dequeue (f);
710 if (PREDICT_FALSE (cursize < relative_offset))
711 return -2; /* nothing in the fifo */
714 real_head = f->head + relative_offset;
715 real_head = real_head >= nitems ? real_head - nitems : real_head;
717 /* Number of bytes we're going to copy */
718 total_copy_bytes = (cursize - relative_offset < max_bytes) ?
719 cursize - relative_offset : max_bytes;
721 if (PREDICT_TRUE (copy_here != 0))
723 /* Number of bytes in first copy segment */
725 ((nitems - real_head) < total_copy_bytes) ?
726 (nitems - real_head) : total_copy_bytes;
727 clib_memcpy (copy_here, &f->data[real_head], first_copy_bytes);
729 /* Number of bytes in second copy segment, if any */
730 second_copy_bytes = total_copy_bytes - first_copy_bytes;
731 if (second_copy_bytes)
733 clib_memcpy (copy_here + first_copy_bytes, &f->data[0],
737 return total_copy_bytes;
740 #define SVM_FIFO_PEEK_CLONE_TEMPLATE(arch, fn, tgt) \
742 __attribute__ ((flatten)) \
743 __attribute__ ((target (tgt))) \
745 fn ## _ ## arch ( svm_fifo_t * f, u32 relative_offset, u32 max_bytes, \
747 { return fn (f, relative_offset, max_bytes, copy_here);}
749 foreach_march_variant (SVM_FIFO_PEEK_CLONE_TEMPLATE, svm_fifo_peek_ma);
750 CLIB_MULTIARCH_SELECT_FN (svm_fifo_peek_ma);
753 svm_fifo_peek (svm_fifo_t * f, u32 relative_offset, u32 max_bytes,
757 return svm_fifo_peek_ma (f, relative_offset, max_bytes, copy_here);
759 static int (*fp) (svm_fifo_t *, u32, u32, u8 *);
761 if (PREDICT_FALSE (fp == 0))
762 fp = (void *) svm_fifo_peek_ma_multiarch_select ();
764 return (*fp) (f, relative_offset, max_bytes, copy_here);
769 svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes)
771 u32 total_drop_bytes, first_drop_bytes, second_drop_bytes;
774 /* read cursize, which can only increase while we're working */
775 cursize = svm_fifo_max_dequeue (f);
776 if (PREDICT_FALSE (cursize == 0))
777 return -2; /* nothing in the fifo */
781 /* Number of bytes we're going to drop */
782 total_drop_bytes = (cursize < max_bytes) ? cursize : max_bytes;
784 svm_fifo_trace_add (f, f->tail, total_drop_bytes, 3);
786 /* Number of bytes in first copy segment */
788 ((nitems - f->head) < total_drop_bytes) ?
789 (nitems - f->head) : total_drop_bytes;
790 f->head += first_drop_bytes;
791 f->head = (f->head == nitems) ? 0 : f->head;
793 /* Number of bytes in second drop segment, if any */
794 second_drop_bytes = total_drop_bytes - first_drop_bytes;
795 if (second_drop_bytes)
797 f->head += second_drop_bytes;
798 f->head = (f->head == nitems) ? 0 : f->head;
801 ASSERT (f->head <= nitems);
802 ASSERT (cursize >= total_drop_bytes);
803 __sync_fetch_and_sub (&f->cursize, total_drop_bytes);
805 return total_drop_bytes;
809 svm_fifo_number_ooo_segments (svm_fifo_t * f)
811 return pool_elts (f->ooo_segments);
815 svm_fifo_first_ooo_segment (svm_fifo_t * f)
817 return pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
821 * Set fifo pointers to requested offset
824 svm_fifo_init_pointers (svm_fifo_t * f, u32 pointer)
826 f->head = f->tail = pointer % f->nitems;
830 * fd.io coding-style-patch-verification: ON
833 * eval: (c-set-style "gnu")