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 SETUP_SKB_PROG_TP, 13 }; 14 15 static struct { 16 const char *prog_name; 17 enum test_setup_type type; 18 } success_tests[] = { 19 {"test_read_write", SETUP_SYSCALL_SLEEP}, 20 {"test_dynptr_data", SETUP_SYSCALL_SLEEP}, 21 {"test_ringbuf", SETUP_SYSCALL_SLEEP}, 22 {"test_skb_readonly", SETUP_SKB_PROG}, 23 {"test_dynptr_skb_data", SETUP_SKB_PROG}, 24 {"test_adjust", SETUP_SYSCALL_SLEEP}, 25 {"test_adjust_err", SETUP_SYSCALL_SLEEP}, 26 {"test_zero_size_dynptr", SETUP_SYSCALL_SLEEP}, 27 {"test_dynptr_is_null", SETUP_SYSCALL_SLEEP}, 28 {"test_dynptr_is_rdonly", SETUP_SKB_PROG}, 29 {"test_dynptr_clone", SETUP_SKB_PROG}, 30 {"test_dynptr_skb_no_buff", SETUP_SKB_PROG}, 31 {"test_dynptr_skb_strcmp", SETUP_SKB_PROG}, 32 {"test_dynptr_skb_tp_btf", SETUP_SKB_PROG_TP}, 33 }; 34 35 static void verify_success(const char *prog_name, enum test_setup_type setup_type) 36 { 37 struct dynptr_success *skel; 38 struct bpf_program *prog; 39 struct bpf_link *link; 40 int err; 41 42 skel = dynptr_success__open(); 43 if (!ASSERT_OK_PTR(skel, "dynptr_success__open")) 44 return; 45 46 skel->bss->pid = getpid(); 47 48 prog = bpf_object__find_program_by_name(skel->obj, prog_name); 49 if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name")) 50 goto cleanup; 51 52 bpf_program__set_autoload(prog, true); 53 54 err = dynptr_success__load(skel); 55 if (!ASSERT_OK(err, "dynptr_success__load")) 56 goto cleanup; 57 58 switch (setup_type) { 59 case SETUP_SYSCALL_SLEEP: 60 link = bpf_program__attach(prog); 61 if (!ASSERT_OK_PTR(link, "bpf_program__attach")) 62 goto cleanup; 63 64 usleep(1); 65 66 bpf_link__destroy(link); 67 break; 68 case SETUP_SKB_PROG: 69 { 70 int prog_fd; 71 char buf[64]; 72 73 LIBBPF_OPTS(bpf_test_run_opts, topts, 74 .data_in = &pkt_v4, 75 .data_size_in = sizeof(pkt_v4), 76 .data_out = buf, 77 .data_size_out = sizeof(buf), 78 .repeat = 1, 79 ); 80 81 prog_fd = bpf_program__fd(prog); 82 if (!ASSERT_GE(prog_fd, 0, "prog_fd")) 83 goto cleanup; 84 85 err = bpf_prog_test_run_opts(prog_fd, &topts); 86 87 if (!ASSERT_OK(err, "test_run")) 88 goto cleanup; 89 90 break; 91 } 92 case SETUP_SKB_PROG_TP: 93 { 94 struct __sk_buff skb = {}; 95 struct bpf_object *obj; 96 int aux_prog_fd; 97 98 /* Just use its test_run to trigger kfree_skb tracepoint */ 99 err = bpf_prog_test_load("./test_pkt_access.bpf.o", BPF_PROG_TYPE_SCHED_CLS, 100 &obj, &aux_prog_fd); 101 if (!ASSERT_OK(err, "prog_load sched cls")) 102 goto cleanup; 103 104 LIBBPF_OPTS(bpf_test_run_opts, topts, 105 .data_in = &pkt_v4, 106 .data_size_in = sizeof(pkt_v4), 107 .ctx_in = &skb, 108 .ctx_size_in = sizeof(skb), 109 ); 110 111 link = bpf_program__attach(prog); 112 if (!ASSERT_OK_PTR(link, "bpf_program__attach")) 113 goto cleanup; 114 115 err = bpf_prog_test_run_opts(aux_prog_fd, &topts); 116 bpf_link__destroy(link); 117 118 if (!ASSERT_OK(err, "test_run")) 119 goto cleanup; 120 121 break; 122 } 123 } 124 125 ASSERT_EQ(skel->bss->err, 0, "err"); 126 127 cleanup: 128 dynptr_success__destroy(skel); 129 } 130 131 void test_dynptr(void) 132 { 133 int i; 134 135 for (i = 0; i < ARRAY_SIZE(success_tests); i++) { 136 if (!test__start_subtest(success_tests[i].prog_name)) 137 continue; 138 139 verify_success(success_tests[i].prog_name, success_tests[i].type); 140 } 141 142 RUN_TESTS(dynptr_fail); 143 } 144