4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <rte_cycles.h>
39 #include <rte_errno.h>
41 #include <rte_reorder.h>
42 #include <rte_lcore.h>
43 #include <rte_malloc.h>
48 #define REORDER_BUFFER_SIZE 16384
49 #define NUM_MBUFS (2*REORDER_BUFFER_SIZE)
50 #define REORDER_BUFFER_SIZE_INVALID 2049
52 struct reorder_unittest_params {
53 struct rte_mempool *p;
54 struct rte_reorder_buffer *b;
57 static struct reorder_unittest_params default_params = {
62 static struct reorder_unittest_params *test_params = &default_params;
65 test_reorder_create(void)
67 struct rte_reorder_buffer *b = NULL;
69 b = rte_reorder_create(NULL, rte_socket_id(), REORDER_BUFFER_SIZE);
70 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
71 "No error on create() with NULL name");
73 b = rte_reorder_create("PKT", rte_socket_id(), REORDER_BUFFER_SIZE_INVALID);
74 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
75 "No error on create() with invalid buffer size param.");
77 b = rte_reorder_create("PKT_RO1", rte_socket_id(), REORDER_BUFFER_SIZE);
78 TEST_ASSERT_EQUAL(b, test_params->b,
79 "New reorder instance created with already existing name");
85 test_reorder_init(void)
87 struct rte_reorder_buffer *b = NULL;
90 * The minimum memory area size that should be passed to library is,
91 * sizeof(struct rte_reorder_buffer) + (2 * size * sizeof(struct rte_mbuf *));
92 * Otherwise error will be thrown
96 b = rte_reorder_init(b, size, "PKT1", REORDER_BUFFER_SIZE);
97 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
98 "No error on init with NULL buffer.");
100 b = rte_malloc(NULL, size, 0);
101 b = rte_reorder_init(b, size, "PKT1", REORDER_BUFFER_SIZE);
102 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
103 "No error on init with invalid mem zone size.");
107 b = rte_malloc(NULL, size, 0);
108 b = rte_reorder_init(b, size, "PKT1", REORDER_BUFFER_SIZE_INVALID);
109 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
110 "No error on init with invalid buffer size param.");
112 b = rte_reorder_init(b, size, NULL, REORDER_BUFFER_SIZE);
113 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
114 "No error on init with invalid name.");
121 test_reorder_find_existing(void)
123 struct rte_reorder_buffer *b = NULL;
125 /* Try to find existing reorder buffer instance */
126 b = rte_reorder_find_existing("PKT_RO1");
127 TEST_ASSERT_EQUAL(b, test_params->b,
128 "existing reorder buffer instance not found");
130 /* Try to find non existing reorder buffer instance */
131 b = rte_reorder_find_existing("ro_find_non_existing");
132 TEST_ASSERT((b == NULL) && (rte_errno == ENOENT),
133 "non existing reorder buffer instance found");
139 test_reorder_free(void)
141 struct rte_reorder_buffer *b1 = NULL, *b2 = NULL;
142 const char *name = "test_free";
144 b1 = rte_reorder_create(name, rte_socket_id(), 8);
145 TEST_ASSERT_NOT_NULL(b1, "Failed to create reorder buffer.");
147 b2 = rte_reorder_find_existing(name);
148 TEST_ASSERT_EQUAL(b1, b2, "Failed to find existing reorder buffer");
150 rte_reorder_free(b1);
152 b2 = rte_reorder_find_existing(name);
153 TEST_ASSERT((b2 == NULL) && (rte_errno == ENOENT),
154 "Found previously freed reorder buffer");
160 test_reorder_insert(void)
162 struct rte_reorder_buffer *b = NULL;
163 struct rte_mempool *p = test_params->p;
164 const unsigned int size = 4;
165 const unsigned int num_bufs = 7;
166 struct rte_mbuf *bufs[num_bufs];
170 /* This would create a reorder buffer instance consisting of:
172 * ready_buf: RB[size] = {NULL, NULL, NULL, NULL}
173 * order_buf: OB[size] = {NULL, NULL, NULL, NULL}
175 b = rte_reorder_create("test_insert", rte_socket_id(), size);
176 TEST_ASSERT_NOT_NULL(b, "Failed to create reorder buffer");
178 for (i = 0; i < num_bufs; i++) {
179 bufs[i] = rte_pktmbuf_alloc(p);
180 TEST_ASSERT_NOT_NULL(bufs[i], "Packet allocation failed\n");
184 /* This should fill up order buffer:
186 * RB[] = {NULL, NULL, NULL, NULL}
187 * OB[] = {0, 1, 2, 3}
189 for (i = 0; i < size; i++) {
190 ret = rte_reorder_insert(b, bufs[i]);
192 printf("%s:%d: Error inserting packet with seqn less than size\n",
200 /* early packet - should move mbufs to ready buf and move sequence window
202 * RB[] = {0, 1, 2, 3}
203 * OB[] = {4, NULL, NULL, NULL}
205 ret = rte_reorder_insert(b, bufs[4]);
207 printf("%s:%d: Error inserting early packet with seqn: size\n",
214 /* early packet from current sequence window - full ready buffer */
215 bufs[5]->seqn = 2 * size;
216 ret = rte_reorder_insert(b, bufs[5]);
217 if (!((ret == -1) && (rte_errno == ENOSPC))) {
218 printf("%s:%d: No error inserting early packet with full ready buffer\n",
226 bufs[6]->seqn = 3 * size;
227 ret = rte_reorder_insert(b, bufs[6]);
228 if (!((ret == -1) && (rte_errno == ERANGE))) {
229 printf("%s:%d: No error inserting late packet with seqn:"
230 " 3 * size\n", __func__, __LINE__);
239 for (i = 0; i < num_bufs; i++) {
241 rte_pktmbuf_free(bufs[i]);
247 test_reorder_drain(void)
249 struct rte_reorder_buffer *b = NULL;
250 struct rte_mempool *p = test_params->p;
251 const unsigned int size = 4;
252 const unsigned int num_bufs = 8;
253 struct rte_mbuf *bufs[num_bufs];
254 struct rte_mbuf *robufs[num_bufs];
258 /* initialize all robufs to NULL */
259 for (i = 0; i < num_bufs; i++)
262 /* This would create a reorder buffer instance consisting of:
264 * ready_buf: RB[size] = {NULL, NULL, NULL, NULL}
265 * order_buf: OB[size] = {NULL, NULL, NULL, NULL}
267 b = rte_reorder_create("test_drain", rte_socket_id(), size);
268 TEST_ASSERT_NOT_NULL(b, "Failed to create reorder buffer");
270 /* Check no drained packets if reorder is empty */
271 cnt = rte_reorder_drain(b, robufs, 1);
273 printf("%s:%d: drained packets from empty reorder buffer\n",
279 for (i = 0; i < num_bufs; i++) {
280 bufs[i] = rte_pktmbuf_alloc(p);
281 TEST_ASSERT_NOT_NULL(bufs[i], "Packet allocation failed\n");
285 /* Insert packet with seqn 1:
287 * RB[] = {NULL, NULL, NULL, NULL}
288 * OB[] = {1, NULL, NULL, NULL}
290 rte_reorder_insert(b, bufs[1]);
293 cnt = rte_reorder_drain(b, robufs, 1);
295 printf("%s:%d:%d: number of expected packets not drained\n",
296 __func__, __LINE__, cnt);
300 if (robufs[0] != NULL)
301 rte_pktmbuf_free(robufs[i]);
303 /* Insert more packets
304 * RB[] = {NULL, NULL, NULL, NULL}
305 * OB[] = {NULL, 2, 3, NULL}
307 rte_reorder_insert(b, bufs[2]);
308 rte_reorder_insert(b, bufs[3]);
312 /* Insert more packets
313 * RB[] = {NULL, NULL, NULL, NULL}
314 * OB[] = {NULL, 2, 3, 4}
316 rte_reorder_insert(b, bufs[4]);
319 /* Insert more packets
320 * RB[] = {2, 3, 4, NULL}
321 * OB[] = {NULL, NULL, 7, NULL}
323 rte_reorder_insert(b, bufs[7]);
326 /* drained expected packets */
327 cnt = rte_reorder_drain(b, robufs, 4);
329 printf("%s:%d:%d: number of expected packets not drained\n",
330 __func__, __LINE__, cnt);
334 for (i = 0; i < 3; i++) {
335 if (robufs[i] != NULL)
336 rte_pktmbuf_free(robufs[i]);
340 * RB[] = {NULL, NULL, NULL, NULL}
341 * OB[] = {NULL, NULL, 7, NULL}
343 cnt = rte_reorder_drain(b, robufs, 1);
345 printf("%s:%d:%d: number of expected packets not drained\n",
346 __func__, __LINE__, cnt);
353 for (i = 0; i < num_bufs; i++) {
355 rte_pktmbuf_free(bufs[i]);
356 if (robufs[i] != NULL)
357 rte_pktmbuf_free(robufs[i]);
365 /* reorder buffer instance creation */
366 if (test_params->b == NULL) {
367 test_params->b = rte_reorder_create("PKT_RO1", rte_socket_id(),
368 REORDER_BUFFER_SIZE);
369 if (test_params->b == NULL) {
370 printf("%s: Error creating reorder buffer instance b\n",
375 rte_reorder_reset(test_params->b);
377 /* mempool creation */
378 if (test_params->p == NULL) {
379 test_params->p = rte_pktmbuf_pool_create("RO_MBUF_POOL",
380 NUM_MBUFS, BURST, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
382 if (test_params->p == NULL) {
383 printf("%s: Error creating mempool\n", __func__);
393 rte_reorder_free(test_params->b);
394 test_params->b = NULL;
395 rte_mempool_free(test_params->p);
396 test_params->p = NULL;
400 static struct unit_test_suite reorder_test_suite = {
403 .teardown = test_teardown,
404 .suite_name = "Reorder Unit Test Suite",
406 TEST_CASE(test_reorder_create),
407 TEST_CASE(test_reorder_init),
408 TEST_CASE(test_reorder_find_existing),
409 TEST_CASE(test_reorder_free),
410 TEST_CASE(test_reorder_insert),
411 TEST_CASE(test_reorder_drain),
419 return unit_test_suite_runner(&reorder_test_suite);
422 REGISTER_TEST_COMMAND(reorder_autotest, test_reorder);