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>
33 #define MBUF_DATA_SIZE 2048
35 #define MBUF_TEST_DATA_LEN 1464
36 #define MBUF_TEST_DATA_LEN2 50
37 #define MBUF_TEST_HDR1_LEN 20
38 #define MBUF_TEST_HDR2_LEN 30
39 #define MBUF_TEST_ALL_HDRS_LEN (MBUF_TEST_HDR1_LEN+MBUF_TEST_HDR2_LEN)
41 /* size of private data for mbuf in pktmbuf_pool2 */
42 #define MBUF2_PRIV_SIZE 128
44 #define REFCNT_MAX_ITER 64
45 #define REFCNT_MAX_TIMEOUT 10
46 #define REFCNT_MAX_REF (RTE_MAX_LCORE)
47 #define REFCNT_MBUF_NUM 64
48 #define REFCNT_RING_SIZE (REFCNT_MBUF_NUM * REFCNT_MAX_REF)
50 #define MAGIC_DATA 0x42424242
52 #define MAKE_STRING(x) # x
54 #ifdef RTE_MBUF_REFCNT_ATOMIC
56 static volatile uint32_t refcnt_stop_slaves;
57 static unsigned refcnt_lcore[RTE_MAX_LCORE];
65 * #. Allocate a mbuf pool.
67 * - The pool contains NB_MBUF elements, where each mbuf is MBUF_SIZE
70 * #. Test multiple allocations of mbufs from this pool.
72 * - Allocate NB_MBUF and store pointers in a table.
73 * - If an allocation fails, return an error.
74 * - Free all these mbufs.
75 * - Repeat the same test to check that mbufs were freed correctly.
77 * #. Test data manipulation in pktmbuf.
80 * - Append data using rte_pktmbuf_append().
81 * - Test for error in rte_pktmbuf_append() when len is too large.
82 * - Trim data at the end of mbuf using rte_pktmbuf_trim().
83 * - Test for error in rte_pktmbuf_trim() when len is too large.
84 * - Prepend a header using rte_pktmbuf_prepend().
85 * - Test for error in rte_pktmbuf_prepend() when len is too large.
86 * - Remove data at the beginning of mbuf using rte_pktmbuf_adj().
87 * - Test for error in rte_pktmbuf_adj() when len is too large.
88 * - Check that appended data is not corrupt.
90 * - Between all these tests, check data_len and pkt_len, and
91 * that the mbuf is contiguous.
92 * - Repeat the test to check that allocation operations
93 * reinitialize the mbuf correctly.
95 * #. Test packet cloning
96 * - Clone a mbuf and verify the data
97 * - Clone the cloned mbuf and verify the data
98 * - Attach a mbuf to another that does not have the same priv_size.
101 #define GOTO_FAIL(str, ...) do { \
102 printf("mbuf test FAILED (l.%d): <" str ">\n", \
103 __LINE__, ##__VA_ARGS__); \
108 * test data manipulation in mbuf with non-ascii data
111 test_pktmbuf_with_non_ascii_data(struct rte_mempool *pktmbuf_pool)
113 struct rte_mbuf *m = NULL;
116 m = rte_pktmbuf_alloc(pktmbuf_pool);
118 GOTO_FAIL("Cannot allocate mbuf");
119 if (rte_pktmbuf_pkt_len(m) != 0)
120 GOTO_FAIL("Bad length");
122 data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN);
124 GOTO_FAIL("Cannot append data");
125 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
126 GOTO_FAIL("Bad pkt length");
127 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
128 GOTO_FAIL("Bad data length");
129 memset(data, 0xff, rte_pktmbuf_pkt_len(m));
130 if (!rte_pktmbuf_is_contiguous(m))
131 GOTO_FAIL("Buffer should be continuous");
132 rte_pktmbuf_dump(stdout, m, MBUF_TEST_DATA_LEN);
146 * test data manipulation in mbuf
149 test_one_pktmbuf(struct rte_mempool *pktmbuf_pool)
151 struct rte_mbuf *m = NULL;
152 char *data, *data2, *hdr;
155 printf("Test pktmbuf API\n");
159 m = rte_pktmbuf_alloc(pktmbuf_pool);
161 GOTO_FAIL("Cannot allocate mbuf");
162 if (rte_pktmbuf_pkt_len(m) != 0)
163 GOTO_FAIL("Bad length");
165 rte_pktmbuf_dump(stdout, m, 0);
169 data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN);
171 GOTO_FAIL("Cannot append data");
172 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
173 GOTO_FAIL("Bad pkt length");
174 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
175 GOTO_FAIL("Bad data length");
176 memset(data, 0x66, rte_pktmbuf_pkt_len(m));
177 if (!rte_pktmbuf_is_contiguous(m))
178 GOTO_FAIL("Buffer should be continuous");
179 rte_pktmbuf_dump(stdout, m, MBUF_TEST_DATA_LEN);
180 rte_pktmbuf_dump(stdout, m, 2*MBUF_TEST_DATA_LEN);
182 /* this append should fail */
184 data2 = rte_pktmbuf_append(m, (uint16_t)(rte_pktmbuf_tailroom(m) + 1));
186 GOTO_FAIL("Append should not succeed");
188 /* append some more data */
190 data2 = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN2);
192 GOTO_FAIL("Cannot append data");
193 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_DATA_LEN2)
194 GOTO_FAIL("Bad pkt length");
195 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_DATA_LEN2)
196 GOTO_FAIL("Bad data length");
197 if (!rte_pktmbuf_is_contiguous(m))
198 GOTO_FAIL("Buffer should be continuous");
200 /* trim data at the end of mbuf */
202 if (rte_pktmbuf_trim(m, MBUF_TEST_DATA_LEN2) < 0)
203 GOTO_FAIL("Cannot trim data");
204 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
205 GOTO_FAIL("Bad pkt length");
206 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
207 GOTO_FAIL("Bad data length");
208 if (!rte_pktmbuf_is_contiguous(m))
209 GOTO_FAIL("Buffer should be continuous");
211 /* this trim should fail */
213 if (rte_pktmbuf_trim(m, (uint16_t)(rte_pktmbuf_data_len(m) + 1)) == 0)
214 GOTO_FAIL("trim should not succeed");
216 /* prepend one header */
218 hdr = rte_pktmbuf_prepend(m, MBUF_TEST_HDR1_LEN);
220 GOTO_FAIL("Cannot prepend");
221 if (data - hdr != MBUF_TEST_HDR1_LEN)
222 GOTO_FAIL("Prepend failed");
223 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_HDR1_LEN)
224 GOTO_FAIL("Bad pkt length");
225 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_HDR1_LEN)
226 GOTO_FAIL("Bad data length");
227 if (!rte_pktmbuf_is_contiguous(m))
228 GOTO_FAIL("Buffer should be continuous");
229 memset(hdr, 0x55, MBUF_TEST_HDR1_LEN);
231 /* prepend another header */
233 hdr = rte_pktmbuf_prepend(m, MBUF_TEST_HDR2_LEN);
235 GOTO_FAIL("Cannot prepend");
236 if (data - hdr != MBUF_TEST_ALL_HDRS_LEN)
237 GOTO_FAIL("Prepend failed");
238 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_ALL_HDRS_LEN)
239 GOTO_FAIL("Bad pkt length");
240 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_ALL_HDRS_LEN)
241 GOTO_FAIL("Bad data length");
242 if (!rte_pktmbuf_is_contiguous(m))
243 GOTO_FAIL("Buffer should be continuous");
244 memset(hdr, 0x55, MBUF_TEST_HDR2_LEN);
246 rte_mbuf_sanity_check(m, 1);
247 rte_mbuf_sanity_check(m, 0);
248 rte_pktmbuf_dump(stdout, m, 0);
250 /* this prepend should fail */
252 hdr = rte_pktmbuf_prepend(m, (uint16_t)(rte_pktmbuf_headroom(m) + 1));
254 GOTO_FAIL("prepend should not succeed");
256 /* remove data at beginning of mbuf (adj) */
258 if (data != rte_pktmbuf_adj(m, MBUF_TEST_ALL_HDRS_LEN))
259 GOTO_FAIL("rte_pktmbuf_adj failed");
260 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
261 GOTO_FAIL("Bad pkt length");
262 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
263 GOTO_FAIL("Bad data length");
264 if (!rte_pktmbuf_is_contiguous(m))
265 GOTO_FAIL("Buffer should be continuous");
267 /* this adj should fail */
269 if (rte_pktmbuf_adj(m, (uint16_t)(rte_pktmbuf_data_len(m) + 1)) != NULL)
270 GOTO_FAIL("rte_pktmbuf_adj should not succeed");
274 if (!rte_pktmbuf_is_contiguous(m))
275 GOTO_FAIL("Buffer should be continuous");
277 for (i=0; i<MBUF_TEST_DATA_LEN; i++) {
279 GOTO_FAIL("Data corrupted at offset %u", i);
295 testclone_testupdate_testdetach(struct rte_mempool *pktmbuf_pool)
297 struct rte_mbuf *m = NULL;
298 struct rte_mbuf *clone = NULL;
299 struct rte_mbuf *clone2 = NULL;
300 unaligned_uint32_t *data;
303 m = rte_pktmbuf_alloc(pktmbuf_pool);
305 GOTO_FAIL("ooops not allocating mbuf");
307 if (rte_pktmbuf_pkt_len(m) != 0)
308 GOTO_FAIL("Bad length");
310 rte_pktmbuf_append(m, sizeof(uint32_t));
311 data = rte_pktmbuf_mtod(m, unaligned_uint32_t *);
314 /* clone the allocated mbuf */
315 clone = rte_pktmbuf_clone(m, pktmbuf_pool);
317 GOTO_FAIL("cannot clone data\n");
319 data = rte_pktmbuf_mtod(clone, unaligned_uint32_t *);
320 if (*data != MAGIC_DATA)
321 GOTO_FAIL("invalid data in clone\n");
323 if (rte_mbuf_refcnt_read(m) != 2)
324 GOTO_FAIL("invalid refcnt in m\n");
327 rte_pktmbuf_free(clone);
330 /* same test with a chained mbuf */
331 m->next = rte_pktmbuf_alloc(pktmbuf_pool);
333 GOTO_FAIL("Next Pkt Null\n");
335 rte_pktmbuf_append(m->next, sizeof(uint32_t));
336 data = rte_pktmbuf_mtod(m->next, unaligned_uint32_t *);
339 clone = rte_pktmbuf_clone(m, pktmbuf_pool);
341 GOTO_FAIL("cannot clone data\n");
343 data = rte_pktmbuf_mtod(clone, unaligned_uint32_t *);
344 if (*data != MAGIC_DATA)
345 GOTO_FAIL("invalid data in clone\n");
347 data = rte_pktmbuf_mtod(clone->next, unaligned_uint32_t *);
348 if (*data != MAGIC_DATA)
349 GOTO_FAIL("invalid data in clone->next\n");
351 if (rte_mbuf_refcnt_read(m) != 2)
352 GOTO_FAIL("invalid refcnt in m\n");
354 if (rte_mbuf_refcnt_read(m->next) != 2)
355 GOTO_FAIL("invalid refcnt in m->next\n");
357 /* try to clone the clone */
359 clone2 = rte_pktmbuf_clone(clone, pktmbuf_pool);
361 GOTO_FAIL("cannot clone the clone\n");
363 data = rte_pktmbuf_mtod(clone2, unaligned_uint32_t *);
364 if (*data != MAGIC_DATA)
365 GOTO_FAIL("invalid data in clone2\n");
367 data = rte_pktmbuf_mtod(clone2->next, unaligned_uint32_t *);
368 if (*data != MAGIC_DATA)
369 GOTO_FAIL("invalid data in clone2->next\n");
371 if (rte_mbuf_refcnt_read(m) != 3)
372 GOTO_FAIL("invalid refcnt in m\n");
374 if (rte_mbuf_refcnt_read(m->next) != 3)
375 GOTO_FAIL("invalid refcnt in m->next\n");
379 rte_pktmbuf_free(clone);
380 rte_pktmbuf_free(clone2);
385 printf("%s ok\n", __func__);
392 rte_pktmbuf_free(clone);
394 rte_pktmbuf_free(clone2);
399 test_attach_from_different_pool(struct rte_mempool *pktmbuf_pool,
400 struct rte_mempool *pktmbuf_pool2)
402 struct rte_mbuf *m = NULL;
403 struct rte_mbuf *clone = NULL;
404 struct rte_mbuf *clone2 = NULL;
405 char *data, *c_data, *c_data2;
408 m = rte_pktmbuf_alloc(pktmbuf_pool);
410 GOTO_FAIL("cannot allocate mbuf");
412 if (rte_pktmbuf_pkt_len(m) != 0)
413 GOTO_FAIL("Bad length");
415 data = rte_pktmbuf_mtod(m, char *);
417 /* allocate a new mbuf from the second pool, and attach it to the first
419 clone = rte_pktmbuf_alloc(pktmbuf_pool2);
421 GOTO_FAIL("cannot allocate mbuf from second pool\n");
423 /* check data room size and priv size, and erase priv */
424 if (rte_pktmbuf_data_room_size(clone->pool) != 0)
425 GOTO_FAIL("data room size should be 0\n");
426 if (rte_pktmbuf_priv_size(clone->pool) != MBUF2_PRIV_SIZE)
427 GOTO_FAIL("data room size should be %d\n", MBUF2_PRIV_SIZE);
428 memset(clone + 1, 0, MBUF2_PRIV_SIZE);
430 /* save data pointer to compare it after detach() */
431 c_data = rte_pktmbuf_mtod(clone, char *);
432 if (c_data != (char *)clone + sizeof(*clone) + MBUF2_PRIV_SIZE)
433 GOTO_FAIL("bad data pointer in clone");
434 if (rte_pktmbuf_headroom(clone) != 0)
435 GOTO_FAIL("bad headroom in clone");
437 rte_pktmbuf_attach(clone, m);
439 if (rte_pktmbuf_mtod(clone, char *) != data)
440 GOTO_FAIL("clone was not attached properly\n");
441 if (rte_pktmbuf_headroom(clone) != RTE_PKTMBUF_HEADROOM)
442 GOTO_FAIL("bad headroom in clone after attach");
443 if (rte_mbuf_refcnt_read(m) != 2)
444 GOTO_FAIL("invalid refcnt in m\n");
446 /* allocate a new mbuf from the second pool, and attach it to the first
448 clone2 = rte_pktmbuf_alloc(pktmbuf_pool2);
450 GOTO_FAIL("cannot allocate clone2 from second pool\n");
452 /* check data room size and priv size, and erase priv */
453 if (rte_pktmbuf_data_room_size(clone2->pool) != 0)
454 GOTO_FAIL("data room size should be 0\n");
455 if (rte_pktmbuf_priv_size(clone2->pool) != MBUF2_PRIV_SIZE)
456 GOTO_FAIL("data room size should be %d\n", MBUF2_PRIV_SIZE);
457 memset(clone2 + 1, 0, MBUF2_PRIV_SIZE);
459 /* save data pointer to compare it after detach() */
460 c_data2 = rte_pktmbuf_mtod(clone2, char *);
461 if (c_data2 != (char *)clone2 + sizeof(*clone2) + MBUF2_PRIV_SIZE)
462 GOTO_FAIL("bad data pointer in clone2");
463 if (rte_pktmbuf_headroom(clone2) != 0)
464 GOTO_FAIL("bad headroom in clone2");
466 rte_pktmbuf_attach(clone2, clone);
468 if (rte_pktmbuf_mtod(clone2, char *) != data)
469 GOTO_FAIL("clone2 was not attached properly\n");
470 if (rte_pktmbuf_headroom(clone2) != RTE_PKTMBUF_HEADROOM)
471 GOTO_FAIL("bad headroom in clone2 after attach");
472 if (rte_mbuf_refcnt_read(m) != 3)
473 GOTO_FAIL("invalid refcnt in m\n");
475 /* detach the clones */
476 rte_pktmbuf_detach(clone);
477 if (c_data != rte_pktmbuf_mtod(clone, char *))
478 GOTO_FAIL("clone was not detached properly\n");
479 if (rte_mbuf_refcnt_read(m) != 2)
480 GOTO_FAIL("invalid refcnt in m\n");
482 rte_pktmbuf_detach(clone2);
483 if (c_data2 != rte_pktmbuf_mtod(clone2, char *))
484 GOTO_FAIL("clone2 was not detached properly\n");
485 if (rte_mbuf_refcnt_read(m) != 1)
486 GOTO_FAIL("invalid refcnt in m\n");
488 /* free the clones and the initial mbuf */
489 rte_pktmbuf_free(clone2);
490 rte_pktmbuf_free(clone);
492 printf("%s ok\n", __func__);
499 rte_pktmbuf_free(clone);
501 rte_pktmbuf_free(clone2);
507 * test allocation and free of mbufs
510 test_pktmbuf_pool(struct rte_mempool *pktmbuf_pool)
513 struct rte_mbuf *m[NB_MBUF];
516 for (i=0; i<NB_MBUF; i++)
519 /* alloc NB_MBUF mbufs */
520 for (i=0; i<NB_MBUF; i++) {
521 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
523 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
527 struct rte_mbuf *extra = NULL;
528 extra = rte_pktmbuf_alloc(pktmbuf_pool);
530 printf("Error pool not empty");
533 extra = rte_pktmbuf_clone(m[0], pktmbuf_pool);
535 printf("Error pool not empty");
539 for (i=0; i<NB_MBUF; i++) {
541 rte_pktmbuf_free(m[i]);
548 * test that the pointer to the data on a packet mbuf is set properly
551 test_pktmbuf_pool_ptr(struct rte_mempool *pktmbuf_pool)
554 struct rte_mbuf *m[NB_MBUF];
557 for (i=0; i<NB_MBUF; i++)
560 /* alloc NB_MBUF mbufs */
561 for (i=0; i<NB_MBUF; i++) {
562 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
564 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
568 m[i]->data_off += 64;
572 for (i=0; i<NB_MBUF; i++) {
574 rte_pktmbuf_free(m[i]);
577 for (i=0; i<NB_MBUF; i++)
580 /* alloc NB_MBUF mbufs */
581 for (i=0; i<NB_MBUF; i++) {
582 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
584 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
588 if (m[i]->data_off != RTE_PKTMBUF_HEADROOM) {
589 printf("invalid data_off\n");
595 for (i=0; i<NB_MBUF; i++) {
597 rte_pktmbuf_free(m[i]);
604 test_pktmbuf_free_segment(struct rte_mempool *pktmbuf_pool)
607 struct rte_mbuf *m[NB_MBUF];
610 for (i=0; i<NB_MBUF; i++)
613 /* alloc NB_MBUF mbufs */
614 for (i=0; i<NB_MBUF; i++) {
615 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
617 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
623 for (i=0; i<NB_MBUF; i++) {
625 struct rte_mbuf *mb, *mt;
631 rte_pktmbuf_free_seg(mt);
640 * Stress test for rte_mbuf atomic refcnt.
641 * Implies that RTE_MBUF_REFCNT_ATOMIC is defined.
642 * For more efficiency, recommended to run with RTE_LIBRTE_MBUF_DEBUG defined.
645 #ifdef RTE_MBUF_REFCNT_ATOMIC
648 test_refcnt_slave(void *arg)
650 unsigned lcore, free;
652 struct rte_ring *refcnt_mbuf_ring = arg;
654 lcore = rte_lcore_id();
655 printf("%s started at lcore %u\n", __func__, lcore);
658 while (refcnt_stop_slaves == 0) {
659 if (rte_ring_dequeue(refcnt_mbuf_ring, &mp) == 0) {
661 rte_pktmbuf_free(mp);
665 refcnt_lcore[lcore] += free;
666 printf("%s finished at lcore %u, "
667 "number of freed mbufs: %u\n",
668 __func__, lcore, free);
673 test_refcnt_iter(unsigned int lcore, unsigned int iter,
674 struct rte_mempool *refcnt_pool,
675 struct rte_ring *refcnt_mbuf_ring)
678 unsigned i, n, tref, wn;
683 /* For each mbuf in the pool:
685 * - increment it's reference up to N+1,
686 * - enqueue it N times into the ring for slave cores to free.
688 for (i = 0, n = rte_mempool_avail_count(refcnt_pool);
689 i != n && (m = rte_pktmbuf_alloc(refcnt_pool)) != NULL;
691 ref = RTE_MAX(rte_rand() % REFCNT_MAX_REF, 1UL);
693 if ((ref & 1) != 0) {
694 rte_pktmbuf_refcnt_update(m, ref);
696 rte_ring_enqueue(refcnt_mbuf_ring, m);
699 rte_pktmbuf_refcnt_update(m, 1);
700 rte_ring_enqueue(refcnt_mbuf_ring, m);
707 rte_panic("(lcore=%u, iter=%u): was able to allocate only "
708 "%u from %u mbufs\n", lcore, iter, i, n);
710 /* wait till slave lcores will consume all mbufs */
711 while (!rte_ring_empty(refcnt_mbuf_ring))
714 /* check that all mbufs are back into mempool by now */
715 for (wn = 0; wn != REFCNT_MAX_TIMEOUT; wn++) {
716 if ((i = rte_mempool_avail_count(refcnt_pool)) == n) {
717 refcnt_lcore[lcore] += tref;
718 printf("%s(lcore=%u, iter=%u) completed, "
719 "%u references processed\n",
720 __func__, lcore, iter, tref);
726 rte_panic("(lcore=%u, iter=%u): after %us only "
727 "%u of %u mbufs left free\n", lcore, iter, wn, i, n);
731 test_refcnt_master(struct rte_mempool *refcnt_pool,
732 struct rte_ring *refcnt_mbuf_ring)
736 lcore = rte_lcore_id();
737 printf("%s started at lcore %u\n", __func__, lcore);
739 for (i = 0; i != REFCNT_MAX_ITER; i++)
740 test_refcnt_iter(lcore, i, refcnt_pool, refcnt_mbuf_ring);
742 refcnt_stop_slaves = 1;
745 printf("%s finished at lcore %u\n", __func__, lcore);
752 test_refcnt_mbuf(void)
754 #ifdef RTE_MBUF_REFCNT_ATOMIC
755 unsigned lnum, master, slave, tref;
757 struct rte_mempool *refcnt_pool = NULL;
758 struct rte_ring *refcnt_mbuf_ring = NULL;
760 if ((lnum = rte_lcore_count()) == 1) {
761 printf("skipping %s, number of lcores: %u is not enough\n",
766 printf("starting %s, at %u lcores\n", __func__, lnum);
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);
1035 struct rte_mempool *pktmbuf_pool = NULL;
1036 struct rte_mempool *pktmbuf_pool2 = NULL;
1039 RTE_BUILD_BUG_ON(sizeof(struct rte_mbuf) != RTE_CACHE_LINE_MIN_SIZE * 2);
1041 /* create pktmbuf pool if it does not exist */
1042 pktmbuf_pool = rte_pktmbuf_pool_create("test_pktmbuf_pool",
1043 NB_MBUF, 32, 0, MBUF_DATA_SIZE, SOCKET_ID_ANY);
1045 if (pktmbuf_pool == NULL) {
1046 printf("cannot allocate mbuf pool\n");
1050 /* create a specific pktmbuf pool with a priv_size != 0 and no data
1052 pktmbuf_pool2 = rte_pktmbuf_pool_create("test_pktmbuf_pool2",
1053 NB_MBUF, 32, MBUF2_PRIV_SIZE, 0, SOCKET_ID_ANY);
1055 if (pktmbuf_pool2 == NULL) {
1056 printf("cannot allocate mbuf pool\n");
1060 /* test multiple mbuf alloc */
1061 if (test_pktmbuf_pool(pktmbuf_pool) < 0) {
1062 printf("test_mbuf_pool() failed\n");
1066 /* do it another time to check that all mbufs were freed */
1067 if (test_pktmbuf_pool(pktmbuf_pool) < 0) {
1068 printf("test_mbuf_pool() failed (2)\n");
1072 /* test that the pointer to the data on a packet mbuf is set properly */
1073 if (test_pktmbuf_pool_ptr(pktmbuf_pool) < 0) {
1074 printf("test_pktmbuf_pool_ptr() failed\n");
1078 /* test data manipulation in mbuf */
1079 if (test_one_pktmbuf(pktmbuf_pool) < 0) {
1080 printf("test_one_mbuf() failed\n");
1086 * do it another time, to check that allocation reinitialize
1087 * the mbuf correctly
1089 if (test_one_pktmbuf(pktmbuf_pool) < 0) {
1090 printf("test_one_mbuf() failed (2)\n");
1094 if (test_pktmbuf_with_non_ascii_data(pktmbuf_pool) < 0) {
1095 printf("test_pktmbuf_with_non_ascii_data() failed\n");
1099 /* test free pktmbuf segment one by one */
1100 if (test_pktmbuf_free_segment(pktmbuf_pool) < 0) {
1101 printf("test_pktmbuf_free_segment() failed.\n");
1105 if (testclone_testupdate_testdetach(pktmbuf_pool) < 0) {
1106 printf("testclone_and_testupdate() failed \n");
1110 if (test_attach_from_different_pool(pktmbuf_pool, pktmbuf_pool2) < 0) {
1111 printf("test_attach_from_different_pool() failed\n");
1115 if (test_refcnt_mbuf()<0){
1116 printf("test_refcnt_mbuf() failed \n");
1120 if (test_failing_mbuf_sanity_check(pktmbuf_pool) < 0) {
1121 printf("test_failing_mbuf_sanity_check() failed\n");
1125 if (test_mbuf_linearize_check(pktmbuf_pool) < 0) {
1126 printf("test_mbuf_linearize_check() failed\n");
1132 rte_mempool_free(pktmbuf_pool);
1133 rte_mempool_free(pktmbuf_pool2);
1137 REGISTER_TEST_COMMAND(mbuf_autotest, test_mbuf);