1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2014 Intel Corporation. 3 * Copyright(c) 2014 6WIND S.A. 4 */ 5 6 #include <stdio.h> 7 8 #include "test.h" 9 10 #include <string.h> 11 #include <stdarg.h> 12 13 #ifdef RTE_EXEC_ENV_WINDOWS 14 static int 15 test_missing_c_flag(void) 16 { 17 printf("missing_c_flag not supported on Windows, skipping test\n"); 18 return TEST_SKIPPED; 19 } 20 21 static int 22 test_main_lcore_flag(void) 23 { 24 printf("main_lcore_flag not supported on Windows, skipping test\n"); 25 return TEST_SKIPPED; 26 } 27 28 static int 29 test_invalid_n_flag(void) 30 { 31 printf("invalid_n_flag not supported on Windows, skipping test\n"); 32 return TEST_SKIPPED; 33 } 34 35 static int 36 test_no_hpet_flag(void) 37 { 38 printf("no_hpet_flag not supported on Windows, skipping test\n"); 39 return TEST_SKIPPED; 40 } 41 42 static int 43 test_no_huge_flag(void) 44 { 45 printf("no_huge_flag not supported on Windows, skipping test\n"); 46 return TEST_SKIPPED; 47 } 48 49 static int 50 test_allow_flag(void) 51 { 52 printf("allow_flag not supported on Windows, skipping test\n"); 53 return TEST_SKIPPED; 54 } 55 56 static int 57 test_invalid_b_flag(void) 58 { 59 printf("invalid_b_flag not supported on Windows, skipping test\n"); 60 return TEST_SKIPPED; 61 } 62 63 static int 64 test_invalid_vdev_flag(void) 65 { 66 printf("invalid_vdev_flag not supported on Windows, skipping test\n"); 67 return TEST_SKIPPED; 68 } 69 70 static int 71 test_invalid_r_flag(void) 72 { 73 printf("invalid_r_flag not supported on Windows, skipping test\n"); 74 return TEST_SKIPPED; 75 } 76 77 static int 78 test_memory_flags(void) 79 { 80 printf("memory_flags not supported on Windows, skipping test\n"); 81 return TEST_SKIPPED; 82 } 83 84 static int 85 test_file_prefix(void) 86 { 87 printf("file_prefix not supported on Windows, skipping test\n"); 88 return TEST_SKIPPED; 89 } 90 91 static int 92 test_misc_flags(void) 93 { 94 printf("misc_flags not supported on Windows, skipping test\n"); 95 return TEST_SKIPPED; 96 } 97 98 #else 99 100 #include <libgen.h> 101 #include <stdlib.h> 102 #include <errno.h> 103 #include <unistd.h> 104 #include <dirent.h> 105 #include <sys/file.h> 106 #include <sys/stat.h> 107 #include <sys/wait.h> 108 #include <limits.h> 109 #include <fcntl.h> 110 111 #include <rte_lcore.h> 112 #include <rte_debug.h> 113 #include <rte_string_fns.h> 114 115 #include "process.h" 116 117 #define DEFAULT_MEM_SIZE "18" 118 #define mp_flag "--proc-type=secondary" 119 #define no_hpet "--no-hpet" 120 #define no_huge "--no-huge" 121 #define no_shconf "--no-shconf" 122 #define allow "--allow" 123 #define vdev "--vdev" 124 #define memtest "memtest" 125 #define memtest1 "memtest1" 126 #define memtest2 "memtest2" 127 #define SOCKET_MEM_STRLEN (RTE_MAX_NUMA_NODES * 20) 128 #define launch_proc(ARGV) process_dup(ARGV, RTE_DIM(ARGV), __func__) 129 130 enum hugepage_action { 131 HUGEPAGE_CHECK_EXISTS = 0, 132 HUGEPAGE_CHECK_LOCKED, 133 HUGEPAGE_DELETE, 134 HUGEPAGE_INVALID 135 }; 136 137 /* if string contains a hugepage path */ 138 static int 139 get_hugepage_path(char * src, int src_len, char * dst, int dst_len) 140 { 141 #define NUM_TOKENS 4 142 char *tokens[NUM_TOKENS]; 143 144 /* if we couldn't properly split the string */ 145 if (rte_strsplit(src, src_len, tokens, NUM_TOKENS, ' ') < NUM_TOKENS) 146 return 0; 147 148 if (strncmp(tokens[2], "hugetlbfs", sizeof("hugetlbfs")) == 0) { 149 strlcpy(dst, tokens[1], dst_len); 150 return 1; 151 } 152 return 0; 153 } 154 155 /* 156 * Cycles through hugepage directories and looks for hugepage 157 * files associated with a given prefix. Depending on value of 158 * action, the hugepages are checked if they exist, checked if 159 * they can be locked, or are simply deleted. 160 * 161 * Returns 1 if it finds at least one hugepage matching the action 162 * Returns 0 if no matching hugepages were found 163 * Returns -1 if it encounters an error 164 */ 165 static int 166 process_hugefiles(const char * prefix, enum hugepage_action action) 167 { 168 FILE * hugedir_handle = NULL; 169 DIR * hugepage_dir = NULL; 170 struct dirent *dirent = NULL; 171 172 char hugefile_prefix[PATH_MAX] = {0}; 173 char hugedir[PATH_MAX] = {0}; 174 char line[PATH_MAX] = {0}; 175 176 int fd, lck_result, result = 0; 177 178 const int prefix_len = snprintf(hugefile_prefix, 179 sizeof(hugefile_prefix), "%smap_", prefix); 180 if (prefix_len <= 0 || prefix_len >= (int)sizeof(hugefile_prefix) 181 || prefix_len >= (int)sizeof(dirent->d_name)) { 182 printf("Error creating hugefile filename prefix\n"); 183 return -1; 184 } 185 186 /* get hugetlbfs mountpoints from /proc/mounts */ 187 hugedir_handle = fopen("/proc/mounts", "r"); 188 189 if (hugedir_handle == NULL) { 190 printf("Error parsing /proc/mounts!\n"); 191 return -1; 192 } 193 194 /* read and parse script output */ 195 while (fgets(line, sizeof(line), hugedir_handle) != NULL) { 196 197 /* check if we have a hugepage filesystem path */ 198 if (!get_hugepage_path(line, sizeof(line), hugedir, sizeof(hugedir))) 199 continue; 200 201 /* check if directory exists */ 202 if ((hugepage_dir = opendir(hugedir)) == NULL) { 203 fclose(hugedir_handle); 204 printf("Error reading %s: %s\n", hugedir, strerror(errno)); 205 return -1; 206 } 207 208 while ((dirent = readdir(hugepage_dir)) != NULL) { 209 if (memcmp(dirent->d_name, hugefile_prefix, prefix_len) != 0) 210 continue; 211 212 switch (action) { 213 case HUGEPAGE_CHECK_EXISTS: 214 { 215 /* file exists, return */ 216 closedir(hugepage_dir); 217 result = 1; 218 goto end; 219 } 220 break; 221 case HUGEPAGE_DELETE: 222 { 223 char file_path[PATH_MAX] = {0}; 224 225 snprintf(file_path, sizeof(file_path), 226 "%s/%s", hugedir, dirent->d_name); 227 228 /* remove file */ 229 if (remove(file_path) < 0) { 230 printf("Error deleting %s - %s!\n", 231 dirent->d_name, strerror(errno)); 232 closedir(hugepage_dir); 233 result = -1; 234 goto end; 235 } 236 result = 1; 237 } 238 break; 239 case HUGEPAGE_CHECK_LOCKED: 240 { 241 /* try and lock the file */ 242 fd = openat(dirfd(hugepage_dir), dirent->d_name, O_RDONLY); 243 244 /* this shouldn't happen */ 245 if (fd == -1) { 246 printf("Error opening %s - %s!\n", 247 dirent->d_name, strerror(errno)); 248 closedir(hugepage_dir); 249 result = -1; 250 goto end; 251 } 252 253 /* non-blocking lock */ 254 lck_result = flock(fd, LOCK_EX | LOCK_NB); 255 256 /* if lock succeeds, there's something wrong */ 257 if (lck_result != -1) { 258 result = 0; 259 260 /* unlock the resulting lock */ 261 flock(fd, LOCK_UN); 262 close(fd); 263 closedir(hugepage_dir); 264 goto end; 265 } 266 result = 1; 267 close(fd); 268 } 269 break; 270 /* shouldn't happen */ 271 default: 272 goto end; 273 } /* switch */ 274 275 } /* read hugepage directory */ 276 closedir(hugepage_dir); 277 } /* read /proc/mounts */ 278 end: 279 fclose(hugedir_handle); 280 return result; 281 } 282 283 #ifdef RTE_EXEC_ENV_LINUX 284 /* 285 * count the number of "node*" files in /sys/devices/system/node/ 286 */ 287 static int 288 get_number_of_sockets(void) 289 { 290 struct dirent *dirent = NULL; 291 const char * nodedir = "/sys/devices/system/node/"; 292 DIR * dir = NULL; 293 int result = 0; 294 295 /* check if directory exists */ 296 if ((dir = opendir(nodedir)) == NULL) { 297 /* if errno==ENOENT this means we don't have NUMA support */ 298 if (errno == ENOENT) { 299 printf("No NUMA nodes detected: assuming 1 available socket\n"); 300 return 1; 301 } 302 printf("Error opening %s: %s\n", nodedir, strerror(errno)); 303 return -1; 304 } 305 306 while ((dirent = readdir(dir)) != NULL) 307 if (strncmp(dirent->d_name, "node", sizeof("node") - 1) == 0) 308 result++; 309 310 closedir(dir); 311 return result; 312 } 313 #endif 314 315 /* 316 * Test that the app doesn't run with invalid allow option. 317 * Final tests ensures it does run with valid options as sanity check (one 318 * test for with Domain+BDF, second for just with BDF) 319 */ 320 static int 321 test_allow_flag(void) 322 { 323 unsigned i; 324 #ifdef RTE_EXEC_ENV_FREEBSD 325 /* BSD target doesn't support prefixes at this point */ 326 const char * prefix = ""; 327 #else 328 char prefix[PATH_MAX], tmp[PATH_MAX]; 329 if (get_current_prefix(tmp, sizeof(tmp)) == NULL) { 330 printf("Error - unable to get current prefix!\n"); 331 return -1; 332 } 333 snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp); 334 #endif 335 336 const char *wlinval[][7] = { 337 {prgname, prefix, mp_flag, 338 allow, "error", "", ""}, 339 {prgname, prefix, mp_flag, 340 allow, "0:0:0", "", ""}, 341 {prgname, prefix, mp_flag, 342 allow, "0:error:0.1", "", ""}, 343 {prgname, prefix, mp_flag, 344 allow, "0:0:0.1error", "", ""}, 345 {prgname, prefix, mp_flag, 346 allow, "error0:0:0.1", "", ""}, 347 {prgname, prefix, mp_flag, 348 allow, "0:0:0.1.2", "", ""}, 349 }; 350 /* Test with valid allow option */ 351 const char *wlval1[] = {prgname, prefix, mp_flag, 352 allow, "00FF:09:0B.3"}; 353 const char *wlval2[] = {prgname, prefix, mp_flag, 354 allow, "09:0B.3", allow, "0a:0b.1"}; 355 const char *wlval3[] = {prgname, prefix, mp_flag, 356 allow, "09:0B.3,type=test", 357 allow, "08:00.1,type=normal", 358 }; 359 360 for (i = 0; i < RTE_DIM(wlinval); i++) { 361 if (launch_proc(wlinval[i]) == 0) { 362 printf("Error - process did run ok with invalid " 363 "allow parameter\n"); 364 return -1; 365 } 366 } 367 if (launch_proc(wlval1) != 0 ) { 368 printf("Error - process did not run ok with valid allow\n"); 369 return -1; 370 } 371 if (launch_proc(wlval2) != 0 ) { 372 printf("Error - process did not run ok with valid allow value set\n"); 373 return -1; 374 } 375 if (launch_proc(wlval3) != 0 ) { 376 printf("Error - process did not run ok with valid allow + args\n"); 377 return -1; 378 } 379 380 return 0; 381 } 382 383 /* 384 * Test that the app doesn't run with invalid blocklist option. 385 * Final test ensures it does run with valid options as sanity check 386 */ 387 static int 388 test_invalid_b_flag(void) 389 { 390 #ifdef RTE_EXEC_ENV_FREEBSD 391 /* BSD target doesn't support prefixes at this point */ 392 const char * prefix = ""; 393 #else 394 char prefix[PATH_MAX], tmp[PATH_MAX]; 395 if (get_current_prefix(tmp, sizeof(tmp)) == NULL) { 396 printf("Error - unable to get current prefix!\n"); 397 return -1; 398 } 399 snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp); 400 #endif 401 402 const char *blinval[][5] = { 403 {prgname, prefix, mp_flag, "-b", "error"}, 404 {prgname, prefix, mp_flag, "-b", "0:0:0"}, 405 {prgname, prefix, mp_flag, "-b", "0:error:0.1"}, 406 {prgname, prefix, mp_flag, "-b", "0:0:0.1error"}, 407 {prgname, prefix, mp_flag, "-b", "error0:0:0.1"}, 408 {prgname, prefix, mp_flag, "-b", "0:0:0.1.2"}, 409 }; 410 /* Test with valid blocklist option */ 411 const char *blval[] = {prgname, prefix, mp_flag, 412 "-b", "FF:09:0B.3"}; 413 414 int i; 415 416 for (i = 0; i != RTE_DIM(blinval); i++) { 417 if (launch_proc(blinval[i]) == 0) { 418 printf("Error - process did run ok with invalid " 419 "blocklist parameter\n"); 420 return -1; 421 } 422 } 423 if (launch_proc(blval) != 0) { 424 printf("Error - process did not run ok with valid blocklist value\n"); 425 return -1; 426 } 427 return 0; 428 } 429 430 /* 431 * Test that the app doesn't run with invalid vdev option. 432 * Final test ensures it does run with valid options as sanity check 433 */ 434 static int 435 test_invalid_vdev_flag(void) 436 { 437 #ifdef RTE_NET_RING 438 #ifdef RTE_EXEC_ENV_FREEBSD 439 /* BSD target doesn't support prefixes at this point, and we also need to 440 * run another primary process here */ 441 const char * prefix = no_shconf; 442 #else 443 const char * prefix = "--file-prefix=vdev"; 444 #endif 445 446 /* Test with invalid vdev option */ 447 const char *vdevinval[] = {prgname, prefix, no_huge, 448 vdev, "eth_dummy"}; 449 450 /* Test with valid vdev option */ 451 const char *vdevval1[] = {prgname, prefix, no_huge, 452 vdev, "net_ring0"}; 453 454 const char *vdevval2[] = {prgname, prefix, no_huge, 455 vdev, "net_ring0,args=test"}; 456 457 const char *vdevval3[] = {prgname, prefix, no_huge, 458 vdev, "net_ring0,nodeaction=r1:0:CREATE"}; 459 460 if (launch_proc(vdevinval) == 0) { 461 printf("Error - process did run ok with invalid " 462 "vdev parameter\n"); 463 return -1; 464 } 465 466 if (launch_proc(vdevval1) != 0) { 467 printf("Error - process did not run ok with valid vdev value\n"); 468 return -1; 469 } 470 471 if (launch_proc(vdevval2) != 0) { 472 printf("Error - process did not run ok with valid vdev value," 473 "with dummy args\n"); 474 return -1; 475 } 476 477 if (launch_proc(vdevval3) != 0) { 478 printf("Error - process did not run ok with valid vdev value," 479 "with valid args\n"); 480 return -1; 481 } 482 return 0; 483 #else 484 return TEST_SKIPPED; 485 #endif 486 } 487 488 /* 489 * Test that the app doesn't run with invalid -r option. 490 */ 491 static int 492 test_invalid_r_flag(void) 493 { 494 #ifdef RTE_EXEC_ENV_FREEBSD 495 /* BSD target doesn't support prefixes at this point */ 496 const char * prefix = ""; 497 #else 498 char prefix[PATH_MAX], tmp[PATH_MAX]; 499 if (get_current_prefix(tmp, sizeof(tmp)) == NULL) { 500 printf("Error - unable to get current prefix!\n"); 501 return -1; 502 } 503 snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp); 504 #endif 505 506 const char *rinval[][5] = { 507 {prgname, prefix, mp_flag, "-r", "error"}, 508 {prgname, prefix, mp_flag, "-r", "0"}, 509 {prgname, prefix, mp_flag, "-r", "-1"}, 510 {prgname, prefix, mp_flag, "-r", "17"}, 511 }; 512 /* Test with valid blocklist option */ 513 const char *rval[] = {prgname, prefix, mp_flag, "-r", "16"}; 514 515 int i; 516 517 for (i = 0; i != RTE_DIM(rinval); i++) { 518 if (launch_proc(rinval[i]) == 0) { 519 printf("Error - process did run ok with invalid " 520 "-r (rank) parameter\n"); 521 return -1; 522 } 523 } 524 if (launch_proc(rval) != 0) { 525 printf("Error - process did not run ok with valid -r (rank) value\n"); 526 return -1; 527 } 528 return 0; 529 } 530 531 /* 532 * Test that the app doesn't run without the coremask/corelist flags. In all cases 533 * should give an error and fail to run 534 */ 535 static int 536 test_missing_c_flag(void) 537 { 538 #ifdef RTE_EXEC_ENV_FREEBSD 539 /* BSD target doesn't support prefixes at this point */ 540 const char * prefix = ""; 541 #else 542 char prefix[PATH_MAX], tmp[PATH_MAX]; 543 if (get_current_prefix(tmp, sizeof(tmp)) == NULL) { 544 printf("Error - unable to get current prefix!\n"); 545 return -1; 546 } 547 snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp); 548 #endif 549 550 /* -c flag but no coremask value */ 551 const char *argv1[] = { prgname, prefix, mp_flag, "-c"}; 552 /* No -c, -l or --lcores flag at all */ 553 const char *argv2[] = { prgname, prefix, mp_flag}; 554 /* bad coremask value */ 555 const char *argv3[] = { prgname, prefix, mp_flag, 556 "-c", "error" }; 557 /* sanity check of tests - valid coremask value */ 558 const char *argv4[] = { prgname, prefix, mp_flag, 559 "-c", "1" }; 560 /* -l flag but no corelist value */ 561 const char *argv5[] = { prgname, prefix, mp_flag, 562 "-l"}; 563 const char *argv6[] = { prgname, prefix, mp_flag, 564 "-l", " " }; 565 /* bad corelist values */ 566 const char *argv7[] = { prgname, prefix, mp_flag, 567 "-l", "error" }; 568 const char *argv8[] = { prgname, prefix, mp_flag, 569 "-l", "1-" }; 570 const char *argv9[] = { prgname, prefix, mp_flag, 571 "-l", "1," }; 572 const char *argv10[] = { prgname, prefix, mp_flag, 573 "-l", "1#2" }; 574 /* core number is negative value */ 575 const char * const argv11[] = { prgname, prefix, mp_flag, 576 "-l", "-5" }; 577 const char * const argv12[] = { prgname, prefix, mp_flag, 578 "-l", "-5-7" }; 579 /* core number is maximum value */ 580 const char * const argv13[] = { prgname, prefix, mp_flag, 581 "-l", RTE_STR(RTE_MAX_LCORE) }; 582 const char * const argv14[] = { prgname, prefix, mp_flag, 583 "-l", "1-"RTE_STR(RTE_MAX_LCORE) }; 584 /* sanity check test - valid corelist value */ 585 const char * const argv15[] = { prgname, prefix, mp_flag, 586 "-l", "1-2,3" }; 587 588 /* --lcores flag but no lcores value */ 589 const char * const argv16[] = { prgname, prefix, mp_flag, 590 "--lcores" }; 591 const char * const argv17[] = { prgname, prefix, mp_flag, 592 "--lcores", " " }; 593 /* bad lcores value */ 594 const char * const argv18[] = { prgname, prefix, mp_flag, 595 "--lcores", "1-3-5" }; 596 const char * const argv19[] = { prgname, prefix, mp_flag, 597 "--lcores", "0-1,,2" }; 598 const char * const argv20[] = { prgname, prefix, mp_flag, 599 "--lcores", "0-,1" }; 600 const char * const argv21[] = { prgname, prefix, mp_flag, 601 "--lcores", "(0-,2-4)" }; 602 const char * const argv22[] = { prgname, prefix, mp_flag, 603 "--lcores", "(-1,2)" }; 604 const char * const argv23[] = { prgname, prefix, mp_flag, 605 "--lcores", "(2-4)@(2-4-6)" }; 606 const char * const argv24[] = { prgname, prefix, mp_flag, 607 "--lcores", "(a,2)" }; 608 const char * const argv25[] = { prgname, prefix, mp_flag, 609 "--lcores", "1-3@(1,3)" }; 610 const char * const argv26[] = { prgname, prefix, mp_flag, 611 "--lcores", "3@((1,3)" }; 612 const char * const argv27[] = { prgname, prefix, mp_flag, 613 "--lcores", "(4-7)=(1,3)" }; 614 const char * const argv28[] = { prgname, prefix, mp_flag, 615 "--lcores", "[4-7]@(1,3)" }; 616 /* sanity check of tests - valid lcores value */ 617 const char * const argv29[] = { prgname, prefix, mp_flag, 618 "--lcores", 619 "0-1,2@(5-7),(3-5)@(0,2),(0,6),7"}; 620 /* check an invalid cpu value >= CPU_SETSIZE */ 621 const char * const argv30[] = { prgname, prefix, mp_flag, 622 "--lcores", "3@" RTE_STR(CPU_SETSIZE) }; 623 624 if (launch_proc(argv2) != 0) { 625 printf("Error - " 626 "process did not run ok when missing -c flag\n"); 627 return -1; 628 } 629 630 if (launch_proc(argv1) == 0 631 || launch_proc(argv3) == 0) { 632 printf("Error - " 633 "process ran without error with invalid -c flag\n"); 634 return -1; 635 } 636 if (launch_proc(argv4) != 0) { 637 printf("Error - " 638 "process did not run ok with valid coremask value\n"); 639 return -1; 640 } 641 642 /* start -l test */ 643 if (launch_proc(argv5) == 0 644 || launch_proc(argv6) == 0 645 || launch_proc(argv7) == 0 646 || launch_proc(argv8) == 0 647 || launch_proc(argv9) == 0 648 || launch_proc(argv10) == 0 649 || launch_proc(argv11) == 0 650 || launch_proc(argv12) == 0 651 || launch_proc(argv13) == 0 652 || launch_proc(argv14) == 0) { 653 printf("Error - " 654 "process ran without error with invalid -l flag\n"); 655 return -1; 656 } 657 if (rte_lcore_is_enabled(0) && rte_lcore_is_enabled(1) && 658 rte_lcore_is_enabled(2) && rte_lcore_is_enabled(3) && 659 launch_proc(argv15) != 0) { 660 printf("Error - " 661 "process did not run ok with valid corelist value\n"); 662 return -1; 663 } 664 665 /* start --lcores tests */ 666 if (launch_proc(argv16) == 0 || launch_proc(argv17) == 0 || 667 launch_proc(argv18) == 0 || launch_proc(argv19) == 0 || 668 launch_proc(argv20) == 0 || launch_proc(argv21) == 0 || 669 launch_proc(argv22) == 0 || launch_proc(argv23) == 0 || 670 launch_proc(argv24) == 0 || launch_proc(argv25) == 0 || 671 launch_proc(argv26) == 0 || launch_proc(argv27) == 0 || 672 launch_proc(argv28) == 0 || launch_proc(argv30) == 0) { 673 printf("Error - " 674 "process ran without error with invalid --lcores flag\n"); 675 return -1; 676 } 677 678 if (rte_lcore_is_enabled(0) && rte_lcore_is_enabled(1) && 679 rte_lcore_is_enabled(2) && rte_lcore_is_enabled(3) && 680 rte_lcore_is_enabled(4) && rte_lcore_is_enabled(5) && 681 rte_lcore_is_enabled(6) && rte_lcore_is_enabled(7) && 682 launch_proc(argv29) != 0) { 683 printf("Error - " 684 "process did not run ok with valid corelist value\n"); 685 return -1; 686 } 687 688 return 0; 689 } 690 691 /* 692 * Test --main-lcore option with matching coremask 693 */ 694 static int 695 test_main_lcore_flag(void) 696 { 697 #ifdef RTE_EXEC_ENV_FREEBSD 698 /* BSD target doesn't support prefixes at this point */ 699 const char *prefix = ""; 700 #else 701 char prefix[PATH_MAX], tmp[PATH_MAX]; 702 if (get_current_prefix(tmp, sizeof(tmp)) == NULL) { 703 printf("Error - unable to get current prefix!\n"); 704 return -1; 705 } 706 snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp); 707 #endif 708 709 if (!rte_lcore_is_enabled(0) || !rte_lcore_is_enabled(1)) 710 return TEST_SKIPPED; 711 712 /* --main-lcore flag but no value */ 713 const char *argv1[] = { prgname, prefix, mp_flag, 714 "-c", "3", "--main-lcore"}; 715 /* --main-lcore flag with invalid value */ 716 const char *argv2[] = { prgname, prefix, mp_flag, 717 "-c", "3", "--main-lcore", "-1"}; 718 const char *argv3[] = { prgname, prefix, mp_flag, 719 "-c", "3", "--main-lcore", "X"}; 720 /* main lcore not in coremask */ 721 const char *argv4[] = { prgname, prefix, mp_flag, 722 "-c", "3", "--main-lcore", "2"}; 723 /* valid value */ 724 const char *argv5[] = { prgname, prefix, mp_flag, 725 "-c", "3", "--main-lcore", "1"}; 726 /* valid value set before coremask */ 727 const char *argv6[] = { prgname, prefix, mp_flag, 728 "--main-lcore", "1", "-c", "3"}; 729 730 if (launch_proc(argv1) == 0 731 || launch_proc(argv2) == 0 732 || launch_proc(argv3) == 0 733 || launch_proc(argv4) == 0) { 734 printf("Error - process ran without error with wrong --main-lcore\n"); 735 return -1; 736 } 737 if (launch_proc(argv5) != 0 738 || launch_proc(argv6) != 0) { 739 printf("Error - process did not run ok with valid --main-lcore\n"); 740 return -1; 741 } 742 return 0; 743 } 744 745 /* 746 * Test that the app doesn't run with invalid -n flag option. 747 * Final test ensures it does run with valid options as sanity check 748 * Since -n is not compulsory for MP, we instead use --no-huge and --no-shconf 749 * flags. 750 */ 751 static int 752 test_invalid_n_flag(void) 753 { 754 #ifdef RTE_EXEC_ENV_FREEBSD 755 /* BSD target doesn't support prefixes at this point */ 756 const char * prefix = ""; 757 #else 758 char prefix[PATH_MAX], tmp[PATH_MAX]; 759 if (get_current_prefix(tmp, sizeof(tmp)) == NULL) { 760 printf("Error - unable to get current prefix!\n"); 761 return -1; 762 } 763 snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp); 764 #endif 765 766 /* -n flag but no value */ 767 const char *argv1[] = { prgname, prefix, no_huge, no_shconf, 768 "-n"}; 769 /* bad numeric value */ 770 const char *argv2[] = { prgname, prefix, no_huge, no_shconf, 771 "-n", "e" }; 772 /* zero is invalid */ 773 const char *argv3[] = { prgname, prefix, no_huge, no_shconf, 774 "-n", "0" }; 775 /* sanity test - check with good value */ 776 const char *argv4[] = { prgname, prefix, no_huge, no_shconf, 777 "-n", "2" }; 778 /* sanity test - check with no -n flag */ 779 const char *argv5[] = { prgname, prefix, no_huge, no_shconf}; 780 781 if (launch_proc(argv1) == 0 782 || launch_proc(argv2) == 0 783 || launch_proc(argv3) == 0) { 784 printf("Error - process ran without error when" 785 "invalid -n flag\n"); 786 return -1; 787 } 788 if (launch_proc(argv4) != 0) { 789 printf("Error - process did not run ok with valid num-channel value\n"); 790 return -1; 791 } 792 if (launch_proc(argv5) != 0) { 793 printf("Error - process did not run ok without -n flag\n"); 794 return -1; 795 } 796 797 return 0; 798 } 799 800 /* 801 * Test that the app runs with HPET, and without HPET 802 */ 803 static int 804 test_no_hpet_flag(void) 805 { 806 char prefix[PATH_MAX] = ""; 807 808 #ifdef RTE_EXEC_ENV_FREEBSD 809 return 0; 810 #else 811 char tmp[PATH_MAX]; 812 if (get_current_prefix(tmp, sizeof(tmp)) == NULL) { 813 printf("Error - unable to get current prefix!\n"); 814 return -1; 815 } 816 snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp); 817 #endif 818 819 /* With --no-hpet */ 820 const char *argv1[] = {prgname, prefix, mp_flag, no_hpet}; 821 /* Without --no-hpet */ 822 const char *argv2[] = {prgname, prefix, mp_flag}; 823 824 if (launch_proc(argv1) != 0) { 825 printf("Error - process did not run ok with --no-hpet flag\n"); 826 return -1; 827 } 828 if (launch_proc(argv2) != 0) { 829 printf("Error - process did not run ok without --no-hpet flag\n"); 830 return -1; 831 } 832 return 0; 833 } 834 835 /* 836 * Test that the app runs with --no-huge and doesn't run when --socket-mem are 837 * specified with --no-huge. 838 */ 839 static int 840 test_no_huge_flag(void) 841 { 842 #ifdef RTE_EXEC_ENV_FREEBSD 843 /* BSD target doesn't support prefixes at this point, and we also need to 844 * run another primary process here */ 845 const char * prefix = no_shconf; 846 #else 847 const char * prefix = "--file-prefix=nohuge"; 848 #endif 849 850 /* With --no-huge */ 851 const char *argv1[] = {prgname, prefix, no_huge}; 852 /* With --no-huge and -m */ 853 const char *argv2[] = {prgname, prefix, no_huge, 854 "-m", DEFAULT_MEM_SIZE}; 855 856 /* With --no-huge and --socket-mem */ 857 const char *argv3[] = {prgname, prefix, no_huge, 858 "--socket-mem=" DEFAULT_MEM_SIZE}; 859 /* With --no-huge, -m and --socket-mem */ 860 const char *argv4[] = {prgname, prefix, no_huge, 861 "-m", DEFAULT_MEM_SIZE, "--socket-mem=" DEFAULT_MEM_SIZE}; 862 863 /* With --no-huge and --huge-worker-stack (should fail) */ 864 const char * const argv5[] = {prgname, prefix, no_huge, 865 "--huge-worker-stack"}; 866 /* With --no-huge and --huge-worker-stack=512 (should fail) */ 867 const char * const argv6[] = {prgname, prefix, no_huge, 868 "--huge-worker-stack=512"}; 869 870 if (launch_proc(argv1) != 0) { 871 printf("Error - process did not run ok with --no-huge flag\n"); 872 return -1; 873 } 874 if (launch_proc(argv2) != 0) { 875 printf("Error - process did not run ok with --no-huge and -m flags\n"); 876 return -1; 877 } 878 #ifdef RTE_EXEC_ENV_FREEBSD 879 /* no other tests are applicable to FreeBSD */ 880 return 0; 881 #endif 882 883 if (launch_proc(argv3) == 0) { 884 printf("Error - process run ok with --no-huge and --socket-mem " 885 "flags\n"); 886 return -1; 887 } 888 if (launch_proc(argv4) == 0) { 889 printf("Error - process run ok with --no-huge, -m and " 890 "--socket-mem flags\n"); 891 return -1; 892 } 893 if (launch_proc(argv5) == 0) { 894 printf("Error - process run ok with --no-huge and --huge-worker-stack flags"); 895 return -1; 896 } 897 if (launch_proc(argv6) == 0) { 898 printf("Error - process run ok with --no-huge and --huge-worker-stack=size flags"); 899 return -1; 900 } 901 return 0; 902 } 903 904 static int 905 test_misc_flags(void) 906 { 907 char hugepath[PATH_MAX] = {0}; 908 char hugepath_dir[PATH_MAX] = {0}; 909 char hugepath_dir2[PATH_MAX] = {0}; 910 char hugepath_dir3[PATH_MAX] = {0}; 911 #ifdef RTE_EXEC_ENV_FREEBSD 912 /* BSD target doesn't support prefixes at this point */ 913 const char * prefix = ""; 914 const char * nosh_prefix = ""; 915 #else 916 char prefix[PATH_MAX], tmp[PATH_MAX]; 917 const char * nosh_prefix = "--file-prefix=noshconf"; 918 FILE * hugedir_handle = NULL; 919 char line[PATH_MAX] = {0}; 920 unsigned i, isempty = 1; 921 922 if (get_current_prefix(tmp, sizeof(tmp)) == NULL) { 923 printf("Error - unable to get current prefix!\n"); 924 return -1; 925 } 926 snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp); 927 928 /* 929 * get first valid hugepage path 930 */ 931 932 /* get hugetlbfs mountpoints from /proc/mounts */ 933 hugedir_handle = fopen("/proc/mounts", "r"); 934 935 if (hugedir_handle == NULL) { 936 printf("Error opening /proc/mounts!\n"); 937 return -1; 938 } 939 940 /* read /proc/mounts */ 941 while (fgets(line, sizeof(line), hugedir_handle) != NULL) { 942 943 /* find first valid hugepath */ 944 if (get_hugepage_path(line, sizeof(line), hugepath, sizeof(hugepath))) 945 break; 946 } 947 948 fclose(hugedir_handle); 949 950 /* check if path is not empty */ 951 for (i = 0; i < sizeof(hugepath); i++) 952 if (hugepath[i] != '\0') 953 isempty = 0; 954 955 if (isempty) { 956 printf("No mounted hugepage dir found!\n"); 957 return -1; 958 } 959 #endif 960 961 snprintf(hugepath_dir, sizeof(hugepath_dir), "%s/dpdk.missing", hugepath); 962 snprintf(hugepath_dir2, sizeof(hugepath_dir2), "%s/dpdk.dir", hugepath); 963 964 if (mkdir(hugepath_dir2, 0700) != 0 && errno != EEXIST) { 965 printf("Error - failed to mkdir(%s)\n", hugepath_dir2); 966 return -1; 967 } 968 969 snprintf(hugepath_dir3, sizeof(hugepath_dir3), "%s/dpdk.dir/sub", hugepath); 970 971 if (mkdir(hugepath_dir3, 0700) != 0 && errno != EEXIST) { 972 printf("Error - failed to mkdir(%s)\n", hugepath_dir3); 973 goto fail; 974 } 975 976 /* check that some general flags don't prevent things from working. 977 * All cases, apart from the first, app should run. 978 * No further testing of output done. 979 */ 980 /* sanity check - failure with invalid option */ 981 const char *argv0[] = {prgname, prefix, mp_flag, "--invalid-opt"}; 982 983 /* With --no-pci */ 984 const char *argv1[] = {prgname, prefix, mp_flag, "--no-pci"}; 985 /* With -v */ 986 const char *argv2[] = {prgname, prefix, mp_flag, "-v"}; 987 /* With valid --syslog */ 988 const char *argv3[] = {prgname, prefix, mp_flag, "--syslog=user"}; 989 /* With empty --syslog (now defaults) */ 990 const char *argv4[] = {prgname, prefix, mp_flag, "--syslog"}; 991 /* With invalid --syslog */ 992 const char *argv5[] = {prgname, prefix, mp_flag, "--syslog=invalid"}; 993 994 /* With no-sh-conf, also use no-huge to ensure this test runs on BSD */ 995 const char *argv6[] = {prgname, "-m", DEFAULT_MEM_SIZE, 996 no_shconf, nosh_prefix, no_huge}; 997 998 /* With --huge-dir */ 999 const char *argv7[] = {prgname, "-m", DEFAULT_MEM_SIZE, 1000 "--file-prefix=hugedir", "--huge-dir", hugepath}; 1001 /* With empty --huge-dir (should fail) */ 1002 const char *argv8[] = {prgname, "-m", DEFAULT_MEM_SIZE, 1003 "--file-prefix=hugedir", "--huge-dir"}; 1004 /* With invalid --huge-dir */ 1005 const char *argv9[] = {prgname, "-m", DEFAULT_MEM_SIZE, 1006 "--file-prefix=hugedir", "--huge-dir", "invalid"}; 1007 /* With invalid --huge-dir sub-directory */ 1008 const char *argv10[] = {prgname, "-m", DEFAULT_MEM_SIZE, 1009 "--file-prefix=hugedir", "--huge-dir", hugepath_dir}; 1010 /* With valid --huge-dir sub-directory */ 1011 const char *argv11[] = {prgname, "-m", DEFAULT_MEM_SIZE, 1012 "--file-prefix=hugedir", "--huge-dir", hugepath_dir2}; 1013 /* Secondary process with invalid --huge-dir (should run as flag has no 1014 * effect on secondary processes) */ 1015 const char *argv12[] = {prgname, prefix, mp_flag, 1016 "--huge-dir", "invalid"}; 1017 1018 /* try running with base-virtaddr param */ 1019 const char *argv13[] = {prgname, "--file-prefix=virtaddr", 1020 "--base-virtaddr=0x23456789"}; 1021 1022 /* try running with --vfio-intr INTx flag */ 1023 const char *argv14[] = {prgname, "--file-prefix=intr", 1024 "--vfio-intr=legacy"}; 1025 1026 /* try running with --vfio-intr MSI flag */ 1027 const char *argv15[] = {prgname, "--file-prefix=intr", 1028 "--vfio-intr=msi"}; 1029 1030 /* try running with --vfio-intr MSI-X flag */ 1031 const char *argv16[] = {prgname, "--file-prefix=intr", 1032 "--vfio-intr=msix"}; 1033 1034 /* try running with --vfio-intr invalid flag */ 1035 const char *argv17[] = {prgname, "--file-prefix=intr", 1036 "--vfio-intr=invalid"}; 1037 1038 /* With process type as auto-detect */ 1039 const char * const argv18[] = {prgname, "--file-prefix=auto", 1040 "--proc-type=auto"}; 1041 1042 /* With process type as auto-detect with no-shconf */ 1043 const char * const argv19[] = {prgname, "--proc-type=auto", 1044 no_shconf, nosh_prefix, no_huge}; 1045 1046 /* With process type as --create-uio-dev flag */ 1047 const char * const argv20[] = {prgname, "--file-prefix=uiodev", 1048 "--create-uio-dev"}; 1049 1050 /* Try running with --huge-worker-stack flag */ 1051 const char * const argv21[] = {prgname, prefix, mp_flag, 1052 "--huge-worker-stack"}; 1053 1054 /* Try running with --huge-worker-stack=512 flag */ 1055 const char * const argv22[] = {prgname, prefix, mp_flag, 1056 "--huge-worker-stack=512"}; 1057 1058 /* Try running with --log-timestamp */ 1059 const char * const argv23[] = {prgname, prefix, mp_flag, 1060 "--log-timestamp" }; 1061 1062 /* Try running with --log-timestamp=iso */ 1063 const char * const argv24[] = {prgname, prefix, mp_flag, 1064 "--log-timestamp=iso" }; 1065 1066 /* Try running with invalid timestamp */ 1067 const char * const argv25[] = {prgname, prefix, mp_flag, 1068 "--log-timestamp=invalid" }; 1069 1070 /* Try running with --log-color */ 1071 const char * const argv26[] = {prgname, prefix, mp_flag, 1072 "--log-color" }; 1073 1074 /* Try running with --log-color=never */ 1075 const char * const argv27[] = {prgname, prefix, mp_flag, 1076 "--log-color=never" }; 1077 1078 /* Try running with --log-color=invalid */ 1079 const char * const argv28[] = {prgname, prefix, mp_flag, 1080 "--log-color=invalid" }; 1081 1082 /* run all tests also applicable to FreeBSD first */ 1083 1084 if (launch_proc(argv0) == 0) { 1085 printf("Error - process ran ok with invalid flag\n"); 1086 goto fail; 1087 } 1088 if (launch_proc(argv1) != 0) { 1089 printf("Error - process did not run ok with --no-pci flag\n"); 1090 goto fail; 1091 } 1092 if (launch_proc(argv2) != 0) { 1093 printf("Error - process did not run ok with -v flag\n"); 1094 goto fail; 1095 } 1096 if (launch_proc(argv6) != 0) { 1097 printf("Error - process did not run ok with --no-shconf flag\n"); 1098 goto fail; 1099 } 1100 1101 #ifdef RTE_EXEC_ENV_FREEBSD 1102 /* no more tests to be done on FreeBSD */ 1103 return 0; 1104 #endif 1105 1106 if (launch_proc(argv3) != 0) { 1107 printf("Error - process did not run ok with --syslog=user flag\n"); 1108 goto fail; 1109 } 1110 if (launch_proc(argv4) != 0) { 1111 printf("Error - process did not run ok with --syslog flag\n"); 1112 goto fail; 1113 } 1114 if (launch_proc(argv5) == 0) { 1115 printf("Error - process run ok with --syslog=invalid flag\n"); 1116 goto fail; 1117 } 1118 if (launch_proc(argv7) != 0) { 1119 printf("Error - process did not run ok with --huge-dir flag\n"); 1120 goto fail; 1121 } 1122 if (launch_proc(argv8) == 0) { 1123 printf("Error - process run ok with empty --huge-dir flag\n"); 1124 goto fail; 1125 } 1126 if (launch_proc(argv9) == 0) { 1127 printf("Error - process run ok with invalid --huge-dir flag\n"); 1128 goto fail; 1129 } 1130 if (launch_proc(argv10) == 0) { 1131 printf("Error - process run ok with invalid --huge-dir sub-dir flag\n"); 1132 goto fail; 1133 } 1134 if (launch_proc(argv11) != 0) { 1135 printf("Error - process did not run ok with --huge-dir subdir flag\n"); 1136 goto fail; 1137 } 1138 if (launch_proc(argv12) != 0) { 1139 printf("Error - secondary process did not run ok with invalid --huge-dir flag\n"); 1140 goto fail; 1141 } 1142 if (launch_proc(argv13) != 0) { 1143 printf("Error - process did not run ok with --base-virtaddr parameter\n"); 1144 goto fail; 1145 } 1146 if (launch_proc(argv14) != 0) { 1147 printf("Error - process did not run ok with " 1148 "--vfio-intr INTx parameter\n"); 1149 goto fail; 1150 } 1151 if (launch_proc(argv15) != 0) { 1152 printf("Error - process did not run ok with " 1153 "--vfio-intr MSI parameter\n"); 1154 goto fail; 1155 } 1156 if (launch_proc(argv16) != 0) { 1157 printf("Error - process did not run ok with " 1158 "--vfio-intr MSI-X parameter\n"); 1159 goto fail; 1160 } 1161 if (launch_proc(argv17) == 0) { 1162 printf("Error - process run ok with " 1163 "--vfio-intr invalid parameter\n"); 1164 goto fail; 1165 } 1166 if (launch_proc(argv18) != 0) { 1167 printf("Error - process did not run ok with " 1168 "--proc-type as auto parameter\n"); 1169 goto fail; 1170 } 1171 if (launch_proc(argv19) != 0) { 1172 printf("Error - process did not run ok with " 1173 "--proc-type and --no-shconf parameter\n"); 1174 goto fail; 1175 } 1176 if (launch_proc(argv20) != 0) { 1177 printf("Error - process did not run ok with " 1178 "--create-uio-dev parameter\n"); 1179 goto fail; 1180 } 1181 if (launch_proc(argv21) != 0) { 1182 printf("Error - process did not run ok with --huge-worker-stack parameter\n"); 1183 goto fail; 1184 } 1185 if (launch_proc(argv22) != 0) { 1186 printf("Error - process did not run ok with --huge-worker-stack=size parameter\n"); 1187 goto fail; 1188 } 1189 if (launch_proc(argv23) != 0) { 1190 printf("Error - process did not run ok with --log-timestamp parameter\n"); 1191 goto fail; 1192 } 1193 if (launch_proc(argv24) != 0) { 1194 printf("Error - process did not run ok with --log-timestamp=iso parameter\n"); 1195 goto fail; 1196 } 1197 if (launch_proc(argv25) == 0) { 1198 printf("Error - process did run ok with --log-timestamp=invalid parameter\n"); 1199 goto fail; 1200 } 1201 if (launch_proc(argv26) != 0) { 1202 printf("Error - process did not run ok with --log-color parameter\n"); 1203 goto fail; 1204 } 1205 if (launch_proc(argv27) != 0) { 1206 printf("Error - process did not run ok with --log-color=never parameter\n"); 1207 goto fail; 1208 } 1209 if (launch_proc(argv28) == 0) { 1210 printf("Error - process did run ok with --log-timestamp=invalid parameter\n"); 1211 goto fail; 1212 } 1213 1214 rmdir(hugepath_dir3); 1215 rmdir(hugepath_dir2); 1216 return 0; 1217 1218 fail: 1219 rmdir(hugepath_dir3); 1220 rmdir(hugepath_dir2); 1221 return -1; 1222 } 1223 1224 static int 1225 test_file_prefix(void) 1226 { 1227 /* 1228 * 1. check if current process hugefiles are locked 1229 * 2. try to run secondary process without a corresponding primary process 1230 * (while failing to run, it will also remove any unused hugepage files) 1231 * 3. check if current process hugefiles are still in place and are locked 1232 * 4. run a primary process with memtest1 prefix in default and legacy 1233 * mem mode 1234 * 5. check if memtest1 hugefiles are created in case of legacy mem 1235 * mode, and deleted in case of default mem mode 1236 * 6. run a primary process with memtest2 prefix in default and legacy 1237 * mem modes 1238 * 7. check that memtest2 hugefiles are present in the hugedir after a 1239 * run in legacy mode, and not present at all after run in default 1240 * mem mode 1241 */ 1242 char prefix[PATH_MAX] = ""; 1243 1244 #ifdef RTE_EXEC_ENV_FREEBSD 1245 return 0; 1246 #else 1247 if (get_current_prefix(prefix, sizeof(prefix)) == NULL) { 1248 printf("Error - unable to get current prefix!\n"); 1249 return -1; 1250 } 1251 #endif 1252 1253 /* this should fail unless the test itself is run with "memtest" prefix */ 1254 const char *argv0[] = {prgname, mp_flag, "-m", 1255 DEFAULT_MEM_SIZE, "--file-prefix=" memtest }; 1256 1257 /* primary process with memtest1 and default mem mode */ 1258 const char *argv1[] = {prgname, "-m", 1259 DEFAULT_MEM_SIZE, "--file-prefix=" memtest1 }; 1260 1261 /* primary process with memtest1 and legacy mem mode */ 1262 const char *argv2[] = {prgname, "-m", 1263 DEFAULT_MEM_SIZE, "--file-prefix=" memtest1, 1264 "--legacy-mem" }; 1265 1266 /* primary process with memtest2 and legacy mem mode */ 1267 const char *argv3[] = {prgname, "-m", 1268 DEFAULT_MEM_SIZE, "--file-prefix=" memtest2, 1269 "--legacy-mem" }; 1270 1271 /* primary process with memtest2 and default mem mode */ 1272 const char *argv4[] = {prgname, "-m", 1273 DEFAULT_MEM_SIZE, "--file-prefix=" memtest2 }; 1274 1275 /* primary process with --in-memory mode */ 1276 const char * const argv5[] = {prgname, "-m", 1277 DEFAULT_MEM_SIZE, "--in-memory" }; 1278 1279 /* primary process with memtest1 and --in-memory mode */ 1280 const char * const argv6[] = {prgname, "-m", 1281 DEFAULT_MEM_SIZE, "--in-memory", 1282 "--file-prefix=" memtest1 }; 1283 1284 /* primary process with parent file-prefix and --in-memory mode */ 1285 const char * const argv7[] = {prgname, "-m", 1286 DEFAULT_MEM_SIZE, "--in-memory", "--file-prefix", prefix }; 1287 1288 /* primary process with memtest1 and --single-file-segments mode */ 1289 const char * const argv8[] = {prgname, "-m", 1290 DEFAULT_MEM_SIZE, "--single-file-segments", 1291 "--file-prefix=" memtest1 }; 1292 1293 /* primary process with memtest1 and --huge-unlink=never mode */ 1294 const char * const argv9[] = {prgname, "-m", 1295 DEFAULT_MEM_SIZE, "--huge-unlink=never", 1296 "--file-prefix=" memtest1 }; 1297 1298 /* check if files for current prefix are present */ 1299 if (process_hugefiles(prefix, HUGEPAGE_CHECK_EXISTS) != 1) { 1300 printf("Error - hugepage files for %s were not created!\n", prefix); 1301 return -1; 1302 } 1303 1304 /* checks if files for current prefix are locked */ 1305 if (process_hugefiles(prefix, HUGEPAGE_CHECK_LOCKED) != 1) { 1306 printf("Error - hugepages for current process aren't locked!\n"); 1307 return -1; 1308 } 1309 1310 /* check if files for secondary process are present */ 1311 if (process_hugefiles(memtest, HUGEPAGE_CHECK_EXISTS) == 1) { 1312 /* check if they are not locked */ 1313 if (process_hugefiles(memtest, HUGEPAGE_CHECK_LOCKED) == 1) { 1314 printf("Error - hugepages for current process are locked!\n"); 1315 return -1; 1316 } 1317 /* they aren't locked, delete them */ 1318 else { 1319 if (process_hugefiles(memtest, HUGEPAGE_DELETE) != 1) { 1320 printf("Error - deleting hugepages failed!\n"); 1321 return -1; 1322 } 1323 } 1324 } 1325 1326 if (launch_proc(argv0) == 0) { 1327 printf("Error - secondary process ran ok without primary process\n"); 1328 return -1; 1329 } 1330 1331 /* check if files for current prefix are present */ 1332 if (process_hugefiles(prefix, HUGEPAGE_CHECK_EXISTS) != 1) { 1333 printf("Error - hugepage files for %s were not created!\n", prefix); 1334 return -1; 1335 } 1336 1337 /* checks if files for current prefix are locked */ 1338 if (process_hugefiles(prefix, HUGEPAGE_CHECK_LOCKED) != 1) { 1339 printf("Error - hugepages for current process aren't locked!\n"); 1340 return -1; 1341 } 1342 1343 /* we're running this process in default memory mode, which means it 1344 * should clean up after itself on exit and leave no hugepages behind. 1345 */ 1346 if (launch_proc(argv1) != 0) { 1347 printf("Error - failed to run with --file-prefix=%s\n", 1348 memtest1); 1349 return -1; 1350 } 1351 1352 /* check if memtest1_map0 is present */ 1353 if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 0) { 1354 printf("Error - hugepage files for %s were not deleted!\n", 1355 memtest1); 1356 return -1; 1357 } 1358 1359 /* now, we're running a process under the same prefix, but with legacy 1360 * mem mode - this should leave behind hugepage files. 1361 */ 1362 if (launch_proc(argv2) != 0) { 1363 printf("Error - failed to run with --file-prefix=%s\n", 1364 memtest1); 1365 return -1; 1366 } 1367 1368 /* check if memtest1_map0 is present */ 1369 if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 1) { 1370 printf("Error - hugepage files for %s were not created!\n", 1371 memtest1); 1372 return -1; 1373 } 1374 1375 if (launch_proc(argv3) != 0) { 1376 printf("Error - failed to run with --file-prefix=%s\n", 1377 memtest2); 1378 return -1; 1379 } 1380 1381 /* check if hugefiles for memtest2 are present */ 1382 if (process_hugefiles(memtest2, HUGEPAGE_CHECK_EXISTS) != 1) { 1383 printf("Error - hugepage files for %s were not created!\n", 1384 memtest2); 1385 return -1; 1386 } 1387 1388 /* check if hugefiles for memtest1 are present */ 1389 if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 0) { 1390 printf("Error - hugepage files for %s were not deleted!\n", 1391 memtest1); 1392 return -1; 1393 } 1394 1395 /* this process will run in default mem mode, so it should not leave any 1396 * hugepage files behind. 1397 */ 1398 if (launch_proc(argv4) != 0) { 1399 printf("Error - failed to run with --file-prefix=%s\n", 1400 memtest2); 1401 return -1; 1402 } 1403 1404 /* check if hugefiles for memtest2 are present */ 1405 if (process_hugefiles(memtest2, HUGEPAGE_CHECK_EXISTS) != 0) { 1406 printf("Error - hugepage files for %s were not deleted!\n", 1407 memtest2); 1408 return -1; 1409 } 1410 1411 /* check if hugefiles for memtest1 are present */ 1412 if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 0) { 1413 printf("Error - hugepage files for %s were not deleted!\n", 1414 memtest1); 1415 return -1; 1416 } 1417 1418 /* this process will run in --in-memory mode, so it should not leave any 1419 * hugepage files behind. 1420 */ 1421 1422 /* test case to check eal-options with --in-memory mode */ 1423 if (launch_proc(argv5) != 0) { 1424 printf("Error - failed to run with --in-memory mode\n"); 1425 return -1; 1426 } 1427 1428 /*test case to check eal-options with --in-memory mode with 1429 * custom file-prefix. 1430 */ 1431 if (launch_proc(argv6) != 0) { 1432 printf("Error - failed to run with --in-memory mode\n"); 1433 return -1; 1434 } 1435 1436 /* check if hugefiles for memtest1 are present */ 1437 if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 0) { 1438 printf("Error - hugepage files for %s were created and not deleted!\n", 1439 memtest1); 1440 return -1; 1441 } 1442 1443 /* test case to check eal-options with --in-memory mode with 1444 * parent file-prefix. 1445 */ 1446 if (launch_proc(argv7) != 0) { 1447 printf("Error - failed to run with --file-prefix=%s\n", prefix); 1448 return -1; 1449 } 1450 1451 /* this process will run in --single-file-segments mode, 1452 * so it should not leave any hugepage files behind. 1453 */ 1454 if (launch_proc(argv8) != 0) { 1455 printf("Error - failed to run with --single-file-segments mode\n"); 1456 return -1; 1457 } 1458 1459 /* check if hugefiles for memtest1 are present */ 1460 if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 0) { 1461 printf("Error - hugepage files for %s were not deleted!\n", 1462 memtest1); 1463 return -1; 1464 } 1465 1466 /* this process will run with --huge-unlink, 1467 * so it should not remove hugepage files when it exits 1468 */ 1469 if (launch_proc(argv9) != 0) { 1470 printf("Error - failed to run with --huge-unlink=never\n"); 1471 return -1; 1472 } 1473 1474 /* check if hugefiles for memtest1 are present */ 1475 if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) == 0) { 1476 printf("Error - hugepage files for %s were deleted!\n", 1477 memtest1); 1478 return -1; 1479 } 1480 if (process_hugefiles(memtest1, HUGEPAGE_DELETE) != 1) { 1481 printf("Error - deleting hugepages failed!\n"); 1482 return -1; 1483 } 1484 1485 return 0; 1486 } 1487 1488 /* This function writes in passed buf pointer a valid --socket-mem= option 1489 * for num_sockets then concatenates the provided suffix string. 1490 * 1491 * Example for num_sockets 4, mem "2", suffix "plop" 1492 * --socket-mem=2,2,2,2plop 1493 */ 1494 static void 1495 populate_socket_mem_param(int num_sockets, const char *mem, 1496 const char *suffix, char *buf, size_t buf_size) 1497 { 1498 unsigned int offset = 0; 1499 int written; 1500 int i; 1501 1502 written = snprintf(&buf[offset], buf_size - offset, "--socket-mem="); 1503 if (written < 0 || written + offset >= buf_size) 1504 return; 1505 offset += written; 1506 1507 for (i = 0; i < num_sockets - 1; i++) { 1508 written = snprintf(&buf[offset], buf_size - offset, 1509 "%s,", mem); 1510 if (written < 0 || written + offset >= buf_size) 1511 return; 1512 offset += written; 1513 } 1514 1515 written = snprintf(&buf[offset], buf_size - offset, "%s%s", mem, 1516 suffix); 1517 if (written < 0 || written + offset >= buf_size) 1518 return; 1519 offset += written; 1520 } 1521 1522 /* 1523 * Tests for correct handling of -m and --socket-mem flags 1524 */ 1525 static int 1526 test_memory_flags(void) 1527 { 1528 #ifdef RTE_EXEC_ENV_FREEBSD 1529 /* BSD target doesn't support prefixes at this point */ 1530 const char * prefix = ""; 1531 #else 1532 char prefix[PATH_MAX], tmp[PATH_MAX]; 1533 if (get_current_prefix(tmp, sizeof(tmp)) == NULL) { 1534 printf("Error - unable to get current prefix!\n"); 1535 return -1; 1536 } 1537 snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp); 1538 #endif 1539 1540 /* valid -m flag and mp flag */ 1541 const char *argv0[] = {prgname, prefix, mp_flag, 1542 "-m", DEFAULT_MEM_SIZE}; 1543 1544 /* valid -m flag */ 1545 const char *argv1[] = {prgname, 1546 "--file-prefix=" memtest, "-m", DEFAULT_MEM_SIZE}; 1547 1548 /* valid (zero) --socket-mem flag */ 1549 char arg2_socket_mem[SOCKET_MEM_STRLEN]; 1550 const char *argv2[] = {prgname, 1551 "--file-prefix=" memtest, arg2_socket_mem}; 1552 1553 /* invalid (incomplete) --socket-mem flag */ 1554 char arg3_socket_mem[SOCKET_MEM_STRLEN]; 1555 const char *argv3[] = {prgname, 1556 "--file-prefix=" memtest, arg3_socket_mem}; 1557 1558 /* invalid (mixed with invalid data) --socket-mem flag */ 1559 char arg4_socket_mem[SOCKET_MEM_STRLEN]; 1560 const char *argv4[] = {prgname, 1561 "--file-prefix=" memtest, arg4_socket_mem}; 1562 1563 /* invalid (with numeric value as last character) --socket-mem flag */ 1564 char arg5_socket_mem[SOCKET_MEM_STRLEN]; 1565 const char *argv5[] = {prgname, 1566 "--file-prefix=" memtest, arg5_socket_mem}; 1567 1568 /* invalid (with empty socket) --socket-mem flag */ 1569 char arg6_socket_mem[SOCKET_MEM_STRLEN]; 1570 const char *argv6[] = {prgname, 1571 "--file-prefix=" memtest, arg6_socket_mem}; 1572 1573 /* invalid (null) --socket-mem flag */ 1574 const char *argv7[] = {prgname, 1575 "--file-prefix=" memtest, "--socket-mem="}; 1576 1577 /* valid --socket-mem specified together with -m flag */ 1578 char arg8_socket_mem[SOCKET_MEM_STRLEN]; 1579 const char *argv8[] = {prgname, 1580 "--file-prefix=" memtest, "-m", DEFAULT_MEM_SIZE, 1581 arg8_socket_mem}; 1582 1583 #ifdef RTE_EXEC_ENV_FREEBSD 1584 int num_sockets = 1; 1585 #else 1586 int num_sockets = RTE_MIN(get_number_of_sockets(), 1587 RTE_MAX_NUMA_NODES); 1588 #endif 1589 1590 if (num_sockets <= 0) { 1591 printf("Error - cannot get number of sockets!\n"); 1592 return -1; 1593 } 1594 1595 /* invalid --socket-mem flag (with extra socket) */ 1596 char invalid_socket_mem[SOCKET_MEM_STRLEN]; 1597 const char *argv9[] = {prgname, 1598 "--file-prefix=" memtest, invalid_socket_mem}; 1599 1600 /* valid --socket-mem flag */ 1601 char valid_socket_mem[SOCKET_MEM_STRLEN]; 1602 const char *argv10[] = {prgname, 1603 "--file-prefix=" memtest, valid_socket_mem}; 1604 1605 if (launch_proc(argv0) != 0) { 1606 printf("Error - secondary process failed with valid -m flag !\n"); 1607 return -1; 1608 } 1609 1610 #ifdef RTE_EXEC_ENV_FREEBSD 1611 /* no other tests are applicable to BSD */ 1612 return 0; 1613 #endif 1614 1615 if (launch_proc(argv1) != 0) { 1616 printf("Error - process failed with valid -m flag!\n"); 1617 return -1; 1618 } 1619 1620 populate_socket_mem_param(num_sockets, "0", "", 1621 arg2_socket_mem, sizeof(arg2_socket_mem)); 1622 if (launch_proc(argv2) != 0) { 1623 printf("Error - process failed with valid (zero) --socket-mem!\n"); 1624 return -1; 1625 } 1626 1627 if (num_sockets > 1) { 1628 populate_socket_mem_param(num_sockets - 1, "2", ",", 1629 arg3_socket_mem, sizeof(arg3_socket_mem)); 1630 if (launch_proc(argv3) == 0) { 1631 printf("Error - process run ok with invalid " 1632 "(incomplete) --socket-mem!\n"); 1633 return -1; 1634 } 1635 1636 populate_socket_mem_param(num_sockets - 1, "2", ",Fred", 1637 arg4_socket_mem, sizeof(arg4_socket_mem)); 1638 if (launch_proc(argv4) == 0) { 1639 printf("Error - process run ok with invalid " 1640 "(mixed with invalid input) --socket-mem!\n"); 1641 return -1; 1642 } 1643 1644 populate_socket_mem_param(num_sockets - 1, "2", ",Fred0", 1645 arg5_socket_mem, sizeof(arg5_socket_mem)); 1646 if (launch_proc(argv5) == 0) { 1647 printf("Error - process run ok with invalid " 1648 "(mixed with invalid input with a numeric value as " 1649 "last character) --socket-mem!\n"); 1650 return -1; 1651 } 1652 } 1653 1654 if (num_sockets > 2) { 1655 populate_socket_mem_param(num_sockets - 2, "2", ",,2", 1656 arg6_socket_mem, sizeof(arg6_socket_mem)); 1657 if (launch_proc(argv6) == 0) { 1658 printf("Error - process run ok with invalid " 1659 "(with empty socket) --socket-mem!\n"); 1660 return -1; 1661 } 1662 } 1663 1664 if (launch_proc(argv7) == 0) { 1665 printf("Error - process run ok with invalid (null) --socket-mem!\n"); 1666 return -1; 1667 } 1668 1669 populate_socket_mem_param(num_sockets, "2", "", 1670 arg8_socket_mem, sizeof(arg8_socket_mem)); 1671 if (launch_proc(argv8) == 0) { 1672 printf("Error - process run ok with --socket-mem and -m specified!\n"); 1673 return -1; 1674 } 1675 1676 populate_socket_mem_param(num_sockets + 1, "2", "", 1677 invalid_socket_mem, sizeof(invalid_socket_mem)); 1678 if (launch_proc(argv9) == 0) { 1679 printf("Error - process run ok with extra socket in --socket-mem!\n"); 1680 return -1; 1681 } 1682 1683 populate_socket_mem_param(num_sockets, "2", "", 1684 valid_socket_mem, sizeof(valid_socket_mem)); 1685 if (launch_proc(argv10) != 0) { 1686 printf("Error - process failed with valid --socket-mem!\n"); 1687 return -1; 1688 } 1689 1690 return 0; 1691 } 1692 1693 #endif /* !RTE_EXEC_ENV_WINDOWS */ 1694 1695 REGISTER_FAST_TEST(eal_flags_c_opt_autotest, false, false, test_missing_c_flag); 1696 REGISTER_FAST_TEST(eal_flags_main_opt_autotest, false, false, test_main_lcore_flag); 1697 REGISTER_FAST_TEST(eal_flags_n_opt_autotest, false, false, test_invalid_n_flag); 1698 REGISTER_FAST_TEST(eal_flags_hpet_autotest, false, false, test_no_hpet_flag); 1699 REGISTER_FAST_TEST(eal_flags_no_huge_autotest, false, false, test_no_huge_flag); 1700 REGISTER_FAST_TEST(eal_flags_a_opt_autotest, false, false, test_allow_flag); 1701 REGISTER_FAST_TEST(eal_flags_b_opt_autotest, false, false, test_invalid_b_flag); 1702 REGISTER_FAST_TEST(eal_flags_vdev_opt_autotest, false, false, test_invalid_vdev_flag); 1703 REGISTER_FAST_TEST(eal_flags_r_opt_autotest, false, false, test_invalid_r_flag); 1704 REGISTER_FAST_TEST(eal_flags_mem_autotest, false, false, test_memory_flags); 1705 REGISTER_FAST_TEST(eal_flags_file_prefix_autotest, false, false, test_file_prefix); 1706 REGISTER_FAST_TEST(eal_flags_misc_autotest, false, false, test_misc_flags); 1707