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 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));
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);
208 /* Check head and tail now since segment may be wider at both ends so
209 * merge tests lower won't work */
210 if (position_lt (f, normalized_position, s->start))
212 s->start = normalized_position;
213 s->length = position_diff (f, s_end_pos, s->start);
215 if (position_gt (f, normalized_end_position, s_end_pos))
217 s->length = position_diff (f, normalized_end_position, s->start);
222 s_index = s - f->ooo_segments;
223 s_end_pos = ooo_segment_end_pos (f, s);
225 /* No overlap, add before current segment */
226 if (position_lt (f, normalized_end_position, s->start))
228 new_s = ooo_segment_new (f, normalized_position, length);
229 new_index = new_s - f->ooo_segments;
231 /* Pool might've moved, get segment again */
232 s = pool_elt_at_index (f->ooo_segments, s_index);
233 if (s->prev != OOO_SEGMENT_INVALID_INDEX)
235 new_s->prev = s->prev;
236 prev = pool_elt_at_index (f->ooo_segments, new_s->prev);
237 prev->next = new_index;
242 f->ooos_list_head = new_index;
245 new_s->next = s_index;
247 f->ooos_newest = new_index;
250 /* No overlap, add after current segment */
251 else if (position_gt (f, normalized_position, s_end_pos))
253 new_s = ooo_segment_new (f, normalized_position, length);
254 new_index = new_s - f->ooo_segments;
256 /* Pool might've moved, get segment again */
257 s = pool_elt_at_index (f->ooo_segments, s_index);
259 ASSERT (s->next == OOO_SEGMENT_INVALID_INDEX);
261 new_s->prev = s_index;
263 f->ooos_newest = new_index;
273 if (position_lt (f, normalized_position, s->start))
275 s->start = normalized_position;
276 s->length = position_diff (f, s_end_pos, s->start);
278 /* Overlapping tail */
279 else if (position_gt (f, normalized_end_position, s_end_pos))
281 s->length = position_diff (f, normalized_end_position, s->start);
283 /* New segment completely covered by current one */
292 /* The new segment's tail may cover multiple smaller ones */
293 if (position_gt (f, normalized_end_position, s_end_pos))
295 /* Remove the completely overlapped segments */
296 it = (s->next != OOO_SEGMENT_INVALID_INDEX) ?
297 pool_elt_at_index (f->ooo_segments, s->next) : 0;
298 while (it && position_leq (f, ooo_segment_end_pos (f, it),
299 normalized_end_position))
301 next = (it->next != OOO_SEGMENT_INVALID_INDEX) ?
302 pool_elt_at_index (f->ooo_segments, it->next) : 0;
303 ooo_segment_del (f, it - f->ooo_segments);
307 /* If partial overlap with last, merge */
308 if (it && position_leq (f, it->start, normalized_end_position))
311 position_diff (f, ooo_segment_end_pos (f, it), s->start);
312 ooo_segment_del (f, it - f->ooo_segments);
317 /* Most recently updated segment */
319 f->ooos_newest = s - f->ooo_segments;
323 * Removes segments that can now be enqueued because the fifo's tail has
324 * advanced. Returns the number of bytes added to tail.
327 ooo_segment_try_collect (svm_fifo_t * f, u32 n_bytes_enqueued)
330 u32 index, bytes = 0;
333 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
334 diff = ooo_segment_distance_to_tail (f, s->start);
336 ASSERT (diff != n_bytes_enqueued);
338 if (diff > n_bytes_enqueued)
341 /* If last tail update overlaps one/multiple ooo segments, remove them */
342 while (0 <= diff && diff < n_bytes_enqueued)
344 index = s - f->ooo_segments;
346 /* Segment end is beyond the tail. Advance tail and remove segment */
347 if (s->length > diff)
349 bytes = s->length - diff;
351 f->tail %= f->nitems;
352 ooo_segment_del (f, index);
356 /* If we have next go on */
357 if (s->next != OOO_SEGMENT_INVALID_INDEX)
359 s = pool_elt_at_index (f->ooo_segments, s->next);
360 diff = ooo_segment_distance_to_tail (f, s->start);
361 ooo_segment_del (f, index);
366 ooo_segment_del (f, index);
371 ASSERT (bytes >= 0 && bytes <= f->nitems);
376 svm_fifo_enqueue_internal (svm_fifo_t * f, u32 max_bytes, u8 * copy_from_here)
378 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
381 /* read cursize, which can only increase while we're working */
382 cursize = svm_fifo_max_dequeue (f);
383 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
385 if (PREDICT_FALSE (cursize == f->nitems))
386 return -2; /* fifo stuffed */
390 /* Number of bytes we're going to copy */
391 total_copy_bytes = (nitems - cursize) < max_bytes ?
392 (nitems - cursize) : max_bytes;
394 if (PREDICT_TRUE (copy_from_here != 0))
396 /* Number of bytes in first copy segment */
397 first_copy_bytes = ((nitems - f->tail) < total_copy_bytes)
398 ? (nitems - f->tail) : total_copy_bytes;
400 clib_memcpy (&f->data[f->tail], copy_from_here, first_copy_bytes);
401 f->tail += first_copy_bytes;
402 f->tail = (f->tail == nitems) ? 0 : f->tail;
404 /* Number of bytes in second copy segment, if any */
405 second_copy_bytes = total_copy_bytes - first_copy_bytes;
406 if (second_copy_bytes)
408 clib_memcpy (&f->data[f->tail], copy_from_here + first_copy_bytes,
410 f->tail += second_copy_bytes;
411 f->tail = (f->tail == nitems) ? 0 : f->tail;
418 /* Account for a zero-copy enqueue done elsewhere */
419 ASSERT (max_bytes <= (nitems - cursize));
420 f->tail += max_bytes;
421 f->tail = f->tail % nitems;
422 total_copy_bytes = max_bytes;
425 /* Any out-of-order segments to collect? */
426 if (PREDICT_FALSE (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX))
427 total_copy_bytes += ooo_segment_try_collect (f, total_copy_bytes);
429 /* Atomically increase the queue length */
430 ASSERT (cursize + total_copy_bytes <= nitems);
431 __sync_fetch_and_add (&f->cursize, total_copy_bytes);
433 return (total_copy_bytes);
436 #define SVM_ENQUEUE_CLONE_TEMPLATE(arch, fn, tgt) \
438 __attribute__ ((flatten)) \
439 __attribute__ ((target (tgt))) \
441 fn ## _ ## arch ( svm_fifo_t * f, u32 max_bytes, u8 * copy_from_here) \
442 { return fn (f, max_bytes, copy_from_here);}
445 svm_fifo_enqueue_nowait_ma (svm_fifo_t * f, u32 max_bytes,
448 return svm_fifo_enqueue_internal (f, max_bytes, copy_from_here);
451 foreach_march_variant (SVM_ENQUEUE_CLONE_TEMPLATE,
452 svm_fifo_enqueue_nowait_ma);
453 CLIB_MULTIARCH_SELECT_FN (svm_fifo_enqueue_nowait_ma);
456 svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_from_here)
459 return svm_fifo_enqueue_nowait_ma (f, max_bytes, copy_from_here);
461 static int (*fp) (svm_fifo_t *, u32, u8 *);
463 if (PREDICT_FALSE (fp == 0))
464 fp = (void *) svm_fifo_enqueue_nowait_ma_multiarch_select ();
466 return (*fp) (f, max_bytes, copy_from_here);
471 * Enqueue a future segment.
473 * Two choices: either copies the entire segment, or copies nothing
474 * Returns 0 of the entire segment was copied
475 * Returns -1 if none of the segment was copied due to lack of space
478 svm_fifo_enqueue_with_offset_internal (svm_fifo_t * f,
483 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
484 u32 cursize, nitems, normalized_offset;
485 u32 offset_from_tail;
487 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
489 /* read cursize, which can only increase while we're working */
490 cursize = svm_fifo_max_dequeue (f);
493 ASSERT (required_bytes < nitems);
495 normalized_offset = (f->tail + offset) % nitems;
497 /* Will this request fit? */
498 offset_from_tail = (nitems + normalized_offset - f->tail) % nitems;
499 if ((required_bytes + offset_from_tail) > (nitems - cursize))
502 ooo_segment_add (f, offset, required_bytes);
504 /* Number of bytes we're going to copy */
505 total_copy_bytes = required_bytes;
507 /* Number of bytes in first copy segment */
508 first_copy_bytes = ((nitems - normalized_offset) < total_copy_bytes)
509 ? (nitems - normalized_offset) : total_copy_bytes;
511 clib_memcpy (&f->data[normalized_offset], copy_from_here, first_copy_bytes);
513 /* Number of bytes in second copy segment, if any */
514 second_copy_bytes = total_copy_bytes - first_copy_bytes;
515 if (second_copy_bytes)
517 normalized_offset += first_copy_bytes;
518 normalized_offset %= nitems;
520 ASSERT (normalized_offset == 0);
522 clib_memcpy (&f->data[normalized_offset],
523 copy_from_here + first_copy_bytes, second_copy_bytes);
531 svm_fifo_enqueue_with_offset (svm_fifo_t * f,
533 u32 required_bytes, u8 * copy_from_here)
535 return svm_fifo_enqueue_with_offset_internal (f, offset, required_bytes,
541 svm_fifo_dequeue_internal (svm_fifo_t * f, u32 max_bytes, u8 * copy_here)
543 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
546 /* read cursize, which can only increase while we're working */
547 cursize = svm_fifo_max_dequeue (f);
548 if (PREDICT_FALSE (cursize == 0))
549 return -2; /* nothing in the fifo */
553 /* Number of bytes we're going to copy */
554 total_copy_bytes = (cursize < max_bytes) ? cursize : max_bytes;
556 if (PREDICT_TRUE (copy_here != 0))
558 /* Number of bytes in first copy segment */
559 first_copy_bytes = ((nitems - f->head) < total_copy_bytes)
560 ? (nitems - f->head) : total_copy_bytes;
561 clib_memcpy (copy_here, &f->data[f->head], first_copy_bytes);
562 f->head += first_copy_bytes;
563 f->head = (f->head == nitems) ? 0 : f->head;
565 /* Number of bytes in second copy segment, if any */
566 second_copy_bytes = total_copy_bytes - first_copy_bytes;
567 if (second_copy_bytes)
569 clib_memcpy (copy_here + first_copy_bytes,
570 &f->data[f->head], second_copy_bytes);
571 f->head += second_copy_bytes;
572 f->head = (f->head == nitems) ? 0 : f->head;
578 /* Account for a zero-copy dequeue done elsewhere */
579 ASSERT (max_bytes <= cursize);
580 f->head += max_bytes;
581 f->head = f->head % nitems;
582 cursize -= max_bytes;
583 total_copy_bytes = max_bytes;
586 ASSERT (f->head <= nitems);
587 ASSERT (cursize >= total_copy_bytes);
588 __sync_fetch_and_sub (&f->cursize, total_copy_bytes);
590 return (total_copy_bytes);
594 svm_fifo_dequeue_nowait_ma (svm_fifo_t * f, u32 max_bytes, u8 * copy_here)
596 return svm_fifo_dequeue_internal (f, max_bytes, copy_here);
599 #define SVM_FIFO_DEQUEUE_CLONE_TEMPLATE(arch, fn, tgt) \
601 __attribute__ ((flatten)) \
602 __attribute__ ((target (tgt))) \
604 fn ## _ ## arch ( svm_fifo_t * f, u32 max_bytes, \
606 { return fn (f, max_bytes, copy_here);}
608 foreach_march_variant (SVM_FIFO_DEQUEUE_CLONE_TEMPLATE,
609 svm_fifo_dequeue_nowait_ma);
610 CLIB_MULTIARCH_SELECT_FN (svm_fifo_dequeue_nowait_ma);
613 svm_fifo_dequeue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_here)
616 return svm_fifo_dequeue_nowait_ma (f, max_bytes, copy_here);
618 static int (*fp) (svm_fifo_t *, u32, u8 *);
620 if (PREDICT_FALSE (fp == 0))
621 fp = (void *) svm_fifo_dequeue_nowait_ma_multiarch_select ();
623 return (*fp) (f, max_bytes, copy_here);
628 svm_fifo_peek_ma (svm_fifo_t * f, u32 relative_offset, u32 max_bytes,
631 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
632 u32 cursize, nitems, real_head;
634 /* read cursize, which can only increase while we're working */
635 cursize = svm_fifo_max_dequeue (f);
636 if (PREDICT_FALSE (cursize < relative_offset))
637 return -2; /* nothing in the fifo */
640 real_head = f->head + relative_offset;
641 real_head = real_head >= nitems ? real_head - nitems : real_head;
643 /* Number of bytes we're going to copy */
644 total_copy_bytes = (cursize - relative_offset < max_bytes) ?
645 cursize - relative_offset : max_bytes;
647 if (PREDICT_TRUE (copy_here != 0))
649 /* Number of bytes in first copy segment */
651 ((nitems - real_head) < total_copy_bytes) ?
652 (nitems - real_head) : total_copy_bytes;
653 clib_memcpy (copy_here, &f->data[real_head], first_copy_bytes);
655 /* Number of bytes in second copy segment, if any */
656 second_copy_bytes = total_copy_bytes - first_copy_bytes;
657 if (second_copy_bytes)
659 clib_memcpy (copy_here + first_copy_bytes, &f->data[0],
663 return total_copy_bytes;
666 #define SVM_FIFO_PEEK_CLONE_TEMPLATE(arch, fn, tgt) \
668 __attribute__ ((flatten)) \
669 __attribute__ ((target (tgt))) \
671 fn ## _ ## arch ( svm_fifo_t * f, u32 relative_offset, u32 max_bytes, \
673 { return fn (f, relative_offset, max_bytes, copy_here);}
675 foreach_march_variant (SVM_FIFO_PEEK_CLONE_TEMPLATE, svm_fifo_peek_ma);
676 CLIB_MULTIARCH_SELECT_FN (svm_fifo_peek_ma);
679 svm_fifo_peek (svm_fifo_t * f, u32 relative_offset, u32 max_bytes,
683 return svm_fifo_peek_ma (f, relative_offset, max_bytes, copy_here);
685 static int (*fp) (svm_fifo_t *, u32, u32, u8 *);
687 if (PREDICT_FALSE (fp == 0))
688 fp = (void *) svm_fifo_peek_ma_multiarch_select ();
690 return (*fp) (f, relative_offset, max_bytes, copy_here);
695 svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes)
697 u32 total_drop_bytes, first_drop_bytes, second_drop_bytes;
700 /* read cursize, which can only increase while we're working */
701 cursize = svm_fifo_max_dequeue (f);
702 if (PREDICT_FALSE (cursize == 0))
703 return -2; /* nothing in the fifo */
707 /* Number of bytes we're going to drop */
708 total_drop_bytes = (cursize < max_bytes) ? cursize : max_bytes;
710 /* Number of bytes in first copy segment */
712 ((nitems - f->head) < total_drop_bytes) ?
713 (nitems - f->head) : total_drop_bytes;
714 f->head += first_drop_bytes;
715 f->head = (f->head == nitems) ? 0 : f->head;
717 /* Number of bytes in second drop segment, if any */
718 second_drop_bytes = total_drop_bytes - first_drop_bytes;
719 if (second_drop_bytes)
721 f->head += second_drop_bytes;
722 f->head = (f->head == nitems) ? 0 : f->head;
725 ASSERT (f->head <= nitems);
726 ASSERT (cursize >= total_drop_bytes);
727 __sync_fetch_and_sub (&f->cursize, total_drop_bytes);
729 return total_drop_bytes;
733 svm_fifo_number_ooo_segments (svm_fifo_t * f)
735 return pool_elts (f->ooo_segments);
739 svm_fifo_first_ooo_segment (svm_fifo_t * f)
741 return pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
745 * Set fifo pointers to requested offset
748 svm_fifo_init_pointers (svm_fifo_t * f, u32 pointer)
750 f->head = f->tail = pointer % f->nitems;
754 * fd.io coding-style-patch-verification: ON
757 * eval: (c-set-style "gnu")