New upstream version 16.11.7
[deb_dpdk.git] / app / test / test_reorder.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include "test.h"
35 #include "stdio.h"
36
37 #include <unistd.h>
38 #include <string.h>
39
40 #include <rte_cycles.h>
41 #include <rte_errno.h>
42 #include <rte_mbuf.h>
43 #include <rte_reorder.h>
44 #include <rte_lcore.h>
45 #include <rte_malloc.h>
46
47 #include "test.h"
48
49 #define BURST 32
50 #define REORDER_BUFFER_SIZE 16384
51 #define NUM_MBUFS (2*REORDER_BUFFER_SIZE)
52 #define REORDER_BUFFER_SIZE_INVALID 2049
53
54 struct reorder_unittest_params {
55         struct rte_mempool *p;
56         struct rte_reorder_buffer *b;
57 };
58
59 static struct reorder_unittest_params default_params  = {
60         .p = NULL,
61         .b = NULL
62 };
63
64 static struct reorder_unittest_params *test_params = &default_params;
65
66 static int
67 test_reorder_create(void)
68 {
69         struct rte_reorder_buffer *b = NULL;
70
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");
74
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.");
78
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");
82
83         return 0;
84 }
85
86 static int
87 test_reorder_init(void)
88 {
89         struct rte_reorder_buffer *b = NULL;
90         unsigned int size;
91         /*
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
95          */
96
97         size = 100;
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.");
101
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.");
106         rte_free(b);
107
108         size = 262336;
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.");
113
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.");
117         rte_free(b);
118
119         return 0;
120 }
121
122 static int
123 test_reorder_find_existing(void)
124 {
125         struct rte_reorder_buffer *b = NULL;
126
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");
131
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");
136
137         return 0;
138 }
139
140 static int
141 test_reorder_free(void)
142 {
143         struct rte_reorder_buffer *b1 = NULL, *b2 = NULL;
144         const char *name = "test_free";
145
146         b1 = rte_reorder_create(name, rte_socket_id(), 8);
147         TEST_ASSERT_NOT_NULL(b1, "Failed to create reorder buffer.");
148
149         b2 = rte_reorder_find_existing(name);
150         TEST_ASSERT_EQUAL(b1, b2, "Failed to find existing reorder buffer");
151
152         rte_reorder_free(b1);
153
154         b2 = rte_reorder_find_existing(name);
155         TEST_ASSERT((b2 == NULL) && (rte_errno == ENOENT),
156                         "Found previously freed reorder buffer");
157
158         return 0;
159 }
160
161 static int
162 test_reorder_insert(void)
163 {
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];
169         int ret = 0;
170         unsigned i;
171
172         /* This would create a reorder buffer instance consisting of:
173          * reorder_seq = 0
174          * ready_buf: RB[size] = {NULL, NULL, NULL, NULL}
175          * order_buf: OB[size] = {NULL, NULL, NULL, NULL}
176          */
177         b = rte_reorder_create("test_insert", rte_socket_id(), size);
178         TEST_ASSERT_NOT_NULL(b, "Failed to create reorder buffer");
179
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");
183                 bufs[i]->seqn = i;
184         }
185
186         /* This should fill up order buffer:
187          * reorder_seq = 0
188          * RB[] = {NULL, NULL, NULL, NULL}
189          * OB[] = {0, 1, 2, 3}
190          */
191         for (i = 0; i < size; i++) {
192                 ret = rte_reorder_insert(b, bufs[i]);
193                 if (ret != 0) {
194                         printf("%s:%d: Error inserting packet with seqn less than size\n",
195                                         __func__, __LINE__);
196                         ret = -1;
197                         goto exit;
198                 }
199                 bufs[i] = NULL;
200         }
201
202         /* early packet - should move mbufs to ready buf and move sequence window
203          * reorder_seq = 4
204          * RB[] = {0, 1, 2, 3}
205          * OB[] = {4, NULL, NULL, NULL}
206          */
207         ret = rte_reorder_insert(b, bufs[4]);
208         if (ret != 0) {
209                 printf("%s:%d: Error inserting early packet with seqn: size\n",
210                                 __func__, __LINE__);
211                 ret = -1;
212                 goto exit;
213         }
214         bufs[4] = NULL;
215
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",
221                                 __func__, __LINE__);
222                 ret = -1;
223                 goto exit;
224         }
225         bufs[5] = NULL;
226
227         /* late packet */
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__);
233                 ret = -1;
234                 goto exit;
235         }
236         bufs[6] = NULL;
237
238         ret = 0;
239 exit:
240         rte_reorder_free(b);
241         for (i = 0; i < num_bufs; i++) {
242                 if (bufs[i] != NULL)
243                         rte_pktmbuf_free(bufs[i]);
244         }
245         return ret;
246 }
247
248 static int
249 test_reorder_drain(void)
250 {
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];
257         int ret = 0;
258         unsigned i, cnt;
259
260         /* initialize all robufs to NULL */
261         for (i = 0; i < num_bufs; i++)
262                 robufs[i] = NULL;
263
264         /* This would create a reorder buffer instance consisting of:
265          * reorder_seq = 0
266          * ready_buf: RB[size] = {NULL, NULL, NULL, NULL}
267          * order_buf: OB[size] = {NULL, NULL, NULL, NULL}
268          */
269         b = rte_reorder_create("test_drain", rte_socket_id(), size);
270         TEST_ASSERT_NOT_NULL(b, "Failed to create reorder buffer");
271
272         /* Check no drained packets if reorder is empty */
273         cnt = rte_reorder_drain(b, robufs, 1);
274         if (cnt != 0) {
275                 printf("%s:%d: drained packets from empty reorder buffer\n",
276                                 __func__, __LINE__);
277                 ret = -1;
278                 goto exit;
279         }
280
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");
284                 bufs[i]->seqn = i;
285         }
286
287         /* Insert packet with seqn 1:
288          * reorder_seq = 0
289          * RB[] = {NULL, NULL, NULL, NULL}
290          * OB[] = {1, NULL, NULL, NULL}
291          */
292         rte_reorder_insert(b, bufs[1]);
293         bufs[1] = NULL;
294
295         cnt = rte_reorder_drain(b, robufs, 1);
296         if (cnt != 1) {
297                 printf("%s:%d:%d: number of expected packets not drained\n",
298                                 __func__, __LINE__, cnt);
299                 ret = -1;
300                 goto exit;
301         }
302         if (robufs[0] != NULL)
303                 rte_pktmbuf_free(robufs[i]);
304
305         /* Insert more packets
306          * RB[] = {NULL, NULL, NULL, NULL}
307          * OB[] = {NULL, 2, 3, NULL}
308          */
309         rte_reorder_insert(b, bufs[2]);
310         rte_reorder_insert(b, bufs[3]);
311         bufs[2] = NULL;
312         bufs[3] = NULL;
313
314         /* Insert more packets
315          * RB[] = {NULL, NULL, NULL, NULL}
316          * OB[] = {NULL, 2, 3, 4}
317          */
318         rte_reorder_insert(b, bufs[4]);
319         bufs[4] = NULL;
320
321         /* Insert more packets
322          * RB[] = {2, 3, 4, NULL}
323          * OB[] = {NULL, NULL, 7, NULL}
324          */
325         rte_reorder_insert(b, bufs[7]);
326         bufs[7] = NULL;
327
328         /* drained expected packets */
329         cnt = rte_reorder_drain(b, robufs, 4);
330         if (cnt != 3) {
331                 printf("%s:%d:%d: number of expected packets not drained\n",
332                                 __func__, __LINE__, cnt);
333                 ret = -1;
334                 goto exit;
335         }
336         for (i = 0; i < 3; i++) {
337                 if (robufs[i] != NULL)
338                         rte_pktmbuf_free(robufs[i]);
339         }
340
341         /*
342          * RB[] = {NULL, NULL, NULL, NULL}
343          * OB[] = {NULL, NULL, 7, NULL}
344          */
345         cnt = rte_reorder_drain(b, robufs, 1);
346         if (cnt != 0) {
347                 printf("%s:%d:%d: number of expected packets not drained\n",
348                                 __func__, __LINE__, cnt);
349                 ret = -1;
350                 goto exit;
351         }
352         ret = 0;
353 exit:
354         rte_reorder_free(b);
355         for (i = 0; i < num_bufs; i++) {
356                 if (bufs[i] != NULL)
357                         rte_pktmbuf_free(bufs[i]);
358                 if (robufs[i] != NULL)
359                         rte_pktmbuf_free(robufs[i]);
360         }
361         return ret;
362 }
363
364 static int
365 test_setup(void)
366 {
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",
373                                         __func__);
374                         return -1;
375                 }
376         } else
377                 rte_reorder_reset(test_params->b);
378
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,
383                         rte_socket_id());
384                 if (test_params->p == NULL) {
385                         printf("%s: Error creating mempool\n", __func__);
386                         return -1;
387                 }
388         }
389         return 0;
390 }
391
392 static void
393 test_teardown(void)
394 {
395         rte_reorder_free(test_params->b);
396         test_params->b = NULL;
397         rte_mempool_free(test_params->p);
398         test_params->p = NULL;
399 }
400
401
402 static struct unit_test_suite reorder_test_suite  = {
403
404         .setup = test_setup,
405         .teardown = test_teardown,
406         .suite_name = "Reorder Unit Test Suite",
407         .unit_test_cases = {
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),
414                 TEST_CASES_END()
415         }
416 };
417
418 static int
419 test_reorder(void)
420 {
421         return unit_test_suite_runner(&reorder_test_suite);
422 }
423
424 REGISTER_TEST_COMMAND(reorder_autotest, test_reorder);