1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
9 #include <rte_cycles.h>
10 #include <rte_errno.h>
12 #include <rte_reorder.h>
13 #include <rte_lcore.h>
14 #include <rte_malloc.h>
19 #define REORDER_BUFFER_SIZE 16384
20 #define NUM_MBUFS (2*REORDER_BUFFER_SIZE)
21 #define REORDER_BUFFER_SIZE_INVALID 2049
23 struct reorder_unittest_params {
24 struct rte_mempool *p;
25 struct rte_reorder_buffer *b;
28 static struct reorder_unittest_params default_params = {
33 static struct reorder_unittest_params *test_params = &default_params;
36 test_reorder_create(void)
38 struct rte_reorder_buffer *b = NULL;
40 b = rte_reorder_create(NULL, rte_socket_id(), REORDER_BUFFER_SIZE);
41 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
42 "No error on create() with NULL name");
44 b = rte_reorder_create("PKT", rte_socket_id(), REORDER_BUFFER_SIZE_INVALID);
45 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
46 "No error on create() with invalid buffer size param.");
48 b = rte_reorder_create("PKT_RO1", rte_socket_id(), REORDER_BUFFER_SIZE);
49 TEST_ASSERT_EQUAL(b, test_params->b,
50 "New reorder instance created with already existing name");
56 test_reorder_init(void)
58 struct rte_reorder_buffer *b = NULL;
61 * The minimum memory area size that should be passed to library is,
62 * sizeof(struct rte_reorder_buffer) + (2 * size * sizeof(struct rte_mbuf *));
63 * Otherwise error will be thrown
67 b = rte_reorder_init(b, size, "PKT1", REORDER_BUFFER_SIZE);
68 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
69 "No error on init with NULL buffer.");
71 b = rte_malloc(NULL, size, 0);
72 b = rte_reorder_init(b, size, "PKT1", REORDER_BUFFER_SIZE);
73 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
74 "No error on init with invalid mem zone size.");
78 b = rte_malloc(NULL, size, 0);
79 b = rte_reorder_init(b, size, "PKT1", REORDER_BUFFER_SIZE_INVALID);
80 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
81 "No error on init with invalid buffer size param.");
83 b = rte_reorder_init(b, size, NULL, REORDER_BUFFER_SIZE);
84 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
85 "No error on init with invalid name.");
92 test_reorder_find_existing(void)
94 struct rte_reorder_buffer *b = NULL;
96 /* Try to find existing reorder buffer instance */
97 b = rte_reorder_find_existing("PKT_RO1");
98 TEST_ASSERT_EQUAL(b, test_params->b,
99 "existing reorder buffer instance not found");
101 /* Try to find non existing reorder buffer instance */
102 b = rte_reorder_find_existing("ro_find_non_existing");
103 TEST_ASSERT((b == NULL) && (rte_errno == ENOENT),
104 "non existing reorder buffer instance found");
110 test_reorder_free(void)
112 struct rte_reorder_buffer *b1 = NULL, *b2 = NULL;
113 const char *name = "test_free";
115 b1 = rte_reorder_create(name, rte_socket_id(), 8);
116 TEST_ASSERT_NOT_NULL(b1, "Failed to create reorder buffer.");
118 b2 = rte_reorder_find_existing(name);
119 TEST_ASSERT_EQUAL(b1, b2, "Failed to find existing reorder buffer");
121 rte_reorder_free(b1);
123 b2 = rte_reorder_find_existing(name);
124 TEST_ASSERT((b2 == NULL) && (rte_errno == ENOENT),
125 "Found previously freed reorder buffer");
131 test_reorder_insert(void)
133 struct rte_reorder_buffer *b = NULL;
134 struct rte_mempool *p = test_params->p;
135 const unsigned int size = 4;
136 const unsigned int num_bufs = 7;
137 struct rte_mbuf *bufs[num_bufs];
141 /* This would create a reorder buffer instance consisting of:
143 * ready_buf: RB[size] = {NULL, NULL, NULL, NULL}
144 * order_buf: OB[size] = {NULL, NULL, NULL, NULL}
146 b = rte_reorder_create("test_insert", rte_socket_id(), size);
147 TEST_ASSERT_NOT_NULL(b, "Failed to create reorder buffer");
149 for (i = 0; i < num_bufs; i++) {
150 bufs[i] = rte_pktmbuf_alloc(p);
151 TEST_ASSERT_NOT_NULL(bufs[i], "Packet allocation failed\n");
155 /* This should fill up order buffer:
157 * RB[] = {NULL, NULL, NULL, NULL}
158 * OB[] = {0, 1, 2, 3}
160 for (i = 0; i < size; i++) {
161 ret = rte_reorder_insert(b, bufs[i]);
163 printf("%s:%d: Error inserting packet with seqn less than size\n",
171 /* early packet - should move mbufs to ready buf and move sequence window
173 * RB[] = {0, 1, 2, 3}
174 * OB[] = {4, NULL, NULL, NULL}
176 ret = rte_reorder_insert(b, bufs[4]);
178 printf("%s:%d: Error inserting early packet with seqn: size\n",
185 /* early packet from current sequence window - full ready buffer */
186 bufs[5]->seqn = 2 * size;
187 ret = rte_reorder_insert(b, bufs[5]);
188 if (!((ret == -1) && (rte_errno == ENOSPC))) {
189 printf("%s:%d: No error inserting early packet with full ready buffer\n",
197 bufs[6]->seqn = 3 * size;
198 ret = rte_reorder_insert(b, bufs[6]);
199 if (!((ret == -1) && (rte_errno == ERANGE))) {
200 printf("%s:%d: No error inserting late packet with seqn:"
201 " 3 * size\n", __func__, __LINE__);
210 for (i = 0; i < num_bufs; i++) {
212 rte_pktmbuf_free(bufs[i]);
218 test_reorder_drain(void)
220 struct rte_reorder_buffer *b = NULL;
221 struct rte_mempool *p = test_params->p;
222 const unsigned int size = 4;
223 const unsigned int num_bufs = 8;
224 struct rte_mbuf *bufs[num_bufs];
225 struct rte_mbuf *robufs[num_bufs];
229 /* initialize all robufs to NULL */
230 for (i = 0; i < num_bufs; i++)
233 /* This would create a reorder buffer instance consisting of:
235 * ready_buf: RB[size] = {NULL, NULL, NULL, NULL}
236 * order_buf: OB[size] = {NULL, NULL, NULL, NULL}
238 b = rte_reorder_create("test_drain", rte_socket_id(), size);
239 TEST_ASSERT_NOT_NULL(b, "Failed to create reorder buffer");
241 /* Check no drained packets if reorder is empty */
242 cnt = rte_reorder_drain(b, robufs, 1);
244 printf("%s:%d: drained packets from empty reorder buffer\n",
250 for (i = 0; i < num_bufs; i++) {
251 bufs[i] = rte_pktmbuf_alloc(p);
252 TEST_ASSERT_NOT_NULL(bufs[i], "Packet allocation failed\n");
256 /* Insert packet with seqn 1:
258 * RB[] = {NULL, NULL, NULL, NULL}
259 * OB[] = {1, NULL, NULL, NULL}
261 rte_reorder_insert(b, bufs[1]);
264 cnt = rte_reorder_drain(b, robufs, 1);
266 printf("%s:%d:%d: number of expected packets not drained\n",
267 __func__, __LINE__, cnt);
271 if (robufs[0] != NULL)
272 rte_pktmbuf_free(robufs[0]);
274 /* Insert more packets
275 * RB[] = {NULL, NULL, NULL, NULL}
276 * OB[] = {NULL, 2, 3, NULL}
278 rte_reorder_insert(b, bufs[2]);
279 rte_reorder_insert(b, bufs[3]);
283 /* Insert more packets
284 * RB[] = {NULL, NULL, NULL, NULL}
285 * OB[] = {NULL, 2, 3, 4}
287 rte_reorder_insert(b, bufs[4]);
290 /* Insert more packets
291 * RB[] = {2, 3, 4, NULL}
292 * OB[] = {NULL, NULL, 7, NULL}
294 rte_reorder_insert(b, bufs[7]);
297 /* drained expected packets */
298 cnt = rte_reorder_drain(b, robufs, 4);
300 printf("%s:%d:%d: number of expected packets not drained\n",
301 __func__, __LINE__, cnt);
305 for (i = 0; i < 3; i++) {
306 if (robufs[i] != NULL)
307 rte_pktmbuf_free(robufs[i]);
311 * RB[] = {NULL, NULL, NULL, NULL}
312 * OB[] = {NULL, NULL, 7, NULL}
314 cnt = rte_reorder_drain(b, robufs, 1);
316 printf("%s:%d:%d: number of expected packets not drained\n",
317 __func__, __LINE__, cnt);
324 for (i = 0; i < num_bufs; i++) {
326 rte_pktmbuf_free(bufs[i]);
327 if (robufs[i] != NULL)
328 rte_pktmbuf_free(robufs[i]);
336 /* reorder buffer instance creation */
337 if (test_params->b == NULL) {
338 test_params->b = rte_reorder_create("PKT_RO1", rte_socket_id(),
339 REORDER_BUFFER_SIZE);
340 if (test_params->b == NULL) {
341 printf("%s: Error creating reorder buffer instance b\n",
346 rte_reorder_reset(test_params->b);
348 /* mempool creation */
349 if (test_params->p == NULL) {
350 test_params->p = rte_pktmbuf_pool_create("RO_MBUF_POOL",
351 NUM_MBUFS, BURST, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
353 if (test_params->p == NULL) {
354 printf("%s: Error creating mempool\n", __func__);
364 rte_reorder_free(test_params->b);
365 test_params->b = NULL;
366 rte_mempool_free(test_params->p);
367 test_params->p = NULL;
371 static struct unit_test_suite reorder_test_suite = {
374 .teardown = test_teardown,
375 .suite_name = "Reorder Unit Test Suite",
377 TEST_CASE(test_reorder_create),
378 TEST_CASE(test_reorder_init),
379 TEST_CASE(test_reorder_find_existing),
380 TEST_CASE(test_reorder_free),
381 TEST_CASE(test_reorder_insert),
382 TEST_CASE(test_reorder_drain),
390 return unit_test_suite_runner(&reorder_test_suite);
393 REGISTER_TEST_COMMAND(reorder_autotest, test_reorder);