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