app/test: rename mbuf variable
[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 *m = NULL;
324         struct rte_mbuf *clone = NULL;
325
326         /* alloc a mbuf */
327         m = rte_pktmbuf_alloc(pktmbuf_pool);
328         if (m == NULL)
329                 GOTO_FAIL("ooops not allocating mbuf");
330
331         if (rte_pktmbuf_pkt_len(m) != 0)
332                 GOTO_FAIL("Bad length");
333
334
335         /* clone the allocated mbuf */
336         clone = rte_pktmbuf_clone(m, pktmbuf_pool);
337         if (clone == NULL)
338                 GOTO_FAIL("cannot clone data\n");
339         rte_pktmbuf_free(clone);
340
341         m->next = rte_pktmbuf_alloc(pktmbuf_pool);
342         if (m->next == NULL)
343                 GOTO_FAIL("Next Pkt Null\n");
344
345         clone = rte_pktmbuf_clone(m, pktmbuf_pool);
346         if (clone == NULL)
347                 GOTO_FAIL("cannot clone data\n");
348
349         /* free mbuf */
350         rte_pktmbuf_free(m);
351         rte_pktmbuf_free(clone);
352         m = NULL;
353         clone = NULL;
354         return 0;
355
356 fail:
357         if (m)
358                 rte_pktmbuf_free(m);
359         return -1;
360 }
361 #undef GOTO_FAIL
362
363
364
365 /*
366  * test allocation and free of mbufs
367  */
368 static int
369 test_pktmbuf_pool(void)
370 {
371         unsigned i;
372         struct rte_mbuf *m[NB_MBUF];
373         int ret = 0;
374
375         for (i=0; i<NB_MBUF; i++)
376                 m[i] = NULL;
377
378         /* alloc NB_MBUF mbufs */
379         for (i=0; i<NB_MBUF; i++) {
380                 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
381                 if (m[i] == NULL) {
382                         printf("rte_pktmbuf_alloc() failed (%u)\n", i);
383                         ret = -1;
384                 }
385         }
386         struct rte_mbuf *extra = NULL;
387         extra = rte_pktmbuf_alloc(pktmbuf_pool);
388         if(extra != NULL) {
389                 printf("Error pool not empty");
390                 ret = -1;
391         }
392         extra = rte_pktmbuf_clone(m[0], pktmbuf_pool);
393         if(extra != NULL) {
394                 printf("Error pool not empty");
395                 ret = -1;
396         }
397         /* free them */
398         for (i=0; i<NB_MBUF; i++) {
399                 if (m[i] != NULL)
400                         rte_pktmbuf_free(m[i]);
401         }
402
403         return ret;
404 }
405
406 /*
407  * test that the pointer to the data on a packet mbuf is set properly
408  */
409 static int
410 test_pktmbuf_pool_ptr(void)
411 {
412         unsigned i;
413         struct rte_mbuf *m[NB_MBUF];
414         int ret = 0;
415
416         for (i=0; i<NB_MBUF; i++)
417                 m[i] = NULL;
418
419         /* alloc NB_MBUF mbufs */
420         for (i=0; i<NB_MBUF; i++) {
421                 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
422                 if (m[i] == NULL) {
423                         printf("rte_pktmbuf_alloc() failed (%u)\n", i);
424                         ret = -1;
425                 }
426                 m[i]->data_off += 64;
427         }
428
429         /* free them */
430         for (i=0; i<NB_MBUF; i++) {
431                 if (m[i] != NULL)
432                         rte_pktmbuf_free(m[i]);
433         }
434
435         for (i=0; i<NB_MBUF; i++)
436                 m[i] = NULL;
437
438         /* alloc NB_MBUF mbufs */
439         for (i=0; i<NB_MBUF; i++) {
440                 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
441                 if (m[i] == NULL) {
442                         printf("rte_pktmbuf_alloc() failed (%u)\n", i);
443                         ret = -1;
444                 }
445                 if (m[i]->data_off != RTE_PKTMBUF_HEADROOM) {
446                         printf("invalid data_off\n");
447                         ret = -1;
448                 }
449         }
450
451         /* free them */
452         for (i=0; i<NB_MBUF; i++) {
453                 if (m[i] != NULL)
454                         rte_pktmbuf_free(m[i]);
455         }
456
457         return ret;
458 }
459
460 static int
461 test_pktmbuf_free_segment(void)
462 {
463         unsigned i;
464         struct rte_mbuf *m[NB_MBUF];
465         int ret = 0;
466
467         for (i=0; i<NB_MBUF; i++)
468                 m[i] = NULL;
469
470         /* alloc NB_MBUF mbufs */
471         for (i=0; i<NB_MBUF; i++) {
472                 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
473                 if (m[i] == NULL) {
474                         printf("rte_pktmbuf_alloc() failed (%u)\n", i);
475                         ret = -1;
476                 }
477         }
478
479         /* free them */
480         for (i=0; i<NB_MBUF; i++) {
481                 if (m[i] != NULL) {
482                         struct rte_mbuf *mb, *mt;
483
484                         mb = m[i];
485                         while(mb != NULL) {
486                                 mt = mb;
487                                 mb = mb->next;
488                                 rte_pktmbuf_free_seg(mt);
489                         }
490                 }
491         }
492
493         return ret;
494 }
495
496 /*
497  * Stress test for rte_mbuf atomic refcnt.
498  * Implies that RTE_MBUF_REFCNT_ATOMIC is defined.
499  * For more efficency, recomended to run with RTE_LIBRTE_MBUF_DEBUG defined.
500  */
501
502 #ifdef RTE_MBUF_REFCNT_ATOMIC
503
504 static int
505 test_refcnt_slave(__attribute__((unused)) void *arg)
506 {
507         unsigned lcore, free;
508         void *mp = 0;
509
510         lcore = rte_lcore_id();
511         printf("%s started at lcore %u\n", __func__, lcore);
512
513         free = 0;
514         while (refcnt_stop_slaves == 0) {
515                 if (rte_ring_dequeue(refcnt_mbuf_ring, &mp) == 0) {
516                         free++;
517                         rte_pktmbuf_free((struct rte_mbuf *)mp);
518                 }
519         }
520
521         refcnt_lcore[lcore] += free;
522         printf("%s finished at lcore %u, "
523                "number of freed mbufs: %u\n",
524                __func__, lcore, free);
525         return (0);
526 }
527
528 static void
529 test_refcnt_iter(unsigned lcore, unsigned iter)
530 {
531         uint16_t ref;
532         unsigned i, n, tref, wn;
533         struct rte_mbuf *m;
534
535         tref = 0;
536
537         /* For each mbuf in the pool:
538          * - allocate mbuf,
539          * - increment it's reference up to N+1,
540          * - enqueue it N times into the ring for slave cores to free.
541          */
542         for (i = 0, n = rte_mempool_count(refcnt_pool);
543             i != n && (m = rte_pktmbuf_alloc(refcnt_pool)) != NULL;
544             i++) {
545                 ref = RTE_MAX(rte_rand() % REFCNT_MAX_REF, 1UL);
546                 tref += ref;
547                 if ((ref & 1) != 0) {
548                         rte_pktmbuf_refcnt_update(m, ref);
549                         while (ref-- != 0)
550                                 rte_ring_enqueue(refcnt_mbuf_ring, m);
551                 } else {
552                         while (ref-- != 0) {
553                                 rte_pktmbuf_refcnt_update(m, 1);
554                                 rte_ring_enqueue(refcnt_mbuf_ring, m);
555                         }
556                 }
557                 rte_pktmbuf_free(m);
558         }
559
560         if (i != n)
561                 rte_panic("(lcore=%u, iter=%u): was able to allocate only "
562                           "%u from %u mbufs\n", lcore, iter, i, n);
563
564         /* wait till slave lcores  will consume all mbufs */
565         while (!rte_ring_empty(refcnt_mbuf_ring))
566                 ;
567
568         /* check that all mbufs are back into mempool by now */
569         for (wn = 0; wn != REFCNT_MAX_TIMEOUT; wn++) {
570                 if ((i = rte_mempool_count(refcnt_pool)) == n) {
571                         refcnt_lcore[lcore] += tref;
572                         printf("%s(lcore=%u, iter=%u) completed, "
573                             "%u references processed\n",
574                             __func__, lcore, iter, tref);
575                         return;
576                 }
577                 rte_delay_ms(1000);
578         }
579
580         rte_panic("(lcore=%u, iter=%u): after %us only "
581                   "%u of %u mbufs left free\n", lcore, iter, wn, i, n);
582 }
583
584 static int
585 test_refcnt_master(void)
586 {
587         unsigned i, lcore;
588
589         lcore = rte_lcore_id();
590         printf("%s started at lcore %u\n", __func__, lcore);
591
592         for (i = 0; i != REFCNT_MAX_ITER; i++)
593                 test_refcnt_iter(lcore, i);
594
595         refcnt_stop_slaves = 1;
596         rte_wmb();
597
598         printf("%s finished at lcore %u\n", __func__, lcore);
599         return (0);
600 }
601
602 #endif
603
604 static int
605 test_refcnt_mbuf(void)
606 {
607 #ifdef RTE_MBUF_REFCNT_ATOMIC
608
609         unsigned lnum, master, slave, tref;
610
611
612         if ((lnum = rte_lcore_count()) == 1) {
613                 printf("skipping %s, number of lcores: %u is not enough\n",
614                     __func__, lnum);
615                 return (0);
616         }
617
618         printf("starting %s, at %u lcores\n", __func__, lnum);
619
620         /* create refcnt pool & ring if they don't exist */
621
622         if (refcnt_pool == NULL &&
623                         (refcnt_pool = rte_pktmbuf_pool_create(
624                                 MAKE_STRING(refcnt_pool),
625                                 REFCNT_MBUF_NUM, 0, 0, 0,
626                                 SOCKET_ID_ANY)) == NULL) {
627                 printf("%s: cannot allocate " MAKE_STRING(refcnt_pool) "\n",
628                     __func__);
629                 return (-1);
630         }
631
632         if (refcnt_mbuf_ring == NULL &&
633                         (refcnt_mbuf_ring = rte_ring_create("refcnt_mbuf_ring",
634                         REFCNT_RING_SIZE, SOCKET_ID_ANY,
635                         RING_F_SP_ENQ)) == NULL) {
636                 printf("%s: cannot allocate " MAKE_STRING(refcnt_mbuf_ring)
637                     "\n", __func__);
638                 return (-1);
639         }
640
641         refcnt_stop_slaves = 0;
642         memset(refcnt_lcore, 0, sizeof (refcnt_lcore));
643
644         rte_eal_mp_remote_launch(test_refcnt_slave, NULL, SKIP_MASTER);
645
646         test_refcnt_master();
647
648         rte_eal_mp_wait_lcore();
649
650         /* check that we porcessed all references */
651         tref = 0;
652         master = rte_get_master_lcore();
653
654         RTE_LCORE_FOREACH_SLAVE(slave)
655                 tref += refcnt_lcore[slave];
656
657         if (tref != refcnt_lcore[master])
658                 rte_panic("refernced mbufs: %u, freed mbufs: %u\n",
659                           tref, refcnt_lcore[master]);
660
661         rte_mempool_dump(stdout, refcnt_pool);
662         rte_ring_dump(stdout, refcnt_mbuf_ring);
663
664 #endif
665         return (0);
666 }
667
668 #include <unistd.h>
669 #include <sys/wait.h>
670
671 /* use fork() to test mbuf errors panic */
672 static int
673 verify_mbuf_check_panics(struct rte_mbuf *buf)
674 {
675         int pid;
676         int status;
677
678         pid = fork();
679
680         if (pid == 0) {
681                 rte_mbuf_sanity_check(buf, 1); /* should panic */
682                 exit(0);  /* return normally if it doesn't panic */
683         } else if (pid < 0){
684                 printf("Fork Failed\n");
685                 return -1;
686         }
687         wait(&status);
688         if(status == 0)
689                 return -1;
690
691         return 0;
692 }
693
694 static int
695 test_failing_mbuf_sanity_check(void)
696 {
697         struct rte_mbuf *buf;
698         struct rte_mbuf badbuf;
699
700         printf("Checking rte_mbuf_sanity_check for failure conditions\n");
701
702         /* get a good mbuf to use to make copies */
703         buf = rte_pktmbuf_alloc(pktmbuf_pool);
704         if (buf == NULL)
705                 return -1;
706         printf("Checking good mbuf initially\n");
707         if (verify_mbuf_check_panics(buf) != -1)
708                 return -1;
709
710         printf("Now checking for error conditions\n");
711
712         if (verify_mbuf_check_panics(NULL)) {
713                 printf("Error with NULL mbuf test\n");
714                 return -1;
715         }
716
717         badbuf = *buf;
718         badbuf.pool = NULL;
719         if (verify_mbuf_check_panics(&badbuf)) {
720                 printf("Error with bad-pool mbuf test\n");
721                 return -1;
722         }
723
724         badbuf = *buf;
725         badbuf.buf_physaddr = 0;
726         if (verify_mbuf_check_panics(&badbuf)) {
727                 printf("Error with bad-physaddr mbuf test\n");
728                 return -1;
729         }
730
731         badbuf = *buf;
732         badbuf.buf_addr = NULL;
733         if (verify_mbuf_check_panics(&badbuf)) {
734                 printf("Error with bad-addr mbuf test\n");
735                 return -1;
736         }
737
738         badbuf = *buf;
739         badbuf.refcnt = 0;
740         if (verify_mbuf_check_panics(&badbuf)) {
741                 printf("Error with bad-refcnt(0) mbuf test\n");
742                 return -1;
743         }
744
745         badbuf = *buf;
746         badbuf.refcnt = UINT16_MAX;
747         if (verify_mbuf_check_panics(&badbuf)) {
748                 printf("Error with bad-refcnt(MAX) mbuf test\n");
749                 return -1;
750         }
751
752         return 0;
753 }
754
755
756 static int
757 test_mbuf(void)
758 {
759         RTE_BUILD_BUG_ON(sizeof(struct rte_mbuf) != RTE_CACHE_LINE_SIZE * 2);
760
761         /* create pktmbuf pool if it does not exist */
762         if (pktmbuf_pool == NULL) {
763                 pktmbuf_pool = rte_pktmbuf_pool_create("test_pktmbuf_pool",
764                         NB_MBUF, 32, 0, MBUF_DATA_SIZE, SOCKET_ID_ANY);
765         }
766
767         if (pktmbuf_pool == NULL) {
768                 printf("cannot allocate mbuf pool\n");
769                 return -1;
770         }
771
772         /* test multiple mbuf alloc */
773         if (test_pktmbuf_pool() < 0) {
774                 printf("test_mbuf_pool() failed\n");
775                 return -1;
776         }
777
778         /* do it another time to check that all mbufs were freed */
779         if (test_pktmbuf_pool() < 0) {
780                 printf("test_mbuf_pool() failed (2)\n");
781                 return -1;
782         }
783
784         /* test that the pointer to the data on a packet mbuf is set properly */
785         if (test_pktmbuf_pool_ptr() < 0) {
786                 printf("test_pktmbuf_pool_ptr() failed\n");
787                 return -1;
788         }
789
790         /* test data manipulation in mbuf */
791         if (test_one_pktmbuf() < 0) {
792                 printf("test_one_mbuf() failed\n");
793                 return -1;
794         }
795
796
797         /*
798          * do it another time, to check that allocation reinitialize
799          * the mbuf correctly
800          */
801         if (test_one_pktmbuf() < 0) {
802                 printf("test_one_mbuf() failed (2)\n");
803                 return -1;
804         }
805
806         if (test_pktmbuf_with_non_ascii_data() < 0) {
807                 printf("test_pktmbuf_with_non_ascii_data() failed\n");
808                 return -1;
809         }
810
811         /* test free pktmbuf segment one by one */
812         if (test_pktmbuf_free_segment() < 0) {
813                 printf("test_pktmbuf_free_segment() failed.\n");
814                 return -1;
815         }
816
817         if (testclone_testupdate_testdetach()<0){
818                 printf("testclone_and_testupdate() failed \n");
819                 return -1;
820         }
821
822         if (test_refcnt_mbuf()<0){
823                 printf("test_refcnt_mbuf() failed \n");
824                 return -1;
825         }
826
827         if (test_failing_mbuf_sanity_check() < 0) {
828                 printf("test_failing_mbuf_sanity_check() failed\n");
829                 return -1;
830         }
831         return 0;
832 }
833
834 static struct test_command mbuf_cmd = {
835         .command = "mbuf_autotest",
836         .callback = test_mbuf,
837 };
838 REGISTER_TEST_COMMAND(mbuf_cmd);