blob: 1723cb4fa95ccebd5d4e6e95725d47e527669256 [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 Corasf22f4e52019-12-19 16:10:58 -080021#include <svm/fifo_segment.h>
Florin Corasf6359c82017-06-19 12:26:09 -040022#include <vppinfra/cpu.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050023
Florin Coras99ad2fc2019-04-18 21:25:49 -070024CLIB_MARCH_FN (svm_fifo_copy_to_chunk, void, svm_fifo_t * f,
25 svm_fifo_chunk_t * c, u32 tail_idx, const u8 * src, u32 len,
Florin Coras4375fa32019-04-19 15:56:00 -070026 svm_fifo_chunk_t ** last)
Florin Coras99ad2fc2019-04-18 21:25:49 -070027{
28 u32 n_chunk;
29
Florin Corasf22f4e52019-12-19 16:10:58 -080030 ASSERT (f_pos_geq (tail_idx, c->start_byte)
31 && f_pos_lt (tail_idx, c->start_byte + c->length));
Florin Coras99ad2fc2019-04-18 21:25:49 -070032
33 tail_idx -= c->start_byte;
34 n_chunk = c->length - tail_idx;
Florin Coras4375fa32019-04-19 15:56:00 -070035 if (n_chunk <= len)
Florin Coras99ad2fc2019-04-18 21:25:49 -070036 {
37 u32 to_copy = len;
38 clib_memcpy_fast (&c->data[tail_idx], src, n_chunk);
Florin Coras4375fa32019-04-19 15:56:00 -070039 c = c->next;
Florin Coras99ad2fc2019-04-18 21:25:49 -070040 while ((to_copy -= n_chunk))
41 {
Florin Coras99ad2fc2019-04-18 21:25:49 -070042 n_chunk = clib_min (c->length, to_copy);
43 clib_memcpy_fast (&c->data[0], src + (len - to_copy), n_chunk);
Florin Coras4375fa32019-04-19 15:56:00 -070044 c = c->length <= to_copy ? c->next : c;
Florin Coras99ad2fc2019-04-18 21:25:49 -070045 }
Florin Coras4375fa32019-04-19 15:56:00 -070046 if (*last)
47 *last = c;
Florin Coras99ad2fc2019-04-18 21:25:49 -070048 }
49 else
50 {
51 clib_memcpy_fast (&c->data[tail_idx], src, len);
52 }
53}
54
55CLIB_MARCH_FN (svm_fifo_copy_from_chunk, void, svm_fifo_t * f,
56 svm_fifo_chunk_t * c, u32 head_idx, u8 * dst, u32 len,
Florin Coras4375fa32019-04-19 15:56:00 -070057 svm_fifo_chunk_t ** last)
Florin Coras99ad2fc2019-04-18 21:25:49 -070058{
59 u32 n_chunk;
60
Florin Corasf22f4e52019-12-19 16:10:58 -080061 ASSERT (f_pos_geq (head_idx, c->start_byte)
62 && f_pos_lt (head_idx, c->start_byte + c->length));
Florin Coras4375fa32019-04-19 15:56:00 -070063
Florin Coras99ad2fc2019-04-18 21:25:49 -070064 head_idx -= c->start_byte;
65 n_chunk = c->length - head_idx;
Florin Coras4375fa32019-04-19 15:56:00 -070066 if (n_chunk <= len)
Florin Coras99ad2fc2019-04-18 21:25:49 -070067 {
68 u32 to_copy = len;
69 clib_memcpy_fast (dst, &c->data[head_idx], n_chunk);
Florin Coras4375fa32019-04-19 15:56:00 -070070 c = c->next;
Florin Coras99ad2fc2019-04-18 21:25:49 -070071 while ((to_copy -= n_chunk))
72 {
Florin Coras99ad2fc2019-04-18 21:25:49 -070073 n_chunk = clib_min (c->length, to_copy);
74 clib_memcpy_fast (dst + (len - to_copy), &c->data[0], n_chunk);
Florin Coras4375fa32019-04-19 15:56:00 -070075 c = c->length <= to_copy ? c->next : c;
Florin Coras99ad2fc2019-04-18 21:25:49 -070076 }
Florin Coras4375fa32019-04-19 15:56:00 -070077 if (*last)
78 *last = c;
Florin Coras99ad2fc2019-04-18 21:25:49 -070079 }
80 else
81 {
82 clib_memcpy_fast (dst, &c->data[head_idx], len);
83 }
84}
85
86#ifndef CLIB_MARCH_VARIANT
87
88static inline void
89svm_fifo_copy_to_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c, u32 tail_idx,
Florin Coras4375fa32019-04-19 15:56:00 -070090 const u8 * src, u32 len, svm_fifo_chunk_t ** last)
Florin Coras99ad2fc2019-04-18 21:25:49 -070091{
92 CLIB_MARCH_FN_SELECT (svm_fifo_copy_to_chunk) (f, c, tail_idx, src, len,
Florin Coras4375fa32019-04-19 15:56:00 -070093 last);
Florin Coras99ad2fc2019-04-18 21:25:49 -070094}
95
96static inline void
97svm_fifo_copy_from_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c, u32 head_idx,
Florin Coras4375fa32019-04-19 15:56:00 -070098 u8 * dst, u32 len, svm_fifo_chunk_t ** last)
Florin Coras99ad2fc2019-04-18 21:25:49 -070099{
100 CLIB_MARCH_FN_SELECT (svm_fifo_copy_from_chunk) (f, c, head_idx, dst, len,
Florin Coras4375fa32019-04-19 15:56:00 -0700101 last);
Florin Coras99ad2fc2019-04-18 21:25:49 -0700102}
103
Florin Corasf03a59a2017-06-09 21:07:32 -0700104static inline u32
Florin Corasf22f4e52019-12-19 16:10:58 -0800105ooo_segment_end_pos (ooo_segment_t * s)
Florin Corasf03a59a2017-06-09 21:07:32 -0700106{
Florin Corasf22f4e52019-12-19 16:10:58 -0800107 return (s->start + s->length);
Florin Corasf03a59a2017-06-09 21:07:32 -0700108}
Dave Barach1f75cfd2017-04-14 16:46:44 -0400109
Florin Coras87b15ce2019-04-28 21:16:30 -0700110void
111svm_fifo_free_ooo_data (svm_fifo_t * f)
Dave Barach1f75cfd2017-04-14 16:46:44 -0400112{
Florin Coras87b15ce2019-04-28 21:16:30 -0700113 pool_free (f->ooo_segments);
114}
115
116static inline ooo_segment_t *
Florin Corasa7570b02019-05-02 12:52:19 -0700117ooo_segment_prev (svm_fifo_t * f, ooo_segment_t * s)
Florin Coras87b15ce2019-04-28 21:16:30 -0700118{
119 if (s->prev == OOO_SEGMENT_INVALID_INDEX)
120 return 0;
121 return pool_elt_at_index (f->ooo_segments, s->prev);
122}
123
124static inline ooo_segment_t *
125ooo_segment_next (svm_fifo_t * f, ooo_segment_t * s)
126{
127 if (s->next == OOO_SEGMENT_INVALID_INDEX)
128 return 0;
129 return pool_elt_at_index (f->ooo_segments, s->next);
130}
131
132static inline ooo_segment_t *
133ooo_segment_alloc (svm_fifo_t * f, u32 start, u32 length)
134{
135 ooo_segment_t *s;
136
137 pool_get (f->ooo_segments, s);
138
139 s->start = start;
140 s->length = length;
141 s->prev = s->next = OOO_SEGMENT_INVALID_INDEX;
142
Dave Barach1f75cfd2017-04-14 16:46:44 -0400143 return s;
144}
145
Florin Coras87b15ce2019-04-28 21:16:30 -0700146static inline void
147ooo_segment_free (svm_fifo_t * f, u32 index)
Florin Coras3eb50622017-07-13 01:24:57 -0400148{
Florin Coras87b15ce2019-04-28 21:16:30 -0700149 ooo_segment_t *cur, *prev = 0, *next = 0;
150 cur = pool_elt_at_index (f->ooo_segments, index);
Florin Coras3eb50622017-07-13 01:24:57 -0400151
Florin Coras87b15ce2019-04-28 21:16:30 -0700152 if (cur->next != OOO_SEGMENT_INVALID_INDEX)
Florin Coras3eb50622017-07-13 01:24:57 -0400153 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700154 next = pool_elt_at_index (f->ooo_segments, cur->next);
155 next->prev = cur->prev;
Florin Coras3eb50622017-07-13 01:24:57 -0400156 }
Florin Coras87b15ce2019-04-28 21:16:30 -0700157
158 if (cur->prev != OOO_SEGMENT_INVALID_INDEX)
159 {
160 prev = pool_elt_at_index (f->ooo_segments, cur->prev);
161 prev->next = cur->next;
162 }
163 else
164 {
165 f->ooos_list_head = cur->next;
166 }
167
168 pool_put (f->ooo_segments, cur);
Florin Coras3eb50622017-07-13 01:24:57 -0400169}
170
Florin Coras87b15ce2019-04-28 21:16:30 -0700171/**
172 * Add segment to fifo's out-of-order segment list. Takes care of merging
173 * adjacent segments and removing overlapping ones.
174 */
175static void
176ooo_segment_add (svm_fifo_t * f, u32 offset, u32 head, u32 tail, u32 length)
Florin Coras3eb50622017-07-13 01:24:57 -0400177{
Florin Coras87b15ce2019-04-28 21:16:30 -0700178 ooo_segment_t *s, *new_s, *prev, *next, *it;
179 u32 new_index, s_end_pos, s_index;
180 u32 offset_pos, offset_end_pos;
Florin Coras3eb50622017-07-13 01:24:57 -0400181
Florin Corasf22f4e52019-12-19 16:10:58 -0800182 ASSERT (offset + length <= f_free_count (f, head, tail));
Florin Coras3eb50622017-07-13 01:24:57 -0400183
Florin Corasf22f4e52019-12-19 16:10:58 -0800184 offset_pos = tail + offset;
185 offset_end_pos = tail + offset + length;
Florin Coras3eb50622017-07-13 01:24:57 -0400186
Florin Coras87b15ce2019-04-28 21:16:30 -0700187 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
Florin Coras3eb50622017-07-13 01:24:57 -0400188
Florin Coras87b15ce2019-04-28 21:16:30 -0700189 if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX)
Florin Coras3eb50622017-07-13 01:24:57 -0400190 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700191 s = ooo_segment_alloc (f, offset_pos, length);
192 f->ooos_list_head = s - f->ooo_segments;
193 f->ooos_newest = f->ooos_list_head;
194 return;
195 }
196
197 /* Find first segment that starts after new segment */
198 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
199 while (s->next != OOO_SEGMENT_INVALID_INDEX
Florin Corasf22f4e52019-12-19 16:10:58 -0800200 && f_pos_lt (s->start, offset_pos))
Florin Coras87b15ce2019-04-28 21:16:30 -0700201 s = pool_elt_at_index (f->ooo_segments, s->next);
202
203 /* If we have a previous and we overlap it, use it as starting point */
Florin Corasa7570b02019-05-02 12:52:19 -0700204 prev = ooo_segment_prev (f, s);
Florin Corasf22f4e52019-12-19 16:10:58 -0800205 if (prev && f_pos_leq (offset_pos, ooo_segment_end_pos (prev)))
Florin Coras87b15ce2019-04-28 21:16:30 -0700206 {
207 s = prev;
Florin Corasf22f4e52019-12-19 16:10:58 -0800208 s_end_pos = ooo_segment_end_pos (s);
Florin Coras87b15ce2019-04-28 21:16:30 -0700209
210 /* Since we have previous, offset start position cannot be smaller
211 * than prev->start. Check tail */
Florin Corasf22f4e52019-12-19 16:10:58 -0800212 ASSERT (f_pos_lt (s->start, offset_pos));
Florin Coras87b15ce2019-04-28 21:16:30 -0700213 goto check_tail;
214 }
215
216 s_index = s - f->ooo_segments;
Florin Corasf22f4e52019-12-19 16:10:58 -0800217 s_end_pos = ooo_segment_end_pos (s);
Florin Coras87b15ce2019-04-28 21:16:30 -0700218
219 /* No overlap, add before current segment */
Florin Corasf22f4e52019-12-19 16:10:58 -0800220 if (f_pos_lt (offset_end_pos, s->start))
Florin Coras87b15ce2019-04-28 21:16:30 -0700221 {
222 new_s = ooo_segment_alloc (f, offset_pos, length);
223 new_index = new_s - f->ooo_segments;
224
225 /* Pool might've moved, get segment again */
226 s = pool_elt_at_index (f->ooo_segments, s_index);
227 if (s->prev != OOO_SEGMENT_INVALID_INDEX)
Florin Coras3eb50622017-07-13 01:24:57 -0400228 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700229 new_s->prev = s->prev;
230 prev = pool_elt_at_index (f->ooo_segments, new_s->prev);
231 prev->next = new_index;
Florin Coras3eb50622017-07-13 01:24:57 -0400232 }
Florin Coras87b15ce2019-04-28 21:16:30 -0700233 else
Florin Coras3eb50622017-07-13 01:24:57 -0400234 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700235 /* New head */
236 f->ooos_list_head = new_index;
Florin Coras3eb50622017-07-13 01:24:57 -0400237 }
Florin Coras87b15ce2019-04-28 21:16:30 -0700238
239 new_s->next = s_index;
240 s->prev = new_index;
241 f->ooos_newest = new_index;
242 return;
243 }
244 /* No overlap, add after current segment */
Florin Corasf22f4e52019-12-19 16:10:58 -0800245 else if (f_pos_gt (offset_pos, s_end_pos))
Florin Coras87b15ce2019-04-28 21:16:30 -0700246 {
247 new_s = ooo_segment_alloc (f, offset_pos, length);
248 new_index = new_s - f->ooo_segments;
249
250 /* Pool might've moved, get segment again */
251 s = pool_elt_at_index (f->ooo_segments, s_index);
252
253 /* Needs to be last */
254 ASSERT (s->next == OOO_SEGMENT_INVALID_INDEX);
255
256 new_s->prev = s_index;
257 s->next = new_index;
258 f->ooos_newest = new_index;
259
260 return;
Florin Coras3eb50622017-07-13 01:24:57 -0400261 }
262
Florin Coras87b15ce2019-04-28 21:16:30 -0700263 /*
264 * Merge needed
265 */
Florin Coras3eb50622017-07-13 01:24:57 -0400266
Florin Coras87b15ce2019-04-28 21:16:30 -0700267 /* Merge at head */
Florin Corasf22f4e52019-12-19 16:10:58 -0800268 if (f_pos_lt (offset_pos, s->start))
Florin Coras87b15ce2019-04-28 21:16:30 -0700269 {
270 s->start = offset_pos;
Florin Corasf22f4e52019-12-19 16:10:58 -0800271 s->length = s_end_pos - s->start;
Florin Coras87b15ce2019-04-28 21:16:30 -0700272 f->ooos_newest = s - f->ooo_segments;
273 }
274
275check_tail:
276
277 /* Overlapping tail */
Florin Corasf22f4e52019-12-19 16:10:58 -0800278 if (f_pos_gt (offset_end_pos, s_end_pos))
Florin Coras87b15ce2019-04-28 21:16:30 -0700279 {
Florin Corasf22f4e52019-12-19 16:10:58 -0800280 s->length = offset_end_pos - s->start;
Florin Coras87b15ce2019-04-28 21:16:30 -0700281
282 /* Remove the completely overlapped segments in the tail */
283 it = ooo_segment_next (f, s);
Florin Corasf22f4e52019-12-19 16:10:58 -0800284 while (it && f_pos_leq (ooo_segment_end_pos (it), offset_end_pos))
Florin Coras87b15ce2019-04-28 21:16:30 -0700285 {
286 next = ooo_segment_next (f, it);
287 ooo_segment_free (f, it - f->ooo_segments);
288 it = next;
289 }
290
291 /* If partial overlap with last, merge */
Florin Corasf22f4e52019-12-19 16:10:58 -0800292 if (it && f_pos_leq (it->start, offset_end_pos))
Florin Coras87b15ce2019-04-28 21:16:30 -0700293 {
Florin Corasf22f4e52019-12-19 16:10:58 -0800294 s->length = ooo_segment_end_pos (it) - s->start;
Florin Coras87b15ce2019-04-28 21:16:30 -0700295 ooo_segment_free (f, it - f->ooo_segments);
296 }
297 f->ooos_newest = s - f->ooo_segments;
298 }
Florin Coras3eb50622017-07-13 01:24:57 -0400299}
300
Florin Coras87b15ce2019-04-28 21:16:30 -0700301/**
302 * Removes segments that can now be enqueued because the fifo's tail has
303 * advanced. Returns the number of bytes added to tail.
304 */
305static int
306ooo_segment_try_collect (svm_fifo_t * f, u32 n_bytes_enqueued, u32 * tail)
Dave Barach1f75cfd2017-04-14 16:46:44 -0400307{
Florin Coras87b15ce2019-04-28 21:16:30 -0700308 u32 s_index, bytes = 0;
309 ooo_segment_t *s;
310 i32 diff;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400311
Florin Coras87b15ce2019-04-28 21:16:30 -0700312 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
Florin Corasf22f4e52019-12-19 16:10:58 -0800313 diff = *tail - s->start;
Florin Coras87b15ce2019-04-28 21:16:30 -0700314
315 ASSERT (diff != n_bytes_enqueued);
316
317 if (diff > n_bytes_enqueued)
318 return 0;
319
320 /* If last tail update overlaps one/multiple ooo segments, remove them */
321 while (0 <= diff && diff < n_bytes_enqueued)
Dave Barach1f75cfd2017-04-14 16:46:44 -0400322 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700323 s_index = s - f->ooo_segments;
324
325 /* Segment end is beyond the tail. Advance tail and remove segment */
326 if (s->length > diff)
327 {
328 bytes = s->length - diff;
Florin Corasf22f4e52019-12-19 16:10:58 -0800329 *tail = *tail + bytes;
Florin Coras87b15ce2019-04-28 21:16:30 -0700330 ooo_segment_free (f, s_index);
331 break;
332 }
333
334 /* If we have next go on */
335 if (s->next != OOO_SEGMENT_INVALID_INDEX)
336 {
337 s = pool_elt_at_index (f->ooo_segments, s->next);
Florin Corasf22f4e52019-12-19 16:10:58 -0800338 diff = *tail - s->start;
Florin Coras87b15ce2019-04-28 21:16:30 -0700339 ooo_segment_free (f, s_index);
340 }
341 /* End of search */
342 else
343 {
344 ooo_segment_free (f, s_index);
345 break;
346 }
Dave Barach1f75cfd2017-04-14 16:46:44 -0400347 }
Florin Coras3eb50622017-07-13 01:24:57 -0400348
Florin Corasf22f4e52019-12-19 16:10:58 -0800349 ASSERT (bytes <= f->size);
Florin Coras87b15ce2019-04-28 21:16:30 -0700350 return bytes;
Florin Corasb59a7052017-04-18 22:07:29 -0700351}
352
Florin Corasf22f4e52019-12-19 16:10:58 -0800353__clib_unused static ooo_segment_t *
Florin Corasa7570b02019-05-02 12:52:19 -0700354ooo_segment_last (svm_fifo_t * f)
355{
356 ooo_segment_t *s;
357
358 if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX)
359 return 0;
360
361 s = svm_fifo_first_ooo_segment (f);
362 while (s->next != OOO_SEGMENT_INVALID_INDEX)
363 s = pool_elt_at_index (f->ooo_segments, s->next);
364 return s;
365}
366
Florin Coras0a846802019-04-09 18:29:14 -0700367void
368svm_fifo_init (svm_fifo_t * f, u32 size)
369{
Florin Corasf22f4e52019-12-19 16:10:58 -0800370 svm_fifo_chunk_t *c, *prev;
Florin Coras2de9c0f2020-02-02 19:30:39 +0000371 u32 min_alloc;
Florin Corasf22f4e52019-12-19 16:10:58 -0800372
Florin Coras0a846802019-04-09 18:29:14 -0700373 f->size = size;
Florin Coras0a846802019-04-09 18:29:14 -0700374 f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
Florin Coras0a846802019-04-09 18:29:14 -0700375 f->segment_index = SVM_FIFO_INVALID_INDEX;
376 f->refcnt = 1;
Florin Coras73cad332019-08-28 17:12:32 -0700377 f->head = f->tail = f->flags = 0;
Florin Corasf22f4e52019-12-19 16:10:58 -0800378 f->head_chunk = f->tail_chunk = f->start_chunk;
379 f->ooo_deq = f->ooo_enq = 0;
Florin Coras0a846802019-04-09 18:29:14 -0700380
Florin Coras2de9c0f2020-02-02 19:30:39 +0000381 min_alloc = size > 32 << 10 ? size >> 3 : 4096;
382 min_alloc = clib_min (min_alloc, 64 << 10);
Florin Corasf22f4e52019-12-19 16:10:58 -0800383 f->min_alloc = min_alloc;
Florin Coraseaacce42019-07-02 13:07:37 -0700384
Florin Corasf22f4e52019-12-19 16:10:58 -0800385 /*
386 * Initialize chunks
387 */
Florin Coraseaacce42019-07-02 13:07:37 -0700388 f->start_chunk->start_byte = 0;
389 prev = f->start_chunk;
390 c = prev->next;
391
Florin Corasf22f4e52019-12-19 16:10:58 -0800392 while (c)
Florin Coraseaacce42019-07-02 13:07:37 -0700393 {
394 c->start_byte = prev->start_byte + prev->length;
Florin Coraseaacce42019-07-02 13:07:37 -0700395 prev = c;
396 c = c->next;
397 }
398}
399
Florin Corasf22f4e52019-12-19 16:10:58 -0800400void
401svm_fifo_init_ooo_lookup (svm_fifo_t * f, u8 ooo_type)
402{
403 if (ooo_type == 0)
404 {
405 ASSERT (!rb_tree_is_init (&f->ooo_enq_lookup));
406 rb_tree_init (&f->ooo_enq_lookup);
407 }
408 else
409 {
410 ASSERT (!rb_tree_is_init (&f->ooo_deq_lookup));
411 rb_tree_init (&f->ooo_deq_lookup);
412 }
413}
414
Florin Corasb095a3c2019-04-25 12:58:46 -0700415/**
416 * Creates a fifo in the current heap. Fails vs blow up the process
417 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500418svm_fifo_t *
Florin Corasf22f4e52019-12-19 16:10:58 -0800419svm_fifo_alloc (u32 data_size_in_bytes)
Dave Barach68b0fb02017-02-28 15:15:56 -0500420{
Dave Barach818eb542017-08-02 13:56:13 -0400421 u32 rounded_data_size;
Florin Corascefd5d82019-05-05 13:19:57 -0700422 svm_fifo_chunk_t *c;
423 svm_fifo_t *f;
Dave Barach68b0fb02017-02-28 15:15:56 -0500424
Florin Corascefd5d82019-05-05 13:19:57 -0700425 f = clib_mem_alloc_aligned_or_null (sizeof (*f), CLIB_CACHE_LINE_BYTES);
Dave Barach68b0fb02017-02-28 15:15:56 -0500426 if (f == 0)
427 return 0;
428
Dave Barachb7b92992018-10-17 10:38:51 -0400429 clib_memset (f, 0, sizeof (*f));
Florin Corascefd5d82019-05-05 13:19:57 -0700430
431 /* always round fifo data size to the next highest power-of-two */
432 rounded_data_size = (1 << (max_log2 (data_size_in_bytes)));
433 c = clib_mem_alloc_aligned_or_null (sizeof (*c) + rounded_data_size,
434 CLIB_CACHE_LINE_BYTES);
435 if (!c)
436 {
437 clib_mem_free (f);
438 return 0;
439 }
440
Florin Corasf22f4e52019-12-19 16:10:58 -0800441 clib_memset (c, 0, sizeof (*c));
Florin Corascefd5d82019-05-05 13:19:57 -0700442 c->start_byte = 0;
443 c->length = data_size_in_bytes;
Florin Corasf22f4e52019-12-19 16:10:58 -0800444 c->enq_rb_index = RBTREE_TNIL_INDEX;
445 c->deq_rb_index = RBTREE_TNIL_INDEX;
Florin Corascefd5d82019-05-05 13:19:57 -0700446 f->start_chunk = f->end_chunk = c;
447
Florin Coras0a846802019-04-09 18:29:14 -0700448 return f;
Dave Barach68b0fb02017-02-28 15:15:56 -0500449}
450
Florin Corasb095a3c2019-04-25 12:58:46 -0700451/**
452 * Creates a fifo chunk in the current heap
453 */
454svm_fifo_chunk_t *
455svm_fifo_chunk_alloc (u32 size)
456{
457 svm_fifo_chunk_t *c;
458 u32 rounded_size;
459
460 /* round chunk size to the next highest power-of-two */
461 rounded_size = (1 << (max_log2 (size)));
462 c = clib_mem_alloc_aligned_or_null (sizeof (*c) + rounded_size,
463 CLIB_CACHE_LINE_BYTES);
464 if (c == 0)
465 return 0;
466
467 clib_memset (c, 0, sizeof (*c));
468 c->length = rounded_size;
469 return c;
470}
471
Florin Corasf22f4e52019-12-19 16:10:58 -0800472/**
473 * Find chunk for given byte position
474 *
475 * @param f fifo
476 * @param pos normalized position in fifo
477 *
478 * @return chunk that includes given position or 0
479 */
480static svm_fifo_chunk_t *
481svm_fifo_find_chunk (svm_fifo_t * f, u32 pos)
Florin Coras4375fa32019-04-19 15:56:00 -0700482{
Florin Corasf22f4e52019-12-19 16:10:58 -0800483 svm_fifo_chunk_t *c;
484
485 c = f->start_chunk;
486 while (c && !f_chunk_includes_pos (c, pos))
487 c = c->next;
488
489 return c;
490}
491
492static svm_fifo_chunk_t *
493svm_fifo_find_next_chunk (svm_fifo_t * f, svm_fifo_chunk_t * start, u32 pos)
494{
495 svm_fifo_chunk_t *c;
496
497 ASSERT (start != 0);
498
499 c = start;
500 while (c && !f_chunk_includes_pos (c, pos))
501 c = c->next;
502
503 return c;
504}
505
506u32
507svm_fifo_max_read_chunk (svm_fifo_t * f)
508{
509 u32 head, tail, end_chunk;
510
511 f_load_head_tail_cons (f, &head, &tail);
512 ASSERT (!f->head_chunk || f_chunk_includes_pos (f->head_chunk, head));
513
514 if (!f->head_chunk)
515 {
516 f->head_chunk = svm_fifo_find_chunk (f, head);
517 if (PREDICT_FALSE (!f->head_chunk))
518 return 0;
519 }
520
521 end_chunk = f_chunk_end (f->head_chunk);
522
523 return f_pos_lt (end_chunk, tail) ? end_chunk - head : tail - head;
524}
525
526u32
527svm_fifo_max_write_chunk (svm_fifo_t * f)
528{
529 u32 head, tail;
530
531 f_load_head_tail_prod (f, &head, &tail);
532 ASSERT (!f->tail_chunk || f_chunk_includes_pos (f->tail_chunk, tail));
533
534 return f->tail_chunk ? f_chunk_end (f->tail_chunk) - tail : 0;
Florin Coras4375fa32019-04-19 15:56:00 -0700535}
536
Florin Corasb0208062019-12-12 12:09:29 -0800537static rb_node_t *
Florin Corasf22f4e52019-12-19 16:10:58 -0800538f_find_node_rbtree (rb_tree_t * rt, u32 pos)
Florin Corasb0208062019-12-12 12:09:29 -0800539{
540 rb_node_t *cur, *prev;
541
542 cur = rb_node (rt, rt->root);
543 if (PREDICT_FALSE (rb_node_is_tnil (rt, cur)))
544 return 0;
545
546 while (pos != cur->key)
547 {
548 prev = cur;
Florin Corasf22f4e52019-12-19 16:10:58 -0800549 if (f_pos_lt (pos, cur->key))
Florin Corasb0208062019-12-12 12:09:29 -0800550 {
551 cur = rb_node_left (rt, cur);
552 if (rb_node_is_tnil (rt, cur))
553 {
554 cur = rb_tree_predecessor (rt, prev);
555 break;
556 }
557 }
558 else
559 {
560 cur = rb_node_right (rt, cur);
561 if (rb_node_is_tnil (rt, cur))
562 {
563 cur = prev;
564 break;
565 }
566 }
567 }
568
569 if (rb_node_is_tnil (rt, cur))
570 return 0;
571
572 return cur;
573}
574
575static svm_fifo_chunk_t *
Florin Corasf22f4e52019-12-19 16:10:58 -0800576f_find_chunk_rbtree (rb_tree_t * rt, u32 pos)
Florin Corasb0208062019-12-12 12:09:29 -0800577{
578 svm_fifo_chunk_t *c;
579 rb_node_t *n;
580
Florin Corasf22f4e52019-12-19 16:10:58 -0800581 if (!rb_tree_is_init (rt))
582 return 0;
583
584 n = f_find_node_rbtree (rt, pos);
Florin Corasb0208062019-12-12 12:09:29 -0800585 if (!n)
586 return 0;
587 c = uword_to_pointer (n->opaque, svm_fifo_chunk_t *);
Florin Corasf22f4e52019-12-19 16:10:58 -0800588 if (f_chunk_includes_pos (c, pos))
Florin Corasb0208062019-12-12 12:09:29 -0800589 return c;
590
591 return 0;
592}
593
Florin Corasb0208062019-12-12 12:09:29 -0800594static void
Florin Corasf22f4e52019-12-19 16:10:58 -0800595f_update_ooo_enq (svm_fifo_t * f, u32 start_pos, u32 end_pos)
Florin Corasb0208062019-12-12 12:09:29 -0800596{
597 rb_tree_t *rt = &f->ooo_enq_lookup;
598 svm_fifo_chunk_t *c;
599 rb_node_t *cur;
600
Florin Corasf22f4e52019-12-19 16:10:58 -0800601 /* Use linear search if rbtree is not initialized */
602 if (PREDICT_FALSE (!rb_tree_is_init (rt)))
603 {
604 f->ooo_enq = svm_fifo_find_next_chunk (f, f->tail_chunk, start_pos);
605 return;
606 }
Florin Corasb0208062019-12-12 12:09:29 -0800607
608 if (rt->root == RBTREE_TNIL_INDEX)
609 {
610 c = f->tail_chunk;
Florin Corasf22f4e52019-12-19 16:10:58 -0800611 ASSERT (c->enq_rb_index == RBTREE_TNIL_INDEX);
612 c->enq_rb_index = rb_tree_add_custom (rt, c->start_byte,
613 pointer_to_uword (c), f_pos_lt);
Florin Corasb0208062019-12-12 12:09:29 -0800614 }
615 else
616 {
Florin Corasf22f4e52019-12-19 16:10:58 -0800617 cur = f_find_node_rbtree (rt, start_pos);
Florin Corasb0208062019-12-12 12:09:29 -0800618 c = uword_to_pointer (cur->opaque, svm_fifo_chunk_t *);
Florin Corasf22f4e52019-12-19 16:10:58 -0800619 ASSERT (f_pos_leq (c->start_byte, start_pos));
Florin Coras4375fa32019-04-19 15:56:00 -0700620 }
621
Florin Corasf22f4e52019-12-19 16:10:58 -0800622 if (f_chunk_includes_pos (c, start_pos))
Florin Corasb0208062019-12-12 12:09:29 -0800623 f->ooo_enq = c;
624
Florin Corasf22f4e52019-12-19 16:10:58 -0800625 if (f_chunk_includes_pos (c, end_pos))
Florin Corasb0208062019-12-12 12:09:29 -0800626 return;
627
628 do
629 {
630 c = c->next;
Florin Corasf22f4e52019-12-19 16:10:58 -0800631 if (!c || c->enq_rb_index != RBTREE_TNIL_INDEX)
Florin Corasb0208062019-12-12 12:09:29 -0800632 break;
633
Florin Corasf22f4e52019-12-19 16:10:58 -0800634 c->enq_rb_index = rb_tree_add_custom (rt, c->start_byte,
635 pointer_to_uword (c), f_pos_lt);
Florin Corasb0208062019-12-12 12:09:29 -0800636
Florin Corasf22f4e52019-12-19 16:10:58 -0800637 if (f_chunk_includes_pos (c, start_pos))
Florin Corasb0208062019-12-12 12:09:29 -0800638 f->ooo_enq = c;
Florin Corasb0208062019-12-12 12:09:29 -0800639 }
Florin Corasf22f4e52019-12-19 16:10:58 -0800640 while (!f_chunk_includes_pos (c, end_pos));
Florin Corasb0208062019-12-12 12:09:29 -0800641}
642
643static void
Florin Corasf22f4e52019-12-19 16:10:58 -0800644f_update_ooo_deq (svm_fifo_t * f, u32 start_pos, u32 end_pos)
Florin Corasb0208062019-12-12 12:09:29 -0800645{
646 rb_tree_t *rt = &f->ooo_deq_lookup;
647 rb_node_t *cur;
648 svm_fifo_chunk_t *c;
649
Florin Corasf22f4e52019-12-19 16:10:58 -0800650 /* Use linear search if rbtree is not initialized */
651 if (PREDICT_FALSE (!rb_tree_is_init (rt)))
652 {
653 f->ooo_deq = svm_fifo_find_chunk (f, start_pos);
654 return;
655 }
Florin Corasb0208062019-12-12 12:09:29 -0800656
657 if (rt->root == RBTREE_TNIL_INDEX)
658 {
Florin Corasf22f4e52019-12-19 16:10:58 -0800659 c = f->start_chunk;
660 ASSERT (c->deq_rb_index == RBTREE_TNIL_INDEX);
661 c->deq_rb_index = rb_tree_add_custom (rt, c->start_byte,
662 pointer_to_uword (c), f_pos_lt);
Florin Corasb0208062019-12-12 12:09:29 -0800663 }
664 else
665 {
Florin Corasf22f4e52019-12-19 16:10:58 -0800666 cur = f_find_node_rbtree (rt, start_pos);
Florin Corasb0208062019-12-12 12:09:29 -0800667 c = uword_to_pointer (cur->opaque, svm_fifo_chunk_t *);
Florin Corasf22f4e52019-12-19 16:10:58 -0800668 ASSERT (f_pos_leq (c->start_byte, start_pos));
Florin Corasb0208062019-12-12 12:09:29 -0800669 }
670
Florin Corasf22f4e52019-12-19 16:10:58 -0800671 if (f_chunk_includes_pos (c, start_pos))
Florin Corasb0208062019-12-12 12:09:29 -0800672 f->ooo_deq = c;
673
Florin Corasf22f4e52019-12-19 16:10:58 -0800674 if (f_chunk_includes_pos (c, end_pos))
Florin Corasb0208062019-12-12 12:09:29 -0800675 return;
676
677 do
678 {
679 c = c->next;
Florin Corasf22f4e52019-12-19 16:10:58 -0800680 if (!c || c->deq_rb_index != RBTREE_TNIL_INDEX)
Florin Corasb0208062019-12-12 12:09:29 -0800681 break;
682
Florin Corasf22f4e52019-12-19 16:10:58 -0800683 c->deq_rb_index = rb_tree_add_custom (rt, c->start_byte,
684 pointer_to_uword (c), f_pos_lt);
Florin Corasb0208062019-12-12 12:09:29 -0800685
Florin Corasf22f4e52019-12-19 16:10:58 -0800686 if (f_chunk_includes_pos (c, start_pos))
Florin Corasb0208062019-12-12 12:09:29 -0800687 f->ooo_deq = c;
Florin Corasb0208062019-12-12 12:09:29 -0800688 }
Florin Corasf22f4e52019-12-19 16:10:58 -0800689 while (!f_chunk_includes_pos (c, end_pos));
Florin Corasb0208062019-12-12 12:09:29 -0800690}
691
692static svm_fifo_chunk_t *
Florin Corasf22f4e52019-12-19 16:10:58 -0800693f_lookup_clear_enq_chunks (svm_fifo_t * f, svm_fifo_chunk_t * start,
694 u32 end_pos)
Florin Corasb0208062019-12-12 12:09:29 -0800695{
Florin Corasf22f4e52019-12-19 16:10:58 -0800696 rb_tree_t *rt = &f->ooo_enq_lookup;
Florin Corasb0208062019-12-12 12:09:29 -0800697 svm_fifo_chunk_t *c;
698 rb_node_t *n;
699
Florin Corasb0208062019-12-12 12:09:29 -0800700 c = start;
Florin Corasf22f4e52019-12-19 16:10:58 -0800701 while (c && !f_chunk_includes_pos (c, end_pos))
Florin Corasb0208062019-12-12 12:09:29 -0800702 {
Florin Corasf22f4e52019-12-19 16:10:58 -0800703 if (c->enq_rb_index != RBTREE_TNIL_INDEX)
Florin Corasb0208062019-12-12 12:09:29 -0800704 {
Florin Corasf22f4e52019-12-19 16:10:58 -0800705 n = rb_node (rt, c->enq_rb_index);
706 rb_tree_del_node (rt, n);
707 c->enq_rb_index = RBTREE_TNIL_INDEX;
Florin Corasb0208062019-12-12 12:09:29 -0800708 }
709
Florin Corasb0208062019-12-12 12:09:29 -0800710 c = c->next;
711 }
Florin Corasf22f4e52019-12-19 16:10:58 -0800712
713 /* No ooo segments left, so make sure the current chunk
714 * is not tracked in the enq rbtree */
715 if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX
716 && c && c->enq_rb_index != RBTREE_TNIL_INDEX)
717 {
718 n = rb_node (rt, c->enq_rb_index);
719 rb_tree_del_node (rt, n);
720 c->enq_rb_index = RBTREE_TNIL_INDEX;
721 }
Florin Corasb0208062019-12-12 12:09:29 -0800722
723 return c;
Florin Coras99ad2fc2019-04-18 21:25:49 -0700724}
725
Florin Corasf22f4e52019-12-19 16:10:58 -0800726static svm_fifo_chunk_t *
727f_lookup_clear_deq_chunks (svm_fifo_t * f, svm_fifo_chunk_t * start,
728 u32 end_pos)
Florin Corasa7570b02019-05-02 12:52:19 -0700729{
Florin Corasf22f4e52019-12-19 16:10:58 -0800730 rb_tree_t *rt = &f->ooo_deq_lookup;
731 svm_fifo_chunk_t *c;
732 rb_node_t *n;
Florin Corasa7570b02019-05-02 12:52:19 -0700733
Florin Corasf22f4e52019-12-19 16:10:58 -0800734 c = start;
735 while (c && !f_chunk_includes_pos (c, end_pos))
Florin Corasa7570b02019-05-02 12:52:19 -0700736 {
Florin Corasf22f4e52019-12-19 16:10:58 -0800737 if (c->deq_rb_index != RBTREE_TNIL_INDEX)
738 {
739 n = rb_node (rt, c->deq_rb_index);
740 rb_tree_del_node (rt, n);
741 c->deq_rb_index = RBTREE_TNIL_INDEX;
742 }
743
Florin Corasa7570b02019-05-02 12:52:19 -0700744 c = c->next;
745 }
Florin Corasa7570b02019-05-02 12:52:19 -0700746
Florin Corasf22f4e52019-12-19 16:10:58 -0800747 return c;
Florin Corasa7570b02019-05-02 12:52:19 -0700748}
749
750void
Florin Corasb095a3c2019-04-25 12:58:46 -0700751svm_fifo_free_chunk_lookup (svm_fifo_t * f)
752{
Florin Corasb0208062019-12-12 12:09:29 -0800753 rb_tree_free_nodes (&f->ooo_enq_lookup);
754 rb_tree_free_nodes (&f->ooo_deq_lookup);
Florin Corasb095a3c2019-04-25 12:58:46 -0700755}
756
757void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700758svm_fifo_free (svm_fifo_t * f)
759{
Dave Barach52851e62017-08-07 09:35:25 -0400760 ASSERT (f->refcnt > 0);
761
762 if (--f->refcnt == 0)
763 {
Florin Corasb095a3c2019-04-25 12:58:46 -0700764 /* ooo data is not allocated on segment heap */
765 svm_fifo_free_chunk_lookup (f);
Dave Barach52851e62017-08-07 09:35:25 -0400766 clib_mem_free (f);
767 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700768}
769
Florin Coras7fb0fe12018-04-09 09:24:52 -0700770void
Florin Coras87b15ce2019-04-28 21:16:30 -0700771svm_fifo_overwrite_head (svm_fifo_t * f, u8 * src, u32 len)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700772{
Florin Coras0a846802019-04-09 18:29:14 -0700773 u32 n_chunk;
Sirshak Das28aa5392019-02-05 01:33:33 -0600774 u32 head, tail, head_idx;
Florin Coras0a846802019-04-09 18:29:14 -0700775 svm_fifo_chunk_t *c;
776
Florin Corasf22f4e52019-12-19 16:10:58 -0800777 ASSERT (len <= f->size);
Sirshak Das28aa5392019-02-05 01:33:33 -0600778
779 f_load_head_tail_cons (f, &head, &tail);
Florin Corasf22f4e52019-12-19 16:10:58 -0800780
781 if (!f->head_chunk)
782 f->head_chunk = svm_fifo_find_chunk (f, head);
783
Florin Coras0a846802019-04-09 18:29:14 -0700784 c = f->head_chunk;
Florin Coras29a59c32019-05-02 12:52:19 -0700785 head_idx = head - c->start_byte;
Florin Coras0a846802019-04-09 18:29:14 -0700786 n_chunk = c->length - head_idx;
787 if (len <= n_chunk)
Florin Coras87b15ce2019-04-28 21:16:30 -0700788 clib_memcpy_fast (&c->data[head_idx], src, len);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700789 else
790 {
Florin Corasf22f4e52019-12-19 16:10:58 -0800791 ASSERT (len - n_chunk <= c->next->length);
Florin Coras87b15ce2019-04-28 21:16:30 -0700792 clib_memcpy_fast (&c->data[head_idx], src, n_chunk);
793 clib_memcpy_fast (&c->next->data[0], src + n_chunk, len - n_chunk);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700794 }
795}
Dave Barach68b0fb02017-02-28 15:15:56 -0500796
Florin Corasf22f4e52019-12-19 16:10:58 -0800797static int
Florin Coras9e61d9a2020-02-05 21:13:18 +0000798f_try_chunk_alloc (svm_fifo_t * f, u32 head, u32 tail, u32 len)
Florin Corasf22f4e52019-12-19 16:10:58 -0800799{
Florin Coras9e61d9a2020-02-05 21:13:18 +0000800 svm_fifo_chunk_t *c, *cur, *prev;
Florin Corasf22f4e52019-12-19 16:10:58 -0800801 u32 alloc_size, free_alloced;
802
803 free_alloced = f_chunk_end (f->end_chunk) - tail;
Florin Corasf22f4e52019-12-19 16:10:58 -0800804
805 alloc_size = clib_min (f->min_alloc, f->size - (tail - head));
806 alloc_size = clib_max (alloc_size, len - free_alloced);
807
808 c = fsh_alloc_chunk (f->fs_hdr, f->slice_index, alloc_size);
809 if (PREDICT_FALSE (!c))
810 return -1;
811
Florin Coras9e61d9a2020-02-05 21:13:18 +0000812 cur = c;
813 prev = f->end_chunk;
814
815 while (cur)
816 {
817 cur->start_byte = prev->start_byte + prev->length;
818 cur->enq_rb_index = RBTREE_TNIL_INDEX;
819 cur->deq_rb_index = RBTREE_TNIL_INDEX;
820
821 prev = cur;
822 cur = cur->next;
823 }
824
825 prev->next = 0;
826 f->end_chunk->next = c;
827 f->end_chunk = prev;
828
829 if (!f->tail_chunk)
830 f->tail_chunk = c;
831
Florin Corasf22f4e52019-12-19 16:10:58 -0800832 return 0;
833}
834
Florin Coras99ad2fc2019-04-18 21:25:49 -0700835int
Florin Coras87b15ce2019-04-28 21:16:30 -0700836svm_fifo_enqueue (svm_fifo_t * f, u32 len, const u8 * src)
Dave Barach68b0fb02017-02-28 15:15:56 -0500837{
Florin Coras99ad2fc2019-04-18 21:25:49 -0700838 u32 tail, head, free_count;
Florin Corasf22f4e52019-12-19 16:10:58 -0800839 svm_fifo_chunk_t *old_tail_c;
840
841 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
Florin Coras99ad2fc2019-04-18 21:25:49 -0700842
843 f_load_head_tail_prod (f, &head, &tail);
844
845 /* free space in fifo can only increase during enqueue: SPSC */
846 free_count = f_free_count (f, head, tail);
847
Florin Coras99ad2fc2019-04-18 21:25:49 -0700848 if (PREDICT_FALSE (free_count == 0))
Florin Coras87b15ce2019-04-28 21:16:30 -0700849 return SVM_FIFO_EFULL;
Florin Coras99ad2fc2019-04-18 21:25:49 -0700850
851 /* number of bytes we're going to copy */
852 len = clib_min (free_count, len);
Florin Corasf22f4e52019-12-19 16:10:58 -0800853
854 if (f_pos_gt (tail + len, f_chunk_end (f->end_chunk)))
855 {
Florin Coras9e61d9a2020-02-05 21:13:18 +0000856 if (PREDICT_FALSE (f_try_chunk_alloc (f, head, tail, len)))
Florin Corasf22f4e52019-12-19 16:10:58 -0800857 {
858 len = f_chunk_end (f->end_chunk) - tail;
859 if (!len)
860 return SVM_FIFO_EGROW;
861 }
862 }
863
864 old_tail_c = f->tail_chunk;
865
Florin Coras29a59c32019-05-02 12:52:19 -0700866 svm_fifo_copy_to_chunk (f, f->tail_chunk, tail, src, len, &f->tail_chunk);
Florin Corasf22f4e52019-12-19 16:10:58 -0800867 tail = tail + len;
Florin Coras99ad2fc2019-04-18 21:25:49 -0700868
Florin Coras87b15ce2019-04-28 21:16:30 -0700869 svm_fifo_trace_add (f, head, len, 2);
Florin Coras99ad2fc2019-04-18 21:25:49 -0700870
871 /* collect out-of-order segments */
872 if (PREDICT_FALSE (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX))
Florin Coraseaacce42019-07-02 13:07:37 -0700873 {
874 len += ooo_segment_try_collect (f, len, &tail);
Florin Corasf22f4e52019-12-19 16:10:58 -0800875 /* Tail chunk might've changed even if nothing was collected */
876 f->tail_chunk = f_lookup_clear_enq_chunks (f, old_tail_c, tail);
877 f->ooo_enq = 0;
Florin Coraseaacce42019-07-02 13:07:37 -0700878 }
Florin Coras99ad2fc2019-04-18 21:25:49 -0700879
880 /* store-rel: producer owned index (paired with load-acq in consumer) */
881 clib_atomic_store_rel_n (&f->tail, tail);
882
883 return len;
884}
885
886/**
887 * Enqueue a future segment.
888 *
889 * Two choices: either copies the entire segment, or copies nothing
890 * Returns 0 of the entire segment was copied
891 * Returns -1 if none of the segment was copied due to lack of space
892 */
893int
894svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 len, u8 * src)
895{
Florin Corasf22f4e52019-12-19 16:10:58 -0800896 u32 tail, head, free_count, enq_pos;
Florin Coras99ad2fc2019-04-18 21:25:49 -0700897
898 f_load_head_tail_prod (f, &head, &tail);
899
900 /* free space in fifo can only increase during enqueue: SPSC */
901 free_count = f_free_count (f, head, tail);
Florin Corasf22f4e52019-12-19 16:10:58 -0800902 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
Florin Coras99ad2fc2019-04-18 21:25:49 -0700903
904 /* will this request fit? */
905 if ((len + offset) > free_count)
Florin Coras87b15ce2019-04-28 21:16:30 -0700906 return SVM_FIFO_EFULL;
Florin Coras99ad2fc2019-04-18 21:25:49 -0700907
Florin Corasf22f4e52019-12-19 16:10:58 -0800908 enq_pos = tail + offset;
909
910 if (f_pos_gt (enq_pos + len, f_chunk_end (f->end_chunk)))
911 {
Florin Coras9e61d9a2020-02-05 21:13:18 +0000912 if (PREDICT_FALSE (f_try_chunk_alloc (f, head, tail, offset + len)))
Florin Corasf22f4e52019-12-19 16:10:58 -0800913 return SVM_FIFO_EGROW;
914 }
915
Florin Coras99ad2fc2019-04-18 21:25:49 -0700916 svm_fifo_trace_add (f, offset, len, 1);
Florin Coras99ad2fc2019-04-18 21:25:49 -0700917 ooo_segment_add (f, offset, head, tail, len);
Florin Coras4375fa32019-04-19 15:56:00 -0700918
Florin Corasf22f4e52019-12-19 16:10:58 -0800919 if (!f->ooo_enq || !f_chunk_includes_pos (f->ooo_enq, enq_pos))
920 f_update_ooo_enq (f, enq_pos, enq_pos + len);
Florin Coras4375fa32019-04-19 15:56:00 -0700921
Florin Corasf22f4e52019-12-19 16:10:58 -0800922 svm_fifo_copy_to_chunk (f, f->ooo_enq, enq_pos, src, len, &f->ooo_enq);
Florin Coras99ad2fc2019-04-18 21:25:49 -0700923
924 return 0;
925}
926
Florin Corasa7570b02019-05-02 12:52:19 -0700927/**
928 * Advance tail
929 */
930void
931svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 len)
932{
933 u32 tail;
934
935 ASSERT (len <= svm_fifo_max_enqueue_prod (f));
936 /* load-relaxed: producer owned index */
937 tail = f->tail;
Florin Corasf22f4e52019-12-19 16:10:58 -0800938 tail = tail + len;
Florin Coraseaacce42019-07-02 13:07:37 -0700939
Florin Corasf22f4e52019-12-19 16:10:58 -0800940 if (rb_tree_is_init (&f->ooo_enq_lookup))
941 {
942 f->tail_chunk = f_lookup_clear_enq_chunks (f, f->tail_chunk, tail);
943 f->ooo_enq = 0;
944 }
945 else
946 {
947 f->tail_chunk = svm_fifo_find_next_chunk (f, f->tail_chunk, tail);
948 }
Florin Coraseaacce42019-07-02 13:07:37 -0700949
Florin Corasa7570b02019-05-02 12:52:19 -0700950 /* store-rel: producer owned index (paired with load-acq in consumer) */
951 clib_atomic_store_rel_n (&f->tail, tail);
952}
953
Florin Corasf22f4e52019-12-19 16:10:58 -0800954always_inline svm_fifo_chunk_t *
955f_unlink_chunks (svm_fifo_t * f, u32 end_pos, u8 maybe_ooo)
956{
957 svm_fifo_chunk_t *start, *prev = 0, *c;
958 rb_tree_t *rt;
959 rb_node_t *n;
960
961 ASSERT (!f_chunk_includes_pos (f->start_chunk, end_pos));
962
963 if (maybe_ooo)
964 rt = &f->ooo_deq_lookup;
965
966 c = f->start_chunk;
967
968 do
969 {
970 if (maybe_ooo && c->deq_rb_index != RBTREE_TNIL_INDEX)
971 {
972 n = rb_node (rt, c->deq_rb_index);
973 ASSERT (n == f_find_node_rbtree (rt, c->start_byte));
974 rb_tree_del_node (rt, n);
975 c->deq_rb_index = RBTREE_TNIL_INDEX;
976 }
977 if (!c->next)
978 break;
979 prev = c;
980 c = c->next;
981 }
982 while (!f_chunk_includes_pos (c, end_pos));
983
984 if (maybe_ooo)
985 {
986 if (f->ooo_deq && f_pos_lt (f->ooo_deq->start_byte, f_chunk_end (c)))
987 f->ooo_deq = 0;
988 }
989 else
990 {
991 if (PREDICT_FALSE (f->ooo_deq != 0))
992 f->ooo_deq = 0;
993 }
994
995 /* Avoid unlinking the last chunk */
996 if (!prev)
997 return 0;
998
999 prev->next = 0;
1000 start = f->start_chunk;
1001 f->start_chunk = c;
1002
1003 return start;
1004}
1005
Florin Coras99ad2fc2019-04-18 21:25:49 -07001006int
Florin Coras87b15ce2019-04-28 21:16:30 -07001007svm_fifo_dequeue (svm_fifo_t * f, u32 len, u8 * dst)
Florin Coras99ad2fc2019-04-18 21:25:49 -07001008{
1009 u32 tail, head, cursize;
Dave Barach68b0fb02017-02-28 15:15:56 -05001010
Sirshak Das28aa5392019-02-05 01:33:33 -06001011 f_load_head_tail_cons (f, &head, &tail);
1012
1013 /* current size of fifo can only increase during dequeue: SPSC */
1014 cursize = f_cursize (f, head, tail);
1015
Florin Coras3e350af2017-03-30 02:54:28 -07001016 if (PREDICT_FALSE (cursize == 0))
Florin Coras87b15ce2019-04-28 21:16:30 -07001017 return SVM_FIFO_EEMPTY;
Dave Barach68b0fb02017-02-28 15:15:56 -05001018
Florin Coras99ad2fc2019-04-18 21:25:49 -07001019 len = clib_min (cursize, len);
Dave Barach68b0fb02017-02-28 15:15:56 -05001020
Florin Corasf22f4e52019-12-19 16:10:58 -08001021 if (!f->head_chunk)
1022 f->head_chunk = svm_fifo_find_chunk (f, head);
1023
1024 svm_fifo_copy_from_chunk (f, f->head_chunk, head, dst, len, &f->head_chunk);
1025 head = head + len;
1026
1027 if (f_pos_geq (head, f_chunk_end (f->start_chunk)))
1028 fsh_collect_chunks (f->fs_hdr, f->slice_index,
1029 f_unlink_chunks (f, head, 0));
Florin Coras2309e7a2019-04-18 18:58:10 -07001030
Sirshak Das28aa5392019-02-05 01:33:33 -06001031 /* store-rel: consumer owned index (paired with load-acq in producer) */
1032 clib_atomic_store_rel_n (&f->head, head);
Dave Barach68b0fb02017-02-28 15:15:56 -05001033
Florin Coras0a846802019-04-09 18:29:14 -07001034 return len;
Dave Barach68b0fb02017-02-28 15:15:56 -05001035}
1036
Dave Barach68b0fb02017-02-28 15:15:56 -05001037int
Florin Coras4375fa32019-04-19 15:56:00 -07001038svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 len, u8 * dst)
Florin Corasf6359c82017-06-19 12:26:09 -04001039{
Florin Coras4375fa32019-04-19 15:56:00 -07001040 u32 tail, head, cursize, head_idx;
Dave Barach68b0fb02017-02-28 15:15:56 -05001041
Sirshak Das28aa5392019-02-05 01:33:33 -06001042 f_load_head_tail_cons (f, &head, &tail);
1043
1044 /* current size of fifo can only increase during peek: SPSC */
1045 cursize = f_cursize (f, head, tail);
1046
Florin Coras4375fa32019-04-19 15:56:00 -07001047 if (PREDICT_FALSE (cursize < offset))
Florin Coras87b15ce2019-04-28 21:16:30 -07001048 return SVM_FIFO_EEMPTY;
Dave Barach68b0fb02017-02-28 15:15:56 -05001049
Florin Coras4375fa32019-04-19 15:56:00 -07001050 len = clib_min (cursize - offset, len);
Florin Corasf22f4e52019-12-19 16:10:58 -08001051 head_idx = head + offset;
Florin Corasb0208062019-12-12 12:09:29 -08001052
Florin Corasf22f4e52019-12-19 16:10:58 -08001053 if (!f->ooo_deq || !f_chunk_includes_pos (f->ooo_deq, head_idx))
1054 f_update_ooo_deq (f, head_idx, head_idx + len);
Dave Barach68b0fb02017-02-28 15:15:56 -05001055
Florin Coras4375fa32019-04-19 15:56:00 -07001056 svm_fifo_copy_from_chunk (f, f->ooo_deq, head_idx, dst, len, &f->ooo_deq);
Florin Coras0a846802019-04-09 18:29:14 -07001057 return len;
Dave Barach68b0fb02017-02-28 15:15:56 -05001058}
1059
1060int
Florin Coras87b15ce2019-04-28 21:16:30 -07001061svm_fifo_dequeue_drop (svm_fifo_t * f, u32 len)
Dave Barach68b0fb02017-02-28 15:15:56 -05001062{
Florin Coras87b15ce2019-04-28 21:16:30 -07001063 u32 total_drop_bytes, tail, head, cursize;
Dave Barach68b0fb02017-02-28 15:15:56 -05001064
Sirshak Das28aa5392019-02-05 01:33:33 -06001065 f_load_head_tail_cons (f, &head, &tail);
1066
Florin Coras87b15ce2019-04-28 21:16:30 -07001067 /* number of bytes available */
Sirshak Das28aa5392019-02-05 01:33:33 -06001068 cursize = f_cursize (f, head, tail);
Florin Coras3e350af2017-03-30 02:54:28 -07001069 if (PREDICT_FALSE (cursize == 0))
Florin Coras87b15ce2019-04-28 21:16:30 -07001070 return SVM_FIFO_EEMPTY;
Dave Barach68b0fb02017-02-28 15:15:56 -05001071
Sirshak Das28aa5392019-02-05 01:33:33 -06001072 /* number of bytes we're going to drop */
Florin Coras87b15ce2019-04-28 21:16:30 -07001073 total_drop_bytes = clib_min (cursize, len);
Dave Barach68b0fb02017-02-28 15:15:56 -05001074
Nathan Skrzypczakb4c67752019-06-20 09:58:37 +02001075 svm_fifo_trace_add (f, tail, total_drop_bytes, 3);
1076
Sirshak Das28aa5392019-02-05 01:33:33 -06001077 /* move head */
Florin Corasf22f4e52019-12-19 16:10:58 -08001078 head = head + total_drop_bytes;
Florin Coras3eb50622017-07-13 01:24:57 -04001079
Florin Corasf22f4e52019-12-19 16:10:58 -08001080 if (f_pos_geq (head, f_chunk_end (f->start_chunk)))
1081 {
1082 fsh_collect_chunks (f->fs_hdr, f->slice_index,
1083 f_unlink_chunks (f, head, 1));
1084 f->head_chunk =
1085 f_chunk_includes_pos (f->start_chunk, head) ? f->start_chunk : 0;
1086 }
Florin Coras0a6562c2019-08-05 09:39:47 -07001087
Sirshak Das28aa5392019-02-05 01:33:33 -06001088 /* store-rel: consumer owned index (paired with load-acq in producer) */
1089 clib_atomic_store_rel_n (&f->head, head);
Dave Barach68b0fb02017-02-28 15:15:56 -05001090
1091 return total_drop_bytes;
1092}
1093
Florin Corasf22f4e52019-12-19 16:10:58 -08001094/**
1095 * Drop all data from fifo
1096 *
Florin Corasf22f4e52019-12-19 16:10:58 -08001097 */
Florin Coras25579b42018-06-06 17:55:02 -07001098void
1099svm_fifo_dequeue_drop_all (svm_fifo_t * f)
1100{
Florin Corasf22f4e52019-12-19 16:10:58 -08001101 u32 head, tail;
Florin Coraseaacce42019-07-02 13:07:37 -07001102
Florin Corasf22f4e52019-12-19 16:10:58 -08001103 f_load_head_tail_all_acq (f, &head, &tail);
Florin Coraseaacce42019-07-02 13:07:37 -07001104
Florin Corasf22f4e52019-12-19 16:10:58 -08001105 if (!f->head_chunk || !f_chunk_includes_pos (f->head_chunk, head))
1106 f->head_chunk = svm_fifo_find_chunk (f, head);
1107
1108 f->head_chunk = f_lookup_clear_deq_chunks (f, f->head_chunk, tail);
1109
1110 if (f_pos_geq (tail, f_chunk_end (f->start_chunk)))
1111 fsh_collect_chunks (f->fs_hdr, f->slice_index,
1112 f_unlink_chunks (f, tail, 0));
Florin Coras0a6562c2019-08-05 09:39:47 -07001113
Sirshak Das28aa5392019-02-05 01:33:33 -06001114 /* store-rel: consumer owned index (paired with load-acq in producer) */
1115 clib_atomic_store_rel_n (&f->head, tail);
Florin Coras25579b42018-06-06 17:55:02 -07001116}
1117
Florin Coras2cba8532018-09-11 16:33:36 -07001118int
Florin Corasf22f4e52019-12-19 16:10:58 -08001119svm_fifo_fill_chunk_list (svm_fifo_t * f)
1120{
1121 u32 head, tail;
1122
1123 f_load_head_tail_prod (f, &head, &tail);
1124
1125 if (f_chunk_end (f->end_chunk) - head >= f->size)
1126 return 0;
1127
Florin Coras9e61d9a2020-02-05 21:13:18 +00001128 if (f_try_chunk_alloc (f, head, tail, f->size - (tail - head)))
Florin Corasf22f4e52019-12-19 16:10:58 -08001129 return SVM_FIFO_EGROW;
1130
1131 return 0;
1132}
1133
1134int
Florin Coras88001c62019-04-24 14:44:46 -07001135svm_fifo_segments (svm_fifo_t * f, svm_fifo_seg_t * fs)
Florin Coras2cba8532018-09-11 16:33:36 -07001136{
Sirshak Das28aa5392019-02-05 01:33:33 -06001137 u32 cursize, head, tail, head_idx;
Florin Coras2cba8532018-09-11 16:33:36 -07001138
Sirshak Das28aa5392019-02-05 01:33:33 -06001139 f_load_head_tail_cons (f, &head, &tail);
1140
1141 /* consumer function, cursize can only increase while we're working */
1142 cursize = f_cursize (f, head, tail);
1143
Florin Coras2cba8532018-09-11 16:33:36 -07001144 if (PREDICT_FALSE (cursize == 0))
Florin Coras87b15ce2019-04-28 21:16:30 -07001145 return SVM_FIFO_EEMPTY;
Florin Coras2cba8532018-09-11 16:33:36 -07001146
Florin Coras29a59c32019-05-02 12:52:19 -07001147 head_idx = head;
Florin Coras2cba8532018-09-11 16:33:36 -07001148
Sirshak Das28aa5392019-02-05 01:33:33 -06001149 if (tail < head)
Florin Coras2cba8532018-09-11 16:33:36 -07001150 {
Sirshak Das28aa5392019-02-05 01:33:33 -06001151 fs[0].len = f->size - head_idx;
Florin Coras0a846802019-04-09 18:29:14 -07001152 fs[0].data = f->head_chunk->data + head_idx;
Florin Coras2cba8532018-09-11 16:33:36 -07001153 fs[1].len = cursize - fs[0].len;
Florin Coras0a846802019-04-09 18:29:14 -07001154 fs[1].data = f->head_chunk->data;
Florin Coras2cba8532018-09-11 16:33:36 -07001155 }
1156 else
1157 {
Sirshak Das28aa5392019-02-05 01:33:33 -06001158 fs[0].len = cursize;
Florin Coras0a846802019-04-09 18:29:14 -07001159 fs[0].data = f->head_chunk->data + head_idx;
Florin Coras2cba8532018-09-11 16:33:36 -07001160 fs[1].len = 0;
1161 fs[1].data = 0;
1162 }
1163 return cursize;
1164}
1165
1166void
Florin Coras88001c62019-04-24 14:44:46 -07001167svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_seg_t * fs)
Florin Coras2cba8532018-09-11 16:33:36 -07001168{
Florin Coras29a59c32019-05-02 12:52:19 -07001169 u32 head;
Florin Coras2cba8532018-09-11 16:33:36 -07001170
Sirshak Das28aa5392019-02-05 01:33:33 -06001171 /* consumer owned index */
1172 head = f->head;
Sirshak Das28aa5392019-02-05 01:33:33 -06001173
Florin Coras29a59c32019-05-02 12:52:19 -07001174 ASSERT (fs[0].data == f->head_chunk->data + head);
Florin Corasf22f4e52019-12-19 16:10:58 -08001175 head = (head + fs[0].len + fs[1].len);
Sirshak Das28aa5392019-02-05 01:33:33 -06001176 /* store-rel: consumer owned index (paired with load-acq in producer) */
1177 clib_atomic_store_rel_n (&f->head, head);
1178}
1179
Florin Coras87b15ce2019-04-28 21:16:30 -07001180/**
1181 * Clones fifo
1182 *
1183 * Assumptions:
1184 * - no prod and cons are accessing either dest or src fifo
1185 * - fifo is not multi chunk
1186 */
Sirshak Das28aa5392019-02-05 01:33:33 -06001187void
1188svm_fifo_clone (svm_fifo_t * df, svm_fifo_t * sf)
1189{
1190 u32 head, tail;
Florin Corasf22f4e52019-12-19 16:10:58 -08001191
1192 /* Support only single chunk clones for now */
1193 ASSERT (svm_fifo_n_chunks (sf) == 1);
1194
Florin Coras0a846802019-04-09 18:29:14 -07001195 clib_memcpy_fast (df->head_chunk->data, sf->head_chunk->data, sf->size);
Sirshak Das28aa5392019-02-05 01:33:33 -06001196
1197 f_load_head_tail_all_acq (sf, &head, &tail);
1198 clib_atomic_store_rel_n (&df->head, head);
1199 clib_atomic_store_rel_n (&df->tail, tail);
Florin Coras2cba8532018-09-11 16:33:36 -07001200}
1201
Dave Barach1f75cfd2017-04-14 16:46:44 -04001202u32
Florin Coras87b15ce2019-04-28 21:16:30 -07001203svm_fifo_n_ooo_segments (svm_fifo_t * f)
Dave Barach1f75cfd2017-04-14 16:46:44 -04001204{
1205 return pool_elts (f->ooo_segments);
1206}
1207
1208ooo_segment_t *
1209svm_fifo_first_ooo_segment (svm_fifo_t * f)
1210{
1211 return pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
1212}
1213
Florin Corasc28764f2017-04-26 00:08:42 -07001214/**
1215 * Set fifo pointers to requested offset
1216 */
1217void
Florin Coras4375fa32019-04-19 15:56:00 -07001218svm_fifo_init_pointers (svm_fifo_t * f, u32 head, u32 tail)
Florin Corasc28764f2017-04-26 00:08:42 -07001219{
Florin Corasf22f4e52019-12-19 16:10:58 -08001220 svm_fifo_chunk_t *c;
1221
Florin Coras4375fa32019-04-19 15:56:00 -07001222 clib_atomic_store_rel_n (&f->head, head);
1223 clib_atomic_store_rel_n (&f->tail, tail);
Florin Corasf22f4e52019-12-19 16:10:58 -08001224
1225 c = svm_fifo_find_chunk (f, head);
1226 ASSERT (c != 0);
1227 f->head_chunk = f->ooo_deq = c;
1228 c = svm_fifo_find_chunk (f, tail);
1229 ASSERT (c != 0);
1230 f->tail_chunk = f->ooo_enq = c;
Florin Corasc28764f2017-04-26 00:08:42 -07001231}
1232
Florin Coras72b04282019-01-14 17:23:11 -08001233void
1234svm_fifo_add_subscriber (svm_fifo_t * f, u8 subscriber)
1235{
1236 if (f->n_subscribers >= SVM_FIFO_MAX_EVT_SUBSCRIBERS)
1237 return;
1238 f->subscribers[f->n_subscribers++] = subscriber;
1239}
1240
1241void
1242svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber)
1243{
1244 int i;
1245
1246 for (i = 0; i < f->n_subscribers; i++)
1247 {
1248 if (f->subscribers[i] != subscriber)
1249 continue;
1250 f->subscribers[i] = f->subscribers[f->n_subscribers - 1];
1251 f->n_subscribers--;
1252 break;
1253 }
1254}
1255
Florin Coraseaacce42019-07-02 13:07:37 -07001256u8
1257svm_fifo_is_sane (svm_fifo_t * f)
1258{
Florin Corasf22f4e52019-12-19 16:10:58 -08001259 svm_fifo_chunk_t *tmp;
Florin Coraseaacce42019-07-02 13:07:37 -07001260
Florin Corasf22f4e52019-12-19 16:10:58 -08001261 if (f->head_chunk && !f_chunk_includes_pos (f->head_chunk, f->head))
1262 return 0;
1263 if (f->tail_chunk && !f_chunk_includes_pos (f->tail_chunk, f->tail))
1264 return 0;
1265 if (f->ooo_deq)
1266 {
1267 if (rb_tree_is_init (&f->ooo_deq_lookup))
1268 {
1269 if (f_pos_lt (f->ooo_deq->start_byte, f->start_chunk->start_byte)
1270 || f_pos_gt (f->ooo_deq->start_byte,
1271 f_chunk_end (f->end_chunk)))
1272 return 0;
1273
1274 tmp = f_find_chunk_rbtree (&f->ooo_deq_lookup,
1275 f->ooo_deq->start_byte);
1276 }
1277 else
1278 tmp = svm_fifo_find_chunk (f, f->ooo_deq->start_byte);
1279 if (tmp != f->ooo_deq)
1280 return 0;
1281 }
1282 if (f->ooo_enq)
1283 {
1284 if (rb_tree_is_init (&f->ooo_enq_lookup))
1285 {
1286 if (f_pos_lt (f->ooo_enq->start_byte, f->start_chunk->start_byte)
1287 || f_pos_gt (f->ooo_enq->start_byte,
1288 f_chunk_end (f->end_chunk)))
1289 return 0;
1290
1291 tmp = f_find_chunk_rbtree (&f->ooo_enq_lookup,
1292 f->ooo_enq->start_byte);
1293 }
1294 else
1295 {
1296 tmp = svm_fifo_find_next_chunk (f, f->tail_chunk,
1297 f->ooo_enq->start_byte);
1298 }
1299 if (tmp != f->ooo_enq)
1300 return 0;
1301 }
1302
1303 if (f->start_chunk->next)
Florin Coraseaacce42019-07-02 13:07:37 -07001304 {
1305 svm_fifo_chunk_t *c, *prev = 0, *tmp;
Florin Corasf22f4e52019-12-19 16:10:58 -08001306 u32 chunks_bytes = 0;
Florin Coraseaacce42019-07-02 13:07:37 -07001307
1308 c = f->start_chunk;
1309 do
1310 {
1311 tmp = svm_fifo_find_chunk (f, c->start_byte);
1312 if (tmp != c)
1313 return 0;
1314 if (prev && (prev->start_byte + prev->length != c->start_byte))
1315 return 0;
Florin Corasb0208062019-12-12 12:09:29 -08001316
Florin Corasf22f4e52019-12-19 16:10:58 -08001317 if (c->enq_rb_index != RBTREE_TNIL_INDEX)
Florin Corasb0208062019-12-12 12:09:29 -08001318 {
Florin Corasf22f4e52019-12-19 16:10:58 -08001319 tmp = f_find_chunk_rbtree (&f->ooo_enq_lookup, c->start_byte);
Florin Corasb0208062019-12-12 12:09:29 -08001320 if (tmp)
1321 {
Florin Corasb0208062019-12-12 12:09:29 -08001322 if (tmp != c)
1323 return 0;
1324 }
Florin Corasf22f4e52019-12-19 16:10:58 -08001325 }
1326 if (c->deq_rb_index != RBTREE_TNIL_INDEX)
1327 {
1328 tmp = f_find_chunk_rbtree (&f->ooo_deq_lookup, c->start_byte);
Florin Corasb0208062019-12-12 12:09:29 -08001329 if (tmp)
1330 {
Florin Corasb0208062019-12-12 12:09:29 -08001331 if (tmp != c)
1332 return 0;
1333 }
Florin Corasb0208062019-12-12 12:09:29 -08001334 }
1335
Florin Corasf22f4e52019-12-19 16:10:58 -08001336 chunks_bytes += c->length;
Florin Coraseaacce42019-07-02 13:07:37 -07001337 prev = c;
1338 c = c->next;
1339 }
Florin Corasf22f4e52019-12-19 16:10:58 -08001340 while (c);
Florin Coraseaacce42019-07-02 13:07:37 -07001341
Florin Corasf22f4e52019-12-19 16:10:58 -08001342 if (chunks_bytes < f->tail - f->head)
Florin Coraseaacce42019-07-02 13:07:37 -07001343 return 0;
1344 }
1345
1346 return 1;
1347}
1348
Florin Corasf22f4e52019-12-19 16:10:58 -08001349u32
1350svm_fifo_n_chunks (svm_fifo_t * f)
Ryujiro Shibuya8e20fe72019-10-16 06:30:02 +01001351{
Florin Corasf22f4e52019-12-19 16:10:58 -08001352 svm_fifo_chunk_t *c;
1353 int n_chunks = 0;
Ryujiro Shibuya8e20fe72019-10-16 06:30:02 +01001354
Florin Corasf22f4e52019-12-19 16:10:58 -08001355 c = f->start_chunk;
1356 while (c)
1357 {
1358 n_chunks++;
1359 c = c->next;
Ryujiro Shibuya8e20fe72019-10-16 06:30:02 +01001360 }
1361
Florin Corasf22f4e52019-12-19 16:10:58 -08001362 return n_chunks;
Ryujiro Shibuya8e20fe72019-10-16 06:30:02 +01001363}
1364
Florin Coras87b15ce2019-04-28 21:16:30 -07001365u8 *
1366format_ooo_segment (u8 * s, va_list * args)
1367{
Florin Corasf22f4e52019-12-19 16:10:58 -08001368 svm_fifo_t __clib_unused *f = va_arg (*args, svm_fifo_t *);
Florin Coras87b15ce2019-04-28 21:16:30 -07001369 ooo_segment_t *seg = va_arg (*args, ooo_segment_t *);
Florin Corasf22f4e52019-12-19 16:10:58 -08001370 s = format (s, "[%u, %u], len %u, next %d, prev %d", seg->start,
1371 seg->start + seg->length, seg->length, seg->next, seg->prev);
Florin Coras87b15ce2019-04-28 21:16:30 -07001372 return s;
1373}
1374
1375u8 *
1376svm_fifo_dump_trace (u8 * s, svm_fifo_t * f)
1377{
1378#if SVM_FIFO_TRACE
1379 svm_fifo_trace_elem_t *seg = 0;
1380 int i = 0;
1381
1382 if (f->trace)
1383 {
1384 vec_foreach (seg, f->trace)
1385 {
1386 s = format (s, "{%u, %u, %u}, ", seg->offset, seg->len, seg->action);
1387 i++;
1388 if (i % 5 == 0)
1389 s = format (s, "\n");
1390 }
1391 s = format (s, "\n");
1392 }
1393 return s;
1394#else
1395 return 0;
1396#endif
1397}
1398
1399u8 *
1400svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose)
1401{
1402 int i, trace_len;
1403 u8 *data = 0;
1404 svm_fifo_trace_elem_t *trace;
1405 u32 offset;
1406 svm_fifo_t *dummy_fifo;
1407
1408 if (!f)
1409 return s;
1410
1411#if SVM_FIFO_TRACE
1412 trace = f->trace;
1413 trace_len = vec_len (trace);
1414#else
1415 trace = 0;
1416 trace_len = 0;
1417#endif
1418
Florin Corasf22f4e52019-12-19 16:10:58 -08001419 dummy_fifo = svm_fifo_alloc (f->size);
1420 svm_fifo_init (f, f->size);
1421 clib_memset (f->head_chunk->data, 0xFF, f->size);
1422 vec_validate (data, f->size);
Florin Coras87b15ce2019-04-28 21:16:30 -07001423 for (i = 0; i < vec_len (data); i++)
1424 data[i] = i;
1425
1426 for (i = 0; i < trace_len; i++)
1427 {
1428 offset = trace[i].offset;
1429 if (trace[i].action == 1)
1430 {
1431 if (verbose)
1432 s = format (s, "adding [%u, %u]:", trace[i].offset,
Florin Corasf22f4e52019-12-19 16:10:58 -08001433 (trace[i].offset + trace[i].len));
Florin Coras87b15ce2019-04-28 21:16:30 -07001434 svm_fifo_enqueue_with_offset (dummy_fifo, trace[i].offset,
1435 trace[i].len, &data[offset]);
1436 }
1437 else if (trace[i].action == 2)
1438 {
1439 if (verbose)
1440 s = format (s, "adding [%u, %u]:", 0, trace[i].len);
1441 svm_fifo_enqueue (dummy_fifo, trace[i].len, &data[offset]);
1442 }
1443 else if (!no_read)
1444 {
1445 if (verbose)
1446 s = format (s, "read: %u", trace[i].len);
1447 svm_fifo_dequeue_drop (dummy_fifo, trace[i].len);
1448 }
1449 if (verbose)
1450 s = format (s, "%U", format_svm_fifo, dummy_fifo, 1);
1451 }
1452
1453 s = format (s, "result: %U", format_svm_fifo, dummy_fifo, 1);
1454
1455 return s;
1456}
1457
1458u8 *
1459format_ooo_list (u8 * s, va_list * args)
1460{
1461 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
1462 u32 indent = va_arg (*args, u32);
1463 u32 ooo_segment_index = f->ooos_list_head;
1464 ooo_segment_t *seg;
1465
1466 while (ooo_segment_index != OOO_SEGMENT_INVALID_INDEX)
1467 {
1468 seg = pool_elt_at_index (f->ooo_segments, ooo_segment_index);
1469 s = format (s, "%U%U\n", format_white_space, indent, format_ooo_segment,
1470 f, seg);
1471 ooo_segment_index = seg->next;
1472 }
1473
1474 return s;
1475}
1476
1477u8 *
1478format_svm_fifo (u8 * s, va_list * args)
1479{
1480 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
1481 int verbose = va_arg (*args, int);
1482 u32 indent;
1483
1484 if (!s)
1485 return s;
1486
1487 indent = format_get_indent (s);
Florin Corasf22f4e52019-12-19 16:10:58 -08001488 s = format (s, "cursize %u nitems %u has_event %d min_alloc %u\n",
1489 svm_fifo_max_dequeue (f), f->size, f->has_event, f->min_alloc);
Florin Coras87b15ce2019-04-28 21:16:30 -07001490 s = format (s, "%Uhead %u tail %u segment manager %u\n", format_white_space,
Florin Corasf22f4e52019-12-19 16:10:58 -08001491 indent, f->head, f->tail, f->segment_manager);
Florin Coras87b15ce2019-04-28 21:16:30 -07001492
1493 if (verbose > 1)
1494 s = format (s, "%Uvpp session %d thread %d app session %d thread %d\n",
1495 format_white_space, indent, f->master_session_index,
1496 f->master_thread_index, f->client_session_index,
1497 f->client_thread_index);
1498
1499 if (verbose)
1500 {
1501 s = format (s, "%Uooo pool %d active elts newest %u\n",
1502 format_white_space, indent, pool_elts (f->ooo_segments),
1503 f->ooos_newest);
1504 if (svm_fifo_has_ooo_data (f))
1505 s = format (s, " %U", format_ooo_list, f, indent, verbose);
1506 }
1507 return s;
1508}
1509
Florin Coras3aa7af32018-06-29 08:44:31 -07001510#endif
Dave Barach68b0fb02017-02-28 15:15:56 -05001511/*
1512 * fd.io coding-style-patch-verification: ON
1513 *
1514 * Local Variables:
1515 * eval: (c-set-style "gnu")
1516 * End:
1517 */