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_memzone.h>
49 #include <rte_launch.h>
51 #include <rte_per_lcore.h>
52 #include <rte_lcore.h>
53 #include <rte_atomic.h>
54 #include <rte_branch_prediction.h>
56 #include <rte_mempool.h>
58 #include <rte_random.h>
59 #include <rte_cycles.h>
63 #define MBUF_DATA_SIZE 2048
65 #define MBUF_TEST_DATA_LEN 1464
66 #define MBUF_TEST_DATA_LEN2 50
67 #define MBUF_TEST_HDR1_LEN 20
68 #define MBUF_TEST_HDR2_LEN 30
69 #define MBUF_TEST_ALL_HDRS_LEN (MBUF_TEST_HDR1_LEN+MBUF_TEST_HDR2_LEN)
71 /* size of private data for mbuf in pktmbuf_pool2 */
72 #define MBUF2_PRIV_SIZE 128
74 #define REFCNT_MAX_ITER 64
75 #define REFCNT_MAX_TIMEOUT 10
76 #define REFCNT_MAX_REF (RTE_MAX_LCORE)
77 #define REFCNT_MBUF_NUM 64
78 #define REFCNT_RING_SIZE (REFCNT_MBUF_NUM * REFCNT_MAX_REF)
80 #define MAGIC_DATA 0x42424242
82 #define MAKE_STRING(x) # x
84 #ifdef RTE_MBUF_REFCNT_ATOMIC
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.
125 * #. Test packet cloning
126 * - Clone a mbuf and verify the data
127 * - Clone the cloned mbuf and verify the data
128 * - Attach a mbuf to another that does not have the same priv_size.
131 #define GOTO_FAIL(str, ...) do { \
132 printf("mbuf test FAILED (l.%d): <" str ">\n", \
133 __LINE__, ##__VA_ARGS__); \
138 * test data manipulation in mbuf with non-ascii data
141 test_pktmbuf_with_non_ascii_data(struct rte_mempool *pktmbuf_pool)
143 struct rte_mbuf *m = NULL;
146 m = rte_pktmbuf_alloc(pktmbuf_pool);
148 GOTO_FAIL("Cannot allocate mbuf");
149 if (rte_pktmbuf_pkt_len(m) != 0)
150 GOTO_FAIL("Bad length");
152 data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN);
154 GOTO_FAIL("Cannot append data");
155 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
156 GOTO_FAIL("Bad pkt length");
157 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
158 GOTO_FAIL("Bad data length");
159 memset(data, 0xff, rte_pktmbuf_pkt_len(m));
160 if (!rte_pktmbuf_is_contiguous(m))
161 GOTO_FAIL("Buffer should be continuous");
162 rte_pktmbuf_dump(stdout, m, MBUF_TEST_DATA_LEN);
176 * test data manipulation in mbuf
179 test_one_pktmbuf(struct rte_mempool *pktmbuf_pool)
181 struct rte_mbuf *m = NULL;
182 char *data, *data2, *hdr;
185 printf("Test pktmbuf API\n");
189 m = rte_pktmbuf_alloc(pktmbuf_pool);
191 GOTO_FAIL("Cannot allocate mbuf");
192 if (rte_pktmbuf_pkt_len(m) != 0)
193 GOTO_FAIL("Bad length");
195 rte_pktmbuf_dump(stdout, m, 0);
199 data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN);
201 GOTO_FAIL("Cannot append data");
202 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
203 GOTO_FAIL("Bad pkt length");
204 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
205 GOTO_FAIL("Bad data length");
206 memset(data, 0x66, rte_pktmbuf_pkt_len(m));
207 if (!rte_pktmbuf_is_contiguous(m))
208 GOTO_FAIL("Buffer should be continuous");
209 rte_pktmbuf_dump(stdout, m, MBUF_TEST_DATA_LEN);
210 rte_pktmbuf_dump(stdout, m, 2*MBUF_TEST_DATA_LEN);
212 /* this append should fail */
214 data2 = rte_pktmbuf_append(m, (uint16_t)(rte_pktmbuf_tailroom(m) + 1));
216 GOTO_FAIL("Append should not succeed");
218 /* append some more data */
220 data2 = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN2);
222 GOTO_FAIL("Cannot append data");
223 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_DATA_LEN2)
224 GOTO_FAIL("Bad pkt length");
225 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_DATA_LEN2)
226 GOTO_FAIL("Bad data length");
227 if (!rte_pktmbuf_is_contiguous(m))
228 GOTO_FAIL("Buffer should be continuous");
230 /* trim data at the end of mbuf */
232 if (rte_pktmbuf_trim(m, MBUF_TEST_DATA_LEN2) < 0)
233 GOTO_FAIL("Cannot trim data");
234 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
235 GOTO_FAIL("Bad pkt length");
236 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
237 GOTO_FAIL("Bad data length");
238 if (!rte_pktmbuf_is_contiguous(m))
239 GOTO_FAIL("Buffer should be continuous");
241 /* this trim should fail */
243 if (rte_pktmbuf_trim(m, (uint16_t)(rte_pktmbuf_data_len(m) + 1)) == 0)
244 GOTO_FAIL("trim should not succeed");
246 /* prepend one header */
248 hdr = rte_pktmbuf_prepend(m, MBUF_TEST_HDR1_LEN);
250 GOTO_FAIL("Cannot prepend");
251 if (data - hdr != MBUF_TEST_HDR1_LEN)
252 GOTO_FAIL("Prepend failed");
253 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_HDR1_LEN)
254 GOTO_FAIL("Bad pkt length");
255 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_HDR1_LEN)
256 GOTO_FAIL("Bad data length");
257 if (!rte_pktmbuf_is_contiguous(m))
258 GOTO_FAIL("Buffer should be continuous");
259 memset(hdr, 0x55, MBUF_TEST_HDR1_LEN);
261 /* prepend another header */
263 hdr = rte_pktmbuf_prepend(m, MBUF_TEST_HDR2_LEN);
265 GOTO_FAIL("Cannot prepend");
266 if (data - hdr != MBUF_TEST_ALL_HDRS_LEN)
267 GOTO_FAIL("Prepend failed");
268 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_ALL_HDRS_LEN)
269 GOTO_FAIL("Bad pkt length");
270 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_ALL_HDRS_LEN)
271 GOTO_FAIL("Bad data length");
272 if (!rte_pktmbuf_is_contiguous(m))
273 GOTO_FAIL("Buffer should be continuous");
274 memset(hdr, 0x55, MBUF_TEST_HDR2_LEN);
276 rte_mbuf_sanity_check(m, 1);
277 rte_mbuf_sanity_check(m, 0);
278 rte_pktmbuf_dump(stdout, m, 0);
280 /* this prepend should fail */
282 hdr = rte_pktmbuf_prepend(m, (uint16_t)(rte_pktmbuf_headroom(m) + 1));
284 GOTO_FAIL("prepend should not succeed");
286 /* remove data at beginning of mbuf (adj) */
288 if (data != rte_pktmbuf_adj(m, MBUF_TEST_ALL_HDRS_LEN))
289 GOTO_FAIL("rte_pktmbuf_adj failed");
290 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
291 GOTO_FAIL("Bad pkt length");
292 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
293 GOTO_FAIL("Bad data length");
294 if (!rte_pktmbuf_is_contiguous(m))
295 GOTO_FAIL("Buffer should be continuous");
297 /* this adj should fail */
299 if (rte_pktmbuf_adj(m, (uint16_t)(rte_pktmbuf_data_len(m) + 1)) != NULL)
300 GOTO_FAIL("rte_pktmbuf_adj should not succeed");
304 if (!rte_pktmbuf_is_contiguous(m))
305 GOTO_FAIL("Buffer should be continuous");
307 for (i=0; i<MBUF_TEST_DATA_LEN; i++) {
309 GOTO_FAIL("Data corrupted at offset %u", i);
325 testclone_testupdate_testdetach(struct rte_mempool *pktmbuf_pool)
327 struct rte_mbuf *m = NULL;
328 struct rte_mbuf *clone = NULL;
329 struct rte_mbuf *clone2 = NULL;
330 unaligned_uint32_t *data;
333 m = rte_pktmbuf_alloc(pktmbuf_pool);
335 GOTO_FAIL("ooops not allocating mbuf");
337 if (rte_pktmbuf_pkt_len(m) != 0)
338 GOTO_FAIL("Bad length");
340 rte_pktmbuf_append(m, sizeof(uint32_t));
341 data = rte_pktmbuf_mtod(m, unaligned_uint32_t *);
344 /* clone the allocated mbuf */
345 clone = rte_pktmbuf_clone(m, pktmbuf_pool);
347 GOTO_FAIL("cannot clone data\n");
349 data = rte_pktmbuf_mtod(clone, unaligned_uint32_t *);
350 if (*data != MAGIC_DATA)
351 GOTO_FAIL("invalid data in clone\n");
353 if (rte_mbuf_refcnt_read(m) != 2)
354 GOTO_FAIL("invalid refcnt in m\n");
357 rte_pktmbuf_free(clone);
360 /* same test with a chained mbuf */
361 m->next = rte_pktmbuf_alloc(pktmbuf_pool);
363 GOTO_FAIL("Next Pkt Null\n");
365 rte_pktmbuf_append(m->next, sizeof(uint32_t));
366 data = rte_pktmbuf_mtod(m->next, unaligned_uint32_t *);
369 clone = rte_pktmbuf_clone(m, pktmbuf_pool);
371 GOTO_FAIL("cannot clone data\n");
373 data = rte_pktmbuf_mtod(clone, unaligned_uint32_t *);
374 if (*data != MAGIC_DATA)
375 GOTO_FAIL("invalid data in clone\n");
377 data = rte_pktmbuf_mtod(clone->next, unaligned_uint32_t *);
378 if (*data != MAGIC_DATA)
379 GOTO_FAIL("invalid data in clone->next\n");
381 if (rte_mbuf_refcnt_read(m) != 2)
382 GOTO_FAIL("invalid refcnt in m\n");
384 if (rte_mbuf_refcnt_read(m->next) != 2)
385 GOTO_FAIL("invalid refcnt in m->next\n");
387 /* try to clone the clone */
389 clone2 = rte_pktmbuf_clone(clone, pktmbuf_pool);
391 GOTO_FAIL("cannot clone the clone\n");
393 data = rte_pktmbuf_mtod(clone2, unaligned_uint32_t *);
394 if (*data != MAGIC_DATA)
395 GOTO_FAIL("invalid data in clone2\n");
397 data = rte_pktmbuf_mtod(clone2->next, unaligned_uint32_t *);
398 if (*data != MAGIC_DATA)
399 GOTO_FAIL("invalid data in clone2->next\n");
401 if (rte_mbuf_refcnt_read(m) != 3)
402 GOTO_FAIL("invalid refcnt in m\n");
404 if (rte_mbuf_refcnt_read(m->next) != 3)
405 GOTO_FAIL("invalid refcnt in m->next\n");
409 rte_pktmbuf_free(clone);
410 rte_pktmbuf_free(clone2);
415 printf("%s ok\n", __func__);
422 rte_pktmbuf_free(clone);
424 rte_pktmbuf_free(clone2);
429 test_attach_from_different_pool(struct rte_mempool *pktmbuf_pool,
430 struct rte_mempool *pktmbuf_pool2)
432 struct rte_mbuf *m = NULL;
433 struct rte_mbuf *clone = NULL;
434 struct rte_mbuf *clone2 = NULL;
435 char *data, *c_data, *c_data2;
438 m = rte_pktmbuf_alloc(pktmbuf_pool);
440 GOTO_FAIL("cannot allocate mbuf");
442 if (rte_pktmbuf_pkt_len(m) != 0)
443 GOTO_FAIL("Bad length");
445 data = rte_pktmbuf_mtod(m, char *);
447 /* allocate a new mbuf from the second pool, and attach it to the first
449 clone = rte_pktmbuf_alloc(pktmbuf_pool2);
451 GOTO_FAIL("cannot allocate mbuf from second pool\n");
453 /* check data room size and priv size, and erase priv */
454 if (rte_pktmbuf_data_room_size(clone->pool) != 0)
455 GOTO_FAIL("data room size should be 0\n");
456 if (rte_pktmbuf_priv_size(clone->pool) != MBUF2_PRIV_SIZE)
457 GOTO_FAIL("data room size should be %d\n", MBUF2_PRIV_SIZE);
458 memset(clone + 1, 0, MBUF2_PRIV_SIZE);
460 /* save data pointer to compare it after detach() */
461 c_data = rte_pktmbuf_mtod(clone, char *);
462 if (c_data != (char *)clone + sizeof(*clone) + MBUF2_PRIV_SIZE)
463 GOTO_FAIL("bad data pointer in clone");
464 if (rte_pktmbuf_headroom(clone) != 0)
465 GOTO_FAIL("bad headroom in clone");
467 rte_pktmbuf_attach(clone, m);
469 if (rte_pktmbuf_mtod(clone, char *) != data)
470 GOTO_FAIL("clone was not attached properly\n");
471 if (rte_pktmbuf_headroom(clone) != RTE_PKTMBUF_HEADROOM)
472 GOTO_FAIL("bad headroom in clone after attach");
473 if (rte_mbuf_refcnt_read(m) != 2)
474 GOTO_FAIL("invalid refcnt in m\n");
476 /* allocate a new mbuf from the second pool, and attach it to the first
478 clone2 = rte_pktmbuf_alloc(pktmbuf_pool2);
480 GOTO_FAIL("cannot allocate clone2 from second pool\n");
482 /* check data room size and priv size, and erase priv */
483 if (rte_pktmbuf_data_room_size(clone2->pool) != 0)
484 GOTO_FAIL("data room size should be 0\n");
485 if (rte_pktmbuf_priv_size(clone2->pool) != MBUF2_PRIV_SIZE)
486 GOTO_FAIL("data room size should be %d\n", MBUF2_PRIV_SIZE);
487 memset(clone2 + 1, 0, MBUF2_PRIV_SIZE);
489 /* save data pointer to compare it after detach() */
490 c_data2 = rte_pktmbuf_mtod(clone2, char *);
491 if (c_data2 != (char *)clone2 + sizeof(*clone2) + MBUF2_PRIV_SIZE)
492 GOTO_FAIL("bad data pointer in clone2");
493 if (rte_pktmbuf_headroom(clone2) != 0)
494 GOTO_FAIL("bad headroom in clone2");
496 rte_pktmbuf_attach(clone2, clone);
498 if (rte_pktmbuf_mtod(clone2, char *) != data)
499 GOTO_FAIL("clone2 was not attached properly\n");
500 if (rte_pktmbuf_headroom(clone2) != RTE_PKTMBUF_HEADROOM)
501 GOTO_FAIL("bad headroom in clone2 after attach");
502 if (rte_mbuf_refcnt_read(m) != 3)
503 GOTO_FAIL("invalid refcnt in m\n");
505 /* detach the clones */
506 rte_pktmbuf_detach(clone);
507 if (c_data != rte_pktmbuf_mtod(clone, char *))
508 GOTO_FAIL("clone was not detached properly\n");
509 if (rte_mbuf_refcnt_read(m) != 2)
510 GOTO_FAIL("invalid refcnt in m\n");
512 rte_pktmbuf_detach(clone2);
513 if (c_data2 != rte_pktmbuf_mtod(clone2, char *))
514 GOTO_FAIL("clone2 was not detached properly\n");
515 if (rte_mbuf_refcnt_read(m) != 1)
516 GOTO_FAIL("invalid refcnt in m\n");
518 /* free the clones and the initial mbuf */
519 rte_pktmbuf_free(clone2);
520 rte_pktmbuf_free(clone);
522 printf("%s ok\n", __func__);
529 rte_pktmbuf_free(clone);
531 rte_pktmbuf_free(clone2);
537 * test allocation and free of mbufs
540 test_pktmbuf_pool(struct rte_mempool *pktmbuf_pool)
543 struct rte_mbuf *m[NB_MBUF];
546 for (i=0; i<NB_MBUF; i++)
549 /* alloc NB_MBUF mbufs */
550 for (i=0; i<NB_MBUF; i++) {
551 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
553 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
557 struct rte_mbuf *extra = NULL;
558 extra = rte_pktmbuf_alloc(pktmbuf_pool);
560 printf("Error pool not empty");
563 extra = rte_pktmbuf_clone(m[0], pktmbuf_pool);
565 printf("Error pool not empty");
569 for (i=0; i<NB_MBUF; i++) {
571 rte_pktmbuf_free(m[i]);
578 * test that the pointer to the data on a packet mbuf is set properly
581 test_pktmbuf_pool_ptr(struct rte_mempool *pktmbuf_pool)
584 struct rte_mbuf *m[NB_MBUF];
587 for (i=0; i<NB_MBUF; i++)
590 /* alloc NB_MBUF mbufs */
591 for (i=0; i<NB_MBUF; i++) {
592 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
594 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);
618 if (m[i]->data_off != RTE_PKTMBUF_HEADROOM) {
619 printf("invalid data_off\n");
625 for (i=0; i<NB_MBUF; i++) {
627 rte_pktmbuf_free(m[i]);
634 test_pktmbuf_free_segment(struct rte_mempool *pktmbuf_pool)
637 struct rte_mbuf *m[NB_MBUF];
640 for (i=0; i<NB_MBUF; i++)
643 /* alloc NB_MBUF mbufs */
644 for (i=0; i<NB_MBUF; i++) {
645 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
647 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
653 for (i=0; i<NB_MBUF; i++) {
655 struct rte_mbuf *mb, *mt;
661 rte_pktmbuf_free_seg(mt);
670 * Stress test for rte_mbuf atomic refcnt.
671 * Implies that RTE_MBUF_REFCNT_ATOMIC is defined.
672 * For more efficiency, recommended to run with RTE_LIBRTE_MBUF_DEBUG defined.
675 #ifdef RTE_MBUF_REFCNT_ATOMIC
678 test_refcnt_slave(void *arg)
680 unsigned lcore, free;
682 struct rte_ring *refcnt_mbuf_ring = arg;
684 lcore = rte_lcore_id();
685 printf("%s started at lcore %u\n", __func__, lcore);
688 while (refcnt_stop_slaves == 0) {
689 if (rte_ring_dequeue(refcnt_mbuf_ring, &mp) == 0) {
691 rte_pktmbuf_free(mp);
695 refcnt_lcore[lcore] += free;
696 printf("%s finished at lcore %u, "
697 "number of freed mbufs: %u\n",
698 __func__, lcore, free);
703 test_refcnt_iter(unsigned int lcore, unsigned int iter,
704 struct rte_mempool *refcnt_pool,
705 struct rte_ring *refcnt_mbuf_ring)
708 unsigned i, n, tref, wn;
713 /* For each mbuf in the pool:
715 * - increment it's reference up to N+1,
716 * - enqueue it N times into the ring for slave cores to free.
718 for (i = 0, n = rte_mempool_avail_count(refcnt_pool);
719 i != n && (m = rte_pktmbuf_alloc(refcnt_pool)) != NULL;
721 ref = RTE_MAX(rte_rand() % REFCNT_MAX_REF, 1UL);
723 if ((ref & 1) != 0) {
724 rte_pktmbuf_refcnt_update(m, ref);
726 rte_ring_enqueue(refcnt_mbuf_ring, m);
729 rte_pktmbuf_refcnt_update(m, 1);
730 rte_ring_enqueue(refcnt_mbuf_ring, m);
737 rte_panic("(lcore=%u, iter=%u): was able to allocate only "
738 "%u from %u mbufs\n", lcore, iter, i, n);
740 /* wait till slave lcores will consume all mbufs */
741 while (!rte_ring_empty(refcnt_mbuf_ring))
744 /* check that all mbufs are back into mempool by now */
745 for (wn = 0; wn != REFCNT_MAX_TIMEOUT; wn++) {
746 if ((i = rte_mempool_avail_count(refcnt_pool)) == n) {
747 refcnt_lcore[lcore] += tref;
748 printf("%s(lcore=%u, iter=%u) completed, "
749 "%u references processed\n",
750 __func__, lcore, iter, tref);
756 rte_panic("(lcore=%u, iter=%u): after %us only "
757 "%u of %u mbufs left free\n", lcore, iter, wn, i, n);
761 test_refcnt_master(struct rte_mempool *refcnt_pool,
762 struct rte_ring *refcnt_mbuf_ring)
766 lcore = rte_lcore_id();
767 printf("%s started at lcore %u\n", __func__, lcore);
769 for (i = 0; i != REFCNT_MAX_ITER; i++)
770 test_refcnt_iter(lcore, i, refcnt_pool, refcnt_mbuf_ring);
772 refcnt_stop_slaves = 1;
775 printf("%s finished at lcore %u\n", __func__, lcore);
782 test_refcnt_mbuf(void)
784 #ifdef RTE_MBUF_REFCNT_ATOMIC
785 unsigned lnum, master, slave, tref;
787 struct rte_mempool *refcnt_pool = NULL;
788 struct rte_ring *refcnt_mbuf_ring = NULL;
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 refcnt_pool = rte_pktmbuf_pool_create(MAKE_STRING(refcnt_pool),
801 REFCNT_MBUF_NUM, 0, 0, 0,
803 if (refcnt_pool == NULL) {
804 printf("%s: cannot allocate " MAKE_STRING(refcnt_pool) "\n",
809 refcnt_mbuf_ring = rte_ring_create("refcnt_mbuf_ring",
810 rte_align32pow2(REFCNT_RING_SIZE), SOCKET_ID_ANY,
812 if (refcnt_mbuf_ring == NULL) {
813 printf("%s: cannot allocate " MAKE_STRING(refcnt_mbuf_ring)
818 refcnt_stop_slaves = 0;
819 memset(refcnt_lcore, 0, sizeof (refcnt_lcore));
821 rte_eal_mp_remote_launch(test_refcnt_slave, refcnt_mbuf_ring,
824 test_refcnt_master(refcnt_pool, refcnt_mbuf_ring);
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);
845 rte_mempool_free(refcnt_pool);
846 rte_ring_free(refcnt_mbuf_ring);
854 #include <sys/wait.h>
856 /* use fork() to test mbuf errors panic */
858 verify_mbuf_check_panics(struct rte_mbuf *buf)
866 rte_mbuf_sanity_check(buf, 1); /* should panic */
867 exit(0); /* return normally if it doesn't panic */
869 printf("Fork Failed\n");
880 test_failing_mbuf_sanity_check(struct rte_mempool *pktmbuf_pool)
882 struct rte_mbuf *buf;
883 struct rte_mbuf badbuf;
885 printf("Checking rte_mbuf_sanity_check for failure conditions\n");
887 /* get a good mbuf to use to make copies */
888 buf = rte_pktmbuf_alloc(pktmbuf_pool);
891 printf("Checking good mbuf initially\n");
892 if (verify_mbuf_check_panics(buf) != -1)
895 printf("Now checking for error conditions\n");
897 if (verify_mbuf_check_panics(NULL)) {
898 printf("Error with NULL mbuf test\n");
904 if (verify_mbuf_check_panics(&badbuf)) {
905 printf("Error with bad-pool mbuf test\n");
910 badbuf.buf_physaddr = 0;
911 if (verify_mbuf_check_panics(&badbuf)) {
912 printf("Error with bad-physaddr mbuf test\n");
917 badbuf.buf_addr = NULL;
918 if (verify_mbuf_check_panics(&badbuf)) {
919 printf("Error with bad-addr mbuf test\n");
925 if (verify_mbuf_check_panics(&badbuf)) {
926 printf("Error with bad-refcnt(0) mbuf test\n");
931 badbuf.refcnt = UINT16_MAX;
932 if (verify_mbuf_check_panics(&badbuf)) {
933 printf("Error with bad-refcnt(MAX) mbuf test\n");
941 test_mbuf_linearize(struct rte_mempool *pktmbuf_pool, int pkt_len,
945 struct rte_mbuf *m = NULL, *mbuf = NULL;
953 printf("Packet size must be 1 or more (is %d)\n", pkt_len);
958 printf("Number of segments must be 1 or more (is %d)\n",
963 seg_len = pkt_len / nb_segs;
969 /* Create chained mbuf_src and fill it generated data */
970 for (seg = 0; remain > 0; seg++) {
972 m = rte_pktmbuf_alloc(pktmbuf_pool);
974 printf("Cannot create segment for source mbuf");
978 /* Make sure if tailroom is zeroed */
979 memset(rte_pktmbuf_mtod(m, uint8_t *), 0,
980 rte_pktmbuf_tailroom(m));
983 if (data_len > seg_len)
986 data = (uint8_t *)rte_pktmbuf_append(m, data_len);
988 printf("Cannot append %d bytes to the mbuf\n",
993 for (i = 0; i < data_len; i++)
994 data[i] = (seg * seg_len + i) % 0x0ff;
999 rte_pktmbuf_chain(mbuf, m);
1004 /* Create destination buffer to store coalesced data */
1005 if (rte_pktmbuf_linearize(mbuf)) {
1006 printf("Mbuf linearization failed\n");
1010 if (!rte_pktmbuf_is_contiguous(mbuf)) {
1011 printf("Source buffer should be contiguous after "
1016 data = rte_pktmbuf_mtod(mbuf, uint8_t *);
1018 for (i = 0; i < pkt_len; i++)
1019 if (data[i] != (i % 0x0ff)) {
1020 printf("Incorrect data in linearized mbuf\n");
1024 rte_pktmbuf_free(mbuf);
1029 rte_pktmbuf_free(mbuf);
1034 test_mbuf_linearize_check(struct rte_mempool *pktmbuf_pool)
1036 struct test_mbuf_array {
1048 printf("Test mbuf linearize API\n");
1050 for (i = 0; i < RTE_DIM(mbuf_array); i++)
1051 if (test_mbuf_linearize(pktmbuf_pool, mbuf_array[i].size,
1052 mbuf_array[i].nb_segs)) {
1053 printf("Test failed for %d, %d\n", mbuf_array[i].size,
1054 mbuf_array[i].nb_segs);
1065 struct rte_mempool *pktmbuf_pool = NULL;
1066 struct rte_mempool *pktmbuf_pool2 = NULL;
1069 RTE_BUILD_BUG_ON(sizeof(struct rte_mbuf) != RTE_CACHE_LINE_MIN_SIZE * 2);
1071 /* create pktmbuf pool if it does not exist */
1072 pktmbuf_pool = rte_pktmbuf_pool_create("test_pktmbuf_pool",
1073 NB_MBUF, 32, 0, MBUF_DATA_SIZE, SOCKET_ID_ANY);
1075 if (pktmbuf_pool == NULL) {
1076 printf("cannot allocate mbuf pool\n");
1080 /* create a specific pktmbuf pool with a priv_size != 0 and no data
1082 pktmbuf_pool2 = rte_pktmbuf_pool_create("test_pktmbuf_pool2",
1083 NB_MBUF, 32, MBUF2_PRIV_SIZE, 0, SOCKET_ID_ANY);
1085 if (pktmbuf_pool2 == NULL) {
1086 printf("cannot allocate mbuf pool\n");
1090 /* test multiple mbuf alloc */
1091 if (test_pktmbuf_pool(pktmbuf_pool) < 0) {
1092 printf("test_mbuf_pool() failed\n");
1096 /* do it another time to check that all mbufs were freed */
1097 if (test_pktmbuf_pool(pktmbuf_pool) < 0) {
1098 printf("test_mbuf_pool() failed (2)\n");
1102 /* test that the pointer to the data on a packet mbuf is set properly */
1103 if (test_pktmbuf_pool_ptr(pktmbuf_pool) < 0) {
1104 printf("test_pktmbuf_pool_ptr() failed\n");
1108 /* test data manipulation in mbuf */
1109 if (test_one_pktmbuf(pktmbuf_pool) < 0) {
1110 printf("test_one_mbuf() failed\n");
1116 * do it another time, to check that allocation reinitialize
1117 * the mbuf correctly
1119 if (test_one_pktmbuf(pktmbuf_pool) < 0) {
1120 printf("test_one_mbuf() failed (2)\n");
1124 if (test_pktmbuf_with_non_ascii_data(pktmbuf_pool) < 0) {
1125 printf("test_pktmbuf_with_non_ascii_data() failed\n");
1129 /* test free pktmbuf segment one by one */
1130 if (test_pktmbuf_free_segment(pktmbuf_pool) < 0) {
1131 printf("test_pktmbuf_free_segment() failed.\n");
1135 if (testclone_testupdate_testdetach(pktmbuf_pool) < 0) {
1136 printf("testclone_and_testupdate() failed \n");
1140 if (test_attach_from_different_pool(pktmbuf_pool, pktmbuf_pool2) < 0) {
1141 printf("test_attach_from_different_pool() failed\n");
1145 if (test_refcnt_mbuf()<0){
1146 printf("test_refcnt_mbuf() failed \n");
1150 if (test_failing_mbuf_sanity_check(pktmbuf_pool) < 0) {
1151 printf("test_failing_mbuf_sanity_check() failed\n");
1155 if (test_mbuf_linearize_check(pktmbuf_pool) < 0) {
1156 printf("test_mbuf_linearize_check() failed\n");
1162 rte_mempool_free(pktmbuf_pool);
1163 rte_mempool_free(pktmbuf_pool2);
1167 REGISTER_TEST_COMMAND(mbuf_autotest, test_mbuf);