session: cleanup segment manager and fifo segment
[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 <vppinfra/cpu.h>
22
23 CLIB_MARCH_FN (svm_fifo_copy_to_chunk, void, svm_fifo_t * f,
24                svm_fifo_chunk_t * c, u32 tail_idx, const u8 * src, u32 len,
25                svm_fifo_chunk_t ** last)
26 {
27   u32 n_chunk;
28
29   ASSERT (tail_idx >= c->start_byte && tail_idx < c->start_byte + c->length);
30
31   tail_idx -= c->start_byte;
32   n_chunk = c->length - tail_idx;
33   if (n_chunk <= len)
34     {
35       u32 to_copy = len;
36       clib_memcpy_fast (&c->data[tail_idx], src, n_chunk);
37       c = c->next;
38       while ((to_copy -= n_chunk))
39         {
40           n_chunk = clib_min (c->length, to_copy);
41           clib_memcpy_fast (&c->data[0], src + (len - to_copy), n_chunk);
42           c = c->length <= to_copy ? c->next : c;
43         }
44       if (*last)
45         *last = c;
46     }
47   else
48     {
49       clib_memcpy_fast (&c->data[tail_idx], src, len);
50     }
51 }
52
53 CLIB_MARCH_FN (svm_fifo_copy_from_chunk, void, svm_fifo_t * f,
54                svm_fifo_chunk_t * c, u32 head_idx, u8 * dst, u32 len,
55                svm_fifo_chunk_t ** last)
56 {
57   u32 n_chunk;
58
59   ASSERT (head_idx >= c->start_byte && head_idx < c->start_byte + c->length);
60
61   head_idx -= c->start_byte;
62   n_chunk = c->length - head_idx;
63   if (n_chunk <= len)
64     {
65       u32 to_copy = len;
66       clib_memcpy_fast (dst, &c->data[head_idx], n_chunk);
67       c = c->next;
68       while ((to_copy -= n_chunk))
69         {
70           n_chunk = clib_min (c->length, to_copy);
71           clib_memcpy_fast (dst + (len - to_copy), &c->data[0], n_chunk);
72           c = c->length <= to_copy ? c->next : c;
73         }
74       if (*last)
75         *last = c;
76     }
77   else
78     {
79       clib_memcpy_fast (dst, &c->data[head_idx], len);
80     }
81 }
82
83 #ifndef CLIB_MARCH_VARIANT
84
85 static inline void
86 svm_fifo_copy_to_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c, u32 tail_idx,
87                         const u8 * src, u32 len, svm_fifo_chunk_t ** last)
88 {
89   CLIB_MARCH_FN_SELECT (svm_fifo_copy_to_chunk) (f, c, tail_idx, src, len,
90                                                  last);
91 }
92
93 static inline void
94 svm_fifo_copy_from_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c, u32 head_idx,
95                           u8 * dst, u32 len, svm_fifo_chunk_t ** last)
96 {
97   CLIB_MARCH_FN_SELECT (svm_fifo_copy_from_chunk) (f, c, head_idx, dst, len,
98                                                    last);
99 }
100
101 static inline u8
102 position_lt (svm_fifo_t * f, u32 a, u32 b, u32 tail)
103 {
104   return (ooo_segment_distance_from_tail (f, a, tail)
105           < ooo_segment_distance_from_tail (f, b, tail));
106 }
107
108 static inline u8
109 position_leq (svm_fifo_t * f, u32 a, u32 b, u32 tail)
110 {
111   return (ooo_segment_distance_from_tail (f, a, tail)
112           <= ooo_segment_distance_from_tail (f, b, tail));
113 }
114
115 static inline u8
116 position_gt (svm_fifo_t * f, u32 a, u32 b, u32 tail)
117 {
118   return (ooo_segment_distance_from_tail (f, a, tail)
119           > ooo_segment_distance_from_tail (f, b, tail));
120 }
121
122 static inline u32
123 position_diff (svm_fifo_t * f, u32 posa, u32 posb, u32 tail)
124 {
125   return ooo_segment_distance_from_tail (f, posa, tail)
126     - ooo_segment_distance_from_tail (f, posb, tail);
127 }
128
129 static inline u32
130 ooo_segment_end_pos (svm_fifo_t * f, ooo_segment_t * s)
131 {
132   return s->start + s->length;
133 }
134
135 u8 *
136 format_ooo_segment (u8 * s, va_list * args)
137 {
138   svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
139   ooo_segment_t *seg = va_arg (*args, ooo_segment_t *);
140   u32 normalized_start = (seg->start + f->nitems - f->tail) % f->size;
141   s = format (s, "[%u, %u], len %u, next %d, prev %d", normalized_start,
142               (normalized_start + seg->length) % f->size, seg->length,
143               seg->next, seg->prev);
144   return s;
145 }
146
147 u8 *
148 svm_fifo_dump_trace (u8 * s, svm_fifo_t * f)
149 {
150 #if SVM_FIFO_TRACE
151   svm_fifo_trace_elem_t *seg = 0;
152   int i = 0;
153
154   if (f->trace)
155     {
156       vec_foreach (seg, f->trace)
157       {
158         s = format (s, "{%u, %u, %u}, ", seg->offset, seg->len, seg->action);
159         i++;
160         if (i % 5 == 0)
161           s = format (s, "\n");
162       }
163       s = format (s, "\n");
164     }
165   return s;
166 #else
167   return 0;
168 #endif
169 }
170
171 u8 *
172 svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose)
173 {
174   int i, trace_len;
175   u8 *data = 0;
176   svm_fifo_trace_elem_t *trace;
177   u32 offset;
178   svm_fifo_t *dummy_fifo;
179
180   if (!f)
181     return s;
182
183 #if SVM_FIFO_TRACE
184   trace = f->trace;
185   trace_len = vec_len (trace);
186 #else
187   trace = 0;
188   trace_len = 0;
189 #endif
190
191   dummy_fifo = svm_fifo_create (f->size);
192   clib_memset (f->head_chunk->data, 0xFF, f->nitems);
193   vec_validate (data, f->nitems);
194   for (i = 0; i < vec_len (data); i++)
195     data[i] = i;
196
197   for (i = 0; i < trace_len; i++)
198     {
199       offset = trace[i].offset;
200       if (trace[i].action == 1)
201         {
202           if (verbose)
203             s = format (s, "adding [%u, %u]:", trace[i].offset,
204                         (trace[i].offset + trace[i].len) % dummy_fifo->size);
205           svm_fifo_enqueue_with_offset (dummy_fifo, trace[i].offset,
206                                         trace[i].len, &data[offset]);
207         }
208       else if (trace[i].action == 2)
209         {
210           if (verbose)
211             s = format (s, "adding [%u, %u]:", 0, trace[i].len);
212           svm_fifo_enqueue_nowait (dummy_fifo, trace[i].len, &data[offset]);
213         }
214       else if (!no_read)
215         {
216           if (verbose)
217             s = format (s, "read: %u", trace[i].len);
218           svm_fifo_dequeue_drop (dummy_fifo, trace[i].len);
219         }
220       if (verbose)
221         s = format (s, "%U", format_svm_fifo, dummy_fifo, 1);
222     }
223
224   s = format (s, "result: %U", format_svm_fifo, dummy_fifo, 1);
225
226   return s;
227 }
228
229 u8 *
230 format_ooo_list (u8 * s, va_list * args)
231 {
232   svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
233   u32 indent = va_arg (*args, u32);
234   u32 ooo_segment_index = f->ooos_list_head;
235   ooo_segment_t *seg;
236
237   while (ooo_segment_index != OOO_SEGMENT_INVALID_INDEX)
238     {
239       seg = pool_elt_at_index (f->ooo_segments, ooo_segment_index);
240       s = format (s, "%U%U\n", format_white_space, indent, format_ooo_segment,
241                   f, seg);
242       ooo_segment_index = seg->next;
243     }
244
245   return s;
246 }
247
248 u8 *
249 format_svm_fifo (u8 * s, va_list * args)
250 {
251   svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
252   int verbose = va_arg (*args, int);
253   u32 indent;
254
255   if (!s)
256     return s;
257
258   indent = format_get_indent (s);
259   s = format (s, "cursize %u nitems %u has_event %d\n",
260               svm_fifo_max_dequeue (f), f->nitems, f->has_event);
261   s = format (s, "%Uhead %u tail %u segment manager %u\n", format_white_space,
262               indent, (f->head % f->size), (f->tail % f->size),
263               f->segment_manager);
264
265   if (verbose > 1)
266     s = format (s, "%Uvpp session %d thread %d app session %d thread %d\n",
267                 format_white_space, indent, f->master_session_index,
268                 f->master_thread_index, f->client_session_index,
269                 f->client_thread_index);
270
271   if (verbose)
272     {
273       s = format (s, "%Uooo pool %d active elts newest %u\n",
274                   format_white_space, indent, pool_elts (f->ooo_segments),
275                   f->ooos_newest);
276       if (svm_fifo_has_ooo_data (f))
277         s = format (s, " %U", format_ooo_list, f, indent, verbose);
278     }
279   return s;
280 }
281
282 void
283 svm_fifo_init (svm_fifo_t * f, u32 size)
284 {
285   f->size = size;
286   /*
287    * usable size of the fifo set to rounded_data_size - 1
288    * to differentiate between free fifo and empty fifo.
289    */
290   f->nitems = f->size - 1;
291   f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
292   f->ct_session_index = SVM_FIFO_INVALID_SESSION_INDEX;
293   f->segment_index = SVM_FIFO_INVALID_INDEX;
294   f->refcnt = 1;
295   f->default_chunk.start_byte = 0;
296   f->default_chunk.length = f->size;
297   f->default_chunk.next = f->start_chunk = f->end_chunk = &f->default_chunk;
298   f->head_chunk = f->tail_chunk = f->ooo_enq = f->ooo_deq = f->start_chunk;
299 }
300
301 /** create an svm fifo, in the current heap. Fails vs blow up the process */
302 svm_fifo_t *
303 svm_fifo_create (u32 data_size_in_bytes)
304 {
305   svm_fifo_t *f;
306   u32 rounded_data_size;
307
308   /* always round fifo data size to the next highest power-of-two */
309   rounded_data_size = (1 << (max_log2 (data_size_in_bytes)));
310   f = clib_mem_alloc_aligned_or_null (sizeof (*f) + rounded_data_size,
311                                       CLIB_CACHE_LINE_BYTES);
312   if (f == 0)
313     return 0;
314
315   clib_memset (f, 0, sizeof (*f));
316   svm_fifo_init (f, data_size_in_bytes);
317   return f;
318 }
319
320 static inline void
321 svm_fifo_size_update (svm_fifo_t * f, svm_fifo_chunk_t * c)
322 {
323   svm_fifo_chunk_t *prev;
324   u32 add_bytes = 0;
325
326   if (!c)
327     return;
328
329   f->end_chunk->next = c;
330   while (c)
331     {
332       add_bytes += c->length;
333       prev = c;
334       c = c->next;
335     }
336   f->end_chunk = prev;
337   prev->next = f->start_chunk;
338   f->size += add_bytes;
339   f->nitems = f->size - 1;
340   f->new_chunks = 0;
341 }
342
343 static void
344 svm_fifo_try_size_update (svm_fifo_t * f, u32 new_head)
345 {
346   if (new_head % f->size > f->tail % f->size)
347     return;
348
349   svm_fifo_size_update (f, f->new_chunks);
350   f->flags &= ~SVM_FIFO_F_SIZE_UPDATE;
351 }
352
353 void
354 svm_fifo_add_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c)
355 {
356   svm_fifo_chunk_t *cur, *prev;
357
358   /* Initialize rbtree if needed and add default chunk to it */
359   if (!(f->flags & SVM_FIFO_F_MULTI_CHUNK))
360     {
361       rb_tree_init (&f->chunk_lookup);
362       rb_tree_add2 (&f->chunk_lookup, 0, pointer_to_uword (f->start_chunk));
363       f->flags |= SVM_FIFO_F_MULTI_CHUNK;
364     }
365
366   /* Initialize chunks and add to lookup rbtree. Expectation is that this is
367    * called with the heap where the rbtree's pool is pushed. */
368   cur = c;
369   if (f->new_chunks)
370     {
371       prev = f->new_chunks;
372       while (prev->next)
373         prev = prev->next;
374       prev->next = c;
375     }
376   else
377     prev = f->end_chunk;
378
379   while (cur)
380     {
381       cur->start_byte = prev->start_byte + prev->length;
382       rb_tree_add2 (&f->chunk_lookup, cur->start_byte,
383                     pointer_to_uword (cur));
384       prev = cur;
385       cur = cur->next;
386     }
387
388   /* If fifo is not wrapped, update the size now */
389   if (!svm_fifo_is_wrapped (f))
390     {
391       ASSERT (!f->new_chunks);
392       svm_fifo_size_update (f, c);
393       return;
394     }
395
396   /* Postpone size update */
397   if (!f->new_chunks)
398     {
399       f->new_chunks = c;
400       f->flags |= SVM_FIFO_F_SIZE_UPDATE;
401     }
402 }
403
404 static inline u8
405 svm_fifo_chunk_includes_pos (svm_fifo_chunk_t * c, u32 pos)
406 {
407   return (pos >= c->start_byte && pos < c->start_byte + c->length);
408 }
409
410 /**
411  * Find chunk for given byte position
412  *
413  * @param f     fifo
414  * @param pos   normalized position in fifo
415  *
416  * @return chunk that includes given position or 0
417  */
418 static svm_fifo_chunk_t *
419 svm_fifo_find_chunk (svm_fifo_t * f, u32 pos)
420 {
421   rb_tree_t *rt = &f->chunk_lookup;
422   rb_node_t *cur, *prev;
423   svm_fifo_chunk_t *c;
424
425   cur = rb_node (rt, rt->root);
426   while (pos != cur->key)
427     {
428       prev = cur;
429       if (pos < cur->key)
430         cur = rb_node_left (rt, cur);
431       else
432         cur = rb_node_right (rt, cur);
433
434       if (rb_node_is_tnil (rt, cur))
435         {
436           /* Hit tnil as a left child. Find predecessor */
437           if (pos < prev->key)
438             {
439               cur = rb_tree_predecessor (rt, prev);
440               c = uword_to_pointer (cur->opaque, svm_fifo_chunk_t *);
441               if (svm_fifo_chunk_includes_pos (c, pos))
442                 return c;
443               return 0;
444             }
445           /* Hit tnil as a right child. Check if this is the one, otherwise
446            * search for successor */
447           c = uword_to_pointer (prev->opaque, svm_fifo_chunk_t *);
448           if (svm_fifo_chunk_includes_pos (c, pos))
449             return c;
450
451           cur = rb_tree_successor (rt, prev);
452           c = uword_to_pointer (cur->opaque, svm_fifo_chunk_t *);
453           if (svm_fifo_chunk_includes_pos (c, pos))
454             return c;
455           return 0;
456         }
457     }
458
459   if (!rb_node_is_tnil (rt, cur))
460     return uword_to_pointer (cur->opaque, svm_fifo_chunk_t *);
461   return 0;
462 }
463
464 void
465 svm_fifo_free (svm_fifo_t * f)
466 {
467   ASSERT (f->refcnt > 0);
468
469   if (--f->refcnt == 0)
470     {
471       pool_free (f->ooo_segments);
472       clib_mem_free (f);
473     }
474 }
475
476 always_inline ooo_segment_t *
477 ooo_segment_new (svm_fifo_t * f, u32 start, u32 length)
478 {
479   ooo_segment_t *s;
480
481   pool_get (f->ooo_segments, s);
482
483   s->start = start;
484   s->length = length;
485
486   s->prev = s->next = OOO_SEGMENT_INVALID_INDEX;
487
488   return s;
489 }
490
491 always_inline void
492 ooo_segment_del (svm_fifo_t * f, u32 index)
493 {
494   ooo_segment_t *cur, *prev = 0, *next = 0;
495   cur = pool_elt_at_index (f->ooo_segments, index);
496
497   if (cur->next != OOO_SEGMENT_INVALID_INDEX)
498     {
499       next = pool_elt_at_index (f->ooo_segments, cur->next);
500       next->prev = cur->prev;
501     }
502
503   if (cur->prev != OOO_SEGMENT_INVALID_INDEX)
504     {
505       prev = pool_elt_at_index (f->ooo_segments, cur->prev);
506       prev->next = cur->next;
507     }
508   else
509     {
510       f->ooos_list_head = cur->next;
511     }
512
513   pool_put (f->ooo_segments, cur);
514 }
515
516 /**
517  * Add segment to fifo's out-of-order segment list. Takes care of merging
518  * adjacent segments and removing overlapping ones.
519  */
520 static void
521 ooo_segment_add (svm_fifo_t * f, u32 offset, u32 head, u32 tail, u32 length)
522 {
523   ooo_segment_t *s, *new_s, *prev, *next, *it;
524   u32 new_index, s_end_pos, s_index;
525   u32 offset_pos, offset_end_pos;
526
527   ASSERT (offset + length <= ooo_segment_distance_from_tail (f, head, tail)
528           || head == tail);
529
530   offset_pos = tail + offset;
531   offset_end_pos = tail + offset + length;
532
533   f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
534
535   if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX)
536     {
537       s = ooo_segment_new (f, offset_pos, length);
538       f->ooos_list_head = s - f->ooo_segments;
539       f->ooos_newest = f->ooos_list_head;
540       return;
541     }
542
543   /* Find first segment that starts after new segment */
544   s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
545   while (s->next != OOO_SEGMENT_INVALID_INDEX
546          && position_lt (f, s->start, offset_pos, tail))
547     s = pool_elt_at_index (f->ooo_segments, s->next);
548
549   /* If we have a previous and we overlap it, use it as starting point */
550   prev = ooo_segment_get_prev (f, s);
551   if (prev
552       && position_leq (f, offset_pos, ooo_segment_end_pos (f, prev), tail))
553     {
554       s = prev;
555       s_end_pos = ooo_segment_end_pos (f, s);
556
557       /* Since we have previous, offset start position cannot be smaller
558        * than prev->start. Check tail */
559       ASSERT (position_lt (f, s->start, offset_pos, tail));
560       goto check_tail;
561     }
562
563   s_index = s - f->ooo_segments;
564   s_end_pos = ooo_segment_end_pos (f, s);
565
566   /* No overlap, add before current segment */
567   if (position_lt (f, offset_end_pos, s->start, tail))
568     {
569       new_s = ooo_segment_new (f, offset_pos, length);
570       new_index = new_s - f->ooo_segments;
571
572       /* Pool might've moved, get segment again */
573       s = pool_elt_at_index (f->ooo_segments, s_index);
574       if (s->prev != OOO_SEGMENT_INVALID_INDEX)
575         {
576           new_s->prev = s->prev;
577           prev = pool_elt_at_index (f->ooo_segments, new_s->prev);
578           prev->next = new_index;
579         }
580       else
581         {
582           /* New head */
583           f->ooos_list_head = new_index;
584         }
585
586       new_s->next = s_index;
587       s->prev = new_index;
588       f->ooos_newest = new_index;
589       return;
590     }
591   /* No overlap, add after current segment */
592   else if (position_gt (f, offset_pos, s_end_pos, tail))
593     {
594       new_s = ooo_segment_new (f, offset_pos, length);
595       new_index = new_s - f->ooo_segments;
596
597       /* Pool might've moved, get segment again */
598       s = pool_elt_at_index (f->ooo_segments, s_index);
599
600       /* Needs to be last */
601       ASSERT (s->next == OOO_SEGMENT_INVALID_INDEX);
602
603       new_s->prev = s_index;
604       s->next = new_index;
605       f->ooos_newest = new_index;
606
607       return;
608     }
609
610   /*
611    * Merge needed
612    */
613
614   /* Merge at head */
615   if (position_lt (f, offset_pos, s->start, tail))
616     {
617       s->start = offset_pos;
618       s->length = position_diff (f, s_end_pos, s->start, tail);
619       f->ooos_newest = s - f->ooo_segments;
620     }
621
622 check_tail:
623
624   /* Overlapping tail */
625   if (position_gt (f, offset_end_pos, s_end_pos, tail))
626     {
627       s->length = position_diff (f, offset_end_pos, s->start, tail);
628
629       /* Remove the completely overlapped segments in the tail */
630       it = ooo_segment_next (f, s);
631       while (it && position_leq (f, ooo_segment_end_pos (f, it),
632                                  offset_end_pos, tail))
633         {
634           next = ooo_segment_next (f, it);
635           ooo_segment_del (f, it - f->ooo_segments);
636           it = next;
637         }
638
639       /* If partial overlap with last, merge */
640       if (it && position_leq (f, it->start, offset_end_pos, tail))
641         {
642           s->length = position_diff (f, ooo_segment_end_pos (f, it),
643                                      s->start, tail);
644           ooo_segment_del (f, it - f->ooo_segments);
645         }
646       f->ooos_newest = s - f->ooo_segments;
647     }
648 }
649
650 /**
651  * Removes segments that can now be enqueued because the fifo's tail has
652  * advanced. Returns the number of bytes added to tail.
653  */
654 static int
655 ooo_segment_try_collect (svm_fifo_t * f, u32 n_bytes_enqueued, u32 * tail)
656 {
657   ooo_segment_t *s;
658   u32 index, bytes = 0;
659   i32 diff;
660
661   s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
662   diff = ooo_segment_distance_to_tail (f, s->start, *tail);
663
664   ASSERT (diff != n_bytes_enqueued);
665
666   if (diff > n_bytes_enqueued)
667     return 0;
668
669   /* If last tail update overlaps one/multiple ooo segments, remove them */
670   while (0 <= diff && diff < n_bytes_enqueued)
671     {
672       index = s - f->ooo_segments;
673
674       /* Segment end is beyond the tail. Advance tail and remove segment */
675       if (s->length > diff)
676         {
677           bytes = s->length - diff;
678           *tail = *tail + bytes;
679           ooo_segment_del (f, index);
680           break;
681         }
682
683       /* If we have next go on */
684       if (s->next != OOO_SEGMENT_INVALID_INDEX)
685         {
686           s = pool_elt_at_index (f->ooo_segments, s->next);
687           diff = ooo_segment_distance_to_tail (f, s->start, *tail);
688           ooo_segment_del (f, index);
689         }
690       /* End of search */
691       else
692         {
693           ooo_segment_del (f, index);
694           break;
695         }
696     }
697
698   ASSERT (bytes <= f->nitems);
699   return bytes;
700 }
701
702 void
703 svm_fifo_overwrite_head (svm_fifo_t * f, u8 * data, u32 len)
704 {
705   u32 n_chunk;
706   u32 head, tail, head_idx;
707   svm_fifo_chunk_t *c;
708
709   ASSERT (len <= f->nitems);
710
711   f_load_head_tail_cons (f, &head, &tail);
712   c = f->head_chunk;
713   head_idx = head % f->size;
714   head_idx -= c->start_byte;
715   n_chunk = c->length - head_idx;
716   if (len <= n_chunk)
717     clib_memcpy_fast (&c->data[head_idx], data, len);
718   else
719     {
720       clib_memcpy_fast (&c->data[head_idx], data, n_chunk);
721       clib_memcpy_fast (&c->next->data[0], data + n_chunk, len - n_chunk);
722     }
723 }
724
725 int
726 svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 len, const u8 * src)
727 {
728   u32 tail, head, free_count;
729
730   f_load_head_tail_prod (f, &head, &tail);
731
732   /* free space in fifo can only increase during enqueue: SPSC */
733   free_count = f_free_count (f, head, tail);
734
735   f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
736
737   if (PREDICT_FALSE (free_count == 0))
738     return SVM_FIFO_FULL;
739
740   /* number of bytes we're going to copy */
741   len = clib_min (free_count, len);
742
743   svm_fifo_copy_to_chunk (f, f->tail_chunk, tail % f->size, src, len,
744                           &f->tail_chunk);
745   tail += len;
746
747   svm_fifo_trace_add (f, head, n_total, 2);
748
749   /* collect out-of-order segments */
750   if (PREDICT_FALSE (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX))
751     len += ooo_segment_try_collect (f, len, &tail);
752
753   /* store-rel: producer owned index (paired with load-acq in consumer) */
754   clib_atomic_store_rel_n (&f->tail, tail);
755
756   return len;
757 }
758
759 /**
760  * Enqueue a future segment.
761  *
762  * Two choices: either copies the entire segment, or copies nothing
763  * Returns 0 of the entire segment was copied
764  * Returns -1 if none of the segment was copied due to lack of space
765  */
766 int
767 svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 len, u8 * src)
768 {
769   u32 tail, head, free_count, tail_idx;
770
771   f_load_head_tail_prod (f, &head, &tail);
772
773   /* free space in fifo can only increase during enqueue: SPSC */
774   free_count = f_free_count (f, head, tail);
775
776   /* will this request fit? */
777   if ((len + offset) > free_count)
778     return -1;
779
780   f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
781
782   svm_fifo_trace_add (f, offset, len, 1);
783
784   ooo_segment_add (f, offset, head, tail, len);
785
786   tail_idx = (tail + offset) % f->size;
787
788   if (!svm_fifo_chunk_includes_pos (f->ooo_enq, tail_idx))
789     f->ooo_enq = svm_fifo_find_chunk (f, tail_idx);
790
791   svm_fifo_copy_to_chunk (f, f->ooo_enq, tail_idx, src, len, &f->ooo_enq);
792
793   return 0;
794 }
795
796 int
797 svm_fifo_dequeue_nowait (svm_fifo_t * f, u32 len, u8 * dst)
798 {
799   u32 tail, head, cursize;
800
801   f_load_head_tail_cons (f, &head, &tail);
802
803   /* current size of fifo can only increase during dequeue: SPSC */
804   cursize = f_cursize (f, head, tail);
805
806   if (PREDICT_FALSE (cursize == 0))
807     return -2;                  /* nothing in the fifo */
808
809   len = clib_min (cursize, len);
810
811   svm_fifo_copy_from_chunk (f, f->head_chunk, head % f->size, dst, len,
812                             &f->head_chunk);
813   head += len;
814
815   if (PREDICT_FALSE (f->flags & SVM_FIFO_F_SIZE_UPDATE))
816     svm_fifo_try_size_update (f, head);
817
818   /* store-rel: consumer owned index (paired with load-acq in producer) */
819   clib_atomic_store_rel_n (&f->head, head);
820
821   return len;
822 }
823
824 int
825 svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 len, u8 * dst)
826 {
827   u32 tail, head, cursize, head_idx;
828
829   f_load_head_tail_cons (f, &head, &tail);
830
831   /* current size of fifo can only increase during peek: SPSC */
832   cursize = f_cursize (f, head, tail);
833
834   if (PREDICT_FALSE (cursize < offset))
835     return -2;                  /* nothing in the fifo */
836
837   len = clib_min (cursize - offset, len);
838   head_idx = (head + offset) % f->size;
839   if (!svm_fifo_chunk_includes_pos (f->ooo_deq, head_idx))
840     f->ooo_deq = svm_fifo_find_chunk (f, head_idx);
841
842   svm_fifo_copy_from_chunk (f, f->ooo_deq, head_idx, dst, len, &f->ooo_deq);
843   return len;
844 }
845
846 int
847 svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes)
848 {
849   u32 total_drop_bytes;
850   u32 tail, head, cursize;
851
852   f_load_head_tail_cons (f, &head, &tail);
853
854   /* number of bytes we're going to drop */
855   cursize = f_cursize (f, head, tail);
856
857   if (PREDICT_FALSE (cursize == 0))
858     return -2;                  /* nothing in the fifo */
859
860   svm_fifo_trace_add (f, tail, total_drop_bytes, 3);
861
862   /* number of bytes we're going to drop */
863   total_drop_bytes = (cursize < max_bytes) ? cursize : max_bytes;
864
865   /* move head */
866   head += total_drop_bytes;
867
868   ASSERT (cursize >= total_drop_bytes);
869   /* store-rel: consumer owned index (paired with load-acq in producer) */
870   clib_atomic_store_rel_n (&f->head, head);
871
872   return total_drop_bytes;
873 }
874
875 void
876 svm_fifo_dequeue_drop_all (svm_fifo_t * f)
877 {
878   /* consumer foreign index */
879   u32 tail = clib_atomic_load_acq_n (&f->tail);
880   /* store-rel: consumer owned index (paired with load-acq in producer) */
881   clib_atomic_store_rel_n (&f->head, tail);
882 }
883
884 int
885 svm_fifo_segments (svm_fifo_t * f, svm_fifo_seg_t * fs)
886 {
887   u32 cursize, head, tail, head_idx;
888
889   f_load_head_tail_cons (f, &head, &tail);
890
891   /* consumer function, cursize can only increase while we're working */
892   cursize = f_cursize (f, head, tail);
893
894   if (PREDICT_FALSE (cursize == 0))
895     return -2;                  /* nothing in the fifo */
896
897   head_idx = head % f->size;
898
899   if (tail < head)
900     {
901       fs[0].len = f->size - head_idx;
902       fs[0].data = f->head_chunk->data + head_idx;
903       fs[1].len = cursize - fs[0].len;
904       fs[1].data = f->head_chunk->data;
905     }
906   else
907     {
908       fs[0].len = cursize;
909       fs[0].data = f->head_chunk->data + head_idx;
910       fs[1].len = 0;
911       fs[1].data = 0;
912     }
913   return cursize;
914 }
915
916 void
917 svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_seg_t * fs)
918 {
919   u32 head, head_idx;
920
921   /* consumer owned index */
922   head = f->head;
923   head_idx = head % f->size;
924
925   ASSERT (fs[0].data == f->head_chunk->data + head_idx);
926   head += fs[0].len + fs[1].len;
927   /* store-rel: consumer owned index (paired with load-acq in producer) */
928   clib_atomic_store_rel_n (&f->head, head);
929 }
930
931 /* Assumption: no prod and cons are accessing either dest or src fifo */
932 void
933 svm_fifo_clone (svm_fifo_t * df, svm_fifo_t * sf)
934 {
935   u32 head, tail;
936   clib_memcpy_fast (df->head_chunk->data, sf->head_chunk->data, sf->size);
937
938   f_load_head_tail_all_acq (sf, &head, &tail);
939   clib_atomic_store_rel_n (&df->head, head);
940   clib_atomic_store_rel_n (&df->tail, tail);
941 }
942
943 u32
944 svm_fifo_number_ooo_segments (svm_fifo_t * f)
945 {
946   return pool_elts (f->ooo_segments);
947 }
948
949 ooo_segment_t *
950 svm_fifo_first_ooo_segment (svm_fifo_t * f)
951 {
952   return pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
953 }
954
955 /**
956  * Set fifo pointers to requested offset
957  */
958 void
959 svm_fifo_init_pointers (svm_fifo_t * f, u32 head, u32 tail)
960 {
961   clib_atomic_store_rel_n (&f->head, head);
962   clib_atomic_store_rel_n (&f->tail, tail);
963   if (f->flags & SVM_FIFO_F_MULTI_CHUNK)
964     {
965       svm_fifo_chunk_t *c;
966       c = svm_fifo_find_chunk (f, head % f->size);
967       ASSERT (c != 0);
968       f->head_chunk = f->ooo_deq = c;
969       c = svm_fifo_find_chunk (f, tail % f->size);
970       ASSERT (c != 0);
971       f->tail_chunk = f->ooo_enq = c;
972     }
973 }
974
975 void
976 svm_fifo_add_subscriber (svm_fifo_t * f, u8 subscriber)
977 {
978   if (f->n_subscribers >= SVM_FIFO_MAX_EVT_SUBSCRIBERS)
979     return;
980   f->subscribers[f->n_subscribers++] = subscriber;
981 }
982
983 void
984 svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber)
985 {
986   int i;
987
988   for (i = 0; i < f->n_subscribers; i++)
989     {
990       if (f->subscribers[i] != subscriber)
991         continue;
992       f->subscribers[i] = f->subscribers[f->n_subscribers - 1];
993       f->n_subscribers--;
994       break;
995     }
996 }
997
998 #endif
999 /*
1000  * fd.io coding-style-patch-verification: ON
1001  *
1002  * Local Variables:
1003  * eval: (c-set-style "gnu")
1004  * End:
1005  */