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_common.h>
47 #include <rte_memory.h>
48 #include <rte_memcpy.h>
49 #include <rte_memzone.h>
50 #include <rte_launch.h>
52 #include <rte_per_lcore.h>
53 #include <rte_lcore.h>
54 #include <rte_atomic.h>
55 #include <rte_branch_prediction.h>
57 #include <rte_mempool.h>
59 #include <rte_random.h>
60 #include <rte_cycles.h>
64 #define MBUF_DATA_SIZE 2048
66 #define MBUF_TEST_DATA_LEN 1464
67 #define MBUF_TEST_DATA_LEN2 50
68 #define MBUF_TEST_HDR1_LEN 20
69 #define MBUF_TEST_HDR2_LEN 30
70 #define MBUF_TEST_ALL_HDRS_LEN (MBUF_TEST_HDR1_LEN+MBUF_TEST_HDR2_LEN)
72 #define REFCNT_MAX_ITER 64
73 #define REFCNT_MAX_TIMEOUT 10
74 #define REFCNT_MAX_REF (RTE_MAX_LCORE)
75 #define REFCNT_MBUF_NUM 64
76 #define REFCNT_RING_SIZE (REFCNT_MBUF_NUM * REFCNT_MAX_REF)
78 #define MAKE_STRING(x) # x
80 static struct rte_mempool *pktmbuf_pool = NULL;
82 #ifdef RTE_MBUF_REFCNT_ATOMIC
84 static struct rte_mempool *refcnt_pool = NULL;
85 static struct rte_ring *refcnt_mbuf_ring = NULL;
86 static volatile uint32_t refcnt_stop_slaves;
87 static unsigned refcnt_lcore[RTE_MAX_LCORE];
95 * #. Allocate a mbuf pool.
97 * - The pool contains NB_MBUF elements, where each mbuf is MBUF_SIZE
100 * #. Test multiple allocations of mbufs from this pool.
102 * - Allocate NB_MBUF and store pointers in a table.
103 * - If an allocation fails, return an error.
104 * - Free all these mbufs.
105 * - Repeat the same test to check that mbufs were freed correctly.
107 * #. Test data manipulation in pktmbuf.
110 * - Append data using rte_pktmbuf_append().
111 * - Test for error in rte_pktmbuf_append() when len is too large.
112 * - Trim data at the end of mbuf using rte_pktmbuf_trim().
113 * - Test for error in rte_pktmbuf_trim() when len is too large.
114 * - Prepend a header using rte_pktmbuf_prepend().
115 * - Test for error in rte_pktmbuf_prepend() when len is too large.
116 * - Remove data at the beginning of mbuf using rte_pktmbuf_adj().
117 * - Test for error in rte_pktmbuf_adj() when len is too large.
118 * - Check that appended data is not corrupt.
120 * - Between all these tests, check data_len and pkt_len, and
121 * that the mbuf is contiguous.
122 * - Repeat the test to check that allocation operations
123 * reinitialize the mbuf correctly.
127 #define GOTO_FAIL(str, ...) do { \
128 printf("mbuf test FAILED (l.%d): <" str ">\n", \
129 __LINE__, ##__VA_ARGS__); \
134 * test data manipulation in mbuf with non-ascii data
137 test_pktmbuf_with_non_ascii_data(void)
139 struct rte_mbuf *m = NULL;
142 m = rte_pktmbuf_alloc(pktmbuf_pool);
144 GOTO_FAIL("Cannot allocate mbuf");
145 if (rte_pktmbuf_pkt_len(m) != 0)
146 GOTO_FAIL("Bad length");
148 data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN);
150 GOTO_FAIL("Cannot append data");
151 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
152 GOTO_FAIL("Bad pkt length");
153 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
154 GOTO_FAIL("Bad data length");
155 memset(data, 0xff, rte_pktmbuf_pkt_len(m));
156 if (!rte_pktmbuf_is_contiguous(m))
157 GOTO_FAIL("Buffer should be continuous");
158 rte_pktmbuf_dump(stdout, m, MBUF_TEST_DATA_LEN);
172 * test data manipulation in mbuf
175 test_one_pktmbuf(void)
177 struct rte_mbuf *m = NULL;
178 char *data, *data2, *hdr;
181 printf("Test pktmbuf API\n");
185 m = rte_pktmbuf_alloc(pktmbuf_pool);
187 GOTO_FAIL("Cannot allocate mbuf");
188 if (rte_pktmbuf_pkt_len(m) != 0)
189 GOTO_FAIL("Bad length");
191 rte_pktmbuf_dump(stdout, m, 0);
195 data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN);
197 GOTO_FAIL("Cannot append data");
198 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
199 GOTO_FAIL("Bad pkt length");
200 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
201 GOTO_FAIL("Bad data length");
202 memset(data, 0x66, rte_pktmbuf_pkt_len(m));
203 if (!rte_pktmbuf_is_contiguous(m))
204 GOTO_FAIL("Buffer should be continuous");
205 rte_pktmbuf_dump(stdout, m, MBUF_TEST_DATA_LEN);
206 rte_pktmbuf_dump(stdout, m, 2*MBUF_TEST_DATA_LEN);
208 /* this append should fail */
210 data2 = rte_pktmbuf_append(m, (uint16_t)(rte_pktmbuf_tailroom(m) + 1));
212 GOTO_FAIL("Append should not succeed");
214 /* append some more data */
216 data2 = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN2);
218 GOTO_FAIL("Cannot append data");
219 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_DATA_LEN2)
220 GOTO_FAIL("Bad pkt length");
221 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_DATA_LEN2)
222 GOTO_FAIL("Bad data length");
223 if (!rte_pktmbuf_is_contiguous(m))
224 GOTO_FAIL("Buffer should be continuous");
226 /* trim data at the end of mbuf */
228 if (rte_pktmbuf_trim(m, MBUF_TEST_DATA_LEN2) < 0)
229 GOTO_FAIL("Cannot trim data");
230 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
231 GOTO_FAIL("Bad pkt length");
232 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
233 GOTO_FAIL("Bad data length");
234 if (!rte_pktmbuf_is_contiguous(m))
235 GOTO_FAIL("Buffer should be continuous");
237 /* this trim should fail */
239 if (rte_pktmbuf_trim(m, (uint16_t)(rte_pktmbuf_data_len(m) + 1)) == 0)
240 GOTO_FAIL("trim should not succeed");
242 /* prepend one header */
244 hdr = rte_pktmbuf_prepend(m, MBUF_TEST_HDR1_LEN);
246 GOTO_FAIL("Cannot prepend");
247 if (data - hdr != MBUF_TEST_HDR1_LEN)
248 GOTO_FAIL("Prepend failed");
249 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_HDR1_LEN)
250 GOTO_FAIL("Bad pkt length");
251 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_HDR1_LEN)
252 GOTO_FAIL("Bad data length");
253 if (!rte_pktmbuf_is_contiguous(m))
254 GOTO_FAIL("Buffer should be continuous");
255 memset(hdr, 0x55, MBUF_TEST_HDR1_LEN);
257 /* prepend another header */
259 hdr = rte_pktmbuf_prepend(m, MBUF_TEST_HDR2_LEN);
261 GOTO_FAIL("Cannot prepend");
262 if (data - hdr != MBUF_TEST_ALL_HDRS_LEN)
263 GOTO_FAIL("Prepend failed");
264 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_ALL_HDRS_LEN)
265 GOTO_FAIL("Bad pkt length");
266 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_ALL_HDRS_LEN)
267 GOTO_FAIL("Bad data length");
268 if (!rte_pktmbuf_is_contiguous(m))
269 GOTO_FAIL("Buffer should be continuous");
270 memset(hdr, 0x55, MBUF_TEST_HDR2_LEN);
272 rte_mbuf_sanity_check(m, 1);
273 rte_mbuf_sanity_check(m, 0);
274 rte_pktmbuf_dump(stdout, m, 0);
276 /* this prepend should fail */
278 hdr = rte_pktmbuf_prepend(m, (uint16_t)(rte_pktmbuf_headroom(m) + 1));
280 GOTO_FAIL("prepend should not succeed");
282 /* remove data at beginning of mbuf (adj) */
284 if (data != rte_pktmbuf_adj(m, MBUF_TEST_ALL_HDRS_LEN))
285 GOTO_FAIL("rte_pktmbuf_adj failed");
286 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
287 GOTO_FAIL("Bad pkt length");
288 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
289 GOTO_FAIL("Bad data length");
290 if (!rte_pktmbuf_is_contiguous(m))
291 GOTO_FAIL("Buffer should be continuous");
293 /* this adj should fail */
295 if (rte_pktmbuf_adj(m, (uint16_t)(rte_pktmbuf_data_len(m) + 1)) != NULL)
296 GOTO_FAIL("rte_pktmbuf_adj should not succeed");
300 if (!rte_pktmbuf_is_contiguous(m))
301 GOTO_FAIL("Buffer should be continuous");
303 for (i=0; i<MBUF_TEST_DATA_LEN; i++) {
305 GOTO_FAIL("Data corrupted at offset %u", i);
321 testclone_testupdate_testdetach(void)
323 struct rte_mbuf *m = NULL;
324 struct rte_mbuf *clone = NULL;
327 m = rte_pktmbuf_alloc(pktmbuf_pool);
329 GOTO_FAIL("ooops not allocating mbuf");
331 if (rte_pktmbuf_pkt_len(m) != 0)
332 GOTO_FAIL("Bad length");
335 /* clone the allocated mbuf */
336 clone = rte_pktmbuf_clone(m, pktmbuf_pool);
338 GOTO_FAIL("cannot clone data\n");
339 rte_pktmbuf_free(clone);
341 m->next = rte_pktmbuf_alloc(pktmbuf_pool);
343 GOTO_FAIL("Next Pkt Null\n");
345 clone = rte_pktmbuf_clone(m, pktmbuf_pool);
347 GOTO_FAIL("cannot clone data\n");
351 rte_pktmbuf_free(clone);
366 * test allocation and free of mbufs
369 test_pktmbuf_pool(void)
372 struct rte_mbuf *m[NB_MBUF];
375 for (i=0; i<NB_MBUF; i++)
378 /* alloc NB_MBUF mbufs */
379 for (i=0; i<NB_MBUF; i++) {
380 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
382 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
386 struct rte_mbuf *extra = NULL;
387 extra = rte_pktmbuf_alloc(pktmbuf_pool);
389 printf("Error pool not empty");
392 extra = rte_pktmbuf_clone(m[0], pktmbuf_pool);
394 printf("Error pool not empty");
398 for (i=0; i<NB_MBUF; i++) {
400 rte_pktmbuf_free(m[i]);
407 * test that the pointer to the data on a packet mbuf is set properly
410 test_pktmbuf_pool_ptr(void)
413 struct rte_mbuf *m[NB_MBUF];
416 for (i=0; i<NB_MBUF; i++)
419 /* alloc NB_MBUF mbufs */
420 for (i=0; i<NB_MBUF; i++) {
421 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
423 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
426 m[i]->data_off += 64;
430 for (i=0; i<NB_MBUF; i++) {
432 rte_pktmbuf_free(m[i]);
435 for (i=0; i<NB_MBUF; i++)
438 /* alloc NB_MBUF mbufs */
439 for (i=0; i<NB_MBUF; i++) {
440 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
442 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
445 if (m[i]->data_off != RTE_PKTMBUF_HEADROOM) {
446 printf("invalid data_off\n");
452 for (i=0; i<NB_MBUF; i++) {
454 rte_pktmbuf_free(m[i]);
461 test_pktmbuf_free_segment(void)
464 struct rte_mbuf *m[NB_MBUF];
467 for (i=0; i<NB_MBUF; i++)
470 /* alloc NB_MBUF mbufs */
471 for (i=0; i<NB_MBUF; i++) {
472 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
474 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
480 for (i=0; i<NB_MBUF; i++) {
482 struct rte_mbuf *mb, *mt;
488 rte_pktmbuf_free_seg(mt);
497 * Stress test for rte_mbuf atomic refcnt.
498 * Implies that RTE_MBUF_REFCNT_ATOMIC is defined.
499 * For more efficency, recomended to run with RTE_LIBRTE_MBUF_DEBUG defined.
502 #ifdef RTE_MBUF_REFCNT_ATOMIC
505 test_refcnt_slave(__attribute__((unused)) void *arg)
507 unsigned lcore, free;
510 lcore = rte_lcore_id();
511 printf("%s started at lcore %u\n", __func__, lcore);
514 while (refcnt_stop_slaves == 0) {
515 if (rte_ring_dequeue(refcnt_mbuf_ring, &mp) == 0) {
517 rte_pktmbuf_free((struct rte_mbuf *)mp);
521 refcnt_lcore[lcore] += free;
522 printf("%s finished at lcore %u, "
523 "number of freed mbufs: %u\n",
524 __func__, lcore, free);
529 test_refcnt_iter(unsigned lcore, unsigned iter)
532 unsigned i, n, tref, wn;
537 /* For each mbuf in the pool:
539 * - increment it's reference up to N+1,
540 * - enqueue it N times into the ring for slave cores to free.
542 for (i = 0, n = rte_mempool_count(refcnt_pool);
543 i != n && (m = rte_pktmbuf_alloc(refcnt_pool)) != NULL;
545 ref = RTE_MAX(rte_rand() % REFCNT_MAX_REF, 1UL);
547 if ((ref & 1) != 0) {
548 rte_pktmbuf_refcnt_update(m, ref);
550 rte_ring_enqueue(refcnt_mbuf_ring, m);
553 rte_pktmbuf_refcnt_update(m, 1);
554 rte_ring_enqueue(refcnt_mbuf_ring, m);
561 rte_panic("(lcore=%u, iter=%u): was able to allocate only "
562 "%u from %u mbufs\n", lcore, iter, i, n);
564 /* wait till slave lcores will consume all mbufs */
565 while (!rte_ring_empty(refcnt_mbuf_ring))
568 /* check that all mbufs are back into mempool by now */
569 for (wn = 0; wn != REFCNT_MAX_TIMEOUT; wn++) {
570 if ((i = rte_mempool_count(refcnt_pool)) == n) {
571 refcnt_lcore[lcore] += tref;
572 printf("%s(lcore=%u, iter=%u) completed, "
573 "%u references processed\n",
574 __func__, lcore, iter, tref);
580 rte_panic("(lcore=%u, iter=%u): after %us only "
581 "%u of %u mbufs left free\n", lcore, iter, wn, i, n);
585 test_refcnt_master(void)
589 lcore = rte_lcore_id();
590 printf("%s started at lcore %u\n", __func__, lcore);
592 for (i = 0; i != REFCNT_MAX_ITER; i++)
593 test_refcnt_iter(lcore, i);
595 refcnt_stop_slaves = 1;
598 printf("%s finished at lcore %u\n", __func__, lcore);
605 test_refcnt_mbuf(void)
607 #ifdef RTE_MBUF_REFCNT_ATOMIC
609 unsigned lnum, master, slave, tref;
612 if ((lnum = rte_lcore_count()) == 1) {
613 printf("skipping %s, number of lcores: %u is not enough\n",
618 printf("starting %s, at %u lcores\n", __func__, lnum);
620 /* create refcnt pool & ring if they don't exist */
622 if (refcnt_pool == NULL &&
623 (refcnt_pool = rte_pktmbuf_pool_create(
624 MAKE_STRING(refcnt_pool),
625 REFCNT_MBUF_NUM, 0, 0, 0,
626 SOCKET_ID_ANY)) == NULL) {
627 printf("%s: cannot allocate " MAKE_STRING(refcnt_pool) "\n",
632 if (refcnt_mbuf_ring == NULL &&
633 (refcnt_mbuf_ring = rte_ring_create("refcnt_mbuf_ring",
634 REFCNT_RING_SIZE, SOCKET_ID_ANY,
635 RING_F_SP_ENQ)) == NULL) {
636 printf("%s: cannot allocate " MAKE_STRING(refcnt_mbuf_ring)
641 refcnt_stop_slaves = 0;
642 memset(refcnt_lcore, 0, sizeof (refcnt_lcore));
644 rte_eal_mp_remote_launch(test_refcnt_slave, NULL, SKIP_MASTER);
646 test_refcnt_master();
648 rte_eal_mp_wait_lcore();
650 /* check that we porcessed all references */
652 master = rte_get_master_lcore();
654 RTE_LCORE_FOREACH_SLAVE(slave)
655 tref += refcnt_lcore[slave];
657 if (tref != refcnt_lcore[master])
658 rte_panic("refernced mbufs: %u, freed mbufs: %u\n",
659 tref, refcnt_lcore[master]);
661 rte_mempool_dump(stdout, refcnt_pool);
662 rte_ring_dump(stdout, refcnt_mbuf_ring);
669 #include <sys/wait.h>
671 /* use fork() to test mbuf errors panic */
673 verify_mbuf_check_panics(struct rte_mbuf *buf)
681 rte_mbuf_sanity_check(buf, 1); /* should panic */
682 exit(0); /* return normally if it doesn't panic */
684 printf("Fork Failed\n");
695 test_failing_mbuf_sanity_check(void)
697 struct rte_mbuf *buf;
698 struct rte_mbuf badbuf;
700 printf("Checking rte_mbuf_sanity_check for failure conditions\n");
702 /* get a good mbuf to use to make copies */
703 buf = rte_pktmbuf_alloc(pktmbuf_pool);
706 printf("Checking good mbuf initially\n");
707 if (verify_mbuf_check_panics(buf) != -1)
710 printf("Now checking for error conditions\n");
712 if (verify_mbuf_check_panics(NULL)) {
713 printf("Error with NULL mbuf test\n");
719 if (verify_mbuf_check_panics(&badbuf)) {
720 printf("Error with bad-pool mbuf test\n");
725 badbuf.buf_physaddr = 0;
726 if (verify_mbuf_check_panics(&badbuf)) {
727 printf("Error with bad-physaddr mbuf test\n");
732 badbuf.buf_addr = NULL;
733 if (verify_mbuf_check_panics(&badbuf)) {
734 printf("Error with bad-addr mbuf test\n");
740 if (verify_mbuf_check_panics(&badbuf)) {
741 printf("Error with bad-refcnt(0) mbuf test\n");
746 badbuf.refcnt = UINT16_MAX;
747 if (verify_mbuf_check_panics(&badbuf)) {
748 printf("Error with bad-refcnt(MAX) mbuf test\n");
759 RTE_BUILD_BUG_ON(sizeof(struct rte_mbuf) != RTE_CACHE_LINE_SIZE * 2);
761 /* create pktmbuf pool if it does not exist */
762 if (pktmbuf_pool == NULL) {
763 pktmbuf_pool = rte_pktmbuf_pool_create("test_pktmbuf_pool",
764 NB_MBUF, 32, 0, MBUF_DATA_SIZE, SOCKET_ID_ANY);
767 if (pktmbuf_pool == NULL) {
768 printf("cannot allocate mbuf pool\n");
772 /* test multiple mbuf alloc */
773 if (test_pktmbuf_pool() < 0) {
774 printf("test_mbuf_pool() failed\n");
778 /* do it another time to check that all mbufs were freed */
779 if (test_pktmbuf_pool() < 0) {
780 printf("test_mbuf_pool() failed (2)\n");
784 /* test that the pointer to the data on a packet mbuf is set properly */
785 if (test_pktmbuf_pool_ptr() < 0) {
786 printf("test_pktmbuf_pool_ptr() failed\n");
790 /* test data manipulation in mbuf */
791 if (test_one_pktmbuf() < 0) {
792 printf("test_one_mbuf() failed\n");
798 * do it another time, to check that allocation reinitialize
801 if (test_one_pktmbuf() < 0) {
802 printf("test_one_mbuf() failed (2)\n");
806 if (test_pktmbuf_with_non_ascii_data() < 0) {
807 printf("test_pktmbuf_with_non_ascii_data() failed\n");
811 /* test free pktmbuf segment one by one */
812 if (test_pktmbuf_free_segment() < 0) {
813 printf("test_pktmbuf_free_segment() failed.\n");
817 if (testclone_testupdate_testdetach()<0){
818 printf("testclone_and_testupdate() failed \n");
822 if (test_refcnt_mbuf()<0){
823 printf("test_refcnt_mbuf() failed \n");
827 if (test_failing_mbuf_sanity_check() < 0) {
828 printf("test_failing_mbuf_sanity_check() failed\n");
834 static struct test_command mbuf_cmd = {
835 .command = "mbuf_autotest",
836 .callback = test_mbuf,
838 REGISTER_TEST_COMMAND(mbuf_cmd);