Fixes and improved tcp/session debugging
[vpp.git] / src / svm / svm_fifo.c
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <svm/svm_fifo.h>
17 #include <vppinfra/cpu.h>
18
19 static inline u8
20 position_lt (svm_fifo_t * f, u32 a, u32 b)
21 {
22   return (ooo_segment_distance_from_tail (f, a)
23           < ooo_segment_distance_from_tail (f, b));
24 }
25
26 static inline u8
27 position_leq (svm_fifo_t * f, u32 a, u32 b)
28 {
29   return (ooo_segment_distance_from_tail (f, a)
30           <= ooo_segment_distance_from_tail (f, b));
31 }
32
33 static inline u8
34 position_gt (svm_fifo_t * f, u32 a, u32 b)
35 {
36   return (ooo_segment_distance_from_tail (f, a)
37           > ooo_segment_distance_from_tail (f, b));
38 }
39
40 static inline u32
41 position_diff (svm_fifo_t * f, u32 posa, u32 posb)
42 {
43   return ooo_segment_distance_from_tail (f, posa)
44     - ooo_segment_distance_from_tail (f, posb);
45 }
46
47 static inline u32
48 ooo_segment_end_pos (svm_fifo_t * f, ooo_segment_t * s)
49 {
50   return (s->start + s->length) % f->nitems;
51 }
52
53 u8 *
54 format_ooo_segment (u8 * s, va_list * args)
55 {
56   ooo_segment_t *seg = va_arg (*args, ooo_segment_t *);
57
58   s = format (s, "pos %u, len %u, next %d, prev %d",
59               seg->start, seg->length, seg->next, seg->prev);
60   return s;
61 }
62
63 u8 *
64 svm_fifo_dump_trace (u8 * s, svm_fifo_t * f)
65 {
66 #if SVM_FIFO_TRACE
67   svm_fifo_trace_elem_t *seg = 0;
68   int i = 0;
69
70   if (f->trace)
71     {
72       vec_foreach (seg, f->trace)
73       {
74         s = format (s, "{%u, %u, %u}, ", seg->offset, seg->len, seg->action);
75         i++;
76         if (i % 5 == 0)
77           s = format (s, "\n");
78       }
79       s = format (s, "\n");
80     }
81   return s;
82 #else
83   return 0;
84 #endif
85 }
86
87 u8 *
88 svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose)
89 {
90   int i, trace_len;
91   u8 *data = 0;
92   svm_fifo_trace_elem_t *trace;
93   u32 offset;
94   svm_fifo_t *dummy_fifo;
95
96   if (!f)
97     return s;
98
99 #if SVM_FIFO_TRACE
100   trace = f->trace;
101   trace_len = vec_len (trace);
102 #else
103   trace = 0;
104   trace_len = 0;
105 #endif
106
107   dummy_fifo = svm_fifo_create (f->nitems);
108   memset (f->data, 0xFF, f->nitems);
109
110   vec_validate (data, f->nitems);
111   for (i = 0; i < vec_len (data); i++)
112     data[i] = i;
113
114   for (i = 0; i < trace_len; i++)
115     {
116       offset = trace[i].offset;
117       if (trace[i].action == 1)
118         {
119           if (verbose)
120             s = format (s, "adding [%u, %u]:", trace[i].offset,
121                         (trace[i].offset +
122                          trace[i].len) % dummy_fifo->nitems);
123           svm_fifo_enqueue_with_offset (dummy_fifo, trace[i].offset,
124                                         trace[i].len, &data[offset]);
125         }
126       else if (trace[i].action == 2)
127         {
128           if (verbose)
129             s = format (s, "adding [%u, %u]:", 0, trace[i].len);
130           svm_fifo_enqueue_nowait (dummy_fifo, trace[i].len, &data[offset]);
131         }
132       else if (!no_read)
133         {
134           if (verbose)
135             s = format (s, "read: %u", trace[i].len);
136           svm_fifo_dequeue_drop (dummy_fifo, trace[i].len);
137         }
138       if (verbose)
139         s = format (s, "%U", format_svm_fifo, dummy_fifo, 1);
140     }
141
142   s = format (s, "result: %U", format_svm_fifo, dummy_fifo, 1);
143
144   return s;
145 }
146
147 u8 *
148 format_ooo_list (u8 * s, va_list * args)
149 {
150   svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
151   u32 ooo_segment_index = f->ooos_list_head;
152   ooo_segment_t *seg;
153
154   while (ooo_segment_index != OOO_SEGMENT_INVALID_INDEX)
155     {
156       seg = pool_elt_at_index (f->ooo_segments, ooo_segment_index);
157       s = format (s, "  %U\n", format_ooo_segment, seg);
158       ooo_segment_index = seg->next;
159     }
160
161   return s;
162 }
163
164 u8 *
165 format_svm_fifo (u8 * s, va_list * args)
166 {
167   svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
168   int verbose = va_arg (*args, int);
169
170   s = format (s, "cursize %u nitems %u has_event %d\n",
171               f->cursize, f->nitems, f->has_event);
172   s = format (s, " head %d tail %d\n", f->head, f->tail);
173
174   if (verbose > 1)
175     s = format
176       (s, " server session %d thread %d client session %d thread %d\n",
177        f->master_session_index, f->master_thread_index,
178        f->client_session_index, f->client_thread_index);
179
180   if (verbose)
181     {
182       s = format (s, " ooo pool %d active elts newest %u\n",
183                   pool_elts (f->ooo_segments), f->ooos_newest);
184       if (svm_fifo_has_ooo_data (f))
185         s = format (s, " %U", format_ooo_list, f, verbose);
186     }
187   return s;
188 }
189
190 /** create an svm fifo, in the current heap. Fails vs blow up the process */
191 svm_fifo_t *
192 svm_fifo_create (u32 data_size_in_bytes)
193 {
194   svm_fifo_t *f;
195
196   f = clib_mem_alloc_aligned_or_null (sizeof (*f) + data_size_in_bytes,
197                                       CLIB_CACHE_LINE_BYTES);
198   if (f == 0)
199     return 0;
200
201   memset (f, 0, sizeof (*f));
202   f->nitems = data_size_in_bytes;
203   f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
204   return (f);
205 }
206
207 void
208 svm_fifo_free (svm_fifo_t * f)
209 {
210   pool_free (f->ooo_segments);
211   clib_mem_free (f);
212 }
213
214 always_inline ooo_segment_t *
215 ooo_segment_new (svm_fifo_t * f, u32 start, u32 length)
216 {
217   ooo_segment_t *s;
218
219   pool_get (f->ooo_segments, s);
220
221   s->start = start;
222   s->length = length;
223
224   s->prev = s->next = OOO_SEGMENT_INVALID_INDEX;
225
226   return s;
227 }
228
229 always_inline void
230 ooo_segment_del (svm_fifo_t * f, u32 index)
231 {
232   ooo_segment_t *cur, *prev = 0, *next = 0;
233   cur = pool_elt_at_index (f->ooo_segments, index);
234
235   if (cur->next != OOO_SEGMENT_INVALID_INDEX)
236     {
237       next = pool_elt_at_index (f->ooo_segments, cur->next);
238       next->prev = cur->prev;
239     }
240
241   if (cur->prev != OOO_SEGMENT_INVALID_INDEX)
242     {
243       prev = pool_elt_at_index (f->ooo_segments, cur->prev);
244       prev->next = cur->next;
245     }
246   else
247     {
248       f->ooos_list_head = cur->next;
249     }
250
251   pool_put (f->ooo_segments, cur);
252 }
253
254 /**
255  * Add segment to fifo's out-of-order segment list. Takes care of merging
256  * adjacent segments and removing overlapping ones.
257  */
258 static void
259 ooo_segment_add (svm_fifo_t * f, u32 offset, u32 length)
260 {
261   ooo_segment_t *s, *new_s, *prev, *next, *it;
262   u32 new_index, s_end_pos, s_index;
263   u32 normalized_position, normalized_end_position;
264
265   ASSERT (offset + length <= ooo_segment_distance_from_tail (f, f->head));
266   normalized_position = (f->tail + offset) % f->nitems;
267   normalized_end_position = (f->tail + offset + length) % f->nitems;
268
269   f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
270
271   if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX)
272     {
273       s = ooo_segment_new (f, normalized_position, length);
274       f->ooos_list_head = s - f->ooo_segments;
275       f->ooos_newest = f->ooos_list_head;
276       return;
277     }
278
279   /* Find first segment that starts after new segment */
280   s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
281   while (s->next != OOO_SEGMENT_INVALID_INDEX
282          && position_lt (f, s->start, normalized_position))
283     s = pool_elt_at_index (f->ooo_segments, s->next);
284
285   /* If we have a previous and we overlap it, use it as starting point */
286   prev = ooo_segment_get_prev (f, s);
287   if (prev
288       && position_leq (f, normalized_position, ooo_segment_end_pos (f, prev)))
289     {
290       s = prev;
291       s_end_pos = ooo_segment_end_pos (f, s);
292
293       /* Since we have previous, normalized start position cannot be smaller
294        * than prev->start. Check tail */
295       ASSERT (position_lt (f, s->start, normalized_position));
296       goto check_tail;
297     }
298
299   s_index = s - f->ooo_segments;
300   s_end_pos = ooo_segment_end_pos (f, s);
301
302   /* No overlap, add before current segment */
303   if (position_lt (f, normalized_end_position, s->start))
304     {
305       new_s = ooo_segment_new (f, normalized_position, length);
306       new_index = new_s - f->ooo_segments;
307
308       /* Pool might've moved, get segment again */
309       s = pool_elt_at_index (f->ooo_segments, s_index);
310       if (s->prev != OOO_SEGMENT_INVALID_INDEX)
311         {
312           new_s->prev = s->prev;
313           prev = pool_elt_at_index (f->ooo_segments, new_s->prev);
314           prev->next = new_index;
315         }
316       else
317         {
318           /* New head */
319           f->ooos_list_head = new_index;
320         }
321
322       new_s->next = s_index;
323       s->prev = new_index;
324       f->ooos_newest = new_index;
325       return;
326     }
327   /* No overlap, add after current segment */
328   else if (position_gt (f, normalized_position, s_end_pos))
329     {
330       new_s = ooo_segment_new (f, normalized_position, length);
331       new_index = new_s - f->ooo_segments;
332
333       /* Pool might've moved, get segment again */
334       s = pool_elt_at_index (f->ooo_segments, s_index);
335
336       /* Needs to be last */
337       ASSERT (s->next == OOO_SEGMENT_INVALID_INDEX);
338
339       new_s->prev = s_index;
340       s->next = new_index;
341       f->ooos_newest = new_index;
342
343       return;
344     }
345
346   /*
347    * Merge needed
348    */
349
350   /* Merge at head */
351   if (position_lt (f, normalized_position, s->start))
352     {
353       s->start = normalized_position;
354       s->length = position_diff (f, s_end_pos, s->start);
355       f->ooos_newest = s - f->ooo_segments;
356     }
357
358 check_tail:
359
360   /* Overlapping tail */
361   if (position_gt (f, normalized_end_position, s_end_pos))
362     {
363       s->length = position_diff (f, normalized_end_position, s->start);
364
365       /* Remove the completely overlapped segments in the tail */
366       it = ooo_segment_next (f, s);
367       while (it && position_leq (f, ooo_segment_end_pos (f, it),
368                                  normalized_end_position))
369         {
370           next = ooo_segment_next (f, it);
371           ooo_segment_del (f, it - f->ooo_segments);
372           it = next;
373         }
374
375       /* If partial overlap with last, merge */
376       if (it && position_leq (f, it->start, normalized_end_position))
377         {
378           s->length = position_diff (f, ooo_segment_end_pos (f, it),
379                                      s->start);
380           ooo_segment_del (f, it - f->ooo_segments);
381         }
382       f->ooos_newest = s - f->ooo_segments;
383     }
384 }
385
386 /**
387  * Removes segments that can now be enqueued because the fifo's tail has
388  * advanced. Returns the number of bytes added to tail.
389  */
390 static int
391 ooo_segment_try_collect (svm_fifo_t * f, u32 n_bytes_enqueued)
392 {
393   ooo_segment_t *s;
394   u32 index, bytes = 0;
395   i32 diff;
396
397   s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
398   diff = ooo_segment_distance_to_tail (f, s->start);
399
400   ASSERT (diff != n_bytes_enqueued);
401
402   if (diff > n_bytes_enqueued)
403     return 0;
404
405   /* If last tail update overlaps one/multiple ooo segments, remove them */
406   while (0 <= diff && diff < n_bytes_enqueued)
407     {
408       index = s - f->ooo_segments;
409
410       /* Segment end is beyond the tail. Advance tail and remove segment */
411       if (s->length > diff)
412         {
413           bytes = s->length - diff;
414           f->tail += bytes;
415           f->tail %= f->nitems;
416           ooo_segment_del (f, index);
417           break;
418         }
419
420       /* If we have next go on */
421       if (s->next != OOO_SEGMENT_INVALID_INDEX)
422         {
423           s = pool_elt_at_index (f->ooo_segments, s->next);
424           diff = ooo_segment_distance_to_tail (f, s->start);
425           ooo_segment_del (f, index);
426         }
427       /* End of search */
428       else
429         {
430           ooo_segment_del (f, index);
431           break;
432         }
433     }
434
435   ASSERT (bytes >= 0 && bytes <= f->nitems);
436   return bytes;
437 }
438
439 static int
440 svm_fifo_enqueue_internal (svm_fifo_t * f, u32 max_bytes, u8 * copy_from_here)
441 {
442   u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
443   u32 cursize, nitems;
444
445   /* read cursize, which can only increase while we're working */
446   cursize = svm_fifo_max_dequeue (f);
447   f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
448
449   if (PREDICT_FALSE (cursize == f->nitems))
450     return -2;                  /* fifo stuffed */
451
452   nitems = f->nitems;
453
454   /* Number of bytes we're going to copy */
455   total_copy_bytes = (nitems - cursize) < max_bytes ?
456     (nitems - cursize) : max_bytes;
457
458   if (PREDICT_TRUE (copy_from_here != 0))
459     {
460       /* Number of bytes in first copy segment */
461       first_copy_bytes = ((nitems - f->tail) < total_copy_bytes)
462         ? (nitems - f->tail) : total_copy_bytes;
463
464       clib_memcpy (&f->data[f->tail], copy_from_here, first_copy_bytes);
465       f->tail += first_copy_bytes;
466       f->tail = (f->tail == nitems) ? 0 : f->tail;
467
468       /* Number of bytes in second copy segment, if any */
469       second_copy_bytes = total_copy_bytes - first_copy_bytes;
470       if (second_copy_bytes)
471         {
472           clib_memcpy (&f->data[f->tail], copy_from_here + first_copy_bytes,
473                        second_copy_bytes);
474           f->tail += second_copy_bytes;
475           f->tail = (f->tail == nitems) ? 0 : f->tail;
476         }
477     }
478   else
479     {
480       ASSERT (0);
481
482       /* Account for a zero-copy enqueue done elsewhere */
483       ASSERT (max_bytes <= (nitems - cursize));
484       f->tail += max_bytes;
485       f->tail = f->tail % nitems;
486       total_copy_bytes = max_bytes;
487     }
488
489   svm_fifo_trace_add (f, f->head, total_copy_bytes, 2);
490
491   /* Any out-of-order segments to collect? */
492   if (PREDICT_FALSE (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX))
493     total_copy_bytes += ooo_segment_try_collect (f, total_copy_bytes);
494
495   /* Atomically increase the queue length */
496   ASSERT (cursize + total_copy_bytes <= nitems);
497   __sync_fetch_and_add (&f->cursize, total_copy_bytes);
498
499   return (total_copy_bytes);
500 }
501
502 #define SVM_ENQUEUE_CLONE_TEMPLATE(arch, fn, tgt)                       \
503   uword                                                                 \
504   __attribute__ ((flatten))                                             \
505   __attribute__ ((target (tgt)))                                        \
506   CLIB_CPU_OPTIMIZED                                                    \
507   fn ## _ ## arch ( svm_fifo_t * f, u32 max_bytes, u8 * copy_from_here) \
508   { return fn (f, max_bytes, copy_from_here);}
509
510 static int
511 svm_fifo_enqueue_nowait_ma (svm_fifo_t * f, u32 max_bytes,
512                             u8 * copy_from_here)
513 {
514   return svm_fifo_enqueue_internal (f, max_bytes, copy_from_here);
515 }
516
517 foreach_march_variant (SVM_ENQUEUE_CLONE_TEMPLATE,
518                        svm_fifo_enqueue_nowait_ma);
519 CLIB_MULTIARCH_SELECT_FN (svm_fifo_enqueue_nowait_ma);
520
521 int
522 svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_from_here)
523 {
524 #if CLIB_DEBUG > 0
525   return svm_fifo_enqueue_nowait_ma (f, max_bytes, copy_from_here);
526 #else
527   static int (*fp) (svm_fifo_t *, u32, u8 *);
528
529   if (PREDICT_FALSE (fp == 0))
530     fp = (void *) svm_fifo_enqueue_nowait_ma_multiarch_select ();
531
532   return (*fp) (f, max_bytes, copy_from_here);
533 #endif
534 }
535
536 /**
537  * Enqueue a future segment.
538  *
539  * Two choices: either copies the entire segment, or copies nothing
540  * Returns 0 of the entire segment was copied
541  * Returns -1 if none of the segment was copied due to lack of space
542  */
543 static int
544 svm_fifo_enqueue_with_offset_internal (svm_fifo_t * f,
545                                        u32 offset,
546                                        u32 required_bytes,
547                                        u8 * copy_from_here)
548 {
549   u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
550   u32 cursize, nitems, normalized_offset;
551   u32 offset_from_tail;
552
553   f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
554
555   /* read cursize, which can only increase while we're working */
556   cursize = svm_fifo_max_dequeue (f);
557   nitems = f->nitems;
558
559   ASSERT (required_bytes < nitems);
560
561   normalized_offset = (f->tail + offset) % nitems;
562
563   /* Will this request fit? */
564   offset_from_tail = (nitems + normalized_offset - f->tail) % nitems;
565   if ((required_bytes + offset_from_tail) > (nitems - cursize))
566     return -1;
567
568   svm_fifo_trace_add (f, offset, required_bytes, 1);
569
570   ooo_segment_add (f, offset, required_bytes);
571
572   /* Number of bytes we're going to copy */
573   total_copy_bytes = required_bytes;
574
575   /* Number of bytes in first copy segment */
576   first_copy_bytes = ((nitems - normalized_offset) < total_copy_bytes)
577     ? (nitems - normalized_offset) : total_copy_bytes;
578
579   clib_memcpy (&f->data[normalized_offset], copy_from_here, first_copy_bytes);
580
581   /* Number of bytes in second copy segment, if any */
582   second_copy_bytes = total_copy_bytes - first_copy_bytes;
583   if (second_copy_bytes)
584     {
585       normalized_offset += first_copy_bytes;
586       normalized_offset %= nitems;
587
588       ASSERT (normalized_offset == 0);
589
590       clib_memcpy (&f->data[normalized_offset],
591                    copy_from_here + first_copy_bytes, second_copy_bytes);
592     }
593
594   return (0);
595 }
596
597
598 int
599 svm_fifo_enqueue_with_offset (svm_fifo_t * f,
600                               u32 offset,
601                               u32 required_bytes, u8 * copy_from_here)
602 {
603   return svm_fifo_enqueue_with_offset_internal (f, offset, required_bytes,
604                                                 copy_from_here);
605 }
606
607
608 static int
609 svm_fifo_dequeue_internal (svm_fifo_t * f, u32 max_bytes, u8 * copy_here)
610 {
611   u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
612   u32 cursize, nitems;
613
614   /* read cursize, which can only increase while we're working */
615   cursize = svm_fifo_max_dequeue (f);
616   if (PREDICT_FALSE (cursize == 0))
617     return -2;                  /* nothing in the fifo */
618
619   nitems = f->nitems;
620
621   /* Number of bytes we're going to copy */
622   total_copy_bytes = (cursize < max_bytes) ? cursize : max_bytes;
623
624   if (PREDICT_TRUE (copy_here != 0))
625     {
626       /* Number of bytes in first copy segment */
627       first_copy_bytes = ((nitems - f->head) < total_copy_bytes)
628         ? (nitems - f->head) : total_copy_bytes;
629       clib_memcpy (copy_here, &f->data[f->head], first_copy_bytes);
630       f->head += first_copy_bytes;
631       f->head = (f->head == nitems) ? 0 : f->head;
632
633       /* Number of bytes in second copy segment, if any */
634       second_copy_bytes = total_copy_bytes - first_copy_bytes;
635       if (second_copy_bytes)
636         {
637           clib_memcpy (copy_here + first_copy_bytes,
638                        &f->data[f->head], second_copy_bytes);
639           f->head += second_copy_bytes;
640           f->head = (f->head == nitems) ? 0 : f->head;
641         }
642     }
643   else
644     {
645       ASSERT (0);
646       /* Account for a zero-copy dequeue done elsewhere */
647       ASSERT (max_bytes <= cursize);
648       f->head += max_bytes;
649       f->head = f->head % nitems;
650       cursize -= max_bytes;
651       total_copy_bytes = max_bytes;
652     }
653
654   ASSERT (f->head <= nitems);
655   ASSERT (cursize >= total_copy_bytes);
656   __sync_fetch_and_sub (&f->cursize, total_copy_bytes);
657
658   return (total_copy_bytes);
659 }
660
661 static int
662 svm_fifo_dequeue_nowait_ma (svm_fifo_t * f, u32 max_bytes, u8 * copy_here)
663 {
664   return svm_fifo_dequeue_internal (f, max_bytes, copy_here);
665 }
666
667 #define SVM_FIFO_DEQUEUE_CLONE_TEMPLATE(arch, fn, tgt)          \
668   uword                                                         \
669   __attribute__ ((flatten))                                     \
670   __attribute__ ((target (tgt)))                                \
671   CLIB_CPU_OPTIMIZED                                            \
672   fn ## _ ## arch ( svm_fifo_t * f, u32 max_bytes,              \
673                     u8 * copy_here)                             \
674   { return fn (f, max_bytes, copy_here);}
675
676 foreach_march_variant (SVM_FIFO_DEQUEUE_CLONE_TEMPLATE,
677                        svm_fifo_dequeue_nowait_ma);
678 CLIB_MULTIARCH_SELECT_FN (svm_fifo_dequeue_nowait_ma);
679
680 int
681 svm_fifo_dequeue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_here)
682 {
683 #if CLIB_DEBUG > 0
684   return svm_fifo_dequeue_nowait_ma (f, max_bytes, copy_here);
685 #else
686   static int (*fp) (svm_fifo_t *, u32, u8 *);
687
688   if (PREDICT_FALSE (fp == 0))
689     fp = (void *) svm_fifo_dequeue_nowait_ma_multiarch_select ();
690
691   return (*fp) (f, max_bytes, copy_here);
692 #endif
693 }
694
695 static int
696 svm_fifo_peek_ma (svm_fifo_t * f, u32 relative_offset, u32 max_bytes,
697                   u8 * copy_here)
698 {
699   u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
700   u32 cursize, nitems, real_head;
701
702   /* read cursize, which can only increase while we're working */
703   cursize = svm_fifo_max_dequeue (f);
704   if (PREDICT_FALSE (cursize < relative_offset))
705     return -2;                  /* nothing in the fifo */
706
707   nitems = f->nitems;
708   real_head = f->head + relative_offset;
709   real_head = real_head >= nitems ? real_head - nitems : real_head;
710
711   /* Number of bytes we're going to copy */
712   total_copy_bytes = (cursize - relative_offset < max_bytes) ?
713     cursize - relative_offset : max_bytes;
714
715   if (PREDICT_TRUE (copy_here != 0))
716     {
717       /* Number of bytes in first copy segment */
718       first_copy_bytes =
719         ((nitems - real_head) < total_copy_bytes) ?
720         (nitems - real_head) : total_copy_bytes;
721       clib_memcpy (copy_here, &f->data[real_head], first_copy_bytes);
722
723       /* Number of bytes in second copy segment, if any */
724       second_copy_bytes = total_copy_bytes - first_copy_bytes;
725       if (second_copy_bytes)
726         {
727           clib_memcpy (copy_here + first_copy_bytes, &f->data[0],
728                        second_copy_bytes);
729         }
730     }
731   return total_copy_bytes;
732 }
733
734 #define SVM_FIFO_PEEK_CLONE_TEMPLATE(arch, fn, tgt)                     \
735   uword                                                                 \
736   __attribute__ ((flatten))                                             \
737   __attribute__ ((target (tgt)))                                        \
738   CLIB_CPU_OPTIMIZED                                                    \
739   fn ## _ ## arch ( svm_fifo_t * f, u32 relative_offset, u32 max_bytes, \
740                     u8 * copy_here)                                     \
741   { return fn (f, relative_offset, max_bytes, copy_here);}
742
743 foreach_march_variant (SVM_FIFO_PEEK_CLONE_TEMPLATE, svm_fifo_peek_ma);
744 CLIB_MULTIARCH_SELECT_FN (svm_fifo_peek_ma);
745
746 int
747 svm_fifo_peek (svm_fifo_t * f, u32 relative_offset, u32 max_bytes,
748                u8 * copy_here)
749 {
750 #if CLIB_DEBUG > 0
751   return svm_fifo_peek_ma (f, relative_offset, max_bytes, copy_here);
752 #else
753   static int (*fp) (svm_fifo_t *, u32, u32, u8 *);
754
755   if (PREDICT_FALSE (fp == 0))
756     fp = (void *) svm_fifo_peek_ma_multiarch_select ();
757
758   return (*fp) (f, relative_offset, max_bytes, copy_here);
759 #endif
760 }
761
762 int
763 svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes)
764 {
765   u32 total_drop_bytes, first_drop_bytes, second_drop_bytes;
766   u32 cursize, nitems;
767
768   /* read cursize, which can only increase while we're working */
769   cursize = svm_fifo_max_dequeue (f);
770   if (PREDICT_FALSE (cursize == 0))
771     return -2;                  /* nothing in the fifo */
772
773   nitems = f->nitems;
774
775   /* Number of bytes we're going to drop */
776   total_drop_bytes = (cursize < max_bytes) ? cursize : max_bytes;
777
778   svm_fifo_trace_add (f, f->tail, total_drop_bytes, 3);
779
780   /* Number of bytes in first copy segment */
781   first_drop_bytes =
782     ((nitems - f->head) < total_drop_bytes) ?
783     (nitems - f->head) : total_drop_bytes;
784   f->head += first_drop_bytes;
785   f->head = (f->head == nitems) ? 0 : f->head;
786
787   /* Number of bytes in second drop segment, if any */
788   second_drop_bytes = total_drop_bytes - first_drop_bytes;
789   if (second_drop_bytes)
790     {
791       f->head += second_drop_bytes;
792       f->head = (f->head == nitems) ? 0 : f->head;
793     }
794
795   ASSERT (f->head <= nitems);
796   ASSERT (cursize >= total_drop_bytes);
797   __sync_fetch_and_sub (&f->cursize, total_drop_bytes);
798
799   return total_drop_bytes;
800 }
801
802 u32
803 svm_fifo_number_ooo_segments (svm_fifo_t * f)
804 {
805   return pool_elts (f->ooo_segments);
806 }
807
808 ooo_segment_t *
809 svm_fifo_first_ooo_segment (svm_fifo_t * f)
810 {
811   return pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
812 }
813
814 /**
815  * Set fifo pointers to requested offset
816  */
817 void
818 svm_fifo_init_pointers (svm_fifo_t * f, u32 pointer)
819 {
820   f->head = f->tail = pointer % f->nitems;
821 }
822
823 /*
824  * fd.io coding-style-patch-verification: ON
825  *
826  * Local Variables:
827  * eval: (c-set-style "gnu")
828  * End:
829  */