update copyright date to 2013
[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
463 static int
464 test_pktmbuf_free_segment(void)
465 {
466         unsigned i;
467         struct rte_mbuf *m[NB_MBUF];
468         int ret = 0;
469
470         for (i=0; i<NB_MBUF; i++)
471                 m[i] = NULL;
472
473         /* alloc NB_MBUF mbufs */
474         for (i=0; i<NB_MBUF; i++) {
475                 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
476                 if (m[i] == NULL) {
477                         printf("rte_pktmbuf_alloc() failed (%u)\n", i);
478                         ret = -1;
479                 }
480         }
481
482         /* free them */
483         for (i=0; i<NB_MBUF; i++) {
484                 if (m[i] != NULL) {
485                         struct rte_mbuf *mb, *mt;
486
487                         mb = m[i];
488                         while(mb != NULL) {
489                                 mt = mb;
490                                 mb = mb->pkt.next;
491                                 rte_pktmbuf_free_seg(mt);
492                         }
493                 }
494         }
495
496         return ret;
497 }
498
499 /*
500  * Stress test for rte_mbuf atomic refcnt.
501  * Implies that:
502  * RTE_MBUF_SCATTER_GATHER and RTE_MBUF_REFCNT_ATOMIC are both defined.
503  * For more efficency, recomended to run with RTE_LIBRTE_MBUF_DEBUG defined.
504  */
505
506 #if defined RTE_MBUF_SCATTER_GATHER  && defined RTE_MBUF_REFCNT_ATOMIC
507
508 static int
509 test_refcnt_slave(__attribute__((unused)) void *arg)
510 {
511         unsigned lcore, free;
512         void *mp = 0;
513
514         lcore = rte_lcore_id();
515         printf("%s started at lcore %u\n", __func__, lcore);
516
517         free = 0;
518         while (refcnt_stop_slaves == 0) {
519                 if (rte_ring_dequeue(refcnt_mbuf_ring, &mp) == 0) {
520                         free++;
521                         rte_pktmbuf_free((struct rte_mbuf *)mp);
522                 }
523         }
524
525         refcnt_lcore[lcore] += free;
526         printf("%s finished at lcore %u, "
527                "number of freed mbufs: %u\n",
528                __func__, lcore, free);
529         return (0);
530 }
531
532 static void
533 test_refcnt_iter(unsigned lcore, unsigned iter)
534 {
535         uint16_t ref;
536         unsigned i, n, tref, wn;
537         struct rte_mbuf *m;
538
539         tref = 0;
540
541         /* For each mbuf in the pool:
542          * - allocate mbuf,
543          * - increment it's reference up to N+1,
544          * - enqueue it N times into the ring for slave cores to free.
545          */
546         for (i = 0, n = rte_mempool_count(refcnt_pool);
547             i != n && (m = rte_pktmbuf_alloc(refcnt_pool)) != NULL;
548             i++) {
549                 ref = RTE_MAX(rte_rand() % REFCNT_MAX_REF, 1UL);
550                 tref += ref;
551                 if ((ref & 1) != 0) {
552                         rte_pktmbuf_refcnt_update(m, ref);
553                         while (ref-- != 0)
554                                 rte_ring_enqueue(refcnt_mbuf_ring, m);
555                 } else {
556                         while (ref-- != 0) {
557                                 rte_pktmbuf_refcnt_update(m, 1);
558                                 rte_ring_enqueue(refcnt_mbuf_ring, m);
559                         }
560                 }
561                 rte_pktmbuf_free(m);
562         }
563
564         if (i != n)
565                 rte_panic("(lcore=%u, iter=%u): was able to allocate only "
566                           "%u from %u mbufs\n", lcore, iter, i, n);
567
568         /* wait till slave lcores  will consume all mbufs */
569         while (!rte_ring_empty(refcnt_mbuf_ring))
570                 ;
571
572         /* check that all mbufs are back into mempool by now */
573         for (wn = 0; wn != REFCNT_MAX_TIMEOUT; wn++) {
574                 if ((i = rte_mempool_count(refcnt_pool)) == n) {
575                         refcnt_lcore[lcore] += tref;
576                         printf("%s(lcore=%u, iter=%u) completed, "
577                             "%u references processed\n",
578                             __func__, lcore, iter, tref);
579                         return;
580                 }
581                 rte_delay_ms(1000);
582         }
583
584         rte_panic("(lcore=%u, iter=%u): after %us only "
585                   "%u of %u mbufs left free\n", lcore, iter, wn, i, n);
586 }
587
588 static int
589 test_refcnt_master(void)
590 {
591         unsigned i, lcore;
592
593         lcore = rte_lcore_id();
594         printf("%s started at lcore %u\n", __func__, lcore);
595
596         for (i = 0; i != REFCNT_MAX_ITER; i++)
597                 test_refcnt_iter(lcore, i);
598
599         refcnt_stop_slaves = 1;
600         rte_wmb();
601
602         printf("%s finished at lcore %u\n", __func__, lcore);
603         return (0);
604 }
605
606 #endif
607
608 static int
609 test_refcnt_mbuf(void)
610 {
611 #if defined RTE_MBUF_SCATTER_GATHER  && defined RTE_MBUF_REFCNT_ATOMIC
612
613         unsigned lnum, master, slave, tref;
614
615
616         if ((lnum = rte_lcore_count()) == 1) {
617                 printf("skipping %s, number of lcores: %u is not enough\n",
618                     __func__, lnum);
619                 return (0);
620         }
621
622         printf("starting %s, at %u lcores\n", __func__, lnum);
623
624         /* create refcnt pool & ring if they don't exist */
625
626         if (refcnt_pool == NULL &&
627                         (refcnt_pool = rte_mempool_create(
628                         MAKE_STRING(refcnt_pool),
629                         REFCNT_MBUF_NUM, REFCNT_MBUF_SIZE, 0,
630                         sizeof(struct rte_pktmbuf_pool_private),
631                         rte_pktmbuf_pool_init, NULL, rte_pktmbuf_init, NULL,
632                         SOCKET_ID_ANY, 0)) == NULL) {
633                 printf("%s: cannot allocate " MAKE_STRING(refcnt_pool) "\n",
634                     __func__);
635                 return (-1);
636         }
637
638         if (refcnt_mbuf_ring == NULL &&
639                         (refcnt_mbuf_ring = rte_ring_create("refcnt_mbuf_ring",
640                         REFCNT_RING_SIZE, SOCKET_ID_ANY,
641                         RING_F_SP_ENQ)) == NULL) {
642                 printf("%s: cannot allocate " MAKE_STRING(refcnt_mbuf_ring)
643                     "\n", __func__);
644                 return (-1);
645         }
646
647         refcnt_stop_slaves = 0;
648         memset(refcnt_lcore, 0, sizeof (refcnt_lcore));
649
650         rte_eal_mp_remote_launch(test_refcnt_slave, NULL, SKIP_MASTER);
651
652         test_refcnt_master();
653
654         rte_eal_mp_wait_lcore();
655
656         /* check that we porcessed all references */
657         tref = 0;
658         master = rte_get_master_lcore();
659
660         RTE_LCORE_FOREACH_SLAVE(slave)
661                 tref += refcnt_lcore[slave];
662
663         if (tref != refcnt_lcore[master])
664                 rte_panic("refernced mbufs: %u, freed mbufs: %u\n",
665                           tref, refcnt_lcore[master]);
666
667         rte_mempool_dump(refcnt_pool);
668         rte_ring_dump(refcnt_mbuf_ring);
669
670 #endif
671         return (0);
672 }
673
674 #ifdef RTE_EXEC_ENV_BAREMETAL
675
676 /* baremetal - don't test failing sanity checks */
677 static int
678 test_failing_mbuf_sanity_check(void)
679 {
680         return 0;
681 }
682
683 #else
684
685 #include <unistd.h>
686 #include <sys/wait.h>
687
688 /* linuxapp - use fork() to test mbuf errors panic */
689 static int
690 verify_mbuf_check_panics(struct rte_mbuf *buf)
691 {
692         int pid;
693         int status;
694
695         pid = fork();
696
697         if (pid == 0) {
698                 rte_mbuf_sanity_check(buf, RTE_MBUF_PKT, 1); /* should panic */
699                 exit(0);  /* return normally if it doesn't panic */
700         } else if (pid < 0){
701                 printf("Fork Failed\n");
702                 return -1;
703         }
704         wait(&status);
705         if(status == 0)
706                 return -1;
707
708         return 0;
709 }
710
711 static int
712 test_failing_mbuf_sanity_check(void)
713 {
714         struct rte_mbuf *buf;
715         struct rte_mbuf badbuf;
716
717         printf("Checking rte_mbuf_sanity_check for failure conditions\n");
718
719         /* get a good mbuf to use to make copies */
720         buf = rte_pktmbuf_alloc(pktmbuf_pool);
721         if (buf == NULL)
722                 return -1;
723         printf("Checking good mbuf initially\n");
724         if (verify_mbuf_check_panics(buf) != -1)
725                 return -1;
726
727         printf("Now checking for error conditions\n");
728
729         if (verify_mbuf_check_panics(NULL)) {
730                 printf("Error with NULL mbuf test\n");
731                 return -1;
732         }
733
734         badbuf = *buf;
735         badbuf.type = (uint8_t)-1;
736         if (verify_mbuf_check_panics(&badbuf)) {
737                 printf("Error with bad-type mbuf test\n");
738                 return -1;
739         }
740
741         badbuf = *buf;
742         badbuf.pool = NULL;
743         if (verify_mbuf_check_panics(&badbuf)) {
744                 printf("Error with bad-pool mbuf test\n");
745                 return -1;
746         }
747
748         badbuf = *buf;
749         badbuf.buf_physaddr = 0;
750         if (verify_mbuf_check_panics(&badbuf)) {
751                 printf("Error with bad-physaddr mbuf test\n");
752                 return -1;
753         }
754
755         badbuf = *buf;
756         badbuf.buf_addr = NULL;
757         if (verify_mbuf_check_panics(&badbuf)) {
758                 printf("Error with bad-addr mbuf test\n");
759                 return -1;
760         }
761
762 #ifdef RTE_MBUF_SCATTER_GATHER
763         badbuf = *buf;
764         badbuf.refcnt = 0;
765         if (verify_mbuf_check_panics(&badbuf)) {
766                 printf("Error with bad-refcnt(0) mbuf test\n");
767                 return -1;
768         }
769
770         badbuf = *buf;
771         badbuf.refcnt = UINT16_MAX;
772         if (verify_mbuf_check_panics(&badbuf)) {
773                 printf("Error with bad-refcnt(MAX) mbuf test\n");
774                 return -1;
775         }
776 #endif
777
778         return 0;
779 }
780 #endif
781
782
783 int
784 test_mbuf(void)
785 {
786         RTE_BUILD_BUG_ON(sizeof(struct rte_mbuf) != 64);
787
788         /* create pktmbuf pool if it does not exist */
789         if (pktmbuf_pool == NULL) {
790                 pktmbuf_pool =
791                         rte_mempool_create("test_pktmbuf_pool", NB_MBUF,
792                                            MBUF_SIZE, 32,
793                                            sizeof(struct rte_pktmbuf_pool_private),
794                                            rte_pktmbuf_pool_init, NULL,
795                                            rte_pktmbuf_init, NULL,
796                                            SOCKET_ID_ANY, 0);
797         }
798
799         if (pktmbuf_pool == NULL) {
800                 printf("cannot allocate mbuf pool\n");
801                 return -1;
802         }
803
804         /* test multiple mbuf alloc */
805         if (test_pktmbuf_pool() < 0) {
806                 printf("test_mbuf_pool() failed\n");
807                 return -1;
808         }
809
810         /* do it another time to check that all mbufs were freed */
811         if (test_pktmbuf_pool() < 0) {
812                 printf("test_mbuf_pool() failed (2)\n");
813                 return -1;
814         }
815
816         /* test data manipulation in mbuf */
817         if (test_one_pktmbuf() < 0) {
818                 printf("test_one_mbuf() failed\n");
819                 return -1;
820         }
821
822
823         /*
824          * do it another time, to check that allocation reinitialize
825          * the mbuf correctly
826          */
827         if (test_one_pktmbuf() < 0) {
828                 printf("test_one_mbuf() failed (2)\n");
829                 return -1;
830         }
831
832         if (test_pktmbuf_with_non_ascii_data() < 0) {
833                 printf("test_pktmbuf_with_non_ascii_data() failed\n");
834                 return -1;
835         }
836
837         /* create ctrlmbuf pool if it does not exist */
838         if (ctrlmbuf_pool == NULL) {
839                 ctrlmbuf_pool =
840                         rte_mempool_create("test_ctrlmbuf_pool", NB_MBUF,
841                                            sizeof(struct rte_mbuf), 32, 0,
842                                            NULL, NULL,
843                                            rte_ctrlmbuf_init, NULL,
844                                            SOCKET_ID_ANY, 0);
845         }
846
847         /* test control mbuf */
848         if (test_one_ctrlmbuf() < 0) {
849                 printf("test_one_ctrlmbuf() failed\n");
850                 return -1;
851         }
852
853         /* test free pktmbuf segment one by one */
854         if (test_pktmbuf_free_segment() < 0) {
855                 printf("test_pktmbuf_free_segment() failed.\n");
856                 return -1;
857         }
858
859         if (testclone_testupdate_testdetach()<0){
860                 printf("testclone_and_testupdate() failed \n");
861                 return -1;
862         }
863
864         if (test_refcnt_mbuf()<0){
865                 printf("test_refcnt_mbuf() failed \n");
866                 return -1;
867         }
868
869         if (test_failing_mbuf_sanity_check() < 0) {
870                 printf("test_failing_mbuf_sanity_check() failed\n");
871                 return -1;
872         }
873         return 0;
874 }