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;
53 #ifndef CLIB_MARCH_VARIANT
56 format_ooo_segment (u8 * s, va_list * args)
58 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
59 ooo_segment_t *seg = va_arg (*args, ooo_segment_t *);
60 u32 normalized_start = (seg->start + f->nitems - f->tail) % f->nitems;
61 s = format (s, "[%u, %u], len %u, next %d, prev %d", normalized_start,
62 (normalized_start + seg->length) % f->nitems, seg->length,
63 seg->next, seg->prev);
68 svm_fifo_dump_trace (u8 * s, svm_fifo_t * f)
71 svm_fifo_trace_elem_t *seg = 0;
76 vec_foreach (seg, f->trace)
78 s = format (s, "{%u, %u, %u}, ", seg->offset, seg->len, seg->action);
92 svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose)
96 svm_fifo_trace_elem_t *trace;
98 svm_fifo_t *dummy_fifo;
105 trace_len = vec_len (trace);
111 dummy_fifo = svm_fifo_create (f->nitems);
112 memset (f->data, 0xFF, f->nitems);
114 vec_validate (data, f->nitems);
115 for (i = 0; i < vec_len (data); i++)
118 for (i = 0; i < trace_len; i++)
120 offset = trace[i].offset;
121 if (trace[i].action == 1)
124 s = format (s, "adding [%u, %u]:", trace[i].offset,
126 trace[i].len) % dummy_fifo->nitems);
127 svm_fifo_enqueue_with_offset (dummy_fifo, trace[i].offset,
128 trace[i].len, &data[offset]);
130 else if (trace[i].action == 2)
133 s = format (s, "adding [%u, %u]:", 0, trace[i].len);
134 svm_fifo_enqueue_nowait (dummy_fifo, trace[i].len, &data[offset]);
139 s = format (s, "read: %u", trace[i].len);
140 svm_fifo_dequeue_drop (dummy_fifo, trace[i].len);
143 s = format (s, "%U", format_svm_fifo, dummy_fifo, 1);
146 s = format (s, "result: %U", format_svm_fifo, dummy_fifo, 1);
152 format_ooo_list (u8 * s, va_list * args)
154 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
155 u32 ooo_segment_index = f->ooos_list_head;
158 while (ooo_segment_index != OOO_SEGMENT_INVALID_INDEX)
160 seg = pool_elt_at_index (f->ooo_segments, ooo_segment_index);
161 s = format (s, " %U\n", format_ooo_segment, f, seg);
162 ooo_segment_index = seg->next;
169 format_svm_fifo (u8 * s, va_list * args)
171 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
172 int verbose = va_arg (*args, int);
177 s = format (s, "cursize %u nitems %u has_event %d\n",
178 f->cursize, f->nitems, f->has_event);
179 s = format (s, " head %d tail %d segment manager %u\n", f->head, f->tail,
184 (s, " vpp session %d thread %d app session %d thread %d\n",
185 f->master_session_index, f->master_thread_index,
186 f->client_session_index, f->client_thread_index);
190 s = format (s, " ooo pool %d active elts newest %u\n",
191 pool_elts (f->ooo_segments), f->ooos_newest);
192 if (svm_fifo_has_ooo_data (f))
193 s = format (s, " %U", format_ooo_list, f, verbose);
198 /** create an svm fifo, in the current heap. Fails vs blow up the process */
200 svm_fifo_create (u32 data_size_in_bytes)
203 u32 rounded_data_size;
205 /* always round fifo data size to the next highest power-of-two */
206 rounded_data_size = (1 << (max_log2 (data_size_in_bytes)));
207 f = clib_mem_alloc_aligned_or_null (sizeof (*f) + rounded_data_size,
208 CLIB_CACHE_LINE_BYTES);
212 memset (f, 0, sizeof (*f));
213 f->nitems = data_size_in_bytes;
214 f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
220 svm_fifo_free (svm_fifo_t * f)
222 ASSERT (f->refcnt > 0);
224 if (--f->refcnt == 0)
226 pool_free (f->ooo_segments);
232 always_inline ooo_segment_t *
233 ooo_segment_new (svm_fifo_t * f, u32 start, u32 length)
237 pool_get (f->ooo_segments, s);
242 s->prev = s->next = OOO_SEGMENT_INVALID_INDEX;
248 ooo_segment_del (svm_fifo_t * f, u32 index)
250 ooo_segment_t *cur, *prev = 0, *next = 0;
251 cur = pool_elt_at_index (f->ooo_segments, index);
253 if (cur->next != OOO_SEGMENT_INVALID_INDEX)
255 next = pool_elt_at_index (f->ooo_segments, cur->next);
256 next->prev = cur->prev;
259 if (cur->prev != OOO_SEGMENT_INVALID_INDEX)
261 prev = pool_elt_at_index (f->ooo_segments, cur->prev);
262 prev->next = cur->next;
266 f->ooos_list_head = cur->next;
269 pool_put (f->ooo_segments, cur);
273 * Add segment to fifo's out-of-order segment list. Takes care of merging
274 * adjacent segments and removing overlapping ones.
277 ooo_segment_add (svm_fifo_t * f, u32 offset, u32 length)
279 ooo_segment_t *s, *new_s, *prev, *next, *it;
280 u32 new_index, s_end_pos, s_index;
281 u32 normalized_position, normalized_end_position;
283 ASSERT (offset + length <= ooo_segment_distance_from_tail (f, f->head));
284 normalized_position = (f->tail + offset) % f->nitems;
285 normalized_end_position = (f->tail + offset + length) % f->nitems;
287 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
289 if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX)
291 s = ooo_segment_new (f, normalized_position, length);
292 f->ooos_list_head = s - f->ooo_segments;
293 f->ooos_newest = f->ooos_list_head;
297 /* Find first segment that starts after new segment */
298 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
299 while (s->next != OOO_SEGMENT_INVALID_INDEX
300 && position_lt (f, s->start, normalized_position))
301 s = pool_elt_at_index (f->ooo_segments, s->next);
303 /* If we have a previous and we overlap it, use it as starting point */
304 prev = ooo_segment_get_prev (f, s);
306 && position_leq (f, normalized_position, ooo_segment_end_pos (f, prev)))
309 s_end_pos = ooo_segment_end_pos (f, s);
311 /* Since we have previous, normalized start position cannot be smaller
312 * than prev->start. Check tail */
313 ASSERT (position_lt (f, s->start, normalized_position));
317 s_index = s - f->ooo_segments;
318 s_end_pos = ooo_segment_end_pos (f, s);
320 /* No overlap, add before current segment */
321 if (position_lt (f, normalized_end_position, s->start))
323 new_s = ooo_segment_new (f, normalized_position, length);
324 new_index = new_s - f->ooo_segments;
326 /* Pool might've moved, get segment again */
327 s = pool_elt_at_index (f->ooo_segments, s_index);
328 if (s->prev != OOO_SEGMENT_INVALID_INDEX)
330 new_s->prev = s->prev;
331 prev = pool_elt_at_index (f->ooo_segments, new_s->prev);
332 prev->next = new_index;
337 f->ooos_list_head = new_index;
340 new_s->next = s_index;
342 f->ooos_newest = new_index;
345 /* No overlap, add after current segment */
346 else if (position_gt (f, normalized_position, s_end_pos))
348 new_s = ooo_segment_new (f, normalized_position, length);
349 new_index = new_s - f->ooo_segments;
351 /* Pool might've moved, get segment again */
352 s = pool_elt_at_index (f->ooo_segments, s_index);
354 /* Needs to be last */
355 ASSERT (s->next == OOO_SEGMENT_INVALID_INDEX);
357 new_s->prev = s_index;
359 f->ooos_newest = new_index;
369 if (position_lt (f, normalized_position, s->start))
371 s->start = normalized_position;
372 s->length = position_diff (f, s_end_pos, s->start);
373 f->ooos_newest = s - f->ooo_segments;
378 /* Overlapping tail */
379 if (position_gt (f, normalized_end_position, s_end_pos))
381 s->length = position_diff (f, normalized_end_position, s->start);
383 /* Remove the completely overlapped segments in the tail */
384 it = ooo_segment_next (f, s);
385 while (it && position_leq (f, ooo_segment_end_pos (f, it),
386 normalized_end_position))
388 next = ooo_segment_next (f, it);
389 ooo_segment_del (f, it - f->ooo_segments);
393 /* If partial overlap with last, merge */
394 if (it && position_leq (f, it->start, normalized_end_position))
396 s->length = position_diff (f, ooo_segment_end_pos (f, it),
398 ooo_segment_del (f, it - f->ooo_segments);
400 f->ooos_newest = s - f->ooo_segments;
405 * Removes segments that can now be enqueued because the fifo's tail has
406 * advanced. Returns the number of bytes added to tail.
409 ooo_segment_try_collect (svm_fifo_t * f, u32 n_bytes_enqueued)
412 u32 index, bytes = 0;
415 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
416 diff = ooo_segment_distance_to_tail (f, s->start);
418 ASSERT (diff != n_bytes_enqueued);
420 if (diff > n_bytes_enqueued)
423 /* If last tail update overlaps one/multiple ooo segments, remove them */
424 while (0 <= diff && diff < n_bytes_enqueued)
426 index = s - f->ooo_segments;
428 /* Segment end is beyond the tail. Advance tail and remove segment */
429 if (s->length > diff)
431 bytes = s->length - diff;
433 f->tail %= f->nitems;
434 ooo_segment_del (f, index);
438 /* If we have next go on */
439 if (s->next != OOO_SEGMENT_INVALID_INDEX)
441 s = pool_elt_at_index (f->ooo_segments, s->next);
442 diff = ooo_segment_distance_to_tail (f, s->start);
443 ooo_segment_del (f, index);
448 ooo_segment_del (f, index);
453 ASSERT (bytes <= f->nitems);
457 CLIB_MARCH_FN (svm_fifo_enqueue_nowait, int, svm_fifo_t * f, u32 max_bytes,
458 const u8 * copy_from_here)
460 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
463 /* read cursize, which can only increase while we're working */
464 cursize = svm_fifo_max_dequeue (f);
465 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
467 if (PREDICT_FALSE (cursize == f->nitems))
468 return SVM_FIFO_FULL;
472 /* Number of bytes we're going to copy */
473 total_copy_bytes = (nitems - cursize) < max_bytes ?
474 (nitems - cursize) : max_bytes;
476 if (PREDICT_TRUE (copy_from_here != 0))
478 /* Number of bytes in first copy segment */
479 first_copy_bytes = ((nitems - f->tail) < total_copy_bytes)
480 ? (nitems - f->tail) : total_copy_bytes;
482 clib_memcpy (&f->data[f->tail], copy_from_here, first_copy_bytes);
483 f->tail += first_copy_bytes;
484 f->tail = (f->tail == nitems) ? 0 : f->tail;
486 /* Number of bytes in second copy segment, if any */
487 second_copy_bytes = total_copy_bytes - first_copy_bytes;
488 if (second_copy_bytes)
490 clib_memcpy (&f->data[f->tail], copy_from_here + first_copy_bytes,
492 f->tail += second_copy_bytes;
493 f->tail = (f->tail == nitems) ? 0 : f->tail;
500 /* Account for a zero-copy enqueue done elsewhere */
501 ASSERT (max_bytes <= (nitems - cursize));
502 f->tail += max_bytes;
503 f->tail = f->tail % nitems;
504 total_copy_bytes = max_bytes;
507 svm_fifo_trace_add (f, f->head, total_copy_bytes, 2);
509 /* Any out-of-order segments to collect? */
510 if (PREDICT_FALSE (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX))
511 total_copy_bytes += ooo_segment_try_collect (f, total_copy_bytes);
513 /* Atomically increase the queue length */
514 ASSERT (cursize + total_copy_bytes <= nitems);
515 __sync_fetch_and_add (&f->cursize, total_copy_bytes);
517 return (total_copy_bytes);
520 #ifndef CLIB_MARCH_VARIANT
522 svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 max_bytes,
523 const u8 * copy_from_here)
525 return CLIB_MARCH_FN_SELECT (svm_fifo_enqueue_nowait) (f, max_bytes,
531 * Enqueue a future segment.
533 * Two choices: either copies the entire segment, or copies nothing
534 * Returns 0 of the entire segment was copied
535 * Returns -1 if none of the segment was copied due to lack of space
537 CLIB_MARCH_FN (svm_fifo_enqueue_with_offset, int, svm_fifo_t * f,
538 u32 offset, u32 required_bytes, u8 * copy_from_here)
540 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
541 u32 cursize, nitems, normalized_offset;
543 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
545 /* read cursize, which can only increase while we're working */
546 cursize = svm_fifo_max_dequeue (f);
549 ASSERT (required_bytes < nitems);
551 normalized_offset = (f->tail + offset) % nitems;
553 /* Will this request fit? */
554 if ((required_bytes + offset) > (nitems - cursize))
557 svm_fifo_trace_add (f, offset, required_bytes, 1);
559 ooo_segment_add (f, offset, required_bytes);
561 /* Number of bytes we're going to copy */
562 total_copy_bytes = required_bytes;
564 /* Number of bytes in first copy segment */
565 first_copy_bytes = ((nitems - normalized_offset) < total_copy_bytes)
566 ? (nitems - normalized_offset) : total_copy_bytes;
568 clib_memcpy (&f->data[normalized_offset], copy_from_here, first_copy_bytes);
570 /* Number of bytes in second copy segment, if any */
571 second_copy_bytes = total_copy_bytes - first_copy_bytes;
572 if (second_copy_bytes)
574 normalized_offset += first_copy_bytes;
575 normalized_offset %= nitems;
577 ASSERT (normalized_offset == 0);
579 clib_memcpy (&f->data[normalized_offset],
580 copy_from_here + first_copy_bytes, second_copy_bytes);
586 #ifndef CLIB_MARCH_VARIANT
589 svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 required_bytes,
592 return CLIB_MARCH_FN_SELECT (svm_fifo_enqueue_with_offset) (f, offset,
598 svm_fifo_overwrite_head (svm_fifo_t * f, u8 * data, u32 len)
601 first_chunk = f->nitems - f->head;
602 ASSERT (len <= f->nitems);
603 if (len <= first_chunk)
604 clib_memcpy (&f->data[f->head], data, len);
607 clib_memcpy (&f->data[f->head], data, first_chunk);
608 clib_memcpy (&f->data[0], data + first_chunk, len - first_chunk);
613 CLIB_MARCH_FN (svm_fifo_dequeue_nowait, int, svm_fifo_t * f, u32 max_bytes,
616 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
619 /* read cursize, which can only increase while we're working */
620 cursize = svm_fifo_max_dequeue (f);
621 if (PREDICT_FALSE (cursize == 0))
622 return -2; /* nothing in the fifo */
626 /* Number of bytes we're going to copy */
627 total_copy_bytes = (cursize < max_bytes) ? cursize : max_bytes;
629 if (PREDICT_TRUE (copy_here != 0))
631 /* Number of bytes in first copy segment */
632 first_copy_bytes = ((nitems - f->head) < total_copy_bytes)
633 ? (nitems - f->head) : total_copy_bytes;
634 clib_memcpy (copy_here, &f->data[f->head], first_copy_bytes);
635 f->head += first_copy_bytes;
636 f->head = (f->head == nitems) ? 0 : f->head;
638 /* Number of bytes in second copy segment, if any */
639 second_copy_bytes = total_copy_bytes - first_copy_bytes;
640 if (second_copy_bytes)
642 clib_memcpy (copy_here + first_copy_bytes,
643 &f->data[f->head], second_copy_bytes);
644 f->head += second_copy_bytes;
645 f->head = (f->head == nitems) ? 0 : f->head;
651 /* Account for a zero-copy dequeue done elsewhere */
652 ASSERT (max_bytes <= cursize);
653 f->head += max_bytes;
654 f->head = f->head % nitems;
655 cursize -= max_bytes;
656 total_copy_bytes = max_bytes;
659 ASSERT (f->head <= nitems);
660 ASSERT (cursize >= total_copy_bytes);
661 __sync_fetch_and_sub (&f->cursize, total_copy_bytes);
663 return (total_copy_bytes);
666 #ifndef CLIB_MARCH_VARIANT
669 svm_fifo_dequeue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_here)
671 return CLIB_MARCH_FN_SELECT (svm_fifo_dequeue_nowait) (f, max_bytes,
676 CLIB_MARCH_FN (svm_fifo_peek, int, svm_fifo_t * f, u32 relative_offset,
677 u32 max_bytes, u8 * copy_here)
679 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
680 u32 cursize, nitems, real_head;
682 /* read cursize, which can only increase while we're working */
683 cursize = svm_fifo_max_dequeue (f);
684 if (PREDICT_FALSE (cursize < relative_offset))
685 return -2; /* nothing in the fifo */
688 real_head = f->head + relative_offset;
689 real_head = real_head >= nitems ? real_head - nitems : real_head;
691 /* Number of bytes we're going to copy */
692 total_copy_bytes = (cursize - relative_offset < max_bytes) ?
693 cursize - relative_offset : max_bytes;
695 if (PREDICT_TRUE (copy_here != 0))
697 /* Number of bytes in first copy segment */
699 ((nitems - real_head) < total_copy_bytes) ?
700 (nitems - real_head) : total_copy_bytes;
701 clib_memcpy (copy_here, &f->data[real_head], first_copy_bytes);
703 /* Number of bytes in second copy segment, if any */
704 second_copy_bytes = total_copy_bytes - first_copy_bytes;
705 if (second_copy_bytes)
707 clib_memcpy (copy_here + first_copy_bytes, &f->data[0],
711 return total_copy_bytes;
714 #ifndef CLIB_MARCH_VARIANT
717 svm_fifo_peek (svm_fifo_t * f, u32 relative_offset, u32 max_bytes,
720 return CLIB_MARCH_FN_SELECT (svm_fifo_peek) (f, relative_offset, max_bytes,
725 svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes)
727 u32 total_drop_bytes, first_drop_bytes, second_drop_bytes;
730 /* read cursize, which can only increase while we're working */
731 cursize = svm_fifo_max_dequeue (f);
732 if (PREDICT_FALSE (cursize == 0))
733 return -2; /* nothing in the fifo */
737 /* Number of bytes we're going to drop */
738 total_drop_bytes = (cursize < max_bytes) ? cursize : max_bytes;
740 svm_fifo_trace_add (f, f->tail, total_drop_bytes, 3);
742 /* Number of bytes in first copy segment */
744 ((nitems - f->head) < total_drop_bytes) ?
745 (nitems - f->head) : total_drop_bytes;
746 f->head += first_drop_bytes;
747 f->head = (f->head == nitems) ? 0 : f->head;
749 /* Number of bytes in second drop segment, if any */
750 second_drop_bytes = total_drop_bytes - first_drop_bytes;
751 if (second_drop_bytes)
753 f->head += second_drop_bytes;
754 f->head = (f->head == nitems) ? 0 : f->head;
757 ASSERT (f->head <= nitems);
758 ASSERT (cursize >= total_drop_bytes);
759 __sync_fetch_and_sub (&f->cursize, total_drop_bytes);
761 return total_drop_bytes;
765 svm_fifo_dequeue_drop_all (svm_fifo_t * f)
768 __sync_fetch_and_sub (&f->cursize, f->cursize);
772 svm_fifo_segments (svm_fifo_t * f, svm_fifo_segment_t * fs)
776 /* read cursize, which can only increase while we're working */
777 cursize = svm_fifo_max_dequeue (f);
778 if (PREDICT_FALSE (cursize == 0))
783 fs[0].len = ((nitems - f->head) < cursize) ? (nitems - f->head) : cursize;
784 fs[0].data = f->data + f->head;
786 if (fs[0].len < cursize)
788 fs[1].len = cursize - fs[0].len;
789 fs[1].data = f->data;
800 svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_segment_t * fs)
802 u32 total_drop_bytes;
804 ASSERT (fs[0].data == f->data + f->head);
808 total_drop_bytes = fs[0].len + fs[1].len;
812 f->head = (f->head + fs[0].len) % f->nitems;
813 total_drop_bytes = fs[0].len;
815 __sync_fetch_and_sub (&f->cursize, total_drop_bytes);
819 svm_fifo_number_ooo_segments (svm_fifo_t * f)
821 return pool_elts (f->ooo_segments);
825 svm_fifo_first_ooo_segment (svm_fifo_t * f)
827 return pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
831 * Set fifo pointers to requested offset
834 svm_fifo_init_pointers (svm_fifo_t * f, u32 pointer)
836 f->head = f->tail = pointer % f->nitems;
841 * fd.io coding-style-patch-verification: ON
844 * eval: (c-set-style "gnu")