blob: d7146ae1fb0b1013a0ad4f5848f8291291e32ff1 [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#ifndef __included_ssvm_fifo_h__
16#define __included_ssvm_fifo_h__
17
18#include <vppinfra/clib.h>
19#include <vppinfra/vec.h>
20#include <vppinfra/mheap.h>
21#include <vppinfra/heap.h>
22#include <vppinfra/pool.h>
23#include <vppinfra/format.h>
24#include <pthread.h>
25
Dave Barach68b0fb02017-02-28 15:15:56 -050026/** Out-of-order segment */
27typedef struct
28{
29 u32 next; /**< Next linked-list element pool index */
30 u32 prev; /**< Previous linked-list element pool index */
31
Dave Barach1f75cfd2017-04-14 16:46:44 -040032 u32 start; /**< Start of segment, normalized*/
Florin Corasa5464812017-04-19 13:00:05 -070033 u32 length; /**< Length of segment */
Dave Barach68b0fb02017-02-28 15:15:56 -050034} ooo_segment_t;
35
Dave Barach1f75cfd2017-04-14 16:46:44 -040036format_function_t format_ooo_segment;
37format_function_t format_ooo_list;
38
Florin Coras326b81e2018-10-04 19:03:05 -070039#define SVM_FIFO_TRACE (0)
40#define OOO_SEGMENT_INVALID_INDEX ((u32)~0)
41#define SVM_FIFO_INVALID_SESSION_INDEX ((u32)~0)
Florin Corasfa76a762018-11-29 12:40:10 -080042#define SVM_FIFO_INVALID_INDEX ((u32)~0)
Florin Coras72b04282019-01-14 17:23:11 -080043#define SVM_FIFO_MAX_EVT_SUBSCRIBERS 8
Dave Barach68b0fb02017-02-28 15:15:56 -050044
Florin Coras1bcad5c2019-01-09 20:04:38 -080045enum
46{
47 SVM_FIFO_NO_TX_NOTIF = 0,
48 SVM_FIFO_WANT_TX_NOTIF = 1,
49 SVM_FIFO_WANT_TX_NOTIF_IF_FULL = 2,
50};
51
Florin Coras3eb50622017-07-13 01:24:57 -040052typedef struct
53{
54 u32 offset;
55 u32 len;
56 u32 action;
57} svm_fifo_trace_elem_t;
58
Dave Barach10d8cc62017-05-30 09:30:07 -040059typedef struct _svm_fifo
Dave Barach68b0fb02017-02-28 15:15:56 -050060{
Florin Coras72b04282019-01-14 17:23:11 -080061 CLIB_CACHE_LINE_ALIGN_MARK (shared_first);
Florin Coras3e350af2017-03-30 02:54:28 -070062 volatile u32 cursize; /**< current fifo size */
63 u32 nitems;
Florin Coras3e350af2017-03-30 02:54:28 -070064
Florin Coras72b04282019-01-14 17:23:11 -080065 CLIB_CACHE_LINE_ALIGN_MARK (shared_second);
Dave Barachacd2a6a2017-05-16 17:41:34 -040066 volatile u32 has_event; /**< non-zero if deq event exists */
Dave Barach68b0fb02017-02-28 15:15:56 -050067
Florin Corasa5464812017-04-19 13:00:05 -070068 u32 master_session_index;
Dave Barach68b0fb02017-02-28 15:15:56 -050069 u32 client_session_index;
Florin Corasa5464812017-04-19 13:00:05 -070070 u8 master_thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -050071 u8 client_thread_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -070072 u32 segment_manager;
Florin Corasfa76a762018-11-29 12:40:10 -080073 u32 segment_index;
Florin Coras326b81e2018-10-04 19:03:05 -070074 u32 ct_session_index; /**< Local session index for vpp */
Florin Coras72b04282019-01-14 17:23:11 -080075 u32 freelist_index; /**< aka log2(allocated_size) - const. */
76 i8 refcnt; /**< reference count */
77
78 CLIB_CACHE_LINE_ALIGN_MARK (consumer);
Dave Barach68b0fb02017-02-28 15:15:56 -050079 u32 head;
Florin Coras1bcad5c2019-01-09 20:04:38 -080080 volatile u32 want_tx_ntf; /**< producer wants nudge */
81 volatile u32 has_tx_ntf;
Dave Barach68b0fb02017-02-28 15:15:56 -050082
Florin Coras72b04282019-01-14 17:23:11 -080083 CLIB_CACHE_LINE_ALIGN_MARK (producer);
Dave Barach68b0fb02017-02-28 15:15:56 -050084 u32 tail;
85
86 ooo_segment_t *ooo_segments; /**< Pool of ooo segments */
87 u32 ooos_list_head; /**< Head of out-of-order linked-list */
88 u32 ooos_newest; /**< Last segment to have been updated */
Dave Barach10d8cc62017-05-30 09:30:07 -040089 struct _svm_fifo *next; /**< next in freelist/active chain */
90 struct _svm_fifo *prev; /**< prev in active chain */
Florin Coras0ef8ef22019-01-18 08:37:13 -080091 volatile u8 n_subscribers;
Florin Coras72b04282019-01-14 17:23:11 -080092 u8 subscribers[SVM_FIFO_MAX_EVT_SUBSCRIBERS];
93
Florin Coras3eb50622017-07-13 01:24:57 -040094#if SVM_FIFO_TRACE
95 svm_fifo_trace_elem_t *trace;
96#endif
Florin Coras72b04282019-01-14 17:23:11 -080097
Dave Barach68b0fb02017-02-28 15:15:56 -050098 CLIB_CACHE_LINE_ALIGN_MARK (data);
99} svm_fifo_t;
100
Florin Coras7fb0fe12018-04-09 09:24:52 -0700101typedef enum
102{
103 SVM_FIFO_FULL = -2,
104} svm_fifo_err_t;
105
Florin Coras2cba8532018-09-11 16:33:36 -0700106typedef struct svm_fifo_segment_
107{
108 u8 *data;
109 u32 len;
110} svm_fifo_segment_t;
111
Florin Coras3eb50622017-07-13 01:24:57 -0400112#if SVM_FIFO_TRACE
113#define svm_fifo_trace_add(_f, _s, _l, _t) \
114{ \
115 svm_fifo_trace_elem_t *trace_elt; \
116 vec_add2(_f->trace, trace_elt, 1); \
117 trace_elt->offset = _s; \
118 trace_elt->len = _l; \
119 trace_elt->action = _t; \
120}
121#else
122#define svm_fifo_trace_add(_f, _s, _l, _t)
123#endif
124
125u8 *svm_fifo_dump_trace (u8 * s, svm_fifo_t * f);
126u8 *svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose);
127
Dave Barach68b0fb02017-02-28 15:15:56 -0500128static inline u32
129svm_fifo_max_dequeue (svm_fifo_t * f)
130{
Sirshak Das19515ac2018-11-07 18:46:42 -0600131 return clib_atomic_load_acq_n (&f->cursize);
Dave Barach68b0fb02017-02-28 15:15:56 -0500132}
133
Florin Coras3c2fed52018-07-04 04:15:05 -0700134static inline int
135svm_fifo_is_full (svm_fifo_t * f)
136{
Sirshak Das19515ac2018-11-07 18:46:42 -0600137 return (clib_atomic_load_acq_n (&f->cursize) == f->nitems);
Florin Coras3c2fed52018-07-04 04:15:05 -0700138}
139
Florin Coras84099552018-07-22 12:59:30 -0700140static inline int
141svm_fifo_is_empty (svm_fifo_t * f)
142{
Sirshak Das19515ac2018-11-07 18:46:42 -0600143 return (clib_atomic_load_acq_n (&f->cursize) == 0);
Florin Coras84099552018-07-22 12:59:30 -0700144}
145
Dave Barach68b0fb02017-02-28 15:15:56 -0500146static inline u32
147svm_fifo_max_enqueue (svm_fifo_t * f)
148{
Florin Coras3e350af2017-03-30 02:54:28 -0700149 return f->nitems - svm_fifo_max_dequeue (f);
Dave Barach68b0fb02017-02-28 15:15:56 -0500150}
151
Florin Coras46f001d2018-07-11 05:25:06 -0700152static inline int
153svm_fifo_has_event (svm_fifo_t * f)
154{
155 return f->has_event;
156}
157
Dave Barach68b0fb02017-02-28 15:15:56 -0500158static inline u8
159svm_fifo_has_ooo_data (svm_fifo_t * f)
160{
161 return f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX;
162}
163
Florin Coras6792ec02017-03-13 03:49:51 -0700164/**
165 * Sets fifo event flag.
166 *
Florin Coras41c9e042018-09-11 00:10:41 -0700167 * Also acts as a release barrier.
168 *
Florin Coras6792ec02017-03-13 03:49:51 -0700169 * @return 1 if flag was not set.
170 */
171always_inline u8
172svm_fifo_set_event (svm_fifo_t * f)
173{
Florin Coras2cba8532018-09-11 16:33:36 -0700174 /* return __sync_lock_test_and_set (&f->has_event, 1) == 0;
175 return __sync_bool_compare_and_swap (&f->has_event, 0, 1); */
Florin Coras41c9e042018-09-11 00:10:41 -0700176 return !__atomic_exchange_n (&f->has_event, 1, __ATOMIC_RELEASE);
Florin Coras6792ec02017-03-13 03:49:51 -0700177}
178
179/**
180 * Unsets fifo event flag.
Florin Coras41c9e042018-09-11 00:10:41 -0700181 *
182 * Also acts as a release barrier.
Florin Coras6792ec02017-03-13 03:49:51 -0700183 */
184always_inline void
185svm_fifo_unset_event (svm_fifo_t * f)
186{
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000187 clib_atomic_release (&f->has_event);
Florin Coras6792ec02017-03-13 03:49:51 -0700188}
189
Dave Barach68b0fb02017-02-28 15:15:56 -0500190svm_fifo_t *svm_fifo_create (u32 data_size_in_bytes);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700191void svm_fifo_free (svm_fifo_t * f);
Dave Barach68b0fb02017-02-28 15:15:56 -0500192
Florin Corasa5464812017-04-19 13:00:05 -0700193int svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 max_bytes,
Florin Coras371ca502018-02-21 12:07:41 -0800194 const u8 * copy_from_here);
Florin Corasa5464812017-04-19 13:00:05 -0700195int svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset,
196 u32 required_bytes, u8 * copy_from_here);
197int svm_fifo_dequeue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_here);
Dave Barach68b0fb02017-02-28 15:15:56 -0500198
Florin Corasa5464812017-04-19 13:00:05 -0700199int svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 max_bytes, u8 * copy_here);
200int svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes);
Florin Coras25579b42018-06-06 17:55:02 -0700201void svm_fifo_dequeue_drop_all (svm_fifo_t * f);
Florin Coras2cba8532018-09-11 16:33:36 -0700202int svm_fifo_segments (svm_fifo_t * f, svm_fifo_segment_t * fs);
203void svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_segment_t * fs);
Florin Corasc28764f2017-04-26 00:08:42 -0700204void svm_fifo_init_pointers (svm_fifo_t * f, u32 pointer);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700205void svm_fifo_overwrite_head (svm_fifo_t * f, u8 * data, u32 len);
Florin Coras72b04282019-01-14 17:23:11 -0800206void svm_fifo_add_subscriber (svm_fifo_t * f, u8 subscriber);
207void svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700208format_function_t format_svm_fifo;
209
Florin Coras58d36f02018-03-09 13:05:53 -0800210/**
211 * Max contiguous chunk of data that can be read
212 */
213always_inline u32
214svm_fifo_max_read_chunk (svm_fifo_t * f)
215{
216 return ((f->tail > f->head) ? (f->tail - f->head) : (f->nitems - f->head));
217}
218
219/**
220 * Max contiguous chunk of data that can be written
221 */
222always_inline u32
223svm_fifo_max_write_chunk (svm_fifo_t * f)
224{
225 return ((f->tail >= f->head) ? (f->nitems - f->tail) : (f->head - f->tail));
226}
227
228/**
229 * Advance tail pointer
230 *
231 * Useful for moving tail pointer after external enqueue.
232 */
233always_inline void
234svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 bytes)
235{
236 ASSERT (bytes <= svm_fifo_max_enqueue (f));
237 f->tail = (f->tail + bytes) % f->nitems;
Florin Coras09649852019-02-20 23:13:43 -0800238 clib_atomic_fetch_add_rel (&f->cursize, bytes);
Florin Coras58d36f02018-03-09 13:05:53 -0800239}
240
241always_inline u8 *
242svm_fifo_head (svm_fifo_t * f)
243{
244 return (f->data + f->head);
245}
246
247always_inline u8 *
248svm_fifo_tail (svm_fifo_t * f)
249{
250 return (f->data + f->tail);
251}
252
Florin Corasf03a59a2017-06-09 21:07:32 -0700253always_inline u32
Florin Coras1bcad5c2019-01-09 20:04:38 -0800254svm_fifo_nitems (svm_fifo_t * f)
255{
256 return f->nitems;
257}
258
259static inline void
260svm_fifo_add_want_tx_ntf (svm_fifo_t * f, u8 ntf_type)
261{
262 f->want_tx_ntf |= ntf_type;
263}
264
265static inline void
266svm_fifo_del_want_tx_ntf (svm_fifo_t * f, u8 ntf_type)
267{
268 f->want_tx_ntf &= ~ntf_type;
269}
270
271static inline void
272svm_fifo_clear_tx_ntf (svm_fifo_t * f)
273{
274 /* Set the flag if want_tx_notif_if_full was the only ntf requested */
275 f->has_tx_ntf = f->want_tx_ntf == SVM_FIFO_WANT_TX_NOTIF_IF_FULL;
276 svm_fifo_del_want_tx_ntf (f, SVM_FIFO_WANT_TX_NOTIF);
277}
278
279static inline void
280svm_fifo_reset_tx_ntf (svm_fifo_t * f)
281{
282 f->has_tx_ntf = 0;
283}
284
285static inline u8
286svm_fifo_needs_tx_ntf (svm_fifo_t * f, u32 n_last_deq)
287{
288 u8 want_ntf = f->want_tx_ntf;
289
290 if (PREDICT_TRUE (want_ntf == SVM_FIFO_NO_TX_NOTIF))
291 return 0;
292 else if (want_ntf & SVM_FIFO_WANT_TX_NOTIF)
293 return 1;
294 else if (want_ntf & SVM_FIFO_WANT_TX_NOTIF_IF_FULL)
295 {
296 u32 max_deq = svm_fifo_max_dequeue (f);
297 u32 nitems = svm_fifo_nitems (f);
298 if (!f->has_tx_ntf && max_deq < nitems
299 && max_deq + n_last_deq >= nitems)
300 return 1;
301
302 return 0;
303 }
304 return 0;
305}
306
Florin Coras72b04282019-01-14 17:23:11 -0800307always_inline u8
308svm_fifo_n_subscribers (svm_fifo_t * f)
309{
310 return f->n_subscribers;
311}
312
Florin Coras1bcad5c2019-01-09 20:04:38 -0800313u32 svm_fifo_number_ooo_segments (svm_fifo_t * f);
314ooo_segment_t *svm_fifo_first_ooo_segment (svm_fifo_t * f);
315
316always_inline ooo_segment_t *
317svm_fifo_newest_ooo_segment (svm_fifo_t * f)
318{
319 if (f->ooos_newest == OOO_SEGMENT_INVALID_INDEX)
320 return 0;
321 return pool_elt_at_index (f->ooo_segments, f->ooos_newest);
322}
323
324always_inline void
325svm_fifo_newest_ooo_segment_reset (svm_fifo_t * f)
326{
327 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
328}
329
330always_inline u32
Dave Barach2c25a622017-06-26 11:35:07 -0400331ooo_segment_distance_from_tail (svm_fifo_t * f, u32 pos)
Florin Corasf03a59a2017-06-09 21:07:32 -0700332{
333 /* Ambiguous. Assumption is that ooo segments don't touch tail */
Dave Barach2c25a622017-06-26 11:35:07 -0400334 if (PREDICT_FALSE (pos == f->tail && f->tail == f->head))
Florin Corasf03a59a2017-06-09 21:07:32 -0700335 return f->nitems;
336
Dave Barach2c25a622017-06-26 11:35:07 -0400337 return (((f->nitems + pos) - f->tail) % f->nitems);
338}
339
340always_inline u32
341ooo_segment_distance_to_tail (svm_fifo_t * f, u32 pos)
342{
343 return (((f->nitems + f->tail) - pos) % f->nitems);
Dave Barach68b0fb02017-02-28 15:15:56 -0500344}
345
346always_inline u32
347ooo_segment_offset (svm_fifo_t * f, ooo_segment_t * s)
348{
Dave Barach2c25a622017-06-26 11:35:07 -0400349 return ooo_segment_distance_from_tail (f, s->start);
Dave Barach68b0fb02017-02-28 15:15:56 -0500350}
351
352always_inline u32
353ooo_segment_end_offset (svm_fifo_t * f, ooo_segment_t * s)
354{
Dave Barach2c25a622017-06-26 11:35:07 -0400355 return ooo_segment_distance_from_tail (f, s->start) + s->length;
Florin Corasf03a59a2017-06-09 21:07:32 -0700356}
357
358always_inline u32
359ooo_segment_length (svm_fifo_t * f, ooo_segment_t * s)
360{
361 return s->length;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400362}
363
364always_inline ooo_segment_t *
365ooo_segment_get_prev (svm_fifo_t * f, ooo_segment_t * s)
366{
367 if (s->prev == OOO_SEGMENT_INVALID_INDEX)
368 return 0;
369 return pool_elt_at_index (f->ooo_segments, s->prev);
Dave Barach68b0fb02017-02-28 15:15:56 -0500370}
371
Florin Coras3eb50622017-07-13 01:24:57 -0400372always_inline ooo_segment_t *
373ooo_segment_next (svm_fifo_t * f, ooo_segment_t * s)
374{
375 if (s->next == OOO_SEGMENT_INVALID_INDEX)
376 return 0;
377 return pool_elt_at_index (f->ooo_segments, s->next);
378}
379
Dave Barach68b0fb02017-02-28 15:15:56 -0500380#endif /* __included_ssvm_fifo_h__ */
381
382/*
383 * fd.io coding-style-patch-verification: ON
384 *
385 * Local Variables:
386 * eval: (c-set-style "gnu")
387 * End:
388 */