blob: 6ca437cfd7b58b6d5fe4f6f69a1baceb1d17d25f [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>
Dave Barach68b0fb02017-02-28 15:15:56 -050017
Florin Corasf03a59a2017-06-09 21:07:32 -070018static inline u8
19position_lt (svm_fifo_t * f, u32 a, u32 b)
20{
21 return (ooo_segment_distance_to_tail (f, a)
22 < ooo_segment_distance_to_tail (f, b));
23}
24
25static inline u8
26position_leq (svm_fifo_t * f, u32 a, u32 b)
27{
28 return (ooo_segment_distance_to_tail (f, a)
29 <= ooo_segment_distance_to_tail (f, b));
30}
31
32static inline u8
33position_gt (svm_fifo_t * f, u32 a, u32 b)
34{
35 return (ooo_segment_distance_to_tail (f, a)
36 > ooo_segment_distance_to_tail (f, b));
37}
38
39static inline u32
40position_diff (svm_fifo_t * f, u32 posa, u32 posb)
41{
42 return ooo_segment_distance_to_tail (f, posa)
43 - ooo_segment_distance_to_tail (f, posb);
44}
45
46static inline u32
47ooo_segment_end_pos (svm_fifo_t * f, ooo_segment_t * s)
48{
49 return (s->start + s->length) % f->nitems;
50}
Dave Barach1f75cfd2017-04-14 16:46:44 -040051
52u8 *
53format_ooo_segment (u8 * s, va_list * args)
54{
55 ooo_segment_t *seg = va_arg (*args, ooo_segment_t *);
56
57 s = format (s, "pos %u, len %u, next %d, prev %d",
58 seg->start, seg->length, seg->next, seg->prev);
59 return s;
60}
61
62u8 *
63format_ooo_list (u8 * s, va_list * args)
64{
65 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
66 u32 ooo_segment_index = f->ooos_list_head;
67 ooo_segment_t *seg;
68
69 while (ooo_segment_index != OOO_SEGMENT_INVALID_INDEX)
70 {
71 seg = pool_elt_at_index (f->ooo_segments, ooo_segment_index);
Florin Corasb59a7052017-04-18 22:07:29 -070072 s = format (s, " %U\n", format_ooo_segment, seg);
Dave Barach1f75cfd2017-04-14 16:46:44 -040073 ooo_segment_index = seg->next;
74 }
75 return s;
76}
77
Florin Corasb59a7052017-04-18 22:07:29 -070078u8 *
79format_svm_fifo (u8 * s, va_list * args)
80{
81 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
82 int verbose = va_arg (*args, int);
83
84 s = format (s, "cursize %u nitems %u has_event %d\n",
85 f->cursize, f->nitems, f->has_event);
Florin Corasbb292f42017-05-19 09:49:19 -070086 s = format (s, " head %d tail %d\n", f->head, f->tail);
Florin Corasb59a7052017-04-18 22:07:29 -070087
88 if (verbose > 1)
89 s = format
Florin Corasbb292f42017-05-19 09:49:19 -070090 (s, " server session %d thread %d client session %d thread %d\n",
Florin Corasa5464812017-04-19 13:00:05 -070091 f->master_session_index, f->master_thread_index,
Florin Corasb59a7052017-04-18 22:07:29 -070092 f->client_session_index, f->client_thread_index);
93
94 if (verbose)
95 {
Florin Corasbb292f42017-05-19 09:49:19 -070096 s = format (s, " ooo pool %d active elts\n",
Florin Corasb59a7052017-04-18 22:07:29 -070097 pool_elts (f->ooo_segments));
Florin Corasbb292f42017-05-19 09:49:19 -070098 if (svm_fifo_has_ooo_data (f))
99 s = format (s, " %U", format_ooo_list, f);
Florin Corasb59a7052017-04-18 22:07:29 -0700100 }
101 return s;
102}
103
Dave Barach68b0fb02017-02-28 15:15:56 -0500104/** create an svm fifo, in the current heap. Fails vs blow up the process */
105svm_fifo_t *
106svm_fifo_create (u32 data_size_in_bytes)
107{
108 svm_fifo_t *f;
Dave Barach68b0fb02017-02-28 15:15:56 -0500109
110 f = clib_mem_alloc_aligned_or_null (sizeof (*f) + data_size_in_bytes,
111 CLIB_CACHE_LINE_BYTES);
112 if (f == 0)
113 return 0;
114
115 memset (f, 0, sizeof (*f) + data_size_in_bytes);
116 f->nitems = data_size_in_bytes;
117 f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
118
Dave Barach68b0fb02017-02-28 15:15:56 -0500119 return (f);
120}
121
Florin Coras6cf30ad2017-04-04 23:08:23 -0700122void
123svm_fifo_free (svm_fifo_t * f)
124{
125 pool_free (f->ooo_segments);
126 clib_mem_free (f);
127}
128
Dave Barach68b0fb02017-02-28 15:15:56 -0500129always_inline ooo_segment_t *
130ooo_segment_new (svm_fifo_t * f, u32 start, u32 length)
131{
132 ooo_segment_t *s;
133
134 pool_get (f->ooo_segments, s);
135
Dave Barach1f75cfd2017-04-14 16:46:44 -0400136 s->start = start;
Dave Barach68b0fb02017-02-28 15:15:56 -0500137 s->length = length;
138
139 s->prev = s->next = OOO_SEGMENT_INVALID_INDEX;
140
141 return s;
142}
143
144always_inline void
145ooo_segment_del (svm_fifo_t * f, u32 index)
146{
147 ooo_segment_t *cur, *prev = 0, *next = 0;
148 cur = pool_elt_at_index (f->ooo_segments, index);
149
150 if (cur->next != OOO_SEGMENT_INVALID_INDEX)
151 {
152 next = pool_elt_at_index (f->ooo_segments, cur->next);
153 next->prev = cur->prev;
154 }
155
156 if (cur->prev != OOO_SEGMENT_INVALID_INDEX)
157 {
158 prev = pool_elt_at_index (f->ooo_segments, cur->prev);
159 prev->next = cur->next;
160 }
161 else
162 {
163 f->ooos_list_head = cur->next;
164 }
165
166 pool_put (f->ooo_segments, cur);
167}
168
169/**
170 * Add segment to fifo's out-of-order segment list. Takes care of merging
171 * adjacent segments and removing overlapping ones.
172 */
173static void
174ooo_segment_add (svm_fifo_t * f, u32 offset, u32 length)
175{
176 ooo_segment_t *s, *new_s, *prev, *next, *it;
Florin Corasf03a59a2017-06-09 21:07:32 -0700177 u32 new_index, s_end_pos, s_index;
178 u32 normalized_position, normalized_end_position;
Dave Barach68b0fb02017-02-28 15:15:56 -0500179
Florin Corasf03a59a2017-06-09 21:07:32 -0700180 normalized_position = (f->tail + offset) % f->nitems;
181 normalized_end_position = (f->tail + offset + length) % f->nitems;
182
183 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
Dave Barach68b0fb02017-02-28 15:15:56 -0500184
185 if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX)
186 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700187 s = ooo_segment_new (f, normalized_position, length);
Dave Barach68b0fb02017-02-28 15:15:56 -0500188 f->ooos_list_head = s - f->ooo_segments;
189 f->ooos_newest = f->ooos_list_head;
190 return;
191 }
192
193 /* Find first segment that starts after new segment */
194 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
195 while (s->next != OOO_SEGMENT_INVALID_INDEX
Florin Corasf03a59a2017-06-09 21:07:32 -0700196 && position_lt (f, s->start, normalized_position))
Dave Barach68b0fb02017-02-28 15:15:56 -0500197 s = pool_elt_at_index (f->ooo_segments, s->next);
198
Florin Corasc28764f2017-04-26 00:08:42 -0700199 /* If we have a previous and we overlap it, use it as starting point */
200 prev = ooo_segment_get_prev (f, s);
Florin Corasf03a59a2017-06-09 21:07:32 -0700201 if (prev
202 && position_leq (f, normalized_position, ooo_segment_end_pos (f, prev)))
Florin Corasc28764f2017-04-26 00:08:42 -0700203 {
204 s = prev;
Florin Corasf03a59a2017-06-09 21:07:32 -0700205 s_end_pos = ooo_segment_end_pos (f, s);
Florin Corasc28764f2017-04-26 00:08:42 -0700206 goto merge;
207 }
208
Dave Barach68b0fb02017-02-28 15:15:56 -0500209 s_index = s - f->ooo_segments;
Florin Corasf03a59a2017-06-09 21:07:32 -0700210 s_end_pos = ooo_segment_end_pos (f, s);
Dave Barach68b0fb02017-02-28 15:15:56 -0500211
212 /* No overlap, add before current segment */
Florin Corasf03a59a2017-06-09 21:07:32 -0700213 if (position_lt (f, normalized_end_position, s->start))
Dave Barach68b0fb02017-02-28 15:15:56 -0500214 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700215 new_s = ooo_segment_new (f, normalized_position, length);
Dave Barach68b0fb02017-02-28 15:15:56 -0500216 new_index = new_s - f->ooo_segments;
217
218 /* Pool might've moved, get segment again */
219 s = pool_elt_at_index (f->ooo_segments, s_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500220 if (s->prev != OOO_SEGMENT_INVALID_INDEX)
221 {
222 new_s->prev = s->prev;
Dave Barach68b0fb02017-02-28 15:15:56 -0500223 prev = pool_elt_at_index (f->ooo_segments, new_s->prev);
224 prev->next = new_index;
225 }
226 else
227 {
228 /* New head */
229 f->ooos_list_head = new_index;
230 }
231
Florin Corasf03a59a2017-06-09 21:07:32 -0700232 new_s->next = s_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500233 s->prev = new_index;
234 f->ooos_newest = new_index;
235 return;
236 }
237 /* No overlap, add after current segment */
Florin Corasf03a59a2017-06-09 21:07:32 -0700238 else if (position_gt (f, normalized_position, s_end_pos))
Dave Barach68b0fb02017-02-28 15:15:56 -0500239 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700240 new_s = ooo_segment_new (f, normalized_position, length);
Dave Barach68b0fb02017-02-28 15:15:56 -0500241 new_index = new_s - f->ooo_segments;
242
243 /* Pool might've moved, get segment again */
244 s = pool_elt_at_index (f->ooo_segments, s_index);
245
Florin Corasf03a59a2017-06-09 21:07:32 -0700246 ASSERT (s->next == OOO_SEGMENT_INVALID_INDEX);
Dave Barach68b0fb02017-02-28 15:15:56 -0500247
Florin Corasf03a59a2017-06-09 21:07:32 -0700248 new_s->prev = s_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500249 s->next = new_index;
250 f->ooos_newest = new_index;
251
252 return;
253 }
254
255 /*
256 * Merge needed
257 */
258
Florin Corasc28764f2017-04-26 00:08:42 -0700259merge:
260
Dave Barach68b0fb02017-02-28 15:15:56 -0500261 /* Merge at head */
Florin Corasf03a59a2017-06-09 21:07:32 -0700262 if (position_lt (f, normalized_position, s->start))
Dave Barach68b0fb02017-02-28 15:15:56 -0500263 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700264 s->start = normalized_position;
265 s->length = position_diff (f, s_end_pos, s->start);
Dave Barach68b0fb02017-02-28 15:15:56 -0500266 }
Florin Corasf03a59a2017-06-09 21:07:32 -0700267 /* Overlapping tail */
268 else if (position_gt (f, normalized_end_position, s_end_pos))
Dave Barach68b0fb02017-02-28 15:15:56 -0500269 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700270 s->length = position_diff (f, normalized_end_position, s->start);
Dave Barach68b0fb02017-02-28 15:15:56 -0500271 }
272 /* New segment completely covered by current one */
273 else
274 {
275 /* Do Nothing */
Florin Corasf03a59a2017-06-09 21:07:32 -0700276 s = 0;
Florin Corasc28764f2017-04-26 00:08:42 -0700277 goto done;
Dave Barach68b0fb02017-02-28 15:15:56 -0500278 }
279
Florin Corasc28764f2017-04-26 00:08:42 -0700280 /* The new segment's tail may cover multiple smaller ones */
Florin Corasf03a59a2017-06-09 21:07:32 -0700281 if (position_gt (f, normalized_end_position, s_end_pos))
Florin Corasc28764f2017-04-26 00:08:42 -0700282 {
283 /* Remove the completely overlapped segments */
284 it = (s->next != OOO_SEGMENT_INVALID_INDEX) ?
285 pool_elt_at_index (f->ooo_segments, s->next) : 0;
Florin Corasf03a59a2017-06-09 21:07:32 -0700286 while (it && position_leq (f, ooo_segment_end_pos (f, it),
287 normalized_end_position))
Florin Corasc28764f2017-04-26 00:08:42 -0700288 {
289 next = (it->next != OOO_SEGMENT_INVALID_INDEX) ?
290 pool_elt_at_index (f->ooo_segments, it->next) : 0;
291 ooo_segment_del (f, it - f->ooo_segments);
292 it = next;
293 }
294
295 /* If partial overlap with last, merge */
Florin Corasf03a59a2017-06-09 21:07:32 -0700296 if (it && position_leq (f, it->start, normalized_end_position))
Florin Corasc28764f2017-04-26 00:08:42 -0700297 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700298 s->length = ooo_segment_end_pos (f, it) - s->start;
Florin Corasc28764f2017-04-26 00:08:42 -0700299 ooo_segment_del (f, it - f->ooo_segments);
300 }
301 }
302
303done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500304 /* Most recently updated segment */
Florin Corasf03a59a2017-06-09 21:07:32 -0700305 if (s)
306 f->ooos_newest = s - f->ooo_segments;
Dave Barach68b0fb02017-02-28 15:15:56 -0500307}
308
309/**
310 * Removes segments that can now be enqueued because the fifo's tail has
311 * advanced. Returns the number of bytes added to tail.
312 */
313static int
314ooo_segment_try_collect (svm_fifo_t * f, u32 n_bytes_enqueued)
315{
316 ooo_segment_t *s;
Florin Corasf03a59a2017-06-09 21:07:32 -0700317 u32 index, bytes = 0;
318 i32 diff;
Dave Barach68b0fb02017-02-28 15:15:56 -0500319
320 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
321
Florin Corasf03a59a2017-06-09 21:07:32 -0700322 diff = (f->tail >= s->start) ?
323 f->tail - s->start : f->nitems + f->tail - s->start;
Florin Corasc28764f2017-04-26 00:08:42 -0700324
Florin Corasf03a59a2017-06-09 21:07:32 -0700325 if (diff > n_bytes_enqueued)
Florin Corasb59a7052017-04-18 22:07:29 -0700326 return 0;
327
Dave Barach68b0fb02017-02-28 15:15:56 -0500328 /* If last tail update overlaps one/multiple ooo segments, remove them */
Florin Corasf03a59a2017-06-09 21:07:32 -0700329 while (0 <= diff && diff < n_bytes_enqueued)
Dave Barach68b0fb02017-02-28 15:15:56 -0500330 {
Florin Corasb59a7052017-04-18 22:07:29 -0700331 index = s - f->ooo_segments;
332
333 /* Segment end is beyond the tail. Advance tail and remove segment */
Florin Corasf03a59a2017-06-09 21:07:32 -0700334 if (s->length > diff)
Dave Barach68b0fb02017-02-28 15:15:56 -0500335 {
Florin Corasb59a7052017-04-18 22:07:29 -0700336 bytes = s->length - diff;
Florin Corasf03a59a2017-06-09 21:07:32 -0700337 f->tail += bytes;
338 f->tail %= f->nitems;
Florin Corasb59a7052017-04-18 22:07:29 -0700339 ooo_segment_del (f, index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500340 break;
341 }
Florin Corasb59a7052017-04-18 22:07:29 -0700342
Dave Barach68b0fb02017-02-28 15:15:56 -0500343 /* If we have next go on */
Florin Corasb59a7052017-04-18 22:07:29 -0700344 if (s->next != OOO_SEGMENT_INVALID_INDEX)
Dave Barach68b0fb02017-02-28 15:15:56 -0500345 {
Dave Barach68b0fb02017-02-28 15:15:56 -0500346 s = pool_elt_at_index (f->ooo_segments, s->next);
Florin Corasf03a59a2017-06-09 21:07:32 -0700347 diff = (f->tail >= s->start) ?
348 f->tail - s->start : f->nitems + f->tail - s->start;
Dave Barach68b0fb02017-02-28 15:15:56 -0500349 ooo_segment_del (f, index);
350 }
351 /* End of search */
352 else
353 {
Florin Corasb59a7052017-04-18 22:07:29 -0700354 ooo_segment_del (f, index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500355 break;
356 }
357 }
358
Dave Barach68b0fb02017-02-28 15:15:56 -0500359 return bytes;
360}
361
362static int
Florin Corasa5464812017-04-19 13:00:05 -0700363svm_fifo_enqueue_internal (svm_fifo_t * f, u32 max_bytes, u8 * copy_from_here)
Dave Barach68b0fb02017-02-28 15:15:56 -0500364{
365 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
366 u32 cursize, nitems;
367
Florin Coras3e350af2017-03-30 02:54:28 -0700368 /* read cursize, which can only increase while we're working */
369 cursize = svm_fifo_max_dequeue (f);
Florin Corasf03a59a2017-06-09 21:07:32 -0700370 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
Florin Coras3e350af2017-03-30 02:54:28 -0700371
372 if (PREDICT_FALSE (cursize == f->nitems))
Dave Barach68b0fb02017-02-28 15:15:56 -0500373 return -2; /* fifo stuffed */
374
Dave Barach68b0fb02017-02-28 15:15:56 -0500375 nitems = f->nitems;
376
377 /* Number of bytes we're going to copy */
378 total_copy_bytes = (nitems - cursize) < max_bytes ?
379 (nitems - cursize) : max_bytes;
380
381 if (PREDICT_TRUE (copy_from_here != 0))
382 {
383 /* Number of bytes in first copy segment */
384 first_copy_bytes = ((nitems - f->tail) < total_copy_bytes)
385 ? (nitems - f->tail) : total_copy_bytes;
386
387 clib_memcpy (&f->data[f->tail], copy_from_here, first_copy_bytes);
388 f->tail += first_copy_bytes;
389 f->tail = (f->tail == nitems) ? 0 : f->tail;
390
391 /* Number of bytes in second copy segment, if any */
392 second_copy_bytes = total_copy_bytes - first_copy_bytes;
393 if (second_copy_bytes)
394 {
395 clib_memcpy (&f->data[f->tail], copy_from_here + first_copy_bytes,
396 second_copy_bytes);
397 f->tail += second_copy_bytes;
398 f->tail = (f->tail == nitems) ? 0 : f->tail;
399 }
400 }
401 else
402 {
403 /* Account for a zero-copy enqueue done elsewhere */
404 ASSERT (max_bytes <= (nitems - cursize));
405 f->tail += max_bytes;
406 f->tail = f->tail % nitems;
407 total_copy_bytes = max_bytes;
408 }
409
410 /* Any out-of-order segments to collect? */
411 if (PREDICT_FALSE (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX))
412 total_copy_bytes += ooo_segment_try_collect (f, total_copy_bytes);
413
414 /* Atomically increase the queue length */
415 __sync_fetch_and_add (&f->cursize, total_copy_bytes);
416
417 return (total_copy_bytes);
418}
419
420int
Florin Corasa5464812017-04-19 13:00:05 -0700421svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_from_here)
Dave Barach68b0fb02017-02-28 15:15:56 -0500422{
Florin Corasa5464812017-04-19 13:00:05 -0700423 return svm_fifo_enqueue_internal (f, max_bytes, copy_from_here);
Dave Barach68b0fb02017-02-28 15:15:56 -0500424}
425
Florin Coras6792ec02017-03-13 03:49:51 -0700426/**
427 * Enqueue a future segment.
428 *
Dave Barach68b0fb02017-02-28 15:15:56 -0500429 * Two choices: either copies the entire segment, or copies nothing
430 * Returns 0 of the entire segment was copied
431 * Returns -1 if none of the segment was copied due to lack of space
432 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500433static int
Florin Coras6792ec02017-03-13 03:49:51 -0700434svm_fifo_enqueue_with_offset_internal (svm_fifo_t * f,
Florin Coras6792ec02017-03-13 03:49:51 -0700435 u32 offset,
436 u32 required_bytes,
437 u8 * copy_from_here)
Dave Barach68b0fb02017-02-28 15:15:56 -0500438{
439 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
Florin Corasf03a59a2017-06-09 21:07:32 -0700440 u32 cursize, nitems, normalized_offset;
441 u32 offset_from_tail;
442
443 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
Dave Barach68b0fb02017-02-28 15:15:56 -0500444
Florin Coras3e350af2017-03-30 02:54:28 -0700445 /* read cursize, which can only increase while we're working */
446 cursize = svm_fifo_max_dequeue (f);
Dave Barach68b0fb02017-02-28 15:15:56 -0500447 nitems = f->nitems;
Florin Corasf03a59a2017-06-09 21:07:32 -0700448
449 normalized_offset = (f->tail + offset) % nitems;
Dave Barach68b0fb02017-02-28 15:15:56 -0500450
451 /* Will this request fit? */
Florin Corasc28764f2017-04-26 00:08:42 -0700452 offset_from_tail = (nitems + normalized_offset - f->tail) % nitems;
453 if ((required_bytes + offset_from_tail) > (nitems - cursize))
Dave Barach68b0fb02017-02-28 15:15:56 -0500454 return -1;
455
456 ooo_segment_add (f, offset, required_bytes);
457
458 /* Number of bytes we're going to copy */
459 total_copy_bytes = required_bytes;
Dave Barach68b0fb02017-02-28 15:15:56 -0500460
461 /* Number of bytes in first copy segment */
Dave Barach1f75cfd2017-04-14 16:46:44 -0400462 first_copy_bytes = ((nitems - normalized_offset) < total_copy_bytes)
463 ? (nitems - normalized_offset) : total_copy_bytes;
Dave Barach68b0fb02017-02-28 15:15:56 -0500464
Dave Barach1f75cfd2017-04-14 16:46:44 -0400465 clib_memcpy (&f->data[normalized_offset], copy_from_here, first_copy_bytes);
Dave Barach68b0fb02017-02-28 15:15:56 -0500466
467 /* Number of bytes in second copy segment, if any */
468 second_copy_bytes = total_copy_bytes - first_copy_bytes;
469 if (second_copy_bytes)
470 {
Dave Barach1f75cfd2017-04-14 16:46:44 -0400471 normalized_offset += first_copy_bytes;
472 normalized_offset %= nitems;
Dave Barach68b0fb02017-02-28 15:15:56 -0500473
Dave Barach1f75cfd2017-04-14 16:46:44 -0400474 ASSERT (normalized_offset == 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500475
Dave Barach1f75cfd2017-04-14 16:46:44 -0400476 clib_memcpy (&f->data[normalized_offset],
Dave Barach68b0fb02017-02-28 15:15:56 -0500477 copy_from_here + first_copy_bytes, second_copy_bytes);
478 }
479
480 return (0);
481}
482
483
484int
485svm_fifo_enqueue_with_offset (svm_fifo_t * f,
Dave Barach68b0fb02017-02-28 15:15:56 -0500486 u32 offset,
487 u32 required_bytes, u8 * copy_from_here)
488{
Florin Corasa5464812017-04-19 13:00:05 -0700489 return svm_fifo_enqueue_with_offset_internal (f, offset, required_bytes,
490 copy_from_here);
Dave Barach68b0fb02017-02-28 15:15:56 -0500491}
492
493
494static int
Florin Corasa5464812017-04-19 13:00:05 -0700495svm_fifo_dequeue_internal (svm_fifo_t * f, u32 max_bytes, u8 * copy_here)
Dave Barach68b0fb02017-02-28 15:15:56 -0500496{
497 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
498 u32 cursize, nitems;
499
Florin Coras3e350af2017-03-30 02:54:28 -0700500 /* read cursize, which can only increase while we're working */
501 cursize = svm_fifo_max_dequeue (f);
502 if (PREDICT_FALSE (cursize == 0))
Dave Barach68b0fb02017-02-28 15:15:56 -0500503 return -2; /* nothing in the fifo */
504
Dave Barach68b0fb02017-02-28 15:15:56 -0500505 nitems = f->nitems;
506
507 /* Number of bytes we're going to copy */
508 total_copy_bytes = (cursize < max_bytes) ? cursize : max_bytes;
509
510 if (PREDICT_TRUE (copy_here != 0))
511 {
512 /* Number of bytes in first copy segment */
513 first_copy_bytes = ((nitems - f->head) < total_copy_bytes)
514 ? (nitems - f->head) : total_copy_bytes;
515 clib_memcpy (copy_here, &f->data[f->head], first_copy_bytes);
516 f->head += first_copy_bytes;
517 f->head = (f->head == nitems) ? 0 : f->head;
518
519 /* Number of bytes in second copy segment, if any */
520 second_copy_bytes = total_copy_bytes - first_copy_bytes;
521 if (second_copy_bytes)
522 {
523 clib_memcpy (copy_here + first_copy_bytes,
524 &f->data[f->head], second_copy_bytes);
525 f->head += second_copy_bytes;
526 f->head = (f->head == nitems) ? 0 : f->head;
527 }
528 }
529 else
530 {
531 /* Account for a zero-copy dequeue done elsewhere */
532 ASSERT (max_bytes <= cursize);
533 f->head += max_bytes;
534 f->head = f->head % nitems;
535 cursize -= max_bytes;
536 total_copy_bytes = max_bytes;
537 }
538
539 __sync_fetch_and_sub (&f->cursize, total_copy_bytes);
540
541 return (total_copy_bytes);
542}
543
544int
Florin Corasa5464812017-04-19 13:00:05 -0700545svm_fifo_dequeue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_here)
Dave Barach68b0fb02017-02-28 15:15:56 -0500546{
Florin Corasa5464812017-04-19 13:00:05 -0700547 return svm_fifo_dequeue_internal (f, max_bytes, copy_here);
Dave Barach68b0fb02017-02-28 15:15:56 -0500548}
549
550int
Florin Corasa5464812017-04-19 13:00:05 -0700551svm_fifo_peek (svm_fifo_t * f, u32 relative_offset, u32 max_bytes,
Dave Barach68b0fb02017-02-28 15:15:56 -0500552 u8 * copy_here)
553{
554 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
Florin Coras6792ec02017-03-13 03:49:51 -0700555 u32 cursize, nitems, real_head;
Dave Barach68b0fb02017-02-28 15:15:56 -0500556
Florin Coras3e350af2017-03-30 02:54:28 -0700557 /* read cursize, which can only increase while we're working */
558 cursize = svm_fifo_max_dequeue (f);
Florin Coras93992a92017-05-24 18:03:56 -0700559 if (PREDICT_FALSE (cursize < relative_offset))
Dave Barach68b0fb02017-02-28 15:15:56 -0500560 return -2; /* nothing in the fifo */
561
Dave Barach68b0fb02017-02-28 15:15:56 -0500562 nitems = f->nitems;
Florin Corasb59a7052017-04-18 22:07:29 -0700563 real_head = f->head + relative_offset;
Florin Coras6792ec02017-03-13 03:49:51 -0700564 real_head = real_head >= nitems ? real_head - nitems : real_head;
Dave Barach68b0fb02017-02-28 15:15:56 -0500565
566 /* Number of bytes we're going to copy */
Florin Coras93992a92017-05-24 18:03:56 -0700567 total_copy_bytes = (cursize - relative_offset < max_bytes) ?
568 cursize - relative_offset : max_bytes;
Dave Barach68b0fb02017-02-28 15:15:56 -0500569
570 if (PREDICT_TRUE (copy_here != 0))
571 {
572 /* Number of bytes in first copy segment */
573 first_copy_bytes =
Florin Coras6792ec02017-03-13 03:49:51 -0700574 ((nitems - real_head) < total_copy_bytes) ?
575 (nitems - real_head) : total_copy_bytes;
576 clib_memcpy (copy_here, &f->data[real_head], first_copy_bytes);
Dave Barach68b0fb02017-02-28 15:15:56 -0500577
578 /* Number of bytes in second copy segment, if any */
579 second_copy_bytes = total_copy_bytes - first_copy_bytes;
580 if (second_copy_bytes)
581 {
582 clib_memcpy (copy_here + first_copy_bytes, &f->data[0],
583 second_copy_bytes);
584 }
585 }
586 return total_copy_bytes;
587}
588
589int
Florin Corasa5464812017-04-19 13:00:05 -0700590svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes)
Dave Barach68b0fb02017-02-28 15:15:56 -0500591{
592 u32 total_drop_bytes, first_drop_bytes, second_drop_bytes;
593 u32 cursize, nitems;
594
Florin Coras3e350af2017-03-30 02:54:28 -0700595 /* read cursize, which can only increase while we're working */
596 cursize = svm_fifo_max_dequeue (f);
597 if (PREDICT_FALSE (cursize == 0))
Dave Barach68b0fb02017-02-28 15:15:56 -0500598 return -2; /* nothing in the fifo */
599
Dave Barach68b0fb02017-02-28 15:15:56 -0500600 nitems = f->nitems;
601
602 /* Number of bytes we're going to drop */
603 total_drop_bytes = (cursize < max_bytes) ? cursize : max_bytes;
604
605 /* Number of bytes in first copy segment */
606 first_drop_bytes =
607 ((nitems - f->head) < total_drop_bytes) ?
608 (nitems - f->head) : total_drop_bytes;
609 f->head += first_drop_bytes;
610 f->head = (f->head == nitems) ? 0 : f->head;
611
612 /* Number of bytes in second drop segment, if any */
613 second_drop_bytes = total_drop_bytes - first_drop_bytes;
614 if (second_drop_bytes)
615 {
616 f->head += second_drop_bytes;
617 f->head = (f->head == nitems) ? 0 : f->head;
618 }
619
620 __sync_fetch_and_sub (&f->cursize, total_drop_bytes);
621
622 return total_drop_bytes;
623}
624
Dave Barach1f75cfd2017-04-14 16:46:44 -0400625u32
626svm_fifo_number_ooo_segments (svm_fifo_t * f)
627{
628 return pool_elts (f->ooo_segments);
629}
630
631ooo_segment_t *
632svm_fifo_first_ooo_segment (svm_fifo_t * f)
633{
634 return pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
635}
636
Florin Corasc28764f2017-04-26 00:08:42 -0700637/**
638 * Set fifo pointers to requested offset
639 */
640void
641svm_fifo_init_pointers (svm_fifo_t * f, u32 pointer)
642{
643 f->head = f->tail = pointer % f->nitems;
644}
645
Dave Barach68b0fb02017-02-28 15:15:56 -0500646/*
647 * fd.io coding-style-patch-verification: ON
648 *
649 * Local Variables:
650 * eval: (c-set-style "gnu")
651 * End:
652 */