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_to_tail (f, a)
23 < ooo_segment_distance_to_tail (f, b));
27 position_leq (svm_fifo_t * f, u32 a, u32 b)
29 return (ooo_segment_distance_to_tail (f, a)
30 <= ooo_segment_distance_to_tail (f, b));
34 position_gt (svm_fifo_t * f, u32 a, u32 b)
36 return (ooo_segment_distance_to_tail (f, a)
37 > ooo_segment_distance_to_tail (f, b));
41 position_diff (svm_fifo_t * f, u32 posa, u32 posb)
43 return ooo_segment_distance_to_tail (f, posa)
44 - ooo_segment_distance_to_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 format_ooo_list (u8 * s, va_list * args)
66 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
67 u32 ooo_segment_index = f->ooos_list_head;
70 while (ooo_segment_index != OOO_SEGMENT_INVALID_INDEX)
72 seg = pool_elt_at_index (f->ooo_segments, ooo_segment_index);
73 s = format (s, " %U\n", format_ooo_segment, seg);
74 ooo_segment_index = seg->next;
80 format_svm_fifo (u8 * s, va_list * args)
82 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
83 int verbose = va_arg (*args, int);
85 s = format (s, "cursize %u nitems %u has_event %d\n",
86 f->cursize, f->nitems, f->has_event);
87 s = format (s, " head %d tail %d\n", f->head, f->tail);
91 (s, " server session %d thread %d client session %d thread %d\n",
92 f->master_session_index, f->master_thread_index,
93 f->client_session_index, f->client_thread_index);
97 s = format (s, " ooo pool %d active elts\n",
98 pool_elts (f->ooo_segments));
99 if (svm_fifo_has_ooo_data (f))
100 s = format (s, " %U", format_ooo_list, f);
105 /** create an svm fifo, in the current heap. Fails vs blow up the process */
107 svm_fifo_create (u32 data_size_in_bytes)
111 f = clib_mem_alloc_aligned_or_null (sizeof (*f) + data_size_in_bytes,
112 CLIB_CACHE_LINE_BYTES);
116 memset (f, 0, sizeof (*f) + data_size_in_bytes);
117 f->nitems = data_size_in_bytes;
118 f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
124 svm_fifo_free (svm_fifo_t * f)
126 pool_free (f->ooo_segments);
130 always_inline ooo_segment_t *
131 ooo_segment_new (svm_fifo_t * f, u32 start, u32 length)
135 pool_get (f->ooo_segments, s);
140 s->prev = s->next = OOO_SEGMENT_INVALID_INDEX;
146 ooo_segment_del (svm_fifo_t * f, u32 index)
148 ooo_segment_t *cur, *prev = 0, *next = 0;
149 cur = pool_elt_at_index (f->ooo_segments, index);
151 if (cur->next != OOO_SEGMENT_INVALID_INDEX)
153 next = pool_elt_at_index (f->ooo_segments, cur->next);
154 next->prev = cur->prev;
157 if (cur->prev != OOO_SEGMENT_INVALID_INDEX)
159 prev = pool_elt_at_index (f->ooo_segments, cur->prev);
160 prev->next = cur->next;
164 f->ooos_list_head = cur->next;
167 pool_put (f->ooo_segments, cur);
171 * Add segment to fifo's out-of-order segment list. Takes care of merging
172 * adjacent segments and removing overlapping ones.
175 ooo_segment_add (svm_fifo_t * f, u32 offset, u32 length)
177 ooo_segment_t *s, *new_s, *prev, *next, *it;
178 u32 new_index, s_end_pos, s_index;
179 u32 normalized_position, normalized_end_position;
181 normalized_position = (f->tail + offset) % f->nitems;
182 normalized_end_position = (f->tail + offset + length) % f->nitems;
184 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
186 if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX)
188 s = ooo_segment_new (f, normalized_position, length);
189 f->ooos_list_head = s - f->ooo_segments;
190 f->ooos_newest = f->ooos_list_head;
194 /* Find first segment that starts after new segment */
195 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
196 while (s->next != OOO_SEGMENT_INVALID_INDEX
197 && position_lt (f, s->start, normalized_position))
198 s = pool_elt_at_index (f->ooo_segments, s->next);
200 /* If we have a previous and we overlap it, use it as starting point */
201 prev = ooo_segment_get_prev (f, s);
203 && position_leq (f, normalized_position, ooo_segment_end_pos (f, prev)))
206 s_end_pos = ooo_segment_end_pos (f, s);
210 s_index = s - f->ooo_segments;
211 s_end_pos = ooo_segment_end_pos (f, s);
213 /* No overlap, add before current segment */
214 if (position_lt (f, normalized_end_position, s->start))
216 new_s = ooo_segment_new (f, normalized_position, length);
217 new_index = new_s - f->ooo_segments;
219 /* Pool might've moved, get segment again */
220 s = pool_elt_at_index (f->ooo_segments, s_index);
221 if (s->prev != OOO_SEGMENT_INVALID_INDEX)
223 new_s->prev = s->prev;
224 prev = pool_elt_at_index (f->ooo_segments, new_s->prev);
225 prev->next = new_index;
230 f->ooos_list_head = new_index;
233 new_s->next = s_index;
235 f->ooos_newest = new_index;
238 /* No overlap, add after current segment */
239 else if (position_gt (f, normalized_position, s_end_pos))
241 new_s = ooo_segment_new (f, normalized_position, length);
242 new_index = new_s - f->ooo_segments;
244 /* Pool might've moved, get segment again */
245 s = pool_elt_at_index (f->ooo_segments, s_index);
247 ASSERT (s->next == OOO_SEGMENT_INVALID_INDEX);
249 new_s->prev = s_index;
251 f->ooos_newest = new_index;
263 if (position_lt (f, normalized_position, s->start))
265 s->start = normalized_position;
266 s->length = position_diff (f, s_end_pos, s->start);
268 /* Overlapping tail */
269 else if (position_gt (f, normalized_end_position, s_end_pos))
271 s->length = position_diff (f, normalized_end_position, s->start);
273 /* New segment completely covered by current one */
281 /* The new segment's tail may cover multiple smaller ones */
282 if (position_gt (f, normalized_end_position, s_end_pos))
284 /* Remove the completely overlapped segments */
285 it = (s->next != OOO_SEGMENT_INVALID_INDEX) ?
286 pool_elt_at_index (f->ooo_segments, s->next) : 0;
287 while (it && position_leq (f, ooo_segment_end_pos (f, it),
288 normalized_end_position))
290 next = (it->next != OOO_SEGMENT_INVALID_INDEX) ?
291 pool_elt_at_index (f->ooo_segments, it->next) : 0;
292 ooo_segment_del (f, it - f->ooo_segments);
296 /* If partial overlap with last, merge */
297 if (it && position_leq (f, it->start, normalized_end_position))
299 s->length = ooo_segment_end_pos (f, it) - s->start;
300 ooo_segment_del (f, it - f->ooo_segments);
305 /* Most recently updated segment */
307 f->ooos_newest = s - f->ooo_segments;
311 * Removes segments that can now be enqueued because the fifo's tail has
312 * advanced. Returns the number of bytes added to tail.
315 ooo_segment_try_collect (svm_fifo_t * f, u32 n_bytes_enqueued)
318 u32 index, bytes = 0;
321 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
323 diff = (f->tail >= s->start) ?
324 f->tail - s->start : f->nitems + f->tail - s->start;
326 if (diff > n_bytes_enqueued)
329 /* If last tail update overlaps one/multiple ooo segments, remove them */
330 while (0 <= diff && diff < n_bytes_enqueued)
332 index = s - f->ooo_segments;
334 /* Segment end is beyond the tail. Advance tail and remove segment */
335 if (s->length > diff)
337 bytes = s->length - diff;
339 f->tail %= f->nitems;
340 ooo_segment_del (f, index);
344 /* If we have next go on */
345 if (s->next != OOO_SEGMENT_INVALID_INDEX)
347 s = pool_elt_at_index (f->ooo_segments, s->next);
348 diff = (f->tail >= s->start) ?
349 f->tail - s->start : f->nitems + f->tail - s->start;
350 ooo_segment_del (f, index);
355 ooo_segment_del (f, index);
364 svm_fifo_enqueue_internal (svm_fifo_t * f, u32 max_bytes, u8 * copy_from_here)
366 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
369 /* read cursize, which can only increase while we're working */
370 cursize = svm_fifo_max_dequeue (f);
371 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
373 if (PREDICT_FALSE (cursize == f->nitems))
374 return -2; /* fifo stuffed */
378 /* Number of bytes we're going to copy */
379 total_copy_bytes = (nitems - cursize) < max_bytes ?
380 (nitems - cursize) : max_bytes;
382 if (PREDICT_TRUE (copy_from_here != 0))
384 /* Number of bytes in first copy segment */
385 first_copy_bytes = ((nitems - f->tail) < total_copy_bytes)
386 ? (nitems - f->tail) : total_copy_bytes;
388 clib_memcpy (&f->data[f->tail], copy_from_here, first_copy_bytes);
389 f->tail += first_copy_bytes;
390 f->tail = (f->tail == nitems) ? 0 : f->tail;
392 /* Number of bytes in second copy segment, if any */
393 second_copy_bytes = total_copy_bytes - first_copy_bytes;
394 if (second_copy_bytes)
396 clib_memcpy (&f->data[f->tail], copy_from_here + first_copy_bytes,
398 f->tail += second_copy_bytes;
399 f->tail = (f->tail == nitems) ? 0 : f->tail;
404 /* Account for a zero-copy enqueue done elsewhere */
405 ASSERT (max_bytes <= (nitems - cursize));
406 f->tail += max_bytes;
407 f->tail = f->tail % nitems;
408 total_copy_bytes = max_bytes;
411 /* Any out-of-order segments to collect? */
412 if (PREDICT_FALSE (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX))
413 total_copy_bytes += ooo_segment_try_collect (f, total_copy_bytes);
415 /* Atomically increase the queue length */
416 __sync_fetch_and_add (&f->cursize, total_copy_bytes);
418 return (total_copy_bytes);
421 #define SVM_ENQUEUE_CLONE_TEMPLATE(arch, fn, tgt) \
423 __attribute__ ((flatten)) \
424 __attribute__ ((target (tgt))) \
426 fn ## _ ## arch ( svm_fifo_t * f, u32 max_bytes, u8 * copy_from_here) \
427 { return fn (f, max_bytes, copy_from_here);}
430 svm_fifo_enqueue_nowait_ma (svm_fifo_t * f, u32 max_bytes,
433 return svm_fifo_enqueue_internal (f, max_bytes, copy_from_here);
436 foreach_march_variant (SVM_ENQUEUE_CLONE_TEMPLATE,
437 svm_fifo_enqueue_nowait_ma);
438 CLIB_MULTIARCH_SELECT_FN (svm_fifo_enqueue_nowait_ma);
441 svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_from_here)
444 return svm_fifo_enqueue_nowait_ma (f, max_bytes, copy_from_here);
446 static int (*fp) (svm_fifo_t *, u32, u8 *);
448 if (PREDICT_FALSE (fp == 0))
449 fp = (void *) svm_fifo_enqueue_nowait_ma_multiarch_select ();
451 return (*fp) (f, max_bytes, copy_from_here);
456 * Enqueue a future segment.
458 * Two choices: either copies the entire segment, or copies nothing
459 * Returns 0 of the entire segment was copied
460 * Returns -1 if none of the segment was copied due to lack of space
463 svm_fifo_enqueue_with_offset_internal (svm_fifo_t * f,
468 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
469 u32 cursize, nitems, normalized_offset;
470 u32 offset_from_tail;
472 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
474 /* read cursize, which can only increase while we're working */
475 cursize = svm_fifo_max_dequeue (f);
478 normalized_offset = (f->tail + offset) % nitems;
480 /* Will this request fit? */
481 offset_from_tail = (nitems + normalized_offset - f->tail) % nitems;
482 if ((required_bytes + offset_from_tail) > (nitems - cursize))
485 ooo_segment_add (f, offset, required_bytes);
487 /* Number of bytes we're going to copy */
488 total_copy_bytes = required_bytes;
490 /* Number of bytes in first copy segment */
491 first_copy_bytes = ((nitems - normalized_offset) < total_copy_bytes)
492 ? (nitems - normalized_offset) : total_copy_bytes;
494 clib_memcpy (&f->data[normalized_offset], copy_from_here, first_copy_bytes);
496 /* Number of bytes in second copy segment, if any */
497 second_copy_bytes = total_copy_bytes - first_copy_bytes;
498 if (second_copy_bytes)
500 normalized_offset += first_copy_bytes;
501 normalized_offset %= nitems;
503 ASSERT (normalized_offset == 0);
505 clib_memcpy (&f->data[normalized_offset],
506 copy_from_here + first_copy_bytes, second_copy_bytes);
514 svm_fifo_enqueue_with_offset (svm_fifo_t * f,
516 u32 required_bytes, u8 * copy_from_here)
518 return svm_fifo_enqueue_with_offset_internal (f, offset, required_bytes,
524 svm_fifo_dequeue_internal (svm_fifo_t * f, u32 max_bytes, u8 * copy_here)
526 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
529 /* read cursize, which can only increase while we're working */
530 cursize = svm_fifo_max_dequeue (f);
531 if (PREDICT_FALSE (cursize == 0))
532 return -2; /* nothing in the fifo */
536 /* Number of bytes we're going to copy */
537 total_copy_bytes = (cursize < max_bytes) ? cursize : max_bytes;
539 if (PREDICT_TRUE (copy_here != 0))
541 /* Number of bytes in first copy segment */
542 first_copy_bytes = ((nitems - f->head) < total_copy_bytes)
543 ? (nitems - f->head) : total_copy_bytes;
544 clib_memcpy (copy_here, &f->data[f->head], first_copy_bytes);
545 f->head += first_copy_bytes;
546 f->head = (f->head == nitems) ? 0 : f->head;
548 /* Number of bytes in second copy segment, if any */
549 second_copy_bytes = total_copy_bytes - first_copy_bytes;
550 if (second_copy_bytes)
552 clib_memcpy (copy_here + first_copy_bytes,
553 &f->data[f->head], second_copy_bytes);
554 f->head += second_copy_bytes;
555 f->head = (f->head == nitems) ? 0 : f->head;
560 /* Account for a zero-copy dequeue done elsewhere */
561 ASSERT (max_bytes <= cursize);
562 f->head += max_bytes;
563 f->head = f->head % nitems;
564 cursize -= max_bytes;
565 total_copy_bytes = max_bytes;
568 __sync_fetch_and_sub (&f->cursize, total_copy_bytes);
570 return (total_copy_bytes);
574 svm_fifo_dequeue_nowait_ma (svm_fifo_t * f, u32 max_bytes, u8 * copy_here)
576 return svm_fifo_dequeue_internal (f, max_bytes, copy_here);
579 #define SVM_FIFO_DEQUEUE_CLONE_TEMPLATE(arch, fn, tgt) \
581 __attribute__ ((flatten)) \
582 __attribute__ ((target (tgt))) \
584 fn ## _ ## arch ( svm_fifo_t * f, u32 max_bytes, \
586 { return fn (f, max_bytes, copy_here);}
588 foreach_march_variant (SVM_FIFO_DEQUEUE_CLONE_TEMPLATE,
589 svm_fifo_dequeue_nowait_ma);
590 CLIB_MULTIARCH_SELECT_FN (svm_fifo_dequeue_nowait_ma);
593 svm_fifo_dequeue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_here)
596 return svm_fifo_dequeue_nowait_ma (f, max_bytes, copy_here);
598 static int (*fp) (svm_fifo_t *, u32, u8 *);
600 if (PREDICT_FALSE (fp == 0))
601 fp = (void *) svm_fifo_dequeue_nowait_ma_multiarch_select ();
603 return (*fp) (f, max_bytes, copy_here);
608 svm_fifo_peek_ma (svm_fifo_t * f, u32 relative_offset, u32 max_bytes,
611 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
612 u32 cursize, nitems, real_head;
614 /* read cursize, which can only increase while we're working */
615 cursize = svm_fifo_max_dequeue (f);
616 if (PREDICT_FALSE (cursize < relative_offset))
617 return -2; /* nothing in the fifo */
620 real_head = f->head + relative_offset;
621 real_head = real_head >= nitems ? real_head - nitems : real_head;
623 /* Number of bytes we're going to copy */
624 total_copy_bytes = (cursize - relative_offset < max_bytes) ?
625 cursize - relative_offset : max_bytes;
627 if (PREDICT_TRUE (copy_here != 0))
629 /* Number of bytes in first copy segment */
631 ((nitems - real_head) < total_copy_bytes) ?
632 (nitems - real_head) : total_copy_bytes;
633 clib_memcpy (copy_here, &f->data[real_head], first_copy_bytes);
635 /* Number of bytes in second copy segment, if any */
636 second_copy_bytes = total_copy_bytes - first_copy_bytes;
637 if (second_copy_bytes)
639 clib_memcpy (copy_here + first_copy_bytes, &f->data[0],
643 return total_copy_bytes;
646 #define SVM_FIFO_PEEK_CLONE_TEMPLATE(arch, fn, tgt) \
648 __attribute__ ((flatten)) \
649 __attribute__ ((target (tgt))) \
651 fn ## _ ## arch ( svm_fifo_t * f, u32 relative_offset, u32 max_bytes, \
653 { return fn (f, relative_offset, max_bytes, copy_here);}
655 foreach_march_variant (SVM_FIFO_PEEK_CLONE_TEMPLATE, svm_fifo_peek_ma);
656 CLIB_MULTIARCH_SELECT_FN (svm_fifo_peek_ma);
659 svm_fifo_peek (svm_fifo_t * f, u32 relative_offset, u32 max_bytes,
663 return svm_fifo_peek_ma (f, relative_offset, max_bytes, copy_here);
665 static int (*fp) (svm_fifo_t *, u32, u32, u8 *);
667 if (PREDICT_FALSE (fp == 0))
668 fp = (void *) svm_fifo_peek_ma_multiarch_select ();
670 return (*fp) (f, relative_offset, max_bytes, copy_here);
675 svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes)
677 u32 total_drop_bytes, first_drop_bytes, second_drop_bytes;
680 /* read cursize, which can only increase while we're working */
681 cursize = svm_fifo_max_dequeue (f);
682 if (PREDICT_FALSE (cursize == 0))
683 return -2; /* nothing in the fifo */
687 /* Number of bytes we're going to drop */
688 total_drop_bytes = (cursize < max_bytes) ? cursize : max_bytes;
690 /* Number of bytes in first copy segment */
692 ((nitems - f->head) < total_drop_bytes) ?
693 (nitems - f->head) : total_drop_bytes;
694 f->head += first_drop_bytes;
695 f->head = (f->head == nitems) ? 0 : f->head;
697 /* Number of bytes in second drop segment, if any */
698 second_drop_bytes = total_drop_bytes - first_drop_bytes;
699 if (second_drop_bytes)
701 f->head += second_drop_bytes;
702 f->head = (f->head == nitems) ? 0 : f->head;
705 __sync_fetch_and_sub (&f->cursize, total_drop_bytes);
707 return total_drop_bytes;
711 svm_fifo_number_ooo_segments (svm_fifo_t * f)
713 return pool_elts (f->ooo_segments);
717 svm_fifo_first_ooo_segment (svm_fifo_t * f)
719 return pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
723 * Set fifo pointers to requested offset
726 svm_fifo_init_pointers (svm_fifo_t * f, u32 pointer)
728 f->head = f->tail = pointer % f->nitems;
732 * fd.io coding-style-patch-verification: ON
735 * eval: (c-set-style "gnu")