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 for (i = 0; i < num_bufs; i++) {
181 bufs[i] = rte_pktmbuf_alloc(p);
182 TEST_ASSERT_NOT_NULL(bufs[i], "Packet allocation failed\n");
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",
202 /* early packet - should move mbufs to ready buf and move sequence window
204 * RB[] = {0, 1, 2, 3}
205 * OB[] = {4, NULL, NULL, NULL}
207 ret = rte_reorder_insert(b, bufs[4]);
209 printf("%s:%d: Error inserting early packet with seqn: size\n",
216 /* early packet from current sequence window - full ready buffer */
217 bufs[5]->seqn = 2 * size;
218 ret = rte_reorder_insert(b, bufs[5]);
219 if (!((ret == -1) && (rte_errno == ENOSPC))) {
220 printf("%s:%d: No error inserting early packet with full ready buffer\n",
228 bufs[6]->seqn = 3 * size;
229 ret = rte_reorder_insert(b, bufs[6]);
230 if (!((ret == -1) && (rte_errno == ERANGE))) {
231 printf("%s:%d: No error inserting late packet with seqn:"
232 " 3 * size\n", __func__, __LINE__);
241 for (i = 0; i < num_bufs; i++) {
243 rte_pktmbuf_free(bufs[i]);
249 test_reorder_drain(void)
251 struct rte_reorder_buffer *b = NULL;
252 struct rte_mempool *p = test_params->p;
253 const unsigned int size = 4;
254 const unsigned int num_bufs = 8;
255 struct rte_mbuf *bufs[num_bufs];
256 struct rte_mbuf *robufs[num_bufs];
260 /* initialize all robufs to NULL */
261 for (i = 0; i < num_bufs; i++)
264 /* This would create a reorder buffer instance consisting of:
266 * ready_buf: RB[size] = {NULL, NULL, NULL, NULL}
267 * order_buf: OB[size] = {NULL, NULL, NULL, NULL}
269 b = rte_reorder_create("test_drain", rte_socket_id(), size);
270 TEST_ASSERT_NOT_NULL(b, "Failed to create reorder buffer");
272 /* Check no drained packets if reorder is empty */
273 cnt = rte_reorder_drain(b, robufs, 1);
275 printf("%s:%d: drained packets from empty reorder buffer\n",
281 for (i = 0; i < num_bufs; i++) {
282 bufs[i] = rte_pktmbuf_alloc(p);
283 TEST_ASSERT_NOT_NULL(bufs[i], "Packet allocation failed\n");
287 /* Insert packet with seqn 1:
289 * RB[] = {NULL, NULL, NULL, NULL}
290 * OB[] = {1, NULL, NULL, NULL}
292 rte_reorder_insert(b, bufs[1]);
295 cnt = rte_reorder_drain(b, robufs, 1);
297 printf("%s:%d:%d: number of expected packets not drained\n",
298 __func__, __LINE__, cnt);
302 if (robufs[0] != NULL)
303 rte_pktmbuf_free(robufs[0]);
305 /* Insert more packets
306 * RB[] = {NULL, NULL, NULL, NULL}
307 * OB[] = {NULL, 2, 3, NULL}
309 rte_reorder_insert(b, bufs[2]);
310 rte_reorder_insert(b, bufs[3]);
314 /* Insert more packets
315 * RB[] = {NULL, NULL, NULL, NULL}
316 * OB[] = {NULL, 2, 3, 4}
318 rte_reorder_insert(b, bufs[4]);
321 /* Insert more packets
322 * RB[] = {2, 3, 4, NULL}
323 * OB[] = {NULL, NULL, 7, NULL}
325 rte_reorder_insert(b, bufs[7]);
328 /* drained expected packets */
329 cnt = rte_reorder_drain(b, robufs, 4);
331 printf("%s:%d:%d: number of expected packets not drained\n",
332 __func__, __LINE__, cnt);
336 for (i = 0; i < 3; i++) {
337 if (robufs[i] != NULL)
338 rte_pktmbuf_free(robufs[i]);
342 * RB[] = {NULL, NULL, NULL, NULL}
343 * OB[] = {NULL, NULL, 7, NULL}
345 cnt = rte_reorder_drain(b, robufs, 1);
347 printf("%s:%d:%d: number of expected packets not drained\n",
348 __func__, __LINE__, cnt);
355 for (i = 0; i < num_bufs; i++) {
357 rte_pktmbuf_free(bufs[i]);
358 if (robufs[i] != NULL)
359 rte_pktmbuf_free(robufs[i]);
367 /* reorder buffer instance creation */
368 if (test_params->b == NULL) {
369 test_params->b = rte_reorder_create("PKT_RO1", rte_socket_id(),
370 REORDER_BUFFER_SIZE);
371 if (test_params->b == NULL) {
372 printf("%s: Error creating reorder buffer instance b\n",
377 rte_reorder_reset(test_params->b);
379 /* mempool creation */
380 if (test_params->p == NULL) {
381 test_params->p = rte_pktmbuf_pool_create("RO_MBUF_POOL",
382 NUM_MBUFS, BURST, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
384 if (test_params->p == NULL) {
385 printf("%s: Error creating mempool\n", __func__);
395 rte_reorder_free(test_params->b);
396 test_params->b = NULL;
397 rte_mempool_free(test_params->p);
398 test_params->p = NULL;
402 static struct unit_test_suite reorder_test_suite = {
405 .teardown = test_teardown,
406 .suite_name = "Reorder Unit Test Suite",
408 TEST_CASE(test_reorder_create),
409 TEST_CASE(test_reorder_init),
410 TEST_CASE(test_reorder_find_existing),
411 TEST_CASE(test_reorder_free),
412 TEST_CASE(test_reorder_insert),
413 TEST_CASE(test_reorder_drain),
421 return unit_test_suite_runner(&reorder_test_suite);
424 REGISTER_TEST_COMMAND(reorder_autotest, test_reorder);