^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <libelf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <gelf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/filter.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/perf_event.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/netlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <sys/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <sys/syscall.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <sys/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <sys/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <assert.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <bpf/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include "bpf_load.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include "perf-sys.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define DEBUGFS "/sys/kernel/debug/tracing/"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static char license[128];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static int kern_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static bool processed_sec[128];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) char bpf_log_buf[BPF_LOG_BUF_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) int map_fd[MAX_MAPS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) int prog_fd[MAX_PROGS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) int event_fd[MAX_PROGS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) int prog_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int prog_array_fd = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct bpf_map_data map_data[MAX_MAPS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int map_data_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static int populate_prog_array(const char *event, int prog_fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) int ind = atoi(event), err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) err = bpf_map_update_elem(prog_array_fd, &ind, &prog_fd, BPF_ANY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) printf("failed to store prog_fd in prog_array\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static int write_kprobe_events(const char *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) int fd, ret, flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (val == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) else if (val[0] == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) flags = O_WRONLY | O_TRUNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) flags = O_WRONLY | O_APPEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) fd = open(DEBUGFS "kprobe_events", flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) ret = write(fd, val, strlen(val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static int load_and_attach(const char *event, struct bpf_insn *prog, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) bool is_socket = strncmp(event, "socket", 6) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) bool is_kprobe = strncmp(event, "kprobe/", 7) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) bool is_kretprobe = strncmp(event, "kretprobe/", 10) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) bool is_tracepoint = strncmp(event, "tracepoint/", 11) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) bool is_raw_tracepoint = strncmp(event, "raw_tracepoint/", 15) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) bool is_xdp = strncmp(event, "xdp", 3) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) bool is_perf_event = strncmp(event, "perf_event", 10) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) bool is_cgroup_skb = strncmp(event, "cgroup/skb", 10) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) bool is_cgroup_sk = strncmp(event, "cgroup/sock", 11) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) bool is_sockops = strncmp(event, "sockops", 7) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) bool is_sk_skb = strncmp(event, "sk_skb", 6) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) bool is_sk_msg = strncmp(event, "sk_msg", 6) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) size_t insns_cnt = size / sizeof(struct bpf_insn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) enum bpf_prog_type prog_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) char buf[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) int fd, efd, err, id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct perf_event_attr attr = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) attr.type = PERF_TYPE_TRACEPOINT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) attr.sample_type = PERF_SAMPLE_RAW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) attr.sample_period = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) attr.wakeup_events = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (is_socket) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) } else if (is_kprobe || is_kretprobe) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) prog_type = BPF_PROG_TYPE_KPROBE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) } else if (is_tracepoint) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) prog_type = BPF_PROG_TYPE_TRACEPOINT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) } else if (is_raw_tracepoint) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) prog_type = BPF_PROG_TYPE_RAW_TRACEPOINT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) } else if (is_xdp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) prog_type = BPF_PROG_TYPE_XDP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) } else if (is_perf_event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) prog_type = BPF_PROG_TYPE_PERF_EVENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) } else if (is_cgroup_skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) prog_type = BPF_PROG_TYPE_CGROUP_SKB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) } else if (is_cgroup_sk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) prog_type = BPF_PROG_TYPE_CGROUP_SOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) } else if (is_sockops) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) prog_type = BPF_PROG_TYPE_SOCK_OPS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) } else if (is_sk_skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) prog_type = BPF_PROG_TYPE_SK_SKB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) } else if (is_sk_msg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) prog_type = BPF_PROG_TYPE_SK_MSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) printf("Unknown event '%s'\n", event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (prog_cnt == MAX_PROGS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) fd = bpf_load_program(prog_type, prog, insns_cnt, license, kern_version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) bpf_log_buf, BPF_LOG_BUF_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) printf("bpf_load_program() err=%d\n%s", errno, bpf_log_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) prog_fd[prog_cnt++] = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (is_xdp || is_perf_event || is_cgroup_skb || is_cgroup_sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (is_socket || is_sockops || is_sk_skb || is_sk_msg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (is_socket)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) event += 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) event += 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (*event != '/')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) event++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (!isdigit(*event)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) printf("invalid prog number\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) return populate_prog_array(event, fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (is_raw_tracepoint) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) efd = bpf_raw_tracepoint_open(event + 15, fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (efd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) printf("tracepoint %s %s\n", event + 15, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) event_fd[prog_cnt - 1] = efd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (is_kprobe || is_kretprobe) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) bool need_normal_check = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) const char *event_prefix = "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (is_kprobe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) event += 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) event += 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (*event == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) printf("event name cannot be empty\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (isdigit(*event))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return populate_prog_array(event, fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) #ifdef __x86_64__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (strncmp(event, "sys_", 4) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) snprintf(buf, sizeof(buf), "%c:__x64_%s __x64_%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) is_kprobe ? 'p' : 'r', event, event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) err = write_kprobe_events(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (err >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) need_normal_check = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) event_prefix = "__x64_";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (need_normal_check) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) snprintf(buf, sizeof(buf), "%c:%s %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) is_kprobe ? 'p' : 'r', event, event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) err = write_kprobe_events(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) printf("failed to create kprobe '%s' error '%s'\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) event, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) strcpy(buf, DEBUGFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) strcat(buf, "events/kprobes/");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) strcat(buf, event_prefix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) strcat(buf, event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) strcat(buf, "/id");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) } else if (is_tracepoint) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) event += 11;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (*event == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) printf("event name cannot be empty\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) strcpy(buf, DEBUGFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) strcat(buf, "events/");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) strcat(buf, event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) strcat(buf, "/id");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) efd = open(buf, O_RDONLY, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (efd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) printf("failed to open event %s\n", event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) err = read(efd, buf, sizeof(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (err < 0 || err >= sizeof(buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) printf("read from '%s' failed '%s'\n", event, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) close(efd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) buf[err] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) id = atoi(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) attr.config = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) efd = sys_perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (efd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) printf("event %d fd %d err %s\n", id, efd, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) event_fd[prog_cnt - 1] = efd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) err = ioctl(efd, PERF_EVENT_IOC_ENABLE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) printf("ioctl PERF_EVENT_IOC_ENABLE failed err %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) err = ioctl(efd, PERF_EVENT_IOC_SET_BPF, fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) printf("ioctl PERF_EVENT_IOC_SET_BPF failed err %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) static int load_maps(struct bpf_map_data *maps, int nr_maps,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) fixup_map_cb fixup_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) int i, numa_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) for (i = 0; i < nr_maps; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (fixup_map) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) fixup_map(&maps[i], i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) /* Allow userspace to assign map FD prior to creation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (maps[i].fd != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) map_fd[i] = maps[i].fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) numa_node = maps[i].def.map_flags & BPF_F_NUMA_NODE ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) maps[i].def.numa_node : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (maps[i].def.type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) maps[i].def.type == BPF_MAP_TYPE_HASH_OF_MAPS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) int inner_map_fd = map_fd[maps[i].def.inner_map_idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) map_fd[i] = bpf_create_map_in_map_node(maps[i].def.type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) maps[i].name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) maps[i].def.key_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) inner_map_fd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) maps[i].def.max_entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) maps[i].def.map_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) numa_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) map_fd[i] = bpf_create_map_node(maps[i].def.type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) maps[i].name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) maps[i].def.key_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) maps[i].def.value_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) maps[i].def.max_entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) maps[i].def.map_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) numa_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (map_fd[i] < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) printf("failed to create map %d (%s): %d %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) i, maps[i].name, errno, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) maps[i].fd = map_fd[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if (maps[i].def.type == BPF_MAP_TYPE_PROG_ARRAY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) prog_array_fd = map_fd[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) static int get_sec(Elf *elf, int i, GElf_Ehdr *ehdr, char **shname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) GElf_Shdr *shdr, Elf_Data **data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) Elf_Scn *scn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) scn = elf_getscn(elf, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (!scn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) if (gelf_getshdr(scn, shdr) != shdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) return 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) *shname = elf_strptr(elf, ehdr->e_shstrndx, shdr->sh_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) if (!*shname || !shdr->sh_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) return 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) *data = elf_getdata(scn, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (!*data || elf_getdata(scn, *data) != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) static int parse_relo_and_apply(Elf_Data *data, Elf_Data *symbols,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) GElf_Shdr *shdr, struct bpf_insn *insn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) struct bpf_map_data *maps, int nr_maps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) int i, nrels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) nrels = shdr->sh_size / shdr->sh_entsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) for (i = 0; i < nrels; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) GElf_Sym sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) GElf_Rel rel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) unsigned int insn_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) bool match = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) int j, map_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) gelf_getrel(data, i, &rel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) insn_idx = rel.r_offset / sizeof(struct bpf_insn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) if (insn[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) printf("invalid relo for insn[%d].code 0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) insn_idx, insn[insn_idx].code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) insn[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) /* Match FD relocation against recorded map_data[] offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) for (map_idx = 0; map_idx < nr_maps; map_idx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) if (maps[map_idx].elf_offset == sym.st_value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) match = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) if (match) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) insn[insn_idx].imm = maps[map_idx].fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) printf("invalid relo for insn[%d] no map_data match\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) insn_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) static int cmp_symbols(const void *l, const void *r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) const GElf_Sym *lsym = (const GElf_Sym *)l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) const GElf_Sym *rsym = (const GElf_Sym *)r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) if (lsym->st_value < rsym->st_value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) else if (lsym->st_value > rsym->st_value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) static int load_elf_maps_section(struct bpf_map_data *maps, int maps_shndx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) Elf *elf, Elf_Data *symbols, int strtabidx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) int map_sz_elf, map_sz_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) bool validate_zero = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) Elf_Data *data_maps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) int i, nr_maps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) GElf_Sym *sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) Elf_Scn *scn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) int copy_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) if (maps_shndx < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) if (!symbols)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) /* Get data for maps section via elf index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) scn = elf_getscn(elf, maps_shndx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) if (scn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) data_maps = elf_getdata(scn, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) if (!scn || !data_maps) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) printf("Failed to get Elf_Data from maps section %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) maps_shndx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) /* For each map get corrosponding symbol table entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) sym = calloc(MAX_MAPS+1, sizeof(GElf_Sym));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) for (i = 0, nr_maps = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) assert(nr_maps < MAX_MAPS+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) if (!gelf_getsym(symbols, i, &sym[nr_maps]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) if (sym[nr_maps].st_shndx != maps_shndx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) /* Only increment iif maps section */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) nr_maps++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) /* Align to map_fd[] order, via sort on offset in sym.st_value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) qsort(sym, nr_maps, sizeof(GElf_Sym), cmp_symbols);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) /* Keeping compatible with ELF maps section changes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) * ------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) * The program size of struct bpf_load_map_def is known by loader
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) * code, but struct stored in ELF file can be different.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) * Unfortunately sym[i].st_size is zero. To calculate the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) * struct size stored in the ELF file, assume all struct have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) * the same size, and simply divide with number of map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) * symbols.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) map_sz_elf = data_maps->d_size / nr_maps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) map_sz_copy = sizeof(struct bpf_load_map_def);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) if (map_sz_elf < map_sz_copy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) * Backward compat, loading older ELF file with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) * smaller struct, keeping remaining bytes zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) map_sz_copy = map_sz_elf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) } else if (map_sz_elf > map_sz_copy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) * Forward compat, loading newer ELF file with larger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) * struct with unknown features. Assume zero means
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) * feature not used. Thus, validate rest of struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) * data is zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) validate_zero = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) /* Memcpy relevant part of ELF maps data to loader maps */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) for (i = 0; i < nr_maps; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) struct bpf_load_map_def *def;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) unsigned char *addr, *end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) const char *map_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) size_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) map_name = elf_strptr(elf, strtabidx, sym[i].st_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) maps[i].name = strdup(map_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) if (!maps[i].name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) printf("strdup(%s): %s(%d)\n", map_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) strerror(errno), errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) free(sym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) /* Symbol value is offset into ELF maps section data area */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) offset = sym[i].st_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) def = (struct bpf_load_map_def *)(data_maps->d_buf + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) maps[i].elf_offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) memset(&maps[i].def, 0, sizeof(struct bpf_load_map_def));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) memcpy(&maps[i].def, def, map_sz_copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) /* Verify no newer features were requested */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) if (validate_zero) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) addr = (unsigned char *) def + map_sz_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) end = (unsigned char *) def + map_sz_elf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) for (; addr < end; addr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) if (*addr != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) free(sym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) return -EFBIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) free(sym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) return nr_maps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) static int do_load_bpf_file(const char *path, fixup_map_cb fixup_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) int fd, i, ret, maps_shndx = -1, strtabidx = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) Elf *elf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) GElf_Ehdr ehdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) GElf_Shdr shdr, shdr_prog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) Elf_Data *data, *data_prog, *data_maps = NULL, *symbols = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) char *shname, *shname_prog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) int nr_maps = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) /* reset global variables */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) kern_version = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) memset(license, 0, sizeof(license));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) memset(processed_sec, 0, sizeof(processed_sec));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) if (elf_version(EV_CURRENT) == EV_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) fd = open(path, O_RDONLY, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) elf = elf_begin(fd, ELF_C_READ, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) if (!elf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) if (gelf_getehdr(elf, &ehdr) != &ehdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) /* clear all kprobes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) i = write_kprobe_events("");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) /* scan over all elf sections to get license and map info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) for (i = 1; i < ehdr.e_shnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) if (0) /* helpful for llvm debugging */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) printf("section %d:%s data %p size %zd link %d flags %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) i, shname, data->d_buf, data->d_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) shdr.sh_link, (int) shdr.sh_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) if (strcmp(shname, "license") == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) processed_sec[i] = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) memcpy(license, data->d_buf, data->d_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) } else if (strcmp(shname, "version") == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) processed_sec[i] = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) if (data->d_size != sizeof(int)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) printf("invalid size of version section %zd\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) data->d_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) memcpy(&kern_version, data->d_buf, sizeof(int));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) } else if (strcmp(shname, "maps") == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) int j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) maps_shndx = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) data_maps = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) for (j = 0; j < MAX_MAPS; j++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) map_data[j].fd = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) } else if (shdr.sh_type == SHT_SYMTAB) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) strtabidx = shdr.sh_link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) symbols = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) if (!symbols) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) printf("missing SHT_SYMTAB section\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) if (data_maps) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) nr_maps = load_elf_maps_section(map_data, maps_shndx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) elf, symbols, strtabidx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) if (nr_maps < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) printf("Error: Failed loading ELF maps (errno:%d):%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) nr_maps, strerror(-nr_maps));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) if (load_maps(map_data, nr_maps, fixup_map))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) map_data_count = nr_maps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) processed_sec[maps_shndx] = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) /* process all relo sections, and rewrite bpf insns for maps */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) for (i = 1; i < ehdr.e_shnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) if (processed_sec[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) if (shdr.sh_type == SHT_REL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) struct bpf_insn *insns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) /* locate prog sec that need map fixup (relocations) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) if (get_sec(elf, shdr.sh_info, &ehdr, &shname_prog,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) &shdr_prog, &data_prog))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) if (shdr_prog.sh_type != SHT_PROGBITS ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) !(shdr_prog.sh_flags & SHF_EXECINSTR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) insns = (struct bpf_insn *) data_prog->d_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) processed_sec[i] = true; /* relo section */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) if (parse_relo_and_apply(data, symbols, &shdr, insns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) map_data, nr_maps))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) /* load programs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) for (i = 1; i < ehdr.e_shnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) if (processed_sec[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) if (memcmp(shname, "kprobe/", 7) == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) memcmp(shname, "kretprobe/", 10) == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) memcmp(shname, "tracepoint/", 11) == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) memcmp(shname, "raw_tracepoint/", 15) == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) memcmp(shname, "xdp", 3) == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) memcmp(shname, "perf_event", 10) == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) memcmp(shname, "socket", 6) == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) memcmp(shname, "cgroup/", 7) == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) memcmp(shname, "sockops", 7) == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) memcmp(shname, "sk_skb", 6) == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) memcmp(shname, "sk_msg", 6) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) ret = load_and_attach(shname, data->d_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) data->d_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) int load_bpf_file(char *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) return do_load_bpf_file(path, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) int load_bpf_file_fixup_map(const char *path, fixup_map_cb fixup_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) return do_load_bpf_file(path, fixup_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) }