blob: 4239e9d78ea10aa4ee2d26d16a89f46d3807b971 [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 Corasf22f4e52019-12-19 16:10:58 -080026#include <svm/fifo_types.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050027
Florin Coras326b81e2018-10-04 19:03:05 -070028#define OOO_SEGMENT_INVALID_INDEX ((u32)~0)
29#define SVM_FIFO_INVALID_SESSION_INDEX ((u32)~0)
Florin Corasfa76a762018-11-29 12:40:10 -080030#define SVM_FIFO_INVALID_INDEX ((u32)~0)
Dave Barach68b0fb02017-02-28 15:15:56 -050031
Florin Coras2d379d82019-06-28 12:45:12 -070032typedef enum svm_fifo_deq_ntf_
Florin Coras1bcad5c2019-01-09 20:04:38 -080033{
Florin Coras2d379d82019-06-28 12:45:12 -070034 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;
Florin Coras1bcad5c2019-01-09 20:04:38 -080039
Florin Coras2309e7a2019-04-18 18:58:10 -070040typedef enum svm_fifo_flag_
41{
Florin Corasf22f4e52019-12-19 16:10:58 -080042 SVM_FIFO_F_LL_TRACKED = 1 << 0,
Florin Coras2309e7a2019-04-18 18:58:10 -070043} svm_fifo_flag_t;
44
Florin Coras7fb0fe12018-04-09 09:24:52 -070045typedef enum
46{
Florin Coras87b15ce2019-04-28 21:16:30 -070047 SVM_FIFO_EFULL = -2,
48 SVM_FIFO_EEMPTY = -3,
Florin Corasf22f4e52019-12-19 16:10:58 -080049 SVM_FIFO_EGROW = -4,
Florin Coras7fb0fe12018-04-09 09:24:52 -070050} svm_fifo_err_t;
51
Florin Coras88001c62019-04-24 14:44:46 -070052typedef struct svm_fifo_seg_
Florin Coras2cba8532018-09-11 16:33:36 -070053{
54 u8 *data;
55 u32 len;
Florin Coras88001c62019-04-24 14:44:46 -070056} svm_fifo_seg_t;
Florin Coras2cba8532018-09-11 16:33:36 -070057
Florin Coras3eb50622017-07-13 01:24:57 -040058#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
71u8 *svm_fifo_dump_trace (u8 * s, svm_fifo_t * f);
72u8 *svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose);
73
Florin Coras87b15ce2019-04-28 21:16:30 -070074/**
75 * Load head and tail optimized for consumer
76 *
77 * Internal function.
78 */
Sirshak Das28aa5392019-02-05 01:33:33 -060079static inline void
80f_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
Florin Coras87b15ce2019-04-28 21:16:30 -070088/** Load head and tail optimized for producer
89 *
90 * Internal function
91 */
Sirshak Das28aa5392019-02-05 01:33:33 -060092static inline void
93f_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
Florin Corasa7570b02019-05-02 12:52:19 -0700101/**
102 * Load head and tail independent of producer/consumer role
Florin Coras87b15ce2019-04-28 21:16:30 -0700103 *
104 * Internal function.
105 */
Sirshak Das28aa5392019-02-05 01:33:33 -0600106static inline void
107f_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
Florin Coras87b15ce2019-04-28 21:16:30 -0700115/**
Florin Coras29a59c32019-05-02 12:52:19 -0700116 * Fifo current size, i.e., number of bytes enqueued
117 *
118 * Internal function.
119 */
120static inline u32
121f_cursize (svm_fifo_t * f, u32 head, u32 tail)
122{
Florin Corasf22f4e52019-12-19 16:10:58 -0800123 return tail - head;
Florin Coras29a59c32019-05-02 12:52:19 -0700124}
125
126/**
127 * Fifo free bytes, i.e., number of free bytes
128 *
129 * Internal function
130 */
131static inline u32
132f_free_count (svm_fifo_t * f, u32 head, u32 tail)
133{
Florin Corasf22f4e52019-12-19 16:10:58 -0800134 return (f->size - f_cursize (f, head, tail));
Florin Coras29a59c32019-05-02 12:52:19 -0700135}
136
Florin Corasf22f4e52019-12-19 16:10:58 -0800137always_inline u32
138f_chunk_end (svm_fifo_chunk_t * c)
139{
140 return c->start_byte + c->length;
141}
142
143always_inline int
144f_pos_lt (u32 a, u32 b)
145{
146 return ((i32) (a - b) < 0);
147}
148
149always_inline int
150f_pos_leq (u32 a, u32 b)
151{
152 return ((i32) (a - b) <= 0);
153}
154
155always_inline int
156f_pos_gt (u32 a, u32 b)
157{
158 return ((i32) (a - b) > 0);
159}
160
161always_inline int
162f_pos_geq (u32 a, u32 b)
163{
164 return ((i32) (a - b) >= 0);
165}
166
167always_inline u8
168f_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}
Florin Corasa7570b02019-05-02 12:52:19 -0700173
174/**
Florin Coras87b15ce2019-04-28 21:16:30 -0700175 * Create fifo of requested size
Florin Coras41c9e042018-09-11 00:10:41 -0700176 *
Florin Coras87b15ce2019-04-28 21:16:30 -0700177 * 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
Florin Coras6792ec02017-03-13 03:49:51 -0700182 */
Florin Corasf22f4e52019-12-19 16:10:58 -0800183svm_fifo_t *svm_fifo_alloc (u32 size);
Florin Coras87b15ce2019-04-28 21:16:30 -0700184/**
185 * Initialize fifo
186 *
Florin Coraseaacce42019-07-02 13:07:37 -0700187 * @param f fifo
Florin Coras87b15ce2019-04-28 21:16:30 -0700188 * @param size size for fifo
189 */
Florin Coras0a846802019-04-09 18:29:14 -0700190void svm_fifo_init (svm_fifo_t * f, u32 size);
Florin Corasb095a3c2019-04-25 12:58:46 -0700191/**
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 */
201svm_fifo_chunk_t *svm_fifo_chunk_alloc (u32 size);
Florin Coras4375fa32019-04-19 15:56:00 -0700202/**
Florin Coras9e61d9a2020-02-05 21:13:18 +0000203 * Ensure the whole fifo size is writeable
Florin Coras4375fa32019-04-19 15:56:00 -0700204 *
Florin Coras9e61d9a2020-02-05 21:13:18 +0000205 * Allocates enough chunks to cover the whole fifo size.
Florin Coras4375fa32019-04-19 15:56:00 -0700206 *
Florin Coras9e61d9a2020-02-05 21:13:18 +0000207 * @param f fifo
Florin Coras4375fa32019-04-19 15:56:00 -0700208 */
Florin Corasf22f4e52019-12-19 16:10:58 -0800209int svm_fifo_fill_chunk_list (svm_fifo_t * f);
Florin Coras9e61d9a2020-02-05 21:13:18 +0000210/**
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 */
Florin Corasf22f4e52019-12-19 16:10:58 -0800216void svm_fifo_init_ooo_lookup (svm_fifo_t * f, u8 ooo_type);
Florin Corasa7570b02019-05-02 12:52:19 -0700217/**
Florin Coras87b15ce2019-04-28 21:16:30 -0700218 * Free fifo and associated state
219 *
220 * @param f fifo
221 */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700222void svm_fifo_free (svm_fifo_t * f);
Florin Corasb095a3c2019-04-25 12:58:46 -0700223/**
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 */
231void 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 */
240void svm_fifo_free_ooo_data (svm_fifo_t * f);
Florin Coras87b15ce2019-04-28 21:16:30 -0700241/**
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 */
248void 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 */
255void 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 */
268int 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 */
282int svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 len,
283 u8 * src);
Florin Corasa7570b02019-05-02 12:52:19 -0700284
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 */
293void svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 len);
Florin Coras87b15ce2019-04-28 21:16:30 -0700294/**
Florin Corasc95cfa22020-11-24 08:41:17 -0800295 * Enqueue array of @ref svm_fifo_seg_t in order
296 *
297 * @param f fifo
298 * @param segs array of segments to enqueue
299 * @param n_segs number of segments
300 * @param allow_partial if set partial enqueues are allowed
301 * @return len if enqueue was successful, error otherwise
302 */
303int svm_fifo_enqueue_segments (svm_fifo_t * f, const svm_fifo_seg_t segs[],
304 u32 n_segs, u8 allow_partial);
305/**
Florin Coras87b15ce2019-04-28 21:16:30 -0700306 * Overwrite fifo head with new data
307 *
308 * This should be typically used by dgram transport protocols that need
Florin Coras97d39e32020-09-04 08:57:27 -0700309 * to update the dgram header after dequeuing a chunk of data. It assumes
Florin Coras87b15ce2019-04-28 21:16:30 -0700310 * that the dgram header is at most spread over two chunks.
311 *
312 * @param f fifo
313 * @param src src of new data
314 * @param len length of new data
315 */
316void svm_fifo_overwrite_head (svm_fifo_t * f, u8 * src, u32 len);
317/**
318 * Dequeue data from fifo
319 *
320 * Data is dequeued to consumer provided buffer and head is atomically
Florin Coras97d39e32020-09-04 08:57:27 -0700321 * updated. This should not be used in combination with ooo lookups. If
322 * ooo peeking of data is needed in combination with dequeuing use @ref
323 * svm_fifo_dequeue_drop.
Florin Coras87b15ce2019-04-28 21:16:30 -0700324 *
325 * @param f fifo
326 * @param len length of data to dequeue
327 * @param dst buffer to where to dequeue the data
328 * @return number of bytes dequeued or error
329 */
330int svm_fifo_dequeue (svm_fifo_t * f, u32 len, u8 * dst);
331/**
332 * Peek data from fifo
333 *
334 * Data is copied from requested offset into provided dst buffer. Head is
335 * not updated.
336 *
337 * @param f fifo
338 * @param offset offset from which to copy the data
339 * @param len length of data to copy
340 * @param dst buffer to where to dequeue the data
341 * @return number of bytes peeked
342 */
343int svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 len, u8 * dst);
344/**
345 * Dequeue and drop bytes from fifo
346 *
347 * Advances fifo head by requested amount of bytes.
348 *
349 * @param f fifo
350 * @param len number of bytes to drop
351 * @return number of bytes dropped
352 */
353int svm_fifo_dequeue_drop (svm_fifo_t * f, u32 len);
354/**
355 * Dequeue and drop all bytes from fifo
356 *
357 * Advances head to tail position.
358 *
359 * @param f fifo
360 */
Florin Coras25579b42018-06-06 17:55:02 -0700361void svm_fifo_dequeue_drop_all (svm_fifo_t * f);
Florin Corasd68faf82020-09-25 15:18:13 -0700362/**
363 * Get pointers to fifo chunks data in @ref svm_fifo_seg_t array
364 *
365 * Populates fifo segment array with pointers to fifo chunk data and lengths.
366 * Because this returns pointers to data, it must be paired with
367 * @ref svm_fifo_dequeue_drop to actually release the fifo chunks after the
368 * data is consumed.
369 *
370 * @param f fifo
Florin Corasd1cc38d2020-10-11 11:05:04 -0700371 * @param offset offset from where to retrieve segments
Florin Corasd68faf82020-09-25 15:18:13 -0700372 * @param fs array of fifo segments allocated by caller
373 * @param n_segs number of fifo segments in array
374 * @param max_bytes max bytes to be mapped to fifo segments
375 * @return number of bytes in fifo segments or SVM_FIFO_EEMPTY
376 */
Florin Corasd1cc38d2020-10-11 11:05:04 -0700377int svm_fifo_segments (svm_fifo_t * f, u32 offset, svm_fifo_seg_t * fs,
378 u32 n_segs, u32 max_bytes);
Florin Coras87b15ce2019-04-28 21:16:30 -0700379/**
380 * Add io events subscriber to list
381 *
382 * @param f fifo
383 * @param sub subscriber opaque index (typically app worker index)
384 */
385void svm_fifo_add_subscriber (svm_fifo_t * f, u8 sub);
386/**
387 * Remove io events subscriber form list
388 *
389 * @param f fifo
390 * @param sub subscriber index to be removed
391 */
Florin Coras72b04282019-01-14 17:23:11 -0800392void svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber);
Florin Coras87b15ce2019-04-28 21:16:30 -0700393/**
394 * Number of out-of-order segments for fifo
395 *
396 * @param f fifo
397 * @return number of out of order segments
398 */
399u32 svm_fifo_n_ooo_segments (svm_fifo_t * f);
Florin Coraseaacce42019-07-02 13:07:37 -0700400/**
Florin Coras87b15ce2019-04-28 21:16:30 -0700401 * First out-of-order segment for fifo
402 *
403 * @param f fifo
404 * @return first out-of-order segment for fifo
405 */
406ooo_segment_t *svm_fifo_first_ooo_segment (svm_fifo_t * f);
Florin Coraseaacce42019-07-02 13:07:37 -0700407/**
408 * Check if fifo is sane. Debug only.
409 *
410 * @param f fifo
411 * @return 1 if sane, 0 otherwise
412 */
413u8 svm_fifo_is_sane (svm_fifo_t * f);
Florin Coras9e61d9a2020-02-05 21:13:18 +0000414/**
415 * Number of chunks linked into the fifo
416 *
417 * @param f fifo
418 * @return number of chunks in fifo linked list
419 */
Florin Corasf22f4e52019-12-19 16:10:58 -0800420u32 svm_fifo_n_chunks (svm_fifo_t * f);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700421format_function_t format_svm_fifo;
422
Florin Coras58d36f02018-03-09 13:05:53 -0800423/**
Florin Coras87b15ce2019-04-28 21:16:30 -0700424 * Fifo max bytes to dequeue optimized for consumer
425 *
426 * @param f fifo
427 * @return max number of bytes that can be dequeued
428 */
429static inline u32
430svm_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 */
443static inline u32
444svm_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 */
458static inline u32
459svm_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 */
472static inline int
473svm_fifo_is_full_prod (svm_fifo_t * f)
474{
Florin Corasf22f4e52019-12-19 16:10:58 -0800475 return (svm_fifo_max_dequeue_prod (f) == f->size);
Florin Coras87b15ce2019-04-28 21:16:30 -0700476}
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 */
484static inline int
485svm_fifo_is_full (svm_fifo_t * f)
486{
Florin Corasf22f4e52019-12-19 16:10:58 -0800487 return (svm_fifo_max_dequeue (f) == f->size);
Florin Coras87b15ce2019-04-28 21:16:30 -0700488}
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 */
496static inline int
497svm_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 */
508static inline int
509svm_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 */
521static inline int
522svm_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 */
533static inline u8
534svm_fifo_is_wrapped (svm_fifo_t * f)
535{
536 u32 head, tail;
537 f_load_head_tail_all_acq (f, &head, &tail);
Florin Coras29a59c32019-05-02 12:52:19 -0700538 return head > tail;
Florin Coras87b15ce2019-04-28 21:16:30 -0700539}
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 */
549static inline u32
550svm_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 */
563static inline u32
564svm_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/**
Florin Corasf22f4e52019-12-19 16:10:58 -0800572 * Max contiguous chunk of data that can be read.
573 *
574 * Should only be called by consumers.
Florin Coras58d36f02018-03-09 13:05:53 -0800575 */
Florin Corasf22f4e52019-12-19 16:10:58 -0800576u32 svm_fifo_max_read_chunk (svm_fifo_t * f);
Florin Coras58d36f02018-03-09 13:05:53 -0800577
578/**
579 * Max contiguous chunk of data that can be written
Florin Corasf22f4e52019-12-19 16:10:58 -0800580 *
581 * Should only be called by producers
Florin Coras58d36f02018-03-09 13:05:53 -0800582 */
Florin Corasf22f4e52019-12-19 16:10:58 -0800583u32 svm_fifo_max_write_chunk (svm_fifo_t * f);
584
Florin Coras9e61d9a2020-02-05 21:13:18 +0000585/**
586 * Fifo head chunk getter
587 *
588 * @param f fifo
589 * @return head chunk pointer
590 */
Florin Corasf22f4e52019-12-19 16:10:58 -0800591static inline svm_fifo_chunk_t *
592svm_fifo_head_chunk (svm_fifo_t * f)
Florin Coras58d36f02018-03-09 13:05:53 -0800593{
Florin Corasf22f4e52019-12-19 16:10:58 -0800594 return f->head_chunk;
Florin Coras58d36f02018-03-09 13:05:53 -0800595}
596
Florin Coras9e61d9a2020-02-05 21:13:18 +0000597/**
598 * Fifo head pointer getter
599 *
600 * @param f fifo
601 * @return head pointer
602 */
Florin Coras87b15ce2019-04-28 21:16:30 -0700603static inline u8 *
Florin Coras58d36f02018-03-09 13:05:53 -0800604svm_fifo_head (svm_fifo_t * f)
605{
Florin Corasf22f4e52019-12-19 16:10:58 -0800606 if (!f->head_chunk)
607 return 0;
Sirshak Das28aa5392019-02-05 01:33:33 -0600608 /* load-relaxed: consumer owned index */
Florin Coras29a59c32019-05-02 12:52:19 -0700609 return (f->head_chunk->data + (f->head - f->head_chunk->start_byte));
Florin Coras58d36f02018-03-09 13:05:53 -0800610}
611
Florin Coras9e61d9a2020-02-05 21:13:18 +0000612/**
613 * Fifo tail chunk getter
614 *
615 * @param f fifo
616 * @return tail chunk pointer
617 */
Florin Corasf22f4e52019-12-19 16:10:58 -0800618static inline svm_fifo_chunk_t *
619svm_fifo_tail_chunk (svm_fifo_t * f)
620{
621 return f->tail_chunk;
622}
623
Florin Coras9e61d9a2020-02-05 21:13:18 +0000624/**
625 * Fifo tail pointer getter
626 *
627 * @param f fifo
628 * @return tail pointer
629 */
Florin Coras87b15ce2019-04-28 21:16:30 -0700630static inline u8 *
Florin Coras58d36f02018-03-09 13:05:53 -0800631svm_fifo_tail (svm_fifo_t * f)
632{
Sirshak Das28aa5392019-02-05 01:33:33 -0600633 /* load-relaxed: producer owned index */
Florin Coras29a59c32019-05-02 12:52:19 -0700634 return (f->tail_chunk->data + (f->tail - f->tail_chunk->start_byte));
Florin Coras1bcad5c2019-01-09 20:04:38 -0800635}
636
Florin Coras9e61d9a2020-02-05 21:13:18 +0000637/**
638 * Fifo number of subscribers getter
639 *
640 * @param f fifo
641 * @return number of subscribers
642 */
Florin Coras87b15ce2019-04-28 21:16:30 -0700643static inline u8
644svm_fifo_n_subscribers (svm_fifo_t * f)
645{
646 return f->n_subscribers;
647}
648
649/**
650 * Check if fifo has out-of-order data
651 *
652 * @param f fifo
653 * @return 1 if fifo has ooo data, 0 otherwise
654 */
655static inline u8
656svm_fifo_has_ooo_data (svm_fifo_t * f)
657{
658 return f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX;
659}
660
661static inline ooo_segment_t *
662svm_fifo_newest_ooo_segment (svm_fifo_t * f)
663{
664 if (f->ooos_newest == OOO_SEGMENT_INVALID_INDEX)
665 return 0;
666 return pool_elt_at_index (f->ooo_segments, f->ooos_newest);
667}
668
669static inline void
670svm_fifo_newest_ooo_segment_reset (svm_fifo_t * f)
671{
672 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
673}
674
675static inline u32
676ooo_segment_offset_prod (svm_fifo_t * f, ooo_segment_t * s)
677{
678 u32 tail;
679 /* load-relaxed: producer owned index */
680 tail = f->tail;
681
Florin Corasf22f4e52019-12-19 16:10:58 -0800682 return (s->start - tail);
Florin Coras87b15ce2019-04-28 21:16:30 -0700683}
684
685static inline u32
686ooo_segment_length (svm_fifo_t * f, ooo_segment_t * s)
687{
688 return s->length;
689}
690
Florin Corasf22f4e52019-12-19 16:10:58 -0800691static inline u32
692svm_fifo_size (svm_fifo_t * f)
693{
694 return f->size;
695}
696
697static inline void
698svm_fifo_set_size (svm_fifo_t * f, u32 size)
699{
Florin Coras0e6199d2020-04-17 20:15:22 +0000700 if (size > (1 << f->fs_hdr->max_log2_chunk_size))
701 return;
Florin Coras06d47752020-03-06 02:23:58 +0000702 fsh_virtual_mem_update (f->fs_hdr, f->slice_index, (int) f->size - size);
Florin Corasf22f4e52019-12-19 16:10:58 -0800703 f->size = size;
704}
705
Florin Coras87b15ce2019-04-28 21:16:30 -0700706/**
707 * Check if fifo has io event
708 *
709 * @param f fifo
710 * @return 1 if fifo has event, 0 otherwise
711 */
712static inline int
713svm_fifo_has_event (svm_fifo_t * f)
714{
715 return f->has_event;
716}
717
718/**
719 * Set fifo event flag.
720 *
721 * Forces release semantics.
722 *
723 * @param f fifo
724 * @return 1 if flag was not set, 0 otherwise
725 */
726always_inline u8
727svm_fifo_set_event (svm_fifo_t * f)
728{
729 return !clib_atomic_swap_rel_n (&f->has_event, 1);
730}
731
732/**
733 * Unset fifo event flag.
734 *
735 * Forces acquire semantics
736 *
737 * @param f fifo
738 */
739always_inline void
740svm_fifo_unset_event (svm_fifo_t * f)
741{
742 clib_atomic_swap_acq_n (&f->has_event, 0);
743}
744
745/**
Florin Coras2d379d82019-06-28 12:45:12 -0700746 * Set specific want notification flag
Florin Coras87b15ce2019-04-28 21:16:30 -0700747 *
Florin Coras2d379d82019-06-28 12:45:12 -0700748 * For list of flags see @ref svm_fifo_deq_ntf_t
Florin Coras87b15ce2019-04-28 21:16:30 -0700749 *
750 * @param f fifo
751 * @param ntf_type type of notification requested
752 */
Florin Coras1bcad5c2019-01-09 20:04:38 -0800753static inline void
Florin Coras2d379d82019-06-28 12:45:12 -0700754svm_fifo_add_want_deq_ntf (svm_fifo_t * f, u8 ntf_type)
Florin Coras1bcad5c2019-01-09 20:04:38 -0800755{
Florin Coras2d379d82019-06-28 12:45:12 -0700756 f->want_deq_ntf |= ntf_type;
Florin Coras1bcad5c2019-01-09 20:04:38 -0800757}
758
Florin Coras87b15ce2019-04-28 21:16:30 -0700759/**
Florin Coras2d379d82019-06-28 12:45:12 -0700760 * Clear specific want notification flag
Florin Coras87b15ce2019-04-28 21:16:30 -0700761 *
Florin Coras2d379d82019-06-28 12:45:12 -0700762 * For list of flags see @ref svm_fifo_ntf_t
Florin Coras87b15ce2019-04-28 21:16:30 -0700763 *
764 * @param f fifo
765 * @param ntf_type type of notification to be cleared
766 */
Florin Coras1bcad5c2019-01-09 20:04:38 -0800767static inline void
Florin Coras2d379d82019-06-28 12:45:12 -0700768svm_fifo_del_want_deq_ntf (svm_fifo_t * f, u8 ntf_type)
Florin Coras1bcad5c2019-01-09 20:04:38 -0800769{
Florin Coras2d379d82019-06-28 12:45:12 -0700770 f->want_deq_ntf &= ~ntf_type;
Florin Coras1bcad5c2019-01-09 20:04:38 -0800771}
772
Florin Coras87b15ce2019-04-28 21:16:30 -0700773/**
Florin Coras2d379d82019-06-28 12:45:12 -0700774 * Clear the want notification flag and set has notification
Florin Coras87b15ce2019-04-28 21:16:30 -0700775 *
Florin Coras2d379d82019-06-28 12:45:12 -0700776 * Should be used after enqueuing an event. This clears the
777 * SVM_FIFO_WANT_NOTIF flag but it does not clear
778 * SVM_FIFO_WANT_NOTIF_IF_FULL. If the latter was set, has_ntf is
779 * set to avoid enqueueing events for for all dequeue operations until
Florin Coras87b15ce2019-04-28 21:16:30 -0700780 * it is manually cleared.
781 *
782 * @param f fifo
783 */
Florin Coras1bcad5c2019-01-09 20:04:38 -0800784static inline void
Florin Coras2d379d82019-06-28 12:45:12 -0700785svm_fifo_clear_deq_ntf (svm_fifo_t * f)
Florin Coras1bcad5c2019-01-09 20:04:38 -0800786{
Florin Coras2d379d82019-06-28 12:45:12 -0700787 /* Set the flag if want_notif_if_full was the only ntf requested */
788 f->has_deq_ntf = f->want_deq_ntf == SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL;
789 svm_fifo_del_want_deq_ntf (f, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras1bcad5c2019-01-09 20:04:38 -0800790}
791
Florin Coras87b15ce2019-04-28 21:16:30 -0700792/**
Florin Coras2d379d82019-06-28 12:45:12 -0700793 * Clear has notification flag
Florin Coras87b15ce2019-04-28 21:16:30 -0700794 *
Florin Coras2d379d82019-06-28 12:45:12 -0700795 * The fifo generates only one event per SVM_FIFO_WANT_NOTIF_IF_FULL
796 * request and sets has_ntf. To received new events the flag must be
Florin Coras87b15ce2019-04-28 21:16:30 -0700797 * cleared using this function.
798 *
799 * @param f fifo
800 */
Florin Coras1bcad5c2019-01-09 20:04:38 -0800801static inline void
Florin Coras2d379d82019-06-28 12:45:12 -0700802svm_fifo_reset_has_deq_ntf (svm_fifo_t * f)
Florin Coras1bcad5c2019-01-09 20:04:38 -0800803{
Florin Coras2d379d82019-06-28 12:45:12 -0700804 f->has_deq_ntf = 0;
Florin Coras1bcad5c2019-01-09 20:04:38 -0800805}
806
Florin Coras87b15ce2019-04-28 21:16:30 -0700807/**
Florin Coras2d379d82019-06-28 12:45:12 -0700808 * Check if fifo needs dequeue notification
Florin Coras87b15ce2019-04-28 21:16:30 -0700809 *
Florin Coras2d379d82019-06-28 12:45:12 -0700810 * Determines based on notification request flags and state of the fifo if
811 * an event should be generated.
Florin Coras87b15ce2019-04-28 21:16:30 -0700812 *
813 * @param f fifo
814 * @param n_last_deq number of bytes last dequeued
Florin Coras2d379d82019-06-28 12:45:12 -0700815 * @return 1 if event should be generated, 0 otherwise
Florin Coras87b15ce2019-04-28 21:16:30 -0700816 */
Florin Coras1bcad5c2019-01-09 20:04:38 -0800817static inline u8
Florin Coras2d379d82019-06-28 12:45:12 -0700818svm_fifo_needs_deq_ntf (svm_fifo_t * f, u32 n_last_deq)
Florin Coras1bcad5c2019-01-09 20:04:38 -0800819{
Florin Coras2d379d82019-06-28 12:45:12 -0700820 u8 want_ntf = f->want_deq_ntf;
Florin Coras1bcad5c2019-01-09 20:04:38 -0800821
Florin Coras2d379d82019-06-28 12:45:12 -0700822 if (PREDICT_TRUE (want_ntf == SVM_FIFO_NO_DEQ_NOTIF))
Florin Coras1bcad5c2019-01-09 20:04:38 -0800823 return 0;
Florin Coras2d379d82019-06-28 12:45:12 -0700824 else if (want_ntf & SVM_FIFO_WANT_DEQ_NOTIF)
Florin Coras1bcad5c2019-01-09 20:04:38 -0800825 return 1;
Florin Coras2d379d82019-06-28 12:45:12 -0700826 if (want_ntf & SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL)
Florin Coras1bcad5c2019-01-09 20:04:38 -0800827 {
Sirshak Das28aa5392019-02-05 01:33:33 -0600828 u32 max_deq = svm_fifo_max_dequeue_cons (f);
Florin Corasf22f4e52019-12-19 16:10:58 -0800829 u32 size = f->size;
830 if (!f->has_deq_ntf && max_deq < size && max_deq + n_last_deq >= size)
Florin Coras1bcad5c2019-01-09 20:04:38 -0800831 return 1;
Nathan Skrzypczake82a7ad2019-05-22 18:41:50 +0200832 }
Florin Coras2d379d82019-06-28 12:45:12 -0700833 if (want_ntf & SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY)
Nathan Skrzypczake82a7ad2019-05-22 18:41:50 +0200834 {
Florin Coras2d379d82019-06-28 12:45:12 -0700835 if (!f->has_deq_ntf && svm_fifo_is_empty (f))
Nathan Skrzypczake82a7ad2019-05-22 18:41:50 +0200836 return 1;
Florin Coras1bcad5c2019-01-09 20:04:38 -0800837 }
838 return 0;
839}
840
Dave Barach68b0fb02017-02-28 15:15:56 -0500841#endif /* __included_ssvm_fifo_h__ */
842
843/*
844 * fd.io coding-style-patch-verification: ON
845 *
846 * Local Variables:
847 * eval: (c-set-style "gnu")
848 * End:
849 */