blob: b5b26ac479b369db50533184fc3b0d1788013440 [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
Florin Corasc5df8c72019-04-08 07:42:30 -07002 * Copyright (c) 2016-2019 Cisco and/or its affiliates.
Sirshak Das28aa5392019-02-05 01:33:33 -06003 * 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).
Dave Barach68b0fb02017-02-28 15:15:56 -05007 * 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>
Dave Barach68b0fb02017-02-28 15:15:56 -050024#include <vppinfra/pool.h>
25#include <vppinfra/format.h>
Florin Coras4375fa32019-04-19 15:56:00 -070026#include <vppinfra/rbtree.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050027
Dave Barach68b0fb02017-02-28 15:15:56 -050028/** Out-of-order segment */
29typedef struct
30{
31 u32 next; /**< Next linked-list element pool index */
32 u32 prev; /**< Previous linked-list element pool index */
Dave Barach1f75cfd2017-04-14 16:46:44 -040033 u32 start; /**< Start of segment, normalized*/
Florin Corasa5464812017-04-19 13:00:05 -070034 u32 length; /**< Length of segment */
Dave Barach68b0fb02017-02-28 15:15:56 -050035} ooo_segment_t;
36
Florin Coras326b81e2018-10-04 19:03:05 -070037#define SVM_FIFO_TRACE (0)
38#define OOO_SEGMENT_INVALID_INDEX ((u32)~0)
39#define SVM_FIFO_INVALID_SESSION_INDEX ((u32)~0)
Florin Corasfa76a762018-11-29 12:40:10 -080040#define SVM_FIFO_INVALID_INDEX ((u32)~0)
Florin Coras4375fa32019-04-19 15:56:00 -070041#define SVM_FIFO_MAX_EVT_SUBSCRIBERS 7
Dave Barach68b0fb02017-02-28 15:15:56 -050042
Florin Coras87b15ce2019-04-28 21:16:30 -070043typedef enum svm_fifo_tx_ntf_
Florin Coras1bcad5c2019-01-09 20:04:38 -080044{
45 SVM_FIFO_NO_TX_NOTIF = 0,
46 SVM_FIFO_WANT_TX_NOTIF = 1,
47 SVM_FIFO_WANT_TX_NOTIF_IF_FULL = 2,
Florin Coras87b15ce2019-04-28 21:16:30 -070048} svm_fifo_tx_ntf_t;
Florin Coras1bcad5c2019-01-09 20:04:38 -080049
Florin Coras3eb50622017-07-13 01:24:57 -040050typedef struct
51{
52 u32 offset;
53 u32 len;
54 u32 action;
55} svm_fifo_trace_elem_t;
56
Florin Coras0a846802019-04-09 18:29:14 -070057typedef struct svm_fifo_chunk_
58{
Florin Coras87b15ce2019-04-28 21:16:30 -070059 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 */
Florin Coras0a846802019-04-09 18:29:14 -070063} svm_fifo_chunk_t;
64
Florin Coras2309e7a2019-04-18 18:58:10 -070065typedef enum svm_fifo_flag_
66{
Florin Corasa7570b02019-05-02 12:52:19 -070067 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,
Florin Coras2309e7a2019-04-18 18:58:10 -070072} svm_fifo_flag_t;
73
Dave Barach10d8cc62017-05-30 09:30:07 -040074typedef struct _svm_fifo
Dave Barach68b0fb02017-02-28 15:15:56 -050075{
Florin Coras72b04282019-01-14 17:23:11 -080076 CLIB_CACHE_LINE_ALIGN_MARK (shared_first);
Florin Coras87b15ce2019-04-28 21:16:30 -070077 u32 size; /**< size of the fifo in bytes */
78 u32 nitems; /**< usable size (size-1) */
Florin Coras2309e7a2019-04-18 18:58:10 -070079 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 */
Florin Coras4375fa32019-04-19 15:56:00 -070083 rb_tree_t chunk_lookup;
Florin Coras3e350af2017-03-30 02:54:28 -070084
Florin Coras72b04282019-01-14 17:23:11 -080085 CLIB_CACHE_LINE_ALIGN_MARK (shared_second);
Dave Barachacd2a6a2017-05-16 17:41:34 -040086 volatile u32 has_event; /**< non-zero if deq event exists */
Florin Coras87b15ce2019-04-28 21:16:30 -070087 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 */
Florin Corasa7570b02019-05-02 12:52:19 -070091 i8 refcnt; /**< reference count */
Florin Coras87b15ce2019-04-28 21:16:30 -070092 u32 segment_manager; /**< session layer segment manager index */
93 u32 segment_index; /**< segment index in segment manager */
Florin Coras2309e7a2019-04-18 18:58:10 -070094 struct _svm_fifo *next; /**< next in freelist/active chain */
95 struct _svm_fifo *prev; /**< prev in active chain */
Florin Corasa7570b02019-05-02 12:52:19 -070096 u32 size_decrement; /**< bytes to remove from fifo */
Florin Coras72b04282019-01-14 17:23:11 -080097
98 CLIB_CACHE_LINE_ALIGN_MARK (consumer);
Florin Coras4375fa32019-04-19 15:56:00 -070099 u32 head; /**< fifo head position/byte */
Florin Coras2309e7a2019-04-18 18:58:10 -0700100 svm_fifo_chunk_t *head_chunk; /**< tracks chunk where head lands */
Florin Coras4375fa32019-04-19 15:56:00 -0700101 svm_fifo_chunk_t *ooo_deq; /**< last chunk used for ooo dequeue */
Florin Coras1bcad5c2019-01-09 20:04:38 -0800102 volatile u32 want_tx_ntf; /**< producer wants nudge */
103 volatile u32 has_tx_ntf;
Dave Barach68b0fb02017-02-28 15:15:56 -0500104
Florin Coras72b04282019-01-14 17:23:11 -0800105 CLIB_CACHE_LINE_ALIGN_MARK (producer);
Florin Coras4375fa32019-04-19 15:56:00 -0700106 u32 tail; /**< fifo tail position/byte */
Dave Barach68b0fb02017-02-28 15:15:56 -0500107 u32 ooos_list_head; /**< Head of out-of-order linked-list */
Florin Coras4375fa32019-04-19 15:56:00 -0700108 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 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500111 u32 ooos_newest; /**< Last segment to have been updated */
Florin Coras87b15ce2019-04-28 21:16:30 -0700112 volatile u8 n_subscribers; /**< Number of subscribers for io events */
Florin Coras72b04282019-01-14 17:23:11 -0800113 u8 subscribers[SVM_FIFO_MAX_EVT_SUBSCRIBERS];
114
Florin Coras3eb50622017-07-13 01:24:57 -0400115#if SVM_FIFO_TRACE
116 svm_fifo_trace_elem_t *trace;
117#endif
Florin Coras72b04282019-01-14 17:23:11 -0800118
Dave Barach68b0fb02017-02-28 15:15:56 -0500119} svm_fifo_t;
120
Florin Coras7fb0fe12018-04-09 09:24:52 -0700121typedef enum
122{
Florin Coras87b15ce2019-04-28 21:16:30 -0700123 SVM_FIFO_EFULL = -2,
124 SVM_FIFO_EEMPTY = -3,
Florin Coras7fb0fe12018-04-09 09:24:52 -0700125} svm_fifo_err_t;
126
Florin Coras88001c62019-04-24 14:44:46 -0700127typedef struct svm_fifo_seg_
Florin Coras2cba8532018-09-11 16:33:36 -0700128{
129 u8 *data;
130 u32 len;
Florin Coras88001c62019-04-24 14:44:46 -0700131} svm_fifo_seg_t;
Florin Coras2cba8532018-09-11 16:33:36 -0700132
Florin Coras3eb50622017-07-13 01:24:57 -0400133#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
146u8 *svm_fifo_dump_trace (u8 * s, svm_fifo_t * f);
147u8 *svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose);
148
Florin Coras87b15ce2019-04-28 21:16:30 -0700149/**
150 * Load head and tail optimized for consumer
151 *
152 * Internal function.
153 */
Sirshak Das28aa5392019-02-05 01:33:33 -0600154static inline void
155f_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
Florin Coras87b15ce2019-04-28 21:16:30 -0700163/** Load head and tail optimized for producer
164 *
165 * Internal function
166 */
Sirshak Das28aa5392019-02-05 01:33:33 -0600167static inline void
168f_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
Florin Corasa7570b02019-05-02 12:52:19 -0700176/**
177 * Load head and tail independent of producer/consumer role
Florin Coras87b15ce2019-04-28 21:16:30 -0700178 *
179 * Internal function.
180 */
Sirshak Das28aa5392019-02-05 01:33:33 -0600181static inline void
182f_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
Florin Coras87b15ce2019-04-28 21:16:30 -0700190/**
Florin Coras87b15ce2019-04-28 21:16:30 -0700191 * Distance to a from b, i.e., a - b in the fifo
192 *
193 * Internal function.
Sirshak Das28aa5392019-02-05 01:33:33 -0600194 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500195static inline u32
Florin Coras87b15ce2019-04-28 21:16:30 -0700196f_distance_to (svm_fifo_t * f, u32 a, u32 b)
Dave Barach68b0fb02017-02-28 15:15:56 -0500197{
Florin Coras87b15ce2019-04-28 21:16:30 -0700198 return ((f->size + a - b) % f->size);
Dave Barach68b0fb02017-02-28 15:15:56 -0500199}
200
Florin Coras6792ec02017-03-13 03:49:51 -0700201/**
Florin Coras87b15ce2019-04-28 21:16:30 -0700202 * Distance from a to b, i.e., b - a in the fifo
Florin Coras6792ec02017-03-13 03:49:51 -0700203 *
Florin Coras87b15ce2019-04-28 21:16:30 -0700204 * Internal function.
Florin Coras6792ec02017-03-13 03:49:51 -0700205 */
Florin Coras87b15ce2019-04-28 21:16:30 -0700206static inline u32
207f_distance_from (svm_fifo_t * f, u32 a, u32 b)
Florin Coras6792ec02017-03-13 03:49:51 -0700208{
Florin Coras87b15ce2019-04-28 21:16:30 -0700209 return ((f->size + b - a) % f->size);
Florin Coras6792ec02017-03-13 03:49:51 -0700210}
211
212/**
Florin Coras29a59c32019-05-02 12:52:19 -0700213 * Fifo current size, i.e., number of bytes enqueued
214 *
215 * Internal function.
216 */
217static inline u32
218f_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 */
228static inline u32
229f_free_count (svm_fifo_t * f, u32 head, u32 tail)
230{
231 return (f->nitems - f_cursize (f, head, tail));
232}
233
234/**
Florin Corasa7570b02019-05-02 12:52:19 -0700235 * Try to shrink fifo size.
236 *
237 * Internal function.
238 */
239void svm_fifo_try_shrink (svm_fifo_t * f, u32 head, u32 tail);
240
241/**
Florin Coras87b15ce2019-04-28 21:16:30 -0700242 * Create fifo of requested size
Florin Coras41c9e042018-09-11 00:10:41 -0700243 *
Florin Coras87b15ce2019-04-28 21:16:30 -0700244 * 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
Florin Coras6792ec02017-03-13 03:49:51 -0700249 */
Florin Coras87b15ce2019-04-28 21:16:30 -0700250svm_fifo_t *svm_fifo_create (u32 size);
251/**
252 * Initialize fifo
253 *
254 * @param size size for fifo
255 */
Florin Coras0a846802019-04-09 18:29:14 -0700256void svm_fifo_init (svm_fifo_t * f, u32 size);
Florin Corasb095a3c2019-04-25 12:58:46 -0700257/**
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 */
267svm_fifo_chunk_t *svm_fifo_chunk_alloc (u32 size);
Florin Coras4375fa32019-04-19 15:56:00 -0700268/**
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 */
Florin Coras99ad2fc2019-04-18 21:25:49 -0700277void svm_fifo_add_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c);
Florin Coras87b15ce2019-04-28 21:16:30 -0700278/**
Florin Corasa7570b02019-05-02 12:52:19 -0700279 * 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 */
294int 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 */
302svm_fifo_chunk_t *svm_fifo_collect_chunks (svm_fifo_t * f);
303/**
Florin Coras87b15ce2019-04-28 21:16:30 -0700304 * Free fifo and associated state
305 *
306 * @param f fifo
307 */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700308void svm_fifo_free (svm_fifo_t * f);
Florin Corasb095a3c2019-04-25 12:58:46 -0700309/**
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 */
317void 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 */
326void svm_fifo_free_ooo_data (svm_fifo_t * f);
Florin Coras87b15ce2019-04-28 21:16:30 -0700327/**
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 */
334void 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 */
341void 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 */
354int 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 */
368int svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 len,
369 u8 * src);
Florin Corasa7570b02019-05-02 12:52:19 -0700370
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 */
379void svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 len);
Florin Coras87b15ce2019-04-28 21:16:30 -0700380/**
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 */
391void 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 */
403int 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 */
416int 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 */
426int 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 */
Florin Coras25579b42018-06-06 17:55:02 -0700434void svm_fifo_dequeue_drop_all (svm_fifo_t * f);
Florin Coras88001c62019-04-24 14:44:46 -0700435int svm_fifo_segments (svm_fifo_t * f, svm_fifo_seg_t * fs);
436void svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_seg_t * fs);
Florin Coras87b15ce2019-04-28 21:16:30 -0700437/**
438 * Add io events subscriber to list
439 *
440 * @param f fifo
441 * @param sub subscriber opaque index (typically app worker index)
442 */
443void 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 */
Florin Coras72b04282019-01-14 17:23:11 -0800450void svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber);
Florin Coras87b15ce2019-04-28 21:16:30 -0700451/**
452 * Number of out-of-order segments for fifo
453 *
454 * @param f fifo
455 * @return number of out of order segments
456 */
457u32 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 */
464ooo_segment_t *svm_fifo_first_ooo_segment (svm_fifo_t * f);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700465format_function_t format_svm_fifo;
466
Florin Coras58d36f02018-03-09 13:05:53 -0800467/**
Florin Coras87b15ce2019-04-28 21:16:30 -0700468 * Fifo max bytes to dequeue optimized for consumer
469 *
470 * @param f fifo
471 * @return max number of bytes that can be dequeued
472 */
473static inline u32
474svm_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 */
487static inline u32
488svm_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 */
502static inline u32
503svm_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 */
516static inline int
517svm_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 */
528static inline int
529svm_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 */
540static inline int
541svm_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 */
552static inline int
553svm_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 */
565static inline int
566svm_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 */
577static inline u8
578svm_fifo_is_wrapped (svm_fifo_t * f)
579{
580 u32 head, tail;
581 f_load_head_tail_all_acq (f, &head, &tail);
Florin Coras29a59c32019-05-02 12:52:19 -0700582 return head > tail;
Florin Coras87b15ce2019-04-28 21:16:30 -0700583}
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 */
593static inline u32
594svm_fifo_max_enqueue_prod (svm_fifo_t * f)
595{
596 u32 head, tail;
597 f_load_head_tail_prod (f, &head, &tail);
Florin Corasa7570b02019-05-02 12:52:19 -0700598 if (PREDICT_FALSE (f->flags & SVM_FIFO_F_SHRINK))
599 svm_fifo_try_shrink (f, head, tail);
Florin Coras87b15ce2019-04-28 21:16:30 -0700600 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 */
609static inline u32
610svm_fifo_max_enqueue (svm_fifo_t * f)
611{
612 u32 head, tail;
613 f_load_head_tail_all_acq (f, &head, &tail);
Florin Corasa7570b02019-05-02 12:52:19 -0700614 if (PREDICT_FALSE (f->flags & SVM_FIFO_F_SHRINK))
615 svm_fifo_try_shrink (f, head, tail);
Florin Coras87b15ce2019-04-28 21:16:30 -0700616 return f_free_count (f, head, tail);
617}
618
619/**
Florin Coras58d36f02018-03-09 13:05:53 -0800620 * Max contiguous chunk of data that can be read
621 */
Florin Coras87b15ce2019-04-28 21:16:30 -0700622static inline u32
Florin Coras58d36f02018-03-09 13:05:53 -0800623svm_fifo_max_read_chunk (svm_fifo_t * f)
624{
Sirshak Das28aa5392019-02-05 01:33:33 -0600625 u32 head, tail;
Sirshak Das28aa5392019-02-05 01:33:33 -0600626 f_load_head_tail_cons (f, &head, &tail);
Florin Coras29a59c32019-05-02 12:52:19 -0700627 return tail >= head ? (tail - head) : (f->size - head);
Florin Coras58d36f02018-03-09 13:05:53 -0800628}
629
630/**
631 * Max contiguous chunk of data that can be written
632 */
Florin Coras87b15ce2019-04-28 21:16:30 -0700633static inline u32
Florin Coras58d36f02018-03-09 13:05:53 -0800634svm_fifo_max_write_chunk (svm_fifo_t * f)
635{
Sirshak Das28aa5392019-02-05 01:33:33 -0600636 u32 head, tail;
Sirshak Das28aa5392019-02-05 01:33:33 -0600637 f_load_head_tail_prod (f, &head, &tail);
Florin Coras29a59c32019-05-02 12:52:19 -0700638 return tail > head ? f->size - tail : f_free_count (f, head, tail);
Florin Coras58d36f02018-03-09 13:05:53 -0800639}
640
Florin Coras87b15ce2019-04-28 21:16:30 -0700641static inline u8 *
Florin Coras58d36f02018-03-09 13:05:53 -0800642svm_fifo_head (svm_fifo_t * f)
643{
Sirshak Das28aa5392019-02-05 01:33:33 -0600644 /* load-relaxed: consumer owned index */
Florin Coras29a59c32019-05-02 12:52:19 -0700645 return (f->head_chunk->data + (f->head - f->head_chunk->start_byte));
Florin Coras58d36f02018-03-09 13:05:53 -0800646}
647
Florin Coras87b15ce2019-04-28 21:16:30 -0700648static inline u8 *
Florin Coras58d36f02018-03-09 13:05:53 -0800649svm_fifo_tail (svm_fifo_t * f)
650{
Sirshak Das28aa5392019-02-05 01:33:33 -0600651 /* load-relaxed: producer owned index */
Florin Coras29a59c32019-05-02 12:52:19 -0700652 return (f->tail_chunk->data + (f->tail - f->tail_chunk->start_byte));
Florin Coras1bcad5c2019-01-09 20:04:38 -0800653}
654
Florin Coras87b15ce2019-04-28 21:16:30 -0700655static inline u8
656svm_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 */
667static inline u8
668svm_fifo_has_ooo_data (svm_fifo_t * f)
669{
670 return f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX;
671}
672
673static inline ooo_segment_t *
674svm_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
681static inline void
682svm_fifo_newest_ooo_segment_reset (svm_fifo_t * f)
683{
684 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
685}
686
687static inline u32
688ooo_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
697static inline u32
698ooo_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 */
709static inline int
710svm_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 */
723always_inline u8
724svm_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 */
736always_inline void
737svm_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 */
Florin Coras1bcad5c2019-01-09 20:04:38 -0800750static inline void
751svm_fifo_add_want_tx_ntf (svm_fifo_t * f, u8 ntf_type)
752{
753 f->want_tx_ntf |= ntf_type;
754}
755
Florin Coras87b15ce2019-04-28 21:16:30 -0700756/**
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 */
Florin Coras1bcad5c2019-01-09 20:04:38 -0800764static inline void
765svm_fifo_del_want_tx_ntf (svm_fifo_t * f, u8 ntf_type)
766{
767 f->want_tx_ntf &= ~ntf_type;
768}
769
Florin Coras87b15ce2019-04-28 21:16:30 -0700770/**
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 */
Florin Coras1bcad5c2019-01-09 20:04:38 -0800781static inline void
782svm_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
Florin Coras87b15ce2019-04-28 21:16:30 -0700789/**
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 */
Florin Coras1bcad5c2019-01-09 20:04:38 -0800798static inline void
799svm_fifo_reset_tx_ntf (svm_fifo_t * f)
800{
801 f->has_tx_ntf = 0;
802}
803
Florin Coras87b15ce2019-04-28 21:16:30 -0700804/**
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 */
Florin Coras1bcad5c2019-01-09 20:04:38 -0800814static inline u8
815svm_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 {
Sirshak Das28aa5392019-02-05 01:33:33 -0600825 u32 max_deq = svm_fifo_max_dequeue_cons (f);
826 u32 nitems = f->nitems;
Florin Coras1bcad5c2019-01-09 20:04:38 -0800827 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
Dave Barach68b0fb02017-02-28 15:15:56 -0500836#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 */