blob: ce4c53d9abebed2cde69cb057daa5958351b6b53 [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 Coras2d379d82019-06-28 12:45:12 -070043typedef enum svm_fifo_deq_ntf_
Florin Coras1bcad5c2019-01-09 20:04:38 -080044{
Florin Coras2d379d82019-06-28 12:45:12 -070045 SVM_FIFO_NO_DEQ_NOTIF = 0, /**< No notification requested */
46 SVM_FIFO_WANT_DEQ_NOTIF = 1, /**< Notify on dequeue */
47 SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL = 2, /**< Notify on transition from full */
48 SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY = 4, /**< Notify on transition to empty */
49} svm_fifo_deq_ntf_t;
Florin Coras1bcad5c2019-01-09 20:04:38 -080050
Florin Coras3eb50622017-07-13 01:24:57 -040051typedef struct
52{
53 u32 offset;
54 u32 len;
55 u32 action;
56} svm_fifo_trace_elem_t;
57
Florin Coras0a846802019-04-09 18:29:14 -070058typedef struct svm_fifo_chunk_
59{
Florin Coras87b15ce2019-04-28 21:16:30 -070060 u32 start_byte; /**< chunk start byte */
61 u32 length; /**< length of chunk in bytes */
62 struct svm_fifo_chunk_ *next; /**< pointer to next chunk in linked-lists */
63 u8 data[0]; /**< start of chunk data */
Florin Coras0a846802019-04-09 18:29:14 -070064} svm_fifo_chunk_t;
65
Florin Coras2309e7a2019-04-18 18:58:10 -070066typedef enum svm_fifo_flag_
67{
Florin Corasa7570b02019-05-02 12:52:19 -070068 SVM_FIFO_F_MULTI_CHUNK = 1 << 0,
69 SVM_FIFO_F_GROW = 1 << 1,
70 SVM_FIFO_F_SHRINK = 1 << 2,
71 SVM_FIFO_F_COLLECT_CHUNKS = 1 << 3,
72 SVM_FIFO_F_LL_TRACKED = 1 << 4,
Ryujiro Shibuya8e20fe72019-10-16 06:30:02 +010073 SVM_FIFO_F_SINGLE_THREAD_OWNED = 1 << 5,
Florin Coras2309e7a2019-04-18 18:58:10 -070074} svm_fifo_flag_t;
75
Dave Barach10d8cc62017-05-30 09:30:07 -040076typedef struct _svm_fifo
Dave Barach68b0fb02017-02-28 15:15:56 -050077{
Florin Coras72b04282019-01-14 17:23:11 -080078 CLIB_CACHE_LINE_ALIGN_MARK (shared_first);
Florin Coras87b15ce2019-04-28 21:16:30 -070079 u32 size; /**< size of the fifo in bytes */
80 u32 nitems; /**< usable size (size-1) */
Florin Coras2309e7a2019-04-18 18:58:10 -070081 u8 flags; /**< fifo flags */
82 svm_fifo_chunk_t *start_chunk;/**< first chunk in fifo chunk list */
83 svm_fifo_chunk_t *end_chunk; /**< end chunk in fifo chunk list */
84 svm_fifo_chunk_t *new_chunks; /**< chunks yet to be added to list */
Florin Coras4375fa32019-04-19 15:56:00 -070085 rb_tree_t chunk_lookup;
Florin Coras3e350af2017-03-30 02:54:28 -070086
Florin Coras72b04282019-01-14 17:23:11 -080087 CLIB_CACHE_LINE_ALIGN_MARK (shared_second);
Dave Barachacd2a6a2017-05-16 17:41:34 -040088 volatile u32 has_event; /**< non-zero if deq event exists */
Florin Coras87b15ce2019-04-28 21:16:30 -070089 u32 master_session_index; /**< session layer session index */
90 u32 client_session_index; /**< app session index */
91 u8 master_thread_index; /**< session layer thread index */
92 u8 client_thread_index; /**< app worker index */
Florin Corasa7570b02019-05-02 12:52:19 -070093 i8 refcnt; /**< reference count */
Florin Coras87b15ce2019-04-28 21:16:30 -070094 u32 segment_manager; /**< session layer segment manager index */
95 u32 segment_index; /**< segment index in segment manager */
Florin Coras2309e7a2019-04-18 18:58:10 -070096 struct _svm_fifo *next; /**< next in freelist/active chain */
97 struct _svm_fifo *prev; /**< prev in active chain */
Florin Corasa7570b02019-05-02 12:52:19 -070098 u32 size_decrement; /**< bytes to remove from fifo */
Florin Coras72b04282019-01-14 17:23:11 -080099
100 CLIB_CACHE_LINE_ALIGN_MARK (consumer);
Florin Coras4375fa32019-04-19 15:56:00 -0700101 u32 head; /**< fifo head position/byte */
Florin Coras2309e7a2019-04-18 18:58:10 -0700102 svm_fifo_chunk_t *head_chunk; /**< tracks chunk where head lands */
Florin Coras4375fa32019-04-19 15:56:00 -0700103 svm_fifo_chunk_t *ooo_deq; /**< last chunk used for ooo dequeue */
Florin Coras2d379d82019-06-28 12:45:12 -0700104 volatile u32 want_deq_ntf; /**< producer wants nudge */
105 volatile u32 has_deq_ntf;
Dave Barach68b0fb02017-02-28 15:15:56 -0500106
Florin Coras72b04282019-01-14 17:23:11 -0800107 CLIB_CACHE_LINE_ALIGN_MARK (producer);
Florin Coras4375fa32019-04-19 15:56:00 -0700108 u32 tail; /**< fifo tail position/byte */
Dave Barach68b0fb02017-02-28 15:15:56 -0500109 u32 ooos_list_head; /**< Head of out-of-order linked-list */
Florin Coras4375fa32019-04-19 15:56:00 -0700110 svm_fifo_chunk_t *tail_chunk; /**< tracks chunk where tail lands */
111 svm_fifo_chunk_t *ooo_enq; /**< last chunk used for ooo enqueue */
112 ooo_segment_t *ooo_segments; /**< Pool of ooo segments */
Dave Barach68b0fb02017-02-28 15:15:56 -0500113 u32 ooos_newest; /**< Last segment to have been updated */
Florin Coras87b15ce2019-04-28 21:16:30 -0700114 volatile u8 n_subscribers; /**< Number of subscribers for io events */
Florin Coras72b04282019-01-14 17:23:11 -0800115 u8 subscribers[SVM_FIFO_MAX_EVT_SUBSCRIBERS];
116
Florin Coras3eb50622017-07-13 01:24:57 -0400117#if SVM_FIFO_TRACE
118 svm_fifo_trace_elem_t *trace;
119#endif
Florin Coras72b04282019-01-14 17:23:11 -0800120
Dave Barach68b0fb02017-02-28 15:15:56 -0500121} svm_fifo_t;
122
Florin Coras7fb0fe12018-04-09 09:24:52 -0700123typedef enum
124{
Florin Coras87b15ce2019-04-28 21:16:30 -0700125 SVM_FIFO_EFULL = -2,
126 SVM_FIFO_EEMPTY = -3,
Florin Coras7fb0fe12018-04-09 09:24:52 -0700127} svm_fifo_err_t;
128
Florin Coras88001c62019-04-24 14:44:46 -0700129typedef struct svm_fifo_seg_
Florin Coras2cba8532018-09-11 16:33:36 -0700130{
131 u8 *data;
132 u32 len;
Florin Coras88001c62019-04-24 14:44:46 -0700133} svm_fifo_seg_t;
Florin Coras2cba8532018-09-11 16:33:36 -0700134
Florin Coras3eb50622017-07-13 01:24:57 -0400135#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
148u8 *svm_fifo_dump_trace (u8 * s, svm_fifo_t * f);
149u8 *svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose);
150
Florin Coras87b15ce2019-04-28 21:16:30 -0700151/**
152 * Load head and tail optimized for consumer
153 *
154 * Internal function.
155 */
Sirshak Das28aa5392019-02-05 01:33:33 -0600156static inline void
157f_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
Florin Coras87b15ce2019-04-28 21:16:30 -0700165/** Load head and tail optimized for producer
166 *
167 * Internal function
168 */
Sirshak Das28aa5392019-02-05 01:33:33 -0600169static inline void
170f_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
Florin Corasa7570b02019-05-02 12:52:19 -0700178/**
179 * Load head and tail independent of producer/consumer role
Florin Coras87b15ce2019-04-28 21:16:30 -0700180 *
181 * Internal function.
182 */
Sirshak Das28aa5392019-02-05 01:33:33 -0600183static inline void
184f_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
Florin Coras87b15ce2019-04-28 21:16:30 -0700192/**
Florin Coras87b15ce2019-04-28 21:16:30 -0700193 * Distance to a from b, i.e., a - b in the fifo
194 *
195 * Internal function.
Sirshak Das28aa5392019-02-05 01:33:33 -0600196 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500197static inline u32
Florin Coras87b15ce2019-04-28 21:16:30 -0700198f_distance_to (svm_fifo_t * f, u32 a, u32 b)
Dave Barach68b0fb02017-02-28 15:15:56 -0500199{
Florin Coras87b15ce2019-04-28 21:16:30 -0700200 return ((f->size + a - b) % f->size);
Dave Barach68b0fb02017-02-28 15:15:56 -0500201}
202
Florin Coras6792ec02017-03-13 03:49:51 -0700203/**
Florin Coras87b15ce2019-04-28 21:16:30 -0700204 * Distance from a to b, i.e., b - a in the fifo
Florin Coras6792ec02017-03-13 03:49:51 -0700205 *
Florin Coras87b15ce2019-04-28 21:16:30 -0700206 * Internal function.
Florin Coras6792ec02017-03-13 03:49:51 -0700207 */
Florin Coras87b15ce2019-04-28 21:16:30 -0700208static inline u32
209f_distance_from (svm_fifo_t * f, u32 a, u32 b)
Florin Coras6792ec02017-03-13 03:49:51 -0700210{
Florin Coras87b15ce2019-04-28 21:16:30 -0700211 return ((f->size + b - a) % f->size);
Florin Coras6792ec02017-03-13 03:49:51 -0700212}
213
214/**
Florin Coras29a59c32019-05-02 12:52:19 -0700215 * Fifo current size, i.e., number of bytes enqueued
216 *
217 * Internal function.
218 */
219static inline u32
220f_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 */
230static inline u32
231f_free_count (svm_fifo_t * f, u32 head, u32 tail)
232{
233 return (f->nitems - f_cursize (f, head, tail));
234}
235
236/**
Florin Corasa7570b02019-05-02 12:52:19 -0700237 * Try to shrink fifo size.
238 *
239 * Internal function.
240 */
241void svm_fifo_try_shrink (svm_fifo_t * f, u32 head, u32 tail);
242
243/**
Florin Coras87b15ce2019-04-28 21:16:30 -0700244 * Create fifo of requested size
Florin Coras41c9e042018-09-11 00:10:41 -0700245 *
Florin Coras87b15ce2019-04-28 21:16:30 -0700246 * 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
Florin Coras6792ec02017-03-13 03:49:51 -0700251 */
Florin Coras87b15ce2019-04-28 21:16:30 -0700252svm_fifo_t *svm_fifo_create (u32 size);
253/**
254 * Initialize fifo
255 *
Florin Coraseaacce42019-07-02 13:07:37 -0700256 * @param f fifo
Florin Coras87b15ce2019-04-28 21:16:30 -0700257 * @param size size for fifo
258 */
Florin Coras0a846802019-04-09 18:29:14 -0700259void svm_fifo_init (svm_fifo_t * f, u32 size);
Florin Corasb095a3c2019-04-25 12:58:46 -0700260/**
Florin Coraseaacce42019-07-02 13:07:37 -0700261 * Initialize fifo chunks and rbtree
262 *
263 * @param f fifo
264 */
265void svm_fifo_init_chunks (svm_fifo_t * f);
266/**
Florin Corasb095a3c2019-04-25 12:58:46 -0700267 * Allocate a fifo chunk on heap
268 *
269 * If the chunk is allocated on a fifo segment, this should be called
270 * with the segment's heap pushed.
271 *
272 * @param size chunk size in bytes. Will be rounded to the next highest
273 * power-of-two
274 * @return new chunk or 0 if alloc failed
275 */
276svm_fifo_chunk_t *svm_fifo_chunk_alloc (u32 size);
Florin Coras4375fa32019-04-19 15:56:00 -0700277/**
278 * Grow fifo size by adding chunk to chunk list
279 *
280 * If fifos are allocated on a segment, this should be called with
281 * the segment's heap pushed.
282 *
283 * @param f fifo to be extended
284 * @param c chunk or linked list of chunks to be added
285 */
Florin Coras99ad2fc2019-04-18 21:25:49 -0700286void svm_fifo_add_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c);
Florin Coras87b15ce2019-04-28 21:16:30 -0700287/**
Florin Corasa7570b02019-05-02 12:52:19 -0700288 * Request to reduce fifo size by amount of bytes
289 *
290 * Because the producer might be enqueuing data when this is called, the
291 * actual size update is only applied when producer tries to enqueue new
292 * data, unless @param try_shrink is set.
293 *
294 * @param f fifo
295 * @param len number of bytes to remove from fifo. The actual number
296 * of bytes to be removed will be less or equal to this
297 * value.
298 * @param try_shrink flg to indicate if it's safe to try to shrink fifo
299 * size. It should be set only if this is called by the
300 * producer of if the producer is not using the fifo
301 * @return actual length fifo size will be reduced by
302 */
303int svm_fifo_reduce_size (svm_fifo_t * f, u32 len, u8 try_shrink);
304/**
305 * Removes chunks that are after fifo end byte
306 *
307 * Needs to be called with segment heap pushed.
308 *
309 * @param f fifo
310 */
311svm_fifo_chunk_t *svm_fifo_collect_chunks (svm_fifo_t * f);
312/**
Florin Coras87b15ce2019-04-28 21:16:30 -0700313 * Free fifo and associated state
314 *
315 * @param f fifo
316 */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700317void svm_fifo_free (svm_fifo_t * f);
Florin Corasb095a3c2019-04-25 12:58:46 -0700318/**
319 * Cleanup fifo chunk lookup rb tree
320 *
321 * The rb tree is allocated in segment heap so this should be called
322 * with it pushed.
323 *
324 * @param f fifo to cleanup
325 */
326void svm_fifo_free_chunk_lookup (svm_fifo_t * f);
327/**
328 * Cleanup fifo ooo data
329 *
330 * The ooo data is allocated in producer process memory. The fifo
331 * segment heap should not be pushed.
332 *
333 * @param f fifo to cleanup
334 */
335void svm_fifo_free_ooo_data (svm_fifo_t * f);
Florin Coras87b15ce2019-04-28 21:16:30 -0700336/**
337 * Init fifo head and tail
338 *
339 * @param f fifo
340 * @param head head value that will be matched to a chunk
341 * @param tail tail value that will be matched to a chunk
342 */
343void svm_fifo_init_pointers (svm_fifo_t * f, u32 head, u32 tail);
344/**
345 * Clone fifo
346 *
347 * Clones single/default chunk fifo. It does not work for fifos with
348 * multiple chunks.
349 */
350void svm_fifo_clone (svm_fifo_t * df, svm_fifo_t * sf);
351/**
352 * Enqueue data to fifo
353 *
354 * Data is enqueued and tail pointer is updated atomically. If the new data
355 * enqueued partly overlaps or "touches" an out-of-order segment, said segment
356 * is "consumed" and the number of bytes returned is appropriately updated.
357 *
358 * @param f fifo
359 * @param len length of data to copy
360 * @param src buffer from where to copy the data
361 * @return number of contiguous bytes that can be consumed or error
362 */
363int svm_fifo_enqueue (svm_fifo_t * f, u32 len, const u8 * src);
364/**
365 * Enqueue data to fifo with offset
366 *
367 * Data is enqueued without updating tail pointer. Instead, an out-of-order
368 * list of segments is generated and maintained. Fifo takes care of coalescing
369 * contiguous or overlapping segments.
370 *
371 * @param f fifo
372 * @param offset offset at which to copy the data
373 * @param len len of data to copy
374 * @param src buffer from where to copy the data
375 * @return 0 if enqueue was successful, error otherwise
376 */
377int svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 len,
378 u8 * src);
Florin Corasa7570b02019-05-02 12:52:19 -0700379
380/**
381 * Advance tail pointer
382 *
383 * Useful for moving tail pointer after external enqueue.
384 *
385 * @param f fifo
386 * @param len number of bytes to add to tail
387 */
388void svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 len);
Florin Coras87b15ce2019-04-28 21:16:30 -0700389/**
390 * Overwrite fifo head with new data
391 *
392 * This should be typically used by dgram transport protocols that need
393 * to update the dgram header after dequeueing a chunk of data. It assumes
394 * that the dgram header is at most spread over two chunks.
395 *
396 * @param f fifo
397 * @param src src of new data
398 * @param len length of new data
399 */
400void svm_fifo_overwrite_head (svm_fifo_t * f, u8 * src, u32 len);
401/**
402 * Dequeue data from fifo
403 *
404 * Data is dequeued to consumer provided buffer and head is atomically
405 * updated.
406 *
407 * @param f fifo
408 * @param len length of data to dequeue
409 * @param dst buffer to where to dequeue the data
410 * @return number of bytes dequeued or error
411 */
412int svm_fifo_dequeue (svm_fifo_t * f, u32 len, u8 * dst);
413/**
414 * Peek data from fifo
415 *
416 * Data is copied from requested offset into provided dst buffer. Head is
417 * not updated.
418 *
419 * @param f fifo
420 * @param offset offset from which to copy the data
421 * @param len length of data to copy
422 * @param dst buffer to where to dequeue the data
423 * @return number of bytes peeked
424 */
425int svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 len, u8 * dst);
426/**
427 * Dequeue and drop bytes from fifo
428 *
429 * Advances fifo head by requested amount of bytes.
430 *
431 * @param f fifo
432 * @param len number of bytes to drop
433 * @return number of bytes dropped
434 */
435int svm_fifo_dequeue_drop (svm_fifo_t * f, u32 len);
436/**
437 * Dequeue and drop all bytes from fifo
438 *
439 * Advances head to tail position.
440 *
441 * @param f fifo
442 */
Florin Coras25579b42018-06-06 17:55:02 -0700443void svm_fifo_dequeue_drop_all (svm_fifo_t * f);
Florin Coras88001c62019-04-24 14:44:46 -0700444int svm_fifo_segments (svm_fifo_t * f, svm_fifo_seg_t * fs);
445void svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_seg_t * fs);
Florin Coras87b15ce2019-04-28 21:16:30 -0700446/**
447 * Add io events subscriber to list
448 *
449 * @param f fifo
450 * @param sub subscriber opaque index (typically app worker index)
451 */
452void svm_fifo_add_subscriber (svm_fifo_t * f, u8 sub);
453/**
454 * Remove io events subscriber form list
455 *
456 * @param f fifo
457 * @param sub subscriber index to be removed
458 */
Florin Coras72b04282019-01-14 17:23:11 -0800459void svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber);
Florin Coras87b15ce2019-04-28 21:16:30 -0700460/**
461 * Number of out-of-order segments for fifo
462 *
463 * @param f fifo
464 * @return number of out of order segments
465 */
466u32 svm_fifo_n_ooo_segments (svm_fifo_t * f);
Florin Coraseaacce42019-07-02 13:07:37 -0700467/**
Florin Coras87b15ce2019-04-28 21:16:30 -0700468 * First out-of-order segment for fifo
469 *
470 * @param f fifo
471 * @return first out-of-order segment for fifo
472 */
473ooo_segment_t *svm_fifo_first_ooo_segment (svm_fifo_t * f);
Florin Coraseaacce42019-07-02 13:07:37 -0700474/**
475 * Check if fifo is sane. Debug only.
476 *
477 * @param f fifo
478 * @return 1 if sane, 0 otherwise
479 */
480u8 svm_fifo_is_sane (svm_fifo_t * f);
Ryujiro Shibuya8e20fe72019-10-16 06:30:02 +0100481/**
482 * Declare this fifo is used by only a single thread.
483 * In this special case, fifo-growth can be done in an efficient way without delay.
484 *
485 * @param f fifo
486 * @return 1 if the fifo is already owned by another thread, 0 otherwise
487 */
488u8 svm_fifo_set_single_thread_owned (svm_fifo_t * f);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700489format_function_t format_svm_fifo;
490
Florin Coras58d36f02018-03-09 13:05:53 -0800491/**
Florin Coras87b15ce2019-04-28 21:16:30 -0700492 * Fifo max bytes to dequeue optimized for consumer
493 *
494 * @param f fifo
495 * @return max number of bytes that can be dequeued
496 */
497static inline u32
498svm_fifo_max_dequeue_cons (svm_fifo_t * f)
499{
500 u32 tail, head;
501 f_load_head_tail_cons (f, &head, &tail);
502 return f_cursize (f, head, tail);
503}
504
505/**
506 * Fifo max bytes to dequeue optimized for producer
507 *
508 * @param f fifo
509 * @return max number of bytes that can be dequeued
510 */
511static inline u32
512svm_fifo_max_dequeue_prod (svm_fifo_t * f)
513{
514 u32 tail, head;
515 f_load_head_tail_prod (f, &head, &tail);
516 return f_cursize (f, head, tail);
517}
518
519/**
520 * Fifo max bytes to dequeue
521 *
522 * Note: use producer or consumer specific functions for performance:
523 * @ref svm_fifo_max_dequeue_cons (svm_fifo_t *f)
524 * @ref svm_fifo_max_dequeue_prod (svm_fifo_t *f)
525 */
526static inline u32
527svm_fifo_max_dequeue (svm_fifo_t * f)
528{
529 u32 tail, head;
530 f_load_head_tail_all_acq (f, &head, &tail);
531 return f_cursize (f, head, tail);
532}
533
534/**
535 * Check if fifo is full optimized for producer
536 *
537 * @param f fifo
538 * @return 1 if fifo is full 0 otherwise
539 */
540static inline int
541svm_fifo_is_full_prod (svm_fifo_t * f)
542{
543 return (svm_fifo_max_dequeue_prod (f) == f->nitems);
544}
545
546/* Check if fifo is full.
547 *
548 * Note: use producer or consumer specific functions for performance.
549 * @ref svm_fifo_is_full_prod (svm_fifo_t * f)
550 * add cons version if needed
551 */
552static inline int
553svm_fifo_is_full (svm_fifo_t * f)
554{
555 return (svm_fifo_max_dequeue (f) == f->nitems);
556}
557
558/**
559 * Check if fifo is empty optimized for consumer
560 *
561 * @param f fifo
562 * @return 1 if fifo is empty 0 otherwise
563 */
564static inline int
565svm_fifo_is_empty_cons (svm_fifo_t * f)
566{
567 return (svm_fifo_max_dequeue_cons (f) == 0);
568}
569
570/**
571 * Check if fifo is empty optimized for producer
572 *
573 * @param f fifo
574 * @return 1 if fifo is empty 0 otherwise
575 */
576static inline int
577svm_fifo_is_empty_prod (svm_fifo_t * f)
578{
579 return (svm_fifo_max_dequeue_prod (f) == 0);
580}
581
582/**
583 * Check if fifo is empty
584 *
585 * Note: use producer or consumer specific functions for perfomance.
586 * @ref svm_fifo_is_empty_cons (svm_fifo_t * f)
587 * @ref svm_fifo_is_empty_prod (svm_fifo_t * f)
588 */
589static inline int
590svm_fifo_is_empty (svm_fifo_t * f)
591{
592 return (svm_fifo_max_dequeue (f) == 0);
593}
594
595/**
596 * Check if fifo is wrapped
597 *
598 * @param f fifo
599 * @return 1 if 'normalized' head is ahead of tail
600 */
601static inline u8
602svm_fifo_is_wrapped (svm_fifo_t * f)
603{
604 u32 head, tail;
605 f_load_head_tail_all_acq (f, &head, &tail);
Florin Coras29a59c32019-05-02 12:52:19 -0700606 return head > tail;
Florin Coras87b15ce2019-04-28 21:16:30 -0700607}
608
609/**
610 * Maximum number of bytes that can be enqueued into fifo
611 *
612 * Optimized for producer
613 *
614 * @param f fifo
615 * @return max number of bytes that can be enqueued into fifo
616 */
617static inline u32
618svm_fifo_max_enqueue_prod (svm_fifo_t * f)
619{
620 u32 head, tail;
621 f_load_head_tail_prod (f, &head, &tail);
Florin Corasa7570b02019-05-02 12:52:19 -0700622 if (PREDICT_FALSE (f->flags & SVM_FIFO_F_SHRINK))
623 svm_fifo_try_shrink (f, head, tail);
Florin Coras87b15ce2019-04-28 21:16:30 -0700624 return f_free_count (f, head, tail);
625}
626
627/* Maximum number of bytes that can be enqueued into fifo
628 *
629 * Note: use producer or consumer specific functions for performance.
630 * @ref svm_fifo_max_enqueue_prod (svm_fifo_t *f)
631 * add consumer specific version if needed.
632 */
633static inline u32
634svm_fifo_max_enqueue (svm_fifo_t * f)
635{
636 u32 head, tail;
637 f_load_head_tail_all_acq (f, &head, &tail);
Florin Corasa7570b02019-05-02 12:52:19 -0700638 if (PREDICT_FALSE (f->flags & SVM_FIFO_F_SHRINK))
639 svm_fifo_try_shrink (f, head, tail);
Florin Coras87b15ce2019-04-28 21:16:30 -0700640 return f_free_count (f, head, tail);
641}
642
643/**
Florin Coras58d36f02018-03-09 13:05:53 -0800644 * Max contiguous chunk of data that can be read
645 */
Florin Coras87b15ce2019-04-28 21:16:30 -0700646static inline u32
Florin Coras58d36f02018-03-09 13:05:53 -0800647svm_fifo_max_read_chunk (svm_fifo_t * f)
648{
Sirshak Das28aa5392019-02-05 01:33:33 -0600649 u32 head, tail;
Sirshak Das28aa5392019-02-05 01:33:33 -0600650 f_load_head_tail_cons (f, &head, &tail);
Florin Coras29a59c32019-05-02 12:52:19 -0700651 return tail >= head ? (tail - head) : (f->size - head);
Florin Coras58d36f02018-03-09 13:05:53 -0800652}
653
654/**
655 * Max contiguous chunk of data that can be written
656 */
Florin Coras87b15ce2019-04-28 21:16:30 -0700657static inline u32
Florin Coras58d36f02018-03-09 13:05:53 -0800658svm_fifo_max_write_chunk (svm_fifo_t * f)
659{
Sirshak Das28aa5392019-02-05 01:33:33 -0600660 u32 head, tail;
Sirshak Das28aa5392019-02-05 01:33:33 -0600661 f_load_head_tail_prod (f, &head, &tail);
Florin Corasfcd26a02019-08-08 12:57:48 -0700662 return tail >= head ? f->size - tail : f_free_count (f, head, tail);
Florin Coras58d36f02018-03-09 13:05:53 -0800663}
664
Florin Coras87b15ce2019-04-28 21:16:30 -0700665static inline u8 *
Florin Coras58d36f02018-03-09 13:05:53 -0800666svm_fifo_head (svm_fifo_t * f)
667{
Sirshak Das28aa5392019-02-05 01:33:33 -0600668 /* load-relaxed: consumer owned index */
Florin Coras29a59c32019-05-02 12:52:19 -0700669 return (f->head_chunk->data + (f->head - f->head_chunk->start_byte));
Florin Coras58d36f02018-03-09 13:05:53 -0800670}
671
Florin Coras87b15ce2019-04-28 21:16:30 -0700672static inline u8 *
Florin Coras58d36f02018-03-09 13:05:53 -0800673svm_fifo_tail (svm_fifo_t * f)
674{
Sirshak Das28aa5392019-02-05 01:33:33 -0600675 /* load-relaxed: producer owned index */
Florin Coras29a59c32019-05-02 12:52:19 -0700676 return (f->tail_chunk->data + (f->tail - f->tail_chunk->start_byte));
Florin Coras1bcad5c2019-01-09 20:04:38 -0800677}
678
Florin Coras87b15ce2019-04-28 21:16:30 -0700679static inline u8
680svm_fifo_n_subscribers (svm_fifo_t * f)
681{
682 return f->n_subscribers;
683}
684
685/**
686 * Check if fifo has out-of-order data
687 *
688 * @param f fifo
689 * @return 1 if fifo has ooo data, 0 otherwise
690 */
691static inline u8
692svm_fifo_has_ooo_data (svm_fifo_t * f)
693{
694 return f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX;
695}
696
697static inline ooo_segment_t *
698svm_fifo_newest_ooo_segment (svm_fifo_t * f)
699{
700 if (f->ooos_newest == OOO_SEGMENT_INVALID_INDEX)
701 return 0;
702 return pool_elt_at_index (f->ooo_segments, f->ooos_newest);
703}
704
705static inline void
706svm_fifo_newest_ooo_segment_reset (svm_fifo_t * f)
707{
708 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
709}
710
711static inline u32
712ooo_segment_offset_prod (svm_fifo_t * f, ooo_segment_t * s)
713{
714 u32 tail;
715 /* load-relaxed: producer owned index */
716 tail = f->tail;
717
718 return f_distance_to (f, s->start, tail);
719}
720
721static inline u32
722ooo_segment_length (svm_fifo_t * f, ooo_segment_t * s)
723{
724 return s->length;
725}
726
727/**
728 * Check if fifo has io event
729 *
730 * @param f fifo
731 * @return 1 if fifo has event, 0 otherwise
732 */
733static inline int
734svm_fifo_has_event (svm_fifo_t * f)
735{
736 return f->has_event;
737}
738
739/**
740 * Set fifo event flag.
741 *
742 * Forces release semantics.
743 *
744 * @param f fifo
745 * @return 1 if flag was not set, 0 otherwise
746 */
747always_inline u8
748svm_fifo_set_event (svm_fifo_t * f)
749{
750 return !clib_atomic_swap_rel_n (&f->has_event, 1);
751}
752
753/**
754 * Unset fifo event flag.
755 *
756 * Forces acquire semantics
757 *
758 * @param f fifo
759 */
760always_inline void
761svm_fifo_unset_event (svm_fifo_t * f)
762{
763 clib_atomic_swap_acq_n (&f->has_event, 0);
764}
765
766/**
Florin Coras2d379d82019-06-28 12:45:12 -0700767 * Set specific want notification flag
Florin Coras87b15ce2019-04-28 21:16:30 -0700768 *
Florin Coras2d379d82019-06-28 12:45:12 -0700769 * For list of flags see @ref svm_fifo_deq_ntf_t
Florin Coras87b15ce2019-04-28 21:16:30 -0700770 *
771 * @param f fifo
772 * @param ntf_type type of notification requested
773 */
Florin Coras1bcad5c2019-01-09 20:04:38 -0800774static inline void
Florin Coras2d379d82019-06-28 12:45:12 -0700775svm_fifo_add_want_deq_ntf (svm_fifo_t * f, u8 ntf_type)
Florin Coras1bcad5c2019-01-09 20:04:38 -0800776{
Florin Coras2d379d82019-06-28 12:45:12 -0700777 f->want_deq_ntf |= ntf_type;
Florin Coras1bcad5c2019-01-09 20:04:38 -0800778}
779
Florin Coras87b15ce2019-04-28 21:16:30 -0700780/**
Florin Coras2d379d82019-06-28 12:45:12 -0700781 * Clear specific want notification flag
Florin Coras87b15ce2019-04-28 21:16:30 -0700782 *
Florin Coras2d379d82019-06-28 12:45:12 -0700783 * For list of flags see @ref svm_fifo_ntf_t
Florin Coras87b15ce2019-04-28 21:16:30 -0700784 *
785 * @param f fifo
786 * @param ntf_type type of notification to be cleared
787 */
Florin Coras1bcad5c2019-01-09 20:04:38 -0800788static inline void
Florin Coras2d379d82019-06-28 12:45:12 -0700789svm_fifo_del_want_deq_ntf (svm_fifo_t * f, u8 ntf_type)
Florin Coras1bcad5c2019-01-09 20:04:38 -0800790{
Florin Coras2d379d82019-06-28 12:45:12 -0700791 f->want_deq_ntf &= ~ntf_type;
Florin Coras1bcad5c2019-01-09 20:04:38 -0800792}
793
Florin Coras87b15ce2019-04-28 21:16:30 -0700794/**
Florin Coras2d379d82019-06-28 12:45:12 -0700795 * Clear the want notification flag and set has notification
Florin Coras87b15ce2019-04-28 21:16:30 -0700796 *
Florin Coras2d379d82019-06-28 12:45:12 -0700797 * Should be used after enqueuing an event. This clears the
798 * SVM_FIFO_WANT_NOTIF flag but it does not clear
799 * SVM_FIFO_WANT_NOTIF_IF_FULL. If the latter was set, has_ntf is
800 * set to avoid enqueueing events for for all dequeue operations until
Florin Coras87b15ce2019-04-28 21:16:30 -0700801 * it is manually cleared.
802 *
803 * @param f fifo
804 */
Florin Coras1bcad5c2019-01-09 20:04:38 -0800805static inline void
Florin Coras2d379d82019-06-28 12:45:12 -0700806svm_fifo_clear_deq_ntf (svm_fifo_t * f)
Florin Coras1bcad5c2019-01-09 20:04:38 -0800807{
Florin Coras2d379d82019-06-28 12:45:12 -0700808 /* Set the flag if want_notif_if_full was the only ntf requested */
809 f->has_deq_ntf = f->want_deq_ntf == SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL;
810 svm_fifo_del_want_deq_ntf (f, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras1bcad5c2019-01-09 20:04:38 -0800811}
812
Florin Coras87b15ce2019-04-28 21:16:30 -0700813/**
Florin Coras2d379d82019-06-28 12:45:12 -0700814 * Clear has notification flag
Florin Coras87b15ce2019-04-28 21:16:30 -0700815 *
Florin Coras2d379d82019-06-28 12:45:12 -0700816 * The fifo generates only one event per SVM_FIFO_WANT_NOTIF_IF_FULL
817 * request and sets has_ntf. To received new events the flag must be
Florin Coras87b15ce2019-04-28 21:16:30 -0700818 * cleared using this function.
819 *
820 * @param f fifo
821 */
Florin Coras1bcad5c2019-01-09 20:04:38 -0800822static inline void
Florin Coras2d379d82019-06-28 12:45:12 -0700823svm_fifo_reset_has_deq_ntf (svm_fifo_t * f)
Florin Coras1bcad5c2019-01-09 20:04:38 -0800824{
Florin Coras2d379d82019-06-28 12:45:12 -0700825 f->has_deq_ntf = 0;
Florin Coras1bcad5c2019-01-09 20:04:38 -0800826}
827
Florin Coras87b15ce2019-04-28 21:16:30 -0700828/**
Florin Coras2d379d82019-06-28 12:45:12 -0700829 * Check if fifo needs dequeue notification
Florin Coras87b15ce2019-04-28 21:16:30 -0700830 *
Florin Coras2d379d82019-06-28 12:45:12 -0700831 * Determines based on notification request flags and state of the fifo if
832 * an event should be generated.
Florin Coras87b15ce2019-04-28 21:16:30 -0700833 *
834 * @param f fifo
835 * @param n_last_deq number of bytes last dequeued
Florin Coras2d379d82019-06-28 12:45:12 -0700836 * @return 1 if event should be generated, 0 otherwise
Florin Coras87b15ce2019-04-28 21:16:30 -0700837 */
Florin Coras1bcad5c2019-01-09 20:04:38 -0800838static inline u8
Florin Coras2d379d82019-06-28 12:45:12 -0700839svm_fifo_needs_deq_ntf (svm_fifo_t * f, u32 n_last_deq)
Florin Coras1bcad5c2019-01-09 20:04:38 -0800840{
Florin Coras2d379d82019-06-28 12:45:12 -0700841 u8 want_ntf = f->want_deq_ntf;
Florin Coras1bcad5c2019-01-09 20:04:38 -0800842
Florin Coras2d379d82019-06-28 12:45:12 -0700843 if (PREDICT_TRUE (want_ntf == SVM_FIFO_NO_DEQ_NOTIF))
Florin Coras1bcad5c2019-01-09 20:04:38 -0800844 return 0;
Florin Coras2d379d82019-06-28 12:45:12 -0700845 else if (want_ntf & SVM_FIFO_WANT_DEQ_NOTIF)
Florin Coras1bcad5c2019-01-09 20:04:38 -0800846 return 1;
Florin Coras2d379d82019-06-28 12:45:12 -0700847 if (want_ntf & SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL)
Florin Coras1bcad5c2019-01-09 20:04:38 -0800848 {
Sirshak Das28aa5392019-02-05 01:33:33 -0600849 u32 max_deq = svm_fifo_max_dequeue_cons (f);
850 u32 nitems = f->nitems;
Florin Coras2d379d82019-06-28 12:45:12 -0700851 if (!f->has_deq_ntf && max_deq < nitems
Florin Coras1bcad5c2019-01-09 20:04:38 -0800852 && max_deq + n_last_deq >= nitems)
853 return 1;
Nathan Skrzypczake82a7ad2019-05-22 18:41:50 +0200854 }
Florin Coras2d379d82019-06-28 12:45:12 -0700855 if (want_ntf & SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY)
Nathan Skrzypczake82a7ad2019-05-22 18:41:50 +0200856 {
Florin Coras2d379d82019-06-28 12:45:12 -0700857 if (!f->has_deq_ntf && svm_fifo_is_empty (f))
Nathan Skrzypczake82a7ad2019-05-22 18:41:50 +0200858 return 1;
Florin Coras1bcad5c2019-01-09 20:04:38 -0800859 }
860 return 0;
861}
862
Dave Barach68b0fb02017-02-28 15:15:56 -0500863#endif /* __included_ssvm_fifo_h__ */
864
865/*
866 * fd.io coding-style-patch-verification: ON
867 *
868 * Local Variables:
869 * eval: (c-set-style "gnu")
870 * End:
871 */