blob: b2c8e5bdb166ae2c71e0926de53f9cd1e16af922 [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
53u8 *
54format_ooo_segment (u8 * s, va_list * args)
55{
Florin Coras1f152cd2017-08-18 19:28:03 -070056 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
Dave Barach1f75cfd2017-04-14 16:46:44 -040057 ooo_segment_t *seg = va_arg (*args, ooo_segment_t *);
Florin Coras1f152cd2017-08-18 19:28:03 -070058 u32 normalized_start = (seg->start + f->nitems - f->tail) % f->nitems;
59 s = format (s, "[%u, %u], len %u, next %d, prev %d", normalized_start,
60 (normalized_start + seg->length) % f->nitems, seg->length,
61 seg->next, seg->prev);
Dave Barach1f75cfd2017-04-14 16:46:44 -040062 return s;
63}
64
65u8 *
Florin Coras3eb50622017-07-13 01:24:57 -040066svm_fifo_dump_trace (u8 * s, svm_fifo_t * f)
67{
68#if SVM_FIFO_TRACE
69 svm_fifo_trace_elem_t *seg = 0;
70 int i = 0;
71
72 if (f->trace)
73 {
74 vec_foreach (seg, f->trace)
75 {
76 s = format (s, "{%u, %u, %u}, ", seg->offset, seg->len, seg->action);
77 i++;
78 if (i % 5 == 0)
79 s = format (s, "\n");
80 }
81 s = format (s, "\n");
82 }
83 return s;
84#else
85 return 0;
86#endif
87}
88
89u8 *
90svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose)
91{
92 int i, trace_len;
93 u8 *data = 0;
94 svm_fifo_trace_elem_t *trace;
95 u32 offset;
96 svm_fifo_t *dummy_fifo;
97
98 if (!f)
99 return s;
100
101#if SVM_FIFO_TRACE
102 trace = f->trace;
103 trace_len = vec_len (trace);
104#else
105 trace = 0;
106 trace_len = 0;
107#endif
108
109 dummy_fifo = svm_fifo_create (f->nitems);
110 memset (f->data, 0xFF, f->nitems);
111
112 vec_validate (data, f->nitems);
113 for (i = 0; i < vec_len (data); i++)
114 data[i] = i;
115
116 for (i = 0; i < trace_len; i++)
117 {
118 offset = trace[i].offset;
119 if (trace[i].action == 1)
120 {
121 if (verbose)
122 s = format (s, "adding [%u, %u]:", trace[i].offset,
123 (trace[i].offset +
124 trace[i].len) % dummy_fifo->nitems);
125 svm_fifo_enqueue_with_offset (dummy_fifo, trace[i].offset,
126 trace[i].len, &data[offset]);
127 }
128 else if (trace[i].action == 2)
129 {
130 if (verbose)
131 s = format (s, "adding [%u, %u]:", 0, trace[i].len);
132 svm_fifo_enqueue_nowait (dummy_fifo, trace[i].len, &data[offset]);
133 }
134 else if (!no_read)
135 {
136 if (verbose)
137 s = format (s, "read: %u", trace[i].len);
138 svm_fifo_dequeue_drop (dummy_fifo, trace[i].len);
139 }
140 if (verbose)
141 s = format (s, "%U", format_svm_fifo, dummy_fifo, 1);
142 }
143
144 s = format (s, "result: %U", format_svm_fifo, dummy_fifo, 1);
145
146 return s;
147}
148
149u8 *
Dave Barach1f75cfd2017-04-14 16:46:44 -0400150format_ooo_list (u8 * s, va_list * args)
151{
152 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
153 u32 ooo_segment_index = f->ooos_list_head;
154 ooo_segment_t *seg;
155
156 while (ooo_segment_index != OOO_SEGMENT_INVALID_INDEX)
157 {
158 seg = pool_elt_at_index (f->ooo_segments, ooo_segment_index);
Florin Coras1f152cd2017-08-18 19:28:03 -0700159 s = format (s, " %U\n", format_ooo_segment, f, seg);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400160 ooo_segment_index = seg->next;
161 }
Florin Coras3eb50622017-07-13 01:24:57 -0400162
Dave Barach1f75cfd2017-04-14 16:46:44 -0400163 return s;
164}
165
Florin Corasb59a7052017-04-18 22:07:29 -0700166u8 *
167format_svm_fifo (u8 * s, va_list * args)
168{
169 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
170 int verbose = va_arg (*args, int);
171
172 s = format (s, "cursize %u nitems %u has_event %d\n",
173 f->cursize, f->nitems, f->has_event);
Florin Corasbb292f42017-05-19 09:49:19 -0700174 s = format (s, " head %d tail %d\n", f->head, f->tail);
Florin Corasb59a7052017-04-18 22:07:29 -0700175
176 if (verbose > 1)
177 s = format
Florin Corasbb292f42017-05-19 09:49:19 -0700178 (s, " server session %d thread %d client session %d thread %d\n",
Florin Corasa5464812017-04-19 13:00:05 -0700179 f->master_session_index, f->master_thread_index,
Florin Corasb59a7052017-04-18 22:07:29 -0700180 f->client_session_index, f->client_thread_index);
181
182 if (verbose)
183 {
Florin Coras3eb50622017-07-13 01:24:57 -0400184 s = format (s, " ooo pool %d active elts newest %u\n",
185 pool_elts (f->ooo_segments), f->ooos_newest);
Florin Corasbb292f42017-05-19 09:49:19 -0700186 if (svm_fifo_has_ooo_data (f))
Florin Coras3eb50622017-07-13 01:24:57 -0400187 s = format (s, " %U", format_ooo_list, f, verbose);
Florin Corasb59a7052017-04-18 22:07:29 -0700188 }
189 return s;
190}
191
Dave Barach68b0fb02017-02-28 15:15:56 -0500192/** create an svm fifo, in the current heap. Fails vs blow up the process */
193svm_fifo_t *
194svm_fifo_create (u32 data_size_in_bytes)
195{
196 svm_fifo_t *f;
Dave Barach818eb542017-08-02 13:56:13 -0400197 u32 rounded_data_size;
Dave Barach68b0fb02017-02-28 15:15:56 -0500198
Dave Barach818eb542017-08-02 13:56:13 -0400199 /* always round fifo data size to the next highest power-of-two */
200 rounded_data_size = (1 << (max_log2 (data_size_in_bytes)));
201 f = clib_mem_alloc_aligned_or_null (sizeof (*f) + rounded_data_size,
Dave Barach68b0fb02017-02-28 15:15:56 -0500202 CLIB_CACHE_LINE_BYTES);
203 if (f == 0)
204 return 0;
205
Dave Barach2c25a622017-06-26 11:35:07 -0400206 memset (f, 0, sizeof (*f));
Dave Barach68b0fb02017-02-28 15:15:56 -0500207 f->nitems = data_size_in_bytes;
208 f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
Dave Barach52851e62017-08-07 09:35:25 -0400209 f->refcnt = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500210 return (f);
211}
212
Florin Coras6cf30ad2017-04-04 23:08:23 -0700213void
214svm_fifo_free (svm_fifo_t * f)
215{
Dave Barach52851e62017-08-07 09:35:25 -0400216 ASSERT (f->refcnt > 0);
217
218 if (--f->refcnt == 0)
219 {
220 pool_free (f->ooo_segments);
221 clib_mem_free (f);
222 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700223}
224
Dave Barach68b0fb02017-02-28 15:15:56 -0500225always_inline ooo_segment_t *
226ooo_segment_new (svm_fifo_t * f, u32 start, u32 length)
227{
228 ooo_segment_t *s;
229
230 pool_get (f->ooo_segments, s);
231
Dave Barach1f75cfd2017-04-14 16:46:44 -0400232 s->start = start;
Dave Barach68b0fb02017-02-28 15:15:56 -0500233 s->length = length;
234
235 s->prev = s->next = OOO_SEGMENT_INVALID_INDEX;
236
237 return s;
238}
239
240always_inline void
241ooo_segment_del (svm_fifo_t * f, u32 index)
242{
243 ooo_segment_t *cur, *prev = 0, *next = 0;
244 cur = pool_elt_at_index (f->ooo_segments, index);
245
246 if (cur->next != OOO_SEGMENT_INVALID_INDEX)
247 {
248 next = pool_elt_at_index (f->ooo_segments, cur->next);
249 next->prev = cur->prev;
250 }
251
252 if (cur->prev != OOO_SEGMENT_INVALID_INDEX)
253 {
254 prev = pool_elt_at_index (f->ooo_segments, cur->prev);
255 prev->next = cur->next;
256 }
257 else
258 {
259 f->ooos_list_head = cur->next;
260 }
261
262 pool_put (f->ooo_segments, cur);
263}
264
265/**
266 * Add segment to fifo's out-of-order segment list. Takes care of merging
267 * adjacent segments and removing overlapping ones.
268 */
269static void
270ooo_segment_add (svm_fifo_t * f, u32 offset, u32 length)
271{
272 ooo_segment_t *s, *new_s, *prev, *next, *it;
Florin Corasf03a59a2017-06-09 21:07:32 -0700273 u32 new_index, s_end_pos, s_index;
274 u32 normalized_position, normalized_end_position;
Dave Barach68b0fb02017-02-28 15:15:56 -0500275
Florin Coras3eb50622017-07-13 01:24:57 -0400276 ASSERT (offset + length <= ooo_segment_distance_from_tail (f, f->head));
Florin Corasf03a59a2017-06-09 21:07:32 -0700277 normalized_position = (f->tail + offset) % f->nitems;
278 normalized_end_position = (f->tail + offset + length) % f->nitems;
279
280 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
Dave Barach68b0fb02017-02-28 15:15:56 -0500281
282 if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX)
283 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700284 s = ooo_segment_new (f, normalized_position, length);
Dave Barach68b0fb02017-02-28 15:15:56 -0500285 f->ooos_list_head = s - f->ooo_segments;
286 f->ooos_newest = f->ooos_list_head;
287 return;
288 }
289
290 /* Find first segment that starts after new segment */
291 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
292 while (s->next != OOO_SEGMENT_INVALID_INDEX
Florin Corasf03a59a2017-06-09 21:07:32 -0700293 && position_lt (f, s->start, normalized_position))
Dave Barach68b0fb02017-02-28 15:15:56 -0500294 s = pool_elt_at_index (f->ooo_segments, s->next);
295
Florin Corasc28764f2017-04-26 00:08:42 -0700296 /* If we have a previous and we overlap it, use it as starting point */
297 prev = ooo_segment_get_prev (f, s);
Florin Corasf03a59a2017-06-09 21:07:32 -0700298 if (prev
299 && position_leq (f, normalized_position, ooo_segment_end_pos (f, prev)))
Florin Corasc28764f2017-04-26 00:08:42 -0700300 {
301 s = prev;
Florin Corasf03a59a2017-06-09 21:07:32 -0700302 s_end_pos = ooo_segment_end_pos (f, s);
Dave Barach2c25a622017-06-26 11:35:07 -0400303
Florin Coras3eb50622017-07-13 01:24:57 -0400304 /* Since we have previous, normalized start position cannot be smaller
305 * than prev->start. Check tail */
306 ASSERT (position_lt (f, s->start, normalized_position));
Dave Barach2c25a622017-06-26 11:35:07 -0400307 goto check_tail;
Florin Corasc28764f2017-04-26 00:08:42 -0700308 }
309
Dave Barach68b0fb02017-02-28 15:15:56 -0500310 s_index = s - f->ooo_segments;
Florin Corasf03a59a2017-06-09 21:07:32 -0700311 s_end_pos = ooo_segment_end_pos (f, s);
Dave Barach68b0fb02017-02-28 15:15:56 -0500312
313 /* No overlap, add before current segment */
Florin Corasf03a59a2017-06-09 21:07:32 -0700314 if (position_lt (f, normalized_end_position, s->start))
Dave Barach68b0fb02017-02-28 15:15:56 -0500315 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700316 new_s = ooo_segment_new (f, normalized_position, length);
Dave Barach68b0fb02017-02-28 15:15:56 -0500317 new_index = new_s - f->ooo_segments;
318
319 /* Pool might've moved, get segment again */
320 s = pool_elt_at_index (f->ooo_segments, s_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500321 if (s->prev != OOO_SEGMENT_INVALID_INDEX)
322 {
323 new_s->prev = s->prev;
Dave Barach68b0fb02017-02-28 15:15:56 -0500324 prev = pool_elt_at_index (f->ooo_segments, new_s->prev);
325 prev->next = new_index;
326 }
327 else
328 {
329 /* New head */
330 f->ooos_list_head = new_index;
331 }
332
Florin Corasf03a59a2017-06-09 21:07:32 -0700333 new_s->next = s_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500334 s->prev = new_index;
335 f->ooos_newest = new_index;
336 return;
337 }
338 /* No overlap, add after current segment */
Florin Corasf03a59a2017-06-09 21:07:32 -0700339 else if (position_gt (f, normalized_position, s_end_pos))
Dave Barach68b0fb02017-02-28 15:15:56 -0500340 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700341 new_s = ooo_segment_new (f, normalized_position, length);
Dave Barach68b0fb02017-02-28 15:15:56 -0500342 new_index = new_s - f->ooo_segments;
343
344 /* Pool might've moved, get segment again */
345 s = pool_elt_at_index (f->ooo_segments, s_index);
346
Florin Coras3eb50622017-07-13 01:24:57 -0400347 /* Needs to be last */
Florin Corasf03a59a2017-06-09 21:07:32 -0700348 ASSERT (s->next == OOO_SEGMENT_INVALID_INDEX);
Dave Barach68b0fb02017-02-28 15:15:56 -0500349
Florin Corasf03a59a2017-06-09 21:07:32 -0700350 new_s->prev = s_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500351 s->next = new_index;
352 f->ooos_newest = new_index;
353
354 return;
355 }
356
357 /*
358 * Merge needed
359 */
360
361 /* Merge at head */
Florin Corasf03a59a2017-06-09 21:07:32 -0700362 if (position_lt (f, normalized_position, s->start))
Dave Barach68b0fb02017-02-28 15:15:56 -0500363 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700364 s->start = normalized_position;
365 s->length = position_diff (f, s_end_pos, s->start);
Florin Coras3eb50622017-07-13 01:24:57 -0400366 f->ooos_newest = s - f->ooo_segments;
Dave Barach68b0fb02017-02-28 15:15:56 -0500367 }
368
Dave Barach2c25a622017-06-26 11:35:07 -0400369check_tail:
Florin Coras3eb50622017-07-13 01:24:57 -0400370
371 /* Overlapping tail */
Florin Corasf03a59a2017-06-09 21:07:32 -0700372 if (position_gt (f, normalized_end_position, s_end_pos))
Florin Corasc28764f2017-04-26 00:08:42 -0700373 {
Florin Coras3eb50622017-07-13 01:24:57 -0400374 s->length = position_diff (f, normalized_end_position, s->start);
375
376 /* Remove the completely overlapped segments in the tail */
377 it = ooo_segment_next (f, s);
Florin Corasf03a59a2017-06-09 21:07:32 -0700378 while (it && position_leq (f, ooo_segment_end_pos (f, it),
379 normalized_end_position))
Florin Corasc28764f2017-04-26 00:08:42 -0700380 {
Florin Coras3eb50622017-07-13 01:24:57 -0400381 next = ooo_segment_next (f, it);
Florin Corasc28764f2017-04-26 00:08:42 -0700382 ooo_segment_del (f, it - f->ooo_segments);
383 it = next;
384 }
385
386 /* If partial overlap with last, merge */
Florin Corasf03a59a2017-06-09 21:07:32 -0700387 if (it && position_leq (f, it->start, normalized_end_position))
Florin Corasc28764f2017-04-26 00:08:42 -0700388 {
Florin Coras3eb50622017-07-13 01:24:57 -0400389 s->length = position_diff (f, ooo_segment_end_pos (f, it),
390 s->start);
Florin Corasc28764f2017-04-26 00:08:42 -0700391 ooo_segment_del (f, it - f->ooo_segments);
392 }
Florin Coras3eb50622017-07-13 01:24:57 -0400393 f->ooos_newest = s - f->ooo_segments;
Florin Corasc28764f2017-04-26 00:08:42 -0700394 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500395}
396
397/**
398 * Removes segments that can now be enqueued because the fifo's tail has
399 * advanced. Returns the number of bytes added to tail.
400 */
401static int
402ooo_segment_try_collect (svm_fifo_t * f, u32 n_bytes_enqueued)
403{
404 ooo_segment_t *s;
Florin Corasf03a59a2017-06-09 21:07:32 -0700405 u32 index, bytes = 0;
406 i32 diff;
Dave Barach68b0fb02017-02-28 15:15:56 -0500407
408 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
Dave Barach2c25a622017-06-26 11:35:07 -0400409 diff = ooo_segment_distance_to_tail (f, s->start);
Dave Barach68b0fb02017-02-28 15:15:56 -0500410
Dave Barach2c25a622017-06-26 11:35:07 -0400411 ASSERT (diff != n_bytes_enqueued);
Florin Corasc28764f2017-04-26 00:08:42 -0700412
Florin Corasf03a59a2017-06-09 21:07:32 -0700413 if (diff > n_bytes_enqueued)
Florin Corasb59a7052017-04-18 22:07:29 -0700414 return 0;
415
Dave Barach68b0fb02017-02-28 15:15:56 -0500416 /* If last tail update overlaps one/multiple ooo segments, remove them */
Florin Corasf03a59a2017-06-09 21:07:32 -0700417 while (0 <= diff && diff < n_bytes_enqueued)
Dave Barach68b0fb02017-02-28 15:15:56 -0500418 {
Florin Corasb59a7052017-04-18 22:07:29 -0700419 index = s - f->ooo_segments;
420
421 /* Segment end is beyond the tail. Advance tail and remove segment */
Florin Corasf03a59a2017-06-09 21:07:32 -0700422 if (s->length > diff)
Dave Barach68b0fb02017-02-28 15:15:56 -0500423 {
Florin Corasb59a7052017-04-18 22:07:29 -0700424 bytes = s->length - diff;
Florin Corasf03a59a2017-06-09 21:07:32 -0700425 f->tail += bytes;
426 f->tail %= f->nitems;
Florin Corasb59a7052017-04-18 22:07:29 -0700427 ooo_segment_del (f, index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500428 break;
429 }
Florin Corasb59a7052017-04-18 22:07:29 -0700430
Dave Barach68b0fb02017-02-28 15:15:56 -0500431 /* If we have next go on */
Florin Corasb59a7052017-04-18 22:07:29 -0700432 if (s->next != OOO_SEGMENT_INVALID_INDEX)
Dave Barach68b0fb02017-02-28 15:15:56 -0500433 {
Dave Barach68b0fb02017-02-28 15:15:56 -0500434 s = pool_elt_at_index (f->ooo_segments, s->next);
Dave Barach2c25a622017-06-26 11:35:07 -0400435 diff = ooo_segment_distance_to_tail (f, s->start);
Dave Barach68b0fb02017-02-28 15:15:56 -0500436 ooo_segment_del (f, index);
437 }
438 /* End of search */
439 else
440 {
Florin Corasb59a7052017-04-18 22:07:29 -0700441 ooo_segment_del (f, index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500442 break;
443 }
444 }
445
Chris Lukeab7b8d92017-09-07 07:40:13 -0400446 ASSERT (bytes <= f->nitems);
Dave Barach68b0fb02017-02-28 15:15:56 -0500447 return bytes;
448}
449
450static int
Florin Coras371ca502018-02-21 12:07:41 -0800451svm_fifo_enqueue_internal (svm_fifo_t * f, u32 max_bytes,
452 const u8 * copy_from_here)
Dave Barach68b0fb02017-02-28 15:15:56 -0500453{
454 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
455 u32 cursize, nitems;
456
Florin Coras3e350af2017-03-30 02:54:28 -0700457 /* read cursize, which can only increase while we're working */
458 cursize = svm_fifo_max_dequeue (f);
Florin Corasf03a59a2017-06-09 21:07:32 -0700459 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
Florin Coras3e350af2017-03-30 02:54:28 -0700460
461 if (PREDICT_FALSE (cursize == f->nitems))
Dave Barach68b0fb02017-02-28 15:15:56 -0500462 return -2; /* fifo stuffed */
463
Dave Barach68b0fb02017-02-28 15:15:56 -0500464 nitems = f->nitems;
465
466 /* Number of bytes we're going to copy */
467 total_copy_bytes = (nitems - cursize) < max_bytes ?
468 (nitems - cursize) : max_bytes;
469
470 if (PREDICT_TRUE (copy_from_here != 0))
471 {
472 /* Number of bytes in first copy segment */
473 first_copy_bytes = ((nitems - f->tail) < total_copy_bytes)
474 ? (nitems - f->tail) : total_copy_bytes;
475
476 clib_memcpy (&f->data[f->tail], copy_from_here, first_copy_bytes);
477 f->tail += first_copy_bytes;
478 f->tail = (f->tail == nitems) ? 0 : f->tail;
479
480 /* Number of bytes in second copy segment, if any */
481 second_copy_bytes = total_copy_bytes - first_copy_bytes;
482 if (second_copy_bytes)
483 {
484 clib_memcpy (&f->data[f->tail], copy_from_here + first_copy_bytes,
485 second_copy_bytes);
486 f->tail += second_copy_bytes;
487 f->tail = (f->tail == nitems) ? 0 : f->tail;
488 }
489 }
490 else
491 {
Dave Barach2c25a622017-06-26 11:35:07 -0400492 ASSERT (0);
493
Dave Barach68b0fb02017-02-28 15:15:56 -0500494 /* Account for a zero-copy enqueue done elsewhere */
495 ASSERT (max_bytes <= (nitems - cursize));
496 f->tail += max_bytes;
497 f->tail = f->tail % nitems;
498 total_copy_bytes = max_bytes;
499 }
500
Florin Coras3eb50622017-07-13 01:24:57 -0400501 svm_fifo_trace_add (f, f->head, total_copy_bytes, 2);
502
Dave Barach68b0fb02017-02-28 15:15:56 -0500503 /* Any out-of-order segments to collect? */
504 if (PREDICT_FALSE (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX))
505 total_copy_bytes += ooo_segment_try_collect (f, total_copy_bytes);
506
507 /* Atomically increase the queue length */
Dave Barach2c25a622017-06-26 11:35:07 -0400508 ASSERT (cursize + total_copy_bytes <= nitems);
Dave Barach68b0fb02017-02-28 15:15:56 -0500509 __sync_fetch_and_add (&f->cursize, total_copy_bytes);
510
511 return (total_copy_bytes);
512}
513
Florin Corasf6359c82017-06-19 12:26:09 -0400514#define SVM_ENQUEUE_CLONE_TEMPLATE(arch, fn, tgt) \
515 uword \
516 __attribute__ ((flatten)) \
517 __attribute__ ((target (tgt))) \
518 CLIB_CPU_OPTIMIZED \
519 fn ## _ ## arch ( svm_fifo_t * f, u32 max_bytes, u8 * copy_from_here) \
520 { return fn (f, max_bytes, copy_from_here);}
521
522static int
523svm_fifo_enqueue_nowait_ma (svm_fifo_t * f, u32 max_bytes,
Florin Coras371ca502018-02-21 12:07:41 -0800524 const u8 * copy_from_here)
Florin Corasf6359c82017-06-19 12:26:09 -0400525{
526 return svm_fifo_enqueue_internal (f, max_bytes, copy_from_here);
527}
528
529foreach_march_variant (SVM_ENQUEUE_CLONE_TEMPLATE,
530 svm_fifo_enqueue_nowait_ma);
531CLIB_MULTIARCH_SELECT_FN (svm_fifo_enqueue_nowait_ma);
532
Dave Barach68b0fb02017-02-28 15:15:56 -0500533int
Florin Coras371ca502018-02-21 12:07:41 -0800534svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 max_bytes,
535 const u8 * copy_from_here)
Dave Barach68b0fb02017-02-28 15:15:56 -0500536{
Florin Corasf6359c82017-06-19 12:26:09 -0400537#if CLIB_DEBUG > 0
538 return svm_fifo_enqueue_nowait_ma (f, max_bytes, copy_from_here);
539#else
Florin Coras371ca502018-02-21 12:07:41 -0800540 static int (*fp) (svm_fifo_t *, u32, const u8 *);
Florin Corasf6359c82017-06-19 12:26:09 -0400541
542 if (PREDICT_FALSE (fp == 0))
543 fp = (void *) svm_fifo_enqueue_nowait_ma_multiarch_select ();
544
545 return (*fp) (f, max_bytes, copy_from_here);
546#endif
Dave Barach68b0fb02017-02-28 15:15:56 -0500547}
548
Florin Coras6792ec02017-03-13 03:49:51 -0700549/**
550 * Enqueue a future segment.
551 *
Dave Barach68b0fb02017-02-28 15:15:56 -0500552 * Two choices: either copies the entire segment, or copies nothing
553 * Returns 0 of the entire segment was copied
554 * Returns -1 if none of the segment was copied due to lack of space
555 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500556static int
Florin Coras6792ec02017-03-13 03:49:51 -0700557svm_fifo_enqueue_with_offset_internal (svm_fifo_t * f,
Florin Coras6792ec02017-03-13 03:49:51 -0700558 u32 offset,
559 u32 required_bytes,
560 u8 * copy_from_here)
Dave Barach68b0fb02017-02-28 15:15:56 -0500561{
562 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
Florin Corasf03a59a2017-06-09 21:07:32 -0700563 u32 cursize, nitems, normalized_offset;
Florin Corasf03a59a2017-06-09 21:07:32 -0700564
565 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
Dave Barach68b0fb02017-02-28 15:15:56 -0500566
Florin Coras3e350af2017-03-30 02:54:28 -0700567 /* read cursize, which can only increase while we're working */
568 cursize = svm_fifo_max_dequeue (f);
Dave Barach68b0fb02017-02-28 15:15:56 -0500569 nitems = f->nitems;
Florin Corasf03a59a2017-06-09 21:07:32 -0700570
Dave Barach2c25a622017-06-26 11:35:07 -0400571 ASSERT (required_bytes < nitems);
572
Florin Corasf03a59a2017-06-09 21:07:32 -0700573 normalized_offset = (f->tail + offset) % nitems;
Dave Barach68b0fb02017-02-28 15:15:56 -0500574
575 /* Will this request fit? */
Florin Coras1f152cd2017-08-18 19:28:03 -0700576 if ((required_bytes + offset) > (nitems - cursize))
Dave Barach68b0fb02017-02-28 15:15:56 -0500577 return -1;
578
Florin Coras3eb50622017-07-13 01:24:57 -0400579 svm_fifo_trace_add (f, offset, required_bytes, 1);
580
Dave Barach68b0fb02017-02-28 15:15:56 -0500581 ooo_segment_add (f, offset, required_bytes);
582
583 /* Number of bytes we're going to copy */
584 total_copy_bytes = required_bytes;
Dave Barach68b0fb02017-02-28 15:15:56 -0500585
586 /* Number of bytes in first copy segment */
Dave Barach1f75cfd2017-04-14 16:46:44 -0400587 first_copy_bytes = ((nitems - normalized_offset) < total_copy_bytes)
588 ? (nitems - normalized_offset) : total_copy_bytes;
Dave Barach68b0fb02017-02-28 15:15:56 -0500589
Dave Barach1f75cfd2017-04-14 16:46:44 -0400590 clib_memcpy (&f->data[normalized_offset], copy_from_here, first_copy_bytes);
Dave Barach68b0fb02017-02-28 15:15:56 -0500591
592 /* Number of bytes in second copy segment, if any */
593 second_copy_bytes = total_copy_bytes - first_copy_bytes;
594 if (second_copy_bytes)
595 {
Dave Barach1f75cfd2017-04-14 16:46:44 -0400596 normalized_offset += first_copy_bytes;
597 normalized_offset %= nitems;
Dave Barach68b0fb02017-02-28 15:15:56 -0500598
Dave Barach1f75cfd2017-04-14 16:46:44 -0400599 ASSERT (normalized_offset == 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500600
Dave Barach1f75cfd2017-04-14 16:46:44 -0400601 clib_memcpy (&f->data[normalized_offset],
Dave Barach68b0fb02017-02-28 15:15:56 -0500602 copy_from_here + first_copy_bytes, second_copy_bytes);
603 }
604
605 return (0);
606}
607
608
609int
610svm_fifo_enqueue_with_offset (svm_fifo_t * f,
Dave Barach68b0fb02017-02-28 15:15:56 -0500611 u32 offset,
612 u32 required_bytes, u8 * copy_from_here)
613{
Florin Corasa5464812017-04-19 13:00:05 -0700614 return svm_fifo_enqueue_with_offset_internal (f, offset, required_bytes,
615 copy_from_here);
Dave Barach68b0fb02017-02-28 15:15:56 -0500616}
617
618
619static int
Florin Corasa5464812017-04-19 13:00:05 -0700620svm_fifo_dequeue_internal (svm_fifo_t * f, u32 max_bytes, u8 * copy_here)
Dave Barach68b0fb02017-02-28 15:15:56 -0500621{
622 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
623 u32 cursize, nitems;
624
Florin Coras3e350af2017-03-30 02:54:28 -0700625 /* read cursize, which can only increase while we're working */
626 cursize = svm_fifo_max_dequeue (f);
627 if (PREDICT_FALSE (cursize == 0))
Dave Barach68b0fb02017-02-28 15:15:56 -0500628 return -2; /* nothing in the fifo */
629
Dave Barach68b0fb02017-02-28 15:15:56 -0500630 nitems = f->nitems;
631
632 /* Number of bytes we're going to copy */
633 total_copy_bytes = (cursize < max_bytes) ? cursize : max_bytes;
634
635 if (PREDICT_TRUE (copy_here != 0))
636 {
637 /* Number of bytes in first copy segment */
638 first_copy_bytes = ((nitems - f->head) < total_copy_bytes)
639 ? (nitems - f->head) : total_copy_bytes;
640 clib_memcpy (copy_here, &f->data[f->head], first_copy_bytes);
641 f->head += first_copy_bytes;
642 f->head = (f->head == nitems) ? 0 : f->head;
643
644 /* Number of bytes in second copy segment, if any */
645 second_copy_bytes = total_copy_bytes - first_copy_bytes;
646 if (second_copy_bytes)
647 {
648 clib_memcpy (copy_here + first_copy_bytes,
649 &f->data[f->head], second_copy_bytes);
650 f->head += second_copy_bytes;
651 f->head = (f->head == nitems) ? 0 : f->head;
652 }
653 }
654 else
655 {
Dave Barach2c25a622017-06-26 11:35:07 -0400656 ASSERT (0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500657 /* Account for a zero-copy dequeue done elsewhere */
658 ASSERT (max_bytes <= cursize);
659 f->head += max_bytes;
660 f->head = f->head % nitems;
661 cursize -= max_bytes;
662 total_copy_bytes = max_bytes;
663 }
664
Dave Barach2c25a622017-06-26 11:35:07 -0400665 ASSERT (f->head <= nitems);
666 ASSERT (cursize >= total_copy_bytes);
Dave Barach68b0fb02017-02-28 15:15:56 -0500667 __sync_fetch_and_sub (&f->cursize, total_copy_bytes);
668
669 return (total_copy_bytes);
670}
671
Florin Corasf6359c82017-06-19 12:26:09 -0400672static int
673svm_fifo_dequeue_nowait_ma (svm_fifo_t * f, u32 max_bytes, u8 * copy_here)
Dave Barach68b0fb02017-02-28 15:15:56 -0500674{
Florin Corasa5464812017-04-19 13:00:05 -0700675 return svm_fifo_dequeue_internal (f, max_bytes, copy_here);
Dave Barach68b0fb02017-02-28 15:15:56 -0500676}
677
Florin Corasf6359c82017-06-19 12:26:09 -0400678#define SVM_FIFO_DEQUEUE_CLONE_TEMPLATE(arch, fn, tgt) \
679 uword \
680 __attribute__ ((flatten)) \
681 __attribute__ ((target (tgt))) \
682 CLIB_CPU_OPTIMIZED \
683 fn ## _ ## arch ( svm_fifo_t * f, u32 max_bytes, \
684 u8 * copy_here) \
685 { return fn (f, max_bytes, copy_here);}
686
687foreach_march_variant (SVM_FIFO_DEQUEUE_CLONE_TEMPLATE,
688 svm_fifo_dequeue_nowait_ma);
689CLIB_MULTIARCH_SELECT_FN (svm_fifo_dequeue_nowait_ma);
690
Dave Barach68b0fb02017-02-28 15:15:56 -0500691int
Florin Corasf6359c82017-06-19 12:26:09 -0400692svm_fifo_dequeue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_here)
693{
694#if CLIB_DEBUG > 0
695 return svm_fifo_dequeue_nowait_ma (f, max_bytes, copy_here);
696#else
697 static int (*fp) (svm_fifo_t *, u32, u8 *);
698
699 if (PREDICT_FALSE (fp == 0))
700 fp = (void *) svm_fifo_dequeue_nowait_ma_multiarch_select ();
701
702 return (*fp) (f, max_bytes, copy_here);
703#endif
704}
705
706static int
707svm_fifo_peek_ma (svm_fifo_t * f, u32 relative_offset, u32 max_bytes,
708 u8 * copy_here)
Dave Barach68b0fb02017-02-28 15:15:56 -0500709{
710 u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
Florin Coras6792ec02017-03-13 03:49:51 -0700711 u32 cursize, nitems, real_head;
Dave Barach68b0fb02017-02-28 15:15:56 -0500712
Florin Coras3e350af2017-03-30 02:54:28 -0700713 /* read cursize, which can only increase while we're working */
714 cursize = svm_fifo_max_dequeue (f);
Florin Coras93992a92017-05-24 18:03:56 -0700715 if (PREDICT_FALSE (cursize < relative_offset))
Dave Barach68b0fb02017-02-28 15:15:56 -0500716 return -2; /* nothing in the fifo */
717
Dave Barach68b0fb02017-02-28 15:15:56 -0500718 nitems = f->nitems;
Florin Corasb59a7052017-04-18 22:07:29 -0700719 real_head = f->head + relative_offset;
Florin Coras6792ec02017-03-13 03:49:51 -0700720 real_head = real_head >= nitems ? real_head - nitems : real_head;
Dave Barach68b0fb02017-02-28 15:15:56 -0500721
722 /* Number of bytes we're going to copy */
Florin Coras93992a92017-05-24 18:03:56 -0700723 total_copy_bytes = (cursize - relative_offset < max_bytes) ?
724 cursize - relative_offset : max_bytes;
Dave Barach68b0fb02017-02-28 15:15:56 -0500725
726 if (PREDICT_TRUE (copy_here != 0))
727 {
728 /* Number of bytes in first copy segment */
729 first_copy_bytes =
Florin Coras6792ec02017-03-13 03:49:51 -0700730 ((nitems - real_head) < total_copy_bytes) ?
731 (nitems - real_head) : total_copy_bytes;
732 clib_memcpy (copy_here, &f->data[real_head], first_copy_bytes);
Dave Barach68b0fb02017-02-28 15:15:56 -0500733
734 /* Number of bytes in second copy segment, if any */
735 second_copy_bytes = total_copy_bytes - first_copy_bytes;
736 if (second_copy_bytes)
737 {
738 clib_memcpy (copy_here + first_copy_bytes, &f->data[0],
739 second_copy_bytes);
740 }
741 }
742 return total_copy_bytes;
743}
744
Florin Corasf6359c82017-06-19 12:26:09 -0400745#define SVM_FIFO_PEEK_CLONE_TEMPLATE(arch, fn, tgt) \
746 uword \
747 __attribute__ ((flatten)) \
748 __attribute__ ((target (tgt))) \
749 CLIB_CPU_OPTIMIZED \
750 fn ## _ ## arch ( svm_fifo_t * f, u32 relative_offset, u32 max_bytes, \
751 u8 * copy_here) \
752 { return fn (f, relative_offset, max_bytes, copy_here);}
753
754foreach_march_variant (SVM_FIFO_PEEK_CLONE_TEMPLATE, svm_fifo_peek_ma);
755CLIB_MULTIARCH_SELECT_FN (svm_fifo_peek_ma);
756
757int
758svm_fifo_peek (svm_fifo_t * f, u32 relative_offset, u32 max_bytes,
759 u8 * copy_here)
760{
761#if CLIB_DEBUG > 0
762 return svm_fifo_peek_ma (f, relative_offset, max_bytes, copy_here);
763#else
764 static int (*fp) (svm_fifo_t *, u32, u32, u8 *);
765
766 if (PREDICT_FALSE (fp == 0))
767 fp = (void *) svm_fifo_peek_ma_multiarch_select ();
768
769 return (*fp) (f, relative_offset, max_bytes, copy_here);
770#endif
771}
772
Dave Barach68b0fb02017-02-28 15:15:56 -0500773int
Florin Corasa5464812017-04-19 13:00:05 -0700774svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes)
Dave Barach68b0fb02017-02-28 15:15:56 -0500775{
776 u32 total_drop_bytes, first_drop_bytes, second_drop_bytes;
777 u32 cursize, nitems;
778
Florin Coras3e350af2017-03-30 02:54:28 -0700779 /* read cursize, which can only increase while we're working */
780 cursize = svm_fifo_max_dequeue (f);
781 if (PREDICT_FALSE (cursize == 0))
Dave Barach68b0fb02017-02-28 15:15:56 -0500782 return -2; /* nothing in the fifo */
783
Dave Barach68b0fb02017-02-28 15:15:56 -0500784 nitems = f->nitems;
785
786 /* Number of bytes we're going to drop */
787 total_drop_bytes = (cursize < max_bytes) ? cursize : max_bytes;
788
Florin Coras3eb50622017-07-13 01:24:57 -0400789 svm_fifo_trace_add (f, f->tail, total_drop_bytes, 3);
790
Dave Barach68b0fb02017-02-28 15:15:56 -0500791 /* Number of bytes in first copy segment */
792 first_drop_bytes =
793 ((nitems - f->head) < total_drop_bytes) ?
794 (nitems - f->head) : total_drop_bytes;
795 f->head += first_drop_bytes;
796 f->head = (f->head == nitems) ? 0 : f->head;
797
798 /* Number of bytes in second drop segment, if any */
799 second_drop_bytes = total_drop_bytes - first_drop_bytes;
800 if (second_drop_bytes)
801 {
802 f->head += second_drop_bytes;
803 f->head = (f->head == nitems) ? 0 : f->head;
804 }
805
Dave Barach2c25a622017-06-26 11:35:07 -0400806 ASSERT (f->head <= nitems);
807 ASSERT (cursize >= total_drop_bytes);
Dave Barach68b0fb02017-02-28 15:15:56 -0500808 __sync_fetch_and_sub (&f->cursize, total_drop_bytes);
809
810 return total_drop_bytes;
811}
812
Dave Barach1f75cfd2017-04-14 16:46:44 -0400813u32
814svm_fifo_number_ooo_segments (svm_fifo_t * f)
815{
816 return pool_elts (f->ooo_segments);
817}
818
819ooo_segment_t *
820svm_fifo_first_ooo_segment (svm_fifo_t * f)
821{
822 return pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
823}
824
Florin Corasc28764f2017-04-26 00:08:42 -0700825/**
826 * Set fifo pointers to requested offset
827 */
828void
829svm_fifo_init_pointers (svm_fifo_t * f, u32 pointer)
830{
831 f->head = f->tail = pointer % f->nitems;
832}
833
Dave Barach68b0fb02017-02-28 15:15:56 -0500834/*
835 * fd.io coding-style-patch-verification: ON
836 *
837 * Local Variables:
838 * eval: (c-set-style "gnu")
839 * End:
840 */