apps: use helper to create mbuf pools
[dpdk.git] / app / test / test_mbuf.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <string.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <stdint.h>
39 #include <inttypes.h>
40 #include <errno.h>
41 #include <sys/queue.h>
42
43 #include <rte_common.h>
44 #include <rte_debug.h>
45 #include <rte_log.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>
51 #include <rte_eal.h>
52 #include <rte_per_lcore.h>
53 #include <rte_lcore.h>
54 #include <rte_atomic.h>
55 #include <rte_branch_prediction.h>
56 #include <rte_ring.h>
57 #include <rte_mempool.h>
58 #include <rte_mbuf.h>
59 #include <rte_random.h>
60 #include <rte_cycles.h>
61
62 #include "test.h"
63
64 #define MBUF_DATA_SIZE          2048
65 #define NB_MBUF                 128
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)
71
72 #define REFCNT_MAX_ITER         64
73 #define REFCNT_MAX_TIMEOUT      10
74 #define REFCNT_MAX_REF          (RTE_MAX_LCORE)
75 #define REFCNT_MBUF_NUM         64
76 #define REFCNT_RING_SIZE        (REFCNT_MBUF_NUM * REFCNT_MAX_REF)
77
78 #define MAKE_STRING(x)          # x
79
80 static struct rte_mempool *pktmbuf_pool = NULL;
81
82 #ifdef RTE_MBUF_REFCNT_ATOMIC
83
84 static struct rte_mempool *refcnt_pool = NULL;
85 static struct rte_ring *refcnt_mbuf_ring = NULL;
86 static volatile uint32_t refcnt_stop_slaves;
87 static unsigned refcnt_lcore[RTE_MAX_LCORE];
88
89 #endif
90
91 /*
92  * MBUF
93  * ====
94  *
95  * #. Allocate a mbuf pool.
96  *
97  *    - The pool contains NB_MBUF elements, where each mbuf is MBUF_SIZE
98  *      bytes long.
99  *
100  * #. Test multiple allocations of mbufs from this pool.
101  *
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.
106  *
107  * #. Test data manipulation in pktmbuf.
108  *
109  *    - Alloc an mbuf.
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.
119  *    - Free the mbuf.
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.
124  *
125  */
126
127 #define GOTO_FAIL(str, ...) do {                                        \
128                 printf("mbuf test FAILED (l.%d): <" str ">\n",          \
129                        __LINE__,  ##__VA_ARGS__);                       \
130                 goto fail;                                              \
131 } while(0)
132
133 /*
134  * test data manipulation in mbuf with non-ascii data
135  */
136 static int
137 test_pktmbuf_with_non_ascii_data(void)
138 {
139         struct rte_mbuf *m = NULL;
140         char *data;
141
142         m = rte_pktmbuf_alloc(pktmbuf_pool);
143         if (m == NULL)
144                 GOTO_FAIL("Cannot allocate mbuf");
145         if (rte_pktmbuf_pkt_len(m) != 0)
146                 GOTO_FAIL("Bad length");
147
148         data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN);
149         if (data == NULL)
150                 GOTO_FAIL("Cannot append data");
151         if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
152                 GOTO_FAIL("Bad pkt length");
153         if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
154                 GOTO_FAIL("Bad data length");
155         memset(data, 0xff, rte_pktmbuf_pkt_len(m));
156         if (!rte_pktmbuf_is_contiguous(m))
157                 GOTO_FAIL("Buffer should be continuous");
158         rte_pktmbuf_dump(stdout, m, MBUF_TEST_DATA_LEN);
159
160         rte_pktmbuf_free(m);
161
162         return 0;
163
164 fail:
165         if(m) {
166                 rte_pktmbuf_free(m);
167         }
168         return -1;
169 }
170
171 /*
172  * test data manipulation in mbuf
173  */
174 static int
175 test_one_pktmbuf(void)
176 {
177         struct rte_mbuf *m = NULL;
178         char *data, *data2, *hdr;
179         unsigned i;
180
181         printf("Test pktmbuf API\n");
182
183         /* alloc a mbuf */
184
185         m = rte_pktmbuf_alloc(pktmbuf_pool);
186         if (m == NULL)
187                 GOTO_FAIL("Cannot allocate mbuf");
188         if (rte_pktmbuf_pkt_len(m) != 0)
189                 GOTO_FAIL("Bad length");
190
191         rte_pktmbuf_dump(stdout, m, 0);
192
193         /* append data */
194
195         data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN);
196         if (data == NULL)
197                 GOTO_FAIL("Cannot append data");
198         if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
199                 GOTO_FAIL("Bad pkt length");
200         if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
201                 GOTO_FAIL("Bad data length");
202         memset(data, 0x66, rte_pktmbuf_pkt_len(m));
203         if (!rte_pktmbuf_is_contiguous(m))
204                 GOTO_FAIL("Buffer should be continuous");
205         rte_pktmbuf_dump(stdout, m, MBUF_TEST_DATA_LEN);
206         rte_pktmbuf_dump(stdout, m, 2*MBUF_TEST_DATA_LEN);
207
208         /* this append should fail */
209
210         data2 = rte_pktmbuf_append(m, (uint16_t)(rte_pktmbuf_tailroom(m) + 1));
211         if (data2 != NULL)
212                 GOTO_FAIL("Append should not succeed");
213
214         /* append some more data */
215
216         data2 = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN2);
217         if (data2 == NULL)
218                 GOTO_FAIL("Cannot append data");
219         if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_DATA_LEN2)
220                 GOTO_FAIL("Bad pkt length");
221         if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_DATA_LEN2)
222                 GOTO_FAIL("Bad data length");
223         if (!rte_pktmbuf_is_contiguous(m))
224                 GOTO_FAIL("Buffer should be continuous");
225
226         /* trim data at the end of mbuf */
227
228         if (rte_pktmbuf_trim(m, MBUF_TEST_DATA_LEN2) < 0)
229                 GOTO_FAIL("Cannot trim data");
230         if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
231                 GOTO_FAIL("Bad pkt length");
232         if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
233                 GOTO_FAIL("Bad data length");
234         if (!rte_pktmbuf_is_contiguous(m))
235                 GOTO_FAIL("Buffer should be continuous");
236
237         /* this trim should fail */
238
239         if (rte_pktmbuf_trim(m, (uint16_t)(rte_pktmbuf_data_len(m) + 1)) == 0)
240                 GOTO_FAIL("trim should not succeed");
241
242         /* prepend one header */
243
244         hdr = rte_pktmbuf_prepend(m, MBUF_TEST_HDR1_LEN);
245         if (hdr == NULL)
246                 GOTO_FAIL("Cannot prepend");
247         if (data - hdr != MBUF_TEST_HDR1_LEN)
248                 GOTO_FAIL("Prepend failed");
249         if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_HDR1_LEN)
250                 GOTO_FAIL("Bad pkt length");
251         if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_HDR1_LEN)
252                 GOTO_FAIL("Bad data length");
253         if (!rte_pktmbuf_is_contiguous(m))
254                 GOTO_FAIL("Buffer should be continuous");
255         memset(hdr, 0x55, MBUF_TEST_HDR1_LEN);
256
257         /* prepend another header */
258
259         hdr = rte_pktmbuf_prepend(m, MBUF_TEST_HDR2_LEN);
260         if (hdr == NULL)
261                 GOTO_FAIL("Cannot prepend");
262         if (data - hdr != MBUF_TEST_ALL_HDRS_LEN)
263                 GOTO_FAIL("Prepend failed");
264         if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_ALL_HDRS_LEN)
265                 GOTO_FAIL("Bad pkt length");
266         if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_ALL_HDRS_LEN)
267                 GOTO_FAIL("Bad data length");
268         if (!rte_pktmbuf_is_contiguous(m))
269                 GOTO_FAIL("Buffer should be continuous");
270         memset(hdr, 0x55, MBUF_TEST_HDR2_LEN);
271
272         rte_mbuf_sanity_check(m, 1);
273         rte_mbuf_sanity_check(m, 0);
274         rte_pktmbuf_dump(stdout, m, 0);
275
276         /* this prepend should fail */
277
278         hdr = rte_pktmbuf_prepend(m, (uint16_t)(rte_pktmbuf_headroom(m) + 1));
279         if (hdr != NULL)
280                 GOTO_FAIL("prepend should not succeed");
281
282         /* remove data at beginning of mbuf (adj) */
283
284         if (data != rte_pktmbuf_adj(m, MBUF_TEST_ALL_HDRS_LEN))
285                 GOTO_FAIL("rte_pktmbuf_adj failed");
286         if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
287                 GOTO_FAIL("Bad pkt length");
288         if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
289                 GOTO_FAIL("Bad data length");
290         if (!rte_pktmbuf_is_contiguous(m))
291                 GOTO_FAIL("Buffer should be continuous");
292
293         /* this adj should fail */
294
295         if (rte_pktmbuf_adj(m, (uint16_t)(rte_pktmbuf_data_len(m) + 1)) != NULL)
296                 GOTO_FAIL("rte_pktmbuf_adj should not succeed");
297
298         /* check data */
299
300         if (!rte_pktmbuf_is_contiguous(m))
301                 GOTO_FAIL("Buffer should be continuous");
302
303         for (i=0; i<MBUF_TEST_DATA_LEN; i++) {
304                 if (data[i] != 0x66)
305                         GOTO_FAIL("Data corrupted at offset %u", i);
306         }
307
308         /* free mbuf */
309
310         rte_pktmbuf_free(m);
311         m = NULL;
312         return 0;
313
314 fail:
315         if (m)
316                 rte_pktmbuf_free(m);
317         return -1;
318 }
319
320 static int
321 testclone_testupdate_testdetach(void)
322 {
323         struct rte_mbuf *mc = NULL;
324         struct rte_mbuf *clone = NULL;
325
326         /* alloc a mbuf */
327
328         mc = rte_pktmbuf_alloc(pktmbuf_pool);
329         if (mc == NULL)
330                 GOTO_FAIL("ooops not allocating mbuf");
331
332         if (rte_pktmbuf_pkt_len(mc) != 0)
333                 GOTO_FAIL("Bad length");
334
335
336         /* clone the allocated mbuf */
337         clone = rte_pktmbuf_clone(mc, pktmbuf_pool);
338         if (clone == NULL)
339                 GOTO_FAIL("cannot clone data\n");
340         rte_pktmbuf_free(clone);
341
342         mc->next = rte_pktmbuf_alloc(pktmbuf_pool);
343         if(mc->next == NULL)
344                 GOTO_FAIL("Next Pkt Null\n");
345
346         clone = rte_pktmbuf_clone(mc, pktmbuf_pool);
347         if (clone == NULL)
348                 GOTO_FAIL("cannot clone data\n");
349
350         /* free mbuf */
351         rte_pktmbuf_free(mc);
352         rte_pktmbuf_free(clone);
353         mc = NULL;
354         clone = NULL;
355         return 0;
356
357 fail:
358         if (mc)
359                 rte_pktmbuf_free(mc);
360         return -1;
361 }
362 #undef GOTO_FAIL
363
364
365
366 /*
367  * test allocation and free of mbufs
368  */
369 static int
370 test_pktmbuf_pool(void)
371 {
372         unsigned i;
373         struct rte_mbuf *m[NB_MBUF];
374         int ret = 0;
375
376         for (i=0; i<NB_MBUF; i++)
377                 m[i] = NULL;
378
379         /* alloc NB_MBUF mbufs */
380         for (i=0; i<NB_MBUF; i++) {
381                 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
382                 if (m[i] == NULL) {
383                         printf("rte_pktmbuf_alloc() failed (%u)\n", i);
384                         ret = -1;
385                 }
386         }
387         struct rte_mbuf *extra = NULL;
388         extra = rte_pktmbuf_alloc(pktmbuf_pool);
389         if(extra != NULL) {
390                 printf("Error pool not empty");
391                 ret = -1;
392         }
393         extra = rte_pktmbuf_clone(m[0], pktmbuf_pool);
394         if(extra != NULL) {
395                 printf("Error pool not empty");
396                 ret = -1;
397         }
398         /* free them */
399         for (i=0; i<NB_MBUF; i++) {
400                 if (m[i] != NULL)
401                         rte_pktmbuf_free(m[i]);
402         }
403
404         return ret;
405 }
406
407 /*
408  * test that the pointer to the data on a packet mbuf is set properly
409  */
410 static int
411 test_pktmbuf_pool_ptr(void)
412 {
413         unsigned i;
414         struct rte_mbuf *m[NB_MBUF];
415         int ret = 0;
416
417         for (i=0; i<NB_MBUF; i++)
418                 m[i] = NULL;
419
420         /* alloc NB_MBUF mbufs */
421         for (i=0; i<NB_MBUF; i++) {
422                 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
423                 if (m[i] == NULL) {
424                         printf("rte_pktmbuf_alloc() failed (%u)\n", i);
425                         ret = -1;
426                 }
427                 m[i]->data_off += 64;
428         }
429
430         /* free them */
431         for (i=0; i<NB_MBUF; i++) {
432                 if (m[i] != NULL)
433                         rte_pktmbuf_free(m[i]);
434         }
435
436         for (i=0; i<NB_MBUF; i++)
437                 m[i] = NULL;
438
439         /* alloc NB_MBUF mbufs */
440         for (i=0; i<NB_MBUF; i++) {
441                 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
442                 if (m[i] == NULL) {
443                         printf("rte_pktmbuf_alloc() failed (%u)\n", i);
444                         ret = -1;
445                 }
446                 if (m[i]->data_off != RTE_PKTMBUF_HEADROOM) {
447                         printf("invalid data_off\n");
448                         ret = -1;
449                 }
450         }
451
452         /* free them */
453         for (i=0; i<NB_MBUF; i++) {
454                 if (m[i] != NULL)
455                         rte_pktmbuf_free(m[i]);
456         }
457
458         return ret;
459 }
460
461 static int
462 test_pktmbuf_free_segment(void)
463 {
464         unsigned i;
465         struct rte_mbuf *m[NB_MBUF];
466         int ret = 0;
467
468         for (i=0; i<NB_MBUF; i++)
469                 m[i] = NULL;
470
471         /* alloc NB_MBUF mbufs */
472         for (i=0; i<NB_MBUF; i++) {
473                 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
474                 if (m[i] == NULL) {
475                         printf("rte_pktmbuf_alloc() failed (%u)\n", i);
476                         ret = -1;
477                 }
478         }
479
480         /* free them */
481         for (i=0; i<NB_MBUF; i++) {
482                 if (m[i] != NULL) {
483                         struct rte_mbuf *mb, *mt;
484
485                         mb = m[i];
486                         while(mb != NULL) {
487                                 mt = mb;
488                                 mb = mb->next;
489                                 rte_pktmbuf_free_seg(mt);
490                         }
491                 }
492         }
493
494         return ret;
495 }
496
497 /*
498  * Stress test for rte_mbuf atomic refcnt.
499  * Implies that RTE_MBUF_REFCNT_ATOMIC is defined.
500  * For more efficency, recomended to run with RTE_LIBRTE_MBUF_DEBUG defined.
501  */
502
503 #ifdef RTE_MBUF_REFCNT_ATOMIC
504
505 static int
506 test_refcnt_slave(__attribute__((unused)) void *arg)
507 {
508         unsigned lcore, free;
509         void *mp = 0;
510
511         lcore = rte_lcore_id();
512         printf("%s started at lcore %u\n", __func__, lcore);
513
514         free = 0;
515         while (refcnt_stop_slaves == 0) {
516                 if (rte_ring_dequeue(refcnt_mbuf_ring, &mp) == 0) {
517                         free++;
518                         rte_pktmbuf_free((struct rte_mbuf *)mp);
519                 }
520         }
521
522         refcnt_lcore[lcore] += free;
523         printf("%s finished at lcore %u, "
524                "number of freed mbufs: %u\n",
525                __func__, lcore, free);
526         return (0);
527 }
528
529 static void
530 test_refcnt_iter(unsigned lcore, unsigned iter)
531 {
532         uint16_t ref;
533         unsigned i, n, tref, wn;
534         struct rte_mbuf *m;
535
536         tref = 0;
537
538         /* For each mbuf in the pool:
539          * - allocate mbuf,
540          * - increment it's reference up to N+1,
541          * - enqueue it N times into the ring for slave cores to free.
542          */
543         for (i = 0, n = rte_mempool_count(refcnt_pool);
544             i != n && (m = rte_pktmbuf_alloc(refcnt_pool)) != NULL;
545             i++) {
546                 ref = RTE_MAX(rte_rand() % REFCNT_MAX_REF, 1UL);
547                 tref += ref;
548                 if ((ref & 1) != 0) {
549                         rte_pktmbuf_refcnt_update(m, ref);
550                         while (ref-- != 0)
551                                 rte_ring_enqueue(refcnt_mbuf_ring, m);
552                 } else {
553                         while (ref-- != 0) {
554                                 rte_pktmbuf_refcnt_update(m, 1);
555                                 rte_ring_enqueue(refcnt_mbuf_ring, m);
556                         }
557                 }
558                 rte_pktmbuf_free(m);
559         }
560
561         if (i != n)
562                 rte_panic("(lcore=%u, iter=%u): was able to allocate only "
563                           "%u from %u mbufs\n", lcore, iter, i, n);
564
565         /* wait till slave lcores  will consume all mbufs */
566         while (!rte_ring_empty(refcnt_mbuf_ring))
567                 ;
568
569         /* check that all mbufs are back into mempool by now */
570         for (wn = 0; wn != REFCNT_MAX_TIMEOUT; wn++) {
571                 if ((i = rte_mempool_count(refcnt_pool)) == n) {
572                         refcnt_lcore[lcore] += tref;
573                         printf("%s(lcore=%u, iter=%u) completed, "
574                             "%u references processed\n",
575                             __func__, lcore, iter, tref);
576                         return;
577                 }
578                 rte_delay_ms(1000);
579         }
580
581         rte_panic("(lcore=%u, iter=%u): after %us only "
582                   "%u of %u mbufs left free\n", lcore, iter, wn, i, n);
583 }
584
585 static int
586 test_refcnt_master(void)
587 {
588         unsigned i, lcore;
589
590         lcore = rte_lcore_id();
591         printf("%s started at lcore %u\n", __func__, lcore);
592
593         for (i = 0; i != REFCNT_MAX_ITER; i++)
594                 test_refcnt_iter(lcore, i);
595
596         refcnt_stop_slaves = 1;
597         rte_wmb();
598
599         printf("%s finished at lcore %u\n", __func__, lcore);
600         return (0);
601 }
602
603 #endif
604
605 static int
606 test_refcnt_mbuf(void)
607 {
608 #ifdef RTE_MBUF_REFCNT_ATOMIC
609
610         unsigned lnum, master, slave, tref;
611
612
613         if ((lnum = rte_lcore_count()) == 1) {
614                 printf("skipping %s, number of lcores: %u is not enough\n",
615                     __func__, lnum);
616                 return (0);
617         }
618
619         printf("starting %s, at %u lcores\n", __func__, lnum);
620
621         /* create refcnt pool & ring if they don't exist */
622
623         if (refcnt_pool == NULL &&
624                         (refcnt_pool = rte_pktmbuf_pool_create(
625                                 MAKE_STRING(refcnt_pool),
626                                 REFCNT_MBUF_NUM, 0, 0, 0,
627                                 SOCKET_ID_ANY)) == NULL) {
628                 printf("%s: cannot allocate " MAKE_STRING(refcnt_pool) "\n",
629                     __func__);
630                 return (-1);
631         }
632
633         if (refcnt_mbuf_ring == NULL &&
634                         (refcnt_mbuf_ring = rte_ring_create("refcnt_mbuf_ring",
635                         REFCNT_RING_SIZE, SOCKET_ID_ANY,
636                         RING_F_SP_ENQ)) == NULL) {
637                 printf("%s: cannot allocate " MAKE_STRING(refcnt_mbuf_ring)
638                     "\n", __func__);
639                 return (-1);
640         }
641
642         refcnt_stop_slaves = 0;
643         memset(refcnt_lcore, 0, sizeof (refcnt_lcore));
644
645         rte_eal_mp_remote_launch(test_refcnt_slave, NULL, SKIP_MASTER);
646
647         test_refcnt_master();
648
649         rte_eal_mp_wait_lcore();
650
651         /* check that we porcessed all references */
652         tref = 0;
653         master = rte_get_master_lcore();
654
655         RTE_LCORE_FOREACH_SLAVE(slave)
656                 tref += refcnt_lcore[slave];
657
658         if (tref != refcnt_lcore[master])
659                 rte_panic("refernced mbufs: %u, freed mbufs: %u\n",
660                           tref, refcnt_lcore[master]);
661
662         rte_mempool_dump(stdout, refcnt_pool);
663         rte_ring_dump(stdout, refcnt_mbuf_ring);
664
665 #endif
666         return (0);
667 }
668
669 #include <unistd.h>
670 #include <sys/wait.h>
671
672 /* use fork() to test mbuf errors panic */
673 static int
674 verify_mbuf_check_panics(struct rte_mbuf *buf)
675 {
676         int pid;
677         int status;
678
679         pid = fork();
680
681         if (pid == 0) {
682                 rte_mbuf_sanity_check(buf, 1); /* should panic */
683                 exit(0);  /* return normally if it doesn't panic */
684         } else if (pid < 0){
685                 printf("Fork Failed\n");
686                 return -1;
687         }
688         wait(&status);
689         if(status == 0)
690                 return -1;
691
692         return 0;
693 }
694
695 static int
696 test_failing_mbuf_sanity_check(void)
697 {
698         struct rte_mbuf *buf;
699         struct rte_mbuf badbuf;
700
701         printf("Checking rte_mbuf_sanity_check for failure conditions\n");
702
703         /* get a good mbuf to use to make copies */
704         buf = rte_pktmbuf_alloc(pktmbuf_pool);
705         if (buf == NULL)
706                 return -1;
707         printf("Checking good mbuf initially\n");
708         if (verify_mbuf_check_panics(buf) != -1)
709                 return -1;
710
711         printf("Now checking for error conditions\n");
712
713         if (verify_mbuf_check_panics(NULL)) {
714                 printf("Error with NULL mbuf test\n");
715                 return -1;
716         }
717
718         badbuf = *buf;
719         badbuf.pool = NULL;
720         if (verify_mbuf_check_panics(&badbuf)) {
721                 printf("Error with bad-pool mbuf test\n");
722                 return -1;
723         }
724
725         badbuf = *buf;
726         badbuf.buf_physaddr = 0;
727         if (verify_mbuf_check_panics(&badbuf)) {
728                 printf("Error with bad-physaddr mbuf test\n");
729                 return -1;
730         }
731
732         badbuf = *buf;
733         badbuf.buf_addr = NULL;
734         if (verify_mbuf_check_panics(&badbuf)) {
735                 printf("Error with bad-addr mbuf test\n");
736                 return -1;
737         }
738
739         badbuf = *buf;
740         badbuf.refcnt = 0;
741         if (verify_mbuf_check_panics(&badbuf)) {
742                 printf("Error with bad-refcnt(0) mbuf test\n");
743                 return -1;
744         }
745
746         badbuf = *buf;
747         badbuf.refcnt = UINT16_MAX;
748         if (verify_mbuf_check_panics(&badbuf)) {
749                 printf("Error with bad-refcnt(MAX) mbuf test\n");
750                 return -1;
751         }
752
753         return 0;
754 }
755
756
757 static int
758 test_mbuf(void)
759 {
760         RTE_BUILD_BUG_ON(sizeof(struct rte_mbuf) != RTE_CACHE_LINE_SIZE * 2);
761
762         /* create pktmbuf pool if it does not exist */
763         if (pktmbuf_pool == NULL) {
764                 pktmbuf_pool = rte_pktmbuf_pool_create("test_pktmbuf_pool",
765                         NB_MBUF, 32, 0, MBUF_DATA_SIZE, SOCKET_ID_ANY);
766         }
767
768         if (pktmbuf_pool == NULL) {
769                 printf("cannot allocate mbuf pool\n");
770                 return -1;
771         }
772
773         /* test multiple mbuf alloc */
774         if (test_pktmbuf_pool() < 0) {
775                 printf("test_mbuf_pool() failed\n");
776                 return -1;
777         }
778
779         /* do it another time to check that all mbufs were freed */
780         if (test_pktmbuf_pool() < 0) {
781                 printf("test_mbuf_pool() failed (2)\n");
782                 return -1;
783         }
784
785         /* test that the pointer to the data on a packet mbuf is set properly */
786         if (test_pktmbuf_pool_ptr() < 0) {
787                 printf("test_pktmbuf_pool_ptr() failed\n");
788                 return -1;
789         }
790
791         /* test data manipulation in mbuf */
792         if (test_one_pktmbuf() < 0) {
793                 printf("test_one_mbuf() failed\n");
794                 return -1;
795         }
796
797
798         /*
799          * do it another time, to check that allocation reinitialize
800          * the mbuf correctly
801          */
802         if (test_one_pktmbuf() < 0) {
803                 printf("test_one_mbuf() failed (2)\n");
804                 return -1;
805         }
806
807         if (test_pktmbuf_with_non_ascii_data() < 0) {
808                 printf("test_pktmbuf_with_non_ascii_data() failed\n");
809                 return -1;
810         }
811
812         /* test free pktmbuf segment one by one */
813         if (test_pktmbuf_free_segment() < 0) {
814                 printf("test_pktmbuf_free_segment() failed.\n");
815                 return -1;
816         }
817
818         if (testclone_testupdate_testdetach()<0){
819                 printf("testclone_and_testupdate() failed \n");
820                 return -1;
821         }
822
823         if (test_refcnt_mbuf()<0){
824                 printf("test_refcnt_mbuf() failed \n");
825                 return -1;
826         }
827
828         if (test_failing_mbuf_sanity_check() < 0) {
829                 printf("test_failing_mbuf_sanity_check() failed\n");
830                 return -1;
831         }
832         return 0;
833 }
834
835 static struct test_command mbuf_cmd = {
836         .command = "mbuf_autotest",
837         .callback = test_mbuf,
838 };
839 REGISTER_TEST_COMMAND(mbuf_cmd);