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