test/mbuf: add unit test on mbuf flag names
[dpdk.git] / app / test / test_mbuf.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <string.h>
6 #include <stdarg.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <stdint.h>
10 #include <inttypes.h>
11 #include <errno.h>
12 #include <sys/queue.h>
13
14 #include <rte_common.h>
15 #include <rte_errno.h>
16 #include <rte_debug.h>
17 #include <rte_log.h>
18 #include <rte_memory.h>
19 #include <rte_memcpy.h>
20 #include <rte_launch.h>
21 #include <rte_eal.h>
22 #include <rte_per_lcore.h>
23 #include <rte_lcore.h>
24 #include <rte_atomic.h>
25 #include <rte_branch_prediction.h>
26 #include <rte_ring.h>
27 #include <rte_mempool.h>
28 #include <rte_mbuf.h>
29 #include <rte_random.h>
30 #include <rte_cycles.h>
31 #include <rte_malloc.h>
32 #include <rte_ether.h>
33 #include <rte_ip.h>
34 #include <rte_tcp.h>
35
36 #include "test.h"
37
38 #define MEMPOOL_CACHE_SIZE      32
39 #define MBUF_DATA_SIZE          2048
40 #define NB_MBUF                 128
41 #define MBUF_TEST_DATA_LEN      1464
42 #define MBUF_TEST_DATA_LEN2     50
43 #define MBUF_TEST_DATA_LEN3     256
44 #define MBUF_TEST_HDR1_LEN      20
45 #define MBUF_TEST_HDR2_LEN      30
46 #define MBUF_TEST_ALL_HDRS_LEN  (MBUF_TEST_HDR1_LEN+MBUF_TEST_HDR2_LEN)
47 #define MBUF_TEST_SEG_SIZE      64
48 #define MBUF_TEST_BURST         8
49 #define EXT_BUF_TEST_DATA_LEN   1024
50 #define MBUF_MAX_SEG            16
51 #define MBUF_NO_HEADER          0
52 #define MBUF_HEADER             1
53 #define MBUF_NEG_TEST_READ      2
54 #define VAL_NAME(flag)          { flag, #flag }
55
56 /* chain length in bulk test */
57 #define CHAIN_LEN 16
58
59 /* size of private data for mbuf in pktmbuf_pool2 */
60 #define MBUF2_PRIV_SIZE         128
61
62 #define REFCNT_MAX_ITER         64
63 #define REFCNT_MAX_TIMEOUT      10
64 #define REFCNT_MAX_REF          (RTE_MAX_LCORE)
65 #define REFCNT_MBUF_NUM         64
66 #define REFCNT_RING_SIZE        (REFCNT_MBUF_NUM * REFCNT_MAX_REF)
67
68 #define MAGIC_DATA              0x42424242
69
70 #define MAKE_STRING(x)          # x
71
72 #ifdef RTE_MBUF_REFCNT_ATOMIC
73
74 static volatile uint32_t refcnt_stop_slaves;
75 static unsigned refcnt_lcore[RTE_MAX_LCORE];
76
77 #endif
78
79 /*
80  * MBUF
81  * ====
82  *
83  * #. Allocate a mbuf pool.
84  *
85  *    - The pool contains NB_MBUF elements, where each mbuf is MBUF_SIZE
86  *      bytes long.
87  *
88  * #. Test multiple allocations of mbufs from this pool.
89  *
90  *    - Allocate NB_MBUF and store pointers in a table.
91  *    - If an allocation fails, return an error.
92  *    - Free all these mbufs.
93  *    - Repeat the same test to check that mbufs were freed correctly.
94  *
95  * #. Test data manipulation in pktmbuf.
96  *
97  *    - Alloc an mbuf.
98  *    - Append data using rte_pktmbuf_append().
99  *    - Test for error in rte_pktmbuf_append() when len is too large.
100  *    - Trim data at the end of mbuf using rte_pktmbuf_trim().
101  *    - Test for error in rte_pktmbuf_trim() when len is too large.
102  *    - Prepend a header using rte_pktmbuf_prepend().
103  *    - Test for error in rte_pktmbuf_prepend() when len is too large.
104  *    - Remove data at the beginning of mbuf using rte_pktmbuf_adj().
105  *    - Test for error in rte_pktmbuf_adj() when len is too large.
106  *    - Check that appended data is not corrupt.
107  *    - Free the mbuf.
108  *    - Between all these tests, check data_len and pkt_len, and
109  *      that the mbuf is contiguous.
110  *    - Repeat the test to check that allocation operations
111  *      reinitialize the mbuf correctly.
112  *
113  * #. Test packet cloning
114  *    - Clone a mbuf and verify the data
115  *    - Clone the cloned mbuf and verify the data
116  *    - Attach a mbuf to another that does not have the same priv_size.
117  */
118
119 #define GOTO_FAIL(str, ...) do {                                        \
120                 printf("mbuf test FAILED (l.%d): <" str ">\n",          \
121                        __LINE__,  ##__VA_ARGS__);                       \
122                 goto fail;                                              \
123 } while(0)
124
125 /*
126  * test data manipulation in mbuf with non-ascii data
127  */
128 static int
129 test_pktmbuf_with_non_ascii_data(struct rte_mempool *pktmbuf_pool)
130 {
131         struct rte_mbuf *m = NULL;
132         char *data;
133
134         m = rte_pktmbuf_alloc(pktmbuf_pool);
135         if (m == NULL)
136                 GOTO_FAIL("Cannot allocate mbuf");
137         if (rte_pktmbuf_pkt_len(m) != 0)
138                 GOTO_FAIL("Bad length");
139
140         data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN);
141         if (data == NULL)
142                 GOTO_FAIL("Cannot append data");
143         if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
144                 GOTO_FAIL("Bad pkt length");
145         if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
146                 GOTO_FAIL("Bad data length");
147         memset(data, 0xff, rte_pktmbuf_pkt_len(m));
148         if (!rte_pktmbuf_is_contiguous(m))
149                 GOTO_FAIL("Buffer should be continuous");
150         rte_pktmbuf_dump(stdout, m, MBUF_TEST_DATA_LEN);
151
152         rte_pktmbuf_free(m);
153
154         return 0;
155
156 fail:
157         if(m) {
158                 rte_pktmbuf_free(m);
159         }
160         return -1;
161 }
162
163 /*
164  * test data manipulation in mbuf
165  */
166 static int
167 test_one_pktmbuf(struct rte_mempool *pktmbuf_pool)
168 {
169         struct rte_mbuf *m = NULL;
170         char *data, *data2, *hdr;
171         unsigned i;
172
173         printf("Test pktmbuf API\n");
174
175         /* alloc a mbuf */
176
177         m = rte_pktmbuf_alloc(pktmbuf_pool);
178         if (m == NULL)
179                 GOTO_FAIL("Cannot allocate mbuf");
180         if (rte_pktmbuf_pkt_len(m) != 0)
181                 GOTO_FAIL("Bad length");
182
183         rte_pktmbuf_dump(stdout, m, 0);
184
185         /* append data */
186
187         data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN);
188         if (data == NULL)
189                 GOTO_FAIL("Cannot append data");
190         if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
191                 GOTO_FAIL("Bad pkt length");
192         if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
193                 GOTO_FAIL("Bad data length");
194         memset(data, 0x66, rte_pktmbuf_pkt_len(m));
195         if (!rte_pktmbuf_is_contiguous(m))
196                 GOTO_FAIL("Buffer should be continuous");
197         rte_pktmbuf_dump(stdout, m, MBUF_TEST_DATA_LEN);
198         rte_pktmbuf_dump(stdout, m, 2*MBUF_TEST_DATA_LEN);
199
200         /* this append should fail */
201
202         data2 = rte_pktmbuf_append(m, (uint16_t)(rte_pktmbuf_tailroom(m) + 1));
203         if (data2 != NULL)
204                 GOTO_FAIL("Append should not succeed");
205
206         /* append some more data */
207
208         data2 = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN2);
209         if (data2 == NULL)
210                 GOTO_FAIL("Cannot append data");
211         if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_DATA_LEN2)
212                 GOTO_FAIL("Bad pkt length");
213         if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_DATA_LEN2)
214                 GOTO_FAIL("Bad data length");
215         if (!rte_pktmbuf_is_contiguous(m))
216                 GOTO_FAIL("Buffer should be continuous");
217
218         /* trim data at the end of mbuf */
219
220         if (rte_pktmbuf_trim(m, MBUF_TEST_DATA_LEN2) < 0)
221                 GOTO_FAIL("Cannot trim data");
222         if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
223                 GOTO_FAIL("Bad pkt length");
224         if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
225                 GOTO_FAIL("Bad data length");
226         if (!rte_pktmbuf_is_contiguous(m))
227                 GOTO_FAIL("Buffer should be continuous");
228
229         /* this trim should fail */
230
231         if (rte_pktmbuf_trim(m, (uint16_t)(rte_pktmbuf_data_len(m) + 1)) == 0)
232                 GOTO_FAIL("trim should not succeed");
233
234         /* prepend one header */
235
236         hdr = rte_pktmbuf_prepend(m, MBUF_TEST_HDR1_LEN);
237         if (hdr == NULL)
238                 GOTO_FAIL("Cannot prepend");
239         if (data - hdr != MBUF_TEST_HDR1_LEN)
240                 GOTO_FAIL("Prepend failed");
241         if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_HDR1_LEN)
242                 GOTO_FAIL("Bad pkt length");
243         if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_HDR1_LEN)
244                 GOTO_FAIL("Bad data length");
245         if (!rte_pktmbuf_is_contiguous(m))
246                 GOTO_FAIL("Buffer should be continuous");
247         memset(hdr, 0x55, MBUF_TEST_HDR1_LEN);
248
249         /* prepend another header */
250
251         hdr = rte_pktmbuf_prepend(m, MBUF_TEST_HDR2_LEN);
252         if (hdr == NULL)
253                 GOTO_FAIL("Cannot prepend");
254         if (data - hdr != MBUF_TEST_ALL_HDRS_LEN)
255                 GOTO_FAIL("Prepend failed");
256         if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_ALL_HDRS_LEN)
257                 GOTO_FAIL("Bad pkt length");
258         if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN + MBUF_TEST_ALL_HDRS_LEN)
259                 GOTO_FAIL("Bad data length");
260         if (!rte_pktmbuf_is_contiguous(m))
261                 GOTO_FAIL("Buffer should be continuous");
262         memset(hdr, 0x55, MBUF_TEST_HDR2_LEN);
263
264         rte_mbuf_sanity_check(m, 1);
265         rte_mbuf_sanity_check(m, 0);
266         rte_pktmbuf_dump(stdout, m, 0);
267
268         /* this prepend should fail */
269
270         hdr = rte_pktmbuf_prepend(m, (uint16_t)(rte_pktmbuf_headroom(m) + 1));
271         if (hdr != NULL)
272                 GOTO_FAIL("prepend should not succeed");
273
274         /* remove data at beginning of mbuf (adj) */
275
276         if (data != rte_pktmbuf_adj(m, MBUF_TEST_ALL_HDRS_LEN))
277                 GOTO_FAIL("rte_pktmbuf_adj failed");
278         if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN)
279                 GOTO_FAIL("Bad pkt length");
280         if (rte_pktmbuf_data_len(m) != MBUF_TEST_DATA_LEN)
281                 GOTO_FAIL("Bad data length");
282         if (!rte_pktmbuf_is_contiguous(m))
283                 GOTO_FAIL("Buffer should be continuous");
284
285         /* this adj should fail */
286
287         if (rte_pktmbuf_adj(m, (uint16_t)(rte_pktmbuf_data_len(m) + 1)) != NULL)
288                 GOTO_FAIL("rte_pktmbuf_adj should not succeed");
289
290         /* check data */
291
292         if (!rte_pktmbuf_is_contiguous(m))
293                 GOTO_FAIL("Buffer should be continuous");
294
295         for (i=0; i<MBUF_TEST_DATA_LEN; i++) {
296                 if (data[i] != 0x66)
297                         GOTO_FAIL("Data corrupted at offset %u", i);
298         }
299
300         /* free mbuf */
301
302         rte_pktmbuf_free(m);
303         m = NULL;
304         return 0;
305
306 fail:
307         if (m)
308                 rte_pktmbuf_free(m);
309         return -1;
310 }
311
312 static int
313 testclone_testupdate_testdetach(struct rte_mempool *pktmbuf_pool)
314 {
315         struct rte_mbuf *m = NULL;
316         struct rte_mbuf *clone = NULL;
317         struct rte_mbuf *clone2 = NULL;
318         unaligned_uint32_t *data;
319
320         /* alloc a mbuf */
321         m = rte_pktmbuf_alloc(pktmbuf_pool);
322         if (m == NULL)
323                 GOTO_FAIL("ooops not allocating mbuf");
324
325         if (rte_pktmbuf_pkt_len(m) != 0)
326                 GOTO_FAIL("Bad length");
327
328         rte_pktmbuf_append(m, sizeof(uint32_t));
329         data = rte_pktmbuf_mtod(m, unaligned_uint32_t *);
330         *data = MAGIC_DATA;
331
332         /* clone the allocated mbuf */
333         clone = rte_pktmbuf_clone(m, pktmbuf_pool);
334         if (clone == NULL)
335                 GOTO_FAIL("cannot clone data\n");
336
337         data = rte_pktmbuf_mtod(clone, unaligned_uint32_t *);
338         if (*data != MAGIC_DATA)
339                 GOTO_FAIL("invalid data in clone\n");
340
341         if (rte_mbuf_refcnt_read(m) != 2)
342                 GOTO_FAIL("invalid refcnt in m\n");
343
344         /* free the clone */
345         rte_pktmbuf_free(clone);
346         clone = NULL;
347
348         /* same test with a chained mbuf */
349         m->next = rte_pktmbuf_alloc(pktmbuf_pool);
350         if (m->next == NULL)
351                 GOTO_FAIL("Next Pkt Null\n");
352         m->nb_segs = 2;
353
354         rte_pktmbuf_append(m->next, sizeof(uint32_t));
355         m->pkt_len = 2 * sizeof(uint32_t);
356
357         data = rte_pktmbuf_mtod(m->next, unaligned_uint32_t *);
358         *data = MAGIC_DATA;
359
360         clone = rte_pktmbuf_clone(m, pktmbuf_pool);
361         if (clone == NULL)
362                 GOTO_FAIL("cannot clone data\n");
363
364         data = rte_pktmbuf_mtod(clone, unaligned_uint32_t *);
365         if (*data != MAGIC_DATA)
366                 GOTO_FAIL("invalid data in clone\n");
367
368         data = rte_pktmbuf_mtod(clone->next, unaligned_uint32_t *);
369         if (*data != MAGIC_DATA)
370                 GOTO_FAIL("invalid data in clone->next\n");
371
372         if (rte_mbuf_refcnt_read(m) != 2)
373                 GOTO_FAIL("invalid refcnt in m\n");
374
375         if (rte_mbuf_refcnt_read(m->next) != 2)
376                 GOTO_FAIL("invalid refcnt in m->next\n");
377
378         /* try to clone the clone */
379
380         clone2 = rte_pktmbuf_clone(clone, pktmbuf_pool);
381         if (clone2 == NULL)
382                 GOTO_FAIL("cannot clone the clone\n");
383
384         data = rte_pktmbuf_mtod(clone2, unaligned_uint32_t *);
385         if (*data != MAGIC_DATA)
386                 GOTO_FAIL("invalid data in clone2\n");
387
388         data = rte_pktmbuf_mtod(clone2->next, unaligned_uint32_t *);
389         if (*data != MAGIC_DATA)
390                 GOTO_FAIL("invalid data in clone2->next\n");
391
392         if (rte_mbuf_refcnt_read(m) != 3)
393                 GOTO_FAIL("invalid refcnt in m\n");
394
395         if (rte_mbuf_refcnt_read(m->next) != 3)
396                 GOTO_FAIL("invalid refcnt in m->next\n");
397
398         /* free mbuf */
399         rte_pktmbuf_free(m);
400         rte_pktmbuf_free(clone);
401         rte_pktmbuf_free(clone2);
402
403         m = NULL;
404         clone = NULL;
405         clone2 = NULL;
406         printf("%s ok\n", __func__);
407         return 0;
408
409 fail:
410         if (m)
411                 rte_pktmbuf_free(m);
412         if (clone)
413                 rte_pktmbuf_free(clone);
414         if (clone2)
415                 rte_pktmbuf_free(clone2);
416         return -1;
417 }
418
419 static int
420 test_pktmbuf_copy(struct rte_mempool *pktmbuf_pool)
421 {
422         struct rte_mbuf *m = NULL;
423         struct rte_mbuf *copy = NULL;
424         struct rte_mbuf *copy2 = NULL;
425         struct rte_mbuf *clone = NULL;
426         unaligned_uint32_t *data;
427
428         /* alloc a mbuf */
429         m = rte_pktmbuf_alloc(pktmbuf_pool);
430         if (m == NULL)
431                 GOTO_FAIL("ooops not allocating mbuf");
432
433         if (rte_pktmbuf_pkt_len(m) != 0)
434                 GOTO_FAIL("Bad length");
435
436         rte_pktmbuf_append(m, sizeof(uint32_t));
437         data = rte_pktmbuf_mtod(m, unaligned_uint32_t *);
438         *data = MAGIC_DATA;
439
440         /* copy the allocated mbuf */
441         copy = rte_pktmbuf_copy(m, pktmbuf_pool, 0, UINT32_MAX);
442         if (copy == NULL)
443                 GOTO_FAIL("cannot copy data\n");
444
445         if (rte_pktmbuf_pkt_len(copy) != sizeof(uint32_t))
446                 GOTO_FAIL("copy length incorrect\n");
447
448         if (rte_pktmbuf_data_len(copy) != sizeof(uint32_t))
449                 GOTO_FAIL("copy data length incorrect\n");
450
451         data = rte_pktmbuf_mtod(copy, unaligned_uint32_t *);
452         if (*data != MAGIC_DATA)
453                 GOTO_FAIL("invalid data in copy\n");
454
455         /* free the copy */
456         rte_pktmbuf_free(copy);
457         copy = NULL;
458
459         /* same test with a cloned mbuf */
460         clone = rte_pktmbuf_clone(m, pktmbuf_pool);
461         if (clone == NULL)
462                 GOTO_FAIL("cannot clone data\n");
463
464         if (!RTE_MBUF_CLONED(clone))
465                 GOTO_FAIL("clone did not give a cloned mbuf\n");
466
467         copy = rte_pktmbuf_copy(clone, pktmbuf_pool, 0, UINT32_MAX);
468         if (copy == NULL)
469                 GOTO_FAIL("cannot copy cloned mbuf\n");
470
471         if (RTE_MBUF_CLONED(copy))
472                 GOTO_FAIL("copy of clone is cloned?\n");
473
474         if (rte_pktmbuf_pkt_len(copy) != sizeof(uint32_t))
475                 GOTO_FAIL("copy clone length incorrect\n");
476
477         if (rte_pktmbuf_data_len(copy) != sizeof(uint32_t))
478                 GOTO_FAIL("copy clone data length incorrect\n");
479
480         data = rte_pktmbuf_mtod(copy, unaligned_uint32_t *);
481         if (*data != MAGIC_DATA)
482                 GOTO_FAIL("invalid data in clone copy\n");
483         rte_pktmbuf_free(clone);
484         rte_pktmbuf_free(copy);
485         copy = NULL;
486         clone = NULL;
487
488
489         /* same test with a chained mbuf */
490         m->next = rte_pktmbuf_alloc(pktmbuf_pool);
491         if (m->next == NULL)
492                 GOTO_FAIL("Next Pkt Null\n");
493         m->nb_segs = 2;
494
495         rte_pktmbuf_append(m->next, sizeof(uint32_t));
496         m->pkt_len = 2 * sizeof(uint32_t);
497         data = rte_pktmbuf_mtod(m->next, unaligned_uint32_t *);
498         *data = MAGIC_DATA + 1;
499
500         copy = rte_pktmbuf_copy(m, pktmbuf_pool, 0, UINT32_MAX);
501         if (copy == NULL)
502                 GOTO_FAIL("cannot copy data\n");
503
504         if (rte_pktmbuf_pkt_len(copy) != 2 * sizeof(uint32_t))
505                 GOTO_FAIL("chain copy length incorrect\n");
506
507         if (rte_pktmbuf_data_len(copy) != 2 * sizeof(uint32_t))
508                 GOTO_FAIL("chain copy data length incorrect\n");
509
510         data = rte_pktmbuf_mtod(copy, unaligned_uint32_t *);
511         if (data[0] != MAGIC_DATA || data[1] != MAGIC_DATA + 1)
512                 GOTO_FAIL("invalid data in copy\n");
513
514         rte_pktmbuf_free(copy2);
515
516         /* test offset copy */
517         copy2 = rte_pktmbuf_copy(copy, pktmbuf_pool,
518                                  sizeof(uint32_t), UINT32_MAX);
519         if (copy2 == NULL)
520                 GOTO_FAIL("cannot copy the copy\n");
521
522         if (rte_pktmbuf_pkt_len(copy2) != sizeof(uint32_t))
523                 GOTO_FAIL("copy with offset, length incorrect\n");
524
525         if (rte_pktmbuf_data_len(copy2) != sizeof(uint32_t))
526                 GOTO_FAIL("copy with offset, data length incorrect\n");
527
528         data = rte_pktmbuf_mtod(copy2, unaligned_uint32_t *);
529         if (data[0] != MAGIC_DATA + 1)
530                 GOTO_FAIL("copy with offset, invalid data\n");
531
532         rte_pktmbuf_free(copy2);
533
534         /* test truncation copy */
535         copy2 = rte_pktmbuf_copy(copy, pktmbuf_pool,
536                                  0, sizeof(uint32_t));
537         if (copy2 == NULL)
538                 GOTO_FAIL("cannot copy the copy\n");
539
540         if (rte_pktmbuf_pkt_len(copy2) != sizeof(uint32_t))
541                 GOTO_FAIL("copy with truncate, length incorrect\n");
542
543         if (rte_pktmbuf_data_len(copy2) != sizeof(uint32_t))
544                 GOTO_FAIL("copy with truncate, data length incorrect\n");
545
546         data = rte_pktmbuf_mtod(copy2, unaligned_uint32_t *);
547         if (data[0] != MAGIC_DATA)
548                 GOTO_FAIL("copy with truncate, invalid data\n");
549
550         /* free mbuf */
551         rte_pktmbuf_free(m);
552         rte_pktmbuf_free(copy);
553         rte_pktmbuf_free(copy2);
554
555         m = NULL;
556         copy = NULL;
557         copy2 = NULL;
558         printf("%s ok\n", __func__);
559         return 0;
560
561 fail:
562         if (m)
563                 rte_pktmbuf_free(m);
564         if (copy)
565                 rte_pktmbuf_free(copy);
566         if (copy2)
567                 rte_pktmbuf_free(copy2);
568         return -1;
569 }
570
571 static int
572 test_attach_from_different_pool(struct rte_mempool *pktmbuf_pool,
573                                 struct rte_mempool *pktmbuf_pool2)
574 {
575         struct rte_mbuf *m = NULL;
576         struct rte_mbuf *clone = NULL;
577         struct rte_mbuf *clone2 = NULL;
578         char *data, *c_data, *c_data2;
579
580         /* alloc a mbuf */
581         m = rte_pktmbuf_alloc(pktmbuf_pool);
582         if (m == NULL)
583                 GOTO_FAIL("cannot allocate mbuf");
584
585         if (rte_pktmbuf_pkt_len(m) != 0)
586                 GOTO_FAIL("Bad length");
587
588         data = rte_pktmbuf_mtod(m, char *);
589
590         /* allocate a new mbuf from the second pool, and attach it to the first
591          * mbuf */
592         clone = rte_pktmbuf_alloc(pktmbuf_pool2);
593         if (clone == NULL)
594                 GOTO_FAIL("cannot allocate mbuf from second pool\n");
595
596         /* check data room size and priv size, and erase priv */
597         if (rte_pktmbuf_data_room_size(clone->pool) != 0)
598                 GOTO_FAIL("data room size should be 0\n");
599         if (rte_pktmbuf_priv_size(clone->pool) != MBUF2_PRIV_SIZE)
600                 GOTO_FAIL("data room size should be %d\n", MBUF2_PRIV_SIZE);
601         memset(clone + 1, 0, MBUF2_PRIV_SIZE);
602
603         /* save data pointer to compare it after detach() */
604         c_data = rte_pktmbuf_mtod(clone, char *);
605         if (c_data != (char *)clone + sizeof(*clone) + MBUF2_PRIV_SIZE)
606                 GOTO_FAIL("bad data pointer in clone");
607         if (rte_pktmbuf_headroom(clone) != 0)
608                 GOTO_FAIL("bad headroom in clone");
609
610         rte_pktmbuf_attach(clone, m);
611
612         if (rte_pktmbuf_mtod(clone, char *) != data)
613                 GOTO_FAIL("clone was not attached properly\n");
614         if (rte_pktmbuf_headroom(clone) != RTE_PKTMBUF_HEADROOM)
615                 GOTO_FAIL("bad headroom in clone after attach");
616         if (rte_mbuf_refcnt_read(m) != 2)
617                 GOTO_FAIL("invalid refcnt in m\n");
618
619         /* allocate a new mbuf from the second pool, and attach it to the first
620          * cloned mbuf */
621         clone2 = rte_pktmbuf_alloc(pktmbuf_pool2);
622         if (clone2 == NULL)
623                 GOTO_FAIL("cannot allocate clone2 from second pool\n");
624
625         /* check data room size and priv size, and erase priv */
626         if (rte_pktmbuf_data_room_size(clone2->pool) != 0)
627                 GOTO_FAIL("data room size should be 0\n");
628         if (rte_pktmbuf_priv_size(clone2->pool) != MBUF2_PRIV_SIZE)
629                 GOTO_FAIL("data room size should be %d\n", MBUF2_PRIV_SIZE);
630         memset(clone2 + 1, 0, MBUF2_PRIV_SIZE);
631
632         /* save data pointer to compare it after detach() */
633         c_data2 = rte_pktmbuf_mtod(clone2, char *);
634         if (c_data2 != (char *)clone2 + sizeof(*clone2) + MBUF2_PRIV_SIZE)
635                 GOTO_FAIL("bad data pointer in clone2");
636         if (rte_pktmbuf_headroom(clone2) != 0)
637                 GOTO_FAIL("bad headroom in clone2");
638
639         rte_pktmbuf_attach(clone2, clone);
640
641         if (rte_pktmbuf_mtod(clone2, char *) != data)
642                 GOTO_FAIL("clone2 was not attached properly\n");
643         if (rte_pktmbuf_headroom(clone2) != RTE_PKTMBUF_HEADROOM)
644                 GOTO_FAIL("bad headroom in clone2 after attach");
645         if (rte_mbuf_refcnt_read(m) != 3)
646                 GOTO_FAIL("invalid refcnt in m\n");
647
648         /* detach the clones */
649         rte_pktmbuf_detach(clone);
650         if (c_data != rte_pktmbuf_mtod(clone, char *))
651                 GOTO_FAIL("clone was not detached properly\n");
652         if (rte_mbuf_refcnt_read(m) != 2)
653                 GOTO_FAIL("invalid refcnt in m\n");
654
655         rte_pktmbuf_detach(clone2);
656         if (c_data2 != rte_pktmbuf_mtod(clone2, char *))
657                 GOTO_FAIL("clone2 was not detached properly\n");
658         if (rte_mbuf_refcnt_read(m) != 1)
659                 GOTO_FAIL("invalid refcnt in m\n");
660
661         /* free the clones and the initial mbuf */
662         rte_pktmbuf_free(clone2);
663         rte_pktmbuf_free(clone);
664         rte_pktmbuf_free(m);
665         printf("%s ok\n", __func__);
666         return 0;
667
668 fail:
669         if (m)
670                 rte_pktmbuf_free(m);
671         if (clone)
672                 rte_pktmbuf_free(clone);
673         if (clone2)
674                 rte_pktmbuf_free(clone2);
675         return -1;
676 }
677
678 /*
679  * test allocation and free of mbufs
680  */
681 static int
682 test_pktmbuf_pool(struct rte_mempool *pktmbuf_pool)
683 {
684         unsigned i;
685         struct rte_mbuf *m[NB_MBUF];
686         int ret = 0;
687
688         for (i=0; i<NB_MBUF; i++)
689                 m[i] = NULL;
690
691         /* alloc NB_MBUF mbufs */
692         for (i=0; i<NB_MBUF; i++) {
693                 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
694                 if (m[i] == NULL) {
695                         printf("rte_pktmbuf_alloc() failed (%u)\n", i);
696                         ret = -1;
697                 }
698         }
699         struct rte_mbuf *extra = NULL;
700         extra = rte_pktmbuf_alloc(pktmbuf_pool);
701         if(extra != NULL) {
702                 printf("Error pool not empty");
703                 ret = -1;
704         }
705         extra = rte_pktmbuf_clone(m[0], pktmbuf_pool);
706         if(extra != NULL) {
707                 printf("Error pool not empty");
708                 ret = -1;
709         }
710         /* free them */
711         for (i=0; i<NB_MBUF; i++) {
712                 if (m[i] != NULL)
713                         rte_pktmbuf_free(m[i]);
714         }
715
716         return ret;
717 }
718
719 /*
720  * test bulk allocation and bulk free of mbufs
721  */
722 static int
723 test_pktmbuf_pool_bulk(void)
724 {
725         struct rte_mempool *pool = NULL;
726         struct rte_mempool *pool2 = NULL;
727         unsigned int i;
728         struct rte_mbuf *m;
729         struct rte_mbuf *mbufs[NB_MBUF];
730         int ret = 0;
731
732         /* We cannot use the preallocated mbuf pools because their caches
733          * prevent us from bulk allocating all objects in them.
734          * So we create our own mbuf pools without caches.
735          */
736         printf("Create mbuf pools for bulk allocation.\n");
737         pool = rte_pktmbuf_pool_create("test_pktmbuf_bulk",
738                         NB_MBUF, 0, 0, MBUF_DATA_SIZE, SOCKET_ID_ANY);
739         if (pool == NULL) {
740                 printf("rte_pktmbuf_pool_create() failed. rte_errno %d\n",
741                        rte_errno);
742                 goto err;
743         }
744         pool2 = rte_pktmbuf_pool_create("test_pktmbuf_bulk2",
745                         NB_MBUF, 0, 0, MBUF_DATA_SIZE, SOCKET_ID_ANY);
746         if (pool2 == NULL) {
747                 printf("rte_pktmbuf_pool_create() failed. rte_errno %d\n",
748                        rte_errno);
749                 goto err;
750         }
751
752         /* Preconditions: Mempools must be full. */
753         if (!(rte_mempool_full(pool) && rte_mempool_full(pool2))) {
754                 printf("Test precondition failed: mempools not full\n");
755                 goto err;
756         }
757         if (!(rte_mempool_avail_count(pool) == NB_MBUF &&
758                         rte_mempool_avail_count(pool2) == NB_MBUF)) {
759                 printf("Test precondition failed: mempools: %u+%u != %u+%u",
760                        rte_mempool_avail_count(pool),
761                        rte_mempool_avail_count(pool2),
762                        NB_MBUF, NB_MBUF);
763                 goto err;
764         }
765
766         printf("Test single bulk alloc, followed by multiple bulk free.\n");
767
768         /* Bulk allocate all mbufs in the pool, in one go. */
769         ret = rte_pktmbuf_alloc_bulk(pool, mbufs, NB_MBUF);
770         if (ret != 0) {
771                 printf("rte_pktmbuf_alloc_bulk() failed: %d\n", ret);
772                 goto err;
773         }
774         /* Test that they have been removed from the pool. */
775         if (!rte_mempool_empty(pool)) {
776                 printf("mempool not empty\n");
777                 goto err;
778         }
779         /* Bulk free all mbufs, in four steps. */
780         RTE_BUILD_BUG_ON(NB_MBUF % 4 != 0);
781         for (i = 0; i < NB_MBUF; i += NB_MBUF / 4) {
782                 rte_pktmbuf_free_bulk(&mbufs[i], NB_MBUF / 4);
783                 /* Test that they have been returned to the pool. */
784                 if (rte_mempool_avail_count(pool) != i + NB_MBUF / 4) {
785                         printf("mempool avail count incorrect\n");
786                         goto err;
787                 }
788         }
789
790         printf("Test multiple bulk alloc, followed by single bulk free.\n");
791
792         /* Bulk allocate all mbufs in the pool, in four steps. */
793         for (i = 0; i < NB_MBUF; i += NB_MBUF / 4) {
794                 ret = rte_pktmbuf_alloc_bulk(pool, &mbufs[i], NB_MBUF / 4);
795                 if (ret != 0) {
796                         printf("rte_pktmbuf_alloc_bulk() failed: %d\n", ret);
797                         goto err;
798                 }
799         }
800         /* Test that they have been removed from the pool. */
801         if (!rte_mempool_empty(pool)) {
802                 printf("mempool not empty\n");
803                 goto err;
804         }
805         /* Bulk free all mbufs, in one go. */
806         rte_pktmbuf_free_bulk(mbufs, NB_MBUF);
807         /* Test that they have been returned to the pool. */
808         if (!rte_mempool_full(pool)) {
809                 printf("mempool not full\n");
810                 goto err;
811         }
812
813         printf("Test bulk free of single long chain.\n");
814
815         /* Bulk allocate all mbufs in the pool, in one go. */
816         ret = rte_pktmbuf_alloc_bulk(pool, mbufs, NB_MBUF);
817         if (ret != 0) {
818                 printf("rte_pktmbuf_alloc_bulk() failed: %d\n", ret);
819                 goto err;
820         }
821         /* Create a long mbuf chain. */
822         for (i = 1; i < NB_MBUF; i++) {
823                 ret = rte_pktmbuf_chain(mbufs[0], mbufs[i]);
824                 if (ret != 0) {
825                         printf("rte_pktmbuf_chain() failed: %d\n", ret);
826                         goto err;
827                 }
828                 mbufs[i] = NULL;
829         }
830         /* Free the mbuf chain containing all the mbufs. */
831         rte_pktmbuf_free_bulk(mbufs, 1);
832         /* Test that they have been returned to the pool. */
833         if (!rte_mempool_full(pool)) {
834                 printf("mempool not full\n");
835                 goto err;
836         }
837
838         printf("Test bulk free of multiple chains using multiple pools.\n");
839
840         /* Create mbuf chains containing mbufs from different pools. */
841         RTE_BUILD_BUG_ON(CHAIN_LEN % 2 != 0);
842         RTE_BUILD_BUG_ON(NB_MBUF % (CHAIN_LEN / 2) != 0);
843         for (i = 0; i < NB_MBUF * 2; i++) {
844                 m = rte_pktmbuf_alloc((i & 4) ? pool2 : pool);
845                 if (m == NULL) {
846                         printf("rte_pktmbuf_alloc() failed (%u)\n", i);
847                         goto err;
848                 }
849                 if ((i % CHAIN_LEN) == 0)
850                         mbufs[i / CHAIN_LEN] = m;
851                 else
852                         rte_pktmbuf_chain(mbufs[i / CHAIN_LEN], m);
853         }
854         /* Test that both pools have been emptied. */
855         if (!(rte_mempool_empty(pool) && rte_mempool_empty(pool2))) {
856                 printf("mempools not empty\n");
857                 goto err;
858         }
859         /* Free one mbuf chain. */
860         rte_pktmbuf_free_bulk(mbufs, 1);
861         /* Test that the segments have been returned to the pools. */
862         if (!(rte_mempool_avail_count(pool) == CHAIN_LEN / 2 &&
863                         rte_mempool_avail_count(pool2) == CHAIN_LEN / 2)) {
864                 printf("all segments of first mbuf have not been returned\n");
865                 goto err;
866         }
867         /* Free the remaining mbuf chains. */
868         rte_pktmbuf_free_bulk(&mbufs[1], NB_MBUF * 2 / CHAIN_LEN - 1);
869         /* Test that they have been returned to the pools. */
870         if (!(rte_mempool_full(pool) && rte_mempool_full(pool2))) {
871                 printf("mempools not full\n");
872                 goto err;
873         }
874
875         ret = 0;
876         goto done;
877
878 err:
879         ret = -1;
880
881 done:
882         printf("Free mbuf pools for bulk allocation.\n");
883         rte_mempool_free(pool);
884         rte_mempool_free(pool2);
885         return ret;
886 }
887
888 /*
889  * test that the pointer to the data on a packet mbuf is set properly
890  */
891 static int
892 test_pktmbuf_pool_ptr(struct rte_mempool *pktmbuf_pool)
893 {
894         unsigned i;
895         struct rte_mbuf *m[NB_MBUF];
896         int ret = 0;
897
898         for (i=0; i<NB_MBUF; i++)
899                 m[i] = NULL;
900
901         /* alloc NB_MBUF mbufs */
902         for (i=0; i<NB_MBUF; i++) {
903                 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
904                 if (m[i] == NULL) {
905                         printf("rte_pktmbuf_alloc() failed (%u)\n", i);
906                         ret = -1;
907                         break;
908                 }
909                 m[i]->data_off += 64;
910         }
911
912         /* free them */
913         for (i=0; i<NB_MBUF; i++) {
914                 if (m[i] != NULL)
915                         rte_pktmbuf_free(m[i]);
916         }
917
918         for (i=0; i<NB_MBUF; i++)
919                 m[i] = NULL;
920
921         /* alloc NB_MBUF mbufs */
922         for (i=0; i<NB_MBUF; i++) {
923                 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
924                 if (m[i] == NULL) {
925                         printf("rte_pktmbuf_alloc() failed (%u)\n", i);
926                         ret = -1;
927                         break;
928                 }
929                 if (m[i]->data_off != RTE_PKTMBUF_HEADROOM) {
930                         printf("invalid data_off\n");
931                         ret = -1;
932                 }
933         }
934
935         /* free them */
936         for (i=0; i<NB_MBUF; i++) {
937                 if (m[i] != NULL)
938                         rte_pktmbuf_free(m[i]);
939         }
940
941         return ret;
942 }
943
944 static int
945 test_pktmbuf_free_segment(struct rte_mempool *pktmbuf_pool)
946 {
947         unsigned i;
948         struct rte_mbuf *m[NB_MBUF];
949         int ret = 0;
950
951         for (i=0; i<NB_MBUF; i++)
952                 m[i] = NULL;
953
954         /* alloc NB_MBUF mbufs */
955         for (i=0; i<NB_MBUF; i++) {
956                 m[i] = rte_pktmbuf_alloc(pktmbuf_pool);
957                 if (m[i] == NULL) {
958                         printf("rte_pktmbuf_alloc() failed (%u)\n", i);
959                         ret = -1;
960                 }
961         }
962
963         /* free them */
964         for (i=0; i<NB_MBUF; i++) {
965                 if (m[i] != NULL) {
966                         struct rte_mbuf *mb, *mt;
967
968                         mb = m[i];
969                         while(mb != NULL) {
970                                 mt = mb;
971                                 mb = mb->next;
972                                 rte_pktmbuf_free_seg(mt);
973                         }
974                 }
975         }
976
977         return ret;
978 }
979
980 /*
981  * Stress test for rte_mbuf atomic refcnt.
982  * Implies that RTE_MBUF_REFCNT_ATOMIC is defined.
983  * For more efficiency, recommended to run with RTE_LIBRTE_MBUF_DEBUG defined.
984  */
985
986 #ifdef RTE_MBUF_REFCNT_ATOMIC
987
988 static int
989 test_refcnt_slave(void *arg)
990 {
991         unsigned lcore, free;
992         void *mp = 0;
993         struct rte_ring *refcnt_mbuf_ring = arg;
994
995         lcore = rte_lcore_id();
996         printf("%s started at lcore %u\n", __func__, lcore);
997
998         free = 0;
999         while (refcnt_stop_slaves == 0) {
1000                 if (rte_ring_dequeue(refcnt_mbuf_ring, &mp) == 0) {
1001                         free++;
1002                         rte_pktmbuf_free(mp);
1003                 }
1004         }
1005
1006         refcnt_lcore[lcore] += free;
1007         printf("%s finished at lcore %u, "
1008                "number of freed mbufs: %u\n",
1009                __func__, lcore, free);
1010         return 0;
1011 }
1012
1013 static void
1014 test_refcnt_iter(unsigned int lcore, unsigned int iter,
1015                  struct rte_mempool *refcnt_pool,
1016                  struct rte_ring *refcnt_mbuf_ring)
1017 {
1018         uint16_t ref;
1019         unsigned i, n, tref, wn;
1020         struct rte_mbuf *m;
1021
1022         tref = 0;
1023
1024         /* For each mbuf in the pool:
1025          * - allocate mbuf,
1026          * - increment it's reference up to N+1,
1027          * - enqueue it N times into the ring for slave cores to free.
1028          */
1029         for (i = 0, n = rte_mempool_avail_count(refcnt_pool);
1030             i != n && (m = rte_pktmbuf_alloc(refcnt_pool)) != NULL;
1031             i++) {
1032                 ref = RTE_MAX(rte_rand() % REFCNT_MAX_REF, 1UL);
1033                 tref += ref;
1034                 if ((ref & 1) != 0) {
1035                         rte_pktmbuf_refcnt_update(m, ref);
1036                         while (ref-- != 0)
1037                                 rte_ring_enqueue(refcnt_mbuf_ring, m);
1038                 } else {
1039                         while (ref-- != 0) {
1040                                 rte_pktmbuf_refcnt_update(m, 1);
1041                                 rte_ring_enqueue(refcnt_mbuf_ring, m);
1042                         }
1043                 }
1044                 rte_pktmbuf_free(m);
1045         }
1046
1047         if (i != n)
1048                 rte_panic("(lcore=%u, iter=%u): was able to allocate only "
1049                           "%u from %u mbufs\n", lcore, iter, i, n);
1050
1051         /* wait till slave lcores  will consume all mbufs */
1052         while (!rte_ring_empty(refcnt_mbuf_ring))
1053                 ;
1054
1055         /* check that all mbufs are back into mempool by now */
1056         for (wn = 0; wn != REFCNT_MAX_TIMEOUT; wn++) {
1057                 if ((i = rte_mempool_avail_count(refcnt_pool)) == n) {
1058                         refcnt_lcore[lcore] += tref;
1059                         printf("%s(lcore=%u, iter=%u) completed, "
1060                             "%u references processed\n",
1061                             __func__, lcore, iter, tref);
1062                         return;
1063                 }
1064                 rte_delay_ms(100);
1065         }
1066
1067         rte_panic("(lcore=%u, iter=%u): after %us only "
1068                   "%u of %u mbufs left free\n", lcore, iter, wn, i, n);
1069 }
1070
1071 static int
1072 test_refcnt_master(struct rte_mempool *refcnt_pool,
1073                    struct rte_ring *refcnt_mbuf_ring)
1074 {
1075         unsigned i, lcore;
1076
1077         lcore = rte_lcore_id();
1078         printf("%s started at lcore %u\n", __func__, lcore);
1079
1080         for (i = 0; i != REFCNT_MAX_ITER; i++)
1081                 test_refcnt_iter(lcore, i, refcnt_pool, refcnt_mbuf_ring);
1082
1083         refcnt_stop_slaves = 1;
1084         rte_wmb();
1085
1086         printf("%s finished at lcore %u\n", __func__, lcore);
1087         return 0;
1088 }
1089
1090 #endif
1091
1092 static int
1093 test_refcnt_mbuf(void)
1094 {
1095 #ifdef RTE_MBUF_REFCNT_ATOMIC
1096         unsigned int master, slave, tref;
1097         int ret = -1;
1098         struct rte_mempool *refcnt_pool = NULL;
1099         struct rte_ring *refcnt_mbuf_ring = NULL;
1100
1101         if (rte_lcore_count() < 2) {
1102                 printf("Not enough cores for test_refcnt_mbuf, expecting at least 2\n");
1103                 return TEST_SKIPPED;
1104         }
1105
1106         printf("starting %s, at %u lcores\n", __func__, rte_lcore_count());
1107
1108         /* create refcnt pool & ring if they don't exist */
1109
1110         refcnt_pool = rte_pktmbuf_pool_create(MAKE_STRING(refcnt_pool),
1111                                               REFCNT_MBUF_NUM, 0, 0, 0,
1112                                               SOCKET_ID_ANY);
1113         if (refcnt_pool == NULL) {
1114                 printf("%s: cannot allocate " MAKE_STRING(refcnt_pool) "\n",
1115                     __func__);
1116                 return -1;
1117         }
1118
1119         refcnt_mbuf_ring = rte_ring_create("refcnt_mbuf_ring",
1120                         rte_align32pow2(REFCNT_RING_SIZE), SOCKET_ID_ANY,
1121                                         RING_F_SP_ENQ);
1122         if (refcnt_mbuf_ring == NULL) {
1123                 printf("%s: cannot allocate " MAKE_STRING(refcnt_mbuf_ring)
1124                     "\n", __func__);
1125                 goto err;
1126         }
1127
1128         refcnt_stop_slaves = 0;
1129         memset(refcnt_lcore, 0, sizeof (refcnt_lcore));
1130
1131         rte_eal_mp_remote_launch(test_refcnt_slave, refcnt_mbuf_ring,
1132                                  SKIP_MASTER);
1133
1134         test_refcnt_master(refcnt_pool, refcnt_mbuf_ring);
1135
1136         rte_eal_mp_wait_lcore();
1137
1138         /* check that we porcessed all references */
1139         tref = 0;
1140         master = rte_get_master_lcore();
1141
1142         RTE_LCORE_FOREACH_SLAVE(slave)
1143                 tref += refcnt_lcore[slave];
1144
1145         if (tref != refcnt_lcore[master])
1146                 rte_panic("refernced mbufs: %u, freed mbufs: %u\n",
1147                           tref, refcnt_lcore[master]);
1148
1149         rte_mempool_dump(stdout, refcnt_pool);
1150         rte_ring_dump(stdout, refcnt_mbuf_ring);
1151
1152         ret = 0;
1153
1154 err:
1155         rte_mempool_free(refcnt_pool);
1156         rte_ring_free(refcnt_mbuf_ring);
1157         return ret;
1158 #else
1159         return 0;
1160 #endif
1161 }
1162
1163 #include <unistd.h>
1164 #include <sys/wait.h>
1165
1166 /* use fork() to test mbuf errors panic */
1167 static int
1168 verify_mbuf_check_panics(struct rte_mbuf *buf)
1169 {
1170         int pid;
1171         int status;
1172
1173         pid = fork();
1174
1175         if (pid == 0) {
1176                 rte_mbuf_sanity_check(buf, 1); /* should panic */
1177                 exit(0);  /* return normally if it doesn't panic */
1178         } else if (pid < 0){
1179                 printf("Fork Failed\n");
1180                 return -1;
1181         }
1182         wait(&status);
1183         if(status == 0)
1184                 return -1;
1185
1186         return 0;
1187 }
1188
1189 static int
1190 test_failing_mbuf_sanity_check(struct rte_mempool *pktmbuf_pool)
1191 {
1192         struct rte_mbuf *buf;
1193         struct rte_mbuf badbuf;
1194
1195         printf("Checking rte_mbuf_sanity_check for failure conditions\n");
1196
1197         /* get a good mbuf to use to make copies */
1198         buf = rte_pktmbuf_alloc(pktmbuf_pool);
1199         if (buf == NULL)
1200                 return -1;
1201         printf("Checking good mbuf initially\n");
1202         if (verify_mbuf_check_panics(buf) != -1)
1203                 return -1;
1204
1205         printf("Now checking for error conditions\n");
1206
1207         if (verify_mbuf_check_panics(NULL)) {
1208                 printf("Error with NULL mbuf test\n");
1209                 return -1;
1210         }
1211
1212         badbuf = *buf;
1213         badbuf.pool = NULL;
1214         if (verify_mbuf_check_panics(&badbuf)) {
1215                 printf("Error with bad-pool mbuf test\n");
1216                 return -1;
1217         }
1218
1219         badbuf = *buf;
1220         badbuf.buf_iova = 0;
1221         if (verify_mbuf_check_panics(&badbuf)) {
1222                 printf("Error with bad-physaddr mbuf test\n");
1223                 return -1;
1224         }
1225
1226         badbuf = *buf;
1227         badbuf.buf_addr = NULL;
1228         if (verify_mbuf_check_panics(&badbuf)) {
1229                 printf("Error with bad-addr mbuf test\n");
1230                 return -1;
1231         }
1232
1233         badbuf = *buf;
1234         badbuf.refcnt = 0;
1235         if (verify_mbuf_check_panics(&badbuf)) {
1236                 printf("Error with bad-refcnt(0) mbuf test\n");
1237                 return -1;
1238         }
1239
1240         badbuf = *buf;
1241         badbuf.refcnt = UINT16_MAX;
1242         if (verify_mbuf_check_panics(&badbuf)) {
1243                 printf("Error with bad-refcnt(MAX) mbuf test\n");
1244                 return -1;
1245         }
1246
1247         return 0;
1248 }
1249
1250 static int
1251 test_mbuf_linearize(struct rte_mempool *pktmbuf_pool, int pkt_len,
1252                     int nb_segs)
1253 {
1254
1255         struct rte_mbuf *m = NULL, *mbuf = NULL;
1256         uint8_t *data;
1257         int data_len = 0;
1258         int remain;
1259         int seg, seg_len;
1260         int i;
1261
1262         if (pkt_len < 1) {
1263                 printf("Packet size must be 1 or more (is %d)\n", pkt_len);
1264                 return -1;
1265         }
1266
1267         if (nb_segs < 1) {
1268                 printf("Number of segments must be 1 or more (is %d)\n",
1269                                 nb_segs);
1270                 return -1;
1271         }
1272
1273         seg_len = pkt_len / nb_segs;
1274         if (seg_len == 0)
1275                 seg_len = 1;
1276
1277         remain = pkt_len;
1278
1279         /* Create chained mbuf_src and fill it generated data */
1280         for (seg = 0; remain > 0; seg++) {
1281
1282                 m = rte_pktmbuf_alloc(pktmbuf_pool);
1283                 if (m == NULL) {
1284                         printf("Cannot create segment for source mbuf");
1285                         goto fail;
1286                 }
1287
1288                 /* Make sure if tailroom is zeroed */
1289                 memset(rte_pktmbuf_mtod(m, uint8_t *), 0,
1290                                 rte_pktmbuf_tailroom(m));
1291
1292                 data_len = remain;
1293                 if (data_len > seg_len)
1294                         data_len = seg_len;
1295
1296                 data = (uint8_t *)rte_pktmbuf_append(m, data_len);
1297                 if (data == NULL) {
1298                         printf("Cannot append %d bytes to the mbuf\n",
1299                                         data_len);
1300                         goto fail;
1301                 }
1302
1303                 for (i = 0; i < data_len; i++)
1304                         data[i] = (seg * seg_len + i) % 0x0ff;
1305
1306                 if (seg == 0)
1307                         mbuf = m;
1308                 else
1309                         rte_pktmbuf_chain(mbuf, m);
1310
1311                 remain -= data_len;
1312         }
1313
1314         /* Create destination buffer to store coalesced data */
1315         if (rte_pktmbuf_linearize(mbuf)) {
1316                 printf("Mbuf linearization failed\n");
1317                 goto fail;
1318         }
1319
1320         if (!rte_pktmbuf_is_contiguous(mbuf)) {
1321                 printf("Source buffer should be contiguous after "
1322                                 "linearization\n");
1323                 goto fail;
1324         }
1325
1326         data = rte_pktmbuf_mtod(mbuf, uint8_t *);
1327
1328         for (i = 0; i < pkt_len; i++)
1329                 if (data[i] != (i % 0x0ff)) {
1330                         printf("Incorrect data in linearized mbuf\n");
1331                         goto fail;
1332                 }
1333
1334         rte_pktmbuf_free(mbuf);
1335         return 0;
1336
1337 fail:
1338         if (mbuf)
1339                 rte_pktmbuf_free(mbuf);
1340         return -1;
1341 }
1342
1343 static int
1344 test_mbuf_linearize_check(struct rte_mempool *pktmbuf_pool)
1345 {
1346         struct test_mbuf_array {
1347                 int size;
1348                 int nb_segs;
1349         } mbuf_array[] = {
1350                         { 128, 1 },
1351                         { 64, 64 },
1352                         { 512, 10 },
1353                         { 250, 11 },
1354                         { 123, 8 },
1355         };
1356         unsigned int i;
1357
1358         printf("Test mbuf linearize API\n");
1359
1360         for (i = 0; i < RTE_DIM(mbuf_array); i++)
1361                 if (test_mbuf_linearize(pktmbuf_pool, mbuf_array[i].size,
1362                                 mbuf_array[i].nb_segs)) {
1363                         printf("Test failed for %d, %d\n", mbuf_array[i].size,
1364                                         mbuf_array[i].nb_segs);
1365                         return -1;
1366                 }
1367
1368         return 0;
1369 }
1370
1371 /*
1372  * Helper function for test_tx_ofload
1373  */
1374 static inline void
1375 set_tx_offload(struct rte_mbuf *mb, uint64_t il2, uint64_t il3, uint64_t il4,
1376         uint64_t tso, uint64_t ol3, uint64_t ol2)
1377 {
1378         mb->l2_len = il2;
1379         mb->l3_len = il3;
1380         mb->l4_len = il4;
1381         mb->tso_segsz = tso;
1382         mb->outer_l3_len = ol3;
1383         mb->outer_l2_len = ol2;
1384 }
1385
1386 static int
1387 test_tx_offload(void)
1388 {
1389         struct rte_mbuf *mb;
1390         uint64_t tm, v1, v2;
1391         size_t sz;
1392         uint32_t i;
1393
1394         static volatile struct {
1395                 uint16_t l2;
1396                 uint16_t l3;
1397                 uint16_t l4;
1398                 uint16_t tso;
1399         } txof;
1400
1401         const uint32_t num = 0x10000;
1402
1403         txof.l2 = rte_rand() % (1 <<  RTE_MBUF_L2_LEN_BITS);
1404         txof.l3 = rte_rand() % (1 <<  RTE_MBUF_L3_LEN_BITS);
1405         txof.l4 = rte_rand() % (1 <<  RTE_MBUF_L4_LEN_BITS);
1406         txof.tso = rte_rand() % (1 <<   RTE_MBUF_TSO_SEGSZ_BITS);
1407
1408         printf("%s started, tx_offload = {\n"
1409                 "\tl2_len=%#hx,\n"
1410                 "\tl3_len=%#hx,\n"
1411                 "\tl4_len=%#hx,\n"
1412                 "\ttso_segsz=%#hx,\n"
1413                 "\touter_l3_len=%#x,\n"
1414                 "\touter_l2_len=%#x,\n"
1415                 "};\n",
1416                 __func__,
1417                 txof.l2, txof.l3, txof.l4, txof.tso, txof.l3, txof.l2);
1418
1419         sz = sizeof(*mb) * num;
1420         mb = rte_zmalloc(NULL, sz, RTE_CACHE_LINE_SIZE);
1421         if (mb == NULL) {
1422                 printf("%s failed, out of memory\n", __func__);
1423                 return -ENOMEM;
1424         }
1425
1426         memset(mb, 0, sz);
1427         tm = rte_rdtsc_precise();
1428
1429         for (i = 0; i != num; i++)
1430                 set_tx_offload(mb + i, txof.l2, txof.l3, txof.l4,
1431                         txof.tso, txof.l3, txof.l2);
1432
1433         tm = rte_rdtsc_precise() - tm;
1434         printf("%s set tx_offload by bit-fields: %u iterations, %"
1435                 PRIu64 " cycles, %#Lf cycles/iter\n",
1436                 __func__, num, tm, (long double)tm / num);
1437
1438         v1 = mb[rte_rand() % num].tx_offload;
1439
1440         memset(mb, 0, sz);
1441         tm = rte_rdtsc_precise();
1442
1443         for (i = 0; i != num; i++)
1444                 mb[i].tx_offload = rte_mbuf_tx_offload(txof.l2, txof.l3,
1445                         txof.l4, txof.tso, txof.l3, txof.l2, 0);
1446
1447         tm = rte_rdtsc_precise() - tm;
1448         printf("%s set raw tx_offload: %u iterations, %"
1449                 PRIu64 " cycles, %#Lf cycles/iter\n",
1450                 __func__, num, tm, (long double)tm / num);
1451
1452         v2 = mb[rte_rand() % num].tx_offload;
1453
1454         rte_free(mb);
1455
1456         printf("%s finished\n"
1457                 "expected tx_offload value: 0x%" PRIx64 ";\n"
1458                 "rte_mbuf_tx_offload value: 0x%" PRIx64 ";\n",
1459                 __func__, v1, v2);
1460
1461         return (v1 == v2) ? 0 : -EINVAL;
1462 }
1463
1464 static int
1465 test_get_rx_ol_flag_list(void)
1466 {
1467         int len = 6, ret = 0;
1468         char buf[256] = "";
1469         int buflen = 0;
1470
1471         /* Test case to check with null buffer */
1472         ret = rte_get_rx_ol_flag_list(0, NULL, 0);
1473         if (ret != -1)
1474                 GOTO_FAIL("%s expected: -1, received = %d\n", __func__, ret);
1475
1476         /* Test case to check with zero buffer len */
1477         ret = rte_get_rx_ol_flag_list(PKT_RX_L4_CKSUM_MASK, buf, 0);
1478         if (ret != -1)
1479                 GOTO_FAIL("%s expected: -1, received = %d\n", __func__, ret);
1480
1481         buflen = strlen(buf);
1482         if (buflen != 0)
1483                 GOTO_FAIL("%s buffer should be empty, received = %d\n",
1484                                 __func__, buflen);
1485
1486         /* Test case to check with reduced buffer len */
1487         ret = rte_get_rx_ol_flag_list(0, buf, len);
1488         if (ret != -1)
1489                 GOTO_FAIL("%s expected: -1, received = %d\n", __func__, ret);
1490
1491         buflen = strlen(buf);
1492         if (buflen != (len - 1))
1493                 GOTO_FAIL("%s invalid buffer length retrieved, expected: %d,"
1494                                 "received = %d\n", __func__,
1495                                 (len - 1), buflen);
1496
1497         /* Test case to check with zero mask value */
1498         ret = rte_get_rx_ol_flag_list(0, buf, sizeof(buf));
1499         if (ret != 0)
1500                 GOTO_FAIL("%s expected: 0, received = %d\n", __func__, ret);
1501
1502         buflen = strlen(buf);
1503         if (buflen == 0)
1504                 GOTO_FAIL("%s expected: %s, received length = 0\n", __func__,
1505                                 "non-zero, buffer should not be empty");
1506
1507         /* Test case to check with valid mask value */
1508         ret = rte_get_rx_ol_flag_list(PKT_RX_SEC_OFFLOAD, buf, sizeof(buf));
1509         if (ret != 0)
1510                 GOTO_FAIL("%s expected: 0, received = %d\n", __func__, ret);
1511
1512         buflen = strlen(buf);
1513         if (buflen == 0)
1514                 GOTO_FAIL("%s expected: %s, received length = 0\n", __func__,
1515                                 "non-zero, buffer should not be empty");
1516
1517         return 0;
1518 fail:
1519         return -1;
1520 }
1521
1522 static int
1523 test_get_tx_ol_flag_list(void)
1524 {
1525         int len = 6, ret = 0;
1526         char buf[256] = "";
1527         int buflen = 0;
1528
1529         /* Test case to check with null buffer */
1530         ret = rte_get_tx_ol_flag_list(0, NULL, 0);
1531         if (ret != -1)
1532                 GOTO_FAIL("%s expected: -1, received = %d\n", __func__, ret);
1533
1534         /* Test case to check with zero buffer len */
1535         ret = rte_get_tx_ol_flag_list(PKT_TX_IP_CKSUM, buf, 0);
1536         if (ret != -1)
1537                 GOTO_FAIL("%s expected: -1, received = %d\n", __func__, ret);
1538
1539         buflen = strlen(buf);
1540         if (buflen != 0) {
1541                 GOTO_FAIL("%s buffer should be empty, received = %d\n",
1542                                 __func__, buflen);
1543         }
1544
1545         /* Test case to check with reduced buffer len */
1546         ret = rte_get_tx_ol_flag_list(0, buf, len);
1547         if (ret != -1)
1548                 GOTO_FAIL("%s expected: -1, received = %d\n", __func__, ret);
1549
1550         buflen = strlen(buf);
1551         if (buflen != (len - 1))
1552                 GOTO_FAIL("%s invalid buffer length retrieved, expected: %d,"
1553                                 "received = %d\n", __func__,
1554                                 (len - 1), buflen);
1555
1556         /* Test case to check with zero mask value */
1557         ret = rte_get_tx_ol_flag_list(0, buf, sizeof(buf));
1558         if (ret != 0)
1559                 GOTO_FAIL("%s expected: 0, received = %d\n", __func__, ret);
1560
1561         buflen = strlen(buf);
1562         if (buflen == 0)
1563                 GOTO_FAIL("%s expected: %s, received length = 0\n", __func__,
1564                                 "non-zero, buffer should not be empty");
1565
1566         /* Test case to check with valid mask value */
1567         ret = rte_get_tx_ol_flag_list(PKT_TX_UDP_CKSUM, buf, sizeof(buf));
1568         if (ret != 0)
1569                 GOTO_FAIL("%s expected: 0, received = %d\n", __func__, ret);
1570
1571         buflen = strlen(buf);
1572         if (buflen == 0)
1573                 GOTO_FAIL("%s expected: %s, received length = 0\n", __func__,
1574                                 "non-zero, buffer should not be empty");
1575
1576         return 0;
1577 fail:
1578         return -1;
1579
1580 }
1581
1582 struct flag_name {
1583         uint64_t flag;
1584         const char *name;
1585 };
1586
1587 static int
1588 test_get_rx_ol_flag_name(void)
1589 {
1590         uint16_t i;
1591         const char *flag_str = NULL;
1592         const struct flag_name rx_flags[] = {
1593                 VAL_NAME(PKT_RX_VLAN),
1594                 VAL_NAME(PKT_RX_RSS_HASH),
1595                 VAL_NAME(PKT_RX_FDIR),
1596                 VAL_NAME(PKT_RX_L4_CKSUM_BAD),
1597                 VAL_NAME(PKT_RX_L4_CKSUM_GOOD),
1598                 VAL_NAME(PKT_RX_L4_CKSUM_NONE),
1599                 VAL_NAME(PKT_RX_IP_CKSUM_BAD),
1600                 VAL_NAME(PKT_RX_IP_CKSUM_GOOD),
1601                 VAL_NAME(PKT_RX_IP_CKSUM_NONE),
1602                 VAL_NAME(PKT_RX_EIP_CKSUM_BAD),
1603                 VAL_NAME(PKT_RX_VLAN_STRIPPED),
1604                 VAL_NAME(PKT_RX_IEEE1588_PTP),
1605                 VAL_NAME(PKT_RX_IEEE1588_TMST),
1606                 VAL_NAME(PKT_RX_FDIR_ID),
1607                 VAL_NAME(PKT_RX_FDIR_FLX),
1608                 VAL_NAME(PKT_RX_QINQ_STRIPPED),
1609                 VAL_NAME(PKT_RX_LRO),
1610                 VAL_NAME(PKT_RX_TIMESTAMP),
1611                 VAL_NAME(PKT_RX_SEC_OFFLOAD),
1612                 VAL_NAME(PKT_RX_SEC_OFFLOAD_FAILED),
1613                 VAL_NAME(PKT_RX_OUTER_L4_CKSUM_BAD),
1614                 VAL_NAME(PKT_RX_OUTER_L4_CKSUM_GOOD),
1615                 VAL_NAME(PKT_RX_OUTER_L4_CKSUM_INVALID),
1616         };
1617
1618         /* Test case to check with valid flag */
1619         for (i = 0; i < RTE_DIM(rx_flags); i++) {
1620                 flag_str = rte_get_rx_ol_flag_name(rx_flags[i].flag);
1621                 if (flag_str == NULL)
1622                         GOTO_FAIL("%s: Expected flagname = %s; received null\n",
1623                                         __func__, rx_flags[i].name);
1624                 if (strcmp(flag_str, rx_flags[i].name) != 0)
1625                         GOTO_FAIL("%s: Expected flagname = %s; received = %s\n",
1626                                 __func__, rx_flags[i].name, flag_str);
1627         }
1628         /* Test case to check with invalid flag */
1629         flag_str = rte_get_rx_ol_flag_name(0);
1630         if (flag_str != NULL) {
1631                 GOTO_FAIL("%s: Expected flag name = null; received = %s\n",
1632                                 __func__, flag_str);
1633         }
1634
1635         return 0;
1636 fail:
1637         return -1;
1638 }
1639
1640 static int
1641 test_get_tx_ol_flag_name(void)
1642 {
1643         uint16_t i;
1644         const char *flag_str = NULL;
1645         const struct flag_name tx_flags[] = {
1646                 VAL_NAME(PKT_TX_VLAN),
1647                 VAL_NAME(PKT_TX_IP_CKSUM),
1648                 VAL_NAME(PKT_TX_TCP_CKSUM),
1649                 VAL_NAME(PKT_TX_SCTP_CKSUM),
1650                 VAL_NAME(PKT_TX_UDP_CKSUM),
1651                 VAL_NAME(PKT_TX_IEEE1588_TMST),
1652                 VAL_NAME(PKT_TX_TCP_SEG),
1653                 VAL_NAME(PKT_TX_IPV4),
1654                 VAL_NAME(PKT_TX_IPV6),
1655                 VAL_NAME(PKT_TX_OUTER_IP_CKSUM),
1656                 VAL_NAME(PKT_TX_OUTER_IPV4),
1657                 VAL_NAME(PKT_TX_OUTER_IPV6),
1658                 VAL_NAME(PKT_TX_TUNNEL_VXLAN),
1659                 VAL_NAME(PKT_TX_TUNNEL_GRE),
1660                 VAL_NAME(PKT_TX_TUNNEL_IPIP),
1661                 VAL_NAME(PKT_TX_TUNNEL_GENEVE),
1662                 VAL_NAME(PKT_TX_TUNNEL_MPLSINUDP),
1663                 VAL_NAME(PKT_TX_TUNNEL_VXLAN_GPE),
1664                 VAL_NAME(PKT_TX_TUNNEL_IP),
1665                 VAL_NAME(PKT_TX_TUNNEL_UDP),
1666                 VAL_NAME(PKT_TX_QINQ),
1667                 VAL_NAME(PKT_TX_MACSEC),
1668                 VAL_NAME(PKT_TX_SEC_OFFLOAD),
1669                 VAL_NAME(PKT_TX_UDP_SEG),
1670                 VAL_NAME(PKT_TX_OUTER_UDP_CKSUM),
1671                 VAL_NAME(PKT_TX_METADATA),
1672         };
1673
1674         /* Test case to check with valid flag */
1675         for (i = 0; i < RTE_DIM(tx_flags); i++) {
1676                 flag_str = rte_get_tx_ol_flag_name(tx_flags[i].flag);
1677                 if (flag_str == NULL)
1678                         GOTO_FAIL("%s: Expected flagname = %s; received null\n",
1679                                 __func__, tx_flags[i].name);
1680                 if (strcmp(flag_str, tx_flags[i].name) != 0)
1681                         GOTO_FAIL("%s: Expected flagname = %s; received = %s\n",
1682                                 __func__, tx_flags[i].name, flag_str);
1683         }
1684         /* Test case to check with invalid flag */
1685         flag_str = rte_get_tx_ol_flag_name(0);
1686         if (flag_str != NULL) {
1687                 GOTO_FAIL("%s: Expected flag name = null; received = %s\n",
1688                                 __func__, flag_str);
1689         }
1690
1691         return 0;
1692 fail:
1693         return -1;
1694
1695 }
1696
1697 static int
1698 test_mbuf_validate_tx_offload(const char *test_name,
1699                 struct rte_mempool *pktmbuf_pool,
1700                 uint64_t ol_flags,
1701                 uint16_t segsize,
1702                 int expected_retval)
1703 {
1704         struct rte_mbuf *m = NULL;
1705         int ret = 0;
1706
1707         /* alloc a mbuf and do sanity check */
1708         m = rte_pktmbuf_alloc(pktmbuf_pool);
1709         if (m == NULL)
1710                 GOTO_FAIL("%s: mbuf allocation failed!\n", __func__);
1711         if (rte_pktmbuf_pkt_len(m) != 0)
1712                 GOTO_FAIL("%s: Bad packet length\n", __func__);
1713         rte_mbuf_sanity_check(m, 0);
1714         m->ol_flags = ol_flags;
1715         m->tso_segsz = segsize;
1716         ret = rte_validate_tx_offload(m);
1717         if (ret != expected_retval)
1718                 GOTO_FAIL("%s(%s): expected ret val: %d; received: %d\n",
1719                                 __func__, test_name, expected_retval, ret);
1720         rte_pktmbuf_free(m);
1721         m = NULL;
1722         return 0;
1723 fail:
1724         if (m) {
1725                 rte_pktmbuf_free(m);
1726                 m = NULL;
1727         }
1728         return -1;
1729 }
1730
1731 static int
1732 test_mbuf_validate_tx_offload_one(struct rte_mempool *pktmbuf_pool)
1733 {
1734         /* test to validate tx offload flags */
1735         uint64_t ol_flags = 0;
1736
1737         /* test to validate if IP checksum is counted only for IPV4 packet */
1738         /* set both IP checksum and IPV6 flags */
1739         ol_flags |= PKT_TX_IP_CKSUM;
1740         ol_flags |= PKT_TX_IPV6;
1741         if (test_mbuf_validate_tx_offload("MBUF_TEST_IP_CKSUM_IPV6_SET",
1742                                 pktmbuf_pool,
1743                                 ol_flags, 0, -EINVAL) < 0)
1744                 GOTO_FAIL("%s failed: IP cksum is set incorrect.\n", __func__);
1745         /* resetting ol_flags for next testcase */
1746         ol_flags = 0;
1747
1748         /* test to validate if IP type is set when required */
1749         ol_flags |= PKT_TX_L4_MASK;
1750         if (test_mbuf_validate_tx_offload("MBUF_TEST_IP_TYPE_NOT_SET",
1751                                 pktmbuf_pool,
1752                                 ol_flags, 0, -EINVAL) < 0)
1753                 GOTO_FAIL("%s failed: IP type is not set.\n", __func__);
1754
1755         /* test if IP type is set when TCP SEG is on */
1756         ol_flags |= PKT_TX_TCP_SEG;
1757         if (test_mbuf_validate_tx_offload("MBUF_TEST_IP_TYPE_NOT_SET",
1758                                 pktmbuf_pool,
1759                                 ol_flags, 0, -EINVAL) < 0)
1760                 GOTO_FAIL("%s failed: IP type is not set.\n", __func__);
1761
1762         ol_flags = 0;
1763         /* test to confirm IP type (IPV4/IPV6) is set */
1764         ol_flags = PKT_TX_L4_MASK;
1765         ol_flags |= PKT_TX_IPV6;
1766         if (test_mbuf_validate_tx_offload("MBUF_TEST_IP_TYPE_SET",
1767                                 pktmbuf_pool,
1768                                 ol_flags, 0, 0) < 0)
1769                 GOTO_FAIL("%s failed: tx offload flag error.\n", __func__);
1770
1771         ol_flags = 0;
1772         /* test to check TSO segment size is non-zero */
1773         ol_flags |= PKT_TX_IPV4;
1774         ol_flags |= PKT_TX_TCP_SEG;
1775         /* set 0 tso segment size */
1776         if (test_mbuf_validate_tx_offload("MBUF_TEST_NULL_TSO_SEGSZ",
1777                                 pktmbuf_pool,
1778                                 ol_flags, 0, -EINVAL) < 0)
1779                 GOTO_FAIL("%s failed: tso segment size is null.\n", __func__);
1780
1781         /* retain IPV4 and PKT_TX_TCP_SEG mask */
1782         /* set valid tso segment size but IP CKSUM not set */
1783         if (test_mbuf_validate_tx_offload("MBUF_TEST_TSO_IP_CKSUM_NOT_SET",
1784                                 pktmbuf_pool,
1785                                 ol_flags, 512, -EINVAL) < 0)
1786                 GOTO_FAIL("%s failed: IP CKSUM is not set.\n", __func__);
1787
1788         /* test to validate if IP checksum is set for TSO capability */
1789         /* retain IPV4, TCP_SEG, tso_seg size */
1790         ol_flags |= PKT_TX_IP_CKSUM;
1791         if (test_mbuf_validate_tx_offload("MBUF_TEST_TSO_IP_CKSUM_SET",
1792                                 pktmbuf_pool,
1793                                 ol_flags, 512, 0) < 0)
1794                 GOTO_FAIL("%s failed: tx offload flag error.\n", __func__);
1795
1796         /* test to confirm TSO for IPV6 type */
1797         ol_flags = 0;
1798         ol_flags |= PKT_TX_IPV6;
1799         ol_flags |= PKT_TX_TCP_SEG;
1800         if (test_mbuf_validate_tx_offload("MBUF_TEST_TSO_IPV6_SET",
1801                                 pktmbuf_pool,
1802                                 ol_flags, 512, 0) < 0)
1803                 GOTO_FAIL("%s failed: TSO req not met.\n", __func__);
1804
1805         ol_flags = 0;
1806         /* test if outer IP checksum set for non outer IPv4 packet */
1807         ol_flags |= PKT_TX_IPV6;
1808         ol_flags |= PKT_TX_OUTER_IP_CKSUM;
1809         if (test_mbuf_validate_tx_offload("MBUF_TEST_OUTER_IPV4_NOT_SET",
1810                                 pktmbuf_pool,
1811                                 ol_flags, 512, -EINVAL) < 0)
1812                 GOTO_FAIL("%s failed: Outer IP cksum set.\n", __func__);
1813
1814         ol_flags = 0;
1815         /* test to confirm outer IP checksum is set for outer IPV4 packet */
1816         ol_flags |= PKT_TX_OUTER_IP_CKSUM;
1817         ol_flags |= PKT_TX_OUTER_IPV4;
1818         if (test_mbuf_validate_tx_offload("MBUF_TEST_OUTER_IPV4_SET",
1819                                 pktmbuf_pool,
1820                                 ol_flags, 512, 0) < 0)
1821                 GOTO_FAIL("%s failed: tx offload flag error.\n", __func__);
1822
1823         ol_flags = 0;
1824         /* test to confirm if packets with no TX_OFFLOAD_MASK are skipped */
1825         if (test_mbuf_validate_tx_offload("MBUF_TEST_OL_MASK_NOT_SET",
1826                                 pktmbuf_pool,
1827                                 ol_flags, 512, 0) < 0)
1828                 GOTO_FAIL("%s failed: tx offload flag error.\n", __func__);
1829         return 0;
1830 fail:
1831         return -1;
1832 }
1833
1834 /*
1835  * Test for allocating a bulk of mbufs
1836  * define an array with positive sizes for mbufs allocations.
1837  */
1838 static int
1839 test_pktmbuf_alloc_bulk(struct rte_mempool *pktmbuf_pool)
1840 {
1841         int ret = 0;
1842         unsigned int idx, loop;
1843         unsigned int alloc_counts[] = {
1844                 0,
1845                 MEMPOOL_CACHE_SIZE - 1,
1846                 MEMPOOL_CACHE_SIZE + 1,
1847                 MEMPOOL_CACHE_SIZE * 1.5,
1848                 MEMPOOL_CACHE_SIZE * 2,
1849                 MEMPOOL_CACHE_SIZE * 2 - 1,
1850                 MEMPOOL_CACHE_SIZE * 2 + 1,
1851                 MEMPOOL_CACHE_SIZE,
1852         };
1853
1854         /* allocate a large array of mbuf pointers */
1855         struct rte_mbuf *mbufs[NB_MBUF] = { 0 };
1856         for (idx = 0; idx < RTE_DIM(alloc_counts); idx++) {
1857                 ret = rte_pktmbuf_alloc_bulk(pktmbuf_pool, mbufs,
1858                                 alloc_counts[idx]);
1859                 if (ret == 0) {
1860                         for (loop = 0; loop < alloc_counts[idx] &&
1861                                         mbufs[loop] != NULL; loop++)
1862                                 rte_pktmbuf_free(mbufs[loop]);
1863                 } else if (ret != 0) {
1864                         printf("%s: Bulk alloc failed count(%u); ret val(%d)\n",
1865                                         __func__, alloc_counts[idx], ret);
1866                         return -1;
1867                 }
1868         }
1869         return 0;
1870 }
1871
1872 /*
1873  * Negative testing for allocating a bulk of mbufs
1874  */
1875 static int
1876 test_neg_pktmbuf_alloc_bulk(struct rte_mempool *pktmbuf_pool)
1877 {
1878         int ret = 0;
1879         unsigned int idx, loop;
1880         unsigned int neg_alloc_counts[] = {
1881                 MEMPOOL_CACHE_SIZE - NB_MBUF,
1882                 NB_MBUF + 1,
1883                 NB_MBUF * 8,
1884                 UINT_MAX
1885         };
1886         struct rte_mbuf *mbufs[NB_MBUF * 8] = { 0 };
1887
1888         for (idx = 0; idx < RTE_DIM(neg_alloc_counts); idx++) {
1889                 ret = rte_pktmbuf_alloc_bulk(pktmbuf_pool, mbufs,
1890                                 neg_alloc_counts[idx]);
1891                 if (ret == 0) {
1892                         printf("%s: Bulk alloc must fail! count(%u); ret(%d)\n",
1893                                         __func__, neg_alloc_counts[idx], ret);
1894                         for (loop = 0; loop < neg_alloc_counts[idx] &&
1895                                         mbufs[loop] != NULL; loop++)
1896                                 rte_pktmbuf_free(mbufs[loop]);
1897                         return -1;
1898                 }
1899         }
1900         return 0;
1901 }
1902
1903 /*
1904  * Test to read mbuf packet using rte_pktmbuf_read
1905  */
1906 static int
1907 test_pktmbuf_read(struct rte_mempool *pktmbuf_pool)
1908 {
1909         struct rte_mbuf *m = NULL;
1910         char *data = NULL;
1911         const char *data_copy = NULL;
1912         int off;
1913
1914         /* alloc a mbuf */
1915         m = rte_pktmbuf_alloc(pktmbuf_pool);
1916         if (m == NULL)
1917                 GOTO_FAIL("%s: mbuf allocation failed!\n", __func__);
1918         if (rte_pktmbuf_pkt_len(m) != 0)
1919                 GOTO_FAIL("%s: Bad packet length\n", __func__);
1920         rte_mbuf_sanity_check(m, 0);
1921
1922         data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN2);
1923         if (data == NULL)
1924                 GOTO_FAIL("%s: Cannot append data\n", __func__);
1925         if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN2)
1926                 GOTO_FAIL("%s: Bad packet length\n", __func__);
1927         memset(data, 0xfe, MBUF_TEST_DATA_LEN2);
1928
1929         /* read the data from mbuf */
1930         data_copy = rte_pktmbuf_read(m, 0, MBUF_TEST_DATA_LEN2, NULL);
1931         if (data_copy == NULL)
1932                 GOTO_FAIL("%s: Error in reading data!\n", __func__);
1933         for (off = 0; off < MBUF_TEST_DATA_LEN2; off++) {
1934                 if (data_copy[off] != (char)0xfe)
1935                         GOTO_FAIL("Data corrupted at offset %u", off);
1936         }
1937         rte_pktmbuf_free(m);
1938         m = NULL;
1939
1940         return 0;
1941 fail:
1942         if (m) {
1943                 rte_pktmbuf_free(m);
1944                 m = NULL;
1945         }
1946         return -1;
1947 }
1948
1949 /*
1950  * Test to read mbuf packet data from offset
1951  */
1952 static int
1953 test_pktmbuf_read_from_offset(struct rte_mempool *pktmbuf_pool)
1954 {
1955         struct rte_mbuf *m = NULL;
1956         struct ether_hdr *hdr = NULL;
1957         char *data = NULL;
1958         const char *data_copy = NULL;
1959         unsigned int off;
1960         unsigned int hdr_len = sizeof(struct rte_ether_hdr);
1961
1962         /* alloc a mbuf */
1963         m = rte_pktmbuf_alloc(pktmbuf_pool);
1964         if (m == NULL)
1965                 GOTO_FAIL("%s: mbuf allocation failed!\n", __func__);
1966
1967         if (rte_pktmbuf_pkt_len(m) != 0)
1968                 GOTO_FAIL("%s: Bad packet length\n", __func__);
1969         rte_mbuf_sanity_check(m, 0);
1970
1971         /* prepend an ethernet header */
1972         hdr = (struct ether_hdr *)rte_pktmbuf_prepend(m, hdr_len);
1973         if (hdr == NULL)
1974                 GOTO_FAIL("%s: Cannot prepend header\n", __func__);
1975         if (rte_pktmbuf_pkt_len(m) != hdr_len)
1976                 GOTO_FAIL("%s: Bad pkt length", __func__);
1977         if (rte_pktmbuf_data_len(m) != hdr_len)
1978                 GOTO_FAIL("%s: Bad data length", __func__);
1979         memset(hdr, 0xde, hdr_len);
1980
1981         /* read mbuf header info from 0 offset */
1982         data_copy = rte_pktmbuf_read(m, 0, hdr_len, NULL);
1983         if (data_copy == NULL)
1984                 GOTO_FAIL("%s: Error in reading header!\n", __func__);
1985         for (off = 0; off < hdr_len; off++) {
1986                 if (data_copy[off] != (char)0xde)
1987                         GOTO_FAIL("Header info corrupted at offset %u", off);
1988         }
1989
1990         /* append sample data after ethernet header */
1991         data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN2);
1992         if (data == NULL)
1993                 GOTO_FAIL("%s: Cannot append data\n", __func__);
1994         if (rte_pktmbuf_pkt_len(m) != hdr_len + MBUF_TEST_DATA_LEN2)
1995                 GOTO_FAIL("%s: Bad packet length\n", __func__);
1996         if (rte_pktmbuf_data_len(m) != hdr_len + MBUF_TEST_DATA_LEN2)
1997                 GOTO_FAIL("%s: Bad data length\n", __func__);
1998         memset(data, 0xcc, MBUF_TEST_DATA_LEN2);
1999
2000         /* read mbuf data after header info */
2001         data_copy = rte_pktmbuf_read(m, hdr_len, MBUF_TEST_DATA_LEN2, NULL);
2002         if (data_copy == NULL)
2003                 GOTO_FAIL("%s: Error in reading header data!\n", __func__);
2004         for (off = 0; off < MBUF_TEST_DATA_LEN2; off++) {
2005                 if (data_copy[off] != (char)0xcc)
2006                         GOTO_FAIL("Data corrupted at offset %u", off);
2007         }
2008
2009         /* partial reading of mbuf data */
2010         data_copy = rte_pktmbuf_read(m, hdr_len + 5, MBUF_TEST_DATA_LEN2 - 5,
2011                         NULL);
2012         if (data_copy == NULL)
2013                 GOTO_FAIL("%s: Error in reading packet data!\n", __func__);
2014         if (strlen(data_copy) != MBUF_TEST_DATA_LEN2 - 5)
2015                 GOTO_FAIL("%s: Incorrect data length!\n", __func__);
2016         for (off = 0; off < MBUF_TEST_DATA_LEN2 - 5; off++) {
2017                 if (data_copy[off] != (char)0xcc)
2018                         GOTO_FAIL("Data corrupted at offset %u", off);
2019         }
2020
2021         /* read length greater than mbuf data_len */
2022         if (rte_pktmbuf_read(m, hdr_len, rte_pktmbuf_data_len(m) + 1,
2023                                 NULL) != NULL)
2024                 GOTO_FAIL("%s: Requested len is larger than mbuf data len!\n",
2025                                 __func__);
2026
2027         /* read length greater than mbuf pkt_len */
2028         if (rte_pktmbuf_read(m, hdr_len, rte_pktmbuf_pkt_len(m) + 1,
2029                                 NULL) != NULL)
2030                 GOTO_FAIL("%s: Requested len is larger than mbuf pkt len!\n",
2031                                 __func__);
2032
2033         /* read data of zero len from valid offset */
2034         data_copy = rte_pktmbuf_read(m, hdr_len, 0, NULL);
2035         if (data_copy == NULL)
2036                 GOTO_FAIL("%s: Error in reading packet data!\n", __func__);
2037         if (strlen(data_copy) != MBUF_TEST_DATA_LEN2)
2038                 GOTO_FAIL("%s: Corrupted data content!\n", __func__);
2039         for (off = 0; off < MBUF_TEST_DATA_LEN2; off++) {
2040                 if (data_copy[off] != (char)0xcc)
2041                         GOTO_FAIL("Data corrupted at offset %u", off);
2042         }
2043
2044         /* read data of zero length from zero offset */
2045         data_copy = rte_pktmbuf_read(m, 0, 0, NULL);
2046         if (data_copy == NULL)
2047                 GOTO_FAIL("%s: Error in reading packet data!\n", __func__);
2048         /* check if the received address is the beginning of header info */
2049         if (hdr != (const struct ether_hdr *)data_copy)
2050                 GOTO_FAIL("%s: Corrupted data address!\n", __func__);
2051
2052         /* read data of max length from valid offset */
2053         data_copy = rte_pktmbuf_read(m, hdr_len, UINT_MAX, NULL);
2054         if (data_copy == NULL)
2055                 GOTO_FAIL("%s: Error in reading packet data!\n", __func__);
2056         /* check if the received address is the beginning of data segment */
2057         if (data_copy != data)
2058                 GOTO_FAIL("%s: Corrupted data address!\n", __func__);
2059
2060         /* try to read from mbuf with max size offset */
2061         data_copy = rte_pktmbuf_read(m, UINT_MAX, 0, NULL);
2062         if (data_copy != NULL)
2063                 GOTO_FAIL("%s: Error in reading packet data!\n", __func__);
2064
2065         /* try to read from mbuf with max size offset and len */
2066         data_copy = rte_pktmbuf_read(m, UINT_MAX, UINT_MAX, NULL);
2067         if (data_copy != NULL)
2068                 GOTO_FAIL("%s: Error in reading packet data!\n", __func__);
2069
2070         rte_pktmbuf_dump(stdout, m, rte_pktmbuf_pkt_len(m));
2071
2072         rte_pktmbuf_free(m);
2073         m = NULL;
2074
2075         return 0;
2076 fail:
2077         if (m) {
2078                 rte_pktmbuf_free(m);
2079                 m = NULL;
2080         }
2081         return -1;
2082 }
2083
2084 struct test_case {
2085         unsigned int seg_count;
2086         unsigned int flags;
2087         uint32_t read_off;
2088         uint32_t read_len;
2089         unsigned int seg_lengths[MBUF_MAX_SEG];
2090 };
2091
2092 /* create a mbuf with different sized segments
2093  *  and fill with data [0x00 0x01 0x02 ...]
2094  */
2095 static struct rte_mbuf *
2096 create_packet(struct rte_mempool *pktmbuf_pool,
2097                 struct test_case *test_data)
2098 {
2099         uint16_t i, ret, seg, seg_len = 0;
2100         uint32_t last_index = 0;
2101         unsigned int seg_lengths[MBUF_MAX_SEG];
2102         unsigned int hdr_len;
2103         struct rte_mbuf *pkt = NULL;
2104         struct rte_mbuf *pkt_seg = NULL;
2105         char *hdr = NULL;
2106         char *data = NULL;
2107
2108         memcpy(seg_lengths, test_data->seg_lengths,
2109                         sizeof(unsigned int)*test_data->seg_count);
2110         for (seg = 0; seg < test_data->seg_count; seg++) {
2111                 hdr_len = 0;
2112                 seg_len =  seg_lengths[seg];
2113                 pkt_seg = rte_pktmbuf_alloc(pktmbuf_pool);
2114                 if (pkt_seg == NULL)
2115                         GOTO_FAIL("%s: mbuf allocation failed!\n", __func__);
2116                 if (rte_pktmbuf_pkt_len(pkt_seg) != 0)
2117                         GOTO_FAIL("%s: Bad packet length\n", __func__);
2118                 rte_mbuf_sanity_check(pkt_seg, 0);
2119                 /* Add header only for the first segment */
2120                 if (test_data->flags == MBUF_HEADER && seg == 0) {
2121                         hdr_len = sizeof(struct rte_ether_hdr);
2122                         /* prepend a header and fill with dummy data */
2123                         hdr = (char *)rte_pktmbuf_prepend(pkt_seg, hdr_len);
2124                         if (hdr == NULL)
2125                                 GOTO_FAIL("%s: Cannot prepend header\n",
2126                                                 __func__);
2127                         if (rte_pktmbuf_pkt_len(pkt_seg) != hdr_len)
2128                                 GOTO_FAIL("%s: Bad pkt length", __func__);
2129                         if (rte_pktmbuf_data_len(pkt_seg) != hdr_len)
2130                                 GOTO_FAIL("%s: Bad data length", __func__);
2131                         for (i = 0; i < hdr_len; i++)
2132                                 hdr[i] = (last_index + i) % 0xffff;
2133                         last_index += hdr_len;
2134                 }
2135                 /* skip appending segment with 0 length */
2136                 if (seg_len == 0)
2137                         continue;
2138                 data = rte_pktmbuf_append(pkt_seg, seg_len);
2139                 if (data == NULL)
2140                         GOTO_FAIL("%s: Cannot append data segment\n", __func__);
2141                 if (rte_pktmbuf_pkt_len(pkt_seg) != hdr_len + seg_len)
2142                         GOTO_FAIL("%s: Bad packet segment length: %d\n",
2143                                         __func__, rte_pktmbuf_pkt_len(pkt_seg));
2144                 if (rte_pktmbuf_data_len(pkt_seg) != hdr_len + seg_len)
2145                         GOTO_FAIL("%s: Bad data length\n", __func__);
2146                 for (i = 0; i < seg_len; i++)
2147                         data[i] = (last_index + i) % 0xffff;
2148                 /* to fill continuous data from one seg to another */
2149                 last_index += i;
2150                 /* create chained mbufs */
2151                 if (seg == 0)
2152                         pkt = pkt_seg;
2153                 else {
2154                         ret = rte_pktmbuf_chain(pkt, pkt_seg);
2155                         if (ret != 0)
2156                                 GOTO_FAIL("%s:FAIL: Chained mbuf creation %d\n",
2157                                                 __func__, ret);
2158                 }
2159
2160                 pkt_seg = pkt_seg->next;
2161         }
2162         return pkt;
2163 fail:
2164         if (pkt != NULL) {
2165                 rte_pktmbuf_free(pkt);
2166                 pkt = NULL;
2167         }
2168         if (pkt_seg != NULL) {
2169                 rte_pktmbuf_free(pkt_seg);
2170                 pkt_seg = NULL;
2171         }
2172         return NULL;
2173 }
2174
2175 static int
2176 test_pktmbuf_read_from_chain(struct rte_mempool *pktmbuf_pool)
2177 {
2178         struct rte_mbuf *m;
2179         struct test_case test_cases[] = {
2180                 {
2181                         .seg_lengths = { 100, 100, 100 },
2182                         .seg_count = 3,
2183                         .flags = MBUF_NO_HEADER,
2184                         .read_off = 0,
2185                         .read_len = 300
2186                 },
2187                 {
2188                         .seg_lengths = { 100, 125, 150 },
2189                         .seg_count = 3,
2190                         .flags = MBUF_NO_HEADER,
2191                         .read_off = 99,
2192                         .read_len = 201
2193                 },
2194                 {
2195                         .seg_lengths = { 100, 100 },
2196                         .seg_count = 2,
2197                         .flags = MBUF_NO_HEADER,
2198                         .read_off = 0,
2199                         .read_len = 100
2200                 },
2201                 {
2202                         .seg_lengths = { 100, 200 },
2203                         .seg_count = 2,
2204                         .flags = MBUF_HEADER,
2205                         .read_off = sizeof(struct rte_ether_hdr),
2206                         .read_len = 150
2207                 },
2208                 {
2209                         .seg_lengths = { 1000, 100 },
2210                         .seg_count = 2,
2211                         .flags = MBUF_NO_HEADER,
2212                         .read_off = 0,
2213                         .read_len = 1000
2214                 },
2215                 {
2216                         .seg_lengths = { 1024, 0, 100 },
2217                         .seg_count = 3,
2218                         .flags = MBUF_NO_HEADER,
2219                         .read_off = 100,
2220                         .read_len = 1001
2221                 },
2222                 {
2223                         .seg_lengths = { 1000, 1, 1000 },
2224                         .seg_count = 3,
2225                         .flags = MBUF_NO_HEADER,
2226                         .read_off = 1000,
2227                         .read_len = 2
2228                 },
2229                 {
2230                         .seg_lengths = { MBUF_TEST_DATA_LEN,
2231                                         MBUF_TEST_DATA_LEN2,
2232                                         MBUF_TEST_DATA_LEN3, 800, 10 },
2233                         .seg_count = 5,
2234                         .flags = MBUF_NEG_TEST_READ,
2235                         .read_off = 1000,
2236                         .read_len = MBUF_DATA_SIZE
2237                 },
2238         };
2239
2240         uint32_t i, pos;
2241         const char *data_copy = NULL;
2242         char data_buf[MBUF_DATA_SIZE];
2243
2244         memset(data_buf, 0, MBUF_DATA_SIZE);
2245
2246         for (i = 0; i < RTE_DIM(test_cases); i++) {
2247                 m = create_packet(pktmbuf_pool, &test_cases[i]);
2248                 if (m == NULL)
2249                         GOTO_FAIL("%s: mbuf allocation failed!\n", __func__);
2250
2251                 data_copy = rte_pktmbuf_read(m, test_cases[i].read_off,
2252                                 test_cases[i].read_len, data_buf);
2253                 if (test_cases[i].flags == MBUF_NEG_TEST_READ) {
2254                         if (data_copy != NULL)
2255                                 GOTO_FAIL("%s: mbuf data read should fail!\n",
2256                                                 __func__);
2257                         else {
2258                                 rte_pktmbuf_free(m);
2259                                 m = NULL;
2260                                 continue;
2261                         }
2262                 }
2263                 if (data_copy == NULL)
2264                         GOTO_FAIL("%s: Error in reading packet data!\n",
2265                                         __func__);
2266                 for (pos = 0; pos < test_cases[i].read_len; pos++) {
2267                         if (data_copy[pos] !=
2268                                         (char)((test_cases[i].read_off + pos)
2269                                                 % 0xffff))
2270                                 GOTO_FAIL("Data corrupted at offset %u is %2X",
2271                                                 pos, data_copy[pos]);
2272                 }
2273                 rte_pktmbuf_dump(stdout, m, rte_pktmbuf_pkt_len(m));
2274                 rte_pktmbuf_free(m);
2275                 m = NULL;
2276         }
2277         return 0;
2278
2279 fail:
2280         if (m != NULL) {
2281                 rte_pktmbuf_free(m);
2282                 m = NULL;
2283         }
2284         return -1;
2285 }
2286
2287 /* Define a free call back function to be used for external buffer */
2288 static void
2289 ext_buf_free_callback_fn(void *addr __rte_unused, void *opaque)
2290 {
2291         void *ext_buf_addr = opaque;
2292
2293         if (ext_buf_addr == NULL) {
2294                 printf("External buffer address is invalid\n");
2295                 return;
2296         }
2297         rte_free(ext_buf_addr);
2298         ext_buf_addr = NULL;
2299         printf("External buffer freed via callback\n");
2300 }
2301
2302 /*
2303  * Test to initialize shared data in external buffer before attaching to mbuf
2304  *  - Allocate mbuf with no data.
2305  *  - Allocate external buffer with size should be large enough to accommodate
2306  *     rte_mbuf_ext_shared_info.
2307  *  - Invoke pktmbuf_ext_shinfo_init_helper to initialize shared data.
2308  *  - Invoke rte_pktmbuf_attach_extbuf to attach external buffer to the mbuf.
2309  *  - Clone another mbuf and attach the same external buffer to it.
2310  *  - Invoke rte_pktmbuf_detach_extbuf to detach the external buffer from mbuf.
2311  */
2312 static int
2313 test_pktmbuf_ext_shinfo_init_helper(struct rte_mempool *pktmbuf_pool)
2314 {
2315         struct rte_mbuf *m = NULL;
2316         struct rte_mbuf *clone = NULL;
2317         struct rte_mbuf_ext_shared_info *ret_shinfo = NULL;
2318         rte_iova_t buf_iova;
2319         void *ext_buf_addr = NULL;
2320         uint16_t buf_len = EXT_BUF_TEST_DATA_LEN +
2321                                 sizeof(struct rte_mbuf_ext_shared_info);
2322
2323         /* alloc a mbuf */
2324         m = rte_pktmbuf_alloc(pktmbuf_pool);
2325         if (m == NULL)
2326                 GOTO_FAIL("%s: mbuf allocation failed!\n", __func__);
2327         if (rte_pktmbuf_pkt_len(m) != 0)
2328                 GOTO_FAIL("%s: Bad packet length\n", __func__);
2329         rte_mbuf_sanity_check(m, 0);
2330
2331         ext_buf_addr = rte_malloc("External buffer", buf_len,
2332                         RTE_CACHE_LINE_SIZE);
2333         if (ext_buf_addr == NULL)
2334                 GOTO_FAIL("%s: External buffer allocation failed\n", __func__);
2335
2336         ret_shinfo = rte_pktmbuf_ext_shinfo_init_helper(ext_buf_addr, &buf_len,
2337                 ext_buf_free_callback_fn, ext_buf_addr);
2338         if (ret_shinfo == NULL)
2339                 GOTO_FAIL("%s: Shared info initialization failed!\n", __func__);
2340
2341         if (rte_mbuf_ext_refcnt_read(ret_shinfo) != 1)
2342                 GOTO_FAIL("%s: External refcount is not 1\n", __func__);
2343
2344         if (rte_mbuf_refcnt_read(m) != 1)
2345                 GOTO_FAIL("%s: Invalid refcnt in mbuf\n", __func__);
2346
2347         buf_iova = rte_mempool_virt2iova(ext_buf_addr);
2348         rte_pktmbuf_attach_extbuf(m, ext_buf_addr, buf_iova, buf_len,
2349                 ret_shinfo);
2350         if (m->ol_flags != EXT_ATTACHED_MBUF)
2351                 GOTO_FAIL("%s: External buffer is not attached to mbuf\n",
2352                                 __func__);
2353
2354         /* allocate one more mbuf */
2355         clone = rte_pktmbuf_clone(m, pktmbuf_pool);
2356         if (clone == NULL)
2357                 GOTO_FAIL("%s: mbuf clone allocation failed!\n", __func__);
2358         if (rte_pktmbuf_pkt_len(clone) != 0)
2359                 GOTO_FAIL("%s: Bad packet length\n", __func__);
2360
2361         /* attach the same external buffer to the cloned mbuf */
2362         rte_pktmbuf_attach_extbuf(clone, ext_buf_addr, buf_iova, buf_len,
2363                         ret_shinfo);
2364         if (clone->ol_flags != EXT_ATTACHED_MBUF)
2365                 GOTO_FAIL("%s: External buffer is not attached to mbuf\n",
2366                                 __func__);
2367
2368         if (rte_mbuf_ext_refcnt_read(ret_shinfo) != 2)
2369                 GOTO_FAIL("%s: Invalid ext_buf ref_cnt\n", __func__);
2370
2371         /* test to manually update ext_buf_ref_cnt from 2 to 3*/
2372         rte_mbuf_ext_refcnt_update(ret_shinfo, 1);
2373         if (rte_mbuf_ext_refcnt_read(ret_shinfo) != 3)
2374                 GOTO_FAIL("%s: Update ext_buf ref_cnt failed\n", __func__);
2375
2376         /* reset the ext_refcnt before freeing the external buffer */
2377         rte_mbuf_ext_refcnt_set(ret_shinfo, 2);
2378         if (rte_mbuf_ext_refcnt_read(ret_shinfo) != 2)
2379                 GOTO_FAIL("%s: set ext_buf ref_cnt failed\n", __func__);
2380
2381         /* detach the external buffer from mbufs */
2382         rte_pktmbuf_detach_extbuf(m);
2383         /* check if ref cnt is decremented */
2384         if (rte_mbuf_ext_refcnt_read(ret_shinfo) != 1)
2385                 GOTO_FAIL("%s: Invalid ext_buf ref_cnt\n", __func__);
2386
2387         rte_pktmbuf_detach_extbuf(clone);
2388         if (rte_mbuf_ext_refcnt_read(ret_shinfo) != 0)
2389                 GOTO_FAIL("%s: Invalid ext_buf ref_cnt\n", __func__);
2390
2391         rte_pktmbuf_free(m);
2392         m = NULL;
2393         rte_pktmbuf_free(clone);
2394         clone = NULL;
2395
2396         return 0;
2397
2398 fail:
2399         if (m) {
2400                 rte_pktmbuf_free(m);
2401                 m = NULL;
2402         }
2403         if (clone) {
2404                 rte_pktmbuf_free(clone);
2405                 clone = NULL;
2406         }
2407         if (ext_buf_addr != NULL) {
2408                 rte_free(ext_buf_addr);
2409                 ext_buf_addr = NULL;
2410         }
2411         return -1;
2412 }
2413
2414 static int
2415 test_mbuf(void)
2416 {
2417         int ret = -1;
2418         struct rte_mempool *pktmbuf_pool = NULL;
2419         struct rte_mempool *pktmbuf_pool2 = NULL;
2420
2421
2422         RTE_BUILD_BUG_ON(sizeof(struct rte_mbuf) != RTE_CACHE_LINE_MIN_SIZE * 2);
2423
2424         /* create pktmbuf pool if it does not exist */
2425         pktmbuf_pool = rte_pktmbuf_pool_create("test_pktmbuf_pool",
2426                         NB_MBUF, MEMPOOL_CACHE_SIZE, 0, MBUF_DATA_SIZE,
2427                         SOCKET_ID_ANY);
2428
2429         if (pktmbuf_pool == NULL) {
2430                 printf("cannot allocate mbuf pool\n");
2431                 goto err;
2432         }
2433
2434         /* create a specific pktmbuf pool with a priv_size != 0 and no data
2435          * room size */
2436         pktmbuf_pool2 = rte_pktmbuf_pool_create("test_pktmbuf_pool2",
2437                         NB_MBUF, MEMPOOL_CACHE_SIZE, MBUF2_PRIV_SIZE, 0,
2438                         SOCKET_ID_ANY);
2439
2440         if (pktmbuf_pool2 == NULL) {
2441                 printf("cannot allocate mbuf pool\n");
2442                 goto err;
2443         }
2444
2445         /* test multiple mbuf alloc */
2446         if (test_pktmbuf_pool(pktmbuf_pool) < 0) {
2447                 printf("test_mbuf_pool() failed\n");
2448                 goto err;
2449         }
2450
2451         /* do it another time to check that all mbufs were freed */
2452         if (test_pktmbuf_pool(pktmbuf_pool) < 0) {
2453                 printf("test_mbuf_pool() failed (2)\n");
2454                 goto err;
2455         }
2456
2457         /* test bulk mbuf alloc and free */
2458         if (test_pktmbuf_pool_bulk() < 0) {
2459                 printf("test_pktmbuf_pool_bulk() failed\n");
2460                 goto err;
2461         }
2462
2463         /* test that the pointer to the data on a packet mbuf is set properly */
2464         if (test_pktmbuf_pool_ptr(pktmbuf_pool) < 0) {
2465                 printf("test_pktmbuf_pool_ptr() failed\n");
2466                 goto err;
2467         }
2468
2469         /* test data manipulation in mbuf */
2470         if (test_one_pktmbuf(pktmbuf_pool) < 0) {
2471                 printf("test_one_mbuf() failed\n");
2472                 goto err;
2473         }
2474
2475
2476         /*
2477          * do it another time, to check that allocation reinitialize
2478          * the mbuf correctly
2479          */
2480         if (test_one_pktmbuf(pktmbuf_pool) < 0) {
2481                 printf("test_one_mbuf() failed (2)\n");
2482                 goto err;
2483         }
2484
2485         if (test_pktmbuf_with_non_ascii_data(pktmbuf_pool) < 0) {
2486                 printf("test_pktmbuf_with_non_ascii_data() failed\n");
2487                 goto err;
2488         }
2489
2490         /* test free pktmbuf segment one by one */
2491         if (test_pktmbuf_free_segment(pktmbuf_pool) < 0) {
2492                 printf("test_pktmbuf_free_segment() failed.\n");
2493                 goto err;
2494         }
2495
2496         if (testclone_testupdate_testdetach(pktmbuf_pool) < 0) {
2497                 printf("testclone_and_testupdate() failed \n");
2498                 goto err;
2499         }
2500
2501         if (test_pktmbuf_copy(pktmbuf_pool) < 0) {
2502                 printf("test_pktmbuf_copy() failed\n");
2503                 goto err;
2504         }
2505
2506         if (test_attach_from_different_pool(pktmbuf_pool, pktmbuf_pool2) < 0) {
2507                 printf("test_attach_from_different_pool() failed\n");
2508                 goto err;
2509         }
2510
2511         if (test_refcnt_mbuf() < 0) {
2512                 printf("test_refcnt_mbuf() failed \n");
2513                 goto err;
2514         }
2515
2516         if (test_failing_mbuf_sanity_check(pktmbuf_pool) < 0) {
2517                 printf("test_failing_mbuf_sanity_check() failed\n");
2518                 goto err;
2519         }
2520
2521         if (test_mbuf_linearize_check(pktmbuf_pool) < 0) {
2522                 printf("test_mbuf_linearize_check() failed\n");
2523                 goto err;
2524         }
2525
2526         if (test_tx_offload() < 0) {
2527                 printf("test_tx_offload() failed\n");
2528                 goto err;
2529         }
2530
2531         if (test_get_rx_ol_flag_list() < 0) {
2532                 printf("test_rte_get_rx_ol_flag_list() failed\n");
2533                 goto err;
2534         }
2535
2536         if (test_get_tx_ol_flag_list() < 0) {
2537                 printf("test_rte_get_tx_ol_flag_list() failed\n");
2538                 goto err;
2539         }
2540
2541         if (test_get_rx_ol_flag_name() < 0) {
2542                 printf("test_rte_get_rx_ol_flag_name() failed\n");
2543                 goto err;
2544         }
2545
2546         if (test_get_tx_ol_flag_name() < 0) {
2547                 printf("test_rte_get_tx_ol_flag_name() failed\n");
2548                 goto err;
2549         }
2550
2551         if (test_mbuf_validate_tx_offload_one(pktmbuf_pool) < 0) {
2552                 printf("test_mbuf_validate_tx_offload_one() failed\n");
2553                 goto err;
2554         }
2555
2556         /* test for allocating a bulk of mbufs with various sizes */
2557         if (test_pktmbuf_alloc_bulk(pktmbuf_pool) < 0) {
2558                 printf("test_rte_pktmbuf_alloc_bulk() failed\n");
2559                 goto err;
2560         }
2561
2562         /* test for allocating a bulk of mbufs with various sizes */
2563         if (test_neg_pktmbuf_alloc_bulk(pktmbuf_pool) < 0) {
2564                 printf("test_neg_rte_pktmbuf_alloc_bulk() failed\n");
2565                 goto err;
2566         }
2567
2568         /* test to read mbuf packet */
2569         if (test_pktmbuf_read(pktmbuf_pool) < 0) {
2570                 printf("test_rte_pktmbuf_read() failed\n");
2571                 goto err;
2572         }
2573
2574         /* test to read mbuf packet from offset */
2575         if (test_pktmbuf_read_from_offset(pktmbuf_pool) < 0) {
2576                 printf("test_rte_pktmbuf_read_from_offset() failed\n");
2577                 goto err;
2578         }
2579
2580         /* test to read data from chain of mbufs with data segments */
2581         if (test_pktmbuf_read_from_chain(pktmbuf_pool) < 0) {
2582                 printf("test_rte_pktmbuf_read_from_chain() failed\n");
2583                 goto err;
2584         }
2585
2586         /* test to initialize shared info. at the end of external buffer */
2587         if (test_pktmbuf_ext_shinfo_init_helper(pktmbuf_pool) < 0) {
2588                 printf("test_pktmbuf_ext_shinfo_init_helper() failed\n");
2589                 goto err;
2590         }
2591
2592         ret = 0;
2593 err:
2594         rte_mempool_free(pktmbuf_pool);
2595         rte_mempool_free(pktmbuf_pool2);
2596         return ret;
2597 }
2598 #undef GOTO_FAIL
2599
2600 REGISTER_TEST_COMMAND(mbuf_autotest, test_mbuf);