host stack: update stale copyright
[vpp.git] / src / svm / svm_fifo.h
1 /*
2  * Copyright (c) 2016-2019 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 #define SVM_FIFO_INVALID_SESSION_INDEX  ((u32)~0)
42 #define SVM_FIFO_INVALID_INDEX          ((u32)~0)
43 #define SVM_FIFO_MAX_EVT_SUBSCRIBERS    8
44
45 enum
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
52 typedef struct
53 {
54   u32 offset;
55   u32 len;
56   u32 action;
57 } svm_fifo_trace_elem_t;
58
59 typedef struct _svm_fifo
60 {
61   CLIB_CACHE_LINE_ALIGN_MARK (shared_first);
62   volatile u32 cursize;         /**< current fifo size */
63   u32 nitems;
64
65     CLIB_CACHE_LINE_ALIGN_MARK (shared_second);
66   volatile u32 has_event;       /**< non-zero if deq event exists */
67
68   u32 master_session_index;
69   u32 client_session_index;
70   u8 master_thread_index;
71   u8 client_thread_index;
72   u32 segment_manager;
73   u32 segment_index;
74   u32 ct_session_index;         /**< Local session index for vpp */
75   u32 freelist_index;           /**< aka log2(allocated_size) - const. */
76   i8 refcnt;                    /**< reference count  */
77
78     CLIB_CACHE_LINE_ALIGN_MARK (consumer);
79   u32 head;
80   volatile u32 want_tx_ntf;     /**< producer wants nudge */
81   volatile u32 has_tx_ntf;
82
83     CLIB_CACHE_LINE_ALIGN_MARK (producer);
84   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 */
89   struct _svm_fifo *next;       /**< next in freelist/active chain */
90   struct _svm_fifo *prev;       /**< prev in active chain */
91   volatile u8 n_subscribers;
92   u8 subscribers[SVM_FIFO_MAX_EVT_SUBSCRIBERS];
93
94 #if SVM_FIFO_TRACE
95   svm_fifo_trace_elem_t *trace;
96 #endif
97
98     CLIB_CACHE_LINE_ALIGN_MARK (data);
99 } svm_fifo_t;
100
101 typedef enum
102 {
103   SVM_FIFO_FULL = -2,
104 } svm_fifo_err_t;
105
106 typedef struct svm_fifo_segment_
107 {
108   u8 *data;
109   u32 len;
110 } svm_fifo_segment_t;
111
112 #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
125 u8 *svm_fifo_dump_trace (u8 * s, svm_fifo_t * f);
126 u8 *svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose);
127
128 static inline u32
129 svm_fifo_max_dequeue (svm_fifo_t * f)
130 {
131   return clib_atomic_load_acq_n (&f->cursize);
132 }
133
134 static inline int
135 svm_fifo_is_full (svm_fifo_t * f)
136 {
137   return (clib_atomic_load_acq_n (&f->cursize) == f->nitems);
138 }
139
140 static inline int
141 svm_fifo_is_empty (svm_fifo_t * f)
142 {
143   return (clib_atomic_load_acq_n (&f->cursize) == 0);
144 }
145
146 static inline u32
147 svm_fifo_max_enqueue (svm_fifo_t * f)
148 {
149   return f->nitems - svm_fifo_max_dequeue (f);
150 }
151
152 static inline int
153 svm_fifo_has_event (svm_fifo_t * f)
154 {
155   return f->has_event;
156 }
157
158 static inline u8
159 svm_fifo_has_ooo_data (svm_fifo_t * f)
160 {
161   return f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX;
162 }
163
164 /**
165  * Sets fifo event flag.
166  *
167  * Also acts as a release barrier.
168  *
169  * @return 1 if flag was not set.
170  */
171 always_inline u8
172 svm_fifo_set_event (svm_fifo_t * f)
173 {
174   /* return __sync_lock_test_and_set (&f->has_event, 1) == 0;
175      return __sync_bool_compare_and_swap (&f->has_event, 0, 1); */
176   return !clib_atomic_swap_rel_n (&f->has_event, 1);
177 }
178
179 /**
180  * Unsets fifo event flag.
181  *
182  * Also acts as an acquire barrier.
183  */
184 always_inline void
185 svm_fifo_unset_event (svm_fifo_t * f)
186 {
187   clib_atomic_swap_acq_n (&f->has_event, 0);
188 }
189
190 svm_fifo_t *svm_fifo_create (u32 data_size_in_bytes);
191 void svm_fifo_free (svm_fifo_t * f);
192
193 int svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 max_bytes,
194                              const u8 * copy_from_here);
195 int svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset,
196                                   u32 required_bytes, u8 * copy_from_here);
197 int svm_fifo_dequeue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_here);
198
199 int svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 max_bytes, u8 * copy_here);
200 int svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes);
201 void svm_fifo_dequeue_drop_all (svm_fifo_t * f);
202 int svm_fifo_segments (svm_fifo_t * f, svm_fifo_segment_t * fs);
203 void svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_segment_t * fs);
204 void svm_fifo_init_pointers (svm_fifo_t * f, u32 pointer);
205 void svm_fifo_overwrite_head (svm_fifo_t * f, u8 * data, u32 len);
206 void svm_fifo_add_subscriber (svm_fifo_t * f, u8 subscriber);
207 void svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber);
208 format_function_t format_svm_fifo;
209
210 /**
211  * Max contiguous chunk of data that can be read
212  */
213 always_inline u32
214 svm_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  */
222 always_inline u32
223 svm_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  */
233 always_inline void
234 svm_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;
238   clib_atomic_fetch_add_rel (&f->cursize, bytes);
239 }
240
241 always_inline u8 *
242 svm_fifo_head (svm_fifo_t * f)
243 {
244   return (f->data + f->head);
245 }
246
247 always_inline u8 *
248 svm_fifo_tail (svm_fifo_t * f)
249 {
250   return (f->data + f->tail);
251 }
252
253 always_inline u32
254 svm_fifo_nitems (svm_fifo_t * f)
255 {
256   return f->nitems;
257 }
258
259 static inline void
260 svm_fifo_add_want_tx_ntf (svm_fifo_t * f, u8 ntf_type)
261 {
262   f->want_tx_ntf |= ntf_type;
263 }
264
265 static inline void
266 svm_fifo_del_want_tx_ntf (svm_fifo_t * f, u8 ntf_type)
267 {
268   f->want_tx_ntf &= ~ntf_type;
269 }
270
271 static inline void
272 svm_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
279 static inline void
280 svm_fifo_reset_tx_ntf (svm_fifo_t * f)
281 {
282   f->has_tx_ntf = 0;
283 }
284
285 static inline u8
286 svm_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
307 always_inline u8
308 svm_fifo_n_subscribers (svm_fifo_t * f)
309 {
310   return f->n_subscribers;
311 }
312
313 u32 svm_fifo_number_ooo_segments (svm_fifo_t * f);
314 ooo_segment_t *svm_fifo_first_ooo_segment (svm_fifo_t * f);
315
316 always_inline ooo_segment_t *
317 svm_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
324 always_inline void
325 svm_fifo_newest_ooo_segment_reset (svm_fifo_t * f)
326 {
327   f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
328 }
329
330 always_inline u32
331 ooo_segment_distance_from_tail (svm_fifo_t * f, u32 pos)
332 {
333   /* Ambiguous. Assumption is that ooo segments don't touch tail */
334   if (PREDICT_FALSE (pos == f->tail && f->tail == f->head))
335     return f->nitems;
336
337   return (((f->nitems + pos) - f->tail) % f->nitems);
338 }
339
340 always_inline u32
341 ooo_segment_distance_to_tail (svm_fifo_t * f, u32 pos)
342 {
343   return (((f->nitems + f->tail) - pos) % f->nitems);
344 }
345
346 always_inline u32
347 ooo_segment_offset (svm_fifo_t * f, ooo_segment_t * s)
348 {
349   return ooo_segment_distance_from_tail (f, s->start);
350 }
351
352 always_inline u32
353 ooo_segment_end_offset (svm_fifo_t * f, ooo_segment_t * s)
354 {
355   return ooo_segment_distance_from_tail (f, s->start) + s->length;
356 }
357
358 always_inline u32
359 ooo_segment_length (svm_fifo_t * f, ooo_segment_t * s)
360 {
361   return s->length;
362 }
363
364 always_inline ooo_segment_t *
365 ooo_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);
370 }
371
372 always_inline ooo_segment_t *
373 ooo_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
380 #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  */