xref: /dpdk/app/test/test_reorder.c (revision e0a8442ccd15bafbb7eb150c35331c8e3b828c53)
1a9de470cSBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
2a9de470cSBruce Richardson  * Copyright(c) 2010-2014 Intel Corporation
3a9de470cSBruce Richardson  */
4a9de470cSBruce Richardson 
53c60274cSJie Zhou #include "test.h"
63c60274cSJie Zhou 
7a9de470cSBruce Richardson #include <stdio.h>
8a9de470cSBruce Richardson #include <unistd.h>
9a9de470cSBruce Richardson #include <string.h>
10a9de470cSBruce Richardson 
11a9de470cSBruce Richardson #include <rte_cycles.h>
12a9de470cSBruce Richardson #include <rte_errno.h>
13a9de470cSBruce Richardson #include <rte_mbuf.h>
14a9de470cSBruce Richardson #include <rte_reorder.h>
15a9de470cSBruce Richardson #include <rte_lcore.h>
16a9de470cSBruce Richardson #include <rte_malloc.h>
17a9de470cSBruce Richardson 
18a9de470cSBruce Richardson #define BURST 32
19a9de470cSBruce Richardson #define REORDER_BUFFER_SIZE 16384
20a9de470cSBruce Richardson #define NUM_MBUFS (2*REORDER_BUFFER_SIZE)
21a9de470cSBruce Richardson #define REORDER_BUFFER_SIZE_INVALID 2049
22a9de470cSBruce Richardson 
23a9de470cSBruce Richardson struct reorder_unittest_params {
24a9de470cSBruce Richardson 	struct rte_mempool *p;
25a9de470cSBruce Richardson 	struct rte_reorder_buffer *b;
26a9de470cSBruce Richardson };
27a9de470cSBruce Richardson 
28a9de470cSBruce Richardson static struct reorder_unittest_params default_params  = {
29a9de470cSBruce Richardson 	.p = NULL,
30a9de470cSBruce Richardson 	.b = NULL
31a9de470cSBruce Richardson };
32a9de470cSBruce Richardson 
33a9de470cSBruce Richardson static struct reorder_unittest_params *test_params = &default_params;
34a9de470cSBruce Richardson 
35a9de470cSBruce Richardson static int
test_reorder_create(void)36a9de470cSBruce Richardson test_reorder_create(void)
37a9de470cSBruce Richardson {
38a9de470cSBruce Richardson 	struct rte_reorder_buffer *b = NULL;
39a9de470cSBruce Richardson 
40a9de470cSBruce Richardson 	b = rte_reorder_create(NULL, rte_socket_id(), REORDER_BUFFER_SIZE);
41a9de470cSBruce Richardson 	TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
42a9de470cSBruce Richardson 			"No error on create() with NULL name");
43a9de470cSBruce Richardson 
44a9de470cSBruce Richardson 	b = rte_reorder_create("PKT", rte_socket_id(), REORDER_BUFFER_SIZE_INVALID);
45a9de470cSBruce Richardson 	TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
46a9de470cSBruce Richardson 			"No error on create() with invalid buffer size param.");
47a9de470cSBruce Richardson 
48a9de470cSBruce Richardson 	b = rte_reorder_create("PKT_RO1", rte_socket_id(), REORDER_BUFFER_SIZE);
49a9de470cSBruce Richardson 	TEST_ASSERT_EQUAL(b, test_params->b,
50a9de470cSBruce Richardson 			"New reorder instance created with already existing name");
51a9de470cSBruce Richardson 
52a9de470cSBruce Richardson 	return 0;
53a9de470cSBruce Richardson }
54a9de470cSBruce Richardson 
55a9de470cSBruce Richardson static int
test_reorder_init(void)56a9de470cSBruce Richardson test_reorder_init(void)
57a9de470cSBruce Richardson {
58a9de470cSBruce Richardson 	struct rte_reorder_buffer *b = NULL;
59a9de470cSBruce Richardson 	unsigned int size;
60a9de470cSBruce Richardson 	/*
61f35f3dcaSVolodymyr Fialko 	 * The minimum memory area size that should be passed to library determined
62f35f3dcaSVolodymyr Fialko 	 * by rte_reorder_memory_footprint_get().
63a9de470cSBruce Richardson 	 * Otherwise error will be thrown
64a9de470cSBruce Richardson 	 */
65a9de470cSBruce Richardson 
66f35f3dcaSVolodymyr Fialko 	size = rte_reorder_memory_footprint_get(REORDER_BUFFER_SIZE) - 1;
67a9de470cSBruce Richardson 	b = rte_reorder_init(b, size, "PKT1", REORDER_BUFFER_SIZE);
68a9de470cSBruce Richardson 	TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
69a9de470cSBruce Richardson 			"No error on init with NULL buffer.");
70a9de470cSBruce Richardson 
71a9de470cSBruce Richardson 	b = rte_malloc(NULL, size, 0);
72a9de470cSBruce Richardson 	b = rte_reorder_init(b, size, "PKT1", REORDER_BUFFER_SIZE);
73a9de470cSBruce Richardson 	TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
74a9de470cSBruce Richardson 			"No error on init with invalid mem zone size.");
75a9de470cSBruce Richardson 	rte_free(b);
76a9de470cSBruce Richardson 
77f35f3dcaSVolodymyr Fialko 	size = rte_reorder_memory_footprint_get(REORDER_BUFFER_SIZE);
78a9de470cSBruce Richardson 	b = rte_malloc(NULL, size, 0);
79a9de470cSBruce Richardson 	b = rte_reorder_init(b, size, "PKT1", REORDER_BUFFER_SIZE_INVALID);
80a9de470cSBruce Richardson 	TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
81a9de470cSBruce Richardson 			"No error on init with invalid buffer size param.");
82a9de470cSBruce Richardson 
83a9de470cSBruce Richardson 	b = rte_reorder_init(b, size, NULL, REORDER_BUFFER_SIZE);
84a9de470cSBruce Richardson 	TEST_ASSERT((b == NULL) && (rte_errno == EINVAL),
85a9de470cSBruce Richardson 			"No error on init with invalid name.");
86a9de470cSBruce Richardson 	rte_free(b);
87a9de470cSBruce Richardson 
88a9de470cSBruce Richardson 	return 0;
89a9de470cSBruce Richardson }
90a9de470cSBruce Richardson 
91a9de470cSBruce Richardson static int
test_reorder_find_existing(void)92a9de470cSBruce Richardson test_reorder_find_existing(void)
93a9de470cSBruce Richardson {
94a9de470cSBruce Richardson 	struct rte_reorder_buffer *b = NULL;
95a9de470cSBruce Richardson 
96a9de470cSBruce Richardson 	/* Try to find existing reorder buffer instance */
97a9de470cSBruce Richardson 	b = rte_reorder_find_existing("PKT_RO1");
98a9de470cSBruce Richardson 	TEST_ASSERT_EQUAL(b, test_params->b,
99a9de470cSBruce Richardson 			"existing reorder buffer instance not found");
100a9de470cSBruce Richardson 
101a9de470cSBruce Richardson 	/* Try to find non existing reorder buffer instance */
102a9de470cSBruce Richardson 	b = rte_reorder_find_existing("ro_find_non_existing");
103a9de470cSBruce Richardson 	TEST_ASSERT((b == NULL) && (rte_errno == ENOENT),
104a9de470cSBruce Richardson 			"non existing reorder buffer instance found");
105a9de470cSBruce Richardson 
106a9de470cSBruce Richardson 	return 0;
107a9de470cSBruce Richardson }
108a9de470cSBruce Richardson 
109a9de470cSBruce Richardson static int
test_reorder_free(void)110a9de470cSBruce Richardson test_reorder_free(void)
111a9de470cSBruce Richardson {
112a9de470cSBruce Richardson 	struct rte_reorder_buffer *b1 = NULL, *b2 = NULL;
113a9de470cSBruce Richardson 	const char *name = "test_free";
114a9de470cSBruce Richardson 
115a9de470cSBruce Richardson 	b1 = rte_reorder_create(name, rte_socket_id(), 8);
116a9de470cSBruce Richardson 	TEST_ASSERT_NOT_NULL(b1, "Failed to create reorder buffer.");
117a9de470cSBruce Richardson 
118a9de470cSBruce Richardson 	b2 = rte_reorder_find_existing(name);
119a9de470cSBruce Richardson 	TEST_ASSERT_EQUAL(b1, b2, "Failed to find existing reorder buffer");
120a9de470cSBruce Richardson 
121a9de470cSBruce Richardson 	rte_reorder_free(b1);
122a9de470cSBruce Richardson 
123a9de470cSBruce Richardson 	b2 = rte_reorder_find_existing(name);
124a9de470cSBruce Richardson 	TEST_ASSERT((b2 == NULL) && (rte_errno == ENOENT),
125a9de470cSBruce Richardson 			"Found previously freed reorder buffer");
126a9de470cSBruce Richardson 
127a9de470cSBruce Richardson 	return 0;
128a9de470cSBruce Richardson }
129a9de470cSBruce Richardson 
130a9de470cSBruce Richardson static int
test_reorder_insert(void)131a9de470cSBruce Richardson test_reorder_insert(void)
132a9de470cSBruce Richardson {
133a9de470cSBruce Richardson 	struct rte_reorder_buffer *b = NULL;
134a9de470cSBruce Richardson 	struct rte_mempool *p = test_params->p;
135a9de470cSBruce Richardson 	const unsigned int size = 4;
136a9de470cSBruce Richardson 	const unsigned int num_bufs = 7;
137a9de470cSBruce Richardson 	struct rte_mbuf *bufs[num_bufs];
138a9de470cSBruce Richardson 	int ret = 0;
139a9de470cSBruce Richardson 	unsigned i;
140a9de470cSBruce Richardson 
141a9de470cSBruce Richardson 	/* This would create a reorder buffer instance consisting of:
142a9de470cSBruce Richardson 	 * reorder_seq = 0
143a9de470cSBruce Richardson 	 * ready_buf: RB[size] = {NULL, NULL, NULL, NULL}
144a9de470cSBruce Richardson 	 * order_buf: OB[size] = {NULL, NULL, NULL, NULL}
145a9de470cSBruce Richardson 	 */
146a9de470cSBruce Richardson 	b = rte_reorder_create("test_insert", rte_socket_id(), size);
147a9de470cSBruce Richardson 	TEST_ASSERT_NOT_NULL(b, "Failed to create reorder buffer");
148a9de470cSBruce Richardson 
149a9de470cSBruce Richardson 	for (i = 0; i < num_bufs; i++) {
150a9de470cSBruce Richardson 		bufs[i] = rte_pktmbuf_alloc(p);
151a9de470cSBruce Richardson 		TEST_ASSERT_NOT_NULL(bufs[i], "Packet allocation failed\n");
15201f34966SDavid Marchand 		*rte_reorder_seqn(bufs[i]) = i;
153a9de470cSBruce Richardson 	}
154a9de470cSBruce Richardson 
155a9de470cSBruce Richardson 	/* This should fill up order buffer:
156a9de470cSBruce Richardson 	 * reorder_seq = 0
157a9de470cSBruce Richardson 	 * RB[] = {NULL, NULL, NULL, NULL}
158a9de470cSBruce Richardson 	 * OB[] = {0, 1, 2, 3}
159a9de470cSBruce Richardson 	 */
160a9de470cSBruce Richardson 	for (i = 0; i < size; i++) {
161a9de470cSBruce Richardson 		ret = rte_reorder_insert(b, bufs[i]);
162a9de470cSBruce Richardson 		if (ret != 0) {
163a9de470cSBruce Richardson 			printf("%s:%d: Error inserting packet with seqn less than size\n",
164a9de470cSBruce Richardson 					__func__, __LINE__);
165a9de470cSBruce Richardson 			ret = -1;
166a9de470cSBruce Richardson 			goto exit;
167a9de470cSBruce Richardson 		}
168a9de470cSBruce Richardson 		bufs[i] = NULL;
169a9de470cSBruce Richardson 	}
170a9de470cSBruce Richardson 
171a9de470cSBruce Richardson 	/* early packet - should move mbufs to ready buf and move sequence window
172a9de470cSBruce Richardson 	 * reorder_seq = 4
173a9de470cSBruce Richardson 	 * RB[] = {0, 1, 2, 3}
174a9de470cSBruce Richardson 	 * OB[] = {4, NULL, NULL, NULL}
175a9de470cSBruce Richardson 	 */
176a9de470cSBruce Richardson 	ret = rte_reorder_insert(b, bufs[4]);
177a9de470cSBruce Richardson 	if (ret != 0) {
178a9de470cSBruce Richardson 		printf("%s:%d: Error inserting early packet with seqn: size\n",
179a9de470cSBruce Richardson 				__func__, __LINE__);
180a9de470cSBruce Richardson 		ret = -1;
181a9de470cSBruce Richardson 		goto exit;
182a9de470cSBruce Richardson 	}
183a9de470cSBruce Richardson 	bufs[4] = NULL;
184a9de470cSBruce Richardson 
185a9de470cSBruce Richardson 	/* early packet from current sequence window - full ready buffer */
18601f34966SDavid Marchand 	*rte_reorder_seqn(bufs[5]) = 2 * size;
187a9de470cSBruce Richardson 	ret = rte_reorder_insert(b, bufs[5]);
188a9de470cSBruce Richardson 	if (!((ret == -1) && (rte_errno == ENOSPC))) {
189a9de470cSBruce Richardson 		printf("%s:%d: No error inserting early packet with full ready buffer\n",
190a9de470cSBruce Richardson 				__func__, __LINE__);
191a9de470cSBruce Richardson 		ret = -1;
192a9de470cSBruce Richardson 		goto exit;
193a9de470cSBruce Richardson 	}
194a9de470cSBruce Richardson 	bufs[5] = NULL;
195a9de470cSBruce Richardson 
196a9de470cSBruce Richardson 	/* late packet */
19701f34966SDavid Marchand 	*rte_reorder_seqn(bufs[6]) = 3 * size;
198a9de470cSBruce Richardson 	ret = rte_reorder_insert(b, bufs[6]);
199a9de470cSBruce Richardson 	if (!((ret == -1) && (rte_errno == ERANGE))) {
200a9de470cSBruce Richardson 		printf("%s:%d: No error inserting late packet with seqn:"
201a9de470cSBruce Richardson 				" 3 * size\n", __func__, __LINE__);
202a9de470cSBruce Richardson 		ret = -1;
203a9de470cSBruce Richardson 		goto exit;
204a9de470cSBruce Richardson 	}
205a9de470cSBruce Richardson 	bufs[6] = NULL;
206a9de470cSBruce Richardson 
207a9de470cSBruce Richardson 	ret = 0;
208a9de470cSBruce Richardson exit:
209a9de470cSBruce Richardson 	rte_reorder_free(b);
210a9de470cSBruce Richardson 	for (i = 0; i < num_bufs; i++) {
211a9de470cSBruce Richardson 		rte_pktmbuf_free(bufs[i]);
212a9de470cSBruce Richardson 	}
213a9de470cSBruce Richardson 	return ret;
214a9de470cSBruce Richardson }
215a9de470cSBruce Richardson 
216a9de470cSBruce Richardson static int
test_reorder_drain(void)217a9de470cSBruce Richardson test_reorder_drain(void)
218a9de470cSBruce Richardson {
219a9de470cSBruce Richardson 	struct rte_reorder_buffer *b = NULL;
220a9de470cSBruce Richardson 	struct rte_mempool *p = test_params->p;
221a9de470cSBruce Richardson 	const unsigned int size = 4;
222a9de470cSBruce Richardson 	const unsigned int num_bufs = 8;
223a9de470cSBruce Richardson 	struct rte_mbuf *bufs[num_bufs];
224a9de470cSBruce Richardson 	struct rte_mbuf *robufs[num_bufs];
225a9de470cSBruce Richardson 	int ret = 0;
226a9de470cSBruce Richardson 	unsigned i, cnt;
227a9de470cSBruce Richardson 
228a9de470cSBruce Richardson 	/* initialize all robufs to NULL */
229a9de470cSBruce Richardson 	for (i = 0; i < num_bufs; i++)
230a9de470cSBruce Richardson 		robufs[i] = NULL;
231a9de470cSBruce Richardson 
232a9de470cSBruce Richardson 	/* This would create a reorder buffer instance consisting of:
233a9de470cSBruce Richardson 	 * reorder_seq = 0
234a9de470cSBruce Richardson 	 * ready_buf: RB[size] = {NULL, NULL, NULL, NULL}
235a9de470cSBruce Richardson 	 * order_buf: OB[size] = {NULL, NULL, NULL, NULL}
236a9de470cSBruce Richardson 	 */
237a9de470cSBruce Richardson 	b = rte_reorder_create("test_drain", rte_socket_id(), size);
238a9de470cSBruce Richardson 	TEST_ASSERT_NOT_NULL(b, "Failed to create reorder buffer");
239a9de470cSBruce Richardson 
240a9de470cSBruce Richardson 	/* Check no drained packets if reorder is empty */
241a9de470cSBruce Richardson 	cnt = rte_reorder_drain(b, robufs, 1);
242a9de470cSBruce Richardson 	if (cnt != 0) {
243a9de470cSBruce Richardson 		printf("%s:%d: drained packets from empty reorder buffer\n",
244a9de470cSBruce Richardson 				__func__, __LINE__);
245a9de470cSBruce Richardson 		ret = -1;
246a9de470cSBruce Richardson 		goto exit;
247a9de470cSBruce Richardson 	}
248a9de470cSBruce Richardson 
249a9de470cSBruce Richardson 	for (i = 0; i < num_bufs; i++) {
250a9de470cSBruce Richardson 		bufs[i] = rte_pktmbuf_alloc(p);
251a9de470cSBruce Richardson 		TEST_ASSERT_NOT_NULL(bufs[i], "Packet allocation failed\n");
25201f34966SDavid Marchand 		*rte_reorder_seqn(bufs[i]) = i;
253a9de470cSBruce Richardson 	}
254a9de470cSBruce Richardson 
255a9de470cSBruce Richardson 	/* Insert packet with seqn 1:
256a9de470cSBruce Richardson 	 * reorder_seq = 0
257a9de470cSBruce Richardson 	 * RB[] = {NULL, NULL, NULL, NULL}
258a9de470cSBruce Richardson 	 * OB[] = {1, NULL, NULL, NULL}
259a9de470cSBruce Richardson 	 */
260a9de470cSBruce Richardson 	rte_reorder_insert(b, bufs[1]);
261a9de470cSBruce Richardson 	bufs[1] = NULL;
262a9de470cSBruce Richardson 
263a9de470cSBruce Richardson 	cnt = rte_reorder_drain(b, robufs, 1);
264a9de470cSBruce Richardson 	if (cnt != 1) {
265a9de470cSBruce Richardson 		printf("%s:%d:%d: number of expected packets not drained\n",
266a9de470cSBruce Richardson 				__func__, __LINE__, cnt);
267a9de470cSBruce Richardson 		ret = -1;
268a9de470cSBruce Richardson 		goto exit;
269a9de470cSBruce Richardson 	}
270a9de470cSBruce Richardson 	rte_pktmbuf_free(robufs[0]);
271d51955eaSVolodymyr Fialko 	memset(robufs, 0, sizeof(robufs));
272a9de470cSBruce Richardson 
273a9de470cSBruce Richardson 	/* Insert more packets
274a9de470cSBruce Richardson 	 * RB[] = {NULL, NULL, NULL, NULL}
275a9de470cSBruce Richardson 	 * OB[] = {NULL, 2, 3, NULL}
276a9de470cSBruce Richardson 	 */
277a9de470cSBruce Richardson 	rte_reorder_insert(b, bufs[2]);
278a9de470cSBruce Richardson 	rte_reorder_insert(b, bufs[3]);
279a9de470cSBruce Richardson 	bufs[2] = NULL;
280a9de470cSBruce Richardson 	bufs[3] = NULL;
281a9de470cSBruce Richardson 
282a9de470cSBruce Richardson 	/* Insert more packets
283a9de470cSBruce Richardson 	 * RB[] = {NULL, NULL, NULL, NULL}
284a9de470cSBruce Richardson 	 * OB[] = {NULL, 2, 3, 4}
285a9de470cSBruce Richardson 	 */
286a9de470cSBruce Richardson 	rte_reorder_insert(b, bufs[4]);
287a9de470cSBruce Richardson 	bufs[4] = NULL;
288a9de470cSBruce Richardson 
289a9de470cSBruce Richardson 	/* Insert more packets
290a9de470cSBruce Richardson 	 * RB[] = {2, 3, 4, NULL}
291a9de470cSBruce Richardson 	 * OB[] = {NULL, NULL, 7, NULL}
292a9de470cSBruce Richardson 	 */
293a9de470cSBruce Richardson 	rte_reorder_insert(b, bufs[7]);
294a9de470cSBruce Richardson 	bufs[7] = NULL;
295a9de470cSBruce Richardson 
296a9de470cSBruce Richardson 	/* drained expected packets */
297a9de470cSBruce Richardson 	cnt = rte_reorder_drain(b, robufs, 4);
298a9de470cSBruce Richardson 	if (cnt != 3) {
299a9de470cSBruce Richardson 		printf("%s:%d:%d: number of expected packets not drained\n",
300a9de470cSBruce Richardson 				__func__, __LINE__, cnt);
301a9de470cSBruce Richardson 		ret = -1;
302a9de470cSBruce Richardson 		goto exit;
303a9de470cSBruce Richardson 	}
304a9de470cSBruce Richardson 	for (i = 0; i < 3; i++) {
305a9de470cSBruce Richardson 		rte_pktmbuf_free(robufs[i]);
306a9de470cSBruce Richardson 	}
307d51955eaSVolodymyr Fialko 	memset(robufs, 0, sizeof(robufs));
308a9de470cSBruce Richardson 
309a9de470cSBruce Richardson 	/*
310a9de470cSBruce Richardson 	 * RB[] = {NULL, NULL, NULL, NULL}
311a9de470cSBruce Richardson 	 * OB[] = {NULL, NULL, 7, NULL}
312a9de470cSBruce Richardson 	 */
313a9de470cSBruce Richardson 	cnt = rte_reorder_drain(b, robufs, 1);
314a9de470cSBruce Richardson 	if (cnt != 0) {
315a9de470cSBruce Richardson 		printf("%s:%d:%d: number of expected packets not drained\n",
316a9de470cSBruce Richardson 				__func__, __LINE__, cnt);
317a9de470cSBruce Richardson 		ret = -1;
318a9de470cSBruce Richardson 		goto exit;
319a9de470cSBruce Richardson 	}
320a9de470cSBruce Richardson 	ret = 0;
321a9de470cSBruce Richardson exit:
322a9de470cSBruce Richardson 	rte_reorder_free(b);
323a9de470cSBruce Richardson 	for (i = 0; i < num_bufs; i++) {
324a9de470cSBruce Richardson 		rte_pktmbuf_free(bufs[i]);
325a9de470cSBruce Richardson 		rte_pktmbuf_free(robufs[i]);
326a9de470cSBruce Richardson 	}
327a9de470cSBruce Richardson 	return ret;
328a9de470cSBruce Richardson }
329a9de470cSBruce Richardson 
3301dbb8485SVolodymyr Fialko static void
buffer_to_reorder_move(struct rte_mbuf ** mbuf,struct rte_reorder_buffer * b)3311dbb8485SVolodymyr Fialko buffer_to_reorder_move(struct rte_mbuf **mbuf, struct rte_reorder_buffer *b)
3321dbb8485SVolodymyr Fialko {
3331dbb8485SVolodymyr Fialko 	rte_reorder_insert(b, *mbuf);
3341dbb8485SVolodymyr Fialko 	*mbuf = NULL;
3351dbb8485SVolodymyr Fialko }
3361dbb8485SVolodymyr Fialko 
3371dbb8485SVolodymyr Fialko static int
test_reorder_drain_up_to_seqn(void)3381dbb8485SVolodymyr Fialko test_reorder_drain_up_to_seqn(void)
3391dbb8485SVolodymyr Fialko {
3401dbb8485SVolodymyr Fialko 	struct rte_mempool *p = test_params->p;
3411dbb8485SVolodymyr Fialko 	struct rte_reorder_buffer *b = NULL;
3421dbb8485SVolodymyr Fialko 	const unsigned int num_bufs = 10;
3431dbb8485SVolodymyr Fialko 	const unsigned int size = 4;
3441dbb8485SVolodymyr Fialko 	unsigned int i, cnt;
3451dbb8485SVolodymyr Fialko 	int ret = 0;
3461dbb8485SVolodymyr Fialko 
3471dbb8485SVolodymyr Fialko 	struct rte_mbuf *bufs[num_bufs];
3481dbb8485SVolodymyr Fialko 	struct rte_mbuf *robufs[num_bufs];
3491dbb8485SVolodymyr Fialko 
3501dbb8485SVolodymyr Fialko 	/* initialize all robufs to NULL */
3511dbb8485SVolodymyr Fialko 	memset(robufs, 0, sizeof(robufs));
3521dbb8485SVolodymyr Fialko 
3531dbb8485SVolodymyr Fialko 	/* This would create a reorder buffer instance consisting of:
3541dbb8485SVolodymyr Fialko 	 * reorder_seq = 0
3551dbb8485SVolodymyr Fialko 	 * ready_buf: RB[size] = {NULL, NULL, NULL, NULL}
3561dbb8485SVolodymyr Fialko 	 * order_buf: OB[size] = {NULL, NULL, NULL, NULL}
3571dbb8485SVolodymyr Fialko 	 */
3581dbb8485SVolodymyr Fialko 	b = rte_reorder_create("test_drain_up_to_seqn", rte_socket_id(), size);
3591dbb8485SVolodymyr Fialko 	TEST_ASSERT_NOT_NULL(b, "Failed to create reorder buffer");
3601dbb8485SVolodymyr Fialko 
3611dbb8485SVolodymyr Fialko 	for (i = 0; i < num_bufs; i++) {
3621dbb8485SVolodymyr Fialko 		bufs[i] = rte_pktmbuf_alloc(p);
3631dbb8485SVolodymyr Fialko 		TEST_ASSERT_NOT_NULL(bufs[i], "Packet allocation failed\n");
3641dbb8485SVolodymyr Fialko 		*rte_reorder_seqn(bufs[i]) = i;
3651dbb8485SVolodymyr Fialko 	}
3661dbb8485SVolodymyr Fialko 
3671dbb8485SVolodymyr Fialko 	/* Insert packet with seqn 1 and 3:
3681dbb8485SVolodymyr Fialko 	 * RB[] = {NULL, NULL, NULL, NULL}
3691dbb8485SVolodymyr Fialko 	 * OB[] = {1, 2, 3, NULL}
3701dbb8485SVolodymyr Fialko 	 */
3711dbb8485SVolodymyr Fialko 	buffer_to_reorder_move(&bufs[1], b);
3721dbb8485SVolodymyr Fialko 	buffer_to_reorder_move(&bufs[2], b);
3731dbb8485SVolodymyr Fialko 	buffer_to_reorder_move(&bufs[3], b);
3741dbb8485SVolodymyr Fialko 	/* Draining 1, 2 */
3751dbb8485SVolodymyr Fialko 	cnt = rte_reorder_drain_up_to_seqn(b, robufs, num_bufs, 3);
3761dbb8485SVolodymyr Fialko 	if (cnt != 2) {
3771dbb8485SVolodymyr Fialko 		printf("%s:%d:%d: number of expected packets not drained\n",
3781dbb8485SVolodymyr Fialko 				__func__, __LINE__, cnt);
3791dbb8485SVolodymyr Fialko 		ret = -1;
3801dbb8485SVolodymyr Fialko 		goto exit;
3811dbb8485SVolodymyr Fialko 	}
3821dbb8485SVolodymyr Fialko 	for (i = 0; i < 2; i++)
3831dbb8485SVolodymyr Fialko 		rte_pktmbuf_free(robufs[i]);
3841dbb8485SVolodymyr Fialko 	memset(robufs, 0, sizeof(robufs));
3851dbb8485SVolodymyr Fialko 
3861dbb8485SVolodymyr Fialko 	/* Insert more packets
3871dbb8485SVolodymyr Fialko 	 * RB[] = {NULL, NULL, NULL, NULL}
3881dbb8485SVolodymyr Fialko 	 * OB[] = {3, 4, NULL, 6}
3891dbb8485SVolodymyr Fialko 	 */
3901dbb8485SVolodymyr Fialko 	buffer_to_reorder_move(&bufs[4], b);
3911dbb8485SVolodymyr Fialko 	buffer_to_reorder_move(&bufs[6], b);
3921dbb8485SVolodymyr Fialko 	/* Insert more packets to utilize Ready buffer
3931dbb8485SVolodymyr Fialko 	 * RB[] = {3, NULL, 5, 6}
3941dbb8485SVolodymyr Fialko 	 * OB[] = {NULL, NULL, 8, NULL}
3951dbb8485SVolodymyr Fialko 	 */
3961dbb8485SVolodymyr Fialko 	buffer_to_reorder_move(&bufs[8], b);
3971dbb8485SVolodymyr Fialko 
3981dbb8485SVolodymyr Fialko 	/* Drain 3 and 5 */
3991dbb8485SVolodymyr Fialko 	cnt = rte_reorder_drain_up_to_seqn(b, robufs, num_bufs, 6);
4001dbb8485SVolodymyr Fialko 	if (cnt != 2) {
4011dbb8485SVolodymyr Fialko 		printf("%s:%d:%d: number of expected packets not drained\n",
4021dbb8485SVolodymyr Fialko 				__func__, __LINE__, cnt);
4031dbb8485SVolodymyr Fialko 		ret = -1;
4041dbb8485SVolodymyr Fialko 		goto exit;
4051dbb8485SVolodymyr Fialko 	}
4061dbb8485SVolodymyr Fialko 	for (i = 0; i < 2; i++)
4071dbb8485SVolodymyr Fialko 		rte_pktmbuf_free(robufs[i]);
4081dbb8485SVolodymyr Fialko 	memset(robufs, 0, sizeof(robufs));
4091dbb8485SVolodymyr Fialko 
4101dbb8485SVolodymyr Fialko 	ret = 0;
4111dbb8485SVolodymyr Fialko exit:
4121dbb8485SVolodymyr Fialko 	rte_reorder_free(b);
4131dbb8485SVolodymyr Fialko 	for (i = 0; i < num_bufs; i++) {
4141dbb8485SVolodymyr Fialko 		rte_pktmbuf_free(bufs[i]);
4151dbb8485SVolodymyr Fialko 		rte_pktmbuf_free(robufs[i]);
4161dbb8485SVolodymyr Fialko 	}
4171dbb8485SVolodymyr Fialko 	return ret;
4181dbb8485SVolodymyr Fialko }
4191dbb8485SVolodymyr Fialko 
420a9de470cSBruce Richardson static int
test_reorder_set_seqn(void)4211bc9a84dSVolodymyr Fialko test_reorder_set_seqn(void)
4221bc9a84dSVolodymyr Fialko {
4231bc9a84dSVolodymyr Fialko 	struct rte_mempool *p = test_params->p;
4241bc9a84dSVolodymyr Fialko 	struct rte_reorder_buffer *b = NULL;
4251bc9a84dSVolodymyr Fialko 	const unsigned int num_bufs = 7;
4261bc9a84dSVolodymyr Fialko 	const unsigned int size = 4;
4271bc9a84dSVolodymyr Fialko 	unsigned int i;
4281bc9a84dSVolodymyr Fialko 	int ret = 0;
4291bc9a84dSVolodymyr Fialko 
4301bc9a84dSVolodymyr Fialko 	struct rte_mbuf *bufs[num_bufs];
4311bc9a84dSVolodymyr Fialko 
4321bc9a84dSVolodymyr Fialko 	/* This would create a reorder buffer instance consisting of:
4331bc9a84dSVolodymyr Fialko 	 * reorder_seq = 0
4341bc9a84dSVolodymyr Fialko 	 * ready_buf: RB[size] = {NULL, NULL, NULL, NULL}
4351bc9a84dSVolodymyr Fialko 	 * order_buf: OB[size] = {NULL, NULL, NULL, NULL}
4361bc9a84dSVolodymyr Fialko 	 */
4371bc9a84dSVolodymyr Fialko 	b = rte_reorder_create("test_min_seqn_set", rte_socket_id(), size);
4381bc9a84dSVolodymyr Fialko 	TEST_ASSERT_NOT_NULL(b, "Failed to create reorder buffer");
4391bc9a84dSVolodymyr Fialko 
4401bc9a84dSVolodymyr Fialko 	for (i = 0; i < num_bufs; i++) {
4411bc9a84dSVolodymyr Fialko 		bufs[i] = rte_pktmbuf_alloc(p);
4421bc9a84dSVolodymyr Fialko 		if (bufs[i] == NULL) {
4431bc9a84dSVolodymyr Fialko 			printf("Packet allocation failed\n");
4441bc9a84dSVolodymyr Fialko 			goto exit;
4451bc9a84dSVolodymyr Fialko 		}
4461bc9a84dSVolodymyr Fialko 		*rte_reorder_seqn(bufs[i]) = i;
4471bc9a84dSVolodymyr Fialko 	}
4481bc9a84dSVolodymyr Fialko 
4491bc9a84dSVolodymyr Fialko 	ret = rte_reorder_min_seqn_set(b, 5);
4501bc9a84dSVolodymyr Fialko 	if (ret != 0) {
4511bc9a84dSVolodymyr Fialko 		printf("%s:%d: Error in setting min sequence number\n", __func__, __LINE__);
4521bc9a84dSVolodymyr Fialko 		ret = -1;
4531bc9a84dSVolodymyr Fialko 		goto exit;
4541bc9a84dSVolodymyr Fialko 	}
4551bc9a84dSVolodymyr Fialko 
4561bc9a84dSVolodymyr Fialko 	ret = rte_reorder_insert(b, bufs[0]);
4571bc9a84dSVolodymyr Fialko 	if (ret >= 0) {
4581bc9a84dSVolodymyr Fialko 		printf("%s:%d: Insertion with value less the min seq number\n", __func__, __LINE__);
4591bc9a84dSVolodymyr Fialko 		ret = -1;
4601bc9a84dSVolodymyr Fialko 		goto exit;
4611bc9a84dSVolodymyr Fialko 	}
4621bc9a84dSVolodymyr Fialko 
4631bc9a84dSVolodymyr Fialko 	ret = rte_reorder_insert(b, bufs[5]);
4641bc9a84dSVolodymyr Fialko 	if (ret != 0) {
4651bc9a84dSVolodymyr Fialko 		printf("%s:%d: Error inserting packet with valid seqn\n", __func__, __LINE__);
4661bc9a84dSVolodymyr Fialko 		ret = -1;
4671bc9a84dSVolodymyr Fialko 		goto exit;
4681bc9a84dSVolodymyr Fialko 	}
4691bc9a84dSVolodymyr Fialko 	bufs[5] = NULL;
4701bc9a84dSVolodymyr Fialko 
4711bc9a84dSVolodymyr Fialko 	ret = rte_reorder_min_seqn_set(b, 0);
4721bc9a84dSVolodymyr Fialko 	if (ret >= 0) {
4731bc9a84dSVolodymyr Fialko 		printf("%s:%d: Error in setting min sequence number with non-empty buffer\n",
4741bc9a84dSVolodymyr Fialko 				__func__, __LINE__);
4751bc9a84dSVolodymyr Fialko 		ret = -1;
4761bc9a84dSVolodymyr Fialko 		goto exit;
4771bc9a84dSVolodymyr Fialko 	}
4781bc9a84dSVolodymyr Fialko 
4791bc9a84dSVolodymyr Fialko 	ret = 0;
4801bc9a84dSVolodymyr Fialko exit:
4811bc9a84dSVolodymyr Fialko 	rte_reorder_free(b);
4821bc9a84dSVolodymyr Fialko 	for (i = 0; i < num_bufs; i++)
4831bc9a84dSVolodymyr Fialko 		rte_pktmbuf_free(bufs[i]);
4841bc9a84dSVolodymyr Fialko 
4851bc9a84dSVolodymyr Fialko 	return ret;
4861bc9a84dSVolodymyr Fialko }
4871bc9a84dSVolodymyr Fialko 
4881bc9a84dSVolodymyr Fialko static int
test_setup(void)489a9de470cSBruce Richardson test_setup(void)
490a9de470cSBruce Richardson {
491a9de470cSBruce Richardson 	/* reorder buffer instance creation */
492a9de470cSBruce Richardson 	if (test_params->b == NULL) {
493a9de470cSBruce Richardson 		test_params->b = rte_reorder_create("PKT_RO1", rte_socket_id(),
494a9de470cSBruce Richardson 							REORDER_BUFFER_SIZE);
495a9de470cSBruce Richardson 		if (test_params->b == NULL) {
496a9de470cSBruce Richardson 			printf("%s: Error creating reorder buffer instance b\n",
497a9de470cSBruce Richardson 					__func__);
498a9de470cSBruce Richardson 			return -1;
499a9de470cSBruce Richardson 		}
500a9de470cSBruce Richardson 	} else
501a9de470cSBruce Richardson 		rte_reorder_reset(test_params->b);
502a9de470cSBruce Richardson 
503a9de470cSBruce Richardson 	/* mempool creation */
504a9de470cSBruce Richardson 	if (test_params->p == NULL) {
505a9de470cSBruce Richardson 		test_params->p = rte_pktmbuf_pool_create("RO_MBUF_POOL",
506a9de470cSBruce Richardson 			NUM_MBUFS, BURST, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
507a9de470cSBruce Richardson 			rte_socket_id());
508a9de470cSBruce Richardson 		if (test_params->p == NULL) {
509a9de470cSBruce Richardson 			printf("%s: Error creating mempool\n", __func__);
510a9de470cSBruce Richardson 			return -1;
511a9de470cSBruce Richardson 		}
512a9de470cSBruce Richardson 	}
513a9de470cSBruce Richardson 	return 0;
514a9de470cSBruce Richardson }
515a9de470cSBruce Richardson 
516a9de470cSBruce Richardson static void
test_teardown(void)517a9de470cSBruce Richardson test_teardown(void)
518a9de470cSBruce Richardson {
519a9de470cSBruce Richardson 	rte_reorder_free(test_params->b);
520a9de470cSBruce Richardson 	test_params->b = NULL;
521a9de470cSBruce Richardson 	rte_mempool_free(test_params->p);
522a9de470cSBruce Richardson 	test_params->p = NULL;
523a9de470cSBruce Richardson }
524a9de470cSBruce Richardson 
525a9de470cSBruce Richardson 
526a9de470cSBruce Richardson static struct unit_test_suite reorder_test_suite  = {
527a9de470cSBruce Richardson 
528a9de470cSBruce Richardson 	.setup = test_setup,
529a9de470cSBruce Richardson 	.teardown = test_teardown,
530a9de470cSBruce Richardson 	.suite_name = "Reorder Unit Test Suite",
531a9de470cSBruce Richardson 	.unit_test_cases = {
532a9de470cSBruce Richardson 		TEST_CASE(test_reorder_create),
533a9de470cSBruce Richardson 		TEST_CASE(test_reorder_init),
534a9de470cSBruce Richardson 		TEST_CASE(test_reorder_find_existing),
535a9de470cSBruce Richardson 		TEST_CASE(test_reorder_free),
536a9de470cSBruce Richardson 		TEST_CASE(test_reorder_insert),
537a9de470cSBruce Richardson 		TEST_CASE(test_reorder_drain),
5381dbb8485SVolodymyr Fialko 		TEST_CASE(test_reorder_drain_up_to_seqn),
5391bc9a84dSVolodymyr Fialko 		TEST_CASE(test_reorder_set_seqn),
540a9de470cSBruce Richardson 		TEST_CASES_END()
541a9de470cSBruce Richardson 	}
542a9de470cSBruce Richardson };
543a9de470cSBruce Richardson 
544a9de470cSBruce Richardson static int
test_reorder(void)545a9de470cSBruce Richardson test_reorder(void)
546a9de470cSBruce Richardson {
547a9de470cSBruce Richardson 	return unit_test_suite_runner(&reorder_test_suite);
548a9de470cSBruce Richardson }
549a9de470cSBruce Richardson 
5503c60274cSJie Zhou 
551*e0a8442cSBruce Richardson REGISTER_FAST_TEST(reorder_autotest, true, true, test_reorder);
552