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