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_debug.h>
17 #include <rte_memory.h>
18 #include <rte_memcpy.h>
19 #include <rte_launch.h>
21 #include <rte_per_lcore.h>
22 #include <rte_lcore.h>
23 #include <rte_atomic.h>
24 #include <rte_branch_prediction.h>
26 #include <rte_mempool.h>
28 #include <rte_random.h>
29 #include <rte_cycles.h>
30 #include <rte_malloc.h>
31 #include <rte_mbuf_dyn.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 /* size of private data for mbuf in pktmbuf_pool2 */
44 #define MBUF2_PRIV_SIZE 128
46 #define REFCNT_MAX_ITER 64
47 #define REFCNT_MAX_TIMEOUT 10
48 #define REFCNT_MAX_REF (RTE_MAX_LCORE)
49 #define REFCNT_MBUF_NUM 64
50 #define REFCNT_RING_SIZE (REFCNT_MBUF_NUM * REFCNT_MAX_REF)
52 #define MAGIC_DATA 0x42424242
54 #define MAKE_STRING(x) # x
56 #ifdef RTE_MBUF_REFCNT_ATOMIC
58 static volatile uint32_t refcnt_stop_slaves;
59 static unsigned refcnt_lcore[RTE_MAX_LCORE];
67 * #. Allocate a mbuf pool.
69 * - The pool contains NB_MBUF elements, where each mbuf is MBUF_SIZE
72 * #. Test multiple allocations of mbufs from this pool.
74 * - Allocate NB_MBUF and store pointers in a table.
75 * - If an allocation fails, return an error.
76 * - Free all these mbufs.
77 * - Repeat the same test to check that mbufs were freed correctly.
79 * #. Test data manipulation in pktmbuf.
82 * - Append data using rte_pktmbuf_append().
83 * - Test for error in rte_pktmbuf_append() when len is too large.
84 * - Trim data at the end of mbuf using rte_pktmbuf_trim().
85 * - Test for error in rte_pktmbuf_trim() when len is too large.
86 * - Prepend a header using rte_pktmbuf_prepend().
87 * - Test for error in rte_pktmbuf_prepend() when len is too large.
88 * - Remove data at the beginning of mbuf using rte_pktmbuf_adj().
89 * - Test for error in rte_pktmbuf_adj() when len is too large.
90 * - Check that appended data is not corrupt.
92 * - Between all these tests, check data_len and pkt_len, and
93 * that the mbuf is contiguous.
94 * - Repeat the test to check that allocation operations
95 * reinitialize the mbuf correctly.
97 * #. Test packet cloning
98 * - Clone a mbuf and verify the data
99 * - Clone the cloned mbuf and verify the data
100 * - Attach a mbuf to another that does not have the same priv_size.
103 #define GOTO_FAIL(str, ...) do { \
104 printf("mbuf test FAILED (l.%d): <" str ">\n", \
105 __LINE__, ##__VA_ARGS__); \
110 * test data manipulation in mbuf with non-ascii data
113 test_pktmbuf_with_non_ascii_data(struct rte_mempool *pktmbuf_pool)
115 struct rte_mbuf *m = NULL;
118 m = rte_pktmbuf_alloc(pktmbuf_pool);
120 GOTO_FAIL("Cannot allocate mbuf");
121 if (rte_pktmbuf_pkt_len(m) != 0)
122 GOTO_FAIL("Bad length");
124 data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN);
126 GOTO_FAIL("Cannot append data");
127 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
128 GOTO_FAIL("Bad pkt length");
129 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
130 GOTO_FAIL("Bad data length");
131 memset(data, 0xff, rte_pktmbuf_pkt_len(m));
132 if (!rte_pktmbuf_is_contiguous(m))
133 GOTO_FAIL("Buffer should be continuous");
134 rte_pktmbuf_dump(stdout, m, MBUF_TEST_DATA_LEN);
148 * test data manipulation in mbuf
151 test_one_pktmbuf(struct rte_mempool *pktmbuf_pool)
153 struct rte_mbuf *m = NULL;
154 char *data, *data2, *hdr;
157 printf("Test pktmbuf API\n");
161 m = rte_pktmbuf_alloc(pktmbuf_pool);
163 GOTO_FAIL("Cannot allocate mbuf");
164 if (rte_pktmbuf_pkt_len(m) != 0)
165 GOTO_FAIL("Bad length");
167 rte_pktmbuf_dump(stdout, m, 0);
171 data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN);
173 GOTO_FAIL("Cannot append data");
174 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
175 GOTO_FAIL("Bad pkt length");
176 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
177 GOTO_FAIL("Bad data length");
178 memset(data, 0x66, rte_pktmbuf_pkt_len(m));
179 if (!rte_pktmbuf_is_contiguous(m))
180 GOTO_FAIL("Buffer should be continuous");
181 rte_pktmbuf_dump(stdout, m, MBUF_TEST_DATA_LEN);
182 rte_pktmbuf_dump(stdout, m, 2*MBUF_TEST_DATA_LEN);
184 /* this append should fail */
186 data2 = rte_pktmbuf_append(m, (uint16_t)(rte_pktmbuf_tailroom(m) + 1));
188 GOTO_FAIL("Append should not succeed");
190 /* append some more data */
192 data2 = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN2);
194 GOTO_FAIL("Cannot append data");
195 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_DATA_LEN2)
196 GOTO_FAIL("Bad pkt length");
197 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_DATA_LEN2)
198 GOTO_FAIL("Bad data length");
199 if (!rte_pktmbuf_is_contiguous(m))
200 GOTO_FAIL("Buffer should be continuous");
202 /* trim data at the end of mbuf */
204 if (rte_pktmbuf_trim(m, MBUF_TEST_DATA_LEN2) < 0)
205 GOTO_FAIL("Cannot trim data");
206 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
207 GOTO_FAIL("Bad pkt length");
208 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
209 GOTO_FAIL("Bad data length");
210 if (!rte_pktmbuf_is_contiguous(m))
211 GOTO_FAIL("Buffer should be continuous");
213 /* this trim should fail */
215 if (rte_pktmbuf_trim(m, (uint16_t)(rte_pktmbuf_data_len(m) + 1)) == 0)
216 GOTO_FAIL("trim should not succeed");
218 /* prepend one header */
220 hdr = rte_pktmbuf_prepend(m, MBUF_TEST_HDR1_LEN);
222 GOTO_FAIL("Cannot prepend");
223 if (data - hdr != MBUF_TEST_HDR1_LEN)
224 GOTO_FAIL("Prepend failed");
225 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_HDR1_LEN)
226 GOTO_FAIL("Bad pkt length");
227 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_HDR1_LEN)
228 GOTO_FAIL("Bad data length");
229 if (!rte_pktmbuf_is_contiguous(m))
230 GOTO_FAIL("Buffer should be continuous");
231 memset(hdr, 0x55, MBUF_TEST_HDR1_LEN);
233 /* prepend another header */
235 hdr = rte_pktmbuf_prepend(m, MBUF_TEST_HDR2_LEN);
237 GOTO_FAIL("Cannot prepend");
238 if (data - hdr != MBUF_TEST_ALL_HDRS_LEN)
239 GOTO_FAIL("Prepend failed");
240 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_ALL_HDRS_LEN)
241 GOTO_FAIL("Bad pkt length");
242 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_ALL_HDRS_LEN)
243 GOTO_FAIL("Bad data length");
244 if (!rte_pktmbuf_is_contiguous(m))
245 GOTO_FAIL("Buffer should be continuous");
246 memset(hdr, 0x55, MBUF_TEST_HDR2_LEN);
248 rte_mbuf_sanity_check(m, 1);
249 rte_mbuf_sanity_check(m, 0);
250 rte_pktmbuf_dump(stdout, m, 0);
252 /* this prepend should fail */
254 hdr = rte_pktmbuf_prepend(m, (uint16_t)(rte_pktmbuf_headroom(m) + 1));
256 GOTO_FAIL("prepend should not succeed");
258 /* remove data at beginning of mbuf (adj) */
260 if (data != rte_pktmbuf_adj(m, MBUF_TEST_ALL_HDRS_LEN))
261 GOTO_FAIL("rte_pktmbuf_adj failed");
262 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
263 GOTO_FAIL("Bad pkt length");
264 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
265 GOTO_FAIL("Bad data length");
266 if (!rte_pktmbuf_is_contiguous(m))
267 GOTO_FAIL("Buffer should be continuous");
269 /* this adj should fail */
271 if (rte_pktmbuf_adj(m, (uint16_t)(rte_pktmbuf_data_len(m) + 1)) != NULL)
272 GOTO_FAIL("rte_pktmbuf_adj should not succeed");
276 if (!rte_pktmbuf_is_contiguous(m))
277 GOTO_FAIL("Buffer should be continuous");
279 for (i=0; i<MBUF_TEST_DATA_LEN; i++) {
281 GOTO_FAIL("Data corrupted at offset %u", i);
297 testclone_testupdate_testdetach(struct rte_mempool *pktmbuf_pool)
299 struct rte_mbuf *m = NULL;
300 struct rte_mbuf *clone = NULL;
301 struct rte_mbuf *clone2 = NULL;
302 unaligned_uint32_t *data;
305 m = rte_pktmbuf_alloc(pktmbuf_pool);
307 GOTO_FAIL("ooops not allocating mbuf");
309 if (rte_pktmbuf_pkt_len(m) != 0)
310 GOTO_FAIL("Bad length");
312 rte_pktmbuf_append(m, sizeof(uint32_t));
313 data = rte_pktmbuf_mtod(m, unaligned_uint32_t *);
316 /* clone the allocated mbuf */
317 clone = rte_pktmbuf_clone(m, pktmbuf_pool);
319 GOTO_FAIL("cannot clone data\n");
321 data = rte_pktmbuf_mtod(clone, unaligned_uint32_t *);
322 if (*data != MAGIC_DATA)
323 GOTO_FAIL("invalid data in clone\n");
325 if (rte_mbuf_refcnt_read(m) != 2)
326 GOTO_FAIL("invalid refcnt in m\n");
329 rte_pktmbuf_free(clone);
332 /* same test with a chained mbuf */
333 m->next = rte_pktmbuf_alloc(pktmbuf_pool);
335 GOTO_FAIL("Next Pkt Null\n");
337 rte_pktmbuf_append(m->next, sizeof(uint32_t));
338 data = rte_pktmbuf_mtod(m->next, unaligned_uint32_t *);
341 clone = rte_pktmbuf_clone(m, pktmbuf_pool);
343 GOTO_FAIL("cannot clone data\n");
345 data = rte_pktmbuf_mtod(clone, unaligned_uint32_t *);
346 if (*data != MAGIC_DATA)
347 GOTO_FAIL("invalid data in clone\n");
349 data = rte_pktmbuf_mtod(clone->next, unaligned_uint32_t *);
350 if (*data != MAGIC_DATA)
351 GOTO_FAIL("invalid data in clone->next\n");
353 if (rte_mbuf_refcnt_read(m) != 2)
354 GOTO_FAIL("invalid refcnt in m\n");
356 if (rte_mbuf_refcnt_read(m->next) != 2)
357 GOTO_FAIL("invalid refcnt in m->next\n");
359 /* try to clone the clone */
361 clone2 = rte_pktmbuf_clone(clone, pktmbuf_pool);
363 GOTO_FAIL("cannot clone the clone\n");
365 data = rte_pktmbuf_mtod(clone2, unaligned_uint32_t *);
366 if (*data != MAGIC_DATA)
367 GOTO_FAIL("invalid data in clone2\n");
369 data = rte_pktmbuf_mtod(clone2->next, unaligned_uint32_t *);
370 if (*data != MAGIC_DATA)
371 GOTO_FAIL("invalid data in clone2->next\n");
373 if (rte_mbuf_refcnt_read(m) != 3)
374 GOTO_FAIL("invalid refcnt in m\n");
376 if (rte_mbuf_refcnt_read(m->next) != 3)
377 GOTO_FAIL("invalid refcnt in m->next\n");
381 rte_pktmbuf_free(clone);
382 rte_pktmbuf_free(clone2);
387 printf("%s ok\n", __func__);
394 rte_pktmbuf_free(clone);
396 rte_pktmbuf_free(clone2);
401 test_attach_from_different_pool(struct rte_mempool *pktmbuf_pool,
402 struct rte_mempool *pktmbuf_pool2)
404 struct rte_mbuf *m = NULL;
405 struct rte_mbuf *clone = NULL;
406 struct rte_mbuf *clone2 = NULL;
407 char *data, *c_data, *c_data2;
410 m = rte_pktmbuf_alloc(pktmbuf_pool);
412 GOTO_FAIL("cannot allocate mbuf");
414 if (rte_pktmbuf_pkt_len(m) != 0)
415 GOTO_FAIL("Bad length");
417 data = rte_pktmbuf_mtod(m, char *);
419 /* allocate a new mbuf from the second pool, and attach it to the first
421 clone = rte_pktmbuf_alloc(pktmbuf_pool2);
423 GOTO_FAIL("cannot allocate mbuf from second pool\n");
425 /* check data room size and priv size, and erase priv */
426 if (rte_pktmbuf_data_room_size(clone->pool) != 0)
427 GOTO_FAIL("data room size should be 0\n");
428 if (rte_pktmbuf_priv_size(clone->pool) != MBUF2_PRIV_SIZE)
429 GOTO_FAIL("data room size should be %d\n", MBUF2_PRIV_SIZE);
430 memset(clone + 1, 0, MBUF2_PRIV_SIZE);
432 /* save data pointer to compare it after detach() */
433 c_data = rte_pktmbuf_mtod(clone, char *);
434 if (c_data != (char *)clone + sizeof(*clone) + MBUF2_PRIV_SIZE)
435 GOTO_FAIL("bad data pointer in clone");
436 if (rte_pktmbuf_headroom(clone) != 0)
437 GOTO_FAIL("bad headroom in clone");
439 rte_pktmbuf_attach(clone, m);
441 if (rte_pktmbuf_mtod(clone, char *) != data)
442 GOTO_FAIL("clone was not attached properly\n");
443 if (rte_pktmbuf_headroom(clone) != RTE_PKTMBUF_HEADROOM)
444 GOTO_FAIL("bad headroom in clone after attach");
445 if (rte_mbuf_refcnt_read(m) != 2)
446 GOTO_FAIL("invalid refcnt in m\n");
448 /* allocate a new mbuf from the second pool, and attach it to the first
450 clone2 = rte_pktmbuf_alloc(pktmbuf_pool2);
452 GOTO_FAIL("cannot allocate clone2 from second pool\n");
454 /* check data room size and priv size, and erase priv */
455 if (rte_pktmbuf_data_room_size(clone2->pool) != 0)
456 GOTO_FAIL("data room size should be 0\n");
457 if (rte_pktmbuf_priv_size(clone2->pool) != MBUF2_PRIV_SIZE)
458 GOTO_FAIL("data room size should be %d\n", MBUF2_PRIV_SIZE);
459 memset(clone2 + 1, 0, MBUF2_PRIV_SIZE);
461 /* save data pointer to compare it after detach() */
462 c_data2 = rte_pktmbuf_mtod(clone2, char *);
463 if (c_data2 != (char *)clone2 + sizeof(*clone2) + MBUF2_PRIV_SIZE)
464 GOTO_FAIL("bad data pointer in clone2");
465 if (rte_pktmbuf_headroom(clone2) != 0)
466 GOTO_FAIL("bad headroom in clone2");
468 rte_pktmbuf_attach(clone2, clone);
470 if (rte_pktmbuf_mtod(clone2, char *) != data)
471 GOTO_FAIL("clone2 was not attached properly\n");
472 if (rte_pktmbuf_headroom(clone2) != RTE_PKTMBUF_HEADROOM)
473 GOTO_FAIL("bad headroom in clone2 after attach");
474 if (rte_mbuf_refcnt_read(m) != 3)
475 GOTO_FAIL("invalid refcnt in m\n");
477 /* detach the clones */
478 rte_pktmbuf_detach(clone);
479 if (c_data != rte_pktmbuf_mtod(clone, char *))
480 GOTO_FAIL("clone was not detached properly\n");
481 if (rte_mbuf_refcnt_read(m) != 2)
482 GOTO_FAIL("invalid refcnt in m\n");
484 rte_pktmbuf_detach(clone2);
485 if (c_data2 != rte_pktmbuf_mtod(clone2, char *))
486 GOTO_FAIL("clone2 was not detached properly\n");
487 if (rte_mbuf_refcnt_read(m) != 1)
488 GOTO_FAIL("invalid refcnt in m\n");
490 /* free the clones and the initial mbuf */
491 rte_pktmbuf_free(clone2);
492 rte_pktmbuf_free(clone);
494 printf("%s ok\n", __func__);
501 rte_pktmbuf_free(clone);
503 rte_pktmbuf_free(clone2);
508 * test allocation and free of mbufs
511 test_pktmbuf_pool(struct rte_mempool *pktmbuf_pool)
514 struct rte_mbuf *m[NB_MBUF];
517 for (i=0; i<NB_MBUF; i++)
520 /* alloc NB_MBUF mbufs */
521 for (i=0; i<NB_MBUF; i++) {
522 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
524 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
528 struct rte_mbuf *extra = NULL;
529 extra = rte_pktmbuf_alloc(pktmbuf_pool);
531 printf("Error pool not empty");
534 extra = rte_pktmbuf_clone(m[0], pktmbuf_pool);
536 printf("Error pool not empty");
540 for (i=0; i<NB_MBUF; i++) {
542 rte_pktmbuf_free(m[i]);
549 * test that the pointer to the data on a packet mbuf is set properly
552 test_pktmbuf_pool_ptr(struct rte_mempool *pktmbuf_pool)
555 struct rte_mbuf *m[NB_MBUF];
558 for (i=0; i<NB_MBUF; i++)
561 /* alloc NB_MBUF mbufs */
562 for (i=0; i<NB_MBUF; i++) {
563 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
565 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
569 m[i]->data_off += 64;
573 for (i=0; i<NB_MBUF; i++) {
575 rte_pktmbuf_free(m[i]);
578 for (i=0; i<NB_MBUF; i++)
581 /* alloc NB_MBUF mbufs */
582 for (i=0; i<NB_MBUF; i++) {
583 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
585 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
589 if (m[i]->data_off != RTE_PKTMBUF_HEADROOM) {
590 printf("invalid data_off\n");
596 for (i=0; i<NB_MBUF; i++) {
598 rte_pktmbuf_free(m[i]);
605 test_pktmbuf_free_segment(struct rte_mempool *pktmbuf_pool)
608 struct rte_mbuf *m[NB_MBUF];
611 for (i=0; i<NB_MBUF; i++)
614 /* alloc NB_MBUF mbufs */
615 for (i=0; i<NB_MBUF; i++) {
616 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
618 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
624 for (i=0; i<NB_MBUF; i++) {
626 struct rte_mbuf *mb, *mt;
632 rte_pktmbuf_free_seg(mt);
641 * Stress test for rte_mbuf atomic refcnt.
642 * Implies that RTE_MBUF_REFCNT_ATOMIC is defined.
643 * For more efficiency, recommended to run with RTE_LIBRTE_MBUF_DEBUG defined.
646 #ifdef RTE_MBUF_REFCNT_ATOMIC
649 test_refcnt_slave(void *arg)
651 unsigned lcore, free;
653 struct rte_ring *refcnt_mbuf_ring = arg;
655 lcore = rte_lcore_id();
656 printf("%s started at lcore %u\n", __func__, lcore);
659 while (refcnt_stop_slaves == 0) {
660 if (rte_ring_dequeue(refcnt_mbuf_ring, &mp) == 0) {
662 rte_pktmbuf_free(mp);
666 refcnt_lcore[lcore] += free;
667 printf("%s finished at lcore %u, "
668 "number of freed mbufs: %u\n",
669 __func__, lcore, free);
674 test_refcnt_iter(unsigned int lcore, unsigned int iter,
675 struct rte_mempool *refcnt_pool,
676 struct rte_ring *refcnt_mbuf_ring)
679 unsigned i, n, tref, wn;
684 /* For each mbuf in the pool:
686 * - increment it's reference up to N+1,
687 * - enqueue it N times into the ring for slave cores to free.
689 for (i = 0, n = rte_mempool_avail_count(refcnt_pool);
690 i != n && (m = rte_pktmbuf_alloc(refcnt_pool)) != NULL;
692 ref = RTE_MAX(rte_rand() % REFCNT_MAX_REF, 1UL);
694 if ((ref & 1) != 0) {
695 rte_pktmbuf_refcnt_update(m, ref);
697 rte_ring_enqueue(refcnt_mbuf_ring, m);
700 rte_pktmbuf_refcnt_update(m, 1);
701 rte_ring_enqueue(refcnt_mbuf_ring, m);
708 rte_panic("(lcore=%u, iter=%u): was able to allocate only "
709 "%u from %u mbufs\n", lcore, iter, i, n);
711 /* wait till slave lcores will consume all mbufs */
712 while (!rte_ring_empty(refcnt_mbuf_ring))
715 /* check that all mbufs are back into mempool by now */
716 for (wn = 0; wn != REFCNT_MAX_TIMEOUT; wn++) {
717 if ((i = rte_mempool_avail_count(refcnt_pool)) == n) {
718 refcnt_lcore[lcore] += tref;
719 printf("%s(lcore=%u, iter=%u) completed, "
720 "%u references processed\n",
721 __func__, lcore, iter, tref);
727 rte_panic("(lcore=%u, iter=%u): after %us only "
728 "%u of %u mbufs left free\n", lcore, iter, wn, i, n);
732 test_refcnt_master(struct rte_mempool *refcnt_pool,
733 struct rte_ring *refcnt_mbuf_ring)
737 lcore = rte_lcore_id();
738 printf("%s started at lcore %u\n", __func__, lcore);
740 for (i = 0; i != REFCNT_MAX_ITER; i++)
741 test_refcnt_iter(lcore, i, refcnt_pool, refcnt_mbuf_ring);
743 refcnt_stop_slaves = 1;
746 printf("%s finished at lcore %u\n", __func__, lcore);
753 test_refcnt_mbuf(void)
755 #ifdef RTE_MBUF_REFCNT_ATOMIC
756 unsigned int master, slave, tref;
758 struct rte_mempool *refcnt_pool = NULL;
759 struct rte_ring *refcnt_mbuf_ring = NULL;
761 if (rte_lcore_count() < 2) {
762 printf("Not enough cores for test_refcnt_mbuf, expecting at least 2\n");
766 printf("starting %s, at %u lcores\n", __func__, rte_lcore_count());
768 /* create refcnt pool & ring if they don't exist */
770 refcnt_pool = rte_pktmbuf_pool_create(MAKE_STRING(refcnt_pool),
771 REFCNT_MBUF_NUM, 0, 0, 0,
773 if (refcnt_pool == NULL) {
774 printf("%s: cannot allocate " MAKE_STRING(refcnt_pool) "\n",
779 refcnt_mbuf_ring = rte_ring_create("refcnt_mbuf_ring",
780 rte_align32pow2(REFCNT_RING_SIZE), SOCKET_ID_ANY,
782 if (refcnt_mbuf_ring == NULL) {
783 printf("%s: cannot allocate " MAKE_STRING(refcnt_mbuf_ring)
788 refcnt_stop_slaves = 0;
789 memset(refcnt_lcore, 0, sizeof (refcnt_lcore));
791 rte_eal_mp_remote_launch(test_refcnt_slave, refcnt_mbuf_ring,
794 test_refcnt_master(refcnt_pool, refcnt_mbuf_ring);
796 rte_eal_mp_wait_lcore();
798 /* check that we porcessed all references */
800 master = rte_get_master_lcore();
802 RTE_LCORE_FOREACH_SLAVE(slave)
803 tref += refcnt_lcore[slave];
805 if (tref != refcnt_lcore[master])
806 rte_panic("refernced mbufs: %u, freed mbufs: %u\n",
807 tref, refcnt_lcore[master]);
809 rte_mempool_dump(stdout, refcnt_pool);
810 rte_ring_dump(stdout, refcnt_mbuf_ring);
815 rte_mempool_free(refcnt_pool);
816 rte_ring_free(refcnt_mbuf_ring);
824 #include <sys/wait.h>
826 /* use fork() to test mbuf errors panic */
828 verify_mbuf_check_panics(struct rte_mbuf *buf)
836 rte_mbuf_sanity_check(buf, 1); /* should panic */
837 exit(0); /* return normally if it doesn't panic */
839 printf("Fork Failed\n");
850 test_failing_mbuf_sanity_check(struct rte_mempool *pktmbuf_pool)
852 struct rte_mbuf *buf;
853 struct rte_mbuf badbuf;
855 printf("Checking rte_mbuf_sanity_check for failure conditions\n");
857 /* get a good mbuf to use to make copies */
858 buf = rte_pktmbuf_alloc(pktmbuf_pool);
861 printf("Checking good mbuf initially\n");
862 if (verify_mbuf_check_panics(buf) != -1)
865 printf("Now checking for error conditions\n");
867 if (verify_mbuf_check_panics(NULL)) {
868 printf("Error with NULL mbuf test\n");
874 if (verify_mbuf_check_panics(&badbuf)) {
875 printf("Error with bad-pool mbuf test\n");
881 if (verify_mbuf_check_panics(&badbuf)) {
882 printf("Error with bad-physaddr mbuf test\n");
887 badbuf.buf_addr = NULL;
888 if (verify_mbuf_check_panics(&badbuf)) {
889 printf("Error with bad-addr mbuf test\n");
895 if (verify_mbuf_check_panics(&badbuf)) {
896 printf("Error with bad-refcnt(0) mbuf test\n");
901 badbuf.refcnt = UINT16_MAX;
902 if (verify_mbuf_check_panics(&badbuf)) {
903 printf("Error with bad-refcnt(MAX) mbuf test\n");
911 test_mbuf_linearize(struct rte_mempool *pktmbuf_pool, int pkt_len,
915 struct rte_mbuf *m = NULL, *mbuf = NULL;
923 printf("Packet size must be 1 or more (is %d)\n", pkt_len);
928 printf("Number of segments must be 1 or more (is %d)\n",
933 seg_len = pkt_len / nb_segs;
939 /* Create chained mbuf_src and fill it generated data */
940 for (seg = 0; remain > 0; seg++) {
942 m = rte_pktmbuf_alloc(pktmbuf_pool);
944 printf("Cannot create segment for source mbuf");
948 /* Make sure if tailroom is zeroed */
949 memset(rte_pktmbuf_mtod(m, uint8_t *), 0,
950 rte_pktmbuf_tailroom(m));
953 if (data_len > seg_len)
956 data = (uint8_t *)rte_pktmbuf_append(m, data_len);
958 printf("Cannot append %d bytes to the mbuf\n",
963 for (i = 0; i < data_len; i++)
964 data[i] = (seg * seg_len + i) % 0x0ff;
969 rte_pktmbuf_chain(mbuf, m);
974 /* Create destination buffer to store coalesced data */
975 if (rte_pktmbuf_linearize(mbuf)) {
976 printf("Mbuf linearization failed\n");
980 if (!rte_pktmbuf_is_contiguous(mbuf)) {
981 printf("Source buffer should be contiguous after "
986 data = rte_pktmbuf_mtod(mbuf, uint8_t *);
988 for (i = 0; i < pkt_len; i++)
989 if (data[i] != (i % 0x0ff)) {
990 printf("Incorrect data in linearized mbuf\n");
994 rte_pktmbuf_free(mbuf);
999 rte_pktmbuf_free(mbuf);
1004 test_mbuf_linearize_check(struct rte_mempool *pktmbuf_pool)
1006 struct test_mbuf_array {
1018 printf("Test mbuf linearize API\n");
1020 for (i = 0; i < RTE_DIM(mbuf_array); i++)
1021 if (test_mbuf_linearize(pktmbuf_pool, mbuf_array[i].size,
1022 mbuf_array[i].nb_segs)) {
1023 printf("Test failed for %d, %d\n", mbuf_array[i].size,
1024 mbuf_array[i].nb_segs);
1032 * Helper function for test_tx_ofload
1035 set_tx_offload(struct rte_mbuf *mb, uint64_t il2, uint64_t il3, uint64_t il4,
1036 uint64_t tso, uint64_t ol3, uint64_t ol2)
1041 mb->tso_segsz = tso;
1042 mb->outer_l3_len = ol3;
1043 mb->outer_l2_len = ol2;
1047 test_tx_offload(void)
1049 struct rte_mbuf *mb;
1050 uint64_t tm, v1, v2;
1054 static volatile struct {
1061 const uint32_t num = 0x10000;
1063 txof.l2 = rte_rand() % (1 << RTE_MBUF_L2_LEN_BITS);
1064 txof.l3 = rte_rand() % (1 << RTE_MBUF_L3_LEN_BITS);
1065 txof.l4 = rte_rand() % (1 << RTE_MBUF_L4_LEN_BITS);
1066 txof.tso = rte_rand() % (1 << RTE_MBUF_TSO_SEGSZ_BITS);
1068 printf("%s started, tx_offload = {\n"
1072 "\ttso_segsz=%#hx,\n"
1073 "\touter_l3_len=%#x,\n"
1074 "\touter_l2_len=%#x,\n"
1077 txof.l2, txof.l3, txof.l4, txof.tso, txof.l3, txof.l2);
1079 sz = sizeof(*mb) * num;
1080 mb = rte_zmalloc(NULL, sz, RTE_CACHE_LINE_SIZE);
1082 printf("%s failed, out of memory\n", __func__);
1087 tm = rte_rdtsc_precise();
1089 for (i = 0; i != num; i++)
1090 set_tx_offload(mb + i, txof.l2, txof.l3, txof.l4,
1091 txof.tso, txof.l3, txof.l2);
1093 tm = rte_rdtsc_precise() - tm;
1094 printf("%s set tx_offload by bit-fields: %u iterations, %"
1095 PRIu64 " cycles, %#Lf cycles/iter\n",
1096 __func__, num, tm, (long double)tm / num);
1098 v1 = mb[rte_rand() % num].tx_offload;
1101 tm = rte_rdtsc_precise();
1103 for (i = 0; i != num; i++)
1104 mb[i].tx_offload = rte_mbuf_tx_offload(txof.l2, txof.l3,
1105 txof.l4, txof.tso, txof.l3, txof.l2, 0);
1107 tm = rte_rdtsc_precise() - tm;
1108 printf("%s set raw tx_offload: %u iterations, %"
1109 PRIu64 " cycles, %#Lf cycles/iter\n",
1110 __func__, num, tm, (long double)tm / num);
1112 v2 = mb[rte_rand() % num].tx_offload;
1116 printf("%s finished\n"
1117 "expected tx_offload value: 0x%" PRIx64 ";\n"
1118 "rte_mbuf_tx_offload value: 0x%" PRIx64 ";\n",
1121 return (v1 == v2) ? 0 : -EINVAL;
1125 test_mbuf_dyn(struct rte_mempool *pktmbuf_pool)
1127 struct rte_mbuf *m = NULL;
1128 int offset, offset2;
1131 offset = rte_mbuf_dynfield_register("test-dynfield", sizeof(uint8_t),
1132 __alignof__(uint8_t), 0);
1134 GOTO_FAIL("failed to register dynamic field, offset=%d: %s",
1135 offset, strerror(errno));
1137 offset2 = rte_mbuf_dynfield_register("test-dynfield", sizeof(uint8_t),
1138 __alignof__(uint8_t), 0);
1139 if (offset2 != offset)
1140 GOTO_FAIL("failed to lookup dynamic field, offset=%d, offset2=%d: %s",
1141 offset, offset2, strerror(errno));
1143 offset2 = rte_mbuf_dynfield_register("test-dynfield2", sizeof(uint16_t),
1144 __alignof__(uint16_t), 0);
1145 if (offset2 == -1 || offset2 == offset || (offset & 1))
1146 GOTO_FAIL("failed to register dynfield field 2, offset=%d, offset2=%d: %s",
1147 offset, offset2, strerror(errno));
1149 printf("offset = %d, offset2 = %d\n", offset, offset2);
1151 offset = rte_mbuf_dynfield_register("test-dynfield-fail", 256, 1, 0);
1153 GOTO_FAIL("dynamic field creation should fail (too big)");
1155 offset = rte_mbuf_dynfield_register("test-dynfield-fail", 1, 3, 0);
1157 GOTO_FAIL("dynamic field creation should fail (bad alignment)");
1159 flag = rte_mbuf_dynflag_register("test-dynflag");
1161 GOTO_FAIL("failed to register dynamic field, flag=%d: %s",
1162 flag, strerror(errno));
1164 flag2 = rte_mbuf_dynflag_register("test-dynflag");
1166 GOTO_FAIL("failed to lookup dynamic field, flag=%d, flag2=%d: %s",
1167 flag, flag2, strerror(errno));
1169 flag2 = rte_mbuf_dynflag_register("test-dynflag2");
1170 if (flag2 == -1 || flag2 == flag)
1171 GOTO_FAIL("failed to register dynflag field 2, flag=%d, flag2=%d: %s",
1172 flag, flag2, strerror(errno));
1174 printf("flag = %d, flag2 = %d\n", flag, flag2);
1176 /* set, get dynamic field */
1177 m = rte_pktmbuf_alloc(pktmbuf_pool);
1179 GOTO_FAIL("Cannot allocate mbuf");
1181 *RTE_MBUF_DYNFIELD(m, offset, uint8_t *) = 1;
1182 if (*RTE_MBUF_DYNFIELD(m, offset, uint8_t *) != 1)
1183 GOTO_FAIL("failed to read dynamic field");
1184 *RTE_MBUF_DYNFIELD(m, offset2, uint16_t *) = 1000;
1185 if (*RTE_MBUF_DYNFIELD(m, offset2, uint16_t *) != 1000)
1186 GOTO_FAIL("failed to read dynamic field");
1188 /* set a dynamic flag */
1189 m->ol_flags |= (1ULL << flag);
1191 rte_pktmbuf_free(m);
1194 rte_pktmbuf_free(m);
1203 struct rte_mempool *pktmbuf_pool = NULL;
1204 struct rte_mempool *pktmbuf_pool2 = NULL;
1207 RTE_BUILD_BUG_ON(sizeof(struct rte_mbuf) != RTE_CACHE_LINE_MIN_SIZE * 2);
1209 /* create pktmbuf pool if it does not exist */
1210 pktmbuf_pool = rte_pktmbuf_pool_create("test_pktmbuf_pool",
1211 NB_MBUF, 32, 0, MBUF_DATA_SIZE, SOCKET_ID_ANY);
1213 if (pktmbuf_pool == NULL) {
1214 printf("cannot allocate mbuf pool\n");
1218 /* test registration of dynamic fields and flags */
1219 if (test_mbuf_dyn(pktmbuf_pool) < 0) {
1220 printf("mbuf dynflag test failed\n");
1224 /* create a specific pktmbuf pool with a priv_size != 0 and no data
1226 pktmbuf_pool2 = rte_pktmbuf_pool_create("test_pktmbuf_pool2",
1227 NB_MBUF, 32, MBUF2_PRIV_SIZE, 0, SOCKET_ID_ANY);
1229 if (pktmbuf_pool2 == NULL) {
1230 printf("cannot allocate mbuf pool\n");
1234 /* test multiple mbuf alloc */
1235 if (test_pktmbuf_pool(pktmbuf_pool) < 0) {
1236 printf("test_mbuf_pool() failed\n");
1240 /* do it another time to check that all mbufs were freed */
1241 if (test_pktmbuf_pool(pktmbuf_pool) < 0) {
1242 printf("test_mbuf_pool() failed (2)\n");
1246 /* test that the pointer to the data on a packet mbuf is set properly */
1247 if (test_pktmbuf_pool_ptr(pktmbuf_pool) < 0) {
1248 printf("test_pktmbuf_pool_ptr() failed\n");
1252 /* test data manipulation in mbuf */
1253 if (test_one_pktmbuf(pktmbuf_pool) < 0) {
1254 printf("test_one_mbuf() failed\n");
1260 * do it another time, to check that allocation reinitialize
1261 * the mbuf correctly
1263 if (test_one_pktmbuf(pktmbuf_pool) < 0) {
1264 printf("test_one_mbuf() failed (2)\n");
1268 if (test_pktmbuf_with_non_ascii_data(pktmbuf_pool) < 0) {
1269 printf("test_pktmbuf_with_non_ascii_data() failed\n");
1273 /* test free pktmbuf segment one by one */
1274 if (test_pktmbuf_free_segment(pktmbuf_pool) < 0) {
1275 printf("test_pktmbuf_free_segment() failed.\n");
1279 if (testclone_testupdate_testdetach(pktmbuf_pool) < 0) {
1280 printf("testclone_and_testupdate() failed \n");
1284 if (test_attach_from_different_pool(pktmbuf_pool, pktmbuf_pool2) < 0) {
1285 printf("test_attach_from_different_pool() failed\n");
1289 if (test_refcnt_mbuf() < 0) {
1290 printf("test_refcnt_mbuf() failed \n");
1294 if (test_failing_mbuf_sanity_check(pktmbuf_pool) < 0) {
1295 printf("test_failing_mbuf_sanity_check() failed\n");
1299 if (test_mbuf_linearize_check(pktmbuf_pool) < 0) {
1300 printf("test_mbuf_linearize_check() failed\n");
1304 if (test_tx_offload() < 0) {
1305 printf("test_tx_offload() failed\n");
1311 rte_mempool_free(pktmbuf_pool);
1312 rte_mempool_free(pktmbuf_pool2);
1316 REGISTER_TEST_COMMAND(mbuf_autotest, test_mbuf);