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 #ifdef RTE_MBUF_REFCNT_ATOMIC
87 static volatile uint32_t refcnt_stop_slaves;
88 static unsigned refcnt_lcore[RTE_MAX_LCORE];
96 * #. Allocate a mbuf pool.
98 * - The pool contains NB_MBUF elements, where each mbuf is MBUF_SIZE
101 * #. Test multiple allocations of mbufs from this pool.
103 * - Allocate NB_MBUF and store pointers in a table.
104 * - If an allocation fails, return an error.
105 * - Free all these mbufs.
106 * - Repeat the same test to check that mbufs were freed correctly.
108 * #. Test data manipulation in pktmbuf.
111 * - Append data using rte_pktmbuf_append().
112 * - Test for error in rte_pktmbuf_append() when len is too large.
113 * - Trim data at the end of mbuf using rte_pktmbuf_trim().
114 * - Test for error in rte_pktmbuf_trim() when len is too large.
115 * - Prepend a header using rte_pktmbuf_prepend().
116 * - Test for error in rte_pktmbuf_prepend() when len is too large.
117 * - Remove data at the beginning of mbuf using rte_pktmbuf_adj().
118 * - Test for error in rte_pktmbuf_adj() when len is too large.
119 * - Check that appended data is not corrupt.
121 * - Between all these tests, check data_len and pkt_len, and
122 * that the mbuf is contiguous.
123 * - Repeat the test to check that allocation operations
124 * reinitialize the mbuf correctly.
126 * #. Test packet cloning
127 * - Clone a mbuf and verify the data
128 * - Clone the cloned mbuf and verify the data
129 * - Attach a mbuf to another that does not have the same priv_size.
132 #define GOTO_FAIL(str, ...) do { \
133 printf("mbuf test FAILED (l.%d): <" str ">\n", \
134 __LINE__, ##__VA_ARGS__); \
139 * test data manipulation in mbuf with non-ascii data
142 test_pktmbuf_with_non_ascii_data(struct rte_mempool *pktmbuf_pool)
144 struct rte_mbuf *m = NULL;
147 m = rte_pktmbuf_alloc(pktmbuf_pool);
149 GOTO_FAIL("Cannot allocate mbuf");
150 if (rte_pktmbuf_pkt_len(m) != 0)
151 GOTO_FAIL("Bad length");
153 data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN);
155 GOTO_FAIL("Cannot append data");
156 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
157 GOTO_FAIL("Bad pkt length");
158 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
159 GOTO_FAIL("Bad data length");
160 memset(data, 0xff, rte_pktmbuf_pkt_len(m));
161 if (!rte_pktmbuf_is_contiguous(m))
162 GOTO_FAIL("Buffer should be continuous");
163 rte_pktmbuf_dump(stdout, m, MBUF_TEST_DATA_LEN);
177 * test data manipulation in mbuf
180 test_one_pktmbuf(struct rte_mempool *pktmbuf_pool)
182 struct rte_mbuf *m = NULL;
183 char *data, *data2, *hdr;
186 printf("Test pktmbuf API\n");
190 m = rte_pktmbuf_alloc(pktmbuf_pool);
192 GOTO_FAIL("Cannot allocate mbuf");
193 if (rte_pktmbuf_pkt_len(m) != 0)
194 GOTO_FAIL("Bad length");
196 rte_pktmbuf_dump(stdout, m, 0);
200 data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN);
202 GOTO_FAIL("Cannot append data");
203 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
204 GOTO_FAIL("Bad pkt length");
205 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
206 GOTO_FAIL("Bad data length");
207 memset(data, 0x66, rte_pktmbuf_pkt_len(m));
208 if (!rte_pktmbuf_is_contiguous(m))
209 GOTO_FAIL("Buffer should be continuous");
210 rte_pktmbuf_dump(stdout, m, MBUF_TEST_DATA_LEN);
211 rte_pktmbuf_dump(stdout, m, 2*MBUF_TEST_DATA_LEN);
213 /* this append should fail */
215 data2 = rte_pktmbuf_append(m, (uint16_t)(rte_pktmbuf_tailroom(m) + 1));
217 GOTO_FAIL("Append should not succeed");
219 /* append some more data */
221 data2 = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN2);
223 GOTO_FAIL("Cannot append data");
224 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_DATA_LEN2)
225 GOTO_FAIL("Bad pkt length");
226 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_DATA_LEN2)
227 GOTO_FAIL("Bad data length");
228 if (!rte_pktmbuf_is_contiguous(m))
229 GOTO_FAIL("Buffer should be continuous");
231 /* trim data at the end of mbuf */
233 if (rte_pktmbuf_trim(m, MBUF_TEST_DATA_LEN2) < 0)
234 GOTO_FAIL("Cannot trim data");
235 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
236 GOTO_FAIL("Bad pkt length");
237 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
238 GOTO_FAIL("Bad data length");
239 if (!rte_pktmbuf_is_contiguous(m))
240 GOTO_FAIL("Buffer should be continuous");
242 /* this trim should fail */
244 if (rte_pktmbuf_trim(m, (uint16_t)(rte_pktmbuf_data_len(m) + 1)) == 0)
245 GOTO_FAIL("trim should not succeed");
247 /* prepend one header */
249 hdr = rte_pktmbuf_prepend(m, MBUF_TEST_HDR1_LEN);
251 GOTO_FAIL("Cannot prepend");
252 if (data - hdr != MBUF_TEST_HDR1_LEN)
253 GOTO_FAIL("Prepend failed");
254 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_HDR1_LEN)
255 GOTO_FAIL("Bad pkt length");
256 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_HDR1_LEN)
257 GOTO_FAIL("Bad data length");
258 if (!rte_pktmbuf_is_contiguous(m))
259 GOTO_FAIL("Buffer should be continuous");
260 memset(hdr, 0x55, MBUF_TEST_HDR1_LEN);
262 /* prepend another header */
264 hdr = rte_pktmbuf_prepend(m, MBUF_TEST_HDR2_LEN);
266 GOTO_FAIL("Cannot prepend");
267 if (data - hdr != MBUF_TEST_ALL_HDRS_LEN)
268 GOTO_FAIL("Prepend failed");
269 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_ALL_HDRS_LEN)
270 GOTO_FAIL("Bad pkt length");
271 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_ALL_HDRS_LEN)
272 GOTO_FAIL("Bad data length");
273 if (!rte_pktmbuf_is_contiguous(m))
274 GOTO_FAIL("Buffer should be continuous");
275 memset(hdr, 0x55, MBUF_TEST_HDR2_LEN);
277 rte_mbuf_sanity_check(m, 1);
278 rte_mbuf_sanity_check(m, 0);
279 rte_pktmbuf_dump(stdout, m, 0);
281 /* this prepend should fail */
283 hdr = rte_pktmbuf_prepend(m, (uint16_t)(rte_pktmbuf_headroom(m) + 1));
285 GOTO_FAIL("prepend should not succeed");
287 /* remove data at beginning of mbuf (adj) */
289 if (data != rte_pktmbuf_adj(m, MBUF_TEST_ALL_HDRS_LEN))
290 GOTO_FAIL("rte_pktmbuf_adj failed");
291 if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
292 GOTO_FAIL("Bad pkt length");
293 if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
294 GOTO_FAIL("Bad data length");
295 if (!rte_pktmbuf_is_contiguous(m))
296 GOTO_FAIL("Buffer should be continuous");
298 /* this adj should fail */
300 if (rte_pktmbuf_adj(m, (uint16_t)(rte_pktmbuf_data_len(m) + 1)) != NULL)
301 GOTO_FAIL("rte_pktmbuf_adj should not succeed");
305 if (!rte_pktmbuf_is_contiguous(m))
306 GOTO_FAIL("Buffer should be continuous");
308 for (i=0; i<MBUF_TEST_DATA_LEN; i++) {
310 GOTO_FAIL("Data corrupted at offset %u", i);
326 testclone_testupdate_testdetach(struct rte_mempool *pktmbuf_pool)
328 struct rte_mbuf *m = NULL;
329 struct rte_mbuf *clone = NULL;
330 struct rte_mbuf *clone2 = NULL;
331 unaligned_uint32_t *data;
334 m = rte_pktmbuf_alloc(pktmbuf_pool);
336 GOTO_FAIL("ooops not allocating mbuf");
338 if (rte_pktmbuf_pkt_len(m) != 0)
339 GOTO_FAIL("Bad length");
341 rte_pktmbuf_append(m, sizeof(uint32_t));
342 data = rte_pktmbuf_mtod(m, unaligned_uint32_t *);
345 /* clone the allocated mbuf */
346 clone = rte_pktmbuf_clone(m, pktmbuf_pool);
348 GOTO_FAIL("cannot clone data\n");
350 data = rte_pktmbuf_mtod(clone, unaligned_uint32_t *);
351 if (*data != MAGIC_DATA)
352 GOTO_FAIL("invalid data in clone\n");
354 if (rte_mbuf_refcnt_read(m) != 2)
355 GOTO_FAIL("invalid refcnt in m\n");
358 rte_pktmbuf_free(clone);
361 /* same test with a chained mbuf */
362 m->next = rte_pktmbuf_alloc(pktmbuf_pool);
364 GOTO_FAIL("Next Pkt Null\n");
366 rte_pktmbuf_append(m->next, sizeof(uint32_t));
367 data = rte_pktmbuf_mtod(m->next, unaligned_uint32_t *);
370 clone = rte_pktmbuf_clone(m, pktmbuf_pool);
372 GOTO_FAIL("cannot clone data\n");
374 data = rte_pktmbuf_mtod(clone, unaligned_uint32_t *);
375 if (*data != MAGIC_DATA)
376 GOTO_FAIL("invalid data in clone\n");
378 data = rte_pktmbuf_mtod(clone->next, unaligned_uint32_t *);
379 if (*data != MAGIC_DATA)
380 GOTO_FAIL("invalid data in clone->next\n");
382 if (rte_mbuf_refcnt_read(m) != 2)
383 GOTO_FAIL("invalid refcnt in m\n");
385 if (rte_mbuf_refcnt_read(m->next) != 2)
386 GOTO_FAIL("invalid refcnt in m->next\n");
388 /* try to clone the clone */
390 clone2 = rte_pktmbuf_clone(clone, pktmbuf_pool);
392 GOTO_FAIL("cannot clone the clone\n");
394 data = rte_pktmbuf_mtod(clone2, unaligned_uint32_t *);
395 if (*data != MAGIC_DATA)
396 GOTO_FAIL("invalid data in clone2\n");
398 data = rte_pktmbuf_mtod(clone2->next, unaligned_uint32_t *);
399 if (*data != MAGIC_DATA)
400 GOTO_FAIL("invalid data in clone2->next\n");
402 if (rte_mbuf_refcnt_read(m) != 3)
403 GOTO_FAIL("invalid refcnt in m\n");
405 if (rte_mbuf_refcnt_read(m->next) != 3)
406 GOTO_FAIL("invalid refcnt in m->next\n");
410 rte_pktmbuf_free(clone);
411 rte_pktmbuf_free(clone2);
416 printf("%s ok\n", __func__);
423 rte_pktmbuf_free(clone);
425 rte_pktmbuf_free(clone2);
430 test_attach_from_different_pool(struct rte_mempool *pktmbuf_pool,
431 struct rte_mempool *pktmbuf_pool2)
433 struct rte_mbuf *m = NULL;
434 struct rte_mbuf *clone = NULL;
435 struct rte_mbuf *clone2 = NULL;
436 char *data, *c_data, *c_data2;
439 m = rte_pktmbuf_alloc(pktmbuf_pool);
441 GOTO_FAIL("cannot allocate mbuf");
443 if (rte_pktmbuf_pkt_len(m) != 0)
444 GOTO_FAIL("Bad length");
446 data = rte_pktmbuf_mtod(m, char *);
448 /* allocate a new mbuf from the second pool, and attach it to the first
450 clone = rte_pktmbuf_alloc(pktmbuf_pool2);
452 GOTO_FAIL("cannot allocate mbuf from second pool\n");
454 /* check data room size and priv size, and erase priv */
455 if (rte_pktmbuf_data_room_size(clone->pool) != 0)
456 GOTO_FAIL("data room size should be 0\n");
457 if (rte_pktmbuf_priv_size(clone->pool) != MBUF2_PRIV_SIZE)
458 GOTO_FAIL("data room size should be %d\n", MBUF2_PRIV_SIZE);
459 memset(clone + 1, 0, MBUF2_PRIV_SIZE);
461 /* save data pointer to compare it after detach() */
462 c_data = rte_pktmbuf_mtod(clone, char *);
463 if (c_data != (char *)clone + sizeof(*clone) + MBUF2_PRIV_SIZE)
464 GOTO_FAIL("bad data pointer in clone");
465 if (rte_pktmbuf_headroom(clone) != 0)
466 GOTO_FAIL("bad headroom in clone");
468 rte_pktmbuf_attach(clone, m);
470 if (rte_pktmbuf_mtod(clone, char *) != data)
471 GOTO_FAIL("clone was not attached properly\n");
472 if (rte_pktmbuf_headroom(clone) != RTE_PKTMBUF_HEADROOM)
473 GOTO_FAIL("bad headroom in clone after attach");
474 if (rte_mbuf_refcnt_read(m) != 2)
475 GOTO_FAIL("invalid refcnt in m\n");
477 /* allocate a new mbuf from the second pool, and attach it to the first
479 clone2 = rte_pktmbuf_alloc(pktmbuf_pool2);
481 GOTO_FAIL("cannot allocate clone2 from second pool\n");
483 /* check data room size and priv size, and erase priv */
484 if (rte_pktmbuf_data_room_size(clone2->pool) != 0)
485 GOTO_FAIL("data room size should be 0\n");
486 if (rte_pktmbuf_priv_size(clone2->pool) != MBUF2_PRIV_SIZE)
487 GOTO_FAIL("data room size should be %d\n", MBUF2_PRIV_SIZE);
488 memset(clone2 + 1, 0, MBUF2_PRIV_SIZE);
490 /* save data pointer to compare it after detach() */
491 c_data2 = rte_pktmbuf_mtod(clone2, char *);
492 if (c_data2 != (char *)clone2 + sizeof(*clone2) + MBUF2_PRIV_SIZE)
493 GOTO_FAIL("bad data pointer in clone2");
494 if (rte_pktmbuf_headroom(clone2) != 0)
495 GOTO_FAIL("bad headroom in clone2");
497 rte_pktmbuf_attach(clone2, clone);
499 if (rte_pktmbuf_mtod(clone2, char *) != data)
500 GOTO_FAIL("clone2 was not attached properly\n");
501 if (rte_pktmbuf_headroom(clone2) != RTE_PKTMBUF_HEADROOM)
502 GOTO_FAIL("bad headroom in clone2 after attach");
503 if (rte_mbuf_refcnt_read(m) != 3)
504 GOTO_FAIL("invalid refcnt in m\n");
506 /* detach the clones */
507 rte_pktmbuf_detach(clone);
508 if (c_data != rte_pktmbuf_mtod(clone, char *))
509 GOTO_FAIL("clone was not detached properly\n");
510 if (rte_mbuf_refcnt_read(m) != 2)
511 GOTO_FAIL("invalid refcnt in m\n");
513 rte_pktmbuf_detach(clone2);
514 if (c_data2 != rte_pktmbuf_mtod(clone2, char *))
515 GOTO_FAIL("clone2 was not detached properly\n");
516 if (rte_mbuf_refcnt_read(m) != 1)
517 GOTO_FAIL("invalid refcnt in m\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(struct rte_mempool *pktmbuf_pool)
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(struct rte_mempool *pktmbuf_pool)
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);
599 m[i]->data_off += 64;
603 for (i=0; i<NB_MBUF; i++) {
605 rte_pktmbuf_free(m[i]);
608 for (i=0; i<NB_MBUF; i++)
611 /* alloc NB_MBUF mbufs */
612 for (i=0; i<NB_MBUF; i++) {
613 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
615 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
619 if (m[i]->data_off != RTE_PKTMBUF_HEADROOM) {
620 printf("invalid data_off\n");
626 for (i=0; i<NB_MBUF; i++) {
628 rte_pktmbuf_free(m[i]);
635 test_pktmbuf_free_segment(struct rte_mempool *pktmbuf_pool)
638 struct rte_mbuf *m[NB_MBUF];
641 for (i=0; i<NB_MBUF; i++)
644 /* alloc NB_MBUF mbufs */
645 for (i=0; i<NB_MBUF; i++) {
646 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
648 printf("rte_pktmbuf_alloc() failed (%u)\n", i);
654 for (i=0; i<NB_MBUF; i++) {
656 struct rte_mbuf *mb, *mt;
662 rte_pktmbuf_free_seg(mt);
671 * Stress test for rte_mbuf atomic refcnt.
672 * Implies that RTE_MBUF_REFCNT_ATOMIC is defined.
673 * For more efficiency, recommended to run with RTE_LIBRTE_MBUF_DEBUG defined.
676 #ifdef RTE_MBUF_REFCNT_ATOMIC
679 test_refcnt_slave(void *arg)
681 unsigned lcore, free;
683 struct rte_ring *refcnt_mbuf_ring = arg;
685 lcore = rte_lcore_id();
686 printf("%s started at lcore %u\n", __func__, lcore);
689 while (refcnt_stop_slaves == 0) {
690 if (rte_ring_dequeue(refcnt_mbuf_ring, &mp) == 0) {
692 rte_pktmbuf_free(mp);
696 refcnt_lcore[lcore] += free;
697 printf("%s finished at lcore %u, "
698 "number of freed mbufs: %u\n",
699 __func__, lcore, free);
704 test_refcnt_iter(unsigned int lcore, unsigned int iter,
705 struct rte_mempool *refcnt_pool,
706 struct rte_ring *refcnt_mbuf_ring)
709 unsigned i, n, tref, wn;
714 /* For each mbuf in the pool:
716 * - increment it's reference up to N+1,
717 * - enqueue it N times into the ring for slave cores to free.
719 for (i = 0, n = rte_mempool_avail_count(refcnt_pool);
720 i != n && (m = rte_pktmbuf_alloc(refcnt_pool)) != NULL;
722 ref = RTE_MAX(rte_rand() % REFCNT_MAX_REF, 1UL);
724 if ((ref & 1) != 0) {
725 rte_pktmbuf_refcnt_update(m, ref);
727 rte_ring_enqueue(refcnt_mbuf_ring, m);
730 rte_pktmbuf_refcnt_update(m, 1);
731 rte_ring_enqueue(refcnt_mbuf_ring, m);
738 rte_panic("(lcore=%u, iter=%u): was able to allocate only "
739 "%u from %u mbufs\n", lcore, iter, i, n);
741 /* wait till slave lcores will consume all mbufs */
742 while (!rte_ring_empty(refcnt_mbuf_ring))
745 /* check that all mbufs are back into mempool by now */
746 for (wn = 0; wn != REFCNT_MAX_TIMEOUT; wn++) {
747 if ((i = rte_mempool_avail_count(refcnt_pool)) == n) {
748 refcnt_lcore[lcore] += tref;
749 printf("%s(lcore=%u, iter=%u) completed, "
750 "%u references processed\n",
751 __func__, lcore, iter, tref);
757 rte_panic("(lcore=%u, iter=%u): after %us only "
758 "%u of %u mbufs left free\n", lcore, iter, wn, i, n);
762 test_refcnt_master(struct rte_mempool *refcnt_pool,
763 struct rte_ring *refcnt_mbuf_ring)
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, refcnt_pool, refcnt_mbuf_ring);
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
786 unsigned lnum, master, slave, tref;
788 struct rte_mempool *refcnt_pool = NULL;
789 struct rte_ring *refcnt_mbuf_ring = NULL;
791 if ((lnum = rte_lcore_count()) == 1) {
792 printf("skipping %s, number of lcores: %u is not enough\n",
797 printf("starting %s, at %u lcores\n", __func__, lnum);
799 /* create refcnt pool & ring if they don't exist */
801 refcnt_pool = rte_pktmbuf_pool_create(MAKE_STRING(refcnt_pool),
802 REFCNT_MBUF_NUM, 0, 0, 0,
804 if (refcnt_pool == NULL) {
805 printf("%s: cannot allocate " MAKE_STRING(refcnt_pool) "\n",
810 refcnt_mbuf_ring = rte_ring_create("refcnt_mbuf_ring",
811 rte_align32pow2(REFCNT_RING_SIZE), SOCKET_ID_ANY,
813 if (refcnt_mbuf_ring == 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, refcnt_mbuf_ring,
825 test_refcnt_master(refcnt_pool, refcnt_mbuf_ring);
827 rte_eal_mp_wait_lcore();
829 /* check that we porcessed all references */
831 master = rte_get_master_lcore();
833 RTE_LCORE_FOREACH_SLAVE(slave)
834 tref += refcnt_lcore[slave];
836 if (tref != refcnt_lcore[master])
837 rte_panic("refernced mbufs: %u, freed mbufs: %u\n",
838 tref, refcnt_lcore[master]);
840 rte_mempool_dump(stdout, refcnt_pool);
841 rte_ring_dump(stdout, refcnt_mbuf_ring);
846 rte_mempool_free(refcnt_pool);
847 rte_ring_free(refcnt_mbuf_ring);
855 #include <sys/wait.h>
857 /* use fork() to test mbuf errors panic */
859 verify_mbuf_check_panics(struct rte_mbuf *buf)
867 rte_mbuf_sanity_check(buf, 1); /* should panic */
868 exit(0); /* return normally if it doesn't panic */
870 printf("Fork Failed\n");
881 test_failing_mbuf_sanity_check(struct rte_mempool *pktmbuf_pool)
883 struct rte_mbuf *buf;
884 struct rte_mbuf badbuf;
886 printf("Checking rte_mbuf_sanity_check for failure conditions\n");
888 /* get a good mbuf to use to make copies */
889 buf = rte_pktmbuf_alloc(pktmbuf_pool);
892 printf("Checking good mbuf initially\n");
893 if (verify_mbuf_check_panics(buf) != -1)
896 printf("Now checking for error conditions\n");
898 if (verify_mbuf_check_panics(NULL)) {
899 printf("Error with NULL mbuf test\n");
905 if (verify_mbuf_check_panics(&badbuf)) {
906 printf("Error with bad-pool mbuf test\n");
911 badbuf.buf_physaddr = 0;
912 if (verify_mbuf_check_panics(&badbuf)) {
913 printf("Error with bad-physaddr mbuf test\n");
918 badbuf.buf_addr = NULL;
919 if (verify_mbuf_check_panics(&badbuf)) {
920 printf("Error with bad-addr mbuf test\n");
926 if (verify_mbuf_check_panics(&badbuf)) {
927 printf("Error with bad-refcnt(0) mbuf test\n");
932 badbuf.refcnt = UINT16_MAX;
933 if (verify_mbuf_check_panics(&badbuf)) {
934 printf("Error with bad-refcnt(MAX) mbuf test\n");
942 test_mbuf_linearize(struct rte_mempool *pktmbuf_pool, int pkt_len,
946 struct rte_mbuf *m = NULL, *mbuf = NULL;
954 printf("Packet size must be 1 or more (is %d)\n", pkt_len);
959 printf("Number of segments must be 1 or more (is %d)\n",
964 seg_len = pkt_len / nb_segs;
970 /* Create chained mbuf_src and fill it generated data */
971 for (seg = 0; remain > 0; seg++) {
973 m = rte_pktmbuf_alloc(pktmbuf_pool);
975 printf("Cannot create segment for source mbuf");
979 /* Make sure if tailroom is zeroed */
980 memset(rte_pktmbuf_mtod(m, uint8_t *), 0,
981 rte_pktmbuf_tailroom(m));
984 if (data_len > seg_len)
987 data = (uint8_t *)rte_pktmbuf_append(m, data_len);
989 printf("Cannot append %d bytes to the mbuf\n",
994 for (i = 0; i < data_len; i++)
995 data[i] = (seg * seg_len + i) % 0x0ff;
1000 rte_pktmbuf_chain(mbuf, m);
1005 /* Create destination buffer to store coalesced data */
1006 if (rte_pktmbuf_linearize(mbuf)) {
1007 printf("Mbuf linearization failed\n");
1011 if (!rte_pktmbuf_is_contiguous(mbuf)) {
1012 printf("Source buffer should be contiguous after "
1017 data = rte_pktmbuf_mtod(mbuf, uint8_t *);
1019 for (i = 0; i < pkt_len; i++)
1020 if (data[i] != (i % 0x0ff)) {
1021 printf("Incorrect data in linearized mbuf\n");
1025 rte_pktmbuf_free(mbuf);
1030 rte_pktmbuf_free(mbuf);
1035 test_mbuf_linearize_check(struct rte_mempool *pktmbuf_pool)
1037 struct test_mbuf_array {
1049 printf("Test mbuf linearize API\n");
1051 for (i = 0; i < RTE_DIM(mbuf_array); i++)
1052 if (test_mbuf_linearize(pktmbuf_pool, mbuf_array[i].size,
1053 mbuf_array[i].nb_segs)) {
1054 printf("Test failed for %d, %d\n", mbuf_array[i].size,
1055 mbuf_array[i].nb_segs);
1066 struct rte_mempool *pktmbuf_pool = NULL;
1067 struct rte_mempool *pktmbuf_pool2 = NULL;
1070 RTE_BUILD_BUG_ON(sizeof(struct rte_mbuf) != RTE_CACHE_LINE_MIN_SIZE * 2);
1072 /* create pktmbuf pool if it does not exist */
1073 pktmbuf_pool = rte_pktmbuf_pool_create("test_pktmbuf_pool",
1074 NB_MBUF, 32, 0, MBUF_DATA_SIZE, SOCKET_ID_ANY);
1076 if (pktmbuf_pool == NULL) {
1077 printf("cannot allocate mbuf pool\n");
1081 /* create a specific pktmbuf pool with a priv_size != 0 and no data
1083 pktmbuf_pool2 = rte_pktmbuf_pool_create("test_pktmbuf_pool2",
1084 NB_MBUF, 32, MBUF2_PRIV_SIZE, 0, SOCKET_ID_ANY);
1086 if (pktmbuf_pool2 == NULL) {
1087 printf("cannot allocate mbuf pool\n");
1091 /* test multiple mbuf alloc */
1092 if (test_pktmbuf_pool(pktmbuf_pool) < 0) {
1093 printf("test_mbuf_pool() failed\n");
1097 /* do it another time to check that all mbufs were freed */
1098 if (test_pktmbuf_pool(pktmbuf_pool) < 0) {
1099 printf("test_mbuf_pool() failed (2)\n");
1103 /* test that the pointer to the data on a packet mbuf is set properly */
1104 if (test_pktmbuf_pool_ptr(pktmbuf_pool) < 0) {
1105 printf("test_pktmbuf_pool_ptr() failed\n");
1109 /* test data manipulation in mbuf */
1110 if (test_one_pktmbuf(pktmbuf_pool) < 0) {
1111 printf("test_one_mbuf() failed\n");
1117 * do it another time, to check that allocation reinitialize
1118 * the mbuf correctly
1120 if (test_one_pktmbuf(pktmbuf_pool) < 0) {
1121 printf("test_one_mbuf() failed (2)\n");
1125 if (test_pktmbuf_with_non_ascii_data(pktmbuf_pool) < 0) {
1126 printf("test_pktmbuf_with_non_ascii_data() failed\n");
1130 /* test free pktmbuf segment one by one */
1131 if (test_pktmbuf_free_segment(pktmbuf_pool) < 0) {
1132 printf("test_pktmbuf_free_segment() failed.\n");
1136 if (testclone_testupdate_testdetach(pktmbuf_pool) < 0) {
1137 printf("testclone_and_testupdate() failed \n");
1141 if (test_attach_from_different_pool(pktmbuf_pool, pktmbuf_pool2) < 0) {
1142 printf("test_attach_from_different_pool() failed\n");
1146 if (test_refcnt_mbuf()<0){
1147 printf("test_refcnt_mbuf() failed \n");
1151 if (test_failing_mbuf_sanity_check(pktmbuf_pool) < 0) {
1152 printf("test_failing_mbuf_sanity_check() failed\n");
1156 if (test_mbuf_linearize_check(pktmbuf_pool) < 0) {
1157 printf("test_mbuf_linearize_check() failed\n");
1163 rte_mempool_free(pktmbuf_pool);
1164 rte_mempool_free(pktmbuf_pool2);
1168 REGISTER_TEST_COMMAND(mbuf_autotest, test_mbuf);