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