blob: 791b513a4a61419088b5aad5048d45649c109486 [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)
Dave Barach68b0fb02017-02-28 15:15:56 -050042
Florin Coras3eb50622017-07-13 01:24:57 -040043typedef struct
44{
45 u32 offset;
46 u32 len;
47 u32 action;
48} svm_fifo_trace_elem_t;
49
Dave Barach10d8cc62017-05-30 09:30:07 -040050typedef struct _svm_fifo
Dave Barach68b0fb02017-02-28 15:15:56 -050051{
Florin Coras3e350af2017-03-30 02:54:28 -070052 volatile u32 cursize; /**< current fifo size */
53 u32 nitems;
54 CLIB_CACHE_LINE_ALIGN_MARK (end_cursize);
55
Dave Barachacd2a6a2017-05-16 17:41:34 -040056 volatile u32 has_event; /**< non-zero if deq event exists */
Dave Barach68b0fb02017-02-28 15:15:56 -050057
58 /* Backpointers */
Florin Corasa5464812017-04-19 13:00:05 -070059 u32 master_session_index;
Dave Barach68b0fb02017-02-28 15:15:56 -050060 u32 client_session_index;
Florin Corasa5464812017-04-19 13:00:05 -070061 u8 master_thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -050062 u8 client_thread_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -070063 u32 segment_manager;
Florin Coras326b81e2018-10-04 19:03:05 -070064 u32 ct_session_index; /**< Local session index for vpp */
Dave Barach68b0fb02017-02-28 15:15:56 -050065 CLIB_CACHE_LINE_ALIGN_MARK (end_shared);
66 u32 head;
Florin Coras0e88e852018-09-17 22:09:02 -070067 volatile u32 want_tx_evt; /**< producer wants nudge */
Dave Barach68b0fb02017-02-28 15:15:56 -050068 CLIB_CACHE_LINE_ALIGN_MARK (end_consumer);
69
70 /* producer */
71 u32 tail;
72
73 ooo_segment_t *ooo_segments; /**< Pool of ooo segments */
74 u32 ooos_list_head; /**< Head of out-of-order linked-list */
75 u32 ooos_newest; /**< Last segment to have been updated */
Dave Barach10d8cc62017-05-30 09:30:07 -040076 struct _svm_fifo *next; /**< next in freelist/active chain */
77 struct _svm_fifo *prev; /**< prev in active chain */
Florin Coras3eb50622017-07-13 01:24:57 -040078#if SVM_FIFO_TRACE
79 svm_fifo_trace_elem_t *trace;
80#endif
Dave Barach818eb542017-08-02 13:56:13 -040081 u32 freelist_index; /**< aka log2(allocated_size) - const. */
82 i8 refcnt; /**< reference count */
Dave Barach68b0fb02017-02-28 15:15:56 -050083 CLIB_CACHE_LINE_ALIGN_MARK (data);
84} svm_fifo_t;
85
Florin Coras7fb0fe12018-04-09 09:24:52 -070086typedef enum
87{
88 SVM_FIFO_FULL = -2,
89} svm_fifo_err_t;
90
Florin Coras2cba8532018-09-11 16:33:36 -070091typedef struct svm_fifo_segment_
92{
93 u8 *data;
94 u32 len;
95} svm_fifo_segment_t;
96
Florin Coras3eb50622017-07-13 01:24:57 -040097#if SVM_FIFO_TRACE
98#define svm_fifo_trace_add(_f, _s, _l, _t) \
99{ \
100 svm_fifo_trace_elem_t *trace_elt; \
101 vec_add2(_f->trace, trace_elt, 1); \
102 trace_elt->offset = _s; \
103 trace_elt->len = _l; \
104 trace_elt->action = _t; \
105}
106#else
107#define svm_fifo_trace_add(_f, _s, _l, _t)
108#endif
109
110u8 *svm_fifo_dump_trace (u8 * s, svm_fifo_t * f);
111u8 *svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose);
112
Dave Barach68b0fb02017-02-28 15:15:56 -0500113static inline u32
114svm_fifo_max_dequeue (svm_fifo_t * f)
115{
Sirshak Das19515ac2018-11-07 18:46:42 -0600116 return clib_atomic_load_acq_n (&f->cursize);
Dave Barach68b0fb02017-02-28 15:15:56 -0500117}
118
Florin Coras3c2fed52018-07-04 04:15:05 -0700119static inline int
120svm_fifo_is_full (svm_fifo_t * f)
121{
Sirshak Das19515ac2018-11-07 18:46:42 -0600122 return (clib_atomic_load_acq_n (&f->cursize) == f->nitems);
Florin Coras3c2fed52018-07-04 04:15:05 -0700123}
124
Florin Coras84099552018-07-22 12:59:30 -0700125static inline int
126svm_fifo_is_empty (svm_fifo_t * f)
127{
Sirshak Das19515ac2018-11-07 18:46:42 -0600128 return (clib_atomic_load_acq_n (&f->cursize) == 0);
Florin Coras84099552018-07-22 12:59:30 -0700129}
130
Dave Barach68b0fb02017-02-28 15:15:56 -0500131static inline u32
132svm_fifo_max_enqueue (svm_fifo_t * f)
133{
Florin Coras3e350af2017-03-30 02:54:28 -0700134 return f->nitems - svm_fifo_max_dequeue (f);
Dave Barach68b0fb02017-02-28 15:15:56 -0500135}
136
Florin Coras46f001d2018-07-11 05:25:06 -0700137static inline int
138svm_fifo_has_event (svm_fifo_t * f)
139{
140 return f->has_event;
141}
142
Dave Barach68b0fb02017-02-28 15:15:56 -0500143static inline u8
144svm_fifo_has_ooo_data (svm_fifo_t * f)
145{
146 return f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX;
147}
148
Florin Coras6792ec02017-03-13 03:49:51 -0700149/**
150 * Sets fifo event flag.
151 *
Florin Coras41c9e042018-09-11 00:10:41 -0700152 * Also acts as a release barrier.
153 *
Florin Coras6792ec02017-03-13 03:49:51 -0700154 * @return 1 if flag was not set.
155 */
156always_inline u8
157svm_fifo_set_event (svm_fifo_t * f)
158{
Florin Coras2cba8532018-09-11 16:33:36 -0700159 /* return __sync_lock_test_and_set (&f->has_event, 1) == 0;
160 return __sync_bool_compare_and_swap (&f->has_event, 0, 1); */
Florin Coras41c9e042018-09-11 00:10:41 -0700161 return !__atomic_exchange_n (&f->has_event, 1, __ATOMIC_RELEASE);
Florin Coras6792ec02017-03-13 03:49:51 -0700162}
163
164/**
165 * Unsets fifo event flag.
Florin Coras41c9e042018-09-11 00:10:41 -0700166 *
167 * Also acts as a release barrier.
Florin Coras6792ec02017-03-13 03:49:51 -0700168 */
169always_inline void
170svm_fifo_unset_event (svm_fifo_t * f)
171{
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000172 clib_atomic_release (&f->has_event);
Florin Coras6792ec02017-03-13 03:49:51 -0700173}
174
Florin Coras0e88e852018-09-17 22:09:02 -0700175static inline void
176svm_fifo_set_want_tx_evt (svm_fifo_t * f, u8 want_evt)
177{
178 f->want_tx_evt = want_evt;
179}
180
181static inline u8
182svm_fifo_want_tx_evt (svm_fifo_t * f)
183{
184 return f->want_tx_evt;
185}
186
Dave Barach68b0fb02017-02-28 15:15:56 -0500187svm_fifo_t *svm_fifo_create (u32 data_size_in_bytes);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700188void svm_fifo_free (svm_fifo_t * f);
Dave Barach68b0fb02017-02-28 15:15:56 -0500189
Florin Corasa5464812017-04-19 13:00:05 -0700190int svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 max_bytes,
Florin Coras371ca502018-02-21 12:07:41 -0800191 const u8 * copy_from_here);
Florin Corasa5464812017-04-19 13:00:05 -0700192int svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset,
193 u32 required_bytes, u8 * copy_from_here);
194int svm_fifo_dequeue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_here);
Dave Barach68b0fb02017-02-28 15:15:56 -0500195
Florin Corasa5464812017-04-19 13:00:05 -0700196int svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 max_bytes, u8 * copy_here);
197int svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes);
Florin Coras25579b42018-06-06 17:55:02 -0700198void svm_fifo_dequeue_drop_all (svm_fifo_t * f);
Florin Coras2cba8532018-09-11 16:33:36 -0700199int svm_fifo_segments (svm_fifo_t * f, svm_fifo_segment_t * fs);
200void svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_segment_t * fs);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400201u32 svm_fifo_number_ooo_segments (svm_fifo_t * f);
202ooo_segment_t *svm_fifo_first_ooo_segment (svm_fifo_t * f);
Florin Corasc28764f2017-04-26 00:08:42 -0700203void svm_fifo_init_pointers (svm_fifo_t * f, u32 pointer);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700204void svm_fifo_overwrite_head (svm_fifo_t * f, u8 * data, u32 len);
Dave Barach68b0fb02017-02-28 15:15:56 -0500205
Florin Coras6cf30ad2017-04-04 23:08:23 -0700206format_function_t format_svm_fifo;
207
Dave Barach68b0fb02017-02-28 15:15:56 -0500208always_inline ooo_segment_t *
209svm_fifo_newest_ooo_segment (svm_fifo_t * f)
210{
Florin Corasf03a59a2017-06-09 21:07:32 -0700211 if (f->ooos_newest == OOO_SEGMENT_INVALID_INDEX)
212 return 0;
213 return pool_elt_at_index (f->ooo_segments, f->ooos_newest);
214}
215
Florin Coras3eb50622017-07-13 01:24:57 -0400216always_inline void
217svm_fifo_newest_ooo_segment_reset (svm_fifo_t * f)
218{
219 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
220}
221
Florin Coras58d36f02018-03-09 13:05:53 -0800222/**
223 * Max contiguous chunk of data that can be read
224 */
225always_inline u32
226svm_fifo_max_read_chunk (svm_fifo_t * f)
227{
228 return ((f->tail > f->head) ? (f->tail - f->head) : (f->nitems - f->head));
229}
230
231/**
232 * Max contiguous chunk of data that can be written
233 */
234always_inline u32
235svm_fifo_max_write_chunk (svm_fifo_t * f)
236{
237 return ((f->tail >= f->head) ? (f->nitems - f->tail) : (f->head - f->tail));
238}
239
240/**
241 * Advance tail pointer
242 *
243 * Useful for moving tail pointer after external enqueue.
244 */
245always_inline void
246svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 bytes)
247{
248 ASSERT (bytes <= svm_fifo_max_enqueue (f));
249 f->tail = (f->tail + bytes) % f->nitems;
250 f->cursize += bytes;
251}
252
253always_inline u8 *
254svm_fifo_head (svm_fifo_t * f)
255{
256 return (f->data + f->head);
257}
258
259always_inline u8 *
260svm_fifo_tail (svm_fifo_t * f)
261{
262 return (f->data + f->tail);
263}
264
Florin Corasf03a59a2017-06-09 21:07:32 -0700265always_inline u32
Dave Barach2c25a622017-06-26 11:35:07 -0400266ooo_segment_distance_from_tail (svm_fifo_t * f, u32 pos)
Florin Corasf03a59a2017-06-09 21:07:32 -0700267{
268 /* Ambiguous. Assumption is that ooo segments don't touch tail */
Dave Barach2c25a622017-06-26 11:35:07 -0400269 if (PREDICT_FALSE (pos == f->tail && f->tail == f->head))
Florin Corasf03a59a2017-06-09 21:07:32 -0700270 return f->nitems;
271
Dave Barach2c25a622017-06-26 11:35:07 -0400272 return (((f->nitems + pos) - f->tail) % f->nitems);
273}
274
275always_inline u32
276ooo_segment_distance_to_tail (svm_fifo_t * f, u32 pos)
277{
278 return (((f->nitems + f->tail) - pos) % f->nitems);
Dave Barach68b0fb02017-02-28 15:15:56 -0500279}
280
281always_inline u32
282ooo_segment_offset (svm_fifo_t * f, ooo_segment_t * s)
283{
Dave Barach2c25a622017-06-26 11:35:07 -0400284 return ooo_segment_distance_from_tail (f, s->start);
Dave Barach68b0fb02017-02-28 15:15:56 -0500285}
286
287always_inline u32
288ooo_segment_end_offset (svm_fifo_t * f, ooo_segment_t * s)
289{
Dave Barach2c25a622017-06-26 11:35:07 -0400290 return ooo_segment_distance_from_tail (f, s->start) + s->length;
Florin Corasf03a59a2017-06-09 21:07:32 -0700291}
292
293always_inline u32
294ooo_segment_length (svm_fifo_t * f, ooo_segment_t * s)
295{
296 return s->length;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400297}
298
299always_inline ooo_segment_t *
300ooo_segment_get_prev (svm_fifo_t * f, ooo_segment_t * s)
301{
302 if (s->prev == OOO_SEGMENT_INVALID_INDEX)
303 return 0;
304 return pool_elt_at_index (f->ooo_segments, s->prev);
Dave Barach68b0fb02017-02-28 15:15:56 -0500305}
306
Florin Coras3eb50622017-07-13 01:24:57 -0400307always_inline ooo_segment_t *
308ooo_segment_next (svm_fifo_t * f, ooo_segment_t * s)
309{
310 if (s->next == OOO_SEGMENT_INVALID_INDEX)
311 return 0;
312 return pool_elt_at_index (f->ooo_segments, s->next);
313}
314
Dave Barach68b0fb02017-02-28 15:15:56 -0500315#endif /* __included_ssvm_fifo_h__ */
316
317/*
318 * fd.io coding-style-patch-verification: ON
319 *
320 * Local Variables:
321 * eval: (c-set-style "gnu")
322 * End:
323 */