1 // SPDX-License-Identifier: GPL-2.0 2 #include <test_progs.h> 3 #include <network_helpers.h> 4 5 struct meta { 6 int ifindex; 7 __u32 cb32_0; 8 __u8 cb8_0; 9 }; 10 11 static union { 12 __u32 cb32[5]; 13 __u8 cb8[20]; 14 } cb = { 15 .cb32[0] = 0x81828384, 16 }; 17 18 static void on_sample(void *ctx, int cpu, void *data, __u32 size) 19 { 20 struct meta *meta = (struct meta *)data; 21 struct ipv6_packet *pkt_v6 = data + sizeof(*meta); 22 int duration = 0; 23 24 if (CHECK(size != 72 + sizeof(*meta), "check_size", "size %u != %zu\n", 25 size, 72 + sizeof(*meta))) 26 return; 27 if (CHECK(meta->ifindex != 1, "check_meta_ifindex", 28 "meta->ifindex = %d\n", meta->ifindex)) 29 /* spurious kfree_skb not on loopback device */ 30 return; 31 if (CHECK(meta->cb8_0 != cb.cb8[0], "check_cb8_0", "cb8_0 %x != %x\n", 32 meta->cb8_0, cb.cb8[0])) 33 return; 34 if (CHECK(meta->cb32_0 != cb.cb32[0], "check_cb32_0", 35 "cb32_0 %x != %x\n", 36 meta->cb32_0, cb.cb32[0])) 37 return; 38 if (CHECK(pkt_v6->eth.h_proto != 0xdd86, "check_eth", 39 "h_proto %x\n", pkt_v6->eth.h_proto)) 40 return; 41 if (CHECK(pkt_v6->iph.nexthdr != 6, "check_ip", 42 "iph.nexthdr %x\n", pkt_v6->iph.nexthdr)) 43 return; 44 if (CHECK(pkt_v6->tcp.doff != 5, "check_tcp", 45 "tcp.doff %x\n", pkt_v6->tcp.doff)) 46 return; 47 48 *(bool *)ctx = true; 49 } 50 51 void test_kfree_skb(void) 52 { 53 struct __sk_buff skb = {}; 54 struct bpf_prog_test_run_attr tattr = { 55 .data_in = &pkt_v6, 56 .data_size_in = sizeof(pkt_v6), 57 .ctx_in = &skb, 58 .ctx_size_in = sizeof(skb), 59 }; 60 struct bpf_prog_load_attr attr = { 61 .file = "./kfree_skb.o", 62 }; 63 64 struct bpf_link *link = NULL, *link_fentry = NULL, *link_fexit = NULL; 65 struct bpf_map *perf_buf_map, *global_data; 66 struct bpf_program *prog, *fentry, *fexit; 67 struct bpf_object *obj, *obj2 = NULL; 68 struct perf_buffer_opts pb_opts = {}; 69 struct perf_buffer *pb = NULL; 70 int err, kfree_skb_fd; 71 bool passed = false; 72 __u32 duration = 0; 73 const int zero = 0; 74 bool test_ok[2]; 75 76 err = bpf_prog_load("./test_pkt_access.o", BPF_PROG_TYPE_SCHED_CLS, 77 &obj, &tattr.prog_fd); 78 if (CHECK(err, "prog_load sched cls", "err %d errno %d\n", err, errno)) 79 return; 80 81 err = bpf_prog_load_xattr(&attr, &obj2, &kfree_skb_fd); 82 if (CHECK(err, "prog_load raw tp", "err %d errno %d\n", err, errno)) 83 goto close_prog; 84 85 prog = bpf_object__find_program_by_title(obj2, "tp_btf/kfree_skb"); 86 if (CHECK(!prog, "find_prog", "prog kfree_skb not found\n")) 87 goto close_prog; 88 fentry = bpf_object__find_program_by_title(obj2, "fentry/eth_type_trans"); 89 if (CHECK(!fentry, "find_prog", "prog eth_type_trans not found\n")) 90 goto close_prog; 91 fexit = bpf_object__find_program_by_title(obj2, "fexit/eth_type_trans"); 92 if (CHECK(!fexit, "find_prog", "prog eth_type_trans not found\n")) 93 goto close_prog; 94 95 global_data = bpf_object__find_map_by_name(obj2, "kfree_sk.bss"); 96 if (CHECK(!global_data, "find global data", "not found\n")) 97 goto close_prog; 98 99 link = bpf_program__attach_raw_tracepoint(prog, NULL); 100 if (CHECK(IS_ERR(link), "attach_raw_tp", "err %ld\n", PTR_ERR(link))) 101 goto close_prog; 102 link_fentry = bpf_program__attach_trace(fentry); 103 if (CHECK(IS_ERR(link_fentry), "attach fentry", "err %ld\n", 104 PTR_ERR(link_fentry))) 105 goto close_prog; 106 link_fexit = bpf_program__attach_trace(fexit); 107 if (CHECK(IS_ERR(link_fexit), "attach fexit", "err %ld\n", 108 PTR_ERR(link_fexit))) 109 goto close_prog; 110 111 perf_buf_map = bpf_object__find_map_by_name(obj2, "perf_buf_map"); 112 if (CHECK(!perf_buf_map, "find_perf_buf_map", "not found\n")) 113 goto close_prog; 114 115 /* set up perf buffer */ 116 pb_opts.sample_cb = on_sample; 117 pb_opts.ctx = &passed; 118 pb = perf_buffer__new(bpf_map__fd(perf_buf_map), 1, &pb_opts); 119 if (CHECK(IS_ERR(pb), "perf_buf__new", "err %ld\n", PTR_ERR(pb))) 120 goto close_prog; 121 122 memcpy(skb.cb, &cb, sizeof(cb)); 123 err = bpf_prog_test_run_xattr(&tattr); 124 duration = tattr.duration; 125 CHECK(err || tattr.retval, "ipv6", 126 "err %d errno %d retval %d duration %d\n", 127 err, errno, tattr.retval, duration); 128 129 /* read perf buffer */ 130 err = perf_buffer__poll(pb, 100); 131 if (CHECK(err < 0, "perf_buffer__poll", "err %d\n", err)) 132 goto close_prog; 133 134 /* make sure kfree_skb program was triggered 135 * and it sent expected skb into ring buffer 136 */ 137 CHECK_FAIL(!passed); 138 139 err = bpf_map_lookup_elem(bpf_map__fd(global_data), &zero, test_ok); 140 if (CHECK(err, "get_result", 141 "failed to get output data: %d\n", err)) 142 goto close_prog; 143 144 CHECK_FAIL(!test_ok[0] || !test_ok[1]); 145 close_prog: 146 perf_buffer__free(pb); 147 if (!IS_ERR_OR_NULL(link)) 148 bpf_link__destroy(link); 149 if (!IS_ERR_OR_NULL(link_fentry)) 150 bpf_link__destroy(link_fentry); 151 if (!IS_ERR_OR_NULL(link_fexit)) 152 bpf_link__destroy(link_fexit); 153 bpf_object__close(obj); 154 bpf_object__close(obj2); 155 } 156