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.
40 #include <rte_cycles.h>
41 #include <rte_errno.h>
43 #include <rte_reorder.h>
44 #include <rte_lcore.h>
45 #include <rte_malloc.h>
50 #define REORDER_BUFFER_SIZE 16384
51 #define NUM_MBUFS (2*REORDER_BUFFER_SIZE)
52 #define REORDER_BUFFER_SIZE_INVALID 2049
54 struct reorder_unittest_params {
55 struct rte_mempool *p;
56 struct rte_reorder_buffer *b;
59 static struct reorder_unittest_params default_params = {
64 static struct reorder_unittest_params *test_params = &default_params;
67 test_reorder_create(void)
69 struct rte_reorder_buffer *b = NULL;
71 b = rte_reorder_create(NULL, rte_socket_id(), REORDER_BUFFER_SIZE);
72 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
73 "No error on create() with NULL name");
75 b = rte_reorder_create("PKT", rte_socket_id(), REORDER_BUFFER_SIZE_INVALID);
76 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
77 "No error on create() with invalid buffer size param.");
79 b = rte_reorder_create("PKT_RO1", rte_socket_id(), REORDER_BUFFER_SIZE);
80 TEST_ASSERT_EQUAL(b, test_params->b,
81 "New reorder instance created with already existing name");
87 test_reorder_init(void)
89 struct rte_reorder_buffer *b = NULL;
92 * The minimum memory area size that should be passed to library is,
93 * sizeof(struct rte_reorder_buffer) + (2 * size * sizeof(struct rte_mbuf *));
94 * Otherwise error will be thrown
98 b = rte_reorder_init(b, size, "PKT1", REORDER_BUFFER_SIZE);
99 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
100 "No error on init with NULL buffer.");
102 b = rte_malloc(NULL, size, 0);
103 b = rte_reorder_init(b, size, "PKT1", REORDER_BUFFER_SIZE);
104 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
105 "No error on init with invalid mem zone size.");
109 b = rte_malloc(NULL, size, 0);
110 b = rte_reorder_init(b, size, "PKT1", REORDER_BUFFER_SIZE_INVALID);
111 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
112 "No error on init with invalid buffer size param.");
114 b = rte_reorder_init(b, size, NULL, REORDER_BUFFER_SIZE);
115 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
116 "No error on init with invalid name.");
123 test_reorder_find_existing(void)
125 struct rte_reorder_buffer *b = NULL;
127 /* Try to find existing reorder buffer instance */
128 b = rte_reorder_find_existing("PKT_RO1");
129 TEST_ASSERT_EQUAL(b, test_params->b,
130 "existing reorder buffer instance not found");
132 /* Try to find non existing reorder buffer instance */
133 b = rte_reorder_find_existing("ro_find_non_existing");
134 TEST_ASSERT((b == NULL) && (rte_errno == ENOENT),
135 "non existing reorder buffer instance found");
141 test_reorder_free(void)
143 struct rte_reorder_buffer *b1 = NULL, *b2 = NULL;
144 const char *name = "test_free";
146 b1 = rte_reorder_create(name, rte_socket_id(), 8);
147 TEST_ASSERT_NOT_NULL(b1, "Failed to create reorder buffer.");
149 b2 = rte_reorder_find_existing(name);
150 TEST_ASSERT_EQUAL(b1, b2, "Failed to find existing reorder buffer");
152 rte_reorder_free(b1);
154 b2 = rte_reorder_find_existing(name);
155 TEST_ASSERT((b2 == NULL) && (rte_errno == ENOENT),
156 "Found previously freed reorder buffer");
162 test_reorder_insert(void)
164 struct rte_reorder_buffer *b = NULL;
165 struct rte_mempool *p = test_params->p;
166 const unsigned int size = 4;
167 const unsigned int num_bufs = 7;
168 struct rte_mbuf *bufs[num_bufs];
172 /* This would create a reorder buffer instance consisting of:
174 * ready_buf: RB[size] = {NULL, NULL, NULL, NULL}
175 * order_buf: OB[size] = {NULL, NULL, NULL, NULL}
177 b = rte_reorder_create("test_insert", rte_socket_id(), size);
178 TEST_ASSERT_NOT_NULL(b, "Failed to create reorder buffer");
180 ret = rte_mempool_get_bulk(p, (void *)bufs, num_bufs);
181 TEST_ASSERT_SUCCESS(ret, "Error getting mbuf from pool");
183 for (i = 0; i < num_bufs; i++)
186 /* This should fill up order buffer:
188 * RB[] = {NULL, NULL, NULL, NULL}
189 * OB[] = {0, 1, 2, 3}
191 for (i = 0; i < size; i++) {
192 ret = rte_reorder_insert(b, bufs[i]);
194 printf("%s:%d: Error inserting packet with seqn less than size\n",
201 /* early packet - should move mbufs to ready buf and move sequence window
203 * RB[] = {0, 1, 2, 3}
204 * OB[] = {4, NULL, NULL, NULL}
206 ret = rte_reorder_insert(b, bufs[4]);
208 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",
225 bufs[6]->seqn = 3 * size;
226 ret = rte_reorder_insert(b, bufs[6]);
227 if (!((ret == -1) && (rte_errno == ERANGE))) {
228 printf("%s:%d: No error inserting late packet with seqn:"
229 " 3 * size\n", __func__, __LINE__);
236 rte_mempool_put_bulk(p, (void *)bufs, num_bufs);
242 test_reorder_drain(void)
244 struct rte_reorder_buffer *b = NULL;
245 struct rte_mempool *p = test_params->p;
246 const unsigned int size = 4;
247 const unsigned int num_bufs = 8;
248 struct rte_mbuf *bufs[num_bufs];
249 struct rte_mbuf *robufs[num_bufs];
253 /* This would create a reorder buffer instance consisting of:
255 * ready_buf: RB[size] = {NULL, NULL, NULL, NULL}
256 * order_buf: OB[size] = {NULL, NULL, NULL, NULL}
258 b = rte_reorder_create("test_drain", rte_socket_id(), size);
259 TEST_ASSERT_NOT_NULL(b, "Failed to create reorder buffer");
261 ret = rte_mempool_get_bulk(p, (void *)bufs, num_bufs);
262 TEST_ASSERT_SUCCESS(ret, "Error getting mbuf from pool");
264 /* Check no drained packets if reorder is empty */
265 cnt = rte_reorder_drain(b, robufs, 1);
267 printf("%s:%d: drained packets from empty reorder buffer\n",
273 for (i = 0; i < num_bufs; i++)
276 /* Insert packet with seqn 1:
278 * RB[] = {NULL, NULL, NULL, NULL}
279 * OB[] = {1, NULL, NULL, NULL}
281 rte_reorder_insert(b, bufs[1]);
283 cnt = rte_reorder_drain(b, robufs, 1);
285 printf("%s:%d:%d: number of expected packets not drained\n",
286 __func__, __LINE__, cnt);
291 /* Insert more packets
292 * RB[] = {NULL, NULL, NULL, NULL}
293 * OB[] = {NULL, 2, 3, NULL}
295 rte_reorder_insert(b, bufs[2]);
296 rte_reorder_insert(b, bufs[3]);
298 /* Insert more packets
299 * RB[] = {NULL, NULL, NULL, NULL}
300 * OB[] = {NULL, 2, 3, 4}
302 rte_reorder_insert(b, bufs[4]);
304 /* Insert more packets
305 * RB[] = {2, 3, 4, NULL}
306 * OB[] = {NULL, NULL, 7, NULL}
308 rte_reorder_insert(b, bufs[7]);
310 /* drained expected packets */
311 cnt = rte_reorder_drain(b, robufs, 4);
313 printf("%s:%d:%d: number of expected packets not drained\n",
314 __func__, __LINE__, cnt);
320 * RB[] = {NULL, NULL, NULL, NULL}
321 * OB[] = {NULL, NULL, 7, NULL}
323 cnt = rte_reorder_drain(b, robufs, 1);
325 printf("%s:%d:%d: number of expected packets not drained\n",
326 __func__, __LINE__, cnt);
332 rte_mempool_put_bulk(p, (void *)bufs, num_bufs);
340 /* reorder buffer instance creation */
341 if (test_params->b == NULL) {
342 test_params->b = rte_reorder_create("PKT_RO1", rte_socket_id(),
343 REORDER_BUFFER_SIZE);
344 if (test_params->b == NULL) {
345 printf("%s: Error creating reorder buffer instance b\n",
350 rte_reorder_reset(test_params->b);
352 /* mempool creation */
353 if (test_params->p == NULL) {
354 test_params->p = rte_pktmbuf_pool_create("RO_MBUF_POOL",
355 NUM_MBUFS, BURST, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
357 if (test_params->p == NULL) {
358 printf("%s: Error creating mempool\n", __func__);
365 static struct unit_test_suite reorder_test_suite = {
368 .suite_name = "Reorder Unit Test Suite",
370 TEST_CASE(test_reorder_create),
371 TEST_CASE(test_reorder_init),
372 TEST_CASE(test_reorder_find_existing),
373 TEST_CASE(test_reorder_free),
374 TEST_CASE(test_reorder_insert),
375 TEST_CASE(test_reorder_drain),
383 return unit_test_suite_runner(&reorder_test_suite);
386 static struct test_command reorder_cmd = {
387 .command = "reorder_autotest",
388 .callback = test_reorder,
390 REGISTER_TEST_COMMAND(reorder_cmd);