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)
195 f = fifo_segment_alloc_fifo (fs, fifo_size, FIFO_SEGMENT_RX_FIFO);
197 /* Paint 1st fifo chunk with -1's */
198 c = svm_fifo_head_chunk (f);
199 clib_memset (c->data, 0xFF, c->length);
201 svm_fifo_init_ooo_lookup (f, 1 /* deq ooo */ );
206 ft_fifo_free (fifo_segment_t * fs, svm_fifo_t * f)
208 fifo_segment_free_fifo (fs, f);
212 compare_data (u8 * data1, u8 * data2, u32 start, u32 len, u32 * index)
216 for (i = start; i < start + len; i++)
218 if (data1[i] != data2[i])
228 sfifo_test_fifo1 (vlib_main_t * vm, unformat_input_t * input)
230 u32 fifo_size = 1 << 20, *test_data = 0, offset, data_word, test_data_len;
231 fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
232 u8 *data, *s, *data_buf = 0;
233 int i, rv, verbose = 0;
234 ooo_segment_t *ooo_seg;
239 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
241 if (unformat (input, "verbose"))
245 test_data_len = fifo_size / sizeof (u32);
246 vec_validate (test_data, test_data_len - 1);
248 for (i = 0; i < vec_len (test_data); i++)
251 fs = fifo_segment_prepare (fsm, "fifo-test1", 0);
252 f = fifo_prepare (fs, fifo_size);
255 * Enqueue an initial (un-dequeued) chunk
257 rv = svm_fifo_enqueue (f, sizeof (u32), (u8 *) test_data);
258 SFIFO_TEST ((rv == sizeof (u32)), "enqueued %d", rv);
259 SFIFO_TEST ((f->tail == 4), "fifo tail %u", f->tail);
262 * Create 3 chunks in the future. The offsets are relative
263 * to the current fifo tail
265 for (i = 0; i < 3; i++)
267 offset = (2 * i + 1) * sizeof (u32) - f->tail;
268 data = (u8 *) (test_data + (2 * i + 1));
271 rv = svm_fifo_enqueue (f, sizeof (u32), data);
272 rv = rv > 0 ? 0 : rv;
275 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
277 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i + 1, offset,
278 offset + sizeof (u32));
281 clib_warning ("enqueue returned %d", rv);
287 vlib_cli_output (vm, "fifo after odd segs: %U", format_svm_fifo, f, 1);
289 SFIFO_TEST ((f->tail == 8), "fifo tail %u", f->tail);
290 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 2),
291 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
294 * Try adding a completely overlapped segment
296 offset = 3 * sizeof (u32) - f->tail;
297 data = (u8 *) (test_data + 3);
298 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
301 clib_warning ("enqueue returned %d", rv);
306 vlib_cli_output (vm, "fifo after overlap seg: %U", format_svm_fifo, f, 1);
308 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 2),
309 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
312 * Make sure format functions are not buggy
314 s = format (0, "%U", format_svm_fifo, f, 2);
318 * Paint some of missing data backwards
320 for (i = 3; i > 1; i--)
322 offset = (2 * i + 0) * sizeof (u32) - f->tail;
323 data = (u8 *) (test_data + (2 * i + 0));
324 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
326 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i, offset,
327 offset + sizeof (u32));
330 clib_warning ("enqueue returned %d", rv);
336 vlib_cli_output (vm, "fifo before missing link: %U", format_svm_fifo, f,
338 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 1),
339 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
340 ooo_seg = svm_fifo_first_ooo_segment (f);
341 SFIFO_TEST ((ooo_seg->start == 12),
342 "first ooo seg position %u", ooo_seg->start);
343 SFIFO_TEST ((ooo_seg->length == 16),
344 "first ooo seg length %u", ooo_seg->length);
347 * Enqueue the missing u32
349 rv = svm_fifo_enqueue (f, sizeof (u32), (u8 *) (test_data + 2));
351 vlib_cli_output (vm, "fifo after missing link: %U", format_svm_fifo, f,
353 SFIFO_TEST ((rv == 20), "bytes to be enqueued %u", rv);
354 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 0),
355 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
360 for (i = 0; i < 7; i++)
362 rv = svm_fifo_dequeue (f, sizeof (u32), (u8 *) & data_word);
363 if (rv != sizeof (u32))
365 clib_warning ("bytes dequeues %u", rv);
368 if (data_word != test_data[i])
370 clib_warning ("recovered [%d] %d not %d", i, data_word,
377 * Test segment overlaps: last ooo segment overlaps all
379 ft_fifo_free (fs, f);
380 f = fifo_prepare (fs, fifo_size);
382 for (i = 0; i < 4; i++)
384 offset = (2 * i + 1) * sizeof (u32) - f->tail;
385 data = (u8 *) (test_data + (2 * i + 1));
386 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
388 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i + 1, offset,
389 offset + sizeof (u32));
392 clib_warning ("enqueue returned %d", rv);
397 rv = svm_fifo_enqueue_with_offset (f, 8 - f->tail, 21, data);
398 SFIFO_TEST ((rv == 0), "ooo enqueued %u", rv);
399 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 1),
400 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
402 /* add missing data to be able to dequeue something */
403 rv = svm_fifo_enqueue (f, 4, data);
404 SFIFO_TEST ((rv == 32), "enqueued %u", rv);
405 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 0),
406 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
408 vec_validate (data_buf, vec_len (test_data));
409 svm_fifo_peek (f, 0, 4, data_buf);
410 if (compare_data (data_buf, data, 0, 4, &j))
411 SFIFO_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j], data[j]);
412 svm_fifo_peek (f, 8, 21, data_buf);
413 if (compare_data (data_buf, data, 0, 21, &j))
414 SFIFO_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j], data[j]);
415 vec_reset_length (data_buf);
418 * Test segment overlaps: enqueue and overlap ooo segments
420 ft_fifo_free (fs, f);
421 f = fifo_prepare (fs, fifo_size);
423 for (i = 0; i < 4; i++)
425 offset = (2 * i + 1) * sizeof (u32) - f->tail;
426 data = (u8 *) (test_data + (2 * i + 1));
427 rv = svm_fifo_enqueue_with_offset (f, offset, sizeof (u32), data);
429 vlib_cli_output (vm, "add [%d] [%d, %d]", 2 * i + 1, offset,
430 offset + sizeof (u32));
433 clib_warning ("enqueue returned %d", rv);
439 vlib_cli_output (vm, "fifo after enqueue: %U", format_svm_fifo, f, 1);
441 rv = svm_fifo_enqueue (f, 29, data);
443 vlib_cli_output (vm, "fifo after enqueueing 29: %U", format_svm_fifo, f,
445 SFIFO_TEST ((rv == 32), "ooo enqueued %u", rv);
446 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 0),
447 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
449 vec_validate (data_buf, vec_len (data));
450 svm_fifo_peek (f, 0, vec_len (data), data_buf);
451 if (compare_data (data_buf, data, 0, vec_len (data), &j))
453 SFIFO_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j], data[j]);
456 /* Try to peek beyond the data */
457 rv = svm_fifo_peek (f, svm_fifo_max_dequeue (f), vec_len (data), data_buf);
458 SFIFO_TEST ((rv == 0), "peeked %u expected 0", rv);
461 ft_fifo_free (fs, f);
462 ft_fifo_segment_free (fsm, fs);
463 vec_free (test_data);
468 ft_fifo_free (fs, f);
469 ft_fifo_segment_free (fsm, fs);
470 vec_free (test_data);
476 sfifo_test_fifo2 (vlib_main_t * vm)
478 fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
479 test_pattern_t *tp, *vp, *test_data;
480 u32 fifo_size = (1 << 20) + 1;
481 int i, rv, test_data_len;
482 ooo_segment_t *ooo_seg;
487 test_data = test_pattern;
488 test_data_len = ARRAY_LEN (test_pattern);
490 vp = fifo_get_validate_pattern (vm, test_data, test_data_len);
493 fs = fifo_segment_prepare (fsm, "fifo-test2", 0);
494 f = fifo_prepare (fs, fifo_size);
497 * Try with sorted data
499 for (i = 0; i < test_data_len; i++)
503 svm_fifo_enqueue_with_offset (f, tp->offset - f->tail, tp->len,
507 /* Expected result: one big fat chunk at offset 4 */
508 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 1),
509 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
510 ooo_seg = svm_fifo_first_ooo_segment (f);
511 SFIFO_TEST ((ooo_seg->start == 4),
512 "first ooo seg position %u", ooo_seg->start);
513 SFIFO_TEST ((ooo_seg->length == 2996),
514 "first ooo seg length %u", ooo_seg->length);
517 rv = svm_fifo_enqueue (f, sizeof (u32), (u8 *) & data64);
518 SFIFO_TEST ((rv == 3000), "bytes to be enqueued %u", rv);
520 ft_fifo_free (fs, f);
524 * Now try it again w/ unsorted data...
527 f = fifo_prepare (fs, fifo_size);
529 for (i = 0; i < test_data_len; i++)
533 rv = svm_fifo_enqueue_with_offset (f, tp->offset - f->tail, tp->len,
537 clib_warning ("enqueue returned %d", rv);
541 /* Expecting the same result: one big fat chunk at offset 4 */
542 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 1),
543 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
544 ooo_seg = svm_fifo_first_ooo_segment (f);
545 SFIFO_TEST ((ooo_seg->start == 4),
546 "first ooo seg position %u", ooo_seg->start);
547 SFIFO_TEST ((ooo_seg->length == 2996),
548 "first ooo seg length %u", ooo_seg->length);
551 rv = svm_fifo_enqueue (f, sizeof (u32), (u8 *) & data64);
553 SFIFO_TEST ((rv == 3000), "bytes to be enqueued %u", rv);
555 ft_fifo_free (fs, f);
556 ft_fifo_segment_free (fsm, fs);
562 sfifo_test_fifo3 (vlib_main_t * vm, unformat_input_t * input)
564 u32 nsegs = 2, seg_size, length_so_far, current_offset, offset_increment;
565 int overlap = 0, verbose = 0, randomize = 1, drop = 0, in_seq_all = 0;
566 u32 len_this_chunk, seed = 0xdeaddabe, j, total_size = 2 << 10;
567 u32 fifo_size = (4 << 10) + 1, fifo_initial_offset = 0;
568 fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
569 u8 *data_pattern = 0, *data_buf = 0;
570 test_pattern_t *tp, *generate = 0;
575 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
577 if (unformat (input, "fifo-size %d", &fifo_size))
579 else if (unformat (input, "total-size %d", &total_size))
581 else if (unformat (input, "verbose"))
583 else if (unformat (input, "overlap"))
585 else if (unformat (input, "initial-offset %d", &fifo_initial_offset))
587 else if (unformat (input, "seed %d", &seed))
589 else if (unformat (input, "nsegs %d", &nsegs))
591 else if (unformat (input, "no-randomize"))
593 else if (unformat (input, "in-seq-all"))
595 else if (unformat (input, "drop"))
599 clib_error_t *e = clib_error_return
600 (0, "unknown input `%U'", format_unformat_error, input);
601 clib_error_report (e);
606 if (total_size > fifo_size)
608 clib_warning ("total_size %d greater than fifo size %d", total_size,
612 if (overlap && randomize == 0)
614 clib_warning ("Can't enqueue in-order with overlap");
621 vec_validate (data_pattern, total_size - 1);
622 for (i = 0; i < vec_len (data_pattern); i++)
623 data_pattern[i] = i & 0xff;
628 seg_size = total_size / nsegs;
630 current_offset = randomize;
631 while (length_so_far < total_size)
633 vec_add2 (generate, tp, 1);
634 len_this_chunk = clib_min (seg_size, total_size - length_so_far);
635 tp->offset = current_offset;
636 tp->len = len_this_chunk;
638 if (overlap && (len_this_chunk == seg_size))
641 offset_increment = len_this_chunk
642 % (1 + (random_u32 (&seed) % len_this_chunk));
644 while (offset_increment == 0);
646 offset_increment = len_this_chunk;
648 current_offset += offset_increment;
649 length_so_far = tp->offset + tp->len;
653 * Validate segment list. Only valid for non-overlap cases.
656 fifo_validate_pattern (vm, generate, vec_len (generate));
660 vlib_cli_output (vm, "raw data pattern:");
661 for (i = 0; i < vec_len (generate); i++)
663 vlib_cli_output (vm, "[%d] offset %u len %u", i,
664 generate[i].offset, generate[i].len);
668 /* Randomize data pattern */
671 for (i = 0; i < vec_len (generate) / 2; i++)
673 u32 src_index, dst_index;
674 test_pattern_t _tmp, *tmp = &_tmp;
676 src_index = random_u32 (&seed) % vec_len (generate);
677 dst_index = random_u32 (&seed) % vec_len (generate);
679 tmp[0] = generate[dst_index];
680 generate[dst_index] = generate[src_index];
681 generate[src_index] = tmp[0];
685 vlib_cli_output (vm, "randomized data pattern:");
686 for (i = 0; i < vec_len (generate); i++)
688 vlib_cli_output (vm, "[%d] offset %u len %u", i,
689 generate[i].offset, generate[i].len);
695 * Create a fifo and add segments
697 fs = fifo_segment_prepare (fsm, "fifo-test3", 0);
698 f = fifo_prepare (fs, fifo_size);
700 /* manually set head and tail pointers to validate modular arithmetic */
701 fifo_initial_offset = fifo_initial_offset % fifo_size;
702 svm_fifo_init_pointers (f, fifo_initial_offset, fifo_initial_offset);
704 for (i = !randomize; i < vec_len (generate); i++)
707 svm_fifo_enqueue_with_offset (f,
708 fifo_initial_offset + tp->offset -
710 (u8 *) data_pattern + tp->offset);
713 /* Add the first segment in order for non random data */
715 svm_fifo_enqueue (f, generate[0].len, (u8 *) data_pattern);
718 * Expected result: one big fat chunk at offset 1 if randomize == 1
722 vlib_cli_output (vm, "fifo before missing link: %U",
723 format_svm_fifo, f, 1 /* verbose */ );
726 * Add the missing byte if segments were randomized
730 u32 bytes_to_enq = 1;
732 bytes_to_enq = total_size;
733 rv = svm_fifo_enqueue (f, bytes_to_enq, data_pattern + 0);
736 vlib_cli_output (vm, "in-order enqueue returned %d", rv);
738 SFIFO_TEST ((rv == total_size), "enqueued %u expected %u", rv,
743 SFIFO_TEST ((svm_fifo_has_ooo_data (f) == 0), "number of ooo segments %u",
744 svm_fifo_n_ooo_segments (f));
747 * Test if peeked data is the same as original data
749 vec_validate (data_buf, vec_len (data_pattern));
750 svm_fifo_peek (f, 0, vec_len (data_pattern), data_buf);
751 if (compare_data (data_buf, data_pattern, 0, vec_len (data_pattern), &j))
753 SFIFO_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j],
758 * Dequeue or drop all data
762 svm_fifo_dequeue_drop (f, vec_len (data_pattern));
766 memset (data_buf, 0, vec_len (data_pattern));
767 svm_fifo_dequeue (f, vec_len (data_pattern), data_buf);
769 (data_buf, data_pattern, 0, vec_len (data_pattern), &j))
771 SFIFO_TEST (0, "[%d] dequeued %u expected %u", j, data_buf[j],
776 SFIFO_TEST ((svm_fifo_max_dequeue (f) == 0), "fifo has %d bytes",
777 svm_fifo_max_dequeue (f));
779 ft_fifo_free (fs, f);
780 ft_fifo_segment_free (fsm, fs);
781 vec_free (data_pattern);
788 sfifo_test_fifo4 (vlib_main_t * vm, unformat_input_t * input)
790 u32 fifo_size = 6 << 10, fifo_initial_offset = 1e9, test_n_bytes = 5000, j;
791 fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
792 u8 *test_data = 0, *data_buf = 0;
793 int i, rv, verbose = 0;
797 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
799 if (unformat (input, "verbose"))
803 clib_error_t *e = clib_error_return
804 (0, "unknown input `%U'", format_unformat_error, input);
805 clib_error_report (e);
811 * Create a fifo and add segments
813 fs = fifo_segment_prepare (fsm, "fifo-test4", 0);
814 f = fifo_prepare (fs, fifo_size);
816 /* Set head and tail pointers */
817 fifo_initial_offset = fifo_initial_offset % fifo_size;
818 svm_fifo_init_pointers (f, fifo_initial_offset, fifo_initial_offset);
820 vec_validate (test_data, test_n_bytes - 1);
821 for (i = 0; i < vec_len (test_data); i++)
824 for (i = test_n_bytes - 1; i > 0; i--)
826 rv = svm_fifo_enqueue_with_offset (f, fifo_initial_offset + i - f->tail,
827 sizeof (u8), &test_data[i]);
829 vlib_cli_output (vm, "add [%d] [%d, %d]", i, i, i + sizeof (u8));
831 SFIFO_TEST (0, "enqueue returned %d", rv);
834 svm_fifo_enqueue (f, sizeof (u8), &test_data[0]);
836 vec_validate (data_buf, vec_len (test_data));
838 svm_fifo_dequeue (f, vec_len (test_data), data_buf);
839 rv = compare_data (data_buf, test_data, 0, vec_len (test_data), &j);
841 vlib_cli_output (vm, "[%d] dequeued %u expected %u", j, data_buf[j],
843 SFIFO_TEST ((rv == 0), "dequeued compared to original returned %d", rv);
845 ft_fifo_free (fs, f);
846 ft_fifo_segment_free (fsm, fs);
847 vec_free (test_data);
852 fifo_pos (svm_fifo_t * f, u32 pos)
857 /* Avoids exposing svm_fifo.c internal function */
858 static ooo_segment_t *
859 ooo_seg_next (svm_fifo_t * f, ooo_segment_t * s)
861 if (pool_is_free_index (f->ooo_segments, s->next))
863 return pool_elt_at_index (f->ooo_segments, s->next);
867 sfifo_test_fifo5 (vlib_main_t * vm, unformat_input_t * input)
869 fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
870 u32 fifo_size = 401, j = 0, offset = 200;
871 u8 *test_data = 0, *data_buf = 0;
872 int i, rv, verbose = 0;
873 ooo_segment_t *ooo_seg;
877 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
879 if (unformat (input, "verbose"))
883 clib_error_t *e = clib_error_return (0, "unknown input `%U'",
884 format_unformat_error, input);
885 clib_error_report (e);
890 fs = fifo_segment_prepare (fsm, "fifo-test5", 0);
891 f = fifo_prepare (fs, fifo_size);
892 svm_fifo_init_pointers (f, offset, offset);
894 vec_validate (test_data, 399);
895 for (i = 0; i < vec_len (test_data); i++)
896 test_data[i] = i % 0xff;
899 * Start with [100, 200] and [300, 400]
901 svm_fifo_enqueue_with_offset (f, 100, 100, &test_data[100]);
902 svm_fifo_enqueue_with_offset (f, 300, 100, &test_data[300]);
904 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 2),
905 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
906 SFIFO_TEST ((f->ooos_newest == 1), "newest %u", f->ooos_newest);
908 vlib_cli_output (vm, "fifo after [100, 200] and [300, 400] : %U",
909 format_svm_fifo, f, 2 /* verbose */ );
915 rv = svm_fifo_enqueue_with_offset (f, 225, 50, &test_data[225]);
917 vlib_cli_output (vm, "fifo after [225, 275] : %U",
918 format_svm_fifo, f, 2 /* verbose */ );
919 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 3),
920 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
921 ooo_seg = svm_fifo_first_ooo_segment (f);
922 SFIFO_TEST ((ooo_seg->start == fifo_pos (f, 100 + offset)),
923 "first seg start %u expected %u", ooo_seg->start,
924 fifo_pos (f, 100 + offset));
925 SFIFO_TEST ((ooo_seg->length == 100), "first seg length %u expected %u",
926 ooo_seg->length, 100);
927 ooo_seg = ooo_seg_next (f, ooo_seg);
928 SFIFO_TEST ((ooo_seg->start == fifo_pos (f, 225 + offset)),
929 "second seg start %u expected %u",
930 ooo_seg->start, fifo_pos (f, 225 + offset));
931 SFIFO_TEST ((ooo_seg->length == 50), "second seg length %u expected %u",
932 ooo_seg->length, 50);
933 ooo_seg = ooo_seg_next (f, ooo_seg);
934 SFIFO_TEST ((ooo_seg->start == fifo_pos (f, 300 + offset)),
935 "third seg start %u expected %u",
936 ooo_seg->start, fifo_pos (f, 300 + offset));
937 SFIFO_TEST ((ooo_seg->length == 100), "third seg length %u expected %u",
938 ooo_seg->length, 100);
939 SFIFO_TEST ((f->ooos_newest == 2), "newest %u", f->ooos_newest);
943 rv = svm_fifo_enqueue_with_offset (f, 190, 120, &test_data[190]);
945 vlib_cli_output (vm, "fifo after [190, 310] : %U",
946 format_svm_fifo, f, 1 /* verbose */ );
947 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 1),
948 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
949 ooo_seg = svm_fifo_first_ooo_segment (f);
950 SFIFO_TEST ((ooo_seg->start == fifo_pos (f, offset + 100)),
951 "first seg start %u expected %u",
952 ooo_seg->start, fifo_pos (f, offset + 100));
953 SFIFO_TEST ((ooo_seg->length == 300), "first seg length %u expected %u",
954 ooo_seg->length, 300);
959 rv = svm_fifo_enqueue (f, 150, test_data);
962 vlib_cli_output (vm, "fifo after [0 150] : %U", format_svm_fifo, f,
965 SFIFO_TEST ((rv == 400), "managed to enqueue %u expected %u", rv, 400);
966 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 0),
967 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
969 vec_validate (data_buf, 399);
970 svm_fifo_peek (f, 0, 400, data_buf);
971 if (compare_data (data_buf, test_data, 0, 400, &j))
973 SFIFO_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j],
978 * Add [100 200] and overlap it with [50 250]
980 ft_fifo_free (fs, f);
981 f = fifo_prepare (fs, fifo_size);
983 svm_fifo_enqueue_with_offset (f, 100, 100, &test_data[100]);
984 svm_fifo_enqueue_with_offset (f, 50, 200, &test_data[50]);
985 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 1),
986 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
987 ooo_seg = svm_fifo_first_ooo_segment (f);
988 SFIFO_TEST ((ooo_seg->start == 50), "first seg start %u expected %u",
990 SFIFO_TEST ((ooo_seg->length == 200), "first seg length %u expected %u",
991 ooo_seg->length, 200);
993 ft_fifo_free (fs, f);
994 ft_fifo_segment_free (fsm, fs);
995 vec_free (test_data);
1000 * Test ooo head/tail u32 wrapping
1003 sfifo_test_fifo6 (vlib_main_t * vm, unformat_input_t * input)
1005 fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
1006 u32 fifo_size = 101, n_test_bytes = 100;
1007 int i, j, rv, __clib_unused verbose = 0;
1008 u8 *test_data = 0, *data_buf = 0;
1009 ooo_segment_t *ooo_seg;
1013 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1015 if (unformat (input, "verbose"))
1019 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
1025 fs = fifo_segment_prepare (fsm, "fifo-test6", 0);
1026 f = fifo_prepare (fs, fifo_size);
1028 vec_validate (test_data, n_test_bytes - 1);
1029 vec_validate (data_buf, n_test_bytes - 1);
1030 for (i = 0; i < vec_len (test_data); i++)
1031 test_data[i] = i % 0xff;
1034 * Add ooo with tail and ooo segment start u32 wrap
1036 svm_fifo_init_pointers (f, ~0 % fifo_size, ~0 % fifo_size);
1037 svm_fifo_enqueue_with_offset (f, 10, 10, &test_data[10]);
1038 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 1),
1039 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
1040 ooo_seg = svm_fifo_first_ooo_segment (f);
1041 rv = ooo_segment_offset_prod (f, ooo_seg);
1042 SFIFO_TEST (rv == 10, "offset should be %u is %u", 10, rv);
1044 svm_fifo_enqueue (f, 10, test_data);
1045 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 0),
1046 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
1047 SFIFO_TEST (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX,
1048 "there should be no ooo seg");
1050 svm_fifo_peek (f, 5, 10, &data_buf[5]);
1051 if (compare_data (data_buf, test_data, 5, 10, (u32 *) & j))
1052 SFIFO_TEST (0, "[%d] dequeued %u expected %u", j, data_buf[j],
1055 svm_fifo_dequeue (f, 20, data_buf);
1056 if (compare_data (data_buf, test_data, 0, 20, (u32 *) & j))
1057 SFIFO_TEST (0, "[%d] dequeued %u expected %u", j, data_buf[j],
1061 * Force collect with tail u32 wrap and without ooo segment start u32 wrap
1063 svm_fifo_init_pointers (f, (~0 - 10) % fifo_size, (~0 - 10) % fifo_size);
1064 svm_fifo_enqueue_with_offset (f, 5, 15, &test_data[5]);
1065 svm_fifo_enqueue (f, 12, test_data);
1067 SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 0),
1068 "number of ooo segments %u", svm_fifo_n_ooo_segments (f));
1069 SFIFO_TEST (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX,
1070 "there should be no ooo seg");
1072 svm_fifo_dequeue (f, 20, data_buf);
1073 if (compare_data (data_buf, test_data, 0, 20, (u32 *) & j))
1074 SFIFO_TEST (0, "[%d] dequeued %u expected %u", j, data_buf[j],
1080 vec_free (test_data);
1081 vec_free (data_buf);
1082 ft_fifo_free (fs, f);
1083 ft_fifo_segment_free (fsm, fs);
1088 * Multiple ooo enqueues and dequeues that force fifo tail/head wrap
1091 sfifo_test_fifo7 (vlib_main_t * vm, unformat_input_t * input)
1093 fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
1094 u32 fifo_size = 101, n_iterations = 100;
1095 int i, j, rv, __clib_unused verbose = 0;
1096 u8 *test_data = 0, *data_buf = 0;
1097 u64 n_test_bytes = 100;
1101 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1103 if (unformat (input, "verbose"))
1107 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
1114 * Prepare data structures
1116 fs = fifo_segment_prepare (fsm, "fifo-test7", 0);
1117 f = fifo_prepare (fs, fifo_size);
1118 svm_fifo_init_pointers (f, ~0 % fifo_size, ~0 % fifo_size);
1120 vec_validate (test_data, n_test_bytes - 1);
1121 vec_validate (data_buf, n_test_bytes - 1);
1122 for (i = 0; i < vec_len (test_data); i++)
1123 test_data[i] = i % 0xff;
1126 * Run n iterations of test
1128 for (i = 0; i < n_iterations; i++)
1130 for (j = n_test_bytes - 1; j > 0; j -= 2)
1132 svm_fifo_enqueue_with_offset (f, j, 1, &test_data[j]);
1133 rv = svm_fifo_n_ooo_segments (f);
1134 if (rv != (n_test_bytes - j) / 2 + 1)
1135 SFIFO_TEST (0, "number of ooo segments expected %u is %u",
1136 (n_test_bytes - j) / 2 + 1, rv);
1139 svm_fifo_enqueue_with_offset (f, 1, n_test_bytes - 1, &test_data[1]);
1140 rv = svm_fifo_n_ooo_segments (f);
1142 SFIFO_TEST (0, "number of ooo segments %u", rv);
1144 svm_fifo_enqueue (f, 1, test_data);
1145 rv = svm_fifo_n_ooo_segments (f);
1147 SFIFO_TEST (0, "number of ooo segments %u", rv);
1149 svm_fifo_dequeue (f, n_test_bytes, data_buf);
1150 if (compare_data (data_buf, test_data, 0, n_test_bytes, (u32 *) & j))
1151 SFIFO_TEST (0, "[%d] dequeued %u expected %u", j, data_buf[j],
1153 svm_fifo_init_pointers (f, (~0 - i) % f->size, (~0 - i) % f->size);
1155 SFIFO_TEST (1, "passed multiple ooo enqueue/dequeue");
1160 vec_free (test_data);
1161 vec_free (data_buf);
1162 ft_fifo_free (fs, f);
1163 ft_fifo_segment_free (fsm, fs);
1168 * Enqueue more than 4GB
1171 sfifo_test_fifo_large (vlib_main_t * vm, unformat_input_t * input)
1173 u32 n_iterations = 100, n_bytes_per_iter, half, fifo_size;
1174 fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
1175 int i, j, rv, __clib_unused verbose = 0;
1176 u8 *test_data = 0, *data_buf = 0;
1177 u64 n_test_bytes = 100;
1181 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1183 if (unformat (input, "verbose"))
1187 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
1193 n_test_bytes = 5ULL << 30;
1194 n_iterations = 1 << 10;
1195 n_bytes_per_iter = n_test_bytes / n_iterations;
1196 fifo_size = n_bytes_per_iter + 1;
1198 fs = fifo_segment_prepare (fsm, "fifo-large", 0);
1199 f = fifo_prepare (fs, fifo_size);
1200 svm_fifo_init_pointers (f, ~0 % fifo_size, ~0 % fifo_size);
1202 vec_validate (test_data, n_bytes_per_iter - 1);
1203 vec_validate (data_buf, n_bytes_per_iter - 1);
1204 for (i = 0; i < vec_len (test_data); i++)
1205 test_data[i] = i % 0xff;
1207 half = n_bytes_per_iter / 2;
1208 for (i = 0; i < n_iterations; i++)
1210 svm_fifo_enqueue_with_offset (f, half, half, &test_data[half]);
1211 svm_fifo_enqueue (f, half, test_data);
1212 rv = svm_fifo_n_ooo_segments (f);
1214 SFIFO_TEST (0, "number of ooo segments %u", rv);
1215 svm_fifo_dequeue (f, n_bytes_per_iter, data_buf);
1216 if (compare_data (data_buf, test_data, 0, n_bytes_per_iter,
1218 SFIFO_TEST (0, "[%d][%d] dequeued %u expected %u", i, j, data_buf[j],
1221 SFIFO_TEST (1, "passed large transfer");
1223 ft_fifo_free (fs, f);
1224 ft_fifo_segment_free (fsm, fs);
1230 validate_test_and_buf_vecs (u8 ** test_data, u8 ** data_buf, u32 len)
1234 cur_len = vec_len (*test_data);
1235 vec_validate (*test_data, len - 1);
1236 vec_validate (*data_buf, len - 1);
1238 for (i = cur_len; i < vec_len (*test_data); i++)
1239 (*test_data)[i] = i;
1243 enqueue_ooo (svm_fifo_t * f, u8 * test_data, u32 len, u32 iterations)
1245 u32 offset, enq_now, ooo_chunk;
1248 ooo_chunk = len / iterations;
1249 for (i = iterations; i > 0; i--)
1251 offset = i * ooo_chunk;
1252 enq_now = clib_min (ooo_chunk, len - offset);
1255 rv = svm_fifo_enqueue_with_offset (f, offset, enq_now,
1256 test_data + offset);
1265 enqueue_ooo_packets (svm_fifo_t * f, u32 len, u32 enq_chunk, u8 * test_data)
1267 u32 offset, enq_now;
1270 for (i = 1; i <= len / enq_chunk; i++)
1272 offset = i * enq_chunk;
1273 enq_now = clib_min (enq_chunk, len - offset);
1276 rv = svm_fifo_enqueue_with_offset (f, offset, enq_now,
1277 test_data + offset);
1281 if (svm_fifo_size (f) < len - 4096)
1282 svm_fifo_set_size (f, svm_fifo_size (f) + enq_now);
1284 svm_fifo_set_size (f, len);
1291 enqueue_packets_inc (svm_fifo_t * f, u32 len, u32 enq_chunk, u8 * test_data)
1293 u32 enq_now, offset;
1296 for (i = 0; i <= len / enq_chunk; i++)
1298 offset = i * enq_chunk;
1299 enq_now = clib_min (enq_chunk, len - offset);
1300 rv = svm_fifo_enqueue (f, enq_now, test_data + offset);
1303 if (svm_fifo_size (f) < len - 4096)
1304 svm_fifo_set_size (f, svm_fifo_size (f) + enq_now);
1306 svm_fifo_set_size (f, len);
1312 dequeue_ooo (svm_fifo_t * f, u8 * data_buf, u32 len, u32 iterations)
1314 u32 offset, ooo_chunk, deq_now;
1317 ooo_chunk = len / iterations;
1318 for (i = iterations; i >= 0; i--)
1320 offset = i * ooo_chunk;
1321 deq_now = clib_min (ooo_chunk, len - offset);
1324 rv = svm_fifo_peek (f, offset, deq_now, data_buf + offset);
1332 dequeue_ooo_inc (svm_fifo_t * f, u8 * data_buf, u32 len, u32 iterations)
1334 u32 offset, ooo_chunk, deq_now;
1337 ooo_chunk = len / iterations;
1338 for (i = 0; i <= iterations; i++)
1340 offset = i * ooo_chunk;
1341 deq_now = clib_min (ooo_chunk, len - offset);
1344 rv = svm_fifo_peek (f, offset, deq_now, data_buf + offset);
1352 dequeue_packets (svm_fifo_t * f, u32 len, u32 deq_chunk, u8 * data_buf)
1354 u32 offset, deq_now;
1357 for (i = 0; i <= len / deq_chunk; i++)
1359 offset = i * deq_chunk;
1360 deq_now = clib_min (deq_chunk, len - offset);
1363 rv = svm_fifo_dequeue (f, deq_now, data_buf + offset);
1371 sfifo_test_fifo_grow (vlib_main_t * vm, unformat_input_t * input)
1373 int __clib_unused verbose = 0, fifo_size = 4096, fifo_inc = 4096, rv, i;
1374 u32 enq_chunk, offset, deq_now, last_start_byte;
1375 fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
1376 u8 *test_data = 0, *data_buf = 0;
1377 svm_fifo_chunk_t *c;
1381 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1383 if (unformat (input, "verbose"))
1387 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
1393 fs = fifo_segment_prepare (fsm, "fifo-grow", 0);
1394 f = fifo_prepare (fs, fifo_size);
1397 * Grow size and alloc chunks by enqueueing in order
1399 fifo_size += fifo_inc;
1400 svm_fifo_set_size (f, fifo_size);
1401 last_start_byte = 4096;
1402 validate_test_and_buf_vecs (&test_data, &data_buf, fifo_size);
1404 rv = svm_fifo_enqueue (f, fifo_size, test_data);
1406 SFIFO_TEST (rv == fifo_size, "enqueue should work");
1407 SFIFO_TEST (svm_fifo_size (f) == fifo_size, "size expected %u is %u",
1408 fifo_size, svm_fifo_size (f));
1409 SFIFO_TEST (svm_fifo_max_dequeue (f) == fifo_size, "max deq should be %u",
1411 rv = svm_fifo_n_chunks (f);
1412 SFIFO_TEST (rv == 2, "should have 2 chunks has %u", rv);
1413 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1416 SFIFO_TEST (c->start_byte == 0, "head start byte should be %u", 0);
1417 SFIFO_TEST (c->length == 4096, "head chunk length should be %u", 4096);
1418 SFIFO_TEST (f->tail_chunk == 0, "no tail chunk");
1419 SFIFO_TEST (f->ooo_enq == 0, "should have no ooo enq chunk");
1420 SFIFO_TEST (f->ooo_deq == 0, "should have no ooo deq chunk");
1422 SFIFO_TEST (c->start_byte == last_start_byte, "end chunk start byte should"
1423 " be %u", last_start_byte);
1424 SFIFO_TEST (c->length == 4096, "end chunk length should be %u", 4096);
1427 * Dequeue and validate data
1430 rv = svm_fifo_dequeue (f, fifo_size, data_buf);
1431 SFIFO_TEST (rv == fifo_size, "should dequeue all data");
1432 last_start_byte += 4096; /* size of last segment */
1434 rv = compare_data (data_buf, test_data, 0, vec_len (test_data),
1438 vlib_cli_output (vm, "[%d] dequeued %u expected %u", i, data_buf[i],
1440 SFIFO_TEST ((rv == 0), "dequeued compared to original returned %d", rv);
1441 SFIFO_TEST (f->head_chunk == 0, "head chunk should be 0");
1442 SFIFO_TEST (f->tail_chunk == 0, "tail chunk should be 0");
1443 SFIFO_TEST (f->ooo_deq == 0, "should have no ooo deq chunk");
1444 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1447 * Allocate one new chunk by enqueueing out of order all but first chunk
1451 enq_chunk = vec_len (test_data) / 10;
1452 rv = enqueue_ooo (f, test_data, vec_len (test_data), 10);
1453 SFIFO_TEST (!rv, "enqueue ooo should work");
1455 SFIFO_TEST (svm_fifo_size (f) == fifo_size, "size expected %u is %u",
1456 fifo_size, svm_fifo_size (f));
1457 SFIFO_TEST (svm_fifo_max_dequeue (f) == 0, "max deq should be %u", 0);
1458 /* Fifo has 2 chunks because the we didn't allow the first chunk to be
1459 * freed when all the data was dequeued. Could be optimized in the future */
1460 rv = svm_fifo_n_chunks (f);
1461 SFIFO_TEST (rv == 2, "should have %u chunks has %u", 2, rv);
1462 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1464 SFIFO_TEST (f->head_chunk == 0, "should have no head chunk");
1465 /* When new fifo chunks are allocated, tail is initialized */
1466 SFIFO_TEST (f->tail_chunk != 0, "should have no tail chunk");
1467 SFIFO_TEST (f->ooo_enq != 0, "should have an ooo enq chunk");
1470 SFIFO_TEST (c->start_byte == last_start_byte,
1471 "end chunk should start at %u", last_start_byte);
1472 SFIFO_TEST (c->length == 8192, "end chunk length should be %u", 8192);
1473 SFIFO_TEST (f->ooo_enq == c, "ooo enq chunk should be end chunk");
1476 * Enqueue the first chunk
1478 rv = svm_fifo_enqueue (f, enq_chunk, test_data);
1479 SFIFO_TEST (rv == fifo_size, "enq should succeed %u", rv);
1480 rv = svm_fifo_max_dequeue (f);
1481 SFIFO_TEST (rv == fifo_size, "max deq should be %u is %u", fifo_size, rv);
1482 rv = svm_fifo_n_chunks (f);
1483 SFIFO_TEST (rv == 2, "should have %u chunks has %u", 2, rv);
1484 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1486 SFIFO_TEST (f->head_chunk == 0, "should have no head chunk");
1487 /* Fifo is full so tail and ooo_enq should be 0 */
1488 SFIFO_TEST (f->tail_chunk == 0, "should have no tail chunk");
1489 SFIFO_TEST (f->ooo_enq == 0, "should have no ooo enq chunk");
1492 * Peek and validate data
1495 memset (data_buf, 0, vec_len (data_buf));
1497 rv = dequeue_ooo_inc (f, data_buf, fifo_size, 10);
1498 SFIFO_TEST (!rv, "ooo deq should work %d", rv);
1500 rv = compare_data (data_buf, test_data, 0, vec_len (test_data),
1503 vlib_cli_output (vm, "[%d] dequeued %u expected %u", i, data_buf[i],
1505 SFIFO_TEST ((rv == 0), "peeked compared to original returned %d", rv);
1506 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1507 /* Peeked all the data in a full fifo so ooo_deq ends up 0 */
1508 SFIFO_TEST (f->ooo_deq == 0, "should have no ooo deq chunk");
1511 * Peek in reverse order and validate data
1513 * RB tree should be exercised
1516 memset (data_buf, 0, vec_len (data_buf));
1517 for (i = 10; i >= 0; i--)
1519 offset = i * enq_chunk;
1520 deq_now = clib_min (enq_chunk, vec_len (test_data) - offset);
1521 rv = svm_fifo_peek (f, offset, deq_now, data_buf + offset);
1523 SFIFO_TEST (0, "failed to peek");
1526 rv = compare_data (data_buf, test_data, 0, vec_len (test_data),
1530 vlib_cli_output (vm, "[%d] dequeued %u expected %u", i, data_buf[i],
1532 SFIFO_TEST ((rv == 0), "peeked compared to original returned %d", rv);
1533 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1534 /* Last chunk peeked is the first, so ooo_deq should be non zero */
1535 SFIFO_TEST (f->ooo_deq != 0, "should have ooo deq chunk");
1538 * Dequeue drop all bytes
1540 rv = svm_fifo_dequeue_drop (f, fifo_size);
1541 SFIFO_TEST ((rv == fifo_size), "all bytes should be dropped %u", rv);
1542 last_start_byte += 8192;
1544 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1545 SFIFO_TEST (f->head_chunk == 0, "should have no head chunk");
1546 SFIFO_TEST (f->tail_chunk == 0, "should have no tail chunk");
1548 /* We don't remove the last chunk even when the fifo goes empty */
1549 rv = svm_fifo_n_chunks (f);
1550 SFIFO_TEST (rv == 1, "should have %u chunks has %u", 1, rv);
1553 * Increase size such that it can't be the sum of multiple chunk lengths
1555 * A chunk of 16kB should be allocated
1557 fifo_size += 2 * fifo_inc - 100;
1558 svm_fifo_set_size (f, fifo_size);
1559 validate_test_and_buf_vecs (&test_data, &data_buf, fifo_size + fifo_inc);
1560 enq_chunk = vec_len (test_data) / 10;
1561 memset (data_buf, 0, vec_len (data_buf));
1566 rv = enqueue_ooo (f, test_data, fifo_size, 10);
1567 SFIFO_TEST (!rv, "enqueue ooo should work");
1569 SFIFO_TEST (svm_fifo_size (f) == fifo_size, "size expected %u is %u",
1570 fifo_size, svm_fifo_size (f));
1571 SFIFO_TEST (svm_fifo_max_dequeue (f) == 0, "max deq should be %u", 0);
1572 /* Fifo has 2 chunks because the we didn't allow the first chunk to be
1573 * freed when all the data was dequeued. Could be optimized in the future */
1574 rv = svm_fifo_n_chunks (f);
1575 SFIFO_TEST (rv == 2, "should have %u chunks has %u", 2, rv);
1576 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1578 SFIFO_TEST (f->head_chunk == 0, "should have no head chunk");
1579 /* When new fifo chunks are allocated, tail is initialized */
1580 SFIFO_TEST (f->tail_chunk != 0, "should have no tail chunk");
1581 SFIFO_TEST (f->ooo_enq != 0, "should have an ooo enq chunk");
1584 SFIFO_TEST (c->start_byte == last_start_byte,
1585 "end chunk should start at %u", last_start_byte);
1586 SFIFO_TEST (c->length == 16384, "end chunk length should be %u", 16384);
1587 SFIFO_TEST (f->ooo_enq == c, "ooo enq chunk should be end chunk");
1590 * Enqueue the first chunk
1592 rv = svm_fifo_enqueue (f, enq_chunk, test_data);
1593 SFIFO_TEST (rv == fifo_size, "enq should succeed %u", rv);
1594 rv = svm_fifo_max_dequeue (f);
1595 SFIFO_TEST (rv == fifo_size, "max deq should be %u is %u", fifo_size, rv);
1596 rv = svm_fifo_n_chunks (f);
1597 SFIFO_TEST (rv == 2, "should have %u chunks has %u", 2, rv);
1598 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1601 * Dequeue just a part of data. Because we're tracking ooo data, we can't
1602 * call dequeue. Therefore, first peek and then dequeue drop
1604 rv = svm_fifo_peek (f, 0, fifo_inc, data_buf);
1605 SFIFO_TEST (rv == fifo_inc, "should dequeue all data");
1606 rv = svm_fifo_dequeue_drop (f, fifo_inc);
1607 SFIFO_TEST (rv == fifo_inc, "should dequeue all data");
1608 rv = svm_fifo_n_chunks (f);
1609 SFIFO_TEST (rv == 1, "should have %u chunks has %u", 1, rv);
1610 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1613 * Enqueue ooo as much data as it was dequeued
1615 rv = enqueue_ooo (f, test_data + fifo_size, fifo_inc, 2);
1616 SFIFO_TEST (!rv, "ooo enqueue should work %d", rv);
1618 rv = svm_fifo_enqueue (f, fifo_inc / 2, test_data + fifo_size);
1619 SFIFO_TEST (rv == fifo_inc, "enqueue should work %d", rv);
1621 rv = svm_fifo_n_chunks (f);
1622 SFIFO_TEST (rv == 2, "should have %u chunks has %u", 2, rv);
1623 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1625 last_start_byte += 16384;
1627 SFIFO_TEST (c->start_byte == last_start_byte,
1628 "end chunk should start at %u", last_start_byte);
1629 SFIFO_TEST (c->length == 4096, "end chunk length should be %u", 4096);
1632 * Dequeue all. Don't call dequeue see above
1634 rv = svm_fifo_peek (f, 0, fifo_size, data_buf + fifo_inc);
1635 SFIFO_TEST (rv == fifo_size, "should dequeue all data");
1636 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1638 rv = compare_data (data_buf, test_data, 0, vec_len (test_data),
1641 vlib_cli_output (vm, "[%d] dequeued %u expected %u", i, data_buf[i],
1643 SFIFO_TEST ((rv == 0), "dequeued compared to original returned %d", rv);
1645 rv = svm_fifo_dequeue_drop (f, fifo_size);
1646 SFIFO_TEST (rv == fifo_size, "should dequeue all data");
1648 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1649 /* fifo does not end on chunk boundary because of the - 100 */
1650 SFIFO_TEST (f->head_chunk != 0, "should have head chunk");
1651 SFIFO_TEST (f->tail_chunk != 0, "should have tail chunk");
1654 * Enqueue and dequeue byte-by-byte ooo
1657 memset (data_buf, 0, vec_len (data_buf));
1659 rv = enqueue_ooo (f, test_data, fifo_size, fifo_size);
1660 SFIFO_TEST (!rv, "ooo enqueue should work %d", rv);
1662 rv = svm_fifo_enqueue (f, 1, test_data);
1663 SFIFO_TEST (rv == fifo_size, "enqueue should work %d", rv);
1665 rv = dequeue_ooo (f, data_buf, fifo_size, fifo_size);
1666 SFIFO_TEST (!rv, "ooo deq should work %d", rv);
1668 rv = compare_data (data_buf, test_data, 0, fifo_size, (u32 *) & i);
1670 vlib_cli_output (vm, "[%d] dequeued %u expected %u", i, data_buf[i],
1672 SFIFO_TEST ((rv == 0), "dequeued compared to original returned %d", rv);
1673 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1675 last_start_byte += 4096;
1677 SFIFO_TEST (c->start_byte == last_start_byte,
1678 "end chunk should start at %u", last_start_byte);
1679 SFIFO_TEST (c->length == 16384, "end chunk length should be %u", 16384);
1681 rv = svm_fifo_n_chunks (f);
1682 SFIFO_TEST (rv == 2, "should have %u chunks has %u", 2, rv);
1685 * Dequeue drop all bytes
1687 rv = svm_fifo_dequeue_drop (f, fifo_size);
1688 SFIFO_TEST ((rv == fifo_size), "all bytes should be dropped %u", rv);
1690 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1691 SFIFO_TEST (f->head_chunk != 0, "should have head chunk");
1692 SFIFO_TEST (f->tail_chunk != 0, "should have tail chunk");
1694 /* We don't remove the last chunk even when the fifo goes empty */
1695 rv = svm_fifo_n_chunks (f);
1696 SFIFO_TEST (rv == 1, "should have %u chunks has %u", 1, rv);
1697 SFIFO_TEST (f->ooo_enq == 0, "should have no ooo enq chunk");
1698 SFIFO_TEST (f->ooo_deq == 0, "should have no ooo deq chunk");
1701 * Grow fifo to 4MB and force only 4kB chunk allocations
1703 fifo_size = 4 << 20;
1704 svm_fifo_set_size (f, fifo_inc);
1705 validate_test_and_buf_vecs (&test_data, &data_buf, fifo_size);
1707 memset (data_buf, 0, vec_len (data_buf));
1709 rv = enqueue_packets_inc (f, fifo_size, enq_chunk, test_data);
1710 SFIFO_TEST (!rv, "incremental packet enqueue should work");
1712 SFIFO_TEST (svm_fifo_max_dequeue (f) == fifo_size, "max deq should be %u",
1714 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1716 rv = svm_fifo_n_chunks (f);
1717 SFIFO_TEST (rv == (fifo_size / 4096) + 1, "should have %u chunks has %u",
1718 (fifo_size / 4096) + 1, rv);
1725 /* Because we're tracking ooo data, we can't call dequeue. Therefore,
1726 * first peek and then dequeue drop */
1727 rv = svm_fifo_peek (f, 0, fifo_size, data_buf);
1728 SFIFO_TEST (rv == fifo_size, "should dequeue all data");
1730 rv = compare_data (data_buf, test_data, 0, vec_len (test_data),
1733 vlib_cli_output (vm, "[%d] dequeued %u expected %u", i, data_buf[i],
1735 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1736 SFIFO_TEST ((rv == 0), "dequeued compared to original returned %d", rv);
1739 rv = svm_fifo_dequeue_drop (f, fifo_size);
1740 SFIFO_TEST ((rv == fifo_size), "all bytes should be dropped %u", rv);
1741 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1742 SFIFO_TEST (f->ooo_deq == 0, "should have no ooo deq chunk");
1743 rv = svm_fifo_n_chunks (f);
1744 SFIFO_TEST (rv == 1, "should have %u chunks has %u", 1, rv);
1750 ft_fifo_free (fs, f);
1751 ft_fifo_segment_free (fsm, fs);
1752 vec_free (test_data);
1753 vec_free (data_buf);
1758 sfifo_test_fifo_shrink (vlib_main_t * vm, unformat_input_t * input)
1760 int __clib_unused verbose = 0, fifo_size = 4096, deq_chunk;
1761 fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
1762 u8 *test_data = 0, *data_buf = 0;
1768 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1770 if (unformat (input, "verbose"))
1774 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
1781 * Init fifo and enqueue data such that multiple 4096 chunks are allocated
1783 fs = fifo_segment_prepare (fsm, "fifo-shrink", 0);
1784 f = fifo_prepare (fs, fifo_size);
1786 fifo_size = 4 << 20;
1787 svm_fifo_set_size (f, 4096);
1788 validate_test_and_buf_vecs (&test_data, &data_buf, fifo_size);
1790 rv = enqueue_packets_inc (f, fifo_size, enq_chunk, test_data);
1791 SFIFO_TEST (!rv, "incremental packet enqueue should work");
1793 rv = svm_fifo_max_enqueue (f);
1794 SFIFO_TEST (rv == 0, "enqueue space %u", rv);
1795 SFIFO_TEST (svm_fifo_max_dequeue (f) == fifo_size, "max deq should be %u",
1797 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1799 rv = svm_fifo_n_chunks (f);
1800 SFIFO_TEST (rv == (fifo_size / 4096), "should have %u chunks has %u",
1801 (fifo_size / 4096), rv);
1804 * Dequeue enough to collect one chunk
1807 rv = svm_fifo_dequeue (f, deq_chunk, data_buf);
1808 SFIFO_TEST (rv == deq_chunk, "should dequeue all data");
1810 rv = svm_fifo_n_chunks (f);
1811 SFIFO_TEST (rv == (fifo_size / 4096) - 1, "should have %u chunks has %u",
1812 (fifo_size / 4096) - 1, rv);
1813 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1815 rv = svm_fifo_max_enqueue (f);
1816 SFIFO_TEST (rv == deq_chunk, "enqueue space %u", rv);
1819 * Dequeue ooo byte-by-byte remaining data
1821 rv = dequeue_ooo (f, data_buf + deq_chunk, fifo_size - deq_chunk,
1822 fifo_size - deq_chunk);
1823 SFIFO_TEST (!rv, "ooo deq should work %d", rv);
1825 rv = compare_data (data_buf, test_data, 0, fifo_size, (u32 *) & i);
1827 vlib_cli_output (vm, "[%d] dequeued %u expected %u", i, data_buf[i],
1830 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1831 rv = svm_fifo_n_chunks (f);
1832 SFIFO_TEST (rv == (fifo_size / 4096) - 1, "should have %u chunks has %u",
1833 (fifo_size / 4096) - 1, rv);
1838 rv = svm_fifo_dequeue_drop (f, fifo_size - deq_chunk);
1839 SFIFO_TEST (rv == fifo_size - deq_chunk, "should drop all data");
1840 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1841 rv = svm_fifo_n_chunks (f);
1842 SFIFO_TEST (rv == 1, "should have %u chunks has %u", 1, rv);
1843 rv = svm_fifo_max_enqueue (f);
1844 SFIFO_TEST (rv == fifo_size, "enqueue space %u", rv);
1848 * Reset size and enqueue ooo all data
1850 svm_fifo_set_size (f, 4096);
1851 enq_chunk = deq_chunk = 1500;
1852 rv = enqueue_ooo_packets (f, vec_len (test_data), 1500, test_data);
1853 SFIFO_TEST (!rv, "enqueue ooo should work");
1854 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1856 /* 1 additional chunk left from previous test */
1857 rv = svm_fifo_n_chunks (f);
1858 SFIFO_TEST (rv == (fifo_size / 4096) + 1, "should have %u chunks has %u",
1859 (fifo_size / 4096) + 1, rv);
1862 * Add missing first chunk
1864 rv = svm_fifo_enqueue (f, enq_chunk, test_data);
1865 SFIFO_TEST (rv == fifo_size, "enq should succeed %u", rv);
1866 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1867 rv = svm_fifo_max_dequeue (f);
1868 SFIFO_TEST (rv == fifo_size, "max deq should be %u is %u", fifo_size, rv);
1869 rv = svm_fifo_n_chunks (f);
1870 SFIFO_TEST (rv == (fifo_size / 4096) + 1, "should have %u chunks has %u",
1871 (fifo_size / 4096) + 1, rv);
1874 * Dequeue as packets
1876 memset (data_buf, 0, vec_len (data_buf));
1877 rv = dequeue_packets (f, fifo_size, deq_chunk, data_buf);
1878 SFIFO_TEST (!rv, "deq pkts should work %d", rv);
1880 rv = compare_data (data_buf, test_data, 0, fifo_size, (u32 *) & i);
1882 vlib_cli_output (vm, "[%d] dequeued %u expected %u", i, data_buf[i],
1886 * Enqueue and dequeue set of packets
1888 svm_fifo_set_size (f, 4096);
1889 for (i = 0; i < 1000; i++)
1891 rv = svm_fifo_enqueue (f, enq_chunk, test_data);
1892 if (rv != enq_chunk)
1893 SFIFO_TEST (0, "enq fail");
1894 rv = svm_fifo_dequeue (f, deq_chunk, data_buf);
1895 if (rv != deq_chunk)
1896 SFIFO_TEST (0, "deq fail");
1903 ft_fifo_free (fs, f);
1904 ft_fifo_segment_free (fsm, fs);
1905 vec_free (test_data);
1906 vec_free (data_buf);
1912 sfifo_test_fifo_indirect (vlib_main_t * vm, unformat_input_t * input)
1914 int __clib_unused verbose = 0, fifo_size = 4096, deq_chunk;
1915 fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
1916 u8 *test_data = 0, *data_buf = 0;
1917 svm_fifo_chunk_t *c;
1922 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1924 if (unformat (input, "verbose"))
1928 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
1935 * Init fifo and enqueue data such that multiple 4096 chunks are allocated
1937 fs = fifo_segment_prepare (fsm, "fifo-indirect", 0);
1938 f = fifo_prepare (fs, fifo_size);
1940 fifo_size = 4 << 20;
1941 svm_fifo_set_size (f, fifo_size);
1942 validate_test_and_buf_vecs (&test_data, &data_buf, fifo_size);
1945 SFIFO_TEST (c->next == 0, "no next");
1947 svm_fifo_fill_chunk_list (f);
1948 SFIFO_TEST (c->next != 0, "new chunk should've been allocated");
1949 SFIFO_TEST (c->next->length == 4 << 20, "new chunk should be 4MB");
1951 rv = svm_fifo_max_write_chunk (f);
1952 SFIFO_TEST (rv == 4096, "max write chunk %u", rv);
1955 * Enqueue enough to fill first chunk
1957 svm_fifo_enqueue_nocopy (f, 4096);
1958 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1960 c = svm_fifo_tail_chunk (f);
1961 SFIFO_TEST (c == f->end_chunk, "tail is end chunk");
1963 /* Initialize head chunk */
1964 rv = svm_fifo_max_read_chunk (f);
1965 SFIFO_TEST (rv == 4096, "max read chunk %u", rv);
1966 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
1969 * Move head over last segment
1971 rv = svm_fifo_dequeue (f, 4096, data_buf);
1972 SFIFO_TEST (rv == 4096, "dequeue should work");
1974 c = svm_fifo_head_chunk (f);
1975 SFIFO_TEST (c == f->end_chunk, "head chunk should be last");
1977 rv = svm_fifo_max_read_chunk (f);
1978 SFIFO_TEST (rv == 0, "max read chunk %u", rv);
1980 rv = svm_fifo_max_write_chunk (f);
1981 SFIFO_TEST (rv == 4 << 20, "max write chunk %u", rv);
1987 ft_fifo_free (fs, f);
1988 ft_fifo_segment_free (fsm, fs);
1989 vec_free (test_data);
1990 vec_free (data_buf);
1996 svm_fifo_trace_elem_t fifo_trace[] = {};
2000 sfifo_test_fifo_replay (vlib_main_t * vm, unformat_input_t * input)
2004 u8 no_read = 0, *str = 0;
2006 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2008 if (unformat (input, "verbose"))
2010 else if (unformat (input, "no-read"))
2014 clib_error_t *e = clib_error_return
2015 (0, "unknown input `%U'", format_unformat_error, input);
2016 clib_error_report (e);
2022 f.trace = fifo_trace;
2025 str = svm_fifo_replay (str, &f, no_read, verbose);
2026 vlib_cli_output (vm, "%v", str);
2031 sfifo_test_fifo_make_rcv_wnd_zero (vlib_main_t * vm, unformat_input_t * input)
2033 int __clib_unused verbose = 0, fifo_size = 4096, deq_chunk;
2034 fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
2035 u8 *test_data = 0, *data_buf = 0;
2040 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2042 if (unformat (input, "verbose"))
2046 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
2053 * Init fifo and enqueue data such that multiple 4096 chunks are allocated
2055 fs = fifo_segment_prepare (fsm, "fifo-rcv-wnd-zero", 0);
2056 f = fifo_prepare (fs, fifo_size);
2058 /* Enqueue 3000 into 4KB chunk, so there'll be 1096 free space */
2059 svm_fifo_set_size (f, 4096);
2060 validate_test_and_buf_vecs (&test_data, &data_buf, fifo_size);
2061 rv = svm_fifo_enqueue (f, 3000, test_data);
2062 SFIFO_TEST (rv == 3000, "enqueued %u", rv);
2063 rv = svm_fifo_max_enqueue (f);
2064 SFIFO_TEST (rv == 1096, "svm_fifo_max_enqueue %u", rv);
2065 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
2067 /* Shrink fifo size to the in-use size */
2068 svm_fifo_set_size (f, 3000);
2069 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
2071 /* In TCP, this should result in rcv-wnd = 0 */
2072 rv = svm_fifo_max_enqueue (f);
2073 SFIFO_TEST (rv == 0, "svm_fifo_max_enqueue %u", rv);
2074 rv = svm_fifo_max_enqueue_prod (f);
2075 SFIFO_TEST (rv == 0, "svm_fifo_max_enqueue_prod %u", rv);
2077 /* Dequeue and ... */
2078 rv = svm_fifo_dequeue (f, 3000, data_buf);
2079 SFIFO_TEST (rv == 3000, "dequeued %u", rv);
2082 ft_fifo_free (fs, f);
2083 ft_fifo_segment_free (fsm, fs);
2084 vec_free (test_data);
2085 vec_free (data_buf);
2091 static fifo_segment_main_t segment_main;
2094 sfifo_test_fifo_segment_hello_world (int verbose)
2096 fifo_segment_create_args_t _a, *a = &_a;
2097 fifo_segment_main_t *sm = &segment_main;
2098 u8 *test_data, *retrieved_data = 0;
2103 clib_memset (a, 0, sizeof (*a));
2104 a->segment_name = "fifo-test1";
2105 a->segment_size = 256 << 10;
2107 rv = fifo_segment_create (sm, a);
2108 SFIFO_TEST (!rv, "svm_fifo_segment_create returned %d", rv);
2110 fs = fifo_segment_get_segment (sm, a->new_segment_indices[0]);
2111 f = fifo_segment_alloc_fifo (fs, 4096, FIFO_SEGMENT_RX_FIFO);
2113 SFIFO_TEST (f != 0, "svm_fifo_segment_alloc_fifo");
2115 test_data = format (0, "Hello world%c", 0);
2116 vec_validate (retrieved_data, vec_len (test_data) - 1);
2118 while (svm_fifo_max_enqueue (f) >= vec_len (test_data))
2119 svm_fifo_enqueue (f, vec_len (test_data), test_data);
2121 while (svm_fifo_max_dequeue (f) >= vec_len (test_data))
2122 svm_fifo_dequeue (f, vec_len (retrieved_data), retrieved_data);
2124 while (svm_fifo_max_enqueue (f) >= vec_len (test_data))
2125 svm_fifo_enqueue (f, vec_len (test_data), test_data);
2127 while (svm_fifo_max_dequeue (f) >= vec_len (test_data))
2128 svm_fifo_dequeue (f, vec_len (retrieved_data), retrieved_data);
2130 SFIFO_TEST (!memcmp (retrieved_data, test_data, vec_len (test_data)),
2131 "data should be identical");
2133 vec_free (test_data);
2134 vec_free (retrieved_data);
2135 vec_free (a->new_segment_indices);
2136 fifo_segment_free_fifo (fs, f);
2137 fifo_segment_delete (sm, fs);
2142 sfifo_test_fifo_segment_fifo_grow (int verbose)
2144 int rv, fifo_size = 4096, n_chunks, n_batch;
2145 fifo_segment_main_t *sm = &segment_main;
2146 fifo_segment_create_args_t _a, *a = &_a;
2147 u8 *test_data = 0, *data_buf = 0;
2148 u32 n_free_chunk_bytes, new_size;
2152 clib_memset (a, 0, sizeof (*a));
2153 a->segment_name = "fifo-test1";
2154 /* size chosen to be able to force multi chunk allocation lower */
2155 a->segment_size = 256 << 10;
2157 /* fifo allocation allocates chunks in batch */
2158 n_batch = FIFO_SEGMENT_ALLOC_BATCH_SIZE;
2160 rv = fifo_segment_create (sm, a);
2162 SFIFO_TEST (!rv, "svm_fifo_segment_create returned %d", rv);
2167 fs = fifo_segment_get_segment (sm, a->new_segment_indices[0]);
2168 fs->h->pct_first_alloc = 100;
2169 f = fifo_segment_alloc_fifo (fs, fifo_size, FIFO_SEGMENT_RX_FIFO);
2171 SFIFO_TEST (f != 0, "svm_fifo_segment_alloc_fifo");
2173 n_chunks = fifo_segment_num_free_chunks (fs, fifo_size);
2174 SFIFO_TEST (n_chunks == n_batch - 1, "free 2^10B chunks "
2175 "should be %u is %u", n_batch - 1, n_chunks);
2176 rv = fifo_segment_fl_chunk_bytes (fs);
2177 SFIFO_TEST (rv == (n_batch - 1) * fifo_size, "free chunk bytes %u "
2178 "expected %u", rv, (n_batch - 1) * fifo_size);
2181 * Grow fifo by preallocated fifo_size chunk
2183 svm_fifo_set_size (f, 2 * fifo_size);
2184 validate_test_and_buf_vecs (&test_data, &data_buf, 2 * fifo_size);
2186 rv = svm_fifo_enqueue (f, vec_len (test_data), test_data);
2187 SFIFO_TEST (rv == vec_len (test_data), "enq should succeed %u", rv);
2188 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
2190 n_chunks = fifo_segment_num_free_chunks (fs, fifo_size);
2191 SFIFO_TEST (n_chunks == n_batch - 2, "free 2^10B chunks "
2192 "should be %u is %u", n_batch - 2, n_chunks);
2193 rv = fifo_segment_fl_chunk_bytes (fs);
2194 SFIFO_TEST (rv == (n_batch - 2) * fifo_size, "free chunk bytes %u "
2195 "expected %u", rv, (n_batch - 2) * fifo_size);
2197 /* Grow by a size not preallocated but first make sure there's space */
2198 rv = fifo_segment_free_bytes (fs);
2199 SFIFO_TEST (rv > 16 * fifo_size, "free bytes %u more than %u", rv,
2202 /* Force fifo growth */
2203 svm_fifo_set_size (f, svm_fifo_size (f) + 16 * fifo_size);
2204 validate_test_and_buf_vecs (&test_data, &data_buf, svm_fifo_size (f));
2205 rv = svm_fifo_enqueue (f, vec_len (test_data), test_data);
2207 SFIFO_TEST (svm_fifo_size (f) == 18 * fifo_size, "fifo size should be %u "
2208 "is %u", 18 * fifo_size, svm_fifo_size (f));
2210 rv = fifo_segment_fl_chunk_bytes (fs);
2211 SFIFO_TEST (rv == (n_batch - 2) * fifo_size, "free chunk bytes %u "
2212 "expected %u", rv, (n_batch - 2) * fifo_size);
2215 * Free and test free list size
2217 fifo_segment_free_fifo (fs, f);
2219 rv = fifo_segment_fl_chunk_bytes (fs);
2220 SFIFO_TEST (rv == (16 + n_batch) * fifo_size, "free chunk bytes expected %u"
2221 " is %u", (16 + n_batch) * fifo_size, rv);
2222 n_chunks = fifo_segment_num_free_chunks (fs, fifo_size);
2223 SFIFO_TEST (n_chunks == n_batch, "free 2^10B chunks "
2224 "should be %u is %u", n_batch, n_chunks);
2225 n_chunks = fifo_segment_num_free_chunks (fs, 16 * fifo_size);
2226 SFIFO_TEST (n_chunks == 1, "free 2^14B chunks should be %u is %u", 1,
2228 n_chunks = fifo_segment_num_free_chunks (fs, ~0);
2229 SFIFO_TEST (n_chunks == 1 + n_batch, "free chunks should be %u is %u",
2230 1 + n_batch, n_chunks);
2235 f = fifo_segment_alloc_fifo (fs, fifo_size, FIFO_SEGMENT_RX_FIFO);
2237 /* Force chunk allocation */
2238 svm_fifo_set_size (f, svm_fifo_size (f) + fifo_size);
2239 rv = svm_fifo_enqueue (f, svm_fifo_size (f), test_data);
2241 SFIFO_TEST (rv == svm_fifo_size (f), "enq should succeed %u", rv);
2242 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
2244 n_chunks = fifo_segment_num_free_chunks (fs, fifo_size);
2245 SFIFO_TEST (n_chunks == n_batch - 2, "free 2^10B chunks should be %u is %u",
2246 n_batch - 2, n_chunks);
2248 /* Grow and alloc 16 * fifo_size chunk */
2249 svm_fifo_set_size (f, svm_fifo_size (f) + 16 * fifo_size);
2250 rv = svm_fifo_enqueue (f, svm_fifo_size (f), test_data);
2252 n_chunks = fifo_segment_num_free_chunks (fs, 16 * fifo_size);
2253 SFIFO_TEST (n_chunks == 0, "free 2^14B chunks should be %u is %u", 0,
2255 n_chunks = fifo_segment_num_free_chunks (fs, ~0);
2256 SFIFO_TEST (n_chunks == n_batch - 2, "free chunks should be %u is %u",
2257 n_batch - 2, n_chunks);
2262 fifo_segment_free_fifo (fs, f);
2263 n_chunks = fifo_segment_num_free_chunks (fs, ~0);
2264 SFIFO_TEST (n_chunks == 1 + n_batch, "free chunks should be %u is %u",
2265 1 + n_batch, n_chunks);
2267 rv = fifo_segment_fl_chunk_bytes (fs);
2268 SFIFO_TEST (rv == (16 + n_batch) * fifo_size, "free chunk bytes expected %u"
2269 " is %u", (16 + n_batch) * fifo_size, rv);
2271 n_free_chunk_bytes = rv;
2274 * Allocate non power of 2 fifo/chunk and check that free chunk bytes
2275 * is correctly updated
2278 f = fifo_segment_alloc_fifo (fs, 16 * fifo_size - 1, FIFO_SEGMENT_RX_FIFO);
2279 rv = fifo_segment_fl_chunk_bytes (fs);
2281 SFIFO_TEST (n_free_chunk_bytes - 16 * fifo_size == rv, "free chunk bytes "
2282 "expected %u is %u", n_free_chunk_bytes - 16 * fifo_size, rv);
2283 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
2285 fifo_segment_free_fifo (fs, f);
2286 rv = fifo_segment_fl_chunk_bytes (fs);
2288 SFIFO_TEST (n_free_chunk_bytes == rv, "free chunk bytes expected %u is %u",
2289 n_free_chunk_bytes, rv);
2292 * Force multi chunk fifo allocation
2295 /* Check that we can force multi chunk allocation. Note that fifo size
2296 * rounded up to power of 2, i.e., 17 becomes 32 */
2297 rv = fifo_segment_free_bytes (fs);
2298 SFIFO_TEST (rv < 32 * fifo_size, "free bytes %u less than %u", rv,
2301 f = fifo_segment_alloc_fifo (fs, 17 * fifo_size, FIFO_SEGMENT_RX_FIFO);
2302 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
2304 rv = fifo_segment_fl_chunk_bytes (fs);
2306 /* Make sure that the non-power of two chunk freed above is correctly
2307 * accounted for in the chunk free bytes reduction due to chunk allocation
2308 * for the fifo, i.e., it's rounded up by 1 */
2309 SFIFO_TEST (n_free_chunk_bytes - 17 * fifo_size == rv, "free chunk bytes "
2310 "expected %u is %u", n_free_chunk_bytes - 17 * fifo_size, rv);
2312 fifo_segment_free_fifo (fs, f);
2314 rv = fifo_segment_fl_chunk_bytes (fs);
2315 SFIFO_TEST (n_free_chunk_bytes == rv, "free chunk bytes expected %u is %u",
2316 n_free_chunk_bytes, rv);
2319 * Allocate fifo that has all chunks. Because we have a chunk size limit of
2320 * segment_size / 2, allocate 2 fifos.
2322 tf = fifo_segment_alloc_fifo (fs, n_free_chunk_bytes / 2,
2323 FIFO_SEGMENT_RX_FIFO);
2324 SFIFO_TEST (tf != 0, "allocation should work");
2325 SFIFO_TEST (svm_fifo_is_sane (tf), "fifo should be sane");
2327 f = fifo_segment_alloc_fifo (fs, n_free_chunk_bytes / 2,
2328 FIFO_SEGMENT_RX_FIFO);
2329 SFIFO_TEST (f != 0, "allocation should work");
2330 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
2332 fifo_segment_free_fifo (fs, tf);
2333 fifo_segment_free_fifo (fs, f);
2335 rv = fifo_segment_fl_chunk_bytes (fs);
2336 SFIFO_TEST (n_free_chunk_bytes == rv, "free chunk bytes expected %u is %u",
2337 n_free_chunk_bytes, rv);
2340 * Try to allocate more than space available
2343 f = fifo_segment_alloc_fifo (fs, n_free_chunk_bytes + fifo_size,
2344 FIFO_SEGMENT_RX_FIFO);
2345 SFIFO_TEST (f == 0, "allocation should fail");
2348 * Allocate fifo and try to grow beyond available space
2350 f = fifo_segment_alloc_fifo (fs, fifo_segment_free_bytes (fs),
2351 FIFO_SEGMENT_RX_FIFO);
2353 /* Try to force fifo growth */
2354 new_size = svm_fifo_size (f) + n_free_chunk_bytes + 1;
2355 svm_fifo_set_size (f, new_size);
2356 validate_test_and_buf_vecs (&test_data, &data_buf, new_size);
2357 rv = svm_fifo_enqueue (f, new_size, test_data);
2359 SFIFO_TEST (rv != new_size, "grow should fail size %u wrote %d",
2362 fifo_segment_free_fifo (fs, f);
2367 fifo_segment_delete (sm, fs);
2368 vec_free (a->new_segment_indices);
2373 sfifo_test_fifo_segment_slave (int verbose)
2375 fifo_segment_create_args_t _a, *a = &_a;
2376 fifo_segment_main_t *sm = &segment_main;
2377 u8 *test_data, *retrieved_data = 0;
2385 sm->timeout_in_seconds = 5;
2386 clib_memset (a, 0, sizeof (*a));
2387 a->segment_name = "fifo-test1";
2389 rv = fifo_segment_attach (sm, a);
2391 SFIFO_TEST (!rv, "svm_fifo_segment_attach returned %d", rv);
2393 fs = fifo_segment_get_segment (sm, a->new_segment_indices[0]);
2394 vec_free (a->new_segment_indices);
2396 /* might wanna wait.. */
2397 f = fifo_segment_get_slice_fifo_list (fs, 0);
2399 /* Lazy bastards united */
2400 test_data = format (0, "Hello world%c", 0);
2401 vec_validate (retrieved_data, vec_len (test_data) - 1);
2403 for (i = 0; i < 1000; i++)
2405 svm_fifo_dequeue (f, vec_len (retrieved_data), retrieved_data);
2406 if (memcmp (retrieved_data, test_data, vec_len (retrieved_data)))
2408 result = (u32 *) f->head_chunk->data;
2414 result = (u32 *) f->head_chunk->data;
2417 vec_free (test_data);
2418 vec_free (retrieved_data);
2423 sfifo_test_fifo_segment_master_slave (int verbose)
2425 fifo_segment_create_args_t _a, *a = &_a;
2426 fifo_segment_main_t *sm = &segment_main;
2436 SFIFO_TEST (0, "fork failed");
2439 sfifo_test_fifo_segment_slave (verbose);
2441 clib_memset (a, 0, sizeof (*a));
2442 a->segment_name = "fifo-test1";
2443 a->segment_size = 256 << 10;
2445 rv = fifo_segment_create (sm, a);
2447 SFIFO_TEST (!rv, "svm_fifo_segment_create returned %d", rv);
2449 sp = fifo_segment_get_segment (sm, a->new_segment_indices[0]);
2450 f = fifo_segment_alloc_fifo (sp, 4096, FIFO_SEGMENT_RX_FIFO);
2452 SFIFO_TEST (f != 0, "svm_fifo_segment_alloc_fifo alloc");
2454 test_data = format (0, "Hello world%c", 0);
2458 for (i = 0; i < 1000; i++)
2459 svm_fifo_enqueue (f, vec_len (test_data), test_data);
2461 /* Wait for slave */
2463 while (svm_fifo_max_dequeue (f) && i++ < 1e10)
2468 result = (u32 *) f->head_chunk->data;
2469 SFIFO_TEST (*result == 0, "slave reported no error");
2471 vec_free (a->new_segment_indices);
2472 vec_free (test_data);
2473 fifo_segment_free_fifo (sp, f);
2474 fifo_segment_delete (sm, sp);
2479 sfifo_test_fifo_segment_mempig (int verbose)
2481 fifo_segment_create_args_t _a, *a = &_a;
2482 fifo_segment_main_t *sm = &segment_main;
2485 svm_fifo_t **flist = 0;
2489 clib_memset (a, 0, sizeof (*a));
2491 a->segment_name = "fifo-test1";
2492 a->segment_size = 256 << 10;
2494 rv = fifo_segment_create (sm, a);
2496 SFIFO_TEST (!rv, "svm_fifo_segment_create returned %d", rv);
2498 sp = fifo_segment_get_segment (sm, a->new_segment_indices[0]);
2500 for (i = 0; i < 1000; i++)
2502 f = fifo_segment_alloc_fifo (sp, 4096, FIFO_SEGMENT_RX_FIFO);
2505 vec_add1 (flist, f);
2508 SFIFO_TEST (vec_len (flist), "created %d fifos", vec_len (flist));
2510 for (i = 0; i < vec_len (flist); i++)
2513 fifo_segment_free_fifo (sp, f);
2516 _vec_len (flist) = 0;
2518 for (i = 0; i < 1000; i++)
2520 f = fifo_segment_alloc_fifo (sp, 4096, FIFO_SEGMENT_RX_FIFO);
2523 vec_add1 (flist, f);
2526 SFIFO_TEST (vec_len (flist), "second try created %d fifos",
2528 for (i = 0; i < vec_len (flist); i++)
2531 fifo_segment_free_fifo (sp, f);
2534 fifo_segment_delete (sm, sp);
2539 approx_leq (uword a, uword b, u32 margin)
2541 if (a - margin <= b && b <= a)
2547 sfifo_test_fifo_segment_prealloc (int verbose)
2549 fifo_segment_create_args_t _a, *a = &_a;
2550 fifo_segment_main_t *sm = &segment_main;
2551 u32 max_pairs, pairs_req, free_space, pair_mem;
2552 svm_fifo_t *f, *tf, *old;
2556 clib_memset (a, 0, sizeof (*a));
2558 a->segment_name = "fifo-test-prealloc";
2559 a->segment_size = 256 << 10;
2560 a->segment_type = SSVM_SEGMENT_MEMFD;
2562 rv = fifo_segment_create (sm, a);
2563 SFIFO_TEST (!rv, "svm_fifo_segment_create returned %d", rv);
2564 fs = fifo_segment_get_segment (sm, a->new_segment_indices[0]);
2565 fs->h->pct_first_alloc = 100;
2568 * Prealloc chunks and headers
2570 free_space = fifo_segment_free_bytes (fs);
2571 SFIFO_TEST (free_space <= 256 << 10, "free space expected %u is %u",
2572 256 << 10, free_space);
2573 rv = fifo_segment_prealloc_fifo_chunks (fs, 0, 4096, 50);
2574 SFIFO_TEST (rv == 0, "chunk prealloc should work");
2575 rv = fifo_segment_num_free_chunks (fs, 4096);
2576 SFIFO_TEST (rv == 50, "prealloc chunks expected %u is %u", 50, rv);
2577 rv = fifo_segment_free_bytes (fs);
2578 free_space -= (sizeof (svm_fifo_chunk_t) + 4096) * 50;
2579 /* Memory alloc alignment accounts for the difference */
2580 SFIFO_TEST (approx_leq (free_space, rv, 16), "free space expected %u is %u",
2583 rv = fifo_segment_fl_chunk_bytes (fs);
2584 SFIFO_TEST (rv == 4096 * 50, "chunk free space expected %u is %u",
2587 rv = fifo_segment_prealloc_fifo_hdrs (fs, 0, 50);
2588 SFIFO_TEST (rv == 0, "fifo hdr prealloc should work");
2589 rv = fifo_segment_num_free_fifos (fs);
2590 SFIFO_TEST (rv == 50, "prealloc fifo hdrs expected %u is %u", 50, rv);
2591 rv = fifo_segment_free_bytes (fs);
2592 free_space -= sizeof (svm_fifo_t) * 50;
2593 /* Memory alloc alignment accounts for the difference */
2594 SFIFO_TEST (approx_leq (free_space, rv, 16), "free space expected %u is %u",
2598 rv = fifo_segment_free_bytes (fs);
2599 SFIFO_TEST (clib_abs (rv - (int) free_space) < 512,
2600 "free space expected %u is %u", free_space, rv);
2602 /* Use all free chunk memory */
2603 f = fifo_segment_alloc_fifo (fs, 100 << 10, FIFO_SEGMENT_RX_FIFO);
2604 SFIFO_TEST (f != 0, "fifo allocated");
2605 SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
2607 tf = fifo_segment_alloc_fifo (fs, 100 << 10, FIFO_SEGMENT_RX_FIFO);
2608 SFIFO_TEST (tf != 0, "fifo allocated");
2609 SFIFO_TEST (svm_fifo_is_sane (tf), "fifo should be sane");
2611 rv = fifo_segment_num_free_chunks (fs, 4096);
2612 SFIFO_TEST (rv == 0, "prealloc chunks expected %u is %u", 0, rv);
2613 rv = fifo_segment_fl_chunk_bytes (fs);
2614 SFIFO_TEST (rv == 0, "chunk free space expected %u is %u", 0, rv);
2618 * Multiple preallocs that consume the remaining space
2620 free_space = fifo_segment_free_bytes (fs);
2621 pair_mem = 2 * (4096 + sizeof (*f) + sizeof (svm_fifo_chunk_t));
2622 max_pairs = pairs_req = (free_space / pair_mem) - 1;
2623 fifo_segment_preallocate_fifo_pairs (fs, 4096, 4096, &pairs_req);
2624 SFIFO_TEST (pairs_req == 0, "prealloc pairs should work req %u", max_pairs);
2625 rv = fifo_segment_num_free_chunks (fs, 4096);
2626 SFIFO_TEST (rv == max_pairs * 2, "prealloc chunks expected %u is %u",
2629 rv = fifo_segment_free_bytes (fs);
2630 SFIFO_TEST (rv < 2 * pair_mem, "free bytes %u less than %u", rv,
2633 /* Preallocate as many more chunks as possible. Heap is almost full
2634 * so we may not use all the free space*/
2636 while (!fifo_segment_prealloc_fifo_chunks (fs, 0, 4096, 1))
2638 SFIFO_TEST (alloc, "chunk prealloc should work %u", alloc);
2639 rv = fifo_segment_num_free_chunks (fs, 4096);
2640 SFIFO_TEST (rv == max_pairs * 2 + alloc, "prealloc chunks expected %u "
2641 "is %u", max_pairs * 2 + alloc, rv);
2643 rv = fifo_segment_free_bytes (fs);
2644 SFIFO_TEST (rv < pair_mem, "free bytes expected less than %u is %u",
2648 * Test negative prealloc cases
2651 fifo_segment_preallocate_fifo_pairs (fs, 4096, 4096, &pairs_req);
2652 SFIFO_TEST (pairs_req == 1, "prealloc pairs should not work");
2655 f = fifo_segment_alloc_fifo (fs, 200 << 10, FIFO_SEGMENT_RX_FIFO);
2656 SFIFO_TEST (f == 0, "fifo alloc should fail");
2658 rv = fifo_segment_prealloc_fifo_chunks (fs, 0, 4096, 50);
2659 SFIFO_TEST (rv == -1, "chunk prealloc should fail");
2661 rv = fifo_segment_prealloc_fifo_hdrs (fs, 0, 50);
2662 SFIFO_TEST (rv == -1, "fifo hdr prealloc should fail");
2667 fifo_segment_free_fifo (fs, old);
2668 fifo_segment_free_fifo (fs, tf);
2669 close (fs->ssvm.fd);
2670 fifo_segment_delete (sm, fs);
2675 sfifo_test_fifo_segment (vlib_main_t * vm, unformat_input_t * input)
2677 int rv, verbose = 0;
2679 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2681 if (unformat (input, "verbose"))
2683 else if (unformat (input, "masterslave"))
2685 if ((rv = sfifo_test_fifo_segment_master_slave (verbose)))
2688 else if (unformat (input, "basic"))
2690 if ((rv = sfifo_test_fifo_segment_hello_world (verbose)))
2693 else if (unformat (input, "mempig"))
2695 if ((rv = sfifo_test_fifo_segment_mempig (verbose)))
2698 else if (unformat (input, "grow fifo"))
2700 if ((rv = sfifo_test_fifo_segment_fifo_grow (verbose)))
2703 else if (unformat (input, "prealloc"))
2705 if ((rv = sfifo_test_fifo_segment_prealloc (verbose)))
2708 else if (unformat (input, "all"))
2710 if ((rv = sfifo_test_fifo_segment_hello_world (verbose)))
2712 if ((rv = sfifo_test_fifo_segment_mempig (verbose)))
2714 if ((rv = sfifo_test_fifo_segment_fifo_grow (verbose)))
2716 if ((rv = sfifo_test_fifo_segment_prealloc (verbose)))
2718 /* Pretty slow so avoid running it always
2719 if ((rv = sfifo_test_fifo_segment_master_slave (verbose)))
2725 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
2733 static clib_error_t *
2734 svm_fifo_test (vlib_main_t * vm, unformat_input_t * input,
2735 vlib_cli_command_t * cmd_arg)
2740 clib_warning ("high mem %lu", HIGH_SEGMENT_BASEVA);
2741 fifo_segment_main_init (&segment_main, HIGH_SEGMENT_BASEVA, 5);
2742 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2744 if (unformat (input, "fifo1"))
2745 res = sfifo_test_fifo1 (vm, input);
2746 else if (unformat (input, "fifo2"))
2747 res = sfifo_test_fifo2 (vm);
2748 else if (unformat (input, "fifo3"))
2749 res = sfifo_test_fifo3 (vm, input);
2750 else if (unformat (input, "fifo4"))
2751 res = sfifo_test_fifo4 (vm, input);
2752 else if (unformat (input, "fifo5"))
2753 res = sfifo_test_fifo5 (vm, input);
2754 else if (unformat (input, "fifo6"))
2755 res = sfifo_test_fifo6 (vm, input);
2756 else if (unformat (input, "fifo7"))
2757 res = sfifo_test_fifo7 (vm, input);
2758 else if (unformat (input, "large"))
2759 res = sfifo_test_fifo_large (vm, input);
2760 else if (unformat (input, "replay"))
2761 res = sfifo_test_fifo_replay (vm, input);
2762 else if (unformat (input, "grow"))
2763 res = sfifo_test_fifo_grow (vm, input);
2764 else if (unformat (input, "shrink"))
2765 res = sfifo_test_fifo_shrink (vm, input);
2766 else if (unformat (input, "indirect"))
2767 res = sfifo_test_fifo_indirect (vm, input);
2768 else if (unformat (input, "zero"))
2769 res = sfifo_test_fifo_make_rcv_wnd_zero (vm, input);
2770 else if (unformat (input, "segment"))
2771 res = sfifo_test_fifo_segment (vm, input);
2772 else if (unformat (input, "all"))
2774 if ((res = sfifo_test_fifo1 (vm, input)))
2777 if ((res = sfifo_test_fifo2 (vm)))
2781 * Run a number of fifo3 configs
2783 str = "nsegs 10 overlap seed 123";
2784 unformat_init_cstring (input, str);
2785 if ((res = sfifo_test_fifo3 (vm, input)))
2787 unformat_free (input);
2789 str = "nsegs 10 overlap seed 123 in-seq-all";
2790 unformat_init_cstring (input, str);
2791 if ((res = sfifo_test_fifo3 (vm, input)))
2793 unformat_free (input);
2795 str = "nsegs 10 overlap seed 123 initial-offset 3917";
2796 unformat_init_cstring (input, str);
2797 if ((res = sfifo_test_fifo3 (vm, input)))
2799 unformat_free (input);
2801 str = "nsegs 10 overlap seed 123 initial-offset 3917 drop";
2802 unformat_init_cstring (input, str);
2803 if ((res = sfifo_test_fifo3 (vm, input)))
2805 unformat_free (input);
2807 str = "nsegs 10 seed 123 initial-offset 3917 drop no-randomize";
2808 unformat_init_cstring (input, str);
2809 if ((res = sfifo_test_fifo3 (vm, input)))
2811 unformat_free (input);
2813 if ((res = sfifo_test_fifo4 (vm, input)))
2816 if ((res = sfifo_test_fifo5 (vm, input)))
2819 if ((res = sfifo_test_fifo6 (vm, input)))
2822 if ((res = sfifo_test_fifo7 (vm, input)))
2825 if ((res = sfifo_test_fifo_grow (vm, input)))
2828 if ((res = sfifo_test_fifo_shrink (vm, input)))
2831 if ((res = sfifo_test_fifo_indirect (vm, input)))
2834 if ((res = sfifo_test_fifo_make_rcv_wnd_zero (vm, input)))
2838 unformat_init_cstring (input, str);
2839 if ((res = sfifo_test_fifo_segment (vm, input)))
2844 vlib_cli_output (vm, "unknown input `%U'", format_unformat_error,
2854 return clib_error_return (0, "svm fifo unit test failed");
2859 VLIB_CLI_COMMAND (svm_fifo_test_command, static) =
2861 .path = "test svm fifo",
2862 .short_help = "internal svm fifo unit tests",
2863 .function = svm_fifo_test,
2868 * fd.io coding-style-patch-verification: ON
2871 * eval: (c-set-style "gnu")