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