blob: de1997452d5267613a32075cde764b5060f86044 [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
Florin Corasc5df8c72019-04-08 07:42:30 -07002 * Copyright (c) 2016-2019 Cisco and/or its affiliates.
Sirshak Das28aa5392019-02-05 01:33:33 -06003 * Copyright (c) 2019 Arm Limited
4 * Copyright (c) 2010-2017 Intel Corporation and/or its affiliates.
5 * Copyright (c) 2007-2009 Kip Macy kmacy@freebsd.org
6 * Inspired from DPDK rte_ring.h (SPSC only) (derived from freebsd bufring.h).
Dave Barach68b0fb02017-02-28 15:15:56 -05007 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at:
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
Florin Coras6792ec02017-03-13 03:49:51 -070020#include <svm/svm_fifo.h>
Florin Corasf6359c82017-06-19 12:26:09 -040021#include <vppinfra/cpu.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050022
Florin Corasf03a59a2017-06-09 21:07:32 -070023static inline u8
Sirshak Das28aa5392019-02-05 01:33:33 -060024position_lt (svm_fifo_t * f, u32 a, u32 b, u32 tail)
Florin Corasf03a59a2017-06-09 21:07:32 -070025{
Sirshak Das28aa5392019-02-05 01:33:33 -060026 return (ooo_segment_distance_from_tail (f, a, tail)
27 < ooo_segment_distance_from_tail (f, b, tail));
Florin Corasf03a59a2017-06-09 21:07:32 -070028}
29
30static inline u8
Sirshak Das28aa5392019-02-05 01:33:33 -060031position_leq (svm_fifo_t * f, u32 a, u32 b, u32 tail)
Florin Corasf03a59a2017-06-09 21:07:32 -070032{
Sirshak Das28aa5392019-02-05 01:33:33 -060033 return (ooo_segment_distance_from_tail (f, a, tail)
34 <= ooo_segment_distance_from_tail (f, b, tail));
Florin Corasf03a59a2017-06-09 21:07:32 -070035}
36
37static inline u8
Sirshak Das28aa5392019-02-05 01:33:33 -060038position_gt (svm_fifo_t * f, u32 a, u32 b, u32 tail)
Florin Corasf03a59a2017-06-09 21:07:32 -070039{
Sirshak Das28aa5392019-02-05 01:33:33 -060040 return (ooo_segment_distance_from_tail (f, a, tail)
41 > ooo_segment_distance_from_tail (f, b, tail));
Florin Corasf03a59a2017-06-09 21:07:32 -070042}
43
44static inline u32
Sirshak Das28aa5392019-02-05 01:33:33 -060045position_diff (svm_fifo_t * f, u32 posa, u32 posb, u32 tail)
Florin Corasf03a59a2017-06-09 21:07:32 -070046{
Sirshak Das28aa5392019-02-05 01:33:33 -060047 return ooo_segment_distance_from_tail (f, posa, tail)
48 - ooo_segment_distance_from_tail (f, posb, tail);
Florin Corasf03a59a2017-06-09 21:07:32 -070049}
50
51static inline u32
52ooo_segment_end_pos (svm_fifo_t * f, ooo_segment_t * s)
53{
Sirshak Das28aa5392019-02-05 01:33:33 -060054 return s->start + s->length;
Florin Corasf03a59a2017-06-09 21:07:32 -070055}
Dave Barach1f75cfd2017-04-14 16:46:44 -040056
Florin Coras3aa7af32018-06-29 08:44:31 -070057#ifndef CLIB_MARCH_VARIANT
58
Dave Barach1f75cfd2017-04-14 16:46:44 -040059u8 *
60format_ooo_segment (u8 * s, va_list * args)
61{
Florin Coras1f152cd2017-08-18 19:28:03 -070062 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
Dave Barach1f75cfd2017-04-14 16:46:44 -040063 ooo_segment_t *seg = va_arg (*args, ooo_segment_t *);
Sirshak Das28aa5392019-02-05 01:33:33 -060064 u32 normalized_start = (seg->start + f->nitems - f->tail) % f->size;
Florin Coras1f152cd2017-08-18 19:28:03 -070065 s = format (s, "[%u, %u], len %u, next %d, prev %d", normalized_start,
Sirshak Das28aa5392019-02-05 01:33:33 -060066 (normalized_start + seg->length) % f->size, seg->length,
Florin Coras1f152cd2017-08-18 19:28:03 -070067 seg->next, seg->prev);
Dave Barach1f75cfd2017-04-14 16:46:44 -040068 return s;
69}
70
71u8 *
Florin Coras3eb50622017-07-13 01:24:57 -040072svm_fifo_dump_trace (u8 * s, svm_fifo_t * f)
73{
74#if SVM_FIFO_TRACE
75 svm_fifo_trace_elem_t *seg = 0;
76 int i = 0;
77
78 if (f->trace)
79 {
80 vec_foreach (seg, f->trace)
81 {
82 s = format (s, "{%u, %u, %u}, ", seg->offset, seg->len, seg->action);
83 i++;
84 if (i % 5 == 0)
85 s = format (s, "\n");
86 }
87 s = format (s, "\n");
88 }
89 return s;
90#else
91 return 0;
92#endif
93}
94
95u8 *
96svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose)
97{
98 int i, trace_len;
99 u8 *data = 0;
100 svm_fifo_trace_elem_t *trace;
101 u32 offset;
102 svm_fifo_t *dummy_fifo;
103
104 if (!f)
105 return s;
106
107#if SVM_FIFO_TRACE
108 trace = f->trace;
109 trace_len = vec_len (trace);
110#else
111 trace = 0;
112 trace_len = 0;
113#endif
114
Sirshak Das28aa5392019-02-05 01:33:33 -0600115 dummy_fifo = svm_fifo_create (f->size);
Florin Coras0a846802019-04-09 18:29:14 -0700116 clib_memset (f->head_chunk->data, 0xFF, f->nitems);
Florin Coras3eb50622017-07-13 01:24:57 -0400117 vec_validate (data, f->nitems);
118 for (i = 0; i < vec_len (data); i++)
119 data[i] = i;
120
121 for (i = 0; i < trace_len; i++)
122 {
123 offset = trace[i].offset;
124 if (trace[i].action == 1)
125 {
126 if (verbose)
127 s = format (s, "adding [%u, %u]:", trace[i].offset,
Sirshak Das28aa5392019-02-05 01:33:33 -0600128 (trace[i].offset + trace[i].len) % dummy_fifo->size);
Florin Coras3eb50622017-07-13 01:24:57 -0400129 svm_fifo_enqueue_with_offset (dummy_fifo, trace[i].offset,
130 trace[i].len, &data[offset]);
131 }
132 else if (trace[i].action == 2)
133 {
134 if (verbose)
135 s = format (s, "adding [%u, %u]:", 0, trace[i].len);
136 svm_fifo_enqueue_nowait (dummy_fifo, trace[i].len, &data[offset]);
137 }
138 else if (!no_read)
139 {
140 if (verbose)
141 s = format (s, "read: %u", trace[i].len);
142 svm_fifo_dequeue_drop (dummy_fifo, trace[i].len);
143 }
144 if (verbose)
145 s = format (s, "%U", format_svm_fifo, dummy_fifo, 1);
146 }
147
148 s = format (s, "result: %U", format_svm_fifo, dummy_fifo, 1);
149
150 return s;
151}
152
153u8 *
Dave Barach1f75cfd2017-04-14 16:46:44 -0400154format_ooo_list (u8 * s, va_list * args)
155{
156 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
Florin Corasde9a8492018-10-24 22:18:58 -0700157 u32 indent = va_arg (*args, u32);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400158 u32 ooo_segment_index = f->ooos_list_head;
159 ooo_segment_t *seg;
160
161 while (ooo_segment_index != OOO_SEGMENT_INVALID_INDEX)
162 {
163 seg = pool_elt_at_index (f->ooo_segments, ooo_segment_index);
Florin Corasde9a8492018-10-24 22:18:58 -0700164 s = format (s, "%U%U\n", format_white_space, indent, format_ooo_segment,
165 f, seg);
Dave Barach1f75cfd2017-04-14 16:46:44 -0400166 ooo_segment_index = seg->next;
167 }
Florin Coras3eb50622017-07-13 01:24:57 -0400168
Dave Barach1f75cfd2017-04-14 16:46:44 -0400169 return s;
170}
171
Florin Corasb59a7052017-04-18 22:07:29 -0700172u8 *
173format_svm_fifo (u8 * s, va_list * args)
174{
175 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
176 int verbose = va_arg (*args, int);
Florin Corasde9a8492018-10-24 22:18:58 -0700177 u32 indent;
Florin Corasb59a7052017-04-18 22:07:29 -0700178
Florin Coras7fb0fe12018-04-09 09:24:52 -0700179 if (!s)
180 return s;
181
Florin Corasde9a8492018-10-24 22:18:58 -0700182 indent = format_get_indent (s);
Florin Corasb59a7052017-04-18 22:07:29 -0700183 s = format (s, "cursize %u nitems %u has_event %d\n",
Sirshak Das28aa5392019-02-05 01:33:33 -0600184 svm_fifo_max_dequeue (f), f->nitems, f->has_event);
185 s = format (s, "%Uhead %u tail %u segment manager %u\n", format_white_space,
186 indent, (f->head % f->size), (f->tail % f->size),
187 f->segment_manager);
Florin Corasb59a7052017-04-18 22:07:29 -0700188
189 if (verbose > 1)
Florin Corasde9a8492018-10-24 22:18:58 -0700190 s = format (s, "%Uvpp session %d thread %d app session %d thread %d\n",
191 format_white_space, indent, f->master_session_index,
192 f->master_thread_index, f->client_session_index,
193 f->client_thread_index);
Florin Corasb59a7052017-04-18 22:07:29 -0700194
195 if (verbose)
196 {
Florin Corasde9a8492018-10-24 22:18:58 -0700197 s = format (s, "%Uooo pool %d active elts newest %u\n",
198 format_white_space, indent, pool_elts (f->ooo_segments),
199 f->ooos_newest);
Florin Corasbb292f42017-05-19 09:49:19 -0700200 if (svm_fifo_has_ooo_data (f))
Florin Corasde9a8492018-10-24 22:18:58 -0700201 s = format (s, " %U", format_ooo_list, f, indent, verbose);
Florin Corasb59a7052017-04-18 22:07:29 -0700202 }
203 return s;
204}
205
Florin Coras0a846802019-04-09 18:29:14 -0700206void
207svm_fifo_init (svm_fifo_t * f, u32 size)
208{
209 f->size = size;
210 /*
211 * usable size of the fifo set to rounded_data_size - 1
212 * to differentiate between free fifo and empty fifo.
213 */
214 f->nitems = f->size - 1;
215 f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
216 f->ct_session_index = SVM_FIFO_INVALID_SESSION_INDEX;
217 f->segment_index = SVM_FIFO_INVALID_INDEX;
218 f->refcnt = 1;
219 f->head_chunk = &f->default_chunk;
220 f->tail_chunk = &f->default_chunk;
221 f->default_chunk.next = &f->default_chunk;
222 f->default_chunk.start_byte = 0;
223 f->default_chunk.length = f->size;
224}
225
Dave Barach68b0fb02017-02-28 15:15:56 -0500226/** create an svm fifo, in the current heap. Fails vs blow up the process */
227svm_fifo_t *
228svm_fifo_create (u32 data_size_in_bytes)
229{
230 svm_fifo_t *f;
Dave Barach818eb542017-08-02 13:56:13 -0400231 u32 rounded_data_size;
Dave Barach68b0fb02017-02-28 15:15:56 -0500232
Dave Barach818eb542017-08-02 13:56:13 -0400233 /* always round fifo data size to the next highest power-of-two */
234 rounded_data_size = (1 << (max_log2 (data_size_in_bytes)));
235 f = clib_mem_alloc_aligned_or_null (sizeof (*f) + rounded_data_size,
Dave Barach68b0fb02017-02-28 15:15:56 -0500236 CLIB_CACHE_LINE_BYTES);
237 if (f == 0)
238 return 0;
239
Dave Barachb7b92992018-10-17 10:38:51 -0400240 clib_memset (f, 0, sizeof (*f));
Florin Coras0a846802019-04-09 18:29:14 -0700241 svm_fifo_init (f, data_size_in_bytes);
242 return f;
Dave Barach68b0fb02017-02-28 15:15:56 -0500243}
244
Florin Coras6cf30ad2017-04-04 23:08:23 -0700245void
246svm_fifo_free (svm_fifo_t * f)
247{
Dave Barach52851e62017-08-07 09:35:25 -0400248 ASSERT (f->refcnt > 0);
249
250 if (--f->refcnt == 0)
251 {
252 pool_free (f->ooo_segments);
253 clib_mem_free (f);
254 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700255}
Florin Coras3aa7af32018-06-29 08:44:31 -0700256#endif
Florin Coras6cf30ad2017-04-04 23:08:23 -0700257
Dave Barach68b0fb02017-02-28 15:15:56 -0500258always_inline ooo_segment_t *
259ooo_segment_new (svm_fifo_t * f, u32 start, u32 length)
260{
261 ooo_segment_t *s;
262
263 pool_get (f->ooo_segments, s);
264
Dave Barach1f75cfd2017-04-14 16:46:44 -0400265 s->start = start;
Dave Barach68b0fb02017-02-28 15:15:56 -0500266 s->length = length;
267
268 s->prev = s->next = OOO_SEGMENT_INVALID_INDEX;
269
270 return s;
271}
272
273always_inline void
274ooo_segment_del (svm_fifo_t * f, u32 index)
275{
276 ooo_segment_t *cur, *prev = 0, *next = 0;
277 cur = pool_elt_at_index (f->ooo_segments, index);
278
279 if (cur->next != OOO_SEGMENT_INVALID_INDEX)
280 {
281 next = pool_elt_at_index (f->ooo_segments, cur->next);
282 next->prev = cur->prev;
283 }
284
285 if (cur->prev != OOO_SEGMENT_INVALID_INDEX)
286 {
287 prev = pool_elt_at_index (f->ooo_segments, cur->prev);
288 prev->next = cur->next;
289 }
290 else
291 {
292 f->ooos_list_head = cur->next;
293 }
294
295 pool_put (f->ooo_segments, cur);
296}
297
298/**
299 * Add segment to fifo's out-of-order segment list. Takes care of merging
300 * adjacent segments and removing overlapping ones.
301 */
302static void
Sirshak Das28aa5392019-02-05 01:33:33 -0600303ooo_segment_add (svm_fifo_t * f, u32 offset, u32 head, u32 tail, u32 length)
Dave Barach68b0fb02017-02-28 15:15:56 -0500304{
305 ooo_segment_t *s, *new_s, *prev, *next, *it;
Florin Corasf03a59a2017-06-09 21:07:32 -0700306 u32 new_index, s_end_pos, s_index;
Sirshak Das28aa5392019-02-05 01:33:33 -0600307 u32 offset_pos, offset_end_pos;
Dave Barach68b0fb02017-02-28 15:15:56 -0500308
Florin Coras0a846802019-04-09 18:29:14 -0700309 ASSERT (offset + length <= ooo_segment_distance_from_tail (f, head, tail)
310 || head == tail);
311
Sirshak Das28aa5392019-02-05 01:33:33 -0600312 offset_pos = tail + offset;
313 offset_end_pos = tail + offset + length;
Florin Corasf03a59a2017-06-09 21:07:32 -0700314
315 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
Dave Barach68b0fb02017-02-28 15:15:56 -0500316
317 if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX)
318 {
Sirshak Das28aa5392019-02-05 01:33:33 -0600319 s = ooo_segment_new (f, offset_pos, length);
Dave Barach68b0fb02017-02-28 15:15:56 -0500320 f->ooos_list_head = s - f->ooo_segments;
321 f->ooos_newest = f->ooos_list_head;
322 return;
323 }
324
325 /* Find first segment that starts after new segment */
326 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
327 while (s->next != OOO_SEGMENT_INVALID_INDEX
Sirshak Das28aa5392019-02-05 01:33:33 -0600328 && position_lt (f, s->start, offset_pos, tail))
Dave Barach68b0fb02017-02-28 15:15:56 -0500329 s = pool_elt_at_index (f->ooo_segments, s->next);
330
Florin Corasc28764f2017-04-26 00:08:42 -0700331 /* If we have a previous and we overlap it, use it as starting point */
332 prev = ooo_segment_get_prev (f, s);
Florin Corasf03a59a2017-06-09 21:07:32 -0700333 if (prev
Sirshak Das28aa5392019-02-05 01:33:33 -0600334 && position_leq (f, offset_pos, ooo_segment_end_pos (f, prev), tail))
Florin Corasc28764f2017-04-26 00:08:42 -0700335 {
336 s = prev;
Florin Corasf03a59a2017-06-09 21:07:32 -0700337 s_end_pos = ooo_segment_end_pos (f, s);
Dave Barach2c25a622017-06-26 11:35:07 -0400338
Sirshak Das28aa5392019-02-05 01:33:33 -0600339 /* Since we have previous, offset start position cannot be smaller
Florin Coras3eb50622017-07-13 01:24:57 -0400340 * than prev->start. Check tail */
Sirshak Das28aa5392019-02-05 01:33:33 -0600341 ASSERT (position_lt (f, s->start, offset_pos, tail));
Dave Barach2c25a622017-06-26 11:35:07 -0400342 goto check_tail;
Florin Corasc28764f2017-04-26 00:08:42 -0700343 }
344
Dave Barach68b0fb02017-02-28 15:15:56 -0500345 s_index = s - f->ooo_segments;
Florin Corasf03a59a2017-06-09 21:07:32 -0700346 s_end_pos = ooo_segment_end_pos (f, s);
Dave Barach68b0fb02017-02-28 15:15:56 -0500347
348 /* No overlap, add before current segment */
Sirshak Das28aa5392019-02-05 01:33:33 -0600349 if (position_lt (f, offset_end_pos, s->start, tail))
Dave Barach68b0fb02017-02-28 15:15:56 -0500350 {
Sirshak Das28aa5392019-02-05 01:33:33 -0600351 new_s = ooo_segment_new (f, offset_pos, length);
Dave Barach68b0fb02017-02-28 15:15:56 -0500352 new_index = new_s - f->ooo_segments;
353
354 /* Pool might've moved, get segment again */
355 s = pool_elt_at_index (f->ooo_segments, s_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500356 if (s->prev != OOO_SEGMENT_INVALID_INDEX)
357 {
358 new_s->prev = s->prev;
Dave Barach68b0fb02017-02-28 15:15:56 -0500359 prev = pool_elt_at_index (f->ooo_segments, new_s->prev);
360 prev->next = new_index;
361 }
362 else
363 {
364 /* New head */
365 f->ooos_list_head = new_index;
366 }
367
Florin Corasf03a59a2017-06-09 21:07:32 -0700368 new_s->next = s_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500369 s->prev = new_index;
370 f->ooos_newest = new_index;
371 return;
372 }
373 /* No overlap, add after current segment */
Sirshak Das28aa5392019-02-05 01:33:33 -0600374 else if (position_gt (f, offset_pos, s_end_pos, tail))
Dave Barach68b0fb02017-02-28 15:15:56 -0500375 {
Sirshak Das28aa5392019-02-05 01:33:33 -0600376 new_s = ooo_segment_new (f, offset_pos, length);
Dave Barach68b0fb02017-02-28 15:15:56 -0500377 new_index = new_s - f->ooo_segments;
378
379 /* Pool might've moved, get segment again */
380 s = pool_elt_at_index (f->ooo_segments, s_index);
381
Florin Coras3eb50622017-07-13 01:24:57 -0400382 /* Needs to be last */
Florin Corasf03a59a2017-06-09 21:07:32 -0700383 ASSERT (s->next == OOO_SEGMENT_INVALID_INDEX);
Dave Barach68b0fb02017-02-28 15:15:56 -0500384
Florin Corasf03a59a2017-06-09 21:07:32 -0700385 new_s->prev = s_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500386 s->next = new_index;
387 f->ooos_newest = new_index;
388
389 return;
390 }
391
392 /*
393 * Merge needed
394 */
395
396 /* Merge at head */
Sirshak Das28aa5392019-02-05 01:33:33 -0600397 if (position_lt (f, offset_pos, s->start, tail))
Dave Barach68b0fb02017-02-28 15:15:56 -0500398 {
Sirshak Das28aa5392019-02-05 01:33:33 -0600399 s->start = offset_pos;
400 s->length = position_diff (f, s_end_pos, s->start, tail);
Florin Coras3eb50622017-07-13 01:24:57 -0400401 f->ooos_newest = s - f->ooo_segments;
Dave Barach68b0fb02017-02-28 15:15:56 -0500402 }
403
Dave Barach2c25a622017-06-26 11:35:07 -0400404check_tail:
Florin Coras3eb50622017-07-13 01:24:57 -0400405
406 /* Overlapping tail */
Sirshak Das28aa5392019-02-05 01:33:33 -0600407 if (position_gt (f, offset_end_pos, s_end_pos, tail))
Florin Corasc28764f2017-04-26 00:08:42 -0700408 {
Sirshak Das28aa5392019-02-05 01:33:33 -0600409 s->length = position_diff (f, offset_end_pos, s->start, tail);
Florin Coras3eb50622017-07-13 01:24:57 -0400410
411 /* Remove the completely overlapped segments in the tail */
412 it = ooo_segment_next (f, s);
Florin Corasf03a59a2017-06-09 21:07:32 -0700413 while (it && position_leq (f, ooo_segment_end_pos (f, it),
Sirshak Das28aa5392019-02-05 01:33:33 -0600414 offset_end_pos, tail))
Florin Corasc28764f2017-04-26 00:08:42 -0700415 {
Florin Coras3eb50622017-07-13 01:24:57 -0400416 next = ooo_segment_next (f, it);
Florin Corasc28764f2017-04-26 00:08:42 -0700417 ooo_segment_del (f, it - f->ooo_segments);
418 it = next;
419 }
420
421 /* If partial overlap with last, merge */
Sirshak Das28aa5392019-02-05 01:33:33 -0600422 if (it && position_leq (f, it->start, offset_end_pos, tail))
Florin Corasc28764f2017-04-26 00:08:42 -0700423 {
Florin Coras3eb50622017-07-13 01:24:57 -0400424 s->length = position_diff (f, ooo_segment_end_pos (f, it),
Sirshak Das28aa5392019-02-05 01:33:33 -0600425 s->start, tail);
Florin Corasc28764f2017-04-26 00:08:42 -0700426 ooo_segment_del (f, it - f->ooo_segments);
427 }
Florin Coras3eb50622017-07-13 01:24:57 -0400428 f->ooos_newest = s - f->ooo_segments;
Florin Corasc28764f2017-04-26 00:08:42 -0700429 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500430}
431
432/**
433 * Removes segments that can now be enqueued because the fifo's tail has
434 * advanced. Returns the number of bytes added to tail.
435 */
436static int
Sirshak Das28aa5392019-02-05 01:33:33 -0600437ooo_segment_try_collect (svm_fifo_t * f, u32 n_bytes_enqueued, u32 * tail)
Dave Barach68b0fb02017-02-28 15:15:56 -0500438{
439 ooo_segment_t *s;
Florin Corasf03a59a2017-06-09 21:07:32 -0700440 u32 index, bytes = 0;
441 i32 diff;
Dave Barach68b0fb02017-02-28 15:15:56 -0500442
443 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
Sirshak Das28aa5392019-02-05 01:33:33 -0600444 diff = ooo_segment_distance_to_tail (f, s->start, *tail);
Dave Barach68b0fb02017-02-28 15:15:56 -0500445
Dave Barach2c25a622017-06-26 11:35:07 -0400446 ASSERT (diff != n_bytes_enqueued);
Florin Corasc28764f2017-04-26 00:08:42 -0700447
Florin Corasf03a59a2017-06-09 21:07:32 -0700448 if (diff > n_bytes_enqueued)
Florin Corasb59a7052017-04-18 22:07:29 -0700449 return 0;
450
Dave Barach68b0fb02017-02-28 15:15:56 -0500451 /* If last tail update overlaps one/multiple ooo segments, remove them */
Florin Corasf03a59a2017-06-09 21:07:32 -0700452 while (0 <= diff && diff < n_bytes_enqueued)
Dave Barach68b0fb02017-02-28 15:15:56 -0500453 {
Florin Corasb59a7052017-04-18 22:07:29 -0700454 index = s - f->ooo_segments;
455
456 /* Segment end is beyond the tail. Advance tail and remove segment */
Florin Corasf03a59a2017-06-09 21:07:32 -0700457 if (s->length > diff)
Dave Barach68b0fb02017-02-28 15:15:56 -0500458 {
Florin Corasb59a7052017-04-18 22:07:29 -0700459 bytes = s->length - diff;
Sirshak Das28aa5392019-02-05 01:33:33 -0600460 *tail = *tail + bytes;
Florin Corasb59a7052017-04-18 22:07:29 -0700461 ooo_segment_del (f, index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500462 break;
463 }
Florin Corasb59a7052017-04-18 22:07:29 -0700464
Dave Barach68b0fb02017-02-28 15:15:56 -0500465 /* If we have next go on */
Florin Corasb59a7052017-04-18 22:07:29 -0700466 if (s->next != OOO_SEGMENT_INVALID_INDEX)
Dave Barach68b0fb02017-02-28 15:15:56 -0500467 {
Dave Barach68b0fb02017-02-28 15:15:56 -0500468 s = pool_elt_at_index (f->ooo_segments, s->next);
Sirshak Das28aa5392019-02-05 01:33:33 -0600469 diff = ooo_segment_distance_to_tail (f, s->start, *tail);
Dave Barach68b0fb02017-02-28 15:15:56 -0500470 ooo_segment_del (f, index);
471 }
472 /* End of search */
473 else
474 {
Florin Corasb59a7052017-04-18 22:07:29 -0700475 ooo_segment_del (f, index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500476 break;
477 }
478 }
479
Chris Lukeab7b8d92017-09-07 07:40:13 -0400480 ASSERT (bytes <= f->nitems);
Dave Barach68b0fb02017-02-28 15:15:56 -0500481 return bytes;
482}
483
Florin Coras0a846802019-04-09 18:29:14 -0700484CLIB_MARCH_FN (svm_fifo_enqueue_nowait, int, svm_fifo_t * f, u32 len,
485 const u8 * src)
Dave Barach68b0fb02017-02-28 15:15:56 -0500486{
Florin Coras0a846802019-04-09 18:29:14 -0700487 u32 n_chunk, to_copy, tail, head, free_count, tail_idx;
488 svm_fifo_chunk_t *c;
Dave Barach68b0fb02017-02-28 15:15:56 -0500489
Sirshak Das28aa5392019-02-05 01:33:33 -0600490 f_load_head_tail_prod (f, &head, &tail);
491
492 /* free space in fifo can only increase during enqueue: SPSC */
493 free_count = f_free_count (f, head, tail);
494
Florin Corasf03a59a2017-06-09 21:07:32 -0700495 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
Florin Coras3e350af2017-03-30 02:54:28 -0700496
Sirshak Das28aa5392019-02-05 01:33:33 -0600497 if (PREDICT_FALSE (free_count == 0))
Florin Coras7fb0fe12018-04-09 09:24:52 -0700498 return SVM_FIFO_FULL;
Dave Barach68b0fb02017-02-28 15:15:56 -0500499
Sirshak Das28aa5392019-02-05 01:33:33 -0600500 /* number of bytes we're going to copy */
Florin Coras0a846802019-04-09 18:29:14 -0700501 to_copy = len = clib_min (free_count, len);
Dave Barach68b0fb02017-02-28 15:15:56 -0500502
Florin Coras0a846802019-04-09 18:29:14 -0700503 c = f->tail_chunk;
Sirshak Das28aa5392019-02-05 01:33:33 -0600504 tail_idx = tail % f->size;
Florin Coras0a846802019-04-09 18:29:14 -0700505 ASSERT (tail_idx >= c->start_byte);
506 tail_idx -= c->start_byte;
507 n_chunk = c->length - tail_idx;
Dave Barach68b0fb02017-02-28 15:15:56 -0500508
Florin Coras0a846802019-04-09 18:29:14 -0700509 if (n_chunk < to_copy)
Dave Barach68b0fb02017-02-28 15:15:56 -0500510 {
Florin Coras0a846802019-04-09 18:29:14 -0700511 clib_memcpy_fast (&c->data[tail_idx], src, n_chunk);
512 while ((to_copy -= n_chunk))
Dave Barach68b0fb02017-02-28 15:15:56 -0500513 {
Florin Coras0a846802019-04-09 18:29:14 -0700514 c = c->next;
515 n_chunk = clib_min (c->length, to_copy);
516 clib_memcpy_fast (&c->data[0], src + (len - to_copy), n_chunk);
Dave Barach68b0fb02017-02-28 15:15:56 -0500517 }
Florin Coras0a846802019-04-09 18:29:14 -0700518 f->tail_chunk = c;
Dave Barach68b0fb02017-02-28 15:15:56 -0500519 }
520 else
521 {
Florin Coras0a846802019-04-09 18:29:14 -0700522 clib_memcpy_fast (&c->data[tail_idx], src, to_copy);
Dave Barach68b0fb02017-02-28 15:15:56 -0500523 }
Florin Coras0a846802019-04-09 18:29:14 -0700524 tail += len;
Dave Barach68b0fb02017-02-28 15:15:56 -0500525
Florin Coras0a846802019-04-09 18:29:14 -0700526 svm_fifo_trace_add (f, head, n_total, 2);
Florin Coras3eb50622017-07-13 01:24:57 -0400527
Sirshak Das28aa5392019-02-05 01:33:33 -0600528 /* collect out-of-order segments */
Dave Barach68b0fb02017-02-28 15:15:56 -0500529 if (PREDICT_FALSE (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX))
Florin Coras0a846802019-04-09 18:29:14 -0700530 len += ooo_segment_try_collect (f, len, &tail);
Dave Barach68b0fb02017-02-28 15:15:56 -0500531
Florin Coras0a846802019-04-09 18:29:14 -0700532 ASSERT (len <= free_count);
Dave Barach68b0fb02017-02-28 15:15:56 -0500533
Sirshak Das28aa5392019-02-05 01:33:33 -0600534 /* store-rel: producer owned index (paired with load-acq in consumer) */
535 clib_atomic_store_rel_n (&f->tail, tail);
536
Florin Coras0a846802019-04-09 18:29:14 -0700537 return len;
Dave Barach68b0fb02017-02-28 15:15:56 -0500538}
539
Florin Coras3aa7af32018-06-29 08:44:31 -0700540#ifndef CLIB_MARCH_VARIANT
Dave Barach68b0fb02017-02-28 15:15:56 -0500541int
Florin Coras371ca502018-02-21 12:07:41 -0800542svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 max_bytes,
543 const u8 * copy_from_here)
Dave Barach68b0fb02017-02-28 15:15:56 -0500544{
Florin Coras983cc7d2018-09-18 23:11:55 -0700545 return CLIB_MARCH_FN_SELECT (svm_fifo_enqueue_nowait) (f, max_bytes,
546 copy_from_here);
Dave Barach68b0fb02017-02-28 15:15:56 -0500547}
Florin Coras3aa7af32018-06-29 08:44:31 -0700548#endif
Dave Barach68b0fb02017-02-28 15:15:56 -0500549
Florin Coras6792ec02017-03-13 03:49:51 -0700550/**
551 * Enqueue a future segment.
552 *
Dave Barach68b0fb02017-02-28 15:15:56 -0500553 * Two choices: either copies the entire segment, or copies nothing
554 * Returns 0 of the entire segment was copied
555 * Returns -1 if none of the segment was copied due to lack of space
556 */
Florin Coras983cc7d2018-09-18 23:11:55 -0700557CLIB_MARCH_FN (svm_fifo_enqueue_with_offset, int, svm_fifo_t * f,
Florin Coras0a846802019-04-09 18:29:14 -0700558 u32 offset, u32 len, u8 * src)
Dave Barach68b0fb02017-02-28 15:15:56 -0500559{
Florin Coras0a846802019-04-09 18:29:14 -0700560 u32 to_copy, n_chunk, tail, head, free_count, tail_offset_idx;
561 svm_fifo_chunk_t *c;
Sirshak Das28aa5392019-02-05 01:33:33 -0600562
563 f_load_head_tail_prod (f, &head, &tail);
564
565 /* free space in fifo can only increase during enqueue: SPSC */
566 free_count = f_free_count (f, head, tail);
567
568 /* will this request fit? */
Florin Coras0a846802019-04-09 18:29:14 -0700569 if ((len + offset) > free_count)
Sirshak Das28aa5392019-02-05 01:33:33 -0600570 return -1;
Florin Corasf03a59a2017-06-09 21:07:32 -0700571
572 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
Dave Barach68b0fb02017-02-28 15:15:56 -0500573
Florin Coras0a846802019-04-09 18:29:14 -0700574 ASSERT (len < f->nitems);
575 svm_fifo_trace_add (f, offset, len, 1);
Florin Corasf03a59a2017-06-09 21:07:32 -0700576
Florin Coras0a846802019-04-09 18:29:14 -0700577 ooo_segment_add (f, offset, head, tail, len);
Dave Barach68b0fb02017-02-28 15:15:56 -0500578
Florin Coras0a846802019-04-09 18:29:14 -0700579 c = f->tail_chunk;
580 tail_offset_idx = (tail + offset) % f->size;
581 tail_offset_idx -= c->start_byte;
582 n_chunk = c->length - tail_offset_idx;
583 to_copy = len;
Florin Coras3eb50622017-07-13 01:24:57 -0400584
Florin Coras0a846802019-04-09 18:29:14 -0700585 if (n_chunk < to_copy)
Dave Barach68b0fb02017-02-28 15:15:56 -0500586 {
Florin Coras0a846802019-04-09 18:29:14 -0700587 clib_memcpy_fast (&c->data[tail_offset_idx], src, n_chunk);
588 while ((to_copy -= n_chunk))
589 {
590 c = c->next;
591 n_chunk = clib_min (c->length, to_copy);
592 clib_memcpy_fast (&c->data[0], src + (len - to_copy), n_chunk);
593 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500594 }
Sirshak Das28aa5392019-02-05 01:33:33 -0600595 else
596 {
Florin Coras0a846802019-04-09 18:29:14 -0700597 clib_memcpy_fast (&c->data[tail_offset_idx], src, len);
Sirshak Das28aa5392019-02-05 01:33:33 -0600598 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500599
Sirshak Das28aa5392019-02-05 01:33:33 -0600600 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500601}
602
Florin Coras3aa7af32018-06-29 08:44:31 -0700603#ifndef CLIB_MARCH_VARIANT
Dave Barach68b0fb02017-02-28 15:15:56 -0500604
605int
Florin Coras3aa7af32018-06-29 08:44:31 -0700606svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 required_bytes,
607 u8 * copy_from_here)
Dave Barach68b0fb02017-02-28 15:15:56 -0500608{
Florin Coras983cc7d2018-09-18 23:11:55 -0700609 return CLIB_MARCH_FN_SELECT (svm_fifo_enqueue_with_offset) (f, offset,
610 required_bytes,
611 copy_from_here);
Dave Barach68b0fb02017-02-28 15:15:56 -0500612}
613
Florin Coras7fb0fe12018-04-09 09:24:52 -0700614void
615svm_fifo_overwrite_head (svm_fifo_t * f, u8 * data, u32 len)
616{
Florin Coras0a846802019-04-09 18:29:14 -0700617 u32 n_chunk;
Sirshak Das28aa5392019-02-05 01:33:33 -0600618 u32 head, tail, head_idx;
Florin Coras0a846802019-04-09 18:29:14 -0700619 svm_fifo_chunk_t *c;
620
621 ASSERT (len <= f->nitems);
Sirshak Das28aa5392019-02-05 01:33:33 -0600622
623 f_load_head_tail_cons (f, &head, &tail);
Florin Coras0a846802019-04-09 18:29:14 -0700624 c = f->head_chunk;
Sirshak Das28aa5392019-02-05 01:33:33 -0600625 head_idx = head % f->size;
Florin Coras0a846802019-04-09 18:29:14 -0700626 head_idx -= c->start_byte;
627 n_chunk = c->length - head_idx;
628 if (len <= n_chunk)
629 clib_memcpy_fast (&c->data[head_idx], data, len);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700630 else
631 {
Florin Coras0a846802019-04-09 18:29:14 -0700632 clib_memcpy_fast (&c->data[head_idx], data, n_chunk);
633 clib_memcpy_fast (&c->next->data[0], data + n_chunk, len - n_chunk);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700634 }
635}
Florin Coras3aa7af32018-06-29 08:44:31 -0700636#endif
Dave Barach68b0fb02017-02-28 15:15:56 -0500637
Florin Coras0a846802019-04-09 18:29:14 -0700638CLIB_MARCH_FN (svm_fifo_dequeue_nowait, int, svm_fifo_t * f, u32 len,
639 u8 * dst)
Dave Barach68b0fb02017-02-28 15:15:56 -0500640{
Florin Coras0a846802019-04-09 18:29:14 -0700641 u32 to_copy, n_chunk, tail, head, cursize, head_idx;
642 svm_fifo_chunk_t *c;
Dave Barach68b0fb02017-02-28 15:15:56 -0500643
Sirshak Das28aa5392019-02-05 01:33:33 -0600644 f_load_head_tail_cons (f, &head, &tail);
645
646 /* current size of fifo can only increase during dequeue: SPSC */
647 cursize = f_cursize (f, head, tail);
648
Florin Coras3e350af2017-03-30 02:54:28 -0700649 if (PREDICT_FALSE (cursize == 0))
Dave Barach68b0fb02017-02-28 15:15:56 -0500650 return -2; /* nothing in the fifo */
651
Florin Coras0a846802019-04-09 18:29:14 -0700652 to_copy = len = clib_min (cursize, len);
Dave Barach68b0fb02017-02-28 15:15:56 -0500653
Florin Coras0a846802019-04-09 18:29:14 -0700654 c = f->head_chunk;
Sirshak Das28aa5392019-02-05 01:33:33 -0600655 head_idx = head % f->size;
Florin Coras0a846802019-04-09 18:29:14 -0700656 head_idx -= c->start_byte;
657 n_chunk = c->length - head_idx;
Sirshak Das28aa5392019-02-05 01:33:33 -0600658
Florin Coras0a846802019-04-09 18:29:14 -0700659 if (n_chunk < to_copy)
Dave Barach68b0fb02017-02-28 15:15:56 -0500660 {
Florin Coras0a846802019-04-09 18:29:14 -0700661 clib_memcpy_fast (dst, &c->data[head_idx], n_chunk);
662 while ((to_copy -= n_chunk))
Dave Barach68b0fb02017-02-28 15:15:56 -0500663 {
Florin Coras0a846802019-04-09 18:29:14 -0700664 c = c->next;
665 n_chunk = clib_min (c->length, to_copy);
666 clib_memcpy_fast (dst + (len - to_copy), &c->data[0], n_chunk);
Dave Barach68b0fb02017-02-28 15:15:56 -0500667 }
Florin Coras0a846802019-04-09 18:29:14 -0700668 f->head_chunk = c;
Dave Barach68b0fb02017-02-28 15:15:56 -0500669 }
670 else
671 {
Florin Coras0a846802019-04-09 18:29:14 -0700672 clib_memcpy_fast (dst, &c->data[head_idx], to_copy);
Dave Barach68b0fb02017-02-28 15:15:56 -0500673 }
Florin Coras0a846802019-04-09 18:29:14 -0700674 head += len;
Dave Barach68b0fb02017-02-28 15:15:56 -0500675
Florin Coras0a846802019-04-09 18:29:14 -0700676 ASSERT (cursize >= to_copy);
Sirshak Das28aa5392019-02-05 01:33:33 -0600677 /* store-rel: consumer owned index (paired with load-acq in producer) */
678 clib_atomic_store_rel_n (&f->head, head);
Dave Barach68b0fb02017-02-28 15:15:56 -0500679
Florin Coras0a846802019-04-09 18:29:14 -0700680 return len;
Dave Barach68b0fb02017-02-28 15:15:56 -0500681}
682
Florin Coras3aa7af32018-06-29 08:44:31 -0700683#ifndef CLIB_MARCH_VARIANT
Florin Corasf6359c82017-06-19 12:26:09 -0400684
Dave Barach68b0fb02017-02-28 15:15:56 -0500685int
Florin Corasf6359c82017-06-19 12:26:09 -0400686svm_fifo_dequeue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_here)
687{
Florin Coras983cc7d2018-09-18 23:11:55 -0700688 return CLIB_MARCH_FN_SELECT (svm_fifo_dequeue_nowait) (f, max_bytes,
689 copy_here);
Florin Corasf6359c82017-06-19 12:26:09 -0400690}
Florin Coras3aa7af32018-06-29 08:44:31 -0700691#endif
Florin Corasf6359c82017-06-19 12:26:09 -0400692
Florin Coras983cc7d2018-09-18 23:11:55 -0700693CLIB_MARCH_FN (svm_fifo_peek, int, svm_fifo_t * f, u32 relative_offset,
Florin Coras0a846802019-04-09 18:29:14 -0700694 u32 len, u8 * dst)
Dave Barach68b0fb02017-02-28 15:15:56 -0500695{
Florin Coras0a846802019-04-09 18:29:14 -0700696 u32 to_copy, n_chunk, tail, head, cursize, head_idx;
697 svm_fifo_chunk_t *c;
Dave Barach68b0fb02017-02-28 15:15:56 -0500698
Sirshak Das28aa5392019-02-05 01:33:33 -0600699 f_load_head_tail_cons (f, &head, &tail);
700
701 /* current size of fifo can only increase during peek: SPSC */
702 cursize = f_cursize (f, head, tail);
703
Florin Coras93992a92017-05-24 18:03:56 -0700704 if (PREDICT_FALSE (cursize < relative_offset))
Dave Barach68b0fb02017-02-28 15:15:56 -0500705 return -2; /* nothing in the fifo */
706
Florin Coras0a846802019-04-09 18:29:14 -0700707 to_copy = len = clib_min (cursize - relative_offset, len);
Dave Barach68b0fb02017-02-28 15:15:56 -0500708
Florin Coras0a846802019-04-09 18:29:14 -0700709 c = f->head_chunk;
710 head_idx = (head + relative_offset) % f->size;
711 head_idx -= c->start_byte;
712 n_chunk = c->length - head_idx;
Dave Barach68b0fb02017-02-28 15:15:56 -0500713
Florin Coras0a846802019-04-09 18:29:14 -0700714 if (n_chunk < to_copy)
Dave Barach68b0fb02017-02-28 15:15:56 -0500715 {
Florin Coras0a846802019-04-09 18:29:14 -0700716 clib_memcpy_fast (dst, &c->data[head_idx], n_chunk);
717 while ((to_copy -= n_chunk))
Dave Barach68b0fb02017-02-28 15:15:56 -0500718 {
Florin Coras0a846802019-04-09 18:29:14 -0700719 c = c->next;
720 n_chunk = clib_min (c->length, to_copy);
721 clib_memcpy_fast (dst + (len - to_copy), &c->data[0], n_chunk);
Dave Barach68b0fb02017-02-28 15:15:56 -0500722 }
Florin Coras0a846802019-04-09 18:29:14 -0700723 f->head_chunk = c;
Dave Barach68b0fb02017-02-28 15:15:56 -0500724 }
Florin Coras0a846802019-04-09 18:29:14 -0700725 else
726 {
727 clib_memcpy_fast (dst, &c->data[head_idx], to_copy);
728 }
729 return len;
Dave Barach68b0fb02017-02-28 15:15:56 -0500730}
731
Florin Coras3aa7af32018-06-29 08:44:31 -0700732#ifndef CLIB_MARCH_VARIANT
Florin Corasf6359c82017-06-19 12:26:09 -0400733
734int
735svm_fifo_peek (svm_fifo_t * f, u32 relative_offset, u32 max_bytes,
736 u8 * copy_here)
737{
Florin Coras983cc7d2018-09-18 23:11:55 -0700738 return CLIB_MARCH_FN_SELECT (svm_fifo_peek) (f, relative_offset, max_bytes,
739 copy_here);
Florin Corasf6359c82017-06-19 12:26:09 -0400740}
741
Dave Barach68b0fb02017-02-28 15:15:56 -0500742int
Florin Corasa5464812017-04-19 13:00:05 -0700743svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes)
Dave Barach68b0fb02017-02-28 15:15:56 -0500744{
Sirshak Das28aa5392019-02-05 01:33:33 -0600745 u32 total_drop_bytes;
746 u32 tail, head, cursize;
Dave Barach68b0fb02017-02-28 15:15:56 -0500747
Sirshak Das28aa5392019-02-05 01:33:33 -0600748 f_load_head_tail_cons (f, &head, &tail);
749
750 /* number of bytes we're going to drop */
751 cursize = f_cursize (f, head, tail);
752
Florin Coras3e350af2017-03-30 02:54:28 -0700753 if (PREDICT_FALSE (cursize == 0))
Dave Barach68b0fb02017-02-28 15:15:56 -0500754 return -2; /* nothing in the fifo */
755
Sirshak Das28aa5392019-02-05 01:33:33 -0600756 svm_fifo_trace_add (f, tail, total_drop_bytes, 3);
Dave Barach68b0fb02017-02-28 15:15:56 -0500757
Sirshak Das28aa5392019-02-05 01:33:33 -0600758 /* number of bytes we're going to drop */
Dave Barach68b0fb02017-02-28 15:15:56 -0500759 total_drop_bytes = (cursize < max_bytes) ? cursize : max_bytes;
760
Sirshak Das28aa5392019-02-05 01:33:33 -0600761 /* move head */
762 head += total_drop_bytes;
Florin Coras3eb50622017-07-13 01:24:57 -0400763
Dave Barach2c25a622017-06-26 11:35:07 -0400764 ASSERT (cursize >= total_drop_bytes);
Sirshak Das28aa5392019-02-05 01:33:33 -0600765 /* store-rel: consumer owned index (paired with load-acq in producer) */
766 clib_atomic_store_rel_n (&f->head, head);
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{
Sirshak Das28aa5392019-02-05 01:33:33 -0600774 /* consumer foreign index */
775 u32 tail = clib_atomic_load_acq_n (&f->tail);
776 /* store-rel: consumer owned index (paired with load-acq in producer) */
777 clib_atomic_store_rel_n (&f->head, tail);
Florin Coras25579b42018-06-06 17:55:02 -0700778}
779
Florin Coras2cba8532018-09-11 16:33:36 -0700780int
781svm_fifo_segments (svm_fifo_t * f, svm_fifo_segment_t * fs)
782{
Sirshak Das28aa5392019-02-05 01:33:33 -0600783 u32 cursize, head, tail, head_idx;
Florin Coras2cba8532018-09-11 16:33:36 -0700784
Sirshak Das28aa5392019-02-05 01:33:33 -0600785 f_load_head_tail_cons (f, &head, &tail);
786
787 /* consumer function, cursize can only increase while we're working */
788 cursize = f_cursize (f, head, tail);
789
Florin Coras2cba8532018-09-11 16:33:36 -0700790 if (PREDICT_FALSE (cursize == 0))
Sirshak Das28aa5392019-02-05 01:33:33 -0600791 return -2; /* nothing in the fifo */
Florin Coras2cba8532018-09-11 16:33:36 -0700792
Sirshak Das28aa5392019-02-05 01:33:33 -0600793 head_idx = head % f->size;
Florin Coras2cba8532018-09-11 16:33:36 -0700794
Sirshak Das28aa5392019-02-05 01:33:33 -0600795 if (tail < head)
Florin Coras2cba8532018-09-11 16:33:36 -0700796 {
Sirshak Das28aa5392019-02-05 01:33:33 -0600797 fs[0].len = f->size - head_idx;
Florin Coras0a846802019-04-09 18:29:14 -0700798 fs[0].data = f->head_chunk->data + head_idx;
Florin Coras2cba8532018-09-11 16:33:36 -0700799 fs[1].len = cursize - fs[0].len;
Florin Coras0a846802019-04-09 18:29:14 -0700800 fs[1].data = f->head_chunk->data;
Florin Coras2cba8532018-09-11 16:33:36 -0700801 }
802 else
803 {
Sirshak Das28aa5392019-02-05 01:33:33 -0600804 fs[0].len = cursize;
Florin Coras0a846802019-04-09 18:29:14 -0700805 fs[0].data = f->head_chunk->data + head_idx;
Florin Coras2cba8532018-09-11 16:33:36 -0700806 fs[1].len = 0;
807 fs[1].data = 0;
808 }
809 return cursize;
810}
811
812void
813svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_segment_t * fs)
814{
Sirshak Das28aa5392019-02-05 01:33:33 -0600815 u32 head, head_idx;
Florin Coras2cba8532018-09-11 16:33:36 -0700816
Sirshak Das28aa5392019-02-05 01:33:33 -0600817 /* consumer owned index */
818 head = f->head;
819 head_idx = head % f->size;
820
Florin Coras0a846802019-04-09 18:29:14 -0700821 ASSERT (fs[0].data == f->head_chunk->data + head_idx);
Sirshak Das28aa5392019-02-05 01:33:33 -0600822 head += fs[0].len + fs[1].len;
823 /* store-rel: consumer owned index (paired with load-acq in producer) */
824 clib_atomic_store_rel_n (&f->head, head);
825}
826
827/* Assumption: no prod and cons are accessing either dest or src fifo */
828void
829svm_fifo_clone (svm_fifo_t * df, svm_fifo_t * sf)
830{
831 u32 head, tail;
Florin Coras0a846802019-04-09 18:29:14 -0700832 clib_memcpy_fast (df->head_chunk->data, sf->head_chunk->data, sf->size);
Sirshak Das28aa5392019-02-05 01:33:33 -0600833
834 f_load_head_tail_all_acq (sf, &head, &tail);
835 clib_atomic_store_rel_n (&df->head, head);
836 clib_atomic_store_rel_n (&df->tail, tail);
Florin Coras2cba8532018-09-11 16:33:36 -0700837}
838
Dave Barach1f75cfd2017-04-14 16:46:44 -0400839u32
840svm_fifo_number_ooo_segments (svm_fifo_t * f)
841{
842 return pool_elts (f->ooo_segments);
843}
844
845ooo_segment_t *
846svm_fifo_first_ooo_segment (svm_fifo_t * f)
847{
848 return pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
849}
850
Florin Corasc28764f2017-04-26 00:08:42 -0700851/**
852 * Set fifo pointers to requested offset
853 */
854void
855svm_fifo_init_pointers (svm_fifo_t * f, u32 pointer)
856{
Sirshak Das28aa5392019-02-05 01:33:33 -0600857 clib_atomic_store_rel_n (&f->head, pointer);
858 clib_atomic_store_rel_n (&f->tail, pointer);
Florin Corasc28764f2017-04-26 00:08:42 -0700859}
860
Florin Coras72b04282019-01-14 17:23:11 -0800861void
862svm_fifo_add_subscriber (svm_fifo_t * f, u8 subscriber)
863{
864 if (f->n_subscribers >= SVM_FIFO_MAX_EVT_SUBSCRIBERS)
865 return;
866 f->subscribers[f->n_subscribers++] = subscriber;
867}
868
869void
870svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber)
871{
872 int i;
873
874 for (i = 0; i < f->n_subscribers; i++)
875 {
876 if (f->subscribers[i] != subscriber)
877 continue;
878 f->subscribers[i] = f->subscribers[f->n_subscribers - 1];
879 f->n_subscribers--;
880 break;
881 }
882}
883
Florin Coras3aa7af32018-06-29 08:44:31 -0700884#endif
Dave Barach68b0fb02017-02-28 15:15:56 -0500885/*
886 * fd.io coding-style-patch-verification: ON
887 *
888 * Local Variables:
889 * eval: (c-set-style "gnu")
890 * End:
891 */