+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;
+
+ 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, 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_chunk_prealloc_bytes (fs);
+ SFIFO_TEST (rv == 4096 * 50, "chunk free space expected %u is %u",
+ 4096 * 50, rv);
+
+ rv = fifo_segment_prealloc_fifo_hdrs (fs, 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_chunk_prealloc_bytes (fs);
+ SFIFO_TEST (rv == 0, "chunk free space expected %u is %u", 0, rv);
+
+ /*
+ * Multiple preallocs that consume the remaining space
+ */
+ 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");
+ rv = fifo_segment_num_free_chunks (fs, 4096);
+ SFIFO_TEST (rv == max_pairs * 2, "prealloc chunks expected %u is %u",
+ max_pairs * 2, rv);
+
+ rv = fifo_segment_prealloc_fifo_chunks (fs, 4096, 2);
+ SFIFO_TEST (rv == 0, "chunk prealloc should work");
+ rv = fifo_segment_num_free_chunks (fs, 4096);
+ SFIFO_TEST (rv == (max_pairs + 1) * 2, "prealloc chunks expected %u is %u",
+ (max_pairs + 1) * 2, rv);
+
+ free_space = fifo_segment_free_bytes (fs);
+ SFIFO_TEST (rv < 8192, "free bytes expected less than %u is %u", 8192, 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, 4096, 50);
+ SFIFO_TEST (rv == -1, "chunk prealloc should fail");
+
+ rv = fifo_segment_prealloc_fifo_hdrs (fs, 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;
+}
+