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