1 // SPDX-License-Identifier: GPL-2.0 2 3 #include <test_progs.h> 4 #include <bpf/btf.h> 5 #include <search.h> 6 #include "bpf/libbpf_internal.h" 7 #include "tracing_multi.skel.h" 8 #include "tracing_multi_module.skel.h" 9 #include "tracing_multi_intersect.skel.h" 10 #include "tracing_multi_session.skel.h" 11 #include "tracing_multi_fail.skel.h" 12 #include "tracing_multi_verifier.skel.h" 13 #include "trace_helpers.h" 14 15 static __u64 bpf_fentry_test_cookies[] = { 16 8, /* bpf_fentry_test1 */ 17 9, /* bpf_fentry_test2 */ 18 7, /* bpf_fentry_test3 */ 19 5, /* bpf_fentry_test4 */ 20 4, /* bpf_fentry_test5 */ 21 2, /* bpf_fentry_test6 */ 22 3, /* bpf_fentry_test7 */ 23 1, /* bpf_fentry_test8 */ 24 10, /* bpf_fentry_test9 */ 25 6, /* bpf_fentry_test10 */ 26 }; 27 28 static const char * const bpf_fentry_test[] = { 29 "bpf_fentry_test1", 30 "bpf_fentry_test2", 31 "bpf_fentry_test3", 32 "bpf_fentry_test4", 33 "bpf_fentry_test5", 34 "bpf_fentry_test6", 35 "bpf_fentry_test7", 36 "bpf_fentry_test8", 37 "bpf_fentry_test9", 38 "bpf_fentry_test10", 39 }; 40 41 static const char * const bpf_testmod_fentry_test[] = { 42 "bpf_testmod_fentry_test1", 43 "bpf_testmod_fentry_test2", 44 "bpf_testmod_fentry_test3", 45 "bpf_testmod_fentry_test7", 46 "bpf_testmod_fentry_test11", 47 }; 48 49 #define FUNCS_CNT (ARRAY_SIZE(bpf_fentry_test)) 50 51 static int get_random_funcs(const char **funcs) 52 { 53 int i, cnt = 0; 54 55 for (i = 0; i < FUNCS_CNT; i++) { 56 if (rand() % 2) 57 funcs[cnt++] = bpf_fentry_test[i]; 58 } 59 /* we always need at least one.. */ 60 if (!cnt) 61 funcs[cnt++] = bpf_fentry_test[rand() % FUNCS_CNT]; 62 return cnt; 63 } 64 65 static int compare(const void *ppa, const void *ppb) 66 { 67 const char *pa = *(const char **) ppa; 68 const char *pb = *(const char **) ppb; 69 70 return strcmp(pa, pb); 71 } 72 73 static void tdestroy_free_nop(void *ptr) 74 { 75 } 76 77 static __u32 *get_ids(const char * const funcs[], int funcs_cnt, const char *mod) 78 { 79 struct btf *btf, *vmlinux_btf = NULL; 80 __u32 nr, type_id, cnt = 0; 81 void *root = NULL; 82 __u32 *ids = NULL; 83 int i, err = 0; 84 85 btf = btf__load_vmlinux_btf(); 86 if (!ASSERT_OK_PTR(btf, "btf__load_vmlinux_btf")) 87 return NULL; 88 89 if (mod) { 90 vmlinux_btf = btf; 91 btf = btf__load_module_btf(mod, vmlinux_btf); 92 if (!ASSERT_OK_PTR(btf, "btf__load_module_btf")) { 93 btf__free(vmlinux_btf); 94 return NULL; 95 } 96 } 97 98 ids = calloc(funcs_cnt, sizeof(ids[0])); 99 if (!ids) 100 goto out; 101 102 /* 103 * We sort function names by name and search them 104 * below for each function. 105 */ 106 for (i = 0; i < funcs_cnt; i++) { 107 if (!tsearch(&funcs[i], &root, compare)) { 108 ASSERT_FAIL("tsearch failed"); 109 err = -1; 110 goto error; 111 } 112 } 113 114 nr = btf__type_cnt(btf); 115 for (type_id = 1; type_id < nr && cnt < funcs_cnt; type_id++) { 116 const struct btf_type *type; 117 const char *str, ***val; 118 unsigned int idx; 119 120 type = btf__type_by_id(btf, type_id); 121 if (!type) { 122 err = -1; 123 break; 124 } 125 126 if (BTF_INFO_KIND(type->info) != BTF_KIND_FUNC) 127 continue; 128 129 str = btf__name_by_offset(btf, type->name_off); 130 if (!str) { 131 err = -1; 132 break; 133 } 134 135 val = tfind(&str, &root, compare); 136 if (!val) 137 continue; 138 139 /* 140 * We keep pointer for each function name so we can get the original 141 * array index and have the resulting ids array matching the original 142 * function array. 143 * 144 * Doing it this way allow us to easily test the cookies support, 145 * because each cookie is attached to particular function/id. 146 */ 147 idx = *val - funcs; 148 ids[idx] = type_id; 149 cnt++; 150 } 151 152 error: 153 if (err) { 154 free(ids); 155 ids = NULL; 156 } 157 158 out: 159 tdestroy(root, tdestroy_free_nop); 160 btf__free(vmlinux_btf); 161 btf__free(btf); 162 return ids; 163 } 164 165 static void tracing_multi_test_run(struct tracing_multi *skel) 166 { 167 LIBBPF_OPTS(bpf_test_run_opts, topts); 168 int err, prog_fd; 169 170 prog_fd = bpf_program__fd(skel->progs.test_fentry); 171 err = bpf_prog_test_run_opts(prog_fd, &topts); 172 ASSERT_OK(err, "test_run"); 173 174 /* extra +1 count for sleepable programs */ 175 ASSERT_EQ(skel->bss->test_result_fentry, FUNCS_CNT + 1, "test_result_fentry"); 176 ASSERT_EQ(skel->bss->test_result_fexit, FUNCS_CNT + 1, "test_result_fexit"); 177 } 178 179 static void test_skel_api(void) 180 { 181 struct tracing_multi *skel; 182 int err; 183 184 skel = tracing_multi__open_and_load(); 185 if (!ASSERT_OK_PTR(skel, "tracing_multi__open_and_load")) 186 return; 187 188 skel->bss->pid = getpid(); 189 190 err = tracing_multi__attach(skel); 191 if (!ASSERT_OK(err, "tracing_multi__attach")) 192 goto cleanup; 193 194 tracing_multi_test_run(skel); 195 196 cleanup: 197 tracing_multi__destroy(skel); 198 } 199 200 static void test_link_api_pattern(void) 201 { 202 struct tracing_multi *skel; 203 204 skel = tracing_multi__open_and_load(); 205 if (!ASSERT_OK_PTR(skel, "tracing_multi__open_and_load")) 206 return; 207 208 skel->bss->pid = getpid(); 209 210 skel->links.test_fentry = bpf_program__attach_tracing_multi(skel->progs.test_fentry, 211 "bpf_fentry_test*", NULL); 212 if (!ASSERT_OK_PTR(skel->links.test_fentry, "bpf_program__attach_tracing_multi")) 213 goto cleanup; 214 215 skel->links.test_fexit = bpf_program__attach_tracing_multi(skel->progs.test_fexit, 216 "bpf_fentry_test*", NULL); 217 if (!ASSERT_OK_PTR(skel->links.test_fexit, "bpf_program__attach_tracing_multi")) 218 goto cleanup; 219 220 skel->links.test_fentry_s = bpf_program__attach_tracing_multi(skel->progs.test_fentry_s, 221 "bpf_fentry_test1", NULL); 222 if (!ASSERT_OK_PTR(skel->links.test_fentry_s, "bpf_program__attach_tracing_multi")) 223 goto cleanup; 224 225 skel->links.test_fexit_s = bpf_program__attach_tracing_multi(skel->progs.test_fexit_s, 226 "bpf_fentry_test1", NULL); 227 if (!ASSERT_OK_PTR(skel->links.test_fexit_s, "bpf_program__attach_tracing_multi")) 228 goto cleanup; 229 230 tracing_multi_test_run(skel); 231 232 cleanup: 233 tracing_multi__destroy(skel); 234 } 235 236 static void test_link_api_ids(bool test_cookies) 237 { 238 LIBBPF_OPTS(bpf_tracing_multi_opts, opts); 239 struct tracing_multi *skel; 240 size_t cnt = FUNCS_CNT; 241 __u32 *ids; 242 243 skel = tracing_multi__open_and_load(); 244 if (!ASSERT_OK_PTR(skel, "tracing_multi__open_and_load")) 245 return; 246 247 skel->bss->pid = getpid(); 248 skel->bss->test_cookies = test_cookies; 249 250 ids = get_ids(bpf_fentry_test, cnt, NULL); 251 if (!ASSERT_OK_PTR(ids, "get_ids")) 252 goto cleanup; 253 254 opts.ids = ids; 255 opts.cnt = cnt; 256 257 if (test_cookies) 258 opts.cookies = bpf_fentry_test_cookies; 259 260 skel->links.test_fentry = bpf_program__attach_tracing_multi(skel->progs.test_fentry, 261 NULL, &opts); 262 if (!ASSERT_OK_PTR(skel->links.test_fentry, "bpf_program__attach_tracing_multi")) 263 goto cleanup; 264 265 skel->links.test_fexit = bpf_program__attach_tracing_multi(skel->progs.test_fexit, 266 NULL, &opts); 267 if (!ASSERT_OK_PTR(skel->links.test_fexit, "bpf_program__attach_tracing_multi")) 268 goto cleanup; 269 270 /* Only bpf_fentry_test1 is allowed for sleepable programs. */ 271 opts.cnt = 1; 272 skel->links.test_fentry_s = bpf_program__attach_tracing_multi(skel->progs.test_fentry_s, 273 NULL, &opts); 274 if (!ASSERT_OK_PTR(skel->links.test_fentry_s, "bpf_program__attach_tracing_multi")) 275 goto cleanup; 276 277 skel->links.test_fexit_s = bpf_program__attach_tracing_multi(skel->progs.test_fexit_s, 278 NULL, &opts); 279 if (!ASSERT_OK_PTR(skel->links.test_fexit_s, "bpf_program__attach_tracing_multi")) 280 goto cleanup; 281 282 tracing_multi_test_run(skel); 283 284 cleanup: 285 tracing_multi__destroy(skel); 286 free(ids); 287 } 288 289 static void test_module_skel_api(void) 290 { 291 struct tracing_multi_module *skel = NULL; 292 int err; 293 294 skel = tracing_multi_module__open_and_load(); 295 if (!ASSERT_OK_PTR(skel, "tracing_multi__open_and_load")) 296 return; 297 298 skel->bss->pid = getpid(); 299 300 err = tracing_multi_module__attach(skel); 301 if (!ASSERT_OK(err, "tracing_multi__attach")) 302 goto cleanup; 303 304 ASSERT_OK(trigger_module_test_read(1), "trigger_read"); 305 ASSERT_EQ(skel->bss->test_result_fentry, 5, "test_result_fentry"); 306 ASSERT_EQ(skel->bss->test_result_fexit, 5, "test_result_fexit"); 307 308 cleanup: 309 tracing_multi_module__destroy(skel); 310 } 311 312 static void test_module_link_api_pattern(void) 313 { 314 struct tracing_multi_module *skel = NULL; 315 316 skel = tracing_multi_module__open_and_load(); 317 if (!ASSERT_OK_PTR(skel, "tracing_multi_module__open_and_load")) 318 return; 319 320 skel->bss->pid = getpid(); 321 322 skel->links.test_fentry = bpf_program__attach_tracing_multi(skel->progs.test_fentry, 323 "bpf_testmod:bpf_testmod_fentry_test*", NULL); 324 if (!ASSERT_OK_PTR(skel->links.test_fentry, "bpf_program__attach_tracing_multi")) 325 goto cleanup; 326 327 skel->links.test_fexit = bpf_program__attach_tracing_multi(skel->progs.test_fexit, 328 "bpf_testmod:bpf_testmod_fentry_test*", NULL); 329 if (!ASSERT_OK_PTR(skel->links.test_fexit, "bpf_program__attach_tracing_multi")) 330 goto cleanup; 331 332 ASSERT_OK(trigger_module_test_read(1), "trigger_read"); 333 ASSERT_EQ(skel->bss->test_result_fentry, 5, "test_result_fentry"); 334 ASSERT_EQ(skel->bss->test_result_fexit, 5, "test_result_fexit"); 335 336 cleanup: 337 tracing_multi_module__destroy(skel); 338 } 339 340 static void test_module_link_api_ids(void) 341 { 342 size_t cnt = ARRAY_SIZE(bpf_testmod_fentry_test); 343 LIBBPF_OPTS(bpf_tracing_multi_opts, opts); 344 struct tracing_multi_module *skel = NULL; 345 __u32 *ids; 346 347 skel = tracing_multi_module__open_and_load(); 348 if (!ASSERT_OK_PTR(skel, "tracing_multi_module__open_and_load")) 349 return; 350 351 skel->bss->pid = getpid(); 352 353 ids = get_ids(bpf_testmod_fentry_test, cnt, "bpf_testmod"); 354 if (!ASSERT_OK_PTR(ids, "get_ids")) 355 goto cleanup; 356 357 opts.ids = ids; 358 opts.cnt = cnt; 359 360 skel->links.test_fentry = bpf_program__attach_tracing_multi(skel->progs.test_fentry, 361 NULL, &opts); 362 if (!ASSERT_OK_PTR(skel->links.test_fentry, "bpf_program__attach_tracing_multi")) 363 goto cleanup; 364 365 skel->links.test_fexit = bpf_program__attach_tracing_multi(skel->progs.test_fexit, 366 NULL, &opts); 367 if (!ASSERT_OK_PTR(skel->links.test_fexit, "bpf_program__attach_tracing_multi")) 368 goto cleanup; 369 370 ASSERT_OK(trigger_module_test_read(1), "trigger_read"); 371 ASSERT_EQ(skel->bss->test_result_fentry, 5, "test_result_fentry"); 372 ASSERT_EQ(skel->bss->test_result_fexit, 5, "test_result_fexit"); 373 374 cleanup: 375 tracing_multi_module__destroy(skel); 376 free(ids); 377 } 378 379 static bool is_set(__u32 mask, __u32 bit) 380 { 381 return (1 << bit) & mask; 382 } 383 384 static void __test_intersect(__u32 mask, const struct bpf_program *progs[4], __u64 *test_results[4]) 385 { 386 LIBBPF_OPTS(bpf_tracing_multi_opts, opts); 387 LIBBPF_OPTS(bpf_test_run_opts, topts); 388 struct bpf_link *links[4] = { NULL }; 389 const char *funcs[FUNCS_CNT]; 390 __u64 expected[4]; 391 __u32 *ids, i; 392 int err, cnt; 393 394 /* 395 * We have 4 programs in progs and the mask bits pick which 396 * of them gets attached to randomly chosen functions. 397 */ 398 for (i = 0; i < 4; i++) { 399 if (!is_set(mask, i)) 400 continue; 401 402 cnt = get_random_funcs(funcs); 403 ids = get_ids(funcs, cnt, NULL); 404 if (!ASSERT_OK_PTR(ids, "get_ids")) 405 goto cleanup; 406 407 opts.ids = ids; 408 opts.cnt = cnt; 409 links[i] = bpf_program__attach_tracing_multi(progs[i], NULL, &opts); 410 free(ids); 411 412 if (!ASSERT_OK_PTR(links[i], "bpf_program__attach_tracing_multi")) 413 goto cleanup; 414 415 expected[i] = *test_results[i] + cnt; 416 } 417 418 err = bpf_prog_test_run_opts(bpf_program__fd(progs[0]), &topts); 419 ASSERT_OK(err, "test_run"); 420 421 for (i = 0; i < 4; i++) { 422 if (!is_set(mask, i)) 423 continue; 424 ASSERT_EQ(*test_results[i], expected[i], "test_results"); 425 } 426 427 cleanup: 428 for (i = 0; i < 4; i++) 429 bpf_link__destroy(links[i]); 430 } 431 432 static void test_intersect(void) 433 { 434 struct tracing_multi_intersect *skel; 435 const struct bpf_program *progs[4]; 436 __u64 *test_results[4]; 437 __u32 i; 438 439 skel = tracing_multi_intersect__open_and_load(); 440 if (!ASSERT_OK_PTR(skel, "tracing_multi_intersect__open_and_load")) 441 return; 442 443 skel->bss->pid = getpid(); 444 445 progs[0] = skel->progs.fentry_1; 446 progs[1] = skel->progs.fexit_1; 447 progs[2] = skel->progs.fentry_2; 448 progs[3] = skel->progs.fexit_2; 449 450 test_results[0] = &skel->bss->test_result_fentry_1; 451 test_results[1] = &skel->bss->test_result_fexit_1; 452 test_results[2] = &skel->bss->test_result_fentry_2; 453 test_results[3] = &skel->bss->test_result_fexit_2; 454 455 for (i = 1; i < 16; i++) 456 __test_intersect(i, progs, test_results); 457 458 tracing_multi_intersect__destroy(skel); 459 } 460 461 static void test_session(void) 462 { 463 LIBBPF_OPTS(bpf_test_run_opts, topts); 464 struct tracing_multi_session *skel; 465 int err, prog_fd; 466 467 skel = tracing_multi_session__open_and_load(); 468 if (!ASSERT_OK_PTR(skel, "tracing_multi_session__open_and_load")) 469 return; 470 471 skel->bss->pid = getpid(); 472 473 err = tracing_multi_session__attach(skel); 474 if (!ASSERT_OK(err, "tracing_multi_session__attach")) 475 goto cleanup; 476 477 /* execute kernel session */ 478 prog_fd = bpf_program__fd(skel->progs.test_session_1); 479 err = bpf_prog_test_run_opts(prog_fd, &topts); 480 ASSERT_OK(err, "test_run"); 481 482 /* 10 for test_session_1, 1 for test_fsession_s */ 483 ASSERT_EQ(skel->bss->test_result_fentry, 11, "test_result_fentry"); 484 /* extra count (+1 for each fexit execution) for test_result_fexit cookie check/inc */ 485 ASSERT_EQ(skel->bss->test_result_fexit, 22, "test_result_fexit"); 486 487 skel->bss->test_result_fentry = 0; 488 skel->bss->test_result_fexit = 0; 489 490 /* execute bpf_testmo.ko session */ 491 ASSERT_OK(trigger_module_test_read(1), "trigger_read"); 492 493 /* 5 for test_session_2 */ 494 ASSERT_EQ(skel->bss->test_result_fentry, 5, "test_result_fentry"); 495 /* extra count (+1 for each fexit execution) for test_result_fexit cookie */ 496 ASSERT_EQ(skel->bss->test_result_fexit, 10, "test_result_fexit"); 497 498 499 cleanup: 500 tracing_multi_session__destroy(skel); 501 } 502 503 static void test_attach_api_fails(void) 504 { 505 LIBBPF_OPTS(bpf_tracing_multi_opts, opts); 506 static const char * const func[] = { 507 "bpf_fentry_test2", 508 }; 509 struct tracing_multi_fail *skel = NULL; 510 __u32 ids[2] = {}, *ids2 = NULL; 511 __u64 cookies[2]; 512 513 skel = tracing_multi_fail__open_and_load(); 514 if (!ASSERT_OK_PTR(skel, "tracing_multi_fail__open_and_load")) 515 return; 516 517 /* fail#1 (libbpf) pattern and opts NULL */ 518 skel->links.test_fentry = bpf_program__attach_tracing_multi(skel->progs.test_fentry, 519 NULL, NULL); 520 if (!ASSERT_EQ(libbpf_get_error(skel->links.test_fentry), -EINVAL, "fail_1")) 521 goto cleanup; 522 523 /* fail#2 (libbpf) pattern and ids */ 524 LIBBPF_OPTS_RESET(opts, 525 .ids = ids, 526 .cnt = 2, 527 ); 528 529 skel->links.test_fentry = bpf_program__attach_tracing_multi(skel->progs.test_fentry, 530 "bpf_fentry_test*", &opts); 531 if (!ASSERT_EQ(libbpf_get_error(skel->links.test_fentry), -EINVAL, "fail_2")) 532 goto cleanup; 533 534 /* fail#3 (libbpf) pattern and cookies */ 535 LIBBPF_OPTS_RESET(opts, 536 .ids = NULL, 537 .cnt = 2, 538 .cookies = cookies, 539 ); 540 541 skel->links.test_fentry = bpf_program__attach_tracing_multi(skel->progs.test_fentry, 542 "bpf_fentry_test*", &opts); 543 if (!ASSERT_EQ(libbpf_get_error(skel->links.test_fentry), -EINVAL, "fail_3")) 544 goto cleanup; 545 546 /* fail#4 (libbpf) bogus pattern */ 547 skel->links.test_fentry = bpf_program__attach_tracing_multi(skel->progs.test_fentry, 548 "bpf_not_really_a_function*", NULL); 549 if (!ASSERT_EQ(libbpf_get_error(skel->links.test_fentry), -EINVAL, "fail_4")) 550 goto cleanup; 551 552 /* fail#5 (kernel) abnormal cnt */ 553 LIBBPF_OPTS_RESET(opts, 554 .ids = ids, 555 .cnt = INT_MAX, 556 ); 557 558 skel->links.test_fentry = bpf_program__attach_tracing_multi(skel->progs.test_fentry, 559 NULL, &opts); 560 if (!ASSERT_EQ(libbpf_get_error(skel->links.test_fentry), -E2BIG, "fail_5")) 561 goto cleanup; 562 563 /* fail#6 (kernel) attach sleepable program to not-allowed function */ 564 ids2 = get_ids(func, 1, NULL); 565 if (!ASSERT_OK_PTR(ids2, "get_ids")) 566 goto cleanup; 567 568 LIBBPF_OPTS_RESET(opts, 569 .ids = ids2, 570 .cnt = 1, 571 ); 572 573 skel->links.test_fentry_s = bpf_program__attach_tracing_multi(skel->progs.test_fentry_s, 574 NULL, &opts); 575 if (!ASSERT_EQ(libbpf_get_error(skel->links.test_fentry_s), -EINVAL, "fail_6")) 576 goto cleanup; 577 578 /* fail#7 (kernel) attach with duplicate id */ 579 ids[0] = ids2[0]; 580 ids[1] = ids2[0]; 581 582 LIBBPF_OPTS_RESET(opts, 583 .ids = ids, 584 .cnt = 2, 585 ); 586 587 skel->links.test_fentry = bpf_program__attach_tracing_multi(skel->progs.test_fentry, 588 NULL, &opts); 589 ASSERT_EQ(libbpf_get_error(skel->links.test_fentry), -EINVAL, "fail_7"); 590 591 cleanup: 592 tracing_multi_fail__destroy(skel); 593 free(ids2); 594 } 595 596 void test_tracing_multi_test(void) 597 { 598 #ifndef __x86_64__ 599 test__skip(); 600 return; 601 #endif 602 603 if (test__start_subtest("skel_api")) 604 test_skel_api(); 605 if (test__start_subtest("link_api_pattern")) 606 test_link_api_pattern(); 607 if (test__start_subtest("link_api_ids")) 608 test_link_api_ids(false); 609 if (test__start_subtest("module_skel_api")) 610 test_module_skel_api(); 611 if (test__start_subtest("module_link_api_pattern")) 612 test_module_link_api_pattern(); 613 if (test__start_subtest("module_link_api_ids")) 614 test_module_link_api_ids(); 615 if (test__start_subtest("intersect")) 616 test_intersect(); 617 if (test__start_subtest("cookies")) 618 test_link_api_ids(true); 619 if (test__start_subtest("session")) 620 test_session(); 621 if (test__start_subtest("attach_api_fails")) 622 test_attach_api_fails(); 623 RUN_TESTS(tracing_multi_verifier); 624 } 625