Implement sack based tcp loss recovery (RFC 6675)
[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
18 #define offset_lt(_a, _b) ((i32)((_a)-(_b)) < 0)
19 #define offset_leq(_a, _b) ((i32)((_a)-(_b)) <= 0)
20 #define offset_gt(_a, _b) ((i32)((_a)-(_b)) > 0)
21 #define offset_geq(_a, _b) ((i32)((_a)-(_b)) >= 0)
22
23 u8 *
24 format_ooo_segment (u8 * s, va_list * args)
25 {
26   ooo_segment_t *seg = va_arg (*args, ooo_segment_t *);
27
28   s = format (s, "pos %u, len %u, next %d, prev %d",
29               seg->start, seg->length, seg->next, seg->prev);
30   return s;
31 }
32
33 u8 *
34 format_ooo_list (u8 * s, va_list * args)
35 {
36   svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
37   u32 ooo_segment_index = f->ooos_list_head;
38   ooo_segment_t *seg;
39
40   while (ooo_segment_index != OOO_SEGMENT_INVALID_INDEX)
41     {
42       seg = pool_elt_at_index (f->ooo_segments, ooo_segment_index);
43       s = format (s, "  %U\n", format_ooo_segment, seg);
44       ooo_segment_index = seg->next;
45     }
46   return s;
47 }
48
49 u8 *
50 format_svm_fifo (u8 * s, va_list * args)
51 {
52   svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
53   int verbose = va_arg (*args, int);
54
55   s = format (s, "cursize %u nitems %u has_event %d\n",
56               f->cursize, f->nitems, f->has_event);
57   s = format (s, " head %d tail %d\n", f->head, f->tail);
58
59   if (verbose > 1)
60     s = format
61       (s, " server session %d thread %d client session %d thread %d\n",
62        f->master_session_index, f->master_thread_index,
63        f->client_session_index, f->client_thread_index);
64
65   if (verbose)
66     {
67       s = format (s, " ooo pool %d active elts\n",
68                   pool_elts (f->ooo_segments));
69       if (svm_fifo_has_ooo_data (f))
70         s = format (s, " %U", format_ooo_list, f);
71     }
72   return s;
73 }
74
75 /** create an svm fifo, in the current heap. Fails vs blow up the process */
76 svm_fifo_t *
77 svm_fifo_create (u32 data_size_in_bytes)
78 {
79   svm_fifo_t *f;
80
81   f = clib_mem_alloc_aligned_or_null (sizeof (*f) + data_size_in_bytes,
82                                       CLIB_CACHE_LINE_BYTES);
83   if (f == 0)
84     return 0;
85
86   memset (f, 0, sizeof (*f) + data_size_in_bytes);
87   f->nitems = data_size_in_bytes;
88   f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
89
90   return (f);
91 }
92
93 void
94 svm_fifo_free (svm_fifo_t * f)
95 {
96   pool_free (f->ooo_segments);
97   clib_mem_free (f);
98 }
99
100 always_inline ooo_segment_t *
101 ooo_segment_new (svm_fifo_t * f, u32 start, u32 length)
102 {
103   ooo_segment_t *s;
104
105   pool_get (f->ooo_segments, s);
106
107   s->start = start;
108   s->length = length;
109
110   s->prev = s->next = OOO_SEGMENT_INVALID_INDEX;
111
112   return s;
113 }
114
115 always_inline void
116 ooo_segment_del (svm_fifo_t * f, u32 index)
117 {
118   ooo_segment_t *cur, *prev = 0, *next = 0;
119   cur = pool_elt_at_index (f->ooo_segments, index);
120
121   if (cur->next != OOO_SEGMENT_INVALID_INDEX)
122     {
123       next = pool_elt_at_index (f->ooo_segments, cur->next);
124       next->prev = cur->prev;
125     }
126
127   if (cur->prev != OOO_SEGMENT_INVALID_INDEX)
128     {
129       prev = pool_elt_at_index (f->ooo_segments, cur->prev);
130       prev->next = cur->next;
131     }
132   else
133     {
134       f->ooos_list_head = cur->next;
135     }
136
137   pool_put (f->ooo_segments, cur);
138 }
139
140 /**
141  * Add segment to fifo's out-of-order segment list. Takes care of merging
142  * adjacent segments and removing overlapping ones.
143  */
144 static void
145 ooo_segment_add (svm_fifo_t * f, u32 offset, u32 length)
146 {
147   ooo_segment_t *s, *new_s, *prev, *next, *it;
148   u32 new_index, end_offset, s_sof, s_eof, s_index;
149
150   end_offset = offset + length;
151
152   if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX)
153     {
154       s = ooo_segment_new (f, offset, length);
155       f->ooos_list_head = s - f->ooo_segments;
156       f->ooos_newest = f->ooos_list_head;
157       return;
158     }
159
160   /* Find first segment that starts after new segment */
161   s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
162   while (s->next != OOO_SEGMENT_INVALID_INDEX
163          && offset_leq (ooo_segment_offset (f, s), offset))
164     s = pool_elt_at_index (f->ooo_segments, s->next);
165
166   /* If we have a previous and we overlap it, use it as starting point */
167   prev = ooo_segment_get_prev (f, s);
168   if (prev && offset_leq (offset, ooo_segment_end_offset (f, prev)))
169     {
170       s = prev;
171       prev = ooo_segment_get_prev (f, s);
172       s_sof = ooo_segment_offset (f, s);
173       s_eof = ooo_segment_end_offset (f, s);
174       goto merge;
175     }
176
177   s_index = s - f->ooo_segments;
178   s_sof = ooo_segment_offset (f, s);
179   s_eof = ooo_segment_end_offset (f, s);
180
181   /* No overlap, add before current segment */
182   if (offset_lt (end_offset, s_sof))
183     {
184       new_s = ooo_segment_new (f, offset, length);
185       new_index = new_s - f->ooo_segments;
186
187       /* Pool might've moved, get segment again */
188       s = pool_elt_at_index (f->ooo_segments, s_index);
189       if (s->prev != OOO_SEGMENT_INVALID_INDEX)
190         {
191           new_s->prev = s->prev;
192           prev = pool_elt_at_index (f->ooo_segments, new_s->prev);
193           prev->next = new_index;
194         }
195       else
196         {
197           /* New head */
198           f->ooos_list_head = new_index;
199         }
200
201       new_s->next = s - f->ooo_segments;
202       s->prev = new_index;
203       f->ooos_newest = new_index;
204       return;
205     }
206   /* No overlap, add after current segment */
207   else if (offset_gt (offset, s_eof))
208     {
209       new_s = ooo_segment_new (f, offset, length);
210       new_index = new_s - f->ooo_segments;
211
212       /* Pool might've moved, get segment again */
213       s = pool_elt_at_index (f->ooo_segments, s_index);
214
215       if (s->next != OOO_SEGMENT_INVALID_INDEX)
216         {
217           new_s->next = s->next;
218           next = pool_elt_at_index (f->ooo_segments, new_s->next);
219           next->prev = new_index;
220         }
221
222       new_s->prev = s - f->ooo_segments;
223       s->next = new_index;
224       f->ooos_newest = new_index;
225
226       return;
227     }
228
229   /*
230    * Merge needed
231    */
232
233 merge:
234
235   /* Merge at head */
236   if (offset_lt (offset, s_sof))
237     {
238       s->start = offset;
239       s->length = s_eof - ooo_segment_offset (f, s);
240     }
241   /* Last but overlapping previous */
242   else if (offset_gt (end_offset, s_eof))
243     {
244       s->length = end_offset - ooo_segment_offset (f, s);
245     }
246   /* New segment completely covered by current one */
247   else
248     {
249       /* Do Nothing */
250       goto done;
251     }
252
253   /* The new segment's tail may cover multiple smaller ones */
254   if (offset_geq (end_offset, s_eof))
255     {
256       /* Remove the completely overlapped segments */
257       it = (s->next != OOO_SEGMENT_INVALID_INDEX) ?
258         pool_elt_at_index (f->ooo_segments, s->next) : 0;
259       while (it && offset_leq (ooo_segment_end_offset (f, it), end_offset))
260         {
261           next = (it->next != OOO_SEGMENT_INVALID_INDEX) ?
262             pool_elt_at_index (f->ooo_segments, it->next) : 0;
263           ooo_segment_del (f, it - f->ooo_segments);
264           it = next;
265         }
266
267       /* If partial overlap with last, merge */
268       if (it && offset_leq (ooo_segment_offset (f, it), end_offset))
269         {
270           s->length = ooo_segment_end_offset (f, it) -
271             ooo_segment_offset (f, s);
272           ooo_segment_del (f, it - f->ooo_segments);
273         }
274     }
275
276 done:
277   /* Most recently updated segment */
278   f->ooos_newest = s - f->ooo_segments;
279 }
280
281 /**
282  * Removes segments that can now be enqueued because the fifo's tail has
283  * advanced. Returns the number of bytes added to tail.
284  */
285 static int
286 ooo_segment_try_collect (svm_fifo_t * f, u32 n_bytes_enqueued)
287 {
288   ooo_segment_t *s;
289   u32 index, bytes = 0, diff;
290   u32 cursize, norm_start, nitems;
291
292   /* current size has not yet been updated */
293   cursize = svm_fifo_max_dequeue (f) + n_bytes_enqueued;
294   nitems = f->nitems;
295
296   s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
297
298   norm_start = s->start % nitems;
299   diff = (f->nitems + (i32) (f->tail - norm_start)) % nitems;
300
301   if (diff > cursize)
302     return 0;
303
304   /* If last tail update overlaps one/multiple ooo segments, remove them */
305   while (0 < diff && diff < cursize)
306     {
307       index = s - f->ooo_segments;
308
309       /* Segment end is beyond the tail. Advance tail and remove segment */
310       if (diff < s->length)
311         {
312           f->tail += s->length - diff;
313           f->tail %= f->nitems;
314           bytes = s->length - diff;
315           ooo_segment_del (f, index);
316           break;
317         }
318
319       /* If we have next go on */
320       if (s->next != OOO_SEGMENT_INVALID_INDEX)
321         {
322           s = pool_elt_at_index (f->ooo_segments, s->next);
323           norm_start = s->start % nitems;
324           diff = (f->nitems + (i32) (f->tail - norm_start)) % nitems;
325           ooo_segment_del (f, index);
326         }
327       /* End of search */
328       else
329         {
330           ooo_segment_del (f, index);
331           break;
332         }
333     }
334
335   /* If tail is adjacent to an ooo segment, 'consume' it */
336   if (diff == 0)
337     {
338       bytes = ((nitems - cursize) >= s->length) ? s->length :
339         nitems - cursize;
340
341       f->tail += bytes;
342       f->tail %= nitems;
343
344       ooo_segment_del (f, s - f->ooo_segments);
345     }
346
347   return bytes;
348 }
349
350 static int
351 svm_fifo_enqueue_internal (svm_fifo_t * f, u32 max_bytes, u8 * copy_from_here)
352 {
353   u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
354   u32 cursize, nitems;
355
356   /* read cursize, which can only increase while we're working */
357   cursize = svm_fifo_max_dequeue (f);
358
359   if (PREDICT_FALSE (cursize == f->nitems))
360     return -2;                  /* fifo stuffed */
361
362   nitems = f->nitems;
363
364   /* Number of bytes we're going to copy */
365   total_copy_bytes = (nitems - cursize) < max_bytes ?
366     (nitems - cursize) : max_bytes;
367
368   if (PREDICT_TRUE (copy_from_here != 0))
369     {
370       /* Number of bytes in first copy segment */
371       first_copy_bytes = ((nitems - f->tail) < total_copy_bytes)
372         ? (nitems - f->tail) : total_copy_bytes;
373
374       clib_memcpy (&f->data[f->tail], copy_from_here, first_copy_bytes);
375       f->tail += first_copy_bytes;
376       f->tail = (f->tail == nitems) ? 0 : f->tail;
377
378       /* Number of bytes in second copy segment, if any */
379       second_copy_bytes = total_copy_bytes - first_copy_bytes;
380       if (second_copy_bytes)
381         {
382           clib_memcpy (&f->data[f->tail], copy_from_here + first_copy_bytes,
383                        second_copy_bytes);
384           f->tail += second_copy_bytes;
385           f->tail = (f->tail == nitems) ? 0 : f->tail;
386         }
387     }
388   else
389     {
390       /* Account for a zero-copy enqueue done elsewhere */
391       ASSERT (max_bytes <= (nitems - cursize));
392       f->tail += max_bytes;
393       f->tail = f->tail % nitems;
394       total_copy_bytes = max_bytes;
395     }
396
397   /* Any out-of-order segments to collect? */
398   if (PREDICT_FALSE (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX))
399     total_copy_bytes += ooo_segment_try_collect (f, total_copy_bytes);
400
401   /* Atomically increase the queue length */
402   __sync_fetch_and_add (&f->cursize, total_copy_bytes);
403
404   return (total_copy_bytes);
405 }
406
407 int
408 svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_from_here)
409 {
410   return svm_fifo_enqueue_internal (f, max_bytes, copy_from_here);
411 }
412
413 /**
414  * Enqueue a future segment.
415  *
416  * Two choices: either copies the entire segment, or copies nothing
417  * Returns 0 of the entire segment was copied
418  * Returns -1 if none of the segment was copied due to lack of space
419  */
420 static int
421 svm_fifo_enqueue_with_offset_internal (svm_fifo_t * f,
422                                        u32 offset,
423                                        u32 required_bytes,
424                                        u8 * copy_from_here)
425 {
426   u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
427   u32 cursize, nitems;
428   u32 normalized_offset, offset_from_tail;
429
430   /* read cursize, which can only increase while we're working */
431   cursize = svm_fifo_max_dequeue (f);
432   nitems = f->nitems;
433   normalized_offset = offset % nitems;
434
435   /* Will this request fit? */
436   offset_from_tail = (nitems + normalized_offset - f->tail) % nitems;
437   if ((required_bytes + offset_from_tail) > (nitems - cursize))
438     return -1;
439
440   ooo_segment_add (f, offset, required_bytes);
441
442   /* Number of bytes we're going to copy */
443   total_copy_bytes = required_bytes;
444
445   /* Number of bytes in first copy segment */
446   first_copy_bytes = ((nitems - normalized_offset) < total_copy_bytes)
447     ? (nitems - normalized_offset) : total_copy_bytes;
448
449   clib_memcpy (&f->data[normalized_offset], copy_from_here, first_copy_bytes);
450
451   /* Number of bytes in second copy segment, if any */
452   second_copy_bytes = total_copy_bytes - first_copy_bytes;
453   if (second_copy_bytes)
454     {
455       normalized_offset += first_copy_bytes;
456       normalized_offset %= nitems;
457
458       ASSERT (normalized_offset == 0);
459
460       clib_memcpy (&f->data[normalized_offset],
461                    copy_from_here + first_copy_bytes, second_copy_bytes);
462     }
463
464   return (0);
465 }
466
467
468 int
469 svm_fifo_enqueue_with_offset (svm_fifo_t * f,
470                               u32 offset,
471                               u32 required_bytes, u8 * copy_from_here)
472 {
473   return svm_fifo_enqueue_with_offset_internal (f, offset, required_bytes,
474                                                 copy_from_here);
475 }
476
477
478 static int
479 svm_fifo_dequeue_internal (svm_fifo_t * f, u32 max_bytes, u8 * copy_here)
480 {
481   u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
482   u32 cursize, nitems;
483
484   /* read cursize, which can only increase while we're working */
485   cursize = svm_fifo_max_dequeue (f);
486   if (PREDICT_FALSE (cursize == 0))
487     return -2;                  /* nothing in the fifo */
488
489   nitems = f->nitems;
490
491   /* Number of bytes we're going to copy */
492   total_copy_bytes = (cursize < max_bytes) ? cursize : max_bytes;
493
494   if (PREDICT_TRUE (copy_here != 0))
495     {
496       /* Number of bytes in first copy segment */
497       first_copy_bytes = ((nitems - f->head) < total_copy_bytes)
498         ? (nitems - f->head) : total_copy_bytes;
499       clib_memcpy (copy_here, &f->data[f->head], first_copy_bytes);
500       f->head += first_copy_bytes;
501       f->head = (f->head == nitems) ? 0 : f->head;
502
503       /* Number of bytes in second copy segment, if any */
504       second_copy_bytes = total_copy_bytes - first_copy_bytes;
505       if (second_copy_bytes)
506         {
507           clib_memcpy (copy_here + first_copy_bytes,
508                        &f->data[f->head], second_copy_bytes);
509           f->head += second_copy_bytes;
510           f->head = (f->head == nitems) ? 0 : f->head;
511         }
512     }
513   else
514     {
515       /* Account for a zero-copy dequeue done elsewhere */
516       ASSERT (max_bytes <= cursize);
517       f->head += max_bytes;
518       f->head = f->head % nitems;
519       cursize -= max_bytes;
520       total_copy_bytes = max_bytes;
521     }
522
523   __sync_fetch_and_sub (&f->cursize, total_copy_bytes);
524
525   return (total_copy_bytes);
526 }
527
528 int
529 svm_fifo_dequeue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_here)
530 {
531   return svm_fifo_dequeue_internal (f, max_bytes, copy_here);
532 }
533
534 int
535 svm_fifo_peek (svm_fifo_t * f, u32 relative_offset, u32 max_bytes,
536                u8 * copy_here)
537 {
538   u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
539   u32 cursize, nitems, real_head;
540
541   /* read cursize, which can only increase while we're working */
542   cursize = svm_fifo_max_dequeue (f);
543   if (PREDICT_FALSE (cursize < relative_offset))
544     return -2;                  /* nothing in the fifo */
545
546   nitems = f->nitems;
547   real_head = f->head + relative_offset;
548   real_head = real_head >= nitems ? real_head - nitems : real_head;
549
550   /* Number of bytes we're going to copy */
551   total_copy_bytes = (cursize - relative_offset < max_bytes) ?
552     cursize - relative_offset : max_bytes;
553
554   if (PREDICT_TRUE (copy_here != 0))
555     {
556       /* Number of bytes in first copy segment */
557       first_copy_bytes =
558         ((nitems - real_head) < total_copy_bytes) ?
559         (nitems - real_head) : total_copy_bytes;
560       clib_memcpy (copy_here, &f->data[real_head], first_copy_bytes);
561
562       /* Number of bytes in second copy segment, if any */
563       second_copy_bytes = total_copy_bytes - first_copy_bytes;
564       if (second_copy_bytes)
565         {
566           clib_memcpy (copy_here + first_copy_bytes, &f->data[0],
567                        second_copy_bytes);
568         }
569     }
570   return total_copy_bytes;
571 }
572
573 int
574 svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes)
575 {
576   u32 total_drop_bytes, first_drop_bytes, second_drop_bytes;
577   u32 cursize, nitems;
578
579   /* read cursize, which can only increase while we're working */
580   cursize = svm_fifo_max_dequeue (f);
581   if (PREDICT_FALSE (cursize == 0))
582     return -2;                  /* nothing in the fifo */
583
584   nitems = f->nitems;
585
586   /* Number of bytes we're going to drop */
587   total_drop_bytes = (cursize < max_bytes) ? cursize : max_bytes;
588
589   /* Number of bytes in first copy segment */
590   first_drop_bytes =
591     ((nitems - f->head) < total_drop_bytes) ?
592     (nitems - f->head) : total_drop_bytes;
593   f->head += first_drop_bytes;
594   f->head = (f->head == nitems) ? 0 : f->head;
595
596   /* Number of bytes in second drop segment, if any */
597   second_drop_bytes = total_drop_bytes - first_drop_bytes;
598   if (second_drop_bytes)
599     {
600       f->head += second_drop_bytes;
601       f->head = (f->head == nitems) ? 0 : f->head;
602     }
603
604   __sync_fetch_and_sub (&f->cursize, total_drop_bytes);
605
606   return total_drop_bytes;
607 }
608
609 u32
610 svm_fifo_number_ooo_segments (svm_fifo_t * f)
611 {
612   return pool_elts (f->ooo_segments);
613 }
614
615 ooo_segment_t *
616 svm_fifo_first_ooo_segment (svm_fifo_t * f)
617 {
618   return pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
619 }
620
621 /**
622  * Set fifo pointers to requested offset
623  */
624 void
625 svm_fifo_init_pointers (svm_fifo_t * f, u32 pointer)
626 {
627   f->head = f->tail = pointer % f->nitems;
628 }
629
630 /*
631  * fd.io coding-style-patch-verification: ON
632  *
633  * Local Variables:
634  * eval: (c-set-style "gnu")
635  * End:
636  */