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)) { \
47 test_pattern_t test_pattern[] = {
48 {380, 8}, {768, 8}, {1156, 8}, {1544, 8}, {1932, 8}, {2320, 8}, {2708, 8},
49 {2992, 8}, {372, 8}, {760, 8}, {1148, 8}, {1536, 8}, {1924, 8}, {2312, 8},
50 {2700, 8}, {2984, 8}, {364, 8}, {752, 8}, {1140, 8}, {1528, 8}, {1916, 8},
51 {2304, 8}, {2692, 8}, {2976, 8}, {356, 8}, {744, 8}, {1132, 8}, {1520, 8},
52 {1908, 8}, {2296, 8}, {2684, 8}, {2968, 8}, {348, 8}, {736, 8}, {1124, 8},
53 {1512, 8}, {1900, 8}, {2288, 8}, {2676, 8}, {2960, 8}, {340, 8}, {728, 8},
54 {1116, 8}, {1504, 8}, {1892, 8}, {2280, 8}, {2668, 8}, {2952, 8}, {332, 8},
55 {720, 8}, {1108, 8}, {1496, 8}, {1884, 8}, {2272, 8}, {2660, 8}, {2944, 8},
56 {324, 8}, {712, 8}, {1100, 8}, {1488, 8}, {1876, 8}, {2264, 8}, {2652, 8},
57 {2936, 8}, {316, 8}, {704, 8}, {1092, 8}, {1480, 8}, {1868, 8}, {2256, 8},
58 {2644, 8}, {2928, 8}, {308, 8}, {696, 8}, {1084, 8}, {1472, 8}, {1860, 8},
59 {2248, 8}, {2636, 8}, {2920, 8}, {300, 8}, {688, 8}, {1076, 8}, {1464, 8},
60 {1852, 8}, {2240, 8}, {2628, 8}, {2912, 8}, {292, 8}, {680, 8}, {1068, 8},
61 {1456, 8}, {1844, 8}, {2232, 8}, {2620, 8}, {2904, 8}, {284, 8}, {672, 8},
62 {1060, 8}, {1448, 8}, {1836, 8}, {2224, 8}, {2612, 8}, {2896, 8}, {276, 8},
63 {664, 8}, {1052, 8}, {1440, 8}, {1828, 8}, {2216, 8}, {2604, 8}, {2888, 8},
64 {268, 8}, {656, 8}, {1044, 8}, {1432, 8}, {1820, 8}, {2208, 8}, {2596, 8},
65 {2880, 8}, {260, 8}, {648, 8}, {1036, 8}, {1424, 8}, {1812, 8}, {2200, 8},
66 {2588, 8}, {2872, 8}, {252, 8}, {640, 8}, {1028, 8}, {1416, 8}, {1804, 8},
67 {2192, 8}, {2580, 8}, {2864, 8}, {244, 8}, {632, 8}, {1020, 8}, {1408, 8},
68 {1796, 8}, {2184, 8}, {2572, 8}, {2856, 8}, {236, 8}, {624, 8}, {1012, 8},
69 {1400, 8}, {1788, 8}, {2176, 8}, {2564, 8}, {2848, 8}, {228, 8}, {616, 8},
70 {1004, 8}, {1392, 8}, {1780, 8}, {2168, 8}, {2556, 8}, {2840, 8}, {220, 8},
71 {608, 8}, {996, 8}, {1384, 8}, {1772, 8}, {2160, 8}, {2548, 8}, {2832, 8},
72 {212, 8}, {600, 8}, {988, 8}, {1376, 8}, {1764, 8}, {2152, 8}, {2540, 8},
73 {2824, 8}, {204, 8}, {592, 8}, {980, 8}, {1368, 8}, {1756, 8}, {2144, 8},
74 {2532, 8}, {2816, 8}, {196, 8}, {584, 8}, {972, 8}, {1360, 8}, {1748, 8},
75 {2136, 8}, {2524, 8}, {2808, 8}, {188, 8}, {576, 8}, {964, 8}, {1352, 8},
76 {1740, 8}, {2128, 8}, {2516, 8}, {2800, 8}, {180, 8}, {568, 8}, {956, 8},
77 {1344, 8}, {1732, 8}, {2120, 8}, {2508, 8}, {2792, 8}, {172, 8}, {560, 8},
78 {948, 8}, {1336, 8}, {1724, 8}, {2112, 8}, {2500, 8}, {2784, 8}, {164, 8},
79 {552, 8}, {940, 8}, {1328, 8}, {1716, 8}, {2104, 8}, {2492, 8}, {2776, 8},
80 {156, 8}, {544, 8}, {932, 8}, {1320, 8}, {1708, 8}, {2096, 8}, {2484, 8},
81 {2768, 8}, {148, 8}, {536, 8}, {924, 8}, {1312, 8}, {1700, 8}, {2088, 8},
82 {2476, 8}, {2760, 8}, {140, 8}, {528, 8}, {916, 8}, {1304, 8}, {1692, 8},
83 {2080, 8}, {2468, 8}, {2752, 8}, {132, 8}, {520, 8}, {908, 8}, {1296, 8},
84 {1684, 8}, {2072, 8}, {2460, 8}, {2744, 8}, {124, 8}, {512, 8}, {900, 8},
85 {1288, 8}, {1676, 8}, {2064, 8}, {2452, 8}, {2736, 8}, {116, 8}, {504, 8},
86 {892, 8}, {1280, 8}, {1668, 8}, {2056, 8}, {2444, 8}, {2728, 8}, {108, 8},
87 {496, 8}, {884, 8}, {1272, 8}, {1660, 8}, {2048, 8}, {2436, 8}, {2720, 8},
88 {100, 8}, {488, 8}, {876, 8}, {1264, 8}, {1652, 8}, {2040, 8}, {2428, 8},
89 {2716, 4}, {92, 8}, {480, 8}, {868, 8}, {1256, 8}, {1644, 8}, {2032, 8},
90 {2420, 8}, {84, 8}, {472, 8}, {860, 8}, {1248, 8}, {1636, 8}, {2024, 8},
91 {2412, 8}, {76, 8}, {464, 8}, {852, 8}, {1240, 8}, {1628, 8}, {2016, 8},
92 {2404, 8}, {68, 8}, {456, 8}, {844, 8}, {1232, 8}, {1620, 8}, {2008, 8},
93 {2396, 8}, {60, 8}, {448, 8}, {836, 8}, {1224, 8}, {1612, 8}, {2000, 8},
94 {2388, 8}, {52, 8}, {440, 8}, {828, 8}, {1216, 8}, {1604, 8}, {1992, 8},
95 {2380, 8}, {44, 8}, {432, 8}, {820, 8}, {1208, 8}, {1596, 8}, {1984, 8},
96 {2372, 8}, {36, 8}, {424, 8}, {812, 8}, {1200, 8}, {1588, 8}, {1976, 8},
97 {2364, 8}, {28, 8}, {416, 8}, {804, 8}, {1192, 8}, {1580, 8}, {1968, 8},
98 {2356, 8}, {20, 8}, {408, 8}, {796, 8}, {1184, 8}, {1572, 8}, {1960, 8},
99 {2348, 8}, {12, 8}, {400, 8}, {788, 8}, {1176, 8}, {1564, 8}, {1952, 8},
100 {2340, 8}, {4, 8}, {392, 8}, {780, 8}, {1168, 8}, {1556, 8}, {1944, 8},
102 /* missing from original data set */
103 {388, 4}, {776, 4}, {1164, 4}, {1552, 4}, {1940, 4}, {2328, 4},
108 pattern_cmp (const void *arg1, const void *arg2)
110 test_pattern_t *a1 = (test_pattern_t *) arg1;
111 test_pattern_t *a2 = (test_pattern_t *) arg2;
113 if (a1->offset < a2->offset)
115 else if (a1->offset > a2->offset)
121 fifo_validate_pattern (vlib_main_t * vm, test_pattern_t * pattern,
124 test_pattern_t *tp = pattern;
127 /* Go through the pattern and make 100% sure it's sane */
128 for (i = 0; i < pattern_length - 1; i++)
130 if (tp->offset + tp->len != (tp + 1)->offset)
132 vlib_cli_output (vm, "[%d] missing {%d, %d}", i,
133 (tp->offset + tp->len),
134 (tp + 1)->offset - (tp->offset + tp->len));
142 static test_pattern_t *
143 fifo_get_validate_pattern (vlib_main_t * vm, test_pattern_t * test_data,
146 test_pattern_t *validate_pattern = 0;
148 /* Validate, and try segments in order... */
149 vec_validate (validate_pattern, test_data_len - 1);
150 memcpy (validate_pattern, test_data,
151 test_data_len * sizeof (test_pattern_t));
152 qsort ((u8 *) validate_pattern, test_data_len, sizeof (test_pattern_t),
155 if (fifo_validate_pattern (vm, validate_pattern, test_data_len) == 0)
158 return validate_pattern;
161 static fifo_segment_t *
162 fifo_segment_prepare (fifo_segment_main_t * sm, char *seg_name, u32 seg_size)
164 fifo_segment_create_args_t _a, *a = &_a;
166 clib_memset (a, 0, sizeof (*a));
167 a->segment_name = seg_name;
168 a->segment_size = seg_size ? seg_size : 32 << 20;
170 if (fifo_segment_create (sm, a))
173 return fifo_segment_get_segment (sm, a->new_segment_indices[0]);
177 ft_fifo_segment_free (fifo_segment_main_t * sm, fifo_segment_t * fs)
179 fifo_segment_delete (sm, fs);
183 fifo_segment_alloc_fifo (fifo_segment_t * fs, u32 data_bytes,
184 fifo_segment_ftype_t ftype)
186 return fifo_segment_alloc_fifo_w_slice (fs, 0, data_bytes, ftype);
190 fifo_prepare (fifo_segment_t * fs, u32 fifo_size)
194 f = fifo_segment_alloc_fifo (fs, fifo_size, FIFO_SEGMENT_RX_FIFO);
196 /* Paint fifo data vector with -1's */
197 clib_memset (svm_fifo_head_chunk (f)->data, 0xFF, fifo_size);
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->tail == 4), "fifo tail %u", f->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->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->tail == 8), "fifo tail %u", f->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->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->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->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->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->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->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->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 (f,
706 fifo_initial_offset + tp->offset -
708 (u8 *) data_pattern + tp->offset);
711 /* Add the first segment in order for non random data */
713 svm_fifo_enqueue (f, generate[0].len, (u8 *) data_pattern);
716 * Expected result: one big fat chunk at offset 1 if randomize == 1
720 vlib_cli_output (vm, "fifo before missing link: %U",
721 format_svm_fifo, f, 1 /* verbose */ );
724 * Add the missing byte if segments were randomized
728 u32 bytes_to_enq = 1;
730 bytes_to_enq = total_size;
731 rv = svm_fifo_enqueue (f, bytes_to_enq, data_pattern + 0);
734 vlib_cli_output (vm, "in-order enqueue returned %d", rv);
736 SFIFO_TEST ((rv == total_size), "enqueued %u expected %u", rv,
741 SFIFO_TEST ((svm_fifo_has_ooo_data (f) == 0), "number of ooo segments %u",
742 svm_fifo_n_ooo_segments (f));
745 * Test if peeked data is the same as original data
747 vec_validate (data_buf, vec_len (data_pattern));
748 svm_fifo_peek (f, 0, vec_len (data_pattern), data_buf);
749 if (compare_data (data_buf, data_pattern, 0, vec_len (data_pattern), &j))
751 SFIFO_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j],
756 * Dequeue or drop all data
760 svm_fifo_dequeue_drop (f, vec_len (data_pattern));
764 memset (data_buf, 0, vec_len (data_pattern));
765 svm_fifo_dequeue (f, vec_len (data_pattern), data_buf);
767 (data_buf, data_pattern, 0, vec_len (data_pattern), &j))
769 SFIFO_TEST (0, "[%d] dequeued %u expected %u", j, data_buf[j],
774 SFIFO_TEST ((svm_fifo_max_dequeue (f) == 0), "fifo has %d bytes",
775 svm_fifo_max_dequeue (f));
777 ft_fifo_free (fs, f);
778 ft_fifo_segment_free (fsm, fs);
779 vec_free (data_pattern);
786 sfifo_test_fifo4 (vlib_main_t * vm, unformat_input_t * input)
788 u32 fifo_size = 6 << 10, fifo_initial_offset = 1e9, test_n_bytes = 5000, j;
789 fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
790 u8 *test_data = 0, *data_buf = 0;
791 int i, rv, verbose = 0;
795 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
797 if (unformat (input, "verbose"))
801 clib_error_t *e = clib_error_return
802 (0, "unknown input `%U'", format_unformat_error, input);
803 clib_error_report (e);
809 * Create a fifo and add segments
811 fs = fifo_segment_prepare (fsm, "fifo-test4", 0);
812 f = fifo_prepare (fs, fifo_size);
814 /* Set head and tail pointers */
815 fifo_initial_offset = fifo_initial_offset % fifo_size;
816 svm_fifo_init_pointers (f, fifo_initial_offset, fifo_initial_offset);
818 vec_validate (test_data, test_n_bytes - 1);
819 for (i = 0; i < vec_len (test_data); i++)
822 for (i = test_n_bytes - 1; i > 0; i--)
824 rv = svm_fifo_enqueue_with_offset (f, fifo_initial_offset + i - f->tail,
825 sizeof (u8), &test_data[i]);
827 vlib_cli_output (vm, "add [%d] [%d, %d]", i, i, i + sizeof (u8));
829 SFIFO_TEST (0, "enqueue returned %d", rv);
832 svm_fifo_enqueue (f, sizeof (u8), &test_data[0]);
834 vec_validate (data_buf, vec_len (test_data));
836 svm_fifo_dequeue (f, vec_len (test_data), data_buf);
837 rv = compare_data (data_buf, test_data, 0, vec_len (test_data), &j);
839 vlib_cli_output (vm, "[%d] dequeued %u expected %u", j, data_buf[j],
841 SFIFO_TEST ((rv == 0), "dequeued compared to original returned %d", rv);
843 ft_fifo_free (fs, f);
844 ft_fifo_segment_free (fsm, fs);
845 vec_free (test_data);
850 fifo_pos (svm_fifo_t * f, u32 pos)
855 /* Avoids exposing svm_fifo.c internal function */
856 static ooo_segment_t *
857 ooo_seg_next (svm_fifo_t * f, ooo_segment_t * s)
859 if (pool_is_free_index (f->ooo_segments, s->next))
861 return pool_elt_at_index (f->ooo_segments, s->next);
865 sfifo_test_fifo5 (vlib_main_t * vm, unformat_input_t * input)
867 fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
868 u32 fifo_size = 401, j = 0, offset = 200;
869 u8 *test_data = 0, *data_buf = 0;
870 int i, rv, verbose = 0;
871 ooo_segment_t *ooo_seg;
875 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
877 if (unformat (input, "verbose"))
881 clib_error_t *e = clib_error_return (0, "unknown input `%U'",
882 format_unformat_error, input);
883 clib_error_report (e);
888 fs = fifo_segment_prepare (fsm, "fifo-test5", 0);
889 f = fifo_prepare (fs, fifo_size);
890 svm_fifo_init_pointers (f, offset, offset);
892 vec_validate (test_data, 399);
893 for (i = 0; i < vec_len (test_data); i++)
894 test_data[i] = i % 0xff;
897 * Start with [100, 200] and [300, 400]
899 svm_fifo_enqueue_with_offset (f, 100, 100, &test_data[100]);
900 svm_fifo_enqueue_with_offset (f, 300, 100, &test_data[300]);
902 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 2),
903 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
904 SFIFO_TEST ((f->ooos_newest == 1), "newest %u", f->ooos_newest);
906 vlib_cli_output (vm, "fifo after [100, 200] and [300, 400] : %U",
907 format_svm_fifo, f, 2 /* verbose */ );
913 rv = svm_fifo_enqueue_with_offset (f, 225, 50, &test_data[225]);
915 vlib_cli_output (vm, "fifo after [225, 275] : %U",
916 format_svm_fifo, f, 2 /* verbose */ );
917 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 3),
918 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
919 ooo_seg = svm_fifo_first_ooo_segment (f);
920 SFIFO_TEST ((ooo_seg->start == fifo_pos (f, 100 + offset)),
921 "first seg start %u expected %u", ooo_seg->start,
922 fifo_pos (f, 100 + offset));
923 SFIFO_TEST ((ooo_seg->length == 100), "first seg length %u expected %u",
924 ooo_seg->length, 100);
925 ooo_seg = ooo_seg_next (f, ooo_seg);
926 SFIFO_TEST ((ooo_seg->start == fifo_pos (f, 225 + offset)),
927 "second seg start %u expected %u",
928 ooo_seg->start, fifo_pos (f, 225 + offset));
929 SFIFO_TEST ((ooo_seg->length == 50), "second seg length %u expected %u",
930 ooo_seg->length, 50);
931 ooo_seg = ooo_seg_next (f, ooo_seg);
932 SFIFO_TEST ((ooo_seg->start == fifo_pos (f, 300 + offset)),
933 "third seg start %u expected %u",
934 ooo_seg->start, fifo_pos (f, 300 + offset));
935 SFIFO_TEST ((ooo_seg->length == 100), "third seg length %u expected %u",
936 ooo_seg->length, 100);
937 SFIFO_TEST ((f->ooos_newest == 2), "newest %u", f->ooos_newest);
941 rv = svm_fifo_enqueue_with_offset (f, 190, 120, &test_data[190]);
943 vlib_cli_output (vm, "fifo after [190, 310] : %U",
944 format_svm_fifo, f, 1 /* verbose */ );
945 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 1),
946 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
947 ooo_seg = svm_fifo_first_ooo_segment (f);
948 SFIFO_TEST ((ooo_seg->start == fifo_pos (f, offset + 100)),
949 "first seg start %u expected %u",
950 ooo_seg->start, fifo_pos (f, offset + 100));
951 SFIFO_TEST ((ooo_seg->length == 300), "first seg length %u expected %u",
952 ooo_seg->length, 300);
957 rv = svm_fifo_enqueue (f, 150, test_data);
960 vlib_cli_output (vm, "fifo after [0 150] : %U", format_svm_fifo, f,
963 SFIFO_TEST ((rv == 400), "managed to enqueue %u expected %u", rv, 400);
964 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 0),
965 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
967 vec_validate (data_buf, 399);
968 svm_fifo_peek (f, 0, 400, data_buf);
969 if (compare_data (data_buf, test_data, 0, 400, &j))
971 SFIFO_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j],
976 * Add [100 200] and overlap it with [50 250]
978 ft_fifo_free (fs, f);
979 f = fifo_prepare (fs, fifo_size);
981 svm_fifo_enqueue_with_offset (f, 100, 100, &test_data[100]);
982 svm_fifo_enqueue_with_offset (f, 50, 200, &test_data[50]);
983 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 1),
984 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
985 ooo_seg = svm_fifo_first_ooo_segment (f);
986 SFIFO_TEST ((ooo_seg->start == 50), "first seg start %u expected %u",
988 SFIFO_TEST ((ooo_seg->length == 200), "first seg length %u expected %u",
989 ooo_seg->length, 200);
991 ft_fifo_free (fs, f);
992 ft_fifo_segment_free (fsm, fs);
993 vec_free (test_data);
998 * Test ooo head/tail u32 wrapping
1001 sfifo_test_fifo6 (vlib_main_t * vm, unformat_input_t * input)
1003 fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
1004 u32 fifo_size = 101, n_test_bytes = 100;
1005 int i, j, rv, __clib_unused verbose = 0;
1006 u8 *test_data = 0, *data_buf = 0;
1007 ooo_segment_t *ooo_seg;
1011 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1013 if (unformat (input, "verbose"))
1017 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
1023 fs = fifo_segment_prepare (fsm, "fifo-test6", 0);
1024 f = fifo_prepare (fs, fifo_size);
1026 vec_validate (test_data, n_test_bytes - 1);
1027 vec_validate (data_buf, n_test_bytes - 1);
1028 for (i = 0; i < vec_len (test_data); i++)
1029 test_data[i] = i % 0xff;
1032 * Add ooo with tail and ooo segment start u32 wrap
1034 svm_fifo_init_pointers (f, ~0 % fifo_size, ~0 % fifo_size);
1035 svm_fifo_enqueue_with_offset (f, 10, 10, &test_data[10]);
1036 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 1),
1037 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
1038 ooo_seg = svm_fifo_first_ooo_segment (f);
1039 rv = ooo_segment_offset_prod (f, ooo_seg);
1040 SFIFO_TEST (rv == 10, "offset should be %u is %u", 10, rv);
1042 svm_fifo_enqueue (f, 10, test_data);
1043 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 0),
1044 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
1045 SFIFO_TEST (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX,
1046 "there should be no ooo seg");
1048 svm_fifo_peek (f, 5, 10, &data_buf[5]);
1049 if (compare_data (data_buf, test_data, 5, 10, (u32 *) & j))
1050 SFIFO_TEST (0, "[%d] dequeued %u expected %u", j, data_buf[j],
1053 svm_fifo_dequeue (f, 20, data_buf);
1054 if (compare_data (data_buf, test_data, 0, 20, (u32 *) & j))
1055 SFIFO_TEST (0, "[%d] dequeued %u expected %u", j, data_buf[j],
1059 * Force collect with tail u32 wrap and without ooo segment start u32 wrap
1061 svm_fifo_init_pointers (f, (~0 - 10) % fifo_size, (~0 - 10) % fifo_size);
1062 svm_fifo_enqueue_with_offset (f, 5, 15, &test_data[5]);
1063 svm_fifo_enqueue (f, 12, test_data);
1065 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 0),
1066 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
1067 SFIFO_TEST (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX,
1068 "there should be no ooo seg");
1070 svm_fifo_dequeue (f, 20, data_buf);
1071 if (compare_data (data_buf, test_data, 0, 20, (u32 *) & j))
1072 SFIFO_TEST (0, "[%d] dequeued %u expected %u", j, data_buf[j],
1078 vec_free (test_data);
1079 vec_free (data_buf);
1080 ft_fifo_free (fs, f);
1081 ft_fifo_segment_free (fsm, fs);
1086 * Multiple ooo enqueues and dequeues that force fifo tail/head wrap
1089 sfifo_test_fifo7 (vlib_main_t * vm, unformat_input_t * input)
1091 fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
1092 u32 fifo_size = 101, n_iterations = 100;
1093 int i, j, rv, __clib_unused verbose = 0;
1094 u8 *test_data = 0, *data_buf = 0;
1095 u64 n_test_bytes = 100;
1099 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1101 if (unformat (input, "verbose"))
1105 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
1112 * Prepare data structures
1114 fs = fifo_segment_prepare (fsm, "fifo-test7", 0);
1115 f = fifo_prepare (fs, fifo_size);
1116 svm_fifo_init_pointers (f, ~0 % fifo_size, ~0 % fifo_size);
1118 vec_validate (test_data, n_test_bytes - 1);
1119 vec_validate (data_buf, n_test_bytes - 1);
1120 for (i = 0; i < vec_len (test_data); i++)
1121 test_data[i] = i % 0xff;
1124 * Run n iterations of test
1126 for (i = 0; i < n_iterations; i++)
1128 for (j = n_test_bytes - 1; j > 0; j -= 2)
1130 svm_fifo_enqueue_with_offset (f, j, 1, &test_data[j]);
1131 rv = svm_fifo_n_ooo_segments (f);
1132 if (rv != (n_test_bytes - j) / 2 + 1)
1133 SFIFO_TEST (0, "number of ooo segments expected %u is %u",
1134 (n_test_bytes - j) / 2 + 1, rv);
1137 svm_fifo_enqueue_with_offset (f, 1, n_test_bytes - 1, &test_data[1]);
1138 rv = svm_fifo_n_ooo_segments (f);
1140 SFIFO_TEST (0, "number of ooo segments %u", rv);
1142 svm_fifo_enqueue (f, 1, test_data);
1143 rv = svm_fifo_n_ooo_segments (f);
1145 SFIFO_TEST (0, "number of ooo segments %u", rv);
1147 svm_fifo_dequeue (f, n_test_bytes, data_buf);
1148 if (compare_data (data_buf, test_data, 0, n_test_bytes, (u32 *) & j))
1149 SFIFO_TEST (0, "[%d] dequeued %u expected %u", j, data_buf[j],
1151 svm_fifo_init_pointers (f, (~0 - i) % f->size, (~0 - i) % f->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");
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->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->head_chunk == 0, "head chunk should be 0");
1440 SFIFO_TEST (f->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->head_chunk == 0, "should have no head chunk");
1463 /* When new fifo chunks are allocated, tail is initialized */
1464 SFIFO_TEST (f->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->head_chunk == 0, "should have no head chunk");
1485 /* Fifo is full so tail and ooo_enq should be 0 */
1486 SFIFO_TEST (f->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->head_chunk == 0, "should have no head chunk");
1544 SFIFO_TEST (f->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->head_chunk == 0, "should have no head chunk");
1577 /* When new fifo chunks are allocated, tail is initialized */
1578 SFIFO_TEST (f->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->head_chunk != 0, "should have head chunk");
1649 SFIFO_TEST (f->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->head_chunk != 0, "should have head chunk");
1690 SFIFO_TEST (f->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);
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 (c->next->length == 4 << 20, "new chunk should be 4MB");
1949 rv = svm_fifo_max_write_chunk (f);
1950 SFIFO_TEST (rv == 4096, "max write chunk %u", rv);
1953 * Enqueue enough to fill first chunk
1955 svm_fifo_enqueue_nocopy (f, 4096);
1956 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1958 c = svm_fifo_tail_chunk (f);
1959 SFIFO_TEST (c == f->end_chunk, "tail is end chunk");
1961 /* Initialize head chunk */
1962 rv = svm_fifo_max_read_chunk (f);
1963 SFIFO_TEST (rv == 4096, "max read chunk %u", rv);
1964 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1967 * Move head over last segment
1969 rv = svm_fifo_dequeue (f, 4096, data_buf);
1970 SFIFO_TEST (rv == 4096, "dequeue should work");
1972 c = svm_fifo_head_chunk (f);
1973 SFIFO_TEST (c == f->end_chunk, "head chunk should be last");
1975 rv = svm_fifo_max_read_chunk (f);
1976 SFIFO_TEST (rv == 0, "max read chunk %u", rv);
1978 rv = svm_fifo_max_write_chunk (f);
1979 SFIFO_TEST (rv == 4 << 20, "max write chunk %u", rv);
1985 ft_fifo_free (fs, f);
1986 ft_fifo_segment_free (fsm, fs);
1987 vec_free (test_data);
1988 vec_free (data_buf);
1994 svm_fifo_trace_elem_t fifo_trace[] = {};
1998 sfifo_test_fifo_replay (vlib_main_t * vm, unformat_input_t * input)
2002 u8 no_read = 0, *str = 0;
2004 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2006 if (unformat (input, "verbose"))
2008 else if (unformat (input, "no-read"))
2012 clib_error_t *e = clib_error_return
2013 (0, "unknown input `%U'", format_unformat_error, input);
2014 clib_error_report (e);
2020 f.trace = fifo_trace;
2023 str = svm_fifo_replay (str, &f, no_read, verbose);
2024 vlib_cli_output (vm, "%v", str);
2029 sfifo_test_fifo_make_rcv_wnd_zero (vlib_main_t * vm, unformat_input_t * input)
2031 int __clib_unused verbose = 0, fifo_size = 4096, deq_chunk;
2032 fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
2033 u8 *test_data = 0, *data_buf = 0;
2038 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2040 if (unformat (input, "verbose"))
2044 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
2051 * Init fifo and enqueue data such that multiple 4096 chunks are allocated
2053 fs = fifo_segment_prepare (fsm, "fifo-rcv-wnd-zero", 0);
2054 f = fifo_prepare (fs, fifo_size);
2056 /* Enqueue 3000 into 4KB chunk, so there'll be 1096 free space */
2057 svm_fifo_set_size (f, 4096);
2058 validate_test_and_buf_vecs (&test_data, &data_buf, fifo_size);
2059 rv = svm_fifo_enqueue (f, 3000, test_data);
2060 SFIFO_TEST (rv == 3000, "enqueued %u", rv);
2061 rv = svm_fifo_max_enqueue (f);
2062 SFIFO_TEST (rv == 1096, "svm_fifo_max_enqueue %u", rv);
2063 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
2065 /* Shrink fifo size to the in-use size */
2066 svm_fifo_set_size (f, 3000);
2067 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
2069 /* In TCP, this should result in rcv-wnd = 0 */
2070 rv = svm_fifo_max_enqueue (f);
2071 SFIFO_TEST (rv == 0, "svm_fifo_max_enqueue %u", rv);
2072 rv = svm_fifo_max_enqueue_prod (f);
2073 SFIFO_TEST (rv == 0, "svm_fifo_max_enqueue_prod %u", rv);
2075 /* Dequeue and ... */
2076 rv = svm_fifo_dequeue (f, 3000, data_buf);
2077 SFIFO_TEST (rv == 3000, "dequeued %u", rv);
2080 ft_fifo_free (fs, f);
2081 ft_fifo_segment_free (fsm, fs);
2082 vec_free (test_data);
2083 vec_free (data_buf);
2089 static fifo_segment_main_t segment_main;
2092 sfifo_test_fifo_segment_hello_world (int verbose)
2094 fifo_segment_create_args_t _a, *a = &_a;
2095 fifo_segment_main_t *sm = &segment_main;
2096 u8 *test_data, *retrieved_data = 0;
2101 clib_memset (a, 0, sizeof (*a));
2102 a->segment_name = "fifo-test1";
2103 a->segment_size = 256 << 10;
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;
2155 /* fifo allocation allocates chunks in batch */
2156 n_batch = FIFO_SEGMENT_ALLOC_BATCH_SIZE;
2158 rv = fifo_segment_create (sm, a);
2160 SFIFO_TEST (!rv, "svm_fifo_segment_create returned %d", rv);
2165 fs = fifo_segment_get_segment (sm, a->new_segment_indices[0]);
2166 fs->h->pct_first_alloc = 100;
2167 f = fifo_segment_alloc_fifo (fs, fifo_size, FIFO_SEGMENT_RX_FIFO);
2169 SFIFO_TEST (f != 0, "svm_fifo_segment_alloc_fifo");
2171 n_chunks = fifo_segment_num_free_chunks (fs, fifo_size);
2172 SFIFO_TEST (n_chunks == n_batch - 1, "free 2^10B chunks "
2173 "should be %u is %u", n_batch - 1, n_chunks);
2174 rv = fifo_segment_fl_chunk_bytes (fs);
2175 SFIFO_TEST (rv == (n_batch - 1) * fifo_size, "free chunk bytes %u "
2176 "expected %u", rv, (n_batch - 1) * fifo_size);
2179 * Grow fifo by preallocated fifo_size chunk
2181 svm_fifo_set_size (f, 2 * fifo_size);
2182 validate_test_and_buf_vecs (&test_data, &data_buf, 2 * fifo_size);
2184 rv = svm_fifo_enqueue (f, vec_len (test_data), test_data);
2185 SFIFO_TEST (rv == vec_len (test_data), "enq should succeed %u", rv);
2186 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
2188 n_chunks = fifo_segment_num_free_chunks (fs, fifo_size);
2189 SFIFO_TEST (n_chunks == n_batch - 2, "free 2^10B chunks "
2190 "should be %u is %u", n_batch - 2, n_chunks);
2191 rv = fifo_segment_fl_chunk_bytes (fs);
2192 SFIFO_TEST (rv == (n_batch - 2) * fifo_size, "free chunk bytes %u "
2193 "expected %u", rv, (n_batch - 2) * fifo_size);
2195 /* Grow by a size not preallocated but first make sure there's space */
2196 rv = fifo_segment_free_bytes (fs);
2197 SFIFO_TEST (rv > 16 * fifo_size, "free bytes %u more than %u", rv,
2200 /* Force fifo growth */
2201 svm_fifo_set_size (f, svm_fifo_size (f) + 16 * fifo_size);
2202 validate_test_and_buf_vecs (&test_data, &data_buf, svm_fifo_size (f));
2203 rv = svm_fifo_enqueue (f, vec_len (test_data), test_data);
2205 SFIFO_TEST (svm_fifo_size (f) == 18 * fifo_size, "fifo size should be %u "
2206 "is %u", 18 * fifo_size, svm_fifo_size (f));
2208 rv = fifo_segment_fl_chunk_bytes (fs);
2209 SFIFO_TEST (rv == (n_batch - 2) * fifo_size, "free chunk bytes %u "
2210 "expected %u", rv, (n_batch - 2) * fifo_size);
2213 * Free and test free list size
2215 fifo_segment_free_fifo (fs, f);
2217 rv = fifo_segment_fl_chunk_bytes (fs);
2218 SFIFO_TEST (rv == (16 + n_batch) * fifo_size, "free chunk bytes expected %u"
2219 " is %u", (16 + n_batch) * fifo_size, rv);
2220 n_chunks = fifo_segment_num_free_chunks (fs, fifo_size);
2221 SFIFO_TEST (n_chunks == n_batch, "free 2^10B chunks "
2222 "should be %u is %u", n_batch, n_chunks);
2223 n_chunks = fifo_segment_num_free_chunks (fs, 16 * fifo_size);
2224 SFIFO_TEST (n_chunks == 1, "free 2^14B chunks should be %u is %u", 1,
2226 n_chunks = fifo_segment_num_free_chunks (fs, ~0);
2227 SFIFO_TEST (n_chunks == 1 + n_batch, "free chunks should be %u is %u",
2228 1 + n_batch, n_chunks);
2233 f = fifo_segment_alloc_fifo (fs, fifo_size, FIFO_SEGMENT_RX_FIFO);
2235 /* Force chunk allocation */
2236 svm_fifo_set_size (f, svm_fifo_size (f) + fifo_size);
2237 rv = svm_fifo_enqueue (f, svm_fifo_size (f), test_data);
2239 SFIFO_TEST (rv == svm_fifo_size (f), "enq should succeed %u", rv);
2240 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
2242 n_chunks = fifo_segment_num_free_chunks (fs, fifo_size);
2243 SFIFO_TEST (n_chunks == n_batch - 2, "free 2^10B chunks should be %u is %u",
2244 n_batch - 2, n_chunks);
2246 /* Grow and alloc 16 * fifo_size chunk */
2247 svm_fifo_set_size (f, svm_fifo_size (f) + 16 * fifo_size);
2248 rv = svm_fifo_enqueue (f, svm_fifo_size (f), test_data);
2250 n_chunks = fifo_segment_num_free_chunks (fs, 16 * fifo_size);
2251 SFIFO_TEST (n_chunks == 0, "free 2^14B chunks should be %u is %u", 0,
2253 n_chunks = fifo_segment_num_free_chunks (fs, ~0);
2254 SFIFO_TEST (n_chunks == n_batch - 2, "free chunks should be %u is %u",
2255 n_batch - 2, n_chunks);
2260 fifo_segment_free_fifo (fs, f);
2261 n_chunks = fifo_segment_num_free_chunks (fs, ~0);
2262 SFIFO_TEST (n_chunks == 1 + n_batch, "free chunks should be %u is %u",
2263 1 + n_batch, n_chunks);
2265 rv = fifo_segment_fl_chunk_bytes (fs);
2266 SFIFO_TEST (rv == (16 + n_batch) * fifo_size, "free chunk bytes expected %u"
2267 " is %u", (16 + n_batch) * fifo_size, rv);
2269 n_free_chunk_bytes = rv;
2272 * Allocate non power of 2 fifo/chunk and check that free chunk bytes
2273 * is correctly updated
2276 f = fifo_segment_alloc_fifo (fs, 16 * fifo_size - 1, FIFO_SEGMENT_RX_FIFO);
2277 rv = fifo_segment_fl_chunk_bytes (fs);
2279 SFIFO_TEST (n_free_chunk_bytes - 16 * fifo_size == rv, "free chunk bytes "
2280 "expected %u is %u", n_free_chunk_bytes - 16 * fifo_size, rv);
2281 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
2283 fifo_segment_free_fifo (fs, f);
2284 rv = fifo_segment_fl_chunk_bytes (fs);
2286 SFIFO_TEST (n_free_chunk_bytes == rv, "free chunk bytes expected %u is %u",
2287 n_free_chunk_bytes, rv);
2290 * Force multi chunk fifo allocation
2293 /* Check that we can force multi chunk allocation. Note that fifo size
2294 * rounded up to power of 2, i.e., 17 becomes 32 */
2295 rv = fifo_segment_free_bytes (fs);
2296 SFIFO_TEST (rv < 32 * fifo_size, "free bytes %u less than %u", rv,
2299 f = fifo_segment_alloc_fifo (fs, 17 * fifo_size, FIFO_SEGMENT_RX_FIFO);
2300 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
2302 rv = fifo_segment_fl_chunk_bytes (fs);
2304 /* Make sure that the non-power of two chunk freed above is correctly
2305 * accounted for in the chunk free bytes reduction due to chunk allocation
2306 * for the fifo, i.e., it's rounded up by 1 */
2307 SFIFO_TEST (n_free_chunk_bytes - 17 * fifo_size == rv, "free chunk bytes "
2308 "expected %u is %u", n_free_chunk_bytes - 17 * fifo_size, rv);
2310 fifo_segment_free_fifo (fs, f);
2312 rv = fifo_segment_fl_chunk_bytes (fs);
2313 SFIFO_TEST (n_free_chunk_bytes == rv, "free chunk bytes expected %u is %u",
2314 n_free_chunk_bytes, rv);
2317 * Allocate fifo that has all chunks. Because we have a chunk size limit of
2318 * segment_size / 2, allocate 2 fifos.
2320 tf = fifo_segment_alloc_fifo (fs, n_free_chunk_bytes / 2,
2321 FIFO_SEGMENT_RX_FIFO);
2322 SFIFO_TEST (tf != 0, "allocation should work");
2323 SFIFO_TEST (svm_fifo_is_sane (tf), "fifo should be sane");
2325 f = fifo_segment_alloc_fifo (fs, n_free_chunk_bytes / 2,
2326 FIFO_SEGMENT_RX_FIFO);
2327 SFIFO_TEST (f != 0, "allocation should work");
2328 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
2330 fifo_segment_free_fifo (fs, tf);
2331 fifo_segment_free_fifo (fs, f);
2333 rv = fifo_segment_fl_chunk_bytes (fs);
2334 SFIFO_TEST (n_free_chunk_bytes == rv, "free chunk bytes expected %u is %u",
2335 n_free_chunk_bytes, rv);
2338 * Try to allocate more than space available
2341 f = fifo_segment_alloc_fifo (fs, n_free_chunk_bytes + fifo_size,
2342 FIFO_SEGMENT_RX_FIFO);
2343 SFIFO_TEST (f == 0, "allocation should fail");
2346 * Allocate fifo and try to grow beyond available space
2348 f = fifo_segment_alloc_fifo (fs, fifo_segment_free_bytes (fs),
2349 FIFO_SEGMENT_RX_FIFO);
2351 /* Try to force fifo growth */
2352 new_size = svm_fifo_size (f) + n_free_chunk_bytes + 1;
2353 svm_fifo_set_size (f, new_size);
2354 validate_test_and_buf_vecs (&test_data, &data_buf, new_size);
2355 rv = svm_fifo_enqueue (f, new_size, test_data);
2357 SFIFO_TEST (rv != new_size, "grow should fail size %u wrote %d",
2360 fifo_segment_free_fifo (fs, f);
2365 fifo_segment_delete (sm, fs);
2366 vec_free (a->new_segment_indices);
2371 sfifo_test_fifo_segment_slave (int verbose)
2373 fifo_segment_create_args_t _a, *a = &_a;
2374 fifo_segment_main_t *sm = &segment_main;
2375 u8 *test_data, *retrieved_data = 0;
2383 sm->timeout_in_seconds = 5;
2384 clib_memset (a, 0, sizeof (*a));
2385 a->segment_name = "fifo-test1";
2387 rv = fifo_segment_attach (sm, a);
2389 SFIFO_TEST (!rv, "svm_fifo_segment_attach returned %d", rv);
2391 fs = fifo_segment_get_segment (sm, a->new_segment_indices[0]);
2392 vec_free (a->new_segment_indices);
2394 /* might wanna wait.. */
2395 f = fifo_segment_get_slice_fifo_list (fs, 0);
2397 /* Lazy bastards united */
2398 test_data = format (0, "Hello world%c", 0);
2399 vec_validate (retrieved_data, vec_len (test_data) - 1);
2401 for (i = 0; i < 1000; i++)
2403 svm_fifo_dequeue (f, vec_len (retrieved_data), retrieved_data);
2404 if (memcmp (retrieved_data, test_data, vec_len (retrieved_data)))
2406 result = (u32 *) f->head_chunk->data;
2412 result = (u32 *) f->head_chunk->data;
2415 vec_free (test_data);
2416 vec_free (retrieved_data);
2421 sfifo_test_fifo_segment_master_slave (int verbose)
2423 fifo_segment_create_args_t _a, *a = &_a;
2424 fifo_segment_main_t *sm = &segment_main;
2434 SFIFO_TEST (0, "fork failed");
2437 sfifo_test_fifo_segment_slave (verbose);
2439 clib_memset (a, 0, sizeof (*a));
2440 a->segment_name = "fifo-test1";
2441 a->segment_size = 256 << 10;
2443 rv = fifo_segment_create (sm, a);
2445 SFIFO_TEST (!rv, "svm_fifo_segment_create returned %d", rv);
2447 sp = fifo_segment_get_segment (sm, a->new_segment_indices[0]);
2448 f = fifo_segment_alloc_fifo (sp, 4096, FIFO_SEGMENT_RX_FIFO);
2450 SFIFO_TEST (f != 0, "svm_fifo_segment_alloc_fifo alloc");
2452 test_data = format (0, "Hello world%c", 0);
2456 for (i = 0; i < 1000; i++)
2457 svm_fifo_enqueue (f, vec_len (test_data), test_data);
2459 /* Wait for slave */
2461 while (svm_fifo_max_dequeue (f) && i++ < 1e10)
2466 result = (u32 *) f->head_chunk->data;
2467 SFIFO_TEST (*result == 0, "slave reported no error");
2469 vec_free (a->new_segment_indices);
2470 vec_free (test_data);
2471 fifo_segment_free_fifo (sp, f);
2472 fifo_segment_delete (sm, sp);
2477 sfifo_test_fifo_segment_mempig (int verbose)
2479 fifo_segment_create_args_t _a, *a = &_a;
2480 fifo_segment_main_t *sm = &segment_main;
2483 svm_fifo_t **flist = 0;
2487 clib_memset (a, 0, sizeof (*a));
2489 a->segment_name = "fifo-test1";
2490 a->segment_size = 256 << 10;
2492 rv = fifo_segment_create (sm, a);
2494 SFIFO_TEST (!rv, "svm_fifo_segment_create returned %d", rv);
2496 sp = fifo_segment_get_segment (sm, a->new_segment_indices[0]);
2498 for (i = 0; i < 1000; i++)
2500 f = fifo_segment_alloc_fifo (sp, 4096, FIFO_SEGMENT_RX_FIFO);
2503 vec_add1 (flist, f);
2506 SFIFO_TEST (vec_len (flist), "created %d fifos", vec_len (flist));
2508 for (i = 0; i < vec_len (flist); i++)
2511 fifo_segment_free_fifo (sp, f);
2514 _vec_len (flist) = 0;
2516 for (i = 0; i < 1000; i++)
2518 f = fifo_segment_alloc_fifo (sp, 4096, FIFO_SEGMENT_RX_FIFO);
2521 vec_add1 (flist, f);
2524 SFIFO_TEST (vec_len (flist), "second try created %d fifos",
2526 for (i = 0; i < vec_len (flist); i++)
2529 fifo_segment_free_fifo (sp, f);
2532 fifo_segment_delete (sm, sp);
2537 sfifo_test_fifo_segment_prealloc (int verbose)
2539 fifo_segment_create_args_t _a, *a = &_a;
2540 fifo_segment_main_t *sm = &segment_main;
2541 u32 max_pairs, pairs_req, free_space, pair_mem;
2542 svm_fifo_t *f, *tf, *old;
2546 clib_memset (a, 0, sizeof (*a));
2548 a->segment_name = "fifo-test-prealloc";
2549 a->segment_size = 256 << 10;
2550 a->segment_type = SSVM_SEGMENT_MEMFD;
2552 rv = fifo_segment_create (sm, a);
2553 SFIFO_TEST (!rv, "svm_fifo_segment_create returned %d", rv);
2554 fs = fifo_segment_get_segment (sm, a->new_segment_indices[0]);
2555 fs->h->pct_first_alloc = 100;
2558 * Prealloc chunks and headers
2560 free_space = fifo_segment_free_bytes (fs);
2561 SFIFO_TEST (free_space <= 256 << 10, "free space expected %u is %u",
2562 256 << 10, free_space);
2563 rv = fifo_segment_prealloc_fifo_chunks (fs, 0, 4096, 50);
2564 SFIFO_TEST (rv == 0, "chunk prealloc should work");
2565 rv = fifo_segment_num_free_chunks (fs, 4096);
2566 SFIFO_TEST (rv == 50, "prealloc chunks expected %u is %u", 50, rv);
2567 rv = fifo_segment_free_bytes (fs);
2568 free_space -= (sizeof (svm_fifo_chunk_t) + 4096) * 50;
2569 SFIFO_TEST (rv == free_space, "free space expected %u is %u", free_space,
2571 rv = fifo_segment_fl_chunk_bytes (fs);
2572 SFIFO_TEST (rv == 4096 * 50, "chunk free space expected %u is %u",
2575 rv = fifo_segment_prealloc_fifo_hdrs (fs, 0, 50);
2576 SFIFO_TEST (rv == 0, "fifo hdr prealloc should work");
2577 rv = fifo_segment_num_free_fifos (fs);
2578 SFIFO_TEST (rv == 50, "prealloc fifo hdrs expected %u is %u", 50, rv);
2579 rv = fifo_segment_free_bytes (fs);
2580 free_space -= sizeof (svm_fifo_t) * 50;
2581 SFIFO_TEST (rv == free_space, "free space expected %u is %u", free_space,
2584 fifo_segment_update_free_bytes (fs);
2585 rv = fifo_segment_free_bytes (fs);
2586 SFIFO_TEST (clib_abs (rv - (int) free_space) < 512,
2587 "free space expected %u is %u", free_space, rv);
2589 /* Use all free chunk memory */
2590 f = fifo_segment_alloc_fifo (fs, 100 << 10, FIFO_SEGMENT_RX_FIFO);
2591 SFIFO_TEST (f != 0, "fifo allocated");
2592 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
2594 tf = fifo_segment_alloc_fifo (fs, 100 << 10, FIFO_SEGMENT_RX_FIFO);
2595 SFIFO_TEST (tf != 0, "fifo allocated");
2596 SFIFO_TEST (svm_fifo_is_sane (tf), "fifo should be sane");
2598 rv = fifo_segment_num_free_chunks (fs, 4096);
2599 SFIFO_TEST (rv == 0, "prealloc chunks expected %u is %u", 0, rv);
2600 rv = fifo_segment_fl_chunk_bytes (fs);
2601 SFIFO_TEST (rv == 0, "chunk free space expected %u is %u", 0, rv);
2605 * Multiple preallocs that consume the remaining space
2607 fifo_segment_update_free_bytes (fs);
2608 free_space = fifo_segment_free_bytes (fs);
2609 pair_mem = 2 * (4096 + sizeof (*f) + sizeof (svm_fifo_chunk_t));
2610 max_pairs = pairs_req = (free_space / pair_mem) - 1;
2611 fifo_segment_preallocate_fifo_pairs (fs, 4096, 4096, &pairs_req);
2612 SFIFO_TEST (pairs_req == 0, "prealloc pairs should work req %u", max_pairs);
2613 rv = fifo_segment_num_free_chunks (fs, 4096);
2614 SFIFO_TEST (rv == max_pairs * 2, "prealloc chunks expected %u is %u",
2617 fifo_segment_update_free_bytes (fs);
2618 rv = fifo_segment_free_bytes (fs);
2619 SFIFO_TEST (rv < 2 * pair_mem, "free bytes %u less than %u", rv,
2622 /* Preallocate as many more chunks as possible. Heap is almost full
2623 * so we may not use all the free space*/
2625 while (!fifo_segment_prealloc_fifo_chunks (fs, 0, 4096, 1))
2627 SFIFO_TEST (alloc, "chunk prealloc should work %u", alloc);
2628 rv = fifo_segment_num_free_chunks (fs, 4096);
2629 SFIFO_TEST (rv == max_pairs * 2 + alloc, "prealloc chunks expected %u "
2630 "is %u", max_pairs * 2 + alloc, rv);
2632 rv = fifo_segment_free_bytes (fs);
2633 SFIFO_TEST (rv < pair_mem, "free bytes expected less than %u is %u",
2637 * Test negative prealloc cases
2640 fifo_segment_preallocate_fifo_pairs (fs, 4096, 4096, &pairs_req);
2641 SFIFO_TEST (pairs_req == 1, "prealloc pairs should not work");
2644 f = fifo_segment_alloc_fifo (fs, 200 << 10, FIFO_SEGMENT_RX_FIFO);
2645 SFIFO_TEST (f == 0, "fifo alloc should fail");
2647 rv = fifo_segment_prealloc_fifo_chunks (fs, 0, 4096, 50);
2648 SFIFO_TEST (rv == -1, "chunk prealloc should fail");
2650 rv = fifo_segment_prealloc_fifo_hdrs (fs, 0, 50);
2651 SFIFO_TEST (rv == -1, "fifo hdr prealloc should fail");
2656 fifo_segment_free_fifo (fs, old);
2657 fifo_segment_free_fifo (fs, tf);
2658 close (fs->ssvm.fd);
2659 fifo_segment_delete (sm, fs);
2664 sfifo_test_fifo_segment (vlib_main_t * vm, unformat_input_t * input)
2666 int rv, verbose = 0;
2668 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2670 if (unformat (input, "verbose"))
2672 else if (unformat (input, "masterslave"))
2674 if ((rv = sfifo_test_fifo_segment_master_slave (verbose)))
2677 else if (unformat (input, "basic"))
2679 if ((rv = sfifo_test_fifo_segment_hello_world (verbose)))
2682 else if (unformat (input, "mempig"))
2684 if ((rv = sfifo_test_fifo_segment_mempig (verbose)))
2687 else if (unformat (input, "grow fifo"))
2689 if ((rv = sfifo_test_fifo_segment_fifo_grow (verbose)))
2692 else if (unformat (input, "prealloc"))
2694 if ((rv = sfifo_test_fifo_segment_prealloc (verbose)))
2697 else if (unformat (input, "all"))
2699 if ((rv = sfifo_test_fifo_segment_hello_world (verbose)))
2701 if ((rv = sfifo_test_fifo_segment_mempig (verbose)))
2703 if ((rv = sfifo_test_fifo_segment_fifo_grow (verbose)))
2705 if ((rv = sfifo_test_fifo_segment_prealloc (verbose)))
2707 /* Pretty slow so avoid running it always
2708 if ((rv = sfifo_test_fifo_segment_master_slave (verbose)))
2714 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
2722 static clib_error_t *
2723 svm_fifo_test (vlib_main_t * vm, unformat_input_t * input,
2724 vlib_cli_command_t * cmd_arg)
2729 clib_warning ("high mem %lu", HIGH_SEGMENT_BASEVA << 1);
2730 fifo_segment_main_init (&segment_main, HIGH_SEGMENT_BASEVA << 1, 5);
2731 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2733 if (unformat (input, "fifo1"))
2734 res = sfifo_test_fifo1 (vm, input);
2735 else if (unformat (input, "fifo2"))
2736 res = sfifo_test_fifo2 (vm);
2737 else if (unformat (input, "fifo3"))
2738 res = sfifo_test_fifo3 (vm, input);
2739 else if (unformat (input, "fifo4"))
2740 res = sfifo_test_fifo4 (vm, input);
2741 else if (unformat (input, "fifo5"))
2742 res = sfifo_test_fifo5 (vm, input);
2743 else if (unformat (input, "fifo6"))
2744 res = sfifo_test_fifo6 (vm, input);
2745 else if (unformat (input, "fifo7"))
2746 res = sfifo_test_fifo7 (vm, input);
2747 else if (unformat (input, "large"))
2748 res = sfifo_test_fifo_large (vm, input);
2749 else if (unformat (input, "replay"))
2750 res = sfifo_test_fifo_replay (vm, input);
2751 else if (unformat (input, "grow"))
2752 res = sfifo_test_fifo_grow (vm, input);
2753 else if (unformat (input, "shrink"))
2754 res = sfifo_test_fifo_shrink (vm, input);
2755 else if (unformat (input, "indirect"))
2756 res = sfifo_test_fifo_indirect (vm, input);
2757 else if (unformat (input, "zero"))
2758 res = sfifo_test_fifo_make_rcv_wnd_zero (vm, input);
2759 else if (unformat (input, "segment"))
2760 res = sfifo_test_fifo_segment (vm, input);
2761 else if (unformat (input, "all"))
2763 if ((res = sfifo_test_fifo1 (vm, input)))
2766 if ((res = sfifo_test_fifo2 (vm)))
2770 * Run a number of fifo3 configs
2772 str = "nsegs 10 overlap seed 123";
2773 unformat_init_cstring (input, str);
2774 if ((res = sfifo_test_fifo3 (vm, input)))
2776 unformat_free (input);
2778 str = "nsegs 10 overlap seed 123 in-seq-all";
2779 unformat_init_cstring (input, str);
2780 if ((res = sfifo_test_fifo3 (vm, input)))
2782 unformat_free (input);
2784 str = "nsegs 10 overlap seed 123 initial-offset 3917";
2785 unformat_init_cstring (input, str);
2786 if ((res = sfifo_test_fifo3 (vm, input)))
2788 unformat_free (input);
2790 str = "nsegs 10 overlap seed 123 initial-offset 3917 drop";
2791 unformat_init_cstring (input, str);
2792 if ((res = sfifo_test_fifo3 (vm, input)))
2794 unformat_free (input);
2796 str = "nsegs 10 seed 123 initial-offset 3917 drop no-randomize";
2797 unformat_init_cstring (input, str);
2798 if ((res = sfifo_test_fifo3 (vm, input)))
2800 unformat_free (input);
2802 if ((res = sfifo_test_fifo4 (vm, input)))
2805 if ((res = sfifo_test_fifo5 (vm, input)))
2808 if ((res = sfifo_test_fifo6 (vm, input)))
2811 if ((res = sfifo_test_fifo7 (vm, input)))
2814 if ((res = sfifo_test_fifo_grow (vm, input)))
2817 if ((res = sfifo_test_fifo_shrink (vm, input)))
2820 if ((res = sfifo_test_fifo_indirect (vm, input)))
2823 if ((res = sfifo_test_fifo_make_rcv_wnd_zero (vm, input)))
2827 unformat_init_cstring (input, str);
2828 if ((res = sfifo_test_fifo_segment (vm, input)))
2833 vlib_cli_output (vm, "unknown input `%U'", format_unformat_error,
2843 return clib_error_return (0, "svm fifo unit test failed");
2848 VLIB_CLI_COMMAND (svm_fifo_test_command, static) =
2850 .path = "test svm fifo",
2851 .short_help = "internal svm fifo unit tests",
2852 .function = svm_fifo_test,
2857 * fd.io coding-style-patch-verification: ON
2860 * eval: (c-set-style "gnu")