svm: reorganize fifo march code
[vpp.git] / src / svm / svm_fifo.h
1 /*
2  * Copyright (c) 2016-2019 Cisco and/or its affiliates.
3  * Copyright (c) 2019 Arm Limited
4  * Copyright (c) 2010-2017 Intel Corporation and/or its affiliates.
5  * Copyright (c) 2007-2009 Kip Macy kmacy@freebsd.org
6  * Inspired from DPDK rte_ring.h (SPSC only) (derived from freebsd bufring.h).
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at:
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 #ifndef __included_ssvm_fifo_h__
20 #define __included_ssvm_fifo_h__
21
22 #include <vppinfra/clib.h>
23 #include <vppinfra/vec.h>
24 #include <vppinfra/pool.h>
25 #include <vppinfra/format.h>
26
27 /** Out-of-order segment */
28 typedef struct
29 {
30   u32 next;     /**< Next linked-list element pool index */
31   u32 prev;     /**< Previous linked-list element pool index */
32
33   u32 start;    /**< Start of segment, normalized*/
34   u32 length;   /**< Length of segment */
35 } ooo_segment_t;
36
37 #define SVM_FIFO_TRACE                  (0)
38 #define OOO_SEGMENT_INVALID_INDEX       ((u32)~0)
39 #define SVM_FIFO_INVALID_SESSION_INDEX  ((u32)~0)
40 #define SVM_FIFO_INVALID_INDEX          ((u32)~0)
41 #define SVM_FIFO_MAX_EVT_SUBSCRIBERS    8
42
43 enum svm_fifo_tx_ntf_
44 {
45   SVM_FIFO_NO_TX_NOTIF = 0,
46   SVM_FIFO_WANT_TX_NOTIF = 1,
47   SVM_FIFO_WANT_TX_NOTIF_IF_FULL = 2,
48 };
49
50 typedef struct
51 {
52   u32 offset;
53   u32 len;
54   u32 action;
55 } svm_fifo_trace_elem_t;
56
57 typedef struct svm_fifo_chunk_
58 {
59   u32 start_byte;
60   u32 length;
61   struct svm_fifo_chunk_ *next;
62   u8 data[0];
63 } svm_fifo_chunk_t;
64
65 typedef enum svm_fifo_flag_
66 {
67   SVM_FIFO_F_SIZE_UPDATE = 1 << 0,
68 } svm_fifo_flag_t;
69
70 typedef struct _svm_fifo
71 {
72   CLIB_CACHE_LINE_ALIGN_MARK (shared_first);
73   u32 size;                     /**< size of the fifo */
74   u32 nitems;                   /**< usable size(size-1) */
75   u8 flags;                     /**< fifo flags */
76   svm_fifo_chunk_t *start_chunk;/**< first chunk in fifo chunk list */
77   svm_fifo_chunk_t *end_chunk;  /**< end chunk in fifo chunk list */
78   svm_fifo_chunk_t *new_chunks; /**< chunks yet to be added to list */
79
80     CLIB_CACHE_LINE_ALIGN_MARK (shared_second);
81   volatile u32 has_event;       /**< non-zero if deq event exists */
82
83   u32 master_session_index;
84   u32 client_session_index;
85   u8 master_thread_index;
86   u8 client_thread_index;
87   u32 segment_manager;
88   u32 segment_index;
89   u32 ct_session_index;         /**< Local session index for vpp */
90   u32 freelist_index;           /**< aka log2(allocated_size) - const. */
91   i8 refcnt;                    /**< reference count  */
92   struct _svm_fifo *next;       /**< next in freelist/active chain */
93   struct _svm_fifo *prev;       /**< prev in active chain */
94
95     CLIB_CACHE_LINE_ALIGN_MARK (consumer);
96   u32 head;
97   svm_fifo_chunk_t *head_chunk; /**< tracks chunk where head lands */
98   volatile u32 want_tx_ntf;     /**< producer wants nudge */
99   volatile u32 has_tx_ntf;
100
101     CLIB_CACHE_LINE_ALIGN_MARK (producer);
102   u32 tail;
103   svm_fifo_chunk_t *tail_chunk; /**< tracks chunk where tail lands */
104
105   ooo_segment_t *ooo_segments;  /**< Pool of ooo segments */
106   u32 ooos_list_head;           /**< Head of out-of-order linked-list */
107   u32 ooos_newest;              /**< Last segment to have been updated */
108   volatile u8 n_subscribers;
109   u8 subscribers[SVM_FIFO_MAX_EVT_SUBSCRIBERS];
110
111 #if SVM_FIFO_TRACE
112   svm_fifo_trace_elem_t *trace;
113 #endif
114
115   svm_fifo_chunk_t default_chunk;
116 } svm_fifo_t;
117
118 typedef enum
119 {
120   SVM_FIFO_FULL = -2,
121 } svm_fifo_err_t;
122
123 typedef struct svm_fifo_segment_
124 {
125   u8 *data;
126   u32 len;
127 } svm_fifo_segment_t;
128
129 #if SVM_FIFO_TRACE
130 #define svm_fifo_trace_add(_f, _s, _l, _t)              \
131 {                                                       \
132   svm_fifo_trace_elem_t *trace_elt;                     \
133   vec_add2(_f->trace, trace_elt, 1);                    \
134   trace_elt->offset = _s;                               \
135   trace_elt->len = _l;                                  \
136   trace_elt->action = _t;                               \
137 }
138 #else
139 #define svm_fifo_trace_add(_f, _s, _l, _t)
140 #endif
141
142 u8 *svm_fifo_dump_trace (u8 * s, svm_fifo_t * f);
143 u8 *svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose);
144
145 /* internal function */
146 static inline void
147 f_load_head_tail_cons (svm_fifo_t * f, u32 * head, u32 * tail)
148 {
149   /* load-relaxed: consumer owned index */
150   *head = f->head;
151   /* load-acq: consumer foreign index (paired with store-rel in producer) */
152   *tail = clib_atomic_load_acq_n (&f->tail);
153 }
154
155 /* internal function */
156 static inline void
157 f_load_head_tail_prod (svm_fifo_t * f, u32 * head, u32 * tail)
158 {
159   /* load relaxed: producer owned index */
160   *tail = f->tail;
161   /* load-acq: producer foreign index (paired with store-rel in consumer) */
162   *head = clib_atomic_load_acq_n (&f->head);
163 }
164
165 /* producer consumer role independent */
166 /* internal function */
167 static inline void
168 f_load_head_tail_all_acq (svm_fifo_t * f, u32 * head, u32 * tail)
169 {
170   /* load-acq : consumer foreign index (paired with store-rel) */
171   *tail = clib_atomic_load_acq_n (&f->tail);
172   /* load-acq : producer foriegn index (paired with store-rel) */
173   *head = clib_atomic_load_acq_n (&f->head);
174 }
175
176 /* internal function */
177 static inline u32
178 f_free_count (svm_fifo_t * f, u32 head, u32 tail)
179 {
180   return (f->nitems + head - tail);
181 }
182
183 /* internal function */
184 static inline u32
185 f_cursize (svm_fifo_t * f, u32 head, u32 tail)
186 {
187   return (f->nitems - f_free_count (f, head, tail));
188 }
189
190 /* used by consumer */
191 static inline u32
192 svm_fifo_max_dequeue_cons (svm_fifo_t * f)
193 {
194   u32 tail, head;
195   f_load_head_tail_cons (f, &head, &tail);
196   return f_cursize (f, head, tail);
197 }
198
199 /* used by producer*/
200 static inline u32
201 svm_fifo_max_dequeue_prod (svm_fifo_t * f)
202 {
203   u32 tail, head;
204   f_load_head_tail_prod (f, &head, &tail);
205   return f_cursize (f, head, tail);
206 }
207
208 /* use producer or consumer specific functions for perfomance.
209  * svm_fifo_max_dequeue_cons (svm_fifo_t *f)
210  * svm_fifo_max_dequeue_prod (svm_fifo_t *f)
211  */
212 static inline u32
213 svm_fifo_max_dequeue (svm_fifo_t * f)
214 {
215   u32 tail, head;
216   f_load_head_tail_all_acq (f, &head, &tail);
217   return f_cursize (f, head, tail);
218 }
219
220 /* used by producer */
221 static inline int
222 svm_fifo_is_full_prod (svm_fifo_t * f)
223 {
224   return (svm_fifo_max_dequeue_prod (f) == f->nitems);
225 }
226
227 /* use producer or consumer specific functions for perfomance.
228  * svm_fifo_is_full_prod (svm_fifo_t * f)
229  * add cons version if needed
230  */
231 static inline int
232 svm_fifo_is_full (svm_fifo_t * f)
233 {
234   return (svm_fifo_max_dequeue (f) == f->nitems);
235 }
236
237 /* used by consumer */
238 static inline int
239 svm_fifo_is_empty_cons (svm_fifo_t * f)
240 {
241   return (svm_fifo_max_dequeue_cons (f) == 0);
242 }
243
244 /* used by producer */
245 static inline int
246 svm_fifo_is_empty_prod (svm_fifo_t * f)
247 {
248   return (svm_fifo_max_dequeue_prod (f) == 0);
249 }
250
251 /* use producer or consumer specific functions for perfomance.
252  * svm_fifo_is_empty_cons (svm_fifo_t * f)
253  * svm_fifo_is_empty_prod (svm_fifo_t * f)
254  */
255 static inline int
256 svm_fifo_is_empty (svm_fifo_t * f)
257 {
258   return (svm_fifo_max_dequeue (f) == 0);
259 }
260
261 static inline u8
262 svm_fifo_is_wrapped (svm_fifo_t * f)
263 {
264   u32 head, tail;
265   f_load_head_tail_all_acq (f, &head, &tail);
266   return head % f->size > tail % f->size;
267 }
268
269 /* used by producer*/
270 static inline u32
271 svm_fifo_max_enqueue_prod (svm_fifo_t * f)
272 {
273   u32 head, tail;
274   f_load_head_tail_prod (f, &head, &tail);
275   return f_free_count (f, head, tail);
276 }
277
278 /* use producer or consumer specfic functions for perfomance.
279  * svm_fifo_max_enqueue_prod (svm_fifo_t *f)
280  * add consumer specific version if needed.
281  */
282 static inline u32
283 svm_fifo_max_enqueue (svm_fifo_t * f)
284 {
285   u32 head, tail;
286   f_load_head_tail_all_acq (f, &head, &tail);
287   return f_free_count (f, head, tail);
288 }
289
290 static inline int
291 svm_fifo_has_event (svm_fifo_t * f)
292 {
293   return f->has_event;
294 }
295
296 static inline u8
297 svm_fifo_has_ooo_data (svm_fifo_t * f)
298 {
299   return f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX;
300 }
301
302 /**
303  * Sets fifo event flag.
304  *
305  * Also acts as a release ordering.
306  *
307  * @return 1 if flag was not set.
308  */
309 always_inline u8
310 svm_fifo_set_event (svm_fifo_t * f)
311 {
312   /* return __sync_lock_test_and_set (&f->has_event, 1) == 0;
313      return __sync_bool_compare_and_swap (&f->has_event, 0, 1); */
314   return !clib_atomic_swap_rel_n (&f->has_event, 1);
315 }
316
317 /**
318  * Unsets fifo event flag.
319  *
320  * Also acts as an acquire barrier.
321  */
322 always_inline void
323 svm_fifo_unset_event (svm_fifo_t * f)
324 {
325   clib_atomic_swap_acq_n (&f->has_event, 0);
326 }
327
328 svm_fifo_t *svm_fifo_create (u32 data_size_in_bytes);
329 void svm_fifo_init (svm_fifo_t * f, u32 size);
330 void svm_fifo_add_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c);
331 void svm_fifo_free (svm_fifo_t * f);
332
333 int svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 max_bytes,
334                              const u8 * copy_from_here);
335 int svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset,
336                                   u32 required_bytes, u8 * copy_from_here);
337 int svm_fifo_dequeue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_here);
338
339 int svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 max_bytes, u8 * copy_here);
340 int svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes);
341 void svm_fifo_dequeue_drop_all (svm_fifo_t * f);
342 int svm_fifo_segments (svm_fifo_t * f, svm_fifo_segment_t * fs);
343 void svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_segment_t * fs);
344 void svm_fifo_init_pointers (svm_fifo_t * f, u32 pointer);
345 void svm_fifo_clone (svm_fifo_t * df, svm_fifo_t * sf);
346 void svm_fifo_overwrite_head (svm_fifo_t * f, u8 * data, u32 len);
347 void svm_fifo_add_subscriber (svm_fifo_t * f, u8 subscriber);
348 void svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber);
349 format_function_t format_svm_fifo;
350
351 /**
352  * Max contiguous chunk of data that can be read
353  */
354 always_inline u32
355 svm_fifo_max_read_chunk (svm_fifo_t * f)
356 {
357   u32 head, tail;
358   u32 head_idx, tail_idx;
359   f_load_head_tail_cons (f, &head, &tail);
360   head_idx = head % f->size;
361   tail_idx = tail % f->size;
362   return tail_idx > head_idx ? (tail_idx - head_idx) : (f->size - head_idx);
363 }
364
365 /**
366  * Max contiguous chunk of data that can be written
367  */
368 always_inline u32
369 svm_fifo_max_write_chunk (svm_fifo_t * f)
370 {
371   u32 head, tail;
372   u32 head_idx, tail_idx;
373   f_load_head_tail_prod (f, &head, &tail);
374   head_idx = head % f->size;
375   tail_idx = tail % f->size;
376   return tail_idx >= head_idx ? (f->size - tail_idx) : (head_idx - tail_idx);
377 }
378
379 /**
380  * Advance tail pointer
381  *
382  * Useful for moving tail pointer after external enqueue.
383  */
384 always_inline void
385 svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 bytes)
386 {
387   ASSERT (bytes <= svm_fifo_max_enqueue_prod (f));
388   /* load-relaxed: producer owned index */
389   u32 tail = f->tail;
390   tail += bytes;
391   /* store-rel: producer owned index (paired with load-acq in consumer) */
392   clib_atomic_store_rel_n (&f->tail, tail);
393 }
394
395 always_inline u8 *
396 svm_fifo_head (svm_fifo_t * f)
397 {
398   /* load-relaxed: consumer owned index */
399   return (f->head_chunk->data
400           + ((f->head % f->size) - f->head_chunk->start_byte));
401 }
402
403 always_inline u8 *
404 svm_fifo_tail (svm_fifo_t * f)
405 {
406   /* load-relaxed: producer owned index */
407   return (f->tail_chunk->data
408           + ((f->tail % f->size) - f->tail_chunk->start_byte));
409 }
410
411 static inline void
412 svm_fifo_add_want_tx_ntf (svm_fifo_t * f, u8 ntf_type)
413 {
414   f->want_tx_ntf |= ntf_type;
415 }
416
417 static inline void
418 svm_fifo_del_want_tx_ntf (svm_fifo_t * f, u8 ntf_type)
419 {
420   f->want_tx_ntf &= ~ntf_type;
421 }
422
423 static inline void
424 svm_fifo_clear_tx_ntf (svm_fifo_t * f)
425 {
426   /* Set the flag if want_tx_notif_if_full was the only ntf requested */
427   f->has_tx_ntf = f->want_tx_ntf == SVM_FIFO_WANT_TX_NOTIF_IF_FULL;
428   svm_fifo_del_want_tx_ntf (f, SVM_FIFO_WANT_TX_NOTIF);
429 }
430
431 static inline void
432 svm_fifo_reset_tx_ntf (svm_fifo_t * f)
433 {
434   f->has_tx_ntf = 0;
435 }
436
437 static inline u8
438 svm_fifo_needs_tx_ntf (svm_fifo_t * f, u32 n_last_deq)
439 {
440   u8 want_ntf = f->want_tx_ntf;
441
442   if (PREDICT_TRUE (want_ntf == SVM_FIFO_NO_TX_NOTIF))
443     return 0;
444   else if (want_ntf & SVM_FIFO_WANT_TX_NOTIF)
445     return 1;
446   else if (want_ntf & SVM_FIFO_WANT_TX_NOTIF_IF_FULL)
447     {
448       u32 max_deq = svm_fifo_max_dequeue_cons (f);
449       u32 nitems = f->nitems;
450       if (!f->has_tx_ntf && max_deq < nitems
451           && max_deq + n_last_deq >= nitems)
452         return 1;
453
454       return 0;
455     }
456   return 0;
457 }
458
459 always_inline u8
460 svm_fifo_n_subscribers (svm_fifo_t * f)
461 {
462   return f->n_subscribers;
463 }
464
465 u32 svm_fifo_number_ooo_segments (svm_fifo_t * f);
466 ooo_segment_t *svm_fifo_first_ooo_segment (svm_fifo_t * f);
467
468 always_inline ooo_segment_t *
469 svm_fifo_newest_ooo_segment (svm_fifo_t * f)
470 {
471   if (f->ooos_newest == OOO_SEGMENT_INVALID_INDEX)
472     return 0;
473   return pool_elt_at_index (f->ooo_segments, f->ooos_newest);
474 }
475
476 always_inline void
477 svm_fifo_newest_ooo_segment_reset (svm_fifo_t * f)
478 {
479   f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
480 }
481
482 always_inline u32
483 ooo_segment_distance_from_tail (svm_fifo_t * f, u32 pos, u32 tail)
484 {
485   return ((pos - tail) % f->size);
486 }
487
488 always_inline u32
489 ooo_segment_distance_to_tail (svm_fifo_t * f, u32 pos, u32 tail)
490 {
491   return ((tail - pos) % f->size);
492 }
493
494 always_inline u32
495 ooo_segment_offset_prod (svm_fifo_t * f, ooo_segment_t * s)
496 {
497   u32 tail;
498   /* load-relaxed: producer owned index */
499   tail = f->tail;
500
501   return ooo_segment_distance_from_tail (f, s->start, tail);
502 }
503
504 always_inline u32
505 ooo_segment_length (svm_fifo_t * f, ooo_segment_t * s)
506 {
507   return s->length;
508 }
509
510 always_inline ooo_segment_t *
511 ooo_segment_get_prev (svm_fifo_t * f, ooo_segment_t * s)
512 {
513   if (s->prev == OOO_SEGMENT_INVALID_INDEX)
514     return 0;
515   return pool_elt_at_index (f->ooo_segments, s->prev);
516 }
517
518 always_inline ooo_segment_t *
519 ooo_segment_next (svm_fifo_t * f, ooo_segment_t * s)
520 {
521   if (s->next == OOO_SEGMENT_INVALID_INDEX)
522     return 0;
523   return pool_elt_at_index (f->ooo_segments, s->next);
524 }
525
526 #endif /* __included_ssvm_fifo_h__ */
527
528 /*
529  * fd.io coding-style-patch-verification: ON
530  *
531  * Local Variables:
532  * eval: (c-set-style "gnu")
533  * End:
534  */