blob: 87e019ab1f1e1e40e67247c73bacfd0227edebb1 [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
Florin Corasc5df8c72019-04-08 07:42:30 -07002 * Copyright (c) 2016-2019 Cisco and/or its affiliates.
Sirshak Das28aa5392019-02-05 01:33:33 -06003 * 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).
Dave Barach68b0fb02017-02-28 15:15:56 -05007 * 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
Florin Coras6792ec02017-03-13 03:49:51 -070020#include <svm/svm_fifo.h>
Florin Corasf6359c82017-06-19 12:26:09 -040021#include <vppinfra/cpu.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050022
Florin Coras99ad2fc2019-04-18 21:25:49 -070023CLIB_MARCH_FN (svm_fifo_copy_to_chunk, void, svm_fifo_t * f,
24 svm_fifo_chunk_t * c, u32 tail_idx, const u8 * src, u32 len,
Florin Coras4375fa32019-04-19 15:56:00 -070025 svm_fifo_chunk_t ** last)
Florin Coras99ad2fc2019-04-18 21:25:49 -070026{
27 u32 n_chunk;
28
29 ASSERT (tail_idx >= c->start_byte && tail_idx < c->start_byte + c->length);
30
31 tail_idx -= c->start_byte;
32 n_chunk = c->length - tail_idx;
Florin Coras4375fa32019-04-19 15:56:00 -070033 if (n_chunk <= len)
Florin Coras99ad2fc2019-04-18 21:25:49 -070034 {
35 u32 to_copy = len;
36 clib_memcpy_fast (&c->data[tail_idx], src, n_chunk);
Florin Coras4375fa32019-04-19 15:56:00 -070037 c = c->next;
Florin Coras99ad2fc2019-04-18 21:25:49 -070038 while ((to_copy -= n_chunk))
39 {
Florin Coras99ad2fc2019-04-18 21:25:49 -070040 n_chunk = clib_min (c->length, to_copy);
41 clib_memcpy_fast (&c->data[0], src + (len - to_copy), n_chunk);
Florin Coras4375fa32019-04-19 15:56:00 -070042 c = c->length <= to_copy ? c->next : c;
Florin Coras99ad2fc2019-04-18 21:25:49 -070043 }
Florin Coras4375fa32019-04-19 15:56:00 -070044 if (*last)
45 *last = c;
Florin Coras99ad2fc2019-04-18 21:25:49 -070046 }
47 else
48 {
49 clib_memcpy_fast (&c->data[tail_idx], src, len);
50 }
51}
52
53CLIB_MARCH_FN (svm_fifo_copy_from_chunk, void, svm_fifo_t * f,
54 svm_fifo_chunk_t * c, u32 head_idx, u8 * dst, u32 len,
Florin Coras4375fa32019-04-19 15:56:00 -070055 svm_fifo_chunk_t ** last)
Florin Coras99ad2fc2019-04-18 21:25:49 -070056{
57 u32 n_chunk;
58
Florin Coras4375fa32019-04-19 15:56:00 -070059 ASSERT (head_idx >= c->start_byte && head_idx < c->start_byte + c->length);
60
Florin Coras99ad2fc2019-04-18 21:25:49 -070061 head_idx -= c->start_byte;
62 n_chunk = c->length - head_idx;
Florin Coras4375fa32019-04-19 15:56:00 -070063 if (n_chunk <= len)
Florin Coras99ad2fc2019-04-18 21:25:49 -070064 {
65 u32 to_copy = len;
66 clib_memcpy_fast (dst, &c->data[head_idx], n_chunk);
Florin Coras4375fa32019-04-19 15:56:00 -070067 c = c->next;
Florin Coras99ad2fc2019-04-18 21:25:49 -070068 while ((to_copy -= n_chunk))
69 {
Florin Coras99ad2fc2019-04-18 21:25:49 -070070 n_chunk = clib_min (c->length, to_copy);
71 clib_memcpy_fast (dst + (len - to_copy), &c->data[0], n_chunk);
Florin Coras4375fa32019-04-19 15:56:00 -070072 c = c->length <= to_copy ? c->next : c;
Florin Coras99ad2fc2019-04-18 21:25:49 -070073 }
Florin Coras4375fa32019-04-19 15:56:00 -070074 if (*last)
75 *last = c;
Florin Coras99ad2fc2019-04-18 21:25:49 -070076 }
77 else
78 {
79 clib_memcpy_fast (dst, &c->data[head_idx], len);
80 }
81}
82
83#ifndef CLIB_MARCH_VARIANT
84
85static inline void
86svm_fifo_copy_to_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c, u32 tail_idx,
Florin Coras4375fa32019-04-19 15:56:00 -070087 const u8 * src, u32 len, svm_fifo_chunk_t ** last)
Florin Coras99ad2fc2019-04-18 21:25:49 -070088{
89 CLIB_MARCH_FN_SELECT (svm_fifo_copy_to_chunk) (f, c, tail_idx, src, len,
Florin Coras4375fa32019-04-19 15:56:00 -070090 last);
Florin Coras99ad2fc2019-04-18 21:25:49 -070091}
92
93static inline void
94svm_fifo_copy_from_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c, u32 head_idx,
Florin Coras4375fa32019-04-19 15:56:00 -070095 u8 * dst, u32 len, svm_fifo_chunk_t ** last)
Florin Coras99ad2fc2019-04-18 21:25:49 -070096{
97 CLIB_MARCH_FN_SELECT (svm_fifo_copy_from_chunk) (f, c, head_idx, dst, len,
Florin Coras4375fa32019-04-19 15:56:00 -070098 last);
Florin Coras99ad2fc2019-04-18 21:25:49 -070099}
100
Florin Corasf03a59a2017-06-09 21:07:32 -0700101static inline u8
Sirshak Das28aa5392019-02-05 01:33:33 -0600102position_lt (svm_fifo_t * f, u32 a, u32 b, u32 tail)
Florin Corasf03a59a2017-06-09 21:07:32 -0700103{
Florin Coras87b15ce2019-04-28 21:16:30 -0700104 return (f_distance_to (f, a, tail) < f_distance_to (f, b, tail));
Florin Corasf03a59a2017-06-09 21:07:32 -0700105}
106
107static inline u8
Sirshak Das28aa5392019-02-05 01:33:33 -0600108position_leq (svm_fifo_t * f, u32 a, u32 b, u32 tail)
Florin Corasf03a59a2017-06-09 21:07:32 -0700109{
Florin Coras87b15ce2019-04-28 21:16:30 -0700110 return (f_distance_to (f, a, tail) <= f_distance_to (f, b, tail));
Florin Corasf03a59a2017-06-09 21:07:32 -0700111}
112
113static inline u8
Sirshak Das28aa5392019-02-05 01:33:33 -0600114position_gt (svm_fifo_t * f, u32 a, u32 b, u32 tail)
Florin Corasf03a59a2017-06-09 21:07:32 -0700115{
Florin Coras87b15ce2019-04-28 21:16:30 -0700116 return (f_distance_to (f, a, tail) > f_distance_to (f, b, tail));
Florin Corasf03a59a2017-06-09 21:07:32 -0700117}
118
119static inline u32
Florin Coras87b15ce2019-04-28 21:16:30 -0700120position_diff (svm_fifo_t * f, u32 a, u32 b, u32 tail)
Florin Corasf03a59a2017-06-09 21:07:32 -0700121{
Florin Coras87b15ce2019-04-28 21:16:30 -0700122 return f_distance_to (f, a, tail) - f_distance_to (f, b, tail);
Florin Corasf03a59a2017-06-09 21:07:32 -0700123}
124
125static inline u32
126ooo_segment_end_pos (svm_fifo_t * f, ooo_segment_t * s)
127{
Florin Coras29a59c32019-05-02 12:52:19 -0700128 return (s->start + s->length) % f->size;
Florin Corasf03a59a2017-06-09 21:07:32 -0700129}
Dave Barach1f75cfd2017-04-14 16:46:44 -0400130
Florin Coras87b15ce2019-04-28 21:16:30 -0700131void
132svm_fifo_free_ooo_data (svm_fifo_t * f)
Dave Barach1f75cfd2017-04-14 16:46:44 -0400133{
Florin Coras87b15ce2019-04-28 21:16:30 -0700134 pool_free (f->ooo_segments);
135}
136
137static inline ooo_segment_t *
Florin Corasa7570b02019-05-02 12:52:19 -0700138ooo_segment_prev (svm_fifo_t * f, ooo_segment_t * s)
Florin Coras87b15ce2019-04-28 21:16:30 -0700139{
140 if (s->prev == OOO_SEGMENT_INVALID_INDEX)
141 return 0;
142 return pool_elt_at_index (f->ooo_segments, s->prev);
143}
144
145static inline ooo_segment_t *
146ooo_segment_next (svm_fifo_t * f, ooo_segment_t * s)
147{
148 if (s->next == OOO_SEGMENT_INVALID_INDEX)
149 return 0;
150 return pool_elt_at_index (f->ooo_segments, s->next);
151}
152
153static inline ooo_segment_t *
154ooo_segment_alloc (svm_fifo_t * f, u32 start, u32 length)
155{
156 ooo_segment_t *s;
157
158 pool_get (f->ooo_segments, s);
159
160 s->start = start;
161 s->length = length;
162 s->prev = s->next = OOO_SEGMENT_INVALID_INDEX;
163
Dave Barach1f75cfd2017-04-14 16:46:44 -0400164 return s;
165}
166
Florin Coras87b15ce2019-04-28 21:16:30 -0700167static inline void
168ooo_segment_free (svm_fifo_t * f, u32 index)
Florin Coras3eb50622017-07-13 01:24:57 -0400169{
Florin Coras87b15ce2019-04-28 21:16:30 -0700170 ooo_segment_t *cur, *prev = 0, *next = 0;
171 cur = pool_elt_at_index (f->ooo_segments, index);
Florin Coras3eb50622017-07-13 01:24:57 -0400172
Florin Coras87b15ce2019-04-28 21:16:30 -0700173 if (cur->next != OOO_SEGMENT_INVALID_INDEX)
Florin Coras3eb50622017-07-13 01:24:57 -0400174 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700175 next = pool_elt_at_index (f->ooo_segments, cur->next);
176 next->prev = cur->prev;
Florin Coras3eb50622017-07-13 01:24:57 -0400177 }
Florin Coras87b15ce2019-04-28 21:16:30 -0700178
179 if (cur->prev != OOO_SEGMENT_INVALID_INDEX)
180 {
181 prev = pool_elt_at_index (f->ooo_segments, cur->prev);
182 prev->next = cur->next;
183 }
184 else
185 {
186 f->ooos_list_head = cur->next;
187 }
188
189 pool_put (f->ooo_segments, cur);
Florin Coras3eb50622017-07-13 01:24:57 -0400190}
191
Florin Coras87b15ce2019-04-28 21:16:30 -0700192/**
193 * Add segment to fifo's out-of-order segment list. Takes care of merging
194 * adjacent segments and removing overlapping ones.
195 */
196static void
197ooo_segment_add (svm_fifo_t * f, u32 offset, u32 head, u32 tail, u32 length)
Florin Coras3eb50622017-07-13 01:24:57 -0400198{
Florin Coras87b15ce2019-04-28 21:16:30 -0700199 ooo_segment_t *s, *new_s, *prev, *next, *it;
200 u32 new_index, s_end_pos, s_index;
201 u32 offset_pos, offset_end_pos;
Florin Coras3eb50622017-07-13 01:24:57 -0400202
Florin Coras87b15ce2019-04-28 21:16:30 -0700203 ASSERT (offset + length <= f_distance_to (f, head, tail) || head == tail);
Florin Coras3eb50622017-07-13 01:24:57 -0400204
Florin Coras29a59c32019-05-02 12:52:19 -0700205 offset_pos = (tail + offset) % f->size;
206 offset_end_pos = (tail + offset + length) % f->size;
Florin Coras3eb50622017-07-13 01:24:57 -0400207
Florin Coras87b15ce2019-04-28 21:16:30 -0700208 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
Florin Coras3eb50622017-07-13 01:24:57 -0400209
Florin Coras87b15ce2019-04-28 21:16:30 -0700210 if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX)
Florin Coras3eb50622017-07-13 01:24:57 -0400211 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700212 s = ooo_segment_alloc (f, offset_pos, length);
213 f->ooos_list_head = s - f->ooo_segments;
214 f->ooos_newest = f->ooos_list_head;
215 return;
216 }
217
218 /* Find first segment that starts after new segment */
219 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
220 while (s->next != OOO_SEGMENT_INVALID_INDEX
221 && position_lt (f, s->start, offset_pos, tail))
222 s = pool_elt_at_index (f->ooo_segments, s->next);
223
224 /* If we have a previous and we overlap it, use it as starting point */
Florin Corasa7570b02019-05-02 12:52:19 -0700225 prev = ooo_segment_prev (f, s);
Florin Coras87b15ce2019-04-28 21:16:30 -0700226 if (prev
227 && position_leq (f, offset_pos, ooo_segment_end_pos (f, prev), tail))
228 {
229 s = prev;
230 s_end_pos = ooo_segment_end_pos (f, s);
231
232 /* Since we have previous, offset start position cannot be smaller
233 * than prev->start. Check tail */
234 ASSERT (position_lt (f, s->start, offset_pos, tail));
235 goto check_tail;
236 }
237
238 s_index = s - f->ooo_segments;
239 s_end_pos = ooo_segment_end_pos (f, s);
240
241 /* No overlap, add before current segment */
242 if (position_lt (f, offset_end_pos, s->start, tail))
243 {
244 new_s = ooo_segment_alloc (f, offset_pos, length);
245 new_index = new_s - f->ooo_segments;
246
247 /* Pool might've moved, get segment again */
248 s = pool_elt_at_index (f->ooo_segments, s_index);
249 if (s->prev != OOO_SEGMENT_INVALID_INDEX)
Florin Coras3eb50622017-07-13 01:24:57 -0400250 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700251 new_s->prev = s->prev;
252 prev = pool_elt_at_index (f->ooo_segments, new_s->prev);
253 prev->next = new_index;
Florin Coras3eb50622017-07-13 01:24:57 -0400254 }
Florin Coras87b15ce2019-04-28 21:16:30 -0700255 else
Florin Coras3eb50622017-07-13 01:24:57 -0400256 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700257 /* New head */
258 f->ooos_list_head = new_index;
Florin Coras3eb50622017-07-13 01:24:57 -0400259 }
Florin Coras87b15ce2019-04-28 21:16:30 -0700260
261 new_s->next = s_index;
262 s->prev = new_index;
263 f->ooos_newest = new_index;
264 return;
265 }
266 /* No overlap, add after current segment */
267 else if (position_gt (f, offset_pos, s_end_pos, tail))
268 {
269 new_s = ooo_segment_alloc (f, offset_pos, length);
270 new_index = new_s - f->ooo_segments;
271
272 /* Pool might've moved, get segment again */
273 s = pool_elt_at_index (f->ooo_segments, s_index);
274
275 /* Needs to be last */
276 ASSERT (s->next == OOO_SEGMENT_INVALID_INDEX);
277
278 new_s->prev = s_index;
279 s->next = new_index;
280 f->ooos_newest = new_index;
281
282 return;
Florin Coras3eb50622017-07-13 01:24:57 -0400283 }
284
Florin Coras87b15ce2019-04-28 21:16:30 -0700285 /*
286 * Merge needed
287 */
Florin Coras3eb50622017-07-13 01:24:57 -0400288
Florin Coras87b15ce2019-04-28 21:16:30 -0700289 /* Merge at head */
290 if (position_lt (f, offset_pos, s->start, tail))
291 {
292 s->start = offset_pos;
293 s->length = position_diff (f, s_end_pos, s->start, tail);
294 f->ooos_newest = s - f->ooo_segments;
295 }
296
297check_tail:
298
299 /* Overlapping tail */
300 if (position_gt (f, offset_end_pos, s_end_pos, tail))
301 {
302 s->length = position_diff (f, offset_end_pos, s->start, tail);
303
304 /* Remove the completely overlapped segments in the tail */
305 it = ooo_segment_next (f, s);
306 while (it && position_leq (f, ooo_segment_end_pos (f, it),
307 offset_end_pos, tail))
308 {
309 next = ooo_segment_next (f, it);
310 ooo_segment_free (f, it - f->ooo_segments);
311 it = next;
312 }
313
314 /* If partial overlap with last, merge */
315 if (it && position_leq (f, it->start, offset_end_pos, tail))
316 {
317 s->length = position_diff (f, ooo_segment_end_pos (f, it),
318 s->start, tail);
319 ooo_segment_free (f, it - f->ooo_segments);
320 }
321 f->ooos_newest = s - f->ooo_segments;
322 }
Florin Coras3eb50622017-07-13 01:24:57 -0400323}
324
Florin Coras87b15ce2019-04-28 21:16:30 -0700325/**
326 * Removes segments that can now be enqueued because the fifo's tail has
327 * advanced. Returns the number of bytes added to tail.
328 */
329static int
330ooo_segment_try_collect (svm_fifo_t * f, u32 n_bytes_enqueued, u32 * tail)
Dave Barach1f75cfd2017-04-14 16:46:44 -0400331{
Florin Coras87b15ce2019-04-28 21:16:30 -0700332 u32 s_index, bytes = 0;
333 ooo_segment_t *s;
334 i32 diff;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400335
Florin Coras87b15ce2019-04-28 21:16:30 -0700336 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
337 diff = f_distance_from (f, s->start, *tail);
338
339 ASSERT (diff != n_bytes_enqueued);
340
341 if (diff > n_bytes_enqueued)
342 return 0;
343
344 /* If last tail update overlaps one/multiple ooo segments, remove them */
345 while (0 <= diff && diff < n_bytes_enqueued)
Dave Barach1f75cfd2017-04-14 16:46:44 -0400346 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700347 s_index = s - f->ooo_segments;
348
349 /* Segment end is beyond the tail. Advance tail and remove segment */
350 if (s->length > diff)
351 {
352 bytes = s->length - diff;
Florin Coras29a59c32019-05-02 12:52:19 -0700353 *tail = (*tail + bytes) % f->size;
Florin Coras87b15ce2019-04-28 21:16:30 -0700354 ooo_segment_free (f, s_index);
355 break;
356 }
357
358 /* If we have next go on */
359 if (s->next != OOO_SEGMENT_INVALID_INDEX)
360 {
361 s = pool_elt_at_index (f->ooo_segments, s->next);
362 diff = f_distance_from (f, s->start, *tail);
363 ooo_segment_free (f, s_index);
364 }
365 /* End of search */
366 else
367 {
368 ooo_segment_free (f, s_index);
369 break;
370 }
Dave Barach1f75cfd2017-04-14 16:46:44 -0400371 }
Florin Coras3eb50622017-07-13 01:24:57 -0400372
Florin Coras87b15ce2019-04-28 21:16:30 -0700373 ASSERT (bytes <= f->nitems);
374 return bytes;
Florin Corasb59a7052017-04-18 22:07:29 -0700375}
376
Florin Corasa7570b02019-05-02 12:52:19 -0700377static ooo_segment_t *
378ooo_segment_last (svm_fifo_t * f)
379{
380 ooo_segment_t *s;
381
382 if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX)
383 return 0;
384
385 s = svm_fifo_first_ooo_segment (f);
386 while (s->next != OOO_SEGMENT_INVALID_INDEX)
387 s = pool_elt_at_index (f->ooo_segments, s->next);
388 return s;
389}
390
Florin Coras0a846802019-04-09 18:29:14 -0700391void
392svm_fifo_init (svm_fifo_t * f, u32 size)
393{
394 f->size = size;
395 /*
396 * usable size of the fifo set to rounded_data_size - 1
397 * to differentiate between free fifo and empty fifo.
398 */
399 f->nitems = f->size - 1;
400 f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
Florin Coras0a846802019-04-09 18:29:14 -0700401 f->segment_index = SVM_FIFO_INVALID_INDEX;
402 f->refcnt = 1;
Florin Corascefd5d82019-05-05 13:19:57 -0700403 f->flags = 0;
Florin Coras4375fa32019-04-19 15:56:00 -0700404 f->head_chunk = f->tail_chunk = f->ooo_enq = f->ooo_deq = f->start_chunk;
Florin Coras0a846802019-04-09 18:29:14 -0700405}
406
Florin Corasb095a3c2019-04-25 12:58:46 -0700407/**
408 * Creates a fifo in the current heap. Fails vs blow up the process
409 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500410svm_fifo_t *
411svm_fifo_create (u32 data_size_in_bytes)
412{
Dave Barach818eb542017-08-02 13:56:13 -0400413 u32 rounded_data_size;
Florin Corascefd5d82019-05-05 13:19:57 -0700414 svm_fifo_chunk_t *c;
415 svm_fifo_t *f;
Dave Barach68b0fb02017-02-28 15:15:56 -0500416
Florin Corascefd5d82019-05-05 13:19:57 -0700417 f = clib_mem_alloc_aligned_or_null (sizeof (*f), CLIB_CACHE_LINE_BYTES);
Dave Barach68b0fb02017-02-28 15:15:56 -0500418 if (f == 0)
419 return 0;
420
Dave Barachb7b92992018-10-17 10:38:51 -0400421 clib_memset (f, 0, sizeof (*f));
Florin Corascefd5d82019-05-05 13:19:57 -0700422
423 /* always round fifo data size to the next highest power-of-two */
424 rounded_data_size = (1 << (max_log2 (data_size_in_bytes)));
425 c = clib_mem_alloc_aligned_or_null (sizeof (*c) + rounded_data_size,
426 CLIB_CACHE_LINE_BYTES);
427 if (!c)
428 {
429 clib_mem_free (f);
430 return 0;
431 }
432
433 c->next = c;
434 c->start_byte = 0;
435 c->length = data_size_in_bytes;
436 f->start_chunk = f->end_chunk = c;
437
Florin Coras0a846802019-04-09 18:29:14 -0700438 svm_fifo_init (f, data_size_in_bytes);
439 return f;
Dave Barach68b0fb02017-02-28 15:15:56 -0500440}
441
Florin Corasb095a3c2019-04-25 12:58:46 -0700442/**
443 * Creates a fifo chunk in the current heap
444 */
445svm_fifo_chunk_t *
446svm_fifo_chunk_alloc (u32 size)
447{
448 svm_fifo_chunk_t *c;
449 u32 rounded_size;
450
451 /* round chunk size to the next highest power-of-two */
452 rounded_size = (1 << (max_log2 (size)));
453 c = clib_mem_alloc_aligned_or_null (sizeof (*c) + rounded_size,
454 CLIB_CACHE_LINE_BYTES);
455 if (c == 0)
456 return 0;
457
458 clib_memset (c, 0, sizeof (*c));
459 c->length = rounded_size;
460 return c;
461}
462
Florin Coras4375fa32019-04-19 15:56:00 -0700463static inline u8
464svm_fifo_chunk_includes_pos (svm_fifo_chunk_t * c, u32 pos)
465{
466 return (pos >= c->start_byte && pos < c->start_byte + c->length);
467}
468
469/**
470 * Find chunk for given byte position
471 *
472 * @param f fifo
473 * @param pos normalized position in fifo
474 *
475 * @return chunk that includes given position or 0
476 */
477static svm_fifo_chunk_t *
478svm_fifo_find_chunk (svm_fifo_t * f, u32 pos)
479{
480 rb_tree_t *rt = &f->chunk_lookup;
481 rb_node_t *cur, *prev;
482 svm_fifo_chunk_t *c;
483
484 cur = rb_node (rt, rt->root);
485 while (pos != cur->key)
486 {
487 prev = cur;
488 if (pos < cur->key)
489 cur = rb_node_left (rt, cur);
490 else
491 cur = rb_node_right (rt, cur);
492
493 if (rb_node_is_tnil (rt, cur))
494 {
495 /* Hit tnil as a left child. Find predecessor */
496 if (pos < prev->key)
497 {
498 cur = rb_tree_predecessor (rt, prev);
499 c = uword_to_pointer (cur->opaque, svm_fifo_chunk_t *);
500 if (svm_fifo_chunk_includes_pos (c, pos))
501 return c;
502 return 0;
503 }
Florin Coras87b15ce2019-04-28 21:16:30 -0700504 /* Hit tnil as a right child. Check if this is the one */
Florin Coras4375fa32019-04-19 15:56:00 -0700505 c = uword_to_pointer (prev->opaque, svm_fifo_chunk_t *);
506 if (svm_fifo_chunk_includes_pos (c, pos))
507 return c;
508
Florin Coras4375fa32019-04-19 15:56:00 -0700509 return 0;
510 }
511 }
512
513 if (!rb_node_is_tnil (rt, cur))
514 return uword_to_pointer (cur->opaque, svm_fifo_chunk_t *);
515 return 0;
Florin Coras99ad2fc2019-04-18 21:25:49 -0700516}
517
Florin Corasa7570b02019-05-02 12:52:19 -0700518static inline void
519svm_fifo_grow (svm_fifo_t * f, svm_fifo_chunk_t * c)
520{
521 svm_fifo_chunk_t *prev;
522 u32 add_bytes = 0;
523
524 if (!c)
525 return;
526
527 f->end_chunk->next = c;
528 while (c)
529 {
530 add_bytes += c->length;
531 prev = c;
532 c = c->next;
533 }
534 f->end_chunk = prev;
535 prev->next = f->start_chunk;
536 f->size += add_bytes;
537 f->nitems = f->size - 1;
538 f->new_chunks = 0;
539}
540
541static void
542svm_fifo_try_grow (svm_fifo_t * f, u32 new_head)
543{
544 if (new_head > f->tail)
545 return;
546
547 svm_fifo_grow (f, f->new_chunks);
548 f->flags &= ~SVM_FIFO_F_GROW;
549}
550
551void
552svm_fifo_add_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c)
553{
554 svm_fifo_chunk_t *cur, *prev;
555
556 /* Initialize rbtree if needed and add default chunk to it. Expectation is
557 * that this is called with the heap where the rbtree's pool is pushed. */
558 if (!(f->flags & SVM_FIFO_F_MULTI_CHUNK))
559 {
560 rb_tree_init (&f->chunk_lookup);
561 rb_tree_add2 (&f->chunk_lookup, 0, pointer_to_uword (f->start_chunk));
562 f->flags |= SVM_FIFO_F_MULTI_CHUNK;
563 }
564
565 /* Initialize chunks and add to lookup rbtree */
566 cur = c;
567 if (f->new_chunks)
568 {
569 prev = f->new_chunks;
570 while (prev->next)
571 prev = prev->next;
572 prev->next = c;
573 }
574 else
575 prev = f->end_chunk;
576
577 while (cur)
578 {
579 cur->start_byte = prev->start_byte + prev->length;
580 rb_tree_add2 (&f->chunk_lookup, cur->start_byte,
581 pointer_to_uword (cur));
582 prev = cur;
583 cur = cur->next;
584 }
585
586 /* If fifo is not wrapped, update the size now */
587 if (!svm_fifo_is_wrapped (f))
588 {
589 ASSERT (!f->new_chunks);
590 svm_fifo_grow (f, c);
591 return;
592 }
593
594 /* Postpone size update */
595 if (!f->new_chunks)
596 {
597 f->new_chunks = c;
598 f->flags |= SVM_FIFO_F_GROW;
599 }
600}
601
602/**
603 * Removes chunks that are after fifo end byte
604 */
605svm_fifo_chunk_t *
606svm_fifo_collect_chunks (svm_fifo_t * f)
607{
608 svm_fifo_chunk_t *list, *cur;
609
610 f->flags &= ~SVM_FIFO_F_COLLECT_CHUNKS;
611
612 list = f->new_chunks;
613 f->new_chunks = 0;
614 cur = list;
615 while (cur)
616 {
617 rb_tree_del (&f->chunk_lookup, cur->start_byte);
618 cur = cur->next;
619 }
620
621 return list;
622}
623
624void
625svm_fifo_try_shrink (svm_fifo_t * f, u32 head, u32 tail)
626{
627 u32 len_to_shrink = 0, tail_pos, len;
628 svm_fifo_chunk_t *cur, *prev, *next, *start;
629
630 tail_pos = tail;
631 if (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX)
632 {
633 ooo_segment_t *last = ooo_segment_last (f);
634 tail_pos = ooo_segment_end_pos (f, last);
635 }
636
637 if (f->size_decrement)
638 {
639 /* Figure out available free space considering that there may be
640 * ooo segments */
641 len = clib_min (f->size_decrement, f_free_count (f, head, tail_pos));
642 f->nitems -= len;
643 f->size_decrement -= len;
644 }
645
646 /* Remove tail chunks if the following hold:
647 * - not wrapped
648 * - last used byte less than start of last chunk
649 */
650 if (tail_pos >= head && tail_pos <= f->end_chunk->start_byte)
651 {
652 /* Lookup the last position not to be removed. Since size still needs
653 * to be nitems + 1, nitems must fall within the usable space */
654 tail_pos = tail_pos > 0 ? tail_pos - 1 : tail_pos;
655 prev = svm_fifo_find_chunk (f, clib_max (f->nitems, tail_pos));
656 next = prev->next;
657 while (next != f->start_chunk)
658 {
659 cur = next;
660 next = cur->next;
661 len_to_shrink += cur->length;
662 }
663 if (len_to_shrink)
664 {
665 f->size -= len_to_shrink;
666 start = prev->next;
667 prev->next = f->start_chunk;
668 f->end_chunk = prev;
669 cur->next = f->new_chunks;
670 f->new_chunks = start;
671 }
672 }
673
674 if (!f->size_decrement && f->size == f->nitems + 1)
675 {
676 f->flags &= ~SVM_FIFO_F_SHRINK;
677 f->flags |= SVM_FIFO_F_COLLECT_CHUNKS;
678 if (f->start_chunk == f->start_chunk->next)
679 f->flags &= ~SVM_FIFO_F_MULTI_CHUNK;
680 }
681}
682
683/**
684 * Request to reduce fifo size by amount of bytes
685 */
686int
687svm_fifo_reduce_size (svm_fifo_t * f, u32 len, u8 try_shrink)
688{
689 svm_fifo_chunk_t *cur;
690 u32 actual_len = 0;
691
Florin Coras344ce422019-05-03 11:46:55 -0700692 /* Abort if trying to reduce by more than fifo size or if
693 * fifo is undergoing resizing already */
694 if (len >= f->size || f->size > f->nitems + 1
695 || (f->flags & SVM_FIFO_F_SHRINK) || (f->flags & SVM_FIFO_F_GROW))
Florin Corasa7570b02019-05-02 12:52:19 -0700696 return 0;
697
698 /* last chunk that will not be removed */
699 cur = svm_fifo_find_chunk (f, f->nitems - len);
700
701 /* sum length of chunks that will be removed */
702 cur = cur->next;
703 while (cur != f->start_chunk)
704 {
705 actual_len += cur->length;
706 cur = cur->next;
707 }
708
709 ASSERT (actual_len <= len);
710 if (!actual_len)
711 return 0;
712
713 f->size_decrement = actual_len;
714 f->flags |= SVM_FIFO_F_SHRINK;
715
716 if (try_shrink)
717 {
718 u32 head, tail;
719 f_load_head_tail_prod (f, &head, &tail);
720 svm_fifo_try_shrink (f, head, tail);
721 }
722
723 return actual_len;
724}
725
Florin Coras6cf30ad2017-04-04 23:08:23 -0700726void
Florin Corasb095a3c2019-04-25 12:58:46 -0700727svm_fifo_free_chunk_lookup (svm_fifo_t * f)
728{
729 rb_tree_free_nodes (&f->chunk_lookup);
730}
731
732void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700733svm_fifo_free (svm_fifo_t * f)
734{
Dave Barach52851e62017-08-07 09:35:25 -0400735 ASSERT (f->refcnt > 0);
736
737 if (--f->refcnt == 0)
738 {
Florin Corasb095a3c2019-04-25 12:58:46 -0700739 /* ooo data is not allocated on segment heap */
740 svm_fifo_free_chunk_lookup (f);
Dave Barach52851e62017-08-07 09:35:25 -0400741 clib_mem_free (f);
742 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700743}
744
Florin Coras7fb0fe12018-04-09 09:24:52 -0700745void
Florin Coras87b15ce2019-04-28 21:16:30 -0700746svm_fifo_overwrite_head (svm_fifo_t * f, u8 * src, u32 len)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700747{
Florin Coras0a846802019-04-09 18:29:14 -0700748 u32 n_chunk;
Sirshak Das28aa5392019-02-05 01:33:33 -0600749 u32 head, tail, head_idx;
Florin Coras0a846802019-04-09 18:29:14 -0700750 svm_fifo_chunk_t *c;
751
752 ASSERT (len <= f->nitems);
Sirshak Das28aa5392019-02-05 01:33:33 -0600753
754 f_load_head_tail_cons (f, &head, &tail);
Florin Coras0a846802019-04-09 18:29:14 -0700755 c = f->head_chunk;
Florin Coras29a59c32019-05-02 12:52:19 -0700756 head_idx = head - c->start_byte;
Florin Coras0a846802019-04-09 18:29:14 -0700757 n_chunk = c->length - head_idx;
758 if (len <= n_chunk)
Florin Coras87b15ce2019-04-28 21:16:30 -0700759 clib_memcpy_fast (&c->data[head_idx], src, len);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700760 else
761 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700762 clib_memcpy_fast (&c->data[head_idx], src, n_chunk);
763 clib_memcpy_fast (&c->next->data[0], src + n_chunk, len - n_chunk);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700764 }
765}
Dave Barach68b0fb02017-02-28 15:15:56 -0500766
Florin Coras99ad2fc2019-04-18 21:25:49 -0700767int
Florin Coras87b15ce2019-04-28 21:16:30 -0700768svm_fifo_enqueue (svm_fifo_t * f, u32 len, const u8 * src)
Dave Barach68b0fb02017-02-28 15:15:56 -0500769{
Florin Coras99ad2fc2019-04-18 21:25:49 -0700770 u32 tail, head, free_count;
771
772 f_load_head_tail_prod (f, &head, &tail);
773
774 /* free space in fifo can only increase during enqueue: SPSC */
775 free_count = f_free_count (f, head, tail);
776
777 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
778
779 if (PREDICT_FALSE (free_count == 0))
Florin Coras87b15ce2019-04-28 21:16:30 -0700780 return SVM_FIFO_EFULL;
Florin Coras99ad2fc2019-04-18 21:25:49 -0700781
782 /* number of bytes we're going to copy */
783 len = clib_min (free_count, len);
Florin Coras29a59c32019-05-02 12:52:19 -0700784 svm_fifo_copy_to_chunk (f, f->tail_chunk, tail, src, len, &f->tail_chunk);
785 tail = (tail + len) % f->size;
Florin Coras99ad2fc2019-04-18 21:25:49 -0700786
Florin Coras87b15ce2019-04-28 21:16:30 -0700787 svm_fifo_trace_add (f, head, len, 2);
Florin Coras99ad2fc2019-04-18 21:25:49 -0700788
789 /* collect out-of-order segments */
790 if (PREDICT_FALSE (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX))
791 len += ooo_segment_try_collect (f, len, &tail);
792
793 /* store-rel: producer owned index (paired with load-acq in consumer) */
794 clib_atomic_store_rel_n (&f->tail, tail);
795
796 return len;
797}
798
799/**
800 * Enqueue a future segment.
801 *
802 * Two choices: either copies the entire segment, or copies nothing
803 * Returns 0 of the entire segment was copied
804 * Returns -1 if none of the segment was copied due to lack of space
805 */
806int
807svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 len, u8 * src)
808{
Florin Coras4375fa32019-04-19 15:56:00 -0700809 u32 tail, head, free_count, tail_idx;
Florin Coras99ad2fc2019-04-18 21:25:49 -0700810
811 f_load_head_tail_prod (f, &head, &tail);
812
Florin Corasa7570b02019-05-02 12:52:19 -0700813 if (PREDICT_FALSE (f->flags & SVM_FIFO_F_SHRINK))
814 svm_fifo_try_shrink (f, head, tail);
815
Florin Coras99ad2fc2019-04-18 21:25:49 -0700816 /* free space in fifo can only increase during enqueue: SPSC */
817 free_count = f_free_count (f, head, tail);
818
819 /* will this request fit? */
820 if ((len + offset) > free_count)
Florin Coras87b15ce2019-04-28 21:16:30 -0700821 return SVM_FIFO_EFULL;
Florin Coras99ad2fc2019-04-18 21:25:49 -0700822
823 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
Florin Coras99ad2fc2019-04-18 21:25:49 -0700824 svm_fifo_trace_add (f, offset, len, 1);
Florin Coras99ad2fc2019-04-18 21:25:49 -0700825 ooo_segment_add (f, offset, head, tail, len);
Florin Coras29a59c32019-05-02 12:52:19 -0700826 tail_idx = (tail + offset) % f->size;
Florin Coras4375fa32019-04-19 15:56:00 -0700827
828 if (!svm_fifo_chunk_includes_pos (f->ooo_enq, tail_idx))
829 f->ooo_enq = svm_fifo_find_chunk (f, tail_idx);
830
831 svm_fifo_copy_to_chunk (f, f->ooo_enq, tail_idx, src, len, &f->ooo_enq);
Florin Coras99ad2fc2019-04-18 21:25:49 -0700832
833 return 0;
834}
835
Florin Corasa7570b02019-05-02 12:52:19 -0700836/**
837 * Advance tail
838 */
839void
840svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 len)
841{
842 u32 tail;
843
844 ASSERT (len <= svm_fifo_max_enqueue_prod (f));
845 /* load-relaxed: producer owned index */
846 tail = f->tail;
847 tail = (tail + len) % f->size;
848 /* store-rel: producer owned index (paired with load-acq in consumer) */
849 clib_atomic_store_rel_n (&f->tail, tail);
850}
851
Florin Coras99ad2fc2019-04-18 21:25:49 -0700852int
Florin Coras87b15ce2019-04-28 21:16:30 -0700853svm_fifo_dequeue (svm_fifo_t * f, u32 len, u8 * dst)
Florin Coras99ad2fc2019-04-18 21:25:49 -0700854{
855 u32 tail, head, cursize;
Dave Barach68b0fb02017-02-28 15:15:56 -0500856
Sirshak Das28aa5392019-02-05 01:33:33 -0600857 f_load_head_tail_cons (f, &head, &tail);
858
859 /* current size of fifo can only increase during dequeue: SPSC */
860 cursize = f_cursize (f, head, tail);
861
Florin Coras3e350af2017-03-30 02:54:28 -0700862 if (PREDICT_FALSE (cursize == 0))
Florin Coras87b15ce2019-04-28 21:16:30 -0700863 return SVM_FIFO_EEMPTY;
Dave Barach68b0fb02017-02-28 15:15:56 -0500864
Florin Coras99ad2fc2019-04-18 21:25:49 -0700865 len = clib_min (cursize, len);
Florin Coras29a59c32019-05-02 12:52:19 -0700866 svm_fifo_copy_from_chunk (f, f->head_chunk, head, dst, len, &f->head_chunk);
867 head = (head + len) % f->size;
Dave Barach68b0fb02017-02-28 15:15:56 -0500868
Florin Corasa7570b02019-05-02 12:52:19 -0700869 if (PREDICT_FALSE (f->flags & SVM_FIFO_F_GROW))
870 svm_fifo_try_grow (f, head);
Florin Coras2309e7a2019-04-18 18:58:10 -0700871
Sirshak Das28aa5392019-02-05 01:33:33 -0600872 /* store-rel: consumer owned index (paired with load-acq in producer) */
873 clib_atomic_store_rel_n (&f->head, head);
Dave Barach68b0fb02017-02-28 15:15:56 -0500874
Florin Coras0a846802019-04-09 18:29:14 -0700875 return len;
Dave Barach68b0fb02017-02-28 15:15:56 -0500876}
877
Dave Barach68b0fb02017-02-28 15:15:56 -0500878int
Florin Coras4375fa32019-04-19 15:56:00 -0700879svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 len, u8 * dst)
Florin Corasf6359c82017-06-19 12:26:09 -0400880{
Florin Coras4375fa32019-04-19 15:56:00 -0700881 u32 tail, head, cursize, head_idx;
Dave Barach68b0fb02017-02-28 15:15:56 -0500882
Sirshak Das28aa5392019-02-05 01:33:33 -0600883 f_load_head_tail_cons (f, &head, &tail);
884
885 /* current size of fifo can only increase during peek: SPSC */
886 cursize = f_cursize (f, head, tail);
887
Florin Coras4375fa32019-04-19 15:56:00 -0700888 if (PREDICT_FALSE (cursize < offset))
Florin Coras87b15ce2019-04-28 21:16:30 -0700889 return SVM_FIFO_EEMPTY;
Dave Barach68b0fb02017-02-28 15:15:56 -0500890
Florin Coras4375fa32019-04-19 15:56:00 -0700891 len = clib_min (cursize - offset, len);
Florin Coras29a59c32019-05-02 12:52:19 -0700892 head_idx = (head + offset) % f->size;
Florin Coras4375fa32019-04-19 15:56:00 -0700893 if (!svm_fifo_chunk_includes_pos (f->ooo_deq, head_idx))
894 f->ooo_deq = svm_fifo_find_chunk (f, head_idx);
Dave Barach68b0fb02017-02-28 15:15:56 -0500895
Florin Coras4375fa32019-04-19 15:56:00 -0700896 svm_fifo_copy_from_chunk (f, f->ooo_deq, head_idx, dst, len, &f->ooo_deq);
Florin Coras0a846802019-04-09 18:29:14 -0700897 return len;
Dave Barach68b0fb02017-02-28 15:15:56 -0500898}
899
900int
Florin Coras87b15ce2019-04-28 21:16:30 -0700901svm_fifo_dequeue_drop (svm_fifo_t * f, u32 len)
Dave Barach68b0fb02017-02-28 15:15:56 -0500902{
Florin Coras87b15ce2019-04-28 21:16:30 -0700903 u32 total_drop_bytes, tail, head, cursize;
Dave Barach68b0fb02017-02-28 15:15:56 -0500904
Sirshak Das28aa5392019-02-05 01:33:33 -0600905 f_load_head_tail_cons (f, &head, &tail);
906
Florin Coras87b15ce2019-04-28 21:16:30 -0700907 /* number of bytes available */
Sirshak Das28aa5392019-02-05 01:33:33 -0600908 cursize = f_cursize (f, head, tail);
Florin Coras3e350af2017-03-30 02:54:28 -0700909 if (PREDICT_FALSE (cursize == 0))
Florin Coras87b15ce2019-04-28 21:16:30 -0700910 return SVM_FIFO_EEMPTY;
Dave Barach68b0fb02017-02-28 15:15:56 -0500911
Sirshak Das28aa5392019-02-05 01:33:33 -0600912 /* number of bytes we're going to drop */
Florin Coras87b15ce2019-04-28 21:16:30 -0700913 total_drop_bytes = clib_min (cursize, len);
Dave Barach68b0fb02017-02-28 15:15:56 -0500914
Nathan Skrzypczakb4c67752019-06-20 09:58:37 +0200915 svm_fifo_trace_add (f, tail, total_drop_bytes, 3);
916
Sirshak Das28aa5392019-02-05 01:33:33 -0600917 /* move head */
Florin Coras29a59c32019-05-02 12:52:19 -0700918 head = (head + total_drop_bytes) % f->size;
Florin Coras3eb50622017-07-13 01:24:57 -0400919
Sirshak Das28aa5392019-02-05 01:33:33 -0600920 /* store-rel: consumer owned index (paired with load-acq in producer) */
921 clib_atomic_store_rel_n (&f->head, head);
Dave Barach68b0fb02017-02-28 15:15:56 -0500922
923 return total_drop_bytes;
924}
925
Florin Coras25579b42018-06-06 17:55:02 -0700926void
927svm_fifo_dequeue_drop_all (svm_fifo_t * f)
928{
Sirshak Das28aa5392019-02-05 01:33:33 -0600929 /* consumer foreign index */
930 u32 tail = clib_atomic_load_acq_n (&f->tail);
931 /* store-rel: consumer owned index (paired with load-acq in producer) */
932 clib_atomic_store_rel_n (&f->head, tail);
Florin Coras25579b42018-06-06 17:55:02 -0700933}
934
Florin Coras2cba8532018-09-11 16:33:36 -0700935int
Florin Coras88001c62019-04-24 14:44:46 -0700936svm_fifo_segments (svm_fifo_t * f, svm_fifo_seg_t * fs)
Florin Coras2cba8532018-09-11 16:33:36 -0700937{
Sirshak Das28aa5392019-02-05 01:33:33 -0600938 u32 cursize, head, tail, head_idx;
Florin Coras2cba8532018-09-11 16:33:36 -0700939
Sirshak Das28aa5392019-02-05 01:33:33 -0600940 f_load_head_tail_cons (f, &head, &tail);
941
942 /* consumer function, cursize can only increase while we're working */
943 cursize = f_cursize (f, head, tail);
944
Florin Coras2cba8532018-09-11 16:33:36 -0700945 if (PREDICT_FALSE (cursize == 0))
Florin Coras87b15ce2019-04-28 21:16:30 -0700946 return SVM_FIFO_EEMPTY;
Florin Coras2cba8532018-09-11 16:33:36 -0700947
Florin Coras29a59c32019-05-02 12:52:19 -0700948 head_idx = head;
Florin Coras2cba8532018-09-11 16:33:36 -0700949
Sirshak Das28aa5392019-02-05 01:33:33 -0600950 if (tail < head)
Florin Coras2cba8532018-09-11 16:33:36 -0700951 {
Sirshak Das28aa5392019-02-05 01:33:33 -0600952 fs[0].len = f->size - head_idx;
Florin Coras0a846802019-04-09 18:29:14 -0700953 fs[0].data = f->head_chunk->data + head_idx;
Florin Coras2cba8532018-09-11 16:33:36 -0700954 fs[1].len = cursize - fs[0].len;
Florin Coras0a846802019-04-09 18:29:14 -0700955 fs[1].data = f->head_chunk->data;
Florin Coras2cba8532018-09-11 16:33:36 -0700956 }
957 else
958 {
Sirshak Das28aa5392019-02-05 01:33:33 -0600959 fs[0].len = cursize;
Florin Coras0a846802019-04-09 18:29:14 -0700960 fs[0].data = f->head_chunk->data + head_idx;
Florin Coras2cba8532018-09-11 16:33:36 -0700961 fs[1].len = 0;
962 fs[1].data = 0;
963 }
964 return cursize;
965}
966
967void
Florin Coras88001c62019-04-24 14:44:46 -0700968svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_seg_t * fs)
Florin Coras2cba8532018-09-11 16:33:36 -0700969{
Florin Coras29a59c32019-05-02 12:52:19 -0700970 u32 head;
Florin Coras2cba8532018-09-11 16:33:36 -0700971
Sirshak Das28aa5392019-02-05 01:33:33 -0600972 /* consumer owned index */
973 head = f->head;
Sirshak Das28aa5392019-02-05 01:33:33 -0600974
Florin Coras29a59c32019-05-02 12:52:19 -0700975 ASSERT (fs[0].data == f->head_chunk->data + head);
976 head = (head + fs[0].len + fs[1].len) % f->size;
Sirshak Das28aa5392019-02-05 01:33:33 -0600977 /* store-rel: consumer owned index (paired with load-acq in producer) */
978 clib_atomic_store_rel_n (&f->head, head);
979}
980
Florin Coras87b15ce2019-04-28 21:16:30 -0700981/**
982 * Clones fifo
983 *
984 * Assumptions:
985 * - no prod and cons are accessing either dest or src fifo
986 * - fifo is not multi chunk
987 */
Sirshak Das28aa5392019-02-05 01:33:33 -0600988void
989svm_fifo_clone (svm_fifo_t * df, svm_fifo_t * sf)
990{
991 u32 head, tail;
Florin Coras0a846802019-04-09 18:29:14 -0700992 clib_memcpy_fast (df->head_chunk->data, sf->head_chunk->data, sf->size);
Sirshak Das28aa5392019-02-05 01:33:33 -0600993
994 f_load_head_tail_all_acq (sf, &head, &tail);
995 clib_atomic_store_rel_n (&df->head, head);
996 clib_atomic_store_rel_n (&df->tail, tail);
Florin Coras2cba8532018-09-11 16:33:36 -0700997}
998
Dave Barach1f75cfd2017-04-14 16:46:44 -0400999u32
Florin Coras87b15ce2019-04-28 21:16:30 -07001000svm_fifo_n_ooo_segments (svm_fifo_t * f)
Dave Barach1f75cfd2017-04-14 16:46:44 -04001001{
1002 return pool_elts (f->ooo_segments);
1003}
1004
1005ooo_segment_t *
1006svm_fifo_first_ooo_segment (svm_fifo_t * f)
1007{
1008 return pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
1009}
1010
Florin Corasc28764f2017-04-26 00:08:42 -07001011/**
1012 * Set fifo pointers to requested offset
1013 */
1014void
Florin Coras4375fa32019-04-19 15:56:00 -07001015svm_fifo_init_pointers (svm_fifo_t * f, u32 head, u32 tail)
Florin Corasc28764f2017-04-26 00:08:42 -07001016{
Florin Coras29a59c32019-05-02 12:52:19 -07001017 head = head % f->size;
1018 tail = tail % f->size;
Florin Coras4375fa32019-04-19 15:56:00 -07001019 clib_atomic_store_rel_n (&f->head, head);
1020 clib_atomic_store_rel_n (&f->tail, tail);
1021 if (f->flags & SVM_FIFO_F_MULTI_CHUNK)
1022 {
1023 svm_fifo_chunk_t *c;
Florin Coras29a59c32019-05-02 12:52:19 -07001024 c = svm_fifo_find_chunk (f, head);
Florin Coras4375fa32019-04-19 15:56:00 -07001025 ASSERT (c != 0);
1026 f->head_chunk = f->ooo_deq = c;
Florin Coras29a59c32019-05-02 12:52:19 -07001027 c = svm_fifo_find_chunk (f, tail);
Florin Coras4375fa32019-04-19 15:56:00 -07001028 ASSERT (c != 0);
1029 f->tail_chunk = f->ooo_enq = c;
1030 }
Florin Corasc28764f2017-04-26 00:08:42 -07001031}
1032
Florin Coras72b04282019-01-14 17:23:11 -08001033void
1034svm_fifo_add_subscriber (svm_fifo_t * f, u8 subscriber)
1035{
1036 if (f->n_subscribers >= SVM_FIFO_MAX_EVT_SUBSCRIBERS)
1037 return;
1038 f->subscribers[f->n_subscribers++] = subscriber;
1039}
1040
1041void
1042svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber)
1043{
1044 int i;
1045
1046 for (i = 0; i < f->n_subscribers; i++)
1047 {
1048 if (f->subscribers[i] != subscriber)
1049 continue;
1050 f->subscribers[i] = f->subscribers[f->n_subscribers - 1];
1051 f->n_subscribers--;
1052 break;
1053 }
1054}
1055
Florin Coras87b15ce2019-04-28 21:16:30 -07001056u8 *
1057format_ooo_segment (u8 * s, va_list * args)
1058{
1059 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
1060 ooo_segment_t *seg = va_arg (*args, ooo_segment_t *);
1061 u32 normalized_start = (seg->start + f->nitems - f->tail) % f->size;
1062 s = format (s, "[%u, %u], len %u, next %d, prev %d", normalized_start,
1063 (normalized_start + seg->length) % f->size, seg->length,
1064 seg->next, seg->prev);
1065 return s;
1066}
1067
1068u8 *
1069svm_fifo_dump_trace (u8 * s, svm_fifo_t * f)
1070{
1071#if SVM_FIFO_TRACE
1072 svm_fifo_trace_elem_t *seg = 0;
1073 int i = 0;
1074
1075 if (f->trace)
1076 {
1077 vec_foreach (seg, f->trace)
1078 {
1079 s = format (s, "{%u, %u, %u}, ", seg->offset, seg->len, seg->action);
1080 i++;
1081 if (i % 5 == 0)
1082 s = format (s, "\n");
1083 }
1084 s = format (s, "\n");
1085 }
1086 return s;
1087#else
1088 return 0;
1089#endif
1090}
1091
1092u8 *
1093svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose)
1094{
1095 int i, trace_len;
1096 u8 *data = 0;
1097 svm_fifo_trace_elem_t *trace;
1098 u32 offset;
1099 svm_fifo_t *dummy_fifo;
1100
1101 if (!f)
1102 return s;
1103
1104#if SVM_FIFO_TRACE
1105 trace = f->trace;
1106 trace_len = vec_len (trace);
1107#else
1108 trace = 0;
1109 trace_len = 0;
1110#endif
1111
1112 dummy_fifo = svm_fifo_create (f->size);
1113 clib_memset (f->head_chunk->data, 0xFF, f->nitems);
1114 vec_validate (data, f->nitems);
1115 for (i = 0; i < vec_len (data); i++)
1116 data[i] = i;
1117
1118 for (i = 0; i < trace_len; i++)
1119 {
1120 offset = trace[i].offset;
1121 if (trace[i].action == 1)
1122 {
1123 if (verbose)
1124 s = format (s, "adding [%u, %u]:", trace[i].offset,
1125 (trace[i].offset + trace[i].len) % dummy_fifo->size);
1126 svm_fifo_enqueue_with_offset (dummy_fifo, trace[i].offset,
1127 trace[i].len, &data[offset]);
1128 }
1129 else if (trace[i].action == 2)
1130 {
1131 if (verbose)
1132 s = format (s, "adding [%u, %u]:", 0, trace[i].len);
1133 svm_fifo_enqueue (dummy_fifo, trace[i].len, &data[offset]);
1134 }
1135 else if (!no_read)
1136 {
1137 if (verbose)
1138 s = format (s, "read: %u", trace[i].len);
1139 svm_fifo_dequeue_drop (dummy_fifo, trace[i].len);
1140 }
1141 if (verbose)
1142 s = format (s, "%U", format_svm_fifo, dummy_fifo, 1);
1143 }
1144
1145 s = format (s, "result: %U", format_svm_fifo, dummy_fifo, 1);
1146
1147 return s;
1148}
1149
1150u8 *
1151format_ooo_list (u8 * s, va_list * args)
1152{
1153 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
1154 u32 indent = va_arg (*args, u32);
1155 u32 ooo_segment_index = f->ooos_list_head;
1156 ooo_segment_t *seg;
1157
1158 while (ooo_segment_index != OOO_SEGMENT_INVALID_INDEX)
1159 {
1160 seg = pool_elt_at_index (f->ooo_segments, ooo_segment_index);
1161 s = format (s, "%U%U\n", format_white_space, indent, format_ooo_segment,
1162 f, seg);
1163 ooo_segment_index = seg->next;
1164 }
1165
1166 return s;
1167}
1168
1169u8 *
1170format_svm_fifo (u8 * s, va_list * args)
1171{
1172 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
1173 int verbose = va_arg (*args, int);
1174 u32 indent;
1175
1176 if (!s)
1177 return s;
1178
1179 indent = format_get_indent (s);
1180 s = format (s, "cursize %u nitems %u has_event %d\n",
1181 svm_fifo_max_dequeue (f), f->nitems, f->has_event);
1182 s = format (s, "%Uhead %u tail %u segment manager %u\n", format_white_space,
1183 indent, (f->head % f->size), (f->tail % f->size),
1184 f->segment_manager);
1185
1186 if (verbose > 1)
1187 s = format (s, "%Uvpp session %d thread %d app session %d thread %d\n",
1188 format_white_space, indent, f->master_session_index,
1189 f->master_thread_index, f->client_session_index,
1190 f->client_thread_index);
1191
1192 if (verbose)
1193 {
1194 s = format (s, "%Uooo pool %d active elts newest %u\n",
1195 format_white_space, indent, pool_elts (f->ooo_segments),
1196 f->ooos_newest);
1197 if (svm_fifo_has_ooo_data (f))
1198 s = format (s, " %U", format_ooo_list, f, indent, verbose);
1199 }
1200 return s;
1201}
1202
Florin Coras3aa7af32018-06-29 08:44:31 -07001203#endif
Dave Barach68b0fb02017-02-28 15:15:56 -05001204/*
1205 * fd.io coding-style-patch-verification: ON
1206 *
1207 * Local Variables:
1208 * eval: (c-set-style "gnu")
1209 * End:
1210 */