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