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