xref: /dpdk/app/test/test_mbuf.c (revision 452c1916)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 
5 #include <string.h>
6 #include <stdarg.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <stdint.h>
10 #include <inttypes.h>
11 #include <errno.h>
12 #include <sys/queue.h>
13 
14 #include <rte_common.h>
15 #include <rte_errno.h>
16 #include <rte_debug.h>
17 #include <rte_log.h>
18 #include <rte_memory.h>
19 #include <rte_memcpy.h>
20 #include <rte_launch.h>
21 #include <rte_eal.h>
22 #include <rte_per_lcore.h>
23 #include <rte_lcore.h>
24 #include <rte_atomic.h>
25 #include <rte_branch_prediction.h>
26 #include <rte_ring.h>
27 #include <rte_mempool.h>
28 #include <rte_mbuf.h>
29 #include <rte_random.h>
30 #include <rte_cycles.h>
31 #include <rte_malloc.h>
32 #include <rte_ether.h>
33 #include <rte_ip.h>
34 #include <rte_tcp.h>
35 #include <rte_mbuf_dyn.h>
36 
37 #include "test.h"
38 
39 #define MEMPOOL_CACHE_SIZE      32
40 #define MBUF_DATA_SIZE          2048
41 #define NB_MBUF                 128
42 #define MBUF_TEST_DATA_LEN      1464
43 #define MBUF_TEST_DATA_LEN2     50
44 #define MBUF_TEST_DATA_LEN3     256
45 #define MBUF_TEST_HDR1_LEN      20
46 #define MBUF_TEST_HDR2_LEN      30
47 #define MBUF_TEST_ALL_HDRS_LEN  (MBUF_TEST_HDR1_LEN+MBUF_TEST_HDR2_LEN)
48 #define MBUF_TEST_SEG_SIZE      64
49 #define MBUF_TEST_BURST         8
50 #define EXT_BUF_TEST_DATA_LEN   1024
51 #define MBUF_MAX_SEG            16
52 #define MBUF_NO_HEADER		0
53 #define MBUF_HEADER		1
54 #define MBUF_NEG_TEST_READ	2
55 #define VAL_NAME(flag)          { flag, #flag }
56 
57 /* chain length in bulk test */
58 #define CHAIN_LEN 16
59 
60 /* size of private data for mbuf in pktmbuf_pool2 */
61 #define MBUF2_PRIV_SIZE         128
62 
63 #define REFCNT_MAX_ITER         64
64 #define REFCNT_MAX_TIMEOUT      10
65 #define REFCNT_MAX_REF          (RTE_MAX_LCORE)
66 #define REFCNT_MBUF_NUM         64
67 #define REFCNT_RING_SIZE        (REFCNT_MBUF_NUM * REFCNT_MAX_REF)
68 
69 #define MAGIC_DATA              0x42424242
70 
71 #define MAKE_STRING(x)          # x
72 
73 #ifdef RTE_MBUF_REFCNT_ATOMIC
74 
75 static volatile uint32_t refcnt_stop_workers;
76 static unsigned refcnt_lcore[RTE_MAX_LCORE];
77 
78 #endif
79 
80 /*
81  * MBUF
82  * ====
83  *
84  * #. Allocate a mbuf pool.
85  *
86  *    - The pool contains NB_MBUF elements, where each mbuf is MBUF_SIZE
87  *      bytes long.
88  *
89  * #. Test multiple allocations of mbufs from this pool.
90  *
91  *    - Allocate NB_MBUF and store pointers in a table.
92  *    - If an allocation fails, return an error.
93  *    - Free all these mbufs.
94  *    - Repeat the same test to check that mbufs were freed correctly.
95  *
96  * #. Test data manipulation in pktmbuf.
97  *
98  *    - Alloc an mbuf.
99  *    - Append data using rte_pktmbuf_append().
100  *    - Test for error in rte_pktmbuf_append() when len is too large.
101  *    - Trim data at the end of mbuf using rte_pktmbuf_trim().
102  *    - Test for error in rte_pktmbuf_trim() when len is too large.
103  *    - Prepend a header using rte_pktmbuf_prepend().
104  *    - Test for error in rte_pktmbuf_prepend() when len is too large.
105  *    - Remove data at the beginning of mbuf using rte_pktmbuf_adj().
106  *    - Test for error in rte_pktmbuf_adj() when len is too large.
107  *    - Check that appended data is not corrupt.
108  *    - Free the mbuf.
109  *    - Between all these tests, check data_len and pkt_len, and
110  *      that the mbuf is contiguous.
111  *    - Repeat the test to check that allocation operations
112  *      reinitialize the mbuf correctly.
113  *
114  * #. Test packet cloning
115  *    - Clone a mbuf and verify the data
116  *    - Clone the cloned mbuf and verify the data
117  *    - Attach a mbuf to another that does not have the same priv_size.
118  */
119 
120 #define GOTO_FAIL(str, ...) do {					\
121 		printf("mbuf test FAILED (l.%d): <" str ">\n",		\
122 		       __LINE__,  ##__VA_ARGS__);			\
123 		goto fail;						\
124 } while(0)
125 
126 /*
127  * test data manipulation in mbuf with non-ascii data
128  */
129 static int
130 test_pktmbuf_with_non_ascii_data(struct rte_mempool *pktmbuf_pool)
131 {
132 	struct rte_mbuf *m = NULL;
133 	char *data;
134 
135 	m = rte_pktmbuf_alloc(pktmbuf_pool);
136 	if (m == NULL)
137 		GOTO_FAIL("Cannot allocate mbuf");
138 	if (rte_pktmbuf_pkt_len(m) != 0)
139 		GOTO_FAIL("Bad length");
140 
141 	data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN);
142 	if (data == NULL)
143 		GOTO_FAIL("Cannot append data");
144 	if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
145 		GOTO_FAIL("Bad pkt length");
146 	if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
147 		GOTO_FAIL("Bad data length");
148 	memset(data, 0xff, rte_pktmbuf_pkt_len(m));
149 	if (!rte_pktmbuf_is_contiguous(m))
150 		GOTO_FAIL("Buffer should be continuous");
151 	rte_pktmbuf_dump(stdout, m, MBUF_TEST_DATA_LEN);
152 
153 	rte_pktmbuf_free(m);
154 
155 	return 0;
156 
157 fail:
158 	if(m) {
159 		rte_pktmbuf_free(m);
160 	}
161 	return -1;
162 }
163 
164 /*
165  * test data manipulation in mbuf
166  */
167 static int
168 test_one_pktmbuf(struct rte_mempool *pktmbuf_pool)
169 {
170 	struct rte_mbuf *m = NULL;
171 	char *data, *data2, *hdr;
172 	unsigned i;
173 
174 	printf("Test pktmbuf API\n");
175 
176 	/* alloc a mbuf */
177 
178 	m = rte_pktmbuf_alloc(pktmbuf_pool);
179 	if (m == NULL)
180 		GOTO_FAIL("Cannot allocate mbuf");
181 	if (rte_pktmbuf_pkt_len(m) != 0)
182 		GOTO_FAIL("Bad length");
183 
184 	rte_pktmbuf_dump(stdout, m, 0);
185 
186 	/* append data */
187 
188 	data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN);
189 	if (data == NULL)
190 		GOTO_FAIL("Cannot append data");
191 	if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
192 		GOTO_FAIL("Bad pkt length");
193 	if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
194 		GOTO_FAIL("Bad data length");
195 	memset(data, 0x66, rte_pktmbuf_pkt_len(m));
196 	if (!rte_pktmbuf_is_contiguous(m))
197 		GOTO_FAIL("Buffer should be continuous");
198 	rte_pktmbuf_dump(stdout, m, MBUF_TEST_DATA_LEN);
199 	rte_pktmbuf_dump(stdout, m, 2*MBUF_TEST_DATA_LEN);
200 
201 	/* this append should fail */
202 
203 	data2 = rte_pktmbuf_append(m, (uint16_t)(rte_pktmbuf_tailroom(m) + 1));
204 	if (data2 != NULL)
205 		GOTO_FAIL("Append should not succeed");
206 
207 	/* append some more data */
208 
209 	data2 = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN2);
210 	if (data2 == NULL)
211 		GOTO_FAIL("Cannot append data");
212 	if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_DATA_LEN2)
213 		GOTO_FAIL("Bad pkt length");
214 	if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_DATA_LEN2)
215 		GOTO_FAIL("Bad data length");
216 	if (!rte_pktmbuf_is_contiguous(m))
217 		GOTO_FAIL("Buffer should be continuous");
218 
219 	/* trim data at the end of mbuf */
220 
221 	if (rte_pktmbuf_trim(m, MBUF_TEST_DATA_LEN2) < 0)
222 		GOTO_FAIL("Cannot trim data");
223 	if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
224 		GOTO_FAIL("Bad pkt length");
225 	if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
226 		GOTO_FAIL("Bad data length");
227 	if (!rte_pktmbuf_is_contiguous(m))
228 		GOTO_FAIL("Buffer should be continuous");
229 
230 	/* this trim should fail */
231 
232 	if (rte_pktmbuf_trim(m, (uint16_t)(rte_pktmbuf_data_len(m) + 1)) == 0)
233 		GOTO_FAIL("trim should not succeed");
234 
235 	/* prepend one header */
236 
237 	hdr = rte_pktmbuf_prepend(m, MBUF_TEST_HDR1_LEN);
238 	if (hdr == NULL)
239 		GOTO_FAIL("Cannot prepend");
240 	if (data - hdr != MBUF_TEST_HDR1_LEN)
241 		GOTO_FAIL("Prepend failed");
242 	if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_HDR1_LEN)
243 		GOTO_FAIL("Bad pkt length");
244 	if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_HDR1_LEN)
245 		GOTO_FAIL("Bad data length");
246 	if (!rte_pktmbuf_is_contiguous(m))
247 		GOTO_FAIL("Buffer should be continuous");
248 	memset(hdr, 0x55, MBUF_TEST_HDR1_LEN);
249 
250 	/* prepend another header */
251 
252 	hdr = rte_pktmbuf_prepend(m, MBUF_TEST_HDR2_LEN);
253 	if (hdr == NULL)
254 		GOTO_FAIL("Cannot prepend");
255 	if (data - hdr != MBUF_TEST_ALL_HDRS_LEN)
256 		GOTO_FAIL("Prepend failed");
257 	if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_ALL_HDRS_LEN)
258 		GOTO_FAIL("Bad pkt length");
259 	if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_ALL_HDRS_LEN)
260 		GOTO_FAIL("Bad data length");
261 	if (!rte_pktmbuf_is_contiguous(m))
262 		GOTO_FAIL("Buffer should be continuous");
263 	memset(hdr, 0x55, MBUF_TEST_HDR2_LEN);
264 
265 	rte_mbuf_sanity_check(m, 1);
266 	rte_mbuf_sanity_check(m, 0);
267 	rte_pktmbuf_dump(stdout, m, 0);
268 
269 	/* this prepend should fail */
270 
271 	hdr = rte_pktmbuf_prepend(m, (uint16_t)(rte_pktmbuf_headroom(m) + 1));
272 	if (hdr != NULL)
273 		GOTO_FAIL("prepend should not succeed");
274 
275 	/* remove data at beginning of mbuf (adj) */
276 
277 	if (data != rte_pktmbuf_adj(m, MBUF_TEST_ALL_HDRS_LEN))
278 		GOTO_FAIL("rte_pktmbuf_adj failed");
279 	if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
280 		GOTO_FAIL("Bad pkt length");
281 	if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
282 		GOTO_FAIL("Bad data length");
283 	if (!rte_pktmbuf_is_contiguous(m))
284 		GOTO_FAIL("Buffer should be continuous");
285 
286 	/* this adj should fail */
287 
288 	if (rte_pktmbuf_adj(m, (uint16_t)(rte_pktmbuf_data_len(m) + 1)) != NULL)
289 		GOTO_FAIL("rte_pktmbuf_adj should not succeed");
290 
291 	/* check data */
292 
293 	if (!rte_pktmbuf_is_contiguous(m))
294 		GOTO_FAIL("Buffer should be continuous");
295 
296 	for (i=0; i<MBUF_TEST_DATA_LEN; i++) {
297 		if (data[i] != 0x66)
298 			GOTO_FAIL("Data corrupted at offset %u", i);
299 	}
300 
301 	/* free mbuf */
302 
303 	rte_pktmbuf_free(m);
304 	m = NULL;
305 	return 0;
306 
307 fail:
308 	if (m)
309 		rte_pktmbuf_free(m);
310 	return -1;
311 }
312 
313 static uint16_t
314 testclone_refcnt_read(struct rte_mbuf *m)
315 {
316 	return RTE_MBUF_HAS_PINNED_EXTBUF(m) ?
317 	       rte_mbuf_ext_refcnt_read(m->shinfo) :
318 	       rte_mbuf_refcnt_read(m);
319 }
320 
321 static int
322 testclone_testupdate_testdetach(struct rte_mempool *pktmbuf_pool,
323 				struct rte_mempool *clone_pool)
324 {
325 	struct rte_mbuf *m = NULL;
326 	struct rte_mbuf *clone = NULL;
327 	struct rte_mbuf *clone2 = NULL;
328 	unaligned_uint32_t *data;
329 
330 	/* alloc a mbuf */
331 	m = rte_pktmbuf_alloc(pktmbuf_pool);
332 	if (m == NULL)
333 		GOTO_FAIL("ooops not allocating mbuf");
334 
335 	if (rte_pktmbuf_pkt_len(m) != 0)
336 		GOTO_FAIL("Bad length");
337 
338 	rte_pktmbuf_append(m, sizeof(uint32_t));
339 	data = rte_pktmbuf_mtod(m, unaligned_uint32_t *);
340 	*data = MAGIC_DATA;
341 
342 	/* clone the allocated mbuf */
343 	clone = rte_pktmbuf_clone(m, clone_pool);
344 	if (clone == NULL)
345 		GOTO_FAIL("cannot clone data\n");
346 
347 	data = rte_pktmbuf_mtod(clone, unaligned_uint32_t *);
348 	if (*data != MAGIC_DATA)
349 		GOTO_FAIL("invalid data in clone\n");
350 
351 	if (testclone_refcnt_read(m) != 2)
352 		GOTO_FAIL("invalid refcnt in m\n");
353 
354 	/* free the clone */
355 	rte_pktmbuf_free(clone);
356 	clone = NULL;
357 
358 	/* same test with a chained mbuf */
359 	m->next = rte_pktmbuf_alloc(pktmbuf_pool);
360 	if (m->next == NULL)
361 		GOTO_FAIL("Next Pkt Null\n");
362 	m->nb_segs = 2;
363 
364 	rte_pktmbuf_append(m->next, sizeof(uint32_t));
365 	m->pkt_len = 2 * sizeof(uint32_t);
366 
367 	data = rte_pktmbuf_mtod(m->next, unaligned_uint32_t *);
368 	*data = MAGIC_DATA;
369 
370 	clone = rte_pktmbuf_clone(m, clone_pool);
371 	if (clone == NULL)
372 		GOTO_FAIL("cannot clone data\n");
373 
374 	data = rte_pktmbuf_mtod(clone, unaligned_uint32_t *);
375 	if (*data != MAGIC_DATA)
376 		GOTO_FAIL("invalid data in clone\n");
377 
378 	data = rte_pktmbuf_mtod(clone->next, unaligned_uint32_t *);
379 	if (*data != MAGIC_DATA)
380 		GOTO_FAIL("invalid data in clone->next\n");
381 
382 	if (testclone_refcnt_read(m) != 2)
383 		GOTO_FAIL("invalid refcnt in m\n");
384 
385 	if (testclone_refcnt_read(m->next) != 2)
386 		GOTO_FAIL("invalid refcnt in m->next\n");
387 
388 	/* try to clone the clone */
389 
390 	clone2 = rte_pktmbuf_clone(clone, clone_pool);
391 	if (clone2 == NULL)
392 		GOTO_FAIL("cannot clone the clone\n");
393 
394 	data = rte_pktmbuf_mtod(clone2, unaligned_uint32_t *);
395 	if (*data != MAGIC_DATA)
396 		GOTO_FAIL("invalid data in clone2\n");
397 
398 	data = rte_pktmbuf_mtod(clone2->next, unaligned_uint32_t *);
399 	if (*data != MAGIC_DATA)
400 		GOTO_FAIL("invalid data in clone2->next\n");
401 
402 	if (testclone_refcnt_read(m) != 3)
403 		GOTO_FAIL("invalid refcnt in m\n");
404 
405 	if (testclone_refcnt_read(m->next) != 3)
406 		GOTO_FAIL("invalid refcnt in m->next\n");
407 
408 	/* free mbuf */
409 	rte_pktmbuf_free(m);
410 	rte_pktmbuf_free(clone);
411 	rte_pktmbuf_free(clone2);
412 
413 	m = NULL;
414 	clone = NULL;
415 	clone2 = NULL;
416 	printf("%s ok\n", __func__);
417 	return 0;
418 
419 fail:
420 	if (m)
421 		rte_pktmbuf_free(m);
422 	if (clone)
423 		rte_pktmbuf_free(clone);
424 	if (clone2)
425 		rte_pktmbuf_free(clone2);
426 	return -1;
427 }
428 
429 static int
430 test_pktmbuf_copy(struct rte_mempool *pktmbuf_pool,
431 		  struct rte_mempool *clone_pool)
432 {
433 	struct rte_mbuf *m = NULL;
434 	struct rte_mbuf *copy = NULL;
435 	struct rte_mbuf *copy2 = NULL;
436 	struct rte_mbuf *clone = NULL;
437 	unaligned_uint32_t *data;
438 
439 	/* alloc a mbuf */
440 	m = rte_pktmbuf_alloc(pktmbuf_pool);
441 	if (m == NULL)
442 		GOTO_FAIL("ooops not allocating mbuf");
443 
444 	if (rte_pktmbuf_pkt_len(m) != 0)
445 		GOTO_FAIL("Bad length");
446 
447 	rte_pktmbuf_append(m, sizeof(uint32_t));
448 	data = rte_pktmbuf_mtod(m, unaligned_uint32_t *);
449 	*data = MAGIC_DATA;
450 
451 	/* copy the allocated mbuf */
452 	copy = rte_pktmbuf_copy(m, pktmbuf_pool, 0, UINT32_MAX);
453 	if (copy == NULL)
454 		GOTO_FAIL("cannot copy data\n");
455 
456 	if (rte_pktmbuf_pkt_len(copy) != sizeof(uint32_t))
457 		GOTO_FAIL("copy length incorrect\n");
458 
459 	if (rte_pktmbuf_data_len(copy) != sizeof(uint32_t))
460 		GOTO_FAIL("copy data length incorrect\n");
461 
462 	data = rte_pktmbuf_mtod(copy, unaligned_uint32_t *);
463 	if (*data != MAGIC_DATA)
464 		GOTO_FAIL("invalid data in copy\n");
465 
466 	/* free the copy */
467 	rte_pktmbuf_free(copy);
468 	copy = NULL;
469 
470 	/* same test with a cloned mbuf */
471 	clone = rte_pktmbuf_clone(m, clone_pool);
472 	if (clone == NULL)
473 		GOTO_FAIL("cannot clone data\n");
474 
475 	if ((!RTE_MBUF_HAS_PINNED_EXTBUF(m) &&
476 	     !RTE_MBUF_CLONED(clone)) ||
477 	    (RTE_MBUF_HAS_PINNED_EXTBUF(m) &&
478 	     !RTE_MBUF_HAS_EXTBUF(clone)))
479 		GOTO_FAIL("clone did not give a cloned mbuf\n");
480 
481 	copy = rte_pktmbuf_copy(clone, pktmbuf_pool, 0, UINT32_MAX);
482 	if (copy == NULL)
483 		GOTO_FAIL("cannot copy cloned mbuf\n");
484 
485 	if (RTE_MBUF_CLONED(copy))
486 		GOTO_FAIL("copy of clone is cloned?\n");
487 
488 	if (rte_pktmbuf_pkt_len(copy) != sizeof(uint32_t))
489 		GOTO_FAIL("copy clone length incorrect\n");
490 
491 	if (rte_pktmbuf_data_len(copy) != sizeof(uint32_t))
492 		GOTO_FAIL("copy clone data length incorrect\n");
493 
494 	data = rte_pktmbuf_mtod(copy, unaligned_uint32_t *);
495 	if (*data != MAGIC_DATA)
496 		GOTO_FAIL("invalid data in clone copy\n");
497 	rte_pktmbuf_free(clone);
498 	rte_pktmbuf_free(copy);
499 	copy = NULL;
500 	clone = NULL;
501 
502 
503 	/* same test with a chained mbuf */
504 	m->next = rte_pktmbuf_alloc(pktmbuf_pool);
505 	if (m->next == NULL)
506 		GOTO_FAIL("Next Pkt Null\n");
507 	m->nb_segs = 2;
508 
509 	rte_pktmbuf_append(m->next, sizeof(uint32_t));
510 	m->pkt_len = 2 * sizeof(uint32_t);
511 	data = rte_pktmbuf_mtod(m->next, unaligned_uint32_t *);
512 	*data = MAGIC_DATA + 1;
513 
514 	copy = rte_pktmbuf_copy(m, pktmbuf_pool, 0, UINT32_MAX);
515 	if (copy == NULL)
516 		GOTO_FAIL("cannot copy data\n");
517 
518 	if (rte_pktmbuf_pkt_len(copy) != 2 * sizeof(uint32_t))
519 		GOTO_FAIL("chain copy length incorrect\n");
520 
521 	if (rte_pktmbuf_data_len(copy) != 2 * sizeof(uint32_t))
522 		GOTO_FAIL("chain copy data length incorrect\n");
523 
524 	data = rte_pktmbuf_mtod(copy, unaligned_uint32_t *);
525 	if (data[0] != MAGIC_DATA || data[1] != MAGIC_DATA + 1)
526 		GOTO_FAIL("invalid data in copy\n");
527 
528 	rte_pktmbuf_free(copy2);
529 
530 	/* test offset copy */
531 	copy2 = rte_pktmbuf_copy(copy, pktmbuf_pool,
532 				 sizeof(uint32_t), UINT32_MAX);
533 	if (copy2 == NULL)
534 		GOTO_FAIL("cannot copy the copy\n");
535 
536 	if (rte_pktmbuf_pkt_len(copy2) != sizeof(uint32_t))
537 		GOTO_FAIL("copy with offset, length incorrect\n");
538 
539 	if (rte_pktmbuf_data_len(copy2) != sizeof(uint32_t))
540 		GOTO_FAIL("copy with offset, data length incorrect\n");
541 
542 	data = rte_pktmbuf_mtod(copy2, unaligned_uint32_t *);
543 	if (data[0] != MAGIC_DATA + 1)
544 		GOTO_FAIL("copy with offset, invalid data\n");
545 
546 	rte_pktmbuf_free(copy2);
547 
548 	/* test truncation copy */
549 	copy2 = rte_pktmbuf_copy(copy, pktmbuf_pool,
550 				 0, sizeof(uint32_t));
551 	if (copy2 == NULL)
552 		GOTO_FAIL("cannot copy the copy\n");
553 
554 	if (rte_pktmbuf_pkt_len(copy2) != sizeof(uint32_t))
555 		GOTO_FAIL("copy with truncate, length incorrect\n");
556 
557 	if (rte_pktmbuf_data_len(copy2) != sizeof(uint32_t))
558 		GOTO_FAIL("copy with truncate, data length incorrect\n");
559 
560 	data = rte_pktmbuf_mtod(copy2, unaligned_uint32_t *);
561 	if (data[0] != MAGIC_DATA)
562 		GOTO_FAIL("copy with truncate, invalid data\n");
563 
564 	/* free mbuf */
565 	rte_pktmbuf_free(m);
566 	rte_pktmbuf_free(copy);
567 	rte_pktmbuf_free(copy2);
568 
569 	m = NULL;
570 	copy = NULL;
571 	copy2 = NULL;
572 	printf("%s ok\n", __func__);
573 	return 0;
574 
575 fail:
576 	if (m)
577 		rte_pktmbuf_free(m);
578 	if (copy)
579 		rte_pktmbuf_free(copy);
580 	if (copy2)
581 		rte_pktmbuf_free(copy2);
582 	return -1;
583 }
584 
585 static int
586 test_attach_from_different_pool(struct rte_mempool *pktmbuf_pool,
587 				struct rte_mempool *pktmbuf_pool2)
588 {
589 	struct rte_mbuf *m = NULL;
590 	struct rte_mbuf *clone = NULL;
591 	struct rte_mbuf *clone2 = NULL;
592 	char *data, *c_data, *c_data2;
593 
594 	/* alloc a mbuf */
595 	m = rte_pktmbuf_alloc(pktmbuf_pool);
596 	if (m == NULL)
597 		GOTO_FAIL("cannot allocate mbuf");
598 
599 	if (rte_pktmbuf_pkt_len(m) != 0)
600 		GOTO_FAIL("Bad length");
601 
602 	data = rte_pktmbuf_mtod(m, char *);
603 
604 	/* allocate a new mbuf from the second pool, and attach it to the first
605 	 * mbuf */
606 	clone = rte_pktmbuf_alloc(pktmbuf_pool2);
607 	if (clone == NULL)
608 		GOTO_FAIL("cannot allocate mbuf from second pool\n");
609 
610 	/* check data room size and priv size, and erase priv */
611 	if (rte_pktmbuf_data_room_size(clone->pool) != 0)
612 		GOTO_FAIL("data room size should be 0\n");
613 	if (rte_pktmbuf_priv_size(clone->pool) != MBUF2_PRIV_SIZE)
614 		GOTO_FAIL("data room size should be %d\n", MBUF2_PRIV_SIZE);
615 	memset(clone + 1, 0, MBUF2_PRIV_SIZE);
616 
617 	/* save data pointer to compare it after detach() */
618 	c_data = rte_pktmbuf_mtod(clone, char *);
619 	if (c_data != (char *)clone + sizeof(*clone) + MBUF2_PRIV_SIZE)
620 		GOTO_FAIL("bad data pointer in clone");
621 	if (rte_pktmbuf_headroom(clone) != 0)
622 		GOTO_FAIL("bad headroom in clone");
623 
624 	rte_pktmbuf_attach(clone, m);
625 
626 	if (rte_pktmbuf_mtod(clone, char *) != data)
627 		GOTO_FAIL("clone was not attached properly\n");
628 	if (rte_pktmbuf_headroom(clone) != RTE_PKTMBUF_HEADROOM)
629 		GOTO_FAIL("bad headroom in clone after attach");
630 	if (rte_mbuf_refcnt_read(m) != 2)
631 		GOTO_FAIL("invalid refcnt in m\n");
632 
633 	/* allocate a new mbuf from the second pool, and attach it to the first
634 	 * cloned mbuf */
635 	clone2 = rte_pktmbuf_alloc(pktmbuf_pool2);
636 	if (clone2 == NULL)
637 		GOTO_FAIL("cannot allocate clone2 from second pool\n");
638 
639 	/* check data room size and priv size, and erase priv */
640 	if (rte_pktmbuf_data_room_size(clone2->pool) != 0)
641 		GOTO_FAIL("data room size should be 0\n");
642 	if (rte_pktmbuf_priv_size(clone2->pool) != MBUF2_PRIV_SIZE)
643 		GOTO_FAIL("data room size should be %d\n", MBUF2_PRIV_SIZE);
644 	memset(clone2 + 1, 0, MBUF2_PRIV_SIZE);
645 
646 	/* save data pointer to compare it after detach() */
647 	c_data2 = rte_pktmbuf_mtod(clone2, char *);
648 	if (c_data2 != (char *)clone2 + sizeof(*clone2) + MBUF2_PRIV_SIZE)
649 		GOTO_FAIL("bad data pointer in clone2");
650 	if (rte_pktmbuf_headroom(clone2) != 0)
651 		GOTO_FAIL("bad headroom in clone2");
652 
653 	rte_pktmbuf_attach(clone2, clone);
654 
655 	if (rte_pktmbuf_mtod(clone2, char *) != data)
656 		GOTO_FAIL("clone2 was not attached properly\n");
657 	if (rte_pktmbuf_headroom(clone2) != RTE_PKTMBUF_HEADROOM)
658 		GOTO_FAIL("bad headroom in clone2 after attach");
659 	if (rte_mbuf_refcnt_read(m) != 3)
660 		GOTO_FAIL("invalid refcnt in m\n");
661 
662 	/* detach the clones */
663 	rte_pktmbuf_detach(clone);
664 	if (c_data != rte_pktmbuf_mtod(clone, char *))
665 		GOTO_FAIL("clone was not detached properly\n");
666 	if (rte_mbuf_refcnt_read(m) != 2)
667 		GOTO_FAIL("invalid refcnt in m\n");
668 
669 	rte_pktmbuf_detach(clone2);
670 	if (c_data2 != rte_pktmbuf_mtod(clone2, char *))
671 		GOTO_FAIL("clone2 was not detached properly\n");
672 	if (rte_mbuf_refcnt_read(m) != 1)
673 		GOTO_FAIL("invalid refcnt in m\n");
674 
675 	/* free the clones and the initial mbuf */
676 	rte_pktmbuf_free(clone2);
677 	rte_pktmbuf_free(clone);
678 	rte_pktmbuf_free(m);
679 	printf("%s ok\n", __func__);
680 	return 0;
681 
682 fail:
683 	if (m)
684 		rte_pktmbuf_free(m);
685 	if (clone)
686 		rte_pktmbuf_free(clone);
687 	if (clone2)
688 		rte_pktmbuf_free(clone2);
689 	return -1;
690 }
691 
692 /*
693  * test allocation and free of mbufs
694  */
695 static int
696 test_pktmbuf_pool(struct rte_mempool *pktmbuf_pool)
697 {
698 	unsigned i;
699 	struct rte_mbuf *m[NB_MBUF];
700 	int ret = 0;
701 
702 	for (i=0; i<NB_MBUF; i++)
703 		m[i] = NULL;
704 
705 	/* alloc NB_MBUF mbufs */
706 	for (i=0; i<NB_MBUF; i++) {
707 		m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
708 		if (m[i] == NULL) {
709 			printf("rte_pktmbuf_alloc() failed (%u)\n", i);
710 			ret = -1;
711 		}
712 	}
713 	struct rte_mbuf *extra = NULL;
714 	extra = rte_pktmbuf_alloc(pktmbuf_pool);
715 	if(extra != NULL) {
716 		printf("Error pool not empty");
717 		ret = -1;
718 	}
719 	extra = rte_pktmbuf_clone(m[0], pktmbuf_pool);
720 	if(extra != NULL) {
721 		printf("Error pool not empty");
722 		ret = -1;
723 	}
724 	/* free them */
725 	for (i=0; i<NB_MBUF; i++) {
726 		if (m[i] != NULL)
727 			rte_pktmbuf_free(m[i]);
728 	}
729 
730 	return ret;
731 }
732 
733 /*
734  * test bulk allocation and bulk free of mbufs
735  */
736 static int
737 test_pktmbuf_pool_bulk(void)
738 {
739 	struct rte_mempool *pool = NULL;
740 	struct rte_mempool *pool2 = NULL;
741 	unsigned int i;
742 	struct rte_mbuf *m;
743 	struct rte_mbuf *mbufs[NB_MBUF];
744 	int ret = 0;
745 
746 	/* We cannot use the preallocated mbuf pools because their caches
747 	 * prevent us from bulk allocating all objects in them.
748 	 * So we create our own mbuf pools without caches.
749 	 */
750 	printf("Create mbuf pools for bulk allocation.\n");
751 	pool = rte_pktmbuf_pool_create("test_pktmbuf_bulk",
752 			NB_MBUF, 0, 0, MBUF_DATA_SIZE, SOCKET_ID_ANY);
753 	if (pool == NULL) {
754 		printf("rte_pktmbuf_pool_create() failed. rte_errno %d\n",
755 		       rte_errno);
756 		goto err;
757 	}
758 	pool2 = rte_pktmbuf_pool_create("test_pktmbuf_bulk2",
759 			NB_MBUF, 0, 0, MBUF_DATA_SIZE, SOCKET_ID_ANY);
760 	if (pool2 == NULL) {
761 		printf("rte_pktmbuf_pool_create() failed. rte_errno %d\n",
762 		       rte_errno);
763 		goto err;
764 	}
765 
766 	/* Preconditions: Mempools must be full. */
767 	if (!(rte_mempool_full(pool) && rte_mempool_full(pool2))) {
768 		printf("Test precondition failed: mempools not full\n");
769 		goto err;
770 	}
771 	if (!(rte_mempool_avail_count(pool) == NB_MBUF &&
772 			rte_mempool_avail_count(pool2) == NB_MBUF)) {
773 		printf("Test precondition failed: mempools: %u+%u != %u+%u",
774 		       rte_mempool_avail_count(pool),
775 		       rte_mempool_avail_count(pool2),
776 		       NB_MBUF, NB_MBUF);
777 		goto err;
778 	}
779 
780 	printf("Test single bulk alloc, followed by multiple bulk free.\n");
781 
782 	/* Bulk allocate all mbufs in the pool, in one go. */
783 	ret = rte_pktmbuf_alloc_bulk(pool, mbufs, NB_MBUF);
784 	if (ret != 0) {
785 		printf("rte_pktmbuf_alloc_bulk() failed: %d\n", ret);
786 		goto err;
787 	}
788 	/* Test that they have been removed from the pool. */
789 	if (!rte_mempool_empty(pool)) {
790 		printf("mempool not empty\n");
791 		goto err;
792 	}
793 	/* Bulk free all mbufs, in four steps. */
794 	RTE_BUILD_BUG_ON(NB_MBUF % 4 != 0);
795 	for (i = 0; i < NB_MBUF; i += NB_MBUF / 4) {
796 		rte_pktmbuf_free_bulk(&mbufs[i], NB_MBUF / 4);
797 		/* Test that they have been returned to the pool. */
798 		if (rte_mempool_avail_count(pool) != i + NB_MBUF / 4) {
799 			printf("mempool avail count incorrect\n");
800 			goto err;
801 		}
802 	}
803 
804 	printf("Test multiple bulk alloc, followed by single bulk free.\n");
805 
806 	/* Bulk allocate all mbufs in the pool, in four steps. */
807 	for (i = 0; i < NB_MBUF; i += NB_MBUF / 4) {
808 		ret = rte_pktmbuf_alloc_bulk(pool, &mbufs[i], NB_MBUF / 4);
809 		if (ret != 0) {
810 			printf("rte_pktmbuf_alloc_bulk() failed: %d\n", ret);
811 			goto err;
812 		}
813 	}
814 	/* Test that they have been removed from the pool. */
815 	if (!rte_mempool_empty(pool)) {
816 		printf("mempool not empty\n");
817 		goto err;
818 	}
819 	/* Bulk free all mbufs, in one go. */
820 	rte_pktmbuf_free_bulk(mbufs, NB_MBUF);
821 	/* Test that they have been returned to the pool. */
822 	if (!rte_mempool_full(pool)) {
823 		printf("mempool not full\n");
824 		goto err;
825 	}
826 
827 	printf("Test bulk free of single long chain.\n");
828 
829 	/* Bulk allocate all mbufs in the pool, in one go. */
830 	ret = rte_pktmbuf_alloc_bulk(pool, mbufs, NB_MBUF);
831 	if (ret != 0) {
832 		printf("rte_pktmbuf_alloc_bulk() failed: %d\n", ret);
833 		goto err;
834 	}
835 	/* Create a long mbuf chain. */
836 	for (i = 1; i < NB_MBUF; i++) {
837 		ret = rte_pktmbuf_chain(mbufs[0], mbufs[i]);
838 		if (ret != 0) {
839 			printf("rte_pktmbuf_chain() failed: %d\n", ret);
840 			goto err;
841 		}
842 		mbufs[i] = NULL;
843 	}
844 	/* Free the mbuf chain containing all the mbufs. */
845 	rte_pktmbuf_free_bulk(mbufs, 1);
846 	/* Test that they have been returned to the pool. */
847 	if (!rte_mempool_full(pool)) {
848 		printf("mempool not full\n");
849 		goto err;
850 	}
851 
852 	printf("Test bulk free of multiple chains using multiple pools.\n");
853 
854 	/* Create mbuf chains containing mbufs from different pools. */
855 	RTE_BUILD_BUG_ON(CHAIN_LEN % 2 != 0);
856 	RTE_BUILD_BUG_ON(NB_MBUF % (CHAIN_LEN / 2) != 0);
857 	for (i = 0; i < NB_MBUF * 2; i++) {
858 		m = rte_pktmbuf_alloc((i & 4) ? pool2 : pool);
859 		if (m == NULL) {
860 			printf("rte_pktmbuf_alloc() failed (%u)\n", i);
861 			goto err;
862 		}
863 		if ((i % CHAIN_LEN) == 0)
864 			mbufs[i / CHAIN_LEN] = m;
865 		else
866 			rte_pktmbuf_chain(mbufs[i / CHAIN_LEN], m);
867 	}
868 	/* Test that both pools have been emptied. */
869 	if (!(rte_mempool_empty(pool) && rte_mempool_empty(pool2))) {
870 		printf("mempools not empty\n");
871 		goto err;
872 	}
873 	/* Free one mbuf chain. */
874 	rte_pktmbuf_free_bulk(mbufs, 1);
875 	/* Test that the segments have been returned to the pools. */
876 	if (!(rte_mempool_avail_count(pool) == CHAIN_LEN / 2 &&
877 			rte_mempool_avail_count(pool2) == CHAIN_LEN / 2)) {
878 		printf("all segments of first mbuf have not been returned\n");
879 		goto err;
880 	}
881 	/* Free the remaining mbuf chains. */
882 	rte_pktmbuf_free_bulk(&mbufs[1], NB_MBUF * 2 / CHAIN_LEN - 1);
883 	/* Test that they have been returned to the pools. */
884 	if (!(rte_mempool_full(pool) && rte_mempool_full(pool2))) {
885 		printf("mempools not full\n");
886 		goto err;
887 	}
888 
889 	ret = 0;
890 	goto done;
891 
892 err:
893 	ret = -1;
894 
895 done:
896 	printf("Free mbuf pools for bulk allocation.\n");
897 	rte_mempool_free(pool);
898 	rte_mempool_free(pool2);
899 	return ret;
900 }
901 
902 /*
903  * test that the pointer to the data on a packet mbuf is set properly
904  */
905 static int
906 test_pktmbuf_pool_ptr(struct rte_mempool *pktmbuf_pool)
907 {
908 	unsigned i;
909 	struct rte_mbuf *m[NB_MBUF];
910 	int ret = 0;
911 
912 	for (i=0; i<NB_MBUF; i++)
913 		m[i] = NULL;
914 
915 	/* alloc NB_MBUF mbufs */
916 	for (i=0; i<NB_MBUF; i++) {
917 		m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
918 		if (m[i] == NULL) {
919 			printf("rte_pktmbuf_alloc() failed (%u)\n", i);
920 			ret = -1;
921 			break;
922 		}
923 		m[i]->data_off += 64;
924 	}
925 
926 	/* free them */
927 	for (i=0; i<NB_MBUF; i++) {
928 		if (m[i] != NULL)
929 			rte_pktmbuf_free(m[i]);
930 	}
931 
932 	for (i=0; i<NB_MBUF; i++)
933 		m[i] = NULL;
934 
935 	/* alloc NB_MBUF mbufs */
936 	for (i=0; i<NB_MBUF; i++) {
937 		m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
938 		if (m[i] == NULL) {
939 			printf("rte_pktmbuf_alloc() failed (%u)\n", i);
940 			ret = -1;
941 			break;
942 		}
943 		if (m[i]->data_off != RTE_PKTMBUF_HEADROOM) {
944 			printf("invalid data_off\n");
945 			ret = -1;
946 		}
947 	}
948 
949 	/* free them */
950 	for (i=0; i<NB_MBUF; i++) {
951 		if (m[i] != NULL)
952 			rte_pktmbuf_free(m[i]);
953 	}
954 
955 	return ret;
956 }
957 
958 static int
959 test_pktmbuf_free_segment(struct rte_mempool *pktmbuf_pool)
960 {
961 	unsigned i;
962 	struct rte_mbuf *m[NB_MBUF];
963 	int ret = 0;
964 
965 	for (i=0; i<NB_MBUF; i++)
966 		m[i] = NULL;
967 
968 	/* alloc NB_MBUF mbufs */
969 	for (i=0; i<NB_MBUF; i++) {
970 		m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
971 		if (m[i] == NULL) {
972 			printf("rte_pktmbuf_alloc() failed (%u)\n", i);
973 			ret = -1;
974 		}
975 	}
976 
977 	/* free them */
978 	for (i=0; i<NB_MBUF; i++) {
979 		if (m[i] != NULL) {
980 			struct rte_mbuf *mb, *mt;
981 
982 			mb = m[i];
983 			while(mb != NULL) {
984 				mt = mb;
985 				mb = mb->next;
986 				rte_pktmbuf_free_seg(mt);
987 			}
988 		}
989 	}
990 
991 	return ret;
992 }
993 
994 /*
995  * Stress test for rte_mbuf atomic refcnt.
996  * Implies that RTE_MBUF_REFCNT_ATOMIC is defined.
997  * For more efficiency, recommended to run with RTE_LIBRTE_MBUF_DEBUG defined.
998  */
999 
1000 #ifdef RTE_MBUF_REFCNT_ATOMIC
1001 
1002 static int
1003 test_refcnt_worker(void *arg)
1004 {
1005 	unsigned lcore, free;
1006 	void *mp = 0;
1007 	struct rte_ring *refcnt_mbuf_ring = arg;
1008 
1009 	lcore = rte_lcore_id();
1010 	printf("%s started at lcore %u\n", __func__, lcore);
1011 
1012 	free = 0;
1013 	while (refcnt_stop_workers == 0) {
1014 		if (rte_ring_dequeue(refcnt_mbuf_ring, &mp) == 0) {
1015 			free++;
1016 			rte_pktmbuf_free(mp);
1017 		}
1018 	}
1019 
1020 	refcnt_lcore[lcore] += free;
1021 	printf("%s finished at lcore %u, "
1022 	       "number of freed mbufs: %u\n",
1023 	       __func__, lcore, free);
1024 	return 0;
1025 }
1026 
1027 static void
1028 test_refcnt_iter(unsigned int lcore, unsigned int iter,
1029 		 struct rte_mempool *refcnt_pool,
1030 		 struct rte_ring *refcnt_mbuf_ring)
1031 {
1032 	uint16_t ref;
1033 	unsigned i, n, tref, wn;
1034 	struct rte_mbuf *m;
1035 
1036 	tref = 0;
1037 
1038 	/* For each mbuf in the pool:
1039 	 * - allocate mbuf,
1040 	 * - increment it's reference up to N+1,
1041 	 * - enqueue it N times into the ring for worker cores to free.
1042 	 */
1043 	for (i = 0, n = rte_mempool_avail_count(refcnt_pool);
1044 	    i != n && (m = rte_pktmbuf_alloc(refcnt_pool)) != NULL;
1045 	    i++) {
1046 		ref = RTE_MAX(rte_rand() % REFCNT_MAX_REF, 1UL);
1047 		tref += ref;
1048 		if ((ref & 1) != 0) {
1049 			rte_pktmbuf_refcnt_update(m, ref);
1050 			while (ref-- != 0)
1051 				rte_ring_enqueue(refcnt_mbuf_ring, m);
1052 		} else {
1053 			while (ref-- != 0) {
1054 				rte_pktmbuf_refcnt_update(m, 1);
1055 				rte_ring_enqueue(refcnt_mbuf_ring, m);
1056 			}
1057 		}
1058 		rte_pktmbuf_free(m);
1059 	}
1060 
1061 	if (i != n)
1062 		rte_panic("(lcore=%u, iter=%u): was able to allocate only "
1063 		          "%u from %u mbufs\n", lcore, iter, i, n);
1064 
1065 	/* wait till worker lcores  will consume all mbufs */
1066 	while (!rte_ring_empty(refcnt_mbuf_ring))
1067 		;
1068 
1069 	/* check that all mbufs are back into mempool by now */
1070 	for (wn = 0; wn != REFCNT_MAX_TIMEOUT; wn++) {
1071 		if ((i = rte_mempool_avail_count(refcnt_pool)) == n) {
1072 			refcnt_lcore[lcore] += tref;
1073 			printf("%s(lcore=%u, iter=%u) completed, "
1074 			    "%u references processed\n",
1075 			    __func__, lcore, iter, tref);
1076 			return;
1077 		}
1078 		rte_delay_ms(100);
1079 	}
1080 
1081 	rte_panic("(lcore=%u, iter=%u): after %us only "
1082 	          "%u of %u mbufs left free\n", lcore, iter, wn, i, n);
1083 }
1084 
1085 static int
1086 test_refcnt_main(struct rte_mempool *refcnt_pool,
1087 		   struct rte_ring *refcnt_mbuf_ring)
1088 {
1089 	unsigned i, lcore;
1090 
1091 	lcore = rte_lcore_id();
1092 	printf("%s started at lcore %u\n", __func__, lcore);
1093 
1094 	for (i = 0; i != REFCNT_MAX_ITER; i++)
1095 		test_refcnt_iter(lcore, i, refcnt_pool, refcnt_mbuf_ring);
1096 
1097 	refcnt_stop_workers = 1;
1098 	rte_wmb();
1099 
1100 	printf("%s finished at lcore %u\n", __func__, lcore);
1101 	return 0;
1102 }
1103 
1104 #endif
1105 
1106 static int
1107 test_refcnt_mbuf(void)
1108 {
1109 #ifdef RTE_MBUF_REFCNT_ATOMIC
1110 	unsigned int main_lcore, worker, tref;
1111 	int ret = -1;
1112 	struct rte_mempool *refcnt_pool = NULL;
1113 	struct rte_ring *refcnt_mbuf_ring = NULL;
1114 
1115 	if (rte_lcore_count() < 2) {
1116 		printf("Not enough cores for test_refcnt_mbuf, expecting at least 2\n");
1117 		return TEST_SKIPPED;
1118 	}
1119 
1120 	printf("starting %s, at %u lcores\n", __func__, rte_lcore_count());
1121 
1122 	/* create refcnt pool & ring if they don't exist */
1123 
1124 	refcnt_pool = rte_pktmbuf_pool_create(MAKE_STRING(refcnt_pool),
1125 					      REFCNT_MBUF_NUM, 0, 0, 0,
1126 					      SOCKET_ID_ANY);
1127 	if (refcnt_pool == NULL) {
1128 		printf("%s: cannot allocate " MAKE_STRING(refcnt_pool) "\n",
1129 		       __func__);
1130 		return -1;
1131 	}
1132 
1133 	refcnt_mbuf_ring = rte_ring_create("refcnt_mbuf_ring",
1134 					   rte_align32pow2(REFCNT_RING_SIZE), SOCKET_ID_ANY,
1135 					   RING_F_SP_ENQ);
1136 	if (refcnt_mbuf_ring == NULL) {
1137 		printf("%s: cannot allocate " MAKE_STRING(refcnt_mbuf_ring)
1138 		       "\n", __func__);
1139 		goto err;
1140 	}
1141 
1142 	refcnt_stop_workers = 0;
1143 	memset(refcnt_lcore, 0, sizeof (refcnt_lcore));
1144 
1145 	rte_eal_mp_remote_launch(test_refcnt_worker, refcnt_mbuf_ring, SKIP_MAIN);
1146 
1147 	test_refcnt_main(refcnt_pool, refcnt_mbuf_ring);
1148 
1149 	rte_eal_mp_wait_lcore();
1150 
1151 	/* check that we porcessed all references */
1152 	tref = 0;
1153 	main_lcore = rte_get_main_lcore();
1154 
1155 	RTE_LCORE_FOREACH_WORKER(worker)
1156 		tref += refcnt_lcore[worker];
1157 
1158 	if (tref != refcnt_lcore[main_lcore])
1159 		rte_panic("referenced mbufs: %u, freed mbufs: %u\n",
1160 			  tref, refcnt_lcore[main_lcore]);
1161 
1162 	rte_mempool_dump(stdout, refcnt_pool);
1163 	rte_ring_dump(stdout, refcnt_mbuf_ring);
1164 
1165 	ret = 0;
1166 
1167 err:
1168 	rte_mempool_free(refcnt_pool);
1169 	rte_ring_free(refcnt_mbuf_ring);
1170 	return ret;
1171 #else
1172 	return 0;
1173 #endif
1174 }
1175 
1176 #include <unistd.h>
1177 #include <sys/resource.h>
1178 #include <sys/time.h>
1179 #include <sys/wait.h>
1180 
1181 /* use fork() to test mbuf errors panic */
1182 static int
1183 verify_mbuf_check_panics(struct rte_mbuf *buf)
1184 {
1185 	int pid;
1186 	int status;
1187 
1188 	pid = fork();
1189 
1190 	if (pid == 0) {
1191 		struct rlimit rl;
1192 
1193 		/* No need to generate a coredump when panicking. */
1194 		rl.rlim_cur = rl.rlim_max = 0;
1195 		setrlimit(RLIMIT_CORE, &rl);
1196 		rte_mbuf_sanity_check(buf, 1); /* should panic */
1197 		exit(0);  /* return normally if it doesn't panic */
1198 	} else if (pid < 0) {
1199 		printf("Fork Failed\n");
1200 		return -1;
1201 	}
1202 	wait(&status);
1203 	if(status == 0)
1204 		return -1;
1205 
1206 	return 0;
1207 }
1208 
1209 static int
1210 test_failing_mbuf_sanity_check(struct rte_mempool *pktmbuf_pool)
1211 {
1212 	struct rte_mbuf *buf;
1213 	struct rte_mbuf badbuf;
1214 
1215 	printf("Checking rte_mbuf_sanity_check for failure conditions\n");
1216 
1217 	/* get a good mbuf to use to make copies */
1218 	buf = rte_pktmbuf_alloc(pktmbuf_pool);
1219 	if (buf == NULL)
1220 		return -1;
1221 
1222 	printf("Checking good mbuf initially\n");
1223 	if (verify_mbuf_check_panics(buf) != -1)
1224 		return -1;
1225 
1226 	printf("Now checking for error conditions\n");
1227 
1228 	if (verify_mbuf_check_panics(NULL)) {
1229 		printf("Error with NULL mbuf test\n");
1230 		return -1;
1231 	}
1232 
1233 	badbuf = *buf;
1234 	badbuf.pool = NULL;
1235 	if (verify_mbuf_check_panics(&badbuf)) {
1236 		printf("Error with bad-pool mbuf test\n");
1237 		return -1;
1238 	}
1239 
1240 	badbuf = *buf;
1241 	badbuf.buf_iova = 0;
1242 	if (verify_mbuf_check_panics(&badbuf)) {
1243 		printf("Error with bad-physaddr mbuf test\n");
1244 		return -1;
1245 	}
1246 
1247 	badbuf = *buf;
1248 	badbuf.buf_addr = NULL;
1249 	if (verify_mbuf_check_panics(&badbuf)) {
1250 		printf("Error with bad-addr mbuf test\n");
1251 		return -1;
1252 	}
1253 
1254 	badbuf = *buf;
1255 	badbuf.refcnt = 0;
1256 	if (verify_mbuf_check_panics(&badbuf)) {
1257 		printf("Error with bad-refcnt(0) mbuf test\n");
1258 		return -1;
1259 	}
1260 
1261 	badbuf = *buf;
1262 	badbuf.refcnt = UINT16_MAX;
1263 	if (verify_mbuf_check_panics(&badbuf)) {
1264 		printf("Error with bad-refcnt(MAX) mbuf test\n");
1265 		return -1;
1266 	}
1267 
1268 	return 0;
1269 }
1270 
1271 static int
1272 test_mbuf_linearize(struct rte_mempool *pktmbuf_pool, int pkt_len,
1273 		    int nb_segs)
1274 {
1275 
1276 	struct rte_mbuf *m = NULL, *mbuf = NULL;
1277 	uint8_t *data;
1278 	int data_len = 0;
1279 	int remain;
1280 	int seg, seg_len;
1281 	int i;
1282 
1283 	if (pkt_len < 1) {
1284 		printf("Packet size must be 1 or more (is %d)\n", pkt_len);
1285 		return -1;
1286 	}
1287 
1288 	if (nb_segs < 1) {
1289 		printf("Number of segments must be 1 or more (is %d)\n",
1290 				nb_segs);
1291 		return -1;
1292 	}
1293 
1294 	seg_len = pkt_len / nb_segs;
1295 	if (seg_len == 0)
1296 		seg_len = 1;
1297 
1298 	remain = pkt_len;
1299 
1300 	/* Create chained mbuf_src and fill it generated data */
1301 	for (seg = 0; remain > 0; seg++) {
1302 
1303 		m = rte_pktmbuf_alloc(pktmbuf_pool);
1304 		if (m == NULL) {
1305 			printf("Cannot create segment for source mbuf");
1306 			goto fail;
1307 		}
1308 
1309 		/* Make sure if tailroom is zeroed */
1310 		memset(rte_pktmbuf_mtod(m, uint8_t *), 0,
1311 				rte_pktmbuf_tailroom(m));
1312 
1313 		data_len = remain;
1314 		if (data_len > seg_len)
1315 			data_len = seg_len;
1316 
1317 		data = (uint8_t *)rte_pktmbuf_append(m, data_len);
1318 		if (data == NULL) {
1319 			printf("Cannot append %d bytes to the mbuf\n",
1320 					data_len);
1321 			goto fail;
1322 		}
1323 
1324 		for (i = 0; i < data_len; i++)
1325 			data[i] = (seg * seg_len + i) % 0x0ff;
1326 
1327 		if (seg == 0)
1328 			mbuf = m;
1329 		else
1330 			rte_pktmbuf_chain(mbuf, m);
1331 
1332 		remain -= data_len;
1333 	}
1334 
1335 	/* Create destination buffer to store coalesced data */
1336 	if (rte_pktmbuf_linearize(mbuf)) {
1337 		printf("Mbuf linearization failed\n");
1338 		goto fail;
1339 	}
1340 
1341 	if (!rte_pktmbuf_is_contiguous(mbuf)) {
1342 		printf("Source buffer should be contiguous after "
1343 				"linearization\n");
1344 		goto fail;
1345 	}
1346 
1347 	data = rte_pktmbuf_mtod(mbuf, uint8_t *);
1348 
1349 	for (i = 0; i < pkt_len; i++)
1350 		if (data[i] != (i % 0x0ff)) {
1351 			printf("Incorrect data in linearized mbuf\n");
1352 			goto fail;
1353 		}
1354 
1355 	rte_pktmbuf_free(mbuf);
1356 	return 0;
1357 
1358 fail:
1359 	if (mbuf)
1360 		rte_pktmbuf_free(mbuf);
1361 	return -1;
1362 }
1363 
1364 static int
1365 test_mbuf_linearize_check(struct rte_mempool *pktmbuf_pool)
1366 {
1367 	struct test_mbuf_array {
1368 		int size;
1369 		int nb_segs;
1370 	} mbuf_array[] = {
1371 			{ 128, 1 },
1372 			{ 64, 64 },
1373 			{ 512, 10 },
1374 			{ 250, 11 },
1375 			{ 123, 8 },
1376 	};
1377 	unsigned int i;
1378 
1379 	printf("Test mbuf linearize API\n");
1380 
1381 	for (i = 0; i < RTE_DIM(mbuf_array); i++)
1382 		if (test_mbuf_linearize(pktmbuf_pool, mbuf_array[i].size,
1383 				mbuf_array[i].nb_segs)) {
1384 			printf("Test failed for %d, %d\n", mbuf_array[i].size,
1385 					mbuf_array[i].nb_segs);
1386 			return -1;
1387 		}
1388 
1389 	return 0;
1390 }
1391 
1392 /*
1393  * Helper function for test_tx_ofload
1394  */
1395 static inline void
1396 set_tx_offload(struct rte_mbuf *mb, uint64_t il2, uint64_t il3, uint64_t il4,
1397 	uint64_t tso, uint64_t ol3, uint64_t ol2)
1398 {
1399 	mb->l2_len = il2;
1400 	mb->l3_len = il3;
1401 	mb->l4_len = il4;
1402 	mb->tso_segsz = tso;
1403 	mb->outer_l3_len = ol3;
1404 	mb->outer_l2_len = ol2;
1405 }
1406 
1407 static int
1408 test_tx_offload(void)
1409 {
1410 	struct rte_mbuf *mb;
1411 	uint64_t tm, v1, v2;
1412 	size_t sz;
1413 	uint32_t i;
1414 
1415 	static volatile struct {
1416 		uint16_t l2;
1417 		uint16_t l3;
1418 		uint16_t l4;
1419 		uint16_t tso;
1420 	} txof;
1421 
1422 	const uint32_t num = 0x10000;
1423 
1424 	txof.l2 = rte_rand() % (1 <<  RTE_MBUF_L2_LEN_BITS);
1425 	txof.l3 = rte_rand() % (1 <<  RTE_MBUF_L3_LEN_BITS);
1426 	txof.l4 = rte_rand() % (1 <<  RTE_MBUF_L4_LEN_BITS);
1427 	txof.tso = rte_rand() % (1 <<   RTE_MBUF_TSO_SEGSZ_BITS);
1428 
1429 	printf("%s started, tx_offload = {\n"
1430 		"\tl2_len=%#hx,\n"
1431 		"\tl3_len=%#hx,\n"
1432 		"\tl4_len=%#hx,\n"
1433 		"\ttso_segsz=%#hx,\n"
1434 		"\touter_l3_len=%#x,\n"
1435 		"\touter_l2_len=%#x,\n"
1436 		"};\n",
1437 		__func__,
1438 		txof.l2, txof.l3, txof.l4, txof.tso, txof.l3, txof.l2);
1439 
1440 	sz = sizeof(*mb) * num;
1441 	mb = rte_zmalloc(NULL, sz, RTE_CACHE_LINE_SIZE);
1442 	if (mb == NULL) {
1443 		printf("%s failed, out of memory\n", __func__);
1444 		return -ENOMEM;
1445 	}
1446 
1447 	memset(mb, 0, sz);
1448 	tm = rte_rdtsc_precise();
1449 
1450 	for (i = 0; i != num; i++)
1451 		set_tx_offload(mb + i, txof.l2, txof.l3, txof.l4,
1452 			txof.tso, txof.l3, txof.l2);
1453 
1454 	tm = rte_rdtsc_precise() - tm;
1455 	printf("%s set tx_offload by bit-fields: %u iterations, %"
1456 		PRIu64 " cycles, %#Lf cycles/iter\n",
1457 		__func__, num, tm, (long double)tm / num);
1458 
1459 	v1 = mb[rte_rand() % num].tx_offload;
1460 
1461 	memset(mb, 0, sz);
1462 	tm = rte_rdtsc_precise();
1463 
1464 	for (i = 0; i != num; i++)
1465 		mb[i].tx_offload = rte_mbuf_tx_offload(txof.l2, txof.l3,
1466 			txof.l4, txof.tso, txof.l3, txof.l2, 0);
1467 
1468 	tm = rte_rdtsc_precise() - tm;
1469 	printf("%s set raw tx_offload: %u iterations, %"
1470 		PRIu64 " cycles, %#Lf cycles/iter\n",
1471 		__func__, num, tm, (long double)tm / num);
1472 
1473 	v2 = mb[rte_rand() % num].tx_offload;
1474 
1475 	rte_free(mb);
1476 
1477 	printf("%s finished\n"
1478 		"expected tx_offload value: 0x%" PRIx64 ";\n"
1479 		"rte_mbuf_tx_offload value: 0x%" PRIx64 ";\n",
1480 		__func__, v1, v2);
1481 
1482 	return (v1 == v2) ? 0 : -EINVAL;
1483 }
1484 
1485 static int
1486 test_get_rx_ol_flag_list(void)
1487 {
1488 	int len = 6, ret = 0;
1489 	char buf[256] = "";
1490 	int buflen = 0;
1491 
1492 	/* Test case to check with null buffer */
1493 	ret = rte_get_rx_ol_flag_list(0, NULL, 0);
1494 	if (ret != -1)
1495 		GOTO_FAIL("%s expected: -1, received = %d\n", __func__, ret);
1496 
1497 	/* Test case to check with zero buffer len */
1498 	ret = rte_get_rx_ol_flag_list(RTE_MBUF_F_RX_L4_CKSUM_MASK, buf, 0);
1499 	if (ret != -1)
1500 		GOTO_FAIL("%s expected: -1, received = %d\n", __func__, ret);
1501 
1502 	buflen = strlen(buf);
1503 	if (buflen != 0)
1504 		GOTO_FAIL("%s buffer should be empty, received = %d\n",
1505 				__func__, buflen);
1506 
1507 	/* Test case to check with reduced buffer len */
1508 	ret = rte_get_rx_ol_flag_list(0, buf, len);
1509 	if (ret != -1)
1510 		GOTO_FAIL("%s expected: -1, received = %d\n", __func__, ret);
1511 
1512 	buflen = strlen(buf);
1513 	if (buflen != (len - 1))
1514 		GOTO_FAIL("%s invalid buffer length retrieved, expected: %d,"
1515 				"received = %d\n", __func__,
1516 				(len - 1), buflen);
1517 
1518 	/* Test case to check with zero mask value */
1519 	ret = rte_get_rx_ol_flag_list(0, buf, sizeof(buf));
1520 	if (ret != 0)
1521 		GOTO_FAIL("%s expected: 0, received = %d\n", __func__, ret);
1522 
1523 	buflen = strlen(buf);
1524 	if (buflen == 0)
1525 		GOTO_FAIL("%s expected: %s, received length = 0\n", __func__,
1526 				"non-zero, buffer should not be empty");
1527 
1528 	/* Test case to check with valid mask value */
1529 	ret = rte_get_rx_ol_flag_list(RTE_MBUF_F_RX_SEC_OFFLOAD, buf,
1530 				      sizeof(buf));
1531 	if (ret != 0)
1532 		GOTO_FAIL("%s expected: 0, received = %d\n", __func__, ret);
1533 
1534 	buflen = strlen(buf);
1535 	if (buflen == 0)
1536 		GOTO_FAIL("%s expected: %s, received length = 0\n", __func__,
1537 				"non-zero, buffer should not be empty");
1538 
1539 	return 0;
1540 fail:
1541 	return -1;
1542 }
1543 
1544 static int
1545 test_get_tx_ol_flag_list(void)
1546 {
1547 	int len = 6, ret = 0;
1548 	char buf[256] = "";
1549 	int buflen = 0;
1550 
1551 	/* Test case to check with null buffer */
1552 	ret = rte_get_tx_ol_flag_list(0, NULL, 0);
1553 	if (ret != -1)
1554 		GOTO_FAIL("%s expected: -1, received = %d\n", __func__, ret);
1555 
1556 	/* Test case to check with zero buffer len */
1557 	ret = rte_get_tx_ol_flag_list(RTE_MBUF_F_TX_IP_CKSUM, buf, 0);
1558 	if (ret != -1)
1559 		GOTO_FAIL("%s expected: -1, received = %d\n", __func__, ret);
1560 
1561 	buflen = strlen(buf);
1562 	if (buflen != 0) {
1563 		GOTO_FAIL("%s buffer should be empty, received = %d\n",
1564 				__func__, buflen);
1565 	}
1566 
1567 	/* Test case to check with reduced buffer len */
1568 	ret = rte_get_tx_ol_flag_list(0, buf, len);
1569 	if (ret != -1)
1570 		GOTO_FAIL("%s expected: -1, received = %d\n", __func__, ret);
1571 
1572 	buflen = strlen(buf);
1573 	if (buflen != (len - 1))
1574 		GOTO_FAIL("%s invalid buffer length retrieved, expected: %d,"
1575 				"received = %d\n", __func__,
1576 				(len - 1), buflen);
1577 
1578 	/* Test case to check with zero mask value */
1579 	ret = rte_get_tx_ol_flag_list(0, buf, sizeof(buf));
1580 	if (ret != 0)
1581 		GOTO_FAIL("%s expected: 0, received = %d\n", __func__, ret);
1582 
1583 	buflen = strlen(buf);
1584 	if (buflen == 0)
1585 		GOTO_FAIL("%s expected: %s, received length = 0\n", __func__,
1586 				"non-zero, buffer should not be empty");
1587 
1588 	/* Test case to check with valid mask value */
1589 	ret = rte_get_tx_ol_flag_list(RTE_MBUF_F_TX_UDP_CKSUM, buf,
1590 				      sizeof(buf));
1591 	if (ret != 0)
1592 		GOTO_FAIL("%s expected: 0, received = %d\n", __func__, ret);
1593 
1594 	buflen = strlen(buf);
1595 	if (buflen == 0)
1596 		GOTO_FAIL("%s expected: %s, received length = 0\n", __func__,
1597 				"non-zero, buffer should not be empty");
1598 
1599 	return 0;
1600 fail:
1601 	return -1;
1602 
1603 }
1604 
1605 struct flag_name {
1606 	uint64_t flag;
1607 	const char *name;
1608 };
1609 
1610 static int
1611 test_get_rx_ol_flag_name(void)
1612 {
1613 	uint16_t i;
1614 	const char *flag_str = NULL;
1615 	const struct flag_name rx_flags[] = {
1616 		VAL_NAME(RTE_MBUF_F_RX_VLAN),
1617 		VAL_NAME(RTE_MBUF_F_RX_RSS_HASH),
1618 		VAL_NAME(RTE_MBUF_F_RX_FDIR),
1619 		VAL_NAME(RTE_MBUF_F_RX_L4_CKSUM_BAD),
1620 		VAL_NAME(RTE_MBUF_F_RX_L4_CKSUM_GOOD),
1621 		VAL_NAME(RTE_MBUF_F_RX_L4_CKSUM_NONE),
1622 		VAL_NAME(RTE_MBUF_F_RX_IP_CKSUM_BAD),
1623 		VAL_NAME(RTE_MBUF_F_RX_IP_CKSUM_GOOD),
1624 		VAL_NAME(RTE_MBUF_F_RX_IP_CKSUM_NONE),
1625 		VAL_NAME(RTE_MBUF_F_RX_OUTER_IP_CKSUM_BAD),
1626 		VAL_NAME(RTE_MBUF_F_RX_VLAN_STRIPPED),
1627 		VAL_NAME(RTE_MBUF_F_RX_IEEE1588_PTP),
1628 		VAL_NAME(RTE_MBUF_F_RX_IEEE1588_TMST),
1629 		VAL_NAME(RTE_MBUF_F_RX_FDIR_ID),
1630 		VAL_NAME(RTE_MBUF_F_RX_FDIR_FLX),
1631 		VAL_NAME(RTE_MBUF_F_RX_QINQ_STRIPPED),
1632 		VAL_NAME(RTE_MBUF_F_RX_LRO),
1633 		VAL_NAME(RTE_MBUF_F_RX_SEC_OFFLOAD),
1634 		VAL_NAME(RTE_MBUF_F_RX_SEC_OFFLOAD_FAILED),
1635 		VAL_NAME(RTE_MBUF_F_RX_OUTER_L4_CKSUM_BAD),
1636 		VAL_NAME(RTE_MBUF_F_RX_OUTER_L4_CKSUM_GOOD),
1637 		VAL_NAME(RTE_MBUF_F_RX_OUTER_L4_CKSUM_INVALID),
1638 	};
1639 
1640 	/* Test case to check with valid flag */
1641 	for (i = 0; i < RTE_DIM(rx_flags); i++) {
1642 		flag_str = rte_get_rx_ol_flag_name(rx_flags[i].flag);
1643 		if (flag_str == NULL)
1644 			GOTO_FAIL("%s: Expected flagname = %s; received null\n",
1645 					__func__, rx_flags[i].name);
1646 		if (strcmp(flag_str, rx_flags[i].name) != 0)
1647 			GOTO_FAIL("%s: Expected flagname = %s; received = %s\n",
1648 				__func__, rx_flags[i].name, flag_str);
1649 	}
1650 	/* Test case to check with invalid flag */
1651 	flag_str = rte_get_rx_ol_flag_name(0);
1652 	if (flag_str != NULL) {
1653 		GOTO_FAIL("%s: Expected flag name = null; received = %s\n",
1654 				__func__, flag_str);
1655 	}
1656 
1657 	return 0;
1658 fail:
1659 	return -1;
1660 }
1661 
1662 static int
1663 test_get_tx_ol_flag_name(void)
1664 {
1665 	uint16_t i;
1666 	const char *flag_str = NULL;
1667 	const struct flag_name tx_flags[] = {
1668 		VAL_NAME(RTE_MBUF_F_TX_VLAN),
1669 		VAL_NAME(RTE_MBUF_F_TX_IP_CKSUM),
1670 		VAL_NAME(RTE_MBUF_F_TX_TCP_CKSUM),
1671 		VAL_NAME(RTE_MBUF_F_TX_SCTP_CKSUM),
1672 		VAL_NAME(RTE_MBUF_F_TX_UDP_CKSUM),
1673 		VAL_NAME(RTE_MBUF_F_TX_IEEE1588_TMST),
1674 		VAL_NAME(RTE_MBUF_F_TX_TCP_SEG),
1675 		VAL_NAME(RTE_MBUF_F_TX_IPV4),
1676 		VAL_NAME(RTE_MBUF_F_TX_IPV6),
1677 		VAL_NAME(RTE_MBUF_F_TX_OUTER_IP_CKSUM),
1678 		VAL_NAME(RTE_MBUF_F_TX_OUTER_IPV4),
1679 		VAL_NAME(RTE_MBUF_F_TX_OUTER_IPV6),
1680 		VAL_NAME(RTE_MBUF_F_TX_TUNNEL_VXLAN),
1681 		VAL_NAME(RTE_MBUF_F_TX_TUNNEL_GRE),
1682 		VAL_NAME(RTE_MBUF_F_TX_TUNNEL_IPIP),
1683 		VAL_NAME(RTE_MBUF_F_TX_TUNNEL_GENEVE),
1684 		VAL_NAME(RTE_MBUF_F_TX_TUNNEL_MPLSINUDP),
1685 		VAL_NAME(RTE_MBUF_F_TX_TUNNEL_VXLAN_GPE),
1686 		VAL_NAME(RTE_MBUF_F_TX_TUNNEL_IP),
1687 		VAL_NAME(RTE_MBUF_F_TX_TUNNEL_UDP),
1688 		VAL_NAME(RTE_MBUF_F_TX_QINQ),
1689 		VAL_NAME(RTE_MBUF_F_TX_MACSEC),
1690 		VAL_NAME(RTE_MBUF_F_TX_SEC_OFFLOAD),
1691 		VAL_NAME(RTE_MBUF_F_TX_UDP_SEG),
1692 		VAL_NAME(RTE_MBUF_F_TX_OUTER_UDP_CKSUM),
1693 	};
1694 
1695 	/* Test case to check with valid flag */
1696 	for (i = 0; i < RTE_DIM(tx_flags); i++) {
1697 		flag_str = rte_get_tx_ol_flag_name(tx_flags[i].flag);
1698 		if (flag_str == NULL)
1699 			GOTO_FAIL("%s: Expected flagname = %s; received null\n",
1700 				__func__, tx_flags[i].name);
1701 		if (strcmp(flag_str, tx_flags[i].name) != 0)
1702 			GOTO_FAIL("%s: Expected flagname = %s; received = %s\n",
1703 				__func__, tx_flags[i].name, flag_str);
1704 	}
1705 	/* Test case to check with invalid flag */
1706 	flag_str = rte_get_tx_ol_flag_name(0);
1707 	if (flag_str != NULL) {
1708 		GOTO_FAIL("%s: Expected flag name = null; received = %s\n",
1709 				__func__, flag_str);
1710 	}
1711 
1712 	return 0;
1713 fail:
1714 	return -1;
1715 
1716 }
1717 
1718 static int
1719 test_mbuf_validate_tx_offload(const char *test_name,
1720 		struct rte_mempool *pktmbuf_pool,
1721 		uint64_t ol_flags,
1722 		uint16_t segsize,
1723 		int expected_retval)
1724 {
1725 	struct rte_mbuf *m = NULL;
1726 	int ret = 0;
1727 
1728 	/* alloc a mbuf and do sanity check */
1729 	m = rte_pktmbuf_alloc(pktmbuf_pool);
1730 	if (m == NULL)
1731 		GOTO_FAIL("%s: mbuf allocation failed!\n", __func__);
1732 	if (rte_pktmbuf_pkt_len(m) != 0)
1733 		GOTO_FAIL("%s: Bad packet length\n", __func__);
1734 	rte_mbuf_sanity_check(m, 0);
1735 	m->ol_flags = ol_flags;
1736 	m->tso_segsz = segsize;
1737 	ret = rte_validate_tx_offload(m);
1738 	if (ret != expected_retval)
1739 		GOTO_FAIL("%s(%s): expected ret val: %d; received: %d\n",
1740 				__func__, test_name, expected_retval, ret);
1741 	rte_pktmbuf_free(m);
1742 	m = NULL;
1743 	return 0;
1744 fail:
1745 	if (m) {
1746 		rte_pktmbuf_free(m);
1747 		m = NULL;
1748 	}
1749 	return -1;
1750 }
1751 
1752 static int
1753 test_mbuf_validate_tx_offload_one(struct rte_mempool *pktmbuf_pool)
1754 {
1755 	/* test to validate tx offload flags */
1756 	uint64_t ol_flags = 0;
1757 
1758 	/* test to validate if IP checksum is counted only for IPV4 packet */
1759 	/* set both IP checksum and IPV6 flags */
1760 	ol_flags |= RTE_MBUF_F_TX_IP_CKSUM;
1761 	ol_flags |= RTE_MBUF_F_TX_IPV6;
1762 	if (test_mbuf_validate_tx_offload("MBUF_TEST_IP_CKSUM_IPV6_SET",
1763 				pktmbuf_pool,
1764 				ol_flags, 0, -EINVAL) < 0)
1765 		GOTO_FAIL("%s failed: IP cksum is set incorrect.\n", __func__);
1766 	/* resetting ol_flags for next testcase */
1767 	ol_flags = 0;
1768 
1769 	/* test to validate if IP type is set when required */
1770 	ol_flags |= RTE_MBUF_F_TX_L4_MASK;
1771 	if (test_mbuf_validate_tx_offload("MBUF_TEST_IP_TYPE_NOT_SET",
1772 				pktmbuf_pool,
1773 				ol_flags, 0, -EINVAL) < 0)
1774 		GOTO_FAIL("%s failed: IP type is not set.\n", __func__);
1775 
1776 	/* test if IP type is set when TCP SEG is on */
1777 	ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
1778 	if (test_mbuf_validate_tx_offload("MBUF_TEST_IP_TYPE_NOT_SET",
1779 				pktmbuf_pool,
1780 				ol_flags, 0, -EINVAL) < 0)
1781 		GOTO_FAIL("%s failed: IP type is not set.\n", __func__);
1782 
1783 	ol_flags = 0;
1784 	/* test to confirm IP type (IPV4/IPV6) is set */
1785 	ol_flags = RTE_MBUF_F_TX_L4_MASK;
1786 	ol_flags |= RTE_MBUF_F_TX_IPV6;
1787 	if (test_mbuf_validate_tx_offload("MBUF_TEST_IP_TYPE_SET",
1788 				pktmbuf_pool,
1789 				ol_flags, 0, 0) < 0)
1790 		GOTO_FAIL("%s failed: tx offload flag error.\n", __func__);
1791 
1792 	ol_flags = 0;
1793 	/* test to check TSO segment size is non-zero */
1794 	ol_flags |= RTE_MBUF_F_TX_IPV4;
1795 	ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
1796 	/* set 0 tso segment size */
1797 	if (test_mbuf_validate_tx_offload("MBUF_TEST_NULL_TSO_SEGSZ",
1798 				pktmbuf_pool,
1799 				ol_flags, 0, -EINVAL) < 0)
1800 		GOTO_FAIL("%s failed: tso segment size is null.\n", __func__);
1801 
1802 	/* retain IPV4 and RTE_MBUF_F_TX_TCP_SEG mask */
1803 	/* set valid tso segment size but IP CKSUM not set */
1804 	if (test_mbuf_validate_tx_offload("MBUF_TEST_TSO_IP_CKSUM_NOT_SET",
1805 				pktmbuf_pool,
1806 				ol_flags, 512, -EINVAL) < 0)
1807 		GOTO_FAIL("%s failed: IP CKSUM is not set.\n", __func__);
1808 
1809 	/* test to validate if IP checksum is set for TSO capability */
1810 	/* retain IPV4, TCP_SEG, tso_seg size */
1811 	ol_flags |= RTE_MBUF_F_TX_IP_CKSUM;
1812 	if (test_mbuf_validate_tx_offload("MBUF_TEST_TSO_IP_CKSUM_SET",
1813 				pktmbuf_pool,
1814 				ol_flags, 512, 0) < 0)
1815 		GOTO_FAIL("%s failed: tx offload flag error.\n", __func__);
1816 
1817 	/* test to confirm TSO for IPV6 type */
1818 	ol_flags = 0;
1819 	ol_flags |= RTE_MBUF_F_TX_IPV6;
1820 	ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
1821 	if (test_mbuf_validate_tx_offload("MBUF_TEST_TSO_IPV6_SET",
1822 				pktmbuf_pool,
1823 				ol_flags, 512, 0) < 0)
1824 		GOTO_FAIL("%s failed: TSO req not met.\n", __func__);
1825 
1826 	ol_flags = 0;
1827 	/* test if outer IP checksum set for non outer IPv4 packet */
1828 	ol_flags |= RTE_MBUF_F_TX_IPV6;
1829 	ol_flags |= RTE_MBUF_F_TX_OUTER_IP_CKSUM;
1830 	if (test_mbuf_validate_tx_offload("MBUF_TEST_OUTER_IPV4_NOT_SET",
1831 				pktmbuf_pool,
1832 				ol_flags, 512, -EINVAL) < 0)
1833 		GOTO_FAIL("%s failed: Outer IP cksum set.\n", __func__);
1834 
1835 	ol_flags = 0;
1836 	/* test to confirm outer IP checksum is set for outer IPV4 packet */
1837 	ol_flags |= RTE_MBUF_F_TX_OUTER_IP_CKSUM;
1838 	ol_flags |= RTE_MBUF_F_TX_OUTER_IPV4;
1839 	if (test_mbuf_validate_tx_offload("MBUF_TEST_OUTER_IPV4_SET",
1840 				pktmbuf_pool,
1841 				ol_flags, 512, 0) < 0)
1842 		GOTO_FAIL("%s failed: tx offload flag error.\n", __func__);
1843 
1844 	ol_flags = 0;
1845 	/* test to confirm if packets with no TX_OFFLOAD_MASK are skipped */
1846 	if (test_mbuf_validate_tx_offload("MBUF_TEST_OL_MASK_NOT_SET",
1847 				pktmbuf_pool,
1848 				ol_flags, 512, 0) < 0)
1849 		GOTO_FAIL("%s failed: tx offload flag error.\n", __func__);
1850 	return 0;
1851 fail:
1852 	return -1;
1853 }
1854 
1855 /*
1856  * Test for allocating a bulk of mbufs
1857  * define an array with positive sizes for mbufs allocations.
1858  */
1859 static int
1860 test_pktmbuf_alloc_bulk(struct rte_mempool *pktmbuf_pool)
1861 {
1862 	int ret = 0;
1863 	unsigned int idx, loop;
1864 	unsigned int alloc_counts[] = {
1865 		0,
1866 		MEMPOOL_CACHE_SIZE - 1,
1867 		MEMPOOL_CACHE_SIZE + 1,
1868 		MEMPOOL_CACHE_SIZE * 1.5,
1869 		MEMPOOL_CACHE_SIZE * 2,
1870 		MEMPOOL_CACHE_SIZE * 2 - 1,
1871 		MEMPOOL_CACHE_SIZE * 2 + 1,
1872 		MEMPOOL_CACHE_SIZE,
1873 	};
1874 
1875 	/* allocate a large array of mbuf pointers */
1876 	struct rte_mbuf *mbufs[NB_MBUF] = { 0 };
1877 	for (idx = 0; idx < RTE_DIM(alloc_counts); idx++) {
1878 		ret = rte_pktmbuf_alloc_bulk(pktmbuf_pool, mbufs,
1879 				alloc_counts[idx]);
1880 		if (ret == 0) {
1881 			for (loop = 0; loop < alloc_counts[idx] &&
1882 					mbufs[loop] != NULL; loop++)
1883 				rte_pktmbuf_free(mbufs[loop]);
1884 		} else if (ret != 0) {
1885 			printf("%s: Bulk alloc failed count(%u); ret val(%d)\n",
1886 					__func__, alloc_counts[idx], ret);
1887 			return -1;
1888 		}
1889 	}
1890 	return 0;
1891 }
1892 
1893 /*
1894  * Negative testing for allocating a bulk of mbufs
1895  */
1896 static int
1897 test_neg_pktmbuf_alloc_bulk(struct rte_mempool *pktmbuf_pool)
1898 {
1899 	int ret = 0;
1900 	unsigned int idx, loop;
1901 	unsigned int neg_alloc_counts[] = {
1902 		MEMPOOL_CACHE_SIZE - NB_MBUF,
1903 		NB_MBUF + 1,
1904 		NB_MBUF * 8,
1905 		UINT_MAX
1906 	};
1907 	struct rte_mbuf *mbufs[NB_MBUF * 8] = { 0 };
1908 
1909 	for (idx = 0; idx < RTE_DIM(neg_alloc_counts); idx++) {
1910 		ret = rte_pktmbuf_alloc_bulk(pktmbuf_pool, mbufs,
1911 				neg_alloc_counts[idx]);
1912 		if (ret == 0) {
1913 			printf("%s: Bulk alloc must fail! count(%u); ret(%d)\n",
1914 					__func__, neg_alloc_counts[idx], ret);
1915 			for (loop = 0; loop < neg_alloc_counts[idx] &&
1916 					mbufs[loop] != NULL; loop++)
1917 				rte_pktmbuf_free(mbufs[loop]);
1918 			return -1;
1919 		}
1920 	}
1921 	return 0;
1922 }
1923 
1924 /*
1925  * Test to read mbuf packet using rte_pktmbuf_read
1926  */
1927 static int
1928 test_pktmbuf_read(struct rte_mempool *pktmbuf_pool)
1929 {
1930 	struct rte_mbuf *m = NULL;
1931 	char *data = NULL;
1932 	const char *data_copy = NULL;
1933 	int off;
1934 
1935 	/* alloc a mbuf */
1936 	m = rte_pktmbuf_alloc(pktmbuf_pool);
1937 	if (m == NULL)
1938 		GOTO_FAIL("%s: mbuf allocation failed!\n", __func__);
1939 	if (rte_pktmbuf_pkt_len(m) != 0)
1940 		GOTO_FAIL("%s: Bad packet length\n", __func__);
1941 	rte_mbuf_sanity_check(m, 0);
1942 
1943 	data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN2);
1944 	if (data == NULL)
1945 		GOTO_FAIL("%s: Cannot append data\n", __func__);
1946 	if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN2)
1947 		GOTO_FAIL("%s: Bad packet length\n", __func__);
1948 	memset(data, 0xfe, MBUF_TEST_DATA_LEN2);
1949 
1950 	/* read the data from mbuf */
1951 	data_copy = rte_pktmbuf_read(m, 0, MBUF_TEST_DATA_LEN2, NULL);
1952 	if (data_copy == NULL)
1953 		GOTO_FAIL("%s: Error in reading data!\n", __func__);
1954 	for (off = 0; off < MBUF_TEST_DATA_LEN2; off++) {
1955 		if (data_copy[off] != (char)0xfe)
1956 			GOTO_FAIL("Data corrupted at offset %u", off);
1957 	}
1958 	rte_pktmbuf_free(m);
1959 	m = NULL;
1960 
1961 	return 0;
1962 fail:
1963 	if (m) {
1964 		rte_pktmbuf_free(m);
1965 		m = NULL;
1966 	}
1967 	return -1;
1968 }
1969 
1970 /*
1971  * Test to read mbuf packet data from offset
1972  */
1973 static int
1974 test_pktmbuf_read_from_offset(struct rte_mempool *pktmbuf_pool)
1975 {
1976 	struct rte_mbuf *m = NULL;
1977 	struct ether_hdr *hdr = NULL;
1978 	char *data = NULL;
1979 	const char *data_copy = NULL;
1980 	unsigned int off;
1981 	unsigned int hdr_len = sizeof(struct rte_ether_hdr);
1982 
1983 	/* alloc a mbuf */
1984 	m = rte_pktmbuf_alloc(pktmbuf_pool);
1985 	if (m == NULL)
1986 		GOTO_FAIL("%s: mbuf allocation failed!\n", __func__);
1987 
1988 	if (rte_pktmbuf_pkt_len(m) != 0)
1989 		GOTO_FAIL("%s: Bad packet length\n", __func__);
1990 	rte_mbuf_sanity_check(m, 0);
1991 
1992 	/* prepend an ethernet header */
1993 	hdr = (struct ether_hdr *)rte_pktmbuf_prepend(m, hdr_len);
1994 	if (hdr == NULL)
1995 		GOTO_FAIL("%s: Cannot prepend header\n", __func__);
1996 	if (rte_pktmbuf_pkt_len(m) != hdr_len)
1997 		GOTO_FAIL("%s: Bad pkt length", __func__);
1998 	if (rte_pktmbuf_data_len(m) != hdr_len)
1999 		GOTO_FAIL("%s: Bad data length", __func__);
2000 	memset(hdr, 0xde, hdr_len);
2001 
2002 	/* read mbuf header info from 0 offset */
2003 	data_copy = rte_pktmbuf_read(m, 0, hdr_len, NULL);
2004 	if (data_copy == NULL)
2005 		GOTO_FAIL("%s: Error in reading header!\n", __func__);
2006 	for (off = 0; off < hdr_len; off++) {
2007 		if (data_copy[off] != (char)0xde)
2008 			GOTO_FAIL("Header info corrupted at offset %u", off);
2009 	}
2010 
2011 	/* append sample data after ethernet header */
2012 	data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN2);
2013 	if (data == NULL)
2014 		GOTO_FAIL("%s: Cannot append data\n", __func__);
2015 	if (rte_pktmbuf_pkt_len(m) != hdr_len + MBUF_TEST_DATA_LEN2)
2016 		GOTO_FAIL("%s: Bad packet length\n", __func__);
2017 	if (rte_pktmbuf_data_len(m) != hdr_len + MBUF_TEST_DATA_LEN2)
2018 		GOTO_FAIL("%s: Bad data length\n", __func__);
2019 	memset(data, 0xcc, MBUF_TEST_DATA_LEN2);
2020 
2021 	/* read mbuf data after header info */
2022 	data_copy = rte_pktmbuf_read(m, hdr_len, MBUF_TEST_DATA_LEN2, NULL);
2023 	if (data_copy == NULL)
2024 		GOTO_FAIL("%s: Error in reading header data!\n", __func__);
2025 	for (off = 0; off < MBUF_TEST_DATA_LEN2; off++) {
2026 		if (data_copy[off] != (char)0xcc)
2027 			GOTO_FAIL("Data corrupted at offset %u", off);
2028 	}
2029 
2030 	/* partial reading of mbuf data */
2031 	data_copy = rte_pktmbuf_read(m, hdr_len + 5, MBUF_TEST_DATA_LEN2 - 5,
2032 			NULL);
2033 	if (data_copy == NULL)
2034 		GOTO_FAIL("%s: Error in reading packet data!\n", __func__);
2035 	if (strlen(data_copy) != MBUF_TEST_DATA_LEN2 - 5)
2036 		GOTO_FAIL("%s: Incorrect data length!\n", __func__);
2037 	for (off = 0; off < MBUF_TEST_DATA_LEN2 - 5; off++) {
2038 		if (data_copy[off] != (char)0xcc)
2039 			GOTO_FAIL("Data corrupted at offset %u", off);
2040 	}
2041 
2042 	/* read length greater than mbuf data_len */
2043 	if (rte_pktmbuf_read(m, hdr_len, rte_pktmbuf_data_len(m) + 1,
2044 				NULL) != NULL)
2045 		GOTO_FAIL("%s: Requested len is larger than mbuf data len!\n",
2046 				__func__);
2047 
2048 	/* read length greater than mbuf pkt_len */
2049 	if (rte_pktmbuf_read(m, hdr_len, rte_pktmbuf_pkt_len(m) + 1,
2050 				NULL) != NULL)
2051 		GOTO_FAIL("%s: Requested len is larger than mbuf pkt len!\n",
2052 				__func__);
2053 
2054 	/* read data of zero len from valid offset */
2055 	data_copy = rte_pktmbuf_read(m, hdr_len, 0, NULL);
2056 	if (data_copy == NULL)
2057 		GOTO_FAIL("%s: Error in reading packet data!\n", __func__);
2058 	if (strlen(data_copy) != MBUF_TEST_DATA_LEN2)
2059 		GOTO_FAIL("%s: Corrupted data content!\n", __func__);
2060 	for (off = 0; off < MBUF_TEST_DATA_LEN2; off++) {
2061 		if (data_copy[off] != (char)0xcc)
2062 			GOTO_FAIL("Data corrupted at offset %u", off);
2063 	}
2064 
2065 	/* read data of zero length from zero offset */
2066 	data_copy = rte_pktmbuf_read(m, 0, 0, NULL);
2067 	if (data_copy == NULL)
2068 		GOTO_FAIL("%s: Error in reading packet data!\n", __func__);
2069 	/* check if the received address is the beginning of header info */
2070 	if (hdr != (const struct ether_hdr *)data_copy)
2071 		GOTO_FAIL("%s: Corrupted data address!\n", __func__);
2072 
2073 	/* read data of max length from valid offset */
2074 	data_copy = rte_pktmbuf_read(m, hdr_len, UINT_MAX, NULL);
2075 	if (data_copy == NULL)
2076 		GOTO_FAIL("%s: Error in reading packet data!\n", __func__);
2077 	/* check if the received address is the beginning of data segment */
2078 	if (data_copy != data)
2079 		GOTO_FAIL("%s: Corrupted data address!\n", __func__);
2080 
2081 	/* try to read from mbuf with max size offset */
2082 	data_copy = rte_pktmbuf_read(m, UINT_MAX, 0, NULL);
2083 	if (data_copy != NULL)
2084 		GOTO_FAIL("%s: Error in reading packet data!\n", __func__);
2085 
2086 	/* try to read from mbuf with max size offset and len */
2087 	data_copy = rte_pktmbuf_read(m, UINT_MAX, UINT_MAX, NULL);
2088 	if (data_copy != NULL)
2089 		GOTO_FAIL("%s: Error in reading packet data!\n", __func__);
2090 
2091 	rte_pktmbuf_dump(stdout, m, rte_pktmbuf_pkt_len(m));
2092 
2093 	rte_pktmbuf_free(m);
2094 	m = NULL;
2095 
2096 	return 0;
2097 fail:
2098 	if (m) {
2099 		rte_pktmbuf_free(m);
2100 		m = NULL;
2101 	}
2102 	return -1;
2103 }
2104 
2105 struct test_case {
2106 	unsigned int seg_count;
2107 	unsigned int flags;
2108 	uint32_t read_off;
2109 	uint32_t read_len;
2110 	unsigned int seg_lengths[MBUF_MAX_SEG];
2111 };
2112 
2113 /* create a mbuf with different sized segments
2114  *  and fill with data [0x00 0x01 0x02 ...]
2115  */
2116 static struct rte_mbuf *
2117 create_packet(struct rte_mempool *pktmbuf_pool,
2118 		struct test_case *test_data)
2119 {
2120 	uint16_t i, ret, seg, seg_len = 0;
2121 	uint32_t last_index = 0;
2122 	unsigned int seg_lengths[MBUF_MAX_SEG];
2123 	unsigned int hdr_len;
2124 	struct rte_mbuf *pkt = NULL;
2125 	struct rte_mbuf	*pkt_seg = NULL;
2126 	char *hdr = NULL;
2127 	char *data = NULL;
2128 
2129 	memcpy(seg_lengths, test_data->seg_lengths,
2130 			sizeof(unsigned int)*test_data->seg_count);
2131 	for (seg = 0; seg < test_data->seg_count; seg++) {
2132 		hdr_len = 0;
2133 		seg_len =  seg_lengths[seg];
2134 		pkt_seg = rte_pktmbuf_alloc(pktmbuf_pool);
2135 		if (pkt_seg == NULL)
2136 			GOTO_FAIL("%s: mbuf allocation failed!\n", __func__);
2137 		if (rte_pktmbuf_pkt_len(pkt_seg) != 0)
2138 			GOTO_FAIL("%s: Bad packet length\n", __func__);
2139 		rte_mbuf_sanity_check(pkt_seg, 0);
2140 		/* Add header only for the first segment */
2141 		if (test_data->flags == MBUF_HEADER && seg == 0) {
2142 			hdr_len = sizeof(struct rte_ether_hdr);
2143 			/* prepend a header and fill with dummy data */
2144 			hdr = (char *)rte_pktmbuf_prepend(pkt_seg, hdr_len);
2145 			if (hdr == NULL)
2146 				GOTO_FAIL("%s: Cannot prepend header\n",
2147 						__func__);
2148 			if (rte_pktmbuf_pkt_len(pkt_seg) != hdr_len)
2149 				GOTO_FAIL("%s: Bad pkt length", __func__);
2150 			if (rte_pktmbuf_data_len(pkt_seg) != hdr_len)
2151 				GOTO_FAIL("%s: Bad data length", __func__);
2152 			for (i = 0; i < hdr_len; i++)
2153 				hdr[i] = (last_index + i) % 0xffff;
2154 			last_index += hdr_len;
2155 		}
2156 		/* skip appending segment with 0 length */
2157 		if (seg_len == 0)
2158 			continue;
2159 		data = rte_pktmbuf_append(pkt_seg, seg_len);
2160 		if (data == NULL)
2161 			GOTO_FAIL("%s: Cannot append data segment\n", __func__);
2162 		if (rte_pktmbuf_pkt_len(pkt_seg) != hdr_len + seg_len)
2163 			GOTO_FAIL("%s: Bad packet segment length: %d\n",
2164 					__func__, rte_pktmbuf_pkt_len(pkt_seg));
2165 		if (rte_pktmbuf_data_len(pkt_seg) != hdr_len + seg_len)
2166 			GOTO_FAIL("%s: Bad data length\n", __func__);
2167 		for (i = 0; i < seg_len; i++)
2168 			data[i] = (last_index + i) % 0xffff;
2169 		/* to fill continuous data from one seg to another */
2170 		last_index += i;
2171 		/* create chained mbufs */
2172 		if (seg == 0)
2173 			pkt = pkt_seg;
2174 		else {
2175 			ret = rte_pktmbuf_chain(pkt, pkt_seg);
2176 			if (ret != 0)
2177 				GOTO_FAIL("%s:FAIL: Chained mbuf creation %d\n",
2178 						__func__, ret);
2179 		}
2180 
2181 		pkt_seg = pkt_seg->next;
2182 	}
2183 	return pkt;
2184 fail:
2185 	if (pkt != NULL) {
2186 		rte_pktmbuf_free(pkt);
2187 		pkt = NULL;
2188 	}
2189 	if (pkt_seg != NULL) {
2190 		rte_pktmbuf_free(pkt_seg);
2191 		pkt_seg = NULL;
2192 	}
2193 	return NULL;
2194 }
2195 
2196 static int
2197 test_pktmbuf_read_from_chain(struct rte_mempool *pktmbuf_pool)
2198 {
2199 	struct rte_mbuf *m;
2200 	struct test_case test_cases[] = {
2201 		{
2202 			.seg_lengths = { 100, 100, 100 },
2203 			.seg_count = 3,
2204 			.flags = MBUF_NO_HEADER,
2205 			.read_off = 0,
2206 			.read_len = 300
2207 		},
2208 		{
2209 			.seg_lengths = { 100, 125, 150 },
2210 			.seg_count = 3,
2211 			.flags = MBUF_NO_HEADER,
2212 			.read_off = 99,
2213 			.read_len = 201
2214 		},
2215 		{
2216 			.seg_lengths = { 100, 100 },
2217 			.seg_count = 2,
2218 			.flags = MBUF_NO_HEADER,
2219 			.read_off = 0,
2220 			.read_len = 100
2221 		},
2222 		{
2223 			.seg_lengths = { 100, 200 },
2224 			.seg_count = 2,
2225 			.flags = MBUF_HEADER,
2226 			.read_off = sizeof(struct rte_ether_hdr),
2227 			.read_len = 150
2228 		},
2229 		{
2230 			.seg_lengths = { 1000, 100 },
2231 			.seg_count = 2,
2232 			.flags = MBUF_NO_HEADER,
2233 			.read_off = 0,
2234 			.read_len = 1000
2235 		},
2236 		{
2237 			.seg_lengths = { 1024, 0, 100 },
2238 			.seg_count = 3,
2239 			.flags = MBUF_NO_HEADER,
2240 			.read_off = 100,
2241 			.read_len = 1001
2242 		},
2243 		{
2244 			.seg_lengths = { 1000, 1, 1000 },
2245 			.seg_count = 3,
2246 			.flags = MBUF_NO_HEADER,
2247 			.read_off = 1000,
2248 			.read_len = 2
2249 		},
2250 		{
2251 			.seg_lengths = { MBUF_TEST_DATA_LEN,
2252 					MBUF_TEST_DATA_LEN2,
2253 					MBUF_TEST_DATA_LEN3, 800, 10 },
2254 			.seg_count = 5,
2255 			.flags = MBUF_NEG_TEST_READ,
2256 			.read_off = 1000,
2257 			.read_len = MBUF_DATA_SIZE
2258 		},
2259 	};
2260 
2261 	uint32_t i, pos;
2262 	const char *data_copy = NULL;
2263 	char data_buf[MBUF_DATA_SIZE];
2264 
2265 	memset(data_buf, 0, MBUF_DATA_SIZE);
2266 
2267 	for (i = 0; i < RTE_DIM(test_cases); i++) {
2268 		m = create_packet(pktmbuf_pool, &test_cases[i]);
2269 		if (m == NULL)
2270 			GOTO_FAIL("%s: mbuf allocation failed!\n", __func__);
2271 
2272 		data_copy = rte_pktmbuf_read(m, test_cases[i].read_off,
2273 				test_cases[i].read_len, data_buf);
2274 		if (test_cases[i].flags == MBUF_NEG_TEST_READ) {
2275 			if (data_copy != NULL)
2276 				GOTO_FAIL("%s: mbuf data read should fail!\n",
2277 						__func__);
2278 			else {
2279 				rte_pktmbuf_free(m);
2280 				m = NULL;
2281 				continue;
2282 			}
2283 		}
2284 		if (data_copy == NULL)
2285 			GOTO_FAIL("%s: Error in reading packet data!\n",
2286 					__func__);
2287 		for (pos = 0; pos < test_cases[i].read_len; pos++) {
2288 			if (data_copy[pos] !=
2289 					(char)((test_cases[i].read_off + pos)
2290 						% 0xffff))
2291 				GOTO_FAIL("Data corrupted at offset %u is %2X",
2292 						pos, data_copy[pos]);
2293 		}
2294 		rte_pktmbuf_dump(stdout, m, rte_pktmbuf_pkt_len(m));
2295 		rte_pktmbuf_free(m);
2296 		m = NULL;
2297 	}
2298 	return 0;
2299 
2300 fail:
2301 	if (m != NULL) {
2302 		rte_pktmbuf_free(m);
2303 		m = NULL;
2304 	}
2305 	return -1;
2306 }
2307 
2308 /* Define a free call back function to be used for external buffer */
2309 static void
2310 ext_buf_free_callback_fn(void *addr __rte_unused, void *opaque)
2311 {
2312 	void *ext_buf_addr = opaque;
2313 
2314 	if (ext_buf_addr == NULL) {
2315 		printf("External buffer address is invalid\n");
2316 		return;
2317 	}
2318 	rte_free(ext_buf_addr);
2319 	ext_buf_addr = NULL;
2320 	printf("External buffer freed via callback\n");
2321 }
2322 
2323 /*
2324  * Test to initialize shared data in external buffer before attaching to mbuf
2325  *  - Allocate mbuf with no data.
2326  *  - Allocate external buffer with size should be large enough to accommodate
2327  *     rte_mbuf_ext_shared_info.
2328  *  - Invoke pktmbuf_ext_shinfo_init_helper to initialize shared data.
2329  *  - Invoke rte_pktmbuf_attach_extbuf to attach external buffer to the mbuf.
2330  *  - Clone another mbuf and attach the same external buffer to it.
2331  *  - Invoke rte_pktmbuf_detach_extbuf to detach the external buffer from mbuf.
2332  */
2333 static int
2334 test_pktmbuf_ext_shinfo_init_helper(struct rte_mempool *pktmbuf_pool)
2335 {
2336 	struct rte_mbuf *m = NULL;
2337 	struct rte_mbuf *clone = NULL;
2338 	struct rte_mbuf_ext_shared_info *ret_shinfo = NULL;
2339 	rte_iova_t buf_iova;
2340 	void *ext_buf_addr = NULL;
2341 	uint16_t buf_len = EXT_BUF_TEST_DATA_LEN +
2342 				sizeof(struct rte_mbuf_ext_shared_info);
2343 
2344 	/* alloc a mbuf */
2345 	m = rte_pktmbuf_alloc(pktmbuf_pool);
2346 	if (m == NULL)
2347 		GOTO_FAIL("%s: mbuf allocation failed!\n", __func__);
2348 	if (rte_pktmbuf_pkt_len(m) != 0)
2349 		GOTO_FAIL("%s: Bad packet length\n", __func__);
2350 	rte_mbuf_sanity_check(m, 0);
2351 
2352 	ext_buf_addr = rte_malloc("External buffer", buf_len,
2353 			RTE_CACHE_LINE_SIZE);
2354 	if (ext_buf_addr == NULL)
2355 		GOTO_FAIL("%s: External buffer allocation failed\n", __func__);
2356 
2357 	ret_shinfo = rte_pktmbuf_ext_shinfo_init_helper(ext_buf_addr, &buf_len,
2358 		ext_buf_free_callback_fn, ext_buf_addr);
2359 	if (ret_shinfo == NULL)
2360 		GOTO_FAIL("%s: Shared info initialization failed!\n", __func__);
2361 
2362 	if (rte_mbuf_ext_refcnt_read(ret_shinfo) != 1)
2363 		GOTO_FAIL("%s: External refcount is not 1\n", __func__);
2364 
2365 	if (rte_mbuf_refcnt_read(m) != 1)
2366 		GOTO_FAIL("%s: Invalid refcnt in mbuf\n", __func__);
2367 
2368 	buf_iova = rte_mem_virt2iova(ext_buf_addr);
2369 	rte_pktmbuf_attach_extbuf(m, ext_buf_addr, buf_iova, buf_len,
2370 		ret_shinfo);
2371 	if (m->ol_flags != RTE_MBUF_F_EXTERNAL)
2372 		GOTO_FAIL("%s: External buffer is not attached to mbuf\n",
2373 				__func__);
2374 
2375 	/* allocate one more mbuf */
2376 	clone = rte_pktmbuf_clone(m, pktmbuf_pool);
2377 	if (clone == NULL)
2378 		GOTO_FAIL("%s: mbuf clone allocation failed!\n", __func__);
2379 	if (rte_pktmbuf_pkt_len(clone) != 0)
2380 		GOTO_FAIL("%s: Bad packet length\n", __func__);
2381 
2382 	/* attach the same external buffer to the cloned mbuf */
2383 	rte_pktmbuf_attach_extbuf(clone, ext_buf_addr, buf_iova, buf_len,
2384 			ret_shinfo);
2385 	if (clone->ol_flags != RTE_MBUF_F_EXTERNAL)
2386 		GOTO_FAIL("%s: External buffer is not attached to mbuf\n",
2387 				__func__);
2388 
2389 	if (rte_mbuf_ext_refcnt_read(ret_shinfo) != 2)
2390 		GOTO_FAIL("%s: Invalid ext_buf ref_cnt\n", __func__);
2391 
2392 	/* test to manually update ext_buf_ref_cnt from 2 to 3*/
2393 	rte_mbuf_ext_refcnt_update(ret_shinfo, 1);
2394 	if (rte_mbuf_ext_refcnt_read(ret_shinfo) != 3)
2395 		GOTO_FAIL("%s: Update ext_buf ref_cnt failed\n", __func__);
2396 
2397 	/* reset the ext_refcnt before freeing the external buffer */
2398 	rte_mbuf_ext_refcnt_set(ret_shinfo, 2);
2399 	if (rte_mbuf_ext_refcnt_read(ret_shinfo) != 2)
2400 		GOTO_FAIL("%s: set ext_buf ref_cnt failed\n", __func__);
2401 
2402 	/* detach the external buffer from mbufs */
2403 	rte_pktmbuf_detach_extbuf(m);
2404 	/* check if ref cnt is decremented */
2405 	if (rte_mbuf_ext_refcnt_read(ret_shinfo) != 1)
2406 		GOTO_FAIL("%s: Invalid ext_buf ref_cnt\n", __func__);
2407 
2408 	rte_pktmbuf_detach_extbuf(clone);
2409 	if (rte_mbuf_ext_refcnt_read(ret_shinfo) != 0)
2410 		GOTO_FAIL("%s: Invalid ext_buf ref_cnt\n", __func__);
2411 
2412 	rte_pktmbuf_free(m);
2413 	m = NULL;
2414 	rte_pktmbuf_free(clone);
2415 	clone = NULL;
2416 
2417 	return 0;
2418 
2419 fail:
2420 	if (m) {
2421 		rte_pktmbuf_free(m);
2422 		m = NULL;
2423 	}
2424 	if (clone) {
2425 		rte_pktmbuf_free(clone);
2426 		clone = NULL;
2427 	}
2428 	if (ext_buf_addr != NULL) {
2429 		rte_free(ext_buf_addr);
2430 		ext_buf_addr = NULL;
2431 	}
2432 	return -1;
2433 }
2434 
2435 /*
2436  * Test the mbuf pool with pinned external data buffers
2437  *  - Allocate memory zone for external buffer
2438  *  - Create the mbuf pool with pinned external buffer
2439  *  - Check the created pool with relevant mbuf pool unit tests
2440  */
2441 static int
2442 test_pktmbuf_ext_pinned_buffer(struct rte_mempool *std_pool)
2443 {
2444 
2445 	struct rte_pktmbuf_extmem ext_mem;
2446 	struct rte_mempool *pinned_pool = NULL;
2447 	const struct rte_memzone *mz = NULL;
2448 
2449 	printf("Test mbuf pool with external pinned data buffers\n");
2450 
2451 	/* Allocate memzone for the external data buffer */
2452 	mz = rte_memzone_reserve("pinned_pool",
2453 				 NB_MBUF * MBUF_DATA_SIZE,
2454 				 SOCKET_ID_ANY,
2455 				 RTE_MEMZONE_2MB | RTE_MEMZONE_SIZE_HINT_ONLY);
2456 	if (mz == NULL)
2457 		GOTO_FAIL("%s: Memzone allocation failed\n", __func__);
2458 
2459 	/* Create the mbuf pool with pinned external data buffer */
2460 	ext_mem.buf_ptr = mz->addr;
2461 	ext_mem.buf_iova = mz->iova;
2462 	ext_mem.buf_len = mz->len;
2463 	ext_mem.elt_size = MBUF_DATA_SIZE;
2464 
2465 	pinned_pool = rte_pktmbuf_pool_create_extbuf("test_pinned_pool",
2466 				NB_MBUF, MEMPOOL_CACHE_SIZE, 0,
2467 				MBUF_DATA_SIZE,	SOCKET_ID_ANY,
2468 				&ext_mem, 1);
2469 	if (pinned_pool == NULL)
2470 		GOTO_FAIL("%s: Mbuf pool with pinned external"
2471 			  " buffer creation failed\n", __func__);
2472 	/* test multiple mbuf alloc */
2473 	if (test_pktmbuf_pool(pinned_pool) < 0)
2474 		GOTO_FAIL("%s: test_mbuf_pool(pinned) failed\n",
2475 			  __func__);
2476 
2477 	/* do it another time to check that all mbufs were freed */
2478 	if (test_pktmbuf_pool(pinned_pool) < 0)
2479 		GOTO_FAIL("%s: test_mbuf_pool(pinned) failed (2)\n",
2480 			  __func__);
2481 
2482 	/* test that the data pointer on a packet mbuf is set properly */
2483 	if (test_pktmbuf_pool_ptr(pinned_pool) < 0)
2484 		GOTO_FAIL("%s: test_pktmbuf_pool_ptr(pinned) failed\n",
2485 			  __func__);
2486 
2487 	/* test data manipulation in mbuf with non-ascii data */
2488 	if (test_pktmbuf_with_non_ascii_data(pinned_pool) < 0)
2489 		GOTO_FAIL("%s: test_pktmbuf_with_non_ascii_data(pinned)"
2490 			  " failed\n", __func__);
2491 
2492 	/* test free pktmbuf segment one by one */
2493 	if (test_pktmbuf_free_segment(pinned_pool) < 0)
2494 		GOTO_FAIL("%s: test_pktmbuf_free_segment(pinned) failed\n",
2495 			  __func__);
2496 
2497 	if (testclone_testupdate_testdetach(pinned_pool, std_pool) < 0)
2498 		GOTO_FAIL("%s: testclone_and_testupdate(pinned) failed\n",
2499 			  __func__);
2500 
2501 	if (test_pktmbuf_copy(pinned_pool, std_pool) < 0)
2502 		GOTO_FAIL("%s: test_pktmbuf_copy(pinned) failed\n",
2503 			  __func__);
2504 
2505 	if (test_failing_mbuf_sanity_check(pinned_pool) < 0)
2506 		GOTO_FAIL("%s: test_failing_mbuf_sanity_check(pinned)"
2507 			  " failed\n", __func__);
2508 
2509 	if (test_mbuf_linearize_check(pinned_pool) < 0)
2510 		GOTO_FAIL("%s: test_mbuf_linearize_check(pinned) failed\n",
2511 			  __func__);
2512 
2513 	/* test for allocating a bulk of mbufs with various sizes */
2514 	if (test_pktmbuf_alloc_bulk(pinned_pool) < 0)
2515 		GOTO_FAIL("%s: test_rte_pktmbuf_alloc_bulk(pinned) failed\n",
2516 			  __func__);
2517 
2518 	/* test for allocating a bulk of mbufs with various sizes */
2519 	if (test_neg_pktmbuf_alloc_bulk(pinned_pool) < 0)
2520 		GOTO_FAIL("%s: test_neg_rte_pktmbuf_alloc_bulk(pinned)"
2521 			  " failed\n", __func__);
2522 
2523 	/* test to read mbuf packet */
2524 	if (test_pktmbuf_read(pinned_pool) < 0)
2525 		GOTO_FAIL("%s: test_rte_pktmbuf_read(pinned) failed\n",
2526 			  __func__);
2527 
2528 	/* test to read mbuf packet from offset */
2529 	if (test_pktmbuf_read_from_offset(pinned_pool) < 0)
2530 		GOTO_FAIL("%s: test_rte_pktmbuf_read_from_offset(pinned)"
2531 			  " failed\n", __func__);
2532 
2533 	/* test to read data from chain of mbufs with data segments */
2534 	if (test_pktmbuf_read_from_chain(pinned_pool) < 0)
2535 		GOTO_FAIL("%s: test_rte_pktmbuf_read_from_chain(pinned)"
2536 			  " failed\n", __func__);
2537 
2538 	RTE_SET_USED(std_pool);
2539 	rte_mempool_free(pinned_pool);
2540 	rte_memzone_free(mz);
2541 	return 0;
2542 
2543 fail:
2544 	rte_mempool_free(pinned_pool);
2545 	rte_memzone_free(mz);
2546 	return -1;
2547 }
2548 
2549 static int
2550 test_mbuf_dyn(struct rte_mempool *pktmbuf_pool)
2551 {
2552 	const struct rte_mbuf_dynfield dynfield = {
2553 		.name = "test-dynfield",
2554 		.size = sizeof(uint8_t),
2555 		.align = __alignof__(uint8_t),
2556 		.flags = 0,
2557 	};
2558 	const struct rte_mbuf_dynfield dynfield2 = {
2559 		.name = "test-dynfield2",
2560 		.size = sizeof(uint16_t),
2561 		.align = __alignof__(uint16_t),
2562 		.flags = 0,
2563 	};
2564 	const struct rte_mbuf_dynfield dynfield3 = {
2565 		.name = "test-dynfield3",
2566 		.size = sizeof(uint8_t),
2567 		.align = __alignof__(uint8_t),
2568 		.flags = 0,
2569 	};
2570 	const struct rte_mbuf_dynfield dynfield_fail_big = {
2571 		.name = "test-dynfield-fail-big",
2572 		.size = 256,
2573 		.align = 1,
2574 		.flags = 0,
2575 	};
2576 	const struct rte_mbuf_dynfield dynfield_fail_align = {
2577 		.name = "test-dynfield-fail-align",
2578 		.size = 1,
2579 		.align = 3,
2580 		.flags = 0,
2581 	};
2582 	const struct rte_mbuf_dynfield dynfield_fail_flag = {
2583 		.name = "test-dynfield",
2584 		.size = sizeof(uint8_t),
2585 		.align = __alignof__(uint8_t),
2586 		.flags = 1,
2587 	};
2588 	const struct rte_mbuf_dynflag dynflag_fail_flag = {
2589 		.name = "test-dynflag",
2590 		.flags = 1,
2591 	};
2592 	const struct rte_mbuf_dynflag dynflag = {
2593 		.name = "test-dynflag",
2594 		.flags = 0,
2595 	};
2596 	const struct rte_mbuf_dynflag dynflag2 = {
2597 		.name = "test-dynflag2",
2598 		.flags = 0,
2599 	};
2600 	const struct rte_mbuf_dynflag dynflag3 = {
2601 		.name = "test-dynflag3",
2602 		.flags = 0,
2603 	};
2604 	struct rte_mbuf *m = NULL;
2605 	int offset, offset2, offset3;
2606 	int flag, flag2, flag3;
2607 	int ret;
2608 
2609 	printf("Test mbuf dynamic fields and flags\n");
2610 	rte_mbuf_dyn_dump(stdout);
2611 
2612 	offset = rte_mbuf_dynfield_register(&dynfield);
2613 	if (offset == -1)
2614 		GOTO_FAIL("failed to register dynamic field, offset=%d: %s",
2615 			offset, strerror(errno));
2616 
2617 	ret = rte_mbuf_dynfield_register(&dynfield);
2618 	if (ret != offset)
2619 		GOTO_FAIL("failed to lookup dynamic field, ret=%d: %s",
2620 			ret, strerror(errno));
2621 
2622 	offset2 = rte_mbuf_dynfield_register(&dynfield2);
2623 	if (offset2 == -1 || offset2 == offset || (offset2 & 1))
2624 		GOTO_FAIL("failed to register dynamic field 2, offset2=%d: %s",
2625 			offset2, strerror(errno));
2626 
2627 	offset3 = rte_mbuf_dynfield_register_offset(&dynfield3,
2628 				offsetof(struct rte_mbuf, dynfield1[1]));
2629 	if (offset3 != offsetof(struct rte_mbuf, dynfield1[1])) {
2630 		if (rte_errno == EBUSY)
2631 			printf("mbuf test error skipped: dynfield is busy\n");
2632 		else
2633 			GOTO_FAIL("failed to register dynamic field 3, offset="
2634 				"%d: %s", offset3, strerror(errno));
2635 	}
2636 
2637 	printf("dynfield: offset=%d, offset2=%d, offset3=%d\n",
2638 		offset, offset2, offset3);
2639 
2640 	ret = rte_mbuf_dynfield_register(&dynfield_fail_big);
2641 	if (ret != -1)
2642 		GOTO_FAIL("dynamic field creation should fail (too big)");
2643 
2644 	ret = rte_mbuf_dynfield_register(&dynfield_fail_align);
2645 	if (ret != -1)
2646 		GOTO_FAIL("dynamic field creation should fail (bad alignment)");
2647 
2648 	ret = rte_mbuf_dynfield_register_offset(&dynfield_fail_align,
2649 				offsetof(struct rte_mbuf, ol_flags));
2650 	if (ret != -1)
2651 		GOTO_FAIL("dynamic field creation should fail (not avail)");
2652 
2653 	ret = rte_mbuf_dynfield_register(&dynfield_fail_flag);
2654 	if (ret != -1)
2655 		GOTO_FAIL("dynamic field creation should fail (invalid flag)");
2656 
2657 	ret = rte_mbuf_dynflag_register(&dynflag_fail_flag);
2658 	if (ret != -1)
2659 		GOTO_FAIL("dynamic flag creation should fail (invalid flag)");
2660 
2661 	flag = rte_mbuf_dynflag_register(&dynflag);
2662 	if (flag == -1)
2663 		GOTO_FAIL("failed to register dynamic flag, flag=%d: %s",
2664 			flag, strerror(errno));
2665 
2666 	ret = rte_mbuf_dynflag_register(&dynflag);
2667 	if (ret != flag)
2668 		GOTO_FAIL("failed to lookup dynamic flag, ret=%d: %s",
2669 			ret, strerror(errno));
2670 
2671 	flag2 = rte_mbuf_dynflag_register(&dynflag2);
2672 	if (flag2 == -1 || flag2 == flag)
2673 		GOTO_FAIL("failed to register dynamic flag 2, flag2=%d: %s",
2674 			flag2, strerror(errno));
2675 
2676 	flag3 = rte_mbuf_dynflag_register_bitnum(&dynflag3,
2677 						rte_bsf64(RTE_MBUF_F_LAST_FREE));
2678 	if (flag3 != rte_bsf64(RTE_MBUF_F_LAST_FREE))
2679 		GOTO_FAIL("failed to register dynamic flag 3, flag3=%d: %s",
2680 			flag3, strerror(errno));
2681 
2682 	printf("dynflag: flag=%d, flag2=%d, flag3=%d\n", flag, flag2, flag3);
2683 
2684 	/* set, get dynamic field */
2685 	m = rte_pktmbuf_alloc(pktmbuf_pool);
2686 	if (m == NULL)
2687 		GOTO_FAIL("Cannot allocate mbuf");
2688 
2689 	*RTE_MBUF_DYNFIELD(m, offset, uint8_t *) = 1;
2690 	if (*RTE_MBUF_DYNFIELD(m, offset, uint8_t *) != 1)
2691 		GOTO_FAIL("failed to read dynamic field");
2692 	*RTE_MBUF_DYNFIELD(m, offset2, uint16_t *) = 1000;
2693 	if (*RTE_MBUF_DYNFIELD(m, offset2, uint16_t *) != 1000)
2694 		GOTO_FAIL("failed to read dynamic field");
2695 
2696 	/* set a dynamic flag */
2697 	m->ol_flags |= (1ULL << flag);
2698 
2699 	rte_mbuf_dyn_dump(stdout);
2700 	rte_pktmbuf_free(m);
2701 	return 0;
2702 fail:
2703 	rte_pktmbuf_free(m);
2704 	return -1;
2705 }
2706 
2707 /* check that m->nb_segs and m->next are reset on mbuf free */
2708 static int
2709 test_nb_segs_and_next_reset(void)
2710 {
2711 	struct rte_mbuf *m0 = NULL, *m1 = NULL, *m2 = NULL;
2712 	struct rte_mempool *pool = NULL;
2713 
2714 	pool = rte_pktmbuf_pool_create("test_mbuf_reset",
2715 			3, 0, 0, MBUF_DATA_SIZE, SOCKET_ID_ANY);
2716 	if (pool == NULL)
2717 		GOTO_FAIL("Failed to create mbuf pool");
2718 
2719 	/* alloc mbufs */
2720 	m0 = rte_pktmbuf_alloc(pool);
2721 	m1 = rte_pktmbuf_alloc(pool);
2722 	m2 = rte_pktmbuf_alloc(pool);
2723 	if (m0 == NULL || m1 == NULL || m2 == NULL)
2724 		GOTO_FAIL("Failed to allocate mbuf");
2725 
2726 	/* append data in all of them */
2727 	if (rte_pktmbuf_append(m0, 500) == NULL ||
2728 			rte_pktmbuf_append(m1, 500) == NULL ||
2729 			rte_pktmbuf_append(m2, 500) == NULL)
2730 		GOTO_FAIL("Failed to append data in mbuf");
2731 
2732 	/* chain them in one mbuf m0 */
2733 	rte_pktmbuf_chain(m1, m2);
2734 	rte_pktmbuf_chain(m0, m1);
2735 	if (m0->nb_segs != 3 || m0->next != m1 || m1->next != m2 ||
2736 			m2->next != NULL) {
2737 		m1 = m2 = NULL;
2738 		GOTO_FAIL("Failed to chain mbufs");
2739 	}
2740 
2741 	/* split m0 chain in two, between m1 and m2 */
2742 	m0->nb_segs = 2;
2743 	m1->next = NULL;
2744 	m2->nb_segs = 1;
2745 
2746 	/* free the 2 mbuf chains m0 and m2  */
2747 	rte_pktmbuf_free(m0);
2748 	rte_pktmbuf_free(m2);
2749 
2750 	/* realloc the 3 mbufs */
2751 	m0 = rte_mbuf_raw_alloc(pool);
2752 	m1 = rte_mbuf_raw_alloc(pool);
2753 	m2 = rte_mbuf_raw_alloc(pool);
2754 	if (m0 == NULL || m1 == NULL || m2 == NULL)
2755 		GOTO_FAIL("Failed to reallocate mbuf");
2756 
2757 	/* ensure that m->next and m->nb_segs are reset allocated mbufs */
2758 	if (m0->nb_segs != 1 || m0->next != NULL ||
2759 			m1->nb_segs != 1 || m1->next != NULL ||
2760 			m2->nb_segs != 1 || m2->next != NULL)
2761 		GOTO_FAIL("nb_segs or next was not reset properly");
2762 
2763 	return 0;
2764 
2765 fail:
2766 	if (pool != NULL)
2767 		rte_mempool_free(pool);
2768 	return -1;
2769 }
2770 
2771 static int
2772 test_mbuf(void)
2773 {
2774 	int ret = -1;
2775 	struct rte_mempool *pktmbuf_pool = NULL;
2776 	struct rte_mempool *pktmbuf_pool2 = NULL;
2777 
2778 
2779 	RTE_BUILD_BUG_ON(sizeof(struct rte_mbuf) != RTE_CACHE_LINE_MIN_SIZE * 2);
2780 
2781 	/* create pktmbuf pool if it does not exist */
2782 	pktmbuf_pool = rte_pktmbuf_pool_create("test_pktmbuf_pool",
2783 			NB_MBUF, MEMPOOL_CACHE_SIZE, 0, MBUF_DATA_SIZE,
2784 			SOCKET_ID_ANY);
2785 
2786 	if (pktmbuf_pool == NULL) {
2787 		printf("cannot allocate mbuf pool\n");
2788 		goto err;
2789 	}
2790 
2791 	/* test registration of dynamic fields and flags */
2792 	if (test_mbuf_dyn(pktmbuf_pool) < 0) {
2793 		printf("mbuf dynflag test failed\n");
2794 		goto err;
2795 	}
2796 
2797 	/* create a specific pktmbuf pool with a priv_size != 0 and no data
2798 	 * room size */
2799 	pktmbuf_pool2 = rte_pktmbuf_pool_create("test_pktmbuf_pool2",
2800 			NB_MBUF, MEMPOOL_CACHE_SIZE, MBUF2_PRIV_SIZE, 0,
2801 			SOCKET_ID_ANY);
2802 
2803 	if (pktmbuf_pool2 == NULL) {
2804 		printf("cannot allocate mbuf pool\n");
2805 		goto err;
2806 	}
2807 
2808 	/* test multiple mbuf alloc */
2809 	if (test_pktmbuf_pool(pktmbuf_pool) < 0) {
2810 		printf("test_mbuf_pool() failed\n");
2811 		goto err;
2812 	}
2813 
2814 	/* do it another time to check that all mbufs were freed */
2815 	if (test_pktmbuf_pool(pktmbuf_pool) < 0) {
2816 		printf("test_mbuf_pool() failed (2)\n");
2817 		goto err;
2818 	}
2819 
2820 	/* test bulk mbuf alloc and free */
2821 	if (test_pktmbuf_pool_bulk() < 0) {
2822 		printf("test_pktmbuf_pool_bulk() failed\n");
2823 		goto err;
2824 	}
2825 
2826 	/* test that the pointer to the data on a packet mbuf is set properly */
2827 	if (test_pktmbuf_pool_ptr(pktmbuf_pool) < 0) {
2828 		printf("test_pktmbuf_pool_ptr() failed\n");
2829 		goto err;
2830 	}
2831 
2832 	/* test data manipulation in mbuf */
2833 	if (test_one_pktmbuf(pktmbuf_pool) < 0) {
2834 		printf("test_one_mbuf() failed\n");
2835 		goto err;
2836 	}
2837 
2838 
2839 	/*
2840 	 * do it another time, to check that allocation reinitialize
2841 	 * the mbuf correctly
2842 	 */
2843 	if (test_one_pktmbuf(pktmbuf_pool) < 0) {
2844 		printf("test_one_mbuf() failed (2)\n");
2845 		goto err;
2846 	}
2847 
2848 	if (test_pktmbuf_with_non_ascii_data(pktmbuf_pool) < 0) {
2849 		printf("test_pktmbuf_with_non_ascii_data() failed\n");
2850 		goto err;
2851 	}
2852 
2853 	/* test free pktmbuf segment one by one */
2854 	if (test_pktmbuf_free_segment(pktmbuf_pool) < 0) {
2855 		printf("test_pktmbuf_free_segment() failed.\n");
2856 		goto err;
2857 	}
2858 
2859 	if (testclone_testupdate_testdetach(pktmbuf_pool, pktmbuf_pool) < 0) {
2860 		printf("testclone_and_testupdate() failed \n");
2861 		goto err;
2862 	}
2863 
2864 	if (test_pktmbuf_copy(pktmbuf_pool, pktmbuf_pool) < 0) {
2865 		printf("test_pktmbuf_copy() failed\n");
2866 		goto err;
2867 	}
2868 
2869 	if (test_attach_from_different_pool(pktmbuf_pool, pktmbuf_pool2) < 0) {
2870 		printf("test_attach_from_different_pool() failed\n");
2871 		goto err;
2872 	}
2873 
2874 	if (test_refcnt_mbuf() < 0) {
2875 		printf("test_refcnt_mbuf() failed \n");
2876 		goto err;
2877 	}
2878 
2879 	if (test_failing_mbuf_sanity_check(pktmbuf_pool) < 0) {
2880 		printf("test_failing_mbuf_sanity_check() failed\n");
2881 		goto err;
2882 	}
2883 
2884 	if (test_mbuf_linearize_check(pktmbuf_pool) < 0) {
2885 		printf("test_mbuf_linearize_check() failed\n");
2886 		goto err;
2887 	}
2888 
2889 	if (test_tx_offload() < 0) {
2890 		printf("test_tx_offload() failed\n");
2891 		goto err;
2892 	}
2893 
2894 	if (test_get_rx_ol_flag_list() < 0) {
2895 		printf("test_rte_get_rx_ol_flag_list() failed\n");
2896 		goto err;
2897 	}
2898 
2899 	if (test_get_tx_ol_flag_list() < 0) {
2900 		printf("test_rte_get_tx_ol_flag_list() failed\n");
2901 		goto err;
2902 	}
2903 
2904 	if (test_get_rx_ol_flag_name() < 0) {
2905 		printf("test_rte_get_rx_ol_flag_name() failed\n");
2906 		goto err;
2907 	}
2908 
2909 	if (test_get_tx_ol_flag_name() < 0) {
2910 		printf("test_rte_get_tx_ol_flag_name() failed\n");
2911 		goto err;
2912 	}
2913 
2914 	if (test_mbuf_validate_tx_offload_one(pktmbuf_pool) < 0) {
2915 		printf("test_mbuf_validate_tx_offload_one() failed\n");
2916 		goto err;
2917 	}
2918 
2919 	/* test for allocating a bulk of mbufs with various sizes */
2920 	if (test_pktmbuf_alloc_bulk(pktmbuf_pool) < 0) {
2921 		printf("test_rte_pktmbuf_alloc_bulk() failed\n");
2922 		goto err;
2923 	}
2924 
2925 	/* test for allocating a bulk of mbufs with various sizes */
2926 	if (test_neg_pktmbuf_alloc_bulk(pktmbuf_pool) < 0) {
2927 		printf("test_neg_rte_pktmbuf_alloc_bulk() failed\n");
2928 		goto err;
2929 	}
2930 
2931 	/* test to read mbuf packet */
2932 	if (test_pktmbuf_read(pktmbuf_pool) < 0) {
2933 		printf("test_rte_pktmbuf_read() failed\n");
2934 		goto err;
2935 	}
2936 
2937 	/* test to read mbuf packet from offset */
2938 	if (test_pktmbuf_read_from_offset(pktmbuf_pool) < 0) {
2939 		printf("test_rte_pktmbuf_read_from_offset() failed\n");
2940 		goto err;
2941 	}
2942 
2943 	/* test to read data from chain of mbufs with data segments */
2944 	if (test_pktmbuf_read_from_chain(pktmbuf_pool) < 0) {
2945 		printf("test_rte_pktmbuf_read_from_chain() failed\n");
2946 		goto err;
2947 	}
2948 
2949 	/* test to initialize shared info. at the end of external buffer */
2950 	if (test_pktmbuf_ext_shinfo_init_helper(pktmbuf_pool) < 0) {
2951 		printf("test_pktmbuf_ext_shinfo_init_helper() failed\n");
2952 		goto err;
2953 	}
2954 
2955 	/* test the mbuf pool with pinned external data buffers */
2956 	if (test_pktmbuf_ext_pinned_buffer(pktmbuf_pool) < 0) {
2957 		printf("test_pktmbuf_ext_pinned_buffer() failed\n");
2958 		goto err;
2959 	}
2960 
2961 	/* test reset of m->nb_segs and m->next on mbuf free */
2962 	if (test_nb_segs_and_next_reset() < 0) {
2963 		printf("test_nb_segs_and_next_reset() failed\n");
2964 		goto err;
2965 	}
2966 
2967 	ret = 0;
2968 err:
2969 	rte_mempool_free(pktmbuf_pool);
2970 	rte_mempool_free(pktmbuf_pool2);
2971 	return ret;
2972 }
2973 #undef GOTO_FAIL
2974 
2975 REGISTER_TEST_COMMAND(mbuf_autotest, test_mbuf);
2976