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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #ifndef __included_ssvm_fifo_h__
16 #define __included_ssvm_fifo_h__
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>
26 /** Out-of-order segment */
29 u32 next; /**< Next linked-list element pool index */
30 u32 prev; /**< Previous linked-list element pool index */
32 u32 start; /**< Start of segment, normalized*/
33 u32 length; /**< Length of segment */
36 format_function_t format_ooo_segment;
37 format_function_t format_ooo_list;
39 #define SVM_FIFO_TRACE (0)
40 #define OOO_SEGMENT_INVALID_INDEX ((u32)~0)
41 #define SVM_FIFO_INVALID_SESSION_INDEX ((u32)~0)
48 } svm_fifo_trace_elem_t;
50 typedef struct _svm_fifo
52 volatile u32 cursize; /**< current fifo size */
54 CLIB_CACHE_LINE_ALIGN_MARK (end_cursize);
56 volatile u32 has_event; /**< non-zero if deq event exists */
59 u32 master_session_index;
60 u32 client_session_index;
61 u8 master_thread_index;
62 u8 client_thread_index;
64 u32 ct_session_index; /**< Local session index for vpp */
65 CLIB_CACHE_LINE_ALIGN_MARK (end_shared);
67 volatile u32 want_tx_evt; /**< producer wants nudge */
68 CLIB_CACHE_LINE_ALIGN_MARK (end_consumer);
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 */
76 struct _svm_fifo *next; /**< next in freelist/active chain */
77 struct _svm_fifo *prev; /**< prev in active chain */
79 svm_fifo_trace_elem_t *trace;
81 u32 freelist_index; /**< aka log2(allocated_size) - const. */
82 i8 refcnt; /**< reference count */
83 CLIB_CACHE_LINE_ALIGN_MARK (data);
91 typedef struct svm_fifo_segment_
98 #define svm_fifo_trace_add(_f, _s, _l, _t) \
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; \
107 #define svm_fifo_trace_add(_f, _s, _l, _t)
110 u8 *svm_fifo_dump_trace (u8 * s, svm_fifo_t * f);
111 u8 *svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose);
114 svm_fifo_max_dequeue (svm_fifo_t * f)
120 svm_fifo_is_full (svm_fifo_t * f)
122 return (f->cursize == f->nitems);
126 svm_fifo_is_empty (svm_fifo_t * f)
128 return (f->cursize == 0);
132 svm_fifo_max_enqueue (svm_fifo_t * f)
134 return f->nitems - svm_fifo_max_dequeue (f);
138 svm_fifo_has_event (svm_fifo_t * f)
144 svm_fifo_has_ooo_data (svm_fifo_t * f)
146 return f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX;
150 * Sets fifo event flag.
152 * Also acts as a release barrier.
154 * @return 1 if flag was not set.
157 svm_fifo_set_event (svm_fifo_t * f)
159 /* return __sync_lock_test_and_set (&f->has_event, 1) == 0;
160 return __sync_bool_compare_and_swap (&f->has_event, 0, 1); */
161 return !__atomic_exchange_n (&f->has_event, 1, __ATOMIC_RELEASE);
165 * Unsets fifo event flag.
167 * Also acts as a release barrier.
170 svm_fifo_unset_event (svm_fifo_t * f)
172 __sync_lock_release (&f->has_event);
176 svm_fifo_set_want_tx_evt (svm_fifo_t * f, u8 want_evt)
178 f->want_tx_evt = want_evt;
182 svm_fifo_want_tx_evt (svm_fifo_t * f)
184 return f->want_tx_evt;
187 svm_fifo_t *svm_fifo_create (u32 data_size_in_bytes);
188 void svm_fifo_free (svm_fifo_t * f);
190 int svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 max_bytes,
191 const u8 * copy_from_here);
192 int svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset,
193 u32 required_bytes, u8 * copy_from_here);
194 int svm_fifo_dequeue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_here);
196 int svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 max_bytes, u8 * copy_here);
197 int svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes);
198 void svm_fifo_dequeue_drop_all (svm_fifo_t * f);
199 int svm_fifo_segments (svm_fifo_t * f, svm_fifo_segment_t * fs);
200 void svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_segment_t * fs);
201 u32 svm_fifo_number_ooo_segments (svm_fifo_t * f);
202 ooo_segment_t *svm_fifo_first_ooo_segment (svm_fifo_t * f);
203 void svm_fifo_init_pointers (svm_fifo_t * f, u32 pointer);
204 void svm_fifo_overwrite_head (svm_fifo_t * f, u8 * data, u32 len);
206 format_function_t format_svm_fifo;
208 always_inline ooo_segment_t *
209 svm_fifo_newest_ooo_segment (svm_fifo_t * f)
211 if (f->ooos_newest == OOO_SEGMENT_INVALID_INDEX)
213 return pool_elt_at_index (f->ooo_segments, f->ooos_newest);
217 svm_fifo_newest_ooo_segment_reset (svm_fifo_t * f)
219 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
223 * Max contiguous chunk of data that can be read
226 svm_fifo_max_read_chunk (svm_fifo_t * f)
228 return ((f->tail > f->head) ? (f->tail - f->head) : (f->nitems - f->head));
232 * Max contiguous chunk of data that can be written
235 svm_fifo_max_write_chunk (svm_fifo_t * f)
237 return ((f->tail >= f->head) ? (f->nitems - f->tail) : (f->head - f->tail));
241 * Advance tail pointer
243 * Useful for moving tail pointer after external enqueue.
246 svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 bytes)
248 ASSERT (bytes <= svm_fifo_max_enqueue (f));
249 f->tail = (f->tail + bytes) % f->nitems;
254 svm_fifo_head (svm_fifo_t * f)
256 return (f->data + f->head);
260 svm_fifo_tail (svm_fifo_t * f)
262 return (f->data + f->tail);
266 ooo_segment_distance_from_tail (svm_fifo_t * f, u32 pos)
268 /* Ambiguous. Assumption is that ooo segments don't touch tail */
269 if (PREDICT_FALSE (pos == f->tail && f->tail == f->head))
272 return (((f->nitems + pos) - f->tail) % f->nitems);
276 ooo_segment_distance_to_tail (svm_fifo_t * f, u32 pos)
278 return (((f->nitems + f->tail) - pos) % f->nitems);
282 ooo_segment_offset (svm_fifo_t * f, ooo_segment_t * s)
284 return ooo_segment_distance_from_tail (f, s->start);
288 ooo_segment_end_offset (svm_fifo_t * f, ooo_segment_t * s)
290 return ooo_segment_distance_from_tail (f, s->start) + s->length;
294 ooo_segment_length (svm_fifo_t * f, ooo_segment_t * s)
299 always_inline ooo_segment_t *
300 ooo_segment_get_prev (svm_fifo_t * f, ooo_segment_t * s)
302 if (s->prev == OOO_SEGMENT_INVALID_INDEX)
304 return pool_elt_at_index (f->ooo_segments, s->prev);
307 always_inline ooo_segment_t *
308 ooo_segment_next (svm_fifo_t * f, ooo_segment_t * s)
310 if (s->next == OOO_SEGMENT_INVALID_INDEX)
312 return pool_elt_at_index (f->ooo_segments, s->next);
315 #endif /* __included_ssvm_fifo_h__ */
318 * fd.io coding-style-patch-verification: ON
321 * eval: (c-set-style "gnu")