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