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
53 #define MBUF_SIZE (2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
55 struct reorder_unittest_params {
56 struct rte_mempool *p;
57 struct rte_reorder_buffer *b;
60 static struct reorder_unittest_params default_params = {
65 static struct reorder_unittest_params *test_params = &default_params;
68 test_reorder_create(void)
70 struct rte_reorder_buffer *b = NULL;
72 b = rte_reorder_create(NULL, rte_socket_id(), REORDER_BUFFER_SIZE);
73 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
74 "No error on create() with NULL name");
76 b = rte_reorder_create("PKT", rte_socket_id(), REORDER_BUFFER_SIZE_INVALID);
77 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
78 "No error on create() with invalid buffer size param.");
80 b = rte_reorder_create("PKT_RO1", rte_socket_id(), REORDER_BUFFER_SIZE);
81 printf("DEBUG: b= %p, orig_b= %p\n", b, test_params->b);
82 TEST_ASSERT_EQUAL(b, test_params->b,
83 "New reorder instance created with already existing name");
89 test_reorder_init(void)
91 struct rte_reorder_buffer *b = NULL;
94 * The minimum memory area size that should be passed to library is,
95 * sizeof(struct rte_reorder_buffer) + (2 * size * sizeof(struct rte_mbuf *));
96 * Otherwise error will be thrown
100 b = rte_reorder_init(b, size, "PKT1", REORDER_BUFFER_SIZE);
101 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
102 "No error on init with NULL buffer.");
104 b = rte_malloc(NULL, size, 0);
105 b = rte_reorder_init(b, size, "PKT1", REORDER_BUFFER_SIZE);
106 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
107 "No error on init with invalid mem zone size.");
111 b = rte_malloc(NULL, size, 0);
112 b = rte_reorder_init(b, size, "PKT1", REORDER_BUFFER_SIZE_INVALID);
113 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
114 "No error on init with invalid buffer size param.");
116 b = rte_reorder_init(b, size, NULL, REORDER_BUFFER_SIZE);
117 TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
118 "No error on init with invalid name.");
125 test_reorder_find_existing(void)
127 struct rte_reorder_buffer *b = NULL;
129 /* Try to find existing reorder buffer instance */
130 b = rte_reorder_find_existing("PKT_RO1");
131 TEST_ASSERT_EQUAL(b, test_params->b,
132 "existing reorder buffer instance not found");
134 /* Try to find non existing reorder buffer instance */
135 b = rte_reorder_find_existing("ro_find_non_existing");
136 TEST_ASSERT((b == NULL) && (rte_errno == ENOENT),
137 "non existing reorder buffer instance found");
143 test_reorder_free(void)
145 struct rte_reorder_buffer *b1 = NULL, *b2 = NULL;
146 const char *name = "test_free";
148 b1 = rte_reorder_create(name, rte_socket_id(), 8);
149 TEST_ASSERT_NOT_NULL(b1, "Failed to create reorder buffer.");
151 b2 = rte_reorder_find_existing(name);
152 TEST_ASSERT_EQUAL(b1, b2, "Failed to find existing reorder buffer");
154 rte_reorder_free(b1);
156 b2 = rte_reorder_find_existing(name);
157 TEST_ASSERT((b2 == NULL) && (rte_errno == ENOENT),
158 "Found previously freed reorder buffer");
164 test_reorder_insert(void)
166 struct rte_reorder_buffer *b = NULL;
167 struct rte_mempool *p = test_params->p;
168 const unsigned int size = 4;
169 const unsigned int num_bufs = 6;
170 struct rte_mbuf *bufs[num_bufs];
174 /* This would create a reorder buffer instance consisting of:
176 * ready_buf: RB[size] = {NULL, NULL, NULL, NULL}
177 * order_buf: OB[size] = {NULL, NULL, NULL, NULL}
179 b = rte_reorder_create("test_insert", rte_socket_id(), size);
180 TEST_ASSERT_NOT_NULL(b, "Failed to create reorder buffer");
182 ret = rte_mempool_get_bulk(p, (void *)bufs, num_bufs);
183 TEST_ASSERT_SUCCESS(ret, "Error getting mbuf from pool");
186 bufs[0]->seqn = 3 * size;
187 ret = rte_reorder_insert(b, bufs[0]);
188 if (!((ret == -1) && (rte_errno == ERANGE))) {
189 printf("%s:%d: No error inserting late packet with seqn:"
190 " 3 * size\n", __func__, __LINE__);
195 for (i = 0; i < num_bufs; i++)
198 /* This should fill up order buffer:
200 * RB[] = {NULL, NULL, NULL, NULL}
201 * OB[] = {0, 1, 2, 3}
203 for (i = 0; i < size; i++) {
204 ret = rte_reorder_insert(b, bufs[i]);
206 printf("%s:%d: Error inserting packet with seqn less than size\n",
213 /* early packet - should move mbufs to ready buf and move sequence window
215 * RB[] = {0, 1, 2, 3}
216 * OB[] = {4, NULL, NULL, NULL}
218 ret = rte_reorder_insert(b, bufs[4]);
220 printf("%s:%d: Error inserting early packet with seqn: size\n",
226 /* early packet from current sequence window - full ready buffer */
227 bufs[5]->seqn = 2 * size;
228 ret = rte_reorder_insert(b, bufs[5]);
229 if (!((ret == -1) && (rte_errno == ENOSPC))) {
230 printf("%s:%d: No error inserting early packet with full ready buffer\n",
238 rte_mempool_put_bulk(p, (void *)bufs, num_bufs);
244 test_reorder_drain(void)
246 struct rte_reorder_buffer *b = NULL;
247 struct rte_mempool *p = test_params->p;
248 const unsigned int size = 4;
249 const unsigned int num_bufs = 10;
250 struct rte_mbuf *bufs[num_bufs];
254 /* This would create a reorder buffer instance consisting of:
256 * ready_buf: RB[size] = {NULL, NULL, NULL, NULL}
257 * order_buf: OB[size] = {NULL, NULL, NULL, NULL}
259 b = rte_reorder_create("test_insert", rte_socket_id(), size);
260 TEST_ASSERT_NOT_NULL(b, "Failed to create reorder buffer");
262 ret = rte_mempool_get_bulk(p, (void *)bufs, num_bufs);
263 TEST_ASSERT_SUCCESS(ret, "Error getting mbuf from pool");
265 /* Check no drained packets if reorder is empty */
266 cnt = rte_reorder_drain(b, bufs, 1);
268 printf("%s:%d: drained packets from empty reorder buffer\n",
274 for (i = 0; i < num_bufs; i++)
277 /* Insert packet with seqn 1:
279 * RB[] = {NULL, NULL, NULL, NULL}
280 * OB[] = {NULL, 1, NULL, NULL}
282 rte_reorder_insert(b, bufs[1]);
284 /* Check no drained packets if no ready/order packets */
285 cnt = rte_reorder_drain(b, bufs, 1);
287 printf("%s:%d: drained packets from empty reorder buffer\n",
293 /* Insert more packets
294 * RB[] = {NULL, NULL, NULL, NULL}
295 * OB[] = {0, 1, NULL, 3}
297 rte_reorder_insert(b, bufs[0]);
298 rte_reorder_insert(b, bufs[3]);
300 /* drained expected packets */
301 cnt = rte_reorder_drain(b, bufs, 4);
303 printf("%s:%d:%d: number of expected packets not drained\n",
304 __func__, __LINE__, cnt);
310 * RB[] = {NULL, NULL, NULL, NULL}
311 * OB[] = {NULL, 3, NULL, NULL}
314 rte_reorder_insert(b, bufs[4]);
315 rte_reorder_insert(b, bufs[7]);
318 * RB[] = {3, 4, NULL, NULL}
319 * OB[] = {NULL, NULL, 7, NULL}
322 cnt = rte_reorder_drain(b, bufs, 4);
324 printf("%s:%d:%d: number of expected packets not drained\n",
325 __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_mempool_create("RO_MBUF_POOL", NUM_MBUFS,
356 sizeof(struct rte_pktmbuf_pool_private),
357 rte_pktmbuf_pool_init, NULL,
358 rte_pktmbuf_init, NULL,
360 if (test_params->p == NULL) {
361 printf("%s: Error creating mempool\n", __func__);
368 static struct unit_test_suite reorder_test_suite = {
371 .suite_name = "Reorder Unit Test Suite",
373 TEST_CASE(test_reorder_create),
374 TEST_CASE(test_reorder_init),
375 TEST_CASE(test_reorder_find_existing),
376 TEST_CASE(test_reorder_free),
377 TEST_CASE(test_reorder_insert),
378 TEST_CASE(test_reorder_drain),
386 return unit_test_suite_runner(&reorder_test_suite);
389 static struct test_command reorder_cmd = {
390 .command = "reorder_autotest",
391 .callback = test_reorder,
393 REGISTER_TEST_COMMAND(reorder_cmd);