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 ret = rte_mempool_get_bulk(p, (void *)bufs, num_bufs);
179 TEST_ASSERT_SUCCESS(ret, "Error getting mbuf from pool");
181 for (i = 0; i < num_bufs; i++)
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",
199 /* early packet - should move mbufs to ready buf and move sequence window
201 * RB[] = {0, 1, 2, 3}
202 * OB[] = {4, NULL, NULL, NULL}
204 ret = rte_reorder_insert(b, bufs[4]);
206 printf("%s:%d: Error inserting early packet with seqn: size\n",
212 /* early packet from current sequence window - full ready buffer */
213 bufs[5]->seqn = 2 * size;
214 ret = rte_reorder_insert(b, bufs[5]);
215 if (!((ret == -1) && (rte_errno == ENOSPC))) {
216 printf("%s:%d: No error inserting early packet with full ready buffer\n",
223 bufs[6]->seqn = 3 * size;
224 ret = rte_reorder_insert(b, bufs[6]);
225 if (!((ret == -1) && (rte_errno == ERANGE))) {
226 printf("%s:%d: No error inserting late packet with seqn:"
227 " 3 * size\n", __func__, __LINE__);
234 rte_mempool_put_bulk(p, (void *)bufs, num_bufs);
240 test_reorder_drain(void)
242 struct rte_reorder_buffer *b = NULL;
243 struct rte_mempool *p = test_params->p;
244 const unsigned int size = 4;
245 const unsigned int num_bufs = 8;
246 struct rte_mbuf *bufs[num_bufs];
247 struct rte_mbuf *robufs[num_bufs];
251 /* This would create a reorder buffer instance consisting of:
253 * ready_buf: RB[size] = {NULL, NULL, NULL, NULL}
254 * order_buf: OB[size] = {NULL, NULL, NULL, NULL}
256 b = rte_reorder_create("test_drain", rte_socket_id(), size);
257 TEST_ASSERT_NOT_NULL(b, "Failed to create reorder buffer");
259 ret = rte_mempool_get_bulk(p, (void *)bufs, num_bufs);
260 TEST_ASSERT_SUCCESS(ret, "Error getting mbuf from pool");
262 /* Check no drained packets if reorder is empty */
263 cnt = rte_reorder_drain(b, robufs, 1);
265 printf("%s:%d: drained packets from empty reorder buffer\n",
271 for (i = 0; i < num_bufs; i++)
274 /* Insert packet with seqn 1:
276 * RB[] = {NULL, NULL, NULL, NULL}
277 * OB[] = {1, NULL, NULL, NULL}
279 rte_reorder_insert(b, bufs[1]);
281 cnt = rte_reorder_drain(b, robufs, 1);
283 printf("%s:%d:%d: number of expected packets not drained\n",
284 __func__, __LINE__, cnt);
289 /* Insert more packets
290 * RB[] = {NULL, NULL, NULL, NULL}
291 * OB[] = {NULL, 2, 3, NULL}
293 rte_reorder_insert(b, bufs[2]);
294 rte_reorder_insert(b, bufs[3]);
296 /* Insert more packets
297 * RB[] = {NULL, NULL, NULL, NULL}
298 * OB[] = {NULL, 2, 3, 4}
300 rte_reorder_insert(b, bufs[4]);
302 /* Insert more packets
303 * RB[] = {2, 3, 4, NULL}
304 * OB[] = {NULL, NULL, 7, NULL}
306 rte_reorder_insert(b, bufs[7]);
308 /* drained expected packets */
309 cnt = rte_reorder_drain(b, robufs, 4);
311 printf("%s:%d:%d: number of expected packets not drained\n",
312 __func__, __LINE__, cnt);
318 * RB[] = {NULL, NULL, NULL, NULL}
319 * OB[] = {NULL, NULL, 7, NULL}
321 cnt = rte_reorder_drain(b, robufs, 1);
323 printf("%s:%d:%d: number of expected packets not drained\n",
324 __func__, __LINE__, cnt);
330 rte_mempool_put_bulk(p, (void *)bufs, num_bufs);
338 /* reorder buffer instance creation */
339 if (test_params->b == NULL) {
340 test_params->b = rte_reorder_create("PKT_RO1", rte_socket_id(),
341 REORDER_BUFFER_SIZE);
342 if (test_params->b == NULL) {
343 printf("%s: Error creating reorder buffer instance b\n",
348 rte_reorder_reset(test_params->b);
350 /* mempool creation */
351 if (test_params->p == NULL) {
352 test_params->p = rte_pktmbuf_pool_create("RO_MBUF_POOL",
353 NUM_MBUFS, BURST, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
355 if (test_params->p == NULL) {
356 printf("%s: Error creating mempool\n", __func__);
363 static struct unit_test_suite reorder_test_suite = {
366 .suite_name = "Reorder Unit Test Suite",
368 TEST_CASE(test_reorder_create),
369 TEST_CASE(test_reorder_init),
370 TEST_CASE(test_reorder_find_existing),
371 TEST_CASE(test_reorder_free),
372 TEST_CASE(test_reorder_insert),
373 TEST_CASE(test_reorder_drain),
381 return unit_test_suite_runner(&reorder_test_suite);
384 REGISTER_TEST_COMMAND(reorder_autotest, test_reorder);