1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 #include <rte_cycles.h>
12 #include <rte_errno.h>
15 #ifdef RTE_EXEC_ENV_WINDOWS
19 printf("reorder not supported on Windows, skipping test\n");
24 #include <rte_reorder.h>
25 #include <rte_lcore.h>
26 #include <rte_malloc.h>
29 #define REORDER_BUFFER_SIZE 16384
30 #define NUM_MBUFS (2*REORDER_BUFFER_SIZE)
31 #define REORDER_BUFFER_SIZE_INVALID 2049
33 struct reorder_unittest_params {
34 struct rte_mempool *p;
35 struct rte_reorder_buffer *b;
38 static struct reorder_unittest_params default_params = {
43 static struct reorder_unittest_params *test_params = &default_params;
46 test_reorder_create(void)
48 struct rte_reorder_buffer *b = NULL;
50 b = rte_reorder_create(NULL, rte_socket_id(), REORDER_BUFFER_SIZE);
51 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
52 "No error on create() with NULL name");
54 b = rte_reorder_create("PKT", rte_socket_id(), REORDER_BUFFER_SIZE_INVALID);
55 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
56 "No error on create() with invalid buffer size param.");
58 b = rte_reorder_create("PKT_RO1", rte_socket_id(), REORDER_BUFFER_SIZE);
59 TEST_ASSERT_EQUAL(b, test_params->b,
60 "New reorder instance created with already existing name");
66 test_reorder_init(void)
68 struct rte_reorder_buffer *b = NULL;
71 * The minimum memory area size that should be passed to library is,
72 * sizeof(struct rte_reorder_buffer) + (2 * size * sizeof(struct rte_mbuf *));
73 * Otherwise error will be thrown
77 b = rte_reorder_init(b, size, "PKT1", REORDER_BUFFER_SIZE);
78 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
79 "No error on init with NULL buffer.");
81 b = rte_malloc(NULL, size, 0);
82 b = rte_reorder_init(b, size, "PKT1", REORDER_BUFFER_SIZE);
83 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
84 "No error on init with invalid mem zone size.");
88 b = rte_malloc(NULL, size, 0);
89 b = rte_reorder_init(b, size, "PKT1", REORDER_BUFFER_SIZE_INVALID);
90 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
91 "No error on init with invalid buffer size param.");
93 b = rte_reorder_init(b, size, NULL, REORDER_BUFFER_SIZE);
94 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
95 "No error on init with invalid name.");
102 test_reorder_find_existing(void)
104 struct rte_reorder_buffer *b = NULL;
106 /* Try to find existing reorder buffer instance */
107 b = rte_reorder_find_existing("PKT_RO1");
108 TEST_ASSERT_EQUAL(b, test_params->b,
109 "existing reorder buffer instance not found");
111 /* Try to find non existing reorder buffer instance */
112 b = rte_reorder_find_existing("ro_find_non_existing");
113 TEST_ASSERT((b == NULL) && (rte_errno == ENOENT),
114 "non existing reorder buffer instance found");
120 test_reorder_free(void)
122 struct rte_reorder_buffer *b1 = NULL, *b2 = NULL;
123 const char *name = "test_free";
125 b1 = rte_reorder_create(name, rte_socket_id(), 8);
126 TEST_ASSERT_NOT_NULL(b1, "Failed to create reorder buffer.");
128 b2 = rte_reorder_find_existing(name);
129 TEST_ASSERT_EQUAL(b1, b2, "Failed to find existing reorder buffer");
131 rte_reorder_free(b1);
133 b2 = rte_reorder_find_existing(name);
134 TEST_ASSERT((b2 == NULL) && (rte_errno == ENOENT),
135 "Found previously freed reorder buffer");
141 test_reorder_insert(void)
143 struct rte_reorder_buffer *b = NULL;
144 struct rte_mempool *p = test_params->p;
145 const unsigned int size = 4;
146 const unsigned int num_bufs = 7;
147 struct rte_mbuf *bufs[num_bufs];
151 /* This would create a reorder buffer instance consisting of:
153 * ready_buf: RB[size] = {NULL, NULL, NULL, NULL}
154 * order_buf: OB[size] = {NULL, NULL, NULL, NULL}
156 b = rte_reorder_create("test_insert", rte_socket_id(), size);
157 TEST_ASSERT_NOT_NULL(b, "Failed to create reorder buffer");
159 for (i = 0; i < num_bufs; i++) {
160 bufs[i] = rte_pktmbuf_alloc(p);
161 TEST_ASSERT_NOT_NULL(bufs[i], "Packet allocation failed\n");
162 *rte_reorder_seqn(bufs[i]) = i;
165 /* This should fill up order buffer:
167 * RB[] = {NULL, NULL, NULL, NULL}
168 * OB[] = {0, 1, 2, 3}
170 for (i = 0; i < size; i++) {
171 ret = rte_reorder_insert(b, bufs[i]);
173 printf("%s:%d: Error inserting packet with seqn less than size\n",
181 /* early packet - should move mbufs to ready buf and move sequence window
183 * RB[] = {0, 1, 2, 3}
184 * OB[] = {4, NULL, NULL, NULL}
186 ret = rte_reorder_insert(b, bufs[4]);
188 printf("%s:%d: Error inserting early packet with seqn: size\n",
195 /* early packet from current sequence window - full ready buffer */
196 *rte_reorder_seqn(bufs[5]) = 2 * size;
197 ret = rte_reorder_insert(b, bufs[5]);
198 if (!((ret == -1) && (rte_errno == ENOSPC))) {
199 printf("%s:%d: No error inserting early packet with full ready buffer\n",
207 *rte_reorder_seqn(bufs[6]) = 3 * size;
208 ret = rte_reorder_insert(b, bufs[6]);
209 if (!((ret == -1) && (rte_errno == ERANGE))) {
210 printf("%s:%d: No error inserting late packet with seqn:"
211 " 3 * size\n", __func__, __LINE__);
220 for (i = 0; i < num_bufs; i++) {
222 rte_pktmbuf_free(bufs[i]);
228 test_reorder_drain(void)
230 struct rte_reorder_buffer *b = NULL;
231 struct rte_mempool *p = test_params->p;
232 const unsigned int size = 4;
233 const unsigned int num_bufs = 8;
234 struct rte_mbuf *bufs[num_bufs];
235 struct rte_mbuf *robufs[num_bufs];
239 /* initialize all robufs to NULL */
240 for (i = 0; i < num_bufs; i++)
243 /* This would create a reorder buffer instance consisting of:
245 * ready_buf: RB[size] = {NULL, NULL, NULL, NULL}
246 * order_buf: OB[size] = {NULL, NULL, NULL, NULL}
248 b = rte_reorder_create("test_drain", rte_socket_id(), size);
249 TEST_ASSERT_NOT_NULL(b, "Failed to create reorder buffer");
251 /* Check no drained packets if reorder is empty */
252 cnt = rte_reorder_drain(b, robufs, 1);
254 printf("%s:%d: drained packets from empty reorder buffer\n",
260 for (i = 0; i < num_bufs; i++) {
261 bufs[i] = rte_pktmbuf_alloc(p);
262 TEST_ASSERT_NOT_NULL(bufs[i], "Packet allocation failed\n");
263 *rte_reorder_seqn(bufs[i]) = i;
266 /* Insert packet with seqn 1:
268 * RB[] = {NULL, NULL, NULL, NULL}
269 * OB[] = {1, NULL, NULL, NULL}
271 rte_reorder_insert(b, bufs[1]);
274 cnt = rte_reorder_drain(b, robufs, 1);
276 printf("%s:%d:%d: number of expected packets not drained\n",
277 __func__, __LINE__, cnt);
281 if (robufs[0] != NULL)
282 rte_pktmbuf_free(robufs[0]);
284 /* Insert more packets
285 * RB[] = {NULL, NULL, NULL, NULL}
286 * OB[] = {NULL, 2, 3, NULL}
288 rte_reorder_insert(b, bufs[2]);
289 rte_reorder_insert(b, bufs[3]);
293 /* Insert more packets
294 * RB[] = {NULL, NULL, NULL, NULL}
295 * OB[] = {NULL, 2, 3, 4}
297 rte_reorder_insert(b, bufs[4]);
300 /* Insert more packets
301 * RB[] = {2, 3, 4, NULL}
302 * OB[] = {NULL, NULL, 7, NULL}
304 rte_reorder_insert(b, bufs[7]);
307 /* drained expected packets */
308 cnt = rte_reorder_drain(b, robufs, 4);
310 printf("%s:%d:%d: number of expected packets not drained\n",
311 __func__, __LINE__, cnt);
315 for (i = 0; i < 3; i++) {
316 if (robufs[i] != NULL)
317 rte_pktmbuf_free(robufs[i]);
321 * RB[] = {NULL, NULL, NULL, NULL}
322 * OB[] = {NULL, NULL, 7, NULL}
324 cnt = rte_reorder_drain(b, robufs, 1);
326 printf("%s:%d:%d: number of expected packets not drained\n",
327 __func__, __LINE__, cnt);
334 for (i = 0; i < num_bufs; i++) {
336 rte_pktmbuf_free(bufs[i]);
337 if (robufs[i] != NULL)
338 rte_pktmbuf_free(robufs[i]);
346 /* reorder buffer instance creation */
347 if (test_params->b == NULL) {
348 test_params->b = rte_reorder_create("PKT_RO1", rte_socket_id(),
349 REORDER_BUFFER_SIZE);
350 if (test_params->b == NULL) {
351 printf("%s: Error creating reorder buffer instance b\n",
356 rte_reorder_reset(test_params->b);
358 /* mempool creation */
359 if (test_params->p == NULL) {
360 test_params->p = rte_pktmbuf_pool_create("RO_MBUF_POOL",
361 NUM_MBUFS, BURST, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
363 if (test_params->p == NULL) {
364 printf("%s: Error creating mempool\n", __func__);
374 rte_reorder_free(test_params->b);
375 test_params->b = NULL;
376 rte_mempool_free(test_params->p);
377 test_params->p = NULL;
381 static struct unit_test_suite reorder_test_suite = {
384 .teardown = test_teardown,
385 .suite_name = "Reorder Unit Test Suite",
387 TEST_CASE(test_reorder_create),
388 TEST_CASE(test_reorder_init),
389 TEST_CASE(test_reorder_find_existing),
390 TEST_CASE(test_reorder_free),
391 TEST_CASE(test_reorder_insert),
392 TEST_CASE(test_reorder_drain),
400 return unit_test_suite_runner(&reorder_test_suite);
403 #endif /* !RTE_EXEC_ENV_WINDOWS */
405 REGISTER_TEST_COMMAND(reorder_autotest, test_reorder);