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>
34 #define MBUF_DATA_SIZE 2048
36 #define MBUF_TEST_DATA_LEN 1464
37 #define MBUF_TEST_DATA_LEN2 50
38 #define MBUF_TEST_HDR1_LEN 20
39 #define MBUF_TEST_HDR2_LEN 30
40 #define MBUF_TEST_ALL_HDRS_LEN (MBUF_TEST_HDR1_LEN+MBUF_TEST_HDR2_LEN)
42 /* size of private data for mbuf in pktmbuf_pool2 */
43 #define MBUF2_PRIV_SIZE 128
45 #define REFCNT_MAX_ITER 64
46 #define REFCNT_MAX_TIMEOUT 10
47 #define REFCNT_MAX_REF (RTE_MAX_LCORE)
48 #define REFCNT_MBUF_NUM 64
49 #define REFCNT_RING_SIZE (REFCNT_MBUF_NUM * REFCNT_MAX_REF)
51 #define MAGIC_DATA 0x42424242
53 #define MAKE_STRING(x) # x
55 #ifdef RTE_MBUF_REFCNT_ATOMIC
57 static volatile uint32_t refcnt_stop_slaves;
58 static unsigned refcnt_lcore[RTE_MAX_LCORE];
66 * #. Allocate a mbuf pool.
68 * - The pool contains NB_MBUF elements, where each mbuf is MBUF_SIZE
71 * #. Test multiple allocations of mbufs from this pool.
73 * - Allocate NB_MBUF and store pointers in a table.
74 * - If an allocation fails, return an error.
75 * - Free all these mbufs.
76 * - Repeat the same test to check that mbufs were freed correctly.
78 * #. Test data manipulation in pktmbuf.
81 * - Append data using rte_pktmbuf_append().
82 * - Test for error in rte_pktmbuf_append() when len is too large.
83 * - Trim data at the end of mbuf using rte_pktmbuf_trim().
84 * - Test for error in rte_pktmbuf_trim() when len is too large.
85 * - Prepend a header using rte_pktmbuf_prepend().
86 * - Test for error in rte_pktmbuf_prepend() when len is too large.
87 * - Remove data at the beginning of mbuf using rte_pktmbuf_adj().
88 * - Test for error in rte_pktmbuf_adj() when len is too large.
89 * - Check that appended data is not corrupt.
91 * - Between all these tests, check data_len and pkt_len, and
92 * that the mbuf is contiguous.
93 * - Repeat the test to check that allocation operations
94 * reinitialize the mbuf correctly.
96 * #. Test packet cloning
97 * - Clone a mbuf and verify the data
98 * - Clone the cloned mbuf and verify the data
99 * - Attach a mbuf to another that does not have the same priv_size.
102 #define GOTO_FAIL(str, ...) do { \
103 printf("mbuf test FAILED (l.%d): <" str ">\n", \
104 __LINE__, ##__VA_ARGS__); \
109 * test data manipulation in mbuf with non-ascii data
112 test_pktmbuf_with_non_ascii_data(struct rte_mempool *pktmbuf_pool)
114 struct rte_mbuf *m = NULL;
117 m = rte_pktmbuf_alloc(pktmbuf_pool);
119 GOTO_FAIL("Cannot allocate mbuf");
120 if (rte_pktmbuf_pkt_len(m) != 0)
121 GOTO_FAIL("Bad length");
123 data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN);
125 GOTO_FAIL("Cannot append data");
126 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
127 GOTO_FAIL("Bad pkt length");
128 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
129 GOTO_FAIL("Bad data length");
130 memset(data, 0xff, rte_pktmbuf_pkt_len(m));
131 if (!rte_pktmbuf_is_contiguous(m))
132 GOTO_FAIL("Buffer should be continuous");
133 rte_pktmbuf_dump(stdout, m, MBUF_TEST_DATA_LEN);
147 * test data manipulation in mbuf
150 test_one_pktmbuf(struct rte_mempool *pktmbuf_pool)
152 struct rte_mbuf *m = NULL;
153 char *data, *data2, *hdr;
156 printf("Test pktmbuf API\n");
160 m = rte_pktmbuf_alloc(pktmbuf_pool);
162 GOTO_FAIL("Cannot allocate mbuf");
163 if (rte_pktmbuf_pkt_len(m) != 0)
164 GOTO_FAIL("Bad length");
166 rte_pktmbuf_dump(stdout, m, 0);
170 data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN);
172 GOTO_FAIL("Cannot append data");
173 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
174 GOTO_FAIL("Bad pkt length");
175 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
176 GOTO_FAIL("Bad data length");
177 memset(data, 0x66, rte_pktmbuf_pkt_len(m));
178 if (!rte_pktmbuf_is_contiguous(m))
179 GOTO_FAIL("Buffer should be continuous");
180 rte_pktmbuf_dump(stdout, m, MBUF_TEST_DATA_LEN);
181 rte_pktmbuf_dump(stdout, m, 2*MBUF_TEST_DATA_LEN);
183 /* this append should fail */
185 data2 = rte_pktmbuf_append(m, (uint16_t)(rte_pktmbuf_tailroom(m) + 1));
187 GOTO_FAIL("Append should not succeed");
189 /* append some more data */
191 data2 = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN2);
193 GOTO_FAIL("Cannot append data");
194 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_DATA_LEN2)
195 GOTO_FAIL("Bad pkt length");
196 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_DATA_LEN2)
197 GOTO_FAIL("Bad data length");
198 if (!rte_pktmbuf_is_contiguous(m))
199 GOTO_FAIL("Buffer should be continuous");
201 /* trim data at the end of mbuf */
203 if (rte_pktmbuf_trim(m, MBUF_TEST_DATA_LEN2) < 0)
204 GOTO_FAIL("Cannot trim data");
205 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
206 GOTO_FAIL("Bad pkt length");
207 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
208 GOTO_FAIL("Bad data length");
209 if (!rte_pktmbuf_is_contiguous(m))
210 GOTO_FAIL("Buffer should be continuous");
212 /* this trim should fail */
214 if (rte_pktmbuf_trim(m, (uint16_t)(rte_pktmbuf_data_len(m) + 1)) == 0)
215 GOTO_FAIL("trim should not succeed");
217 /* prepend one header */
219 hdr = rte_pktmbuf_prepend(m, MBUF_TEST_HDR1_LEN);
221 GOTO_FAIL("Cannot prepend");
222 if (data - hdr != MBUF_TEST_HDR1_LEN)
223 GOTO_FAIL("Prepend failed");
224 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_HDR1_LEN)
225 GOTO_FAIL("Bad pkt length");
226 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_HDR1_LEN)
227 GOTO_FAIL("Bad data length");
228 if (!rte_pktmbuf_is_contiguous(m))
229 GOTO_FAIL("Buffer should be continuous");
230 memset(hdr, 0x55, MBUF_TEST_HDR1_LEN);
232 /* prepend another header */
234 hdr = rte_pktmbuf_prepend(m, MBUF_TEST_HDR2_LEN);
236 GOTO_FAIL("Cannot prepend");
237 if (data - hdr != MBUF_TEST_ALL_HDRS_LEN)
238 GOTO_FAIL("Prepend failed");
239 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_ALL_HDRS_LEN)
240 GOTO_FAIL("Bad pkt length");
241 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_ALL_HDRS_LEN)
242 GOTO_FAIL("Bad data length");
243 if (!rte_pktmbuf_is_contiguous(m))
244 GOTO_FAIL("Buffer should be continuous");
245 memset(hdr, 0x55, MBUF_TEST_HDR2_LEN);
247 rte_mbuf_sanity_check(m, 1);
248 rte_mbuf_sanity_check(m, 0);
249 rte_pktmbuf_dump(stdout, m, 0);
251 /* this prepend should fail */
253 hdr = rte_pktmbuf_prepend(m, (uint16_t)(rte_pktmbuf_headroom(m) + 1));
255 GOTO_FAIL("prepend should not succeed");
257 /* remove data at beginning of mbuf (adj) */
259 if (data != rte_pktmbuf_adj(m, MBUF_TEST_ALL_HDRS_LEN))
260 GOTO_FAIL("rte_pktmbuf_adj failed");
261 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
262 GOTO_FAIL("Bad pkt length");
263 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
264 GOTO_FAIL("Bad data length");
265 if (!rte_pktmbuf_is_contiguous(m))
266 GOTO_FAIL("Buffer should be continuous");
268 /* this adj should fail */
270 if (rte_pktmbuf_adj(m, (uint16_t)(rte_pktmbuf_data_len(m) + 1)) != NULL)
271 GOTO_FAIL("rte_pktmbuf_adj should not succeed");
275 if (!rte_pktmbuf_is_contiguous(m))
276 GOTO_FAIL("Buffer should be continuous");
278 for (i=0; i<MBUF_TEST_DATA_LEN; i++) {
280 GOTO_FAIL("Data corrupted at offset %u", i);
296 testclone_testupdate_testdetach(struct rte_mempool *pktmbuf_pool)
298 struct rte_mbuf *m = NULL;
299 struct rte_mbuf *clone = NULL;
300 struct rte_mbuf *clone2 = NULL;
301 unaligned_uint32_t *data;
304 m = rte_pktmbuf_alloc(pktmbuf_pool);
306 GOTO_FAIL("ooops not allocating mbuf");
308 if (rte_pktmbuf_pkt_len(m) != 0)
309 GOTO_FAIL("Bad length");
311 rte_pktmbuf_append(m, sizeof(uint32_t));
312 data = rte_pktmbuf_mtod(m, unaligned_uint32_t *);
315 /* clone the allocated mbuf */
316 clone = rte_pktmbuf_clone(m, pktmbuf_pool);
318 GOTO_FAIL("cannot clone data\n");
320 data = rte_pktmbuf_mtod(clone, unaligned_uint32_t *);
321 if (*data != MAGIC_DATA)
322 GOTO_FAIL("invalid data in clone\n");
324 if (rte_mbuf_refcnt_read(m) != 2)
325 GOTO_FAIL("invalid refcnt in m\n");
328 rte_pktmbuf_free(clone);
331 /* same test with a chained mbuf */
332 m->next = rte_pktmbuf_alloc(pktmbuf_pool);
334 GOTO_FAIL("Next Pkt Null\n");
337 rte_pktmbuf_append(m->next, sizeof(uint32_t));
338 m->pkt_len = 2 * sizeof(uint32_t);
340 data = rte_pktmbuf_mtod(m->next, unaligned_uint32_t *);
343 clone = rte_pktmbuf_clone(m, pktmbuf_pool);
345 GOTO_FAIL("cannot clone data\n");
347 data = rte_pktmbuf_mtod(clone, unaligned_uint32_t *);
348 if (*data != MAGIC_DATA)
349 GOTO_FAIL("invalid data in clone\n");
351 data = rte_pktmbuf_mtod(clone->next, unaligned_uint32_t *);
352 if (*data != MAGIC_DATA)
353 GOTO_FAIL("invalid data in clone->next\n");
355 if (rte_mbuf_refcnt_read(m) != 2)
356 GOTO_FAIL("invalid refcnt in m\n");
358 if (rte_mbuf_refcnt_read(m->next) != 2)
359 GOTO_FAIL("invalid refcnt in m->next\n");
361 /* try to clone the clone */
363 clone2 = rte_pktmbuf_clone(clone, pktmbuf_pool);
365 GOTO_FAIL("cannot clone the clone\n");
367 data = rte_pktmbuf_mtod(clone2, unaligned_uint32_t *);
368 if (*data != MAGIC_DATA)
369 GOTO_FAIL("invalid data in clone2\n");
371 data = rte_pktmbuf_mtod(clone2->next, unaligned_uint32_t *);
372 if (*data != MAGIC_DATA)
373 GOTO_FAIL("invalid data in clone2->next\n");
375 if (rte_mbuf_refcnt_read(m) != 3)
376 GOTO_FAIL("invalid refcnt in m\n");
378 if (rte_mbuf_refcnt_read(m->next) != 3)
379 GOTO_FAIL("invalid refcnt in m->next\n");
383 rte_pktmbuf_free(clone);
384 rte_pktmbuf_free(clone2);
389 printf("%s ok\n", __func__);
396 rte_pktmbuf_free(clone);
398 rte_pktmbuf_free(clone2);
403 test_attach_from_different_pool(struct rte_mempool *pktmbuf_pool,
404 struct rte_mempool *pktmbuf_pool2)
406 struct rte_mbuf *m = NULL;
407 struct rte_mbuf *clone = NULL;
408 struct rte_mbuf *clone2 = NULL;
409 char *data, *c_data, *c_data2;
412 m = rte_pktmbuf_alloc(pktmbuf_pool);
414 GOTO_FAIL("cannot allocate mbuf");
416 if (rte_pktmbuf_pkt_len(m) != 0)
417 GOTO_FAIL("Bad length");
419 data = rte_pktmbuf_mtod(m, char *);
421 /* allocate a new mbuf from the second pool, and attach it to the first
423 clone = rte_pktmbuf_alloc(pktmbuf_pool2);
425 GOTO_FAIL("cannot allocate mbuf from second pool\n");
427 /* check data room size and priv size, and erase priv */
428 if (rte_pktmbuf_data_room_size(clone->pool) != 0)
429 GOTO_FAIL("data room size should be 0\n");
430 if (rte_pktmbuf_priv_size(clone->pool) != MBUF2_PRIV_SIZE)
431 GOTO_FAIL("data room size should be %d\n", MBUF2_PRIV_SIZE);
432 memset(clone + 1, 0, MBUF2_PRIV_SIZE);
434 /* save data pointer to compare it after detach() */
435 c_data = rte_pktmbuf_mtod(clone, char *);
436 if (c_data != (char *)clone + sizeof(*clone) + MBUF2_PRIV_SIZE)
437 GOTO_FAIL("bad data pointer in clone");
438 if (rte_pktmbuf_headroom(clone) != 0)
439 GOTO_FAIL("bad headroom in clone");
441 rte_pktmbuf_attach(clone, m);
443 if (rte_pktmbuf_mtod(clone, char *) != data)
444 GOTO_FAIL("clone was not attached properly\n");
445 if (rte_pktmbuf_headroom(clone) != RTE_PKTMBUF_HEADROOM)
446 GOTO_FAIL("bad headroom in clone after attach");
447 if (rte_mbuf_refcnt_read(m) != 2)
448 GOTO_FAIL("invalid refcnt in m\n");
450 /* allocate a new mbuf from the second pool, and attach it to the first
452 clone2 = rte_pktmbuf_alloc(pktmbuf_pool2);
454 GOTO_FAIL("cannot allocate clone2 from second pool\n");
456 /* check data room size and priv size, and erase priv */
457 if (rte_pktmbuf_data_room_size(clone2->pool) != 0)
458 GOTO_FAIL("data room size should be 0\n");
459 if (rte_pktmbuf_priv_size(clone2->pool) != MBUF2_PRIV_SIZE)
460 GOTO_FAIL("data room size should be %d\n", MBUF2_PRIV_SIZE);
461 memset(clone2 + 1, 0, MBUF2_PRIV_SIZE);
463 /* save data pointer to compare it after detach() */
464 c_data2 = rte_pktmbuf_mtod(clone2, char *);
465 if (c_data2 != (char *)clone2 + sizeof(*clone2) + MBUF2_PRIV_SIZE)
466 GOTO_FAIL("bad data pointer in clone2");
467 if (rte_pktmbuf_headroom(clone2) != 0)
468 GOTO_FAIL("bad headroom in clone2");
470 rte_pktmbuf_attach(clone2, clone);
472 if (rte_pktmbuf_mtod(clone2, char *) != data)
473 GOTO_FAIL("clone2 was not attached properly\n");
474 if (rte_pktmbuf_headroom(clone2) != RTE_PKTMBUF_HEADROOM)
475 GOTO_FAIL("bad headroom in clone2 after attach");
476 if (rte_mbuf_refcnt_read(m) != 3)
477 GOTO_FAIL("invalid refcnt in m\n");
479 /* detach the clones */
480 rte_pktmbuf_detach(clone);
481 if (c_data != rte_pktmbuf_mtod(clone, char *))
482 GOTO_FAIL("clone was not detached properly\n");
483 if (rte_mbuf_refcnt_read(m) != 2)
484 GOTO_FAIL("invalid refcnt in m\n");
486 rte_pktmbuf_detach(clone2);
487 if (c_data2 != rte_pktmbuf_mtod(clone2, char *))
488 GOTO_FAIL("clone2 was not detached properly\n");
489 if (rte_mbuf_refcnt_read(m) != 1)
490 GOTO_FAIL("invalid refcnt in m\n");
492 /* free the clones and the initial mbuf */
493 rte_pktmbuf_free(clone2);
494 rte_pktmbuf_free(clone);
496 printf("%s ok\n", __func__);
503 rte_pktmbuf_free(clone);
505 rte_pktmbuf_free(clone2);
511 * test allocation and free of mbufs
514 test_pktmbuf_pool(struct rte_mempool *pktmbuf_pool)
517 struct rte_mbuf *m[NB_MBUF];
520 for (i=0; i<NB_MBUF; i++)
523 /* alloc NB_MBUF mbufs */
524 for (i=0; i<NB_MBUF; i++) {
525 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
527 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
531 struct rte_mbuf *extra = NULL;
532 extra = rte_pktmbuf_alloc(pktmbuf_pool);
534 printf("Error pool not empty");
537 extra = rte_pktmbuf_clone(m[0], pktmbuf_pool);
539 printf("Error pool not empty");
543 for (i=0; i<NB_MBUF; i++) {
545 rte_pktmbuf_free(m[i]);
552 * test that the pointer to the data on a packet mbuf is set properly
555 test_pktmbuf_pool_ptr(struct rte_mempool *pktmbuf_pool)
558 struct rte_mbuf *m[NB_MBUF];
561 for (i=0; i<NB_MBUF; i++)
564 /* alloc NB_MBUF mbufs */
565 for (i=0; i<NB_MBUF; i++) {
566 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
568 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
572 m[i]->data_off += 64;
576 for (i=0; i<NB_MBUF; i++) {
578 rte_pktmbuf_free(m[i]);
581 for (i=0; i<NB_MBUF; i++)
584 /* alloc NB_MBUF mbufs */
585 for (i=0; i<NB_MBUF; i++) {
586 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
588 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
592 if (m[i]->data_off != RTE_PKTMBUF_HEADROOM) {
593 printf("invalid data_off\n");
599 for (i=0; i<NB_MBUF; i++) {
601 rte_pktmbuf_free(m[i]);
608 test_pktmbuf_free_segment(struct rte_mempool *pktmbuf_pool)
611 struct rte_mbuf *m[NB_MBUF];
614 for (i=0; i<NB_MBUF; i++)
617 /* alloc NB_MBUF mbufs */
618 for (i=0; i<NB_MBUF; i++) {
619 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
621 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
627 for (i=0; i<NB_MBUF; i++) {
629 struct rte_mbuf *mb, *mt;
635 rte_pktmbuf_free_seg(mt);
644 * Stress test for rte_mbuf atomic refcnt.
645 * Implies that RTE_MBUF_REFCNT_ATOMIC is defined.
646 * For more efficiency, recommended to run with RTE_LIBRTE_MBUF_DEBUG defined.
649 #ifdef RTE_MBUF_REFCNT_ATOMIC
652 test_refcnt_slave(void *arg)
654 unsigned lcore, free;
656 struct rte_ring *refcnt_mbuf_ring = arg;
658 lcore = rte_lcore_id();
659 printf("%s started at lcore %u\n", __func__, lcore);
662 while (refcnt_stop_slaves == 0) {
663 if (rte_ring_dequeue(refcnt_mbuf_ring, &mp) == 0) {
665 rte_pktmbuf_free(mp);
669 refcnt_lcore[lcore] += free;
670 printf("%s finished at lcore %u, "
671 "number of freed mbufs: %u\n",
672 __func__, lcore, free);
677 test_refcnt_iter(unsigned int lcore, unsigned int iter,
678 struct rte_mempool *refcnt_pool,
679 struct rte_ring *refcnt_mbuf_ring)
682 unsigned i, n, tref, wn;
687 /* For each mbuf in the pool:
689 * - increment it's reference up to N+1,
690 * - enqueue it N times into the ring for slave cores to free.
692 for (i = 0, n = rte_mempool_avail_count(refcnt_pool);
693 i != n && (m = rte_pktmbuf_alloc(refcnt_pool)) != NULL;
695 ref = RTE_MAX(rte_rand() % REFCNT_MAX_REF, 1UL);
697 if ((ref & 1) != 0) {
698 rte_pktmbuf_refcnt_update(m, ref);
700 rte_ring_enqueue(refcnt_mbuf_ring, m);
703 rte_pktmbuf_refcnt_update(m, 1);
704 rte_ring_enqueue(refcnt_mbuf_ring, m);
711 rte_panic("(lcore=%u, iter=%u): was able to allocate only "
712 "%u from %u mbufs\n", lcore, iter, i, n);
714 /* wait till slave lcores will consume all mbufs */
715 while (!rte_ring_empty(refcnt_mbuf_ring))
718 /* check that all mbufs are back into mempool by now */
719 for (wn = 0; wn != REFCNT_MAX_TIMEOUT; wn++) {
720 if ((i = rte_mempool_avail_count(refcnt_pool)) == n) {
721 refcnt_lcore[lcore] += tref;
722 printf("%s(lcore=%u, iter=%u) completed, "
723 "%u references processed\n",
724 __func__, lcore, iter, tref);
730 rte_panic("(lcore=%u, iter=%u): after %us only "
731 "%u of %u mbufs left free\n", lcore, iter, wn, i, n);
735 test_refcnt_master(struct rte_mempool *refcnt_pool,
736 struct rte_ring *refcnt_mbuf_ring)
740 lcore = rte_lcore_id();
741 printf("%s started at lcore %u\n", __func__, lcore);
743 for (i = 0; i != REFCNT_MAX_ITER; i++)
744 test_refcnt_iter(lcore, i, refcnt_pool, refcnt_mbuf_ring);
746 refcnt_stop_slaves = 1;
749 printf("%s finished at lcore %u\n", __func__, lcore);
756 test_refcnt_mbuf(void)
758 #ifdef RTE_MBUF_REFCNT_ATOMIC
759 unsigned int master, slave, tref;
761 struct rte_mempool *refcnt_pool = NULL;
762 struct rte_ring *refcnt_mbuf_ring = NULL;
764 if (rte_lcore_count() < 2) {
765 printf("Not enough cores for test_refcnt_mbuf, expecting at least 2\n");
769 printf("starting %s, at %u lcores\n", __func__, rte_lcore_count());
771 /* create refcnt pool & ring if they don't exist */
773 refcnt_pool = rte_pktmbuf_pool_create(MAKE_STRING(refcnt_pool),
774 REFCNT_MBUF_NUM, 0, 0, 0,
776 if (refcnt_pool == NULL) {
777 printf("%s: cannot allocate " MAKE_STRING(refcnt_pool) "\n",
782 refcnt_mbuf_ring = rte_ring_create("refcnt_mbuf_ring",
783 rte_align32pow2(REFCNT_RING_SIZE), SOCKET_ID_ANY,
785 if (refcnt_mbuf_ring == NULL) {
786 printf("%s: cannot allocate " MAKE_STRING(refcnt_mbuf_ring)
791 refcnt_stop_slaves = 0;
792 memset(refcnt_lcore, 0, sizeof (refcnt_lcore));
794 rte_eal_mp_remote_launch(test_refcnt_slave, refcnt_mbuf_ring,
797 test_refcnt_master(refcnt_pool, refcnt_mbuf_ring);
799 rte_eal_mp_wait_lcore();
801 /* check that we porcessed all references */
803 master = rte_get_master_lcore();
805 RTE_LCORE_FOREACH_SLAVE(slave)
806 tref += refcnt_lcore[slave];
808 if (tref != refcnt_lcore[master])
809 rte_panic("refernced mbufs: %u, freed mbufs: %u\n",
810 tref, refcnt_lcore[master]);
812 rte_mempool_dump(stdout, refcnt_pool);
813 rte_ring_dump(stdout, refcnt_mbuf_ring);
818 rte_mempool_free(refcnt_pool);
819 rte_ring_free(refcnt_mbuf_ring);
827 #include <sys/wait.h>
829 /* use fork() to test mbuf errors panic */
831 verify_mbuf_check_panics(struct rte_mbuf *buf)
839 rte_mbuf_sanity_check(buf, 1); /* should panic */
840 exit(0); /* return normally if it doesn't panic */
842 printf("Fork Failed\n");
853 test_failing_mbuf_sanity_check(struct rte_mempool *pktmbuf_pool)
855 struct rte_mbuf *buf;
856 struct rte_mbuf badbuf;
858 printf("Checking rte_mbuf_sanity_check for failure conditions\n");
860 /* get a good mbuf to use to make copies */
861 buf = rte_pktmbuf_alloc(pktmbuf_pool);
864 printf("Checking good mbuf initially\n");
865 if (verify_mbuf_check_panics(buf) != -1)
868 printf("Now checking for error conditions\n");
870 if (verify_mbuf_check_panics(NULL)) {
871 printf("Error with NULL mbuf test\n");
877 if (verify_mbuf_check_panics(&badbuf)) {
878 printf("Error with bad-pool mbuf test\n");
884 if (verify_mbuf_check_panics(&badbuf)) {
885 printf("Error with bad-physaddr mbuf test\n");
890 badbuf.buf_addr = NULL;
891 if (verify_mbuf_check_panics(&badbuf)) {
892 printf("Error with bad-addr mbuf test\n");
898 if (verify_mbuf_check_panics(&badbuf)) {
899 printf("Error with bad-refcnt(0) mbuf test\n");
904 badbuf.refcnt = UINT16_MAX;
905 if (verify_mbuf_check_panics(&badbuf)) {
906 printf("Error with bad-refcnt(MAX) mbuf test\n");
914 test_mbuf_linearize(struct rte_mempool *pktmbuf_pool, int pkt_len,
918 struct rte_mbuf *m = NULL, *mbuf = NULL;
926 printf("Packet size must be 1 or more (is %d)\n", pkt_len);
931 printf("Number of segments must be 1 or more (is %d)\n",
936 seg_len = pkt_len / nb_segs;
942 /* Create chained mbuf_src and fill it generated data */
943 for (seg = 0; remain > 0; seg++) {
945 m = rte_pktmbuf_alloc(pktmbuf_pool);
947 printf("Cannot create segment for source mbuf");
951 /* Make sure if tailroom is zeroed */
952 memset(rte_pktmbuf_mtod(m, uint8_t *), 0,
953 rte_pktmbuf_tailroom(m));
956 if (data_len > seg_len)
959 data = (uint8_t *)rte_pktmbuf_append(m, data_len);
961 printf("Cannot append %d bytes to the mbuf\n",
966 for (i = 0; i < data_len; i++)
967 data[i] = (seg * seg_len + i) % 0x0ff;
972 rte_pktmbuf_chain(mbuf, m);
977 /* Create destination buffer to store coalesced data */
978 if (rte_pktmbuf_linearize(mbuf)) {
979 printf("Mbuf linearization failed\n");
983 if (!rte_pktmbuf_is_contiguous(mbuf)) {
984 printf("Source buffer should be contiguous after "
989 data = rte_pktmbuf_mtod(mbuf, uint8_t *);
991 for (i = 0; i < pkt_len; i++)
992 if (data[i] != (i % 0x0ff)) {
993 printf("Incorrect data in linearized mbuf\n");
997 rte_pktmbuf_free(mbuf);
1002 rte_pktmbuf_free(mbuf);
1007 test_mbuf_linearize_check(struct rte_mempool *pktmbuf_pool)
1009 struct test_mbuf_array {
1021 printf("Test mbuf linearize API\n");
1023 for (i = 0; i < RTE_DIM(mbuf_array); i++)
1024 if (test_mbuf_linearize(pktmbuf_pool, mbuf_array[i].size,
1025 mbuf_array[i].nb_segs)) {
1026 printf("Test failed for %d, %d\n", mbuf_array[i].size,
1027 mbuf_array[i].nb_segs);
1035 * Helper function for test_tx_ofload
1038 set_tx_offload(struct rte_mbuf *mb, uint64_t il2, uint64_t il3, uint64_t il4,
1039 uint64_t tso, uint64_t ol3, uint64_t ol2)
1044 mb->tso_segsz = tso;
1045 mb->outer_l3_len = ol3;
1046 mb->outer_l2_len = ol2;
1050 test_tx_offload(void)
1052 struct rte_mbuf *mb;
1053 uint64_t tm, v1, v2;
1057 static volatile struct {
1064 const uint32_t num = 0x10000;
1066 txof.l2 = rte_rand() % (1 << RTE_MBUF_L2_LEN_BITS);
1067 txof.l3 = rte_rand() % (1 << RTE_MBUF_L3_LEN_BITS);
1068 txof.l4 = rte_rand() % (1 << RTE_MBUF_L4_LEN_BITS);
1069 txof.tso = rte_rand() % (1 << RTE_MBUF_TSO_SEGSZ_BITS);
1071 printf("%s started, tx_offload = {\n"
1075 "\ttso_segsz=%#hx,\n"
1076 "\touter_l3_len=%#x,\n"
1077 "\touter_l2_len=%#x,\n"
1080 txof.l2, txof.l3, txof.l4, txof.tso, txof.l3, txof.l2);
1082 sz = sizeof(*mb) * num;
1083 mb = rte_zmalloc(NULL, sz, RTE_CACHE_LINE_SIZE);
1085 printf("%s failed, out of memory\n", __func__);
1090 tm = rte_rdtsc_precise();
1092 for (i = 0; i != num; i++)
1093 set_tx_offload(mb + i, txof.l2, txof.l3, txof.l4,
1094 txof.tso, txof.l3, txof.l2);
1096 tm = rte_rdtsc_precise() - tm;
1097 printf("%s set tx_offload by bit-fields: %u iterations, %"
1098 PRIu64 " cycles, %#Lf cycles/iter\n",
1099 __func__, num, tm, (long double)tm / num);
1101 v1 = mb[rte_rand() % num].tx_offload;
1104 tm = rte_rdtsc_precise();
1106 for (i = 0; i != num; i++)
1107 mb[i].tx_offload = rte_mbuf_tx_offload(txof.l2, txof.l3,
1108 txof.l4, txof.tso, txof.l3, txof.l2, 0);
1110 tm = rte_rdtsc_precise() - tm;
1111 printf("%s set raw tx_offload: %u iterations, %"
1112 PRIu64 " cycles, %#Lf cycles/iter\n",
1113 __func__, num, tm, (long double)tm / num);
1115 v2 = mb[rte_rand() % num].tx_offload;
1119 printf("%s finished\n"
1120 "expected tx_offload value: 0x%" PRIx64 ";\n"
1121 "rte_mbuf_tx_offload value: 0x%" PRIx64 ";\n",
1124 return (v1 == v2) ? 0 : -EINVAL;
1131 struct rte_mempool *pktmbuf_pool = NULL;
1132 struct rte_mempool *pktmbuf_pool2 = NULL;
1135 RTE_BUILD_BUG_ON(sizeof(struct rte_mbuf) != RTE_CACHE_LINE_MIN_SIZE * 2);
1137 /* create pktmbuf pool if it does not exist */
1138 pktmbuf_pool = rte_pktmbuf_pool_create("test_pktmbuf_pool",
1139 NB_MBUF, 32, 0, MBUF_DATA_SIZE, SOCKET_ID_ANY);
1141 if (pktmbuf_pool == NULL) {
1142 printf("cannot allocate mbuf pool\n");
1146 /* create a specific pktmbuf pool with a priv_size != 0 and no data
1148 pktmbuf_pool2 = rte_pktmbuf_pool_create("test_pktmbuf_pool2",
1149 NB_MBUF, 32, MBUF2_PRIV_SIZE, 0, SOCKET_ID_ANY);
1151 if (pktmbuf_pool2 == NULL) {
1152 printf("cannot allocate mbuf pool\n");
1156 /* test multiple mbuf alloc */
1157 if (test_pktmbuf_pool(pktmbuf_pool) < 0) {
1158 printf("test_mbuf_pool() failed\n");
1162 /* do it another time to check that all mbufs were freed */
1163 if (test_pktmbuf_pool(pktmbuf_pool) < 0) {
1164 printf("test_mbuf_pool() failed (2)\n");
1168 /* test that the pointer to the data on a packet mbuf is set properly */
1169 if (test_pktmbuf_pool_ptr(pktmbuf_pool) < 0) {
1170 printf("test_pktmbuf_pool_ptr() failed\n");
1174 /* test data manipulation in mbuf */
1175 if (test_one_pktmbuf(pktmbuf_pool) < 0) {
1176 printf("test_one_mbuf() failed\n");
1182 * do it another time, to check that allocation reinitialize
1183 * the mbuf correctly
1185 if (test_one_pktmbuf(pktmbuf_pool) < 0) {
1186 printf("test_one_mbuf() failed (2)\n");
1190 if (test_pktmbuf_with_non_ascii_data(pktmbuf_pool) < 0) {
1191 printf("test_pktmbuf_with_non_ascii_data() failed\n");
1195 /* test free pktmbuf segment one by one */
1196 if (test_pktmbuf_free_segment(pktmbuf_pool) < 0) {
1197 printf("test_pktmbuf_free_segment() failed.\n");
1201 if (testclone_testupdate_testdetach(pktmbuf_pool) < 0) {
1202 printf("testclone_and_testupdate() failed \n");
1206 if (test_attach_from_different_pool(pktmbuf_pool, pktmbuf_pool2) < 0) {
1207 printf("test_attach_from_different_pool() failed\n");
1211 if (test_refcnt_mbuf() < 0) {
1212 printf("test_refcnt_mbuf() failed \n");
1216 if (test_failing_mbuf_sanity_check(pktmbuf_pool) < 0) {
1217 printf("test_failing_mbuf_sanity_check() failed\n");
1221 if (test_mbuf_linearize_check(pktmbuf_pool) < 0) {
1222 printf("test_mbuf_linearize_check() failed\n");
1226 if (test_tx_offload() < 0) {
1227 printf("test_tx_offload() failed\n");
1233 rte_mempool_free(pktmbuf_pool);
1234 rte_mempool_free(pktmbuf_pool2);
1238 REGISTER_TEST_COMMAND(mbuf_autotest, test_mbuf);