2 * Copyright (c) 2019 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <svm/svm_fifo.h>
16 #include <vlib/vlib.h>
17 #include <svm/svm_common.h>
18 #include <svm/fifo_segment.h>
20 #define SFIFO_TEST_I(_cond, _comment, _args...) \
22 int _evald = (_cond); \
24 fformat(stderr, "FAIL:%d: " _comment "\n", \
27 fformat(stderr, "PASS:%d: " _comment "\n", \
33 #define SFIFO_TEST(_cond, _comment, _args...) \
35 if (!SFIFO_TEST_I(_cond, _comment, ##_args)) { \
46 test_pattern_t test_pattern[] = {
47 {380, 8}, {768, 8}, {1156, 8}, {1544, 8}, {1932, 8}, {2320, 8}, {2708, 8},
48 {2992, 8}, {372, 8}, {760, 8}, {1148, 8}, {1536, 8}, {1924, 8}, {2312, 8},
49 {2700, 8}, {2984, 8}, {364, 8}, {752, 8}, {1140, 8}, {1528, 8}, {1916, 8},
50 {2304, 8}, {2692, 8}, {2976, 8}, {356, 8}, {744, 8}, {1132, 8}, {1520, 8},
51 {1908, 8}, {2296, 8}, {2684, 8}, {2968, 8}, {348, 8}, {736, 8}, {1124, 8},
52 {1512, 8}, {1900, 8}, {2288, 8}, {2676, 8}, {2960, 8}, {340, 8}, {728, 8},
53 {1116, 8}, {1504, 8}, {1892, 8}, {2280, 8}, {2668, 8}, {2952, 8}, {332, 8},
54 {720, 8}, {1108, 8}, {1496, 8}, {1884, 8}, {2272, 8}, {2660, 8}, {2944, 8},
55 {324, 8}, {712, 8}, {1100, 8}, {1488, 8}, {1876, 8}, {2264, 8}, {2652, 8},
56 {2936, 8}, {316, 8}, {704, 8}, {1092, 8}, {1480, 8}, {1868, 8}, {2256, 8},
57 {2644, 8}, {2928, 8}, {308, 8}, {696, 8}, {1084, 8}, {1472, 8}, {1860, 8},
58 {2248, 8}, {2636, 8}, {2920, 8}, {300, 8}, {688, 8}, {1076, 8}, {1464, 8},
59 {1852, 8}, {2240, 8}, {2628, 8}, {2912, 8}, {292, 8}, {680, 8}, {1068, 8},
60 {1456, 8}, {1844, 8}, {2232, 8}, {2620, 8}, {2904, 8}, {284, 8}, {672, 8},
61 {1060, 8}, {1448, 8}, {1836, 8}, {2224, 8}, {2612, 8}, {2896, 8}, {276, 8},
62 {664, 8}, {1052, 8}, {1440, 8}, {1828, 8}, {2216, 8}, {2604, 8}, {2888, 8},
63 {268, 8}, {656, 8}, {1044, 8}, {1432, 8}, {1820, 8}, {2208, 8}, {2596, 8},
64 {2880, 8}, {260, 8}, {648, 8}, {1036, 8}, {1424, 8}, {1812, 8}, {2200, 8},
65 {2588, 8}, {2872, 8}, {252, 8}, {640, 8}, {1028, 8}, {1416, 8}, {1804, 8},
66 {2192, 8}, {2580, 8}, {2864, 8}, {244, 8}, {632, 8}, {1020, 8}, {1408, 8},
67 {1796, 8}, {2184, 8}, {2572, 8}, {2856, 8}, {236, 8}, {624, 8}, {1012, 8},
68 {1400, 8}, {1788, 8}, {2176, 8}, {2564, 8}, {2848, 8}, {228, 8}, {616, 8},
69 {1004, 8}, {1392, 8}, {1780, 8}, {2168, 8}, {2556, 8}, {2840, 8}, {220, 8},
70 {608, 8}, {996, 8}, {1384, 8}, {1772, 8}, {2160, 8}, {2548, 8}, {2832, 8},
71 {212, 8}, {600, 8}, {988, 8}, {1376, 8}, {1764, 8}, {2152, 8}, {2540, 8},
72 {2824, 8}, {204, 8}, {592, 8}, {980, 8}, {1368, 8}, {1756, 8}, {2144, 8},
73 {2532, 8}, {2816, 8}, {196, 8}, {584, 8}, {972, 8}, {1360, 8}, {1748, 8},
74 {2136, 8}, {2524, 8}, {2808, 8}, {188, 8}, {576, 8}, {964, 8}, {1352, 8},
75 {1740, 8}, {2128, 8}, {2516, 8}, {2800, 8}, {180, 8}, {568, 8}, {956, 8},
76 {1344, 8}, {1732, 8}, {2120, 8}, {2508, 8}, {2792, 8}, {172, 8}, {560, 8},
77 {948, 8}, {1336, 8}, {1724, 8}, {2112, 8}, {2500, 8}, {2784, 8}, {164, 8},
78 {552, 8}, {940, 8}, {1328, 8}, {1716, 8}, {2104, 8}, {2492, 8}, {2776, 8},
79 {156, 8}, {544, 8}, {932, 8}, {1320, 8}, {1708, 8}, {2096, 8}, {2484, 8},
80 {2768, 8}, {148, 8}, {536, 8}, {924, 8}, {1312, 8}, {1700, 8}, {2088, 8},
81 {2476, 8}, {2760, 8}, {140, 8}, {528, 8}, {916, 8}, {1304, 8}, {1692, 8},
82 {2080, 8}, {2468, 8}, {2752, 8}, {132, 8}, {520, 8}, {908, 8}, {1296, 8},
83 {1684, 8}, {2072, 8}, {2460, 8}, {2744, 8}, {124, 8}, {512, 8}, {900, 8},
84 {1288, 8}, {1676, 8}, {2064, 8}, {2452, 8}, {2736, 8}, {116, 8}, {504, 8},
85 {892, 8}, {1280, 8}, {1668, 8}, {2056, 8}, {2444, 8}, {2728, 8}, {108, 8},
86 {496, 8}, {884, 8}, {1272, 8}, {1660, 8}, {2048, 8}, {2436, 8}, {2720, 8},
87 {100, 8}, {488, 8}, {876, 8}, {1264, 8}, {1652, 8}, {2040, 8}, {2428, 8},
88 {2716, 4}, {92, 8}, {480, 8}, {868, 8}, {1256, 8}, {1644, 8}, {2032, 8},
89 {2420, 8}, {84, 8}, {472, 8}, {860, 8}, {1248, 8}, {1636, 8}, {2024, 8},
90 {2412, 8}, {76, 8}, {464, 8}, {852, 8}, {1240, 8}, {1628, 8}, {2016, 8},
91 {2404, 8}, {68, 8}, {456, 8}, {844, 8}, {1232, 8}, {1620, 8}, {2008, 8},
92 {2396, 8}, {60, 8}, {448, 8}, {836, 8}, {1224, 8}, {1612, 8}, {2000, 8},
93 {2388, 8}, {52, 8}, {440, 8}, {828, 8}, {1216, 8}, {1604, 8}, {1992, 8},
94 {2380, 8}, {44, 8}, {432, 8}, {820, 8}, {1208, 8}, {1596, 8}, {1984, 8},
95 {2372, 8}, {36, 8}, {424, 8}, {812, 8}, {1200, 8}, {1588, 8}, {1976, 8},
96 {2364, 8}, {28, 8}, {416, 8}, {804, 8}, {1192, 8}, {1580, 8}, {1968, 8},
97 {2356, 8}, {20, 8}, {408, 8}, {796, 8}, {1184, 8}, {1572, 8}, {1960, 8},
98 {2348, 8}, {12, 8}, {400, 8}, {788, 8}, {1176, 8}, {1564, 8}, {1952, 8},
99 {2340, 8}, {4, 8}, {392, 8}, {780, 8}, {1168, 8}, {1556, 8}, {1944, 8},
101 /* missing from original data set */
102 {388, 4}, {776, 4}, {1164, 4}, {1552, 4}, {1940, 4}, {2328, 4},
106 pattern_cmp (const void *arg1, const void *arg2)
108 test_pattern_t *a1 = (test_pattern_t *) arg1;
109 test_pattern_t *a2 = (test_pattern_t *) arg2;
111 if (a1->offset < a2->offset)
113 else if (a1->offset > a2->offset)
119 fifo_validate_pattern (vlib_main_t * vm, test_pattern_t * pattern,
122 test_pattern_t *tp = pattern;
125 /* Go through the pattern and make 100% sure it's sane */
126 for (i = 0; i < pattern_length - 1; i++)
128 if (tp->offset + tp->len != (tp + 1)->offset)
130 vlib_cli_output (vm, "[%d] missing {%d, %d}", i,
131 (tp->offset + tp->len),
132 (tp + 1)->offset - (tp->offset + tp->len));
140 static test_pattern_t *
141 fifo_get_validate_pattern (vlib_main_t * vm, test_pattern_t * test_data,
144 test_pattern_t *validate_pattern = 0;
146 /* Validate, and try segments in order... */
147 vec_validate (validate_pattern, test_data_len - 1);
148 memcpy (validate_pattern, test_data,
149 test_data_len * sizeof (test_pattern_t));
150 qsort ((u8 *) validate_pattern, test_data_len, sizeof (test_pattern_t),
153 if (fifo_validate_pattern (vm, validate_pattern, test_data_len) == 0)
156 return validate_pattern;
159 static fifo_segment_t *
160 fifo_segment_prepare (fifo_segment_main_t * sm, char *seg_name, u32 seg_size)
162 fifo_segment_create_args_t _a, *a = &_a;
164 clib_memset (a, 0, sizeof (*a));
165 a->segment_name = seg_name;
166 a->segment_size = seg_size ? seg_size : 32 << 20;
168 if (fifo_segment_create (sm, a))
171 return fifo_segment_get_segment (sm, a->new_segment_indices[0]);
175 ft_fifo_segment_free (fifo_segment_main_t * sm, fifo_segment_t * fs)
177 fifo_segment_delete (sm, fs);
181 fifo_segment_alloc_fifo (fifo_segment_t * fs, u32 data_bytes,
182 fifo_segment_ftype_t ftype)
184 return fifo_segment_alloc_fifo_w_slice (fs, 0, data_bytes, ftype);
188 fifo_prepare (fifo_segment_t * fs, u32 fifo_size)
193 f = fifo_segment_alloc_fifo (fs, fifo_size, FIFO_SEGMENT_RX_FIFO);
195 /* Paint 1st fifo chunk with -1's */
197 clib_memset (c->data, 0xFF, c->length);
199 svm_fifo_init_ooo_lookup (f, 1 /* deq ooo */ );
204 ft_fifo_free (fifo_segment_t * fs, svm_fifo_t * f)
206 fifo_segment_free_fifo (fs, f);
210 compare_data (u8 * data1, u8 * data2, u32 start, u32 len, u32 * index)
214 for (i = start; i < start + len; i++)
216 if (data1[i] != data2[i])
226 sfifo_test_fifo1 (vlib_main_t * vm, unformat_input_t * input)
228 u32 fifo_size = 1 << 20, *test_data = 0, offset, data_word, test_data_len;
229 fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
230 u8 *data, *s, *data_buf = 0;
231 int i, rv, verbose = 0;
232 ooo_segment_t *ooo_seg;
237 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
239 if (unformat (input, "verbose"))
243 test_data_len = fifo_size / sizeof (u32);
244 vec_validate (test_data, test_data_len - 1);
246 for (i = 0; i < vec_len (test_data); i++)
249 fs = fifo_segment_prepare (fsm, "fifo-test1", 0);
250 f = fifo_prepare (fs, fifo_size);
253 * Enqueue an initial (un-dequeued) chunk
255 rv = svm_fifo_enqueue (f, sizeof (u32), (u8 *) test_data);
256 SFIFO_TEST ((rv == sizeof (u32)), "enqueued %d", rv);
257 SFIFO_TEST ((f->shr->tail == 4), "fifo tail %u", f->shr->tail);
260 * Create 3 chunks in the future. The offsets are relative
261 * to the current fifo tail
263 for (i = 0; i < 3; i++)
265 offset = (2 * i + 1) * sizeof (u32) - f->shr->tail;
266 data = (u8 *) (test_data + (2 * i + 1));
269 rv = svm_fifo_enqueue (f, sizeof (u32), data);
270 rv = rv > 0 ? 0 : rv;
273 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
275 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i + 1, offset,
276 offset + sizeof (u32));
279 clib_warning ("enqueue returned %d", rv);
285 vlib_cli_output (vm, "fifo after odd segs: %U", format_svm_fifo, f, 1);
287 SFIFO_TEST ((f->shr->tail == 8), "fifo tail %u", f->shr->tail);
288 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 2),
289 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
292 * Try adding a completely overlapped segment
294 offset = 3 * sizeof (u32) - f->shr->tail;
295 data = (u8 *) (test_data + 3);
296 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
299 clib_warning ("enqueue returned %d", rv);
304 vlib_cli_output (vm, "fifo after overlap seg: %U", format_svm_fifo, f, 1);
306 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 2),
307 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
310 * Make sure format functions are not buggy
312 s = format (0, "%U", format_svm_fifo, f, 2);
316 * Paint some of missing data backwards
318 for (i = 3; i > 1; i--)
320 offset = (2 * i + 0) * sizeof (u32) - f->shr->tail;
321 data = (u8 *) (test_data + (2 * i + 0));
322 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
324 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i, offset,
325 offset + sizeof (u32));
328 clib_warning ("enqueue returned %d", rv);
334 vlib_cli_output (vm, "fifo before missing link: %U", format_svm_fifo, f,
336 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 1),
337 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
338 ooo_seg = svm_fifo_first_ooo_segment (f);
339 SFIFO_TEST ((ooo_seg->start == 12),
340 "first ooo seg position %u", ooo_seg->start);
341 SFIFO_TEST ((ooo_seg->length == 16),
342 "first ooo seg length %u", ooo_seg->length);
345 * Enqueue the missing u32
347 rv = svm_fifo_enqueue (f, sizeof (u32), (u8 *) (test_data + 2));
349 vlib_cli_output (vm, "fifo after missing link: %U", format_svm_fifo, f,
351 SFIFO_TEST ((rv == 20), "bytes to be enqueued %u", rv);
352 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 0),
353 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
358 for (i = 0; i < 7; i++)
360 rv = svm_fifo_dequeue (f, sizeof (u32), (u8 *) & data_word);
361 if (rv != sizeof (u32))
363 clib_warning ("bytes dequeues %u", rv);
366 if (data_word != test_data[i])
368 clib_warning ("recovered [%d] %d not %d", i, data_word,
375 * Test segment overlaps: last ooo segment overlaps all
377 ft_fifo_free (fs, f);
378 f = fifo_prepare (fs, fifo_size);
380 for (i = 0; i < 4; i++)
382 offset = (2 * i + 1) * sizeof (u32) - f->shr->tail;
383 data = (u8 *) (test_data + (2 * i + 1));
384 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
386 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i + 1, offset,
387 offset + sizeof (u32));
390 clib_warning ("enqueue returned %d", rv);
395 rv = svm_fifo_enqueue_with_offset (f, 8 - f->shr->tail, 21, data);
396 SFIFO_TEST ((rv == 0), "ooo enqueued %u", rv);
397 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 1),
398 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
400 /* add missing data to be able to dequeue something */
401 rv = svm_fifo_enqueue (f, 4, data);
402 SFIFO_TEST ((rv == 32), "enqueued %u", rv);
403 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 0),
404 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
406 vec_validate (data_buf, vec_len (test_data));
407 svm_fifo_peek (f, 0, 4, data_buf);
408 if (compare_data (data_buf, data, 0, 4, &j))
409 SFIFO_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j], data[j]);
410 svm_fifo_peek (f, 8, 21, data_buf);
411 if (compare_data (data_buf, data, 0, 21, &j))
412 SFIFO_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j], data[j]);
413 vec_reset_length (data_buf);
416 * Test segment overlaps: enqueue and overlap ooo segments
418 ft_fifo_free (fs, f);
419 f = fifo_prepare (fs, fifo_size);
421 for (i = 0; i < 4; i++)
423 offset = (2 * i + 1) * sizeof (u32) - f->shr->tail;
424 data = (u8 *) (test_data + (2 * i + 1));
425 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
427 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i + 1, offset,
428 offset + sizeof (u32));
431 clib_warning ("enqueue returned %d", rv);
437 vlib_cli_output (vm, "fifo after enqueue: %U", format_svm_fifo, f, 1);
439 rv = svm_fifo_enqueue (f, 29, data);
441 vlib_cli_output (vm, "fifo after enqueueing 29: %U", format_svm_fifo, f,
443 SFIFO_TEST ((rv == 32), "ooo enqueued %u", rv);
444 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 0),
445 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
447 vec_validate (data_buf, vec_len (data));
448 svm_fifo_peek (f, 0, vec_len (data), data_buf);
449 if (compare_data (data_buf, data, 0, vec_len (data), &j))
451 SFIFO_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j], data[j]);
454 /* Try to peek beyond the data */
455 rv = svm_fifo_peek (f, svm_fifo_max_dequeue (f), vec_len (data), data_buf);
456 SFIFO_TEST ((rv == 0), "peeked %u expected 0", rv);
459 ft_fifo_free (fs, f);
460 ft_fifo_segment_free (fsm, fs);
461 vec_free (test_data);
466 ft_fifo_free (fs, f);
467 ft_fifo_segment_free (fsm, fs);
468 vec_free (test_data);
474 sfifo_test_fifo2 (vlib_main_t * vm)
476 fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
477 test_pattern_t *tp, *vp, *test_data;
478 u32 fifo_size = (1 << 20) + 1;
479 int i, rv, test_data_len;
480 ooo_segment_t *ooo_seg;
485 test_data = test_pattern;
486 test_data_len = ARRAY_LEN (test_pattern);
488 vp = fifo_get_validate_pattern (vm, test_data, test_data_len);
491 fs = fifo_segment_prepare (fsm, "fifo-test2", 0);
492 f = fifo_prepare (fs, fifo_size);
495 * Try with sorted data
497 for (i = 0; i < test_data_len; i++)
501 svm_fifo_enqueue_with_offset (f, tp->offset - f->shr->tail, tp->len,
505 /* Expected result: one big fat chunk at offset 4 */
506 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 1),
507 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
508 ooo_seg = svm_fifo_first_ooo_segment (f);
509 SFIFO_TEST ((ooo_seg->start == 4),
510 "first ooo seg position %u", ooo_seg->start);
511 SFIFO_TEST ((ooo_seg->length == 2996),
512 "first ooo seg length %u", ooo_seg->length);
515 rv = svm_fifo_enqueue (f, sizeof (u32), (u8 *) & data64);
516 SFIFO_TEST ((rv == 3000), "bytes to be enqueued %u", rv);
518 ft_fifo_free (fs, f);
522 * Now try it again w/ unsorted data...
525 f = fifo_prepare (fs, fifo_size);
527 for (i = 0; i < test_data_len; i++)
531 rv = svm_fifo_enqueue_with_offset (f, tp->offset - f->shr->tail, tp->len,
535 clib_warning ("enqueue returned %d", rv);
539 /* Expecting the same result: one big fat chunk at offset 4 */
540 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 1),
541 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
542 ooo_seg = svm_fifo_first_ooo_segment (f);
543 SFIFO_TEST ((ooo_seg->start == 4),
544 "first ooo seg position %u", ooo_seg->start);
545 SFIFO_TEST ((ooo_seg->length == 2996),
546 "first ooo seg length %u", ooo_seg->length);
549 rv = svm_fifo_enqueue (f, sizeof (u32), (u8 *) & data64);
551 SFIFO_TEST ((rv == 3000), "bytes to be enqueued %u", rv);
553 ft_fifo_free (fs, f);
554 ft_fifo_segment_free (fsm, fs);
560 sfifo_test_fifo3 (vlib_main_t * vm, unformat_input_t * input)
562 u32 nsegs = 2, seg_size, length_so_far, current_offset, offset_increment;
563 int overlap = 0, verbose = 0, randomize = 1, drop = 0, in_seq_all = 0;
564 u32 len_this_chunk, seed = 0xdeaddabe, j, total_size = 2 << 10;
565 u32 fifo_size = (4 << 10) + 1, fifo_initial_offset = 0;
566 fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
567 u8 *data_pattern = 0, *data_buf = 0;
568 test_pattern_t *tp, *generate = 0;
573 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
575 if (unformat (input, "fifo-size %d", &fifo_size))
577 else if (unformat (input, "total-size %d", &total_size))
579 else if (unformat (input, "verbose"))
581 else if (unformat (input, "overlap"))
583 else if (unformat (input, "initial-offset %d", &fifo_initial_offset))
585 else if (unformat (input, "seed %d", &seed))
587 else if (unformat (input, "nsegs %d", &nsegs))
589 else if (unformat (input, "no-randomize"))
591 else if (unformat (input, "in-seq-all"))
593 else if (unformat (input, "drop"))
597 clib_error_t *e = clib_error_return
598 (0, "unknown input `%U'", format_unformat_error, input);
599 clib_error_report (e);
604 if (total_size > fifo_size)
606 clib_warning ("total_size %d greater than fifo size %d", total_size,
610 if (overlap && randomize == 0)
612 clib_warning ("Can't enqueue in-order with overlap");
619 vec_validate (data_pattern, total_size - 1);
620 for (i = 0; i < vec_len (data_pattern); i++)
621 data_pattern[i] = i & 0xff;
626 seg_size = total_size / nsegs;
628 current_offset = randomize;
629 while (length_so_far < total_size)
631 vec_add2 (generate, tp, 1);
632 len_this_chunk = clib_min (seg_size, total_size - length_so_far);
633 tp->offset = current_offset;
634 tp->len = len_this_chunk;
636 if (overlap && (len_this_chunk == seg_size))
639 offset_increment = len_this_chunk
640 % (1 + (random_u32 (&seed) % len_this_chunk));
642 while (offset_increment == 0);
644 offset_increment = len_this_chunk;
646 current_offset += offset_increment;
647 length_so_far = tp->offset + tp->len;
651 * Validate segment list. Only valid for non-overlap cases.
654 fifo_validate_pattern (vm, generate, vec_len (generate));
658 vlib_cli_output (vm, "raw data pattern:");
659 for (i = 0; i < vec_len (generate); i++)
661 vlib_cli_output (vm, "[%d] offset %u len %u", i,
662 generate[i].offset, generate[i].len);
666 /* Randomize data pattern */
669 for (i = 0; i < vec_len (generate) / 2; i++)
671 u32 src_index, dst_index;
672 test_pattern_t _tmp, *tmp = &_tmp;
674 src_index = random_u32 (&seed) % vec_len (generate);
675 dst_index = random_u32 (&seed) % vec_len (generate);
677 tmp[0] = generate[dst_index];
678 generate[dst_index] = generate[src_index];
679 generate[src_index] = tmp[0];
683 vlib_cli_output (vm, "randomized data pattern:");
684 for (i = 0; i < vec_len (generate); i++)
686 vlib_cli_output (vm, "[%d] offset %u len %u", i,
687 generate[i].offset, generate[i].len);
693 * Create a fifo and add segments
695 fs = fifo_segment_prepare (fsm, "fifo-test3", 0);
696 f = fifo_prepare (fs, fifo_size);
698 /* manually set head and tail pointers to validate modular arithmetic */
699 fifo_initial_offset = fifo_initial_offset % fifo_size;
700 svm_fifo_init_pointers (f, fifo_initial_offset, fifo_initial_offset);
702 for (i = !randomize; i < vec_len (generate); i++)
705 svm_fifo_enqueue_with_offset (
706 f, fifo_initial_offset + tp->offset - f->shr->tail, tp->len,
707 (u8 *) data_pattern + tp->offset);
710 /* Add the first segment in order for non random data */
712 svm_fifo_enqueue (f, generate[0].len, (u8 *) data_pattern);
715 * Expected result: one big fat chunk at offset 1 if randomize == 1
719 vlib_cli_output (vm, "fifo before missing link: %U",
720 format_svm_fifo, f, 1 /* verbose */ );
723 * Add the missing byte if segments were randomized
727 u32 bytes_to_enq = 1;
729 bytes_to_enq = total_size;
730 rv = svm_fifo_enqueue (f, bytes_to_enq, data_pattern + 0);
733 vlib_cli_output (vm, "in-order enqueue returned %d", rv);
735 SFIFO_TEST ((rv == total_size), "enqueued %u expected %u", rv,
740 SFIFO_TEST ((svm_fifo_has_ooo_data (f) == 0), "number of ooo segments %u",
741 svm_fifo_n_ooo_segments (f));
744 * Test if peeked data is the same as original data
746 vec_validate (data_buf, vec_len (data_pattern));
747 svm_fifo_peek (f, 0, vec_len (data_pattern), data_buf);
748 if (compare_data (data_buf, data_pattern, 0, vec_len (data_pattern), &j))
750 SFIFO_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j],
755 * Dequeue or drop all data
759 svm_fifo_dequeue_drop (f, vec_len (data_pattern));
763 memset (data_buf, 0, vec_len (data_pattern));
764 svm_fifo_dequeue (f, vec_len (data_pattern), data_buf);
766 (data_buf, data_pattern, 0, vec_len (data_pattern), &j))
768 SFIFO_TEST (0, "[%d] dequeued %u expected %u", j, data_buf[j],
773 SFIFO_TEST ((svm_fifo_max_dequeue (f) == 0), "fifo has %d bytes",
774 svm_fifo_max_dequeue (f));
776 ft_fifo_free (fs, f);
777 ft_fifo_segment_free (fsm, fs);
778 vec_free (data_pattern);
785 sfifo_test_fifo4 (vlib_main_t * vm, unformat_input_t * input)
787 u32 fifo_size = 6 << 10, fifo_initial_offset = 1e9, test_n_bytes = 5000, j;
788 fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
789 u8 *test_data = 0, *data_buf = 0;
790 int i, rv, verbose = 0;
794 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
796 if (unformat (input, "verbose"))
800 clib_error_t *e = clib_error_return
801 (0, "unknown input `%U'", format_unformat_error, input);
802 clib_error_report (e);
808 * Create a fifo and add segments
810 fs = fifo_segment_prepare (fsm, "fifo-test4", 0);
811 f = fifo_prepare (fs, fifo_size);
813 /* Set head and tail pointers */
814 fifo_initial_offset = fifo_initial_offset % fifo_size;
815 svm_fifo_init_pointers (f, fifo_initial_offset, fifo_initial_offset);
817 vec_validate (test_data, test_n_bytes - 1);
818 for (i = 0; i < vec_len (test_data); i++)
821 for (i = test_n_bytes - 1; i > 0; i--)
823 rv = svm_fifo_enqueue_with_offset (
824 f, fifo_initial_offset + i - f->shr->tail, sizeof (u8), &test_data[i]);
826 vlib_cli_output (vm, "add [%d] [%d, %d]", i, i, i + sizeof (u8));
828 SFIFO_TEST (0, "enqueue returned %d", rv);
831 svm_fifo_enqueue (f, sizeof (u8), &test_data[0]);
833 vec_validate (data_buf, vec_len (test_data));
835 svm_fifo_dequeue (f, vec_len (test_data), data_buf);
836 rv = compare_data (data_buf, test_data, 0, vec_len (test_data), &j);
838 vlib_cli_output (vm, "[%d] dequeued %u expected %u", j, data_buf[j],
840 SFIFO_TEST ((rv == 0), "dequeued compared to original returned %d", rv);
842 ft_fifo_free (fs, f);
843 ft_fifo_segment_free (fsm, fs);
844 vec_free (test_data);
849 fifo_pos (svm_fifo_t * f, u32 pos)
854 /* Avoids exposing svm_fifo.c internal function */
855 static ooo_segment_t *
856 ooo_seg_next (svm_fifo_t * f, ooo_segment_t * s)
858 if (pool_is_free_index (f->ooo_segments, s->next))
860 return pool_elt_at_index (f->ooo_segments, s->next);
864 sfifo_test_fifo5 (vlib_main_t * vm, unformat_input_t * input)
866 fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
867 u32 fifo_size = 401, j = 0, offset = 200;
868 u8 *test_data = 0, *data_buf = 0;
869 int i, rv, verbose = 0;
870 ooo_segment_t *ooo_seg;
874 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
876 if (unformat (input, "verbose"))
880 clib_error_t *e = clib_error_return (0, "unknown input `%U'",
881 format_unformat_error, input);
882 clib_error_report (e);
887 fs = fifo_segment_prepare (fsm, "fifo-test5", 0);
888 f = fifo_prepare (fs, fifo_size);
889 svm_fifo_init_pointers (f, offset, offset);
891 vec_validate (test_data, 399);
892 for (i = 0; i < vec_len (test_data); i++)
893 test_data[i] = i % 0xff;
896 * Start with [100, 200] and [300, 400]
898 svm_fifo_enqueue_with_offset (f, 100, 100, &test_data[100]);
899 svm_fifo_enqueue_with_offset (f, 300, 100, &test_data[300]);
901 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 2),
902 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
903 SFIFO_TEST ((f->ooos_newest == 1), "newest %u", f->ooos_newest);
905 vlib_cli_output (vm, "fifo after [100, 200] and [300, 400] : %U",
906 format_svm_fifo, f, 2 /* verbose */ );
912 rv = svm_fifo_enqueue_with_offset (f, 225, 50, &test_data[225]);
914 vlib_cli_output (vm, "fifo after [225, 275] : %U",
915 format_svm_fifo, f, 2 /* verbose */ );
916 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 3),
917 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
918 ooo_seg = svm_fifo_first_ooo_segment (f);
919 SFIFO_TEST ((ooo_seg->start == fifo_pos (f, 100 + offset)),
920 "first seg start %u expected %u", ooo_seg->start,
921 fifo_pos (f, 100 + offset));
922 SFIFO_TEST ((ooo_seg->length == 100), "first seg length %u expected %u",
923 ooo_seg->length, 100);
924 ooo_seg = ooo_seg_next (f, ooo_seg);
925 SFIFO_TEST ((ooo_seg->start == fifo_pos (f, 225 + offset)),
926 "second seg start %u expected %u",
927 ooo_seg->start, fifo_pos (f, 225 + offset));
928 SFIFO_TEST ((ooo_seg->length == 50), "second seg length %u expected %u",
929 ooo_seg->length, 50);
930 ooo_seg = ooo_seg_next (f, ooo_seg);
931 SFIFO_TEST ((ooo_seg->start == fifo_pos (f, 300 + offset)),
932 "third seg start %u expected %u",
933 ooo_seg->start, fifo_pos (f, 300 + offset));
934 SFIFO_TEST ((ooo_seg->length == 100), "third seg length %u expected %u",
935 ooo_seg->length, 100);
936 SFIFO_TEST ((f->ooos_newest == 2), "newest %u", f->ooos_newest);
940 rv = svm_fifo_enqueue_with_offset (f, 190, 120, &test_data[190]);
942 vlib_cli_output (vm, "fifo after [190, 310] : %U",
943 format_svm_fifo, f, 1 /* verbose */ );
944 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 1),
945 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
946 ooo_seg = svm_fifo_first_ooo_segment (f);
947 SFIFO_TEST ((ooo_seg->start == fifo_pos (f, offset + 100)),
948 "first seg start %u expected %u",
949 ooo_seg->start, fifo_pos (f, offset + 100));
950 SFIFO_TEST ((ooo_seg->length == 300), "first seg length %u expected %u",
951 ooo_seg->length, 300);
956 rv = svm_fifo_enqueue (f, 150, test_data);
959 vlib_cli_output (vm, "fifo after [0 150] : %U", format_svm_fifo, f,
962 SFIFO_TEST ((rv == 400), "managed to enqueue %u expected %u", rv, 400);
963 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 0),
964 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
966 vec_validate (data_buf, 399);
967 svm_fifo_peek (f, 0, 400, data_buf);
968 if (compare_data (data_buf, test_data, 0, 400, &j))
970 SFIFO_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j],
975 * Add [100 200] and overlap it with [50 250]
977 ft_fifo_free (fs, f);
978 f = fifo_prepare (fs, fifo_size);
980 svm_fifo_enqueue_with_offset (f, 100, 100, &test_data[100]);
981 svm_fifo_enqueue_with_offset (f, 50, 200, &test_data[50]);
982 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 1),
983 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
984 ooo_seg = svm_fifo_first_ooo_segment (f);
985 SFIFO_TEST ((ooo_seg->start == 50), "first seg start %u expected %u",
987 SFIFO_TEST ((ooo_seg->length == 200), "first seg length %u expected %u",
988 ooo_seg->length, 200);
990 ft_fifo_free (fs, f);
991 ft_fifo_segment_free (fsm, fs);
992 vec_free (test_data);
997 * Test ooo head/tail u32 wrapping
1000 sfifo_test_fifo6 (vlib_main_t * vm, unformat_input_t * input)
1002 fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
1003 u32 fifo_size = 101, n_test_bytes = 100;
1004 int i, j, rv, __clib_unused verbose = 0;
1005 u8 *test_data = 0, *data_buf = 0;
1006 ooo_segment_t *ooo_seg;
1010 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1012 if (unformat (input, "verbose"))
1016 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
1022 fs = fifo_segment_prepare (fsm, "fifo-test6", 0);
1023 f = fifo_prepare (fs, fifo_size);
1025 vec_validate (test_data, n_test_bytes - 1);
1026 vec_validate (data_buf, n_test_bytes - 1);
1027 for (i = 0; i < vec_len (test_data); i++)
1028 test_data[i] = i % 0xff;
1031 * Add ooo with tail and ooo segment start u32 wrap
1033 svm_fifo_init_pointers (f, ~0 % fifo_size, ~0 % fifo_size);
1034 svm_fifo_enqueue_with_offset (f, 10, 10, &test_data[10]);
1035 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 1),
1036 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
1037 ooo_seg = svm_fifo_first_ooo_segment (f);
1038 rv = ooo_segment_offset_prod (f, ooo_seg);
1039 SFIFO_TEST (rv == 10, "offset should be %u is %u", 10, rv);
1041 svm_fifo_enqueue (f, 10, test_data);
1042 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 0),
1043 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
1044 SFIFO_TEST (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX,
1045 "there should be no ooo seg");
1047 svm_fifo_peek (f, 5, 10, &data_buf[5]);
1048 if (compare_data (data_buf, test_data, 5, 10, (u32 *) & j))
1049 SFIFO_TEST (0, "[%d] dequeued %u expected %u", j, data_buf[j],
1052 svm_fifo_dequeue (f, 20, data_buf);
1053 if (compare_data (data_buf, test_data, 0, 20, (u32 *) & j))
1054 SFIFO_TEST (0, "[%d] dequeued %u expected %u", j, data_buf[j],
1058 * Force collect with tail u32 wrap and without ooo segment start u32 wrap
1060 svm_fifo_init_pointers (f, (~0 - 10) % fifo_size, (~0 - 10) % fifo_size);
1061 svm_fifo_enqueue_with_offset (f, 5, 15, &test_data[5]);
1062 svm_fifo_enqueue (f, 12, test_data);
1064 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 0),
1065 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
1066 SFIFO_TEST (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX,
1067 "there should be no ooo seg");
1069 svm_fifo_dequeue (f, 20, data_buf);
1070 if (compare_data (data_buf, test_data, 0, 20, (u32 *) & j))
1071 SFIFO_TEST (0, "[%d] dequeued %u expected %u", j, data_buf[j],
1077 vec_free (test_data);
1078 vec_free (data_buf);
1079 ft_fifo_free (fs, f);
1080 ft_fifo_segment_free (fsm, fs);
1085 * Multiple ooo enqueues and dequeues that force fifo tail/head wrap
1088 sfifo_test_fifo7 (vlib_main_t * vm, unformat_input_t * input)
1090 fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
1091 u32 fifo_size = 101, n_iterations = 100;
1092 int i, j, rv, __clib_unused verbose = 0;
1093 u8 *test_data = 0, *data_buf = 0;
1094 u64 n_test_bytes = 100;
1098 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1100 if (unformat (input, "verbose"))
1104 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
1111 * Prepare data structures
1113 fs = fifo_segment_prepare (fsm, "fifo-test7", 0);
1114 f = fifo_prepare (fs, fifo_size);
1115 svm_fifo_init_pointers (f, ~0 % fifo_size, ~0 % fifo_size);
1117 vec_validate (test_data, n_test_bytes - 1);
1118 vec_validate (data_buf, n_test_bytes - 1);
1119 for (i = 0; i < vec_len (test_data); i++)
1120 test_data[i] = i % 0xff;
1123 * Run n iterations of test
1125 for (i = 0; i < n_iterations; i++)
1127 for (j = n_test_bytes - 1; j > 0; j -= 2)
1129 svm_fifo_enqueue_with_offset (f, j, 1, &test_data[j]);
1130 rv = svm_fifo_n_ooo_segments (f);
1131 if (rv != (n_test_bytes - j) / 2 + 1)
1132 SFIFO_TEST (0, "number of ooo segments expected %u is %u",
1133 (n_test_bytes - j) / 2 + 1, rv);
1136 svm_fifo_enqueue_with_offset (f, 1, n_test_bytes - 1, &test_data[1]);
1137 rv = svm_fifo_n_ooo_segments (f);
1139 SFIFO_TEST (0, "number of ooo segments %u", rv);
1141 svm_fifo_enqueue (f, 1, test_data);
1142 rv = svm_fifo_n_ooo_segments (f);
1144 SFIFO_TEST (0, "number of ooo segments %u", rv);
1146 svm_fifo_dequeue (f, n_test_bytes, data_buf);
1147 if (compare_data (data_buf, test_data, 0, n_test_bytes, (u32 *) & j))
1148 SFIFO_TEST (0, "[%d] dequeued %u expected %u", j, data_buf[j],
1150 svm_fifo_init_pointers (f, (~0 - i) % f->shr->size,
1151 (~0 - i) % f->shr->size);
1153 SFIFO_TEST (1, "passed multiple ooo enqueue/dequeue");
1158 vec_free (test_data);
1159 vec_free (data_buf);
1160 ft_fifo_free (fs, f);
1161 ft_fifo_segment_free (fsm, fs);
1166 * Enqueue more than 4GB
1169 sfifo_test_fifo_large (vlib_main_t * vm, unformat_input_t * input)
1171 u32 n_iterations = 100, n_bytes_per_iter, half, fifo_size;
1172 fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
1173 int i, j, rv, __clib_unused verbose = 0;
1174 u8 *test_data = 0, *data_buf = 0;
1175 u64 n_test_bytes = 100;
1179 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1181 if (unformat (input, "verbose"))
1185 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
1191 n_test_bytes = 5ULL << 30;
1192 n_iterations = 1 << 10;
1193 n_bytes_per_iter = n_test_bytes / n_iterations;
1194 fifo_size = n_bytes_per_iter + 1;
1196 fs = fifo_segment_prepare (fsm, "fifo-large", 0);
1197 f = fifo_prepare (fs, fifo_size);
1198 svm_fifo_init_pointers (f, ~0 % fifo_size, ~0 % fifo_size);
1200 vec_validate (test_data, n_bytes_per_iter - 1);
1201 vec_validate (data_buf, n_bytes_per_iter - 1);
1202 for (i = 0; i < vec_len (test_data); i++)
1203 test_data[i] = i % 0xff;
1205 half = n_bytes_per_iter / 2;
1206 for (i = 0; i < n_iterations; i++)
1208 svm_fifo_enqueue_with_offset (f, half, half, &test_data[half]);
1209 svm_fifo_enqueue (f, half, test_data);
1210 rv = svm_fifo_n_ooo_segments (f);
1212 SFIFO_TEST (0, "number of ooo segments %u", rv);
1213 svm_fifo_dequeue (f, n_bytes_per_iter, data_buf);
1214 if (compare_data (data_buf, test_data, 0, n_bytes_per_iter,
1216 SFIFO_TEST (0, "[%d][%d] dequeued %u expected %u", i, j, data_buf[j],
1219 SFIFO_TEST (1, "passed large transfer");
1221 ft_fifo_free (fs, f);
1222 ft_fifo_segment_free (fsm, fs);
1228 validate_test_and_buf_vecs (u8 ** test_data, u8 ** data_buf, u32 len)
1232 cur_len = vec_len (*test_data);
1233 vec_validate (*test_data, len - 1);
1234 vec_validate (*data_buf, len - 1);
1236 for (i = cur_len; i < vec_len (*test_data); i++)
1237 (*test_data)[i] = i;
1241 enqueue_ooo (svm_fifo_t * f, u8 * test_data, u32 len, u32 iterations)
1243 u32 offset, enq_now, ooo_chunk;
1246 ooo_chunk = len / iterations;
1247 for (i = iterations; i > 0; i--)
1249 offset = i * ooo_chunk;
1250 enq_now = clib_min (ooo_chunk, len - offset);
1253 rv = svm_fifo_enqueue_with_offset (f, offset, enq_now,
1254 test_data + offset);
1263 enqueue_ooo_packets (svm_fifo_t * f, u32 len, u32 enq_chunk, u8 * test_data)
1265 u32 offset, enq_now;
1268 for (i = 1; i <= len / enq_chunk; i++)
1270 offset = i * enq_chunk;
1271 enq_now = clib_min (enq_chunk, len - offset);
1274 rv = svm_fifo_enqueue_with_offset (f, offset, enq_now,
1275 test_data + offset);
1279 if (svm_fifo_size (f) < len - 4096)
1280 svm_fifo_set_size (f, svm_fifo_size (f) + enq_now);
1282 svm_fifo_set_size (f, len);
1289 enqueue_packets_inc (svm_fifo_t * f, u32 len, u32 enq_chunk, u8 * test_data)
1291 u32 enq_now, offset;
1294 for (i = 0; i <= len / enq_chunk; i++)
1296 offset = i * enq_chunk;
1297 enq_now = clib_min (enq_chunk, len - offset);
1298 rv = svm_fifo_enqueue (f, enq_now, test_data + offset);
1301 if (svm_fifo_size (f) < len - 4096)
1302 svm_fifo_set_size (f, svm_fifo_size (f) + enq_now);
1304 svm_fifo_set_size (f, len);
1310 dequeue_ooo (svm_fifo_t * f, u8 * data_buf, u32 len, u32 iterations)
1312 u32 offset, ooo_chunk, deq_now;
1315 ooo_chunk = len / iterations;
1316 for (i = iterations; i >= 0; i--)
1318 offset = i * ooo_chunk;
1319 deq_now = clib_min (ooo_chunk, len - offset);
1322 rv = svm_fifo_peek (f, offset, deq_now, data_buf + offset);
1330 dequeue_ooo_inc (svm_fifo_t * f, u8 * data_buf, u32 len, u32 iterations)
1332 u32 offset, ooo_chunk, deq_now;
1335 ooo_chunk = len / iterations;
1336 for (i = 0; i <= iterations; i++)
1338 offset = i * ooo_chunk;
1339 deq_now = clib_min (ooo_chunk, len - offset);
1342 rv = svm_fifo_peek (f, offset, deq_now, data_buf + offset);
1350 dequeue_packets (svm_fifo_t * f, u32 len, u32 deq_chunk, u8 * data_buf)
1352 u32 offset, deq_now;
1355 for (i = 0; i <= len / deq_chunk; i++)
1357 offset = i * deq_chunk;
1358 deq_now = clib_min (deq_chunk, len - offset);
1361 rv = svm_fifo_dequeue (f, deq_now, data_buf + offset);
1369 sfifo_test_fifo_grow (vlib_main_t * vm, unformat_input_t * input)
1371 int __clib_unused verbose = 0, fifo_size = 4096, fifo_inc = 4096, rv, i;
1372 u32 enq_chunk, offset, deq_now, last_start_byte;
1373 fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
1374 u8 *test_data = 0, *data_buf = 0;
1375 svm_fifo_chunk_t *c;
1379 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1381 if (unformat (input, "verbose"))
1385 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
1391 fs = fifo_segment_prepare (fsm, "fifo-grow", 0);
1392 f = fifo_prepare (fs, fifo_size);
1395 * Grow size and alloc chunks by enqueueing in order
1397 fifo_size += fifo_inc;
1398 svm_fifo_set_size (f, fifo_size);
1399 last_start_byte = 4096;
1400 validate_test_and_buf_vecs (&test_data, &data_buf, fifo_size);
1402 rv = svm_fifo_enqueue (f, fifo_size, test_data);
1404 SFIFO_TEST (rv == fifo_size, "enqueue should work");
1405 SFIFO_TEST (svm_fifo_size (f) == fifo_size, "size expected %u is %u",
1406 fifo_size, svm_fifo_size (f));
1407 SFIFO_TEST (svm_fifo_max_dequeue (f) == fifo_size, "max deq should be %u",
1409 rv = svm_fifo_n_chunks (f);
1410 SFIFO_TEST (rv == 2, "should have 2 chunks has %u", rv);
1411 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1413 c = f_head_cptr (f);
1414 SFIFO_TEST (c->start_byte == 0, "head start byte should be %u", 0);
1415 SFIFO_TEST (c->length == 4096, "head chunk length should be %u", 4096);
1416 SFIFO_TEST (f->shr->tail_chunk == 0, "no tail chunk");
1417 SFIFO_TEST (f->ooo_enq == 0, "should have no ooo enq chunk");
1418 SFIFO_TEST (f->ooo_deq == 0, "should have no ooo deq chunk");
1420 SFIFO_TEST (c->start_byte == last_start_byte, "end chunk start byte should"
1421 " be %u", last_start_byte);
1422 SFIFO_TEST (c->length == 4096, "end chunk length should be %u", 4096);
1425 * Dequeue and validate data
1428 rv = svm_fifo_dequeue (f, fifo_size, data_buf);
1429 SFIFO_TEST (rv == fifo_size, "should dequeue all data");
1430 last_start_byte += 4096; /* size of last segment */
1432 rv = compare_data (data_buf, test_data, 0, vec_len (test_data),
1436 vlib_cli_output (vm, "[%d] dequeued %u expected %u", i, data_buf[i],
1438 SFIFO_TEST ((rv == 0), "dequeued compared to original returned %d", rv);
1439 SFIFO_TEST (f->shr->head_chunk == 0, "head chunk should be 0");
1440 SFIFO_TEST (f->shr->tail_chunk == 0, "tail chunk should be 0");
1441 SFIFO_TEST (f->ooo_deq == 0, "should have no ooo deq chunk");
1442 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1445 * Allocate one new chunk by enqueueing out of order all but first chunk
1449 enq_chunk = vec_len (test_data) / 10;
1450 rv = enqueue_ooo (f, test_data, vec_len (test_data), 10);
1451 SFIFO_TEST (!rv, "enqueue ooo should work");
1453 SFIFO_TEST (svm_fifo_size (f) == fifo_size, "size expected %u is %u",
1454 fifo_size, svm_fifo_size (f));
1455 SFIFO_TEST (svm_fifo_max_dequeue (f) == 0, "max deq should be %u", 0);
1456 /* Fifo has 2 chunks because the we didn't allow the first chunk to be
1457 * freed when all the data was dequeued. Could be optimized in the future */
1458 rv = svm_fifo_n_chunks (f);
1459 SFIFO_TEST (rv == 2, "should have %u chunks has %u", 2, rv);
1460 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1462 SFIFO_TEST (f->shr->head_chunk == 0, "should have no head chunk");
1463 /* When new fifo chunks are allocated, tail is initialized */
1464 SFIFO_TEST (f->shr->tail_chunk != 0, "should have no tail chunk");
1465 SFIFO_TEST (f->ooo_enq != 0, "should have an ooo enq chunk");
1468 SFIFO_TEST (c->start_byte == last_start_byte,
1469 "end chunk should start at %u", last_start_byte);
1470 SFIFO_TEST (c->length == 8192, "end chunk length should be %u", 8192);
1471 SFIFO_TEST (f->ooo_enq == c, "ooo enq chunk should be end chunk");
1474 * Enqueue the first chunk
1476 rv = svm_fifo_enqueue (f, enq_chunk, test_data);
1477 SFIFO_TEST (rv == fifo_size, "enq should succeed %u", rv);
1478 rv = svm_fifo_max_dequeue (f);
1479 SFIFO_TEST (rv == fifo_size, "max deq should be %u is %u", fifo_size, rv);
1480 rv = svm_fifo_n_chunks (f);
1481 SFIFO_TEST (rv == 2, "should have %u chunks has %u", 2, rv);
1482 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1484 SFIFO_TEST (f->shr->head_chunk == 0, "should have no head chunk");
1485 /* Fifo is full so tail and ooo_enq should be 0 */
1486 SFIFO_TEST (f->shr->tail_chunk == 0, "should have no tail chunk");
1487 SFIFO_TEST (f->ooo_enq == 0, "should have no ooo enq chunk");
1490 * Peek and validate data
1493 memset (data_buf, 0, vec_len (data_buf));
1495 rv = dequeue_ooo_inc (f, data_buf, fifo_size, 10);
1496 SFIFO_TEST (!rv, "ooo deq should work %d", rv);
1498 rv = compare_data (data_buf, test_data, 0, vec_len (test_data),
1501 vlib_cli_output (vm, "[%d] dequeued %u expected %u", i, data_buf[i],
1503 SFIFO_TEST ((rv == 0), "peeked compared to original returned %d", rv);
1504 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1505 /* Peeked all the data in a full fifo so ooo_deq ends up 0 */
1506 SFIFO_TEST (f->ooo_deq == 0, "should have no ooo deq chunk");
1509 * Peek in reverse order and validate data
1511 * RB tree should be exercised
1514 memset (data_buf, 0, vec_len (data_buf));
1515 for (i = 10; i >= 0; i--)
1517 offset = i * enq_chunk;
1518 deq_now = clib_min (enq_chunk, vec_len (test_data) - offset);
1519 rv = svm_fifo_peek (f, offset, deq_now, data_buf + offset);
1521 SFIFO_TEST (0, "failed to peek");
1524 rv = compare_data (data_buf, test_data, 0, vec_len (test_data),
1528 vlib_cli_output (vm, "[%d] dequeued %u expected %u", i, data_buf[i],
1530 SFIFO_TEST ((rv == 0), "peeked compared to original returned %d", rv);
1531 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1532 /* Last chunk peeked is the first, so ooo_deq should be non zero */
1533 SFIFO_TEST (f->ooo_deq != 0, "should have ooo deq chunk");
1536 * Dequeue drop all bytes
1538 rv = svm_fifo_dequeue_drop (f, fifo_size);
1539 SFIFO_TEST ((rv == fifo_size), "all bytes should be dropped %u", rv);
1540 last_start_byte += 8192;
1542 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1543 SFIFO_TEST (f->shr->head_chunk == 0, "should have no head chunk");
1544 SFIFO_TEST (f->shr->tail_chunk == 0, "should have no tail chunk");
1546 /* We don't remove the last chunk even when the fifo goes empty */
1547 rv = svm_fifo_n_chunks (f);
1548 SFIFO_TEST (rv == 1, "should have %u chunks has %u", 1, rv);
1551 * Increase size such that it can't be the sum of multiple chunk lengths
1553 * A chunk of 16kB should be allocated
1555 fifo_size += 2 * fifo_inc - 100;
1556 svm_fifo_set_size (f, fifo_size);
1557 validate_test_and_buf_vecs (&test_data, &data_buf, fifo_size + fifo_inc);
1558 enq_chunk = vec_len (test_data) / 10;
1559 memset (data_buf, 0, vec_len (data_buf));
1564 rv = enqueue_ooo (f, test_data, fifo_size, 10);
1565 SFIFO_TEST (!rv, "enqueue ooo should work");
1567 SFIFO_TEST (svm_fifo_size (f) == fifo_size, "size expected %u is %u",
1568 fifo_size, svm_fifo_size (f));
1569 SFIFO_TEST (svm_fifo_max_dequeue (f) == 0, "max deq should be %u", 0);
1570 /* Fifo has 2 chunks because the we didn't allow the first chunk to be
1571 * freed when all the data was dequeued. Could be optimized in the future */
1572 rv = svm_fifo_n_chunks (f);
1573 SFIFO_TEST (rv == 2, "should have %u chunks has %u", 2, rv);
1574 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1576 SFIFO_TEST (f->shr->head_chunk == 0, "should have no head chunk");
1577 /* When new fifo chunks are allocated, tail is initialized */
1578 SFIFO_TEST (f->shr->tail_chunk != 0, "should have no tail chunk");
1579 SFIFO_TEST (f->ooo_enq != 0, "should have an ooo enq chunk");
1582 SFIFO_TEST (c->start_byte == last_start_byte,
1583 "end chunk should start at %u", last_start_byte);
1584 SFIFO_TEST (c->length == 16384, "end chunk length should be %u", 16384);
1585 SFIFO_TEST (f->ooo_enq == c, "ooo enq chunk should be end chunk");
1588 * Enqueue the first chunk
1590 rv = svm_fifo_enqueue (f, enq_chunk, test_data);
1591 SFIFO_TEST (rv == fifo_size, "enq should succeed %u", rv);
1592 rv = svm_fifo_max_dequeue (f);
1593 SFIFO_TEST (rv == fifo_size, "max deq should be %u is %u", fifo_size, rv);
1594 rv = svm_fifo_n_chunks (f);
1595 SFIFO_TEST (rv == 2, "should have %u chunks has %u", 2, rv);
1596 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1599 * Dequeue just a part of data. Because we're tracking ooo data, we can't
1600 * call dequeue. Therefore, first peek and then dequeue drop
1602 rv = svm_fifo_peek (f, 0, fifo_inc, data_buf);
1603 SFIFO_TEST (rv == fifo_inc, "should dequeue all data");
1604 rv = svm_fifo_dequeue_drop (f, fifo_inc);
1605 SFIFO_TEST (rv == fifo_inc, "should dequeue all data");
1606 rv = svm_fifo_n_chunks (f);
1607 SFIFO_TEST (rv == 1, "should have %u chunks has %u", 1, rv);
1608 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1611 * Enqueue ooo as much data as it was dequeued
1613 rv = enqueue_ooo (f, test_data + fifo_size, fifo_inc, 2);
1614 SFIFO_TEST (!rv, "ooo enqueue should work %d", rv);
1616 rv = svm_fifo_enqueue (f, fifo_inc / 2, test_data + fifo_size);
1617 SFIFO_TEST (rv == fifo_inc, "enqueue should work %d", rv);
1619 rv = svm_fifo_n_chunks (f);
1620 SFIFO_TEST (rv == 2, "should have %u chunks has %u", 2, rv);
1621 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1623 last_start_byte += 16384;
1625 SFIFO_TEST (c->start_byte == last_start_byte,
1626 "end chunk should start at %u", last_start_byte);
1627 SFIFO_TEST (c->length == 4096, "end chunk length should be %u", 4096);
1630 * Dequeue all. Don't call dequeue see above
1632 rv = svm_fifo_peek (f, 0, fifo_size, data_buf + fifo_inc);
1633 SFIFO_TEST (rv == fifo_size, "should dequeue all data");
1634 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1636 rv = compare_data (data_buf, test_data, 0, vec_len (test_data),
1639 vlib_cli_output (vm, "[%d] dequeued %u expected %u", i, data_buf[i],
1641 SFIFO_TEST ((rv == 0), "dequeued compared to original returned %d", rv);
1643 rv = svm_fifo_dequeue_drop (f, fifo_size);
1644 SFIFO_TEST (rv == fifo_size, "should dequeue all data");
1646 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1647 /* fifo does not end on chunk boundary because of the - 100 */
1648 SFIFO_TEST (f->shr->head_chunk != 0, "should have head chunk");
1649 SFIFO_TEST (f->shr->tail_chunk != 0, "should have tail chunk");
1652 * Enqueue and dequeue byte-by-byte ooo
1655 memset (data_buf, 0, vec_len (data_buf));
1657 rv = enqueue_ooo (f, test_data, fifo_size, fifo_size);
1658 SFIFO_TEST (!rv, "ooo enqueue should work %d", rv);
1660 rv = svm_fifo_enqueue (f, 1, test_data);
1661 SFIFO_TEST (rv == fifo_size, "enqueue should work %d", rv);
1663 rv = dequeue_ooo (f, data_buf, fifo_size, fifo_size);
1664 SFIFO_TEST (!rv, "ooo deq should work %d", rv);
1666 rv = compare_data (data_buf, test_data, 0, fifo_size, (u32 *) & i);
1668 vlib_cli_output (vm, "[%d] dequeued %u expected %u", i, data_buf[i],
1670 SFIFO_TEST ((rv == 0), "dequeued compared to original returned %d", rv);
1671 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1673 last_start_byte += 4096;
1675 SFIFO_TEST (c->start_byte == last_start_byte,
1676 "end chunk should start at %u", last_start_byte);
1677 SFIFO_TEST (c->length == 16384, "end chunk length should be %u", 16384);
1679 rv = svm_fifo_n_chunks (f);
1680 SFIFO_TEST (rv == 2, "should have %u chunks has %u", 2, rv);
1683 * Dequeue drop all bytes
1685 rv = svm_fifo_dequeue_drop (f, fifo_size);
1686 SFIFO_TEST ((rv == fifo_size), "all bytes should be dropped %u", rv);
1688 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1689 SFIFO_TEST (f->shr->head_chunk != 0, "should have head chunk");
1690 SFIFO_TEST (f->shr->tail_chunk != 0, "should have tail chunk");
1692 /* We don't remove the last chunk even when the fifo goes empty */
1693 rv = svm_fifo_n_chunks (f);
1694 SFIFO_TEST (rv == 1, "should have %u chunks has %u", 1, rv);
1695 SFIFO_TEST (f->ooo_enq == 0, "should have no ooo enq chunk");
1696 SFIFO_TEST (f->ooo_deq == 0, "should have no ooo deq chunk");
1699 * Grow fifo to 4MB and force only 4kB chunk allocations
1701 fifo_size = 4 << 20;
1702 svm_fifo_set_size (f, fifo_inc);
1703 validate_test_and_buf_vecs (&test_data, &data_buf, fifo_size);
1705 memset (data_buf, 0, vec_len (data_buf));
1707 rv = enqueue_packets_inc (f, fifo_size, enq_chunk, test_data);
1708 SFIFO_TEST (!rv, "incremental packet enqueue should work");
1710 SFIFO_TEST (svm_fifo_max_dequeue (f) == fifo_size, "max deq should be %u",
1712 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1714 rv = svm_fifo_n_chunks (f);
1715 SFIFO_TEST (rv == (fifo_size / 4096) + 1, "should have %u chunks has %u",
1716 (fifo_size / 4096) + 1, rv);
1723 /* Because we're tracking ooo data, we can't call dequeue. Therefore,
1724 * first peek and then dequeue drop */
1725 rv = svm_fifo_peek (f, 0, fifo_size, data_buf);
1726 SFIFO_TEST (rv == fifo_size, "should dequeue all data");
1728 rv = compare_data (data_buf, test_data, 0, vec_len (test_data),
1731 vlib_cli_output (vm, "[%d] dequeued %u expected %u", i, data_buf[i],
1733 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1734 SFIFO_TEST ((rv == 0), "dequeued compared to original returned %d", rv);
1737 rv = svm_fifo_dequeue_drop (f, fifo_size);
1738 SFIFO_TEST ((rv == fifo_size), "all bytes should be dropped %u", rv);
1739 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1740 SFIFO_TEST (f->ooo_deq == 0, "should have no ooo deq chunk");
1741 rv = svm_fifo_n_chunks (f);
1742 SFIFO_TEST (rv == 1, "should have %u chunks has %u", 1, rv);
1748 ft_fifo_free (fs, f);
1749 ft_fifo_segment_free (fsm, fs);
1750 vec_free (test_data);
1751 vec_free (data_buf);
1756 sfifo_test_fifo_shrink (vlib_main_t * vm, unformat_input_t * input)
1758 int __clib_unused verbose = 0, fifo_size = 4096, deq_chunk;
1759 fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
1760 u8 *test_data = 0, *data_buf = 0;
1766 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1768 if (unformat (input, "verbose"))
1772 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
1779 * Init fifo and enqueue data such that multiple 4096 chunks are allocated
1781 fs = fifo_segment_prepare (fsm, "fifo-shrink", 0);
1782 f = fifo_prepare (fs, fifo_size);
1784 fifo_size = 4 << 20;
1785 svm_fifo_set_size (f, 4096);
1786 validate_test_and_buf_vecs (&test_data, &data_buf, fifo_size);
1788 rv = enqueue_packets_inc (f, fifo_size, enq_chunk, test_data);
1789 SFIFO_TEST (!rv, "incremental packet enqueue should work");
1791 rv = svm_fifo_max_enqueue (f);
1792 SFIFO_TEST (rv == 0, "enqueue space %u", rv);
1793 SFIFO_TEST (svm_fifo_max_dequeue (f) == fifo_size, "max deq should be %u",
1795 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1797 rv = svm_fifo_n_chunks (f);
1798 SFIFO_TEST (rv == (fifo_size / 4096), "should have %u chunks has %u",
1799 (fifo_size / 4096), rv);
1802 * Dequeue enough to collect one chunk
1805 rv = svm_fifo_dequeue (f, deq_chunk, data_buf);
1806 SFIFO_TEST (rv == deq_chunk, "should dequeue all data");
1808 rv = svm_fifo_n_chunks (f);
1809 SFIFO_TEST (rv == (fifo_size / 4096) - 1, "should have %u chunks has %u",
1810 (fifo_size / 4096) - 1, rv);
1811 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1813 rv = svm_fifo_max_enqueue (f);
1814 SFIFO_TEST (rv == deq_chunk, "enqueue space %u", rv);
1817 * Dequeue ooo byte-by-byte remaining data
1819 rv = dequeue_ooo (f, data_buf + deq_chunk, fifo_size - deq_chunk,
1820 fifo_size - deq_chunk);
1821 SFIFO_TEST (!rv, "ooo deq should work %d", rv);
1823 rv = compare_data (data_buf, test_data, 0, fifo_size, (u32 *) & i);
1825 vlib_cli_output (vm, "[%d] dequeued %u expected %u", i, data_buf[i],
1828 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1829 rv = svm_fifo_n_chunks (f);
1830 SFIFO_TEST (rv == (fifo_size / 4096) - 1, "should have %u chunks has %u",
1831 (fifo_size / 4096) - 1, rv);
1836 rv = svm_fifo_dequeue_drop (f, fifo_size - deq_chunk);
1837 SFIFO_TEST (rv == fifo_size - deq_chunk, "should drop all data");
1838 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1839 rv = svm_fifo_n_chunks (f);
1840 SFIFO_TEST (rv == 1, "should have %u chunks has %u", 1, rv);
1841 rv = svm_fifo_max_enqueue (f);
1842 SFIFO_TEST (rv == fifo_size, "enqueue space %u", rv);
1846 * Reset size and enqueue ooo all data
1848 svm_fifo_set_size (f, 4096);
1849 enq_chunk = deq_chunk = 1500;
1850 rv = enqueue_ooo_packets (f, vec_len (test_data), 1500, test_data);
1851 SFIFO_TEST (!rv, "enqueue ooo should work");
1852 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1854 /* 1 additional chunk left from previous test */
1855 rv = svm_fifo_n_chunks (f);
1856 SFIFO_TEST (rv == (fifo_size / 4096) + 1, "should have %u chunks has %u",
1857 (fifo_size / 4096) + 1, rv);
1860 * Add missing first chunk
1862 rv = svm_fifo_enqueue (f, enq_chunk, test_data);
1863 SFIFO_TEST (rv == fifo_size, "enq should succeed %u", rv);
1864 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1865 rv = svm_fifo_max_dequeue (f);
1866 SFIFO_TEST (rv == fifo_size, "max deq should be %u is %u", fifo_size, rv);
1867 rv = svm_fifo_n_chunks (f);
1868 SFIFO_TEST (rv == (fifo_size / 4096) + 1, "should have %u chunks has %u",
1869 (fifo_size / 4096) + 1, rv);
1872 * Dequeue as packets
1874 memset (data_buf, 0, vec_len (data_buf));
1875 rv = dequeue_packets (f, fifo_size, deq_chunk, data_buf);
1876 SFIFO_TEST (!rv, "deq pkts should work %d", rv);
1878 rv = compare_data (data_buf, test_data, 0, fifo_size, (u32 *) & i);
1880 vlib_cli_output (vm, "[%d] dequeued %u expected %u", i, data_buf[i],
1884 * Enqueue and dequeue set of packets
1886 svm_fifo_set_size (f, 4096);
1887 for (i = 0; i < 1000; i++)
1889 rv = svm_fifo_enqueue (f, enq_chunk, test_data);
1890 if (rv != enq_chunk)
1891 SFIFO_TEST (0, "enq fail");
1892 rv = svm_fifo_dequeue (f, deq_chunk, data_buf);
1893 if (rv != deq_chunk)
1894 SFIFO_TEST (0, "deq fail");
1901 ft_fifo_free (fs, f);
1902 ft_fifo_segment_free (fsm, fs);
1903 vec_free (test_data);
1904 vec_free (data_buf);
1910 sfifo_test_fifo_indirect (vlib_main_t * vm, unformat_input_t * input)
1912 int __clib_unused verbose = 0, fifo_size = 4096, deq_chunk;
1913 fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
1914 u8 *test_data = 0, *data_buf = 0;
1915 svm_fifo_chunk_t *c;
1920 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1922 if (unformat (input, "verbose"))
1926 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
1933 * Init fifo and enqueue data such that multiple 4096 chunks are allocated
1935 fs = fifo_segment_prepare (fsm, "fifo-indirect", 0);
1936 f = fifo_prepare (fs, fifo_size);
1938 fifo_size = 4 << 20;
1939 svm_fifo_set_size (f, fifo_size);
1940 validate_test_and_buf_vecs (&test_data, &data_buf, fifo_size);
1942 c = f_start_cptr (f);
1943 SFIFO_TEST (c->next == 0, "no next");
1945 svm_fifo_fill_chunk_list (f);
1946 SFIFO_TEST (c->next != 0, "new chunk should've been allocated");
1947 SFIFO_TEST (f_cptr (f, c->next)->length == 4 << 20,
1948 "new chunk should be 4MB");
1950 rv = svm_fifo_max_write_chunk (f);
1951 SFIFO_TEST (rv == 4096, "max write chunk %u", rv);
1954 * Enqueue enough to fill first chunk
1956 svm_fifo_enqueue_nocopy (f, 4096);
1957 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1959 c = f_tail_cptr (f);
1960 SFIFO_TEST (c == f_end_cptr (f), "tail is end chunk");
1962 /* Initialize head chunk */
1963 rv = svm_fifo_max_read_chunk (f);
1964 SFIFO_TEST (rv == 4096, "max read chunk %u", rv);
1965 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1968 * Move head over last segment
1970 rv = svm_fifo_dequeue (f, 4096, data_buf);
1971 SFIFO_TEST (rv == 4096, "dequeue should work");
1973 c = f_head_cptr (f);
1974 SFIFO_TEST (c == f_end_cptr (f), "head chunk should be last");
1976 rv = svm_fifo_max_read_chunk (f);
1977 SFIFO_TEST (rv == 0, "max read chunk %u", rv);
1979 rv = svm_fifo_max_write_chunk (f);
1980 SFIFO_TEST (rv == 4 << 20, "max write chunk %u", rv);
1986 ft_fifo_free (fs, f);
1987 ft_fifo_segment_free (fsm, fs);
1988 vec_free (test_data);
1989 vec_free (data_buf);
1994 svm_fifo_trace_elem_t fifo_trace[] = {};
1997 sfifo_test_fifo_replay (vlib_main_t * vm, unformat_input_t * input)
2001 u8 no_read = 0, *str = 0;
2003 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2005 if (unformat (input, "verbose"))
2007 else if (unformat (input, "no-read"))
2011 clib_error_t *e = clib_error_return
2012 (0, "unknown input `%U'", format_unformat_error, input);
2013 clib_error_report (e);
2019 f.trace = fifo_trace;
2022 str = svm_fifo_replay (str, &f, no_read, verbose);
2023 vlib_cli_output (vm, "%v", str);
2028 sfifo_test_fifo_make_rcv_wnd_zero (vlib_main_t * vm, unformat_input_t * input)
2030 int __clib_unused verbose = 0, fifo_size = 4096, deq_chunk;
2031 fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
2032 u8 *test_data = 0, *data_buf = 0;
2037 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2039 if (unformat (input, "verbose"))
2043 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
2050 * Init fifo and enqueue data such that multiple 4096 chunks are allocated
2052 fs = fifo_segment_prepare (fsm, "fifo-rcv-wnd-zero", 0);
2053 f = fifo_prepare (fs, fifo_size);
2055 /* Enqueue 3000 into 4KB chunk, so there'll be 1096 free space */
2056 svm_fifo_set_size (f, 4096);
2057 validate_test_and_buf_vecs (&test_data, &data_buf, fifo_size);
2058 rv = svm_fifo_enqueue (f, 3000, test_data);
2059 SFIFO_TEST (rv == 3000, "enqueued %u", rv);
2060 rv = svm_fifo_max_enqueue (f);
2061 SFIFO_TEST (rv == 1096, "svm_fifo_max_enqueue %u", rv);
2062 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
2064 /* Shrink fifo size to the in-use size */
2065 svm_fifo_set_size (f, 3000);
2066 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
2068 /* In TCP, this should result in rcv-wnd = 0 */
2069 rv = svm_fifo_max_enqueue (f);
2070 SFIFO_TEST (rv == 0, "svm_fifo_max_enqueue %u", rv);
2071 rv = svm_fifo_max_enqueue_prod (f);
2072 SFIFO_TEST (rv == 0, "svm_fifo_max_enqueue_prod %u", rv);
2074 /* Dequeue and ... */
2075 rv = svm_fifo_dequeue (f, 3000, data_buf);
2076 SFIFO_TEST (rv == 3000, "dequeued %u", rv);
2079 ft_fifo_free (fs, f);
2080 ft_fifo_segment_free (fsm, fs);
2081 vec_free (test_data);
2082 vec_free (data_buf);
2088 static fifo_segment_main_t segment_main;
2091 sfifo_test_fifo_segment_hello_world (int verbose)
2093 fifo_segment_create_args_t _a, *a = &_a;
2094 fifo_segment_main_t *sm = &segment_main;
2095 u8 *test_data, *retrieved_data = 0;
2100 clib_memset (a, 0, sizeof (*a));
2101 a->segment_name = "fifo-test1";
2102 a->segment_size = 256 << 10;
2103 a->segment_type = SSVM_SEGMENT_PRIVATE;
2105 rv = fifo_segment_create (sm, a);
2106 SFIFO_TEST (!rv, "svm_fifo_segment_create returned %d", rv);
2108 fs = fifo_segment_get_segment (sm, a->new_segment_indices[0]);
2109 f = fifo_segment_alloc_fifo (fs, 4096, FIFO_SEGMENT_RX_FIFO);
2111 SFIFO_TEST (f != 0, "svm_fifo_segment_alloc_fifo");
2113 test_data = format (0, "Hello world%c", 0);
2114 vec_validate (retrieved_data, vec_len (test_data) - 1);
2116 while (svm_fifo_max_enqueue (f) >= vec_len (test_data))
2117 svm_fifo_enqueue (f, vec_len (test_data), test_data);
2119 while (svm_fifo_max_dequeue (f) >= vec_len (test_data))
2120 svm_fifo_dequeue (f, vec_len (retrieved_data), retrieved_data);
2122 while (svm_fifo_max_enqueue (f) >= vec_len (test_data))
2123 svm_fifo_enqueue (f, vec_len (test_data), test_data);
2125 while (svm_fifo_max_dequeue (f) >= vec_len (test_data))
2126 svm_fifo_dequeue (f, vec_len (retrieved_data), retrieved_data);
2128 SFIFO_TEST (!memcmp (retrieved_data, test_data, vec_len (test_data)),
2129 "data should be identical");
2131 vec_free (test_data);
2132 vec_free (retrieved_data);
2133 vec_free (a->new_segment_indices);
2134 fifo_segment_free_fifo (fs, f);
2135 fifo_segment_delete (sm, fs);
2140 sfifo_test_fifo_segment_fifo_grow (int verbose)
2142 int rv, fifo_size = 4096, n_chunks, n_batch;
2143 fifo_segment_main_t *sm = &segment_main;
2144 fifo_segment_create_args_t _a, *a = &_a;
2145 u8 *test_data = 0, *data_buf = 0;
2146 u32 n_free_chunk_bytes, new_size;
2150 clib_memset (a, 0, sizeof (*a));
2151 a->segment_name = "fifo-test1";
2152 /* size chosen to be able to force multi chunk allocation lower */
2153 a->segment_size = 256 << 10;
2154 /* overhead that reduces the amount of space dedicated to fifos */
2155 a->segment_size += 1 << 14;
2156 a->segment_type = SSVM_SEGMENT_PRIVATE;
2158 /* fifo allocation allocates chunks in batch */
2159 n_batch = FIFO_SEGMENT_ALLOC_BATCH_SIZE;
2161 rv = fifo_segment_create (sm, a);
2163 SFIFO_TEST (!rv, "svm_fifo_segment_create returned %d", rv);
2168 fs = fifo_segment_get_segment (sm, a->new_segment_indices[0]);
2169 fs->h->pct_first_alloc = 100;
2170 f = fifo_segment_alloc_fifo (fs, fifo_size, FIFO_SEGMENT_RX_FIFO);
2172 SFIFO_TEST (f != 0, "svm_fifo_segment_alloc_fifo");
2174 n_chunks = fifo_segment_num_free_chunks (fs, fifo_size);
2175 SFIFO_TEST (n_chunks == n_batch - 1, "free 2^10B chunks "
2176 "should be %u is %u", n_batch - 1, n_chunks);
2177 rv = fifo_segment_fl_chunk_bytes (fs);
2178 SFIFO_TEST (rv == (n_batch - 1) * fifo_size, "free chunk bytes %u "
2179 "expected %u", rv, (n_batch - 1) * fifo_size);
2182 * Grow fifo by preallocated fifo_size chunk
2184 svm_fifo_set_size (f, 2 * fifo_size);
2185 validate_test_and_buf_vecs (&test_data, &data_buf, 2 * fifo_size);
2187 rv = svm_fifo_enqueue (f, vec_len (test_data), test_data);
2188 SFIFO_TEST (rv == vec_len (test_data), "enq should succeed %u", rv);
2189 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
2191 n_chunks = fifo_segment_num_free_chunks (fs, fifo_size);
2192 SFIFO_TEST (n_chunks == n_batch - 2, "free 2^10B chunks "
2193 "should be %u is %u", n_batch - 2, n_chunks);
2194 rv = fifo_segment_fl_chunk_bytes (fs);
2195 SFIFO_TEST (rv == (n_batch - 2) * fifo_size, "free chunk bytes %u "
2196 "expected %u", rv, (n_batch - 2) * fifo_size);
2198 /* Grow by a size not preallocated but first make sure there's space */
2199 rv = fifo_segment_free_bytes (fs);
2200 SFIFO_TEST (rv > 16 * fifo_size, "free bytes %u more than %u", rv,
2203 /* Force fifo growth */
2204 svm_fifo_set_size (f, svm_fifo_size (f) + 16 * fifo_size);
2205 validate_test_and_buf_vecs (&test_data, &data_buf, svm_fifo_size (f));
2206 rv = svm_fifo_enqueue (f, vec_len (test_data), test_data);
2208 SFIFO_TEST (svm_fifo_size (f) == 18 * fifo_size, "fifo size should be %u "
2209 "is %u", 18 * fifo_size, svm_fifo_size (f));
2211 rv = fifo_segment_fl_chunk_bytes (fs);
2212 SFIFO_TEST (rv == (n_batch - 2) * fifo_size, "free chunk bytes %u "
2213 "expected %u", rv, (n_batch - 2) * fifo_size);
2216 * Free and test free list size
2218 fifo_segment_free_fifo (fs, f);
2220 rv = fifo_segment_fl_chunk_bytes (fs);
2221 SFIFO_TEST (rv == (16 + n_batch) * fifo_size, "free chunk bytes expected %u"
2222 " is %u", (16 + n_batch) * fifo_size, rv);
2223 n_chunks = fifo_segment_num_free_chunks (fs, fifo_size);
2224 SFIFO_TEST (n_chunks == n_batch, "free 2^10B chunks "
2225 "should be %u is %u", n_batch, n_chunks);
2226 n_chunks = fifo_segment_num_free_chunks (fs, 16 * fifo_size);
2227 SFIFO_TEST (n_chunks == 1, "free 2^14B chunks should be %u is %u", 1,
2229 n_chunks = fifo_segment_num_free_chunks (fs, ~0);
2230 SFIFO_TEST (n_chunks == 1 + n_batch, "free chunks should be %u is %u",
2231 1 + n_batch, n_chunks);
2236 f = fifo_segment_alloc_fifo (fs, fifo_size, FIFO_SEGMENT_RX_FIFO);
2238 /* Force chunk allocation */
2239 svm_fifo_set_size (f, svm_fifo_size (f) + fifo_size);
2240 rv = svm_fifo_enqueue (f, svm_fifo_size (f), test_data);
2242 SFIFO_TEST (rv == svm_fifo_size (f), "enq should succeed %u", rv);
2243 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
2245 n_chunks = fifo_segment_num_free_chunks (fs, fifo_size);
2246 SFIFO_TEST (n_chunks == n_batch - 2, "free 2^10B chunks should be %u is %u",
2247 n_batch - 2, n_chunks);
2249 /* Grow and alloc 16 * fifo_size chunk */
2250 svm_fifo_set_size (f, svm_fifo_size (f) + 16 * fifo_size);
2251 rv = svm_fifo_enqueue (f, svm_fifo_size (f), test_data);
2253 n_chunks = fifo_segment_num_free_chunks (fs, 16 * fifo_size);
2254 SFIFO_TEST (n_chunks == 0, "free 2^14B chunks should be %u is %u", 0,
2256 n_chunks = fifo_segment_num_free_chunks (fs, ~0);
2257 SFIFO_TEST (n_chunks == n_batch - 2, "free chunks should be %u is %u",
2258 n_batch - 2, n_chunks);
2263 fifo_segment_free_fifo (fs, f);
2264 n_chunks = fifo_segment_num_free_chunks (fs, ~0);
2265 SFIFO_TEST (n_chunks == 1 + n_batch, "free chunks should be %u is %u",
2266 1 + n_batch, n_chunks);
2268 rv = fifo_segment_fl_chunk_bytes (fs);
2269 SFIFO_TEST (rv == (16 + n_batch) * fifo_size, "free chunk bytes expected %u"
2270 " is %u", (16 + n_batch) * fifo_size, rv);
2272 n_free_chunk_bytes = rv;
2275 * Allocate non power of 2 fifo/chunk and check that free chunk bytes
2276 * is correctly updated
2279 f = fifo_segment_alloc_fifo (fs, 16 * fifo_size - 1, FIFO_SEGMENT_RX_FIFO);
2280 rv = fifo_segment_fl_chunk_bytes (fs);
2282 SFIFO_TEST (n_free_chunk_bytes - 16 * fifo_size == rv, "free chunk bytes "
2283 "expected %u is %u", n_free_chunk_bytes - 16 * fifo_size, rv);
2284 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
2286 fifo_segment_free_fifo (fs, f);
2287 rv = fifo_segment_fl_chunk_bytes (fs);
2289 SFIFO_TEST (n_free_chunk_bytes == rv, "free chunk bytes expected %u is %u",
2290 n_free_chunk_bytes, rv);
2293 * Force multi chunk fifo allocation
2296 /* Check that we can force multi chunk allocation. Note that fifo size
2297 * rounded up to power of 2, i.e., 17 becomes 32 */
2298 rv = fifo_segment_free_bytes (fs);
2299 SFIFO_TEST (rv < 32 * fifo_size, "free bytes %u less than %u", rv,
2302 f = fifo_segment_alloc_fifo (fs, 17 * fifo_size, FIFO_SEGMENT_RX_FIFO);
2303 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
2305 rv = fifo_segment_fl_chunk_bytes (fs);
2307 /* Make sure that the non-power of two chunk freed above is correctly
2308 * accounted for in the chunk free bytes reduction due to chunk allocation
2309 * for the fifo, i.e., it's rounded up by 1 */
2310 SFIFO_TEST (n_free_chunk_bytes - 17 * fifo_size == rv, "free chunk bytes "
2311 "expected %u is %u", n_free_chunk_bytes - 17 * fifo_size, rv);
2313 fifo_segment_free_fifo (fs, f);
2315 rv = fifo_segment_fl_chunk_bytes (fs);
2316 SFIFO_TEST (n_free_chunk_bytes == rv, "free chunk bytes expected %u is %u",
2317 n_free_chunk_bytes, rv);
2320 * Allocate fifo that has all chunks. Because we have a chunk size limit of
2321 * segment_size / 2, allocate 2 fifos.
2323 tf = fifo_segment_alloc_fifo (fs, n_free_chunk_bytes / 2,
2324 FIFO_SEGMENT_RX_FIFO);
2325 SFIFO_TEST (tf != 0, "allocation should work");
2326 SFIFO_TEST (svm_fifo_is_sane (tf), "fifo should be sane");
2328 f = fifo_segment_alloc_fifo (fs, n_free_chunk_bytes / 2,
2329 FIFO_SEGMENT_RX_FIFO);
2330 SFIFO_TEST (f != 0, "allocation should work");
2331 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
2333 fifo_segment_free_fifo (fs, tf);
2334 fifo_segment_free_fifo (fs, f);
2336 rv = fifo_segment_fl_chunk_bytes (fs);
2337 SFIFO_TEST (n_free_chunk_bytes == rv, "free chunk bytes expected %u is %u",
2338 n_free_chunk_bytes, rv);
2341 * Try to allocate more than space available
2344 f = fifo_segment_alloc_fifo (fs, n_free_chunk_bytes + fifo_size,
2345 FIFO_SEGMENT_RX_FIFO);
2346 SFIFO_TEST (f == 0, "allocation should fail");
2349 * Allocate fifo and try to grow beyond available space
2351 f = fifo_segment_alloc_fifo (fs, fifo_segment_free_bytes (fs),
2352 FIFO_SEGMENT_RX_FIFO);
2354 /* Try to force fifo growth */
2355 new_size = svm_fifo_size (f) + n_free_chunk_bytes + 1;
2356 svm_fifo_set_size (f, new_size);
2357 validate_test_and_buf_vecs (&test_data, &data_buf, new_size);
2358 rv = svm_fifo_enqueue (f, new_size, test_data);
2360 SFIFO_TEST (rv != new_size, "grow should fail size %u wrote %d",
2363 fifo_segment_free_fifo (fs, f);
2368 fifo_segment_delete (sm, fs);
2369 vec_free (a->new_segment_indices);
2374 sfifo_test_fifo_segment_slave (int verbose)
2376 fifo_segment_create_args_t _a, *a = &_a;
2377 fifo_segment_main_t *sm = &segment_main;
2378 u8 *test_data, *retrieved_data = 0;
2386 sm->timeout_in_seconds = 5;
2387 clib_memset (a, 0, sizeof (*a));
2388 a->segment_name = "fifo-test1";
2390 rv = fifo_segment_attach (sm, a);
2392 SFIFO_TEST (!rv, "svm_fifo_segment_attach returned %d", rv);
2394 fs = fifo_segment_get_segment (sm, a->new_segment_indices[0]);
2395 vec_free (a->new_segment_indices);
2397 /* might wanna wait.. */
2398 f = fifo_segment_get_slice_fifo_list (fs, 0);
2400 /* Lazy bastards united */
2401 test_data = format (0, "Hello world%c", 0);
2402 vec_validate (retrieved_data, vec_len (test_data) - 1);
2404 for (i = 0; i < 1000; i++)
2406 svm_fifo_dequeue (f, vec_len (retrieved_data), retrieved_data);
2407 if (memcmp (retrieved_data, test_data, vec_len (retrieved_data)))
2409 result = (u32 *) f_head_cptr (f)->data;
2415 result = (u32 *) f_head_cptr (f)->data;
2418 vec_free (test_data);
2419 vec_free (retrieved_data);
2424 sfifo_test_fifo_segment_master_slave (int verbose)
2426 fifo_segment_create_args_t _a, *a = &_a;
2427 fifo_segment_main_t *sm = &segment_main;
2437 SFIFO_TEST (0, "fork failed");
2440 sfifo_test_fifo_segment_slave (verbose);
2442 clib_memset (a, 0, sizeof (*a));
2443 a->segment_name = "fifo-test1";
2444 a->segment_size = 256 << 10;
2446 rv = fifo_segment_create (sm, a);
2448 SFIFO_TEST (!rv, "svm_fifo_segment_create returned %d", rv);
2450 sp = fifo_segment_get_segment (sm, a->new_segment_indices[0]);
2451 f = fifo_segment_alloc_fifo (sp, 4096, FIFO_SEGMENT_RX_FIFO);
2453 SFIFO_TEST (f != 0, "svm_fifo_segment_alloc_fifo alloc");
2455 test_data = format (0, "Hello world%c", 0);
2459 for (i = 0; i < 1000; i++)
2460 svm_fifo_enqueue (f, vec_len (test_data), test_data);
2462 /* Wait for slave */
2464 while (svm_fifo_max_dequeue (f) && i++ < 1e10)
2469 result = (u32 *) f_head_cptr (f)->data;
2470 SFIFO_TEST (*result == 0, "slave reported no error");
2472 vec_free (a->new_segment_indices);
2473 vec_free (test_data);
2474 fifo_segment_free_fifo (sp, f);
2475 fifo_segment_delete (sm, sp);
2480 sfifo_test_fifo_segment_mempig (int verbose)
2482 fifo_segment_create_args_t _a, *a = &_a;
2483 fifo_segment_main_t *sm = &segment_main;
2486 svm_fifo_t **flist = 0;
2490 clib_memset (a, 0, sizeof (*a));
2492 a->segment_name = "fifo-test1";
2493 a->segment_size = 256 << 10;
2494 a->segment_type = SSVM_SEGMENT_PRIVATE;
2496 rv = fifo_segment_create (sm, a);
2498 SFIFO_TEST (!rv, "svm_fifo_segment_create returned %d", rv);
2500 sp = fifo_segment_get_segment (sm, a->new_segment_indices[0]);
2502 for (i = 0; i < 1000; i++)
2504 f = fifo_segment_alloc_fifo (sp, 4096, FIFO_SEGMENT_RX_FIFO);
2507 vec_add1 (flist, f);
2510 SFIFO_TEST (vec_len (flist), "created %d fifos", vec_len (flist));
2512 for (i = 0; i < vec_len (flist); i++)
2515 fifo_segment_free_fifo (sp, f);
2518 vec_set_len (flist, 0);
2520 for (i = 0; i < 1000; i++)
2522 f = fifo_segment_alloc_fifo (sp, 4096, FIFO_SEGMENT_RX_FIFO);
2525 vec_add1 (flist, f);
2528 SFIFO_TEST (vec_len (flist), "second try created %d fifos",
2530 for (i = 0; i < vec_len (flist); i++)
2533 fifo_segment_free_fifo (sp, f);
2536 fifo_segment_delete (sm, sp);
2541 approx_leq (uword a, uword b, u32 margin)
2543 if (a - margin <= b && b <= a)
2549 sfifo_test_fifo_segment_prealloc (int verbose)
2551 u32 max_pairs, pairs_req, free_space, pair_mem, overhead;
2552 fifo_segment_create_args_t _a, *a = &_a;
2553 fifo_segment_main_t *sm = &segment_main;
2554 svm_fifo_t *f, *tf, *old;
2558 clib_memset (a, 0, sizeof (*a));
2560 /* Overhead due to segment internal headers and offsets. The magic 384
2561 * bytes are the fsh->n_reserved_bytes after seg init */
2562 overhead = (8 << 10) + 384;
2563 a->segment_name = "fifo-test-prealloc";
2564 a->segment_size = (256 << 10) + overhead;
2565 a->segment_type = SSVM_SEGMENT_PRIVATE;
2567 rv = fifo_segment_create (sm, a);
2568 SFIFO_TEST (!rv, "svm_fifo_segment_create returned %d", rv);
2569 fs = fifo_segment_get_segment (sm, a->new_segment_indices[0]);
2570 fs->h->pct_first_alloc = 100;
2573 * Prealloc chunks and headers
2575 free_space = fifo_segment_free_bytes (fs);
2576 SFIFO_TEST (free_space - 4096 <= 256 << 10, "free space expected %u is %u",
2577 256 << 10, free_space);
2578 rv = fifo_segment_prealloc_fifo_chunks (fs, 0, 4096, 50);
2579 SFIFO_TEST (rv == 0, "chunk prealloc should work");
2580 rv = fifo_segment_num_free_chunks (fs, 4096);
2581 SFIFO_TEST (rv == 50, "prealloc chunks expected %u is %u", 50, rv);
2582 rv = fifo_segment_free_bytes (fs);
2583 free_space -= (sizeof (svm_fifo_chunk_t) + 4096) * 50;
2584 /* Memory alloc alignment accounts for the difference */
2585 SFIFO_TEST (approx_leq (free_space, rv, 16), "free space expected %u is %u",
2588 rv = fifo_segment_fl_chunk_bytes (fs);
2589 SFIFO_TEST (rv == 4096 * 50, "chunk free space expected %u is %u",
2592 rv = fifo_segment_prealloc_fifo_hdrs (fs, 0, 50);
2593 SFIFO_TEST (rv == 0, "fifo hdr prealloc should work");
2594 rv = fifo_segment_num_free_fifos (fs);
2595 SFIFO_TEST (rv == 50, "prealloc fifo hdrs expected %u is %u", 50, rv);
2596 rv = fifo_segment_free_bytes (fs);
2597 free_space -= sizeof (svm_fifo_shared_t) * 50;
2598 /* Memory alloc alignment accounts for the difference */
2599 SFIFO_TEST (approx_leq (free_space, rv, 128), "free space expected %u is %u",
2603 rv = fifo_segment_free_bytes (fs);
2604 SFIFO_TEST (clib_abs (rv - (int) free_space) < 512,
2605 "free space expected %u is %u", free_space, rv);
2607 /* Use all free chunk memory */
2608 f = fifo_segment_alloc_fifo (fs, 100 << 10, FIFO_SEGMENT_RX_FIFO);
2609 SFIFO_TEST (f != 0, "fifo allocated");
2610 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
2612 tf = fifo_segment_alloc_fifo (fs, 100 << 10, FIFO_SEGMENT_RX_FIFO);
2613 SFIFO_TEST (tf != 0, "fifo allocated");
2614 SFIFO_TEST (svm_fifo_is_sane (tf), "fifo should be sane");
2616 rv = fifo_segment_num_free_chunks (fs, 4096);
2617 SFIFO_TEST (rv == 0, "prealloc chunks expected %u is %u", 0, rv);
2618 rv = fifo_segment_fl_chunk_bytes (fs);
2619 SFIFO_TEST (rv == 0, "chunk free space expected %u is %u", 0, rv);
2623 * Multiple preallocs that consume the remaining space
2625 free_space = fifo_segment_free_bytes (fs);
2626 pair_mem = 2 * (4096 + sizeof (*f) + sizeof (svm_fifo_chunk_t));
2627 max_pairs = pairs_req = (free_space / pair_mem) - 1;
2628 fifo_segment_preallocate_fifo_pairs (fs, 4096, 4096, &pairs_req);
2629 SFIFO_TEST (pairs_req == 0, "prealloc pairs should work req %u", max_pairs);
2630 rv = fifo_segment_num_free_chunks (fs, 4096);
2631 SFIFO_TEST (rv == max_pairs * 2, "prealloc chunks expected %u is %u",
2634 rv = fifo_segment_free_bytes (fs);
2635 SFIFO_TEST (rv < 2 * pair_mem, "free bytes %u less than %u", rv,
2638 /* Preallocate as many more chunks as possible. Heap is almost full
2639 * so we may not use all the free space*/
2641 while (!fifo_segment_prealloc_fifo_chunks (fs, 0, 4096, 1))
2643 SFIFO_TEST (alloc, "chunk prealloc should work %u", alloc);
2644 rv = fifo_segment_num_free_chunks (fs, 4096);
2645 SFIFO_TEST (rv == max_pairs * 2 + alloc, "prealloc chunks expected %u "
2646 "is %u", max_pairs * 2 + alloc, rv);
2648 rv = fifo_segment_free_bytes (fs);
2649 SFIFO_TEST (rv < pair_mem, "free bytes expected less than %u is %u",
2653 * Test negative prealloc cases
2656 fifo_segment_preallocate_fifo_pairs (fs, 4096, 4096, &pairs_req);
2657 SFIFO_TEST (pairs_req == 1, "prealloc pairs should not work");
2660 f = fifo_segment_alloc_fifo (fs, 200 << 10, FIFO_SEGMENT_RX_FIFO);
2661 SFIFO_TEST (f == 0, "fifo alloc should fail");
2663 rv = fifo_segment_prealloc_fifo_chunks (fs, 0, 4096, 50);
2664 SFIFO_TEST (rv == -1, "chunk prealloc should fail");
2666 rv = fifo_segment_prealloc_fifo_hdrs (fs, 0, 50);
2667 SFIFO_TEST (rv == -1, "fifo hdr prealloc should fail");
2672 fifo_segment_free_fifo (fs, old);
2673 fifo_segment_free_fifo (fs, tf);
2674 fifo_segment_delete (sm, fs);
2679 sfifo_test_fifo_segment (vlib_main_t * vm, unformat_input_t * input)
2681 int rv, verbose = 0;
2683 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2685 if (unformat (input, "verbose"))
2687 else if (unformat (input, "masterslave"))
2689 if ((rv = sfifo_test_fifo_segment_master_slave (verbose)))
2692 else if (unformat (input, "basic"))
2694 if ((rv = sfifo_test_fifo_segment_hello_world (verbose)))
2697 else if (unformat (input, "mempig"))
2699 if ((rv = sfifo_test_fifo_segment_mempig (verbose)))
2702 else if (unformat (input, "grow fifo"))
2704 if ((rv = sfifo_test_fifo_segment_fifo_grow (verbose)))
2707 else if (unformat (input, "prealloc"))
2709 if ((rv = sfifo_test_fifo_segment_prealloc (verbose)))
2712 else if (unformat (input, "all"))
2714 if ((rv = sfifo_test_fifo_segment_hello_world (verbose)))
2716 if ((rv = sfifo_test_fifo_segment_mempig (verbose)))
2718 if ((rv = sfifo_test_fifo_segment_fifo_grow (verbose)))
2720 if ((rv = sfifo_test_fifo_segment_prealloc (verbose)))
2722 /* Pretty slow so avoid running it always
2723 if ((rv = sfifo_test_fifo_segment_master_slave (verbose)))
2729 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
2737 static clib_error_t *
2738 svm_fifo_test (vlib_main_t * vm, unformat_input_t * input,
2739 vlib_cli_command_t * cmd_arg)
2744 clib_warning ("high mem %lu", HIGH_SEGMENT_BASEVA);
2745 fifo_segment_main_init (&segment_main, HIGH_SEGMENT_BASEVA, 5);
2746 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2748 if (unformat (input, "fifo1"))
2749 res = sfifo_test_fifo1 (vm, input);
2750 else if (unformat (input, "fifo2"))
2751 res = sfifo_test_fifo2 (vm);
2752 else if (unformat (input, "fifo3"))
2753 res = sfifo_test_fifo3 (vm, input);
2754 else if (unformat (input, "fifo4"))
2755 res = sfifo_test_fifo4 (vm, input);
2756 else if (unformat (input, "fifo5"))
2757 res = sfifo_test_fifo5 (vm, input);
2758 else if (unformat (input, "fifo6"))
2759 res = sfifo_test_fifo6 (vm, input);
2760 else if (unformat (input, "fifo7"))
2761 res = sfifo_test_fifo7 (vm, input);
2762 else if (unformat (input, "large"))
2763 res = sfifo_test_fifo_large (vm, input);
2764 else if (unformat (input, "replay"))
2765 res = sfifo_test_fifo_replay (vm, input);
2766 else if (unformat (input, "grow"))
2767 res = sfifo_test_fifo_grow (vm, input);
2768 else if (unformat (input, "shrink"))
2769 res = sfifo_test_fifo_shrink (vm, input);
2770 else if (unformat (input, "indirect"))
2771 res = sfifo_test_fifo_indirect (vm, input);
2772 else if (unformat (input, "zero"))
2773 res = sfifo_test_fifo_make_rcv_wnd_zero (vm, input);
2774 else if (unformat (input, "segment"))
2775 res = sfifo_test_fifo_segment (vm, input);
2776 else if (unformat (input, "all"))
2778 if ((res = sfifo_test_fifo1 (vm, input)))
2781 if ((res = sfifo_test_fifo2 (vm)))
2785 * Run a number of fifo3 configs
2787 str = "nsegs 10 overlap seed 123";
2788 unformat_init_cstring (input, str);
2789 if ((res = sfifo_test_fifo3 (vm, input)))
2791 unformat_free (input);
2793 str = "nsegs 10 overlap seed 123 in-seq-all";
2794 unformat_init_cstring (input, str);
2795 if ((res = sfifo_test_fifo3 (vm, input)))
2797 unformat_free (input);
2799 str = "nsegs 10 overlap seed 123 initial-offset 3917";
2800 unformat_init_cstring (input, str);
2801 if ((res = sfifo_test_fifo3 (vm, input)))
2803 unformat_free (input);
2805 str = "nsegs 10 overlap seed 123 initial-offset 3917 drop";
2806 unformat_init_cstring (input, str);
2807 if ((res = sfifo_test_fifo3 (vm, input)))
2809 unformat_free (input);
2811 str = "nsegs 10 seed 123 initial-offset 3917 drop no-randomize";
2812 unformat_init_cstring (input, str);
2813 if ((res = sfifo_test_fifo3 (vm, input)))
2815 unformat_free (input);
2817 if ((res = sfifo_test_fifo4 (vm, input)))
2820 if ((res = sfifo_test_fifo5 (vm, input)))
2823 if ((res = sfifo_test_fifo6 (vm, input)))
2826 if ((res = sfifo_test_fifo7 (vm, input)))
2829 if ((res = sfifo_test_fifo_grow (vm, input)))
2832 if ((res = sfifo_test_fifo_shrink (vm, input)))
2835 if ((res = sfifo_test_fifo_indirect (vm, input)))
2838 if ((res = sfifo_test_fifo_make_rcv_wnd_zero (vm, input)))
2842 unformat_init_cstring (input, str);
2843 if ((res = sfifo_test_fifo_segment (vm, input)))
2848 vlib_cli_output (vm, "unknown input `%U'", format_unformat_error,
2858 return clib_error_return (0, "svm fifo unit test failed");
2862 VLIB_CLI_COMMAND (svm_fifo_test_command, static) =
2864 .path = "test svm fifo",
2865 .short_help = "internal svm fifo unit tests",
2866 .function = svm_fifo_test,
2870 * fd.io coding-style-patch-verification: ON
2873 * eval: (c-set-style "gnu")