blob: d49cbe2bf7df0951330c55c1759e3b4b866c2c77 [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 {
BenoƮt Gannedf601ae2020-10-20 14:31:55 +020073 CLIB_MEM_UNPOISON (c, sizeof (*c));
74 CLIB_MEM_UNPOISON (c->data, c->length);
Florin Coras99ad2fc2019-04-18 21:25:49 -070075 n_chunk = clib_min (c->length, to_copy);
76 clib_memcpy_fast (dst + (len - to_copy), &c->data[0], n_chunk);
Florin Coras4375fa32019-04-19 15:56:00 -070077 c = c->length <= to_copy ? c->next : c;
Florin Coras99ad2fc2019-04-18 21:25:49 -070078 }
Florin Coras4375fa32019-04-19 15:56:00 -070079 if (*last)
80 *last = c;
Florin Coras99ad2fc2019-04-18 21:25:49 -070081 }
82 else
83 {
84 clib_memcpy_fast (dst, &c->data[head_idx], len);
85 }
86}
87
88#ifndef CLIB_MARCH_VARIANT
89
90static inline void
91svm_fifo_copy_to_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c, u32 tail_idx,
Florin Coras4375fa32019-04-19 15:56:00 -070092 const u8 * src, u32 len, svm_fifo_chunk_t ** last)
Florin Coras99ad2fc2019-04-18 21:25:49 -070093{
94 CLIB_MARCH_FN_SELECT (svm_fifo_copy_to_chunk) (f, c, tail_idx, src, len,
Florin Coras4375fa32019-04-19 15:56:00 -070095 last);
Florin Coras99ad2fc2019-04-18 21:25:49 -070096}
97
98static inline void
99svm_fifo_copy_from_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c, u32 head_idx,
Florin Coras4375fa32019-04-19 15:56:00 -0700100 u8 * dst, u32 len, svm_fifo_chunk_t ** last)
Florin Coras99ad2fc2019-04-18 21:25:49 -0700101{
102 CLIB_MARCH_FN_SELECT (svm_fifo_copy_from_chunk) (f, c, head_idx, dst, len,
Florin Coras4375fa32019-04-19 15:56:00 -0700103 last);
Florin Coras99ad2fc2019-04-18 21:25:49 -0700104}
105
Florin Corasf03a59a2017-06-09 21:07:32 -0700106static inline u32
Florin Corasf22f4e52019-12-19 16:10:58 -0800107ooo_segment_end_pos (ooo_segment_t * s)
Florin Corasf03a59a2017-06-09 21:07:32 -0700108{
Florin Corasf22f4e52019-12-19 16:10:58 -0800109 return (s->start + s->length);
Florin Corasf03a59a2017-06-09 21:07:32 -0700110}
Dave Barach1f75cfd2017-04-14 16:46:44 -0400111
Florin Coras87b15ce2019-04-28 21:16:30 -0700112void
113svm_fifo_free_ooo_data (svm_fifo_t * f)
Dave Barach1f75cfd2017-04-14 16:46:44 -0400114{
Florin Coras87b15ce2019-04-28 21:16:30 -0700115 pool_free (f->ooo_segments);
116}
117
118static inline ooo_segment_t *
Florin Corasa7570b02019-05-02 12:52:19 -0700119ooo_segment_prev (svm_fifo_t * f, ooo_segment_t * s)
Florin Coras87b15ce2019-04-28 21:16:30 -0700120{
121 if (s->prev == OOO_SEGMENT_INVALID_INDEX)
122 return 0;
123 return pool_elt_at_index (f->ooo_segments, s->prev);
124}
125
126static inline ooo_segment_t *
127ooo_segment_next (svm_fifo_t * f, ooo_segment_t * s)
128{
129 if (s->next == OOO_SEGMENT_INVALID_INDEX)
130 return 0;
131 return pool_elt_at_index (f->ooo_segments, s->next);
132}
133
134static inline ooo_segment_t *
135ooo_segment_alloc (svm_fifo_t * f, u32 start, u32 length)
136{
137 ooo_segment_t *s;
138
139 pool_get (f->ooo_segments, s);
140
141 s->start = start;
142 s->length = length;
143 s->prev = s->next = OOO_SEGMENT_INVALID_INDEX;
144
Dave Barach1f75cfd2017-04-14 16:46:44 -0400145 return s;
146}
147
Florin Coras87b15ce2019-04-28 21:16:30 -0700148static inline void
149ooo_segment_free (svm_fifo_t * f, u32 index)
Florin Coras3eb50622017-07-13 01:24:57 -0400150{
Florin Coras87b15ce2019-04-28 21:16:30 -0700151 ooo_segment_t *cur, *prev = 0, *next = 0;
152 cur = pool_elt_at_index (f->ooo_segments, index);
Florin Coras3eb50622017-07-13 01:24:57 -0400153
Florin Coras87b15ce2019-04-28 21:16:30 -0700154 if (cur->next != OOO_SEGMENT_INVALID_INDEX)
Florin Coras3eb50622017-07-13 01:24:57 -0400155 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700156 next = pool_elt_at_index (f->ooo_segments, cur->next);
157 next->prev = cur->prev;
Florin Coras3eb50622017-07-13 01:24:57 -0400158 }
Florin Coras87b15ce2019-04-28 21:16:30 -0700159
160 if (cur->prev != OOO_SEGMENT_INVALID_INDEX)
161 {
162 prev = pool_elt_at_index (f->ooo_segments, cur->prev);
163 prev->next = cur->next;
164 }
165 else
166 {
167 f->ooos_list_head = cur->next;
168 }
169
170 pool_put (f->ooo_segments, cur);
Florin Coras3eb50622017-07-13 01:24:57 -0400171}
172
Florin Coras87b15ce2019-04-28 21:16:30 -0700173/**
174 * Add segment to fifo's out-of-order segment list. Takes care of merging
175 * adjacent segments and removing overlapping ones.
176 */
177static void
178ooo_segment_add (svm_fifo_t * f, u32 offset, u32 head, u32 tail, u32 length)
Florin Coras3eb50622017-07-13 01:24:57 -0400179{
Florin Coras87b15ce2019-04-28 21:16:30 -0700180 ooo_segment_t *s, *new_s, *prev, *next, *it;
181 u32 new_index, s_end_pos, s_index;
182 u32 offset_pos, offset_end_pos;
Florin Coras3eb50622017-07-13 01:24:57 -0400183
Florin Corasf22f4e52019-12-19 16:10:58 -0800184 ASSERT (offset + length <= f_free_count (f, head, tail));
Florin Coras3eb50622017-07-13 01:24:57 -0400185
Florin Corasf22f4e52019-12-19 16:10:58 -0800186 offset_pos = tail + offset;
187 offset_end_pos = tail + offset + length;
Florin Coras3eb50622017-07-13 01:24:57 -0400188
Florin Coras87b15ce2019-04-28 21:16:30 -0700189 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
Florin Coras3eb50622017-07-13 01:24:57 -0400190
Florin Coras87b15ce2019-04-28 21:16:30 -0700191 if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX)
Florin Coras3eb50622017-07-13 01:24:57 -0400192 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700193 s = ooo_segment_alloc (f, offset_pos, length);
194 f->ooos_list_head = s - f->ooo_segments;
195 f->ooos_newest = f->ooos_list_head;
196 return;
197 }
198
199 /* Find first segment that starts after new segment */
200 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
201 while (s->next != OOO_SEGMENT_INVALID_INDEX
Florin Corasf22f4e52019-12-19 16:10:58 -0800202 && f_pos_lt (s->start, offset_pos))
Florin Coras87b15ce2019-04-28 21:16:30 -0700203 s = pool_elt_at_index (f->ooo_segments, s->next);
204
205 /* If we have a previous and we overlap it, use it as starting point */
Florin Corasa7570b02019-05-02 12:52:19 -0700206 prev = ooo_segment_prev (f, s);
Florin Corasf22f4e52019-12-19 16:10:58 -0800207 if (prev && f_pos_leq (offset_pos, ooo_segment_end_pos (prev)))
Florin Coras87b15ce2019-04-28 21:16:30 -0700208 {
209 s = prev;
Florin Corasf22f4e52019-12-19 16:10:58 -0800210 s_end_pos = ooo_segment_end_pos (s);
Florin Coras87b15ce2019-04-28 21:16:30 -0700211
212 /* Since we have previous, offset start position cannot be smaller
213 * than prev->start. Check tail */
Florin Corasf22f4e52019-12-19 16:10:58 -0800214 ASSERT (f_pos_lt (s->start, offset_pos));
Florin Coras87b15ce2019-04-28 21:16:30 -0700215 goto check_tail;
216 }
217
218 s_index = s - f->ooo_segments;
Florin Corasf22f4e52019-12-19 16:10:58 -0800219 s_end_pos = ooo_segment_end_pos (s);
Florin Coras87b15ce2019-04-28 21:16:30 -0700220
221 /* No overlap, add before current segment */
Florin Corasf22f4e52019-12-19 16:10:58 -0800222 if (f_pos_lt (offset_end_pos, s->start))
Florin Coras87b15ce2019-04-28 21:16:30 -0700223 {
224 new_s = ooo_segment_alloc (f, offset_pos, length);
225 new_index = new_s - f->ooo_segments;
226
227 /* Pool might've moved, get segment again */
228 s = pool_elt_at_index (f->ooo_segments, s_index);
229 if (s->prev != OOO_SEGMENT_INVALID_INDEX)
Florin Coras3eb50622017-07-13 01:24:57 -0400230 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700231 new_s->prev = s->prev;
232 prev = pool_elt_at_index (f->ooo_segments, new_s->prev);
233 prev->next = new_index;
Florin Coras3eb50622017-07-13 01:24:57 -0400234 }
Florin Coras87b15ce2019-04-28 21:16:30 -0700235 else
Florin Coras3eb50622017-07-13 01:24:57 -0400236 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700237 /* New head */
238 f->ooos_list_head = new_index;
Florin Coras3eb50622017-07-13 01:24:57 -0400239 }
Florin Coras87b15ce2019-04-28 21:16:30 -0700240
241 new_s->next = s_index;
242 s->prev = new_index;
243 f->ooos_newest = new_index;
244 return;
245 }
246 /* No overlap, add after current segment */
Florin Corasf22f4e52019-12-19 16:10:58 -0800247 else if (f_pos_gt (offset_pos, s_end_pos))
Florin Coras87b15ce2019-04-28 21:16:30 -0700248 {
249 new_s = ooo_segment_alloc (f, offset_pos, length);
250 new_index = new_s - f->ooo_segments;
251
252 /* Pool might've moved, get segment again */
253 s = pool_elt_at_index (f->ooo_segments, s_index);
254
255 /* Needs to be last */
256 ASSERT (s->next == OOO_SEGMENT_INVALID_INDEX);
257
258 new_s->prev = s_index;
259 s->next = new_index;
260 f->ooos_newest = new_index;
261
262 return;
Florin Coras3eb50622017-07-13 01:24:57 -0400263 }
264
Florin Coras87b15ce2019-04-28 21:16:30 -0700265 /*
266 * Merge needed
267 */
Florin Coras3eb50622017-07-13 01:24:57 -0400268
Florin Coras87b15ce2019-04-28 21:16:30 -0700269 /* Merge at head */
Florin Corasf22f4e52019-12-19 16:10:58 -0800270 if (f_pos_lt (offset_pos, s->start))
Florin Coras87b15ce2019-04-28 21:16:30 -0700271 {
272 s->start = offset_pos;
Florin Corasf22f4e52019-12-19 16:10:58 -0800273 s->length = s_end_pos - s->start;
Florin Coras87b15ce2019-04-28 21:16:30 -0700274 f->ooos_newest = s - f->ooo_segments;
275 }
276
277check_tail:
278
279 /* Overlapping tail */
Florin Corasf22f4e52019-12-19 16:10:58 -0800280 if (f_pos_gt (offset_end_pos, s_end_pos))
Florin Coras87b15ce2019-04-28 21:16:30 -0700281 {
Florin Corasf22f4e52019-12-19 16:10:58 -0800282 s->length = offset_end_pos - s->start;
Florin Coras87b15ce2019-04-28 21:16:30 -0700283
284 /* Remove the completely overlapped segments in the tail */
285 it = ooo_segment_next (f, s);
Florin Corasf22f4e52019-12-19 16:10:58 -0800286 while (it && f_pos_leq (ooo_segment_end_pos (it), offset_end_pos))
Florin Coras87b15ce2019-04-28 21:16:30 -0700287 {
288 next = ooo_segment_next (f, it);
289 ooo_segment_free (f, it - f->ooo_segments);
290 it = next;
291 }
292
293 /* If partial overlap with last, merge */
Florin Corasf22f4e52019-12-19 16:10:58 -0800294 if (it && f_pos_leq (it->start, offset_end_pos))
Florin Coras87b15ce2019-04-28 21:16:30 -0700295 {
Florin Corasf22f4e52019-12-19 16:10:58 -0800296 s->length = ooo_segment_end_pos (it) - s->start;
Florin Coras87b15ce2019-04-28 21:16:30 -0700297 ooo_segment_free (f, it - f->ooo_segments);
298 }
299 f->ooos_newest = s - f->ooo_segments;
300 }
Florin Coras3eb50622017-07-13 01:24:57 -0400301}
302
Florin Coras87b15ce2019-04-28 21:16:30 -0700303/**
304 * Removes segments that can now be enqueued because the fifo's tail has
305 * advanced. Returns the number of bytes added to tail.
306 */
307static int
308ooo_segment_try_collect (svm_fifo_t * f, u32 n_bytes_enqueued, u32 * tail)
Dave Barach1f75cfd2017-04-14 16:46:44 -0400309{
Florin Coras87b15ce2019-04-28 21:16:30 -0700310 u32 s_index, bytes = 0;
311 ooo_segment_t *s;
312 i32 diff;
Dave Barach1f75cfd2017-04-14 16:46:44 -0400313
Florin Coras87b15ce2019-04-28 21:16:30 -0700314 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
Florin Corasf22f4e52019-12-19 16:10:58 -0800315 diff = *tail - s->start;
Florin Coras87b15ce2019-04-28 21:16:30 -0700316
317 ASSERT (diff != n_bytes_enqueued);
318
319 if (diff > n_bytes_enqueued)
320 return 0;
321
322 /* If last tail update overlaps one/multiple ooo segments, remove them */
323 while (0 <= diff && diff < n_bytes_enqueued)
Dave Barach1f75cfd2017-04-14 16:46:44 -0400324 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700325 s_index = s - f->ooo_segments;
326
327 /* Segment end is beyond the tail. Advance tail and remove segment */
328 if (s->length > diff)
329 {
330 bytes = s->length - diff;
Florin Corasf22f4e52019-12-19 16:10:58 -0800331 *tail = *tail + bytes;
Florin Coras87b15ce2019-04-28 21:16:30 -0700332 ooo_segment_free (f, s_index);
333 break;
334 }
335
336 /* If we have next go on */
337 if (s->next != OOO_SEGMENT_INVALID_INDEX)
338 {
339 s = pool_elt_at_index (f->ooo_segments, s->next);
Florin Corasf22f4e52019-12-19 16:10:58 -0800340 diff = *tail - s->start;
Florin Coras87b15ce2019-04-28 21:16:30 -0700341 ooo_segment_free (f, s_index);
342 }
343 /* End of search */
344 else
345 {
346 ooo_segment_free (f, s_index);
347 break;
348 }
Dave Barach1f75cfd2017-04-14 16:46:44 -0400349 }
Florin Coras3eb50622017-07-13 01:24:57 -0400350
Florin Corasf22f4e52019-12-19 16:10:58 -0800351 ASSERT (bytes <= f->size);
Florin Coras87b15ce2019-04-28 21:16:30 -0700352 return bytes;
Florin Corasb59a7052017-04-18 22:07:29 -0700353}
354
Florin Corasf22f4e52019-12-19 16:10:58 -0800355__clib_unused static ooo_segment_t *
Florin Corasa7570b02019-05-02 12:52:19 -0700356ooo_segment_last (svm_fifo_t * f)
357{
358 ooo_segment_t *s;
359
360 if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX)
361 return 0;
362
363 s = svm_fifo_first_ooo_segment (f);
364 while (s->next != OOO_SEGMENT_INVALID_INDEX)
365 s = pool_elt_at_index (f->ooo_segments, s->next);
366 return s;
367}
368
Florin Coras0a846802019-04-09 18:29:14 -0700369void
370svm_fifo_init (svm_fifo_t * f, u32 size)
371{
Florin Corasf22f4e52019-12-19 16:10:58 -0800372 svm_fifo_chunk_t *c, *prev;
Florin Coras2de9c0f2020-02-02 19:30:39 +0000373 u32 min_alloc;
Florin Corasf22f4e52019-12-19 16:10:58 -0800374
Florin Coras0a846802019-04-09 18:29:14 -0700375 f->size = size;
Florin Coras0a846802019-04-09 18:29:14 -0700376 f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
Florin Coras0a846802019-04-09 18:29:14 -0700377 f->segment_index = SVM_FIFO_INVALID_INDEX;
378 f->refcnt = 1;
Florin Coras73cad332019-08-28 17:12:32 -0700379 f->head = f->tail = f->flags = 0;
Florin Corasf22f4e52019-12-19 16:10:58 -0800380 f->head_chunk = f->tail_chunk = f->start_chunk;
381 f->ooo_deq = f->ooo_enq = 0;
Florin Coras0a846802019-04-09 18:29:14 -0700382
Florin Coras2de9c0f2020-02-02 19:30:39 +0000383 min_alloc = size > 32 << 10 ? size >> 3 : 4096;
384 min_alloc = clib_min (min_alloc, 64 << 10);
Florin Corasf22f4e52019-12-19 16:10:58 -0800385 f->min_alloc = min_alloc;
Florin Coraseaacce42019-07-02 13:07:37 -0700386
Florin Corasf22f4e52019-12-19 16:10:58 -0800387 /*
388 * Initialize chunks
389 */
Florin Coraseaacce42019-07-02 13:07:37 -0700390 f->start_chunk->start_byte = 0;
391 prev = f->start_chunk;
392 c = prev->next;
393
Florin Corasf22f4e52019-12-19 16:10:58 -0800394 while (c)
Florin Coraseaacce42019-07-02 13:07:37 -0700395 {
396 c->start_byte = prev->start_byte + prev->length;
Florin Coraseaacce42019-07-02 13:07:37 -0700397 prev = c;
398 c = c->next;
399 }
400}
401
Florin Corasf22f4e52019-12-19 16:10:58 -0800402void
403svm_fifo_init_ooo_lookup (svm_fifo_t * f, u8 ooo_type)
404{
405 if (ooo_type == 0)
406 {
407 ASSERT (!rb_tree_is_init (&f->ooo_enq_lookup));
408 rb_tree_init (&f->ooo_enq_lookup);
409 }
410 else
411 {
412 ASSERT (!rb_tree_is_init (&f->ooo_deq_lookup));
413 rb_tree_init (&f->ooo_deq_lookup);
414 }
415}
416
Florin Corasb095a3c2019-04-25 12:58:46 -0700417/**
418 * Creates a fifo in the current heap. Fails vs blow up the process
419 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500420svm_fifo_t *
Florin Corasf22f4e52019-12-19 16:10:58 -0800421svm_fifo_alloc (u32 data_size_in_bytes)
Dave Barach68b0fb02017-02-28 15:15:56 -0500422{
Dave Barach818eb542017-08-02 13:56:13 -0400423 u32 rounded_data_size;
Florin Corascefd5d82019-05-05 13:19:57 -0700424 svm_fifo_chunk_t *c;
425 svm_fifo_t *f;
Dave Barach68b0fb02017-02-28 15:15:56 -0500426
Florin Corascefd5d82019-05-05 13:19:57 -0700427 f = clib_mem_alloc_aligned_or_null (sizeof (*f), CLIB_CACHE_LINE_BYTES);
Dave Barach68b0fb02017-02-28 15:15:56 -0500428 if (f == 0)
429 return 0;
430
Dave Barachb7b92992018-10-17 10:38:51 -0400431 clib_memset (f, 0, sizeof (*f));
Florin Corascefd5d82019-05-05 13:19:57 -0700432
433 /* always round fifo data size to the next highest power-of-two */
434 rounded_data_size = (1 << (max_log2 (data_size_in_bytes)));
435 c = clib_mem_alloc_aligned_or_null (sizeof (*c) + rounded_data_size,
436 CLIB_CACHE_LINE_BYTES);
437 if (!c)
438 {
439 clib_mem_free (f);
440 return 0;
441 }
442
Florin Corasf22f4e52019-12-19 16:10:58 -0800443 clib_memset (c, 0, sizeof (*c));
Florin Corascefd5d82019-05-05 13:19:57 -0700444 c->start_byte = 0;
445 c->length = data_size_in_bytes;
Florin Corasf22f4e52019-12-19 16:10:58 -0800446 c->enq_rb_index = RBTREE_TNIL_INDEX;
447 c->deq_rb_index = RBTREE_TNIL_INDEX;
Florin Corascefd5d82019-05-05 13:19:57 -0700448 f->start_chunk = f->end_chunk = c;
449
Florin Coras0a846802019-04-09 18:29:14 -0700450 return f;
Dave Barach68b0fb02017-02-28 15:15:56 -0500451}
452
Florin Corasb095a3c2019-04-25 12:58:46 -0700453/**
454 * Creates a fifo chunk in the current heap
455 */
456svm_fifo_chunk_t *
457svm_fifo_chunk_alloc (u32 size)
458{
459 svm_fifo_chunk_t *c;
460 u32 rounded_size;
461
462 /* round chunk size to the next highest power-of-two */
463 rounded_size = (1 << (max_log2 (size)));
464 c = clib_mem_alloc_aligned_or_null (sizeof (*c) + rounded_size,
465 CLIB_CACHE_LINE_BYTES);
466 if (c == 0)
467 return 0;
468
469 clib_memset (c, 0, sizeof (*c));
470 c->length = rounded_size;
471 return c;
472}
473
Florin Corasf22f4e52019-12-19 16:10:58 -0800474/**
475 * Find chunk for given byte position
476 *
477 * @param f fifo
478 * @param pos normalized position in fifo
479 *
480 * @return chunk that includes given position or 0
481 */
482static svm_fifo_chunk_t *
483svm_fifo_find_chunk (svm_fifo_t * f, u32 pos)
Florin Coras4375fa32019-04-19 15:56:00 -0700484{
Florin Corasf22f4e52019-12-19 16:10:58 -0800485 svm_fifo_chunk_t *c;
486
487 c = f->start_chunk;
488 while (c && !f_chunk_includes_pos (c, pos))
489 c = c->next;
490
491 return c;
492}
493
494static svm_fifo_chunk_t *
495svm_fifo_find_next_chunk (svm_fifo_t * f, svm_fifo_chunk_t * start, u32 pos)
496{
497 svm_fifo_chunk_t *c;
498
499 ASSERT (start != 0);
500
501 c = start;
502 while (c && !f_chunk_includes_pos (c, pos))
503 c = c->next;
504
505 return c;
506}
507
508u32
509svm_fifo_max_read_chunk (svm_fifo_t * f)
510{
511 u32 head, tail, end_chunk;
512
513 f_load_head_tail_cons (f, &head, &tail);
514 ASSERT (!f->head_chunk || f_chunk_includes_pos (f->head_chunk, head));
515
516 if (!f->head_chunk)
517 {
518 f->head_chunk = svm_fifo_find_chunk (f, head);
519 if (PREDICT_FALSE (!f->head_chunk))
520 return 0;
521 }
522
523 end_chunk = f_chunk_end (f->head_chunk);
524
525 return f_pos_lt (end_chunk, tail) ? end_chunk - head : tail - head;
526}
527
528u32
529svm_fifo_max_write_chunk (svm_fifo_t * f)
530{
531 u32 head, tail;
532
533 f_load_head_tail_prod (f, &head, &tail);
534 ASSERT (!f->tail_chunk || f_chunk_includes_pos (f->tail_chunk, tail));
535
536 return f->tail_chunk ? f_chunk_end (f->tail_chunk) - tail : 0;
Florin Coras4375fa32019-04-19 15:56:00 -0700537}
538
Florin Corasb0208062019-12-12 12:09:29 -0800539static rb_node_t *
Florin Corasf22f4e52019-12-19 16:10:58 -0800540f_find_node_rbtree (rb_tree_t * rt, u32 pos)
Florin Corasb0208062019-12-12 12:09:29 -0800541{
542 rb_node_t *cur, *prev;
543
544 cur = rb_node (rt, rt->root);
545 if (PREDICT_FALSE (rb_node_is_tnil (rt, cur)))
546 return 0;
547
548 while (pos != cur->key)
549 {
550 prev = cur;
Florin Corasf22f4e52019-12-19 16:10:58 -0800551 if (f_pos_lt (pos, cur->key))
Florin Corasb0208062019-12-12 12:09:29 -0800552 {
553 cur = rb_node_left (rt, cur);
554 if (rb_node_is_tnil (rt, cur))
555 {
556 cur = rb_tree_predecessor (rt, prev);
557 break;
558 }
559 }
560 else
561 {
562 cur = rb_node_right (rt, cur);
563 if (rb_node_is_tnil (rt, cur))
564 {
565 cur = prev;
566 break;
567 }
568 }
569 }
570
571 if (rb_node_is_tnil (rt, cur))
572 return 0;
573
574 return cur;
575}
576
577static svm_fifo_chunk_t *
Florin Corasf22f4e52019-12-19 16:10:58 -0800578f_find_chunk_rbtree (rb_tree_t * rt, u32 pos)
Florin Corasb0208062019-12-12 12:09:29 -0800579{
580 svm_fifo_chunk_t *c;
581 rb_node_t *n;
582
Florin Corasf22f4e52019-12-19 16:10:58 -0800583 if (!rb_tree_is_init (rt))
584 return 0;
585
586 n = f_find_node_rbtree (rt, pos);
Florin Corasb0208062019-12-12 12:09:29 -0800587 if (!n)
588 return 0;
589 c = uword_to_pointer (n->opaque, svm_fifo_chunk_t *);
Florin Corasf22f4e52019-12-19 16:10:58 -0800590 if (f_chunk_includes_pos (c, pos))
Florin Corasb0208062019-12-12 12:09:29 -0800591 return c;
592
593 return 0;
594}
595
Florin Corasb0208062019-12-12 12:09:29 -0800596static void
Florin Corasf22f4e52019-12-19 16:10:58 -0800597f_update_ooo_enq (svm_fifo_t * f, u32 start_pos, u32 end_pos)
Florin Corasb0208062019-12-12 12:09:29 -0800598{
599 rb_tree_t *rt = &f->ooo_enq_lookup;
600 svm_fifo_chunk_t *c;
601 rb_node_t *cur;
602
Florin Corasf22f4e52019-12-19 16:10:58 -0800603 /* Use linear search if rbtree is not initialized */
604 if (PREDICT_FALSE (!rb_tree_is_init (rt)))
605 {
606 f->ooo_enq = svm_fifo_find_next_chunk (f, f->tail_chunk, start_pos);
607 return;
608 }
Florin Corasb0208062019-12-12 12:09:29 -0800609
610 if (rt->root == RBTREE_TNIL_INDEX)
611 {
612 c = f->tail_chunk;
Florin Corasf22f4e52019-12-19 16:10:58 -0800613 ASSERT (c->enq_rb_index == RBTREE_TNIL_INDEX);
614 c->enq_rb_index = rb_tree_add_custom (rt, c->start_byte,
615 pointer_to_uword (c), f_pos_lt);
Florin Corasb0208062019-12-12 12:09:29 -0800616 }
617 else
618 {
Florin Corasf22f4e52019-12-19 16:10:58 -0800619 cur = f_find_node_rbtree (rt, start_pos);
Florin Corasb0208062019-12-12 12:09:29 -0800620 c = uword_to_pointer (cur->opaque, svm_fifo_chunk_t *);
Florin Corasf22f4e52019-12-19 16:10:58 -0800621 ASSERT (f_pos_leq (c->start_byte, start_pos));
Florin Coras4375fa32019-04-19 15:56:00 -0700622 }
623
Florin Corasf22f4e52019-12-19 16:10:58 -0800624 if (f_chunk_includes_pos (c, start_pos))
Florin Corasb0208062019-12-12 12:09:29 -0800625 f->ooo_enq = c;
626
Florin Corasf22f4e52019-12-19 16:10:58 -0800627 if (f_chunk_includes_pos (c, end_pos))
Florin Corasb0208062019-12-12 12:09:29 -0800628 return;
629
630 do
631 {
632 c = c->next;
Florin Corasf22f4e52019-12-19 16:10:58 -0800633 if (!c || c->enq_rb_index != RBTREE_TNIL_INDEX)
Florin Corasb0208062019-12-12 12:09:29 -0800634 break;
635
Florin Corasf22f4e52019-12-19 16:10:58 -0800636 c->enq_rb_index = rb_tree_add_custom (rt, c->start_byte,
637 pointer_to_uword (c), f_pos_lt);
Florin Corasb0208062019-12-12 12:09:29 -0800638
Florin Corasf22f4e52019-12-19 16:10:58 -0800639 if (f_chunk_includes_pos (c, start_pos))
Florin Corasb0208062019-12-12 12:09:29 -0800640 f->ooo_enq = c;
Florin Corasb0208062019-12-12 12:09:29 -0800641 }
Florin Corasf22f4e52019-12-19 16:10:58 -0800642 while (!f_chunk_includes_pos (c, end_pos));
Florin Corasb0208062019-12-12 12:09:29 -0800643}
644
645static void
Florin Corasf22f4e52019-12-19 16:10:58 -0800646f_update_ooo_deq (svm_fifo_t * f, u32 start_pos, u32 end_pos)
Florin Corasb0208062019-12-12 12:09:29 -0800647{
648 rb_tree_t *rt = &f->ooo_deq_lookup;
649 rb_node_t *cur;
650 svm_fifo_chunk_t *c;
651
Florin Corasf22f4e52019-12-19 16:10:58 -0800652 /* Use linear search if rbtree is not initialized */
653 if (PREDICT_FALSE (!rb_tree_is_init (rt)))
654 {
655 f->ooo_deq = svm_fifo_find_chunk (f, start_pos);
656 return;
657 }
Florin Corasb0208062019-12-12 12:09:29 -0800658
659 if (rt->root == RBTREE_TNIL_INDEX)
660 {
Florin Corasf22f4e52019-12-19 16:10:58 -0800661 c = f->start_chunk;
662 ASSERT (c->deq_rb_index == RBTREE_TNIL_INDEX);
663 c->deq_rb_index = rb_tree_add_custom (rt, c->start_byte,
664 pointer_to_uword (c), f_pos_lt);
Florin Corasb0208062019-12-12 12:09:29 -0800665 }
666 else
667 {
Florin Corasf22f4e52019-12-19 16:10:58 -0800668 cur = f_find_node_rbtree (rt, start_pos);
Florin Corasb0208062019-12-12 12:09:29 -0800669 c = uword_to_pointer (cur->opaque, svm_fifo_chunk_t *);
Florin Corasf22f4e52019-12-19 16:10:58 -0800670 ASSERT (f_pos_leq (c->start_byte, start_pos));
Florin Corasb0208062019-12-12 12:09:29 -0800671 }
672
Florin Corasf22f4e52019-12-19 16:10:58 -0800673 if (f_chunk_includes_pos (c, start_pos))
Florin Corasb0208062019-12-12 12:09:29 -0800674 f->ooo_deq = c;
675
Florin Corasf22f4e52019-12-19 16:10:58 -0800676 if (f_chunk_includes_pos (c, end_pos))
Florin Corasb0208062019-12-12 12:09:29 -0800677 return;
678
679 do
680 {
681 c = c->next;
Florin Corasf22f4e52019-12-19 16:10:58 -0800682 if (!c || c->deq_rb_index != RBTREE_TNIL_INDEX)
Florin Corasb0208062019-12-12 12:09:29 -0800683 break;
684
Florin Corasf22f4e52019-12-19 16:10:58 -0800685 c->deq_rb_index = rb_tree_add_custom (rt, c->start_byte,
686 pointer_to_uword (c), f_pos_lt);
Florin Corasb0208062019-12-12 12:09:29 -0800687
Florin Corasf22f4e52019-12-19 16:10:58 -0800688 if (f_chunk_includes_pos (c, start_pos))
Florin Corasb0208062019-12-12 12:09:29 -0800689 f->ooo_deq = c;
Florin Corasb0208062019-12-12 12:09:29 -0800690 }
Florin Corasf22f4e52019-12-19 16:10:58 -0800691 while (!f_chunk_includes_pos (c, end_pos));
Florin Corasb0208062019-12-12 12:09:29 -0800692}
693
694static svm_fifo_chunk_t *
Florin Corasf22f4e52019-12-19 16:10:58 -0800695f_lookup_clear_enq_chunks (svm_fifo_t * f, svm_fifo_chunk_t * start,
696 u32 end_pos)
Florin Corasb0208062019-12-12 12:09:29 -0800697{
Florin Corasf22f4e52019-12-19 16:10:58 -0800698 rb_tree_t *rt = &f->ooo_enq_lookup;
Florin Corasb0208062019-12-12 12:09:29 -0800699 svm_fifo_chunk_t *c;
700 rb_node_t *n;
701
Florin Corasb0208062019-12-12 12:09:29 -0800702 c = start;
Florin Corasf22f4e52019-12-19 16:10:58 -0800703 while (c && !f_chunk_includes_pos (c, end_pos))
Florin Corasb0208062019-12-12 12:09:29 -0800704 {
Florin Corasf22f4e52019-12-19 16:10:58 -0800705 if (c->enq_rb_index != RBTREE_TNIL_INDEX)
Florin Corasb0208062019-12-12 12:09:29 -0800706 {
Florin Corasf22f4e52019-12-19 16:10:58 -0800707 n = rb_node (rt, c->enq_rb_index);
708 rb_tree_del_node (rt, n);
709 c->enq_rb_index = RBTREE_TNIL_INDEX;
Florin Corasb0208062019-12-12 12:09:29 -0800710 }
711
Florin Corasb0208062019-12-12 12:09:29 -0800712 c = c->next;
713 }
Florin Corasf22f4e52019-12-19 16:10:58 -0800714
715 /* No ooo segments left, so make sure the current chunk
716 * is not tracked in the enq rbtree */
717 if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX
718 && c && c->enq_rb_index != RBTREE_TNIL_INDEX)
719 {
720 n = rb_node (rt, c->enq_rb_index);
721 rb_tree_del_node (rt, n);
722 c->enq_rb_index = RBTREE_TNIL_INDEX;
723 }
Florin Corasb0208062019-12-12 12:09:29 -0800724
725 return c;
Florin Coras99ad2fc2019-04-18 21:25:49 -0700726}
727
Florin Corasf22f4e52019-12-19 16:10:58 -0800728static svm_fifo_chunk_t *
729f_lookup_clear_deq_chunks (svm_fifo_t * f, svm_fifo_chunk_t * start,
730 u32 end_pos)
Florin Corasa7570b02019-05-02 12:52:19 -0700731{
Florin Corasf22f4e52019-12-19 16:10:58 -0800732 rb_tree_t *rt = &f->ooo_deq_lookup;
733 svm_fifo_chunk_t *c;
734 rb_node_t *n;
Florin Corasa7570b02019-05-02 12:52:19 -0700735
Florin Corasf22f4e52019-12-19 16:10:58 -0800736 c = start;
737 while (c && !f_chunk_includes_pos (c, end_pos))
Florin Corasa7570b02019-05-02 12:52:19 -0700738 {
Florin Corasf22f4e52019-12-19 16:10:58 -0800739 if (c->deq_rb_index != RBTREE_TNIL_INDEX)
740 {
741 n = rb_node (rt, c->deq_rb_index);
742 rb_tree_del_node (rt, n);
743 c->deq_rb_index = RBTREE_TNIL_INDEX;
744 }
745
Florin Corasa7570b02019-05-02 12:52:19 -0700746 c = c->next;
747 }
Florin Corasa7570b02019-05-02 12:52:19 -0700748
Florin Corasf22f4e52019-12-19 16:10:58 -0800749 return c;
Florin Corasa7570b02019-05-02 12:52:19 -0700750}
751
752void
Florin Corasb095a3c2019-04-25 12:58:46 -0700753svm_fifo_free_chunk_lookup (svm_fifo_t * f)
754{
Florin Corasb0208062019-12-12 12:09:29 -0800755 rb_tree_free_nodes (&f->ooo_enq_lookup);
756 rb_tree_free_nodes (&f->ooo_deq_lookup);
Florin Corasb095a3c2019-04-25 12:58:46 -0700757}
758
759void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700760svm_fifo_free (svm_fifo_t * f)
761{
Dave Barach52851e62017-08-07 09:35:25 -0400762 ASSERT (f->refcnt > 0);
763
764 if (--f->refcnt == 0)
765 {
Florin Corasb095a3c2019-04-25 12:58:46 -0700766 /* ooo data is not allocated on segment heap */
767 svm_fifo_free_chunk_lookup (f);
Dave Barach52851e62017-08-07 09:35:25 -0400768 clib_mem_free (f);
769 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700770}
771
Florin Coras7fb0fe12018-04-09 09:24:52 -0700772void
Florin Coras87b15ce2019-04-28 21:16:30 -0700773svm_fifo_overwrite_head (svm_fifo_t * f, u8 * src, u32 len)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700774{
Florin Coras0a846802019-04-09 18:29:14 -0700775 u32 n_chunk;
Sirshak Das28aa5392019-02-05 01:33:33 -0600776 u32 head, tail, head_idx;
Florin Coras0a846802019-04-09 18:29:14 -0700777 svm_fifo_chunk_t *c;
778
Florin Corasf22f4e52019-12-19 16:10:58 -0800779 ASSERT (len <= f->size);
Sirshak Das28aa5392019-02-05 01:33:33 -0600780
781 f_load_head_tail_cons (f, &head, &tail);
Florin Corasf22f4e52019-12-19 16:10:58 -0800782
783 if (!f->head_chunk)
784 f->head_chunk = svm_fifo_find_chunk (f, head);
785
Florin Coras0a846802019-04-09 18:29:14 -0700786 c = f->head_chunk;
Florin Coras29a59c32019-05-02 12:52:19 -0700787 head_idx = head - c->start_byte;
Florin Coras0a846802019-04-09 18:29:14 -0700788 n_chunk = c->length - head_idx;
789 if (len <= n_chunk)
Florin Coras87b15ce2019-04-28 21:16:30 -0700790 clib_memcpy_fast (&c->data[head_idx], src, len);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700791 else
792 {
Florin Corasf22f4e52019-12-19 16:10:58 -0800793 ASSERT (len - n_chunk <= c->next->length);
Florin Coras87b15ce2019-04-28 21:16:30 -0700794 clib_memcpy_fast (&c->data[head_idx], src, n_chunk);
795 clib_memcpy_fast (&c->next->data[0], src + n_chunk, len - n_chunk);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700796 }
797}
Dave Barach68b0fb02017-02-28 15:15:56 -0500798
Florin Corasf22f4e52019-12-19 16:10:58 -0800799static int
Florin Coras9e61d9a2020-02-05 21:13:18 +0000800f_try_chunk_alloc (svm_fifo_t * f, u32 head, u32 tail, u32 len)
Florin Corasf22f4e52019-12-19 16:10:58 -0800801{
Florin Coras9e61d9a2020-02-05 21:13:18 +0000802 svm_fifo_chunk_t *c, *cur, *prev;
Florin Corasf22f4e52019-12-19 16:10:58 -0800803 u32 alloc_size, free_alloced;
804
805 free_alloced = f_chunk_end (f->end_chunk) - tail;
Florin Corasf22f4e52019-12-19 16:10:58 -0800806
807 alloc_size = clib_min (f->min_alloc, f->size - (tail - head));
808 alloc_size = clib_max (alloc_size, len - free_alloced);
809
810 c = fsh_alloc_chunk (f->fs_hdr, f->slice_index, alloc_size);
811 if (PREDICT_FALSE (!c))
812 return -1;
813
Florin Coras9e61d9a2020-02-05 21:13:18 +0000814 cur = c;
815 prev = f->end_chunk;
816
817 while (cur)
818 {
819 cur->start_byte = prev->start_byte + prev->length;
820 cur->enq_rb_index = RBTREE_TNIL_INDEX;
821 cur->deq_rb_index = RBTREE_TNIL_INDEX;
822
823 prev = cur;
824 cur = cur->next;
825 }
826
827 prev->next = 0;
828 f->end_chunk->next = c;
829 f->end_chunk = prev;
830
831 if (!f->tail_chunk)
832 f->tail_chunk = c;
833
Florin Corasf22f4e52019-12-19 16:10:58 -0800834 return 0;
835}
836
Florin Coras99ad2fc2019-04-18 21:25:49 -0700837int
Florin Coras87b15ce2019-04-28 21:16:30 -0700838svm_fifo_enqueue (svm_fifo_t * f, u32 len, const u8 * src)
Dave Barach68b0fb02017-02-28 15:15:56 -0500839{
Florin Coras99ad2fc2019-04-18 21:25:49 -0700840 u32 tail, head, free_count;
Florin Corasf22f4e52019-12-19 16:10:58 -0800841 svm_fifo_chunk_t *old_tail_c;
842
843 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
Florin Coras99ad2fc2019-04-18 21:25:49 -0700844
845 f_load_head_tail_prod (f, &head, &tail);
846
847 /* free space in fifo can only increase during enqueue: SPSC */
848 free_count = f_free_count (f, head, tail);
849
Florin Coras99ad2fc2019-04-18 21:25:49 -0700850 if (PREDICT_FALSE (free_count == 0))
Florin Coras87b15ce2019-04-28 21:16:30 -0700851 return SVM_FIFO_EFULL;
Florin Coras99ad2fc2019-04-18 21:25:49 -0700852
853 /* number of bytes we're going to copy */
854 len = clib_min (free_count, len);
Florin Corasf22f4e52019-12-19 16:10:58 -0800855
856 if (f_pos_gt (tail + len, f_chunk_end (f->end_chunk)))
857 {
Florin Coras9e61d9a2020-02-05 21:13:18 +0000858 if (PREDICT_FALSE (f_try_chunk_alloc (f, head, tail, len)))
Florin Corasf22f4e52019-12-19 16:10:58 -0800859 {
860 len = f_chunk_end (f->end_chunk) - tail;
861 if (!len)
862 return SVM_FIFO_EGROW;
863 }
864 }
865
866 old_tail_c = f->tail_chunk;
867
Florin Coras29a59c32019-05-02 12:52:19 -0700868 svm_fifo_copy_to_chunk (f, f->tail_chunk, tail, src, len, &f->tail_chunk);
Florin Corasf22f4e52019-12-19 16:10:58 -0800869 tail = tail + len;
Florin Coras99ad2fc2019-04-18 21:25:49 -0700870
Florin Coras87b15ce2019-04-28 21:16:30 -0700871 svm_fifo_trace_add (f, head, len, 2);
Florin Coras99ad2fc2019-04-18 21:25:49 -0700872
873 /* collect out-of-order segments */
874 if (PREDICT_FALSE (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX))
Florin Coraseaacce42019-07-02 13:07:37 -0700875 {
876 len += ooo_segment_try_collect (f, len, &tail);
Florin Corasf22f4e52019-12-19 16:10:58 -0800877 /* Tail chunk might've changed even if nothing was collected */
878 f->tail_chunk = f_lookup_clear_enq_chunks (f, old_tail_c, tail);
879 f->ooo_enq = 0;
Florin Coraseaacce42019-07-02 13:07:37 -0700880 }
Florin Coras99ad2fc2019-04-18 21:25:49 -0700881
882 /* store-rel: producer owned index (paired with load-acq in consumer) */
883 clib_atomic_store_rel_n (&f->tail, tail);
884
885 return len;
886}
887
888/**
889 * Enqueue a future segment.
890 *
891 * Two choices: either copies the entire segment, or copies nothing
892 * Returns 0 of the entire segment was copied
893 * Returns -1 if none of the segment was copied due to lack of space
894 */
895int
896svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 len, u8 * src)
897{
Florin Corasf22f4e52019-12-19 16:10:58 -0800898 u32 tail, head, free_count, enq_pos;
Florin Coras99ad2fc2019-04-18 21:25:49 -0700899
900 f_load_head_tail_prod (f, &head, &tail);
901
902 /* free space in fifo can only increase during enqueue: SPSC */
903 free_count = f_free_count (f, head, tail);
Florin Corasf22f4e52019-12-19 16:10:58 -0800904 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
Florin Coras99ad2fc2019-04-18 21:25:49 -0700905
906 /* will this request fit? */
907 if ((len + offset) > free_count)
Florin Coras87b15ce2019-04-28 21:16:30 -0700908 return SVM_FIFO_EFULL;
Florin Coras99ad2fc2019-04-18 21:25:49 -0700909
Florin Corasf22f4e52019-12-19 16:10:58 -0800910 enq_pos = tail + offset;
911
912 if (f_pos_gt (enq_pos + len, f_chunk_end (f->end_chunk)))
913 {
Florin Coras9e61d9a2020-02-05 21:13:18 +0000914 if (PREDICT_FALSE (f_try_chunk_alloc (f, head, tail, offset + len)))
Florin Corasf22f4e52019-12-19 16:10:58 -0800915 return SVM_FIFO_EGROW;
916 }
917
Florin Coras99ad2fc2019-04-18 21:25:49 -0700918 svm_fifo_trace_add (f, offset, len, 1);
Florin Coras99ad2fc2019-04-18 21:25:49 -0700919 ooo_segment_add (f, offset, head, tail, len);
Florin Coras4375fa32019-04-19 15:56:00 -0700920
Florin Corasf22f4e52019-12-19 16:10:58 -0800921 if (!f->ooo_enq || !f_chunk_includes_pos (f->ooo_enq, enq_pos))
922 f_update_ooo_enq (f, enq_pos, enq_pos + len);
Florin Coras4375fa32019-04-19 15:56:00 -0700923
Florin Corasf22f4e52019-12-19 16:10:58 -0800924 svm_fifo_copy_to_chunk (f, f->ooo_enq, enq_pos, src, len, &f->ooo_enq);
Florin Coras99ad2fc2019-04-18 21:25:49 -0700925
926 return 0;
927}
928
Florin Corasa7570b02019-05-02 12:52:19 -0700929/**
930 * Advance tail
931 */
932void
933svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 len)
934{
935 u32 tail;
936
937 ASSERT (len <= svm_fifo_max_enqueue_prod (f));
938 /* load-relaxed: producer owned index */
939 tail = f->tail;
Florin Corasf22f4e52019-12-19 16:10:58 -0800940 tail = tail + len;
Florin Coraseaacce42019-07-02 13:07:37 -0700941
Florin Corasf22f4e52019-12-19 16:10:58 -0800942 if (rb_tree_is_init (&f->ooo_enq_lookup))
943 {
944 f->tail_chunk = f_lookup_clear_enq_chunks (f, f->tail_chunk, tail);
945 f->ooo_enq = 0;
946 }
947 else
948 {
949 f->tail_chunk = svm_fifo_find_next_chunk (f, f->tail_chunk, tail);
950 }
Florin Coraseaacce42019-07-02 13:07:37 -0700951
Florin Corasa7570b02019-05-02 12:52:19 -0700952 /* store-rel: producer owned index (paired with load-acq in consumer) */
953 clib_atomic_store_rel_n (&f->tail, tail);
954}
955
Florin Corasf22f4e52019-12-19 16:10:58 -0800956always_inline svm_fifo_chunk_t *
957f_unlink_chunks (svm_fifo_t * f, u32 end_pos, u8 maybe_ooo)
958{
959 svm_fifo_chunk_t *start, *prev = 0, *c;
960 rb_tree_t *rt;
961 rb_node_t *n;
962
963 ASSERT (!f_chunk_includes_pos (f->start_chunk, end_pos));
964
965 if (maybe_ooo)
966 rt = &f->ooo_deq_lookup;
967
968 c = f->start_chunk;
969
970 do
971 {
972 if (maybe_ooo && c->deq_rb_index != RBTREE_TNIL_INDEX)
973 {
974 n = rb_node (rt, c->deq_rb_index);
975 ASSERT (n == f_find_node_rbtree (rt, c->start_byte));
976 rb_tree_del_node (rt, n);
977 c->deq_rb_index = RBTREE_TNIL_INDEX;
978 }
979 if (!c->next)
980 break;
981 prev = c;
982 c = c->next;
983 }
984 while (!f_chunk_includes_pos (c, end_pos));
985
986 if (maybe_ooo)
987 {
988 if (f->ooo_deq && f_pos_lt (f->ooo_deq->start_byte, f_chunk_end (c)))
989 f->ooo_deq = 0;
990 }
991 else
992 {
993 if (PREDICT_FALSE (f->ooo_deq != 0))
994 f->ooo_deq = 0;
995 }
996
997 /* Avoid unlinking the last chunk */
998 if (!prev)
999 return 0;
1000
1001 prev->next = 0;
1002 start = f->start_chunk;
1003 f->start_chunk = c;
1004
1005 return start;
1006}
1007
Florin Coras99ad2fc2019-04-18 21:25:49 -07001008int
Florin Coras87b15ce2019-04-28 21:16:30 -07001009svm_fifo_dequeue (svm_fifo_t * f, u32 len, u8 * dst)
Florin Coras99ad2fc2019-04-18 21:25:49 -07001010{
1011 u32 tail, head, cursize;
Dave Barach68b0fb02017-02-28 15:15:56 -05001012
Sirshak Das28aa5392019-02-05 01:33:33 -06001013 f_load_head_tail_cons (f, &head, &tail);
1014
1015 /* current size of fifo can only increase during dequeue: SPSC */
1016 cursize = f_cursize (f, head, tail);
1017
Florin Coras3e350af2017-03-30 02:54:28 -07001018 if (PREDICT_FALSE (cursize == 0))
Florin Coras87b15ce2019-04-28 21:16:30 -07001019 return SVM_FIFO_EEMPTY;
Dave Barach68b0fb02017-02-28 15:15:56 -05001020
Florin Coras99ad2fc2019-04-18 21:25:49 -07001021 len = clib_min (cursize, len);
Dave Barach68b0fb02017-02-28 15:15:56 -05001022
Florin Corasf22f4e52019-12-19 16:10:58 -08001023 if (!f->head_chunk)
1024 f->head_chunk = svm_fifo_find_chunk (f, head);
1025
1026 svm_fifo_copy_from_chunk (f, f->head_chunk, head, dst, len, &f->head_chunk);
1027 head = head + len;
1028
Florin Coras97d39e32020-09-04 08:57:27 -07001029 /* In order dequeues are not supported in combination with ooo peeking.
1030 * Use svm_fifo_dequeue_drop instead. */
1031 ASSERT (rb_tree_n_nodes (&f->ooo_deq_lookup) <= 1);
1032
Florin Corasf22f4e52019-12-19 16:10:58 -08001033 if (f_pos_geq (head, f_chunk_end (f->start_chunk)))
1034 fsh_collect_chunks (f->fs_hdr, f->slice_index,
1035 f_unlink_chunks (f, head, 0));
Florin Coras2309e7a2019-04-18 18:58:10 -07001036
Sirshak Das28aa5392019-02-05 01:33:33 -06001037 /* store-rel: consumer owned index (paired with load-acq in producer) */
1038 clib_atomic_store_rel_n (&f->head, head);
Dave Barach68b0fb02017-02-28 15:15:56 -05001039
Florin Coras0a846802019-04-09 18:29:14 -07001040 return len;
Dave Barach68b0fb02017-02-28 15:15:56 -05001041}
1042
Dave Barach68b0fb02017-02-28 15:15:56 -05001043int
Florin Coras4375fa32019-04-19 15:56:00 -07001044svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 len, u8 * dst)
Florin Corasf6359c82017-06-19 12:26:09 -04001045{
Florin Coras4375fa32019-04-19 15:56:00 -07001046 u32 tail, head, cursize, head_idx;
Dave Barach68b0fb02017-02-28 15:15:56 -05001047
Sirshak Das28aa5392019-02-05 01:33:33 -06001048 f_load_head_tail_cons (f, &head, &tail);
1049
1050 /* current size of fifo can only increase during peek: SPSC */
1051 cursize = f_cursize (f, head, tail);
1052
Florin Coras4375fa32019-04-19 15:56:00 -07001053 if (PREDICT_FALSE (cursize < offset))
Florin Coras87b15ce2019-04-28 21:16:30 -07001054 return SVM_FIFO_EEMPTY;
Dave Barach68b0fb02017-02-28 15:15:56 -05001055
Florin Coras4375fa32019-04-19 15:56:00 -07001056 len = clib_min (cursize - offset, len);
Florin Corasf22f4e52019-12-19 16:10:58 -08001057 head_idx = head + offset;
Florin Corasb0208062019-12-12 12:09:29 -08001058
BenoƮt Gannedf601ae2020-10-20 14:31:55 +02001059 CLIB_MEM_UNPOISON (f->ooo_deq, sizeof (*f->ooo_deq));
Florin Corasf22f4e52019-12-19 16:10:58 -08001060 if (!f->ooo_deq || !f_chunk_includes_pos (f->ooo_deq, head_idx))
1061 f_update_ooo_deq (f, head_idx, head_idx + len);
Dave Barach68b0fb02017-02-28 15:15:56 -05001062
Florin Coras4375fa32019-04-19 15:56:00 -07001063 svm_fifo_copy_from_chunk (f, f->ooo_deq, head_idx, dst, len, &f->ooo_deq);
Florin Coras0a846802019-04-09 18:29:14 -07001064 return len;
Dave Barach68b0fb02017-02-28 15:15:56 -05001065}
1066
1067int
Florin Coras87b15ce2019-04-28 21:16:30 -07001068svm_fifo_dequeue_drop (svm_fifo_t * f, u32 len)
Dave Barach68b0fb02017-02-28 15:15:56 -05001069{
Florin Coras87b15ce2019-04-28 21:16:30 -07001070 u32 total_drop_bytes, tail, head, cursize;
Dave Barach68b0fb02017-02-28 15:15:56 -05001071
Sirshak Das28aa5392019-02-05 01:33:33 -06001072 f_load_head_tail_cons (f, &head, &tail);
1073
Florin Coras87b15ce2019-04-28 21:16:30 -07001074 /* number of bytes available */
Sirshak Das28aa5392019-02-05 01:33:33 -06001075 cursize = f_cursize (f, head, tail);
Florin Coras3e350af2017-03-30 02:54:28 -07001076 if (PREDICT_FALSE (cursize == 0))
Florin Coras87b15ce2019-04-28 21:16:30 -07001077 return SVM_FIFO_EEMPTY;
Dave Barach68b0fb02017-02-28 15:15:56 -05001078
Sirshak Das28aa5392019-02-05 01:33:33 -06001079 /* number of bytes we're going to drop */
Florin Coras87b15ce2019-04-28 21:16:30 -07001080 total_drop_bytes = clib_min (cursize, len);
Dave Barach68b0fb02017-02-28 15:15:56 -05001081
Nathan Skrzypczakb4c67752019-06-20 09:58:37 +02001082 svm_fifo_trace_add (f, tail, total_drop_bytes, 3);
1083
Sirshak Das28aa5392019-02-05 01:33:33 -06001084 /* move head */
Florin Corasf22f4e52019-12-19 16:10:58 -08001085 head = head + total_drop_bytes;
Florin Coras3eb50622017-07-13 01:24:57 -04001086
Florin Corasf22f4e52019-12-19 16:10:58 -08001087 if (f_pos_geq (head, f_chunk_end (f->start_chunk)))
1088 {
1089 fsh_collect_chunks (f->fs_hdr, f->slice_index,
1090 f_unlink_chunks (f, head, 1));
1091 f->head_chunk =
1092 f_chunk_includes_pos (f->start_chunk, head) ? f->start_chunk : 0;
1093 }
Florin Coras0a6562c2019-08-05 09:39:47 -07001094
Sirshak Das28aa5392019-02-05 01:33:33 -06001095 /* store-rel: consumer owned index (paired with load-acq in producer) */
1096 clib_atomic_store_rel_n (&f->head, head);
Dave Barach68b0fb02017-02-28 15:15:56 -05001097
1098 return total_drop_bytes;
1099}
1100
Florin Corasf22f4e52019-12-19 16:10:58 -08001101/**
1102 * Drop all data from fifo
1103 *
Florin Corasf22f4e52019-12-19 16:10:58 -08001104 */
Florin Coras25579b42018-06-06 17:55:02 -07001105void
1106svm_fifo_dequeue_drop_all (svm_fifo_t * f)
1107{
Florin Corasf22f4e52019-12-19 16:10:58 -08001108 u32 head, tail;
Florin Coraseaacce42019-07-02 13:07:37 -07001109
Florin Corasf22f4e52019-12-19 16:10:58 -08001110 f_load_head_tail_all_acq (f, &head, &tail);
Florin Coraseaacce42019-07-02 13:07:37 -07001111
Florin Corasf22f4e52019-12-19 16:10:58 -08001112 if (!f->head_chunk || !f_chunk_includes_pos (f->head_chunk, head))
1113 f->head_chunk = svm_fifo_find_chunk (f, head);
1114
1115 f->head_chunk = f_lookup_clear_deq_chunks (f, f->head_chunk, tail);
1116
1117 if (f_pos_geq (tail, f_chunk_end (f->start_chunk)))
1118 fsh_collect_chunks (f->fs_hdr, f->slice_index,
1119 f_unlink_chunks (f, tail, 0));
Florin Coras0a6562c2019-08-05 09:39:47 -07001120
Sirshak Das28aa5392019-02-05 01:33:33 -06001121 /* store-rel: consumer owned index (paired with load-acq in producer) */
1122 clib_atomic_store_rel_n (&f->head, tail);
Florin Coras25579b42018-06-06 17:55:02 -07001123}
1124
Florin Coras2cba8532018-09-11 16:33:36 -07001125int
Florin Corasf22f4e52019-12-19 16:10:58 -08001126svm_fifo_fill_chunk_list (svm_fifo_t * f)
1127{
1128 u32 head, tail;
1129
1130 f_load_head_tail_prod (f, &head, &tail);
1131
1132 if (f_chunk_end (f->end_chunk) - head >= f->size)
1133 return 0;
1134
Florin Coras9e61d9a2020-02-05 21:13:18 +00001135 if (f_try_chunk_alloc (f, head, tail, f->size - (tail - head)))
Florin Corasf22f4e52019-12-19 16:10:58 -08001136 return SVM_FIFO_EGROW;
1137
1138 return 0;
1139}
1140
1141int
Florin Corasd1cc38d2020-10-11 11:05:04 -07001142svm_fifo_segments (svm_fifo_t * f, u32 offset, svm_fifo_seg_t * fs,
1143 u32 n_segs, u32 max_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07001144{
Florin Corasd1cc38d2020-10-11 11:05:04 -07001145 u32 cursize, to_read, head, tail, fs_index = 1;
1146 u32 n_bytes, head_pos, len, start;
Florin Corasd68faf82020-09-25 15:18:13 -07001147 svm_fifo_chunk_t *c;
Florin Coras2cba8532018-09-11 16:33:36 -07001148
Sirshak Das28aa5392019-02-05 01:33:33 -06001149 f_load_head_tail_cons (f, &head, &tail);
1150
1151 /* consumer function, cursize can only increase while we're working */
1152 cursize = f_cursize (f, head, tail);
1153
Florin Coras2cba8532018-09-11 16:33:36 -07001154 if (PREDICT_FALSE (cursize == 0))
Florin Coras87b15ce2019-04-28 21:16:30 -07001155 return SVM_FIFO_EEMPTY;
Florin Coras2cba8532018-09-11 16:33:36 -07001156
Florin Corasd1cc38d2020-10-11 11:05:04 -07001157 if (offset >= cursize)
1158 return SVM_FIFO_EEMPTY;
1159
1160 to_read = clib_min (cursize - offset, max_bytes);
1161 start = head + offset;
1162
1163 if (!f->head_chunk)
1164 f->head_chunk = svm_fifo_find_chunk (f, head);
Florin Coras2cba8532018-09-11 16:33:36 -07001165
Florin Corasd68faf82020-09-25 15:18:13 -07001166 c = f->head_chunk;
Florin Corasd1cc38d2020-10-11 11:05:04 -07001167
1168 while (!f_chunk_includes_pos (c, start))
1169 c = c->next;
1170
1171 head_pos = start - c->start_byte;
Florin Corasd68faf82020-09-25 15:18:13 -07001172 fs[0].data = c->data + head_pos;
Florin Corasd1cc38d2020-10-11 11:05:04 -07001173 fs[0].len = clib_min (c->length - head_pos, cursize - offset);
Florin Corasd68faf82020-09-25 15:18:13 -07001174 n_bytes = fs[0].len;
Florin Corasd68faf82020-09-25 15:18:13 -07001175
1176 while (n_bytes < to_read && fs_index < n_segs)
Florin Coras2cba8532018-09-11 16:33:36 -07001177 {
Florin Corasd1cc38d2020-10-11 11:05:04 -07001178 c = c->next;
Florin Corasd68faf82020-09-25 15:18:13 -07001179 len = clib_min (c->length, to_read - n_bytes);
1180 fs[fs_index].data = c->data;
1181 fs[fs_index].len = len;
1182 n_bytes += len;
Florin Corasd68faf82020-09-25 15:18:13 -07001183 fs_index += 1;
Florin Coras2cba8532018-09-11 16:33:36 -07001184 }
Florin Coras2cba8532018-09-11 16:33:36 -07001185
Florin Corasd68faf82020-09-25 15:18:13 -07001186 return n_bytes;
Sirshak Das28aa5392019-02-05 01:33:33 -06001187}
1188
Florin Coras87b15ce2019-04-28 21:16:30 -07001189/**
1190 * Clones fifo
1191 *
1192 * Assumptions:
1193 * - no prod and cons are accessing either dest or src fifo
1194 * - fifo is not multi chunk
1195 */
Sirshak Das28aa5392019-02-05 01:33:33 -06001196void
1197svm_fifo_clone (svm_fifo_t * df, svm_fifo_t * sf)
1198{
1199 u32 head, tail;
Florin Corasf22f4e52019-12-19 16:10:58 -08001200
1201 /* Support only single chunk clones for now */
1202 ASSERT (svm_fifo_n_chunks (sf) == 1);
1203
Florin Coras0a846802019-04-09 18:29:14 -07001204 clib_memcpy_fast (df->head_chunk->data, sf->head_chunk->data, sf->size);
Sirshak Das28aa5392019-02-05 01:33:33 -06001205
1206 f_load_head_tail_all_acq (sf, &head, &tail);
1207 clib_atomic_store_rel_n (&df->head, head);
1208 clib_atomic_store_rel_n (&df->tail, tail);
Florin Coras2cba8532018-09-11 16:33:36 -07001209}
1210
Dave Barach1f75cfd2017-04-14 16:46:44 -04001211u32
Florin Coras87b15ce2019-04-28 21:16:30 -07001212svm_fifo_n_ooo_segments (svm_fifo_t * f)
Dave Barach1f75cfd2017-04-14 16:46:44 -04001213{
1214 return pool_elts (f->ooo_segments);
1215}
1216
1217ooo_segment_t *
1218svm_fifo_first_ooo_segment (svm_fifo_t * f)
1219{
1220 return pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
1221}
1222
Florin Corasc28764f2017-04-26 00:08:42 -07001223/**
1224 * Set fifo pointers to requested offset
1225 */
1226void
Florin Coras4375fa32019-04-19 15:56:00 -07001227svm_fifo_init_pointers (svm_fifo_t * f, u32 head, u32 tail)
Florin Corasc28764f2017-04-26 00:08:42 -07001228{
Florin Corasf22f4e52019-12-19 16:10:58 -08001229 svm_fifo_chunk_t *c;
1230
Florin Coras4375fa32019-04-19 15:56:00 -07001231 clib_atomic_store_rel_n (&f->head, head);
1232 clib_atomic_store_rel_n (&f->tail, tail);
Florin Corasf22f4e52019-12-19 16:10:58 -08001233
1234 c = svm_fifo_find_chunk (f, head);
1235 ASSERT (c != 0);
1236 f->head_chunk = f->ooo_deq = c;
1237 c = svm_fifo_find_chunk (f, tail);
1238 ASSERT (c != 0);
1239 f->tail_chunk = f->ooo_enq = c;
Florin Corasc28764f2017-04-26 00:08:42 -07001240}
1241
Florin Coras72b04282019-01-14 17:23:11 -08001242void
1243svm_fifo_add_subscriber (svm_fifo_t * f, u8 subscriber)
1244{
1245 if (f->n_subscribers >= SVM_FIFO_MAX_EVT_SUBSCRIBERS)
1246 return;
1247 f->subscribers[f->n_subscribers++] = subscriber;
1248}
1249
1250void
1251svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber)
1252{
1253 int i;
1254
1255 for (i = 0; i < f->n_subscribers; i++)
1256 {
1257 if (f->subscribers[i] != subscriber)
1258 continue;
1259 f->subscribers[i] = f->subscribers[f->n_subscribers - 1];
1260 f->n_subscribers--;
1261 break;
1262 }
1263}
1264
Florin Coraseaacce42019-07-02 13:07:37 -07001265u8
1266svm_fifo_is_sane (svm_fifo_t * f)
1267{
Florin Corasf22f4e52019-12-19 16:10:58 -08001268 svm_fifo_chunk_t *tmp;
Florin Coraseaacce42019-07-02 13:07:37 -07001269
Florin Corasf22f4e52019-12-19 16:10:58 -08001270 if (f->head_chunk && !f_chunk_includes_pos (f->head_chunk, f->head))
1271 return 0;
1272 if (f->tail_chunk && !f_chunk_includes_pos (f->tail_chunk, f->tail))
1273 return 0;
1274 if (f->ooo_deq)
1275 {
1276 if (rb_tree_is_init (&f->ooo_deq_lookup))
1277 {
1278 if (f_pos_lt (f->ooo_deq->start_byte, f->start_chunk->start_byte)
1279 || f_pos_gt (f->ooo_deq->start_byte,
1280 f_chunk_end (f->end_chunk)))
1281 return 0;
1282
1283 tmp = f_find_chunk_rbtree (&f->ooo_deq_lookup,
1284 f->ooo_deq->start_byte);
1285 }
1286 else
1287 tmp = svm_fifo_find_chunk (f, f->ooo_deq->start_byte);
1288 if (tmp != f->ooo_deq)
1289 return 0;
1290 }
1291 if (f->ooo_enq)
1292 {
1293 if (rb_tree_is_init (&f->ooo_enq_lookup))
1294 {
1295 if (f_pos_lt (f->ooo_enq->start_byte, f->start_chunk->start_byte)
1296 || f_pos_gt (f->ooo_enq->start_byte,
1297 f_chunk_end (f->end_chunk)))
1298 return 0;
1299
1300 tmp = f_find_chunk_rbtree (&f->ooo_enq_lookup,
1301 f->ooo_enq->start_byte);
1302 }
1303 else
1304 {
1305 tmp = svm_fifo_find_next_chunk (f, f->tail_chunk,
1306 f->ooo_enq->start_byte);
1307 }
1308 if (tmp != f->ooo_enq)
1309 return 0;
1310 }
1311
1312 if (f->start_chunk->next)
Florin Coraseaacce42019-07-02 13:07:37 -07001313 {
1314 svm_fifo_chunk_t *c, *prev = 0, *tmp;
Florin Corasf22f4e52019-12-19 16:10:58 -08001315 u32 chunks_bytes = 0;
Florin Coraseaacce42019-07-02 13:07:37 -07001316
1317 c = f->start_chunk;
1318 do
1319 {
1320 tmp = svm_fifo_find_chunk (f, c->start_byte);
1321 if (tmp != c)
1322 return 0;
1323 if (prev && (prev->start_byte + prev->length != c->start_byte))
1324 return 0;
Florin Corasb0208062019-12-12 12:09:29 -08001325
Florin Corasf22f4e52019-12-19 16:10:58 -08001326 if (c->enq_rb_index != RBTREE_TNIL_INDEX)
Florin Corasb0208062019-12-12 12:09:29 -08001327 {
Florin Corasf22f4e52019-12-19 16:10:58 -08001328 tmp = f_find_chunk_rbtree (&f->ooo_enq_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 Corasf22f4e52019-12-19 16:10:58 -08001334 }
1335 if (c->deq_rb_index != RBTREE_TNIL_INDEX)
1336 {
1337 tmp = f_find_chunk_rbtree (&f->ooo_deq_lookup, c->start_byte);
Florin Corasb0208062019-12-12 12:09:29 -08001338 if (tmp)
1339 {
Florin Corasb0208062019-12-12 12:09:29 -08001340 if (tmp != c)
1341 return 0;
1342 }
Florin Corasb0208062019-12-12 12:09:29 -08001343 }
1344
Florin Corasf22f4e52019-12-19 16:10:58 -08001345 chunks_bytes += c->length;
Florin Coraseaacce42019-07-02 13:07:37 -07001346 prev = c;
1347 c = c->next;
1348 }
Florin Corasf22f4e52019-12-19 16:10:58 -08001349 while (c);
Florin Coraseaacce42019-07-02 13:07:37 -07001350
Florin Corasf22f4e52019-12-19 16:10:58 -08001351 if (chunks_bytes < f->tail - f->head)
Florin Coraseaacce42019-07-02 13:07:37 -07001352 return 0;
1353 }
1354
1355 return 1;
1356}
1357
Florin Corasf22f4e52019-12-19 16:10:58 -08001358u32
1359svm_fifo_n_chunks (svm_fifo_t * f)
Ryujiro Shibuya8e20fe72019-10-16 06:30:02 +01001360{
Florin Corasf22f4e52019-12-19 16:10:58 -08001361 svm_fifo_chunk_t *c;
1362 int n_chunks = 0;
Ryujiro Shibuya8e20fe72019-10-16 06:30:02 +01001363
Florin Corasf22f4e52019-12-19 16:10:58 -08001364 c = f->start_chunk;
1365 while (c)
1366 {
1367 n_chunks++;
1368 c = c->next;
Ryujiro Shibuya8e20fe72019-10-16 06:30:02 +01001369 }
1370
Florin Corasf22f4e52019-12-19 16:10:58 -08001371 return n_chunks;
Ryujiro Shibuya8e20fe72019-10-16 06:30:02 +01001372}
1373
Florin Coras87b15ce2019-04-28 21:16:30 -07001374u8 *
1375format_ooo_segment (u8 * s, va_list * args)
1376{
Florin Corasf22f4e52019-12-19 16:10:58 -08001377 svm_fifo_t __clib_unused *f = va_arg (*args, svm_fifo_t *);
Florin Coras87b15ce2019-04-28 21:16:30 -07001378 ooo_segment_t *seg = va_arg (*args, ooo_segment_t *);
Florin Corasf22f4e52019-12-19 16:10:58 -08001379 s = format (s, "[%u, %u], len %u, next %d, prev %d", seg->start,
1380 seg->start + seg->length, seg->length, seg->next, seg->prev);
Florin Coras87b15ce2019-04-28 21:16:30 -07001381 return s;
1382}
1383
1384u8 *
1385svm_fifo_dump_trace (u8 * s, svm_fifo_t * f)
1386{
1387#if SVM_FIFO_TRACE
1388 svm_fifo_trace_elem_t *seg = 0;
1389 int i = 0;
1390
1391 if (f->trace)
1392 {
1393 vec_foreach (seg, f->trace)
1394 {
1395 s = format (s, "{%u, %u, %u}, ", seg->offset, seg->len, seg->action);
1396 i++;
1397 if (i % 5 == 0)
1398 s = format (s, "\n");
1399 }
1400 s = format (s, "\n");
1401 }
1402 return s;
1403#else
1404 return 0;
1405#endif
1406}
1407
1408u8 *
1409svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose)
1410{
1411 int i, trace_len;
1412 u8 *data = 0;
1413 svm_fifo_trace_elem_t *trace;
1414 u32 offset;
Dave Barach11fb09e2020-08-06 12:10:09 -04001415 svm_fifo_t *placeholder_fifo;
Florin Coras87b15ce2019-04-28 21:16:30 -07001416
1417 if (!f)
1418 return s;
1419
1420#if SVM_FIFO_TRACE
1421 trace = f->trace;
1422 trace_len = vec_len (trace);
1423#else
1424 trace = 0;
1425 trace_len = 0;
1426#endif
1427
Dave Barach11fb09e2020-08-06 12:10:09 -04001428 placeholder_fifo = svm_fifo_alloc (f->size);
Florin Corasf22f4e52019-12-19 16:10:58 -08001429 svm_fifo_init (f, f->size);
1430 clib_memset (f->head_chunk->data, 0xFF, f->size);
1431 vec_validate (data, f->size);
Florin Coras87b15ce2019-04-28 21:16:30 -07001432 for (i = 0; i < vec_len (data); i++)
1433 data[i] = i;
1434
1435 for (i = 0; i < trace_len; i++)
1436 {
1437 offset = trace[i].offset;
1438 if (trace[i].action == 1)
1439 {
1440 if (verbose)
1441 s = format (s, "adding [%u, %u]:", trace[i].offset,
Florin Corasf22f4e52019-12-19 16:10:58 -08001442 (trace[i].offset + trace[i].len));
Dave Barach11fb09e2020-08-06 12:10:09 -04001443 svm_fifo_enqueue_with_offset (placeholder_fifo, trace[i].offset,
Florin Coras87b15ce2019-04-28 21:16:30 -07001444 trace[i].len, &data[offset]);
1445 }
1446 else if (trace[i].action == 2)
1447 {
1448 if (verbose)
1449 s = format (s, "adding [%u, %u]:", 0, trace[i].len);
Dave Barach11fb09e2020-08-06 12:10:09 -04001450 svm_fifo_enqueue (placeholder_fifo, trace[i].len, &data[offset]);
Florin Coras87b15ce2019-04-28 21:16:30 -07001451 }
1452 else if (!no_read)
1453 {
1454 if (verbose)
1455 s = format (s, "read: %u", trace[i].len);
Dave Barach11fb09e2020-08-06 12:10:09 -04001456 svm_fifo_dequeue_drop (placeholder_fifo, trace[i].len);
Florin Coras87b15ce2019-04-28 21:16:30 -07001457 }
1458 if (verbose)
Dave Barach11fb09e2020-08-06 12:10:09 -04001459 s = format (s, "%U", format_svm_fifo, placeholder_fifo, 1);
Florin Coras87b15ce2019-04-28 21:16:30 -07001460 }
1461
Dave Barach11fb09e2020-08-06 12:10:09 -04001462 s = format (s, "result: %U", format_svm_fifo, placeholder_fifo, 1);
Florin Coras87b15ce2019-04-28 21:16:30 -07001463
1464 return s;
1465}
1466
1467u8 *
1468format_ooo_list (u8 * s, va_list * args)
1469{
1470 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
1471 u32 indent = va_arg (*args, u32);
1472 u32 ooo_segment_index = f->ooos_list_head;
1473 ooo_segment_t *seg;
1474
1475 while (ooo_segment_index != OOO_SEGMENT_INVALID_INDEX)
1476 {
1477 seg = pool_elt_at_index (f->ooo_segments, ooo_segment_index);
1478 s = format (s, "%U%U\n", format_white_space, indent, format_ooo_segment,
1479 f, seg);
1480 ooo_segment_index = seg->next;
1481 }
1482
1483 return s;
1484}
1485
1486u8 *
1487format_svm_fifo (u8 * s, va_list * args)
1488{
1489 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
1490 int verbose = va_arg (*args, int);
1491 u32 indent;
1492
1493 if (!s)
1494 return s;
1495
1496 indent = format_get_indent (s);
Florin Corasf22f4e52019-12-19 16:10:58 -08001497 s = format (s, "cursize %u nitems %u has_event %d min_alloc %u\n",
1498 svm_fifo_max_dequeue (f), f->size, f->has_event, f->min_alloc);
Florin Coras87b15ce2019-04-28 21:16:30 -07001499 s = format (s, "%Uhead %u tail %u segment manager %u\n", format_white_space,
Florin Corasf22f4e52019-12-19 16:10:58 -08001500 indent, f->head, f->tail, f->segment_manager);
Florin Coras87b15ce2019-04-28 21:16:30 -07001501
1502 if (verbose > 1)
1503 s = format (s, "%Uvpp session %d thread %d app session %d thread %d\n",
1504 format_white_space, indent, f->master_session_index,
1505 f->master_thread_index, f->client_session_index,
1506 f->client_thread_index);
1507
1508 if (verbose)
1509 {
1510 s = format (s, "%Uooo pool %d active elts newest %u\n",
1511 format_white_space, indent, pool_elts (f->ooo_segments),
1512 f->ooos_newest);
1513 if (svm_fifo_has_ooo_data (f))
1514 s = format (s, " %U", format_ooo_list, f, indent, verbose);
1515 }
1516 return s;
1517}
1518
Florin Coras3aa7af32018-06-29 08:44:31 -07001519#endif
Dave Barach68b0fb02017-02-28 15:15:56 -05001520/*
1521 * fd.io coding-style-patch-verification: ON
1522 *
1523 * Local Variables:
1524 * eval: (c-set-style "gnu")
1525 * End:
1526 */