svm: split fifo into private and shared structs
[vpp.git] / src / svm / svm_fifo.c
1 /*
2  * Copyright (c) 2016-2019 Cisco and/or its affiliates.
3  * Copyright (c) 2019 Arm Limited
4  * Copyright (c) 2010-2017 Intel Corporation and/or its affiliates.
5  * Copyright (c) 2007-2009 Kip Macy kmacy@freebsd.org
6  * Inspired from DPDK rte_ring.h (SPSC only) (derived from freebsd bufring.h).
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at:
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #include <svm/svm_fifo.h>
21 #include <svm/fifo_segment.h>
22 #include <vppinfra/cpu.h>
23
24 CLIB_MARCH_FN (svm_fifo_copy_to_chunk, void, svm_fifo_t * f,
25                svm_fifo_chunk_t * c, u32 tail_idx, const u8 * src, u32 len,
26                svm_fifo_chunk_t ** last)
27 {
28   u32 n_chunk;
29
30   ASSERT (f_pos_geq (tail_idx, c->start_byte)
31           && f_pos_lt (tail_idx, c->start_byte + c->length));
32
33   tail_idx -= c->start_byte;
34   n_chunk = c->length - tail_idx;
35   if (n_chunk <= len)
36     {
37       u32 to_copy = len;
38       clib_memcpy_fast (&c->data[tail_idx], src, n_chunk);
39       c = c->next;
40       while ((to_copy -= n_chunk))
41         {
42           n_chunk = clib_min (c->length, to_copy);
43           clib_memcpy_fast (&c->data[0], src + (len - to_copy), n_chunk);
44           c = c->length <= to_copy ? c->next : c;
45         }
46       if (*last)
47         *last = c;
48     }
49   else
50     {
51       clib_memcpy_fast (&c->data[tail_idx], src, len);
52     }
53 }
54
55 CLIB_MARCH_FN (svm_fifo_copy_from_chunk, void, svm_fifo_t * f,
56                svm_fifo_chunk_t * c, u32 head_idx, u8 * dst, u32 len,
57                svm_fifo_chunk_t ** last)
58 {
59   u32 n_chunk;
60
61   ASSERT (f_pos_geq (head_idx, c->start_byte)
62           && f_pos_lt (head_idx, c->start_byte + c->length));
63
64   head_idx -= c->start_byte;
65   n_chunk = c->length - head_idx;
66   if (n_chunk <= len)
67     {
68       u32 to_copy = len;
69       clib_memcpy_fast (dst, &c->data[head_idx], n_chunk);
70       c = c->next;
71       while ((to_copy -= n_chunk))
72         {
73           CLIB_MEM_UNPOISON (c, sizeof (*c));
74           CLIB_MEM_UNPOISON (c->data, c->length);
75           n_chunk = clib_min (c->length, to_copy);
76           clib_memcpy_fast (dst + (len - to_copy), &c->data[0], n_chunk);
77           c = c->length <= to_copy ? c->next : c;
78         }
79       if (*last)
80         *last = c;
81     }
82   else
83     {
84       clib_memcpy_fast (dst, &c->data[head_idx], len);
85     }
86 }
87
88 #ifndef CLIB_MARCH_VARIANT
89
90 static inline void
91 svm_fifo_copy_to_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c, u32 tail_idx,
92                         const u8 * src, u32 len, svm_fifo_chunk_t ** last)
93 {
94   CLIB_MARCH_FN_SELECT (svm_fifo_copy_to_chunk) (f, c, tail_idx, src, len,
95                                                  last);
96 }
97
98 static inline void
99 svm_fifo_copy_from_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c, u32 head_idx,
100                           u8 * dst, u32 len, svm_fifo_chunk_t ** last)
101 {
102   CLIB_MARCH_FN_SELECT (svm_fifo_copy_from_chunk) (f, c, head_idx, dst, len,
103                                                    last);
104 }
105
106 static inline u32
107 ooo_segment_end_pos (ooo_segment_t * s)
108 {
109   return (s->start + s->length);
110 }
111
112 void
113 svm_fifo_free_ooo_data (svm_fifo_t * f)
114 {
115   pool_free (f->ooo_segments);
116 }
117
118 static inline ooo_segment_t *
119 ooo_segment_prev (svm_fifo_t * f, ooo_segment_t * s)
120 {
121   if (s->prev == OOO_SEGMENT_INVALID_INDEX)
122     return 0;
123   return pool_elt_at_index (f->ooo_segments, s->prev);
124 }
125
126 static inline ooo_segment_t *
127 ooo_segment_next (svm_fifo_t * f, ooo_segment_t * s)
128 {
129   if (s->next == OOO_SEGMENT_INVALID_INDEX)
130     return 0;
131   return pool_elt_at_index (f->ooo_segments, s->next);
132 }
133
134 static inline ooo_segment_t *
135 ooo_segment_alloc (svm_fifo_t * f, u32 start, u32 length)
136 {
137   ooo_segment_t *s;
138
139   pool_get (f->ooo_segments, s);
140
141   s->start = start;
142   s->length = length;
143   s->prev = s->next = OOO_SEGMENT_INVALID_INDEX;
144
145   return s;
146 }
147
148 static inline void
149 ooo_segment_free (svm_fifo_t * f, u32 index)
150 {
151   ooo_segment_t *cur, *prev = 0, *next = 0;
152   cur = pool_elt_at_index (f->ooo_segments, index);
153
154   if (cur->next != OOO_SEGMENT_INVALID_INDEX)
155     {
156       next = pool_elt_at_index (f->ooo_segments, cur->next);
157       next->prev = cur->prev;
158     }
159
160   if (cur->prev != OOO_SEGMENT_INVALID_INDEX)
161     {
162       prev = pool_elt_at_index (f->ooo_segments, cur->prev);
163       prev->next = cur->next;
164     }
165   else
166     {
167       f->ooos_list_head = cur->next;
168     }
169
170   pool_put (f->ooo_segments, cur);
171 }
172
173 /**
174  * Add segment to fifo's out-of-order segment list. Takes care of merging
175  * adjacent segments and removing overlapping ones.
176  */
177 static void
178 ooo_segment_add (svm_fifo_t * f, u32 offset, u32 head, u32 tail, u32 length)
179 {
180   ooo_segment_t *s, *new_s, *prev, *next, *it;
181   u32 new_index, s_end_pos, s_index;
182   u32 offset_pos, offset_end_pos;
183
184   ASSERT (offset + length <= f_free_count (f, head, tail));
185
186   offset_pos = tail + offset;
187   offset_end_pos = tail + offset + length;
188
189   f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
190
191   if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX)
192     {
193       s = ooo_segment_alloc (f, offset_pos, length);
194       f->ooos_list_head = s - f->ooo_segments;
195       f->ooos_newest = f->ooos_list_head;
196       return;
197     }
198
199   /* Find first segment that starts after new segment */
200   s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
201   while (s->next != OOO_SEGMENT_INVALID_INDEX
202          && f_pos_lt (s->start, offset_pos))
203     s = pool_elt_at_index (f->ooo_segments, s->next);
204
205   /* If we have a previous and we overlap it, use it as starting point */
206   prev = ooo_segment_prev (f, s);
207   if (prev && f_pos_leq (offset_pos, ooo_segment_end_pos (prev)))
208     {
209       s = prev;
210       s_end_pos = ooo_segment_end_pos (s);
211
212       /* Since we have previous, offset start position cannot be smaller
213        * than prev->start. Check tail */
214       ASSERT (f_pos_lt (s->start, offset_pos));
215       goto check_tail;
216     }
217
218   s_index = s - f->ooo_segments;
219   s_end_pos = ooo_segment_end_pos (s);
220
221   /* No overlap, add before current segment */
222   if (f_pos_lt (offset_end_pos, s->start))
223     {
224       new_s = ooo_segment_alloc (f, offset_pos, length);
225       new_index = new_s - f->ooo_segments;
226
227       /* Pool might've moved, get segment again */
228       s = pool_elt_at_index (f->ooo_segments, s_index);
229       if (s->prev != OOO_SEGMENT_INVALID_INDEX)
230         {
231           new_s->prev = s->prev;
232           prev = pool_elt_at_index (f->ooo_segments, new_s->prev);
233           prev->next = new_index;
234         }
235       else
236         {
237           /* New head */
238           f->ooos_list_head = new_index;
239         }
240
241       new_s->next = s_index;
242       s->prev = new_index;
243       f->ooos_newest = new_index;
244       return;
245     }
246   /* No overlap, add after current segment */
247   else if (f_pos_gt (offset_pos, s_end_pos))
248     {
249       new_s = ooo_segment_alloc (f, offset_pos, length);
250       new_index = new_s - f->ooo_segments;
251
252       /* Pool might've moved, get segment again */
253       s = pool_elt_at_index (f->ooo_segments, s_index);
254
255       /* Needs to be last */
256       ASSERT (s->next == OOO_SEGMENT_INVALID_INDEX);
257
258       new_s->prev = s_index;
259       s->next = new_index;
260       f->ooos_newest = new_index;
261
262       return;
263     }
264
265   /*
266    * Merge needed
267    */
268
269   /* Merge at head */
270   if (f_pos_lt (offset_pos, s->start))
271     {
272       s->start = offset_pos;
273       s->length = s_end_pos - s->start;
274       f->ooos_newest = s - f->ooo_segments;
275     }
276
277 check_tail:
278
279   /* Overlapping tail */
280   if (f_pos_gt (offset_end_pos, s_end_pos))
281     {
282       s->length = offset_end_pos - s->start;
283
284       /* Remove the completely overlapped segments in the tail */
285       it = ooo_segment_next (f, s);
286       while (it && f_pos_leq (ooo_segment_end_pos (it), offset_end_pos))
287         {
288           next = ooo_segment_next (f, it);
289           ooo_segment_free (f, it - f->ooo_segments);
290           it = next;
291         }
292
293       /* If partial overlap with last, merge */
294       if (it && f_pos_leq (it->start, offset_end_pos))
295         {
296           s->length = ooo_segment_end_pos (it) - s->start;
297           ooo_segment_free (f, it - f->ooo_segments);
298         }
299       f->ooos_newest = s - f->ooo_segments;
300     }
301 }
302
303 /**
304  * Removes segments that can now be enqueued because the fifo's tail has
305  * advanced. Returns the number of bytes added to tail.
306  */
307 static int
308 ooo_segment_try_collect (svm_fifo_t * f, u32 n_bytes_enqueued, u32 * tail)
309 {
310   u32 s_index, bytes = 0;
311   ooo_segment_t *s;
312   i32 diff;
313
314   s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
315   diff = *tail - s->start;
316
317   ASSERT (diff != n_bytes_enqueued);
318
319   if (diff > n_bytes_enqueued)
320     return 0;
321
322   /* If last tail update overlaps one/multiple ooo segments, remove them */
323   while (0 <= diff && diff < n_bytes_enqueued)
324     {
325       s_index = s - f->ooo_segments;
326
327       /* Segment end is beyond the tail. Advance tail and remove segment */
328       if (s->length > diff)
329         {
330           bytes = s->length - diff;
331           *tail = *tail + bytes;
332           ooo_segment_free (f, s_index);
333           break;
334         }
335
336       /* If we have next go on */
337       if (s->next != OOO_SEGMENT_INVALID_INDEX)
338         {
339           s = pool_elt_at_index (f->ooo_segments, s->next);
340           diff = *tail - s->start;
341           ooo_segment_free (f, s_index);
342         }
343       /* End of search */
344       else
345         {
346           ooo_segment_free (f, s_index);
347           break;
348         }
349     }
350
351   ASSERT (bytes <= f->shr->size);
352   return bytes;
353 }
354
355 __clib_unused static ooo_segment_t *
356 ooo_segment_last (svm_fifo_t * f)
357 {
358   ooo_segment_t *s;
359
360   if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX)
361     return 0;
362
363   s = svm_fifo_first_ooo_segment (f);
364   while (s->next != OOO_SEGMENT_INVALID_INDEX)
365     s = pool_elt_at_index (f->ooo_segments, s->next);
366   return s;
367 }
368
369 void
370 svm_fifo_init (svm_fifo_t * f, u32 size)
371 {
372   svm_fifo_chunk_t *c, *prev;
373   u32 min_alloc;
374
375   f->shr->size = size;
376   f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
377   f->segment_index = SVM_FIFO_INVALID_INDEX;
378   f->refcnt = 1;
379   f->shr->head = f->shr->tail = f->flags = 0;
380   f->shr->head_chunk = f->shr->tail_chunk = f->shr->start_chunk;
381   f->ooo_deq = f->ooo_enq = 0;
382
383   min_alloc = size > 32 << 10 ? size >> 3 : 4096;
384   min_alloc = clib_min (min_alloc, 64 << 10);
385   f->shr->min_alloc = min_alloc;
386
387   /*
388    * Initialize chunks
389    */
390   f->shr->start_chunk->start_byte = 0;
391   prev = f->shr->start_chunk;
392   prev->enq_rb_index = prev->deq_rb_index = RBTREE_TNIL_INDEX;
393   c = prev->next;
394
395   while (c)
396     {
397       c->start_byte = prev->start_byte + prev->length;
398       c->enq_rb_index = c->deq_rb_index = RBTREE_TNIL_INDEX;
399       prev = c;
400       c = c->next;
401     }
402 }
403
404 void
405 svm_fifo_init_ooo_lookup (svm_fifo_t * f, u8 ooo_type)
406 {
407   if (ooo_type == 0)
408     {
409       ASSERT (!rb_tree_is_init (&f->ooo_enq_lookup));
410       rb_tree_init (&f->ooo_enq_lookup);
411     }
412   else
413     {
414       ASSERT (!rb_tree_is_init (&f->ooo_deq_lookup));
415       rb_tree_init (&f->ooo_deq_lookup);
416     }
417 }
418
419 /**
420  * Creates a fifo in the current heap. Fails vs blow up the process
421  */
422 svm_fifo_t *
423 svm_fifo_alloc (u32 data_size_in_bytes)
424 {
425   u32 rounded_data_size;
426   svm_fifo_chunk_t *c;
427   svm_fifo_t *f;
428
429   f = clib_mem_alloc_aligned_or_null (sizeof (*f), CLIB_CACHE_LINE_BYTES);
430   if (f == 0)
431     return 0;
432
433   clib_memset (f, 0, sizeof (*f));
434
435   /* always round fifo data size to the next highest power-of-two */
436   rounded_data_size = (1 << (max_log2 (data_size_in_bytes)));
437   c = clib_mem_alloc_aligned_or_null (sizeof (*c) + rounded_data_size,
438                                       CLIB_CACHE_LINE_BYTES);
439   if (!c)
440     {
441       clib_mem_free (f);
442       return 0;
443     }
444
445   clib_memset (c, 0, sizeof (*c));
446   c->start_byte = 0;
447   c->length = data_size_in_bytes;
448   c->enq_rb_index = RBTREE_TNIL_INDEX;
449   c->deq_rb_index = RBTREE_TNIL_INDEX;
450   f->shr->start_chunk = f->shr->end_chunk = c;
451
452   return f;
453 }
454
455 /**
456  * Creates a fifo chunk in the current heap
457  */
458 svm_fifo_chunk_t *
459 svm_fifo_chunk_alloc (u32 size)
460 {
461   svm_fifo_chunk_t *c;
462   u32 rounded_size;
463
464   /* round chunk size to the next highest power-of-two */
465   rounded_size = (1 << (max_log2 (size)));
466   c = clib_mem_alloc_aligned_or_null (sizeof (*c) + rounded_size,
467                                       CLIB_CACHE_LINE_BYTES);
468   if (c == 0)
469     return 0;
470
471   clib_memset (c, 0, sizeof (*c));
472   c->length = rounded_size;
473   return c;
474 }
475
476 /**
477  * Find chunk for given byte position
478  *
479  * @param f     fifo
480  * @param pos   normalized position in fifo
481  *
482  * @return chunk that includes given position or 0
483  */
484 static svm_fifo_chunk_t *
485 svm_fifo_find_chunk (svm_fifo_t * f, u32 pos)
486 {
487   svm_fifo_chunk_t *c;
488
489   c = f->shr->start_chunk;
490   while (c && !f_chunk_includes_pos (c, pos))
491     c = c->next;
492
493   return c;
494 }
495
496 static svm_fifo_chunk_t *
497 svm_fifo_find_next_chunk (svm_fifo_t * f, svm_fifo_chunk_t * start, u32 pos)
498 {
499   svm_fifo_chunk_t *c;
500
501   ASSERT (start != 0);
502
503   c = start;
504   while (c && !f_chunk_includes_pos (c, pos))
505     c = c->next;
506
507   return c;
508 }
509
510 u32
511 svm_fifo_max_read_chunk (svm_fifo_t * f)
512 {
513   u32 head, tail, end_chunk;
514
515   f_load_head_tail_cons (f, &head, &tail);
516   ASSERT (!f->shr->head_chunk ||
517           f_chunk_includes_pos (f->shr->head_chunk, head));
518
519   if (!f->shr->head_chunk)
520     {
521       f->shr->head_chunk = svm_fifo_find_chunk (f, head);
522       if (PREDICT_FALSE (!f->shr->head_chunk))
523         return 0;
524     }
525
526   end_chunk = f_chunk_end (f->shr->head_chunk);
527
528   return f_pos_lt (end_chunk, tail) ? end_chunk - head : tail - head;
529 }
530
531 u32
532 svm_fifo_max_write_chunk (svm_fifo_t * f)
533 {
534   u32 head, tail;
535
536   f_load_head_tail_prod (f, &head, &tail);
537   ASSERT (!f->shr->tail_chunk ||
538           f_chunk_includes_pos (f->shr->tail_chunk, tail));
539
540   return f->shr->tail_chunk ? f_chunk_end (f->shr->tail_chunk) - tail : 0;
541 }
542
543 static rb_node_t *
544 f_find_node_rbtree (rb_tree_t * rt, u32 pos)
545 {
546   rb_node_t *cur, *prev;
547
548   cur = rb_node (rt, rt->root);
549   if (PREDICT_FALSE (rb_node_is_tnil (rt, cur)))
550     return 0;
551
552   while (pos != cur->key)
553     {
554       prev = cur;
555       if (f_pos_lt (pos, cur->key))
556         {
557           cur = rb_node_left (rt, cur);
558           if (rb_node_is_tnil (rt, cur))
559             {
560               cur = rb_tree_predecessor (rt, prev);
561               break;
562             }
563         }
564       else
565         {
566           cur = rb_node_right (rt, cur);
567           if (rb_node_is_tnil (rt, cur))
568             {
569               cur = prev;
570               break;
571             }
572         }
573     }
574
575   if (rb_node_is_tnil (rt, cur))
576     return 0;
577
578   return cur;
579 }
580
581 static svm_fifo_chunk_t *
582 f_find_chunk_rbtree (rb_tree_t * rt, u32 pos)
583 {
584   svm_fifo_chunk_t *c;
585   rb_node_t *n;
586
587   if (!rb_tree_is_init (rt))
588     return 0;
589
590   n = f_find_node_rbtree (rt, pos);
591   if (!n)
592     return 0;
593   c = uword_to_pointer (n->opaque, svm_fifo_chunk_t *);
594   if (f_chunk_includes_pos (c, pos))
595     return c;
596
597   return 0;
598 }
599
600 static void
601 f_update_ooo_enq (svm_fifo_t * f, u32 start_pos, u32 end_pos)
602 {
603   rb_tree_t *rt = &f->ooo_enq_lookup;
604   svm_fifo_chunk_t *c;
605   rb_node_t *cur;
606
607   /* Use linear search if rbtree is not initialized */
608   if (PREDICT_FALSE (!rb_tree_is_init (rt)))
609     {
610       f->ooo_enq = svm_fifo_find_next_chunk (f, f->shr->tail_chunk, start_pos);
611       return;
612     }
613
614   if (rt->root == RBTREE_TNIL_INDEX)
615     {
616       c = f->shr->tail_chunk;
617       ASSERT (c->enq_rb_index == RBTREE_TNIL_INDEX);
618       c->enq_rb_index = rb_tree_add_custom (rt, c->start_byte,
619                                             pointer_to_uword (c), f_pos_lt);
620     }
621   else
622     {
623       cur = f_find_node_rbtree (rt, start_pos);
624       c = uword_to_pointer (cur->opaque, svm_fifo_chunk_t *);
625       ASSERT (f_pos_leq (c->start_byte, start_pos));
626     }
627
628   if (f_chunk_includes_pos (c, start_pos))
629     f->ooo_enq = c;
630
631   if (f_chunk_includes_pos (c, end_pos))
632     return;
633
634   do
635     {
636       c = c->next;
637       if (!c || c->enq_rb_index != RBTREE_TNIL_INDEX)
638         break;
639
640       c->enq_rb_index = rb_tree_add_custom (rt, c->start_byte,
641                                             pointer_to_uword (c), f_pos_lt);
642
643       if (f_chunk_includes_pos (c, start_pos))
644         f->ooo_enq = c;
645     }
646   while (!f_chunk_includes_pos (c, end_pos));
647 }
648
649 static void
650 f_update_ooo_deq (svm_fifo_t * f, u32 start_pos, u32 end_pos)
651 {
652   rb_tree_t *rt = &f->ooo_deq_lookup;
653   rb_node_t *cur;
654   svm_fifo_chunk_t *c;
655
656   /* Use linear search if rbtree is not initialized  */
657   if (PREDICT_FALSE (!rb_tree_is_init (rt)))
658     {
659       f->ooo_deq = svm_fifo_find_chunk (f, start_pos);
660       return;
661     }
662
663   if (rt->root == RBTREE_TNIL_INDEX)
664     {
665       c = f->shr->start_chunk;
666       ASSERT (c->deq_rb_index == RBTREE_TNIL_INDEX);
667       c->deq_rb_index = rb_tree_add_custom (rt, c->start_byte,
668                                             pointer_to_uword (c), f_pos_lt);
669     }
670   else
671     {
672       cur = f_find_node_rbtree (rt, start_pos);
673       c = uword_to_pointer (cur->opaque, svm_fifo_chunk_t *);
674       ASSERT (f_pos_leq (c->start_byte, start_pos));
675     }
676
677   if (f_chunk_includes_pos (c, start_pos))
678     f->ooo_deq = c;
679
680   if (f_chunk_includes_pos (c, end_pos))
681     return;
682
683   do
684     {
685       c = c->next;
686       if (!c || c->deq_rb_index != RBTREE_TNIL_INDEX)
687         break;
688
689       c->deq_rb_index = rb_tree_add_custom (rt, c->start_byte,
690                                             pointer_to_uword (c), f_pos_lt);
691
692       if (f_chunk_includes_pos (c, start_pos))
693         f->ooo_deq = c;
694     }
695   while (!f_chunk_includes_pos (c, end_pos));
696 }
697
698 static svm_fifo_chunk_t *
699 f_lookup_clear_enq_chunks (svm_fifo_t * f, svm_fifo_chunk_t * start,
700                            u32 end_pos)
701 {
702   rb_tree_t *rt = &f->ooo_enq_lookup;
703   svm_fifo_chunk_t *c;
704   rb_node_t *n;
705
706   c = start;
707   while (c && !f_chunk_includes_pos (c, end_pos))
708     {
709       if (c->enq_rb_index != RBTREE_TNIL_INDEX)
710         {
711           n = rb_node (rt, c->enq_rb_index);
712           rb_tree_del_node (rt, n);
713           c->enq_rb_index = RBTREE_TNIL_INDEX;
714         }
715
716       c = c->next;
717     }
718
719   /* No ooo segments left, so make sure the current chunk
720    * is not tracked in the enq rbtree */
721   if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX
722       && c && c->enq_rb_index != RBTREE_TNIL_INDEX)
723     {
724       n = rb_node (rt, c->enq_rb_index);
725       rb_tree_del_node (rt, n);
726       c->enq_rb_index = RBTREE_TNIL_INDEX;
727     }
728
729   return c;
730 }
731
732 static svm_fifo_chunk_t *
733 f_lookup_clear_deq_chunks (svm_fifo_t * f, svm_fifo_chunk_t * start,
734                            u32 end_pos)
735 {
736   rb_tree_t *rt = &f->ooo_deq_lookup;
737   svm_fifo_chunk_t *c;
738   rb_node_t *n;
739
740   c = start;
741   while (c && !f_chunk_includes_pos (c, end_pos))
742     {
743       if (c->deq_rb_index != RBTREE_TNIL_INDEX)
744         {
745           n = rb_node (rt, c->deq_rb_index);
746           rb_tree_del_node (rt, n);
747           c->deq_rb_index = RBTREE_TNIL_INDEX;
748         }
749
750       c = c->next;
751     }
752
753   return c;
754 }
755
756 void
757 svm_fifo_free_chunk_lookup (svm_fifo_t * f)
758 {
759   rb_tree_free_nodes (&f->ooo_enq_lookup);
760   rb_tree_free_nodes (&f->ooo_deq_lookup);
761 }
762
763 void
764 svm_fifo_free (svm_fifo_t * f)
765 {
766   ASSERT (f->refcnt > 0);
767
768   if (--f->refcnt == 0)
769     {
770       /* ooo data is not allocated on segment heap */
771       svm_fifo_free_chunk_lookup (f);
772       clib_mem_free (f);
773     }
774 }
775
776 void
777 svm_fifo_overwrite_head (svm_fifo_t * f, u8 * src, u32 len)
778 {
779   u32 n_chunk;
780   u32 head, tail, head_idx;
781   svm_fifo_chunk_t *c;
782
783   ASSERT (len <= f->shr->size);
784
785   f_load_head_tail_cons (f, &head, &tail);
786
787   if (!f->shr->head_chunk)
788     f->shr->head_chunk = svm_fifo_find_chunk (f, head);
789
790   c = f->shr->head_chunk;
791   head_idx = head - c->start_byte;
792   n_chunk = c->length - head_idx;
793   if (len <= n_chunk)
794     clib_memcpy_fast (&c->data[head_idx], src, len);
795   else
796     {
797       ASSERT (len - n_chunk <= c->next->length);
798       clib_memcpy_fast (&c->data[head_idx], src, n_chunk);
799       clib_memcpy_fast (&c->next->data[0], src + n_chunk, len - n_chunk);
800     }
801 }
802
803 static int
804 f_try_chunk_alloc (svm_fifo_t * f, u32 head, u32 tail, u32 len)
805 {
806   svm_fifo_chunk_t *c, *cur, *prev;
807   u32 alloc_size, free_alloced;
808
809   free_alloced = f_chunk_end (f->shr->end_chunk) - tail;
810
811   alloc_size = clib_min (f->shr->min_alloc, f->shr->size - (tail - head));
812   alloc_size = clib_max (alloc_size, len - free_alloced);
813
814   c = fsh_alloc_chunk (f->fs_hdr, f->shr->slice_index, alloc_size);
815   if (PREDICT_FALSE (!c))
816     return -1;
817
818   cur = c;
819   prev = f->shr->end_chunk;
820
821   while (cur)
822     {
823       cur->start_byte = prev->start_byte + prev->length;
824       cur->enq_rb_index = RBTREE_TNIL_INDEX;
825       cur->deq_rb_index = RBTREE_TNIL_INDEX;
826
827       prev = cur;
828       cur = cur->next;
829     }
830
831   prev->next = 0;
832   f->shr->end_chunk->next = c;
833   f->shr->end_chunk = prev;
834
835   if (!f->shr->tail_chunk)
836     f->shr->tail_chunk = c;
837
838   return 0;
839 }
840
841 int
842 svm_fifo_enqueue (svm_fifo_t * f, u32 len, const u8 * src)
843 {
844   u32 tail, head, free_count;
845   svm_fifo_chunk_t *old_tail_c;
846
847   f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
848
849   f_load_head_tail_prod (f, &head, &tail);
850
851   /* free space in fifo can only increase during enqueue: SPSC */
852   free_count = f_free_count (f, head, tail);
853
854   if (PREDICT_FALSE (free_count == 0))
855     return SVM_FIFO_EFULL;
856
857   /* number of bytes we're going to copy */
858   len = clib_min (free_count, len);
859
860   if (f_pos_gt (tail + len, f_chunk_end (f->shr->end_chunk)))
861     {
862       if (PREDICT_FALSE (f_try_chunk_alloc (f, head, tail, len)))
863         {
864           len = f_chunk_end (f->shr->end_chunk) - tail;
865           if (!len)
866             return SVM_FIFO_EGROW;
867         }
868     }
869
870   old_tail_c = f->shr->tail_chunk;
871
872   svm_fifo_copy_to_chunk (f, f->shr->tail_chunk, tail, src, len,
873                           &f->shr->tail_chunk);
874   tail = tail + len;
875
876   svm_fifo_trace_add (f, head, len, 2);
877
878   /* collect out-of-order segments */
879   if (PREDICT_FALSE (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX))
880     {
881       len += ooo_segment_try_collect (f, len, &tail);
882       /* Tail chunk might've changed even if nothing was collected */
883       f->shr->tail_chunk = f_lookup_clear_enq_chunks (f, old_tail_c, tail);
884       f->ooo_enq = 0;
885     }
886
887   /* store-rel: producer owned index (paired with load-acq in consumer) */
888   clib_atomic_store_rel_n (&f->shr->tail, tail);
889
890   return len;
891 }
892
893 /**
894  * Enqueue a future segment.
895  *
896  * Two choices: either copies the entire segment, or copies nothing
897  * Returns 0 of the entire segment was copied
898  * Returns -1 if none of the segment was copied due to lack of space
899  */
900 int
901 svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 len, u8 * src)
902 {
903   u32 tail, head, free_count, enq_pos;
904
905   f_load_head_tail_prod (f, &head, &tail);
906
907   /* free space in fifo can only increase during enqueue: SPSC */
908   free_count = f_free_count (f, head, tail);
909   f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
910
911   /* will this request fit? */
912   if ((len + offset) > free_count)
913     return SVM_FIFO_EFULL;
914
915   enq_pos = tail + offset;
916
917   if (f_pos_gt (enq_pos + len, f_chunk_end (f->shr->end_chunk)))
918     {
919       if (PREDICT_FALSE (f_try_chunk_alloc (f, head, tail, offset + len)))
920         return SVM_FIFO_EGROW;
921     }
922
923   svm_fifo_trace_add (f, offset, len, 1);
924   ooo_segment_add (f, offset, head, tail, len);
925
926   if (!f->ooo_enq || !f_chunk_includes_pos (f->ooo_enq, enq_pos))
927     f_update_ooo_enq (f, enq_pos, enq_pos + len);
928
929   svm_fifo_copy_to_chunk (f, f->ooo_enq, enq_pos, src, len, &f->ooo_enq);
930
931   return 0;
932 }
933
934 /**
935  * Advance tail
936  */
937 void
938 svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 len)
939 {
940   u32 tail;
941
942   ASSERT (len <= svm_fifo_max_enqueue_prod (f));
943   /* load-relaxed: producer owned index */
944   tail = f->shr->tail;
945   tail = tail + len;
946
947   if (rb_tree_is_init (&f->ooo_enq_lookup))
948     {
949       f->shr->tail_chunk =
950         f_lookup_clear_enq_chunks (f, f->shr->tail_chunk, tail);
951       f->ooo_enq = 0;
952     }
953   else
954     {
955       f->shr->tail_chunk =
956         svm_fifo_find_next_chunk (f, f->shr->tail_chunk, tail);
957     }
958
959   /* store-rel: producer owned index (paired with load-acq in consumer) */
960   clib_atomic_store_rel_n (&f->shr->tail, tail);
961 }
962
963 int
964 svm_fifo_enqueue_segments (svm_fifo_t * f, const svm_fifo_seg_t segs[],
965                            u32 n_segs, u8 allow_partial)
966 {
967   u32 tail, head, free_count, len = 0, i;
968   svm_fifo_chunk_t *old_tail_c;
969
970   f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
971
972   f_load_head_tail_prod (f, &head, &tail);
973
974   /* free space in fifo can only increase during enqueue: SPSC */
975   free_count = f_free_count (f, head, tail);
976
977   if (PREDICT_FALSE (free_count == 0))
978     return SVM_FIFO_EFULL;
979
980   for (i = 0; i < n_segs; i++)
981     len += segs[i].len;
982
983   old_tail_c = f->shr->tail_chunk;
984
985   if (!allow_partial)
986     {
987       if (PREDICT_FALSE (free_count < len))
988         return SVM_FIFO_EFULL;
989
990       if (f_pos_gt (tail + len, f_chunk_end (f->shr->end_chunk)))
991         {
992           if (PREDICT_FALSE (f_try_chunk_alloc (f, head, tail, len)))
993             return SVM_FIFO_EGROW;
994         }
995
996       for (i = 0; i < n_segs; i++)
997         {
998           svm_fifo_copy_to_chunk (f, f->shr->tail_chunk, tail, segs[i].data,
999                                   segs[i].len, &f->shr->tail_chunk);
1000           tail += segs[i].len;
1001         }
1002     }
1003   else
1004     {
1005       len = clib_min (free_count, len);
1006
1007       if (f_pos_gt (tail + len, f_chunk_end (f->shr->end_chunk)))
1008         {
1009           if (PREDICT_FALSE (f_try_chunk_alloc (f, head, tail, len)))
1010             {
1011               len = f_chunk_end (f->shr->end_chunk) - tail;
1012               if (!len)
1013                 return SVM_FIFO_EGROW;
1014             }
1015         }
1016
1017       i = 0;
1018       while (len)
1019         {
1020           u32 to_copy = clib_min (segs[i].len, len);
1021           svm_fifo_copy_to_chunk (f, f->shr->tail_chunk, tail, segs[i].data,
1022                                   to_copy, &f->shr->tail_chunk);
1023           len -= to_copy;
1024           tail += to_copy;
1025           i++;
1026         }
1027     }
1028
1029   /* collect out-of-order segments */
1030   if (PREDICT_FALSE (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX))
1031     {
1032       len += ooo_segment_try_collect (f, len, &tail);
1033       /* Tail chunk might've changed even if nothing was collected */
1034       f->shr->tail_chunk = f_lookup_clear_enq_chunks (f, old_tail_c, tail);
1035       f->ooo_enq = 0;
1036     }
1037
1038   /* store-rel: producer owned index (paired with load-acq in consumer) */
1039   clib_atomic_store_rel_n (&f->shr->tail, tail);
1040
1041   return len;
1042 }
1043
1044 always_inline svm_fifo_chunk_t *
1045 f_unlink_chunks (svm_fifo_t * f, u32 end_pos, u8 maybe_ooo)
1046 {
1047   svm_fifo_chunk_t *start, *prev = 0, *c;
1048   rb_tree_t *rt;
1049   rb_node_t *n;
1050
1051   ASSERT (!f_chunk_includes_pos (f->shr->start_chunk, end_pos));
1052
1053   if (maybe_ooo)
1054     rt = &f->ooo_deq_lookup;
1055
1056   c = f->shr->start_chunk;
1057
1058   do
1059     {
1060       if (maybe_ooo && c->deq_rb_index != RBTREE_TNIL_INDEX)
1061         {
1062           n = rb_node (rt, c->deq_rb_index);
1063           ASSERT (n == f_find_node_rbtree (rt, c->start_byte));
1064           rb_tree_del_node (rt, n);
1065           c->deq_rb_index = RBTREE_TNIL_INDEX;
1066         }
1067       if (!c->next)
1068         break;
1069       prev = c;
1070       c = c->next;
1071     }
1072   while (!f_chunk_includes_pos (c, end_pos));
1073
1074   if (maybe_ooo)
1075     {
1076       if (f->ooo_deq && f_pos_lt (f->ooo_deq->start_byte, f_chunk_end (c)))
1077         f->ooo_deq = 0;
1078     }
1079   else
1080     {
1081       if (PREDICT_FALSE (f->ooo_deq != 0))
1082         f->ooo_deq = 0;
1083     }
1084
1085   /* Avoid unlinking the last chunk */
1086   if (!prev)
1087     return 0;
1088
1089   prev->next = 0;
1090   start = f->shr->start_chunk;
1091   f->shr->start_chunk = c;
1092
1093   return start;
1094 }
1095
1096 int
1097 svm_fifo_dequeue (svm_fifo_t * f, u32 len, u8 * dst)
1098 {
1099   u32 tail, head, cursize;
1100
1101   f_load_head_tail_cons (f, &head, &tail);
1102
1103   /* current size of fifo can only increase during dequeue: SPSC */
1104   cursize = f_cursize (f, head, tail);
1105
1106   if (PREDICT_FALSE (cursize == 0))
1107     return SVM_FIFO_EEMPTY;
1108
1109   len = clib_min (cursize, len);
1110
1111   if (!f->shr->head_chunk)
1112     f->shr->head_chunk = svm_fifo_find_chunk (f, head);
1113
1114   svm_fifo_copy_from_chunk (f, f->shr->head_chunk, head, dst, len,
1115                             &f->shr->head_chunk);
1116   head = head + len;
1117
1118   /* In order dequeues are not supported in combination with ooo peeking.
1119    * Use svm_fifo_dequeue_drop instead. */
1120   ASSERT (rb_tree_n_nodes (&f->ooo_deq_lookup) <= 1);
1121
1122   if (f_pos_geq (head, f_chunk_end (f->shr->start_chunk)))
1123     fsh_collect_chunks (f->fs_hdr, f->shr->slice_index,
1124                         f_unlink_chunks (f, head, 0));
1125
1126   /* store-rel: consumer owned index (paired with load-acq in producer) */
1127   clib_atomic_store_rel_n (&f->shr->head, head);
1128
1129   return len;
1130 }
1131
1132 int
1133 svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 len, u8 * dst)
1134 {
1135   u32 tail, head, cursize, head_idx;
1136
1137   f_load_head_tail_cons (f, &head, &tail);
1138
1139   /* current size of fifo can only increase during peek: SPSC */
1140   cursize = f_cursize (f, head, tail);
1141
1142   if (PREDICT_FALSE (cursize < offset))
1143     return SVM_FIFO_EEMPTY;
1144
1145   len = clib_min (cursize - offset, len);
1146   head_idx = head + offset;
1147
1148   CLIB_MEM_UNPOISON (f->ooo_deq, sizeof (*f->ooo_deq));
1149   if (!f->ooo_deq || !f_chunk_includes_pos (f->ooo_deq, head_idx))
1150     f_update_ooo_deq (f, head_idx, head_idx + len);
1151
1152   svm_fifo_copy_from_chunk (f, f->ooo_deq, head_idx, dst, len, &f->ooo_deq);
1153   return len;
1154 }
1155
1156 int
1157 svm_fifo_dequeue_drop (svm_fifo_t * f, u32 len)
1158 {
1159   u32 total_drop_bytes, tail, head, cursize;
1160
1161   f_load_head_tail_cons (f, &head, &tail);
1162
1163   /* number of bytes available */
1164   cursize = f_cursize (f, head, tail);
1165   if (PREDICT_FALSE (cursize == 0))
1166     return SVM_FIFO_EEMPTY;
1167
1168   /* number of bytes we're going to drop */
1169   total_drop_bytes = clib_min (cursize, len);
1170
1171   svm_fifo_trace_add (f, tail, total_drop_bytes, 3);
1172
1173   /* move head */
1174   head = head + total_drop_bytes;
1175
1176   if (f_pos_geq (head, f_chunk_end (f->shr->start_chunk)))
1177     {
1178       fsh_collect_chunks (f->fs_hdr, f->shr->slice_index,
1179                           f_unlink_chunks (f, head, 1));
1180       f->shr->head_chunk = f_chunk_includes_pos (f->shr->start_chunk, head) ?
1181                              f->shr->start_chunk :
1182                              0;
1183     }
1184
1185   /* store-rel: consumer owned index (paired with load-acq in producer) */
1186   clib_atomic_store_rel_n (&f->shr->head, head);
1187
1188   return total_drop_bytes;
1189 }
1190
1191 /**
1192  * Drop all data from fifo
1193  *
1194  */
1195 void
1196 svm_fifo_dequeue_drop_all (svm_fifo_t * f)
1197 {
1198   u32 head, tail;
1199
1200   f_load_head_tail_all_acq (f, &head, &tail);
1201
1202   if (!f->shr->head_chunk || !f_chunk_includes_pos (f->shr->head_chunk, head))
1203     f->shr->head_chunk = svm_fifo_find_chunk (f, head);
1204
1205   f->shr->head_chunk = f_lookup_clear_deq_chunks (f, f->shr->head_chunk, tail);
1206
1207   if (f_pos_geq (tail, f_chunk_end (f->shr->start_chunk)))
1208     fsh_collect_chunks (f->fs_hdr, f->shr->slice_index,
1209                         f_unlink_chunks (f, tail, 0));
1210
1211   /* store-rel: consumer owned index (paired with load-acq in producer) */
1212   clib_atomic_store_rel_n (&f->shr->head, tail);
1213 }
1214
1215 int
1216 svm_fifo_fill_chunk_list (svm_fifo_t * f)
1217 {
1218   u32 head, tail;
1219
1220   f_load_head_tail_prod (f, &head, &tail);
1221
1222   if (f_chunk_end (f->shr->end_chunk) - head >= f->shr->size)
1223     return 0;
1224
1225   if (f_try_chunk_alloc (f, head, tail, f->shr->size - (tail - head)))
1226     return SVM_FIFO_EGROW;
1227
1228   return 0;
1229 }
1230
1231 int
1232 svm_fifo_provision_chunks (svm_fifo_t *f, svm_fifo_seg_t *fs, u32 n_segs,
1233                            u32 len)
1234 {
1235   u32 head, tail, n_avail, head_pos, n_bytes, fs_index = 1, clen;
1236   svm_fifo_chunk_t *c;
1237
1238   f_load_head_tail_prod (f, &head, &tail);
1239
1240   if (f_free_count (f, head, tail) < len)
1241     return SVM_FIFO_EFULL;
1242
1243   n_avail = f_chunk_end (f->shr->end_chunk) - tail;
1244
1245   if (n_avail < len && f_try_chunk_alloc (f, head, tail, len))
1246     return SVM_FIFO_EGROW;
1247
1248   c = f->shr->tail_chunk;
1249   head_pos = (tail - c->start_byte);
1250   fs[0].data = c->data + head_pos;
1251   fs[0].len = clib_min (c->length - head_pos, len);
1252   n_bytes = fs[0].len;
1253
1254   while (n_bytes < len && fs_index < n_segs)
1255     {
1256       c = c->next;
1257       clen = clib_min (c->length, len - n_bytes);
1258       fs[fs_index].data = c->data;
1259       fs[fs_index].len = clen;
1260       n_bytes += clen;
1261       fs_index += 1;
1262     }
1263
1264   return fs_index;
1265 }
1266
1267 int
1268 svm_fifo_segments (svm_fifo_t * f, u32 offset, svm_fifo_seg_t * fs,
1269                    u32 n_segs, u32 max_bytes)
1270 {
1271   u32 cursize, to_read, head, tail, fs_index = 1;
1272   u32 n_bytes, head_pos, len, start;
1273   svm_fifo_chunk_t *c;
1274
1275   f_load_head_tail_cons (f, &head, &tail);
1276
1277   /* consumer function, cursize can only increase while we're working */
1278   cursize = f_cursize (f, head, tail);
1279
1280   if (PREDICT_FALSE (cursize == 0))
1281     return SVM_FIFO_EEMPTY;
1282
1283   if (offset >= cursize)
1284     return SVM_FIFO_EEMPTY;
1285
1286   to_read = clib_min (cursize - offset, max_bytes);
1287   start = head + offset;
1288
1289   if (!f->shr->head_chunk)
1290     f->shr->head_chunk = svm_fifo_find_chunk (f, head);
1291
1292   c = f->shr->head_chunk;
1293
1294   while (!f_chunk_includes_pos (c, start))
1295     c = c->next;
1296
1297   head_pos = start - c->start_byte;
1298   fs[0].data = c->data + head_pos;
1299   fs[0].len = clib_min (c->length - head_pos, to_read);
1300   n_bytes = fs[0].len;
1301
1302   while (n_bytes < to_read && fs_index < n_segs)
1303     {
1304       c = c->next;
1305       len = clib_min (c->length, to_read - n_bytes);
1306       fs[fs_index].data = c->data;
1307       fs[fs_index].len = len;
1308       n_bytes += len;
1309       fs_index += 1;
1310     }
1311
1312   return n_bytes;
1313 }
1314
1315 /**
1316  * Clones fifo
1317  *
1318  * Assumptions:
1319  * - no prod and cons are accessing either dest or src fifo
1320  * - fifo is not multi chunk
1321  */
1322 void
1323 svm_fifo_clone (svm_fifo_t * df, svm_fifo_t * sf)
1324 {
1325   u32 head, tail;
1326
1327   /* Support only single chunk clones for now */
1328   ASSERT (svm_fifo_n_chunks (sf) == 1);
1329
1330   clib_memcpy_fast (df->shr->head_chunk->data, sf->shr->head_chunk->data,
1331                     sf->shr->size);
1332
1333   f_load_head_tail_all_acq (sf, &head, &tail);
1334   clib_atomic_store_rel_n (&df->shr->head, head);
1335   clib_atomic_store_rel_n (&df->shr->tail, tail);
1336 }
1337
1338 u32
1339 svm_fifo_n_ooo_segments (svm_fifo_t * f)
1340 {
1341   return pool_elts (f->ooo_segments);
1342 }
1343
1344 ooo_segment_t *
1345 svm_fifo_first_ooo_segment (svm_fifo_t * f)
1346 {
1347   return pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
1348 }
1349
1350 /**
1351  * Set fifo pointers to requested offset
1352  */
1353 void
1354 svm_fifo_init_pointers (svm_fifo_t * f, u32 head, u32 tail)
1355 {
1356   svm_fifo_chunk_t *c;
1357
1358   clib_atomic_store_rel_n (&f->shr->head, head);
1359   clib_atomic_store_rel_n (&f->shr->tail, tail);
1360
1361   c = svm_fifo_find_chunk (f, head);
1362   ASSERT (c != 0);
1363   f->shr->head_chunk = f->ooo_deq = c;
1364   c = svm_fifo_find_chunk (f, tail);
1365   ASSERT (c != 0);
1366   f->shr->tail_chunk = f->ooo_enq = c;
1367 }
1368
1369 void
1370 svm_fifo_add_subscriber (svm_fifo_t * f, u8 subscriber)
1371 {
1372   if (f->shr->n_subscribers >= SVM_FIFO_MAX_EVT_SUBSCRIBERS)
1373     return;
1374   f->shr->subscribers[f->shr->n_subscribers++] = subscriber;
1375 }
1376
1377 void
1378 svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber)
1379 {
1380   int i;
1381
1382   for (i = 0; i < f->shr->n_subscribers; i++)
1383     {
1384       if (f->shr->subscribers[i] != subscriber)
1385         continue;
1386       f->shr->subscribers[i] = f->shr->subscribers[f->shr->n_subscribers - 1];
1387       f->shr->n_subscribers--;
1388       break;
1389     }
1390 }
1391
1392 u8
1393 svm_fifo_is_sane (svm_fifo_t * f)
1394 {
1395   svm_fifo_chunk_t *tmp;
1396
1397   if (f->shr->head_chunk &&
1398       !f_chunk_includes_pos (f->shr->head_chunk, f->shr->head))
1399     return 0;
1400   if (f->shr->tail_chunk &&
1401       !f_chunk_includes_pos (f->shr->tail_chunk, f->shr->tail))
1402     return 0;
1403   if (f->ooo_deq)
1404     {
1405       if (rb_tree_is_init (&f->ooo_deq_lookup))
1406         {
1407           if (f_pos_lt (f->ooo_deq->start_byte,
1408                         f->shr->start_chunk->start_byte) ||
1409               f_pos_gt (f->ooo_deq->start_byte,
1410                         f_chunk_end (f->shr->end_chunk)))
1411             return 0;
1412
1413           tmp = f_find_chunk_rbtree (&f->ooo_deq_lookup,
1414                                      f->ooo_deq->start_byte);
1415         }
1416       else
1417         tmp = svm_fifo_find_chunk (f, f->ooo_deq->start_byte);
1418       if (tmp != f->ooo_deq)
1419         return 0;
1420     }
1421   if (f->ooo_enq)
1422     {
1423       if (rb_tree_is_init (&f->ooo_enq_lookup))
1424         {
1425           if (f_pos_lt (f->ooo_enq->start_byte,
1426                         f->shr->start_chunk->start_byte) ||
1427               f_pos_gt (f->ooo_enq->start_byte,
1428                         f_chunk_end (f->shr->end_chunk)))
1429             return 0;
1430
1431           tmp = f_find_chunk_rbtree (&f->ooo_enq_lookup,
1432                                      f->ooo_enq->start_byte);
1433         }
1434       else
1435         {
1436           tmp = svm_fifo_find_next_chunk (f, f->shr->tail_chunk,
1437                                           f->ooo_enq->start_byte);
1438         }
1439       if (tmp != f->ooo_enq)
1440         return 0;
1441     }
1442
1443   if (f->shr->start_chunk->next)
1444     {
1445       svm_fifo_chunk_t *c, *prev = 0, *tmp;
1446       u32 chunks_bytes = 0;
1447
1448       c = f->shr->start_chunk;
1449       do
1450         {
1451           tmp = svm_fifo_find_chunk (f, c->start_byte);
1452           if (tmp != c)
1453             return 0;
1454           if (prev && (prev->start_byte + prev->length != c->start_byte))
1455             return 0;
1456
1457           if (c->enq_rb_index != RBTREE_TNIL_INDEX)
1458             {
1459               tmp = f_find_chunk_rbtree (&f->ooo_enq_lookup, c->start_byte);
1460               if (tmp)
1461                 {
1462                   if (tmp != c)
1463                     return 0;
1464                 }
1465             }
1466           if (c->deq_rb_index != RBTREE_TNIL_INDEX)
1467             {
1468               tmp = f_find_chunk_rbtree (&f->ooo_deq_lookup, c->start_byte);
1469               if (tmp)
1470                 {
1471                   if (tmp != c)
1472                     return 0;
1473                 }
1474             }
1475
1476           chunks_bytes += c->length;
1477           prev = c;
1478           c = c->next;
1479         }
1480       while (c);
1481
1482       if (chunks_bytes < f->shr->tail - f->shr->head)
1483         return 0;
1484     }
1485
1486   return 1;
1487 }
1488
1489 u32
1490 svm_fifo_n_chunks (svm_fifo_t * f)
1491 {
1492   svm_fifo_chunk_t *c;
1493   int n_chunks = 0;
1494
1495   c = f->shr->start_chunk;
1496   while (c)
1497     {
1498       n_chunks++;
1499       c = c->next;
1500     }
1501
1502   return n_chunks;
1503 }
1504
1505 u8 *
1506 format_ooo_segment (u8 * s, va_list * args)
1507 {
1508   svm_fifo_t __clib_unused *f = va_arg (*args, svm_fifo_t *);
1509   ooo_segment_t *seg = va_arg (*args, ooo_segment_t *);
1510   s = format (s, "[%u, %u], len %u, next %d, prev %d", seg->start,
1511               seg->start + seg->length, seg->length, seg->next, seg->prev);
1512   return s;
1513 }
1514
1515 u8 *
1516 svm_fifo_dump_trace (u8 * s, svm_fifo_t * f)
1517 {
1518 #if SVM_FIFO_TRACE
1519   svm_fifo_trace_elem_t *seg = 0;
1520   int i = 0;
1521
1522   if (f->trace)
1523     {
1524       vec_foreach (seg, f->trace)
1525       {
1526         s = format (s, "{%u, %u, %u}, ", seg->offset, seg->len, seg->action);
1527         i++;
1528         if (i % 5 == 0)
1529           s = format (s, "\n");
1530       }
1531       s = format (s, "\n");
1532     }
1533   return s;
1534 #else
1535   return 0;
1536 #endif
1537 }
1538
1539 u8 *
1540 svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose)
1541 {
1542   int i, trace_len;
1543   u8 *data = 0;
1544   svm_fifo_trace_elem_t *trace;
1545   u32 offset;
1546   svm_fifo_t *placeholder_fifo;
1547
1548   if (!f)
1549     return s;
1550
1551 #if SVM_FIFO_TRACE
1552   trace = f->trace;
1553   trace_len = vec_len (trace);
1554 #else
1555   trace = 0;
1556   trace_len = 0;
1557 #endif
1558
1559   placeholder_fifo = svm_fifo_alloc (f->shr->size);
1560   svm_fifo_init (f, f->shr->size);
1561   clib_memset (f->shr->head_chunk->data, 0xFF, f->shr->size);
1562   vec_validate (data, f->shr->size);
1563   for (i = 0; i < vec_len (data); i++)
1564     data[i] = i;
1565
1566   for (i = 0; i < trace_len; i++)
1567     {
1568       offset = trace[i].offset;
1569       if (trace[i].action == 1)
1570         {
1571           if (verbose)
1572             s = format (s, "adding [%u, %u]:", trace[i].offset,
1573                         (trace[i].offset + trace[i].len));
1574           svm_fifo_enqueue_with_offset (placeholder_fifo, trace[i].offset,
1575                                         trace[i].len, &data[offset]);
1576         }
1577       else if (trace[i].action == 2)
1578         {
1579           if (verbose)
1580             s = format (s, "adding [%u, %u]:", 0, trace[i].len);
1581           svm_fifo_enqueue (placeholder_fifo, trace[i].len, &data[offset]);
1582         }
1583       else if (!no_read)
1584         {
1585           if (verbose)
1586             s = format (s, "read: %u", trace[i].len);
1587           svm_fifo_dequeue_drop (placeholder_fifo, trace[i].len);
1588         }
1589       if (verbose)
1590         s = format (s, "%U", format_svm_fifo, placeholder_fifo, 1);
1591     }
1592
1593   s = format (s, "result: %U", format_svm_fifo, placeholder_fifo, 1);
1594
1595   return s;
1596 }
1597
1598 u8 *
1599 format_ooo_list (u8 * s, va_list * args)
1600 {
1601   svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
1602   u32 indent = va_arg (*args, u32);
1603   u32 ooo_segment_index = f->ooos_list_head;
1604   ooo_segment_t *seg;
1605
1606   while (ooo_segment_index != OOO_SEGMENT_INVALID_INDEX)
1607     {
1608       seg = pool_elt_at_index (f->ooo_segments, ooo_segment_index);
1609       s = format (s, "%U%U\n", format_white_space, indent, format_ooo_segment,
1610                   f, seg);
1611       ooo_segment_index = seg->next;
1612     }
1613
1614   return s;
1615 }
1616
1617 u8 *
1618 format_svm_fifo (u8 * s, va_list * args)
1619 {
1620   svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
1621   int verbose = va_arg (*args, int);
1622   u32 indent;
1623
1624   if (!s)
1625     return s;
1626
1627   indent = format_get_indent (s);
1628   s = format (s, "cursize %u nitems %u has_event %d min_alloc %u\n",
1629               svm_fifo_max_dequeue (f), f->shr->size, f->shr->has_event,
1630               f->shr->min_alloc);
1631   s = format (s, "%Uhead %u tail %u segment manager %u\n", format_white_space,
1632               indent, f->shr->head, f->shr->tail, f->segment_manager);
1633
1634   if (verbose > 1)
1635     s = format (s, "%Uvpp session %d thread %d app session %d thread %d\n",
1636                 format_white_space, indent, f->shr->master_session_index,
1637                 f->master_thread_index, f->shr->client_session_index,
1638                 f->client_thread_index);
1639
1640   if (verbose)
1641     {
1642       s = format (s, "%Uooo pool %d active elts newest %u\n",
1643                   format_white_space, indent, pool_elts (f->ooo_segments),
1644                   f->ooos_newest);
1645       if (svm_fifo_has_ooo_data (f))
1646         s = format (s, " %U", format_ooo_list, f, indent, verbose);
1647     }
1648   return s;
1649 }
1650
1651 #endif
1652 /*
1653  * fd.io coding-style-patch-verification: ON
1654  *
1655  * Local Variables:
1656  * eval: (c-set-style "gnu")
1657  * End:
1658  */