vcl: improve read and fifo event handling
[vpp.git] / src / svm / svm_fifo.h
1 /*
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
26 /** Out-of-order segment */
27 typedef struct
28 {
29   u32 next;     /**< Next linked-list element pool index */
30   u32 prev;     /**< Previous linked-list element pool index */
31
32   u32 start;    /**< Start of segment, normalized*/
33   u32 length;   /**< Length of segment */
34 } ooo_segment_t;
35
36 format_function_t format_ooo_segment;
37 format_function_t format_ooo_list;
38
39 #define SVM_FIFO_TRACE (0)
40 #define OOO_SEGMENT_INVALID_INDEX ((u32)~0)
41
42 typedef struct
43 {
44   u32 offset;
45   u32 len;
46   u32 action;
47 } svm_fifo_trace_elem_t;
48
49 typedef struct _svm_fifo
50 {
51   volatile u32 cursize;         /**< current fifo size */
52   u32 nitems;
53     CLIB_CACHE_LINE_ALIGN_MARK (end_cursize);
54
55   volatile u32 has_event;       /**< non-zero if deq event exists */
56
57   /* Backpointers */
58   u32 master_session_index;
59   u32 client_session_index;
60   u8 master_thread_index;
61   u8 client_thread_index;
62   u32 segment_manager;
63     CLIB_CACHE_LINE_ALIGN_MARK (end_shared);
64   u32 head;
65     CLIB_CACHE_LINE_ALIGN_MARK (end_consumer);
66
67   /* producer */
68   u32 tail;
69
70   ooo_segment_t *ooo_segments;  /**< Pool of ooo segments */
71   u32 ooos_list_head;           /**< Head of out-of-order linked-list */
72   u32 ooos_newest;              /**< Last segment to have been updated */
73   struct _svm_fifo *next;       /**< next in freelist/active chain */
74   struct _svm_fifo *prev;       /**< prev in active chain */
75 #if SVM_FIFO_TRACE
76   svm_fifo_trace_elem_t *trace;
77 #endif
78   u32 freelist_index;           /**< aka log2(allocated_size) - const. */
79   i8 refcnt;                    /**< reference count  */
80     CLIB_CACHE_LINE_ALIGN_MARK (data);
81 } svm_fifo_t;
82
83 typedef enum
84 {
85   SVM_FIFO_FULL = -2,
86 } svm_fifo_err_t;
87
88 #if SVM_FIFO_TRACE
89 #define svm_fifo_trace_add(_f, _s, _l, _t)              \
90 {                                                       \
91   svm_fifo_trace_elem_t *trace_elt;                     \
92   vec_add2(_f->trace, trace_elt, 1);                    \
93   trace_elt->offset = _s;                               \
94   trace_elt->len = _l;                                  \
95   trace_elt->action = _t;                               \
96 }
97 #else
98 #define svm_fifo_trace_add(_f, _s, _l, _t)
99 #endif
100
101 u8 *svm_fifo_dump_trace (u8 * s, svm_fifo_t * f);
102 u8 *svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose);
103
104 static inline u32
105 svm_fifo_max_dequeue (svm_fifo_t * f)
106 {
107   return f->cursize;
108 }
109
110 static inline int
111 svm_fifo_is_full (svm_fifo_t * f)
112 {
113   return (f->cursize == f->nitems);
114 }
115
116 static inline int
117 svm_fifo_is_empty (svm_fifo_t * f)
118 {
119   return (f->cursize == 0);
120 }
121
122 static inline u32
123 svm_fifo_max_enqueue (svm_fifo_t * f)
124 {
125   return f->nitems - svm_fifo_max_dequeue (f);
126 }
127
128 static inline int
129 svm_fifo_has_event (svm_fifo_t * f)
130 {
131   return f->has_event;
132 }
133
134 static inline u8
135 svm_fifo_has_ooo_data (svm_fifo_t * f)
136 {
137   return f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX;
138 }
139
140 /**
141  * Sets fifo event flag.
142  *
143  * Also acts as a release barrier.
144  *
145  * @return 1 if flag was not set.
146  */
147 always_inline u8
148 svm_fifo_set_event (svm_fifo_t * f)
149 {
150 //  return __sync_lock_test_and_set (&f->has_event, 1) == 0;
151 //  return __sync_bool_compare_and_swap (&f->has_event, 0, 1);
152   return !__atomic_exchange_n (&f->has_event, 1, __ATOMIC_RELEASE);
153 }
154
155 /**
156  * Unsets fifo event flag.
157  *
158  * Also acts as a release barrier.
159  */
160 always_inline void
161 svm_fifo_unset_event (svm_fifo_t * f)
162 {
163   __sync_lock_release (&f->has_event);
164 }
165
166 svm_fifo_t *svm_fifo_create (u32 data_size_in_bytes);
167 void svm_fifo_free (svm_fifo_t * f);
168
169 int svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 max_bytes,
170                              const u8 * copy_from_here);
171 int svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset,
172                                   u32 required_bytes, u8 * copy_from_here);
173 int svm_fifo_dequeue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_here);
174
175 int svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 max_bytes, u8 * copy_here);
176 int svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes);
177 void svm_fifo_dequeue_drop_all (svm_fifo_t * f);
178 u32 svm_fifo_number_ooo_segments (svm_fifo_t * f);
179 ooo_segment_t *svm_fifo_first_ooo_segment (svm_fifo_t * f);
180 void svm_fifo_init_pointers (svm_fifo_t * f, u32 pointer);
181 void svm_fifo_overwrite_head (svm_fifo_t * f, u8 * data, u32 len);
182
183 format_function_t format_svm_fifo;
184
185 always_inline ooo_segment_t *
186 svm_fifo_newest_ooo_segment (svm_fifo_t * f)
187 {
188   if (f->ooos_newest == OOO_SEGMENT_INVALID_INDEX)
189     return 0;
190   return pool_elt_at_index (f->ooo_segments, f->ooos_newest);
191 }
192
193 always_inline void
194 svm_fifo_newest_ooo_segment_reset (svm_fifo_t * f)
195 {
196   f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
197 }
198
199 /**
200  * Max contiguous chunk of data that can be read
201  */
202 always_inline u32
203 svm_fifo_max_read_chunk (svm_fifo_t * f)
204 {
205   return ((f->tail > f->head) ? (f->tail - f->head) : (f->nitems - f->head));
206 }
207
208 /**
209  * Max contiguous chunk of data that can be written
210  */
211 always_inline u32
212 svm_fifo_max_write_chunk (svm_fifo_t * f)
213 {
214   return ((f->tail >= f->head) ? (f->nitems - f->tail) : (f->head - f->tail));
215 }
216
217 /**
218  * Advance tail pointer
219  *
220  * Useful for moving tail pointer after external enqueue.
221  */
222 always_inline void
223 svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 bytes)
224 {
225   ASSERT (bytes <= svm_fifo_max_enqueue (f));
226   f->tail = (f->tail + bytes) % f->nitems;
227   f->cursize += bytes;
228 }
229
230 always_inline u8 *
231 svm_fifo_head (svm_fifo_t * f)
232 {
233   return (f->data + f->head);
234 }
235
236 always_inline u8 *
237 svm_fifo_tail (svm_fifo_t * f)
238 {
239   return (f->data + f->tail);
240 }
241
242 always_inline u32
243 ooo_segment_distance_from_tail (svm_fifo_t * f, u32 pos)
244 {
245   /* Ambiguous. Assumption is that ooo segments don't touch tail */
246   if (PREDICT_FALSE (pos == f->tail && f->tail == f->head))
247     return f->nitems;
248
249   return (((f->nitems + pos) - f->tail) % f->nitems);
250 }
251
252 always_inline u32
253 ooo_segment_distance_to_tail (svm_fifo_t * f, u32 pos)
254 {
255   return (((f->nitems + f->tail) - pos) % f->nitems);
256 }
257
258 always_inline u32
259 ooo_segment_offset (svm_fifo_t * f, ooo_segment_t * s)
260 {
261   return ooo_segment_distance_from_tail (f, s->start);
262 }
263
264 always_inline u32
265 ooo_segment_end_offset (svm_fifo_t * f, ooo_segment_t * s)
266 {
267   return ooo_segment_distance_from_tail (f, s->start) + s->length;
268 }
269
270 always_inline u32
271 ooo_segment_length (svm_fifo_t * f, ooo_segment_t * s)
272 {
273   return s->length;
274 }
275
276 always_inline ooo_segment_t *
277 ooo_segment_get_prev (svm_fifo_t * f, ooo_segment_t * s)
278 {
279   if (s->prev == OOO_SEGMENT_INVALID_INDEX)
280     return 0;
281   return pool_elt_at_index (f->ooo_segments, s->prev);
282 }
283
284 always_inline ooo_segment_t *
285 ooo_segment_next (svm_fifo_t * f, ooo_segment_t * s)
286 {
287   if (s->next == OOO_SEGMENT_INVALID_INDEX)
288     return 0;
289   return pool_elt_at_index (f->ooo_segments, s->next);
290 }
291
292 #endif /* __included_ssvm_fifo_h__ */
293
294 /*
295  * fd.io coding-style-patch-verification: ON
296  *
297  * Local Variables:
298  * eval: (c-set-style "gnu")
299  * End:
300  */