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;
336 unaligned_uint32_t *data;
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, unaligned_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, unaligned_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, unaligned_uint32_t *);
375 clone = rte_pktmbuf_clone(m, pktmbuf_pool);
377 GOTO_FAIL("cannot clone data\n");
379 data = rte_pktmbuf_mtod(clone, unaligned_uint32_t *);
380 if (*data != MAGIC_DATA)
381 GOTO_FAIL("invalid data in clone\n");
383 data = rte_pktmbuf_mtod(clone->next, unaligned_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, unaligned_uint32_t *);
400 if (*data != MAGIC_DATA)
401 GOTO_FAIL("invalid data in clone2\n");
403 data = rte_pktmbuf_mtod(clone2->next, unaligned_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");
514 if (rte_mbuf_refcnt_read(m) != 2)
515 GOTO_FAIL("invalid refcnt in m\n");
517 rte_pktmbuf_detach(clone2);
518 if (c_data2 != rte_pktmbuf_mtod(clone2, char *))
519 GOTO_FAIL("clone2 was not detached properly\n");
520 if (rte_mbuf_refcnt_read(m) != 1)
521 GOTO_FAIL("invalid refcnt in m\n");
523 /* free the clones and the initial mbuf */
524 rte_pktmbuf_free(clone2);
525 rte_pktmbuf_free(clone);
527 printf("%s ok\n", __func__);
534 rte_pktmbuf_free(clone);
536 rte_pktmbuf_free(clone2);
542 * test allocation and free of mbufs
545 test_pktmbuf_pool(void)
548 struct rte_mbuf *m[NB_MBUF];
551 for (i=0; i<NB_MBUF; i++)
554 /* alloc NB_MBUF mbufs */
555 for (i=0; i<NB_MBUF; i++) {
556 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
558 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
562 struct rte_mbuf *extra = NULL;
563 extra = rte_pktmbuf_alloc(pktmbuf_pool);
565 printf("Error pool not empty");
568 extra = rte_pktmbuf_clone(m[0], pktmbuf_pool);
570 printf("Error pool not empty");
574 for (i=0; i<NB_MBUF; i++) {
576 rte_pktmbuf_free(m[i]);
583 * test that the pointer to the data on a packet mbuf is set properly
586 test_pktmbuf_pool_ptr(void)
589 struct rte_mbuf *m[NB_MBUF];
592 for (i=0; i<NB_MBUF; i++)
595 /* alloc NB_MBUF mbufs */
596 for (i=0; i<NB_MBUF; i++) {
597 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
599 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
603 m[i]->data_off += 64;
607 for (i=0; i<NB_MBUF; i++) {
609 rte_pktmbuf_free(m[i]);
612 for (i=0; i<NB_MBUF; i++)
615 /* alloc NB_MBUF mbufs */
616 for (i=0; i<NB_MBUF; i++) {
617 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
619 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
623 if (m[i]->data_off != RTE_PKTMBUF_HEADROOM) {
624 printf("invalid data_off\n");
630 for (i=0; i<NB_MBUF; i++) {
632 rte_pktmbuf_free(m[i]);
639 test_pktmbuf_free_segment(void)
642 struct rte_mbuf *m[NB_MBUF];
645 for (i=0; i<NB_MBUF; i++)
648 /* alloc NB_MBUF mbufs */
649 for (i=0; i<NB_MBUF; i++) {
650 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
652 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
658 for (i=0; i<NB_MBUF; i++) {
660 struct rte_mbuf *mb, *mt;
666 rte_pktmbuf_free_seg(mt);
675 * Stress test for rte_mbuf atomic refcnt.
676 * Implies that RTE_MBUF_REFCNT_ATOMIC is defined.
677 * For more efficency, recomended to run with RTE_LIBRTE_MBUF_DEBUG defined.
680 #ifdef RTE_MBUF_REFCNT_ATOMIC
683 test_refcnt_slave(__attribute__((unused)) void *arg)
685 unsigned lcore, free;
688 lcore = rte_lcore_id();
689 printf("%s started at lcore %u\n", __func__, lcore);
692 while (refcnt_stop_slaves == 0) {
693 if (rte_ring_dequeue(refcnt_mbuf_ring, &mp) == 0) {
695 rte_pktmbuf_free((struct rte_mbuf *)mp);
699 refcnt_lcore[lcore] += free;
700 printf("%s finished at lcore %u, "
701 "number of freed mbufs: %u\n",
702 __func__, lcore, free);
707 test_refcnt_iter(unsigned lcore, unsigned iter)
710 unsigned i, n, tref, wn;
715 /* For each mbuf in the pool:
717 * - increment it's reference up to N+1,
718 * - enqueue it N times into the ring for slave cores to free.
720 for (i = 0, n = rte_mempool_avail_count(refcnt_pool);
721 i != n && (m = rte_pktmbuf_alloc(refcnt_pool)) != NULL;
723 ref = RTE_MAX(rte_rand() % REFCNT_MAX_REF, 1UL);
725 if ((ref & 1) != 0) {
726 rte_pktmbuf_refcnt_update(m, ref);
728 rte_ring_enqueue(refcnt_mbuf_ring, m);
731 rte_pktmbuf_refcnt_update(m, 1);
732 rte_ring_enqueue(refcnt_mbuf_ring, m);
739 rte_panic("(lcore=%u, iter=%u): was able to allocate only "
740 "%u from %u mbufs\n", lcore, iter, i, n);
742 /* wait till slave lcores will consume all mbufs */
743 while (!rte_ring_empty(refcnt_mbuf_ring))
746 /* check that all mbufs are back into mempool by now */
747 for (wn = 0; wn != REFCNT_MAX_TIMEOUT; wn++) {
748 if ((i = rte_mempool_avail_count(refcnt_pool)) == n) {
749 refcnt_lcore[lcore] += tref;
750 printf("%s(lcore=%u, iter=%u) completed, "
751 "%u references processed\n",
752 __func__, lcore, iter, tref);
758 rte_panic("(lcore=%u, iter=%u): after %us only "
759 "%u of %u mbufs left free\n", lcore, iter, wn, i, n);
763 test_refcnt_master(void)
767 lcore = rte_lcore_id();
768 printf("%s started at lcore %u\n", __func__, lcore);
770 for (i = 0; i != REFCNT_MAX_ITER; i++)
771 test_refcnt_iter(lcore, i);
773 refcnt_stop_slaves = 1;
776 printf("%s finished at lcore %u\n", __func__, lcore);
783 test_refcnt_mbuf(void)
785 #ifdef RTE_MBUF_REFCNT_ATOMIC
787 unsigned lnum, master, slave, tref;
790 if ((lnum = rte_lcore_count()) == 1) {
791 printf("skipping %s, number of lcores: %u is not enough\n",
796 printf("starting %s, at %u lcores\n", __func__, lnum);
798 /* create refcnt pool & ring if they don't exist */
800 if (refcnt_pool == NULL &&
801 (refcnt_pool = rte_pktmbuf_pool_create(
802 MAKE_STRING(refcnt_pool),
803 REFCNT_MBUF_NUM, 0, 0, 0,
804 SOCKET_ID_ANY)) == NULL) {
805 printf("%s: cannot allocate " MAKE_STRING(refcnt_pool) "\n",
810 if (refcnt_mbuf_ring == NULL &&
811 (refcnt_mbuf_ring = rte_ring_create("refcnt_mbuf_ring",
812 rte_align32pow2(REFCNT_RING_SIZE), SOCKET_ID_ANY,
813 RING_F_SP_ENQ)) == NULL) {
814 printf("%s: cannot allocate " MAKE_STRING(refcnt_mbuf_ring)
819 refcnt_stop_slaves = 0;
820 memset(refcnt_lcore, 0, sizeof (refcnt_lcore));
822 rte_eal_mp_remote_launch(test_refcnt_slave, NULL, SKIP_MASTER);
824 test_refcnt_master();
826 rte_eal_mp_wait_lcore();
828 /* check that we porcessed all references */
830 master = rte_get_master_lcore();
832 RTE_LCORE_FOREACH_SLAVE(slave)
833 tref += refcnt_lcore[slave];
835 if (tref != refcnt_lcore[master])
836 rte_panic("refernced mbufs: %u, freed mbufs: %u\n",
837 tref, refcnt_lcore[master]);
839 rte_mempool_dump(stdout, refcnt_pool);
840 rte_ring_dump(stdout, refcnt_mbuf_ring);
847 #include <sys/wait.h>
849 /* use fork() to test mbuf errors panic */
851 verify_mbuf_check_panics(struct rte_mbuf *buf)
859 rte_mbuf_sanity_check(buf, 1); /* should panic */
860 exit(0); /* return normally if it doesn't panic */
862 printf("Fork Failed\n");
873 test_failing_mbuf_sanity_check(void)
875 struct rte_mbuf *buf;
876 struct rte_mbuf badbuf;
878 printf("Checking rte_mbuf_sanity_check for failure conditions\n");
880 /* get a good mbuf to use to make copies */
881 buf = rte_pktmbuf_alloc(pktmbuf_pool);
884 printf("Checking good mbuf initially\n");
885 if (verify_mbuf_check_panics(buf) != -1)
888 printf("Now checking for error conditions\n");
890 if (verify_mbuf_check_panics(NULL)) {
891 printf("Error with NULL mbuf test\n");
897 if (verify_mbuf_check_panics(&badbuf)) {
898 printf("Error with bad-pool mbuf test\n");
903 badbuf.buf_physaddr = 0;
904 if (verify_mbuf_check_panics(&badbuf)) {
905 printf("Error with bad-physaddr mbuf test\n");
910 badbuf.buf_addr = NULL;
911 if (verify_mbuf_check_panics(&badbuf)) {
912 printf("Error with bad-addr mbuf test\n");
918 if (verify_mbuf_check_panics(&badbuf)) {
919 printf("Error with bad-refcnt(0) mbuf test\n");
924 badbuf.refcnt = UINT16_MAX;
925 if (verify_mbuf_check_panics(&badbuf)) {
926 printf("Error with bad-refcnt(MAX) mbuf test\n");
934 test_mbuf_linearize(int pkt_len, int nb_segs) {
936 struct rte_mbuf *m = NULL, *mbuf = NULL;
944 printf("Packet size must be 1 or more (is %d)\n", pkt_len);
949 printf("Number of segments must be 1 or more (is %d)\n",
954 seg_len = pkt_len / nb_segs;
960 /* Create chained mbuf_src and fill it generated data */
961 for (seg = 0; remain > 0; seg++) {
963 m = rte_pktmbuf_alloc(pktmbuf_pool);
965 printf("Cannot create segment for source mbuf");
969 /* Make sure if tailroom is zeroed */
970 memset(rte_pktmbuf_mtod(m, uint8_t *), 0,
971 rte_pktmbuf_tailroom(m));
974 if (data_len > seg_len)
977 data = (uint8_t *)rte_pktmbuf_append(m, data_len);
979 printf("Cannot append %d bytes to the mbuf\n",
984 for (i = 0; i < data_len; i++)
985 data[i] = (seg * seg_len + i) % 0x0ff;
990 rte_pktmbuf_chain(mbuf, m);
995 /* Create destination buffer to store coalesced data */
996 if (rte_pktmbuf_linearize(mbuf)) {
997 printf("Mbuf linearization failed\n");
1001 if (!rte_pktmbuf_is_contiguous(mbuf)) {
1002 printf("Source buffer should be contiguous after "
1007 data = rte_pktmbuf_mtod(mbuf, uint8_t *);
1009 for (i = 0; i < pkt_len; i++)
1010 if (data[i] != (i % 0x0ff)) {
1011 printf("Incorrect data in linearized mbuf\n");
1015 rte_pktmbuf_free(mbuf);
1020 rte_pktmbuf_free(mbuf);
1025 test_mbuf_linearize_check(void)
1027 struct test_mbuf_array {
1039 printf("Test mbuf linearize API\n");
1041 for (i = 0; i < RTE_DIM(mbuf_array); i++)
1042 if (test_mbuf_linearize(mbuf_array[i].size,
1043 mbuf_array[i].nb_segs)) {
1044 printf("Test failed for %d, %d\n", mbuf_array[i].size,
1045 mbuf_array[i].nb_segs);
1055 RTE_BUILD_BUG_ON(sizeof(struct rte_mbuf) != RTE_CACHE_LINE_MIN_SIZE * 2);
1057 /* create pktmbuf pool if it does not exist */
1058 if (pktmbuf_pool == NULL) {
1059 pktmbuf_pool = rte_pktmbuf_pool_create("test_pktmbuf_pool",
1060 NB_MBUF, 32, 0, MBUF_DATA_SIZE, SOCKET_ID_ANY);
1063 if (pktmbuf_pool == NULL) {
1064 printf("cannot allocate mbuf pool\n");
1068 /* create a specific pktmbuf pool with a priv_size != 0 and no data
1070 if (pktmbuf_pool2 == NULL) {
1071 pktmbuf_pool2 = rte_pktmbuf_pool_create("test_pktmbuf_pool2",
1072 NB_MBUF, 32, MBUF2_PRIV_SIZE, 0, SOCKET_ID_ANY);
1075 if (pktmbuf_pool2 == NULL) {
1076 printf("cannot allocate mbuf pool\n");
1080 /* test multiple mbuf alloc */
1081 if (test_pktmbuf_pool() < 0) {
1082 printf("test_mbuf_pool() failed\n");
1086 /* do it another time to check that all mbufs were freed */
1087 if (test_pktmbuf_pool() < 0) {
1088 printf("test_mbuf_pool() failed (2)\n");
1092 /* test that the pointer to the data on a packet mbuf is set properly */
1093 if (test_pktmbuf_pool_ptr() < 0) {
1094 printf("test_pktmbuf_pool_ptr() failed\n");
1098 /* test data manipulation in mbuf */
1099 if (test_one_pktmbuf() < 0) {
1100 printf("test_one_mbuf() failed\n");
1106 * do it another time, to check that allocation reinitialize
1107 * the mbuf correctly
1109 if (test_one_pktmbuf() < 0) {
1110 printf("test_one_mbuf() failed (2)\n");
1114 if (test_pktmbuf_with_non_ascii_data() < 0) {
1115 printf("test_pktmbuf_with_non_ascii_data() failed\n");
1119 /* test free pktmbuf segment one by one */
1120 if (test_pktmbuf_free_segment() < 0) {
1121 printf("test_pktmbuf_free_segment() failed.\n");
1125 if (testclone_testupdate_testdetach()<0){
1126 printf("testclone_and_testupdate() failed \n");
1130 if (test_attach_from_different_pool() < 0) {
1131 printf("test_attach_from_different_pool() failed\n");
1135 if (test_refcnt_mbuf()<0){
1136 printf("test_refcnt_mbuf() failed \n");
1140 if (test_failing_mbuf_sanity_check() < 0) {
1141 printf("test_failing_mbuf_sanity_check() failed\n");
1145 if (test_mbuf_linearize_check() < 0) {
1146 printf("test_mbuf_linearize_check() failed\n");
1152 REGISTER_TEST_COMMAND(mbuf_autotest, test_mbuf);