blob: 817cb8d2639e1e7d60a0a52e94848103d412c16d [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
Florin Coras6792ec02017-03-13 03:49:51 -070016#include <svm/svm_fifo.h>
Florin Corasf6359c82017-06-19 12:26:09 -040017#include <vppinfra/cpu.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050018
Florin Corasf03a59a2017-06-09 21:07:32 -070019static inline u8
20position_lt (svm_fifo_t * f, u32 a, u32 b)
21{
Dave Barach2c25a622017-06-26 11:35:07 -040022 return (ooo_segment_distance_from_tail (f, a)
23 < ooo_segment_distance_from_tail (f, b));
Florin Corasf03a59a2017-06-09 21:07:32 -070024}
25
26static inline u8
27position_leq (svm_fifo_t * f, u32 a, u32 b)
28{
Dave Barach2c25a622017-06-26 11:35:07 -040029 return (ooo_segment_distance_from_tail (f, a)
30 <= ooo_segment_distance_from_tail (f, b));
Florin Corasf03a59a2017-06-09 21:07:32 -070031}
32
33static inline u8
34position_gt (svm_fifo_t * f, u32 a, u32 b)
35{
Dave Barach2c25a622017-06-26 11:35:07 -040036 return (ooo_segment_distance_from_tail (f, a)
37 > ooo_segment_distance_from_tail (f, b));
Florin Corasf03a59a2017-06-09 21:07:32 -070038}
39
40static inline u32
41position_diff (svm_fifo_t * f, u32 posa, u32 posb)
42{
Dave Barach2c25a622017-06-26 11:35:07 -040043 return ooo_segment_distance_from_tail (f, posa)
44 - ooo_segment_distance_from_tail (f, posb);
Florin Corasf03a59a2017-06-09 21:07:32 -070045}
46
47static inline u32
48ooo_segment_end_pos (svm_fifo_t * f, ooo_segment_t * s)
49{
50 return (s->start + s->length) % f->nitems;
51}
Dave Barach1f75cfd2017-04-14 16:46:44 -040052
Florin Coras3aa7af32018-06-29 08:44:31 -070053#ifndef CLIB_MARCH_VARIANT
54
Dave Barach1f75cfd2017-04-14 16:46:44 -040055u8 *
56format_ooo_segment (u8 * s, va_list * args)
57{
Florin Coras1f152cd2017-08-18 19:28:03 -070058 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
Dave Barach1f75cfd2017-04-14 16:46:44 -040059 ooo_segment_t *seg = va_arg (*args, ooo_segment_t *);
Florin Coras1f152cd2017-08-18 19:28:03 -070060 u32 normalized_start = (seg->start + f->nitems - f->tail) % f->nitems;
61 s = format (s, "[%u, %u], len %u, next %d, prev %d", normalized_start,
62 (normalized_start + seg->length) % f->nitems, seg->length,
63 seg->next, seg->prev);
Dave Barach1f75cfd2017-04-14 16:46:44 -040064 return s;
65}
66
67u8 *
Florin Coras3eb50622017-07-13 01:24:57 -040068svm_fifo_dump_trace (u8 * s, svm_fifo_t * f)
69{
70#if SVM_FIFO_TRACE
71 svm_fifo_trace_elem_t *seg = 0;
72 int i = 0;
73
74 if (f->trace)
75 {
76 vec_foreach (seg, f->trace)
77 {
78 s = format (s, "{%u, %u, %u}, ", seg->offset, seg->len, seg->action);
79 i++;
80 if (i % 5 == 0)
81 s = format (s, "\n");
82 }
83 s = format (s, "\n");
84 }
85 return s;
86#else
87 return 0;
88#endif
89}
90
91u8 *
92svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose)
93{
94 int i, trace_len;
95 u8 *data = 0;
96 svm_fifo_trace_elem_t *trace;
97 u32 offset;
98 svm_fifo_t *dummy_fifo;
99
100 if (!f)
101 return s;
102
103#if SVM_FIFO_TRACE
104 trace = f->trace;
105 trace_len = vec_len (trace);
106#else
107 trace = 0;
108 trace_len = 0;
109#endif
110
111 dummy_fifo = svm_fifo_create (f->nitems);
Dave Barachb7b92992018-10-17 10:38:51 -0400112 clib_memset (f->data, 0xFF, f->nitems);
Florin Coras3eb50622017-07-13 01:24:57 -0400113
114 vec_validate (data, f->nitems);
115 for (i = 0; i < vec_len (data); i++)
116 data[i] = i;
117
118 for (i = 0; i < trace_len; i++)
119 {
120 offset = trace[i].offset;
121 if (trace[i].action == 1)
122 {
123 if (verbose)
124 s = format (s, "adding [%u, %u]:", trace[i].offset,
125 (trace[i].offset +
126 trace[i].len) % dummy_fifo->nitems);
127 svm_fifo_enqueue_with_offset (dummy_fifo, trace[i].offset,
128 trace[i].len, &data[offset]);
129 }
130 else if (trace[i].action == 2)
131 {
132 if (verbose)
133 s = format (s, "adding [%u, %u]:", 0, trace[i].len);
134 svm_fifo_enqueue_nowait (dummy_fifo, trace[i].len, &data[offset]);
135 }
136 else if (!no_read)
137 {
138 if (verbose)
139 s = format (s, "read: %u", trace[i].len);
140 svm_fifo_dequeue_drop (dummy_fifo, trace[i].len);
141 }
142 if (verbose)
143 s = format (s, "%U", format_svm_fifo, dummy_fifo, 1);
144 }
145
146 s = format (s, "result: %U", format_svm_fifo, dummy_fifo, 1);
147
148 return s;
149}
150
151u8 *
Dave Barach1f75cfd2017-04-14 16:46:44 -0400152format_ooo_list (u8 * s, va_list * args)
153{
154 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
Florin Corasde9a8492018-10-24 22:18:58 -0700155 u32 indent = va_arg (*args, u32);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400156 u32 ooo_segment_index = f->ooos_list_head;
157 ooo_segment_t *seg;
158
159 while (ooo_segment_index != OOO_SEGMENT_INVALID_INDEX)
160 {
161 seg = pool_elt_at_index (f->ooo_segments, ooo_segment_index);
Florin Corasde9a8492018-10-24 22:18:58 -0700162 s = format (s, "%U%U\n", format_white_space, indent, format_ooo_segment,
163 f, seg);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400164 ooo_segment_index = seg->next;
165 }
Florin Coras3eb50622017-07-13 01:24:57 -0400166
Dave Barach1f75cfd2017-04-14 16:46:44 -0400167 return s;
168}
169
Florin Corasb59a7052017-04-18 22:07:29 -0700170u8 *
171format_svm_fifo (u8 * s, va_list * args)
172{
173 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
174 int verbose = va_arg (*args, int);
Florin Corasde9a8492018-10-24 22:18:58 -0700175 u32 indent;
Florin Corasb59a7052017-04-18 22:07:29 -0700176
Florin Coras7fb0fe12018-04-09 09:24:52 -0700177 if (!s)
178 return s;
179
Florin Corasde9a8492018-10-24 22:18:58 -0700180 indent = format_get_indent (s);
Florin Corasb59a7052017-04-18 22:07:29 -0700181 s = format (s, "cursize %u nitems %u has_event %d\n",
182 f->cursize, f->nitems, f->has_event);
Florin Corasde9a8492018-10-24 22:18:58 -0700183 s = format (s, "%Uhead %d tail %d segment manager %u\n", format_white_space,
184 indent, f->head, f->tail, f->segment_manager);
Florin Corasb59a7052017-04-18 22:07:29 -0700185
186 if (verbose > 1)
Florin Corasde9a8492018-10-24 22:18:58 -0700187 s = format (s, "%Uvpp session %d thread %d app session %d thread %d\n",
188 format_white_space, indent, f->master_session_index,
189 f->master_thread_index, f->client_session_index,
190 f->client_thread_index);
Florin Corasb59a7052017-04-18 22:07:29 -0700191
192 if (verbose)
193 {
Florin Corasde9a8492018-10-24 22:18:58 -0700194 s = format (s, "%Uooo pool %d active elts newest %u\n",
195 format_white_space, indent, pool_elts (f->ooo_segments),
196 f->ooos_newest);
Florin Corasbb292f42017-05-19 09:49:19 -0700197 if (svm_fifo_has_ooo_data (f))
Florin Corasde9a8492018-10-24 22:18:58 -0700198 s = format (s, " %U", format_ooo_list, f, indent, verbose);
Florin Corasb59a7052017-04-18 22:07:29 -0700199 }
200 return s;
201}
202
Dave Barach68b0fb02017-02-28 15:15:56 -0500203/** create an svm fifo, in the current heap. Fails vs blow up the process */
204svm_fifo_t *
205svm_fifo_create (u32 data_size_in_bytes)
206{
207 svm_fifo_t *f;
Dave Barach818eb542017-08-02 13:56:13 -0400208 u32 rounded_data_size;
Dave Barach68b0fb02017-02-28 15:15:56 -0500209
Dave Barach818eb542017-08-02 13:56:13 -0400210 /* always round fifo data size to the next highest power-of-two */
211 rounded_data_size = (1 << (max_log2 (data_size_in_bytes)));
212 f = clib_mem_alloc_aligned_or_null (sizeof (*f) + rounded_data_size,
Dave Barach68b0fb02017-02-28 15:15:56 -0500213 CLIB_CACHE_LINE_BYTES);
214 if (f == 0)
215 return 0;
216
Dave Barachb7b92992018-10-17 10:38:51 -0400217 clib_memset (f, 0, sizeof (*f));
Dave Barach68b0fb02017-02-28 15:15:56 -0500218 f->nitems = data_size_in_bytes;
219 f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
Florin Coras326b81e2018-10-04 19:03:05 -0700220 f->ct_session_index = SVM_FIFO_INVALID_SESSION_INDEX;
Dave Barach52851e62017-08-07 09:35:25 -0400221 f->refcnt = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500222 return (f);
223}
224
Florin Coras6cf30ad2017-04-04 23:08:23 -0700225void
226svm_fifo_free (svm_fifo_t * f)
227{
Dave Barach52851e62017-08-07 09:35:25 -0400228 ASSERT (f->refcnt > 0);
229
230 if (--f->refcnt == 0)
231 {
232 pool_free (f->ooo_segments);
233 clib_mem_free (f);
234 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700235}
Florin Coras3aa7af32018-06-29 08:44:31 -0700236#endif
Florin Coras6cf30ad2017-04-04 23:08:23 -0700237
Dave Barach68b0fb02017-02-28 15:15:56 -0500238always_inline ooo_segment_t *
239ooo_segment_new (svm_fifo_t * f, u32 start, u32 length)
240{
241 ooo_segment_t *s;
242
243 pool_get (f->ooo_segments, s);
244
Dave Barach1f75cfd2017-04-14 16:46:44 -0400245 s->start = start;
Dave Barach68b0fb02017-02-28 15:15:56 -0500246 s->length = length;
247
248 s->prev = s->next = OOO_SEGMENT_INVALID_INDEX;
249
250 return s;
251}
252
253always_inline void
254ooo_segment_del (svm_fifo_t * f, u32 index)
255{
256 ooo_segment_t *cur, *prev = 0, *next = 0;
257 cur = pool_elt_at_index (f->ooo_segments, index);
258
259 if (cur->next != OOO_SEGMENT_INVALID_INDEX)
260 {
261 next = pool_elt_at_index (f->ooo_segments, cur->next);
262 next->prev = cur->prev;
263 }
264
265 if (cur->prev != OOO_SEGMENT_INVALID_INDEX)
266 {
267 prev = pool_elt_at_index (f->ooo_segments, cur->prev);
268 prev->next = cur->next;
269 }
270 else
271 {
272 f->ooos_list_head = cur->next;
273 }
274
275 pool_put (f->ooo_segments, cur);
276}
277
278/**
279 * Add segment to fifo's out-of-order segment list. Takes care of merging
280 * adjacent segments and removing overlapping ones.
281 */
282static void
283ooo_segment_add (svm_fifo_t * f, u32 offset, u32 length)
284{
285 ooo_segment_t *s, *new_s, *prev, *next, *it;
Florin Corasf03a59a2017-06-09 21:07:32 -0700286 u32 new_index, s_end_pos, s_index;
287 u32 normalized_position, normalized_end_position;
Dave Barach68b0fb02017-02-28 15:15:56 -0500288
Florin Coras3eb50622017-07-13 01:24:57 -0400289 ASSERT (offset + length <= ooo_segment_distance_from_tail (f, f->head));
Florin Corasf03a59a2017-06-09 21:07:32 -0700290 normalized_position = (f->tail + offset) % f->nitems;
291 normalized_end_position = (f->tail + offset + length) % f->nitems;
292
293 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
Dave Barach68b0fb02017-02-28 15:15:56 -0500294
295 if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX)
296 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700297 s = ooo_segment_new (f, normalized_position, length);
Dave Barach68b0fb02017-02-28 15:15:56 -0500298 f->ooos_list_head = s - f->ooo_segments;
299 f->ooos_newest = f->ooos_list_head;
300 return;
301 }
302
303 /* Find first segment that starts after new segment */
304 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
305 while (s->next != OOO_SEGMENT_INVALID_INDEX
Florin Corasf03a59a2017-06-09 21:07:32 -0700306 && position_lt (f, s->start, normalized_position))
Dave Barach68b0fb02017-02-28 15:15:56 -0500307 s = pool_elt_at_index (f->ooo_segments, s->next);
308
Florin Corasc28764f2017-04-26 00:08:42 -0700309 /* If we have a previous and we overlap it, use it as starting point */
310 prev = ooo_segment_get_prev (f, s);
Florin Corasf03a59a2017-06-09 21:07:32 -0700311 if (prev
312 && position_leq (f, normalized_position, ooo_segment_end_pos (f, prev)))
Florin Corasc28764f2017-04-26 00:08:42 -0700313 {
314 s = prev;
Florin Corasf03a59a2017-06-09 21:07:32 -0700315 s_end_pos = ooo_segment_end_pos (f, s);
Dave Barach2c25a622017-06-26 11:35:07 -0400316
Florin Coras3eb50622017-07-13 01:24:57 -0400317 /* Since we have previous, normalized start position cannot be smaller
318 * than prev->start. Check tail */
319 ASSERT (position_lt (f, s->start, normalized_position));
Dave Barach2c25a622017-06-26 11:35:07 -0400320 goto check_tail;
Florin Corasc28764f2017-04-26 00:08:42 -0700321 }
322
Dave Barach68b0fb02017-02-28 15:15:56 -0500323 s_index = s - f->ooo_segments;
Florin Corasf03a59a2017-06-09 21:07:32 -0700324 s_end_pos = ooo_segment_end_pos (f, s);
Dave Barach68b0fb02017-02-28 15:15:56 -0500325
326 /* No overlap, add before current segment */
Florin Corasf03a59a2017-06-09 21:07:32 -0700327 if (position_lt (f, normalized_end_position, s->start))
Dave Barach68b0fb02017-02-28 15:15:56 -0500328 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700329 new_s = ooo_segment_new (f, normalized_position, length);
Dave Barach68b0fb02017-02-28 15:15:56 -0500330 new_index = new_s - f->ooo_segments;
331
332 /* Pool might've moved, get segment again */
333 s = pool_elt_at_index (f->ooo_segments, s_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500334 if (s->prev != OOO_SEGMENT_INVALID_INDEX)
335 {
336 new_s->prev = s->prev;
Dave Barach68b0fb02017-02-28 15:15:56 -0500337 prev = pool_elt_at_index (f->ooo_segments, new_s->prev);
338 prev->next = new_index;
339 }
340 else
341 {
342 /* New head */
343 f->ooos_list_head = new_index;
344 }
345
Florin Corasf03a59a2017-06-09 21:07:32 -0700346 new_s->next = s_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500347 s->prev = new_index;
348 f->ooos_newest = new_index;
349 return;
350 }
351 /* No overlap, add after current segment */
Florin Corasf03a59a2017-06-09 21:07:32 -0700352 else if (position_gt (f, normalized_position, s_end_pos))
Dave Barach68b0fb02017-02-28 15:15:56 -0500353 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700354 new_s = ooo_segment_new (f, normalized_position, length);
Dave Barach68b0fb02017-02-28 15:15:56 -0500355 new_index = new_s - f->ooo_segments;
356
357 /* Pool might've moved, get segment again */
358 s = pool_elt_at_index (f->ooo_segments, s_index);
359
Florin Coras3eb50622017-07-13 01:24:57 -0400360 /* Needs to be last */
Florin Corasf03a59a2017-06-09 21:07:32 -0700361 ASSERT (s->next == OOO_SEGMENT_INVALID_INDEX);
Dave Barach68b0fb02017-02-28 15:15:56 -0500362
Florin Corasf03a59a2017-06-09 21:07:32 -0700363 new_s->prev = s_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500364 s->next = new_index;
365 f->ooos_newest = new_index;
366
367 return;
368 }
369
370 /*
371 * Merge needed
372 */
373
374 /* Merge at head */
Florin Corasf03a59a2017-06-09 21:07:32 -0700375 if (position_lt (f, normalized_position, s->start))
Dave Barach68b0fb02017-02-28 15:15:56 -0500376 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700377 s->start = normalized_position;
378 s->length = position_diff (f, s_end_pos, s->start);
Florin Coras3eb50622017-07-13 01:24:57 -0400379 f->ooos_newest = s - f->ooo_segments;
Dave Barach68b0fb02017-02-28 15:15:56 -0500380 }
381
Dave Barach2c25a622017-06-26 11:35:07 -0400382check_tail:
Florin Coras3eb50622017-07-13 01:24:57 -0400383
384 /* Overlapping tail */
Florin Corasf03a59a2017-06-09 21:07:32 -0700385 if (position_gt (f, normalized_end_position, s_end_pos))
Florin Corasc28764f2017-04-26 00:08:42 -0700386 {
Florin Coras3eb50622017-07-13 01:24:57 -0400387 s->length = position_diff (f, normalized_end_position, s->start);
388
389 /* Remove the completely overlapped segments in the tail */
390 it = ooo_segment_next (f, s);
Florin Corasf03a59a2017-06-09 21:07:32 -0700391 while (it && position_leq (f, ooo_segment_end_pos (f, it),
392 normalized_end_position))
Florin Corasc28764f2017-04-26 00:08:42 -0700393 {
Florin Coras3eb50622017-07-13 01:24:57 -0400394 next = ooo_segment_next (f, it);
Florin Corasc28764f2017-04-26 00:08:42 -0700395 ooo_segment_del (f, it - f->ooo_segments);
396 it = next;
397 }
398
399 /* If partial overlap with last, merge */
Florin Corasf03a59a2017-06-09 21:07:32 -0700400 if (it && position_leq (f, it->start, normalized_end_position))
Florin Corasc28764f2017-04-26 00:08:42 -0700401 {
Florin Coras3eb50622017-07-13 01:24:57 -0400402 s->length = position_diff (f, ooo_segment_end_pos (f, it),
403 s->start);
Florin Corasc28764f2017-04-26 00:08:42 -0700404 ooo_segment_del (f, it - f->ooo_segments);
405 }
Florin Coras3eb50622017-07-13 01:24:57 -0400406 f->ooos_newest = s - f->ooo_segments;
Florin Corasc28764f2017-04-26 00:08:42 -0700407 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500408}
409
410/**
411 * Removes segments that can now be enqueued because the fifo's tail has
412 * advanced. Returns the number of bytes added to tail.
413 */
414static int
415ooo_segment_try_collect (svm_fifo_t * f, u32 n_bytes_enqueued)
416{
417 ooo_segment_t *s;
Florin Corasf03a59a2017-06-09 21:07:32 -0700418 u32 index, bytes = 0;
419 i32 diff;
Dave Barach68b0fb02017-02-28 15:15:56 -0500420
421 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
Dave Barach2c25a622017-06-26 11:35:07 -0400422 diff = ooo_segment_distance_to_tail (f, s->start);
Dave Barach68b0fb02017-02-28 15:15:56 -0500423
Dave Barach2c25a622017-06-26 11:35:07 -0400424 ASSERT (diff != n_bytes_enqueued);
Florin Corasc28764f2017-04-26 00:08:42 -0700425
Florin Corasf03a59a2017-06-09 21:07:32 -0700426 if (diff > n_bytes_enqueued)
Florin Corasb59a7052017-04-18 22:07:29 -0700427 return 0;
428
Dave Barach68b0fb02017-02-28 15:15:56 -0500429 /* If last tail update overlaps one/multiple ooo segments, remove them */
Florin Corasf03a59a2017-06-09 21:07:32 -0700430 while (0 <= diff && diff < n_bytes_enqueued)
Dave Barach68b0fb02017-02-28 15:15:56 -0500431 {
Florin Corasb59a7052017-04-18 22:07:29 -0700432 index = s - f->ooo_segments;
433
434 /* Segment end is beyond the tail. Advance tail and remove segment */
Florin Corasf03a59a2017-06-09 21:07:32 -0700435 if (s->length > diff)
Dave Barach68b0fb02017-02-28 15:15:56 -0500436 {
Florin Corasb59a7052017-04-18 22:07:29 -0700437 bytes = s->length - diff;
Florin Corasf03a59a2017-06-09 21:07:32 -0700438 f->tail += bytes;
439 f->tail %= f->nitems;
Florin Corasb59a7052017-04-18 22:07:29 -0700440 ooo_segment_del (f, index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500441 break;
442 }
Florin Corasb59a7052017-04-18 22:07:29 -0700443
Dave Barach68b0fb02017-02-28 15:15:56 -0500444 /* If we have next go on */
Florin Corasb59a7052017-04-18 22:07:29 -0700445 if (s->next != OOO_SEGMENT_INVALID_INDEX)
Dave Barach68b0fb02017-02-28 15:15:56 -0500446 {
Dave Barach68b0fb02017-02-28 15:15:56 -0500447 s = pool_elt_at_index (f->ooo_segments, s->next);
Dave Barach2c25a622017-06-26 11:35:07 -0400448 diff = ooo_segment_distance_to_tail (f, s->start);
Dave Barach68b0fb02017-02-28 15:15:56 -0500449 ooo_segment_del (f, index);
450 }
451 /* End of search */
452 else
453 {
Florin Corasb59a7052017-04-18 22:07:29 -0700454 ooo_segment_del (f, index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500455 break;
456 }
457 }
458
Chris Lukeab7b8d92017-09-07 07:40:13 -0400459 ASSERT (bytes <= f->nitems);
Dave Barach68b0fb02017-02-28 15:15:56 -0500460 return bytes;
461}
462
Florin Coras983cc7d2018-09-18 23:11:55 -0700463CLIB_MARCH_FN (svm_fifo_enqueue_nowait, int, svm_fifo_t * f, u32 max_bytes,
464 const u8 * copy_from_here)
Dave Barach68b0fb02017-02-28 15:15:56 -0500465{
466 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
467 u32 cursize, nitems;
468
Florin Coras3e350af2017-03-30 02:54:28 -0700469 /* read cursize, which can only increase while we're working */
470 cursize = svm_fifo_max_dequeue (f);
Florin Corasf03a59a2017-06-09 21:07:32 -0700471 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
Florin Coras3e350af2017-03-30 02:54:28 -0700472
473 if (PREDICT_FALSE (cursize == f->nitems))
Florin Coras7fb0fe12018-04-09 09:24:52 -0700474 return SVM_FIFO_FULL;
Dave Barach68b0fb02017-02-28 15:15:56 -0500475
Dave Barach68b0fb02017-02-28 15:15:56 -0500476 nitems = f->nitems;
477
478 /* Number of bytes we're going to copy */
479 total_copy_bytes = (nitems - cursize) < max_bytes ?
480 (nitems - cursize) : max_bytes;
481
482 if (PREDICT_TRUE (copy_from_here != 0))
483 {
484 /* Number of bytes in first copy segment */
485 first_copy_bytes = ((nitems - f->tail) < total_copy_bytes)
486 ? (nitems - f->tail) : total_copy_bytes;
487
Dave Barachb7b92992018-10-17 10:38:51 -0400488 _clib_memcpy (&f->data[f->tail], copy_from_here, first_copy_bytes);
Dave Barach68b0fb02017-02-28 15:15:56 -0500489 f->tail += first_copy_bytes;
490 f->tail = (f->tail == nitems) ? 0 : f->tail;
491
492 /* Number of bytes in second copy segment, if any */
493 second_copy_bytes = total_copy_bytes - first_copy_bytes;
494 if (second_copy_bytes)
495 {
Dave Barachb7b92992018-10-17 10:38:51 -0400496 _clib_memcpy (&f->data[f->tail], copy_from_here + first_copy_bytes,
497 second_copy_bytes);
Dave Barach68b0fb02017-02-28 15:15:56 -0500498 f->tail += second_copy_bytes;
499 f->tail = (f->tail == nitems) ? 0 : f->tail;
500 }
501 }
502 else
503 {
Dave Barach2c25a622017-06-26 11:35:07 -0400504 ASSERT (0);
505
Dave Barach68b0fb02017-02-28 15:15:56 -0500506 /* Account for a zero-copy enqueue done elsewhere */
507 ASSERT (max_bytes <= (nitems - cursize));
508 f->tail += max_bytes;
509 f->tail = f->tail % nitems;
510 total_copy_bytes = max_bytes;
511 }
512
Florin Coras3eb50622017-07-13 01:24:57 -0400513 svm_fifo_trace_add (f, f->head, total_copy_bytes, 2);
514
Dave Barach68b0fb02017-02-28 15:15:56 -0500515 /* Any out-of-order segments to collect? */
516 if (PREDICT_FALSE (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX))
517 total_copy_bytes += ooo_segment_try_collect (f, total_copy_bytes);
518
519 /* Atomically increase the queue length */
Dave Barach2c25a622017-06-26 11:35:07 -0400520 ASSERT (cursize + total_copy_bytes <= nitems);
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000521 clib_atomic_fetch_add (&f->cursize, total_copy_bytes);
Dave Barach68b0fb02017-02-28 15:15:56 -0500522
523 return (total_copy_bytes);
524}
525
Florin Coras3aa7af32018-06-29 08:44:31 -0700526#ifndef CLIB_MARCH_VARIANT
Dave Barach68b0fb02017-02-28 15:15:56 -0500527int
Florin Coras371ca502018-02-21 12:07:41 -0800528svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 max_bytes,
529 const u8 * copy_from_here)
Dave Barach68b0fb02017-02-28 15:15:56 -0500530{
Florin Coras983cc7d2018-09-18 23:11:55 -0700531 return CLIB_MARCH_FN_SELECT (svm_fifo_enqueue_nowait) (f, max_bytes,
532 copy_from_here);
Dave Barach68b0fb02017-02-28 15:15:56 -0500533}
Florin Coras3aa7af32018-06-29 08:44:31 -0700534#endif
Dave Barach68b0fb02017-02-28 15:15:56 -0500535
Florin Coras6792ec02017-03-13 03:49:51 -0700536/**
537 * Enqueue a future segment.
538 *
Dave Barach68b0fb02017-02-28 15:15:56 -0500539 * Two choices: either copies the entire segment, or copies nothing
540 * Returns 0 of the entire segment was copied
541 * Returns -1 if none of the segment was copied due to lack of space
542 */
Florin Coras983cc7d2018-09-18 23:11:55 -0700543CLIB_MARCH_FN (svm_fifo_enqueue_with_offset, int, svm_fifo_t * f,
544 u32 offset, u32 required_bytes, u8 * copy_from_here)
Dave Barach68b0fb02017-02-28 15:15:56 -0500545{
546 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
Florin Corasf03a59a2017-06-09 21:07:32 -0700547 u32 cursize, nitems, normalized_offset;
Florin Corasf03a59a2017-06-09 21:07:32 -0700548
549 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
Dave Barach68b0fb02017-02-28 15:15:56 -0500550
Florin Coras3e350af2017-03-30 02:54:28 -0700551 /* read cursize, which can only increase while we're working */
552 cursize = svm_fifo_max_dequeue (f);
Dave Barach68b0fb02017-02-28 15:15:56 -0500553 nitems = f->nitems;
Florin Corasf03a59a2017-06-09 21:07:32 -0700554
Dave Barach2c25a622017-06-26 11:35:07 -0400555 ASSERT (required_bytes < nitems);
556
Florin Corasf03a59a2017-06-09 21:07:32 -0700557 normalized_offset = (f->tail + offset) % nitems;
Dave Barach68b0fb02017-02-28 15:15:56 -0500558
559 /* Will this request fit? */
Florin Coras1f152cd2017-08-18 19:28:03 -0700560 if ((required_bytes + offset) > (nitems - cursize))
Dave Barach68b0fb02017-02-28 15:15:56 -0500561 return -1;
562
Florin Coras3eb50622017-07-13 01:24:57 -0400563 svm_fifo_trace_add (f, offset, required_bytes, 1);
564
Dave Barach68b0fb02017-02-28 15:15:56 -0500565 ooo_segment_add (f, offset, required_bytes);
566
567 /* Number of bytes we're going to copy */
568 total_copy_bytes = required_bytes;
Dave Barach68b0fb02017-02-28 15:15:56 -0500569
570 /* Number of bytes in first copy segment */
Dave Barach1f75cfd2017-04-14 16:46:44 -0400571 first_copy_bytes = ((nitems - normalized_offset) < total_copy_bytes)
572 ? (nitems - normalized_offset) : total_copy_bytes;
Dave Barach68b0fb02017-02-28 15:15:56 -0500573
Dave Barachb7b92992018-10-17 10:38:51 -0400574 _clib_memcpy (&f->data[normalized_offset], copy_from_here,
575 first_copy_bytes);
Dave Barach68b0fb02017-02-28 15:15:56 -0500576
577 /* Number of bytes in second copy segment, if any */
578 second_copy_bytes = total_copy_bytes - first_copy_bytes;
579 if (second_copy_bytes)
580 {
Dave Barach1f75cfd2017-04-14 16:46:44 -0400581 normalized_offset += first_copy_bytes;
582 normalized_offset %= nitems;
Dave Barach68b0fb02017-02-28 15:15:56 -0500583
Dave Barach1f75cfd2017-04-14 16:46:44 -0400584 ASSERT (normalized_offset == 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500585
Dave Barachb7b92992018-10-17 10:38:51 -0400586 _clib_memcpy (&f->data[normalized_offset],
587 copy_from_here + first_copy_bytes, second_copy_bytes);
Dave Barach68b0fb02017-02-28 15:15:56 -0500588 }
589
590 return (0);
591}
592
Florin Coras3aa7af32018-06-29 08:44:31 -0700593#ifndef CLIB_MARCH_VARIANT
Dave Barach68b0fb02017-02-28 15:15:56 -0500594
595int
Florin Coras3aa7af32018-06-29 08:44:31 -0700596svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 required_bytes,
597 u8 * copy_from_here)
Dave Barach68b0fb02017-02-28 15:15:56 -0500598{
Florin Coras983cc7d2018-09-18 23:11:55 -0700599 return CLIB_MARCH_FN_SELECT (svm_fifo_enqueue_with_offset) (f, offset,
600 required_bytes,
601 copy_from_here);
Dave Barach68b0fb02017-02-28 15:15:56 -0500602}
603
Florin Coras7fb0fe12018-04-09 09:24:52 -0700604void
605svm_fifo_overwrite_head (svm_fifo_t * f, u8 * data, u32 len)
606{
607 u32 first_chunk;
Florin Coras8e43d042018-05-04 15:46:57 -0700608 first_chunk = f->nitems - f->head;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700609 ASSERT (len <= f->nitems);
Florin Coras8e43d042018-05-04 15:46:57 -0700610 if (len <= first_chunk)
Dave Barachb7b92992018-10-17 10:38:51 -0400611 _clib_memcpy (&f->data[f->head], data, len);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700612 else
613 {
Dave Barachb7b92992018-10-17 10:38:51 -0400614 _clib_memcpy (&f->data[f->head], data, first_chunk);
615 _clib_memcpy (&f->data[0], data + first_chunk, len - first_chunk);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700616 }
617}
Florin Coras3aa7af32018-06-29 08:44:31 -0700618#endif
Dave Barach68b0fb02017-02-28 15:15:56 -0500619
Florin Coras983cc7d2018-09-18 23:11:55 -0700620CLIB_MARCH_FN (svm_fifo_dequeue_nowait, int, svm_fifo_t * f, u32 max_bytes,
621 u8 * copy_here)
Dave Barach68b0fb02017-02-28 15:15:56 -0500622{
623 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
624 u32 cursize, nitems;
625
Florin Coras3e350af2017-03-30 02:54:28 -0700626 /* read cursize, which can only increase while we're working */
627 cursize = svm_fifo_max_dequeue (f);
628 if (PREDICT_FALSE (cursize == 0))
Dave Barach68b0fb02017-02-28 15:15:56 -0500629 return -2; /* nothing in the fifo */
630
Dave Barach68b0fb02017-02-28 15:15:56 -0500631 nitems = f->nitems;
632
633 /* Number of bytes we're going to copy */
634 total_copy_bytes = (cursize < max_bytes) ? cursize : max_bytes;
635
636 if (PREDICT_TRUE (copy_here != 0))
637 {
638 /* Number of bytes in first copy segment */
639 first_copy_bytes = ((nitems - f->head) < total_copy_bytes)
640 ? (nitems - f->head) : total_copy_bytes;
Dave Barachb7b92992018-10-17 10:38:51 -0400641 _clib_memcpy (copy_here, &f->data[f->head], first_copy_bytes);
Dave Barach68b0fb02017-02-28 15:15:56 -0500642 f->head += first_copy_bytes;
643 f->head = (f->head == nitems) ? 0 : f->head;
644
645 /* Number of bytes in second copy segment, if any */
646 second_copy_bytes = total_copy_bytes - first_copy_bytes;
647 if (second_copy_bytes)
648 {
Dave Barachb7b92992018-10-17 10:38:51 -0400649 _clib_memcpy (copy_here + first_copy_bytes,
650 &f->data[f->head], second_copy_bytes);
Dave Barach68b0fb02017-02-28 15:15:56 -0500651 f->head += second_copy_bytes;
652 f->head = (f->head == nitems) ? 0 : f->head;
653 }
654 }
655 else
656 {
Dave Barach2c25a622017-06-26 11:35:07 -0400657 ASSERT (0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500658 /* Account for a zero-copy dequeue done elsewhere */
659 ASSERT (max_bytes <= cursize);
660 f->head += max_bytes;
661 f->head = f->head % nitems;
662 cursize -= max_bytes;
663 total_copy_bytes = max_bytes;
664 }
665
Dave Barach2c25a622017-06-26 11:35:07 -0400666 ASSERT (f->head <= nitems);
667 ASSERT (cursize >= total_copy_bytes);
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000668 clib_atomic_fetch_sub (&f->cursize, total_copy_bytes);
Dave Barach68b0fb02017-02-28 15:15:56 -0500669
670 return (total_copy_bytes);
671}
672
Florin Coras3aa7af32018-06-29 08:44:31 -0700673#ifndef CLIB_MARCH_VARIANT
Florin Corasf6359c82017-06-19 12:26:09 -0400674
Dave Barach68b0fb02017-02-28 15:15:56 -0500675int
Florin Corasf6359c82017-06-19 12:26:09 -0400676svm_fifo_dequeue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_here)
677{
Florin Coras983cc7d2018-09-18 23:11:55 -0700678 return CLIB_MARCH_FN_SELECT (svm_fifo_dequeue_nowait) (f, max_bytes,
679 copy_here);
Florin Corasf6359c82017-06-19 12:26:09 -0400680}
Florin Coras3aa7af32018-06-29 08:44:31 -0700681#endif
Florin Corasf6359c82017-06-19 12:26:09 -0400682
Florin Coras983cc7d2018-09-18 23:11:55 -0700683CLIB_MARCH_FN (svm_fifo_peek, int, svm_fifo_t * f, u32 relative_offset,
684 u32 max_bytes, u8 * copy_here)
Dave Barach68b0fb02017-02-28 15:15:56 -0500685{
686 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
Florin Coras6792ec02017-03-13 03:49:51 -0700687 u32 cursize, nitems, real_head;
Dave Barach68b0fb02017-02-28 15:15:56 -0500688
Florin Coras3e350af2017-03-30 02:54:28 -0700689 /* read cursize, which can only increase while we're working */
690 cursize = svm_fifo_max_dequeue (f);
Florin Coras93992a92017-05-24 18:03:56 -0700691 if (PREDICT_FALSE (cursize < relative_offset))
Dave Barach68b0fb02017-02-28 15:15:56 -0500692 return -2; /* nothing in the fifo */
693
Dave Barach68b0fb02017-02-28 15:15:56 -0500694 nitems = f->nitems;
Florin Corasb59a7052017-04-18 22:07:29 -0700695 real_head = f->head + relative_offset;
Florin Coras6792ec02017-03-13 03:49:51 -0700696 real_head = real_head >= nitems ? real_head - nitems : real_head;
Dave Barach68b0fb02017-02-28 15:15:56 -0500697
698 /* Number of bytes we're going to copy */
Florin Coras93992a92017-05-24 18:03:56 -0700699 total_copy_bytes = (cursize - relative_offset < max_bytes) ?
700 cursize - relative_offset : max_bytes;
Dave Barach68b0fb02017-02-28 15:15:56 -0500701
702 if (PREDICT_TRUE (copy_here != 0))
703 {
704 /* Number of bytes in first copy segment */
705 first_copy_bytes =
Florin Coras6792ec02017-03-13 03:49:51 -0700706 ((nitems - real_head) < total_copy_bytes) ?
707 (nitems - real_head) : total_copy_bytes;
Dave Barachb7b92992018-10-17 10:38:51 -0400708 _clib_memcpy (copy_here, &f->data[real_head], first_copy_bytes);
Dave Barach68b0fb02017-02-28 15:15:56 -0500709
710 /* Number of bytes in second copy segment, if any */
711 second_copy_bytes = total_copy_bytes - first_copy_bytes;
712 if (second_copy_bytes)
713 {
Dave Barachb7b92992018-10-17 10:38:51 -0400714 _clib_memcpy (copy_here + first_copy_bytes, &f->data[0],
715 second_copy_bytes);
Dave Barach68b0fb02017-02-28 15:15:56 -0500716 }
717 }
718 return total_copy_bytes;
719}
720
Florin Coras3aa7af32018-06-29 08:44:31 -0700721#ifndef CLIB_MARCH_VARIANT
Florin Corasf6359c82017-06-19 12:26:09 -0400722
723int
724svm_fifo_peek (svm_fifo_t * f, u32 relative_offset, u32 max_bytes,
725 u8 * copy_here)
726{
Florin Coras983cc7d2018-09-18 23:11:55 -0700727 return CLIB_MARCH_FN_SELECT (svm_fifo_peek) (f, relative_offset, max_bytes,
728 copy_here);
Florin Corasf6359c82017-06-19 12:26:09 -0400729}
730
Dave Barach68b0fb02017-02-28 15:15:56 -0500731int
Florin Corasa5464812017-04-19 13:00:05 -0700732svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes)
Dave Barach68b0fb02017-02-28 15:15:56 -0500733{
734 u32 total_drop_bytes, first_drop_bytes, second_drop_bytes;
735 u32 cursize, nitems;
736
Florin Coras3e350af2017-03-30 02:54:28 -0700737 /* read cursize, which can only increase while we're working */
738 cursize = svm_fifo_max_dequeue (f);
739 if (PREDICT_FALSE (cursize == 0))
Dave Barach68b0fb02017-02-28 15:15:56 -0500740 return -2; /* nothing in the fifo */
741
Dave Barach68b0fb02017-02-28 15:15:56 -0500742 nitems = f->nitems;
743
744 /* Number of bytes we're going to drop */
745 total_drop_bytes = (cursize < max_bytes) ? cursize : max_bytes;
746
Florin Coras3eb50622017-07-13 01:24:57 -0400747 svm_fifo_trace_add (f, f->tail, total_drop_bytes, 3);
748
Dave Barach68b0fb02017-02-28 15:15:56 -0500749 /* Number of bytes in first copy segment */
750 first_drop_bytes =
751 ((nitems - f->head) < total_drop_bytes) ?
752 (nitems - f->head) : total_drop_bytes;
753 f->head += first_drop_bytes;
754 f->head = (f->head == nitems) ? 0 : f->head;
755
756 /* Number of bytes in second drop segment, if any */
757 second_drop_bytes = total_drop_bytes - first_drop_bytes;
758 if (second_drop_bytes)
759 {
760 f->head += second_drop_bytes;
761 f->head = (f->head == nitems) ? 0 : f->head;
762 }
763
Dave Barach2c25a622017-06-26 11:35:07 -0400764 ASSERT (f->head <= nitems);
765 ASSERT (cursize >= total_drop_bytes);
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000766 clib_atomic_fetch_sub (&f->cursize, total_drop_bytes);
Dave Barach68b0fb02017-02-28 15:15:56 -0500767
768 return total_drop_bytes;
769}
770
Florin Coras25579b42018-06-06 17:55:02 -0700771void
772svm_fifo_dequeue_drop_all (svm_fifo_t * f)
773{
774 f->head = f->tail;
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000775 clib_atomic_fetch_sub (&f->cursize, f->cursize);
Florin Coras25579b42018-06-06 17:55:02 -0700776}
777
Florin Coras2cba8532018-09-11 16:33:36 -0700778int
779svm_fifo_segments (svm_fifo_t * f, svm_fifo_segment_t * fs)
780{
781 u32 cursize, nitems;
782
783 /* read cursize, which can only increase while we're working */
784 cursize = svm_fifo_max_dequeue (f);
785 if (PREDICT_FALSE (cursize == 0))
786 return -2;
787
788 nitems = f->nitems;
789
790 fs[0].len = ((nitems - f->head) < cursize) ? (nitems - f->head) : cursize;
791 fs[0].data = f->data + f->head;
792
793 if (fs[0].len < cursize)
794 {
795 fs[1].len = cursize - fs[0].len;
796 fs[1].data = f->data;
797 }
798 else
799 {
800 fs[1].len = 0;
801 fs[1].data = 0;
802 }
803 return cursize;
804}
805
806void
807svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_segment_t * fs)
808{
809 u32 total_drop_bytes;
810
811 ASSERT (fs[0].data == f->data + f->head);
812 if (fs[1].len)
813 {
814 f->head = fs[1].len;
815 total_drop_bytes = fs[0].len + fs[1].len;
816 }
817 else
818 {
819 f->head = (f->head + fs[0].len) % f->nitems;
820 total_drop_bytes = fs[0].len;
821 }
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000822 clib_atomic_fetch_sub (&f->cursize, total_drop_bytes);
Florin Coras2cba8532018-09-11 16:33:36 -0700823}
824
Dave Barach1f75cfd2017-04-14 16:46:44 -0400825u32
826svm_fifo_number_ooo_segments (svm_fifo_t * f)
827{
828 return pool_elts (f->ooo_segments);
829}
830
831ooo_segment_t *
832svm_fifo_first_ooo_segment (svm_fifo_t * f)
833{
834 return pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
835}
836
Florin Corasc28764f2017-04-26 00:08:42 -0700837/**
838 * Set fifo pointers to requested offset
839 */
840void
841svm_fifo_init_pointers (svm_fifo_t * f, u32 pointer)
842{
843 f->head = f->tail = pointer % f->nitems;
844}
845
Florin Coras3aa7af32018-06-29 08:44:31 -0700846#endif
Dave Barach68b0fb02017-02-28 15:15:56 -0500847/*
848 * fd.io coding-style-patch-verification: ON
849 *
850 * Local Variables:
851 * eval: (c-set-style "gnu")
852 * End:
853 */