1 // SPDX-License-Identifier: GPL-2.0 2 #define _GNU_SOURCE 3 #include <test_progs.h> 4 #include <network_helpers.h> 5 #include "kfree_skb.skel.h" 6 7 struct meta { 8 int ifindex; 9 __u32 cb32_0; 10 __u8 cb8_0; 11 }; 12 13 static union { 14 __u32 cb32[5]; 15 __u8 cb8[20]; 16 } cb = { 17 .cb32[0] = 0x81828384, 18 }; 19 20 static void on_sample(void *ctx, int cpu, void *data, __u32 size) 21 { 22 struct meta *meta = (struct meta *)data; 23 struct ipv6_packet *pkt_v6 = data + sizeof(*meta); 24 int duration = 0; 25 26 if (CHECK(size != 72 + sizeof(*meta), "check_size", "size %u != %zu\n", 27 size, 72 + sizeof(*meta))) 28 return; 29 if (CHECK(meta->ifindex != 1, "check_meta_ifindex", 30 "meta->ifindex = %d\n", meta->ifindex)) 31 /* spurious kfree_skb not on loopback device */ 32 return; 33 if (CHECK(meta->cb8_0 != cb.cb8[0], "check_cb8_0", "cb8_0 %x != %x\n", 34 meta->cb8_0, cb.cb8[0])) 35 return; 36 if (CHECK(meta->cb32_0 != cb.cb32[0], "check_cb32_0", 37 "cb32_0 %x != %x\n", 38 meta->cb32_0, cb.cb32[0])) 39 return; 40 if (CHECK(pkt_v6->eth.h_proto != htons(ETH_P_IPV6), "check_eth", 41 "h_proto %x\n", pkt_v6->eth.h_proto)) 42 return; 43 if (CHECK(pkt_v6->iph.nexthdr != 6, "check_ip", 44 "iph.nexthdr %x\n", pkt_v6->iph.nexthdr)) 45 return; 46 if (CHECK(pkt_v6->tcp.doff != 5, "check_tcp", 47 "tcp.doff %x\n", pkt_v6->tcp.doff)) 48 return; 49 50 *(bool *)ctx = true; 51 } 52 53 /* TODO: fix kernel panic caused by this test in parallel mode */ 54 void serial_test_kfree_skb(void) 55 { 56 struct __sk_buff skb = {}; 57 LIBBPF_OPTS(bpf_test_run_opts, topts, 58 .data_in = &pkt_v6, 59 .data_size_in = sizeof(pkt_v6), 60 .ctx_in = &skb, 61 .ctx_size_in = sizeof(skb), 62 ); 63 struct kfree_skb *skel = NULL; 64 struct bpf_link *link; 65 struct bpf_object *obj; 66 struct perf_buffer *pb = NULL; 67 int err, prog_fd; 68 bool passed = false; 69 __u32 duration = 0; 70 const int zero = 0; 71 bool test_ok[2]; 72 73 err = bpf_prog_test_load("./test_pkt_access.bpf.o", BPF_PROG_TYPE_SCHED_CLS, 74 &obj, &prog_fd); 75 if (CHECK(err, "prog_load sched cls", "err %d errno %d\n", err, errno)) 76 return; 77 78 skel = kfree_skb__open_and_load(); 79 if (!ASSERT_OK_PTR(skel, "kfree_skb_skel")) 80 goto close_prog; 81 82 link = bpf_program__attach_raw_tracepoint(skel->progs.trace_kfree_skb, NULL); 83 if (!ASSERT_OK_PTR(link, "attach_raw_tp")) 84 goto close_prog; 85 skel->links.trace_kfree_skb = link; 86 87 link = bpf_program__attach_trace(skel->progs.fentry_eth_type_trans); 88 if (!ASSERT_OK_PTR(link, "attach fentry")) 89 goto close_prog; 90 skel->links.fentry_eth_type_trans = link; 91 92 link = bpf_program__attach_trace(skel->progs.fexit_eth_type_trans); 93 if (!ASSERT_OK_PTR(link, "attach fexit")) 94 goto close_prog; 95 skel->links.fexit_eth_type_trans = link; 96 97 /* set up perf buffer */ 98 pb = perf_buffer__new(bpf_map__fd(skel->maps.perf_buf_map), 1, 99 on_sample, NULL, &passed, NULL); 100 if (!ASSERT_OK_PTR(pb, "perf_buf__new")) 101 goto close_prog; 102 103 memcpy(skb.cb, &cb, sizeof(cb)); 104 err = bpf_prog_test_run_opts(prog_fd, &topts); 105 ASSERT_OK(err, "ipv6 test_run"); 106 ASSERT_OK(topts.retval, "ipv6 test_run retval"); 107 108 /* read perf buffer */ 109 err = perf_buffer__poll(pb, 100); 110 if (CHECK(err < 0, "perf_buffer__poll", "err %d\n", err)) 111 goto close_prog; 112 113 /* make sure kfree_skb program was triggered 114 * and it sent expected skb into ring buffer 115 */ 116 ASSERT_TRUE(passed, "passed"); 117 118 err = bpf_map_lookup_elem(bpf_map__fd(skel->maps.bss), &zero, test_ok); 119 if (CHECK(err, "get_result", 120 "failed to get output data: %d\n", err)) 121 goto close_prog; 122 123 CHECK_FAIL(!test_ok[0] || !test_ok[1]); 124 close_prog: 125 perf_buffer__free(pb); 126 bpf_object__close(obj); 127 kfree_skb__destroy(skel); 128 } 129