svm: support addition of chunks to fifos
[vpp.git] / src / svm / svm_fifo.h
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 #ifndef __included_ssvm_fifo_h__
20 #define __included_ssvm_fifo_h__
21
22 #include <vppinfra/clib.h>
23 #include <vppinfra/vec.h>
24 #include <vppinfra/mheap.h>
25 #include <vppinfra/heap.h>
26 #include <vppinfra/pool.h>
27 #include <vppinfra/format.h>
28 #include <pthread.h>
29
30 /** Out-of-order segment */
31 typedef struct
32 {
33   u32 next;     /**< Next linked-list element pool index */
34   u32 prev;     /**< Previous linked-list element pool index */
35
36   u32 start;    /**< Start of segment, normalized*/
37   u32 length;   /**< Length of segment */
38 } ooo_segment_t;
39
40 format_function_t format_ooo_segment;
41 format_function_t format_ooo_list;
42
43 #define SVM_FIFO_TRACE                  (0)
44 #define OOO_SEGMENT_INVALID_INDEX       ((u32)~0)
45 #define SVM_FIFO_INVALID_SESSION_INDEX  ((u32)~0)
46 #define SVM_FIFO_INVALID_INDEX          ((u32)~0)
47 #define SVM_FIFO_MAX_EVT_SUBSCRIBERS    8
48
49 enum svm_fifo_tx_ntf_
50 {
51   SVM_FIFO_NO_TX_NOTIF = 0,
52   SVM_FIFO_WANT_TX_NOTIF = 1,
53   SVM_FIFO_WANT_TX_NOTIF_IF_FULL = 2,
54 };
55
56 typedef struct
57 {
58   u32 offset;
59   u32 len;
60   u32 action;
61 } svm_fifo_trace_elem_t;
62
63 typedef struct svm_fifo_chunk_
64 {
65   u32 start_byte;
66   u32 length;
67   struct svm_fifo_chunk_ *next;
68   u8 data[0];
69 } svm_fifo_chunk_t;
70
71 typedef enum svm_fifo_flag_
72 {
73   SVM_FIFO_F_SIZE_UPDATE = 1 << 0,
74 } svm_fifo_flag_t;
75
76 typedef struct _svm_fifo
77 {
78   CLIB_CACHE_LINE_ALIGN_MARK (shared_first);
79   u32 size;                     /**< size of the fifo */
80   u32 nitems;                   /**< usable size(size-1) */
81   u8 flags;                     /**< fifo flags */
82   svm_fifo_chunk_t *start_chunk;/**< first chunk in fifo chunk list */
83   svm_fifo_chunk_t *end_chunk;  /**< end chunk in fifo chunk list */
84   svm_fifo_chunk_t *new_chunks; /**< chunks yet to be added to list */
85
86     CLIB_CACHE_LINE_ALIGN_MARK (shared_second);
87   volatile u32 has_event;       /**< non-zero if deq event exists */
88
89   u32 master_session_index;
90   u32 client_session_index;
91   u8 master_thread_index;
92   u8 client_thread_index;
93   u32 segment_manager;
94   u32 segment_index;
95   u32 ct_session_index;         /**< Local session index for vpp */
96   u32 freelist_index;           /**< aka log2(allocated_size) - const. */
97   i8 refcnt;                    /**< reference count  */
98   struct _svm_fifo *next;       /**< next in freelist/active chain */
99   struct _svm_fifo *prev;       /**< prev in active chain */
100
101     CLIB_CACHE_LINE_ALIGN_MARK (consumer);
102   u32 head;
103   svm_fifo_chunk_t *head_chunk; /**< tracks chunk where head lands */
104   volatile u32 want_tx_ntf;     /**< producer wants nudge */
105   volatile u32 has_tx_ntf;
106
107     CLIB_CACHE_LINE_ALIGN_MARK (producer);
108   u32 tail;
109   svm_fifo_chunk_t *tail_chunk; /**< tracks chunk where tail lands */
110
111   ooo_segment_t *ooo_segments;  /**< Pool of ooo segments */
112   u32 ooos_list_head;           /**< Head of out-of-order linked-list */
113   u32 ooos_newest;              /**< Last segment to have been updated */
114   volatile u8 n_subscribers;
115   u8 subscribers[SVM_FIFO_MAX_EVT_SUBSCRIBERS];
116
117 #if SVM_FIFO_TRACE
118   svm_fifo_trace_elem_t *trace;
119 #endif
120
121   svm_fifo_chunk_t default_chunk;
122 } svm_fifo_t;
123
124 typedef enum
125 {
126   SVM_FIFO_FULL = -2,
127 } svm_fifo_err_t;
128
129 typedef struct svm_fifo_segment_
130 {
131   u8 *data;
132   u32 len;
133 } svm_fifo_segment_t;
134
135 #if SVM_FIFO_TRACE
136 #define svm_fifo_trace_add(_f, _s, _l, _t)              \
137 {                                                       \
138   svm_fifo_trace_elem_t *trace_elt;                     \
139   vec_add2(_f->trace, trace_elt, 1);                    \
140   trace_elt->offset = _s;                               \
141   trace_elt->len = _l;                                  \
142   trace_elt->action = _t;                               \
143 }
144 #else
145 #define svm_fifo_trace_add(_f, _s, _l, _t)
146 #endif
147
148 u8 *svm_fifo_dump_trace (u8 * s, svm_fifo_t * f);
149 u8 *svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose);
150
151 /* internal function */
152 static inline void
153 f_load_head_tail_cons (svm_fifo_t * f, u32 * head, u32 * tail)
154 {
155   /* load-relaxed: consumer owned index */
156   *head = f->head;
157   /* load-acq: consumer foreign index (paired with store-rel in producer) */
158   *tail = clib_atomic_load_acq_n (&f->tail);
159 }
160
161 /* internal function */
162 static inline void
163 f_load_head_tail_prod (svm_fifo_t * f, u32 * head, u32 * tail)
164 {
165   /* load relaxed: producer owned index */
166   *tail = f->tail;
167   /* load-acq: producer foreign index (paired with store-rel in consumer) */
168   *head = clib_atomic_load_acq_n (&f->head);
169 }
170
171 /* producer consumer role independent */
172 /* internal function */
173 static inline void
174 f_load_head_tail_all_acq (svm_fifo_t * f, u32 * head, u32 * tail)
175 {
176   /* load-acq : consumer foreign index (paired with store-rel) */
177   *tail = clib_atomic_load_acq_n (&f->tail);
178   /* load-acq : producer foriegn index (paired with store-rel) */
179   *head = clib_atomic_load_acq_n (&f->head);
180 }
181
182 /* internal function */
183 static inline u32
184 f_free_count (svm_fifo_t * f, u32 head, u32 tail)
185 {
186   return (f->nitems + head - tail);
187 }
188
189 /* internal function */
190 static inline u32
191 f_cursize (svm_fifo_t * f, u32 head, u32 tail)
192 {
193   return (f->nitems - f_free_count (f, head, tail));
194 }
195
196 /* used by consumer */
197 static inline u32
198 svm_fifo_max_dequeue_cons (svm_fifo_t * f)
199 {
200   u32 tail, head;
201   f_load_head_tail_cons (f, &head, &tail);
202   return f_cursize (f, head, tail);
203 }
204
205 /* used by producer*/
206 static inline u32
207 svm_fifo_max_dequeue_prod (svm_fifo_t * f)
208 {
209   u32 tail, head;
210   f_load_head_tail_prod (f, &head, &tail);
211   return f_cursize (f, head, tail);
212 }
213
214 /* use producer or consumer specific functions for perfomance.
215  * svm_fifo_max_dequeue_cons (svm_fifo_t *f)
216  * svm_fifo_max_dequeue_prod (svm_fifo_t *f)
217  */
218 static inline u32
219 svm_fifo_max_dequeue (svm_fifo_t * f)
220 {
221   u32 tail, head;
222   f_load_head_tail_all_acq (f, &head, &tail);
223   return f_cursize (f, head, tail);
224 }
225
226 /* used by producer */
227 static inline int
228 svm_fifo_is_full_prod (svm_fifo_t * f)
229 {
230   return (svm_fifo_max_dequeue_prod (f) == f->nitems);
231 }
232
233 /* use producer or consumer specific functions for perfomance.
234  * svm_fifo_is_full_prod (svm_fifo_t * f)
235  * add cons version if needed
236  */
237 static inline int
238 svm_fifo_is_full (svm_fifo_t * f)
239 {
240   return (svm_fifo_max_dequeue (f) == f->nitems);
241 }
242
243 /* used by consumer */
244 static inline int
245 svm_fifo_is_empty_cons (svm_fifo_t * f)
246 {
247   return (svm_fifo_max_dequeue_cons (f) == 0);
248 }
249
250 /* used by producer */
251 static inline int
252 svm_fifo_is_empty_prod (svm_fifo_t * f)
253 {
254   return (svm_fifo_max_dequeue_prod (f) == 0);
255 }
256
257 /* use producer or consumer specific functions for perfomance.
258  * svm_fifo_is_empty_cons (svm_fifo_t * f)
259  * svm_fifo_is_empty_prod (svm_fifo_t * f)
260  */
261 static inline int
262 svm_fifo_is_empty (svm_fifo_t * f)
263 {
264   return (svm_fifo_max_dequeue (f) == 0);
265 }
266
267 /* used by producer*/
268 static inline u32
269 svm_fifo_max_enqueue_prod (svm_fifo_t * f)
270 {
271   u32 head, tail;
272   f_load_head_tail_prod (f, &head, &tail);
273   return f_free_count (f, head, tail);
274 }
275
276 /* use producer or consumer specfic functions for perfomance.
277  * svm_fifo_max_enqueue_prod (svm_fifo_t *f)
278  * add consumer specific version if needed.
279  */
280 static inline u32
281 svm_fifo_max_enqueue (svm_fifo_t * f)
282 {
283   u32 head, tail;
284   f_load_head_tail_all_acq (f, &head, &tail);
285   return f_free_count (f, head, tail);
286 }
287
288 static inline int
289 svm_fifo_has_event (svm_fifo_t * f)
290 {
291   return f->has_event;
292 }
293
294 static inline u8
295 svm_fifo_has_ooo_data (svm_fifo_t * f)
296 {
297   return f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX;
298 }
299
300 /**
301  * Sets fifo event flag.
302  *
303  * Also acts as a release ordering.
304  *
305  * @return 1 if flag was not set.
306  */
307 always_inline u8
308 svm_fifo_set_event (svm_fifo_t * f)
309 {
310   /* return __sync_lock_test_and_set (&f->has_event, 1) == 0;
311      return __sync_bool_compare_and_swap (&f->has_event, 0, 1); */
312   return !clib_atomic_swap_rel_n (&f->has_event, 1);
313 }
314
315 /**
316  * Unsets fifo event flag.
317  *
318  * Also acts as an acquire barrier.
319  */
320 always_inline void
321 svm_fifo_unset_event (svm_fifo_t * f)
322 {
323   clib_atomic_swap_acq_n (&f->has_event, 0);
324 }
325
326 svm_fifo_t *svm_fifo_create (u32 data_size_in_bytes);
327 void svm_fifo_init (svm_fifo_t * f, u32 size);
328 void svm_fifo_free (svm_fifo_t * f);
329
330 int svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 max_bytes,
331                              const u8 * copy_from_here);
332 int svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset,
333                                   u32 required_bytes, u8 * copy_from_here);
334 int svm_fifo_dequeue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_here);
335
336 int svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 max_bytes, u8 * copy_here);
337 int svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes);
338 void svm_fifo_dequeue_drop_all (svm_fifo_t * f);
339 int svm_fifo_segments (svm_fifo_t * f, svm_fifo_segment_t * fs);
340 void svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_segment_t * fs);
341 void svm_fifo_init_pointers (svm_fifo_t * f, u32 pointer);
342 void svm_fifo_clone (svm_fifo_t * df, svm_fifo_t * sf);
343 void svm_fifo_overwrite_head (svm_fifo_t * f, u8 * data, u32 len);
344 void svm_fifo_add_subscriber (svm_fifo_t * f, u8 subscriber);
345 void svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber);
346 format_function_t format_svm_fifo;
347
348 /**
349  * Max contiguous chunk of data that can be read
350  */
351 always_inline u32
352 svm_fifo_max_read_chunk (svm_fifo_t * f)
353 {
354   u32 head, tail;
355   u32 head_idx, tail_idx;
356   f_load_head_tail_cons (f, &head, &tail);
357   head_idx = head % f->size;
358   tail_idx = tail % f->size;
359   return tail_idx > head_idx ? (tail_idx - head_idx) : (f->size - head_idx);
360 }
361
362 /**
363  * Max contiguous chunk of data that can be written
364  */
365 always_inline u32
366 svm_fifo_max_write_chunk (svm_fifo_t * f)
367 {
368   u32 head, tail;
369   u32 head_idx, tail_idx;
370   f_load_head_tail_prod (f, &head, &tail);
371   head_idx = head % f->size;
372   tail_idx = tail % f->size;
373   return tail_idx >= head_idx ? (f->size - tail_idx) : (head_idx - tail_idx);
374 }
375
376 /**
377  * Advance tail pointer
378  *
379  * Useful for moving tail pointer after external enqueue.
380  */
381 always_inline void
382 svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 bytes)
383 {
384   ASSERT (bytes <= svm_fifo_max_enqueue_prod (f));
385   /* load-relaxed: producer owned index */
386   u32 tail = f->tail;
387   tail += bytes;
388   /* store-rel: producer owned index (paired with load-acq in consumer) */
389   clib_atomic_store_rel_n (&f->tail, tail);
390 }
391
392 always_inline u8 *
393 svm_fifo_head (svm_fifo_t * f)
394 {
395   /* load-relaxed: consumer owned index */
396   return (f->head_chunk->data
397           + ((f->head % f->size) - f->head_chunk->start_byte));
398 }
399
400 always_inline u8 *
401 svm_fifo_tail (svm_fifo_t * f)
402 {
403   /* load-relaxed: producer owned index */
404   return (f->tail_chunk->data
405           + ((f->tail % f->size) - f->tail_chunk->start_byte));
406 }
407
408 static inline void
409 svm_fifo_add_want_tx_ntf (svm_fifo_t * f, u8 ntf_type)
410 {
411   f->want_tx_ntf |= ntf_type;
412 }
413
414 static inline void
415 svm_fifo_del_want_tx_ntf (svm_fifo_t * f, u8 ntf_type)
416 {
417   f->want_tx_ntf &= ~ntf_type;
418 }
419
420 static inline void
421 svm_fifo_clear_tx_ntf (svm_fifo_t * f)
422 {
423   /* Set the flag if want_tx_notif_if_full was the only ntf requested */
424   f->has_tx_ntf = f->want_tx_ntf == SVM_FIFO_WANT_TX_NOTIF_IF_FULL;
425   svm_fifo_del_want_tx_ntf (f, SVM_FIFO_WANT_TX_NOTIF);
426 }
427
428 static inline void
429 svm_fifo_reset_tx_ntf (svm_fifo_t * f)
430 {
431   f->has_tx_ntf = 0;
432 }
433
434 static inline u8
435 svm_fifo_needs_tx_ntf (svm_fifo_t * f, u32 n_last_deq)
436 {
437   u8 want_ntf = f->want_tx_ntf;
438
439   if (PREDICT_TRUE (want_ntf == SVM_FIFO_NO_TX_NOTIF))
440     return 0;
441   else if (want_ntf & SVM_FIFO_WANT_TX_NOTIF)
442     return 1;
443   else if (want_ntf & SVM_FIFO_WANT_TX_NOTIF_IF_FULL)
444     {
445       u32 max_deq = svm_fifo_max_dequeue_cons (f);
446       u32 nitems = f->nitems;
447       if (!f->has_tx_ntf && max_deq < nitems
448           && max_deq + n_last_deq >= nitems)
449         return 1;
450
451       return 0;
452     }
453   return 0;
454 }
455
456 always_inline u8
457 svm_fifo_n_subscribers (svm_fifo_t * f)
458 {
459   return f->n_subscribers;
460 }
461
462 u32 svm_fifo_number_ooo_segments (svm_fifo_t * f);
463 ooo_segment_t *svm_fifo_first_ooo_segment (svm_fifo_t * f);
464
465 always_inline ooo_segment_t *
466 svm_fifo_newest_ooo_segment (svm_fifo_t * f)
467 {
468   if (f->ooos_newest == OOO_SEGMENT_INVALID_INDEX)
469     return 0;
470   return pool_elt_at_index (f->ooo_segments, f->ooos_newest);
471 }
472
473 always_inline void
474 svm_fifo_newest_ooo_segment_reset (svm_fifo_t * f)
475 {
476   f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
477 }
478
479 always_inline u32
480 ooo_segment_distance_from_tail (svm_fifo_t * f, u32 pos, u32 tail)
481 {
482   return ((pos - tail) % f->size);
483 }
484
485 always_inline u32
486 ooo_segment_distance_to_tail (svm_fifo_t * f, u32 pos, u32 tail)
487 {
488   return ((tail - pos) % f->size);
489 }
490
491 always_inline u32
492 ooo_segment_offset_prod (svm_fifo_t * f, ooo_segment_t * s)
493 {
494   u32 tail;
495   /* load-relaxed: producer owned index */
496   tail = f->tail;
497
498   return ooo_segment_distance_from_tail (f, s->start, tail);
499 }
500
501 always_inline u32
502 ooo_segment_length (svm_fifo_t * f, ooo_segment_t * s)
503 {
504   return s->length;
505 }
506
507 always_inline ooo_segment_t *
508 ooo_segment_get_prev (svm_fifo_t * f, ooo_segment_t * s)
509 {
510   if (s->prev == OOO_SEGMENT_INVALID_INDEX)
511     return 0;
512   return pool_elt_at_index (f->ooo_segments, s->prev);
513 }
514
515 always_inline ooo_segment_t *
516 ooo_segment_next (svm_fifo_t * f, ooo_segment_t * s)
517 {
518   if (s->next == OOO_SEGMENT_INVALID_INDEX)
519     return 0;
520   return pool_elt_at_index (f->ooo_segments, s->next);
521 }
522
523 static inline u8
524 svm_fifo_is_wrapped (svm_fifo_t * f)
525 {
526   u32 head, tail;
527   f_load_head_tail_all_acq (f, &head, &tail);
528   return head % f->size > tail % f->size;
529 }
530
531 static inline void
532 svm_fifo_size_update (svm_fifo_t * f, svm_fifo_chunk_t * c)
533 {
534   svm_fifo_chunk_t *prev;
535   u32 add_bytes = 0;
536
537   prev = f->end_chunk;
538   while (c)
539     {
540       c->start_byte = prev->start_byte + prev->length;
541       add_bytes += c->length;
542       prev->next = c;
543       prev = c;
544       c = c->next;
545     }
546   f->end_chunk = prev;
547   prev->next = f->start_chunk;
548   f->size += add_bytes;
549   f->nitems = f->size - 1;
550   f->new_chunks = 0;
551 }
552
553 static inline void
554 svm_fifo_add_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c)
555 {
556   if (svm_fifo_is_wrapped (f))
557     {
558       if (f->new_chunks)
559         {
560           svm_fifo_chunk_t *prev;
561
562           prev = f->new_chunks;
563           while (prev->next)
564             prev = prev->next;
565           prev->next = c;
566         }
567       else
568         {
569           f->new_chunks = c;
570         }
571       f->flags |= SVM_FIFO_F_SIZE_UPDATE;
572       return;
573     }
574
575   svm_fifo_size_update (f, c);
576 }
577
578 static inline void
579 svm_fifo_try_size_update (svm_fifo_t * f, u32 new_head)
580 {
581   if (new_head % f->size > f->tail % f->size)
582     return;
583
584   svm_fifo_size_update (f, f->new_chunks);
585   f->flags &= ~SVM_FIFO_F_SIZE_UPDATE;
586 }
587 #endif /* __included_ssvm_fifo_h__ */
588
589 /*
590  * fd.io coding-style-patch-verification: ON
591  *
592  * Local Variables:
593  * eval: (c-set-style "gnu")
594  * End:
595  */