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