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