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 /* size of private data for mbuf in pktmbuf_pool2 */
73 #define MBUF2_PRIV_SIZE 128
75 #define REFCNT_MAX_ITER 64
76 #define REFCNT_MAX_TIMEOUT 10
77 #define REFCNT_MAX_REF (RTE_MAX_LCORE)
78 #define REFCNT_MBUF_NUM 64
79 #define REFCNT_RING_SIZE (REFCNT_MBUF_NUM * REFCNT_MAX_REF)
81 #define MAGIC_DATA 0x42424242
83 #define MAKE_STRING(x) # x
85 static struct rte_mempool *pktmbuf_pool = NULL;
86 static struct rte_mempool *pktmbuf_pool2 = NULL;
88 #ifdef RTE_MBUF_REFCNT_ATOMIC
90 static struct rte_mempool *refcnt_pool = NULL;
91 static struct rte_ring *refcnt_mbuf_ring = NULL;
92 static volatile uint32_t refcnt_stop_slaves;
93 static unsigned refcnt_lcore[RTE_MAX_LCORE];
101 * #. Allocate a mbuf pool.
103 * - The pool contains NB_MBUF elements, where each mbuf is MBUF_SIZE
106 * #. Test multiple allocations of mbufs from this pool.
108 * - Allocate NB_MBUF and store pointers in a table.
109 * - If an allocation fails, return an error.
110 * - Free all these mbufs.
111 * - Repeat the same test to check that mbufs were freed correctly.
113 * #. Test data manipulation in pktmbuf.
116 * - Append data using rte_pktmbuf_append().
117 * - Test for error in rte_pktmbuf_append() when len is too large.
118 * - Trim data at the end of mbuf using rte_pktmbuf_trim().
119 * - Test for error in rte_pktmbuf_trim() when len is too large.
120 * - Prepend a header using rte_pktmbuf_prepend().
121 * - Test for error in rte_pktmbuf_prepend() when len is too large.
122 * - Remove data at the beginning of mbuf using rte_pktmbuf_adj().
123 * - Test for error in rte_pktmbuf_adj() when len is too large.
124 * - Check that appended data is not corrupt.
126 * - Between all these tests, check data_len and pkt_len, and
127 * that the mbuf is contiguous.
128 * - Repeat the test to check that allocation operations
129 * reinitialize the mbuf correctly.
131 * #. Test packet cloning
132 * - Clone a mbuf and verify the data
133 * - Clone the cloned mbuf and verify the data
134 * - Attach a mbuf to another that does not have the same priv_size.
137 #define GOTO_FAIL(str, ...) do { \
138 printf("mbuf test FAILED (l.%d): <" str ">\n", \
139 __LINE__, ##__VA_ARGS__); \
144 * test data manipulation in mbuf with non-ascii data
147 test_pktmbuf_with_non_ascii_data(void)
149 struct rte_mbuf *m = NULL;
152 m = rte_pktmbuf_alloc(pktmbuf_pool);
154 GOTO_FAIL("Cannot allocate mbuf");
155 if (rte_pktmbuf_pkt_len(m) != 0)
156 GOTO_FAIL("Bad length");
158 data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN);
160 GOTO_FAIL("Cannot append data");
161 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
162 GOTO_FAIL("Bad pkt length");
163 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
164 GOTO_FAIL("Bad data length");
165 memset(data, 0xff, rte_pktmbuf_pkt_len(m));
166 if (!rte_pktmbuf_is_contiguous(m))
167 GOTO_FAIL("Buffer should be continuous");
168 rte_pktmbuf_dump(stdout, m, MBUF_TEST_DATA_LEN);
182 * test data manipulation in mbuf
185 test_one_pktmbuf(void)
187 struct rte_mbuf *m = NULL;
188 char *data, *data2, *hdr;
191 printf("Test pktmbuf API\n");
195 m = rte_pktmbuf_alloc(pktmbuf_pool);
197 GOTO_FAIL("Cannot allocate mbuf");
198 if (rte_pktmbuf_pkt_len(m) != 0)
199 GOTO_FAIL("Bad length");
201 rte_pktmbuf_dump(stdout, m, 0);
205 data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN);
207 GOTO_FAIL("Cannot append data");
208 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
209 GOTO_FAIL("Bad pkt length");
210 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
211 GOTO_FAIL("Bad data length");
212 memset(data, 0x66, rte_pktmbuf_pkt_len(m));
213 if (!rte_pktmbuf_is_contiguous(m))
214 GOTO_FAIL("Buffer should be continuous");
215 rte_pktmbuf_dump(stdout, m, MBUF_TEST_DATA_LEN);
216 rte_pktmbuf_dump(stdout, m, 2*MBUF_TEST_DATA_LEN);
218 /* this append should fail */
220 data2 = rte_pktmbuf_append(m, (uint16_t)(rte_pktmbuf_tailroom(m) + 1));
222 GOTO_FAIL("Append should not succeed");
224 /* append some more data */
226 data2 = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN2);
228 GOTO_FAIL("Cannot append data");
229 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_DATA_LEN2)
230 GOTO_FAIL("Bad pkt length");
231 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_DATA_LEN2)
232 GOTO_FAIL("Bad data length");
233 if (!rte_pktmbuf_is_contiguous(m))
234 GOTO_FAIL("Buffer should be continuous");
236 /* trim data at the end of mbuf */
238 if (rte_pktmbuf_trim(m, MBUF_TEST_DATA_LEN2) < 0)
239 GOTO_FAIL("Cannot trim data");
240 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
241 GOTO_FAIL("Bad pkt length");
242 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
243 GOTO_FAIL("Bad data length");
244 if (!rte_pktmbuf_is_contiguous(m))
245 GOTO_FAIL("Buffer should be continuous");
247 /* this trim should fail */
249 if (rte_pktmbuf_trim(m, (uint16_t)(rte_pktmbuf_data_len(m) + 1)) == 0)
250 GOTO_FAIL("trim should not succeed");
252 /* prepend one header */
254 hdr = rte_pktmbuf_prepend(m, MBUF_TEST_HDR1_LEN);
256 GOTO_FAIL("Cannot prepend");
257 if (data - hdr != MBUF_TEST_HDR1_LEN)
258 GOTO_FAIL("Prepend failed");
259 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_HDR1_LEN)
260 GOTO_FAIL("Bad pkt length");
261 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_HDR1_LEN)
262 GOTO_FAIL("Bad data length");
263 if (!rte_pktmbuf_is_contiguous(m))
264 GOTO_FAIL("Buffer should be continuous");
265 memset(hdr, 0x55, MBUF_TEST_HDR1_LEN);
267 /* prepend another header */
269 hdr = rte_pktmbuf_prepend(m, MBUF_TEST_HDR2_LEN);
271 GOTO_FAIL("Cannot prepend");
272 if (data - hdr != MBUF_TEST_ALL_HDRS_LEN)
273 GOTO_FAIL("Prepend failed");
274 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_ALL_HDRS_LEN)
275 GOTO_FAIL("Bad pkt length");
276 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_ALL_HDRS_LEN)
277 GOTO_FAIL("Bad data length");
278 if (!rte_pktmbuf_is_contiguous(m))
279 GOTO_FAIL("Buffer should be continuous");
280 memset(hdr, 0x55, MBUF_TEST_HDR2_LEN);
282 rte_mbuf_sanity_check(m, 1);
283 rte_mbuf_sanity_check(m, 0);
284 rte_pktmbuf_dump(stdout, m, 0);
286 /* this prepend should fail */
288 hdr = rte_pktmbuf_prepend(m, (uint16_t)(rte_pktmbuf_headroom(m) + 1));
290 GOTO_FAIL("prepend should not succeed");
292 /* remove data at beginning of mbuf (adj) */
294 if (data != rte_pktmbuf_adj(m, MBUF_TEST_ALL_HDRS_LEN))
295 GOTO_FAIL("rte_pktmbuf_adj failed");
296 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
297 GOTO_FAIL("Bad pkt length");
298 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
299 GOTO_FAIL("Bad data length");
300 if (!rte_pktmbuf_is_contiguous(m))
301 GOTO_FAIL("Buffer should be continuous");
303 /* this adj should fail */
305 if (rte_pktmbuf_adj(m, (uint16_t)(rte_pktmbuf_data_len(m) + 1)) != NULL)
306 GOTO_FAIL("rte_pktmbuf_adj should not succeed");
310 if (!rte_pktmbuf_is_contiguous(m))
311 GOTO_FAIL("Buffer should be continuous");
313 for (i=0; i<MBUF_TEST_DATA_LEN; i++) {
315 GOTO_FAIL("Data corrupted at offset %u", i);
331 testclone_testupdate_testdetach(void)
333 struct rte_mbuf *m = NULL;
334 struct rte_mbuf *clone = NULL;
335 struct rte_mbuf *clone2 = NULL;
339 m = rte_pktmbuf_alloc(pktmbuf_pool);
341 GOTO_FAIL("ooops not allocating mbuf");
343 if (rte_pktmbuf_pkt_len(m) != 0)
344 GOTO_FAIL("Bad length");
346 rte_pktmbuf_append(m, sizeof(uint32_t));
347 data = rte_pktmbuf_mtod(m, uint32_t *);
350 /* clone the allocated mbuf */
351 clone = rte_pktmbuf_clone(m, pktmbuf_pool);
353 GOTO_FAIL("cannot clone data\n");
355 data = rte_pktmbuf_mtod(clone, uint32_t *);
356 if (*data != MAGIC_DATA)
357 GOTO_FAIL("invalid data in clone\n");
359 if (rte_mbuf_refcnt_read(m) != 2)
360 GOTO_FAIL("invalid refcnt in m\n");
363 rte_pktmbuf_free(clone);
366 /* same test with a chained mbuf */
367 m->next = rte_pktmbuf_alloc(pktmbuf_pool);
369 GOTO_FAIL("Next Pkt Null\n");
371 rte_pktmbuf_append(m->next, sizeof(uint32_t));
372 data = rte_pktmbuf_mtod(m->next, uint32_t *);
375 clone = rte_pktmbuf_clone(m, pktmbuf_pool);
377 GOTO_FAIL("cannot clone data\n");
379 data = rte_pktmbuf_mtod(clone, uint32_t *);
380 if (*data != MAGIC_DATA)
381 GOTO_FAIL("invalid data in clone\n");
383 data = rte_pktmbuf_mtod(clone->next, uint32_t *);
384 if (*data != MAGIC_DATA)
385 GOTO_FAIL("invalid data in clone->next\n");
387 if (rte_mbuf_refcnt_read(m) != 2)
388 GOTO_FAIL("invalid refcnt in m\n");
390 if (rte_mbuf_refcnt_read(m->next) != 2)
391 GOTO_FAIL("invalid refcnt in m->next\n");
393 /* try to clone the clone */
395 clone2 = rte_pktmbuf_clone(clone, pktmbuf_pool);
397 GOTO_FAIL("cannot clone the clone\n");
399 data = rte_pktmbuf_mtod(clone2, uint32_t *);
400 if (*data != MAGIC_DATA)
401 GOTO_FAIL("invalid data in clone2\n");
403 data = rte_pktmbuf_mtod(clone2->next, uint32_t *);
404 if (*data != MAGIC_DATA)
405 GOTO_FAIL("invalid data in clone2->next\n");
407 if (rte_mbuf_refcnt_read(m) != 3)
408 GOTO_FAIL("invalid refcnt in m\n");
410 if (rte_mbuf_refcnt_read(m->next) != 3)
411 GOTO_FAIL("invalid refcnt in m->next\n");
415 rte_pktmbuf_free(clone);
416 rte_pktmbuf_free(clone2);
421 printf("%s ok\n", __func__);
428 rte_pktmbuf_free(clone);
430 rte_pktmbuf_free(clone2);
435 test_attach_from_different_pool(void)
437 struct rte_mbuf *m = NULL;
438 struct rte_mbuf *clone = NULL;
439 struct rte_mbuf *clone2 = NULL;
440 char *data, *c_data, *c_data2;
443 m = rte_pktmbuf_alloc(pktmbuf_pool);
445 GOTO_FAIL("cannot allocate mbuf");
447 if (rte_pktmbuf_pkt_len(m) != 0)
448 GOTO_FAIL("Bad length");
450 data = rte_pktmbuf_mtod(m, char *);
452 /* allocate a new mbuf from the second pool, and attach it to the first
454 clone = rte_pktmbuf_alloc(pktmbuf_pool2);
456 GOTO_FAIL("cannot allocate mbuf from second pool\n");
458 /* check data room size and priv size, and erase priv */
459 if (rte_pktmbuf_data_room_size(clone->pool) != 0)
460 GOTO_FAIL("data room size should be 0\n");
461 if (rte_pktmbuf_priv_size(clone->pool) != MBUF2_PRIV_SIZE)
462 GOTO_FAIL("data room size should be %d\n", MBUF2_PRIV_SIZE);
463 memset(clone + 1, 0, MBUF2_PRIV_SIZE);
465 /* save data pointer to compare it after detach() */
466 c_data = rte_pktmbuf_mtod(clone, char *);
467 if (c_data != (char *)clone + sizeof(*clone) + MBUF2_PRIV_SIZE)
468 GOTO_FAIL("bad data pointer in clone");
469 if (rte_pktmbuf_headroom(clone) != 0)
470 GOTO_FAIL("bad headroom in clone");
472 rte_pktmbuf_attach(clone, m);
474 if (rte_pktmbuf_mtod(clone, char *) != data)
475 GOTO_FAIL("clone was not attached properly\n");
476 if (rte_pktmbuf_headroom(clone) != RTE_PKTMBUF_HEADROOM)
477 GOTO_FAIL("bad headroom in clone after attach");
478 if (rte_mbuf_refcnt_read(m) != 2)
479 GOTO_FAIL("invalid refcnt in m\n");
481 /* allocate a new mbuf from the second pool, and attach it to the first
483 clone2 = rte_pktmbuf_alloc(pktmbuf_pool2);
485 GOTO_FAIL("cannot allocate clone2 from second pool\n");
487 /* check data room size and priv size, and erase priv */
488 if (rte_pktmbuf_data_room_size(clone2->pool) != 0)
489 GOTO_FAIL("data room size should be 0\n");
490 if (rte_pktmbuf_priv_size(clone2->pool) != MBUF2_PRIV_SIZE)
491 GOTO_FAIL("data room size should be %d\n", MBUF2_PRIV_SIZE);
492 memset(clone2 + 1, 0, MBUF2_PRIV_SIZE);
494 /* save data pointer to compare it after detach() */
495 c_data2 = rte_pktmbuf_mtod(clone2, char *);
496 if (c_data2 != (char *)clone2 + sizeof(*clone2) + MBUF2_PRIV_SIZE)
497 GOTO_FAIL("bad data pointer in clone2");
498 if (rte_pktmbuf_headroom(clone2) != 0)
499 GOTO_FAIL("bad headroom in clone2");
501 rte_pktmbuf_attach(clone2, clone);
503 if (rte_pktmbuf_mtod(clone2, char *) != data)
504 GOTO_FAIL("clone2 was not attached properly\n");
505 if (rte_pktmbuf_headroom(clone2) != RTE_PKTMBUF_HEADROOM)
506 GOTO_FAIL("bad headroom in clone2 after attach");
507 if (rte_mbuf_refcnt_read(m) != 3)
508 GOTO_FAIL("invalid refcnt in m\n");
510 /* detach the clones */
511 rte_pktmbuf_detach(clone);
512 if (c_data != rte_pktmbuf_mtod(clone, char *))
513 GOTO_FAIL("clone was not detached properly\n");
515 rte_pktmbuf_detach(clone2);
516 if (c_data2 != rte_pktmbuf_mtod(clone2, char *))
517 GOTO_FAIL("clone2 was not detached properly\n");
519 /* free the clones and the initial mbuf */
520 rte_pktmbuf_free(clone2);
521 rte_pktmbuf_free(clone);
523 printf("%s ok\n", __func__);
530 rte_pktmbuf_free(clone);
532 rte_pktmbuf_free(clone2);
538 * test allocation and free of mbufs
541 test_pktmbuf_pool(void)
544 struct rte_mbuf *m[NB_MBUF];
547 for (i=0; i<NB_MBUF; i++)
550 /* alloc NB_MBUF mbufs */
551 for (i=0; i<NB_MBUF; i++) {
552 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
554 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
558 struct rte_mbuf *extra = NULL;
559 extra = rte_pktmbuf_alloc(pktmbuf_pool);
561 printf("Error pool not empty");
564 extra = rte_pktmbuf_clone(m[0], pktmbuf_pool);
566 printf("Error pool not empty");
570 for (i=0; i<NB_MBUF; i++) {
572 rte_pktmbuf_free(m[i]);
579 * test that the pointer to the data on a packet mbuf is set properly
582 test_pktmbuf_pool_ptr(void)
585 struct rte_mbuf *m[NB_MBUF];
588 for (i=0; i<NB_MBUF; i++)
591 /* alloc NB_MBUF mbufs */
592 for (i=0; i<NB_MBUF; i++) {
593 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
595 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
598 m[i]->data_off += 64;
602 for (i=0; i<NB_MBUF; i++) {
604 rte_pktmbuf_free(m[i]);
607 for (i=0; i<NB_MBUF; i++)
610 /* alloc NB_MBUF mbufs */
611 for (i=0; i<NB_MBUF; i++) {
612 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
614 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(void)
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 efficency, recomended to run with RTE_LIBRTE_MBUF_DEBUG defined.
674 #ifdef RTE_MBUF_REFCNT_ATOMIC
677 test_refcnt_slave(__attribute__((unused)) void *arg)
679 unsigned lcore, free;
682 lcore = rte_lcore_id();
683 printf("%s started at lcore %u\n", __func__, lcore);
686 while (refcnt_stop_slaves == 0) {
687 if (rte_ring_dequeue(refcnt_mbuf_ring, &mp) == 0) {
689 rte_pktmbuf_free((struct rte_mbuf *)mp);
693 refcnt_lcore[lcore] += free;
694 printf("%s finished at lcore %u, "
695 "number of freed mbufs: %u\n",
696 __func__, lcore, free);
701 test_refcnt_iter(unsigned lcore, unsigned iter)
704 unsigned i, n, tref, wn;
709 /* For each mbuf in the pool:
711 * - increment it's reference up to N+1,
712 * - enqueue it N times into the ring for slave cores to free.
714 for (i = 0, n = rte_mempool_count(refcnt_pool);
715 i != n && (m = rte_pktmbuf_alloc(refcnt_pool)) != NULL;
717 ref = RTE_MAX(rte_rand() % REFCNT_MAX_REF, 1UL);
719 if ((ref & 1) != 0) {
720 rte_pktmbuf_refcnt_update(m, ref);
722 rte_ring_enqueue(refcnt_mbuf_ring, m);
725 rte_pktmbuf_refcnt_update(m, 1);
726 rte_ring_enqueue(refcnt_mbuf_ring, m);
733 rte_panic("(lcore=%u, iter=%u): was able to allocate only "
734 "%u from %u mbufs\n", lcore, iter, i, n);
736 /* wait till slave lcores will consume all mbufs */
737 while (!rte_ring_empty(refcnt_mbuf_ring))
740 /* check that all mbufs are back into mempool by now */
741 for (wn = 0; wn != REFCNT_MAX_TIMEOUT; wn++) {
742 if ((i = rte_mempool_count(refcnt_pool)) == n) {
743 refcnt_lcore[lcore] += tref;
744 printf("%s(lcore=%u, iter=%u) completed, "
745 "%u references processed\n",
746 __func__, lcore, iter, tref);
752 rte_panic("(lcore=%u, iter=%u): after %us only "
753 "%u of %u mbufs left free\n", lcore, iter, wn, i, n);
757 test_refcnt_master(void)
761 lcore = rte_lcore_id();
762 printf("%s started at lcore %u\n", __func__, lcore);
764 for (i = 0; i != REFCNT_MAX_ITER; i++)
765 test_refcnt_iter(lcore, i);
767 refcnt_stop_slaves = 1;
770 printf("%s finished at lcore %u\n", __func__, lcore);
777 test_refcnt_mbuf(void)
779 #ifdef RTE_MBUF_REFCNT_ATOMIC
781 unsigned lnum, master, slave, tref;
784 if ((lnum = rte_lcore_count()) == 1) {
785 printf("skipping %s, number of lcores: %u is not enough\n",
790 printf("starting %s, at %u lcores\n", __func__, lnum);
792 /* create refcnt pool & ring if they don't exist */
794 if (refcnt_pool == NULL &&
795 (refcnt_pool = rte_pktmbuf_pool_create(
796 MAKE_STRING(refcnt_pool),
797 REFCNT_MBUF_NUM, 0, 0, 0,
798 SOCKET_ID_ANY)) == NULL) {
799 printf("%s: cannot allocate " MAKE_STRING(refcnt_pool) "\n",
804 if (refcnt_mbuf_ring == NULL &&
805 (refcnt_mbuf_ring = rte_ring_create("refcnt_mbuf_ring",
806 REFCNT_RING_SIZE, SOCKET_ID_ANY,
807 RING_F_SP_ENQ)) == NULL) {
808 printf("%s: cannot allocate " MAKE_STRING(refcnt_mbuf_ring)
813 refcnt_stop_slaves = 0;
814 memset(refcnt_lcore, 0, sizeof (refcnt_lcore));
816 rte_eal_mp_remote_launch(test_refcnt_slave, NULL, SKIP_MASTER);
818 test_refcnt_master();
820 rte_eal_mp_wait_lcore();
822 /* check that we porcessed all references */
824 master = rte_get_master_lcore();
826 RTE_LCORE_FOREACH_SLAVE(slave)
827 tref += refcnt_lcore[slave];
829 if (tref != refcnt_lcore[master])
830 rte_panic("refernced mbufs: %u, freed mbufs: %u\n",
831 tref, refcnt_lcore[master]);
833 rte_mempool_dump(stdout, refcnt_pool);
834 rte_ring_dump(stdout, refcnt_mbuf_ring);
841 #include <sys/wait.h>
843 /* use fork() to test mbuf errors panic */
845 verify_mbuf_check_panics(struct rte_mbuf *buf)
853 rte_mbuf_sanity_check(buf, 1); /* should panic */
854 exit(0); /* return normally if it doesn't panic */
856 printf("Fork Failed\n");
867 test_failing_mbuf_sanity_check(void)
869 struct rte_mbuf *buf;
870 struct rte_mbuf badbuf;
872 printf("Checking rte_mbuf_sanity_check for failure conditions\n");
874 /* get a good mbuf to use to make copies */
875 buf = rte_pktmbuf_alloc(pktmbuf_pool);
878 printf("Checking good mbuf initially\n");
879 if (verify_mbuf_check_panics(buf) != -1)
882 printf("Now checking for error conditions\n");
884 if (verify_mbuf_check_panics(NULL)) {
885 printf("Error with NULL mbuf test\n");
891 if (verify_mbuf_check_panics(&badbuf)) {
892 printf("Error with bad-pool mbuf test\n");
897 badbuf.buf_physaddr = 0;
898 if (verify_mbuf_check_panics(&badbuf)) {
899 printf("Error with bad-physaddr mbuf test\n");
904 badbuf.buf_addr = NULL;
905 if (verify_mbuf_check_panics(&badbuf)) {
906 printf("Error with bad-addr mbuf test\n");
912 if (verify_mbuf_check_panics(&badbuf)) {
913 printf("Error with bad-refcnt(0) mbuf test\n");
918 badbuf.refcnt = UINT16_MAX;
919 if (verify_mbuf_check_panics(&badbuf)) {
920 printf("Error with bad-refcnt(MAX) mbuf test\n");
931 RTE_BUILD_BUG_ON(sizeof(struct rte_mbuf) != RTE_CACHE_LINE_SIZE * 2);
933 /* create pktmbuf pool if it does not exist */
934 if (pktmbuf_pool == NULL) {
935 pktmbuf_pool = rte_pktmbuf_pool_create("test_pktmbuf_pool",
936 NB_MBUF, 32, 0, MBUF_DATA_SIZE, SOCKET_ID_ANY);
939 if (pktmbuf_pool == NULL) {
940 printf("cannot allocate mbuf pool\n");
944 /* create a specific pktmbuf pool with a priv_size != 0 and no data
946 if (pktmbuf_pool2 == NULL) {
947 pktmbuf_pool2 = rte_pktmbuf_pool_create("test_pktmbuf_pool2",
948 NB_MBUF, 32, MBUF2_PRIV_SIZE, 0, SOCKET_ID_ANY);
951 if (pktmbuf_pool2 == NULL) {
952 printf("cannot allocate mbuf pool\n");
956 /* test multiple mbuf alloc */
957 if (test_pktmbuf_pool() < 0) {
958 printf("test_mbuf_pool() failed\n");
962 /* do it another time to check that all mbufs were freed */
963 if (test_pktmbuf_pool() < 0) {
964 printf("test_mbuf_pool() failed (2)\n");
968 /* test that the pointer to the data on a packet mbuf is set properly */
969 if (test_pktmbuf_pool_ptr() < 0) {
970 printf("test_pktmbuf_pool_ptr() failed\n");
974 /* test data manipulation in mbuf */
975 if (test_one_pktmbuf() < 0) {
976 printf("test_one_mbuf() failed\n");
982 * do it another time, to check that allocation reinitialize
985 if (test_one_pktmbuf() < 0) {
986 printf("test_one_mbuf() failed (2)\n");
990 if (test_pktmbuf_with_non_ascii_data() < 0) {
991 printf("test_pktmbuf_with_non_ascii_data() failed\n");
995 /* test free pktmbuf segment one by one */
996 if (test_pktmbuf_free_segment() < 0) {
997 printf("test_pktmbuf_free_segment() failed.\n");
1001 if (testclone_testupdate_testdetach()<0){
1002 printf("testclone_and_testupdate() failed \n");
1006 if (test_attach_from_different_pool() < 0) {
1007 printf("test_attach_from_different_pool() failed\n");
1011 if (test_refcnt_mbuf()<0){
1012 printf("test_refcnt_mbuf() failed \n");
1016 if (test_failing_mbuf_sanity_check() < 0) {
1017 printf("test_failing_mbuf_sanity_check() failed\n");
1023 static struct test_command mbuf_cmd = {
1024 .command = "mbuf_autotest",
1025 .callback = test_mbuf,
1027 REGISTER_TEST_COMMAND(mbuf_cmd);