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