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