1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
12 #include <sys/queue.h>
14 #include <rte_common.h>
15 #include <rte_errno.h>
16 #include <rte_debug.h>
18 #include <rte_memory.h>
19 #include <rte_memcpy.h>
20 #include <rte_launch.h>
22 #include <rte_per_lcore.h>
23 #include <rte_lcore.h>
24 #include <rte_atomic.h>
25 #include <rte_branch_prediction.h>
27 #include <rte_mempool.h>
29 #include <rte_random.h>
30 #include <rte_cycles.h>
31 #include <rte_malloc.h>
35 #define MBUF_DATA_SIZE 2048
37 #define MBUF_TEST_DATA_LEN 1464
38 #define MBUF_TEST_DATA_LEN2 50
39 #define MBUF_TEST_HDR1_LEN 20
40 #define MBUF_TEST_HDR2_LEN 30
41 #define MBUF_TEST_ALL_HDRS_LEN (MBUF_TEST_HDR1_LEN+MBUF_TEST_HDR2_LEN)
43 /* chain length in bulk test */
46 /* size of private data for mbuf in pktmbuf_pool2 */
47 #define MBUF2_PRIV_SIZE 128
49 #define REFCNT_MAX_ITER 64
50 #define REFCNT_MAX_TIMEOUT 10
51 #define REFCNT_MAX_REF (RTE_MAX_LCORE)
52 #define REFCNT_MBUF_NUM 64
53 #define REFCNT_RING_SIZE (REFCNT_MBUF_NUM * REFCNT_MAX_REF)
55 #define MAGIC_DATA 0x42424242
57 #define MAKE_STRING(x) # x
59 #ifdef RTE_MBUF_REFCNT_ATOMIC
61 static volatile uint32_t refcnt_stop_slaves;
62 static unsigned refcnt_lcore[RTE_MAX_LCORE];
70 * #. Allocate a mbuf pool.
72 * - The pool contains NB_MBUF elements, where each mbuf is MBUF_SIZE
75 * #. Test multiple allocations of mbufs from this pool.
77 * - Allocate NB_MBUF and store pointers in a table.
78 * - If an allocation fails, return an error.
79 * - Free all these mbufs.
80 * - Repeat the same test to check that mbufs were freed correctly.
82 * #. Test data manipulation in pktmbuf.
85 * - Append data using rte_pktmbuf_append().
86 * - Test for error in rte_pktmbuf_append() when len is too large.
87 * - Trim data at the end of mbuf using rte_pktmbuf_trim().
88 * - Test for error in rte_pktmbuf_trim() when len is too large.
89 * - Prepend a header using rte_pktmbuf_prepend().
90 * - Test for error in rte_pktmbuf_prepend() when len is too large.
91 * - Remove data at the beginning of mbuf using rte_pktmbuf_adj().
92 * - Test for error in rte_pktmbuf_adj() when len is too large.
93 * - Check that appended data is not corrupt.
95 * - Between all these tests, check data_len and pkt_len, and
96 * that the mbuf is contiguous.
97 * - Repeat the test to check that allocation operations
98 * reinitialize the mbuf correctly.
100 * #. Test packet cloning
101 * - Clone a mbuf and verify the data
102 * - Clone the cloned mbuf and verify the data
103 * - Attach a mbuf to another that does not have the same priv_size.
106 #define GOTO_FAIL(str, ...) do { \
107 printf("mbuf test FAILED (l.%d): <" str ">\n", \
108 __LINE__, ##__VA_ARGS__); \
113 * test data manipulation in mbuf with non-ascii data
116 test_pktmbuf_with_non_ascii_data(struct rte_mempool *pktmbuf_pool)
118 struct rte_mbuf *m = NULL;
121 m = rte_pktmbuf_alloc(pktmbuf_pool);
123 GOTO_FAIL("Cannot allocate mbuf");
124 if (rte_pktmbuf_pkt_len(m) != 0)
125 GOTO_FAIL("Bad length");
127 data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN);
129 GOTO_FAIL("Cannot append data");
130 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
131 GOTO_FAIL("Bad pkt length");
132 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
133 GOTO_FAIL("Bad data length");
134 memset(data, 0xff, rte_pktmbuf_pkt_len(m));
135 if (!rte_pktmbuf_is_contiguous(m))
136 GOTO_FAIL("Buffer should be continuous");
137 rte_pktmbuf_dump(stdout, m, MBUF_TEST_DATA_LEN);
151 * test data manipulation in mbuf
154 test_one_pktmbuf(struct rte_mempool *pktmbuf_pool)
156 struct rte_mbuf *m = NULL;
157 char *data, *data2, *hdr;
160 printf("Test pktmbuf API\n");
164 m = rte_pktmbuf_alloc(pktmbuf_pool);
166 GOTO_FAIL("Cannot allocate mbuf");
167 if (rte_pktmbuf_pkt_len(m) != 0)
168 GOTO_FAIL("Bad length");
170 rte_pktmbuf_dump(stdout, m, 0);
174 data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN);
176 GOTO_FAIL("Cannot append data");
177 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
178 GOTO_FAIL("Bad pkt length");
179 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
180 GOTO_FAIL("Bad data length");
181 memset(data, 0x66, rte_pktmbuf_pkt_len(m));
182 if (!rte_pktmbuf_is_contiguous(m))
183 GOTO_FAIL("Buffer should be continuous");
184 rte_pktmbuf_dump(stdout, m, MBUF_TEST_DATA_LEN);
185 rte_pktmbuf_dump(stdout, m, 2*MBUF_TEST_DATA_LEN);
187 /* this append should fail */
189 data2 = rte_pktmbuf_append(m, (uint16_t)(rte_pktmbuf_tailroom(m) + 1));
191 GOTO_FAIL("Append should not succeed");
193 /* append some more data */
195 data2 = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN2);
197 GOTO_FAIL("Cannot append data");
198 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_DATA_LEN2)
199 GOTO_FAIL("Bad pkt length");
200 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_DATA_LEN2)
201 GOTO_FAIL("Bad data length");
202 if (!rte_pktmbuf_is_contiguous(m))
203 GOTO_FAIL("Buffer should be continuous");
205 /* trim data at the end of mbuf */
207 if (rte_pktmbuf_trim(m, MBUF_TEST_DATA_LEN2) < 0)
208 GOTO_FAIL("Cannot trim data");
209 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
210 GOTO_FAIL("Bad pkt length");
211 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
212 GOTO_FAIL("Bad data length");
213 if (!rte_pktmbuf_is_contiguous(m))
214 GOTO_FAIL("Buffer should be continuous");
216 /* this trim should fail */
218 if (rte_pktmbuf_trim(m, (uint16_t)(rte_pktmbuf_data_len(m) + 1)) == 0)
219 GOTO_FAIL("trim should not succeed");
221 /* prepend one header */
223 hdr = rte_pktmbuf_prepend(m, MBUF_TEST_HDR1_LEN);
225 GOTO_FAIL("Cannot prepend");
226 if (data - hdr != MBUF_TEST_HDR1_LEN)
227 GOTO_FAIL("Prepend failed");
228 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_HDR1_LEN)
229 GOTO_FAIL("Bad pkt length");
230 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_HDR1_LEN)
231 GOTO_FAIL("Bad data length");
232 if (!rte_pktmbuf_is_contiguous(m))
233 GOTO_FAIL("Buffer should be continuous");
234 memset(hdr, 0x55, MBUF_TEST_HDR1_LEN);
236 /* prepend another header */
238 hdr = rte_pktmbuf_prepend(m, MBUF_TEST_HDR2_LEN);
240 GOTO_FAIL("Cannot prepend");
241 if (data - hdr != MBUF_TEST_ALL_HDRS_LEN)
242 GOTO_FAIL("Prepend failed");
243 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_ALL_HDRS_LEN)
244 GOTO_FAIL("Bad pkt length");
245 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_ALL_HDRS_LEN)
246 GOTO_FAIL("Bad data length");
247 if (!rte_pktmbuf_is_contiguous(m))
248 GOTO_FAIL("Buffer should be continuous");
249 memset(hdr, 0x55, MBUF_TEST_HDR2_LEN);
251 rte_mbuf_sanity_check(m, 1);
252 rte_mbuf_sanity_check(m, 0);
253 rte_pktmbuf_dump(stdout, m, 0);
255 /* this prepend should fail */
257 hdr = rte_pktmbuf_prepend(m, (uint16_t)(rte_pktmbuf_headroom(m) + 1));
259 GOTO_FAIL("prepend should not succeed");
261 /* remove data at beginning of mbuf (adj) */
263 if (data != rte_pktmbuf_adj(m, MBUF_TEST_ALL_HDRS_LEN))
264 GOTO_FAIL("rte_pktmbuf_adj failed");
265 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
266 GOTO_FAIL("Bad pkt length");
267 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
268 GOTO_FAIL("Bad data length");
269 if (!rte_pktmbuf_is_contiguous(m))
270 GOTO_FAIL("Buffer should be continuous");
272 /* this adj should fail */
274 if (rte_pktmbuf_adj(m, (uint16_t)(rte_pktmbuf_data_len(m) + 1)) != NULL)
275 GOTO_FAIL("rte_pktmbuf_adj should not succeed");
279 if (!rte_pktmbuf_is_contiguous(m))
280 GOTO_FAIL("Buffer should be continuous");
282 for (i=0; i<MBUF_TEST_DATA_LEN; i++) {
284 GOTO_FAIL("Data corrupted at offset %u", i);
300 testclone_testupdate_testdetach(struct rte_mempool *pktmbuf_pool)
302 struct rte_mbuf *m = NULL;
303 struct rte_mbuf *clone = NULL;
304 struct rte_mbuf *clone2 = NULL;
305 unaligned_uint32_t *data;
308 m = rte_pktmbuf_alloc(pktmbuf_pool);
310 GOTO_FAIL("ooops not allocating mbuf");
312 if (rte_pktmbuf_pkt_len(m) != 0)
313 GOTO_FAIL("Bad length");
315 rte_pktmbuf_append(m, sizeof(uint32_t));
316 data = rte_pktmbuf_mtod(m, unaligned_uint32_t *);
319 /* clone the allocated mbuf */
320 clone = rte_pktmbuf_clone(m, pktmbuf_pool);
322 GOTO_FAIL("cannot clone data\n");
324 data = rte_pktmbuf_mtod(clone, unaligned_uint32_t *);
325 if (*data != MAGIC_DATA)
326 GOTO_FAIL("invalid data in clone\n");
328 if (rte_mbuf_refcnt_read(m) != 2)
329 GOTO_FAIL("invalid refcnt in m\n");
332 rte_pktmbuf_free(clone);
335 /* same test with a chained mbuf */
336 m->next = rte_pktmbuf_alloc(pktmbuf_pool);
338 GOTO_FAIL("Next Pkt Null\n");
341 rte_pktmbuf_append(m->next, sizeof(uint32_t));
342 m->pkt_len = 2 * sizeof(uint32_t);
344 data = rte_pktmbuf_mtod(m->next, unaligned_uint32_t *);
347 clone = rte_pktmbuf_clone(m, pktmbuf_pool);
349 GOTO_FAIL("cannot clone data\n");
351 data = rte_pktmbuf_mtod(clone, unaligned_uint32_t *);
352 if (*data != MAGIC_DATA)
353 GOTO_FAIL("invalid data in clone\n");
355 data = rte_pktmbuf_mtod(clone->next, unaligned_uint32_t *);
356 if (*data != MAGIC_DATA)
357 GOTO_FAIL("invalid data in clone->next\n");
359 if (rte_mbuf_refcnt_read(m) != 2)
360 GOTO_FAIL("invalid refcnt in m\n");
362 if (rte_mbuf_refcnt_read(m->next) != 2)
363 GOTO_FAIL("invalid refcnt in m->next\n");
365 /* try to clone the clone */
367 clone2 = rte_pktmbuf_clone(clone, pktmbuf_pool);
369 GOTO_FAIL("cannot clone the clone\n");
371 data = rte_pktmbuf_mtod(clone2, unaligned_uint32_t *);
372 if (*data != MAGIC_DATA)
373 GOTO_FAIL("invalid data in clone2\n");
375 data = rte_pktmbuf_mtod(clone2->next, unaligned_uint32_t *);
376 if (*data != MAGIC_DATA)
377 GOTO_FAIL("invalid data in clone2->next\n");
379 if (rte_mbuf_refcnt_read(m) != 3)
380 GOTO_FAIL("invalid refcnt in m\n");
382 if (rte_mbuf_refcnt_read(m->next) != 3)
383 GOTO_FAIL("invalid refcnt in m->next\n");
387 rte_pktmbuf_free(clone);
388 rte_pktmbuf_free(clone2);
393 printf("%s ok\n", __func__);
400 rte_pktmbuf_free(clone);
402 rte_pktmbuf_free(clone2);
407 test_pktmbuf_copy(struct rte_mempool *pktmbuf_pool)
409 struct rte_mbuf *m = NULL;
410 struct rte_mbuf *copy = NULL;
411 struct rte_mbuf *copy2 = NULL;
412 struct rte_mbuf *clone = NULL;
413 unaligned_uint32_t *data;
416 m = rte_pktmbuf_alloc(pktmbuf_pool);
418 GOTO_FAIL("ooops not allocating mbuf");
420 if (rte_pktmbuf_pkt_len(m) != 0)
421 GOTO_FAIL("Bad length");
423 rte_pktmbuf_append(m, sizeof(uint32_t));
424 data = rte_pktmbuf_mtod(m, unaligned_uint32_t *);
427 /* copy the allocated mbuf */
428 copy = rte_pktmbuf_copy(m, pktmbuf_pool, 0, UINT32_MAX);
430 GOTO_FAIL("cannot copy data\n");
432 if (rte_pktmbuf_pkt_len(copy) != sizeof(uint32_t))
433 GOTO_FAIL("copy length incorrect\n");
435 if (rte_pktmbuf_data_len(copy) != sizeof(uint32_t))
436 GOTO_FAIL("copy data length incorrect\n");
438 data = rte_pktmbuf_mtod(copy, unaligned_uint32_t *);
439 if (*data != MAGIC_DATA)
440 GOTO_FAIL("invalid data in copy\n");
443 rte_pktmbuf_free(copy);
446 /* same test with a cloned mbuf */
447 clone = rte_pktmbuf_clone(m, pktmbuf_pool);
449 GOTO_FAIL("cannot clone data\n");
451 if (!RTE_MBUF_CLONED(clone))
452 GOTO_FAIL("clone did not give a cloned mbuf\n");
454 copy = rte_pktmbuf_copy(clone, pktmbuf_pool, 0, UINT32_MAX);
456 GOTO_FAIL("cannot copy cloned mbuf\n");
458 if (RTE_MBUF_CLONED(copy))
459 GOTO_FAIL("copy of clone is cloned?\n");
461 if (rte_pktmbuf_pkt_len(copy) != sizeof(uint32_t))
462 GOTO_FAIL("copy clone length incorrect\n");
464 if (rte_pktmbuf_data_len(copy) != sizeof(uint32_t))
465 GOTO_FAIL("copy clone data length incorrect\n");
467 data = rte_pktmbuf_mtod(copy, unaligned_uint32_t *);
468 if (*data != MAGIC_DATA)
469 GOTO_FAIL("invalid data in clone copy\n");
470 rte_pktmbuf_free(clone);
471 rte_pktmbuf_free(copy);
476 /* same test with a chained mbuf */
477 m->next = rte_pktmbuf_alloc(pktmbuf_pool);
479 GOTO_FAIL("Next Pkt Null\n");
482 rte_pktmbuf_append(m->next, sizeof(uint32_t));
483 m->pkt_len = 2 * sizeof(uint32_t);
484 data = rte_pktmbuf_mtod(m->next, unaligned_uint32_t *);
485 *data = MAGIC_DATA + 1;
487 copy = rte_pktmbuf_copy(m, pktmbuf_pool, 0, UINT32_MAX);
489 GOTO_FAIL("cannot copy data\n");
491 if (rte_pktmbuf_pkt_len(copy) != 2 * sizeof(uint32_t))
492 GOTO_FAIL("chain copy length incorrect\n");
494 if (rte_pktmbuf_data_len(copy) != 2 * sizeof(uint32_t))
495 GOTO_FAIL("chain copy data length incorrect\n");
497 data = rte_pktmbuf_mtod(copy, unaligned_uint32_t *);
498 if (data[0] != MAGIC_DATA || data[1] != MAGIC_DATA + 1)
499 GOTO_FAIL("invalid data in copy\n");
501 rte_pktmbuf_free(copy2);
503 /* test offset copy */
504 copy2 = rte_pktmbuf_copy(copy, pktmbuf_pool,
505 sizeof(uint32_t), UINT32_MAX);
507 GOTO_FAIL("cannot copy the copy\n");
509 if (rte_pktmbuf_pkt_len(copy2) != sizeof(uint32_t))
510 GOTO_FAIL("copy with offset, length incorrect\n");
512 if (rte_pktmbuf_data_len(copy2) != sizeof(uint32_t))
513 GOTO_FAIL("copy with offset, data length incorrect\n");
515 data = rte_pktmbuf_mtod(copy2, unaligned_uint32_t *);
516 if (data[0] != MAGIC_DATA + 1)
517 GOTO_FAIL("copy with offset, invalid data\n");
519 rte_pktmbuf_free(copy2);
521 /* test truncation copy */
522 copy2 = rte_pktmbuf_copy(copy, pktmbuf_pool,
523 0, sizeof(uint32_t));
525 GOTO_FAIL("cannot copy the copy\n");
527 if (rte_pktmbuf_pkt_len(copy2) != sizeof(uint32_t))
528 GOTO_FAIL("copy with truncate, length incorrect\n");
530 if (rte_pktmbuf_data_len(copy2) != sizeof(uint32_t))
531 GOTO_FAIL("copy with truncate, data length incorrect\n");
533 data = rte_pktmbuf_mtod(copy2, unaligned_uint32_t *);
534 if (data[0] != MAGIC_DATA)
535 GOTO_FAIL("copy with truncate, invalid data\n");
539 rte_pktmbuf_free(copy);
540 rte_pktmbuf_free(copy2);
545 printf("%s ok\n", __func__);
552 rte_pktmbuf_free(copy);
554 rte_pktmbuf_free(copy2);
559 test_attach_from_different_pool(struct rte_mempool *pktmbuf_pool,
560 struct rte_mempool *pktmbuf_pool2)
562 struct rte_mbuf *m = NULL;
563 struct rte_mbuf *clone = NULL;
564 struct rte_mbuf *clone2 = NULL;
565 char *data, *c_data, *c_data2;
568 m = rte_pktmbuf_alloc(pktmbuf_pool);
570 GOTO_FAIL("cannot allocate mbuf");
572 if (rte_pktmbuf_pkt_len(m) != 0)
573 GOTO_FAIL("Bad length");
575 data = rte_pktmbuf_mtod(m, char *);
577 /* allocate a new mbuf from the second pool, and attach it to the first
579 clone = rte_pktmbuf_alloc(pktmbuf_pool2);
581 GOTO_FAIL("cannot allocate mbuf from second pool\n");
583 /* check data room size and priv size, and erase priv */
584 if (rte_pktmbuf_data_room_size(clone->pool) != 0)
585 GOTO_FAIL("data room size should be 0\n");
586 if (rte_pktmbuf_priv_size(clone->pool) != MBUF2_PRIV_SIZE)
587 GOTO_FAIL("data room size should be %d\n", MBUF2_PRIV_SIZE);
588 memset(clone + 1, 0, MBUF2_PRIV_SIZE);
590 /* save data pointer to compare it after detach() */
591 c_data = rte_pktmbuf_mtod(clone, char *);
592 if (c_data != (char *)clone + sizeof(*clone) + MBUF2_PRIV_SIZE)
593 GOTO_FAIL("bad data pointer in clone");
594 if (rte_pktmbuf_headroom(clone) != 0)
595 GOTO_FAIL("bad headroom in clone");
597 rte_pktmbuf_attach(clone, m);
599 if (rte_pktmbuf_mtod(clone, char *) != data)
600 GOTO_FAIL("clone was not attached properly\n");
601 if (rte_pktmbuf_headroom(clone) != RTE_PKTMBUF_HEADROOM)
602 GOTO_FAIL("bad headroom in clone after attach");
603 if (rte_mbuf_refcnt_read(m) != 2)
604 GOTO_FAIL("invalid refcnt in m\n");
606 /* allocate a new mbuf from the second pool, and attach it to the first
608 clone2 = rte_pktmbuf_alloc(pktmbuf_pool2);
610 GOTO_FAIL("cannot allocate clone2 from second pool\n");
612 /* check data room size and priv size, and erase priv */
613 if (rte_pktmbuf_data_room_size(clone2->pool) != 0)
614 GOTO_FAIL("data room size should be 0\n");
615 if (rte_pktmbuf_priv_size(clone2->pool) != MBUF2_PRIV_SIZE)
616 GOTO_FAIL("data room size should be %d\n", MBUF2_PRIV_SIZE);
617 memset(clone2 + 1, 0, MBUF2_PRIV_SIZE);
619 /* save data pointer to compare it after detach() */
620 c_data2 = rte_pktmbuf_mtod(clone2, char *);
621 if (c_data2 != (char *)clone2 + sizeof(*clone2) + MBUF2_PRIV_SIZE)
622 GOTO_FAIL("bad data pointer in clone2");
623 if (rte_pktmbuf_headroom(clone2) != 0)
624 GOTO_FAIL("bad headroom in clone2");
626 rte_pktmbuf_attach(clone2, clone);
628 if (rte_pktmbuf_mtod(clone2, char *) != data)
629 GOTO_FAIL("clone2 was not attached properly\n");
630 if (rte_pktmbuf_headroom(clone2) != RTE_PKTMBUF_HEADROOM)
631 GOTO_FAIL("bad headroom in clone2 after attach");
632 if (rte_mbuf_refcnt_read(m) != 3)
633 GOTO_FAIL("invalid refcnt in m\n");
635 /* detach the clones */
636 rte_pktmbuf_detach(clone);
637 if (c_data != rte_pktmbuf_mtod(clone, char *))
638 GOTO_FAIL("clone was not detached properly\n");
639 if (rte_mbuf_refcnt_read(m) != 2)
640 GOTO_FAIL("invalid refcnt in m\n");
642 rte_pktmbuf_detach(clone2);
643 if (c_data2 != rte_pktmbuf_mtod(clone2, char *))
644 GOTO_FAIL("clone2 was not detached properly\n");
645 if (rte_mbuf_refcnt_read(m) != 1)
646 GOTO_FAIL("invalid refcnt in m\n");
648 /* free the clones and the initial mbuf */
649 rte_pktmbuf_free(clone2);
650 rte_pktmbuf_free(clone);
652 printf("%s ok\n", __func__);
659 rte_pktmbuf_free(clone);
661 rte_pktmbuf_free(clone2);
667 * test allocation and free of mbufs
670 test_pktmbuf_pool(struct rte_mempool *pktmbuf_pool)
673 struct rte_mbuf *m[NB_MBUF];
676 for (i=0; i<NB_MBUF; i++)
679 /* alloc NB_MBUF mbufs */
680 for (i=0; i<NB_MBUF; i++) {
681 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
683 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
687 struct rte_mbuf *extra = NULL;
688 extra = rte_pktmbuf_alloc(pktmbuf_pool);
690 printf("Error pool not empty");
693 extra = rte_pktmbuf_clone(m[0], pktmbuf_pool);
695 printf("Error pool not empty");
699 for (i=0; i<NB_MBUF; i++) {
701 rte_pktmbuf_free(m[i]);
708 * test bulk allocation and bulk free of mbufs
711 test_pktmbuf_pool_bulk(void)
713 struct rte_mempool *pool = NULL;
714 struct rte_mempool *pool2 = NULL;
717 struct rte_mbuf *mbufs[NB_MBUF];
720 /* We cannot use the preallocated mbuf pools because their caches
721 * prevent us from bulk allocating all objects in them.
722 * So we create our own mbuf pools without caches.
724 printf("Create mbuf pools for bulk allocation.\n");
725 pool = rte_pktmbuf_pool_create("test_pktmbuf_bulk",
726 NB_MBUF, 0, 0, MBUF_DATA_SIZE, SOCKET_ID_ANY);
728 printf("rte_pktmbuf_pool_create() failed. rte_errno %d\n",
732 pool2 = rte_pktmbuf_pool_create("test_pktmbuf_bulk2",
733 NB_MBUF, 0, 0, MBUF_DATA_SIZE, SOCKET_ID_ANY);
735 printf("rte_pktmbuf_pool_create() failed. rte_errno %d\n",
740 /* Preconditions: Mempools must be full. */
741 if (!(rte_mempool_full(pool) && rte_mempool_full(pool2))) {
742 printf("Test precondition failed: mempools not full\n");
745 if (!(rte_mempool_avail_count(pool) == NB_MBUF &&
746 rte_mempool_avail_count(pool2) == NB_MBUF)) {
747 printf("Test precondition failed: mempools: %u+%u != %u+%u",
748 rte_mempool_avail_count(pool),
749 rte_mempool_avail_count(pool2),
754 printf("Test single bulk alloc, followed by multiple bulk free.\n");
756 /* Bulk allocate all mbufs in the pool, in one go. */
757 ret = rte_pktmbuf_alloc_bulk(pool, mbufs, NB_MBUF);
759 printf("rte_pktmbuf_alloc_bulk() failed: %d\n", ret);
762 /* Test that they have been removed from the pool. */
763 if (!rte_mempool_empty(pool)) {
764 printf("mempool not empty\n");
767 /* Bulk free all mbufs, in four steps. */
768 RTE_BUILD_BUG_ON(NB_MBUF % 4 != 0);
769 for (i = 0; i < NB_MBUF; i += NB_MBUF / 4) {
770 rte_pktmbuf_free_bulk(&mbufs[i], NB_MBUF / 4);
771 /* Test that they have been returned to the pool. */
772 if (rte_mempool_avail_count(pool) != i + NB_MBUF / 4) {
773 printf("mempool avail count incorrect\n");
778 printf("Test multiple bulk alloc, followed by single bulk free.\n");
780 /* Bulk allocate all mbufs in the pool, in four steps. */
781 for (i = 0; i < NB_MBUF; i += NB_MBUF / 4) {
782 ret = rte_pktmbuf_alloc_bulk(pool, &mbufs[i], NB_MBUF / 4);
784 printf("rte_pktmbuf_alloc_bulk() failed: %d\n", ret);
788 /* Test that they have been removed from the pool. */
789 if (!rte_mempool_empty(pool)) {
790 printf("mempool not empty\n");
793 /* Bulk free all mbufs, in one go. */
794 rte_pktmbuf_free_bulk(mbufs, NB_MBUF);
795 /* Test that they have been returned to the pool. */
796 if (!rte_mempool_full(pool)) {
797 printf("mempool not full\n");
801 printf("Test bulk free of single long chain.\n");
803 /* Bulk allocate all mbufs in the pool, in one go. */
804 ret = rte_pktmbuf_alloc_bulk(pool, mbufs, NB_MBUF);
806 printf("rte_pktmbuf_alloc_bulk() failed: %d\n", ret);
809 /* Create a long mbuf chain. */
810 for (i = 1; i < NB_MBUF; i++) {
811 ret = rte_pktmbuf_chain(mbufs[0], mbufs[i]);
813 printf("rte_pktmbuf_chain() failed: %d\n", ret);
818 /* Free the mbuf chain containing all the mbufs. */
819 rte_pktmbuf_free_bulk(mbufs, 1);
820 /* Test that they have been returned to the pool. */
821 if (!rte_mempool_full(pool)) {
822 printf("mempool not full\n");
826 printf("Test bulk free of multiple chains using multiple pools.\n");
828 /* Create mbuf chains containing mbufs from different pools. */
829 RTE_BUILD_BUG_ON(CHAIN_LEN % 2 != 0);
830 RTE_BUILD_BUG_ON(NB_MBUF % (CHAIN_LEN / 2) != 0);
831 for (i = 0; i < NB_MBUF * 2; i++) {
832 m = rte_pktmbuf_alloc((i & 4) ? pool2 : pool);
834 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
837 if ((i % CHAIN_LEN) == 0)
838 mbufs[i / CHAIN_LEN] = m;
840 rte_pktmbuf_chain(mbufs[i / CHAIN_LEN], m);
842 /* Test that both pools have been emptied. */
843 if (!(rte_mempool_empty(pool) && rte_mempool_empty(pool2))) {
844 printf("mempools not empty\n");
847 /* Free one mbuf chain. */
848 rte_pktmbuf_free_bulk(mbufs, 1);
849 /* Test that the segments have been returned to the pools. */
850 if (!(rte_mempool_avail_count(pool) == CHAIN_LEN / 2 &&
851 rte_mempool_avail_count(pool2) == CHAIN_LEN / 2)) {
852 printf("all segments of first mbuf have not been returned\n");
855 /* Free the remaining mbuf chains. */
856 rte_pktmbuf_free_bulk(&mbufs[1], NB_MBUF * 2 / CHAIN_LEN - 1);
857 /* Test that they have been returned to the pools. */
858 if (!(rte_mempool_full(pool) && rte_mempool_full(pool2))) {
859 printf("mempools not full\n");
870 printf("Free mbuf pools for bulk allocation.\n");
871 rte_mempool_free(pool);
872 rte_mempool_free(pool2);
877 * test that the pointer to the data on a packet mbuf is set properly
880 test_pktmbuf_pool_ptr(struct rte_mempool *pktmbuf_pool)
883 struct rte_mbuf *m[NB_MBUF];
886 for (i=0; i<NB_MBUF; i++)
889 /* alloc NB_MBUF mbufs */
890 for (i=0; i<NB_MBUF; i++) {
891 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
893 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
897 m[i]->data_off += 64;
901 for (i=0; i<NB_MBUF; i++) {
903 rte_pktmbuf_free(m[i]);
906 for (i=0; i<NB_MBUF; i++)
909 /* alloc NB_MBUF mbufs */
910 for (i=0; i<NB_MBUF; i++) {
911 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
913 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
917 if (m[i]->data_off != RTE_PKTMBUF_HEADROOM) {
918 printf("invalid data_off\n");
924 for (i=0; i<NB_MBUF; i++) {
926 rte_pktmbuf_free(m[i]);
933 test_pktmbuf_free_segment(struct rte_mempool *pktmbuf_pool)
936 struct rte_mbuf *m[NB_MBUF];
939 for (i=0; i<NB_MBUF; i++)
942 /* alloc NB_MBUF mbufs */
943 for (i=0; i<NB_MBUF; i++) {
944 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
946 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
952 for (i=0; i<NB_MBUF; i++) {
954 struct rte_mbuf *mb, *mt;
960 rte_pktmbuf_free_seg(mt);
969 * Stress test for rte_mbuf atomic refcnt.
970 * Implies that RTE_MBUF_REFCNT_ATOMIC is defined.
971 * For more efficiency, recommended to run with RTE_LIBRTE_MBUF_DEBUG defined.
974 #ifdef RTE_MBUF_REFCNT_ATOMIC
977 test_refcnt_slave(void *arg)
979 unsigned lcore, free;
981 struct rte_ring *refcnt_mbuf_ring = arg;
983 lcore = rte_lcore_id();
984 printf("%s started at lcore %u\n", __func__, lcore);
987 while (refcnt_stop_slaves == 0) {
988 if (rte_ring_dequeue(refcnt_mbuf_ring, &mp) == 0) {
990 rte_pktmbuf_free(mp);
994 refcnt_lcore[lcore] += free;
995 printf("%s finished at lcore %u, "
996 "number of freed mbufs: %u\n",
997 __func__, lcore, free);
1002 test_refcnt_iter(unsigned int lcore, unsigned int iter,
1003 struct rte_mempool *refcnt_pool,
1004 struct rte_ring *refcnt_mbuf_ring)
1007 unsigned i, n, tref, wn;
1012 /* For each mbuf in the pool:
1014 * - increment it's reference up to N+1,
1015 * - enqueue it N times into the ring for slave cores to free.
1017 for (i = 0, n = rte_mempool_avail_count(refcnt_pool);
1018 i != n && (m = rte_pktmbuf_alloc(refcnt_pool)) != NULL;
1020 ref = RTE_MAX(rte_rand() % REFCNT_MAX_REF, 1UL);
1022 if ((ref & 1) != 0) {
1023 rte_pktmbuf_refcnt_update(m, ref);
1025 rte_ring_enqueue(refcnt_mbuf_ring, m);
1027 while (ref-- != 0) {
1028 rte_pktmbuf_refcnt_update(m, 1);
1029 rte_ring_enqueue(refcnt_mbuf_ring, m);
1032 rte_pktmbuf_free(m);
1036 rte_panic("(lcore=%u, iter=%u): was able to allocate only "
1037 "%u from %u mbufs\n", lcore, iter, i, n);
1039 /* wait till slave lcores will consume all mbufs */
1040 while (!rte_ring_empty(refcnt_mbuf_ring))
1043 /* check that all mbufs are back into mempool by now */
1044 for (wn = 0; wn != REFCNT_MAX_TIMEOUT; wn++) {
1045 if ((i = rte_mempool_avail_count(refcnt_pool)) == n) {
1046 refcnt_lcore[lcore] += tref;
1047 printf("%s(lcore=%u, iter=%u) completed, "
1048 "%u references processed\n",
1049 __func__, lcore, iter, tref);
1055 rte_panic("(lcore=%u, iter=%u): after %us only "
1056 "%u of %u mbufs left free\n", lcore, iter, wn, i, n);
1060 test_refcnt_master(struct rte_mempool *refcnt_pool,
1061 struct rte_ring *refcnt_mbuf_ring)
1065 lcore = rte_lcore_id();
1066 printf("%s started at lcore %u\n", __func__, lcore);
1068 for (i = 0; i != REFCNT_MAX_ITER; i++)
1069 test_refcnt_iter(lcore, i, refcnt_pool, refcnt_mbuf_ring);
1071 refcnt_stop_slaves = 1;
1074 printf("%s finished at lcore %u\n", __func__, lcore);
1081 test_refcnt_mbuf(void)
1083 #ifdef RTE_MBUF_REFCNT_ATOMIC
1084 unsigned int master, slave, tref;
1086 struct rte_mempool *refcnt_pool = NULL;
1087 struct rte_ring *refcnt_mbuf_ring = NULL;
1089 if (rte_lcore_count() < 2) {
1090 printf("Not enough cores for test_refcnt_mbuf, expecting at least 2\n");
1091 return TEST_SKIPPED;
1094 printf("starting %s, at %u lcores\n", __func__, rte_lcore_count());
1096 /* create refcnt pool & ring if they don't exist */
1098 refcnt_pool = rte_pktmbuf_pool_create(MAKE_STRING(refcnt_pool),
1099 REFCNT_MBUF_NUM, 0, 0, 0,
1101 if (refcnt_pool == NULL) {
1102 printf("%s: cannot allocate " MAKE_STRING(refcnt_pool) "\n",
1107 refcnt_mbuf_ring = rte_ring_create("refcnt_mbuf_ring",
1108 rte_align32pow2(REFCNT_RING_SIZE), SOCKET_ID_ANY,
1110 if (refcnt_mbuf_ring == NULL) {
1111 printf("%s: cannot allocate " MAKE_STRING(refcnt_mbuf_ring)
1116 refcnt_stop_slaves = 0;
1117 memset(refcnt_lcore, 0, sizeof (refcnt_lcore));
1119 rte_eal_mp_remote_launch(test_refcnt_slave, refcnt_mbuf_ring,
1122 test_refcnt_master(refcnt_pool, refcnt_mbuf_ring);
1124 rte_eal_mp_wait_lcore();
1126 /* check that we porcessed all references */
1128 master = rte_get_master_lcore();
1130 RTE_LCORE_FOREACH_SLAVE(slave)
1131 tref += refcnt_lcore[slave];
1133 if (tref != refcnt_lcore[master])
1134 rte_panic("refernced mbufs: %u, freed mbufs: %u\n",
1135 tref, refcnt_lcore[master]);
1137 rte_mempool_dump(stdout, refcnt_pool);
1138 rte_ring_dump(stdout, refcnt_mbuf_ring);
1143 rte_mempool_free(refcnt_pool);
1144 rte_ring_free(refcnt_mbuf_ring);
1152 #include <sys/wait.h>
1154 /* use fork() to test mbuf errors panic */
1156 verify_mbuf_check_panics(struct rte_mbuf *buf)
1164 rte_mbuf_sanity_check(buf, 1); /* should panic */
1165 exit(0); /* return normally if it doesn't panic */
1166 } else if (pid < 0){
1167 printf("Fork Failed\n");
1178 test_failing_mbuf_sanity_check(struct rte_mempool *pktmbuf_pool)
1180 struct rte_mbuf *buf;
1181 struct rte_mbuf badbuf;
1183 printf("Checking rte_mbuf_sanity_check for failure conditions\n");
1185 /* get a good mbuf to use to make copies */
1186 buf = rte_pktmbuf_alloc(pktmbuf_pool);
1189 printf("Checking good mbuf initially\n");
1190 if (verify_mbuf_check_panics(buf) != -1)
1193 printf("Now checking for error conditions\n");
1195 if (verify_mbuf_check_panics(NULL)) {
1196 printf("Error with NULL mbuf test\n");
1202 if (verify_mbuf_check_panics(&badbuf)) {
1203 printf("Error with bad-pool mbuf test\n");
1208 badbuf.buf_iova = 0;
1209 if (verify_mbuf_check_panics(&badbuf)) {
1210 printf("Error with bad-physaddr mbuf test\n");
1215 badbuf.buf_addr = NULL;
1216 if (verify_mbuf_check_panics(&badbuf)) {
1217 printf("Error with bad-addr mbuf test\n");
1223 if (verify_mbuf_check_panics(&badbuf)) {
1224 printf("Error with bad-refcnt(0) mbuf test\n");
1229 badbuf.refcnt = UINT16_MAX;
1230 if (verify_mbuf_check_panics(&badbuf)) {
1231 printf("Error with bad-refcnt(MAX) mbuf test\n");
1239 test_mbuf_linearize(struct rte_mempool *pktmbuf_pool, int pkt_len,
1243 struct rte_mbuf *m = NULL, *mbuf = NULL;
1251 printf("Packet size must be 1 or more (is %d)\n", pkt_len);
1256 printf("Number of segments must be 1 or more (is %d)\n",
1261 seg_len = pkt_len / nb_segs;
1267 /* Create chained mbuf_src and fill it generated data */
1268 for (seg = 0; remain > 0; seg++) {
1270 m = rte_pktmbuf_alloc(pktmbuf_pool);
1272 printf("Cannot create segment for source mbuf");
1276 /* Make sure if tailroom is zeroed */
1277 memset(rte_pktmbuf_mtod(m, uint8_t *), 0,
1278 rte_pktmbuf_tailroom(m));
1281 if (data_len > seg_len)
1284 data = (uint8_t *)rte_pktmbuf_append(m, data_len);
1286 printf("Cannot append %d bytes to the mbuf\n",
1291 for (i = 0; i < data_len; i++)
1292 data[i] = (seg * seg_len + i) % 0x0ff;
1297 rte_pktmbuf_chain(mbuf, m);
1302 /* Create destination buffer to store coalesced data */
1303 if (rte_pktmbuf_linearize(mbuf)) {
1304 printf("Mbuf linearization failed\n");
1308 if (!rte_pktmbuf_is_contiguous(mbuf)) {
1309 printf("Source buffer should be contiguous after "
1314 data = rte_pktmbuf_mtod(mbuf, uint8_t *);
1316 for (i = 0; i < pkt_len; i++)
1317 if (data[i] != (i % 0x0ff)) {
1318 printf("Incorrect data in linearized mbuf\n");
1322 rte_pktmbuf_free(mbuf);
1327 rte_pktmbuf_free(mbuf);
1332 test_mbuf_linearize_check(struct rte_mempool *pktmbuf_pool)
1334 struct test_mbuf_array {
1346 printf("Test mbuf linearize API\n");
1348 for (i = 0; i < RTE_DIM(mbuf_array); i++)
1349 if (test_mbuf_linearize(pktmbuf_pool, mbuf_array[i].size,
1350 mbuf_array[i].nb_segs)) {
1351 printf("Test failed for %d, %d\n", mbuf_array[i].size,
1352 mbuf_array[i].nb_segs);
1360 * Helper function for test_tx_ofload
1363 set_tx_offload(struct rte_mbuf *mb, uint64_t il2, uint64_t il3, uint64_t il4,
1364 uint64_t tso, uint64_t ol3, uint64_t ol2)
1369 mb->tso_segsz = tso;
1370 mb->outer_l3_len = ol3;
1371 mb->outer_l2_len = ol2;
1375 test_tx_offload(void)
1377 struct rte_mbuf *mb;
1378 uint64_t tm, v1, v2;
1382 static volatile struct {
1389 const uint32_t num = 0x10000;
1391 txof.l2 = rte_rand() % (1 << RTE_MBUF_L2_LEN_BITS);
1392 txof.l3 = rte_rand() % (1 << RTE_MBUF_L3_LEN_BITS);
1393 txof.l4 = rte_rand() % (1 << RTE_MBUF_L4_LEN_BITS);
1394 txof.tso = rte_rand() % (1 << RTE_MBUF_TSO_SEGSZ_BITS);
1396 printf("%s started, tx_offload = {\n"
1400 "\ttso_segsz=%#hx,\n"
1401 "\touter_l3_len=%#x,\n"
1402 "\touter_l2_len=%#x,\n"
1405 txof.l2, txof.l3, txof.l4, txof.tso, txof.l3, txof.l2);
1407 sz = sizeof(*mb) * num;
1408 mb = rte_zmalloc(NULL, sz, RTE_CACHE_LINE_SIZE);
1410 printf("%s failed, out of memory\n", __func__);
1415 tm = rte_rdtsc_precise();
1417 for (i = 0; i != num; i++)
1418 set_tx_offload(mb + i, txof.l2, txof.l3, txof.l4,
1419 txof.tso, txof.l3, txof.l2);
1421 tm = rte_rdtsc_precise() - tm;
1422 printf("%s set tx_offload by bit-fields: %u iterations, %"
1423 PRIu64 " cycles, %#Lf cycles/iter\n",
1424 __func__, num, tm, (long double)tm / num);
1426 v1 = mb[rte_rand() % num].tx_offload;
1429 tm = rte_rdtsc_precise();
1431 for (i = 0; i != num; i++)
1432 mb[i].tx_offload = rte_mbuf_tx_offload(txof.l2, txof.l3,
1433 txof.l4, txof.tso, txof.l3, txof.l2, 0);
1435 tm = rte_rdtsc_precise() - tm;
1436 printf("%s set raw tx_offload: %u iterations, %"
1437 PRIu64 " cycles, %#Lf cycles/iter\n",
1438 __func__, num, tm, (long double)tm / num);
1440 v2 = mb[rte_rand() % num].tx_offload;
1444 printf("%s finished\n"
1445 "expected tx_offload value: 0x%" PRIx64 ";\n"
1446 "rte_mbuf_tx_offload value: 0x%" PRIx64 ";\n",
1449 return (v1 == v2) ? 0 : -EINVAL;
1456 struct rte_mempool *pktmbuf_pool = NULL;
1457 struct rte_mempool *pktmbuf_pool2 = NULL;
1460 RTE_BUILD_BUG_ON(sizeof(struct rte_mbuf) != RTE_CACHE_LINE_MIN_SIZE * 2);
1462 /* create pktmbuf pool if it does not exist */
1463 pktmbuf_pool = rte_pktmbuf_pool_create("test_pktmbuf_pool",
1464 NB_MBUF, 32, 0, MBUF_DATA_SIZE, SOCKET_ID_ANY);
1466 if (pktmbuf_pool == NULL) {
1467 printf("cannot allocate mbuf pool\n");
1471 /* create a specific pktmbuf pool with a priv_size != 0 and no data
1473 pktmbuf_pool2 = rte_pktmbuf_pool_create("test_pktmbuf_pool2",
1474 NB_MBUF, 32, MBUF2_PRIV_SIZE, 0, SOCKET_ID_ANY);
1476 if (pktmbuf_pool2 == NULL) {
1477 printf("cannot allocate mbuf pool\n");
1481 /* test multiple mbuf alloc */
1482 if (test_pktmbuf_pool(pktmbuf_pool) < 0) {
1483 printf("test_mbuf_pool() failed\n");
1487 /* do it another time to check that all mbufs were freed */
1488 if (test_pktmbuf_pool(pktmbuf_pool) < 0) {
1489 printf("test_mbuf_pool() failed (2)\n");
1493 /* test bulk mbuf alloc and free */
1494 if (test_pktmbuf_pool_bulk() < 0) {
1495 printf("test_pktmbuf_pool_bulk() failed\n");
1499 /* test that the pointer to the data on a packet mbuf is set properly */
1500 if (test_pktmbuf_pool_ptr(pktmbuf_pool) < 0) {
1501 printf("test_pktmbuf_pool_ptr() failed\n");
1505 /* test data manipulation in mbuf */
1506 if (test_one_pktmbuf(pktmbuf_pool) < 0) {
1507 printf("test_one_mbuf() failed\n");
1513 * do it another time, to check that allocation reinitialize
1514 * the mbuf correctly
1516 if (test_one_pktmbuf(pktmbuf_pool) < 0) {
1517 printf("test_one_mbuf() failed (2)\n");
1521 if (test_pktmbuf_with_non_ascii_data(pktmbuf_pool) < 0) {
1522 printf("test_pktmbuf_with_non_ascii_data() failed\n");
1526 /* test free pktmbuf segment one by one */
1527 if (test_pktmbuf_free_segment(pktmbuf_pool) < 0) {
1528 printf("test_pktmbuf_free_segment() failed.\n");
1532 if (testclone_testupdate_testdetach(pktmbuf_pool) < 0) {
1533 printf("testclone_and_testupdate() failed \n");
1537 if (test_pktmbuf_copy(pktmbuf_pool) < 0) {
1538 printf("test_pktmbuf_copy() failed\n");
1542 if (test_attach_from_different_pool(pktmbuf_pool, pktmbuf_pool2) < 0) {
1543 printf("test_attach_from_different_pool() failed\n");
1547 if (test_refcnt_mbuf() < 0) {
1548 printf("test_refcnt_mbuf() failed \n");
1552 if (test_failing_mbuf_sanity_check(pktmbuf_pool) < 0) {
1553 printf("test_failing_mbuf_sanity_check() failed\n");
1557 if (test_mbuf_linearize_check(pktmbuf_pool) < 0) {
1558 printf("test_mbuf_linearize_check() failed\n");
1562 if (test_tx_offload() < 0) {
1563 printf("test_tx_offload() failed\n");
1569 rte_mempool_free(pktmbuf_pool);
1570 rte_mempool_free(pktmbuf_pool2);
1574 REGISTER_TEST_COMMAND(mbuf_autotest, test_mbuf);