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 *mc = NULL;
324 struct rte_mbuf *clone = NULL;
328 mc = rte_pktmbuf_alloc(pktmbuf_pool);
330 GOTO_FAIL("ooops not allocating mbuf");
332 if (rte_pktmbuf_pkt_len(mc) != 0)
333 GOTO_FAIL("Bad length");
336 /* clone the allocated mbuf */
337 clone = rte_pktmbuf_clone(mc, pktmbuf_pool);
339 GOTO_FAIL("cannot clone data\n");
340 rte_pktmbuf_free(clone);
342 mc->next = rte_pktmbuf_alloc(pktmbuf_pool);
344 GOTO_FAIL("Next Pkt Null\n");
346 clone = rte_pktmbuf_clone(mc, pktmbuf_pool);
348 GOTO_FAIL("cannot clone data\n");
351 rte_pktmbuf_free(mc);
352 rte_pktmbuf_free(clone);
359 rte_pktmbuf_free(mc);
367 * test allocation and free of mbufs
370 test_pktmbuf_pool(void)
373 struct rte_mbuf *m[NB_MBUF];
376 for (i=0; i<NB_MBUF; i++)
379 /* alloc NB_MBUF mbufs */
380 for (i=0; i<NB_MBUF; i++) {
381 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
383 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
387 struct rte_mbuf *extra = NULL;
388 extra = rte_pktmbuf_alloc(pktmbuf_pool);
390 printf("Error pool not empty");
393 extra = rte_pktmbuf_clone(m[0], pktmbuf_pool);
395 printf("Error pool not empty");
399 for (i=0; i<NB_MBUF; i++) {
401 rte_pktmbuf_free(m[i]);
408 * test that the pointer to the data on a packet mbuf is set properly
411 test_pktmbuf_pool_ptr(void)
414 struct rte_mbuf *m[NB_MBUF];
417 for (i=0; i<NB_MBUF; i++)
420 /* alloc NB_MBUF mbufs */
421 for (i=0; i<NB_MBUF; i++) {
422 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
424 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
427 m[i]->data_off += 64;
431 for (i=0; i<NB_MBUF; i++) {
433 rte_pktmbuf_free(m[i]);
436 for (i=0; i<NB_MBUF; i++)
439 /* alloc NB_MBUF mbufs */
440 for (i=0; i<NB_MBUF; i++) {
441 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
443 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
446 if (m[i]->data_off != RTE_PKTMBUF_HEADROOM) {
447 printf("invalid data_off\n");
453 for (i=0; i<NB_MBUF; i++) {
455 rte_pktmbuf_free(m[i]);
462 test_pktmbuf_free_segment(void)
465 struct rte_mbuf *m[NB_MBUF];
468 for (i=0; i<NB_MBUF; i++)
471 /* alloc NB_MBUF mbufs */
472 for (i=0; i<NB_MBUF; i++) {
473 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
475 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
481 for (i=0; i<NB_MBUF; i++) {
483 struct rte_mbuf *mb, *mt;
489 rte_pktmbuf_free_seg(mt);
498 * Stress test for rte_mbuf atomic refcnt.
499 * Implies that RTE_MBUF_REFCNT_ATOMIC is defined.
500 * For more efficency, recomended to run with RTE_LIBRTE_MBUF_DEBUG defined.
503 #ifdef RTE_MBUF_REFCNT_ATOMIC
506 test_refcnt_slave(__attribute__((unused)) void *arg)
508 unsigned lcore, free;
511 lcore = rte_lcore_id();
512 printf("%s started at lcore %u\n", __func__, lcore);
515 while (refcnt_stop_slaves == 0) {
516 if (rte_ring_dequeue(refcnt_mbuf_ring, &mp) == 0) {
518 rte_pktmbuf_free((struct rte_mbuf *)mp);
522 refcnt_lcore[lcore] += free;
523 printf("%s finished at lcore %u, "
524 "number of freed mbufs: %u\n",
525 __func__, lcore, free);
530 test_refcnt_iter(unsigned lcore, unsigned iter)
533 unsigned i, n, tref, wn;
538 /* For each mbuf in the pool:
540 * - increment it's reference up to N+1,
541 * - enqueue it N times into the ring for slave cores to free.
543 for (i = 0, n = rte_mempool_count(refcnt_pool);
544 i != n && (m = rte_pktmbuf_alloc(refcnt_pool)) != NULL;
546 ref = RTE_MAX(rte_rand() % REFCNT_MAX_REF, 1UL);
548 if ((ref & 1) != 0) {
549 rte_pktmbuf_refcnt_update(m, ref);
551 rte_ring_enqueue(refcnt_mbuf_ring, m);
554 rte_pktmbuf_refcnt_update(m, 1);
555 rte_ring_enqueue(refcnt_mbuf_ring, m);
562 rte_panic("(lcore=%u, iter=%u): was able to allocate only "
563 "%u from %u mbufs\n", lcore, iter, i, n);
565 /* wait till slave lcores will consume all mbufs */
566 while (!rte_ring_empty(refcnt_mbuf_ring))
569 /* check that all mbufs are back into mempool by now */
570 for (wn = 0; wn != REFCNT_MAX_TIMEOUT; wn++) {
571 if ((i = rte_mempool_count(refcnt_pool)) == n) {
572 refcnt_lcore[lcore] += tref;
573 printf("%s(lcore=%u, iter=%u) completed, "
574 "%u references processed\n",
575 __func__, lcore, iter, tref);
581 rte_panic("(lcore=%u, iter=%u): after %us only "
582 "%u of %u mbufs left free\n", lcore, iter, wn, i, n);
586 test_refcnt_master(void)
590 lcore = rte_lcore_id();
591 printf("%s started at lcore %u\n", __func__, lcore);
593 for (i = 0; i != REFCNT_MAX_ITER; i++)
594 test_refcnt_iter(lcore, i);
596 refcnt_stop_slaves = 1;
599 printf("%s finished at lcore %u\n", __func__, lcore);
606 test_refcnt_mbuf(void)
608 #ifdef RTE_MBUF_REFCNT_ATOMIC
610 unsigned lnum, master, slave, tref;
613 if ((lnum = rte_lcore_count()) == 1) {
614 printf("skipping %s, number of lcores: %u is not enough\n",
619 printf("starting %s, at %u lcores\n", __func__, lnum);
621 /* create refcnt pool & ring if they don't exist */
623 if (refcnt_pool == NULL &&
624 (refcnt_pool = rte_pktmbuf_pool_create(
625 MAKE_STRING(refcnt_pool),
626 REFCNT_MBUF_NUM, 0, 0, 0,
627 SOCKET_ID_ANY)) == NULL) {
628 printf("%s: cannot allocate " MAKE_STRING(refcnt_pool) "\n",
633 if (refcnt_mbuf_ring == NULL &&
634 (refcnt_mbuf_ring = rte_ring_create("refcnt_mbuf_ring",
635 REFCNT_RING_SIZE, SOCKET_ID_ANY,
636 RING_F_SP_ENQ)) == NULL) {
637 printf("%s: cannot allocate " MAKE_STRING(refcnt_mbuf_ring)
642 refcnt_stop_slaves = 0;
643 memset(refcnt_lcore, 0, sizeof (refcnt_lcore));
645 rte_eal_mp_remote_launch(test_refcnt_slave, NULL, SKIP_MASTER);
647 test_refcnt_master();
649 rte_eal_mp_wait_lcore();
651 /* check that we porcessed all references */
653 master = rte_get_master_lcore();
655 RTE_LCORE_FOREACH_SLAVE(slave)
656 tref += refcnt_lcore[slave];
658 if (tref != refcnt_lcore[master])
659 rte_panic("refernced mbufs: %u, freed mbufs: %u\n",
660 tref, refcnt_lcore[master]);
662 rte_mempool_dump(stdout, refcnt_pool);
663 rte_ring_dump(stdout, refcnt_mbuf_ring);
670 #include <sys/wait.h>
672 /* use fork() to test mbuf errors panic */
674 verify_mbuf_check_panics(struct rte_mbuf *buf)
682 rte_mbuf_sanity_check(buf, 1); /* should panic */
683 exit(0); /* return normally if it doesn't panic */
685 printf("Fork Failed\n");
696 test_failing_mbuf_sanity_check(void)
698 struct rte_mbuf *buf;
699 struct rte_mbuf badbuf;
701 printf("Checking rte_mbuf_sanity_check for failure conditions\n");
703 /* get a good mbuf to use to make copies */
704 buf = rte_pktmbuf_alloc(pktmbuf_pool);
707 printf("Checking good mbuf initially\n");
708 if (verify_mbuf_check_panics(buf) != -1)
711 printf("Now checking for error conditions\n");
713 if (verify_mbuf_check_panics(NULL)) {
714 printf("Error with NULL mbuf test\n");
720 if (verify_mbuf_check_panics(&badbuf)) {
721 printf("Error with bad-pool mbuf test\n");
726 badbuf.buf_physaddr = 0;
727 if (verify_mbuf_check_panics(&badbuf)) {
728 printf("Error with bad-physaddr mbuf test\n");
733 badbuf.buf_addr = NULL;
734 if (verify_mbuf_check_panics(&badbuf)) {
735 printf("Error with bad-addr mbuf test\n");
741 if (verify_mbuf_check_panics(&badbuf)) {
742 printf("Error with bad-refcnt(0) mbuf test\n");
747 badbuf.refcnt = UINT16_MAX;
748 if (verify_mbuf_check_panics(&badbuf)) {
749 printf("Error with bad-refcnt(MAX) mbuf test\n");
760 RTE_BUILD_BUG_ON(sizeof(struct rte_mbuf) != RTE_CACHE_LINE_SIZE * 2);
762 /* create pktmbuf pool if it does not exist */
763 if (pktmbuf_pool == NULL) {
764 pktmbuf_pool = rte_pktmbuf_pool_create("test_pktmbuf_pool",
765 NB_MBUF, 32, 0, MBUF_DATA_SIZE, SOCKET_ID_ANY);
768 if (pktmbuf_pool == NULL) {
769 printf("cannot allocate mbuf pool\n");
773 /* test multiple mbuf alloc */
774 if (test_pktmbuf_pool() < 0) {
775 printf("test_mbuf_pool() failed\n");
779 /* do it another time to check that all mbufs were freed */
780 if (test_pktmbuf_pool() < 0) {
781 printf("test_mbuf_pool() failed (2)\n");
785 /* test that the pointer to the data on a packet mbuf is set properly */
786 if (test_pktmbuf_pool_ptr() < 0) {
787 printf("test_pktmbuf_pool_ptr() failed\n");
791 /* test data manipulation in mbuf */
792 if (test_one_pktmbuf() < 0) {
793 printf("test_one_mbuf() failed\n");
799 * do it another time, to check that allocation reinitialize
802 if (test_one_pktmbuf() < 0) {
803 printf("test_one_mbuf() failed (2)\n");
807 if (test_pktmbuf_with_non_ascii_data() < 0) {
808 printf("test_pktmbuf_with_non_ascii_data() failed\n");
812 /* test free pktmbuf segment one by one */
813 if (test_pktmbuf_free_segment() < 0) {
814 printf("test_pktmbuf_free_segment() failed.\n");
818 if (testclone_testupdate_testdetach()<0){
819 printf("testclone_and_testupdate() failed \n");
823 if (test_refcnt_mbuf()<0){
824 printf("test_refcnt_mbuf() failed \n");
828 if (test_failing_mbuf_sanity_check() < 0) {
829 printf("test_failing_mbuf_sanity_check() failed\n");
835 static struct test_command mbuf_cmd = {
836 .command = "mbuf_autotest",
837 .callback = test_mbuf,
839 REGISTER_TEST_COMMAND(mbuf_cmd);