+ rv = svm_fifo_dequeue (f, fifo_size, data_buf);
+ SFIFO_TEST (rv == fifo_size, "should dequeue all data");
+ last_start_byte += 4096; /* size of last segment */
+
+ rv = compare_data (data_buf, test_data, 0, vec_len (test_data),
+ (u32 *) & i);
+
+ if (rv)
+ vlib_cli_output (vm, "[%d] dequeued %u expected %u", i, data_buf[i],
+ test_data[i]);
+ SFIFO_TEST ((rv == 0), "dequeued compared to original returned %d", rv);
+ SFIFO_TEST (f->head_chunk == 0, "head chunk should be 0");
+ SFIFO_TEST (f->tail_chunk == 0, "tail chunk should be 0");
+ SFIFO_TEST (f->ooo_deq == 0, "should have no ooo deq chunk");
+ SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
+
+ /*
+ * Allocate one new chunk by enqueueing out of order all but first chunk
+ *
+ */
+
+ enq_chunk = vec_len (test_data) / 10;
+ rv = enqueue_ooo (f, test_data, vec_len (test_data), 10);
+ SFIFO_TEST (!rv, "enqueue ooo should work");
+
+ SFIFO_TEST (svm_fifo_size (f) == fifo_size, "size expected %u is %u",
+ fifo_size, svm_fifo_size (f));
+ SFIFO_TEST (svm_fifo_max_dequeue (f) == 0, "max deq should be %u", 0);
+ /* Fifo has 2 chunks because the we didn't allow the first chunk to be
+ * freed when all the data was dequeued. Could be optimized in the future */
+ rv = svm_fifo_n_chunks (f);
+ SFIFO_TEST (rv == 2, "should have %u chunks has %u", 2, rv);
+ SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
+
+ SFIFO_TEST (f->head_chunk == 0, "should have no head chunk");
+ /* When new fifo chunks are allocated, tail is initialized */
+ SFIFO_TEST (f->tail_chunk != 0, "should have no tail chunk");
+ SFIFO_TEST (f->ooo_enq != 0, "should have an ooo enq chunk");
+
+ c = f->end_chunk;
+ SFIFO_TEST (c->start_byte == last_start_byte,
+ "end chunk should start at %u", last_start_byte);
+ SFIFO_TEST (c->length == 8192, "end chunk length should be %u", 8192);
+ SFIFO_TEST (f->ooo_enq == c, "ooo enq chunk should be end chunk");
+
+ /*
+ * Enqueue the first chunk
+ */
+ rv = svm_fifo_enqueue (f, enq_chunk, test_data);
+ SFIFO_TEST (rv == fifo_size, "enq should succeed %u", rv);
+ rv = svm_fifo_max_dequeue (f);
+ SFIFO_TEST (rv == fifo_size, "max deq should be %u is %u", fifo_size, rv);
+ rv = svm_fifo_n_chunks (f);
+ SFIFO_TEST (rv == 2, "should have %u chunks has %u", 2, rv);
+ SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
+
+ SFIFO_TEST (f->head_chunk == 0, "should have no head chunk");
+ /* Fifo is full so tail and ooo_enq should be 0 */
+ SFIFO_TEST (f->tail_chunk == 0, "should have no tail chunk");
+ SFIFO_TEST (f->ooo_enq == 0, "should have no ooo enq chunk");
+
+ /*
+ * Peek and validate data
+ */
+
+ memset (data_buf, 0, vec_len (data_buf));
+
+ rv = dequeue_ooo_inc (f, data_buf, fifo_size, 10);
+ SFIFO_TEST (!rv, "ooo deq should work %d", rv);
+
+ rv = compare_data (data_buf, test_data, 0, vec_len (test_data),
+ (u32 *) & i);
+ if (rv)
+ vlib_cli_output (vm, "[%d] dequeued %u expected %u", i, data_buf[i],
+ test_data[i]);
+ SFIFO_TEST ((rv == 0), "peeked compared to original returned %d", rv);
+ SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
+ /* Peeked all the data in a full fifo so ooo_deq ends up 0 */
+ SFIFO_TEST (f->ooo_deq == 0, "should have no ooo deq chunk");
+
+ /*
+ * Peek in reverse order and validate data
+ *
+ * RB tree should be exercised
+ */
+
+ memset (data_buf, 0, vec_len (data_buf));
+ for (i = 10; i >= 0; i--)
+ {
+ offset = i * enq_chunk;
+ deq_now = clib_min (enq_chunk, vec_len (test_data) - offset);
+ rv = svm_fifo_peek (f, offset, deq_now, data_buf + offset);
+ if (rv != deq_now)
+ SFIFO_TEST (0, "failed to peek");
+ }
+
+ rv = compare_data (data_buf, test_data, 0, vec_len (test_data),
+ (u32 *) & i);
+
+ if (rv)
+ vlib_cli_output (vm, "[%d] dequeued %u expected %u", i, data_buf[i],
+ test_data[i]);
+ SFIFO_TEST ((rv == 0), "peeked compared to original returned %d", rv);
+ SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
+ /* Last chunk peeked is the first, so ooo_deq should be non zero */
+ SFIFO_TEST (f->ooo_deq != 0, "should have ooo deq chunk");
+
+ /*
+ * Dequeue drop all bytes
+ */
+ rv = svm_fifo_dequeue_drop (f, fifo_size);
+ SFIFO_TEST ((rv == fifo_size), "all bytes should be dropped %u", rv);
+ last_start_byte += 8192;
+
+ SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
+ SFIFO_TEST (f->head_chunk == 0, "should have no head chunk");
+ SFIFO_TEST (f->tail_chunk == 0, "should have no tail chunk");
+
+ /* We don't remove the last chunk even when the fifo goes empty */
+ rv = svm_fifo_n_chunks (f);
+ SFIFO_TEST (rv == 1, "should have %u chunks has %u", 1, rv);
+
+ /*
+ * Increase size such that it can't be the sum of multiple chunk lengths
+ *
+ * A chunk of 16kB should be allocated
+ */
+ fifo_size += 2 * fifo_inc - 100;
+ svm_fifo_set_size (f, fifo_size);
+ validate_test_and_buf_vecs (&test_data, &data_buf, fifo_size + fifo_inc);
+ enq_chunk = vec_len (test_data) / 10;
+ memset (data_buf, 0, vec_len (data_buf));
+
+ /*
+ * Enqueue data ooo
+ */
+ rv = enqueue_ooo (f, test_data, fifo_size, 10);
+ SFIFO_TEST (!rv, "enqueue ooo should work");
+
+ SFIFO_TEST (svm_fifo_size (f) == fifo_size, "size expected %u is %u",
+ fifo_size, svm_fifo_size (f));
+ SFIFO_TEST (svm_fifo_max_dequeue (f) == 0, "max deq should be %u", 0);
+ /* Fifo has 2 chunks because the we didn't allow the first chunk to be
+ * freed when all the data was dequeued. Could be optimized in the future */
+ rv = svm_fifo_n_chunks (f);
+ SFIFO_TEST (rv == 2, "should have %u chunks has %u", 2, rv);
+ SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
+
+ SFIFO_TEST (f->head_chunk == 0, "should have no head chunk");
+ /* When new fifo chunks are allocated, tail is initialized */
+ SFIFO_TEST (f->tail_chunk != 0, "should have no tail chunk");
+ SFIFO_TEST (f->ooo_enq != 0, "should have an ooo enq chunk");
+
+ c = f->end_chunk;
+ SFIFO_TEST (c->start_byte == last_start_byte,
+ "end chunk should start at %u", last_start_byte);
+ SFIFO_TEST (c->length == 16384, "end chunk length should be %u", 16384);
+ SFIFO_TEST (f->ooo_enq == c, "ooo enq chunk should be end chunk");
+
+ /*
+ * Enqueue the first chunk
+ */
+ rv = svm_fifo_enqueue (f, enq_chunk, test_data);
+ SFIFO_TEST (rv == fifo_size, "enq should succeed %u", rv);
+ rv = svm_fifo_max_dequeue (f);
+ SFIFO_TEST (rv == fifo_size, "max deq should be %u is %u", fifo_size, rv);
+ rv = svm_fifo_n_chunks (f);
+ SFIFO_TEST (rv == 2, "should have %u chunks has %u", 2, rv);
+ SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
+
+ /*
+ * Dequeue just a part of data
+ */
+ rv = svm_fifo_dequeue (f, fifo_inc, data_buf);
+ SFIFO_TEST (rv == fifo_inc, "should dequeue all data");
+ rv = svm_fifo_n_chunks (f);
+ SFIFO_TEST (rv == 1, "should have %u chunks has %u", 1, rv);
+ SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
+
+ /*
+ * Enqueue ooo as much data as it was dequeued
+ */
+ rv = enqueue_ooo (f, test_data + fifo_size, fifo_inc, 2);
+ SFIFO_TEST (!rv, "ooo enqueue should work %d", rv);
+
+ rv = svm_fifo_enqueue (f, fifo_inc / 2, test_data + fifo_size);
+ SFIFO_TEST (rv == fifo_inc, "enqueue should work %d", rv);
+
+ rv = svm_fifo_n_chunks (f);
+ SFIFO_TEST (rv == 2, "should have %u chunks has %u", 2, rv);
+ SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
+
+ last_start_byte += 16384;
+ c = f->end_chunk;
+ SFIFO_TEST (c->start_byte == last_start_byte,
+ "end chunk should start at %u", last_start_byte);
+ SFIFO_TEST (c->length == 4096, "end chunk length should be %u", 4096);
+
+ /*
+ * Dequeue all
+ */
+ rv = svm_fifo_dequeue (f, fifo_size, data_buf + fifo_inc);
+ SFIFO_TEST (rv == fifo_size, "should dequeue all data");
+
+ rv = compare_data (data_buf, test_data, 0, vec_len (test_data),
+ (u32 *) & i);
+ if (rv)
+ vlib_cli_output (vm, "[%d] dequeued %u expected %u", i, data_buf[i],
+ test_data[i]);
+ SFIFO_TEST ((rv == 0), "dequeued compared to original returned %d", rv);
+ SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
+ /* fifo does not end on chunk boundary because of the - 100 */
+ SFIFO_TEST (f->head_chunk != 0, "should have head chunk");
+ SFIFO_TEST (f->tail_chunk != 0, "should have tail chunk");
+
+ /*
+ * Enqueue and dequeue byte-by-byte ooo
+ */
+
+ memset (data_buf, 0, vec_len (data_buf));
+
+ rv = enqueue_ooo (f, test_data, fifo_size, fifo_size);
+ SFIFO_TEST (!rv, "ooo enqueue should work %d", rv);
+
+ rv = svm_fifo_enqueue (f, 1, test_data);
+ SFIFO_TEST (rv == fifo_size, "enqueue should work %d", rv);
+
+ rv = dequeue_ooo (f, data_buf, fifo_size, fifo_size);
+ SFIFO_TEST (!rv, "ooo deq should work %d", rv);
+
+ rv = compare_data (data_buf, test_data, 0, fifo_size, (u32 *) & i);
+ if (rv)
+ vlib_cli_output (vm, "[%d] dequeued %u expected %u", i, data_buf[i],
+ test_data[i]);
+ SFIFO_TEST ((rv == 0), "dequeued compared to original returned %d", rv);
+ SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
+
+ last_start_byte += 4096;
+ c = f->end_chunk;
+ SFIFO_TEST (c->start_byte == last_start_byte,
+ "end chunk should start at %u", last_start_byte);
+ SFIFO_TEST (c->length == 16384, "end chunk length should be %u", 16384);
+
+ rv = svm_fifo_n_chunks (f);
+ SFIFO_TEST (rv == 2, "should have %u chunks has %u", 2, rv);
+
+ /*
+ * Dequeue drop all bytes
+ */
+ rv = svm_fifo_dequeue_drop (f, fifo_size);
+ SFIFO_TEST ((rv == fifo_size), "all bytes should be dropped %u", rv);
+
+ SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
+ SFIFO_TEST (f->head_chunk != 0, "should have head chunk");
+ SFIFO_TEST (f->tail_chunk != 0, "should have tail chunk");
+
+ /* We don't remove the last chunk even when the fifo goes empty */
+ rv = svm_fifo_n_chunks (f);
+ SFIFO_TEST (rv == 1, "should have %u chunks has %u", 1, rv);
+ SFIFO_TEST (f->ooo_enq == 0, "should have no ooo enq chunk");
+ SFIFO_TEST (f->ooo_deq == 0, "should have no ooo deq chunk");
+
+ /*
+ * Grow fifo to 4MB and force only 4kB chunk allocations
+ */
+ fifo_size = 4 << 20;
+ svm_fifo_set_size (f, fifo_inc);
+ validate_test_and_buf_vecs (&test_data, &data_buf, fifo_size);
+ enq_chunk = 1500;
+ memset (data_buf, 0, vec_len (data_buf));
+
+ rv = enqueue_packets_inc (f, fifo_size, enq_chunk, test_data);
+ SFIFO_TEST (!rv, "incremental packet enqueue should work");
+
+ SFIFO_TEST (svm_fifo_max_dequeue (f) == fifo_size, "max deq should be %u",
+ fifo_size);
+ SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
+
+ rv = svm_fifo_n_chunks (f);
+ SFIFO_TEST (rv == (fifo_size / 4096) + 1, "should have %u chunks has %u",
+ (fifo_size / 4096) + 1, rv);
+
+
+ /*
+ * Dequeue all
+ */
+ rv = svm_fifo_dequeue (f, fifo_size, data_buf);
+ SFIFO_TEST (rv == fifo_size, "should dequeue all data");
+
+ rv = compare_data (data_buf, test_data, 0, vec_len (test_data),
+ (u32 *) & i);
+ if (rv)
+ vlib_cli_output (vm, "[%d] dequeued %u expected %u", i, data_buf[i],
+ test_data[i]);
+ SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
+ SFIFO_TEST ((rv == 0), "dequeued compared to original returned %d", rv);
+ SFIFO_TEST (f->ooo_deq == 0, "should have no ooo deq chunk");
+ rv = svm_fifo_n_chunks (f);
+ SFIFO_TEST (rv == 1, "should have %u chunks has %u", 1, rv);
+
+ /*
+ * Cleanup
+ */
+
+ ft_fifo_free (fs, f);
+ ft_fifo_segment_free (fsm, fs);
+ vec_free (test_data);
+ vec_free (data_buf);
+ return 0;
+}
+
+static int
+sfifo_test_fifo_shrink (vlib_main_t * vm, unformat_input_t * input)
+{
+ int __clib_unused verbose = 0, fifo_size = 4096, deq_chunk;
+ fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
+ u8 *test_data = 0, *data_buf = 0;
+ fifo_segment_t *fs;
+ svm_fifo_t *f;
+ u32 enq_chunk;
+ int i, rv;
+
+ 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 and enqueue data such that multiple 4096 chunks are allocated
+ */
+ fs = fifo_segment_prepare (fsm, "fifo-shrink", 0);
+ f = fifo_prepare (fs, fifo_size);
+
+ fifo_size = 4 << 20;
+ svm_fifo_set_size (f, 4096);
+ validate_test_and_buf_vecs (&test_data, &data_buf, fifo_size);
+ enq_chunk = 1500;
+ rv = enqueue_packets_inc (f, fifo_size, enq_chunk, test_data);
+ SFIFO_TEST (!rv, "incremental packet enqueue should work");
+
+ rv = svm_fifo_max_enqueue (f);
+ SFIFO_TEST (rv == 0, "enqueue space %u", rv);
+ SFIFO_TEST (svm_fifo_max_dequeue (f) == fifo_size, "max deq should be %u",
+ fifo_size);
+ SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
+
+ rv = svm_fifo_n_chunks (f);
+ SFIFO_TEST (rv == (fifo_size / 4096), "should have %u chunks has %u",
+ (fifo_size / 4096), rv);
+
+ /*
+ * Dequeue enough to collect one chunk
+ */
+ deq_chunk = 4096;
+ rv = svm_fifo_dequeue (f, deq_chunk, data_buf);
+ SFIFO_TEST (rv == deq_chunk, "should dequeue all data");
+
+ rv = svm_fifo_n_chunks (f);
+ SFIFO_TEST (rv == (fifo_size / 4096) - 1, "should have %u chunks has %u",
+ (fifo_size / 4096) - 1, rv);
+ SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
+
+ rv = svm_fifo_max_enqueue (f);
+ SFIFO_TEST (rv == deq_chunk, "enqueue space %u", rv);
+
+ /*
+ * Dequeue ooo byte-by-byte remaining data
+ */
+ rv = dequeue_ooo (f, data_buf + deq_chunk, fifo_size - deq_chunk,
+ fifo_size - deq_chunk);
+ SFIFO_TEST (!rv, "ooo deq should work %d", rv);
+
+ rv = compare_data (data_buf, test_data, 0, fifo_size, (u32 *) & i);
+ if (rv)
+ vlib_cli_output (vm, "[%d] dequeued %u expected %u", i, data_buf[i],
+ test_data[i]);
+
+ SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
+ rv = svm_fifo_n_chunks (f);
+ SFIFO_TEST (rv == (fifo_size / 4096) - 1, "should have %u chunks has %u",
+ (fifo_size / 4096) - 1, rv);
+
+ /*
+ * Drop all data
+ */
+ rv = svm_fifo_dequeue_drop (f, fifo_size - deq_chunk);
+ SFIFO_TEST (rv == fifo_size - deq_chunk, "should drop all data");
+ SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
+ rv = svm_fifo_n_chunks (f);
+ SFIFO_TEST (rv == 1, "should have %u chunks has %u", 1, rv);
+ rv = svm_fifo_max_enqueue (f);
+ SFIFO_TEST (rv == fifo_size, "enqueue space %u", rv);
+
+
+ /*
+ * Reset size and enqueue ooo all data
+ */
+ svm_fifo_set_size (f, 4096);
+ enq_chunk = deq_chunk = 1500;
+ rv = enqueue_ooo_packets (f, vec_len (test_data), 1500, test_data);
+ SFIFO_TEST (!rv, "enqueue ooo should work");
+ SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
+
+ /* 1 additional chunk left from previous test */
+ rv = svm_fifo_n_chunks (f);
+ SFIFO_TEST (rv == (fifo_size / 4096) + 1, "should have %u chunks has %u",
+ (fifo_size / 4096) + 1, rv);
+
+ /*
+ * Add missing first chunk
+ */
+ rv = svm_fifo_enqueue (f, enq_chunk, test_data);
+ SFIFO_TEST (rv == fifo_size, "enq should succeed %u", rv);
+ SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
+ rv = svm_fifo_max_dequeue (f);
+ SFIFO_TEST (rv == fifo_size, "max deq should be %u is %u", fifo_size, rv);
+ rv = svm_fifo_n_chunks (f);
+ SFIFO_TEST (rv == (fifo_size / 4096) + 1, "should have %u chunks has %u",
+ (fifo_size / 4096) + 1, rv);
+
+ /*
+ * Dequeue as packets
+ */
+ memset (data_buf, 0, vec_len (data_buf));
+ rv = dequeue_packets (f, fifo_size, deq_chunk, data_buf);
+ SFIFO_TEST (!rv, "deq pkts should work %d", rv);
+
+ rv = compare_data (data_buf, test_data, 0, fifo_size, (u32 *) & i);
+ if (rv)
+ vlib_cli_output (vm, "[%d] dequeued %u expected %u", i, data_buf[i],
+ test_data[i]);
+
+ /*
+ * Enqueue and dequeue set of packets
+ */
+ svm_fifo_set_size (f, 4096);
+ for (i = 0; i < 1000; i++)
+ {
+ rv = svm_fifo_enqueue (f, enq_chunk, test_data);
+ if (rv != enq_chunk)
+ SFIFO_TEST (0, "enq fail");
+ rv = svm_fifo_dequeue (f, deq_chunk, data_buf);
+ if (rv != deq_chunk)
+ SFIFO_TEST (0, "deq fail");
+ }
+
+ /*
+ * Cleanup
+ */
+
+ ft_fifo_free (fs, f);
+ ft_fifo_segment_free (fsm, fs);
+ vec_free (test_data);
+ vec_free (data_buf);
+
+ return 0;
+}
+
+static int
+sfifo_test_fifo_indirect (vlib_main_t * vm, unformat_input_t * input)
+{
+ int __clib_unused verbose = 0, fifo_size = 4096, deq_chunk;
+ fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
+ u8 *test_data = 0, *data_buf = 0;
+ svm_fifo_chunk_t *c;
+ fifo_segment_t *fs;
+ svm_fifo_t *f;
+ int rv;
+
+ 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 and enqueue data such that multiple 4096 chunks are allocated
+ */
+ fs = fifo_segment_prepare (fsm, "fifo-indirect", 0);
+ f = fifo_prepare (fs, fifo_size);
+
+ fifo_size = 4 << 20;
+ svm_fifo_set_size (f, fifo_size);
+ validate_test_and_buf_vecs (&test_data, &data_buf, fifo_size);
+
+ c = f->start_chunk;
+ SFIFO_TEST (c->next == 0, "no next");
+
+ svm_fifo_fill_chunk_list (f);
+ SFIFO_TEST (c->next != 0, "new chunk should've been allocated");
+ SFIFO_TEST (c->next->length == 4 << 20, "new chunk should be 4MB");
+
+ rv = svm_fifo_max_write_chunk (f);
+ SFIFO_TEST (rv == 4096, "max write chunk %u", rv);
+
+ /*
+ * Enqueue enough to fill first chunk
+ */
+ svm_fifo_enqueue_nocopy (f, 4096);
+ SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
+
+ c = svm_fifo_tail_chunk (f);
+ SFIFO_TEST (c == f->end_chunk, "tail is end chunk");
+
+ /* Initialize head chunk */
+ rv = svm_fifo_max_read_chunk (f);
+ SFIFO_TEST (rv == 4096, "max read chunk %u", rv);
+ SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
+
+ /*
+ * Move head over last segment
+ */
+ rv = svm_fifo_dequeue (f, 4096, data_buf);
+ SFIFO_TEST (rv == 4096, "dequeue should work");
+
+ c = svm_fifo_head_chunk (f);
+ SFIFO_TEST (c == f->end_chunk, "head chunk should be last");
+
+ rv = svm_fifo_max_read_chunk (f);
+ SFIFO_TEST (rv == 0, "max read chunk %u", rv);
+
+ rv = svm_fifo_max_write_chunk (f);
+ SFIFO_TEST (rv == 4 << 20, "max write chunk %u", rv);
+
+ /*
+ * Cleanup
+ */
+
+ ft_fifo_free (fs, f);
+ ft_fifo_segment_free (fsm, fs);
+ vec_free (test_data);
+ vec_free (data_buf);
+
+ return 0;
+}
+
+/* *INDENT-OFF* */
+svm_fifo_trace_elem_t fifo_trace[] = {};
+/* *INDENT-ON* */
+
+static int
+sfifo_test_fifo_replay (vlib_main_t * vm, unformat_input_t * input)
+{
+ svm_fifo_t f;
+ int verbose = 0;
+ u8 no_read = 0, *str = 0;
+
+ while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (input, "verbose"))
+ verbose = 1;
+ else if (unformat (input, "no-read"))
+ no_read = 1;
+ else
+ {
+ clib_error_t *e = clib_error_return
+ (0, "unknown input `%U'", format_unformat_error, input);
+ clib_error_report (e);
+ return -1;
+ }
+ }
+
+#if SVMF_FIFO_TRACE
+ f.trace = fifo_trace;
+#endif
+
+ str = svm_fifo_replay (str, &f, no_read, verbose);
+ vlib_cli_output (vm, "%v", str);
+ return 0;
+}
+
+static int
+sfifo_test_fifo_make_rcv_wnd_zero (vlib_main_t * vm, unformat_input_t * input)
+{
+ int __clib_unused verbose = 0, fifo_size = 4096, deq_chunk;
+ fifo_segment_main_t _fsm = { 0 }, *fsm = &_fsm;
+ u8 *test_data = 0, *data_buf = 0;
+ fifo_segment_t *fs;
+ svm_fifo_t *f;
+ int rv;
+
+ 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 and enqueue data such that multiple 4096 chunks are allocated
+ */
+ fs = fifo_segment_prepare (fsm, "fifo-rcv-wnd-zero", 0);
+ f = fifo_prepare (fs, fifo_size);
+
+ /* Enqueue 3000 into 4KB chunk, so there'll be 1096 free space */
+ svm_fifo_set_size (f, 4096);
+ validate_test_and_buf_vecs (&test_data, &data_buf, fifo_size);
+ rv = svm_fifo_enqueue (f, 3000, test_data);
+ SFIFO_TEST (rv == 3000, "enqueued %u", rv);
+ rv = svm_fifo_max_enqueue (f);
+ SFIFO_TEST (rv == 1096, "svm_fifo_max_enqueue %u", rv);
+ SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
+
+ /* Shrink fifo size to the in-use size */
+ svm_fifo_set_size (f, 3000);
+ SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
+
+ /* In TCP, this should result in rcv-wnd = 0 */
+ rv = svm_fifo_max_enqueue (f);
+ SFIFO_TEST (rv == 0, "svm_fifo_max_enqueue %u", rv);
+ rv = svm_fifo_max_enqueue_prod (f);
+ SFIFO_TEST (rv == 0, "svm_fifo_max_enqueue_prod %u", rv);
+
+ /* Dequeue and ... */
+ rv = svm_fifo_dequeue (f, 3000, data_buf);
+ SFIFO_TEST (rv == 3000, "dequeued %u", rv);
+
+ /* Clean up */
+ ft_fifo_free (fs, f);
+ ft_fifo_segment_free (fsm, fs);
+ vec_free (test_data);
+ vec_free (data_buf);
+
+ return 0;
+}
+
+
+static fifo_segment_main_t segment_main;
+
+static int
+sfifo_test_fifo_segment_hello_world (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 *fs;