X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fplugins%2Funittest%2Fsvm_fifo_test.c;h=8b43ee370f1f9709899fa45c8e17155cddcff665;hb=b020806806c0e6c54886cdb4347a5fd1f19504b0;hp=4e44441ebdc63b0d7be580701e112d85c8ace368;hpb=b095a3cd221a142f7d2b4897b812b2781de05d29;p=vpp.git diff --git a/src/plugins/unittest/svm_fifo_test.c b/src/plugins/unittest/svm_fifo_test.c index 4e44441ebdc..8b43ee370f1 100644 --- a/src/plugins/unittest/svm_fifo_test.c +++ b/src/plugins/unittest/svm_fifo_test.c @@ -14,6 +14,7 @@ */ #include #include +#include #include #define SFIFO_TEST_I(_cond, _comment, _args...) \ @@ -174,7 +175,7 @@ compare_data (u8 * data1, u8 * data2, u32 start, u32 len, u32 * index) { int i; - for (i = start; i < len; i++) + for (i = start; i < start + len; i++) { if (data1[i] != data2[i]) { @@ -214,7 +215,7 @@ sfifo_test_fifo1 (vlib_main_t * vm, unformat_input_t * input) /* * Enqueue an initial (un-dequeued) chunk */ - rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) test_data); + rv = svm_fifo_enqueue (f, sizeof (u32), (u8 *) test_data); SFIFO_TEST ((rv == sizeof (u32)), "enqueued %d", rv); SFIFO_TEST ((f->tail == 4), "fifo tail %u", f->tail); @@ -228,7 +229,7 @@ sfifo_test_fifo1 (vlib_main_t * vm, unformat_input_t * input) data = (u8 *) (test_data + (2 * i + 1)); if (i == 0) { - rv = svm_fifo_enqueue_nowait (f, sizeof (u32), data); + rv = svm_fifo_enqueue (f, sizeof (u32), data); rv = rv > 0 ? 0 : rv; } else @@ -247,8 +248,8 @@ sfifo_test_fifo1 (vlib_main_t * vm, unformat_input_t * input) vlib_cli_output (vm, "fifo after odd segs: %U", format_svm_fifo, f, 1); SFIFO_TEST ((f->tail == 8), "fifo tail %u", f->tail); - SFIFO_TEST ((svm_fifo_number_ooo_segments (f) == 2), - "number of ooo segments %u", svm_fifo_number_ooo_segments (f)); + SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 2), + "number of ooo segments %u", svm_fifo_n_ooo_segments (f)); /* * Try adding a completely overlapped segment @@ -265,8 +266,8 @@ sfifo_test_fifo1 (vlib_main_t * vm, unformat_input_t * input) if (verbose) vlib_cli_output (vm, "fifo after overlap seg: %U", format_svm_fifo, f, 1); - SFIFO_TEST ((svm_fifo_number_ooo_segments (f) == 2), - "number of ooo segments %u", svm_fifo_number_ooo_segments (f)); + SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 2), + "number of ooo segments %u", svm_fifo_n_ooo_segments (f)); /* * Make sure format functions are not buggy @@ -295,8 +296,8 @@ sfifo_test_fifo1 (vlib_main_t * vm, unformat_input_t * input) if (verbose) vlib_cli_output (vm, "fifo before missing link: %U", format_svm_fifo, f, 1); - SFIFO_TEST ((svm_fifo_number_ooo_segments (f) == 1), - "number of ooo segments %u", svm_fifo_number_ooo_segments (f)); + SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 1), + "number of ooo segments %u", svm_fifo_n_ooo_segments (f)); ooo_seg = svm_fifo_first_ooo_segment (f); SFIFO_TEST ((ooo_seg->start == 12), "first ooo seg position %u", ooo_seg->start); @@ -306,20 +307,20 @@ sfifo_test_fifo1 (vlib_main_t * vm, unformat_input_t * input) /* * Enqueue the missing u32 */ - rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) (test_data + 2)); + rv = svm_fifo_enqueue (f, sizeof (u32), (u8 *) (test_data + 2)); if (verbose) vlib_cli_output (vm, "fifo after missing link: %U", format_svm_fifo, f, 1); SFIFO_TEST ((rv == 20), "bytes to be enqueued %u", rv); - SFIFO_TEST ((svm_fifo_number_ooo_segments (f) == 0), - "number of ooo segments %u", svm_fifo_number_ooo_segments (f)); + SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 0), + "number of ooo segments %u", svm_fifo_n_ooo_segments (f)); /* * Collect results */ for (i = 0; i < 7; i++) { - rv = svm_fifo_dequeue_nowait (f, sizeof (u32), (u8 *) & data_word); + rv = svm_fifo_dequeue (f, sizeof (u32), (u8 *) & data_word); if (rv != sizeof (u32)) { clib_warning ("bytes dequeues %u", rv); @@ -356,15 +357,22 @@ sfifo_test_fifo1 (vlib_main_t * vm, unformat_input_t * input) rv = svm_fifo_enqueue_with_offset (f, 8 - f->tail, 21, data); SFIFO_TEST ((rv == 0), "ooo enqueued %u", rv); - SFIFO_TEST ((svm_fifo_number_ooo_segments (f) == 1), - "number of ooo segments %u", svm_fifo_number_ooo_segments (f)); + SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 1), + "number of ooo segments %u", svm_fifo_n_ooo_segments (f)); - vec_validate (data_buf, vec_len (data)); - svm_fifo_peek (f, 0, vec_len (data), data_buf); - if (compare_data (data_buf, data, 8, vec_len (data), &j)) - { - SFIFO_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j], data[j]); - } + /* add missing data to be able to dequeue something */ + rv = svm_fifo_enqueue (f, 4, data); + SFIFO_TEST ((rv == 32), "enqueued %u", rv); + SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 0), + "number of ooo segments %u", svm_fifo_n_ooo_segments (f)); + + vec_validate (data_buf, vec_len (test_data)); + svm_fifo_peek (f, 0, 4, data_buf); + if (compare_data (data_buf, data, 0, 4, &j)) + SFIFO_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j], data[j]); + svm_fifo_peek (f, 8, 21, data_buf); + if (compare_data (data_buf, data, 0, 21, &j)) + SFIFO_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j], data[j]); vec_reset_length (data_buf); /* @@ -391,13 +399,13 @@ sfifo_test_fifo1 (vlib_main_t * vm, unformat_input_t * input) if (verbose) vlib_cli_output (vm, "fifo after enqueue: %U", format_svm_fifo, f, 1); - rv = svm_fifo_enqueue_nowait (f, 29, data); + rv = svm_fifo_enqueue (f, 29, data); if (verbose) vlib_cli_output (vm, "fifo after enqueueing 29: %U", format_svm_fifo, f, 1); SFIFO_TEST ((rv == 32), "ooo enqueued %u", rv); - SFIFO_TEST ((svm_fifo_number_ooo_segments (f) == 0), - "number of ooo segments %u", svm_fifo_number_ooo_segments (f)); + SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 0), + "number of ooo segments %u", svm_fifo_n_ooo_segments (f)); vec_validate (data_buf, vec_len (data)); svm_fifo_peek (f, 0, vec_len (data), data_buf); @@ -426,7 +434,7 @@ static int sfifo_test_fifo2 (vlib_main_t * vm) { svm_fifo_t *f; - u32 fifo_size = 1 << 20; + u32 fifo_size = (1 << 20) + 1; int i, rv, test_data_len; u64 data64; test_pattern_t *tp, *vp, *test_data; @@ -452,8 +460,8 @@ sfifo_test_fifo2 (vlib_main_t * vm) } /* Expected result: one big fat chunk at offset 4 */ - SFIFO_TEST ((svm_fifo_number_ooo_segments (f) == 1), - "number of ooo segments %u", svm_fifo_number_ooo_segments (f)); + SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 1), + "number of ooo segments %u", svm_fifo_n_ooo_segments (f)); ooo_seg = svm_fifo_first_ooo_segment (f); SFIFO_TEST ((ooo_seg->start == 4), "first ooo seg position %u", ooo_seg->start); @@ -461,7 +469,7 @@ sfifo_test_fifo2 (vlib_main_t * vm) "first ooo seg length %u", ooo_seg->length); data64 = 0; - rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) & data64); + rv = svm_fifo_enqueue (f, sizeof (u32), (u8 *) & data64); SFIFO_TEST ((rv == 3000), "bytes to be enqueued %u", rv); svm_fifo_free (f); @@ -486,8 +494,8 @@ sfifo_test_fifo2 (vlib_main_t * vm) } /* Expecting the same result: one big fat chunk at offset 4 */ - SFIFO_TEST ((svm_fifo_number_ooo_segments (f) == 1), - "number of ooo segments %u", svm_fifo_number_ooo_segments (f)); + SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 1), + "number of ooo segments %u", svm_fifo_n_ooo_segments (f)); ooo_seg = svm_fifo_first_ooo_segment (f); SFIFO_TEST ((ooo_seg->start == 4), "first ooo seg position %u", ooo_seg->start); @@ -495,7 +503,7 @@ sfifo_test_fifo2 (vlib_main_t * vm) "first ooo seg length %u", ooo_seg->length); data64 = 0; - rv = svm_fifo_enqueue_nowait (f, sizeof (u32), (u8 *) & data64); + rv = svm_fifo_enqueue (f, sizeof (u32), (u8 *) & data64); SFIFO_TEST ((rv == 3000), "bytes to be enqueued %u", rv); @@ -508,7 +516,7 @@ static int sfifo_test_fifo3 (vlib_main_t * vm, unformat_input_t * input) { svm_fifo_t *f; - u32 fifo_size = 4 << 10; + u32 fifo_size = (4 << 10) + 1; u32 fifo_initial_offset = 0; u32 total_size = 2 << 10; int overlap = 0, verbose = 0, randomize = 1, drop = 0, in_seq_all = 0; @@ -658,7 +666,7 @@ sfifo_test_fifo3 (vlib_main_t * vm, unformat_input_t * input) /* Add the first segment in order for non random data */ if (!randomize) - svm_fifo_enqueue_nowait (f, generate[0].len, (u8 *) data_pattern); + svm_fifo_enqueue (f, generate[0].len, (u8 *) data_pattern); /* * Expected result: one big fat chunk at offset 1 if randomize == 1 @@ -676,7 +684,7 @@ sfifo_test_fifo3 (vlib_main_t * vm, unformat_input_t * input) u32 bytes_to_enq = 1; if (in_seq_all) bytes_to_enq = total_size; - rv = svm_fifo_enqueue_nowait (f, bytes_to_enq, data_pattern + 0); + rv = svm_fifo_enqueue (f, bytes_to_enq, data_pattern + 0); if (verbose) vlib_cli_output (vm, "in-order enqueue returned %d", rv); @@ -687,7 +695,7 @@ sfifo_test_fifo3 (vlib_main_t * vm, unformat_input_t * input) } SFIFO_TEST ((svm_fifo_has_ooo_data (f) == 0), "number of ooo segments %u", - svm_fifo_number_ooo_segments (f)); + svm_fifo_n_ooo_segments (f)); /* * Test if peeked data is the same as original data @@ -699,7 +707,6 @@ sfifo_test_fifo3 (vlib_main_t * vm, unformat_input_t * input) SFIFO_TEST (0, "[%d] peeked %u expected %u", j, data_buf[j], data_pattern[j]); } - vec_reset_length (data_buf); /* * Dequeue or drop all data @@ -710,7 +717,8 @@ sfifo_test_fifo3 (vlib_main_t * vm, unformat_input_t * input) } else { - svm_fifo_dequeue_nowait (f, vec_len (data_pattern), data_buf); + memset (data_buf, 0, vec_len (data_pattern)); + svm_fifo_dequeue (f, vec_len (data_pattern), data_buf); if (compare_data (data_buf, data_pattern, 0, vec_len (data_pattern), &j)) { @@ -780,11 +788,11 @@ sfifo_test_fifo4 (vlib_main_t * vm, unformat_input_t * input) } } - svm_fifo_enqueue_nowait (f, sizeof (u8), &test_data[0]); + svm_fifo_enqueue (f, sizeof (u8), &test_data[0]); vec_validate (data_buf, vec_len (test_data)); - svm_fifo_dequeue_nowait (f, vec_len (test_data), data_buf); + svm_fifo_dequeue (f, vec_len (test_data), data_buf); rv = compare_data (data_buf, test_data, 0, vec_len (test_data), &j); if (rv) vlib_cli_output (vm, "[%d] dequeued %u expected %u", j, data_buf[j], @@ -799,7 +807,16 @@ sfifo_test_fifo4 (vlib_main_t * vm, unformat_input_t * input) static u32 fifo_pos (svm_fifo_t * f, u32 pos) { - return pos; + return pos % f->size; +} + +/* Avoids exposing svm_fifo.c internal function */ +static ooo_segment_t * +ooo_seg_next (svm_fifo_t * f, ooo_segment_t * s) +{ + if (pool_is_free_index (f->ooo_segments, s->next)) + return 0; + return pool_elt_at_index (f->ooo_segments, s->next); } static int @@ -837,8 +854,8 @@ sfifo_test_fifo5 (vlib_main_t * vm, unformat_input_t * input) svm_fifo_enqueue_with_offset (f, 100, 100, &test_data[100]); svm_fifo_enqueue_with_offset (f, 300, 100, &test_data[300]); - SFIFO_TEST ((svm_fifo_number_ooo_segments (f) == 2), - "number of ooo segments %u", svm_fifo_number_ooo_segments (f)); + SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 2), + "number of ooo segments %u", svm_fifo_n_ooo_segments (f)); SFIFO_TEST ((f->ooos_newest == 1), "newest %u", f->ooos_newest); if (verbose) vlib_cli_output (vm, "fifo after [100, 200] and [300, 400] : %U", @@ -848,25 +865,25 @@ sfifo_test_fifo5 (vlib_main_t * vm, unformat_input_t * input) * Add [225, 275] */ - rv = svm_fifo_enqueue_with_offset (f, 225, 50, &test_data[200]); + rv = svm_fifo_enqueue_with_offset (f, 225, 50, &test_data[225]); if (verbose) vlib_cli_output (vm, "fifo after [225, 275] : %U", format_svm_fifo, f, 2 /* verbose */ ); - SFIFO_TEST ((svm_fifo_number_ooo_segments (f) == 3), - "number of ooo segments %u", svm_fifo_number_ooo_segments (f)); + SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 3), + "number of ooo segments %u", svm_fifo_n_ooo_segments (f)); ooo_seg = svm_fifo_first_ooo_segment (f); SFIFO_TEST ((ooo_seg->start == fifo_pos (f, 100 + offset)), "first seg start %u expected %u", ooo_seg->start, fifo_pos (f, 100 + offset)); SFIFO_TEST ((ooo_seg->length == 100), "first seg length %u expected %u", ooo_seg->length, 100); - ooo_seg = ooo_segment_next (f, ooo_seg); + ooo_seg = ooo_seg_next (f, ooo_seg); SFIFO_TEST ((ooo_seg->start == fifo_pos (f, 225 + offset)), "second seg start %u expected %u", ooo_seg->start, fifo_pos (f, 225 + offset)); SFIFO_TEST ((ooo_seg->length == 50), "second seg length %u expected %u", ooo_seg->length, 50); - ooo_seg = ooo_segment_next (f, ooo_seg); + ooo_seg = ooo_seg_next (f, ooo_seg); SFIFO_TEST ((ooo_seg->start == fifo_pos (f, 300 + offset)), "third seg start %u expected %u", ooo_seg->start, fifo_pos (f, 300 + offset)); @@ -880,8 +897,8 @@ sfifo_test_fifo5 (vlib_main_t * vm, unformat_input_t * input) if (verbose) vlib_cli_output (vm, "fifo after [190, 310] : %U", format_svm_fifo, f, 1 /* verbose */ ); - SFIFO_TEST ((svm_fifo_number_ooo_segments (f) == 1), - "number of ooo segments %u", svm_fifo_number_ooo_segments (f)); + SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 1), + "number of ooo segments %u", svm_fifo_n_ooo_segments (f)); ooo_seg = svm_fifo_first_ooo_segment (f); SFIFO_TEST ((ooo_seg->start == fifo_pos (f, offset + 100)), "first seg start %u expected %u", @@ -892,15 +909,15 @@ sfifo_test_fifo5 (vlib_main_t * vm, unformat_input_t * input) /* * Add [0, 150] */ - rv = svm_fifo_enqueue_nowait (f, 150, test_data); + rv = svm_fifo_enqueue (f, 150, test_data); if (verbose) vlib_cli_output (vm, "fifo after [0 150] : %U", format_svm_fifo, f, 2 /* verbose */ ); SFIFO_TEST ((rv == 400), "managed to enqueue %u expected %u", rv, 400); - SFIFO_TEST ((svm_fifo_number_ooo_segments (f) == 0), - "number of ooo segments %u", svm_fifo_number_ooo_segments (f)); + SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 0), + "number of ooo segments %u", svm_fifo_n_ooo_segments (f)); vec_validate (data_buf, 399); svm_fifo_peek (f, 0, 400, data_buf); @@ -918,8 +935,8 @@ sfifo_test_fifo5 (vlib_main_t * vm, unformat_input_t * input) svm_fifo_enqueue_with_offset (f, 100, 100, &test_data[100]); svm_fifo_enqueue_with_offset (f, 50, 200, &test_data[50]); - SFIFO_TEST ((svm_fifo_number_ooo_segments (f) == 1), - "number of ooo segments %u", svm_fifo_number_ooo_segments (f)); + SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 1), + "number of ooo segments %u", svm_fifo_n_ooo_segments (f)); ooo_seg = svm_fifo_first_ooo_segment (f); SFIFO_TEST ((ooo_seg->start == 50), "first seg start %u expected %u", ooo_seg->start, 50); @@ -931,6 +948,245 @@ sfifo_test_fifo5 (vlib_main_t * vm, unformat_input_t * input) return 0; } +/* + * Test ooo head/tail u32 wrapping + */ +static int +sfifo_test_fifo6 (vlib_main_t * vm, unformat_input_t * input) +{ + u32 fifo_size = 101, n_test_bytes = 100; + int i, j, rv, __clib_unused verbose = 0; + u8 *test_data = 0, *data_buf = 0; + ooo_segment_t *ooo_seg; + svm_fifo_t *f; + + while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) + { + if (unformat (input, "verbose")) + verbose = 1; + else + { + vlib_cli_output (vm, "parse error: '%U'", format_unformat_error, + input); + return -1; + } + } + + f = fifo_prepare (fifo_size); + vec_validate (test_data, n_test_bytes - 1); + vec_validate (data_buf, n_test_bytes - 1); + for (i = 0; i < vec_len (test_data); i++) + test_data[i] = i % 0xff; + + /* + * Test ooo segment distance to/from tail with u32 wrap + */ + + /* + * |0|---[start]--(len5)-->|0|--(len6)-->[end]---|0| + */ + rv = f_distance_from (f, ~0 - 5, 5); + SFIFO_TEST (rv == 11, "distance to tail should be %u is %u", 11, rv); + + rv = f_distance_to (f, ~0 - 5, 5); + SFIFO_TEST (rv == f->size - 11, "distance from tail should be %u is %u", + f->size - 11, rv); + + /* + * |0|---[end]--(len5)-->|0|--(len6)-->[start]---|0| + */ + rv = f_distance_to (f, 5, ~0 - 5); + SFIFO_TEST (rv == 11, "distance from tail should be %u is %u", 11, rv); + + rv = f_distance_from (f, 5, ~0 - 5); + SFIFO_TEST (rv == f->size - 11, "distance to tail should be %u is %u", + f->size - 11, rv); + + /* + * Add ooo with tail and ooo segment start u32 wrap + */ + svm_fifo_init_pointers (f, ~0, ~0); + svm_fifo_enqueue_with_offset (f, 10, 10, &test_data[10]); + SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 1), + "number of ooo segments %u", svm_fifo_n_ooo_segments (f)); + ooo_seg = svm_fifo_first_ooo_segment (f); + rv = ooo_segment_offset_prod (f, ooo_seg); + SFIFO_TEST (rv == 10, "offset should be %u is %u", 10, rv); + + svm_fifo_enqueue (f, 10, test_data); + SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 0), + "number of ooo segments %u", svm_fifo_n_ooo_segments (f)); + SFIFO_TEST (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX, + "there should be no ooo seg"); + + svm_fifo_peek (f, 5, 10, &data_buf[5]); + if (compare_data (data_buf, test_data, 5, 10, (u32 *) & j)) + SFIFO_TEST (0, "[%d] dequeued %u expected %u", j, data_buf[j], + test_data[j]); + + svm_fifo_dequeue (f, 20, data_buf); + if (compare_data (data_buf, test_data, 0, 20, (u32 *) & j)) + SFIFO_TEST (0, "[%d] dequeued %u expected %u", j, data_buf[j], + test_data[j]); + + /* + * Force collect with tail u32 wrap and without ooo segment start u32 wrap + */ + svm_fifo_init_pointers (f, ~0 - 10, ~0 - 10); + svm_fifo_enqueue_with_offset (f, 5, 15, &test_data[5]); + svm_fifo_enqueue (f, 12, test_data); + + SFIFO_TEST ((svm_fifo_n_ooo_segments (f) == 0), + "number of ooo segments %u", svm_fifo_n_ooo_segments (f)); + SFIFO_TEST (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX, + "there should be no ooo seg"); + + svm_fifo_dequeue (f, 20, data_buf); + if (compare_data (data_buf, test_data, 0, 20, (u32 *) & j)) + SFIFO_TEST (0, "[%d] dequeued %u expected %u", j, data_buf[j], + test_data[j]); + + /* + * Cleanup + */ + vec_free (test_data); + vec_free (data_buf); + svm_fifo_free (f); + return 0; +} + +/* + * Multiple ooo enqueues and dequeues that force fifo tail/head wrap + */ +static int +sfifo_test_fifo7 (vlib_main_t * vm, unformat_input_t * input) +{ + u32 fifo_size = 101, n_iterations = 100; + int i, j, rv, __clib_unused verbose = 0; + u8 *test_data = 0, *data_buf = 0; + u64 n_test_bytes = 100; + svm_fifo_t *f; + + while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) + { + if (unformat (input, "verbose")) + verbose = 1; + else + { + vlib_cli_output (vm, "parse error: '%U'", format_unformat_error, + input); + return -1; + } + } + + /* + * Prepare data structures + */ + f = fifo_prepare (fifo_size); + svm_fifo_init_pointers (f, ~0, ~0); + + vec_validate (test_data, n_test_bytes - 1); + vec_validate (data_buf, n_test_bytes - 1); + for (i = 0; i < vec_len (test_data); i++) + test_data[i] = i % 0xff; + + /* + * Run n iterations of test + */ + for (i = 0; i < n_iterations; i++) + { + for (j = n_test_bytes - 1; j > 0; j -= 2) + { + svm_fifo_enqueue_with_offset (f, j, 1, &test_data[j]); + rv = svm_fifo_n_ooo_segments (f); + if (rv != (n_test_bytes - j) / 2 + 1) + SFIFO_TEST (0, "number of ooo segments expected %u is %u", + (n_test_bytes - j) / 2 + 1, rv); + } + + svm_fifo_enqueue_with_offset (f, 1, n_test_bytes - 1, &test_data[1]); + rv = svm_fifo_n_ooo_segments (f); + if (rv != 1) + SFIFO_TEST (0, "number of ooo segments %u", rv); + + svm_fifo_enqueue (f, 1, test_data); + rv = svm_fifo_n_ooo_segments (f); + if (rv != 0) + SFIFO_TEST (0, "number of ooo segments %u", rv); + + svm_fifo_dequeue (f, n_test_bytes, data_buf); + if (compare_data (data_buf, test_data, 0, n_test_bytes, (u32 *) & j)) + SFIFO_TEST (0, "[%d] dequeued %u expected %u", j, data_buf[j], + test_data[j]); + svm_fifo_init_pointers (f, ~0 - i, ~0 - i); + } + SFIFO_TEST (1, "passed multiple ooo enqueue/dequeue"); + + /* + * Cleanup + */ + vec_free (test_data); + vec_free (data_buf); + svm_fifo_free (f); + return 0; +} + +/* + * Enqueue more than 4GB + */ +static int +sfifo_test_fifo_large (vlib_main_t * vm, unformat_input_t * input) +{ + u32 n_iterations = 100, n_bytes_per_iter, half; + int i, j, rv, __clib_unused verbose = 0; + u8 *test_data = 0, *data_buf = 0; + u64 n_test_bytes = 100; + svm_fifo_t *f; + + while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) + { + if (unformat (input, "verbose")) + verbose = 1; + else + { + vlib_cli_output (vm, "parse error: '%U'", format_unformat_error, + input); + return -1; + } + } + + + n_test_bytes = 5ULL << 30; + n_iterations = 1 << 10; + n_bytes_per_iter = n_test_bytes / n_iterations; + + f = fifo_prepare (n_bytes_per_iter + 1); + svm_fifo_init_pointers (f, ~0, ~0); + + vec_validate (test_data, n_bytes_per_iter - 1); + vec_validate (data_buf, n_bytes_per_iter - 1); + for (i = 0; i < vec_len (test_data); i++) + test_data[i] = i % 0xff; + + half = n_bytes_per_iter / 2; + for (i = 0; i < n_iterations; i++) + { + svm_fifo_enqueue_with_offset (f, half, half, &test_data[half]); + svm_fifo_enqueue (f, half, test_data); + rv = svm_fifo_n_ooo_segments (f); + if (rv != 0) + SFIFO_TEST (0, "number of ooo segments %u", rv); + svm_fifo_dequeue (f, n_bytes_per_iter, data_buf); + if (compare_data (data_buf, test_data, 0, n_bytes_per_iter, + (u32 *) & j)) + SFIFO_TEST (0, "[%d][%d] dequeued %u expected %u", i, j, data_buf[j], + test_data[j]); + } + SFIFO_TEST (1, "passed large transfer"); + + return 0; +} + static int sfifo_test_fifo_grow (vlib_main_t * vm, unformat_input_t * input) { @@ -938,6 +1194,7 @@ sfifo_test_fifo_grow (vlib_main_t * vm, unformat_input_t * input) int test_n_bytes, deq_bytes, enq_bytes, n_deqs, n_enqs; svm_fifo_chunk_t *c, *next, *prev; u8 *test_data = 0, *data_buf = 0; + u32 old_tail, offset; svm_fifo_t *f; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) @@ -969,6 +1226,7 @@ sfifo_test_fifo_grow (vlib_main_t * vm, unformat_input_t * input) fifo_size + 100, f->size); SFIFO_TEST (c->start_byte == fifo_size, "start byte expected %u is %u", fifo_size, c->start_byte); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); /* * Add with fifo wrapped @@ -987,18 +1245,20 @@ sfifo_test_fifo_grow (vlib_main_t * vm, unformat_input_t * input) fifo_size + 100, f->size); SFIFO_TEST (c->start_byte == fifo_size + 100, "start byte expected %u is " " %u", fifo_size + 100, c->start_byte); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); /* * Unwrap fifo */ vec_validate (data_buf, 200); - svm_fifo_dequeue_nowait (f, 201, data_buf); + svm_fifo_dequeue (f, 201, data_buf); SFIFO_TEST (f->end_chunk == c, "tail chunk should be updated"); SFIFO_TEST (f->size == fifo_size + 200, "size expected %u is %u", fifo_size + 200, f->size); SFIFO_TEST (c->start_byte == fifo_size + 100, "start byte expected %u is " "%u", fifo_size + 100, c->start_byte); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); /* * Add N chunks @@ -1018,6 +1278,7 @@ sfifo_test_fifo_grow (vlib_main_t * vm, unformat_input_t * input) svm_fifo_add_chunk (f, c); SFIFO_TEST (f->size == fifo_size + 200, "size expected %u is %u", fifo_size + 200, f->size); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); prev = 0; for (i = 0; i < 5; i++) @@ -1033,10 +1294,15 @@ sfifo_test_fifo_grow (vlib_main_t * vm, unformat_input_t * input) SFIFO_TEST (f->size == fifo_size + 200, "size expected %u is %u", fifo_size + 200, f->size); - svm_fifo_dequeue_nowait (f, 201, data_buf); + old_tail = f->tail; + svm_fifo_dequeue (f, 101, data_buf); SFIFO_TEST (f->size == fifo_size + 200 + 10 * 100, "size expected %u is %u", fifo_size + 200 + 10 * 100, f->size); + SFIFO_TEST (f->tail == old_tail, "new tail expected %u is %u", old_tail, + f->tail); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + /* * Enqueue/dequeue tests */ @@ -1057,11 +1323,11 @@ sfifo_test_fifo_grow (vlib_main_t * vm, unformat_input_t * input) SFIFO_TEST (f->tail_chunk->start_byte == 201, "start byte expected %u is " "%u", 201, f->tail_chunk->start_byte); - svm_fifo_enqueue_nowait (f, 200, test_data); + svm_fifo_enqueue (f, 200, test_data); SFIFO_TEST (f->tail_chunk->start_byte == 401, "start byte expected %u is " "%u", 401, f->tail_chunk->start_byte); - svm_fifo_dequeue_nowait (f, 200, data_buf); + svm_fifo_dequeue (f, 200, data_buf); SFIFO_TEST (f->head_chunk->start_byte == 401, "start byte expected %u is " "%u", 401, f->head_chunk->start_byte); @@ -1071,7 +1337,7 @@ sfifo_test_fifo_grow (vlib_main_t * vm, unformat_input_t * input) svm_fifo_init_pointers (f, f->nitems / 2, f->nitems / 2); for (i = 0; i < test_n_bytes; i++) { - rv = svm_fifo_enqueue_nowait (f, sizeof (u8), &test_data[i]); + rv = svm_fifo_enqueue (f, sizeof (u8), &test_data[i]); if (rv < 0) { clib_warning ("enqueue returned %d", rv); @@ -1083,7 +1349,7 @@ sfifo_test_fifo_grow (vlib_main_t * vm, unformat_input_t * input) "is %u", test_n_bytes, svm_fifo_max_dequeue (f)); for (i = 0; i < test_n_bytes; i++) - svm_fifo_dequeue_nowait (f, 1, &data_buf[i]); + svm_fifo_dequeue (f, 1, &data_buf[i]); rv = compare_data (data_buf, test_data, 0, vec_len (test_data), (u32 *) & j); @@ -1091,13 +1357,14 @@ sfifo_test_fifo_grow (vlib_main_t * vm, unformat_input_t * input) vlib_cli_output (vm, "[%d] dequeued %u expected %u", j, data_buf[j], test_data[j]); SFIFO_TEST ((rv == 0), "dequeued compared to original returned %d", rv); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); /* * Simple enqueue/deq and data validation (2) */ for (i = 0; i <= n_enqs; i++) { - rv = svm_fifo_enqueue_nowait (f, enq_bytes, test_data + i * enq_bytes); + rv = svm_fifo_enqueue (f, enq_bytes, test_data + i * enq_bytes); if (rv < 0) { clib_warning ("enqueue returned %d", rv); @@ -1109,7 +1376,7 @@ sfifo_test_fifo_grow (vlib_main_t * vm, unformat_input_t * input) "is %u", test_n_bytes, svm_fifo_max_dequeue (f)); for (i = 0; i <= n_deqs; i++) - svm_fifo_dequeue_nowait (f, deq_bytes, data_buf + i * deq_bytes); + svm_fifo_dequeue (f, deq_bytes, data_buf + i * deq_bytes); rv = compare_data (data_buf, test_data, 0, vec_len (test_data), (u32 *) & j); @@ -1117,6 +1384,42 @@ sfifo_test_fifo_grow (vlib_main_t * vm, unformat_input_t * input) vlib_cli_output (vm, "[%d] dequeued %u expected %u", j, data_buf[j], test_data[j]); SFIFO_TEST ((rv == 0), "dequeued compared to original returned %d", rv); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + /* + * Simple enqueue and drop + */ + for (i = 0; i <= n_enqs; i++) + { + rv = svm_fifo_enqueue (f, enq_bytes, test_data + i * enq_bytes); + if (rv < 0) + SFIFO_TEST (0, "failed to enqueue"); + } + + rv = svm_fifo_dequeue_drop (f, test_n_bytes / 2); + SFIFO_TEST (rv == test_n_bytes / 2, "drop should be equal"); + SFIFO_TEST (svm_fifo_is_sane (f), "head chunk should be valid"); + rv = svm_fifo_dequeue_drop (f, test_n_bytes / 2); + SFIFO_TEST (rv == test_n_bytes / 2, "drop should be equal"); + SFIFO_TEST (svm_fifo_is_sane (f), "head chunk should be valid"); + SFIFO_TEST (svm_fifo_max_dequeue (f) == 0, "should be empty"); + + /* + * Simple enqueue and drop all + */ + + /* Enqueue just enough data to make sure fifo is not full */ + for (i = 0; i <= n_enqs / 2; i++) + { + rv = svm_fifo_enqueue (f, enq_bytes, test_data + i * enq_bytes); + if (rv < 0) + SFIFO_TEST (0, "failed to enqueue"); + } + + /* check drop all as well */ + svm_fifo_dequeue_drop_all (f); + SFIFO_TEST (svm_fifo_is_sane (f), "head chunk should be valid"); + SFIFO_TEST (svm_fifo_max_dequeue (f) == 0, "should be empty"); /* * OOO enqueues/dequeues and data validation (1) @@ -1133,14 +1436,22 @@ sfifo_test_fifo_grow (vlib_main_t * vm, unformat_input_t * input) } } + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); SFIFO_TEST (svm_fifo_max_dequeue (f) == 0, "max deq expected %u is %u", 0, svm_fifo_max_dequeue (f)); - svm_fifo_enqueue_nowait (f, sizeof (u8), &test_data[0]); + svm_fifo_enqueue (f, sizeof (u8), &test_data[0]); memset (data_buf, 0, vec_len (data_buf)); + offset = 0; for (i = 0; i <= n_deqs; i++) - svm_fifo_dequeue_nowait (f, deq_bytes, data_buf + i * deq_bytes); + { + rv = svm_fifo_peek (f, offset, deq_bytes, data_buf + i * deq_bytes); + if (rv < 0 || (rv != deq_bytes && i != n_deqs)) + SFIFO_TEST (0, "unexpected peek %d", rv); + offset += rv; + } + svm_fifo_dequeue_drop (f, offset); rv = compare_data (data_buf, test_data, 0, vec_len (test_data), (u32 *) & j); @@ -1148,6 +1459,7 @@ sfifo_test_fifo_grow (vlib_main_t * vm, unformat_input_t * input) vlib_cli_output (vm, "[%d] dequeued %u expected %u", j, data_buf[j], test_data[j]); SFIFO_TEST ((rv == 0), "dequeued compared to original returned %d", rv); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); /* * OOO enqueues/dequeues and data validation (2) @@ -1167,12 +1479,14 @@ sfifo_test_fifo_grow (vlib_main_t * vm, unformat_input_t * input) goto cleanup; } } + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); - svm_fifo_enqueue_nowait (f, enq_bytes, &test_data[0]); + svm_fifo_enqueue (f, enq_bytes, &test_data[0]); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); memset (data_buf, 0, vec_len (data_buf)); for (i = 0; i <= n_deqs; i++) - svm_fifo_dequeue_nowait (f, deq_bytes, data_buf + i * deq_bytes); + svm_fifo_dequeue (f, deq_bytes, data_buf + i * deq_bytes); rv = compare_data (data_buf, test_data, 0, vec_len (test_data), (u32 *) & j); @@ -1180,6 +1494,7 @@ sfifo_test_fifo_grow (vlib_main_t * vm, unformat_input_t * input) vlib_cli_output (vm, "[%d] dequeued %u expected %u", j, data_buf[j], test_data[j]); SFIFO_TEST ((rv == 0), "dequeued compared to original returned %d", rv); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); /* * Cleanup @@ -1196,8 +1511,478 @@ cleanup: } svm_fifo_free (f); + vec_free (test_data); + vec_free (data_buf); + return 0; +} +static int +chunk_list_len (svm_fifo_chunk_t * c) +{ + svm_fifo_chunk_t *it; + int count = 0; + + if (!c) + return 0; + + count = 1; + it = c->next; + while (it && it != c) + { + it = it->next; + count++; + } + return count; +} + +static void +chunk_list_free (svm_fifo_chunk_t * c, svm_fifo_chunk_t * stop) +{ + svm_fifo_chunk_t *it, *next; + + it = c; + while (it && it != stop) + { + next = it->next; + clib_mem_free (it); + it = next; + } +} + +static void +chunk_list_splice (svm_fifo_chunk_t * a, svm_fifo_chunk_t * b) +{ + svm_fifo_chunk_t *it; + + it = a; + while (it->next) + it = it->next; + it->next = b; +} + +static int +sfifo_test_fifo_shrink (vlib_main_t * vm, unformat_input_t * input) +{ + int __clib_unused verbose = 0, fifo_size = 101, chunk_size = 100; + int i, rv, test_n_bytes, diff, deq_bytes; + svm_fifo_chunk_t *c, *prev, *collected; + u8 *test_data = 0, *data_buf = 0; + svm_fifo_t *f; + + while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) + { + if (unformat (input, "verbose")) + verbose = 1; + else + { + vlib_cli_output (vm, "parse error: '%U'", format_unformat_error, + input); + return -1; + } + } + + /* + * Init fifo with multiple chunks + */ + f = fifo_prepare (fifo_size); + svm_fifo_init_pointers (f, 0, 0); + + prev = 0; + for (i = 0; i < 11; i++) + { + c = clib_mem_alloc (sizeof (svm_fifo_chunk_t) + chunk_size); + c->length = 100; + c->start_byte = ~0; + c->next = prev; + prev = c; + } + + svm_fifo_add_chunk (f, c); + SFIFO_TEST (f->size == 12 * chunk_size + 1, "size expected %u is %u", + 12 * chunk_size + 1, f->size); + + /* + * No fifo wrap and no chunk used (one chunk) + */ + rv = svm_fifo_reduce_size (f, chunk_size, 0); + SFIFO_TEST (rv == chunk_size, "len expected %u is %u", chunk_size, rv); + SFIFO_TEST (f->size == 12 * chunk_size + 1, "size expected %u is %u", + 12 * chunk_size + 1, f->size); + SFIFO_TEST (f->flags & SVM_FIFO_F_SHRINK, "shrink flag should be set"); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + /* Check enqueue space to force size reduction */ + (void) svm_fifo_max_enqueue (f); + + SFIFO_TEST (f->size == 11 * chunk_size + 1, "size expected %u is %u", + 11 * chunk_size + 1, f->size); + SFIFO_TEST (f->flags & SVM_FIFO_F_COLLECT_CHUNKS, "collect flag should" + " be set"); + SFIFO_TEST (!(f->flags & SVM_FIFO_F_SHRINK), "shrink flag should not be" + " set"); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + collected = c = svm_fifo_collect_chunks (f); + rv = chunk_list_len (c); + SFIFO_TEST (rv == 1, "expected %u chunks got %u", 1, rv); + rv = chunk_list_len (f->start_chunk); + SFIFO_TEST (rv == 11, "expected %u chunks got %u", 11, rv); + SFIFO_TEST (!(f->flags & SVM_FIFO_F_COLLECT_CHUNKS), "collect flag should" + " not be set"); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + /* + * Fifo wrap and multiple chunks used + */ + + /* Init test data and fifo */ + test_n_bytes = f->nitems; + vec_validate (test_data, test_n_bytes - 1); + vec_validate (data_buf, vec_len (test_data)); + + for (i = 0; i < vec_len (test_data); i++) + test_data[i] = i; + + svm_fifo_init_pointers (f, f->size / 2, f->size / 2); + for (i = 0; i < test_n_bytes; i++) + { + rv = svm_fifo_enqueue (f, sizeof (u8), &test_data[i]); + if (rv < 0) + SFIFO_TEST (0, "enqueue returned"); + } + + /* Try to reduce fifo size with fifo full */ + rv = svm_fifo_reduce_size (f, 3.5 * chunk_size, 0); + SFIFO_TEST (rv == 3 * chunk_size, "len expected %u is %u", 3 * chunk_size, + rv); + SFIFO_TEST (f->size == 11 * chunk_size + 1, "size expected %u is %u", + 11 * chunk_size + 1, f->size); + SFIFO_TEST (f->flags & SVM_FIFO_F_SHRINK, "shrink flag should be set"); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + /* Check enqueue space to try size reduction. Should not work */ + rv = svm_fifo_max_enqueue (f); + + SFIFO_TEST (rv == 0, "free space expected %u is %u", 0, rv); + SFIFO_TEST (f->size == 11 * chunk_size + 1, "size expected %u is %u", + 11 * chunk_size + 1, f->size); + SFIFO_TEST (f->flags & SVM_FIFO_F_SHRINK, "shrink flag should be set"); + SFIFO_TEST (!(f->flags & SVM_FIFO_F_COLLECT_CHUNKS), "collect flag should" + " not be set"); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + /* Dequeue byte-by-byte up to last byte on last chunk */ + deq_bytes = f->size - f->size / 2 - 1; + for (i = 0; i < deq_bytes; i++) + { + (void) svm_fifo_max_enqueue (f); + rv = svm_fifo_dequeue (f, 1, &data_buf[i]); + if (rv < 0) + SFIFO_TEST (0, "dequeue returned"); + } + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + rv = svm_fifo_max_enqueue (f); + + /* We've dequeued more than 3*chunk_size so nitems should be updated */ + SFIFO_TEST (f->nitems == 8 * chunk_size, "nitems expected %u is %u", + 8 * chunk_size, f->nitems); + /* Free space should be what was dequeued - 3 * chunk_size, which was + * consumed by shrinking the fifo */ + diff = deq_bytes - 3 * chunk_size; + SFIFO_TEST (rv == diff, "free space expected %u is %u", diff, rv); + SFIFO_TEST (f->size == 11 * chunk_size + 1, "size expected %u is %u", + 11 * chunk_size + 1, f->size); + SFIFO_TEST (f->flags & SVM_FIFO_F_SHRINK, "shrink flag should be set"); + SFIFO_TEST (!(f->flags & SVM_FIFO_F_COLLECT_CHUNKS), "collect flag should" + " not be set"); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + /* Dequeue one more such that head goes beyond last chunk */ + rv = svm_fifo_dequeue (f, 1, &data_buf[deq_bytes]); + if (rv < 0) + SFIFO_TEST (0, "dequeue returned"); + + rv = svm_fifo_max_enqueue (f); + SFIFO_TEST (f->nitems == 8 * chunk_size, "nitems expected %u is %u", + 8 * chunk_size, f->nitems); + SFIFO_TEST (rv == diff + 1, "free space expected %u is %u", diff + 1, rv); + SFIFO_TEST (f->size == 8 * chunk_size + 1, "size expected %u is %u", + 8 * chunk_size + 1, f->size); + SFIFO_TEST (!(f->flags & SVM_FIFO_F_SHRINK), "shrink flag should not be" + " set"); + SFIFO_TEST (f->flags & SVM_FIFO_F_COLLECT_CHUNKS, "collect flag should" + " be set"); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + /* Dequeue the rest of the data */ + deq_bytes += 1; + for (i = 0; i < test_n_bytes - deq_bytes; i++) + { + rv = svm_fifo_dequeue (f, 1, &data_buf[i + deq_bytes]); + if (rv < 0) + SFIFO_TEST (0, "dequeue returned"); + } + + rv = svm_fifo_max_enqueue (f); + + SFIFO_TEST (f->size == 8 * chunk_size + 1, "size expected %u is %u", + 8 * chunk_size + 1, f->size); + SFIFO_TEST (rv == 8 * chunk_size, "free space expected %u is %u", + 8 * chunk_size, rv); + + rv = compare_data (data_buf, test_data, 0, vec_len (test_data), + (u32 *) & i); + if (rv) + SFIFO_TEST (0, "[%d] dequeued %u expected %u", i, data_buf[i], + test_data[i]); + + c = svm_fifo_collect_chunks (f); + rv = chunk_list_len (c); + SFIFO_TEST (rv == 3, "expected %u chunks got %u", 3, rv); + rv = chunk_list_len (f->start_chunk); + SFIFO_TEST (rv == 8, "expected %u chunks got %u", 8, rv); + SFIFO_TEST (!(f->flags & SVM_FIFO_F_COLLECT_CHUNKS), "collect flag should" + " not be set"); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + /* + * OOO segment on chunk that should be removed + */ + + svm_fifo_add_chunk (f, c); + SFIFO_TEST (f->size == 11 * chunk_size + 1, "size expected %u is %u", + 11 * chunk_size + 1, f->size); + + memset (data_buf, 0, vec_len (data_buf)); + svm_fifo_init_pointers (f, f->size / 2, f->size / 2); + svm_fifo_enqueue (f, 200, test_data); + svm_fifo_enqueue_with_offset (f, 50, vec_len (test_data) - 250, + &test_data[250]); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + /* Free space */ + rv = svm_fifo_max_enqueue (f); + SFIFO_TEST (rv == vec_len (test_data) - 200, "free space expected %u is %u", + vec_len (test_data) - 200, rv); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + /* Ask to reduce size */ + rv = svm_fifo_reduce_size (f, 3.5 * chunk_size, 0); + SFIFO_TEST (rv == 3 * chunk_size, "len expected %u is %u", 3 * chunk_size, + rv); + SFIFO_TEST (f->size == 11 * chunk_size + 1, "size expected %u is %u", + 11 * chunk_size + 1, f->size); + SFIFO_TEST (f->flags & SVM_FIFO_F_SHRINK, "shrink flag should be set"); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + /* Try to force size reduction but it should fail */ + rv = svm_fifo_max_enqueue (f); + + SFIFO_TEST (rv == vec_len (test_data) - 200, "free space expected %u is %u", + vec_len (test_data) - 200, rv); + SFIFO_TEST (f->size == 11 * chunk_size + 1, "size expected %u is %u", + 11 * chunk_size + 1, f->size); + SFIFO_TEST (f->flags & SVM_FIFO_F_SHRINK, "shrink flag should be set"); + SFIFO_TEST (!(f->flags & SVM_FIFO_F_COLLECT_CHUNKS), "collect flag should" + " not be set"); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + /* Dequeue the in order data. This should shrink nitems */ + rv = svm_fifo_dequeue (f, 200, data_buf); + if (rv < 0) + SFIFO_TEST (0, "dequeue returned"); + + rv = svm_fifo_max_enqueue (f); + SFIFO_TEST (rv == vec_len (test_data) - 200, "free space expected %u is %u", + vec_len (test_data) - 200, rv); + SFIFO_TEST (f->size == 11 * chunk_size + 1, "size expected %u is %u", + 11 * chunk_size + 1, f->size); + SFIFO_TEST (f->nitems == 11 * chunk_size - 200, "nitems expected %u is %u", + 11 * chunk_size - 200, f->nitems); + SFIFO_TEST (f->flags & SVM_FIFO_F_SHRINK, "shrink flag should be set"); + SFIFO_TEST (!(f->flags & SVM_FIFO_F_COLLECT_CHUNKS), "collect flag should" + " not be set"); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + /* Enqueue the missing 50 bytes. Fifo will become full */ + rv = svm_fifo_enqueue (f, 50, &test_data[200]); + SFIFO_TEST (rv == vec_len (test_data) - 200, "free space expected %u is %u", + vec_len (test_data) - 200, rv); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + rv = svm_fifo_max_enqueue (f); + + SFIFO_TEST (rv == 0, "free space expected %u is %u", 0, rv); + SFIFO_TEST (f->size == 11 * chunk_size + 1, "size expected %u is %u", + 11 * chunk_size + 1, f->size); + SFIFO_TEST (f->flags & SVM_FIFO_F_SHRINK, "shrink flag should be set"); + SFIFO_TEST (!(f->flags & SVM_FIFO_F_COLLECT_CHUNKS), "collect flag should" + " not be set"); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + + /* Dequeue a chunk and check nitems shrink but fifo still full */ + svm_fifo_dequeue (f, 100, &data_buf[200]); + + rv = svm_fifo_max_enqueue (f); + + SFIFO_TEST (rv == 0, "free space expected %u is %u", 0, rv); + SFIFO_TEST (f->size == 11 * chunk_size + 1, "size expected %u is %u", + 11 * chunk_size + 1, f->size); + SFIFO_TEST (f->nitems == 11 * chunk_size - 300, "nitems expected %u is %u", + 11 * chunk_size - 300, f->nitems); + SFIFO_TEST (f->flags & SVM_FIFO_F_SHRINK, "shrink flag should be set"); + SFIFO_TEST (!(f->flags & SVM_FIFO_F_COLLECT_CHUNKS), "collect flag should" + " not be set"); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + /* Dequeue enough to unwrap the fifo */ + deq_bytes = f->size - f->size / 2 - 300; + svm_fifo_dequeue (f, deq_bytes, &data_buf[300]); + rv = svm_fifo_max_enqueue (f); + + /* Overall we've dequeued deq_bytes + 300, but fifo size shrunk 300 */ + SFIFO_TEST (rv == 300 + deq_bytes - 300, "free space expected %u is %u", + 300 + deq_bytes - 300, rv); + SFIFO_TEST (f->size == 8 * chunk_size + 1, "size expected %u is %u", + 8 * chunk_size + 1, f->size); + SFIFO_TEST (f->nitems == 8 * chunk_size, "nitems expected %u is %u", + 8 * chunk_size, f->nitems); + SFIFO_TEST (!(f->flags & SVM_FIFO_F_SHRINK), "shrink flag should not be" + " set"); + SFIFO_TEST (f->flags & SVM_FIFO_F_COLLECT_CHUNKS, "collect flag should" + " be set"); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + /* Dequeue the rest */ + svm_fifo_dequeue (f, test_n_bytes / 2, &data_buf[300 + deq_bytes]); + rv = compare_data (data_buf, test_data, 0, vec_len (test_data), + (u32 *) & i); + if (rv) + SFIFO_TEST (0, "[%d] dequeued %u expected %u", i, data_buf[i], + test_data[i]); + + c = svm_fifo_collect_chunks (f); + rv = chunk_list_len (c); + SFIFO_TEST (rv == 3, "expected %u chunks got %u", 3, rv); + rv = chunk_list_len (f->start_chunk); + SFIFO_TEST (rv == 8, "expected %u chunks got %u", 8, rv); + SFIFO_TEST (!(f->flags & SVM_FIFO_F_COLLECT_CHUNKS), "collect flag should" + " not be set"); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + chunk_list_splice (collected, c); + + /* + * Remove all chunks possible (1) + * + * Tail and head are in first chunk that is not removed + */ + svm_fifo_init_pointers (f, 600, 600); + rv = svm_fifo_reduce_size (f, 8 * chunk_size, 1); + SFIFO_TEST (rv == 7 * chunk_size, "actual len expected %u is %u", + 7 * chunk_size, rv); + SFIFO_TEST (f->size == 6 * chunk_size + 1, "size expected %u is %u", + 6 * chunk_size + 1, f->size); + SFIFO_TEST (f->nitems == 1 * chunk_size, "nitems expected %u is %u", + 1 * chunk_size, f->nitems); + SFIFO_TEST (f->flags & SVM_FIFO_F_SHRINK, "shrink flag should be set"); + SFIFO_TEST (!(f->flags & SVM_FIFO_F_COLLECT_CHUNKS), "collect flag should" + " not be set"); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + rv = svm_fifo_max_enqueue (f); + SFIFO_TEST (rv == chunk_size, "free space expected %u is %u", chunk_size, + rv); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + /* Force head/tail to move to first chunk */ + svm_fifo_enqueue (f, 1, test_data); + svm_fifo_dequeue (f, 1, data_buf); + rv = svm_fifo_max_enqueue (f); + + SFIFO_TEST (rv == chunk_size, "free space expected %u is %u", chunk_size, + rv); + SFIFO_TEST (f->size == chunk_size + 1, "size expected %u is %u", + chunk_size + 1, f->size); + SFIFO_TEST (!(f->flags & SVM_FIFO_F_SHRINK), "shrink flag should not be" + " set"); + SFIFO_TEST (f->flags & SVM_FIFO_F_COLLECT_CHUNKS, "collect flag should" + " be set"); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + c = svm_fifo_collect_chunks (f); + rv = chunk_list_len (c); + SFIFO_TEST (rv == 7, "expected %u chunks got %u", 7, rv); + SFIFO_TEST (!(f->flags & SVM_FIFO_F_COLLECT_CHUNKS), "collect flag should" + " not be set"); + SFIFO_TEST (!(f->flags & SVM_FIFO_F_MULTI_CHUNK), "multi-chunk flag should" + " not be set"); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + /* re-add chunks for next test */ + svm_fifo_add_chunk (f, c); + + /* + * Remove all chunks possible (2) + * + * Tail and head are in the first chunk that should eventually be removed + */ + svm_fifo_init_pointers (f, 601, 601); + rv = svm_fifo_reduce_size (f, 8 * chunk_size, 1); + SFIFO_TEST (rv == 7 * chunk_size, "actual len expected %u is %u", + 7 * chunk_size, rv); + SFIFO_TEST (f->size == 7 * chunk_size + 1, "size expected %u is %u", + 7 * chunk_size + 1, f->size); + SFIFO_TEST (f->nitems == 1 * chunk_size, "nitems expected %u is %u", + 1 * chunk_size, f->nitems); + SFIFO_TEST (f->flags & SVM_FIFO_F_SHRINK, "shrink flag should be set"); + SFIFO_TEST (!(f->flags & SVM_FIFO_F_COLLECT_CHUNKS), "collect flag should" + " not be set"); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + rv = svm_fifo_max_enqueue (f); + SFIFO_TEST (rv == chunk_size, "free space expected %u is %u", chunk_size, + rv); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + /* Force head/tail to move to first chunk */ + svm_fifo_enqueue (f, chunk_size, test_data); + svm_fifo_dequeue (f, chunk_size, data_buf); + rv = svm_fifo_max_enqueue (f); + + SFIFO_TEST (rv == chunk_size, "free space expected %u is %u", chunk_size, + rv); + SFIFO_TEST (f->size == chunk_size + 1, "size expected %u is %u", + chunk_size + 1, f->size); + SFIFO_TEST (!(f->flags & SVM_FIFO_F_SHRINK), "shrink flag should not be" + " set"); + SFIFO_TEST (f->flags & SVM_FIFO_F_COLLECT_CHUNKS, "collect flag should" + " be set"); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + c = svm_fifo_collect_chunks (f); + rv = chunk_list_len (c); + SFIFO_TEST (rv == 7, "expected %u chunks got %u", 7, rv); + SFIFO_TEST (!(f->flags & SVM_FIFO_F_COLLECT_CHUNKS), "collect flag should" + " not be set"); + SFIFO_TEST (!(f->flags & SVM_FIFO_F_MULTI_CHUNK), "multi-chunk flag should" + " not be set"); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + chunk_list_splice (collected, c); + + /* + * Cleanup + */ + + chunk_list_free (f->start_chunk->next, f->start_chunk); + chunk_list_free (collected, 0); + svm_fifo_free (f); + vec_free (test_data); vec_free (data_buf); + return 0; } @@ -1238,6 +2023,13 @@ sfifo_test_fifo_replay (vlib_main_t * vm, unformat_input_t * input) static fifo_segment_main_t segment_main; +static svm_fifo_t * +fifo_segment_alloc_fifo (fifo_segment_t * fs, u32 data_bytes, + fifo_segment_ftype_t ftype) +{ + return fifo_segment_alloc_fifo_w_slice (fs, 0, data_bytes, ftype); +} + static int sfifo_test_fifo_segment_hello_world (int verbose) { @@ -1253,7 +2045,6 @@ sfifo_test_fifo_segment_hello_world (int verbose) a->segment_size = 256 << 10; rv = fifo_segment_create (sm, a); - SFIFO_TEST (!rv, "svm_fifo_segment_create returned %d", rv); fs = fifo_segment_get_segment (sm, a->new_segment_indices[0]); @@ -1265,16 +2056,16 @@ sfifo_test_fifo_segment_hello_world (int verbose) vec_validate (retrieved_data, vec_len (test_data) - 1); while (svm_fifo_max_enqueue (f) >= vec_len (test_data)) - svm_fifo_enqueue_nowait (f, vec_len (test_data), test_data); + svm_fifo_enqueue (f, vec_len (test_data), test_data); while (svm_fifo_max_dequeue (f) >= vec_len (test_data)) - svm_fifo_dequeue_nowait (f, vec_len (retrieved_data), retrieved_data); + svm_fifo_dequeue (f, vec_len (retrieved_data), retrieved_data); while (svm_fifo_max_enqueue (f) >= vec_len (test_data)) - svm_fifo_enqueue_nowait (f, vec_len (test_data), test_data); + svm_fifo_enqueue (f, vec_len (test_data), test_data); while (svm_fifo_max_dequeue (f) >= vec_len (test_data)) - svm_fifo_dequeue_nowait (f, vec_len (retrieved_data), retrieved_data); + svm_fifo_dequeue (f, vec_len (retrieved_data), retrieved_data); SFIFO_TEST (!memcmp (retrieved_data, test_data, vec_len (test_data)), "data should be identical"); @@ -1290,16 +2081,21 @@ sfifo_test_fifo_segment_hello_world (int verbose) static int sfifo_test_fifo_segment_fifo_grow (int verbose) { + int rv, fifo_size = 4096, n_chunks, n_batch; fifo_segment_main_t *sm = &segment_main; fifo_segment_create_args_t _a, *a = &_a; - int rv, fifo_size = 4096, n_chunks; + u32 n_free_chunk_bytes; fifo_segment_t *fs; svm_fifo_t *f; clib_memset (a, 0, sizeof (*a)); a->segment_name = "fifo-test1"; + /* size chosen to be able to force multi chunk allocation lower */ a->segment_size = 256 << 10; + /* fifo allocation allocates chunks in batch */ + n_batch = FIFO_SEGMENT_ALLOC_BATCH_SIZE; + rv = fifo_segment_create (sm, a); SFIFO_TEST (!rv, "svm_fifo_segment_create returned %d", rv); @@ -1312,27 +2108,56 @@ sfifo_test_fifo_segment_fifo_grow (int verbose) SFIFO_TEST (f != 0, "svm_fifo_segment_alloc_fifo"); + n_chunks = fifo_segment_num_free_chunks (fs, fifo_size); + SFIFO_TEST (n_chunks == n_batch - 1, "free 2^10B chunks " + "should be %u is %u", n_batch - 1, n_chunks); + rv = fifo_segment_fl_chunk_bytes (fs); + SFIFO_TEST (rv == (n_batch - 1) * fifo_size, "free chunk bytes %u " + "expected %u", rv, (n_batch - 1) * fifo_size); + + /* Grow by preallocated fifo_size chunk */ fifo_segment_grow_fifo (fs, f, fifo_size); SFIFO_TEST (f->size == 2 * fifo_size, "fifo size should be %u is %u", 2 * fifo_size, f->size); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + n_chunks = fifo_segment_num_free_chunks (fs, fifo_size); + SFIFO_TEST (n_chunks == n_batch - 2, "free 2^10B chunks " + "should be %u is %u", n_batch - 2, n_chunks); + rv = fifo_segment_fl_chunk_bytes (fs); + SFIFO_TEST (rv == (n_batch - 2) * fifo_size, "free chunk bytes %u " + "expected %u", rv, (n_batch - 2) * fifo_size); + + /* Grow by a size not preallocated but first make sure there's space */ + rv = fifo_segment_free_bytes (fs); + SFIFO_TEST (rv > 16 * fifo_size, "free bytes %u more than %u", rv, + 16 * fifo_size); fifo_segment_grow_fifo (fs, f, 16 * fifo_size); SFIFO_TEST (f->size == 18 * fifo_size, "fifo size should be %u is %u", 18 * fifo_size, f->size); + rv = fifo_segment_fl_chunk_bytes (fs); + SFIFO_TEST (rv == (n_batch - 2) * fifo_size, "free chunk bytes %u " + "expected %u", rv, (n_batch - 2) * fifo_size); + /* * Free and test free list size */ fifo_segment_free_fifo (fs, f); + rv = fifo_segment_fl_chunk_bytes (fs); + SFIFO_TEST (rv == (16 + n_batch) * fifo_size, "free chunk bytes expected %u" + " is %u", (16 + n_batch) * fifo_size, rv); n_chunks = fifo_segment_num_free_chunks (fs, fifo_size); - SFIFO_TEST (n_chunks == 1, "free 2^10B chunks should be %u is %u", 1, - n_chunks); + SFIFO_TEST (n_chunks == n_batch, "free 2^10B chunks " + "should be %u is %u", n_batch, n_chunks); n_chunks = fifo_segment_num_free_chunks (fs, 16 * fifo_size); SFIFO_TEST (n_chunks == 1, "free 2^14B chunks should be %u is %u", 1, n_chunks); n_chunks = fifo_segment_num_free_chunks (fs, ~0); - SFIFO_TEST (n_chunks == 2, "free chunks should be %u is %u", 2, n_chunks); + SFIFO_TEST (n_chunks == 1 + n_batch, "free chunks should be %u is %u", + 1 + n_batch, n_chunks); /* * Realloc fifo @@ -1341,21 +2166,183 @@ sfifo_test_fifo_segment_fifo_grow (int verbose) fifo_segment_grow_fifo (fs, f, fifo_size); n_chunks = fifo_segment_num_free_chunks (fs, fifo_size); - SFIFO_TEST (n_chunks == 0, "free 2^10B chunks should be %u is %u", 0, - n_chunks); + SFIFO_TEST (n_chunks == n_batch - 2, "free 2^10B chunks should be %u is %u", + n_batch - 2, n_chunks); fifo_segment_grow_fifo (fs, f, 16 * fifo_size); + n_chunks = fifo_segment_num_free_chunks (fs, 16 * fifo_size); SFIFO_TEST (n_chunks == 0, "free 2^14B chunks should be %u is %u", 0, n_chunks); n_chunks = fifo_segment_num_free_chunks (fs, ~0); - SFIFO_TEST (n_chunks == 0, "free chunks should be %u is %u", 0, n_chunks); + SFIFO_TEST (n_chunks == n_batch - 2, "free chunks should be %u is %u", + n_batch - 2, n_chunks); /* * Free again */ fifo_segment_free_fifo (fs, f); n_chunks = fifo_segment_num_free_chunks (fs, ~0); - SFIFO_TEST (n_chunks == 2, "free chunks should be %u is %u", 2, n_chunks); + SFIFO_TEST (n_chunks == 1 + n_batch, "free chunks should be %u is %u", + 1 + n_batch, n_chunks); + + rv = fifo_segment_fl_chunk_bytes (fs); + SFIFO_TEST (rv == (16 + n_batch) * fifo_size, "free chunk bytes expected %u" + " is %u", (16 + n_batch) * fifo_size, rv); + + n_free_chunk_bytes = rv; + + /* + * Allocate non power of 2 fifo/chunk and check that free chunk bytes + * is correctly updated + */ + + f = fifo_segment_alloc_fifo (fs, 16 * fifo_size - 1, FIFO_SEGMENT_RX_FIFO); + rv = fifo_segment_fl_chunk_bytes (fs); + + SFIFO_TEST (n_free_chunk_bytes - 16 * fifo_size == rv, "free chunk bytes " + "expected %u is %u", n_free_chunk_bytes - 16 * fifo_size, rv); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + fifo_segment_free_fifo (fs, f); + rv = fifo_segment_fl_chunk_bytes (fs); + + SFIFO_TEST (n_free_chunk_bytes == rv, "free chunk bytes expected %u is %u", + n_free_chunk_bytes, rv); + + /* + * Force multi chunk fifo allocation + */ + + /* Check that we can force multi chunk allocation. Note that fifo size + * rounded up to power of 2, i.e., 17 becomes 32 */ + rv = fifo_segment_free_bytes (fs); + SFIFO_TEST (rv < 32 * fifo_size, "free bytes %u less than %u", rv, + 32 * fifo_size); + + f = fifo_segment_alloc_fifo (fs, 17 * fifo_size, FIFO_SEGMENT_RX_FIFO); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + rv = fifo_segment_fl_chunk_bytes (fs); + + /* Make sure that the non-power of two chunk freed above is correctly + * accounted for in the chunk free bytes reduction due to chunk allocation + * for the fifo, i.e., it's rounded up by 1 */ + SFIFO_TEST (n_free_chunk_bytes - 17 * fifo_size == rv, "free chunk bytes " + "expected %u is %u", n_free_chunk_bytes - 17 * fifo_size, rv); + + fifo_segment_free_fifo (fs, f); + + rv = fifo_segment_fl_chunk_bytes (fs); + SFIFO_TEST (n_free_chunk_bytes == rv, "free chunk bytes expected %u is %u", + n_free_chunk_bytes, rv); + + /* + * Allocate fifo that has all chunks + */ + f = fifo_segment_alloc_fifo (fs, n_free_chunk_bytes, FIFO_SEGMENT_RX_FIFO); + SFIFO_TEST (f != 0, "allocation should work"); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + fifo_segment_free_fifo (fs, f); + + rv = fifo_segment_fl_chunk_bytes (fs); + SFIFO_TEST (n_free_chunk_bytes == rv, "free chunk bytes expected %u is %u", + n_free_chunk_bytes, rv); + + /* + * Try to allocate more than space available + */ + + f = fifo_segment_alloc_fifo (fs, n_free_chunk_bytes + fifo_size, + FIFO_SEGMENT_RX_FIFO); + SFIFO_TEST (f == 0, "allocation should fail"); + + /* + * Allocate fifo and try to grow beyond available space + */ + f = fifo_segment_alloc_fifo (fs, fifo_size, FIFO_SEGMENT_RX_FIFO); + rv = fifo_segment_grow_fifo (fs, f, n_free_chunk_bytes); + + SFIFO_TEST (rv == -1, "grow should fail"); + + fifo_segment_free_fifo (fs, f); + + /* + * Cleanup + */ + fifo_segment_delete (sm, fs); + vec_free (a->new_segment_indices); + return 0; +} + +static int +sfifo_test_fifo_segment_fifo_shrink (int verbose) +{ + int i, rv, chunk_size = 4096, n_chunks, n_free; + fifo_segment_main_t *sm = &segment_main; + fifo_segment_create_args_t _a, *a = &_a; + fifo_segment_t *fs; + svm_fifo_t *f; + + clib_memset (a, 0, sizeof (*a)); + a->segment_name = "fifo-test1"; + a->segment_size = 256 << 10; + + rv = fifo_segment_create (sm, a); + + SFIFO_TEST (!rv, "svm_fifo_segment_create returned %d", rv); + + /* + * Alloc and grow fifo + */ + fs = fifo_segment_get_segment (sm, a->new_segment_indices[0]); + f = fifo_segment_alloc_fifo (fs, chunk_size, FIFO_SEGMENT_RX_FIFO); + n_free = FIFO_SEGMENT_ALLOC_BATCH_SIZE - 1; + + SFIFO_TEST (f != 0, "svm_fifo_segment_alloc_fifo"); + + for (i = 0; i < 9; i++) + { + fifo_segment_grow_fifo (fs, f, chunk_size); + n_free -= 1; + if (f->size != (i + 2) * chunk_size) + SFIFO_TEST (0, "fifo size should be %u is %u", + (i + 2) * chunk_size, f->size); + } + + rv = svm_fifo_reduce_size (f, 3.5 * chunk_size, 1 /* is producer */ ); + SFIFO_TEST (rv == 3 * chunk_size, "len expected %u is %u", 3 * chunk_size, + rv); + + n_chunks = fifo_segment_num_free_chunks (fs, chunk_size); + SFIFO_TEST (n_chunks == n_free, "free chunks should be %u is %u", n_free, + n_chunks); + + fifo_segment_collect_fifo_chunks (fs, f); + + n_free += 3; + n_chunks = fifo_segment_num_free_chunks (fs, chunk_size); + SFIFO_TEST (n_chunks == n_free, "free chunks should be %u is %u", n_free, + n_chunks); + + rv = svm_fifo_reduce_size (f, 7 * chunk_size - 1, 1 /* is producer */ ); + SFIFO_TEST (rv == 6 * chunk_size, "len expected %u is %u", 6 * chunk_size, + rv); + + fifo_segment_collect_fifo_chunks (fs, f); + + n_free += 6; + n_chunks = fifo_segment_num_free_chunks (fs, chunk_size); + SFIFO_TEST (n_chunks == n_free, "free chunks should be %u is %u", n_free, + n_chunks); + /* + * Free + */ + fifo_segment_free_fifo (fs, f); + n_free += 1; + n_chunks = fifo_segment_num_free_chunks (fs, ~0); + SFIFO_TEST (n_chunks == n_free, "free chunks should be %u is %u", n_free, + n_chunks); /* * Cleanup @@ -1371,9 +2358,7 @@ sfifo_test_fifo_segment_slave (int verbose) fifo_segment_create_args_t _a, *a = &_a; fifo_segment_main_t *sm = &segment_main; u8 *test_data, *retrieved_data = 0; - fifo_segment_t *sp; - fifo_segment_header_t *fsh; - ssvm_shared_header_t *sh; + fifo_segment_t *fs; svm_fifo_t *f; u32 *result; int rv, i; @@ -1388,13 +2373,11 @@ sfifo_test_fifo_segment_slave (int verbose) SFIFO_TEST (!rv, "svm_fifo_segment_attach returned %d", rv); - sp = fifo_segment_get_segment (sm, a->new_segment_indices[0]); + fs = fifo_segment_get_segment (sm, a->new_segment_indices[0]); vec_free (a->new_segment_indices); - sh = sp->ssvm.sh; - fsh = (fifo_segment_header_t *) sh->opaque[0]; /* might wanna wait.. */ - f = fsh->fifos; + f = fifo_segment_get_slice_fifo_list (fs, 0); /* Lazy bastards united */ test_data = format (0, "Hello world%c", 0); @@ -1402,7 +2385,7 @@ sfifo_test_fifo_segment_slave (int verbose) for (i = 0; i < 1000; i++) { - svm_fifo_dequeue_nowait (f, vec_len (retrieved_data), retrieved_data); + svm_fifo_dequeue (f, vec_len (retrieved_data), retrieved_data); if (memcmp (retrieved_data, test_data, vec_len (retrieved_data))) { result = (u32 *) f->head_chunk->data; @@ -1456,7 +2439,7 @@ sfifo_test_fifo_segment_master_slave (int verbose) usleep (200e3); for (i = 0; i < 1000; i++) - svm_fifo_enqueue_nowait (f, vec_len (test_data), test_data); + svm_fifo_enqueue (f, vec_len (test_data), test_data); /* Wait for slave */ i = 0; @@ -1535,11 +2518,130 @@ sfifo_test_fifo_segment_mempig (int verbose) return 0; } +static int +sfifo_test_fifo_segment_prealloc (int verbose) +{ + fifo_segment_create_args_t _a, *a = &_a; + fifo_segment_main_t *sm = &segment_main; + u32 max_pairs, pairs_req, free_space, pair_mem; + svm_fifo_t *f, *old; + fifo_segment_t *fs; + int rv, alloc; + + clib_memset (a, 0, sizeof (*a)); + + a->segment_name = "fifo-test-prealloc"; + a->segment_size = 256 << 10; + a->segment_type = SSVM_SEGMENT_MEMFD; + + rv = fifo_segment_create (sm, a); + SFIFO_TEST (!rv, "svm_fifo_segment_create returned %d", rv); + fs = fifo_segment_get_segment (sm, a->new_segment_indices[0]); + + /* + * Prealloc chunks and headers + */ + free_space = fifo_segment_free_bytes (fs); + SFIFO_TEST (free_space <= 256 << 10, "free space expected %u is %u", + 256 << 10, free_space); + rv = fifo_segment_prealloc_fifo_chunks (fs, 0, 4096, 50); + SFIFO_TEST (rv == 0, "chunk prealloc should work"); + rv = fifo_segment_num_free_chunks (fs, 4096); + SFIFO_TEST (rv == 50, "prealloc chunks expected %u is %u", 50, rv); + rv = fifo_segment_free_bytes (fs); + free_space -= (sizeof (svm_fifo_chunk_t) + 4096) * 50; + SFIFO_TEST (rv == free_space, "free space expected %u is %u", free_space, + rv); + rv = fifo_segment_fl_chunk_bytes (fs); + SFIFO_TEST (rv == 4096 * 50, "chunk free space expected %u is %u", + 4096 * 50, rv); + + rv = fifo_segment_prealloc_fifo_hdrs (fs, 0, 50); + SFIFO_TEST (rv == 0, "fifo hdr prealloc should work"); + rv = fifo_segment_num_free_fifos (fs); + SFIFO_TEST (rv == 50, "prealloc fifo hdrs expected %u is %u", 50, rv); + rv = fifo_segment_free_bytes (fs); + free_space -= sizeof (svm_fifo_t) * 50; + SFIFO_TEST (rv == free_space, "free space expected %u is %u", free_space, + rv); + + fifo_segment_update_free_bytes (fs); + rv = fifo_segment_free_bytes (fs); + SFIFO_TEST (clib_abs (rv - (int) free_space) < 512, + "free space expected %u is %u", free_space, rv); + + f = fifo_segment_alloc_fifo (fs, 200 << 10, FIFO_SEGMENT_RX_FIFO); + SFIFO_TEST (f != 0, "fifo allocated"); + rv = fifo_segment_num_free_chunks (fs, 4096); + SFIFO_TEST (rv == 0, "prealloc chunks expected %u is %u", 0, rv); + rv = fifo_segment_fl_chunk_bytes (fs); + SFIFO_TEST (rv == 0, "chunk free space expected %u is %u", 0, rv); + SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane"); + + /* + * Multiple preallocs that consume the remaining space + */ + fifo_segment_update_free_bytes (fs); + free_space = fifo_segment_free_bytes (fs); + pair_mem = 2 * (4096 + sizeof (*f) + sizeof (svm_fifo_chunk_t)); + max_pairs = pairs_req = (free_space / pair_mem) - 1; + fifo_segment_preallocate_fifo_pairs (fs, 4096, 4096, &pairs_req); + SFIFO_TEST (pairs_req == 0, "prealloc pairs should work req %u", max_pairs); + rv = fifo_segment_num_free_chunks (fs, 4096); + SFIFO_TEST (rv == max_pairs * 2, "prealloc chunks expected %u is %u", + max_pairs * 2, rv); + + fifo_segment_update_free_bytes (fs); + rv = fifo_segment_free_bytes (fs); + SFIFO_TEST (rv < 2 * pair_mem, "free bytes %u less than %u", rv, + 2 * pair_mem); + + /* Preallocate as many more chunks as possible. Heap is almost full + * so we may not use all the free space*/ + alloc = 0; + while (!fifo_segment_prealloc_fifo_chunks (fs, 0, 4096, 1)) + alloc++; + SFIFO_TEST (alloc, "chunk prealloc should work %u", alloc); + rv = fifo_segment_num_free_chunks (fs, 4096); + SFIFO_TEST (rv == max_pairs * 2 + alloc, "prealloc chunks expected %u " + "is %u", max_pairs * 2 + alloc, rv); + + rv = fifo_segment_free_bytes (fs); + SFIFO_TEST (rv < pair_mem, "free bytes expected less than %u is %u", + pair_mem, rv); + + /* + * Test negative prealloc cases + */ + pairs_req = 1; + fifo_segment_preallocate_fifo_pairs (fs, 4096, 4096, &pairs_req); + SFIFO_TEST (pairs_req == 1, "prealloc pairs should not work"); + + old = f; + f = fifo_segment_alloc_fifo (fs, 200 << 10, FIFO_SEGMENT_RX_FIFO); + SFIFO_TEST (f == 0, "fifo alloc should fail"); + + rv = fifo_segment_prealloc_fifo_chunks (fs, 0, 4096, 50); + SFIFO_TEST (rv == -1, "chunk prealloc should fail"); + + rv = fifo_segment_prealloc_fifo_hdrs (fs, 0, 50); + SFIFO_TEST (rv == -1, "fifo hdr prealloc should fail"); + + /* + * Cleanup + */ + fifo_segment_free_fifo (fs, old); + close (fs->ssvm.fd); + fifo_segment_delete (sm, fs); + return 0; +} + static int sfifo_test_fifo_segment (vlib_main_t * vm, unformat_input_t * input) { int rv, verbose = 0; + fifo_segment_main_init (&segment_main, HIGH_SEGMENT_BASEVA, 5); while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "verbose")) @@ -1564,6 +2666,16 @@ sfifo_test_fifo_segment (vlib_main_t * vm, unformat_input_t * input) if ((rv = sfifo_test_fifo_segment_fifo_grow (verbose))) return -1; } + else if (unformat (input, "shrink fifo")) + { + if ((rv = sfifo_test_fifo_segment_fifo_shrink (verbose))) + return -1; + } + else if (unformat (input, "prealloc")) + { + if ((rv = sfifo_test_fifo_segment_prealloc (verbose))) + return -1; + } else if (unformat (input, "all")) { if ((rv = sfifo_test_fifo_segment_hello_world (verbose))) @@ -1572,6 +2684,10 @@ sfifo_test_fifo_segment (vlib_main_t * vm, unformat_input_t * input) return -1; if ((rv = sfifo_test_fifo_segment_fifo_grow (verbose))) return -1; + if ((rv = sfifo_test_fifo_segment_fifo_shrink (verbose))) + return -1; + if ((rv = sfifo_test_fifo_segment_prealloc (verbose))) + return -1; /* Pretty slow so avoid running it always if ((rv = sfifo_test_fifo_segment_master_slave (verbose))) return -1; @@ -1594,6 +2710,7 @@ svm_fifo_test (vlib_main_t * vm, unformat_input_t * input, int res = 0; char *str; + while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "fifo1")) @@ -1606,10 +2723,18 @@ svm_fifo_test (vlib_main_t * vm, unformat_input_t * input, res = sfifo_test_fifo4 (vm, input); else if (unformat (input, "fifo5")) res = sfifo_test_fifo5 (vm, input); + else if (unformat (input, "fifo6")) + res = sfifo_test_fifo6 (vm, input); + else if (unformat (input, "fifo7")) + res = sfifo_test_fifo7 (vm, input); + else if (unformat (input, "large")) + res = sfifo_test_fifo_large (vm, input); else if (unformat (input, "replay")) res = sfifo_test_fifo_replay (vm, input); else if (unformat (input, "grow")) res = sfifo_test_fifo_grow (vm, input); + else if (unformat (input, "shrink")) + res = sfifo_test_fifo_shrink (vm, input); else if (unformat (input, "segment")) res = sfifo_test_fifo_segment (vm, input); else if (unformat (input, "all")) @@ -1659,9 +2784,18 @@ svm_fifo_test (vlib_main_t * vm, unformat_input_t * input, if ((res = sfifo_test_fifo5 (vm, input))) goto done; + if ((res = sfifo_test_fifo6 (vm, input))) + goto done; + + if ((res = sfifo_test_fifo7 (vm, input))) + goto done; + if ((res = sfifo_test_fifo_grow (vm, input))) goto done; + if ((res = sfifo_test_fifo_shrink (vm, input))) + goto done; + str = "all"; unformat_init_cstring (input, str); if ((res = sfifo_test_fifo_segment (vm, input)))