1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2022 Facebook */ 3 4 #include <test_progs.h> 5 #include <network_helpers.h> 6 #include "dynptr_fail.skel.h" 7 #include "dynptr_success.skel.h" 8 9 enum test_setup_type { 10 SETUP_SYSCALL_SLEEP, 11 SETUP_SKB_PROG, 12 }; 13 14 static struct { 15 const char *prog_name; 16 enum test_setup_type type; 17 } success_tests[] = { 18 {"test_read_write", SETUP_SYSCALL_SLEEP}, 19 {"test_dynptr_data", SETUP_SYSCALL_SLEEP}, 20 {"test_ringbuf", SETUP_SYSCALL_SLEEP}, 21 {"test_skb_readonly", SETUP_SKB_PROG}, 22 {"test_dynptr_skb_data", SETUP_SKB_PROG}, 23 }; 24 25 static void verify_success(const char *prog_name, enum test_setup_type setup_type) 26 { 27 struct dynptr_success *skel; 28 struct bpf_program *prog; 29 struct bpf_link *link; 30 int err; 31 32 skel = dynptr_success__open(); 33 if (!ASSERT_OK_PTR(skel, "dynptr_success__open")) 34 return; 35 36 skel->bss->pid = getpid(); 37 38 prog = bpf_object__find_program_by_name(skel->obj, prog_name); 39 if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name")) 40 goto cleanup; 41 42 bpf_program__set_autoload(prog, true); 43 44 err = dynptr_success__load(skel); 45 if (!ASSERT_OK(err, "dynptr_success__load")) 46 goto cleanup; 47 48 switch (setup_type) { 49 case SETUP_SYSCALL_SLEEP: 50 link = bpf_program__attach(prog); 51 if (!ASSERT_OK_PTR(link, "bpf_program__attach")) 52 goto cleanup; 53 54 usleep(1); 55 56 bpf_link__destroy(link); 57 break; 58 case SETUP_SKB_PROG: 59 { 60 int prog_fd; 61 char buf[64]; 62 63 LIBBPF_OPTS(bpf_test_run_opts, topts, 64 .data_in = &pkt_v4, 65 .data_size_in = sizeof(pkt_v4), 66 .data_out = buf, 67 .data_size_out = sizeof(buf), 68 .repeat = 1, 69 ); 70 71 prog_fd = bpf_program__fd(prog); 72 if (!ASSERT_GE(prog_fd, 0, "prog_fd")) 73 goto cleanup; 74 75 err = bpf_prog_test_run_opts(prog_fd, &topts); 76 77 if (!ASSERT_OK(err, "test_run")) 78 goto cleanup; 79 80 break; 81 } 82 } 83 84 ASSERT_EQ(skel->bss->err, 0, "err"); 85 86 cleanup: 87 dynptr_success__destroy(skel); 88 } 89 90 void test_dynptr(void) 91 { 92 int i; 93 94 for (i = 0; i < ARRAY_SIZE(success_tests); i++) { 95 if (!test__start_subtest(success_tests[i].prog_name)) 96 continue; 97 98 verify_success(success_tests[i].prog_name, success_tests[i].type); 99 } 100 101 RUN_TESTS(dynptr_fail); 102 } 103