1 // SPDX-License-Identifier: GPL-2.0 2 #include <test_progs.h> 3 #include <network_helpers.h> 4 #include <net/if.h> 5 #include "test_xdp.skel.h" 6 #include "test_xdp_bpf2bpf.skel.h" 7 8 struct meta { 9 int ifindex; 10 int pkt_len; 11 }; 12 13 static void on_sample(void *ctx, int cpu, void *data, __u32 size) 14 { 15 int duration = 0; 16 struct meta *meta = (struct meta *)data; 17 struct ipv4_packet *trace_pkt_v4 = data + sizeof(*meta); 18 19 if (CHECK(size < sizeof(pkt_v4) + sizeof(*meta), 20 "check_size", "size %u < %zu\n", 21 size, sizeof(pkt_v4) + sizeof(*meta))) 22 return; 23 24 if (CHECK(meta->ifindex != if_nametoindex("lo"), "check_meta_ifindex", 25 "meta->ifindex = %d\n", meta->ifindex)) 26 return; 27 28 if (CHECK(meta->pkt_len != sizeof(pkt_v4), "check_meta_pkt_len", 29 "meta->pkt_len = %zd\n", sizeof(pkt_v4))) 30 return; 31 32 if (CHECK(memcmp(trace_pkt_v4, &pkt_v4, sizeof(pkt_v4)), 33 "check_packet_content", "content not the same\n")) 34 return; 35 36 *(bool *)ctx = true; 37 } 38 39 void test_xdp_bpf2bpf(void) 40 { 41 __u32 duration = 0, retval, size; 42 char buf[128]; 43 int err, pkt_fd, map_fd; 44 bool passed = false; 45 struct iphdr iph; 46 struct iptnl_info value4 = {.family = AF_INET}; 47 struct test_xdp *pkt_skel = NULL; 48 struct test_xdp_bpf2bpf *ftrace_skel = NULL; 49 struct vip key4 = {.protocol = 6, .family = AF_INET}; 50 struct bpf_program *prog; 51 struct perf_buffer *pb = NULL; 52 53 /* Load XDP program to introspect */ 54 pkt_skel = test_xdp__open_and_load(); 55 if (CHECK(!pkt_skel, "pkt_skel_load", "test_xdp skeleton failed\n")) 56 return; 57 58 pkt_fd = bpf_program__fd(pkt_skel->progs._xdp_tx_iptunnel); 59 60 map_fd = bpf_map__fd(pkt_skel->maps.vip2tnl); 61 bpf_map_update_elem(map_fd, &key4, &value4, 0); 62 63 /* Load trace program */ 64 ftrace_skel = test_xdp_bpf2bpf__open(); 65 if (CHECK(!ftrace_skel, "__open", "ftrace skeleton failed\n")) 66 goto out; 67 68 /* Demonstrate the bpf_program__set_attach_target() API rather than 69 * the load with options, i.e. opts.attach_prog_fd. 70 */ 71 prog = ftrace_skel->progs.trace_on_entry; 72 bpf_program__set_expected_attach_type(prog, BPF_TRACE_FENTRY); 73 bpf_program__set_attach_target(prog, pkt_fd, "_xdp_tx_iptunnel"); 74 75 prog = ftrace_skel->progs.trace_on_exit; 76 bpf_program__set_expected_attach_type(prog, BPF_TRACE_FEXIT); 77 bpf_program__set_attach_target(prog, pkt_fd, "_xdp_tx_iptunnel"); 78 79 err = test_xdp_bpf2bpf__load(ftrace_skel); 80 if (CHECK(err, "__load", "ftrace skeleton failed\n")) 81 goto out; 82 83 err = test_xdp_bpf2bpf__attach(ftrace_skel); 84 if (CHECK(err, "ftrace_attach", "ftrace attach failed: %d\n", err)) 85 goto out; 86 87 /* Set up perf buffer */ 88 pb = perf_buffer__new(bpf_map__fd(ftrace_skel->maps.perf_buf_map), 1, 89 on_sample, NULL, &passed, NULL); 90 if (!ASSERT_OK_PTR(pb, "perf_buf__new")) 91 goto out; 92 93 /* Run test program */ 94 err = bpf_prog_test_run(pkt_fd, 1, &pkt_v4, sizeof(pkt_v4), 95 buf, &size, &retval, &duration); 96 memcpy(&iph, buf + sizeof(struct ethhdr), sizeof(iph)); 97 if (CHECK(err || retval != XDP_TX || size != 74 || 98 iph.protocol != IPPROTO_IPIP, "ipv4", 99 "err %d errno %d retval %d size %d\n", 100 err, errno, retval, size)) 101 goto out; 102 103 /* Make sure bpf_xdp_output() was triggered and it sent the expected 104 * data to the perf ring buffer. 105 */ 106 err = perf_buffer__poll(pb, 100); 107 if (CHECK(err < 0, "perf_buffer__poll", "err %d\n", err)) 108 goto out; 109 110 CHECK_FAIL(!passed); 111 112 /* Verify test results */ 113 if (CHECK(ftrace_skel->bss->test_result_fentry != if_nametoindex("lo"), 114 "result", "fentry failed err %llu\n", 115 ftrace_skel->bss->test_result_fentry)) 116 goto out; 117 118 CHECK(ftrace_skel->bss->test_result_fexit != XDP_TX, "result", 119 "fexit failed err %llu\n", ftrace_skel->bss->test_result_fexit); 120 121 out: 122 if (pb) 123 perf_buffer__free(pb); 124 test_xdp__destroy(pkt_skel); 125 test_xdp_bpf2bpf__destroy(ftrace_skel); 126 } 127