2b6e8542cdf120f5edb42d674480442cbcd603d5
[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   u32 start;    /**< Start of segment, normalized*/
34   u32 length;   /**< Length of segment */
35 } ooo_segment_t;
36
37 #define SVM_FIFO_TRACE                  (0)
38 #define OOO_SEGMENT_INVALID_INDEX       ((u32)~0)
39 #define SVM_FIFO_INVALID_SESSION_INDEX  ((u32)~0)
40 #define SVM_FIFO_INVALID_INDEX          ((u32)~0)
41 #define SVM_FIFO_MAX_EVT_SUBSCRIBERS    7
42
43 typedef enum svm_fifo_deq_ntf_
44 {
45   SVM_FIFO_NO_DEQ_NOTIF = 0,            /**< No notification requested */
46   SVM_FIFO_WANT_DEQ_NOTIF = 1,          /**< Notify on dequeue */
47   SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL = 2,  /**< Notify on transition from full */
48   SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY = 4, /**< Notify on transition to empty */
49 } svm_fifo_deq_ntf_t;
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;               /**< chunk start byte */
61   u32 length;                   /**< length of chunk in bytes */
62   struct svm_fifo_chunk_ *next; /**< pointer to next chunk in linked-lists */
63   rb_node_index_t rb_index;     /**< node index if chunk in rbtree */
64   u8 data[0];                   /**< start of chunk data */
65 } svm_fifo_chunk_t;
66
67 typedef enum svm_fifo_flag_
68 {
69   SVM_FIFO_F_MULTI_CHUNK = 1 << 0,
70   SVM_FIFO_F_GROW = 1 << 1,
71   SVM_FIFO_F_SHRINK = 1 << 2,
72   SVM_FIFO_F_COLLECT_CHUNKS = 1 << 3,
73   SVM_FIFO_F_LL_TRACKED = 1 << 4,
74   SVM_FIFO_F_SINGLE_THREAD_OWNED = 1 << 5,
75 } svm_fifo_flag_t;
76
77 typedef struct _svm_fifo
78 {
79   CLIB_CACHE_LINE_ALIGN_MARK (shared_first);
80   u32 size;                     /**< size of the fifo in bytes */
81   u32 nitems;                   /**< usable size (size-1) */
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   rb_tree_t ooo_enq_lookup;     /**< rbtree for ooo enq chunk lookup */
85   rb_tree_t ooo_deq_lookup;     /**< rbtree for ooo deq chunk lookup */
86   u8 flags;                     /**< fifo flags */
87   u8 slice_index;               /**< segment slice for fifo */
88
89     CLIB_CACHE_LINE_ALIGN_MARK (shared_second);
90   volatile u32 has_event;       /**< non-zero if deq event exists */
91   u32 master_session_index;     /**< session layer session index */
92   u32 client_session_index;     /**< app session index */
93   u8 master_thread_index;       /**< session layer thread index */
94   u8 client_thread_index;       /**< app worker index */
95   i8 refcnt;                    /**< reference count  */
96   u32 segment_manager;          /**< session layer segment manager index */
97   u32 segment_index;            /**< segment index in segment manager */
98   struct _svm_fifo *next;       /**< next in freelist/active chain */
99   struct _svm_fifo *prev;       /**< prev in active chain */
100   svm_fifo_chunk_t *new_chunks; /**< chunks yet to be added to list */
101   u32 size_decrement;           /**< bytes to remove from fifo */
102
103     CLIB_CACHE_LINE_ALIGN_MARK (consumer);
104   u32 head;                     /**< fifo head position/byte */
105   svm_fifo_chunk_t *head_chunk; /**< tracks chunk where head lands */
106   svm_fifo_chunk_t *ooo_deq;    /**< last chunk used for ooo dequeue */
107   volatile u32 want_deq_ntf;    /**< producer wants nudge */
108   volatile u32 has_deq_ntf;
109
110     CLIB_CACHE_LINE_ALIGN_MARK (producer);
111   u32 tail;                     /**< fifo tail position/byte */
112   u32 ooos_list_head;           /**< Head of out-of-order linked-list */
113   svm_fifo_chunk_t *tail_chunk; /**< tracks chunk where tail lands */
114   svm_fifo_chunk_t *ooo_enq;    /**< last chunk used for ooo enqueue */
115   ooo_segment_t *ooo_segments;  /**< Pool of ooo segments */
116   u32 ooos_newest;              /**< Last segment to have been updated */
117   volatile u8 n_subscribers;    /**< Number of subscribers for io events */
118   u8 subscribers[SVM_FIFO_MAX_EVT_SUBSCRIBERS];
119
120 #if SVM_FIFO_TRACE
121   svm_fifo_trace_elem_t *trace;
122 #endif
123
124 } svm_fifo_t;
125
126 typedef enum
127 {
128   SVM_FIFO_EFULL = -2,
129   SVM_FIFO_EEMPTY = -3,
130 } svm_fifo_err_t;
131
132 typedef struct svm_fifo_seg_
133 {
134   u8 *data;
135   u32 len;
136 } svm_fifo_seg_t;
137
138 #if SVM_FIFO_TRACE
139 #define svm_fifo_trace_add(_f, _s, _l, _t)              \
140 {                                                       \
141   svm_fifo_trace_elem_t *trace_elt;                     \
142   vec_add2(_f->trace, trace_elt, 1);                    \
143   trace_elt->offset = _s;                               \
144   trace_elt->len = _l;                                  \
145   trace_elt->action = _t;                               \
146 }
147 #else
148 #define svm_fifo_trace_add(_f, _s, _l, _t)
149 #endif
150
151 u8 *svm_fifo_dump_trace (u8 * s, svm_fifo_t * f);
152 u8 *svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose);
153
154 /**
155  * Load head and tail optimized for consumer
156  *
157  * Internal function.
158  */
159 static inline void
160 f_load_head_tail_cons (svm_fifo_t * f, u32 * head, u32 * tail)
161 {
162   /* load-relaxed: consumer owned index */
163   *head = f->head;
164   /* load-acq: consumer foreign index (paired with store-rel in producer) */
165   *tail = clib_atomic_load_acq_n (&f->tail);
166 }
167
168 /** Load head and tail optimized for producer
169  *
170  * Internal function
171  */
172 static inline void
173 f_load_head_tail_prod (svm_fifo_t * f, u32 * head, u32 * tail)
174 {
175   /* load relaxed: producer owned index */
176   *tail = f->tail;
177   /* load-acq: producer foreign index (paired with store-rel in consumer) */
178   *head = clib_atomic_load_acq_n (&f->head);
179 }
180
181 /**
182  * Load head and tail independent of producer/consumer role
183  *
184  * Internal function.
185  */
186 static inline void
187 f_load_head_tail_all_acq (svm_fifo_t * f, u32 * head, u32 * tail)
188 {
189   /* load-acq : consumer foreign index (paired with store-rel) */
190   *tail = clib_atomic_load_acq_n (&f->tail);
191   /* load-acq : producer foriegn index (paired with store-rel) */
192   *head = clib_atomic_load_acq_n (&f->head);
193 }
194
195 /**
196  * Distance to a from b, i.e., a - b in the fifo
197  *
198  * Internal function.
199  */
200 static inline u32
201 f_distance_to (svm_fifo_t * f, u32 a, u32 b)
202 {
203   return ((f->size + a - b) % f->size);
204 }
205
206 /**
207  * Distance from a to b, i.e., b - a in the fifo
208  *
209  * Internal function.
210  */
211 static inline u32
212 f_distance_from (svm_fifo_t * f, u32 a, u32 b)
213 {
214   return ((f->size + b - a) % f->size);
215 }
216
217 /**
218  * Fifo current size, i.e., number of bytes enqueued
219  *
220  * Internal function.
221  */
222 static inline u32
223 f_cursize (svm_fifo_t * f, u32 head, u32 tail)
224 {
225   return (head <= tail ? tail - head : f->size + tail - head);
226 }
227
228 /**
229  * Fifo free bytes, i.e., number of free bytes
230  *
231  * Internal function
232  */
233 static inline u32
234 f_free_count (svm_fifo_t * f, u32 head, u32 tail)
235 {
236   return (f->nitems - f_cursize (f, head, tail));
237 }
238
239 /**
240  * Try to shrink fifo size.
241  *
242  * Internal function.
243  */
244 void svm_fifo_try_shrink (svm_fifo_t * f, u32 head, u32 tail);
245
246 /**
247  * Create fifo of requested size
248  *
249  * Allocates fifo on current heap.
250  *
251  * @param size          data size in bytes for fifo to be allocated. Will be
252  *                      rounded to the next highest power-of-two value.
253  * @return              pointer to new fifo
254  */
255 svm_fifo_t *svm_fifo_create (u32 size);
256 /**
257  * Initialize fifo
258  *
259  * @param f             fifo
260  * @param size          size for fifo
261  */
262 void svm_fifo_init (svm_fifo_t * f, u32 size);
263 /**
264  * Initialize fifo chunks and rbtree
265  *
266  * @param f             fifo
267  */
268 void svm_fifo_init_chunks (svm_fifo_t * f);
269 /**
270  * Allocate a fifo chunk on heap
271  *
272  * If the chunk is allocated on a fifo segment, this should be called
273  * with the segment's heap pushed.
274  *
275  * @param size  chunk size in bytes. Will be rounded to the next highest
276  *              power-of-two
277  * @return      new chunk or 0 if alloc failed
278  */
279 svm_fifo_chunk_t *svm_fifo_chunk_alloc (u32 size);
280 /**
281  * Grow fifo size by adding chunk to chunk list
282  *
283  * If fifos are allocated on a segment, this should be called with
284  * the segment's heap pushed.
285  *
286  * @param f     fifo to be extended
287  * @param c     chunk or linked list of chunks to be added
288  */
289 void svm_fifo_add_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c);
290 /**
291  * Request to reduce fifo size by amount of bytes
292  *
293  * Because the producer might be enqueuing data when this is called, the
294  * actual size update is only applied when producer tries to enqueue new
295  * data, unless @param try_shrink is set.
296  *
297  * @param f             fifo
298  * @param len           number of bytes to remove from fifo. The actual number
299  *                      of bytes to be removed will be less or equal to this
300  *                      value.
301  * @param try_shrink    flg to indicate if it's safe to try to shrink fifo
302  *                      size. It should be set only if this is called by the
303  *                      producer of if the producer is not using the fifo
304  * @return              actual length fifo size will be reduced by
305  */
306 int svm_fifo_reduce_size (svm_fifo_t * f, u32 len, u8 try_shrink);
307 /**
308  * Removes chunks that are after fifo end byte
309  *
310  * Needs to be called with segment heap pushed.
311  *
312  * @param f fifo
313  */
314 svm_fifo_chunk_t *svm_fifo_collect_chunks (svm_fifo_t * f);
315 /**
316  * Free fifo and associated state
317  *
318  * @param f     fifo
319  */
320 void svm_fifo_free (svm_fifo_t * f);
321 /**
322  * Cleanup fifo chunk lookup rb tree
323  *
324  * The rb tree is allocated in segment heap so this should be called
325  * with it pushed.
326  *
327  * @param f     fifo to cleanup
328  */
329 void svm_fifo_free_chunk_lookup (svm_fifo_t * f);
330 /**
331  * Cleanup fifo ooo data
332  *
333  * The ooo data is allocated in producer process memory. The fifo
334  * segment heap should not be pushed.
335  *
336  * @param f     fifo to cleanup
337  */
338 void svm_fifo_free_ooo_data (svm_fifo_t * f);
339 /**
340  * Init fifo head and tail
341  *
342  * @param f     fifo
343  * @param head  head value that will be matched to a chunk
344  * @param tail  tail value that will be matched to a chunk
345  */
346 void svm_fifo_init_pointers (svm_fifo_t * f, u32 head, u32 tail);
347 /**
348  * Clone fifo
349  *
350  * Clones single/default chunk fifo. It does not work for fifos with
351  * multiple chunks.
352  */
353 void svm_fifo_clone (svm_fifo_t * df, svm_fifo_t * sf);
354 /**
355  * Enqueue data to fifo
356  *
357  * Data is enqueued and tail pointer is updated atomically. If the new data
358  * enqueued partly overlaps or "touches" an out-of-order segment, said segment
359  * is "consumed" and the number of bytes returned is appropriately updated.
360  *
361  * @param f     fifo
362  * @param len   length of data to copy
363  * @param src   buffer from where to copy the data
364  * @return      number of contiguous bytes that can be consumed or error
365  */
366 int svm_fifo_enqueue (svm_fifo_t * f, u32 len, const u8 * src);
367 /**
368  * Enqueue data to fifo with offset
369  *
370  * Data is enqueued without updating tail pointer. Instead, an out-of-order
371  * list of segments is generated and maintained. Fifo takes care of coalescing
372  * contiguous or overlapping segments.
373  *
374  * @param f             fifo
375  * @param offset        offset at which to copy the data
376  * @param len           len of data to copy
377  * @param src           buffer from where to copy the data
378  * @return              0 if enqueue was successful, error otherwise
379  */
380 int svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 len,
381                                   u8 * src);
382
383 /**
384  * Advance tail pointer
385  *
386  * Useful for moving tail pointer after external enqueue.
387  *
388  * @param f             fifo
389  * @param len           number of bytes to add to tail
390  */
391 void svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 len);
392 /**
393  * Overwrite fifo head with new data
394  *
395  * This should be typically used by dgram transport protocols that need
396  * to update the dgram header after dequeueing a chunk of data. It assumes
397  * that the dgram header is at most spread over two chunks.
398  *
399  * @param f             fifo
400  * @param src           src of new data
401  * @param len           length of new data
402  */
403 void svm_fifo_overwrite_head (svm_fifo_t * f, u8 * src, u32 len);
404 /**
405  * Dequeue data from fifo
406  *
407  * Data is dequeued to consumer provided buffer and head is atomically
408  * updated.
409  *
410  * @param f             fifo
411  * @param len           length of data to dequeue
412  * @param dst           buffer to where to dequeue the data
413  * @return              number of bytes dequeued or error
414  */
415 int svm_fifo_dequeue (svm_fifo_t * f, u32 len, u8 * dst);
416 /**
417  * Peek data from fifo
418  *
419  * Data is copied from requested offset into provided dst buffer. Head is
420  * not updated.
421  *
422  * @param f             fifo
423  * @param offset        offset from which to copy the data
424  * @param len           length of data to copy
425  * @param dst           buffer to where to dequeue the data
426  * @return              number of bytes peeked
427  */
428 int svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 len, u8 * dst);
429 /**
430  * Dequeue and drop bytes from fifo
431  *
432  * Advances fifo head by requested amount of bytes.
433  *
434  * @param f             fifo
435  * @param len           number of bytes to drop
436  * @return              number of bytes dropped
437  */
438 int svm_fifo_dequeue_drop (svm_fifo_t * f, u32 len);
439 /**
440  * Dequeue and drop all bytes from fifo
441  *
442  * Advances head to tail position.
443  *
444  * @param f             fifo
445  */
446 void svm_fifo_dequeue_drop_all (svm_fifo_t * f);
447 int svm_fifo_segments (svm_fifo_t * f, svm_fifo_seg_t * fs);
448 void svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_seg_t * fs);
449 /**
450  * Add io events subscriber to list
451  *
452  * @param f     fifo
453  * @param sub   subscriber opaque index (typically app worker index)
454  */
455 void svm_fifo_add_subscriber (svm_fifo_t * f, u8 sub);
456 /**
457  * Remove io events subscriber form list
458  *
459  * @param f     fifo
460  * @param sub   subscriber index to be removed
461  */
462 void svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber);
463 /**
464  * Number of out-of-order segments for fifo
465  *
466  * @param f     fifo
467  * @return      number of out of order segments
468  */
469 u32 svm_fifo_n_ooo_segments (svm_fifo_t * f);
470 /**
471  * First out-of-order segment for fifo
472  *
473  * @param f     fifo
474  * @return      first out-of-order segment for fifo
475  */
476 ooo_segment_t *svm_fifo_first_ooo_segment (svm_fifo_t * f);
477 /**
478  * Check if fifo is sane. Debug only.
479  *
480  * @param f     fifo
481  * @return      1 if sane, 0 otherwise
482  */
483 u8 svm_fifo_is_sane (svm_fifo_t * f);
484 /**
485  * Declare this fifo is used by only a single thread.
486  * In this special case, fifo-growth can be done in an efficient way without delay.
487  *
488  * @param f             fifo
489  * @return              1 if the fifo is already owned by another thread, 0 otherwise
490  */
491 u8 svm_fifo_set_single_thread_owned (svm_fifo_t * f);
492 format_function_t format_svm_fifo;
493
494 /**
495  * Fifo max bytes to dequeue optimized for consumer
496  *
497  * @param f     fifo
498  * @return      max number of bytes that can be dequeued
499  */
500 static inline u32
501 svm_fifo_max_dequeue_cons (svm_fifo_t * f)
502 {
503   u32 tail, head;
504   f_load_head_tail_cons (f, &head, &tail);
505   return f_cursize (f, head, tail);
506 }
507
508 /**
509  * Fifo max bytes to dequeue optimized for producer
510  *
511  * @param f     fifo
512  * @return      max number of bytes that can be dequeued
513  */
514 static inline u32
515 svm_fifo_max_dequeue_prod (svm_fifo_t * f)
516 {
517   u32 tail, head;
518   f_load_head_tail_prod (f, &head, &tail);
519   return f_cursize (f, head, tail);
520 }
521
522 /**
523  * Fifo max bytes to dequeue
524  *
525  * Note: use producer or consumer specific functions for performance:
526  * @ref svm_fifo_max_dequeue_cons (svm_fifo_t *f)
527  * @ref svm_fifo_max_dequeue_prod (svm_fifo_t *f)
528  */
529 static inline u32
530 svm_fifo_max_dequeue (svm_fifo_t * f)
531 {
532   u32 tail, head;
533   f_load_head_tail_all_acq (f, &head, &tail);
534   return f_cursize (f, head, tail);
535 }
536
537 /**
538  * Check if fifo is full optimized for producer
539  *
540  * @param f     fifo
541  * @return      1 if fifo is full 0 otherwise
542  */
543 static inline int
544 svm_fifo_is_full_prod (svm_fifo_t * f)
545 {
546   return (svm_fifo_max_dequeue_prod (f) == f->nitems);
547 }
548
549 /* Check if fifo is full.
550  *
551  * Note: use producer or consumer specific functions for performance.
552  * @ref svm_fifo_is_full_prod (svm_fifo_t * f)
553  * add cons version if needed
554  */
555 static inline int
556 svm_fifo_is_full (svm_fifo_t * f)
557 {
558   return (svm_fifo_max_dequeue (f) == f->nitems);
559 }
560
561 /**
562  * Check if fifo is empty optimized for consumer
563  *
564  * @param f     fifo
565  * @return      1 if fifo is empty 0 otherwise
566  */
567 static inline int
568 svm_fifo_is_empty_cons (svm_fifo_t * f)
569 {
570   return (svm_fifo_max_dequeue_cons (f) == 0);
571 }
572
573 /**
574  * Check if fifo is empty optimized for producer
575  *
576  * @param f     fifo
577  * @return      1 if fifo is empty 0 otherwise
578  */
579 static inline int
580 svm_fifo_is_empty_prod (svm_fifo_t * f)
581 {
582   return (svm_fifo_max_dequeue_prod (f) == 0);
583 }
584
585 /**
586  * Check if fifo is empty
587  *
588  * Note: use producer or consumer specific functions for perfomance.
589  * @ref svm_fifo_is_empty_cons (svm_fifo_t * f)
590  * @ref svm_fifo_is_empty_prod (svm_fifo_t * f)
591  */
592 static inline int
593 svm_fifo_is_empty (svm_fifo_t * f)
594 {
595   return (svm_fifo_max_dequeue (f) == 0);
596 }
597
598 /**
599  * Check if fifo is wrapped
600  *
601  * @param f     fifo
602  * @return      1 if 'normalized' head is ahead of tail
603  */
604 static inline u8
605 svm_fifo_is_wrapped (svm_fifo_t * f)
606 {
607   u32 head, tail;
608   f_load_head_tail_all_acq (f, &head, &tail);
609   return head > tail;
610 }
611
612 /**
613  * Maximum number of bytes that can be enqueued into fifo
614  *
615  * Optimized for producer
616  *
617  * @param f     fifo
618  * @return      max number of bytes that can be enqueued into fifo
619  */
620 static inline u32
621 svm_fifo_max_enqueue_prod (svm_fifo_t * f)
622 {
623   u32 head, tail;
624   f_load_head_tail_prod (f, &head, &tail);
625   if (PREDICT_FALSE (f->flags & SVM_FIFO_F_SHRINK))
626     svm_fifo_try_shrink (f, head, tail);
627   return f_free_count (f, head, tail);
628 }
629
630 /* Maximum number of bytes that can be enqueued into fifo
631  *
632  * Note: use producer or consumer specific functions for performance.
633  * @ref svm_fifo_max_enqueue_prod (svm_fifo_t *f)
634  * add consumer specific version if needed.
635  */
636 static inline u32
637 svm_fifo_max_enqueue (svm_fifo_t * f)
638 {
639   u32 head, tail;
640   f_load_head_tail_all_acq (f, &head, &tail);
641   if (PREDICT_FALSE (f->flags & SVM_FIFO_F_SHRINK))
642     svm_fifo_try_shrink (f, head, tail);
643   return f_free_count (f, head, tail);
644 }
645
646 /**
647  * Max contiguous chunk of data that can be read
648  */
649 static inline u32
650 svm_fifo_max_read_chunk (svm_fifo_t * f)
651 {
652   u32 head, tail;
653   f_load_head_tail_cons (f, &head, &tail);
654   return tail >= head ? (tail - head) : (f->size - head);
655 }
656
657 /**
658  * Max contiguous chunk of data that can be written
659  */
660 static inline u32
661 svm_fifo_max_write_chunk (svm_fifo_t * f)
662 {
663   u32 head, tail;
664   f_load_head_tail_prod (f, &head, &tail);
665   return tail >= head ? f->size - tail : f_free_count (f, head, tail);
666 }
667
668 static inline u8 *
669 svm_fifo_head (svm_fifo_t * f)
670 {
671   /* load-relaxed: consumer owned index */
672   return (f->head_chunk->data + (f->head - f->head_chunk->start_byte));
673 }
674
675 static inline u8 *
676 svm_fifo_tail (svm_fifo_t * f)
677 {
678   /* load-relaxed: producer owned index */
679   return (f->tail_chunk->data + (f->tail - f->tail_chunk->start_byte));
680 }
681
682 static inline u8
683 svm_fifo_n_subscribers (svm_fifo_t * f)
684 {
685   return f->n_subscribers;
686 }
687
688 /**
689  * Check if fifo has out-of-order data
690  *
691  * @param f     fifo
692  * @return      1 if fifo has ooo data, 0 otherwise
693  */
694 static inline u8
695 svm_fifo_has_ooo_data (svm_fifo_t * f)
696 {
697   return f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX;
698 }
699
700 static inline ooo_segment_t *
701 svm_fifo_newest_ooo_segment (svm_fifo_t * f)
702 {
703   if (f->ooos_newest == OOO_SEGMENT_INVALID_INDEX)
704     return 0;
705   return pool_elt_at_index (f->ooo_segments, f->ooos_newest);
706 }
707
708 static inline void
709 svm_fifo_newest_ooo_segment_reset (svm_fifo_t * f)
710 {
711   f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
712 }
713
714 static inline u32
715 ooo_segment_offset_prod (svm_fifo_t * f, ooo_segment_t * s)
716 {
717   u32 tail;
718   /* load-relaxed: producer owned index */
719   tail = f->tail;
720
721   return f_distance_to (f, s->start, tail);
722 }
723
724 static inline u32
725 ooo_segment_length (svm_fifo_t * f, ooo_segment_t * s)
726 {
727   return s->length;
728 }
729
730 /**
731  * Check if fifo has io event
732  *
733  * @param f     fifo
734  * @return      1 if fifo has event, 0 otherwise
735  */
736 static inline int
737 svm_fifo_has_event (svm_fifo_t * f)
738 {
739   return f->has_event;
740 }
741
742 /**
743  * Set fifo event flag.
744  *
745  * Forces release semantics.
746  *
747  * @param f     fifo
748  * @return      1 if flag was not set, 0 otherwise
749  */
750 always_inline u8
751 svm_fifo_set_event (svm_fifo_t * f)
752 {
753   return !clib_atomic_swap_rel_n (&f->has_event, 1);
754 }
755
756 /**
757  * Unset fifo event flag.
758  *
759  * Forces acquire semantics
760  *
761  * @param f     fifo
762  */
763 always_inline void
764 svm_fifo_unset_event (svm_fifo_t * f)
765 {
766   clib_atomic_swap_acq_n (&f->has_event, 0);
767 }
768
769 /**
770  * Set specific want notification flag
771  *
772  * For list of flags see @ref svm_fifo_deq_ntf_t
773  *
774  * @param f             fifo
775  * @param ntf_type      type of notification requested
776  */
777 static inline void
778 svm_fifo_add_want_deq_ntf (svm_fifo_t * f, u8 ntf_type)
779 {
780   f->want_deq_ntf |= ntf_type;
781 }
782
783 /**
784  * Clear specific want notification flag
785  *
786  * For list of flags see @ref svm_fifo_ntf_t
787  *
788  * @param f             fifo
789  * @param ntf_type      type of notification to be cleared
790  */
791 static inline void
792 svm_fifo_del_want_deq_ntf (svm_fifo_t * f, u8 ntf_type)
793 {
794   f->want_deq_ntf &= ~ntf_type;
795 }
796
797 /**
798  * Clear the want notification flag and set has notification
799  *
800  * Should be used after enqueuing an event. This clears the
801  * SVM_FIFO_WANT_NOTIF flag but it does not clear
802  * SVM_FIFO_WANT_NOTIF_IF_FULL. If the latter was set, has_ntf is
803  * set to avoid enqueueing events for for all dequeue operations until
804  * it is manually cleared.
805  *
806  * @param f     fifo
807  */
808 static inline void
809 svm_fifo_clear_deq_ntf (svm_fifo_t * f)
810 {
811   /* Set the flag if want_notif_if_full was the only ntf requested */
812   f->has_deq_ntf = f->want_deq_ntf == SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL;
813   svm_fifo_del_want_deq_ntf (f, SVM_FIFO_WANT_DEQ_NOTIF);
814 }
815
816 /**
817  * Clear has notification flag
818  *
819  * The fifo generates only one event per SVM_FIFO_WANT_NOTIF_IF_FULL
820  * request and sets has_ntf. To received new events the flag must be
821  * cleared using this function.
822  *
823  * @param f     fifo
824  */
825 static inline void
826 svm_fifo_reset_has_deq_ntf (svm_fifo_t * f)
827 {
828   f->has_deq_ntf = 0;
829 }
830
831 /**
832  * Check if fifo needs dequeue notification
833  *
834  * Determines based on notification request flags and state of the fifo if
835  * an event should be generated.
836  *
837  * @param f             fifo
838  * @param n_last_deq    number of bytes last dequeued
839  * @return              1 if event should be generated, 0 otherwise
840  */
841 static inline u8
842 svm_fifo_needs_deq_ntf (svm_fifo_t * f, u32 n_last_deq)
843 {
844   u8 want_ntf = f->want_deq_ntf;
845
846   if (PREDICT_TRUE (want_ntf == SVM_FIFO_NO_DEQ_NOTIF))
847     return 0;
848   else if (want_ntf & SVM_FIFO_WANT_DEQ_NOTIF)
849     return 1;
850   if (want_ntf & SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL)
851     {
852       u32 max_deq = svm_fifo_max_dequeue_cons (f);
853       u32 nitems = f->nitems;
854       if (!f->has_deq_ntf && max_deq < nitems
855           && max_deq + n_last_deq >= nitems)
856         return 1;
857     }
858   if (want_ntf & SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY)
859     {
860       if (!f->has_deq_ntf && svm_fifo_is_empty (f))
861         return 1;
862     }
863   return 0;
864 }
865
866 #endif /* __included_ssvm_fifo_h__ */
867
868 /*
869  * fd.io coding-style-patch-verification: ON
870  *
871  * Local Variables:
872  * eval: (c-set-style "gnu")
873  * End:
874  */