4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #include <sys/queue.h>
43 #include <rte_common.h>
44 #include <rte_debug.h>
46 #include <rte_memory.h>
47 #include <rte_memcpy.h>
48 #include <rte_launch.h>
50 #include <rte_per_lcore.h>
51 #include <rte_lcore.h>
52 #include <rte_atomic.h>
53 #include <rte_branch_prediction.h>
55 #include <rte_mempool.h>
57 #include <rte_random.h>
58 #include <rte_cycles.h>
62 #define MBUF_DATA_SIZE 2048
64 #define MBUF_TEST_DATA_LEN 1464
65 #define MBUF_TEST_DATA_LEN2 50
66 #define MBUF_TEST_HDR1_LEN 20
67 #define MBUF_TEST_HDR2_LEN 30
68 #define MBUF_TEST_ALL_HDRS_LEN (MBUF_TEST_HDR1_LEN+MBUF_TEST_HDR2_LEN)
70 /* size of private data for mbuf in pktmbuf_pool2 */
71 #define MBUF2_PRIV_SIZE 128
73 #define REFCNT_MAX_ITER 64
74 #define REFCNT_MAX_TIMEOUT 10
75 #define REFCNT_MAX_REF (RTE_MAX_LCORE)
76 #define REFCNT_MBUF_NUM 64
77 #define REFCNT_RING_SIZE (REFCNT_MBUF_NUM * REFCNT_MAX_REF)
79 #define MAGIC_DATA 0x42424242
81 #define MAKE_STRING(x) # x
83 #ifdef RTE_MBUF_REFCNT_ATOMIC
85 static volatile uint32_t refcnt_stop_slaves;
86 static unsigned refcnt_lcore[RTE_MAX_LCORE];
94 * #. Allocate a mbuf pool.
96 * - The pool contains NB_MBUF elements, where each mbuf is MBUF_SIZE
99 * #. Test multiple allocations of mbufs from this pool.
101 * - Allocate NB_MBUF and store pointers in a table.
102 * - If an allocation fails, return an error.
103 * - Free all these mbufs.
104 * - Repeat the same test to check that mbufs were freed correctly.
106 * #. Test data manipulation in pktmbuf.
109 * - Append data using rte_pktmbuf_append().
110 * - Test for error in rte_pktmbuf_append() when len is too large.
111 * - Trim data at the end of mbuf using rte_pktmbuf_trim().
112 * - Test for error in rte_pktmbuf_trim() when len is too large.
113 * - Prepend a header using rte_pktmbuf_prepend().
114 * - Test for error in rte_pktmbuf_prepend() when len is too large.
115 * - Remove data at the beginning of mbuf using rte_pktmbuf_adj().
116 * - Test for error in rte_pktmbuf_adj() when len is too large.
117 * - Check that appended data is not corrupt.
119 * - Between all these tests, check data_len and pkt_len, and
120 * that the mbuf is contiguous.
121 * - Repeat the test to check that allocation operations
122 * reinitialize the mbuf correctly.
124 * #. Test packet cloning
125 * - Clone a mbuf and verify the data
126 * - Clone the cloned mbuf and verify the data
127 * - Attach a mbuf to another that does not have the same priv_size.
130 #define GOTO_FAIL(str, ...) do { \
131 printf("mbuf test FAILED (l.%d): <" str ">\n", \
132 __LINE__, ##__VA_ARGS__); \
137 * test data manipulation in mbuf with non-ascii data
140 test_pktmbuf_with_non_ascii_data(struct rte_mempool *pktmbuf_pool)
142 struct rte_mbuf *m = NULL;
145 m = rte_pktmbuf_alloc(pktmbuf_pool);
147 GOTO_FAIL("Cannot allocate mbuf");
148 if (rte_pktmbuf_pkt_len(m) != 0)
149 GOTO_FAIL("Bad length");
151 data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN);
153 GOTO_FAIL("Cannot append data");
154 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
155 GOTO_FAIL("Bad pkt length");
156 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
157 GOTO_FAIL("Bad data length");
158 memset(data, 0xff, rte_pktmbuf_pkt_len(m));
159 if (!rte_pktmbuf_is_contiguous(m))
160 GOTO_FAIL("Buffer should be continuous");
161 rte_pktmbuf_dump(stdout, m, MBUF_TEST_DATA_LEN);
175 * test data manipulation in mbuf
178 test_one_pktmbuf(struct rte_mempool *pktmbuf_pool)
180 struct rte_mbuf *m = NULL;
181 char *data, *data2, *hdr;
184 printf("Test pktmbuf API\n");
188 m = rte_pktmbuf_alloc(pktmbuf_pool);
190 GOTO_FAIL("Cannot allocate mbuf");
191 if (rte_pktmbuf_pkt_len(m) != 0)
192 GOTO_FAIL("Bad length");
194 rte_pktmbuf_dump(stdout, m, 0);
198 data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN);
200 GOTO_FAIL("Cannot append data");
201 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
202 GOTO_FAIL("Bad pkt length");
203 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
204 GOTO_FAIL("Bad data length");
205 memset(data, 0x66, rte_pktmbuf_pkt_len(m));
206 if (!rte_pktmbuf_is_contiguous(m))
207 GOTO_FAIL("Buffer should be continuous");
208 rte_pktmbuf_dump(stdout, m, MBUF_TEST_DATA_LEN);
209 rte_pktmbuf_dump(stdout, m, 2*MBUF_TEST_DATA_LEN);
211 /* this append should fail */
213 data2 = rte_pktmbuf_append(m, (uint16_t)(rte_pktmbuf_tailroom(m) + 1));
215 GOTO_FAIL("Append should not succeed");
217 /* append some more data */
219 data2 = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN2);
221 GOTO_FAIL("Cannot append data");
222 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_DATA_LEN2)
223 GOTO_FAIL("Bad pkt length");
224 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_DATA_LEN2)
225 GOTO_FAIL("Bad data length");
226 if (!rte_pktmbuf_is_contiguous(m))
227 GOTO_FAIL("Buffer should be continuous");
229 /* trim data at the end of mbuf */
231 if (rte_pktmbuf_trim(m, MBUF_TEST_DATA_LEN2) < 0)
232 GOTO_FAIL("Cannot trim data");
233 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
234 GOTO_FAIL("Bad pkt length");
235 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
236 GOTO_FAIL("Bad data length");
237 if (!rte_pktmbuf_is_contiguous(m))
238 GOTO_FAIL("Buffer should be continuous");
240 /* this trim should fail */
242 if (rte_pktmbuf_trim(m, (uint16_t)(rte_pktmbuf_data_len(m) + 1)) == 0)
243 GOTO_FAIL("trim should not succeed");
245 /* prepend one header */
247 hdr = rte_pktmbuf_prepend(m, MBUF_TEST_HDR1_LEN);
249 GOTO_FAIL("Cannot prepend");
250 if (data - hdr != MBUF_TEST_HDR1_LEN)
251 GOTO_FAIL("Prepend failed");
252 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_HDR1_LEN)
253 GOTO_FAIL("Bad pkt length");
254 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_HDR1_LEN)
255 GOTO_FAIL("Bad data length");
256 if (!rte_pktmbuf_is_contiguous(m))
257 GOTO_FAIL("Buffer should be continuous");
258 memset(hdr, 0x55, MBUF_TEST_HDR1_LEN);
260 /* prepend another header */
262 hdr = rte_pktmbuf_prepend(m, MBUF_TEST_HDR2_LEN);
264 GOTO_FAIL("Cannot prepend");
265 if (data - hdr != MBUF_TEST_ALL_HDRS_LEN)
266 GOTO_FAIL("Prepend failed");
267 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_ALL_HDRS_LEN)
268 GOTO_FAIL("Bad pkt length");
269 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_ALL_HDRS_LEN)
270 GOTO_FAIL("Bad data length");
271 if (!rte_pktmbuf_is_contiguous(m))
272 GOTO_FAIL("Buffer should be continuous");
273 memset(hdr, 0x55, MBUF_TEST_HDR2_LEN);
275 rte_mbuf_sanity_check(m, 1);
276 rte_mbuf_sanity_check(m, 0);
277 rte_pktmbuf_dump(stdout, m, 0);
279 /* this prepend should fail */
281 hdr = rte_pktmbuf_prepend(m, (uint16_t)(rte_pktmbuf_headroom(m) + 1));
283 GOTO_FAIL("prepend should not succeed");
285 /* remove data at beginning of mbuf (adj) */
287 if (data != rte_pktmbuf_adj(m, MBUF_TEST_ALL_HDRS_LEN))
288 GOTO_FAIL("rte_pktmbuf_adj failed");
289 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
290 GOTO_FAIL("Bad pkt length");
291 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
292 GOTO_FAIL("Bad data length");
293 if (!rte_pktmbuf_is_contiguous(m))
294 GOTO_FAIL("Buffer should be continuous");
296 /* this adj should fail */
298 if (rte_pktmbuf_adj(m, (uint16_t)(rte_pktmbuf_data_len(m) + 1)) != NULL)
299 GOTO_FAIL("rte_pktmbuf_adj should not succeed");
303 if (!rte_pktmbuf_is_contiguous(m))
304 GOTO_FAIL("Buffer should be continuous");
306 for (i=0; i<MBUF_TEST_DATA_LEN; i++) {
308 GOTO_FAIL("Data corrupted at offset %u", i);
324 testclone_testupdate_testdetach(struct rte_mempool *pktmbuf_pool)
326 struct rte_mbuf *m = NULL;
327 struct rte_mbuf *clone = NULL;
328 struct rte_mbuf *clone2 = NULL;
329 unaligned_uint32_t *data;
332 m = rte_pktmbuf_alloc(pktmbuf_pool);
334 GOTO_FAIL("ooops not allocating mbuf");
336 if (rte_pktmbuf_pkt_len(m) != 0)
337 GOTO_FAIL("Bad length");
339 rte_pktmbuf_append(m, sizeof(uint32_t));
340 data = rte_pktmbuf_mtod(m, unaligned_uint32_t *);
343 /* clone the allocated mbuf */
344 clone = rte_pktmbuf_clone(m, pktmbuf_pool);
346 GOTO_FAIL("cannot clone data\n");
348 data = rte_pktmbuf_mtod(clone, unaligned_uint32_t *);
349 if (*data != MAGIC_DATA)
350 GOTO_FAIL("invalid data in clone\n");
352 if (rte_mbuf_refcnt_read(m) != 2)
353 GOTO_FAIL("invalid refcnt in m\n");
356 rte_pktmbuf_free(clone);
359 /* same test with a chained mbuf */
360 m->next = rte_pktmbuf_alloc(pktmbuf_pool);
362 GOTO_FAIL("Next Pkt Null\n");
364 rte_pktmbuf_append(m->next, sizeof(uint32_t));
365 data = rte_pktmbuf_mtod(m->next, unaligned_uint32_t *);
368 clone = rte_pktmbuf_clone(m, pktmbuf_pool);
370 GOTO_FAIL("cannot clone data\n");
372 data = rte_pktmbuf_mtod(clone, unaligned_uint32_t *);
373 if (*data != MAGIC_DATA)
374 GOTO_FAIL("invalid data in clone\n");
376 data = rte_pktmbuf_mtod(clone->next, unaligned_uint32_t *);
377 if (*data != MAGIC_DATA)
378 GOTO_FAIL("invalid data in clone->next\n");
380 if (rte_mbuf_refcnt_read(m) != 2)
381 GOTO_FAIL("invalid refcnt in m\n");
383 if (rte_mbuf_refcnt_read(m->next) != 2)
384 GOTO_FAIL("invalid refcnt in m->next\n");
386 /* try to clone the clone */
388 clone2 = rte_pktmbuf_clone(clone, pktmbuf_pool);
390 GOTO_FAIL("cannot clone the clone\n");
392 data = rte_pktmbuf_mtod(clone2, unaligned_uint32_t *);
393 if (*data != MAGIC_DATA)
394 GOTO_FAIL("invalid data in clone2\n");
396 data = rte_pktmbuf_mtod(clone2->next, unaligned_uint32_t *);
397 if (*data != MAGIC_DATA)
398 GOTO_FAIL("invalid data in clone2->next\n");
400 if (rte_mbuf_refcnt_read(m) != 3)
401 GOTO_FAIL("invalid refcnt in m\n");
403 if (rte_mbuf_refcnt_read(m->next) != 3)
404 GOTO_FAIL("invalid refcnt in m->next\n");
408 rte_pktmbuf_free(clone);
409 rte_pktmbuf_free(clone2);
414 printf("%s ok\n", __func__);
421 rte_pktmbuf_free(clone);
423 rte_pktmbuf_free(clone2);
428 test_attach_from_different_pool(struct rte_mempool *pktmbuf_pool,
429 struct rte_mempool *pktmbuf_pool2)
431 struct rte_mbuf *m = NULL;
432 struct rte_mbuf *clone = NULL;
433 struct rte_mbuf *clone2 = NULL;
434 char *data, *c_data, *c_data2;
437 m = rte_pktmbuf_alloc(pktmbuf_pool);
439 GOTO_FAIL("cannot allocate mbuf");
441 if (rte_pktmbuf_pkt_len(m) != 0)
442 GOTO_FAIL("Bad length");
444 data = rte_pktmbuf_mtod(m, char *);
446 /* allocate a new mbuf from the second pool, and attach it to the first
448 clone = rte_pktmbuf_alloc(pktmbuf_pool2);
450 GOTO_FAIL("cannot allocate mbuf from second pool\n");
452 /* check data room size and priv size, and erase priv */
453 if (rte_pktmbuf_data_room_size(clone->pool) != 0)
454 GOTO_FAIL("data room size should be 0\n");
455 if (rte_pktmbuf_priv_size(clone->pool) != MBUF2_PRIV_SIZE)
456 GOTO_FAIL("data room size should be %d\n", MBUF2_PRIV_SIZE);
457 memset(clone + 1, 0, MBUF2_PRIV_SIZE);
459 /* save data pointer to compare it after detach() */
460 c_data = rte_pktmbuf_mtod(clone, char *);
461 if (c_data != (char *)clone + sizeof(*clone) + MBUF2_PRIV_SIZE)
462 GOTO_FAIL("bad data pointer in clone");
463 if (rte_pktmbuf_headroom(clone) != 0)
464 GOTO_FAIL("bad headroom in clone");
466 rte_pktmbuf_attach(clone, m);
468 if (rte_pktmbuf_mtod(clone, char *) != data)
469 GOTO_FAIL("clone was not attached properly\n");
470 if (rte_pktmbuf_headroom(clone) != RTE_PKTMBUF_HEADROOM)
471 GOTO_FAIL("bad headroom in clone after attach");
472 if (rte_mbuf_refcnt_read(m) != 2)
473 GOTO_FAIL("invalid refcnt in m\n");
475 /* allocate a new mbuf from the second pool, and attach it to the first
477 clone2 = rte_pktmbuf_alloc(pktmbuf_pool2);
479 GOTO_FAIL("cannot allocate clone2 from second pool\n");
481 /* check data room size and priv size, and erase priv */
482 if (rte_pktmbuf_data_room_size(clone2->pool) != 0)
483 GOTO_FAIL("data room size should be 0\n");
484 if (rte_pktmbuf_priv_size(clone2->pool) != MBUF2_PRIV_SIZE)
485 GOTO_FAIL("data room size should be %d\n", MBUF2_PRIV_SIZE);
486 memset(clone2 + 1, 0, MBUF2_PRIV_SIZE);
488 /* save data pointer to compare it after detach() */
489 c_data2 = rte_pktmbuf_mtod(clone2, char *);
490 if (c_data2 != (char *)clone2 + sizeof(*clone2) + MBUF2_PRIV_SIZE)
491 GOTO_FAIL("bad data pointer in clone2");
492 if (rte_pktmbuf_headroom(clone2) != 0)
493 GOTO_FAIL("bad headroom in clone2");
495 rte_pktmbuf_attach(clone2, clone);
497 if (rte_pktmbuf_mtod(clone2, char *) != data)
498 GOTO_FAIL("clone2 was not attached properly\n");
499 if (rte_pktmbuf_headroom(clone2) != RTE_PKTMBUF_HEADROOM)
500 GOTO_FAIL("bad headroom in clone2 after attach");
501 if (rte_mbuf_refcnt_read(m) != 3)
502 GOTO_FAIL("invalid refcnt in m\n");
504 /* detach the clones */
505 rte_pktmbuf_detach(clone);
506 if (c_data != rte_pktmbuf_mtod(clone, char *))
507 GOTO_FAIL("clone was not detached properly\n");
508 if (rte_mbuf_refcnt_read(m) != 2)
509 GOTO_FAIL("invalid refcnt in m\n");
511 rte_pktmbuf_detach(clone2);
512 if (c_data2 != rte_pktmbuf_mtod(clone2, char *))
513 GOTO_FAIL("clone2 was not detached properly\n");
514 if (rte_mbuf_refcnt_read(m) != 1)
515 GOTO_FAIL("invalid refcnt in m\n");
517 /* free the clones and the initial mbuf */
518 rte_pktmbuf_free(clone2);
519 rte_pktmbuf_free(clone);
521 printf("%s ok\n", __func__);
528 rte_pktmbuf_free(clone);
530 rte_pktmbuf_free(clone2);
536 * test allocation and free of mbufs
539 test_pktmbuf_pool(struct rte_mempool *pktmbuf_pool)
542 struct rte_mbuf *m[NB_MBUF];
545 for (i=0; i<NB_MBUF; i++)
548 /* alloc NB_MBUF mbufs */
549 for (i=0; i<NB_MBUF; i++) {
550 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
552 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
556 struct rte_mbuf *extra = NULL;
557 extra = rte_pktmbuf_alloc(pktmbuf_pool);
559 printf("Error pool not empty");
562 extra = rte_pktmbuf_clone(m[0], pktmbuf_pool);
564 printf("Error pool not empty");
568 for (i=0; i<NB_MBUF; i++) {
570 rte_pktmbuf_free(m[i]);
577 * test that the pointer to the data on a packet mbuf is set properly
580 test_pktmbuf_pool_ptr(struct rte_mempool *pktmbuf_pool)
583 struct rte_mbuf *m[NB_MBUF];
586 for (i=0; i<NB_MBUF; i++)
589 /* alloc NB_MBUF mbufs */
590 for (i=0; i<NB_MBUF; i++) {
591 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
593 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
597 m[i]->data_off += 64;
601 for (i=0; i<NB_MBUF; i++) {
603 rte_pktmbuf_free(m[i]);
606 for (i=0; i<NB_MBUF; i++)
609 /* alloc NB_MBUF mbufs */
610 for (i=0; i<NB_MBUF; i++) {
611 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
613 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
617 if (m[i]->data_off != RTE_PKTMBUF_HEADROOM) {
618 printf("invalid data_off\n");
624 for (i=0; i<NB_MBUF; i++) {
626 rte_pktmbuf_free(m[i]);
633 test_pktmbuf_free_segment(struct rte_mempool *pktmbuf_pool)
636 struct rte_mbuf *m[NB_MBUF];
639 for (i=0; i<NB_MBUF; i++)
642 /* alloc NB_MBUF mbufs */
643 for (i=0; i<NB_MBUF; i++) {
644 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
646 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
652 for (i=0; i<NB_MBUF; i++) {
654 struct rte_mbuf *mb, *mt;
660 rte_pktmbuf_free_seg(mt);
669 * Stress test for rte_mbuf atomic refcnt.
670 * Implies that RTE_MBUF_REFCNT_ATOMIC is defined.
671 * For more efficiency, recommended to run with RTE_LIBRTE_MBUF_DEBUG defined.
674 #ifdef RTE_MBUF_REFCNT_ATOMIC
677 test_refcnt_slave(void *arg)
679 unsigned lcore, free;
681 struct rte_ring *refcnt_mbuf_ring = arg;
683 lcore = rte_lcore_id();
684 printf("%s started at lcore %u\n", __func__, lcore);
687 while (refcnt_stop_slaves == 0) {
688 if (rte_ring_dequeue(refcnt_mbuf_ring, &mp) == 0) {
690 rte_pktmbuf_free(mp);
694 refcnt_lcore[lcore] += free;
695 printf("%s finished at lcore %u, "
696 "number of freed mbufs: %u\n",
697 __func__, lcore, free);
702 test_refcnt_iter(unsigned int lcore, unsigned int iter,
703 struct rte_mempool *refcnt_pool,
704 struct rte_ring *refcnt_mbuf_ring)
707 unsigned i, n, tref, wn;
712 /* For each mbuf in the pool:
714 * - increment it's reference up to N+1,
715 * - enqueue it N times into the ring for slave cores to free.
717 for (i = 0, n = rte_mempool_avail_count(refcnt_pool);
718 i != n && (m = rte_pktmbuf_alloc(refcnt_pool)) != NULL;
720 ref = RTE_MAX(rte_rand() % REFCNT_MAX_REF, 1UL);
722 if ((ref & 1) != 0) {
723 rte_pktmbuf_refcnt_update(m, ref);
725 rte_ring_enqueue(refcnt_mbuf_ring, m);
728 rte_pktmbuf_refcnt_update(m, 1);
729 rte_ring_enqueue(refcnt_mbuf_ring, m);
736 rte_panic("(lcore=%u, iter=%u): was able to allocate only "
737 "%u from %u mbufs\n", lcore, iter, i, n);
739 /* wait till slave lcores will consume all mbufs */
740 while (!rte_ring_empty(refcnt_mbuf_ring))
743 /* check that all mbufs are back into mempool by now */
744 for (wn = 0; wn != REFCNT_MAX_TIMEOUT; wn++) {
745 if ((i = rte_mempool_avail_count(refcnt_pool)) == n) {
746 refcnt_lcore[lcore] += tref;
747 printf("%s(lcore=%u, iter=%u) completed, "
748 "%u references processed\n",
749 __func__, lcore, iter, tref);
755 rte_panic("(lcore=%u, iter=%u): after %us only "
756 "%u of %u mbufs left free\n", lcore, iter, wn, i, n);
760 test_refcnt_master(struct rte_mempool *refcnt_pool,
761 struct rte_ring *refcnt_mbuf_ring)
765 lcore = rte_lcore_id();
766 printf("%s started at lcore %u\n", __func__, lcore);
768 for (i = 0; i != REFCNT_MAX_ITER; i++)
769 test_refcnt_iter(lcore, i, refcnt_pool, refcnt_mbuf_ring);
771 refcnt_stop_slaves = 1;
774 printf("%s finished at lcore %u\n", __func__, lcore);
781 test_refcnt_mbuf(void)
783 #ifdef RTE_MBUF_REFCNT_ATOMIC
784 unsigned lnum, master, slave, tref;
786 struct rte_mempool *refcnt_pool = NULL;
787 struct rte_ring *refcnt_mbuf_ring = NULL;
789 if ((lnum = rte_lcore_count()) == 1) {
790 printf("skipping %s, number of lcores: %u is not enough\n",
795 printf("starting %s, at %u lcores\n", __func__, lnum);
797 /* create refcnt pool & ring if they don't exist */
799 refcnt_pool = rte_pktmbuf_pool_create(MAKE_STRING(refcnt_pool),
800 REFCNT_MBUF_NUM, 0, 0, 0,
802 if (refcnt_pool == NULL) {
803 printf("%s: cannot allocate " MAKE_STRING(refcnt_pool) "\n",
808 refcnt_mbuf_ring = rte_ring_create("refcnt_mbuf_ring",
809 rte_align32pow2(REFCNT_RING_SIZE), SOCKET_ID_ANY,
811 if (refcnt_mbuf_ring == NULL) {
812 printf("%s: cannot allocate " MAKE_STRING(refcnt_mbuf_ring)
817 refcnt_stop_slaves = 0;
818 memset(refcnt_lcore, 0, sizeof (refcnt_lcore));
820 rte_eal_mp_remote_launch(test_refcnt_slave, refcnt_mbuf_ring,
823 test_refcnt_master(refcnt_pool, refcnt_mbuf_ring);
825 rte_eal_mp_wait_lcore();
827 /* check that we porcessed all references */
829 master = rte_get_master_lcore();
831 RTE_LCORE_FOREACH_SLAVE(slave)
832 tref += refcnt_lcore[slave];
834 if (tref != refcnt_lcore[master])
835 rte_panic("refernced mbufs: %u, freed mbufs: %u\n",
836 tref, refcnt_lcore[master]);
838 rte_mempool_dump(stdout, refcnt_pool);
839 rte_ring_dump(stdout, refcnt_mbuf_ring);
844 rte_mempool_free(refcnt_pool);
845 rte_ring_free(refcnt_mbuf_ring);
853 #include <sys/wait.h>
855 /* use fork() to test mbuf errors panic */
857 verify_mbuf_check_panics(struct rte_mbuf *buf)
865 rte_mbuf_sanity_check(buf, 1); /* should panic */
866 exit(0); /* return normally if it doesn't panic */
868 printf("Fork Failed\n");
879 test_failing_mbuf_sanity_check(struct rte_mempool *pktmbuf_pool)
881 struct rte_mbuf *buf;
882 struct rte_mbuf badbuf;
884 printf("Checking rte_mbuf_sanity_check for failure conditions\n");
886 /* get a good mbuf to use to make copies */
887 buf = rte_pktmbuf_alloc(pktmbuf_pool);
890 printf("Checking good mbuf initially\n");
891 if (verify_mbuf_check_panics(buf) != -1)
894 printf("Now checking for error conditions\n");
896 if (verify_mbuf_check_panics(NULL)) {
897 printf("Error with NULL mbuf test\n");
903 if (verify_mbuf_check_panics(&badbuf)) {
904 printf("Error with bad-pool mbuf test\n");
910 if (verify_mbuf_check_panics(&badbuf)) {
911 printf("Error with bad-physaddr mbuf test\n");
916 badbuf.buf_addr = NULL;
917 if (verify_mbuf_check_panics(&badbuf)) {
918 printf("Error with bad-addr mbuf test\n");
924 if (verify_mbuf_check_panics(&badbuf)) {
925 printf("Error with bad-refcnt(0) mbuf test\n");
930 badbuf.refcnt = UINT16_MAX;
931 if (verify_mbuf_check_panics(&badbuf)) {
932 printf("Error with bad-refcnt(MAX) mbuf test\n");
940 test_mbuf_linearize(struct rte_mempool *pktmbuf_pool, int pkt_len,
944 struct rte_mbuf *m = NULL, *mbuf = NULL;
952 printf("Packet size must be 1 or more (is %d)\n", pkt_len);
957 printf("Number of segments must be 1 or more (is %d)\n",
962 seg_len = pkt_len / nb_segs;
968 /* Create chained mbuf_src and fill it generated data */
969 for (seg = 0; remain > 0; seg++) {
971 m = rte_pktmbuf_alloc(pktmbuf_pool);
973 printf("Cannot create segment for source mbuf");
977 /* Make sure if tailroom is zeroed */
978 memset(rte_pktmbuf_mtod(m, uint8_t *), 0,
979 rte_pktmbuf_tailroom(m));
982 if (data_len > seg_len)
985 data = (uint8_t *)rte_pktmbuf_append(m, data_len);
987 printf("Cannot append %d bytes to the mbuf\n",
992 for (i = 0; i < data_len; i++)
993 data[i] = (seg * seg_len + i) % 0x0ff;
998 rte_pktmbuf_chain(mbuf, m);
1003 /* Create destination buffer to store coalesced data */
1004 if (rte_pktmbuf_linearize(mbuf)) {
1005 printf("Mbuf linearization failed\n");
1009 if (!rte_pktmbuf_is_contiguous(mbuf)) {
1010 printf("Source buffer should be contiguous after "
1015 data = rte_pktmbuf_mtod(mbuf, uint8_t *);
1017 for (i = 0; i < pkt_len; i++)
1018 if (data[i] != (i % 0x0ff)) {
1019 printf("Incorrect data in linearized mbuf\n");
1023 rte_pktmbuf_free(mbuf);
1028 rte_pktmbuf_free(mbuf);
1033 test_mbuf_linearize_check(struct rte_mempool *pktmbuf_pool)
1035 struct test_mbuf_array {
1047 printf("Test mbuf linearize API\n");
1049 for (i = 0; i < RTE_DIM(mbuf_array); i++)
1050 if (test_mbuf_linearize(pktmbuf_pool, mbuf_array[i].size,
1051 mbuf_array[i].nb_segs)) {
1052 printf("Test failed for %d, %d\n", mbuf_array[i].size,
1053 mbuf_array[i].nb_segs);
1064 struct rte_mempool *pktmbuf_pool = NULL;
1065 struct rte_mempool *pktmbuf_pool2 = NULL;
1068 RTE_BUILD_BUG_ON(sizeof(struct rte_mbuf) != RTE_CACHE_LINE_MIN_SIZE * 2);
1070 /* create pktmbuf pool if it does not exist */
1071 pktmbuf_pool = rte_pktmbuf_pool_create("test_pktmbuf_pool",
1072 NB_MBUF, 32, 0, MBUF_DATA_SIZE, SOCKET_ID_ANY);
1074 if (pktmbuf_pool == NULL) {
1075 printf("cannot allocate mbuf pool\n");
1079 /* create a specific pktmbuf pool with a priv_size != 0 and no data
1081 pktmbuf_pool2 = rte_pktmbuf_pool_create("test_pktmbuf_pool2",
1082 NB_MBUF, 32, MBUF2_PRIV_SIZE, 0, SOCKET_ID_ANY);
1084 if (pktmbuf_pool2 == NULL) {
1085 printf("cannot allocate mbuf pool\n");
1089 /* test multiple mbuf alloc */
1090 if (test_pktmbuf_pool(pktmbuf_pool) < 0) {
1091 printf("test_mbuf_pool() failed\n");
1095 /* do it another time to check that all mbufs were freed */
1096 if (test_pktmbuf_pool(pktmbuf_pool) < 0) {
1097 printf("test_mbuf_pool() failed (2)\n");
1101 /* test that the pointer to the data on a packet mbuf is set properly */
1102 if (test_pktmbuf_pool_ptr(pktmbuf_pool) < 0) {
1103 printf("test_pktmbuf_pool_ptr() failed\n");
1107 /* test data manipulation in mbuf */
1108 if (test_one_pktmbuf(pktmbuf_pool) < 0) {
1109 printf("test_one_mbuf() failed\n");
1115 * do it another time, to check that allocation reinitialize
1116 * the mbuf correctly
1118 if (test_one_pktmbuf(pktmbuf_pool) < 0) {
1119 printf("test_one_mbuf() failed (2)\n");
1123 if (test_pktmbuf_with_non_ascii_data(pktmbuf_pool) < 0) {
1124 printf("test_pktmbuf_with_non_ascii_data() failed\n");
1128 /* test free pktmbuf segment one by one */
1129 if (test_pktmbuf_free_segment(pktmbuf_pool) < 0) {
1130 printf("test_pktmbuf_free_segment() failed.\n");
1134 if (testclone_testupdate_testdetach(pktmbuf_pool) < 0) {
1135 printf("testclone_and_testupdate() failed \n");
1139 if (test_attach_from_different_pool(pktmbuf_pool, pktmbuf_pool2) < 0) {
1140 printf("test_attach_from_different_pool() failed\n");
1144 if (test_refcnt_mbuf()<0){
1145 printf("test_refcnt_mbuf() failed \n");
1149 if (test_failing_mbuf_sanity_check(pktmbuf_pool) < 0) {
1150 printf("test_failing_mbuf_sanity_check() failed\n");
1154 if (test_mbuf_linearize_check(pktmbuf_pool) < 0) {
1155 printf("test_mbuf_linearize_check() failed\n");
1161 rte_mempool_free(pktmbuf_pool);
1162 rte_mempool_free(pktmbuf_pool2);
1166 REGISTER_TEST_COMMAND(mbuf_autotest, test_mbuf);