Alexei Starovoitov | 1bc38b8 | 2018-10-05 16:40:00 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) |
Eric Leblond | 6061a3d | 2018-01-30 21:55:03 +0100 | [diff] [blame] | 2 | |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 3 | /* |
| 4 | * Common eBPF ELF object loading operations. |
| 5 | * |
| 6 | * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org> |
| 7 | * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com> |
| 8 | * Copyright (C) 2015 Huawei Inc. |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 9 | * Copyright (C) 2017 Nicira, Inc. |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 10 | * Copyright (C) 2019 Isovalent, Inc. |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
Yonghong Song | b426995 | 2018-11-29 15:31:45 -0800 | [diff] [blame] | 13 | #ifndef _GNU_SOURCE |
Jakub Kicinski | 531b014 | 2018-07-10 14:43:05 -0700 | [diff] [blame] | 14 | #define _GNU_SOURCE |
Yonghong Song | b426995 | 2018-11-29 15:31:45 -0800 | [diff] [blame] | 15 | #endif |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 16 | #include <stdlib.h> |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame] | 17 | #include <stdio.h> |
| 18 | #include <stdarg.h> |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 19 | #include <libgen.h> |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 20 | #include <inttypes.h> |
Andrii Nakryiko | 8ab9da5 | 2019-12-23 10:03:05 -0800 | [diff] [blame] | 21 | #include <limits.h> |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame] | 22 | #include <string.h> |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 23 | #include <unistd.h> |
Arnaldo Carvalho de Melo | cdb2f92 | 2019-07-19 11:34:06 -0300 | [diff] [blame] | 24 | #include <endian.h> |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 25 | #include <fcntl.h> |
| 26 | #include <errno.h> |
Toke Høiland-Jørgensen | 113e6b7 | 2020-02-17 18:17:01 +0100 | [diff] [blame] | 27 | #include <ctype.h> |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 28 | #include <asm/unistd.h> |
Joe Stringer | e28ff1a | 2017-01-22 17:11:25 -0800 | [diff] [blame] | 29 | #include <linux/err.h> |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 30 | #include <linux/kernel.h> |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 31 | #include <linux/bpf.h> |
Martin KaFai Lau | 38d5d3b | 2018-07-24 08:40:22 -0700 | [diff] [blame] | 32 | #include <linux/btf.h> |
Stanislav Fomichev | 47eff61 | 2018-11-20 17:11:19 -0800 | [diff] [blame] | 33 | #include <linux/filter.h> |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 34 | #include <linux/list.h> |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 35 | #include <linux/limits.h> |
Yonghong Song | 438363c | 2018-10-09 16:14:47 -0700 | [diff] [blame] | 36 | #include <linux/perf_event.h> |
Daniel Borkmann | a64af0e | 2018-10-19 15:51:03 +0200 | [diff] [blame] | 37 | #include <linux/ring_buffer.h> |
Andrii Nakryiko | 5e61f27 | 2019-10-04 15:40:34 -0700 | [diff] [blame] | 38 | #include <linux/version.h> |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 39 | #include <sys/epoll.h> |
Andrii Nakryiko | 63f2f5e | 2019-07-01 16:58:57 -0700 | [diff] [blame] | 40 | #include <sys/ioctl.h> |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 41 | #include <sys/mman.h> |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 42 | #include <sys/stat.h> |
| 43 | #include <sys/types.h> |
| 44 | #include <sys/vfs.h> |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 45 | #include <sys/utsname.h> |
Toke Høiland-Jørgensen | dc3a2d2 | 2019-12-16 19:12:04 +0100 | [diff] [blame] | 46 | #include <sys/resource.h> |
Jakub Kicinski | 531b014 | 2018-07-10 14:43:05 -0700 | [diff] [blame] | 47 | #include <tools/libc_compat.h> |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 48 | #include <libelf.h> |
| 49 | #include <gelf.h> |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 50 | #include <zlib.h> |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 51 | |
| 52 | #include "libbpf.h" |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 53 | #include "bpf.h" |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 54 | #include "btf.h" |
Arnaldo Carvalho de Melo | 6d41907 | 2018-09-14 16:47:14 -0300 | [diff] [blame] | 55 | #include "str_error.h" |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 56 | #include "libbpf_internal.h" |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 57 | #include "hashmap.h" |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame] | 58 | |
Andrii Nakryiko | 1d1a3bc | 2020-01-10 10:19:16 -0800 | [diff] [blame] | 59 | /* make sure libbpf doesn't use kernel-only integer typedefs */ |
| 60 | #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64 |
| 61 | |
Wang Nan | 9b16137 | 2016-07-18 06:01:08 +0000 | [diff] [blame] | 62 | #ifndef EM_BPF |
| 63 | #define EM_BPF 247 |
| 64 | #endif |
| 65 | |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 66 | #ifndef BPF_FS_MAGIC |
| 67 | #define BPF_FS_MAGIC 0xcafe4a11 |
| 68 | #endif |
| 69 | |
Andrey Ignatov | ff466b5 | 2019-04-06 22:37:34 -0700 | [diff] [blame] | 70 | /* vsprintf() in __base_pr() uses nonliteral format string. It may break |
| 71 | * compilation if user enables corresponding warning. Disable it explicitly. |
| 72 | */ |
| 73 | #pragma GCC diagnostic ignored "-Wformat-nonliteral" |
| 74 | |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame] | 75 | #define __printf(a, b) __attribute__((format(printf, a, b))) |
| 76 | |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 77 | static struct bpf_map *bpf_object__add_map(struct bpf_object *obj); |
| 78 | static struct bpf_program *bpf_object__find_prog_by_idx(struct bpf_object *obj, |
| 79 | int idx); |
| 80 | static const struct btf_type * |
| 81 | skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id); |
| 82 | |
Stanislav Fomichev | a8a1f7d | 2019-02-04 16:20:55 -0800 | [diff] [blame] | 83 | static int __base_pr(enum libbpf_print_level level, const char *format, |
| 84 | va_list args) |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame] | 85 | { |
Yonghong Song | 6f1ae8b | 2019-02-01 16:14:17 -0800 | [diff] [blame] | 86 | if (level == LIBBPF_DEBUG) |
| 87 | return 0; |
| 88 | |
Stanislav Fomichev | a8a1f7d | 2019-02-04 16:20:55 -0800 | [diff] [blame] | 89 | return vfprintf(stderr, format, args); |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Stanislav Fomichev | a8a1f7d | 2019-02-04 16:20:55 -0800 | [diff] [blame] | 92 | static libbpf_print_fn_t __libbpf_pr = __base_pr; |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame] | 93 | |
Andrii Nakryiko | e87fd8b | 2019-07-27 20:25:26 -0700 | [diff] [blame] | 94 | libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn) |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame] | 95 | { |
Andrii Nakryiko | e87fd8b | 2019-07-27 20:25:26 -0700 | [diff] [blame] | 96 | libbpf_print_fn_t old_print_fn = __libbpf_pr; |
| 97 | |
Yonghong Song | 6f1ae8b | 2019-02-01 16:14:17 -0800 | [diff] [blame] | 98 | __libbpf_pr = fn; |
Andrii Nakryiko | e87fd8b | 2019-07-27 20:25:26 -0700 | [diff] [blame] | 99 | return old_print_fn; |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame] | 100 | } |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 101 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 102 | __printf(2, 3) |
| 103 | void libbpf_print(enum libbpf_print_level level, const char *format, ...) |
| 104 | { |
| 105 | va_list args; |
| 106 | |
Yonghong Song | 6f1ae8b | 2019-02-01 16:14:17 -0800 | [diff] [blame] | 107 | if (!__libbpf_pr) |
| 108 | return; |
| 109 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 110 | va_start(args, format); |
Yonghong Song | 6f1ae8b | 2019-02-01 16:14:17 -0800 | [diff] [blame] | 111 | __libbpf_pr(level, format, args); |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 112 | va_end(args); |
| 113 | } |
| 114 | |
Toke Høiland-Jørgensen | dc3a2d2 | 2019-12-16 19:12:04 +0100 | [diff] [blame] | 115 | static void pr_perm_msg(int err) |
| 116 | { |
| 117 | struct rlimit limit; |
| 118 | char buf[100]; |
| 119 | |
| 120 | if (err != -EPERM || geteuid() != 0) |
| 121 | return; |
| 122 | |
| 123 | err = getrlimit(RLIMIT_MEMLOCK, &limit); |
| 124 | if (err) |
| 125 | return; |
| 126 | |
| 127 | if (limit.rlim_cur == RLIM_INFINITY) |
| 128 | return; |
| 129 | |
| 130 | if (limit.rlim_cur < 1024) |
Toke Høiland-Jørgensen | b5c7d0d | 2019-12-19 10:02:36 +0100 | [diff] [blame] | 131 | snprintf(buf, sizeof(buf), "%zu bytes", (size_t)limit.rlim_cur); |
Toke Høiland-Jørgensen | dc3a2d2 | 2019-12-16 19:12:04 +0100 | [diff] [blame] | 132 | else if (limit.rlim_cur < 1024*1024) |
| 133 | snprintf(buf, sizeof(buf), "%.1f KiB", (double)limit.rlim_cur / 1024); |
| 134 | else |
| 135 | snprintf(buf, sizeof(buf), "%.1f MiB", (double)limit.rlim_cur / (1024*1024)); |
| 136 | |
| 137 | pr_warn("permission error while running as root; try raising 'ulimit -l'? current value: %s\n", |
| 138 | buf); |
| 139 | } |
| 140 | |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 141 | #define STRERR_BUFSIZE 128 |
| 142 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 143 | /* Copied from tools/perf/util/util.h */ |
| 144 | #ifndef zfree |
| 145 | # define zfree(ptr) ({ free(*ptr); *ptr = NULL; }) |
| 146 | #endif |
| 147 | |
| 148 | #ifndef zclose |
| 149 | # define zclose(fd) ({ \ |
| 150 | int ___err = 0; \ |
| 151 | if ((fd) >= 0) \ |
| 152 | ___err = close((fd)); \ |
| 153 | fd = -1; \ |
| 154 | ___err; }) |
| 155 | #endif |
| 156 | |
| 157 | #ifdef HAVE_LIBELF_MMAP_SUPPORT |
| 158 | # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP |
| 159 | #else |
| 160 | # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ |
| 161 | #endif |
| 162 | |
Song Liu | 34be1646 | 2019-03-11 22:30:38 -0700 | [diff] [blame] | 163 | static inline __u64 ptr_to_u64(const void *ptr) |
| 164 | { |
| 165 | return (__u64) (unsigned long) ptr; |
| 166 | } |
| 167 | |
Stanislav Fomichev | 47eff61 | 2018-11-20 17:11:19 -0800 | [diff] [blame] | 168 | struct bpf_capabilities { |
| 169 | /* v4.14: kernel support for program & map names. */ |
| 170 | __u32 name:1; |
Daniel Borkmann | 8837fe5 | 2019-04-24 00:45:56 +0200 | [diff] [blame] | 171 | /* v5.2: kernel support for global data sections. */ |
| 172 | __u32 global_data:1; |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 173 | /* BTF_KIND_FUNC and BTF_KIND_FUNC_PROTO support */ |
| 174 | __u32 btf_func:1; |
| 175 | /* BTF_KIND_VAR and BTF_KIND_DATASEC support */ |
| 176 | __u32 btf_datasec:1; |
Andrii Nakryiko | 7fe74b4 | 2019-11-17 09:28:05 -0800 | [diff] [blame] | 177 | /* BPF_F_MMAPABLE is supported for arrays */ |
| 178 | __u32 array_mmap:1; |
Alexei Starovoitov | 2d3eb67 | 2020-01-09 22:41:19 -0800 | [diff] [blame] | 179 | /* BTF_FUNC_GLOBAL is supported */ |
| 180 | __u32 btf_func_global:1; |
Stanislav Fomichev | 47eff61 | 2018-11-20 17:11:19 -0800 | [diff] [blame] | 181 | }; |
| 182 | |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 183 | enum reloc_type { |
| 184 | RELO_LD64, |
| 185 | RELO_CALL, |
| 186 | RELO_DATA, |
| 187 | RELO_EXTERN, |
| 188 | }; |
| 189 | |
| 190 | struct reloc_desc { |
| 191 | enum reloc_type type; |
| 192 | int insn_idx; |
| 193 | int map_idx; |
| 194 | int sym_off; |
| 195 | }; |
| 196 | |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 197 | /* |
| 198 | * bpf_prog should be a better name but it has been used in |
| 199 | * linux/filter.h. |
| 200 | */ |
| 201 | struct bpf_program { |
| 202 | /* Index in elf obj file, for relocation use. */ |
| 203 | int idx; |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 204 | char *name; |
David Beckett | f0307a7 | 2018-05-16 14:02:49 -0700 | [diff] [blame] | 205 | int prog_ifindex; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 206 | char *section_name; |
Stanislav Fomichev | 33a2c75 | 2018-11-09 08:21:43 -0800 | [diff] [blame] | 207 | /* section_name with / replaced by _; makes recursive pinning |
| 208 | * in bpf_object__pin_programs easier |
| 209 | */ |
| 210 | char *pin_name; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 211 | struct bpf_insn *insns; |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 212 | size_t insns_cnt, main_prog_cnt; |
Wang Nan | 5f44e4c8 | 2016-07-13 10:44:01 +0000 | [diff] [blame] | 213 | enum bpf_prog_type type; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 214 | |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 215 | struct reloc_desc *reloc_desc; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 216 | int nr_reloc; |
Alexei Starovoitov | da11b41 | 2019-04-01 21:27:47 -0700 | [diff] [blame] | 217 | int log_level; |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 218 | |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 219 | struct { |
| 220 | int nr; |
| 221 | int *fds; |
| 222 | } instances; |
| 223 | bpf_program_prep_t preprocessor; |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 224 | |
| 225 | struct bpf_object *obj; |
| 226 | void *priv; |
| 227 | bpf_program_clear_priv_t clear_priv; |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 228 | |
| 229 | enum bpf_attach_type expected_attach_type; |
Alexei Starovoitov | 12a8654 | 2019-10-30 15:32:12 -0700 | [diff] [blame] | 230 | __u32 attach_btf_id; |
Alexei Starovoitov | e7bf94d | 2019-11-14 10:57:18 -0800 | [diff] [blame] | 231 | __u32 attach_prog_fd; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 232 | void *func_info; |
| 233 | __u32 func_info_rec_size; |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 234 | __u32 func_info_cnt; |
Stanislav Fomichev | 47eff61 | 2018-11-20 17:11:19 -0800 | [diff] [blame] | 235 | |
| 236 | struct bpf_capabilities *caps; |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 237 | |
| 238 | void *line_info; |
| 239 | __u32 line_info_rec_size; |
| 240 | __u32 line_info_cnt; |
Jiong Wang | 0465619 | 2019-05-24 23:25:19 +0100 | [diff] [blame] | 241 | __u32 prog_flags; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 242 | }; |
| 243 | |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 244 | struct bpf_struct_ops { |
| 245 | const char *tname; |
| 246 | const struct btf_type *type; |
| 247 | struct bpf_program **progs; |
| 248 | __u32 *kern_func_off; |
| 249 | /* e.g. struct tcp_congestion_ops in bpf_prog's btf format */ |
| 250 | void *data; |
| 251 | /* e.g. struct bpf_struct_ops_tcp_congestion_ops in |
| 252 | * btf_vmlinux's format. |
| 253 | * struct bpf_struct_ops_tcp_congestion_ops { |
| 254 | * [... some other kernel fields ...] |
| 255 | * struct tcp_congestion_ops data; |
| 256 | * } |
| 257 | * kern_vdata-size == sizeof(struct bpf_struct_ops_tcp_congestion_ops) |
| 258 | * bpf_map__init_kern_struct_ops() will populate the "kern_vdata" |
| 259 | * from "data". |
| 260 | */ |
| 261 | void *kern_vdata; |
| 262 | __u32 type_id; |
| 263 | }; |
| 264 | |
Andrii Nakryiko | ac9d138 | 2019-12-13 17:47:07 -0800 | [diff] [blame] | 265 | #define DATA_SEC ".data" |
| 266 | #define BSS_SEC ".bss" |
| 267 | #define RODATA_SEC ".rodata" |
Andrii Nakryiko | 81bfdd0 | 2019-12-18 16:28:34 -0800 | [diff] [blame] | 268 | #define KCONFIG_SEC ".kconfig" |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 269 | #define STRUCT_OPS_SEC ".struct_ops" |
Andrii Nakryiko | ac9d138 | 2019-12-13 17:47:07 -0800 | [diff] [blame] | 270 | |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 271 | enum libbpf_map_type { |
| 272 | LIBBPF_MAP_UNSPEC, |
| 273 | LIBBPF_MAP_DATA, |
| 274 | LIBBPF_MAP_BSS, |
| 275 | LIBBPF_MAP_RODATA, |
Andrii Nakryiko | 81bfdd0 | 2019-12-18 16:28:34 -0800 | [diff] [blame] | 276 | LIBBPF_MAP_KCONFIG, |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 277 | }; |
| 278 | |
| 279 | static const char * const libbpf_type_to_btf_name[] = { |
Andrii Nakryiko | ac9d138 | 2019-12-13 17:47:07 -0800 | [diff] [blame] | 280 | [LIBBPF_MAP_DATA] = DATA_SEC, |
| 281 | [LIBBPF_MAP_BSS] = BSS_SEC, |
| 282 | [LIBBPF_MAP_RODATA] = RODATA_SEC, |
Andrii Nakryiko | 81bfdd0 | 2019-12-18 16:28:34 -0800 | [diff] [blame] | 283 | [LIBBPF_MAP_KCONFIG] = KCONFIG_SEC, |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 284 | }; |
| 285 | |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 286 | struct bpf_map { |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 287 | char *name; |
Andrii Nakryiko | 01af3bf | 2019-12-13 17:43:32 -0800 | [diff] [blame] | 288 | int fd; |
Andrii Nakryiko | db48814 | 2019-06-17 12:26:54 -0700 | [diff] [blame] | 289 | int sec_idx; |
| 290 | size_t sec_offset; |
David Beckett | f0307a7 | 2018-05-16 14:02:49 -0700 | [diff] [blame] | 291 | int map_ifindex; |
Nikita V. Shirokov | addb9fc | 2018-11-20 20:55:56 -0800 | [diff] [blame] | 292 | int inner_map_fd; |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 293 | struct bpf_map_def def; |
Martin KaFai Lau | 5b891af | 2018-07-24 08:40:21 -0700 | [diff] [blame] | 294 | __u32 btf_key_type_id; |
| 295 | __u32 btf_value_type_id; |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 296 | __u32 btf_vmlinux_value_type_id; |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 297 | void *priv; |
| 298 | bpf_map_clear_priv_t clear_priv; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 299 | enum libbpf_map_type libbpf_type; |
Andrii Nakryiko | eba9c5f | 2019-12-13 17:43:33 -0800 | [diff] [blame] | 300 | void *mmaped; |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 301 | struct bpf_struct_ops *st_ops; |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 302 | char *pin_path; |
| 303 | bool pinned; |
Toke Høiland-Jørgensen | ec6d5f47 | 2019-11-09 21:37:27 +0100 | [diff] [blame] | 304 | bool reused; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 305 | }; |
| 306 | |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 307 | enum extern_type { |
| 308 | EXT_UNKNOWN, |
| 309 | EXT_CHAR, |
| 310 | EXT_BOOL, |
| 311 | EXT_INT, |
| 312 | EXT_TRISTATE, |
| 313 | EXT_CHAR_ARR, |
| 314 | }; |
| 315 | |
| 316 | struct extern_desc { |
| 317 | const char *name; |
| 318 | int sym_idx; |
| 319 | int btf_id; |
| 320 | enum extern_type type; |
| 321 | int sz; |
| 322 | int align; |
| 323 | int data_off; |
| 324 | bool is_signed; |
| 325 | bool is_weak; |
| 326 | bool is_set; |
| 327 | }; |
| 328 | |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 329 | static LIST_HEAD(bpf_objects_list); |
| 330 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 331 | struct bpf_object { |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 332 | char name[BPF_OBJ_NAME_LEN]; |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 333 | char license[64]; |
Yonghong Song | 438363c | 2018-10-09 16:14:47 -0700 | [diff] [blame] | 334 | __u32 kern_version; |
Wang Nan | 0b3d1ef | 2015-07-01 02:13:58 +0000 | [diff] [blame] | 335 | |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 336 | struct bpf_program *programs; |
| 337 | size_t nr_programs; |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 338 | struct bpf_map *maps; |
| 339 | size_t nr_maps; |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 340 | size_t maps_cap; |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 341 | |
Andrii Nakryiko | 8601fd4 | 2019-12-18 16:28:35 -0800 | [diff] [blame] | 342 | char *kconfig; |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 343 | struct extern_desc *externs; |
| 344 | int nr_extern; |
Andrii Nakryiko | 81bfdd0 | 2019-12-18 16:28:34 -0800 | [diff] [blame] | 345 | int kconfig_map_idx; |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 346 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 347 | bool loaded; |
Jakub Kicinski | 9a94f27 | 2018-06-28 14:41:38 -0700 | [diff] [blame] | 348 | bool has_pseudo_calls; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 349 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 350 | /* |
| 351 | * Information when doing elf related work. Only valid if fd |
| 352 | * is valid. |
| 353 | */ |
| 354 | struct { |
| 355 | int fd; |
Andrii Nakryiko | 5e61f27 | 2019-10-04 15:40:34 -0700 | [diff] [blame] | 356 | const void *obj_buf; |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 357 | size_t obj_buf_sz; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 358 | Elf *elf; |
| 359 | GElf_Ehdr ehdr; |
Wang Nan | bec7d68 | 2015-07-01 02:13:59 +0000 | [diff] [blame] | 360 | Elf_Data *symbols; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 361 | Elf_Data *data; |
| 362 | Elf_Data *rodata; |
| 363 | Elf_Data *bss; |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 364 | Elf_Data *st_ops_data; |
Wang Nan | 77ba9a5 | 2015-12-08 02:25:30 +0000 | [diff] [blame] | 365 | size_t strtabidx; |
Wang Nan | b62f06e | 2015-07-01 02:14:01 +0000 | [diff] [blame] | 366 | struct { |
| 367 | GElf_Shdr shdr; |
| 368 | Elf_Data *data; |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 369 | } *reloc_sects; |
| 370 | int nr_reloc_sects; |
Wang Nan | 666810e | 2016-01-25 09:55:49 +0000 | [diff] [blame] | 371 | int maps_shndx; |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 372 | int btf_maps_shndx; |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 373 | int text_shndx; |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 374 | int symbols_shndx; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 375 | int data_shndx; |
| 376 | int rodata_shndx; |
| 377 | int bss_shndx; |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 378 | int st_ops_shndx; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 379 | } efile; |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 380 | /* |
| 381 | * All loaded bpf_object is linked in a list, which is |
| 382 | * hidden to caller. bpf_objects__<func> handlers deal with |
| 383 | * all objects. |
| 384 | */ |
| 385 | struct list_head list; |
Wang Nan | 10931d2 | 2016-11-26 07:03:26 +0000 | [diff] [blame] | 386 | |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 387 | struct btf *btf; |
KP Singh | a6ed02c | 2020-01-17 22:28:25 +0100 | [diff] [blame] | 388 | /* Parse and load BTF vmlinux if any of the programs in the object need |
| 389 | * it at load time. |
| 390 | */ |
| 391 | struct btf *btf_vmlinux; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 392 | struct btf_ext *btf_ext; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 393 | |
Wang Nan | 10931d2 | 2016-11-26 07:03:26 +0000 | [diff] [blame] | 394 | void *priv; |
| 395 | bpf_object_clear_priv_t clear_priv; |
| 396 | |
Stanislav Fomichev | 47eff61 | 2018-11-20 17:11:19 -0800 | [diff] [blame] | 397 | struct bpf_capabilities caps; |
| 398 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 399 | char path[]; |
| 400 | }; |
| 401 | #define obj_elf_valid(o) ((o)->efile.elf) |
| 402 | |
Joe Stringer | 29cd77f | 2018-10-02 13:35:39 -0700 | [diff] [blame] | 403 | void bpf_program__unload(struct bpf_program *prog) |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 404 | { |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 405 | int i; |
| 406 | |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 407 | if (!prog) |
| 408 | return; |
| 409 | |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 410 | /* |
| 411 | * If the object is opened but the program was never loaded, |
| 412 | * it is possible that prog->instances.nr == -1. |
| 413 | */ |
| 414 | if (prog->instances.nr > 0) { |
| 415 | for (i = 0; i < prog->instances.nr; i++) |
| 416 | zclose(prog->instances.fds[i]); |
| 417 | } else if (prog->instances.nr != -1) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 418 | pr_warn("Internal error: instances.nr is %d\n", |
| 419 | prog->instances.nr); |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | prog->instances.nr = -1; |
| 423 | zfree(&prog->instances.fds); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 424 | |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 425 | zfree(&prog->func_info); |
Prashant Bhole | 07a09d1 | 2018-12-17 16:57:50 +0900 | [diff] [blame] | 426 | zfree(&prog->line_info); |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 429 | static void bpf_program__exit(struct bpf_program *prog) |
| 430 | { |
| 431 | if (!prog) |
| 432 | return; |
| 433 | |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 434 | if (prog->clear_priv) |
| 435 | prog->clear_priv(prog, prog->priv); |
| 436 | |
| 437 | prog->priv = NULL; |
| 438 | prog->clear_priv = NULL; |
| 439 | |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 440 | bpf_program__unload(prog); |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 441 | zfree(&prog->name); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 442 | zfree(&prog->section_name); |
Stanislav Fomichev | 33a2c75 | 2018-11-09 08:21:43 -0800 | [diff] [blame] | 443 | zfree(&prog->pin_name); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 444 | zfree(&prog->insns); |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 445 | zfree(&prog->reloc_desc); |
| 446 | |
| 447 | prog->nr_reloc = 0; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 448 | prog->insns_cnt = 0; |
| 449 | prog->idx = -1; |
| 450 | } |
| 451 | |
Stanislav Fomichev | 33a2c75 | 2018-11-09 08:21:43 -0800 | [diff] [blame] | 452 | static char *__bpf_program__pin_name(struct bpf_program *prog) |
| 453 | { |
| 454 | char *name, *p; |
| 455 | |
| 456 | name = p = strdup(prog->section_name); |
| 457 | while ((p = strchr(p, '/'))) |
| 458 | *p = '_'; |
| 459 | |
| 460 | return name; |
| 461 | } |
| 462 | |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 463 | static int |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 464 | bpf_program__init(void *data, size_t size, char *section_name, int idx, |
| 465 | struct bpf_program *prog) |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 466 | { |
Andrii Nakryiko | 8ca990c | 2019-05-29 10:36:03 -0700 | [diff] [blame] | 467 | const size_t bpf_insn_sz = sizeof(struct bpf_insn); |
| 468 | |
| 469 | if (size == 0 || size % bpf_insn_sz) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 470 | pr_warn("corrupted section '%s', size: %zu\n", |
| 471 | section_name, size); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 472 | return -EINVAL; |
| 473 | } |
| 474 | |
Andrii Nakryiko | 1ad9cbb | 2019-02-13 10:25:53 -0800 | [diff] [blame] | 475 | memset(prog, 0, sizeof(*prog)); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 476 | |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 477 | prog->section_name = strdup(section_name); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 478 | if (!prog->section_name) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 479 | pr_warn("failed to alloc name for prog under section(%d) %s\n", |
| 480 | idx, section_name); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 481 | goto errout; |
| 482 | } |
| 483 | |
Stanislav Fomichev | 33a2c75 | 2018-11-09 08:21:43 -0800 | [diff] [blame] | 484 | prog->pin_name = __bpf_program__pin_name(prog); |
| 485 | if (!prog->pin_name) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 486 | pr_warn("failed to alloc pin name for prog under section(%d) %s\n", |
| 487 | idx, section_name); |
Stanislav Fomichev | 33a2c75 | 2018-11-09 08:21:43 -0800 | [diff] [blame] | 488 | goto errout; |
| 489 | } |
| 490 | |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 491 | prog->insns = malloc(size); |
| 492 | if (!prog->insns) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 493 | pr_warn("failed to alloc insns for prog under section %s\n", |
| 494 | section_name); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 495 | goto errout; |
| 496 | } |
Andrii Nakryiko | 8ca990c | 2019-05-29 10:36:03 -0700 | [diff] [blame] | 497 | prog->insns_cnt = size / bpf_insn_sz; |
| 498 | memcpy(prog->insns, data, size); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 499 | prog->idx = idx; |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 500 | prog->instances.fds = NULL; |
| 501 | prog->instances.nr = -1; |
Nikita V. Shirokov | 47ae7e3 | 2018-11-23 12:58:12 -0800 | [diff] [blame] | 502 | prog->type = BPF_PROG_TYPE_UNSPEC; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 503 | |
| 504 | return 0; |
| 505 | errout: |
| 506 | bpf_program__exit(prog); |
| 507 | return -ENOMEM; |
| 508 | } |
| 509 | |
| 510 | static int |
| 511 | bpf_object__add_program(struct bpf_object *obj, void *data, size_t size, |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 512 | char *section_name, int idx) |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 513 | { |
| 514 | struct bpf_program prog, *progs; |
| 515 | int nr_progs, err; |
| 516 | |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 517 | err = bpf_program__init(data, size, section_name, idx, &prog); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 518 | if (err) |
| 519 | return err; |
| 520 | |
Stanislav Fomichev | 47eff61 | 2018-11-20 17:11:19 -0800 | [diff] [blame] | 521 | prog.caps = &obj->caps; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 522 | progs = obj->programs; |
| 523 | nr_progs = obj->nr_programs; |
| 524 | |
Jakub Kicinski | 531b014 | 2018-07-10 14:43:05 -0700 | [diff] [blame] | 525 | progs = reallocarray(progs, nr_progs + 1, sizeof(progs[0])); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 526 | if (!progs) { |
| 527 | /* |
| 528 | * In this case the original obj->programs |
| 529 | * is still valid, so don't need special treat for |
| 530 | * bpf_close_object(). |
| 531 | */ |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 532 | pr_warn("failed to alloc a new program under section '%s'\n", |
| 533 | section_name); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 534 | bpf_program__exit(&prog); |
| 535 | return -ENOMEM; |
| 536 | } |
| 537 | |
| 538 | pr_debug("found program %s\n", prog.section_name); |
| 539 | obj->programs = progs; |
| 540 | obj->nr_programs = nr_progs + 1; |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 541 | prog.obj = obj; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 542 | progs[nr_progs] = prog; |
| 543 | return 0; |
| 544 | } |
| 545 | |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 546 | static int |
| 547 | bpf_object__init_prog_names(struct bpf_object *obj) |
| 548 | { |
| 549 | Elf_Data *symbols = obj->efile.symbols; |
| 550 | struct bpf_program *prog; |
| 551 | size_t pi, si; |
| 552 | |
| 553 | for (pi = 0; pi < obj->nr_programs; pi++) { |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 554 | const char *name = NULL; |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 555 | |
| 556 | prog = &obj->programs[pi]; |
| 557 | |
| 558 | for (si = 0; si < symbols->d_size / sizeof(GElf_Sym) && !name; |
| 559 | si++) { |
| 560 | GElf_Sym sym; |
| 561 | |
| 562 | if (!gelf_getsym(symbols, si, &sym)) |
| 563 | continue; |
| 564 | if (sym.st_shndx != prog->idx) |
| 565 | continue; |
Roman Gushchin | fe4d44b | 2017-12-13 15:18:52 +0000 | [diff] [blame] | 566 | if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL) |
| 567 | continue; |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 568 | |
| 569 | name = elf_strptr(obj->efile.elf, |
| 570 | obj->efile.strtabidx, |
| 571 | sym.st_name); |
| 572 | if (!name) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 573 | pr_warn("failed to get sym name string for prog %s\n", |
| 574 | prog->section_name); |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 575 | return -LIBBPF_ERRNO__LIBELF; |
| 576 | } |
| 577 | } |
| 578 | |
Jakub Kicinski | 9a94f27 | 2018-06-28 14:41:38 -0700 | [diff] [blame] | 579 | if (!name && prog->idx == obj->efile.text_shndx) |
| 580 | name = ".text"; |
| 581 | |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 582 | if (!name) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 583 | pr_warn("failed to find sym for prog %s\n", |
| 584 | prog->section_name); |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 585 | return -EINVAL; |
| 586 | } |
Jakub Kicinski | 9a94f27 | 2018-06-28 14:41:38 -0700 | [diff] [blame] | 587 | |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 588 | prog->name = strdup(name); |
| 589 | if (!prog->name) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 590 | pr_warn("failed to allocate memory for prog sym %s\n", |
| 591 | name); |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 592 | return -ENOMEM; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | return 0; |
| 597 | } |
| 598 | |
Andrii Nakryiko | 5e61f27 | 2019-10-04 15:40:34 -0700 | [diff] [blame] | 599 | static __u32 get_kernel_version(void) |
| 600 | { |
| 601 | __u32 major, minor, patch; |
| 602 | struct utsname info; |
| 603 | |
| 604 | uname(&info); |
| 605 | if (sscanf(info.release, "%u.%u.%u", &major, &minor, &patch) != 3) |
| 606 | return 0; |
| 607 | return KERNEL_VERSION(major, minor, patch); |
| 608 | } |
| 609 | |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 610 | static const struct btf_member * |
| 611 | find_member_by_offset(const struct btf_type *t, __u32 bit_offset) |
| 612 | { |
| 613 | struct btf_member *m; |
| 614 | int i; |
| 615 | |
| 616 | for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) { |
| 617 | if (btf_member_bit_offset(t, i) == bit_offset) |
| 618 | return m; |
| 619 | } |
| 620 | |
| 621 | return NULL; |
| 622 | } |
| 623 | |
| 624 | static const struct btf_member * |
| 625 | find_member_by_name(const struct btf *btf, const struct btf_type *t, |
| 626 | const char *name) |
| 627 | { |
| 628 | struct btf_member *m; |
| 629 | int i; |
| 630 | |
| 631 | for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) { |
| 632 | if (!strcmp(btf__name_by_offset(btf, m->name_off), name)) |
| 633 | return m; |
| 634 | } |
| 635 | |
| 636 | return NULL; |
| 637 | } |
| 638 | |
| 639 | #define STRUCT_OPS_VALUE_PREFIX "bpf_struct_ops_" |
KP Singh | a6ed02c | 2020-01-17 22:28:25 +0100 | [diff] [blame] | 640 | static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix, |
| 641 | const char *name, __u32 kind); |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 642 | |
| 643 | static int |
| 644 | find_struct_ops_kern_types(const struct btf *btf, const char *tname, |
| 645 | const struct btf_type **type, __u32 *type_id, |
| 646 | const struct btf_type **vtype, __u32 *vtype_id, |
| 647 | const struct btf_member **data_member) |
| 648 | { |
| 649 | const struct btf_type *kern_type, *kern_vtype; |
| 650 | const struct btf_member *kern_data_member; |
| 651 | __s32 kern_vtype_id, kern_type_id; |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 652 | __u32 i; |
| 653 | |
| 654 | kern_type_id = btf__find_by_name_kind(btf, tname, BTF_KIND_STRUCT); |
| 655 | if (kern_type_id < 0) { |
| 656 | pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n", |
| 657 | tname); |
| 658 | return kern_type_id; |
| 659 | } |
| 660 | kern_type = btf__type_by_id(btf, kern_type_id); |
| 661 | |
| 662 | /* Find the corresponding "map_value" type that will be used |
| 663 | * in map_update(BPF_MAP_TYPE_STRUCT_OPS). For example, |
| 664 | * find "struct bpf_struct_ops_tcp_congestion_ops" from the |
| 665 | * btf_vmlinux. |
| 666 | */ |
KP Singh | a6ed02c | 2020-01-17 22:28:25 +0100 | [diff] [blame] | 667 | kern_vtype_id = find_btf_by_prefix_kind(btf, STRUCT_OPS_VALUE_PREFIX, |
| 668 | tname, BTF_KIND_STRUCT); |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 669 | if (kern_vtype_id < 0) { |
KP Singh | a6ed02c | 2020-01-17 22:28:25 +0100 | [diff] [blame] | 670 | pr_warn("struct_ops init_kern: struct %s%s is not found in kernel BTF\n", |
| 671 | STRUCT_OPS_VALUE_PREFIX, tname); |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 672 | return kern_vtype_id; |
| 673 | } |
| 674 | kern_vtype = btf__type_by_id(btf, kern_vtype_id); |
| 675 | |
| 676 | /* Find "struct tcp_congestion_ops" from |
| 677 | * struct bpf_struct_ops_tcp_congestion_ops { |
| 678 | * [ ... ] |
| 679 | * struct tcp_congestion_ops data; |
| 680 | * } |
| 681 | */ |
| 682 | kern_data_member = btf_members(kern_vtype); |
| 683 | for (i = 0; i < btf_vlen(kern_vtype); i++, kern_data_member++) { |
| 684 | if (kern_data_member->type == kern_type_id) |
| 685 | break; |
| 686 | } |
| 687 | if (i == btf_vlen(kern_vtype)) { |
KP Singh | a6ed02c | 2020-01-17 22:28:25 +0100 | [diff] [blame] | 688 | pr_warn("struct_ops init_kern: struct %s data is not found in struct %s%s\n", |
| 689 | tname, STRUCT_OPS_VALUE_PREFIX, tname); |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 690 | return -EINVAL; |
| 691 | } |
| 692 | |
| 693 | *type = kern_type; |
| 694 | *type_id = kern_type_id; |
| 695 | *vtype = kern_vtype; |
| 696 | *vtype_id = kern_vtype_id; |
| 697 | *data_member = kern_data_member; |
| 698 | |
| 699 | return 0; |
| 700 | } |
| 701 | |
| 702 | static bool bpf_map__is_struct_ops(const struct bpf_map *map) |
| 703 | { |
| 704 | return map->def.type == BPF_MAP_TYPE_STRUCT_OPS; |
| 705 | } |
| 706 | |
| 707 | /* Init the map's fields that depend on kern_btf */ |
| 708 | static int bpf_map__init_kern_struct_ops(struct bpf_map *map, |
| 709 | const struct btf *btf, |
| 710 | const struct btf *kern_btf) |
| 711 | { |
| 712 | const struct btf_member *member, *kern_member, *kern_data_member; |
| 713 | const struct btf_type *type, *kern_type, *kern_vtype; |
| 714 | __u32 i, kern_type_id, kern_vtype_id, kern_data_off; |
| 715 | struct bpf_struct_ops *st_ops; |
| 716 | void *data, *kern_data; |
| 717 | const char *tname; |
| 718 | int err; |
| 719 | |
| 720 | st_ops = map->st_ops; |
| 721 | type = st_ops->type; |
| 722 | tname = st_ops->tname; |
| 723 | err = find_struct_ops_kern_types(kern_btf, tname, |
| 724 | &kern_type, &kern_type_id, |
| 725 | &kern_vtype, &kern_vtype_id, |
| 726 | &kern_data_member); |
| 727 | if (err) |
| 728 | return err; |
| 729 | |
| 730 | pr_debug("struct_ops init_kern %s: type_id:%u kern_type_id:%u kern_vtype_id:%u\n", |
| 731 | map->name, st_ops->type_id, kern_type_id, kern_vtype_id); |
| 732 | |
| 733 | map->def.value_size = kern_vtype->size; |
| 734 | map->btf_vmlinux_value_type_id = kern_vtype_id; |
| 735 | |
| 736 | st_ops->kern_vdata = calloc(1, kern_vtype->size); |
| 737 | if (!st_ops->kern_vdata) |
| 738 | return -ENOMEM; |
| 739 | |
| 740 | data = st_ops->data; |
| 741 | kern_data_off = kern_data_member->offset / 8; |
| 742 | kern_data = st_ops->kern_vdata + kern_data_off; |
| 743 | |
| 744 | member = btf_members(type); |
| 745 | for (i = 0; i < btf_vlen(type); i++, member++) { |
| 746 | const struct btf_type *mtype, *kern_mtype; |
| 747 | __u32 mtype_id, kern_mtype_id; |
| 748 | void *mdata, *kern_mdata; |
| 749 | __s64 msize, kern_msize; |
| 750 | __u32 moff, kern_moff; |
| 751 | __u32 kern_member_idx; |
| 752 | const char *mname; |
| 753 | |
| 754 | mname = btf__name_by_offset(btf, member->name_off); |
| 755 | kern_member = find_member_by_name(kern_btf, kern_type, mname); |
| 756 | if (!kern_member) { |
| 757 | pr_warn("struct_ops init_kern %s: Cannot find member %s in kernel BTF\n", |
| 758 | map->name, mname); |
| 759 | return -ENOTSUP; |
| 760 | } |
| 761 | |
| 762 | kern_member_idx = kern_member - btf_members(kern_type); |
| 763 | if (btf_member_bitfield_size(type, i) || |
| 764 | btf_member_bitfield_size(kern_type, kern_member_idx)) { |
| 765 | pr_warn("struct_ops init_kern %s: bitfield %s is not supported\n", |
| 766 | map->name, mname); |
| 767 | return -ENOTSUP; |
| 768 | } |
| 769 | |
| 770 | moff = member->offset / 8; |
| 771 | kern_moff = kern_member->offset / 8; |
| 772 | |
| 773 | mdata = data + moff; |
| 774 | kern_mdata = kern_data + kern_moff; |
| 775 | |
| 776 | mtype = skip_mods_and_typedefs(btf, member->type, &mtype_id); |
| 777 | kern_mtype = skip_mods_and_typedefs(kern_btf, kern_member->type, |
| 778 | &kern_mtype_id); |
| 779 | if (BTF_INFO_KIND(mtype->info) != |
| 780 | BTF_INFO_KIND(kern_mtype->info)) { |
| 781 | pr_warn("struct_ops init_kern %s: Unmatched member type %s %u != %u(kernel)\n", |
| 782 | map->name, mname, BTF_INFO_KIND(mtype->info), |
| 783 | BTF_INFO_KIND(kern_mtype->info)); |
| 784 | return -ENOTSUP; |
| 785 | } |
| 786 | |
| 787 | if (btf_is_ptr(mtype)) { |
| 788 | struct bpf_program *prog; |
| 789 | |
| 790 | mtype = skip_mods_and_typedefs(btf, mtype->type, &mtype_id); |
| 791 | kern_mtype = skip_mods_and_typedefs(kern_btf, |
| 792 | kern_mtype->type, |
| 793 | &kern_mtype_id); |
| 794 | if (!btf_is_func_proto(mtype) || |
| 795 | !btf_is_func_proto(kern_mtype)) { |
| 796 | pr_warn("struct_ops init_kern %s: non func ptr %s is not supported\n", |
| 797 | map->name, mname); |
| 798 | return -ENOTSUP; |
| 799 | } |
| 800 | |
| 801 | prog = st_ops->progs[i]; |
| 802 | if (!prog) { |
| 803 | pr_debug("struct_ops init_kern %s: func ptr %s is not set\n", |
| 804 | map->name, mname); |
| 805 | continue; |
| 806 | } |
| 807 | |
| 808 | prog->attach_btf_id = kern_type_id; |
| 809 | prog->expected_attach_type = kern_member_idx; |
| 810 | |
| 811 | st_ops->kern_func_off[i] = kern_data_off + kern_moff; |
| 812 | |
| 813 | pr_debug("struct_ops init_kern %s: func ptr %s is set to prog %s from data(+%u) to kern_data(+%u)\n", |
| 814 | map->name, mname, prog->name, moff, |
| 815 | kern_moff); |
| 816 | |
| 817 | continue; |
| 818 | } |
| 819 | |
| 820 | msize = btf__resolve_size(btf, mtype_id); |
| 821 | kern_msize = btf__resolve_size(kern_btf, kern_mtype_id); |
| 822 | if (msize < 0 || kern_msize < 0 || msize != kern_msize) { |
| 823 | pr_warn("struct_ops init_kern %s: Error in size of member %s: %zd != %zd(kernel)\n", |
| 824 | map->name, mname, (ssize_t)msize, |
| 825 | (ssize_t)kern_msize); |
| 826 | return -ENOTSUP; |
| 827 | } |
| 828 | |
| 829 | pr_debug("struct_ops init_kern %s: copy %s %u bytes from data(+%u) to kern_data(+%u)\n", |
| 830 | map->name, mname, (unsigned int)msize, |
| 831 | moff, kern_moff); |
| 832 | memcpy(kern_mdata, mdata, msize); |
| 833 | } |
| 834 | |
| 835 | return 0; |
| 836 | } |
| 837 | |
| 838 | static int bpf_object__init_kern_struct_ops_maps(struct bpf_object *obj) |
| 839 | { |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 840 | struct bpf_map *map; |
| 841 | size_t i; |
| 842 | int err; |
| 843 | |
| 844 | for (i = 0; i < obj->nr_maps; i++) { |
| 845 | map = &obj->maps[i]; |
| 846 | |
| 847 | if (!bpf_map__is_struct_ops(map)) |
| 848 | continue; |
| 849 | |
KP Singh | a6ed02c | 2020-01-17 22:28:25 +0100 | [diff] [blame] | 850 | err = bpf_map__init_kern_struct_ops(map, obj->btf, |
| 851 | obj->btf_vmlinux); |
| 852 | if (err) |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 853 | return err; |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 854 | } |
| 855 | |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 856 | return 0; |
| 857 | } |
| 858 | |
| 859 | static int bpf_object__init_struct_ops_maps(struct bpf_object *obj) |
| 860 | { |
| 861 | const struct btf_type *type, *datasec; |
| 862 | const struct btf_var_secinfo *vsi; |
| 863 | struct bpf_struct_ops *st_ops; |
| 864 | const char *tname, *var_name; |
| 865 | __s32 type_id, datasec_id; |
| 866 | const struct btf *btf; |
| 867 | struct bpf_map *map; |
| 868 | __u32 i; |
| 869 | |
| 870 | if (obj->efile.st_ops_shndx == -1) |
| 871 | return 0; |
| 872 | |
| 873 | btf = obj->btf; |
| 874 | datasec_id = btf__find_by_name_kind(btf, STRUCT_OPS_SEC, |
| 875 | BTF_KIND_DATASEC); |
| 876 | if (datasec_id < 0) { |
| 877 | pr_warn("struct_ops init: DATASEC %s not found\n", |
| 878 | STRUCT_OPS_SEC); |
| 879 | return -EINVAL; |
| 880 | } |
| 881 | |
| 882 | datasec = btf__type_by_id(btf, datasec_id); |
| 883 | vsi = btf_var_secinfos(datasec); |
| 884 | for (i = 0; i < btf_vlen(datasec); i++, vsi++) { |
| 885 | type = btf__type_by_id(obj->btf, vsi->type); |
| 886 | var_name = btf__name_by_offset(obj->btf, type->name_off); |
| 887 | |
| 888 | type_id = btf__resolve_type(obj->btf, vsi->type); |
| 889 | if (type_id < 0) { |
| 890 | pr_warn("struct_ops init: Cannot resolve var type_id %u in DATASEC %s\n", |
| 891 | vsi->type, STRUCT_OPS_SEC); |
| 892 | return -EINVAL; |
| 893 | } |
| 894 | |
| 895 | type = btf__type_by_id(obj->btf, type_id); |
| 896 | tname = btf__name_by_offset(obj->btf, type->name_off); |
| 897 | if (!tname[0]) { |
| 898 | pr_warn("struct_ops init: anonymous type is not supported\n"); |
| 899 | return -ENOTSUP; |
| 900 | } |
| 901 | if (!btf_is_struct(type)) { |
| 902 | pr_warn("struct_ops init: %s is not a struct\n", tname); |
| 903 | return -EINVAL; |
| 904 | } |
| 905 | |
| 906 | map = bpf_object__add_map(obj); |
| 907 | if (IS_ERR(map)) |
| 908 | return PTR_ERR(map); |
| 909 | |
| 910 | map->sec_idx = obj->efile.st_ops_shndx; |
| 911 | map->sec_offset = vsi->offset; |
| 912 | map->name = strdup(var_name); |
| 913 | if (!map->name) |
| 914 | return -ENOMEM; |
| 915 | |
| 916 | map->def.type = BPF_MAP_TYPE_STRUCT_OPS; |
| 917 | map->def.key_size = sizeof(int); |
| 918 | map->def.value_size = type->size; |
| 919 | map->def.max_entries = 1; |
| 920 | |
| 921 | map->st_ops = calloc(1, sizeof(*map->st_ops)); |
| 922 | if (!map->st_ops) |
| 923 | return -ENOMEM; |
| 924 | st_ops = map->st_ops; |
| 925 | st_ops->data = malloc(type->size); |
| 926 | st_ops->progs = calloc(btf_vlen(type), sizeof(*st_ops->progs)); |
| 927 | st_ops->kern_func_off = malloc(btf_vlen(type) * |
| 928 | sizeof(*st_ops->kern_func_off)); |
| 929 | if (!st_ops->data || !st_ops->progs || !st_ops->kern_func_off) |
| 930 | return -ENOMEM; |
| 931 | |
| 932 | if (vsi->offset + type->size > obj->efile.st_ops_data->d_size) { |
| 933 | pr_warn("struct_ops init: var %s is beyond the end of DATASEC %s\n", |
| 934 | var_name, STRUCT_OPS_SEC); |
| 935 | return -EINVAL; |
| 936 | } |
| 937 | |
| 938 | memcpy(st_ops->data, |
| 939 | obj->efile.st_ops_data->d_buf + vsi->offset, |
| 940 | type->size); |
| 941 | st_ops->tname = tname; |
| 942 | st_ops->type = type; |
| 943 | st_ops->type_id = type_id; |
| 944 | |
| 945 | pr_debug("struct_ops init: struct %s(type_id=%u) %s found at offset %u\n", |
| 946 | tname, type_id, var_name, vsi->offset); |
| 947 | } |
| 948 | |
| 949 | return 0; |
| 950 | } |
| 951 | |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 952 | static struct bpf_object *bpf_object__new(const char *path, |
Andrii Nakryiko | 5e61f27 | 2019-10-04 15:40:34 -0700 | [diff] [blame] | 953 | const void *obj_buf, |
Andrii Nakryiko | 2ce8450 | 2019-10-04 15:40:35 -0700 | [diff] [blame] | 954 | size_t obj_buf_sz, |
| 955 | const char *obj_name) |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 956 | { |
| 957 | struct bpf_object *obj; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 958 | char *end; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 959 | |
| 960 | obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1); |
| 961 | if (!obj) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 962 | pr_warn("alloc memory failed for %s\n", path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 963 | return ERR_PTR(-ENOMEM); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | strcpy(obj->path, path); |
Andrii Nakryiko | 2ce8450 | 2019-10-04 15:40:35 -0700 | [diff] [blame] | 967 | if (obj_name) { |
| 968 | strncpy(obj->name, obj_name, sizeof(obj->name) - 1); |
| 969 | obj->name[sizeof(obj->name) - 1] = 0; |
| 970 | } else { |
| 971 | /* Using basename() GNU version which doesn't modify arg. */ |
| 972 | strncpy(obj->name, basename((void *)path), |
| 973 | sizeof(obj->name) - 1); |
| 974 | end = strchr(obj->name, '.'); |
| 975 | if (end) |
| 976 | *end = 0; |
| 977 | } |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 978 | |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 979 | obj->efile.fd = -1; |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 980 | /* |
Andrii Nakryiko | 76e1022 | 2019-05-29 10:36:10 -0700 | [diff] [blame] | 981 | * Caller of this function should also call |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 982 | * bpf_object__elf_finish() after data collection to return |
| 983 | * obj_buf to user. If not, we should duplicate the buffer to |
| 984 | * avoid user freeing them before elf finish. |
| 985 | */ |
| 986 | obj->efile.obj_buf = obj_buf; |
| 987 | obj->efile.obj_buf_sz = obj_buf_sz; |
Wang Nan | 666810e | 2016-01-25 09:55:49 +0000 | [diff] [blame] | 988 | obj->efile.maps_shndx = -1; |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 989 | obj->efile.btf_maps_shndx = -1; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 990 | obj->efile.data_shndx = -1; |
| 991 | obj->efile.rodata_shndx = -1; |
| 992 | obj->efile.bss_shndx = -1; |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 993 | obj->efile.st_ops_shndx = -1; |
Andrii Nakryiko | 81bfdd0 | 2019-12-18 16:28:34 -0800 | [diff] [blame] | 994 | obj->kconfig_map_idx = -1; |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 995 | |
Andrii Nakryiko | 5e61f27 | 2019-10-04 15:40:34 -0700 | [diff] [blame] | 996 | obj->kern_version = get_kernel_version(); |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 997 | obj->loaded = false; |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 998 | |
| 999 | INIT_LIST_HEAD(&obj->list); |
| 1000 | list_add(&obj->list, &bpf_objects_list); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1001 | return obj; |
| 1002 | } |
| 1003 | |
| 1004 | static void bpf_object__elf_finish(struct bpf_object *obj) |
| 1005 | { |
| 1006 | if (!obj_elf_valid(obj)) |
| 1007 | return; |
| 1008 | |
| 1009 | if (obj->efile.elf) { |
| 1010 | elf_end(obj->efile.elf); |
| 1011 | obj->efile.elf = NULL; |
| 1012 | } |
Wang Nan | bec7d68 | 2015-07-01 02:13:59 +0000 | [diff] [blame] | 1013 | obj->efile.symbols = NULL; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 1014 | obj->efile.data = NULL; |
| 1015 | obj->efile.rodata = NULL; |
| 1016 | obj->efile.bss = NULL; |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 1017 | obj->efile.st_ops_data = NULL; |
Wang Nan | b62f06e | 2015-07-01 02:14:01 +0000 | [diff] [blame] | 1018 | |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 1019 | zfree(&obj->efile.reloc_sects); |
| 1020 | obj->efile.nr_reloc_sects = 0; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1021 | zclose(obj->efile.fd); |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 1022 | obj->efile.obj_buf = NULL; |
| 1023 | obj->efile.obj_buf_sz = 0; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | static int bpf_object__elf_init(struct bpf_object *obj) |
| 1027 | { |
| 1028 | int err = 0; |
| 1029 | GElf_Ehdr *ep; |
| 1030 | |
| 1031 | if (obj_elf_valid(obj)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1032 | pr_warn("elf init: internal error\n"); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1033 | return -LIBBPF_ERRNO__LIBELF; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 1036 | if (obj->efile.obj_buf_sz > 0) { |
| 1037 | /* |
| 1038 | * obj_buf should have been validated by |
| 1039 | * bpf_object__open_buffer(). |
| 1040 | */ |
Andrii Nakryiko | 5e61f27 | 2019-10-04 15:40:34 -0700 | [diff] [blame] | 1041 | obj->efile.elf = elf_memory((char *)obj->efile.obj_buf, |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 1042 | obj->efile.obj_buf_sz); |
| 1043 | } else { |
| 1044 | obj->efile.fd = open(obj->path, O_RDONLY); |
| 1045 | if (obj->efile.fd < 0) { |
Andrii Nakryiko | be5c5d4 | 2019-05-29 10:36:04 -0700 | [diff] [blame] | 1046 | char errmsg[STRERR_BUFSIZE], *cp; |
Thomas Richter | 1ce6a9f | 2018-07-30 10:53:23 +0200 | [diff] [blame] | 1047 | |
Andrii Nakryiko | be5c5d4 | 2019-05-29 10:36:04 -0700 | [diff] [blame] | 1048 | err = -errno; |
| 1049 | cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg)); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1050 | pr_warn("failed to open %s: %s\n", obj->path, cp); |
Andrii Nakryiko | be5c5d4 | 2019-05-29 10:36:04 -0700 | [diff] [blame] | 1051 | return err; |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | obj->efile.elf = elf_begin(obj->efile.fd, |
Andrii Nakryiko | 76e1022 | 2019-05-29 10:36:10 -0700 | [diff] [blame] | 1055 | LIBBPF_ELF_C_READ_MMAP, NULL); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1058 | if (!obj->efile.elf) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1059 | pr_warn("failed to open %s as ELF file\n", obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1060 | err = -LIBBPF_ERRNO__LIBELF; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1061 | goto errout; |
| 1062 | } |
| 1063 | |
| 1064 | if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1065 | pr_warn("failed to get EHDR from %s\n", obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1066 | err = -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1067 | goto errout; |
| 1068 | } |
| 1069 | ep = &obj->efile.ehdr; |
| 1070 | |
Wang Nan | 9b16137 | 2016-07-18 06:01:08 +0000 | [diff] [blame] | 1071 | /* Old LLVM set e_machine to EM_NONE */ |
Andrii Nakryiko | 76e1022 | 2019-05-29 10:36:10 -0700 | [diff] [blame] | 1072 | if (ep->e_type != ET_REL || |
| 1073 | (ep->e_machine && ep->e_machine != EM_BPF)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1074 | pr_warn("%s is not an eBPF object file\n", obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1075 | err = -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1076 | goto errout; |
| 1077 | } |
| 1078 | |
| 1079 | return 0; |
| 1080 | errout: |
| 1081 | bpf_object__elf_finish(obj); |
| 1082 | return err; |
| 1083 | } |
| 1084 | |
Andrii Nakryiko | 12ef563 | 2019-05-29 10:36:05 -0700 | [diff] [blame] | 1085 | static int bpf_object__check_endianness(struct bpf_object *obj) |
Wang Nan | cc4228d | 2015-07-01 02:13:55 +0000 | [diff] [blame] | 1086 | { |
Arnaldo Carvalho de Melo | cdb2f92 | 2019-07-19 11:34:06 -0300 | [diff] [blame] | 1087 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
Andrii Nakryiko | 12ef563 | 2019-05-29 10:36:05 -0700 | [diff] [blame] | 1088 | if (obj->efile.ehdr.e_ident[EI_DATA] == ELFDATA2LSB) |
| 1089 | return 0; |
Arnaldo Carvalho de Melo | cdb2f92 | 2019-07-19 11:34:06 -0300 | [diff] [blame] | 1090 | #elif __BYTE_ORDER == __BIG_ENDIAN |
Andrii Nakryiko | 12ef563 | 2019-05-29 10:36:05 -0700 | [diff] [blame] | 1091 | if (obj->efile.ehdr.e_ident[EI_DATA] == ELFDATA2MSB) |
| 1092 | return 0; |
| 1093 | #else |
| 1094 | # error "Unrecognized __BYTE_ORDER__" |
| 1095 | #endif |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1096 | pr_warn("endianness mismatch.\n"); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1097 | return -LIBBPF_ERRNO__ENDIAN; |
Wang Nan | cc4228d | 2015-07-01 02:13:55 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 1100 | static int |
Andrii Nakryiko | 399dc65 | 2019-05-29 10:36:11 -0700 | [diff] [blame] | 1101 | bpf_object__init_license(struct bpf_object *obj, void *data, size_t size) |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 1102 | { |
Andrii Nakryiko | 399dc65 | 2019-05-29 10:36:11 -0700 | [diff] [blame] | 1103 | memcpy(obj->license, data, min(size, sizeof(obj->license) - 1)); |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 1104 | pr_debug("license of %s is %s\n", obj->path, obj->license); |
| 1105 | return 0; |
| 1106 | } |
| 1107 | |
John Fastabend | 54b8625 | 2019-10-18 07:41:26 -0700 | [diff] [blame] | 1108 | static int |
| 1109 | bpf_object__init_kversion(struct bpf_object *obj, void *data, size_t size) |
| 1110 | { |
| 1111 | __u32 kver; |
| 1112 | |
| 1113 | if (size != sizeof(kver)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1114 | pr_warn("invalid kver section in %s\n", obj->path); |
John Fastabend | 54b8625 | 2019-10-18 07:41:26 -0700 | [diff] [blame] | 1115 | return -LIBBPF_ERRNO__FORMAT; |
| 1116 | } |
| 1117 | memcpy(&kver, data, sizeof(kver)); |
| 1118 | obj->kern_version = kver; |
| 1119 | pr_debug("kernel version of %s is %x\n", obj->path, obj->kern_version); |
| 1120 | return 0; |
| 1121 | } |
| 1122 | |
Nikita V. Shirokov | addb9fc | 2018-11-20 20:55:56 -0800 | [diff] [blame] | 1123 | static bool bpf_map_type__is_map_in_map(enum bpf_map_type type) |
| 1124 | { |
| 1125 | if (type == BPF_MAP_TYPE_ARRAY_OF_MAPS || |
| 1126 | type == BPF_MAP_TYPE_HASH_OF_MAPS) |
| 1127 | return true; |
| 1128 | return false; |
| 1129 | } |
| 1130 | |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 1131 | static int bpf_object_search_section_size(const struct bpf_object *obj, |
| 1132 | const char *name, size_t *d_size) |
| 1133 | { |
| 1134 | const GElf_Ehdr *ep = &obj->efile.ehdr; |
| 1135 | Elf *elf = obj->efile.elf; |
| 1136 | Elf_Scn *scn = NULL; |
| 1137 | int idx = 0; |
| 1138 | |
| 1139 | while ((scn = elf_nextscn(elf, scn)) != NULL) { |
| 1140 | const char *sec_name; |
| 1141 | Elf_Data *data; |
| 1142 | GElf_Shdr sh; |
| 1143 | |
| 1144 | idx++; |
| 1145 | if (gelf_getshdr(scn, &sh) != &sh) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1146 | pr_warn("failed to get section(%d) header from %s\n", |
| 1147 | idx, obj->path); |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 1148 | return -EIO; |
| 1149 | } |
| 1150 | |
| 1151 | sec_name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name); |
| 1152 | if (!sec_name) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1153 | pr_warn("failed to get section(%d) name from %s\n", |
| 1154 | idx, obj->path); |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 1155 | return -EIO; |
| 1156 | } |
| 1157 | |
| 1158 | if (strcmp(name, sec_name)) |
| 1159 | continue; |
| 1160 | |
| 1161 | data = elf_getdata(scn, 0); |
| 1162 | if (!data) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1163 | pr_warn("failed to get section(%d) data from %s(%s)\n", |
| 1164 | idx, name, obj->path); |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 1165 | return -EIO; |
| 1166 | } |
| 1167 | |
| 1168 | *d_size = data->d_size; |
| 1169 | return 0; |
| 1170 | } |
| 1171 | |
| 1172 | return -ENOENT; |
| 1173 | } |
| 1174 | |
| 1175 | int bpf_object__section_size(const struct bpf_object *obj, const char *name, |
| 1176 | __u32 *size) |
| 1177 | { |
| 1178 | int ret = -ENOENT; |
| 1179 | size_t d_size; |
| 1180 | |
| 1181 | *size = 0; |
| 1182 | if (!name) { |
| 1183 | return -EINVAL; |
Andrii Nakryiko | ac9d138 | 2019-12-13 17:47:07 -0800 | [diff] [blame] | 1184 | } else if (!strcmp(name, DATA_SEC)) { |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 1185 | if (obj->efile.data) |
| 1186 | *size = obj->efile.data->d_size; |
Andrii Nakryiko | ac9d138 | 2019-12-13 17:47:07 -0800 | [diff] [blame] | 1187 | } else if (!strcmp(name, BSS_SEC)) { |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 1188 | if (obj->efile.bss) |
| 1189 | *size = obj->efile.bss->d_size; |
Andrii Nakryiko | ac9d138 | 2019-12-13 17:47:07 -0800 | [diff] [blame] | 1190 | } else if (!strcmp(name, RODATA_SEC)) { |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 1191 | if (obj->efile.rodata) |
| 1192 | *size = obj->efile.rodata->d_size; |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 1193 | } else if (!strcmp(name, STRUCT_OPS_SEC)) { |
| 1194 | if (obj->efile.st_ops_data) |
| 1195 | *size = obj->efile.st_ops_data->d_size; |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 1196 | } else { |
| 1197 | ret = bpf_object_search_section_size(obj, name, &d_size); |
| 1198 | if (!ret) |
| 1199 | *size = d_size; |
| 1200 | } |
| 1201 | |
| 1202 | return *size ? 0 : ret; |
| 1203 | } |
| 1204 | |
| 1205 | int bpf_object__variable_offset(const struct bpf_object *obj, const char *name, |
| 1206 | __u32 *off) |
| 1207 | { |
| 1208 | Elf_Data *symbols = obj->efile.symbols; |
| 1209 | const char *sname; |
| 1210 | size_t si; |
| 1211 | |
| 1212 | if (!name || !off) |
| 1213 | return -EINVAL; |
| 1214 | |
| 1215 | for (si = 0; si < symbols->d_size / sizeof(GElf_Sym); si++) { |
| 1216 | GElf_Sym sym; |
| 1217 | |
| 1218 | if (!gelf_getsym(symbols, si, &sym)) |
| 1219 | continue; |
| 1220 | if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL || |
| 1221 | GELF_ST_TYPE(sym.st_info) != STT_OBJECT) |
| 1222 | continue; |
| 1223 | |
| 1224 | sname = elf_strptr(obj->efile.elf, obj->efile.strtabidx, |
| 1225 | sym.st_name); |
| 1226 | if (!sname) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1227 | pr_warn("failed to get sym name string for var %s\n", |
| 1228 | name); |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 1229 | return -EIO; |
| 1230 | } |
| 1231 | if (strcmp(name, sname) == 0) { |
| 1232 | *off = sym.st_value; |
| 1233 | return 0; |
| 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | return -ENOENT; |
| 1238 | } |
| 1239 | |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1240 | static struct bpf_map *bpf_object__add_map(struct bpf_object *obj) |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 1241 | { |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1242 | struct bpf_map *new_maps; |
| 1243 | size_t new_cap; |
| 1244 | int i; |
| 1245 | |
| 1246 | if (obj->nr_maps < obj->maps_cap) |
| 1247 | return &obj->maps[obj->nr_maps++]; |
| 1248 | |
Ivan Khoronzhuk | 9506497 | 2019-06-26 13:38:37 +0300 | [diff] [blame] | 1249 | new_cap = max((size_t)4, obj->maps_cap * 3 / 2); |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1250 | new_maps = realloc(obj->maps, new_cap * sizeof(*obj->maps)); |
| 1251 | if (!new_maps) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1252 | pr_warn("alloc maps for object failed\n"); |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1253 | return ERR_PTR(-ENOMEM); |
| 1254 | } |
| 1255 | |
| 1256 | obj->maps_cap = new_cap; |
| 1257 | obj->maps = new_maps; |
| 1258 | |
| 1259 | /* zero out new maps */ |
| 1260 | memset(obj->maps + obj->nr_maps, 0, |
| 1261 | (obj->maps_cap - obj->nr_maps) * sizeof(*obj->maps)); |
| 1262 | /* |
| 1263 | * fill all fd with -1 so won't close incorrect fd (fd=0 is stdin) |
| 1264 | * when failure (zclose won't close negative fd)). |
| 1265 | */ |
| 1266 | for (i = obj->nr_maps; i < obj->maps_cap; i++) { |
| 1267 | obj->maps[i].fd = -1; |
| 1268 | obj->maps[i].inner_map_fd = -1; |
| 1269 | } |
| 1270 | |
| 1271 | return &obj->maps[obj->nr_maps++]; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 1272 | } |
| 1273 | |
Andrii Nakryiko | eba9c5f | 2019-12-13 17:43:33 -0800 | [diff] [blame] | 1274 | static size_t bpf_map_mmap_sz(const struct bpf_map *map) |
| 1275 | { |
| 1276 | long page_sz = sysconf(_SC_PAGE_SIZE); |
| 1277 | size_t map_sz; |
| 1278 | |
Andrii Nakryiko | c701917 | 2020-01-16 22:08:00 -0800 | [diff] [blame] | 1279 | map_sz = (size_t)roundup(map->def.value_size, 8) * map->def.max_entries; |
Andrii Nakryiko | eba9c5f | 2019-12-13 17:43:33 -0800 | [diff] [blame] | 1280 | map_sz = roundup(map_sz, page_sz); |
| 1281 | return map_sz; |
| 1282 | } |
| 1283 | |
Andrii Nakryiko | 81bfdd0 | 2019-12-18 16:28:34 -0800 | [diff] [blame] | 1284 | static char *internal_map_name(struct bpf_object *obj, |
| 1285 | enum libbpf_map_type type) |
| 1286 | { |
Toke Høiland-Jørgensen | 113e6b7 | 2020-02-17 18:17:01 +0100 | [diff] [blame] | 1287 | char map_name[BPF_OBJ_NAME_LEN], *p; |
Andrii Nakryiko | 81bfdd0 | 2019-12-18 16:28:34 -0800 | [diff] [blame] | 1288 | const char *sfx = libbpf_type_to_btf_name[type]; |
| 1289 | int sfx_len = max((size_t)7, strlen(sfx)); |
| 1290 | int pfx_len = min((size_t)BPF_OBJ_NAME_LEN - sfx_len - 1, |
| 1291 | strlen(obj->name)); |
| 1292 | |
| 1293 | snprintf(map_name, sizeof(map_name), "%.*s%.*s", pfx_len, obj->name, |
| 1294 | sfx_len, libbpf_type_to_btf_name[type]); |
| 1295 | |
Toke Høiland-Jørgensen | 113e6b7 | 2020-02-17 18:17:01 +0100 | [diff] [blame] | 1296 | /* sanitise map name to characters allowed by kernel */ |
| 1297 | for (p = map_name; *p && p < map_name + sizeof(map_name); p++) |
| 1298 | if (!isalnum(*p) && *p != '_' && *p != '.') |
| 1299 | *p = '_'; |
| 1300 | |
Andrii Nakryiko | 81bfdd0 | 2019-12-18 16:28:34 -0800 | [diff] [blame] | 1301 | return strdup(map_name); |
| 1302 | } |
| 1303 | |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 1304 | static int |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1305 | bpf_object__init_internal_map(struct bpf_object *obj, enum libbpf_map_type type, |
Andrii Nakryiko | eba9c5f | 2019-12-13 17:43:33 -0800 | [diff] [blame] | 1306 | int sec_idx, void *data, size_t data_sz) |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 1307 | { |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1308 | struct bpf_map_def *def; |
| 1309 | struct bpf_map *map; |
Andrii Nakryiko | eba9c5f | 2019-12-13 17:43:33 -0800 | [diff] [blame] | 1310 | int err; |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1311 | |
| 1312 | map = bpf_object__add_map(obj); |
| 1313 | if (IS_ERR(map)) |
| 1314 | return PTR_ERR(map); |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 1315 | |
| 1316 | map->libbpf_type = type; |
Andrii Nakryiko | db48814 | 2019-06-17 12:26:54 -0700 | [diff] [blame] | 1317 | map->sec_idx = sec_idx; |
| 1318 | map->sec_offset = 0; |
Andrii Nakryiko | 81bfdd0 | 2019-12-18 16:28:34 -0800 | [diff] [blame] | 1319 | map->name = internal_map_name(obj, type); |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 1320 | if (!map->name) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1321 | pr_warn("failed to alloc map name\n"); |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 1322 | return -ENOMEM; |
| 1323 | } |
| 1324 | |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1325 | def = &map->def; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 1326 | def->type = BPF_MAP_TYPE_ARRAY; |
| 1327 | def->key_size = sizeof(int); |
Andrii Nakryiko | eba9c5f | 2019-12-13 17:43:33 -0800 | [diff] [blame] | 1328 | def->value_size = data_sz; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 1329 | def->max_entries = 1; |
Andrii Nakryiko | 81bfdd0 | 2019-12-18 16:28:34 -0800 | [diff] [blame] | 1330 | def->map_flags = type == LIBBPF_MAP_RODATA || type == LIBBPF_MAP_KCONFIG |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 1331 | ? BPF_F_RDONLY_PROG : 0; |
Andrii Nakryiko | 0d13bfc | 2019-12-13 17:43:25 -0800 | [diff] [blame] | 1332 | def->map_flags |= BPF_F_MMAPABLE; |
Andrii Nakryiko | 7fe74b4 | 2019-11-17 09:28:05 -0800 | [diff] [blame] | 1333 | |
| 1334 | pr_debug("map '%s' (global data): at sec_idx %d, offset %zu, flags %x.\n", |
Andrii Nakryiko | 81bfdd0 | 2019-12-18 16:28:34 -0800 | [diff] [blame] | 1335 | map->name, map->sec_idx, map->sec_offset, def->map_flags); |
Andrii Nakryiko | 7fe74b4 | 2019-11-17 09:28:05 -0800 | [diff] [blame] | 1336 | |
Andrii Nakryiko | eba9c5f | 2019-12-13 17:43:33 -0800 | [diff] [blame] | 1337 | map->mmaped = mmap(NULL, bpf_map_mmap_sz(map), PROT_READ | PROT_WRITE, |
| 1338 | MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
| 1339 | if (map->mmaped == MAP_FAILED) { |
| 1340 | err = -errno; |
| 1341 | map->mmaped = NULL; |
| 1342 | pr_warn("failed to alloc map '%s' content buffer: %d\n", |
| 1343 | map->name, err); |
| 1344 | zfree(&map->name); |
| 1345 | return err; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 1346 | } |
| 1347 | |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 1348 | if (data) |
Andrii Nakryiko | eba9c5f | 2019-12-13 17:43:33 -0800 | [diff] [blame] | 1349 | memcpy(map->mmaped, data, data_sz); |
| 1350 | |
Andrii Nakryiko | e1d1dc4 | 2019-04-16 11:47:17 -0700 | [diff] [blame] | 1351 | pr_debug("map %td is \"%s\"\n", map - obj->maps, map->name); |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 1352 | return 0; |
| 1353 | } |
| 1354 | |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1355 | static int bpf_object__init_global_data_maps(struct bpf_object *obj) |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 1356 | { |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1357 | int err; |
| 1358 | |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1359 | /* |
| 1360 | * Populate obj->maps with libbpf internal maps. |
| 1361 | */ |
| 1362 | if (obj->efile.data_shndx >= 0) { |
| 1363 | err = bpf_object__init_internal_map(obj, LIBBPF_MAP_DATA, |
Andrii Nakryiko | db48814 | 2019-06-17 12:26:54 -0700 | [diff] [blame] | 1364 | obj->efile.data_shndx, |
Andrii Nakryiko | eba9c5f | 2019-12-13 17:43:33 -0800 | [diff] [blame] | 1365 | obj->efile.data->d_buf, |
| 1366 | obj->efile.data->d_size); |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1367 | if (err) |
| 1368 | return err; |
| 1369 | } |
| 1370 | if (obj->efile.rodata_shndx >= 0) { |
| 1371 | err = bpf_object__init_internal_map(obj, LIBBPF_MAP_RODATA, |
Andrii Nakryiko | db48814 | 2019-06-17 12:26:54 -0700 | [diff] [blame] | 1372 | obj->efile.rodata_shndx, |
Andrii Nakryiko | eba9c5f | 2019-12-13 17:43:33 -0800 | [diff] [blame] | 1373 | obj->efile.rodata->d_buf, |
| 1374 | obj->efile.rodata->d_size); |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1375 | if (err) |
| 1376 | return err; |
| 1377 | } |
| 1378 | if (obj->efile.bss_shndx >= 0) { |
| 1379 | err = bpf_object__init_internal_map(obj, LIBBPF_MAP_BSS, |
Andrii Nakryiko | db48814 | 2019-06-17 12:26:54 -0700 | [diff] [blame] | 1380 | obj->efile.bss_shndx, |
Andrii Nakryiko | eba9c5f | 2019-12-13 17:43:33 -0800 | [diff] [blame] | 1381 | NULL, |
| 1382 | obj->efile.bss->d_size); |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1383 | if (err) |
| 1384 | return err; |
| 1385 | } |
| 1386 | return 0; |
| 1387 | } |
| 1388 | |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 1389 | |
| 1390 | static struct extern_desc *find_extern_by_name(const struct bpf_object *obj, |
| 1391 | const void *name) |
| 1392 | { |
| 1393 | int i; |
| 1394 | |
| 1395 | for (i = 0; i < obj->nr_extern; i++) { |
| 1396 | if (strcmp(obj->externs[i].name, name) == 0) |
| 1397 | return &obj->externs[i]; |
| 1398 | } |
| 1399 | return NULL; |
| 1400 | } |
| 1401 | |
| 1402 | static int set_ext_value_tri(struct extern_desc *ext, void *ext_val, |
| 1403 | char value) |
| 1404 | { |
| 1405 | switch (ext->type) { |
| 1406 | case EXT_BOOL: |
| 1407 | if (value == 'm') { |
| 1408 | pr_warn("extern %s=%c should be tristate or char\n", |
| 1409 | ext->name, value); |
| 1410 | return -EINVAL; |
| 1411 | } |
| 1412 | *(bool *)ext_val = value == 'y' ? true : false; |
| 1413 | break; |
| 1414 | case EXT_TRISTATE: |
| 1415 | if (value == 'y') |
| 1416 | *(enum libbpf_tristate *)ext_val = TRI_YES; |
| 1417 | else if (value == 'm') |
| 1418 | *(enum libbpf_tristate *)ext_val = TRI_MODULE; |
| 1419 | else /* value == 'n' */ |
| 1420 | *(enum libbpf_tristate *)ext_val = TRI_NO; |
| 1421 | break; |
| 1422 | case EXT_CHAR: |
| 1423 | *(char *)ext_val = value; |
| 1424 | break; |
| 1425 | case EXT_UNKNOWN: |
| 1426 | case EXT_INT: |
| 1427 | case EXT_CHAR_ARR: |
| 1428 | default: |
| 1429 | pr_warn("extern %s=%c should be bool, tristate, or char\n", |
| 1430 | ext->name, value); |
| 1431 | return -EINVAL; |
| 1432 | } |
| 1433 | ext->is_set = true; |
| 1434 | return 0; |
| 1435 | } |
| 1436 | |
| 1437 | static int set_ext_value_str(struct extern_desc *ext, char *ext_val, |
| 1438 | const char *value) |
| 1439 | { |
| 1440 | size_t len; |
| 1441 | |
| 1442 | if (ext->type != EXT_CHAR_ARR) { |
| 1443 | pr_warn("extern %s=%s should char array\n", ext->name, value); |
| 1444 | return -EINVAL; |
| 1445 | } |
| 1446 | |
| 1447 | len = strlen(value); |
| 1448 | if (value[len - 1] != '"') { |
| 1449 | pr_warn("extern '%s': invalid string config '%s'\n", |
| 1450 | ext->name, value); |
| 1451 | return -EINVAL; |
| 1452 | } |
| 1453 | |
| 1454 | /* strip quotes */ |
| 1455 | len -= 2; |
| 1456 | if (len >= ext->sz) { |
| 1457 | pr_warn("extern '%s': long string config %s of (%zu bytes) truncated to %d bytes\n", |
| 1458 | ext->name, value, len, ext->sz - 1); |
| 1459 | len = ext->sz - 1; |
| 1460 | } |
| 1461 | memcpy(ext_val, value + 1, len); |
| 1462 | ext_val[len] = '\0'; |
| 1463 | ext->is_set = true; |
| 1464 | return 0; |
| 1465 | } |
| 1466 | |
| 1467 | static int parse_u64(const char *value, __u64 *res) |
| 1468 | { |
| 1469 | char *value_end; |
| 1470 | int err; |
| 1471 | |
| 1472 | errno = 0; |
| 1473 | *res = strtoull(value, &value_end, 0); |
| 1474 | if (errno) { |
| 1475 | err = -errno; |
| 1476 | pr_warn("failed to parse '%s' as integer: %d\n", value, err); |
| 1477 | return err; |
| 1478 | } |
| 1479 | if (*value_end) { |
| 1480 | pr_warn("failed to parse '%s' as integer completely\n", value); |
| 1481 | return -EINVAL; |
| 1482 | } |
| 1483 | return 0; |
| 1484 | } |
| 1485 | |
| 1486 | static bool is_ext_value_in_range(const struct extern_desc *ext, __u64 v) |
| 1487 | { |
| 1488 | int bit_sz = ext->sz * 8; |
| 1489 | |
| 1490 | if (ext->sz == 8) |
| 1491 | return true; |
| 1492 | |
| 1493 | /* Validate that value stored in u64 fits in integer of `ext->sz` |
| 1494 | * bytes size without any loss of information. If the target integer |
| 1495 | * is signed, we rely on the following limits of integer type of |
| 1496 | * Y bits and subsequent transformation: |
| 1497 | * |
| 1498 | * -2^(Y-1) <= X <= 2^(Y-1) - 1 |
| 1499 | * 0 <= X + 2^(Y-1) <= 2^Y - 1 |
| 1500 | * 0 <= X + 2^(Y-1) < 2^Y |
| 1501 | * |
| 1502 | * For unsigned target integer, check that all the (64 - Y) bits are |
| 1503 | * zero. |
| 1504 | */ |
| 1505 | if (ext->is_signed) |
| 1506 | return v + (1ULL << (bit_sz - 1)) < (1ULL << bit_sz); |
| 1507 | else |
| 1508 | return (v >> bit_sz) == 0; |
| 1509 | } |
| 1510 | |
| 1511 | static int set_ext_value_num(struct extern_desc *ext, void *ext_val, |
| 1512 | __u64 value) |
| 1513 | { |
| 1514 | if (ext->type != EXT_INT && ext->type != EXT_CHAR) { |
| 1515 | pr_warn("extern %s=%llu should be integer\n", |
Andrii Nakryiko | 7745ff9 | 2019-12-18 21:21:03 -0800 | [diff] [blame] | 1516 | ext->name, (unsigned long long)value); |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 1517 | return -EINVAL; |
| 1518 | } |
| 1519 | if (!is_ext_value_in_range(ext, value)) { |
| 1520 | pr_warn("extern %s=%llu value doesn't fit in %d bytes\n", |
Andrii Nakryiko | 7745ff9 | 2019-12-18 21:21:03 -0800 | [diff] [blame] | 1521 | ext->name, (unsigned long long)value, ext->sz); |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 1522 | return -ERANGE; |
| 1523 | } |
| 1524 | switch (ext->sz) { |
| 1525 | case 1: *(__u8 *)ext_val = value; break; |
| 1526 | case 2: *(__u16 *)ext_val = value; break; |
| 1527 | case 4: *(__u32 *)ext_val = value; break; |
| 1528 | case 8: *(__u64 *)ext_val = value; break; |
| 1529 | default: |
| 1530 | return -EINVAL; |
| 1531 | } |
| 1532 | ext->is_set = true; |
| 1533 | return 0; |
| 1534 | } |
| 1535 | |
Andrii Nakryiko | 8601fd4 | 2019-12-18 16:28:35 -0800 | [diff] [blame] | 1536 | static int bpf_object__process_kconfig_line(struct bpf_object *obj, |
| 1537 | char *buf, void *data) |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 1538 | { |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 1539 | struct extern_desc *ext; |
Andrii Nakryiko | 8601fd4 | 2019-12-18 16:28:35 -0800 | [diff] [blame] | 1540 | char *sep, *value; |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 1541 | int len, err = 0; |
| 1542 | void *ext_val; |
| 1543 | __u64 num; |
Andrii Nakryiko | 8601fd4 | 2019-12-18 16:28:35 -0800 | [diff] [blame] | 1544 | |
| 1545 | if (strncmp(buf, "CONFIG_", 7)) |
| 1546 | return 0; |
| 1547 | |
| 1548 | sep = strchr(buf, '='); |
| 1549 | if (!sep) { |
| 1550 | pr_warn("failed to parse '%s': no separator\n", buf); |
| 1551 | return -EINVAL; |
| 1552 | } |
| 1553 | |
| 1554 | /* Trim ending '\n' */ |
| 1555 | len = strlen(buf); |
| 1556 | if (buf[len - 1] == '\n') |
| 1557 | buf[len - 1] = '\0'; |
| 1558 | /* Split on '=' and ensure that a value is present. */ |
| 1559 | *sep = '\0'; |
| 1560 | if (!sep[1]) { |
| 1561 | *sep = '='; |
| 1562 | pr_warn("failed to parse '%s': no value\n", buf); |
| 1563 | return -EINVAL; |
| 1564 | } |
| 1565 | |
| 1566 | ext = find_extern_by_name(obj, buf); |
| 1567 | if (!ext || ext->is_set) |
| 1568 | return 0; |
| 1569 | |
| 1570 | ext_val = data + ext->data_off; |
| 1571 | value = sep + 1; |
| 1572 | |
| 1573 | switch (*value) { |
| 1574 | case 'y': case 'n': case 'm': |
| 1575 | err = set_ext_value_tri(ext, ext_val, *value); |
| 1576 | break; |
| 1577 | case '"': |
| 1578 | err = set_ext_value_str(ext, ext_val, value); |
| 1579 | break; |
| 1580 | default: |
| 1581 | /* assume integer */ |
| 1582 | err = parse_u64(value, &num); |
| 1583 | if (err) { |
| 1584 | pr_warn("extern %s=%s should be integer\n", |
| 1585 | ext->name, value); |
| 1586 | return err; |
| 1587 | } |
| 1588 | err = set_ext_value_num(ext, ext_val, num); |
| 1589 | break; |
| 1590 | } |
| 1591 | if (err) |
| 1592 | return err; |
| 1593 | pr_debug("extern %s=%s\n", ext->name, value); |
| 1594 | return 0; |
| 1595 | } |
| 1596 | |
| 1597 | static int bpf_object__read_kconfig_file(struct bpf_object *obj, void *data) |
| 1598 | { |
| 1599 | char buf[PATH_MAX]; |
| 1600 | struct utsname uts; |
| 1601 | int len, err = 0; |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 1602 | gzFile file; |
| 1603 | |
Andrii Nakryiko | 8601fd4 | 2019-12-18 16:28:35 -0800 | [diff] [blame] | 1604 | uname(&uts); |
| 1605 | len = snprintf(buf, PATH_MAX, "/boot/config-%s", uts.release); |
| 1606 | if (len < 0) |
| 1607 | return -EINVAL; |
| 1608 | else if (len >= PATH_MAX) |
| 1609 | return -ENAMETOOLONG; |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 1610 | |
Andrii Nakryiko | 8601fd4 | 2019-12-18 16:28:35 -0800 | [diff] [blame] | 1611 | /* gzopen also accepts uncompressed files. */ |
| 1612 | file = gzopen(buf, "r"); |
| 1613 | if (!file) |
| 1614 | file = gzopen("/proc/config.gz", "r"); |
| 1615 | |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 1616 | if (!file) { |
Andrii Nakryiko | 8601fd4 | 2019-12-18 16:28:35 -0800 | [diff] [blame] | 1617 | pr_warn("failed to open system Kconfig\n"); |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 1618 | return -ENOENT; |
| 1619 | } |
| 1620 | |
| 1621 | while (gzgets(file, buf, sizeof(buf))) { |
Andrii Nakryiko | 8601fd4 | 2019-12-18 16:28:35 -0800 | [diff] [blame] | 1622 | err = bpf_object__process_kconfig_line(obj, buf, data); |
| 1623 | if (err) { |
| 1624 | pr_warn("error parsing system Kconfig line '%s': %d\n", |
| 1625 | buf, err); |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 1626 | goto out; |
| 1627 | } |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | out: |
| 1631 | gzclose(file); |
| 1632 | return err; |
| 1633 | } |
| 1634 | |
Andrii Nakryiko | 8601fd4 | 2019-12-18 16:28:35 -0800 | [diff] [blame] | 1635 | static int bpf_object__read_kconfig_mem(struct bpf_object *obj, |
| 1636 | const char *config, void *data) |
| 1637 | { |
| 1638 | char buf[PATH_MAX]; |
| 1639 | int err = 0; |
| 1640 | FILE *file; |
| 1641 | |
| 1642 | file = fmemopen((void *)config, strlen(config), "r"); |
| 1643 | if (!file) { |
| 1644 | err = -errno; |
| 1645 | pr_warn("failed to open in-memory Kconfig: %d\n", err); |
| 1646 | return err; |
| 1647 | } |
| 1648 | |
| 1649 | while (fgets(buf, sizeof(buf), file)) { |
| 1650 | err = bpf_object__process_kconfig_line(obj, buf, data); |
| 1651 | if (err) { |
| 1652 | pr_warn("error parsing in-memory Kconfig line '%s': %d\n", |
| 1653 | buf, err); |
| 1654 | break; |
| 1655 | } |
| 1656 | } |
| 1657 | |
| 1658 | fclose(file); |
| 1659 | return err; |
| 1660 | } |
| 1661 | |
Andrii Nakryiko | 81bfdd0 | 2019-12-18 16:28:34 -0800 | [diff] [blame] | 1662 | static int bpf_object__init_kconfig_map(struct bpf_object *obj) |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 1663 | { |
| 1664 | struct extern_desc *last_ext; |
| 1665 | size_t map_sz; |
| 1666 | int err; |
| 1667 | |
| 1668 | if (obj->nr_extern == 0) |
| 1669 | return 0; |
| 1670 | |
| 1671 | last_ext = &obj->externs[obj->nr_extern - 1]; |
| 1672 | map_sz = last_ext->data_off + last_ext->sz; |
| 1673 | |
Andrii Nakryiko | 81bfdd0 | 2019-12-18 16:28:34 -0800 | [diff] [blame] | 1674 | err = bpf_object__init_internal_map(obj, LIBBPF_MAP_KCONFIG, |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 1675 | obj->efile.symbols_shndx, |
| 1676 | NULL, map_sz); |
| 1677 | if (err) |
| 1678 | return err; |
| 1679 | |
Andrii Nakryiko | 81bfdd0 | 2019-12-18 16:28:34 -0800 | [diff] [blame] | 1680 | obj->kconfig_map_idx = obj->nr_maps - 1; |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 1681 | |
| 1682 | return 0; |
| 1683 | } |
| 1684 | |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1685 | static int bpf_object__init_user_maps(struct bpf_object *obj, bool strict) |
| 1686 | { |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 1687 | Elf_Data *symbols = obj->efile.symbols; |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1688 | int i, map_def_sz = 0, nr_maps = 0, nr_syms; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 1689 | Elf_Data *data = NULL; |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1690 | Elf_Scn *scn; |
| 1691 | |
| 1692 | if (obj->efile.maps_shndx < 0) |
| 1693 | return 0; |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 1694 | |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 1695 | if (!symbols) |
| 1696 | return -EINVAL; |
| 1697 | |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1698 | scn = elf_getscn(obj->efile.elf, obj->efile.maps_shndx); |
| 1699 | if (scn) |
| 1700 | data = elf_getdata(scn, NULL); |
| 1701 | if (!scn || !data) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1702 | pr_warn("failed to get Elf_Data from map section %d\n", |
| 1703 | obj->efile.maps_shndx); |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1704 | return -EINVAL; |
Wang Nan | 0b3d1ef | 2015-07-01 02:13:58 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 1707 | /* |
| 1708 | * Count number of maps. Each map has a name. |
| 1709 | * Array of maps is not supported: only the first element is |
| 1710 | * considered. |
| 1711 | * |
| 1712 | * TODO: Detect array of map and report error. |
| 1713 | */ |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1714 | nr_syms = symbols->d_size / sizeof(GElf_Sym); |
| 1715 | for (i = 0; i < nr_syms; i++) { |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 1716 | GElf_Sym sym; |
| 1717 | |
| 1718 | if (!gelf_getsym(symbols, i, &sym)) |
| 1719 | continue; |
| 1720 | if (sym.st_shndx != obj->efile.maps_shndx) |
| 1721 | continue; |
| 1722 | nr_maps++; |
| 1723 | } |
Craig Gallek | b13c5c1 | 2017-10-05 10:41:57 -0400 | [diff] [blame] | 1724 | /* Assume equally sized map definitions */ |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1725 | pr_debug("maps in %s: %d maps in %zd bytes\n", |
| 1726 | obj->path, nr_maps, data->d_size); |
Daniel Borkmann | 4f8827d | 2019-04-24 00:45:57 +0200 | [diff] [blame] | 1727 | |
Andrii Nakryiko | 98e527af | 2019-11-06 18:08:55 -0800 | [diff] [blame] | 1728 | if (!data->d_size || nr_maps == 0 || (data->d_size % nr_maps) != 0) { |
Andrii Nakryiko | 8983b73 | 2019-11-20 23:07:42 -0800 | [diff] [blame] | 1729 | pr_warn("unable to determine map definition size section %s, %d maps in %zd bytes\n", |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1730 | obj->path, nr_maps, data->d_size); |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1731 | return -EINVAL; |
Craig Gallek | b13c5c1 | 2017-10-05 10:41:57 -0400 | [diff] [blame] | 1732 | } |
Andrii Nakryiko | 98e527af | 2019-11-06 18:08:55 -0800 | [diff] [blame] | 1733 | map_def_sz = data->d_size / nr_maps; |
Craig Gallek | b13c5c1 | 2017-10-05 10:41:57 -0400 | [diff] [blame] | 1734 | |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1735 | /* Fill obj->maps using data in "maps" section. */ |
| 1736 | for (i = 0; i < nr_syms; i++) { |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 1737 | GElf_Sym sym; |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 1738 | const char *map_name; |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 1739 | struct bpf_map_def *def; |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1740 | struct bpf_map *map; |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 1741 | |
| 1742 | if (!gelf_getsym(symbols, i, &sym)) |
| 1743 | continue; |
Wang Nan | 666810e | 2016-01-25 09:55:49 +0000 | [diff] [blame] | 1744 | if (sym.st_shndx != obj->efile.maps_shndx) |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 1745 | continue; |
| 1746 | |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1747 | map = bpf_object__add_map(obj); |
| 1748 | if (IS_ERR(map)) |
| 1749 | return PTR_ERR(map); |
| 1750 | |
| 1751 | map_name = elf_strptr(obj->efile.elf, obj->efile.strtabidx, |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 1752 | sym.st_name); |
Andrii Nakryiko | c51829b | 2019-05-29 10:36:06 -0700 | [diff] [blame] | 1753 | if (!map_name) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1754 | pr_warn("failed to get map #%d name sym string for obj %s\n", |
| 1755 | i, obj->path); |
Andrii Nakryiko | c51829b | 2019-05-29 10:36:06 -0700 | [diff] [blame] | 1756 | return -LIBBPF_ERRNO__FORMAT; |
| 1757 | } |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 1758 | |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1759 | map->libbpf_type = LIBBPF_MAP_UNSPEC; |
Andrii Nakryiko | db48814 | 2019-06-17 12:26:54 -0700 | [diff] [blame] | 1760 | map->sec_idx = sym.st_shndx; |
| 1761 | map->sec_offset = sym.st_value; |
| 1762 | pr_debug("map '%s' (legacy): at sec_idx %d, offset %zu.\n", |
| 1763 | map_name, map->sec_idx, map->sec_offset); |
Craig Gallek | b13c5c1 | 2017-10-05 10:41:57 -0400 | [diff] [blame] | 1764 | if (sym.st_value + map_def_sz > data->d_size) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1765 | pr_warn("corrupted maps section in %s: last map \"%s\" too small\n", |
| 1766 | obj->path, map_name); |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 1767 | return -EINVAL; |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 1768 | } |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 1769 | |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1770 | map->name = strdup(map_name); |
| 1771 | if (!map->name) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1772 | pr_warn("failed to alloc map name\n"); |
Wang Nan | 973170e | 2015-12-08 02:25:29 +0000 | [diff] [blame] | 1773 | return -ENOMEM; |
| 1774 | } |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1775 | pr_debug("map %d is \"%s\"\n", i, map->name); |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 1776 | def = (struct bpf_map_def *)(data->d_buf + sym.st_value); |
Craig Gallek | b13c5c1 | 2017-10-05 10:41:57 -0400 | [diff] [blame] | 1777 | /* |
| 1778 | * If the definition of the map in the object file fits in |
| 1779 | * bpf_map_def, copy it. Any extra fields in our version |
| 1780 | * of bpf_map_def will default to zero as a result of the |
| 1781 | * calloc above. |
| 1782 | */ |
| 1783 | if (map_def_sz <= sizeof(struct bpf_map_def)) { |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1784 | memcpy(&map->def, def, map_def_sz); |
Craig Gallek | b13c5c1 | 2017-10-05 10:41:57 -0400 | [diff] [blame] | 1785 | } else { |
| 1786 | /* |
| 1787 | * Here the map structure being read is bigger than what |
| 1788 | * we expect, truncate if the excess bits are all zero. |
| 1789 | * If they are not zero, reject this map as |
| 1790 | * incompatible. |
| 1791 | */ |
| 1792 | char *b; |
Andrii Nakryiko | 8983b73 | 2019-11-20 23:07:42 -0800 | [diff] [blame] | 1793 | |
Craig Gallek | b13c5c1 | 2017-10-05 10:41:57 -0400 | [diff] [blame] | 1794 | for (b = ((char *)def) + sizeof(struct bpf_map_def); |
| 1795 | b < ((char *)def) + map_def_sz; b++) { |
| 1796 | if (*b != 0) { |
Andrii Nakryiko | 8983b73 | 2019-11-20 23:07:42 -0800 | [diff] [blame] | 1797 | pr_warn("maps section in %s: \"%s\" has unrecognized, non-zero options\n", |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1798 | obj->path, map_name); |
John Fastabend | c034a17 | 2018-10-15 11:19:55 -0700 | [diff] [blame] | 1799 | if (strict) |
| 1800 | return -EINVAL; |
Craig Gallek | b13c5c1 | 2017-10-05 10:41:57 -0400 | [diff] [blame] | 1801 | } |
| 1802 | } |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1803 | memcpy(&map->def, def, sizeof(struct bpf_map_def)); |
Craig Gallek | b13c5c1 | 2017-10-05 10:41:57 -0400 | [diff] [blame] | 1804 | } |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 1805 | } |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1806 | return 0; |
| 1807 | } |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 1808 | |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 1809 | static const struct btf_type * |
| 1810 | skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id) |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1811 | { |
| 1812 | const struct btf_type *t = btf__type_by_id(btf, id); |
| 1813 | |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 1814 | if (res_id) |
| 1815 | *res_id = id; |
| 1816 | |
| 1817 | while (btf_is_mod(t) || btf_is_typedef(t)) { |
| 1818 | if (res_id) |
| 1819 | *res_id = t->type; |
| 1820 | t = btf__type_by_id(btf, t->type); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1821 | } |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 1822 | |
| 1823 | return t; |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1824 | } |
| 1825 | |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 1826 | static const struct btf_type * |
| 1827 | resolve_func_ptr(const struct btf *btf, __u32 id, __u32 *res_id) |
| 1828 | { |
| 1829 | const struct btf_type *t; |
| 1830 | |
| 1831 | t = skip_mods_and_typedefs(btf, id, NULL); |
| 1832 | if (!btf_is_ptr(t)) |
| 1833 | return NULL; |
| 1834 | |
| 1835 | t = skip_mods_and_typedefs(btf, t->type, res_id); |
| 1836 | |
| 1837 | return btf_is_func_proto(t) ? t : NULL; |
| 1838 | } |
| 1839 | |
Andrii Nakryiko | ef99b02 | 2019-07-05 08:50:09 -0700 | [diff] [blame] | 1840 | /* |
| 1841 | * Fetch integer attribute of BTF map definition. Such attributes are |
| 1842 | * represented using a pointer to an array, in which dimensionality of array |
| 1843 | * encodes specified integer value. E.g., int (*type)[BPF_MAP_TYPE_ARRAY]; |
| 1844 | * encodes `type => BPF_MAP_TYPE_ARRAY` key/value pair completely using BTF |
| 1845 | * type definition, while using only sizeof(void *) space in ELF data section. |
| 1846 | */ |
| 1847 | static bool get_map_field_int(const char *map_name, const struct btf *btf, |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1848 | const struct btf_type *def, |
Andrii Nakryiko | 8983b73 | 2019-11-20 23:07:42 -0800 | [diff] [blame] | 1849 | const struct btf_member *m, __u32 *res) |
| 1850 | { |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 1851 | const struct btf_type *t = skip_mods_and_typedefs(btf, m->type, NULL); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1852 | const char *name = btf__name_by_offset(btf, m->name_off); |
Andrii Nakryiko | ef99b02 | 2019-07-05 08:50:09 -0700 | [diff] [blame] | 1853 | const struct btf_array *arr_info; |
| 1854 | const struct btf_type *arr_t; |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1855 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1856 | if (!btf_is_ptr(t)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1857 | pr_warn("map '%s': attr '%s': expected PTR, got %u.\n", |
| 1858 | map_name, name, btf_kind(t)); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1859 | return false; |
| 1860 | } |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 1861 | |
Andrii Nakryiko | ef99b02 | 2019-07-05 08:50:09 -0700 | [diff] [blame] | 1862 | arr_t = btf__type_by_id(btf, t->type); |
| 1863 | if (!arr_t) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1864 | pr_warn("map '%s': attr '%s': type [%u] not found.\n", |
| 1865 | map_name, name, t->type); |
Andrii Nakryiko | ef99b02 | 2019-07-05 08:50:09 -0700 | [diff] [blame] | 1866 | return false; |
| 1867 | } |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1868 | if (!btf_is_array(arr_t)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1869 | pr_warn("map '%s': attr '%s': expected ARRAY, got %u.\n", |
| 1870 | map_name, name, btf_kind(arr_t)); |
Andrii Nakryiko | ef99b02 | 2019-07-05 08:50:09 -0700 | [diff] [blame] | 1871 | return false; |
| 1872 | } |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1873 | arr_info = btf_array(arr_t); |
Andrii Nakryiko | ef99b02 | 2019-07-05 08:50:09 -0700 | [diff] [blame] | 1874 | *res = arr_info->nelems; |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1875 | return true; |
| 1876 | } |
Daniel Borkmann | 8837fe5 | 2019-04-24 00:45:56 +0200 | [diff] [blame] | 1877 | |
Toke Høiland-Jørgensen | 57a00f4 | 2019-11-02 12:09:41 +0100 | [diff] [blame] | 1878 | static int build_map_pin_path(struct bpf_map *map, const char *path) |
| 1879 | { |
| 1880 | char buf[PATH_MAX]; |
| 1881 | int err, len; |
| 1882 | |
| 1883 | if (!path) |
| 1884 | path = "/sys/fs/bpf"; |
| 1885 | |
| 1886 | len = snprintf(buf, PATH_MAX, "%s/%s", path, bpf_map__name(map)); |
| 1887 | if (len < 0) |
| 1888 | return -EINVAL; |
| 1889 | else if (len >= PATH_MAX) |
| 1890 | return -ENAMETOOLONG; |
| 1891 | |
| 1892 | err = bpf_map__set_pin_path(map, buf); |
| 1893 | if (err) |
| 1894 | return err; |
| 1895 | |
| 1896 | return 0; |
| 1897 | } |
| 1898 | |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1899 | static int bpf_object__init_user_btf_map(struct bpf_object *obj, |
| 1900 | const struct btf_type *sec, |
| 1901 | int var_idx, int sec_idx, |
Toke Høiland-Jørgensen | 57a00f4 | 2019-11-02 12:09:41 +0100 | [diff] [blame] | 1902 | const Elf_Data *data, bool strict, |
| 1903 | const char *pin_root_path) |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1904 | { |
| 1905 | const struct btf_type *var, *def, *t; |
| 1906 | const struct btf_var_secinfo *vi; |
| 1907 | const struct btf_var *var_extra; |
| 1908 | const struct btf_member *m; |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1909 | const char *map_name; |
| 1910 | struct bpf_map *map; |
| 1911 | int vlen, i; |
| 1912 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1913 | vi = btf_var_secinfos(sec) + var_idx; |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1914 | var = btf__type_by_id(obj->btf, vi->type); |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1915 | var_extra = btf_var(var); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1916 | map_name = btf__name_by_offset(obj->btf, var->name_off); |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1917 | vlen = btf_vlen(var); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1918 | |
| 1919 | if (map_name == NULL || map_name[0] == '\0') { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1920 | pr_warn("map #%d: empty name.\n", var_idx); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1921 | return -EINVAL; |
| 1922 | } |
| 1923 | if ((__u64)vi->offset + vi->size > data->d_size) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1924 | pr_warn("map '%s' BTF data is corrupted.\n", map_name); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1925 | return -EINVAL; |
| 1926 | } |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1927 | if (!btf_is_var(var)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1928 | pr_warn("map '%s': unexpected var kind %u.\n", |
| 1929 | map_name, btf_kind(var)); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1930 | return -EINVAL; |
| 1931 | } |
| 1932 | if (var_extra->linkage != BTF_VAR_GLOBAL_ALLOCATED && |
| 1933 | var_extra->linkage != BTF_VAR_STATIC) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1934 | pr_warn("map '%s': unsupported var linkage %u.\n", |
| 1935 | map_name, var_extra->linkage); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1936 | return -EOPNOTSUPP; |
| 1937 | } |
| 1938 | |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 1939 | def = skip_mods_and_typedefs(obj->btf, var->type, NULL); |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1940 | if (!btf_is_struct(def)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1941 | pr_warn("map '%s': unexpected def kind %u.\n", |
| 1942 | map_name, btf_kind(var)); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1943 | return -EINVAL; |
| 1944 | } |
| 1945 | if (def->size > vi->size) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1946 | pr_warn("map '%s': invalid def size.\n", map_name); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1947 | return -EINVAL; |
| 1948 | } |
| 1949 | |
| 1950 | map = bpf_object__add_map(obj); |
| 1951 | if (IS_ERR(map)) |
| 1952 | return PTR_ERR(map); |
| 1953 | map->name = strdup(map_name); |
| 1954 | if (!map->name) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1955 | pr_warn("map '%s': failed to alloc map name.\n", map_name); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1956 | return -ENOMEM; |
| 1957 | } |
| 1958 | map->libbpf_type = LIBBPF_MAP_UNSPEC; |
| 1959 | map->def.type = BPF_MAP_TYPE_UNSPEC; |
| 1960 | map->sec_idx = sec_idx; |
| 1961 | map->sec_offset = vi->offset; |
| 1962 | pr_debug("map '%s': at sec_idx %d, offset %zu.\n", |
| 1963 | map_name, map->sec_idx, map->sec_offset); |
| 1964 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1965 | vlen = btf_vlen(def); |
| 1966 | m = btf_members(def); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1967 | for (i = 0; i < vlen; i++, m++) { |
| 1968 | const char *name = btf__name_by_offset(obj->btf, m->name_off); |
| 1969 | |
| 1970 | if (!name) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1971 | pr_warn("map '%s': invalid field #%d.\n", map_name, i); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1972 | return -EINVAL; |
| 1973 | } |
| 1974 | if (strcmp(name, "type") == 0) { |
| 1975 | if (!get_map_field_int(map_name, obj->btf, def, m, |
Andrii Nakryiko | ef99b02 | 2019-07-05 08:50:09 -0700 | [diff] [blame] | 1976 | &map->def.type)) |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1977 | return -EINVAL; |
| 1978 | pr_debug("map '%s': found type = %u.\n", |
| 1979 | map_name, map->def.type); |
| 1980 | } else if (strcmp(name, "max_entries") == 0) { |
| 1981 | if (!get_map_field_int(map_name, obj->btf, def, m, |
Andrii Nakryiko | ef99b02 | 2019-07-05 08:50:09 -0700 | [diff] [blame] | 1982 | &map->def.max_entries)) |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1983 | return -EINVAL; |
| 1984 | pr_debug("map '%s': found max_entries = %u.\n", |
| 1985 | map_name, map->def.max_entries); |
| 1986 | } else if (strcmp(name, "map_flags") == 0) { |
| 1987 | if (!get_map_field_int(map_name, obj->btf, def, m, |
Andrii Nakryiko | ef99b02 | 2019-07-05 08:50:09 -0700 | [diff] [blame] | 1988 | &map->def.map_flags)) |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1989 | return -EINVAL; |
| 1990 | pr_debug("map '%s': found map_flags = %u.\n", |
| 1991 | map_name, map->def.map_flags); |
| 1992 | } else if (strcmp(name, "key_size") == 0) { |
| 1993 | __u32 sz; |
| 1994 | |
| 1995 | if (!get_map_field_int(map_name, obj->btf, def, m, |
Andrii Nakryiko | ef99b02 | 2019-07-05 08:50:09 -0700 | [diff] [blame] | 1996 | &sz)) |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1997 | return -EINVAL; |
| 1998 | pr_debug("map '%s': found key_size = %u.\n", |
| 1999 | map_name, sz); |
| 2000 | if (map->def.key_size && map->def.key_size != sz) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2001 | pr_warn("map '%s': conflicting key size %u != %u.\n", |
| 2002 | map_name, map->def.key_size, sz); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2003 | return -EINVAL; |
| 2004 | } |
| 2005 | map->def.key_size = sz; |
| 2006 | } else if (strcmp(name, "key") == 0) { |
| 2007 | __s64 sz; |
| 2008 | |
| 2009 | t = btf__type_by_id(obj->btf, m->type); |
| 2010 | if (!t) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2011 | pr_warn("map '%s': key type [%d] not found.\n", |
| 2012 | map_name, m->type); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2013 | return -EINVAL; |
| 2014 | } |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2015 | if (!btf_is_ptr(t)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2016 | pr_warn("map '%s': key spec is not PTR: %u.\n", |
| 2017 | map_name, btf_kind(t)); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2018 | return -EINVAL; |
| 2019 | } |
| 2020 | sz = btf__resolve_size(obj->btf, t->type); |
| 2021 | if (sz < 0) { |
Andrii Nakryiko | 679152d | 2019-12-12 09:19:18 -0800 | [diff] [blame] | 2022 | pr_warn("map '%s': can't determine key size for type [%u]: %zd.\n", |
| 2023 | map_name, t->type, (ssize_t)sz); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2024 | return sz; |
| 2025 | } |
Andrii Nakryiko | 679152d | 2019-12-12 09:19:18 -0800 | [diff] [blame] | 2026 | pr_debug("map '%s': found key [%u], sz = %zd.\n", |
| 2027 | map_name, t->type, (ssize_t)sz); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2028 | if (map->def.key_size && map->def.key_size != sz) { |
Andrii Nakryiko | 679152d | 2019-12-12 09:19:18 -0800 | [diff] [blame] | 2029 | pr_warn("map '%s': conflicting key size %u != %zd.\n", |
| 2030 | map_name, map->def.key_size, (ssize_t)sz); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2031 | return -EINVAL; |
| 2032 | } |
| 2033 | map->def.key_size = sz; |
| 2034 | map->btf_key_type_id = t->type; |
| 2035 | } else if (strcmp(name, "value_size") == 0) { |
| 2036 | __u32 sz; |
| 2037 | |
| 2038 | if (!get_map_field_int(map_name, obj->btf, def, m, |
Andrii Nakryiko | ef99b02 | 2019-07-05 08:50:09 -0700 | [diff] [blame] | 2039 | &sz)) |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2040 | return -EINVAL; |
| 2041 | pr_debug("map '%s': found value_size = %u.\n", |
| 2042 | map_name, sz); |
| 2043 | if (map->def.value_size && map->def.value_size != sz) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2044 | pr_warn("map '%s': conflicting value size %u != %u.\n", |
| 2045 | map_name, map->def.value_size, sz); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2046 | return -EINVAL; |
| 2047 | } |
| 2048 | map->def.value_size = sz; |
| 2049 | } else if (strcmp(name, "value") == 0) { |
| 2050 | __s64 sz; |
| 2051 | |
| 2052 | t = btf__type_by_id(obj->btf, m->type); |
| 2053 | if (!t) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2054 | pr_warn("map '%s': value type [%d] not found.\n", |
| 2055 | map_name, m->type); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2056 | return -EINVAL; |
| 2057 | } |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2058 | if (!btf_is_ptr(t)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2059 | pr_warn("map '%s': value spec is not PTR: %u.\n", |
| 2060 | map_name, btf_kind(t)); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2061 | return -EINVAL; |
| 2062 | } |
| 2063 | sz = btf__resolve_size(obj->btf, t->type); |
| 2064 | if (sz < 0) { |
Andrii Nakryiko | 679152d | 2019-12-12 09:19:18 -0800 | [diff] [blame] | 2065 | pr_warn("map '%s': can't determine value size for type [%u]: %zd.\n", |
| 2066 | map_name, t->type, (ssize_t)sz); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2067 | return sz; |
| 2068 | } |
Andrii Nakryiko | 679152d | 2019-12-12 09:19:18 -0800 | [diff] [blame] | 2069 | pr_debug("map '%s': found value [%u], sz = %zd.\n", |
| 2070 | map_name, t->type, (ssize_t)sz); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2071 | if (map->def.value_size && map->def.value_size != sz) { |
Andrii Nakryiko | 679152d | 2019-12-12 09:19:18 -0800 | [diff] [blame] | 2072 | pr_warn("map '%s': conflicting value size %u != %zd.\n", |
| 2073 | map_name, map->def.value_size, (ssize_t)sz); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2074 | return -EINVAL; |
| 2075 | } |
| 2076 | map->def.value_size = sz; |
| 2077 | map->btf_value_type_id = t->type; |
Toke Høiland-Jørgensen | 57a00f4 | 2019-11-02 12:09:41 +0100 | [diff] [blame] | 2078 | } else if (strcmp(name, "pinning") == 0) { |
| 2079 | __u32 val; |
| 2080 | int err; |
| 2081 | |
| 2082 | if (!get_map_field_int(map_name, obj->btf, def, m, |
| 2083 | &val)) |
| 2084 | return -EINVAL; |
| 2085 | pr_debug("map '%s': found pinning = %u.\n", |
| 2086 | map_name, val); |
| 2087 | |
| 2088 | if (val != LIBBPF_PIN_NONE && |
| 2089 | val != LIBBPF_PIN_BY_NAME) { |
| 2090 | pr_warn("map '%s': invalid pinning value %u.\n", |
| 2091 | map_name, val); |
| 2092 | return -EINVAL; |
| 2093 | } |
| 2094 | if (val == LIBBPF_PIN_BY_NAME) { |
| 2095 | err = build_map_pin_path(map, pin_root_path); |
| 2096 | if (err) { |
| 2097 | pr_warn("map '%s': couldn't build pin path.\n", |
| 2098 | map_name); |
| 2099 | return err; |
| 2100 | } |
| 2101 | } |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2102 | } else { |
| 2103 | if (strict) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2104 | pr_warn("map '%s': unknown field '%s'.\n", |
| 2105 | map_name, name); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2106 | return -ENOTSUP; |
| 2107 | } |
| 2108 | pr_debug("map '%s': ignoring unknown field '%s'.\n", |
| 2109 | map_name, name); |
| 2110 | } |
| 2111 | } |
| 2112 | |
| 2113 | if (map->def.type == BPF_MAP_TYPE_UNSPEC) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2114 | pr_warn("map '%s': map type isn't specified.\n", map_name); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2115 | return -EINVAL; |
| 2116 | } |
| 2117 | |
| 2118 | return 0; |
| 2119 | } |
| 2120 | |
Toke Høiland-Jørgensen | 57a00f4 | 2019-11-02 12:09:41 +0100 | [diff] [blame] | 2121 | static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict, |
| 2122 | const char *pin_root_path) |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2123 | { |
| 2124 | const struct btf_type *sec = NULL; |
| 2125 | int nr_types, i, vlen, err; |
| 2126 | const struct btf_type *t; |
| 2127 | const char *name; |
| 2128 | Elf_Data *data; |
| 2129 | Elf_Scn *scn; |
| 2130 | |
| 2131 | if (obj->efile.btf_maps_shndx < 0) |
| 2132 | return 0; |
| 2133 | |
| 2134 | scn = elf_getscn(obj->efile.elf, obj->efile.btf_maps_shndx); |
| 2135 | if (scn) |
| 2136 | data = elf_getdata(scn, NULL); |
| 2137 | if (!scn || !data) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2138 | pr_warn("failed to get Elf_Data from map section %d (%s)\n", |
| 2139 | obj->efile.maps_shndx, MAPS_ELF_SEC); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2140 | return -EINVAL; |
| 2141 | } |
| 2142 | |
| 2143 | nr_types = btf__get_nr_types(obj->btf); |
| 2144 | for (i = 1; i <= nr_types; i++) { |
| 2145 | t = btf__type_by_id(obj->btf, i); |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2146 | if (!btf_is_datasec(t)) |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2147 | continue; |
| 2148 | name = btf__name_by_offset(obj->btf, t->name_off); |
| 2149 | if (strcmp(name, MAPS_ELF_SEC) == 0) { |
| 2150 | sec = t; |
| 2151 | break; |
| 2152 | } |
| 2153 | } |
| 2154 | |
| 2155 | if (!sec) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2156 | pr_warn("DATASEC '%s' not found.\n", MAPS_ELF_SEC); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2157 | return -ENOENT; |
| 2158 | } |
| 2159 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2160 | vlen = btf_vlen(sec); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2161 | for (i = 0; i < vlen; i++) { |
| 2162 | err = bpf_object__init_user_btf_map(obj, sec, i, |
| 2163 | obj->efile.btf_maps_shndx, |
Andrii Nakryiko | 8983b73 | 2019-11-20 23:07:42 -0800 | [diff] [blame] | 2164 | data, strict, |
| 2165 | pin_root_path); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2166 | if (err) |
| 2167 | return err; |
| 2168 | } |
| 2169 | |
| 2170 | return 0; |
| 2171 | } |
| 2172 | |
Andrii Nakryiko | 0d13bfc | 2019-12-13 17:43:25 -0800 | [diff] [blame] | 2173 | static int bpf_object__init_maps(struct bpf_object *obj, |
Andrii Nakryiko | 01af3bf | 2019-12-13 17:43:32 -0800 | [diff] [blame] | 2174 | const struct bpf_object_open_opts *opts) |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 2175 | { |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 2176 | const char *pin_root_path; |
| 2177 | bool strict; |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 2178 | int err; |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 2179 | |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 2180 | strict = !OPTS_GET(opts, relaxed_maps, false); |
| 2181 | pin_root_path = OPTS_GET(opts, pin_root_path, NULL); |
| 2182 | |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 2183 | err = bpf_object__init_user_maps(obj, strict); |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 2184 | err = err ?: bpf_object__init_user_btf_maps(obj, strict, pin_root_path); |
| 2185 | err = err ?: bpf_object__init_global_data_maps(obj); |
Andrii Nakryiko | 81bfdd0 | 2019-12-18 16:28:34 -0800 | [diff] [blame] | 2186 | err = err ?: bpf_object__init_kconfig_map(obj); |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 2187 | err = err ?: bpf_object__init_struct_ops_maps(obj); |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 2188 | if (err) |
| 2189 | return err; |
| 2190 | |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 2191 | return 0; |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
Jesper Dangaard Brouer | e3d91b0 | 2018-02-08 12:48:32 +0100 | [diff] [blame] | 2194 | static bool section_have_execinstr(struct bpf_object *obj, int idx) |
| 2195 | { |
| 2196 | Elf_Scn *scn; |
| 2197 | GElf_Shdr sh; |
| 2198 | |
| 2199 | scn = elf_getscn(obj->efile.elf, idx); |
| 2200 | if (!scn) |
| 2201 | return false; |
| 2202 | |
| 2203 | if (gelf_getshdr(scn, &sh) != &sh) |
| 2204 | return false; |
| 2205 | |
| 2206 | if (sh.sh_flags & SHF_EXECINSTR) |
| 2207 | return true; |
| 2208 | |
| 2209 | return false; |
| 2210 | } |
| 2211 | |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 2212 | static void bpf_object__sanitize_btf(struct bpf_object *obj) |
| 2213 | { |
Alexei Starovoitov | 2d3eb67 | 2020-01-09 22:41:19 -0800 | [diff] [blame] | 2214 | bool has_func_global = obj->caps.btf_func_global; |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 2215 | bool has_datasec = obj->caps.btf_datasec; |
| 2216 | bool has_func = obj->caps.btf_func; |
| 2217 | struct btf *btf = obj->btf; |
| 2218 | struct btf_type *t; |
| 2219 | int i, j, vlen; |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 2220 | |
Alexei Starovoitov | 2d3eb67 | 2020-01-09 22:41:19 -0800 | [diff] [blame] | 2221 | if (!obj->btf || (has_func && has_datasec && has_func_global)) |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 2222 | return; |
| 2223 | |
| 2224 | for (i = 1; i <= btf__get_nr_types(btf); i++) { |
| 2225 | t = (struct btf_type *)btf__type_by_id(btf, i); |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 2226 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2227 | if (!has_datasec && btf_is_var(t)) { |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 2228 | /* replace VAR with INT */ |
| 2229 | t->info = BTF_INFO_ENC(BTF_KIND_INT, 0, 0); |
Andrii Nakryiko | 1d4126c | 2019-07-19 12:46:03 -0700 | [diff] [blame] | 2230 | /* |
| 2231 | * using size = 1 is the safest choice, 4 will be too |
| 2232 | * big and cause kernel BTF validation failure if |
| 2233 | * original variable took less than 4 bytes |
| 2234 | */ |
| 2235 | t->size = 1; |
Jakub Kicinski | 708852d | 2019-08-13 16:24:57 -0700 | [diff] [blame] | 2236 | *(int *)(t + 1) = BTF_INT_ENC(0, 0, 8); |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2237 | } else if (!has_datasec && btf_is_datasec(t)) { |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 2238 | /* replace DATASEC with STRUCT */ |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2239 | const struct btf_var_secinfo *v = btf_var_secinfos(t); |
| 2240 | struct btf_member *m = btf_members(t); |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 2241 | struct btf_type *vt; |
| 2242 | char *name; |
| 2243 | |
| 2244 | name = (char *)btf__name_by_offset(btf, t->name_off); |
| 2245 | while (*name) { |
| 2246 | if (*name == '.') |
| 2247 | *name = '_'; |
| 2248 | name++; |
| 2249 | } |
| 2250 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2251 | vlen = btf_vlen(t); |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 2252 | t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, vlen); |
| 2253 | for (j = 0; j < vlen; j++, v++, m++) { |
| 2254 | /* order of field assignments is important */ |
| 2255 | m->offset = v->offset * 8; |
| 2256 | m->type = v->type; |
| 2257 | /* preserve variable name as member name */ |
| 2258 | vt = (void *)btf__type_by_id(btf, v->type); |
| 2259 | m->name_off = vt->name_off; |
| 2260 | } |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2261 | } else if (!has_func && btf_is_func_proto(t)) { |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 2262 | /* replace FUNC_PROTO with ENUM */ |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2263 | vlen = btf_vlen(t); |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 2264 | t->info = BTF_INFO_ENC(BTF_KIND_ENUM, 0, vlen); |
| 2265 | t->size = sizeof(__u32); /* kernel enforced */ |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 2266 | } else if (!has_func && btf_is_func(t)) { |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 2267 | /* replace FUNC with TYPEDEF */ |
| 2268 | t->info = BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0); |
Alexei Starovoitov | 2d3eb67 | 2020-01-09 22:41:19 -0800 | [diff] [blame] | 2269 | } else if (!has_func_global && btf_is_func(t)) { |
| 2270 | /* replace BTF_FUNC_GLOBAL with BTF_FUNC_STATIC */ |
| 2271 | t->info = BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0); |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 2272 | } |
| 2273 | } |
| 2274 | } |
| 2275 | |
| 2276 | static void bpf_object__sanitize_btf_ext(struct bpf_object *obj) |
| 2277 | { |
| 2278 | if (!obj->btf_ext) |
| 2279 | return; |
| 2280 | |
| 2281 | if (!obj->caps.btf_func) { |
| 2282 | btf_ext__free(obj->btf_ext); |
| 2283 | obj->btf_ext = NULL; |
| 2284 | } |
| 2285 | } |
| 2286 | |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2287 | static bool bpf_object__is_btf_mandatory(const struct bpf_object *obj) |
| 2288 | { |
Andrii Nakryiko | 5327644 | 2020-02-19 22:26:35 -0800 | [diff] [blame] | 2289 | return obj->efile.st_ops_shndx >= 0 || obj->nr_extern > 0; |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2290 | } |
| 2291 | |
Andrii Nakryiko | 063183b | 2019-06-17 12:26:55 -0700 | [diff] [blame] | 2292 | static int bpf_object__init_btf(struct bpf_object *obj, |
Andrii Nakryiko | 9c6660d | 2019-06-17 12:26:51 -0700 | [diff] [blame] | 2293 | Elf_Data *btf_data, |
| 2294 | Elf_Data *btf_ext_data) |
| 2295 | { |
Andrii Nakryiko | b7d7f3e | 2020-01-16 22:07:59 -0800 | [diff] [blame] | 2296 | int err = -ENOENT; |
Andrii Nakryiko | 9c6660d | 2019-06-17 12:26:51 -0700 | [diff] [blame] | 2297 | |
| 2298 | if (btf_data) { |
| 2299 | obj->btf = btf__new(btf_data->d_buf, btf_data->d_size); |
| 2300 | if (IS_ERR(obj->btf)) { |
Andrii Nakryiko | b7d7f3e | 2020-01-16 22:07:59 -0800 | [diff] [blame] | 2301 | err = PTR_ERR(obj->btf); |
| 2302 | obj->btf = NULL; |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2303 | pr_warn("Error loading ELF section %s: %d.\n", |
| 2304 | BTF_ELF_SEC, err); |
Andrii Nakryiko | 9c6660d | 2019-06-17 12:26:51 -0700 | [diff] [blame] | 2305 | goto out; |
| 2306 | } |
Andrii Nakryiko | b7d7f3e | 2020-01-16 22:07:59 -0800 | [diff] [blame] | 2307 | err = 0; |
Andrii Nakryiko | 9c6660d | 2019-06-17 12:26:51 -0700 | [diff] [blame] | 2308 | } |
| 2309 | if (btf_ext_data) { |
| 2310 | if (!obj->btf) { |
| 2311 | pr_debug("Ignore ELF section %s because its depending ELF section %s is not found.\n", |
| 2312 | BTF_EXT_ELF_SEC, BTF_ELF_SEC); |
| 2313 | goto out; |
| 2314 | } |
| 2315 | obj->btf_ext = btf_ext__new(btf_ext_data->d_buf, |
| 2316 | btf_ext_data->d_size); |
| 2317 | if (IS_ERR(obj->btf_ext)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2318 | pr_warn("Error loading ELF section %s: %ld. Ignored and continue.\n", |
| 2319 | BTF_EXT_ELF_SEC, PTR_ERR(obj->btf_ext)); |
Andrii Nakryiko | 9c6660d | 2019-06-17 12:26:51 -0700 | [diff] [blame] | 2320 | obj->btf_ext = NULL; |
| 2321 | goto out; |
| 2322 | } |
Andrii Nakryiko | 9c6660d | 2019-06-17 12:26:51 -0700 | [diff] [blame] | 2323 | } |
| 2324 | out: |
Andrii Nakryiko | b7d7f3e | 2020-01-16 22:07:59 -0800 | [diff] [blame] | 2325 | if (err && bpf_object__is_btf_mandatory(obj)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2326 | pr_warn("BTF is required, but is missing or corrupted.\n"); |
Andrii Nakryiko | b7d7f3e | 2020-01-16 22:07:59 -0800 | [diff] [blame] | 2327 | return err; |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2328 | } |
Andrii Nakryiko | 9c6660d | 2019-06-17 12:26:51 -0700 | [diff] [blame] | 2329 | return 0; |
| 2330 | } |
| 2331 | |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 2332 | static int bpf_object__finalize_btf(struct bpf_object *obj) |
| 2333 | { |
| 2334 | int err; |
| 2335 | |
| 2336 | if (!obj->btf) |
| 2337 | return 0; |
| 2338 | |
| 2339 | err = btf__finalize_data(obj, obj->btf); |
| 2340 | if (!err) |
| 2341 | return 0; |
| 2342 | |
| 2343 | pr_warn("Error finalizing %s: %d.\n", BTF_ELF_SEC, err); |
| 2344 | btf__free(obj->btf); |
| 2345 | obj->btf = NULL; |
| 2346 | btf_ext__free(obj->btf_ext); |
| 2347 | obj->btf_ext = NULL; |
| 2348 | |
| 2349 | if (bpf_object__is_btf_mandatory(obj)) { |
| 2350 | pr_warn("BTF is required, but is missing or corrupted.\n"); |
| 2351 | return -ENOENT; |
| 2352 | } |
| 2353 | return 0; |
| 2354 | } |
| 2355 | |
KP Singh | a6ed02c | 2020-01-17 22:28:25 +0100 | [diff] [blame] | 2356 | static inline bool libbpf_prog_needs_vmlinux_btf(struct bpf_program *prog) |
| 2357 | { |
| 2358 | if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) |
| 2359 | return true; |
| 2360 | |
| 2361 | /* BPF_PROG_TYPE_TRACING programs which do not attach to other programs |
| 2362 | * also need vmlinux BTF |
| 2363 | */ |
| 2364 | if (prog->type == BPF_PROG_TYPE_TRACING && !prog->attach_prog_fd) |
| 2365 | return true; |
| 2366 | |
| 2367 | return false; |
| 2368 | } |
| 2369 | |
| 2370 | static int bpf_object__load_vmlinux_btf(struct bpf_object *obj) |
| 2371 | { |
| 2372 | struct bpf_program *prog; |
| 2373 | int err; |
| 2374 | |
| 2375 | bpf_object__for_each_program(prog, obj) { |
| 2376 | if (libbpf_prog_needs_vmlinux_btf(prog)) { |
| 2377 | obj->btf_vmlinux = libbpf_find_kernel_btf(); |
| 2378 | if (IS_ERR(obj->btf_vmlinux)) { |
| 2379 | err = PTR_ERR(obj->btf_vmlinux); |
| 2380 | pr_warn("Error loading vmlinux BTF: %d\n", err); |
| 2381 | obj->btf_vmlinux = NULL; |
| 2382 | return err; |
| 2383 | } |
| 2384 | return 0; |
| 2385 | } |
| 2386 | } |
| 2387 | |
| 2388 | return 0; |
| 2389 | } |
| 2390 | |
Andrii Nakryiko | 063183b | 2019-06-17 12:26:55 -0700 | [diff] [blame] | 2391 | static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj) |
| 2392 | { |
| 2393 | int err = 0; |
| 2394 | |
| 2395 | if (!obj->btf) |
| 2396 | return 0; |
| 2397 | |
| 2398 | bpf_object__sanitize_btf(obj); |
| 2399 | bpf_object__sanitize_btf_ext(obj); |
| 2400 | |
| 2401 | err = btf__load(obj->btf); |
| 2402 | if (err) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2403 | pr_warn("Error loading %s into kernel: %d.\n", |
| 2404 | BTF_ELF_SEC, err); |
Andrii Nakryiko | 063183b | 2019-06-17 12:26:55 -0700 | [diff] [blame] | 2405 | btf__free(obj->btf); |
| 2406 | obj->btf = NULL; |
Andrii Nakryiko | 04efe59 | 2019-07-19 12:32:42 -0700 | [diff] [blame] | 2407 | /* btf_ext can't exist without btf, so free it as well */ |
| 2408 | if (obj->btf_ext) { |
| 2409 | btf_ext__free(obj->btf_ext); |
| 2410 | obj->btf_ext = NULL; |
| 2411 | } |
| 2412 | |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2413 | if (bpf_object__is_btf_mandatory(obj)) |
| 2414 | return err; |
Andrii Nakryiko | 063183b | 2019-06-17 12:26:55 -0700 | [diff] [blame] | 2415 | } |
| 2416 | return 0; |
| 2417 | } |
| 2418 | |
Andrii Nakryiko | 0d13bfc | 2019-12-13 17:43:25 -0800 | [diff] [blame] | 2419 | static int bpf_object__elf_collect(struct bpf_object *obj) |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 2420 | { |
| 2421 | Elf *elf = obj->efile.elf; |
| 2422 | GElf_Ehdr *ep = &obj->efile.ehdr; |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 2423 | Elf_Data *btf_ext_data = NULL; |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 2424 | Elf_Data *btf_data = NULL; |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 2425 | Elf_Scn *scn = NULL; |
Wang Nan | 666810e | 2016-01-25 09:55:49 +0000 | [diff] [blame] | 2426 | int idx = 0, err = 0; |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 2427 | |
| 2428 | /* Elf is corrupted/truncated, avoid calling elf_strptr. */ |
| 2429 | if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2430 | pr_warn("failed to get e_shstrndx from %s\n", obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 2431 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 2432 | } |
| 2433 | |
| 2434 | while ((scn = elf_nextscn(elf, scn)) != NULL) { |
| 2435 | char *name; |
| 2436 | GElf_Shdr sh; |
| 2437 | Elf_Data *data; |
| 2438 | |
| 2439 | idx++; |
| 2440 | if (gelf_getshdr(scn, &sh) != &sh) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2441 | pr_warn("failed to get section(%d) header from %s\n", |
| 2442 | idx, obj->path); |
Andrii Nakryiko | 01b29d1 | 2019-06-17 12:26:52 -0700 | [diff] [blame] | 2443 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 2444 | } |
| 2445 | |
| 2446 | name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name); |
| 2447 | if (!name) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2448 | pr_warn("failed to get section(%d) name from %s\n", |
| 2449 | idx, obj->path); |
Andrii Nakryiko | 01b29d1 | 2019-06-17 12:26:52 -0700 | [diff] [blame] | 2450 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 2451 | } |
| 2452 | |
| 2453 | data = elf_getdata(scn, 0); |
| 2454 | if (!data) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2455 | pr_warn("failed to get section(%d) data from %s(%s)\n", |
| 2456 | idx, name, obj->path); |
Andrii Nakryiko | 01b29d1 | 2019-06-17 12:26:52 -0700 | [diff] [blame] | 2457 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 2458 | } |
Jesper Dangaard Brouer | 077c066 | 2018-02-08 12:48:17 +0100 | [diff] [blame] | 2459 | pr_debug("section(%d) %s, size %ld, link %d, flags %lx, type=%d\n", |
| 2460 | idx, name, (unsigned long)data->d_size, |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 2461 | (int)sh.sh_link, (unsigned long)sh.sh_flags, |
| 2462 | (int)sh.sh_type); |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 2463 | |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 2464 | if (strcmp(name, "license") == 0) { |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 2465 | err = bpf_object__init_license(obj, |
| 2466 | data->d_buf, |
| 2467 | data->d_size); |
Andrii Nakryiko | 01b29d1 | 2019-06-17 12:26:52 -0700 | [diff] [blame] | 2468 | if (err) |
| 2469 | return err; |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 2470 | } else if (strcmp(name, "version") == 0) { |
John Fastabend | 54b8625 | 2019-10-18 07:41:26 -0700 | [diff] [blame] | 2471 | err = bpf_object__init_kversion(obj, |
| 2472 | data->d_buf, |
| 2473 | data->d_size); |
| 2474 | if (err) |
| 2475 | return err; |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 2476 | } else if (strcmp(name, "maps") == 0) { |
Wang Nan | 666810e | 2016-01-25 09:55:49 +0000 | [diff] [blame] | 2477 | obj->efile.maps_shndx = idx; |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2478 | } else if (strcmp(name, MAPS_ELF_SEC) == 0) { |
| 2479 | obj->efile.btf_maps_shndx = idx; |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 2480 | } else if (strcmp(name, BTF_ELF_SEC) == 0) { |
| 2481 | btf_data = data; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 2482 | } else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) { |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 2483 | btf_ext_data = data; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 2484 | } else if (sh.sh_type == SHT_SYMTAB) { |
Wang Nan | bec7d68 | 2015-07-01 02:13:59 +0000 | [diff] [blame] | 2485 | if (obj->efile.symbols) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2486 | pr_warn("bpf: multiple SYMTAB in %s\n", |
| 2487 | obj->path); |
Andrii Nakryiko | 01b29d1 | 2019-06-17 12:26:52 -0700 | [diff] [blame] | 2488 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 77ba9a5 | 2015-12-08 02:25:30 +0000 | [diff] [blame] | 2489 | } |
Andrii Nakryiko | 01b29d1 | 2019-06-17 12:26:52 -0700 | [diff] [blame] | 2490 | obj->efile.symbols = data; |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 2491 | obj->efile.symbols_shndx = idx; |
Andrii Nakryiko | 01b29d1 | 2019-06-17 12:26:52 -0700 | [diff] [blame] | 2492 | obj->efile.strtabidx = sh.sh_link; |
Joe Stringer | f8c7a4d | 2019-04-09 23:20:12 +0200 | [diff] [blame] | 2493 | } else if (sh.sh_type == SHT_PROGBITS && data->d_size > 0) { |
| 2494 | if (sh.sh_flags & SHF_EXECINSTR) { |
| 2495 | if (strcmp(name, ".text") == 0) |
| 2496 | obj->efile.text_shndx = idx; |
| 2497 | err = bpf_object__add_program(obj, data->d_buf, |
Andrii Nakryiko | 8983b73 | 2019-11-20 23:07:42 -0800 | [diff] [blame] | 2498 | data->d_size, |
| 2499 | name, idx); |
Joe Stringer | f8c7a4d | 2019-04-09 23:20:12 +0200 | [diff] [blame] | 2500 | if (err) { |
| 2501 | char errmsg[STRERR_BUFSIZE]; |
Andrii Nakryiko | 8983b73 | 2019-11-20 23:07:42 -0800 | [diff] [blame] | 2502 | char *cp; |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 2503 | |
Andrii Nakryiko | 8983b73 | 2019-11-20 23:07:42 -0800 | [diff] [blame] | 2504 | cp = libbpf_strerror_r(-err, errmsg, |
| 2505 | sizeof(errmsg)); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2506 | pr_warn("failed to alloc program %s (%s): %s", |
| 2507 | name, obj->path, cp); |
Andrii Nakryiko | 01b29d1 | 2019-06-17 12:26:52 -0700 | [diff] [blame] | 2508 | return err; |
Joe Stringer | f8c7a4d | 2019-04-09 23:20:12 +0200 | [diff] [blame] | 2509 | } |
Andrii Nakryiko | ac9d138 | 2019-12-13 17:47:07 -0800 | [diff] [blame] | 2510 | } else if (strcmp(name, DATA_SEC) == 0) { |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 2511 | obj->efile.data = data; |
| 2512 | obj->efile.data_shndx = idx; |
Andrii Nakryiko | ac9d138 | 2019-12-13 17:47:07 -0800 | [diff] [blame] | 2513 | } else if (strcmp(name, RODATA_SEC) == 0) { |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 2514 | obj->efile.rodata = data; |
| 2515 | obj->efile.rodata_shndx = idx; |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 2516 | } else if (strcmp(name, STRUCT_OPS_SEC) == 0) { |
| 2517 | obj->efile.st_ops_data = data; |
| 2518 | obj->efile.st_ops_shndx = idx; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 2519 | } else { |
| 2520 | pr_debug("skip section(%d) %s\n", idx, name); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 2521 | } |
Wang Nan | b62f06e | 2015-07-01 02:14:01 +0000 | [diff] [blame] | 2522 | } else if (sh.sh_type == SHT_REL) { |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 2523 | int nr_sects = obj->efile.nr_reloc_sects; |
| 2524 | void *sects = obj->efile.reloc_sects; |
Jesper Dangaard Brouer | e3d91b0 | 2018-02-08 12:48:32 +0100 | [diff] [blame] | 2525 | int sec = sh.sh_info; /* points to other section */ |
| 2526 | |
| 2527 | /* Only do relo for section with exec instructions */ |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 2528 | if (!section_have_execinstr(obj, sec) && |
| 2529 | strcmp(name, ".rel" STRUCT_OPS_SEC)) { |
Jesper Dangaard Brouer | e3d91b0 | 2018-02-08 12:48:32 +0100 | [diff] [blame] | 2530 | pr_debug("skip relo %s(%d) for section(%d)\n", |
| 2531 | name, idx, sec); |
| 2532 | continue; |
| 2533 | } |
Wang Nan | b62f06e | 2015-07-01 02:14:01 +0000 | [diff] [blame] | 2534 | |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 2535 | sects = reallocarray(sects, nr_sects + 1, |
| 2536 | sizeof(*obj->efile.reloc_sects)); |
| 2537 | if (!sects) { |
| 2538 | pr_warn("reloc_sects realloc failed\n"); |
Andrii Nakryiko | 01b29d1 | 2019-06-17 12:26:52 -0700 | [diff] [blame] | 2539 | return -ENOMEM; |
Wang Nan | b62f06e | 2015-07-01 02:14:01 +0000 | [diff] [blame] | 2540 | } |
Andrii Nakryiko | 01b29d1 | 2019-06-17 12:26:52 -0700 | [diff] [blame] | 2541 | |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 2542 | obj->efile.reloc_sects = sects; |
| 2543 | obj->efile.nr_reloc_sects++; |
Andrii Nakryiko | 01b29d1 | 2019-06-17 12:26:52 -0700 | [diff] [blame] | 2544 | |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 2545 | obj->efile.reloc_sects[nr_sects].shdr = sh; |
| 2546 | obj->efile.reloc_sects[nr_sects].data = data; |
Andrii Nakryiko | ac9d138 | 2019-12-13 17:47:07 -0800 | [diff] [blame] | 2547 | } else if (sh.sh_type == SHT_NOBITS && |
| 2548 | strcmp(name, BSS_SEC) == 0) { |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 2549 | obj->efile.bss = data; |
| 2550 | obj->efile.bss_shndx = idx; |
Jesper Dangaard Brouer | 077c066 | 2018-02-08 12:48:17 +0100 | [diff] [blame] | 2551 | } else { |
| 2552 | pr_debug("skip section(%d) %s\n", idx, name); |
Wang Nan | bec7d68 | 2015-07-01 02:13:59 +0000 | [diff] [blame] | 2553 | } |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 2554 | } |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 2555 | |
Andrii Nakryiko | d3a3aa0 | 2019-10-28 16:37:27 -0700 | [diff] [blame] | 2556 | if (!obj->efile.strtabidx || obj->efile.strtabidx > idx) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2557 | pr_warn("Corrupted ELF file: index of strtab invalid\n"); |
Andrii Nakryiko | f102154 | 2019-05-29 10:36:07 -0700 | [diff] [blame] | 2558 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 77ba9a5 | 2015-12-08 02:25:30 +0000 | [diff] [blame] | 2559 | } |
Andrii Nakryiko | 0d13bfc | 2019-12-13 17:43:25 -0800 | [diff] [blame] | 2560 | return bpf_object__init_btf(obj, btf_data, btf_ext_data); |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 2561 | } |
| 2562 | |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 2563 | static bool sym_is_extern(const GElf_Sym *sym) |
| 2564 | { |
| 2565 | int bind = GELF_ST_BIND(sym->st_info); |
| 2566 | /* externs are symbols w/ type=NOTYPE, bind=GLOBAL|WEAK, section=UND */ |
| 2567 | return sym->st_shndx == SHN_UNDEF && |
| 2568 | (bind == STB_GLOBAL || bind == STB_WEAK) && |
| 2569 | GELF_ST_TYPE(sym->st_info) == STT_NOTYPE; |
| 2570 | } |
| 2571 | |
| 2572 | static int find_extern_btf_id(const struct btf *btf, const char *ext_name) |
| 2573 | { |
| 2574 | const struct btf_type *t; |
| 2575 | const char *var_name; |
| 2576 | int i, n; |
| 2577 | |
| 2578 | if (!btf) |
| 2579 | return -ESRCH; |
| 2580 | |
| 2581 | n = btf__get_nr_types(btf); |
| 2582 | for (i = 1; i <= n; i++) { |
| 2583 | t = btf__type_by_id(btf, i); |
| 2584 | |
| 2585 | if (!btf_is_var(t)) |
| 2586 | continue; |
| 2587 | |
| 2588 | var_name = btf__name_by_offset(btf, t->name_off); |
| 2589 | if (strcmp(var_name, ext_name)) |
| 2590 | continue; |
| 2591 | |
| 2592 | if (btf_var(t)->linkage != BTF_VAR_GLOBAL_EXTERN) |
| 2593 | return -EINVAL; |
| 2594 | |
| 2595 | return i; |
| 2596 | } |
| 2597 | |
| 2598 | return -ENOENT; |
| 2599 | } |
| 2600 | |
| 2601 | static enum extern_type find_extern_type(const struct btf *btf, int id, |
| 2602 | bool *is_signed) |
| 2603 | { |
| 2604 | const struct btf_type *t; |
| 2605 | const char *name; |
| 2606 | |
| 2607 | t = skip_mods_and_typedefs(btf, id, NULL); |
| 2608 | name = btf__name_by_offset(btf, t->name_off); |
| 2609 | |
| 2610 | if (is_signed) |
| 2611 | *is_signed = false; |
| 2612 | switch (btf_kind(t)) { |
| 2613 | case BTF_KIND_INT: { |
| 2614 | int enc = btf_int_encoding(t); |
| 2615 | |
| 2616 | if (enc & BTF_INT_BOOL) |
| 2617 | return t->size == 1 ? EXT_BOOL : EXT_UNKNOWN; |
| 2618 | if (is_signed) |
| 2619 | *is_signed = enc & BTF_INT_SIGNED; |
| 2620 | if (t->size == 1) |
| 2621 | return EXT_CHAR; |
| 2622 | if (t->size < 1 || t->size > 8 || (t->size & (t->size - 1))) |
| 2623 | return EXT_UNKNOWN; |
| 2624 | return EXT_INT; |
| 2625 | } |
| 2626 | case BTF_KIND_ENUM: |
| 2627 | if (t->size != 4) |
| 2628 | return EXT_UNKNOWN; |
| 2629 | if (strcmp(name, "libbpf_tristate")) |
| 2630 | return EXT_UNKNOWN; |
| 2631 | return EXT_TRISTATE; |
| 2632 | case BTF_KIND_ARRAY: |
| 2633 | if (btf_array(t)->nelems == 0) |
| 2634 | return EXT_UNKNOWN; |
| 2635 | if (find_extern_type(btf, btf_array(t)->type, NULL) != EXT_CHAR) |
| 2636 | return EXT_UNKNOWN; |
| 2637 | return EXT_CHAR_ARR; |
| 2638 | default: |
| 2639 | return EXT_UNKNOWN; |
| 2640 | } |
| 2641 | } |
| 2642 | |
| 2643 | static int cmp_externs(const void *_a, const void *_b) |
| 2644 | { |
| 2645 | const struct extern_desc *a = _a; |
| 2646 | const struct extern_desc *b = _b; |
| 2647 | |
| 2648 | /* descending order by alignment requirements */ |
| 2649 | if (a->align != b->align) |
| 2650 | return a->align > b->align ? -1 : 1; |
| 2651 | /* ascending order by size, within same alignment class */ |
| 2652 | if (a->sz != b->sz) |
| 2653 | return a->sz < b->sz ? -1 : 1; |
| 2654 | /* resolve ties by name */ |
| 2655 | return strcmp(a->name, b->name); |
| 2656 | } |
| 2657 | |
| 2658 | static int bpf_object__collect_externs(struct bpf_object *obj) |
| 2659 | { |
| 2660 | const struct btf_type *t; |
| 2661 | struct extern_desc *ext; |
| 2662 | int i, n, off, btf_id; |
| 2663 | struct btf_type *sec; |
| 2664 | const char *ext_name; |
| 2665 | Elf_Scn *scn; |
| 2666 | GElf_Shdr sh; |
| 2667 | |
| 2668 | if (!obj->efile.symbols) |
| 2669 | return 0; |
| 2670 | |
| 2671 | scn = elf_getscn(obj->efile.elf, obj->efile.symbols_shndx); |
| 2672 | if (!scn) |
| 2673 | return -LIBBPF_ERRNO__FORMAT; |
| 2674 | if (gelf_getshdr(scn, &sh) != &sh) |
| 2675 | return -LIBBPF_ERRNO__FORMAT; |
| 2676 | n = sh.sh_size / sh.sh_entsize; |
| 2677 | |
| 2678 | pr_debug("looking for externs among %d symbols...\n", n); |
| 2679 | for (i = 0; i < n; i++) { |
| 2680 | GElf_Sym sym; |
| 2681 | |
| 2682 | if (!gelf_getsym(obj->efile.symbols, i, &sym)) |
| 2683 | return -LIBBPF_ERRNO__FORMAT; |
| 2684 | if (!sym_is_extern(&sym)) |
| 2685 | continue; |
| 2686 | ext_name = elf_strptr(obj->efile.elf, obj->efile.strtabidx, |
| 2687 | sym.st_name); |
| 2688 | if (!ext_name || !ext_name[0]) |
| 2689 | continue; |
| 2690 | |
| 2691 | ext = obj->externs; |
| 2692 | ext = reallocarray(ext, obj->nr_extern + 1, sizeof(*ext)); |
| 2693 | if (!ext) |
| 2694 | return -ENOMEM; |
| 2695 | obj->externs = ext; |
| 2696 | ext = &ext[obj->nr_extern]; |
| 2697 | memset(ext, 0, sizeof(*ext)); |
| 2698 | obj->nr_extern++; |
| 2699 | |
| 2700 | ext->btf_id = find_extern_btf_id(obj->btf, ext_name); |
| 2701 | if (ext->btf_id <= 0) { |
| 2702 | pr_warn("failed to find BTF for extern '%s': %d\n", |
| 2703 | ext_name, ext->btf_id); |
| 2704 | return ext->btf_id; |
| 2705 | } |
| 2706 | t = btf__type_by_id(obj->btf, ext->btf_id); |
| 2707 | ext->name = btf__name_by_offset(obj->btf, t->name_off); |
| 2708 | ext->sym_idx = i; |
| 2709 | ext->is_weak = GELF_ST_BIND(sym.st_info) == STB_WEAK; |
| 2710 | ext->sz = btf__resolve_size(obj->btf, t->type); |
| 2711 | if (ext->sz <= 0) { |
| 2712 | pr_warn("failed to resolve size of extern '%s': %d\n", |
| 2713 | ext_name, ext->sz); |
| 2714 | return ext->sz; |
| 2715 | } |
| 2716 | ext->align = btf__align_of(obj->btf, t->type); |
| 2717 | if (ext->align <= 0) { |
| 2718 | pr_warn("failed to determine alignment of extern '%s': %d\n", |
| 2719 | ext_name, ext->align); |
| 2720 | return -EINVAL; |
| 2721 | } |
| 2722 | ext->type = find_extern_type(obj->btf, t->type, |
| 2723 | &ext->is_signed); |
| 2724 | if (ext->type == EXT_UNKNOWN) { |
| 2725 | pr_warn("extern '%s' type is unsupported\n", ext_name); |
| 2726 | return -ENOTSUP; |
| 2727 | } |
| 2728 | } |
| 2729 | pr_debug("collected %d externs total\n", obj->nr_extern); |
| 2730 | |
| 2731 | if (!obj->nr_extern) |
| 2732 | return 0; |
| 2733 | |
| 2734 | /* sort externs by (alignment, size, name) and calculate their offsets |
| 2735 | * within a map */ |
| 2736 | qsort(obj->externs, obj->nr_extern, sizeof(*ext), cmp_externs); |
| 2737 | off = 0; |
| 2738 | for (i = 0; i < obj->nr_extern; i++) { |
| 2739 | ext = &obj->externs[i]; |
| 2740 | ext->data_off = roundup(off, ext->align); |
| 2741 | off = ext->data_off + ext->sz; |
| 2742 | pr_debug("extern #%d: symbol %d, off %u, name %s\n", |
| 2743 | i, ext->sym_idx, ext->data_off, ext->name); |
| 2744 | } |
| 2745 | |
Andrii Nakryiko | 81bfdd0 | 2019-12-18 16:28:34 -0800 | [diff] [blame] | 2746 | btf_id = btf__find_by_name(obj->btf, KCONFIG_SEC); |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 2747 | if (btf_id <= 0) { |
Andrii Nakryiko | 81bfdd0 | 2019-12-18 16:28:34 -0800 | [diff] [blame] | 2748 | pr_warn("no BTF info found for '%s' datasec\n", KCONFIG_SEC); |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 2749 | return -ESRCH; |
| 2750 | } |
| 2751 | |
| 2752 | sec = (struct btf_type *)btf__type_by_id(obj->btf, btf_id); |
| 2753 | sec->size = off; |
| 2754 | n = btf_vlen(sec); |
| 2755 | for (i = 0; i < n; i++) { |
| 2756 | struct btf_var_secinfo *vs = btf_var_secinfos(sec) + i; |
| 2757 | |
| 2758 | t = btf__type_by_id(obj->btf, vs->type); |
| 2759 | ext_name = btf__name_by_offset(obj->btf, t->name_off); |
| 2760 | ext = find_extern_by_name(obj, ext_name); |
| 2761 | if (!ext) { |
| 2762 | pr_warn("failed to find extern definition for BTF var '%s'\n", |
| 2763 | ext_name); |
| 2764 | return -ESRCH; |
| 2765 | } |
| 2766 | vs->offset = ext->data_off; |
| 2767 | btf_var(t)->linkage = BTF_VAR_GLOBAL_ALLOCATED; |
| 2768 | } |
| 2769 | |
| 2770 | return 0; |
| 2771 | } |
| 2772 | |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 2773 | static struct bpf_program * |
| 2774 | bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx) |
| 2775 | { |
| 2776 | struct bpf_program *prog; |
| 2777 | size_t i; |
| 2778 | |
| 2779 | for (i = 0; i < obj->nr_programs; i++) { |
| 2780 | prog = &obj->programs[i]; |
| 2781 | if (prog->idx == idx) |
| 2782 | return prog; |
| 2783 | } |
| 2784 | return NULL; |
| 2785 | } |
| 2786 | |
Jakub Kicinski | 6d4b198 | 2018-07-26 14:32:19 -0700 | [diff] [blame] | 2787 | struct bpf_program * |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 2788 | bpf_object__find_program_by_title(const struct bpf_object *obj, |
| 2789 | const char *title) |
Jakub Kicinski | 6d4b198 | 2018-07-26 14:32:19 -0700 | [diff] [blame] | 2790 | { |
| 2791 | struct bpf_program *pos; |
| 2792 | |
| 2793 | bpf_object__for_each_program(pos, obj) { |
| 2794 | if (pos->section_name && !strcmp(pos->section_name, title)) |
| 2795 | return pos; |
| 2796 | } |
| 2797 | return NULL; |
| 2798 | } |
| 2799 | |
Andrii Nakryiko | 01af3bf | 2019-12-13 17:43:32 -0800 | [diff] [blame] | 2800 | struct bpf_program * |
| 2801 | bpf_object__find_program_by_name(const struct bpf_object *obj, |
| 2802 | const char *name) |
| 2803 | { |
| 2804 | struct bpf_program *prog; |
| 2805 | |
| 2806 | bpf_object__for_each_program(prog, obj) { |
| 2807 | if (!strcmp(prog->name, name)) |
| 2808 | return prog; |
| 2809 | } |
| 2810 | return NULL; |
| 2811 | } |
| 2812 | |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 2813 | static bool bpf_object__shndx_is_data(const struct bpf_object *obj, |
| 2814 | int shndx) |
| 2815 | { |
| 2816 | return shndx == obj->efile.data_shndx || |
| 2817 | shndx == obj->efile.bss_shndx || |
| 2818 | shndx == obj->efile.rodata_shndx; |
| 2819 | } |
| 2820 | |
| 2821 | static bool bpf_object__shndx_is_maps(const struct bpf_object *obj, |
| 2822 | int shndx) |
| 2823 | { |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2824 | return shndx == obj->efile.maps_shndx || |
| 2825 | shndx == obj->efile.btf_maps_shndx; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 2826 | } |
| 2827 | |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 2828 | static enum libbpf_map_type |
| 2829 | bpf_object__section_to_libbpf_map_type(const struct bpf_object *obj, int shndx) |
| 2830 | { |
| 2831 | if (shndx == obj->efile.data_shndx) |
| 2832 | return LIBBPF_MAP_DATA; |
| 2833 | else if (shndx == obj->efile.bss_shndx) |
| 2834 | return LIBBPF_MAP_BSS; |
| 2835 | else if (shndx == obj->efile.rodata_shndx) |
| 2836 | return LIBBPF_MAP_RODATA; |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 2837 | else if (shndx == obj->efile.symbols_shndx) |
Andrii Nakryiko | 81bfdd0 | 2019-12-18 16:28:34 -0800 | [diff] [blame] | 2838 | return LIBBPF_MAP_KCONFIG; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 2839 | else |
| 2840 | return LIBBPF_MAP_UNSPEC; |
| 2841 | } |
| 2842 | |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 2843 | static int bpf_program__record_reloc(struct bpf_program *prog, |
| 2844 | struct reloc_desc *reloc_desc, |
| 2845 | __u32 insn_idx, const char *name, |
| 2846 | const GElf_Sym *sym, const GElf_Rel *rel) |
| 2847 | { |
| 2848 | struct bpf_insn *insn = &prog->insns[insn_idx]; |
| 2849 | size_t map_idx, nr_maps = prog->obj->nr_maps; |
| 2850 | struct bpf_object *obj = prog->obj; |
| 2851 | __u32 shdr_idx = sym->st_shndx; |
| 2852 | enum libbpf_map_type type; |
| 2853 | struct bpf_map *map; |
| 2854 | |
| 2855 | /* sub-program call relocation */ |
| 2856 | if (insn->code == (BPF_JMP | BPF_CALL)) { |
| 2857 | if (insn->src_reg != BPF_PSEUDO_CALL) { |
| 2858 | pr_warn("incorrect bpf_call opcode\n"); |
| 2859 | return -LIBBPF_ERRNO__RELOC; |
| 2860 | } |
| 2861 | /* text_shndx can be 0, if no default "main" program exists */ |
| 2862 | if (!shdr_idx || shdr_idx != obj->efile.text_shndx) { |
| 2863 | pr_warn("bad call relo against section %u\n", shdr_idx); |
| 2864 | return -LIBBPF_ERRNO__RELOC; |
| 2865 | } |
| 2866 | if (sym->st_value % 8) { |
Andrii Nakryiko | 679152d | 2019-12-12 09:19:18 -0800 | [diff] [blame] | 2867 | pr_warn("bad call relo offset: %zu\n", |
| 2868 | (size_t)sym->st_value); |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 2869 | return -LIBBPF_ERRNO__RELOC; |
| 2870 | } |
| 2871 | reloc_desc->type = RELO_CALL; |
| 2872 | reloc_desc->insn_idx = insn_idx; |
Andrii Nakryiko | 53f8dd4 | 2019-11-27 12:06:50 -0800 | [diff] [blame] | 2873 | reloc_desc->sym_off = sym->st_value; |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 2874 | obj->has_pseudo_calls = true; |
| 2875 | return 0; |
| 2876 | } |
| 2877 | |
| 2878 | if (insn->code != (BPF_LD | BPF_IMM | BPF_DW)) { |
Andrii Nakryiko | 8983b73 | 2019-11-20 23:07:42 -0800 | [diff] [blame] | 2879 | pr_warn("invalid relo for insns[%d].code 0x%x\n", |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 2880 | insn_idx, insn->code); |
| 2881 | return -LIBBPF_ERRNO__RELOC; |
| 2882 | } |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 2883 | |
| 2884 | if (sym_is_extern(sym)) { |
| 2885 | int sym_idx = GELF_R_SYM(rel->r_info); |
| 2886 | int i, n = obj->nr_extern; |
| 2887 | struct extern_desc *ext; |
| 2888 | |
| 2889 | for (i = 0; i < n; i++) { |
| 2890 | ext = &obj->externs[i]; |
| 2891 | if (ext->sym_idx == sym_idx) |
| 2892 | break; |
| 2893 | } |
| 2894 | if (i >= n) { |
| 2895 | pr_warn("extern relo failed to find extern for sym %d\n", |
| 2896 | sym_idx); |
| 2897 | return -LIBBPF_ERRNO__RELOC; |
| 2898 | } |
| 2899 | pr_debug("found extern #%d '%s' (sym %d, off %u) for insn %u\n", |
| 2900 | i, ext->name, ext->sym_idx, ext->data_off, insn_idx); |
| 2901 | reloc_desc->type = RELO_EXTERN; |
| 2902 | reloc_desc->insn_idx = insn_idx; |
| 2903 | reloc_desc->sym_off = ext->data_off; |
| 2904 | return 0; |
| 2905 | } |
| 2906 | |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 2907 | if (!shdr_idx || shdr_idx >= SHN_LORESERVE) { |
Andrii Nakryiko | 393cdfb | 2019-11-20 23:07:43 -0800 | [diff] [blame] | 2908 | pr_warn("invalid relo for \'%s\' in special section 0x%x; forgot to initialize global var?..\n", |
| 2909 | name, shdr_idx); |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 2910 | return -LIBBPF_ERRNO__RELOC; |
| 2911 | } |
| 2912 | |
| 2913 | type = bpf_object__section_to_libbpf_map_type(obj, shdr_idx); |
| 2914 | |
| 2915 | /* generic map reference relocation */ |
| 2916 | if (type == LIBBPF_MAP_UNSPEC) { |
| 2917 | if (!bpf_object__shndx_is_maps(obj, shdr_idx)) { |
| 2918 | pr_warn("bad map relo against section %u\n", |
| 2919 | shdr_idx); |
| 2920 | return -LIBBPF_ERRNO__RELOC; |
| 2921 | } |
| 2922 | for (map_idx = 0; map_idx < nr_maps; map_idx++) { |
| 2923 | map = &obj->maps[map_idx]; |
| 2924 | if (map->libbpf_type != type || |
| 2925 | map->sec_idx != sym->st_shndx || |
| 2926 | map->sec_offset != sym->st_value) |
| 2927 | continue; |
| 2928 | pr_debug("found map %zd (%s, sec %d, off %zu) for insn %u\n", |
| 2929 | map_idx, map->name, map->sec_idx, |
| 2930 | map->sec_offset, insn_idx); |
| 2931 | break; |
| 2932 | } |
| 2933 | if (map_idx >= nr_maps) { |
Andrii Nakryiko | 679152d | 2019-12-12 09:19:18 -0800 | [diff] [blame] | 2934 | pr_warn("map relo failed to find map for sec %u, off %zu\n", |
| 2935 | shdr_idx, (size_t)sym->st_value); |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 2936 | return -LIBBPF_ERRNO__RELOC; |
| 2937 | } |
| 2938 | reloc_desc->type = RELO_LD64; |
| 2939 | reloc_desc->insn_idx = insn_idx; |
| 2940 | reloc_desc->map_idx = map_idx; |
Andrii Nakryiko | 53f8dd4 | 2019-11-27 12:06:50 -0800 | [diff] [blame] | 2941 | reloc_desc->sym_off = 0; /* sym->st_value determines map_idx */ |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 2942 | return 0; |
| 2943 | } |
| 2944 | |
| 2945 | /* global data map relocation */ |
| 2946 | if (!bpf_object__shndx_is_data(obj, shdr_idx)) { |
| 2947 | pr_warn("bad data relo against section %u\n", shdr_idx); |
| 2948 | return -LIBBPF_ERRNO__RELOC; |
| 2949 | } |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 2950 | for (map_idx = 0; map_idx < nr_maps; map_idx++) { |
| 2951 | map = &obj->maps[map_idx]; |
| 2952 | if (map->libbpf_type != type) |
| 2953 | continue; |
| 2954 | pr_debug("found data map %zd (%s, sec %d, off %zu) for insn %u\n", |
| 2955 | map_idx, map->name, map->sec_idx, map->sec_offset, |
| 2956 | insn_idx); |
| 2957 | break; |
| 2958 | } |
| 2959 | if (map_idx >= nr_maps) { |
| 2960 | pr_warn("data relo failed to find map for sec %u\n", |
| 2961 | shdr_idx); |
| 2962 | return -LIBBPF_ERRNO__RELOC; |
| 2963 | } |
| 2964 | |
| 2965 | reloc_desc->type = RELO_DATA; |
| 2966 | reloc_desc->insn_idx = insn_idx; |
| 2967 | reloc_desc->map_idx = map_idx; |
Andrii Nakryiko | 53f8dd4 | 2019-11-27 12:06:50 -0800 | [diff] [blame] | 2968 | reloc_desc->sym_off = sym->st_value; |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 2969 | return 0; |
| 2970 | } |
| 2971 | |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 2972 | static int |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 2973 | bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr, |
| 2974 | Elf_Data *data, struct bpf_object *obj) |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 2975 | { |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 2976 | Elf_Data *symbols = obj->efile.symbols; |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 2977 | int err, i, nrels; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 2978 | |
Andrii Nakryiko | 399dc65 | 2019-05-29 10:36:11 -0700 | [diff] [blame] | 2979 | pr_debug("collecting relocating info for: '%s'\n", prog->section_name); |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 2980 | nrels = shdr->sh_size / shdr->sh_entsize; |
| 2981 | |
| 2982 | prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels); |
| 2983 | if (!prog->reloc_desc) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2984 | pr_warn("failed to alloc memory in relocation\n"); |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 2985 | return -ENOMEM; |
| 2986 | } |
| 2987 | prog->nr_reloc = nrels; |
| 2988 | |
| 2989 | for (i = 0; i < nrels; i++) { |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 2990 | const char *name; |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 2991 | __u32 insn_idx; |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2992 | GElf_Sym sym; |
| 2993 | GElf_Rel rel; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 2994 | |
| 2995 | if (!gelf_getrel(data, i, &rel)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2996 | pr_warn("relocation: failed to get %d reloc\n", i); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 2997 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 2998 | } |
Andrii Nakryiko | 399dc65 | 2019-05-29 10:36:11 -0700 | [diff] [blame] | 2999 | if (!gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3000 | pr_warn("relocation: symbol %"PRIx64" not found\n", |
| 3001 | GELF_R_SYM(rel.r_info)); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 3002 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 3003 | } |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 3004 | if (rel.r_offset % sizeof(struct bpf_insn)) |
| 3005 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 3006 | |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 3007 | insn_idx = rel.r_offset / sizeof(struct bpf_insn); |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 3008 | name = elf_strptr(obj->efile.elf, obj->efile.strtabidx, |
| 3009 | sym.st_name) ? : "<?>"; |
| 3010 | |
Andrii Nakryiko | 679152d | 2019-12-12 09:19:18 -0800 | [diff] [blame] | 3011 | pr_debug("relo for shdr %u, symb %zu, value %zu, type %d, bind %d, name %d (\'%s\'), insn %u\n", |
| 3012 | (__u32)sym.st_shndx, (size_t)GELF_R_SYM(rel.r_info), |
| 3013 | (size_t)sym.st_value, GELF_ST_TYPE(sym.st_info), |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 3014 | GELF_ST_BIND(sym.st_info), sym.st_name, name, |
| 3015 | insn_idx); |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 3016 | |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 3017 | err = bpf_program__record_reloc(prog, &prog->reloc_desc[i], |
| 3018 | insn_idx, name, &sym, &rel); |
| 3019 | if (err) |
| 3020 | return err; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 3021 | } |
| 3022 | return 0; |
| 3023 | } |
| 3024 | |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 3025 | static int bpf_map_find_btf_info(struct bpf_object *obj, struct bpf_map *map) |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 3026 | { |
| 3027 | struct bpf_map_def *def = &map->def; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 3028 | __u32 key_type_id = 0, value_type_id = 0; |
Yonghong Song | 96408c4 | 2019-02-04 11:00:58 -0800 | [diff] [blame] | 3029 | int ret; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 3030 | |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 3031 | /* if it's BTF-defined map, we don't need to search for type IDs. |
| 3032 | * For struct_ops map, it does not need btf_key_type_id and |
| 3033 | * btf_value_type_id. |
| 3034 | */ |
| 3035 | if (map->sec_idx == obj->efile.btf_maps_shndx || |
| 3036 | bpf_map__is_struct_ops(map)) |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 3037 | return 0; |
| 3038 | |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 3039 | if (!bpf_map__is_internal(map)) { |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 3040 | ret = btf__get_map_kv_tids(obj->btf, map->name, def->key_size, |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 3041 | def->value_size, &key_type_id, |
| 3042 | &value_type_id); |
| 3043 | } else { |
| 3044 | /* |
| 3045 | * LLVM annotates global data differently in BTF, that is, |
| 3046 | * only as '.data', '.bss' or '.rodata'. |
| 3047 | */ |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 3048 | ret = btf__find_by_name(obj->btf, |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 3049 | libbpf_type_to_btf_name[map->libbpf_type]); |
| 3050 | } |
| 3051 | if (ret < 0) |
Yonghong Song | 96408c4 | 2019-02-04 11:00:58 -0800 | [diff] [blame] | 3052 | return ret; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 3053 | |
Yonghong Song | 96408c4 | 2019-02-04 11:00:58 -0800 | [diff] [blame] | 3054 | map->btf_key_type_id = key_type_id; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 3055 | map->btf_value_type_id = bpf_map__is_internal(map) ? |
| 3056 | ret : value_type_id; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 3057 | return 0; |
| 3058 | } |
| 3059 | |
Jakub Kicinski | 26736eb | 2018-07-10 14:43:06 -0700 | [diff] [blame] | 3060 | int bpf_map__reuse_fd(struct bpf_map *map, int fd) |
| 3061 | { |
| 3062 | struct bpf_map_info info = {}; |
| 3063 | __u32 len = sizeof(info); |
| 3064 | int new_fd, err; |
| 3065 | char *new_name; |
| 3066 | |
| 3067 | err = bpf_obj_get_info_by_fd(fd, &info, &len); |
| 3068 | if (err) |
| 3069 | return err; |
| 3070 | |
| 3071 | new_name = strdup(info.name); |
| 3072 | if (!new_name) |
| 3073 | return -errno; |
| 3074 | |
| 3075 | new_fd = open("/", O_RDONLY | O_CLOEXEC); |
Toke Høiland-Jørgensen | d1b4574 | 2019-11-02 12:09:37 +0100 | [diff] [blame] | 3076 | if (new_fd < 0) { |
| 3077 | err = -errno; |
Jakub Kicinski | 26736eb | 2018-07-10 14:43:06 -0700 | [diff] [blame] | 3078 | goto err_free_new_name; |
Toke Høiland-Jørgensen | d1b4574 | 2019-11-02 12:09:37 +0100 | [diff] [blame] | 3079 | } |
Jakub Kicinski | 26736eb | 2018-07-10 14:43:06 -0700 | [diff] [blame] | 3080 | |
| 3081 | new_fd = dup3(fd, new_fd, O_CLOEXEC); |
Toke Høiland-Jørgensen | d1b4574 | 2019-11-02 12:09:37 +0100 | [diff] [blame] | 3082 | if (new_fd < 0) { |
| 3083 | err = -errno; |
Jakub Kicinski | 26736eb | 2018-07-10 14:43:06 -0700 | [diff] [blame] | 3084 | goto err_close_new_fd; |
Toke Høiland-Jørgensen | d1b4574 | 2019-11-02 12:09:37 +0100 | [diff] [blame] | 3085 | } |
Jakub Kicinski | 26736eb | 2018-07-10 14:43:06 -0700 | [diff] [blame] | 3086 | |
| 3087 | err = zclose(map->fd); |
Toke Høiland-Jørgensen | d1b4574 | 2019-11-02 12:09:37 +0100 | [diff] [blame] | 3088 | if (err) { |
| 3089 | err = -errno; |
Jakub Kicinski | 26736eb | 2018-07-10 14:43:06 -0700 | [diff] [blame] | 3090 | goto err_close_new_fd; |
Toke Høiland-Jørgensen | d1b4574 | 2019-11-02 12:09:37 +0100 | [diff] [blame] | 3091 | } |
Jakub Kicinski | 26736eb | 2018-07-10 14:43:06 -0700 | [diff] [blame] | 3092 | free(map->name); |
| 3093 | |
| 3094 | map->fd = new_fd; |
| 3095 | map->name = new_name; |
| 3096 | map->def.type = info.type; |
| 3097 | map->def.key_size = info.key_size; |
| 3098 | map->def.value_size = info.value_size; |
| 3099 | map->def.max_entries = info.max_entries; |
| 3100 | map->def.map_flags = info.map_flags; |
| 3101 | map->btf_key_type_id = info.btf_key_type_id; |
| 3102 | map->btf_value_type_id = info.btf_value_type_id; |
Toke Høiland-Jørgensen | ec6d5f47 | 2019-11-09 21:37:27 +0100 | [diff] [blame] | 3103 | map->reused = true; |
Jakub Kicinski | 26736eb | 2018-07-10 14:43:06 -0700 | [diff] [blame] | 3104 | |
| 3105 | return 0; |
| 3106 | |
| 3107 | err_close_new_fd: |
| 3108 | close(new_fd); |
| 3109 | err_free_new_name: |
| 3110 | free(new_name); |
Toke Høiland-Jørgensen | d1b4574 | 2019-11-02 12:09:37 +0100 | [diff] [blame] | 3111 | return err; |
Jakub Kicinski | 26736eb | 2018-07-10 14:43:06 -0700 | [diff] [blame] | 3112 | } |
| 3113 | |
Andrey Ignatov | 1a11a4c | 2019-02-14 15:01:42 -0800 | [diff] [blame] | 3114 | int bpf_map__resize(struct bpf_map *map, __u32 max_entries) |
| 3115 | { |
| 3116 | if (!map || !max_entries) |
| 3117 | return -EINVAL; |
| 3118 | |
| 3119 | /* If map already created, its attributes can't be changed. */ |
| 3120 | if (map->fd >= 0) |
| 3121 | return -EBUSY; |
| 3122 | |
| 3123 | map->def.max_entries = max_entries; |
| 3124 | |
| 3125 | return 0; |
| 3126 | } |
| 3127 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 3128 | static int |
Stanislav Fomichev | 47eff61 | 2018-11-20 17:11:19 -0800 | [diff] [blame] | 3129 | bpf_object__probe_name(struct bpf_object *obj) |
| 3130 | { |
| 3131 | struct bpf_load_program_attr attr; |
| 3132 | char *cp, errmsg[STRERR_BUFSIZE]; |
| 3133 | struct bpf_insn insns[] = { |
| 3134 | BPF_MOV64_IMM(BPF_REG_0, 0), |
| 3135 | BPF_EXIT_INSN(), |
| 3136 | }; |
| 3137 | int ret; |
| 3138 | |
| 3139 | /* make sure basic loading works */ |
| 3140 | |
| 3141 | memset(&attr, 0, sizeof(attr)); |
| 3142 | attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER; |
| 3143 | attr.insns = insns; |
| 3144 | attr.insns_cnt = ARRAY_SIZE(insns); |
| 3145 | attr.license = "GPL"; |
| 3146 | |
| 3147 | ret = bpf_load_program_xattr(&attr, NULL, 0); |
| 3148 | if (ret < 0) { |
| 3149 | cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg)); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3150 | pr_warn("Error in %s():%s(%d). Couldn't load basic 'r0 = 0' BPF program.\n", |
| 3151 | __func__, cp, errno); |
Stanislav Fomichev | 47eff61 | 2018-11-20 17:11:19 -0800 | [diff] [blame] | 3152 | return -errno; |
| 3153 | } |
| 3154 | close(ret); |
| 3155 | |
| 3156 | /* now try the same program, but with the name */ |
| 3157 | |
| 3158 | attr.name = "test"; |
| 3159 | ret = bpf_load_program_xattr(&attr, NULL, 0); |
| 3160 | if (ret >= 0) { |
| 3161 | obj->caps.name = 1; |
| 3162 | close(ret); |
| 3163 | } |
| 3164 | |
| 3165 | return 0; |
| 3166 | } |
| 3167 | |
| 3168 | static int |
Daniel Borkmann | 8837fe5 | 2019-04-24 00:45:56 +0200 | [diff] [blame] | 3169 | bpf_object__probe_global_data(struct bpf_object *obj) |
| 3170 | { |
| 3171 | struct bpf_load_program_attr prg_attr; |
| 3172 | struct bpf_create_map_attr map_attr; |
| 3173 | char *cp, errmsg[STRERR_BUFSIZE]; |
| 3174 | struct bpf_insn insns[] = { |
| 3175 | BPF_LD_MAP_VALUE(BPF_REG_1, 0, 16), |
| 3176 | BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 42), |
| 3177 | BPF_MOV64_IMM(BPF_REG_0, 0), |
| 3178 | BPF_EXIT_INSN(), |
| 3179 | }; |
| 3180 | int ret, map; |
| 3181 | |
| 3182 | memset(&map_attr, 0, sizeof(map_attr)); |
| 3183 | map_attr.map_type = BPF_MAP_TYPE_ARRAY; |
| 3184 | map_attr.key_size = sizeof(int); |
| 3185 | map_attr.value_size = 32; |
| 3186 | map_attr.max_entries = 1; |
| 3187 | |
| 3188 | map = bpf_create_map_xattr(&map_attr); |
| 3189 | if (map < 0) { |
| 3190 | cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg)); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3191 | pr_warn("Error in %s():%s(%d). Couldn't create simple array map.\n", |
| 3192 | __func__, cp, errno); |
Daniel Borkmann | 8837fe5 | 2019-04-24 00:45:56 +0200 | [diff] [blame] | 3193 | return -errno; |
| 3194 | } |
| 3195 | |
| 3196 | insns[0].imm = map; |
| 3197 | |
| 3198 | memset(&prg_attr, 0, sizeof(prg_attr)); |
| 3199 | prg_attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER; |
| 3200 | prg_attr.insns = insns; |
| 3201 | prg_attr.insns_cnt = ARRAY_SIZE(insns); |
| 3202 | prg_attr.license = "GPL"; |
| 3203 | |
| 3204 | ret = bpf_load_program_xattr(&prg_attr, NULL, 0); |
| 3205 | if (ret >= 0) { |
| 3206 | obj->caps.global_data = 1; |
| 3207 | close(ret); |
| 3208 | } |
| 3209 | |
| 3210 | close(map); |
| 3211 | return 0; |
| 3212 | } |
| 3213 | |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 3214 | static int bpf_object__probe_btf_func(struct bpf_object *obj) |
| 3215 | { |
Andrii Nakryiko | 8983b73 | 2019-11-20 23:07:42 -0800 | [diff] [blame] | 3216 | static const char strs[] = "\0int\0x\0a"; |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 3217 | /* void x(int a) {} */ |
| 3218 | __u32 types[] = { |
| 3219 | /* int */ |
| 3220 | BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ |
| 3221 | /* FUNC_PROTO */ /* [2] */ |
| 3222 | BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 0), |
| 3223 | BTF_PARAM_ENC(7, 1), |
| 3224 | /* FUNC x */ /* [3] */ |
| 3225 | BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0), 2), |
| 3226 | }; |
Michal Rostecki | cfd4921 | 2019-05-29 20:31:09 +0200 | [diff] [blame] | 3227 | int btf_fd; |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 3228 | |
Michal Rostecki | cfd4921 | 2019-05-29 20:31:09 +0200 | [diff] [blame] | 3229 | btf_fd = libbpf__load_raw_btf((char *)types, sizeof(types), |
| 3230 | strs, sizeof(strs)); |
| 3231 | if (btf_fd >= 0) { |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 3232 | obj->caps.btf_func = 1; |
Michal Rostecki | cfd4921 | 2019-05-29 20:31:09 +0200 | [diff] [blame] | 3233 | close(btf_fd); |
| 3234 | return 1; |
| 3235 | } |
| 3236 | |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 3237 | return 0; |
| 3238 | } |
| 3239 | |
Alexei Starovoitov | 2d3eb67 | 2020-01-09 22:41:19 -0800 | [diff] [blame] | 3240 | static int bpf_object__probe_btf_func_global(struct bpf_object *obj) |
| 3241 | { |
| 3242 | static const char strs[] = "\0int\0x\0a"; |
| 3243 | /* static void x(int a) {} */ |
| 3244 | __u32 types[] = { |
| 3245 | /* int */ |
| 3246 | BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ |
| 3247 | /* FUNC_PROTO */ /* [2] */ |
| 3248 | BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 0), |
| 3249 | BTF_PARAM_ENC(7, 1), |
| 3250 | /* FUNC x BTF_FUNC_GLOBAL */ /* [3] */ |
| 3251 | BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, BTF_FUNC_GLOBAL), 2), |
| 3252 | }; |
| 3253 | int btf_fd; |
| 3254 | |
| 3255 | btf_fd = libbpf__load_raw_btf((char *)types, sizeof(types), |
| 3256 | strs, sizeof(strs)); |
| 3257 | if (btf_fd >= 0) { |
| 3258 | obj->caps.btf_func_global = 1; |
| 3259 | close(btf_fd); |
| 3260 | return 1; |
| 3261 | } |
| 3262 | |
| 3263 | return 0; |
| 3264 | } |
| 3265 | |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 3266 | static int bpf_object__probe_btf_datasec(struct bpf_object *obj) |
| 3267 | { |
Andrii Nakryiko | 8983b73 | 2019-11-20 23:07:42 -0800 | [diff] [blame] | 3268 | static const char strs[] = "\0x\0.data"; |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 3269 | /* static int a; */ |
| 3270 | __u32 types[] = { |
| 3271 | /* int */ |
| 3272 | BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ |
| 3273 | /* VAR x */ /* [2] */ |
| 3274 | BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1), |
| 3275 | BTF_VAR_STATIC, |
| 3276 | /* DATASEC val */ /* [3] */ |
| 3277 | BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4), |
| 3278 | BTF_VAR_SECINFO_ENC(2, 0, 4), |
| 3279 | }; |
Michal Rostecki | cfd4921 | 2019-05-29 20:31:09 +0200 | [diff] [blame] | 3280 | int btf_fd; |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 3281 | |
Michal Rostecki | cfd4921 | 2019-05-29 20:31:09 +0200 | [diff] [blame] | 3282 | btf_fd = libbpf__load_raw_btf((char *)types, sizeof(types), |
| 3283 | strs, sizeof(strs)); |
| 3284 | if (btf_fd >= 0) { |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 3285 | obj->caps.btf_datasec = 1; |
Michal Rostecki | cfd4921 | 2019-05-29 20:31:09 +0200 | [diff] [blame] | 3286 | close(btf_fd); |
| 3287 | return 1; |
| 3288 | } |
| 3289 | |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 3290 | return 0; |
| 3291 | } |
| 3292 | |
Andrii Nakryiko | 7fe74b4 | 2019-11-17 09:28:05 -0800 | [diff] [blame] | 3293 | static int bpf_object__probe_array_mmap(struct bpf_object *obj) |
| 3294 | { |
| 3295 | struct bpf_create_map_attr attr = { |
| 3296 | .map_type = BPF_MAP_TYPE_ARRAY, |
| 3297 | .map_flags = BPF_F_MMAPABLE, |
| 3298 | .key_size = sizeof(int), |
| 3299 | .value_size = sizeof(int), |
| 3300 | .max_entries = 1, |
| 3301 | }; |
| 3302 | int fd; |
| 3303 | |
| 3304 | fd = bpf_create_map_xattr(&attr); |
| 3305 | if (fd >= 0) { |
| 3306 | obj->caps.array_mmap = 1; |
| 3307 | close(fd); |
| 3308 | return 1; |
| 3309 | } |
| 3310 | |
| 3311 | return 0; |
| 3312 | } |
| 3313 | |
Daniel Borkmann | 8837fe5 | 2019-04-24 00:45:56 +0200 | [diff] [blame] | 3314 | static int |
Stanislav Fomichev | 47eff61 | 2018-11-20 17:11:19 -0800 | [diff] [blame] | 3315 | bpf_object__probe_caps(struct bpf_object *obj) |
| 3316 | { |
Daniel Borkmann | 8837fe5 | 2019-04-24 00:45:56 +0200 | [diff] [blame] | 3317 | int (*probe_fn[])(struct bpf_object *obj) = { |
| 3318 | bpf_object__probe_name, |
| 3319 | bpf_object__probe_global_data, |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 3320 | bpf_object__probe_btf_func, |
Alexei Starovoitov | 2d3eb67 | 2020-01-09 22:41:19 -0800 | [diff] [blame] | 3321 | bpf_object__probe_btf_func_global, |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 3322 | bpf_object__probe_btf_datasec, |
Andrii Nakryiko | 7fe74b4 | 2019-11-17 09:28:05 -0800 | [diff] [blame] | 3323 | bpf_object__probe_array_mmap, |
Daniel Borkmann | 8837fe5 | 2019-04-24 00:45:56 +0200 | [diff] [blame] | 3324 | }; |
| 3325 | int i, ret; |
| 3326 | |
| 3327 | for (i = 0; i < ARRAY_SIZE(probe_fn); i++) { |
| 3328 | ret = probe_fn[i](obj); |
| 3329 | if (ret < 0) |
Stanislav Fomichev | 15ea164 | 2019-05-14 20:38:49 -0700 | [diff] [blame] | 3330 | pr_debug("Probe #%d failed with %d.\n", i, ret); |
Daniel Borkmann | 8837fe5 | 2019-04-24 00:45:56 +0200 | [diff] [blame] | 3331 | } |
| 3332 | |
| 3333 | return 0; |
Stanislav Fomichev | 47eff61 | 2018-11-20 17:11:19 -0800 | [diff] [blame] | 3334 | } |
| 3335 | |
Toke Høiland-Jørgensen | 57a00f4 | 2019-11-02 12:09:41 +0100 | [diff] [blame] | 3336 | static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd) |
| 3337 | { |
| 3338 | struct bpf_map_info map_info = {}; |
| 3339 | char msg[STRERR_BUFSIZE]; |
| 3340 | __u32 map_info_len; |
| 3341 | |
| 3342 | map_info_len = sizeof(map_info); |
| 3343 | |
| 3344 | if (bpf_obj_get_info_by_fd(map_fd, &map_info, &map_info_len)) { |
| 3345 | pr_warn("failed to get map info for map FD %d: %s\n", |
| 3346 | map_fd, libbpf_strerror_r(errno, msg, sizeof(msg))); |
| 3347 | return false; |
| 3348 | } |
| 3349 | |
| 3350 | return (map_info.type == map->def.type && |
| 3351 | map_info.key_size == map->def.key_size && |
| 3352 | map_info.value_size == map->def.value_size && |
| 3353 | map_info.max_entries == map->def.max_entries && |
| 3354 | map_info.map_flags == map->def.map_flags); |
| 3355 | } |
| 3356 | |
| 3357 | static int |
| 3358 | bpf_object__reuse_map(struct bpf_map *map) |
| 3359 | { |
| 3360 | char *cp, errmsg[STRERR_BUFSIZE]; |
| 3361 | int err, pin_fd; |
| 3362 | |
| 3363 | pin_fd = bpf_obj_get(map->pin_path); |
| 3364 | if (pin_fd < 0) { |
| 3365 | err = -errno; |
| 3366 | if (err == -ENOENT) { |
| 3367 | pr_debug("found no pinned map to reuse at '%s'\n", |
| 3368 | map->pin_path); |
| 3369 | return 0; |
| 3370 | } |
| 3371 | |
| 3372 | cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg)); |
| 3373 | pr_warn("couldn't retrieve pinned map '%s': %s\n", |
| 3374 | map->pin_path, cp); |
| 3375 | return err; |
| 3376 | } |
| 3377 | |
| 3378 | if (!map_is_reuse_compat(map, pin_fd)) { |
| 3379 | pr_warn("couldn't reuse pinned map at '%s': parameter mismatch\n", |
| 3380 | map->pin_path); |
| 3381 | close(pin_fd); |
| 3382 | return -EINVAL; |
| 3383 | } |
| 3384 | |
| 3385 | err = bpf_map__reuse_fd(map, pin_fd); |
| 3386 | if (err) { |
| 3387 | close(pin_fd); |
| 3388 | return err; |
| 3389 | } |
| 3390 | map->pinned = true; |
| 3391 | pr_debug("reused pinned map at '%s'\n", map->pin_path); |
| 3392 | |
| 3393 | return 0; |
| 3394 | } |
| 3395 | |
Stanislav Fomichev | 47eff61 | 2018-11-20 17:11:19 -0800 | [diff] [blame] | 3396 | static int |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 3397 | bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map) |
| 3398 | { |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 3399 | enum libbpf_map_type map_type = map->libbpf_type; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 3400 | char *cp, errmsg[STRERR_BUFSIZE]; |
| 3401 | int err, zero = 0; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 3402 | |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 3403 | /* kernel already zero-initializes .bss map. */ |
| 3404 | if (map_type == LIBBPF_MAP_BSS) |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 3405 | return 0; |
| 3406 | |
Andrii Nakryiko | eba9c5f | 2019-12-13 17:43:33 -0800 | [diff] [blame] | 3407 | err = bpf_map_update_elem(map->fd, &zero, map->mmaped, 0); |
| 3408 | if (err) { |
| 3409 | err = -errno; |
| 3410 | cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg)); |
| 3411 | pr_warn("Error setting initial map(%s) contents: %s\n", |
| 3412 | map->name, cp); |
| 3413 | return err; |
| 3414 | } |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 3415 | |
Andrii Nakryiko | 81bfdd0 | 2019-12-18 16:28:34 -0800 | [diff] [blame] | 3416 | /* Freeze .rodata and .kconfig map as read-only from syscall side. */ |
| 3417 | if (map_type == LIBBPF_MAP_RODATA || map_type == LIBBPF_MAP_KCONFIG) { |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 3418 | err = bpf_map_freeze(map->fd); |
| 3419 | if (err) { |
Andrii Nakryiko | eba9c5f | 2019-12-13 17:43:33 -0800 | [diff] [blame] | 3420 | err = -errno; |
| 3421 | cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg)); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3422 | pr_warn("Error freezing map(%s) as read-only: %s\n", |
| 3423 | map->name, cp); |
Andrii Nakryiko | eba9c5f | 2019-12-13 17:43:33 -0800 | [diff] [blame] | 3424 | return err; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 3425 | } |
| 3426 | } |
Andrii Nakryiko | eba9c5f | 2019-12-13 17:43:33 -0800 | [diff] [blame] | 3427 | return 0; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 3428 | } |
| 3429 | |
| 3430 | static int |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 3431 | bpf_object__create_maps(struct bpf_object *obj) |
| 3432 | { |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 3433 | struct bpf_create_map_attr create_attr = {}; |
Andrii Nakryiko | d7ff34d | 2019-07-06 11:06:25 -0700 | [diff] [blame] | 3434 | int nr_cpus = 0; |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 3435 | unsigned int i; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 3436 | int err; |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 3437 | |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 3438 | for (i = 0; i < obj->nr_maps; i++) { |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 3439 | struct bpf_map *map = &obj->maps[i]; |
| 3440 | struct bpf_map_def *def = &map->def; |
Thomas Richter | 1ce6a9f | 2018-07-30 10:53:23 +0200 | [diff] [blame] | 3441 | char *cp, errmsg[STRERR_BUFSIZE]; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 3442 | int *pfd = &map->fd; |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 3443 | |
Toke Høiland-Jørgensen | 57a00f4 | 2019-11-02 12:09:41 +0100 | [diff] [blame] | 3444 | if (map->pin_path) { |
| 3445 | err = bpf_object__reuse_map(map); |
| 3446 | if (err) { |
| 3447 | pr_warn("error reusing pinned map %s\n", |
| 3448 | map->name); |
| 3449 | return err; |
| 3450 | } |
| 3451 | } |
| 3452 | |
Jakub Kicinski | 26736eb | 2018-07-10 14:43:06 -0700 | [diff] [blame] | 3453 | if (map->fd >= 0) { |
| 3454 | pr_debug("skip map create (preset) %s: fd=%d\n", |
| 3455 | map->name, map->fd); |
| 3456 | continue; |
| 3457 | } |
| 3458 | |
Stanislav Fomichev | 94cb310 | 2018-11-20 17:11:20 -0800 | [diff] [blame] | 3459 | if (obj->caps.name) |
| 3460 | create_attr.name = map->name; |
David Beckett | f0307a7 | 2018-05-16 14:02:49 -0700 | [diff] [blame] | 3461 | create_attr.map_ifindex = map->map_ifindex; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 3462 | create_attr.map_type = def->type; |
| 3463 | create_attr.map_flags = def->map_flags; |
| 3464 | create_attr.key_size = def->key_size; |
| 3465 | create_attr.value_size = def->value_size; |
Andrii Nakryiko | d7ff34d | 2019-07-06 11:06:25 -0700 | [diff] [blame] | 3466 | if (def->type == BPF_MAP_TYPE_PERF_EVENT_ARRAY && |
| 3467 | !def->max_entries) { |
| 3468 | if (!nr_cpus) |
| 3469 | nr_cpus = libbpf_num_possible_cpus(); |
| 3470 | if (nr_cpus < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3471 | pr_warn("failed to determine number of system CPUs: %d\n", |
| 3472 | nr_cpus); |
Andrii Nakryiko | d7ff34d | 2019-07-06 11:06:25 -0700 | [diff] [blame] | 3473 | err = nr_cpus; |
| 3474 | goto err_out; |
| 3475 | } |
| 3476 | pr_debug("map '%s': setting size to %d\n", |
| 3477 | map->name, nr_cpus); |
| 3478 | create_attr.max_entries = nr_cpus; |
| 3479 | } else { |
| 3480 | create_attr.max_entries = def->max_entries; |
| 3481 | } |
Andrii Nakryiko | e55d54f | 2019-06-12 22:04:57 -0700 | [diff] [blame] | 3482 | create_attr.btf_fd = 0; |
Martin KaFai Lau | 61746dbe | 2018-05-22 15:04:24 -0700 | [diff] [blame] | 3483 | create_attr.btf_key_type_id = 0; |
| 3484 | create_attr.btf_value_type_id = 0; |
Nikita V. Shirokov | addb9fc | 2018-11-20 20:55:56 -0800 | [diff] [blame] | 3485 | if (bpf_map_type__is_map_in_map(def->type) && |
| 3486 | map->inner_map_fd >= 0) |
| 3487 | create_attr.inner_map_fd = map->inner_map_fd; |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 3488 | if (bpf_map__is_struct_ops(map)) |
| 3489 | create_attr.btf_vmlinux_value_type_id = |
| 3490 | map->btf_vmlinux_value_type_id; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 3491 | |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 3492 | if (obj->btf && !bpf_map_find_btf_info(obj, map)) { |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 3493 | create_attr.btf_fd = btf__fd(obj->btf); |
Martin KaFai Lau | 61746dbe | 2018-05-22 15:04:24 -0700 | [diff] [blame] | 3494 | create_attr.btf_key_type_id = map->btf_key_type_id; |
| 3495 | create_attr.btf_value_type_id = map->btf_value_type_id; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 3496 | } |
| 3497 | |
| 3498 | *pfd = bpf_create_map_xattr(&create_attr); |
Andrii Nakryiko | e55d54f | 2019-06-12 22:04:57 -0700 | [diff] [blame] | 3499 | if (*pfd < 0 && (create_attr.btf_key_type_id || |
| 3500 | create_attr.btf_value_type_id)) { |
Andrii Nakryiko | d7ff34d | 2019-07-06 11:06:25 -0700 | [diff] [blame] | 3501 | err = -errno; |
| 3502 | cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg)); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3503 | pr_warn("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n", |
| 3504 | map->name, cp, err); |
Andrii Nakryiko | e55d54f | 2019-06-12 22:04:57 -0700 | [diff] [blame] | 3505 | create_attr.btf_fd = 0; |
Martin KaFai Lau | 61746dbe | 2018-05-22 15:04:24 -0700 | [diff] [blame] | 3506 | create_attr.btf_key_type_id = 0; |
| 3507 | create_attr.btf_value_type_id = 0; |
| 3508 | map->btf_key_type_id = 0; |
| 3509 | map->btf_value_type_id = 0; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 3510 | *pfd = bpf_create_map_xattr(&create_attr); |
| 3511 | } |
| 3512 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 3513 | if (*pfd < 0) { |
| 3514 | size_t j; |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 3515 | |
Andrii Nakryiko | d7ff34d | 2019-07-06 11:06:25 -0700 | [diff] [blame] | 3516 | err = -errno; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 3517 | err_out: |
Andrii Nakryiko | d7ff34d | 2019-07-06 11:06:25 -0700 | [diff] [blame] | 3518 | cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg)); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3519 | pr_warn("failed to create map (name: '%s'): %s(%d)\n", |
| 3520 | map->name, cp, err); |
Toke Høiland-Jørgensen | dc3a2d2 | 2019-12-16 19:12:04 +0100 | [diff] [blame] | 3521 | pr_perm_msg(err); |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 3522 | for (j = 0; j < i; j++) |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 3523 | zclose(obj->maps[j].fd); |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 3524 | return err; |
| 3525 | } |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 3526 | |
| 3527 | if (bpf_map__is_internal(map)) { |
| 3528 | err = bpf_object__populate_internal_map(obj, map); |
| 3529 | if (err < 0) { |
| 3530 | zclose(*pfd); |
| 3531 | goto err_out; |
| 3532 | } |
| 3533 | } |
| 3534 | |
Toke Høiland-Jørgensen | 57a00f4 | 2019-11-02 12:09:41 +0100 | [diff] [blame] | 3535 | if (map->pin_path && !map->pinned) { |
| 3536 | err = bpf_map__pin(map, NULL); |
| 3537 | if (err) { |
| 3538 | pr_warn("failed to auto-pin map name '%s' at '%s'\n", |
| 3539 | map->name, map->pin_path); |
| 3540 | return err; |
| 3541 | } |
| 3542 | } |
| 3543 | |
Andrii Nakryiko | 76e1022 | 2019-05-29 10:36:10 -0700 | [diff] [blame] | 3544 | pr_debug("created map %s: fd=%d\n", map->name, *pfd); |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 3545 | } |
| 3546 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 3547 | return 0; |
| 3548 | } |
| 3549 | |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 3550 | static int |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 3551 | check_btf_ext_reloc_err(struct bpf_program *prog, int err, |
| 3552 | void *btf_prog_info, const char *info_name) |
| 3553 | { |
| 3554 | if (err != -ENOENT) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3555 | pr_warn("Error in loading %s for sec %s.\n", |
| 3556 | info_name, prog->section_name); |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 3557 | return err; |
| 3558 | } |
| 3559 | |
| 3560 | /* err == -ENOENT (i.e. prog->section_name not found in btf_ext) */ |
| 3561 | |
| 3562 | if (btf_prog_info) { |
| 3563 | /* |
| 3564 | * Some info has already been found but has problem |
Andrii Nakryiko | 399dc65 | 2019-05-29 10:36:11 -0700 | [diff] [blame] | 3565 | * in the last btf_ext reloc. Must have to error out. |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 3566 | */ |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3567 | pr_warn("Error in relocating %s for sec %s.\n", |
| 3568 | info_name, prog->section_name); |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 3569 | return err; |
| 3570 | } |
| 3571 | |
Andrii Nakryiko | 399dc65 | 2019-05-29 10:36:11 -0700 | [diff] [blame] | 3572 | /* Have problem loading the very first info. Ignore the rest. */ |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3573 | pr_warn("Cannot find %s for main program sec %s. Ignore all %s.\n", |
| 3574 | info_name, prog->section_name, info_name); |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 3575 | return 0; |
| 3576 | } |
| 3577 | |
| 3578 | static int |
| 3579 | bpf_program_reloc_btf_ext(struct bpf_program *prog, struct bpf_object *obj, |
| 3580 | const char *section_name, __u32 insn_offset) |
| 3581 | { |
| 3582 | int err; |
| 3583 | |
| 3584 | if (!insn_offset || prog->func_info) { |
| 3585 | /* |
| 3586 | * !insn_offset => main program |
| 3587 | * |
| 3588 | * For sub prog, the main program's func_info has to |
| 3589 | * be loaded first (i.e. prog->func_info != NULL) |
| 3590 | */ |
| 3591 | err = btf_ext__reloc_func_info(obj->btf, obj->btf_ext, |
| 3592 | section_name, insn_offset, |
| 3593 | &prog->func_info, |
| 3594 | &prog->func_info_cnt); |
| 3595 | if (err) |
| 3596 | return check_btf_ext_reloc_err(prog, err, |
| 3597 | prog->func_info, |
| 3598 | "bpf_func_info"); |
| 3599 | |
| 3600 | prog->func_info_rec_size = btf_ext__func_info_rec_size(obj->btf_ext); |
| 3601 | } |
| 3602 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 3603 | if (!insn_offset || prog->line_info) { |
| 3604 | err = btf_ext__reloc_line_info(obj->btf, obj->btf_ext, |
| 3605 | section_name, insn_offset, |
| 3606 | &prog->line_info, |
| 3607 | &prog->line_info_cnt); |
| 3608 | if (err) |
| 3609 | return check_btf_ext_reloc_err(prog, err, |
| 3610 | prog->line_info, |
| 3611 | "bpf_line_info"); |
| 3612 | |
| 3613 | prog->line_info_rec_size = btf_ext__line_info_rec_size(obj->btf_ext); |
| 3614 | } |
| 3615 | |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 3616 | return 0; |
| 3617 | } |
| 3618 | |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3619 | #define BPF_CORE_SPEC_MAX_LEN 64 |
| 3620 | |
| 3621 | /* represents BPF CO-RE field or array element accessor */ |
| 3622 | struct bpf_core_accessor { |
| 3623 | __u32 type_id; /* struct/union type or array element type */ |
| 3624 | __u32 idx; /* field index or array index */ |
| 3625 | const char *name; /* field name or NULL for array accessor */ |
| 3626 | }; |
| 3627 | |
| 3628 | struct bpf_core_spec { |
| 3629 | const struct btf *btf; |
| 3630 | /* high-level spec: named fields and array indices only */ |
| 3631 | struct bpf_core_accessor spec[BPF_CORE_SPEC_MAX_LEN]; |
| 3632 | /* high-level spec length */ |
| 3633 | int len; |
| 3634 | /* raw, low-level spec: 1-to-1 with accessor spec string */ |
| 3635 | int raw_spec[BPF_CORE_SPEC_MAX_LEN]; |
| 3636 | /* raw spec length */ |
| 3637 | int raw_len; |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 3638 | /* field bit offset represented by spec */ |
| 3639 | __u32 bit_offset; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3640 | }; |
| 3641 | |
| 3642 | static bool str_is_empty(const char *s) |
| 3643 | { |
| 3644 | return !s || !s[0]; |
| 3645 | } |
| 3646 | |
Andrii Nakryiko | 1b484b3 | 2019-12-14 23:08:43 -0800 | [diff] [blame] | 3647 | static bool is_flex_arr(const struct btf *btf, |
| 3648 | const struct bpf_core_accessor *acc, |
| 3649 | const struct btf_array *arr) |
| 3650 | { |
| 3651 | const struct btf_type *t; |
| 3652 | |
| 3653 | /* not a flexible array, if not inside a struct or has non-zero size */ |
| 3654 | if (!acc->name || arr->nelems > 0) |
| 3655 | return false; |
| 3656 | |
| 3657 | /* has to be the last member of enclosing struct */ |
| 3658 | t = btf__type_by_id(btf, acc->type_id); |
| 3659 | return acc->idx == btf_vlen(t) - 1; |
| 3660 | } |
| 3661 | |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3662 | /* |
Andrii Nakryiko | 511bb00 | 2019-10-15 11:28:45 -0700 | [diff] [blame] | 3663 | * Turn bpf_field_reloc into a low- and high-level spec representation, |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3664 | * validating correctness along the way, as well as calculating resulting |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 3665 | * field bit offset, specified by accessor string. Low-level spec captures |
| 3666 | * every single level of nestedness, including traversing anonymous |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3667 | * struct/union members. High-level one only captures semantically meaningful |
| 3668 | * "turning points": named fields and array indicies. |
| 3669 | * E.g., for this case: |
| 3670 | * |
| 3671 | * struct sample { |
| 3672 | * int __unimportant; |
| 3673 | * struct { |
| 3674 | * int __1; |
| 3675 | * int __2; |
| 3676 | * int a[7]; |
| 3677 | * }; |
| 3678 | * }; |
| 3679 | * |
| 3680 | * struct sample *s = ...; |
| 3681 | * |
| 3682 | * int x = &s->a[3]; // access string = '0:1:2:3' |
| 3683 | * |
| 3684 | * Low-level spec has 1:1 mapping with each element of access string (it's |
| 3685 | * just a parsed access string representation): [0, 1, 2, 3]. |
| 3686 | * |
| 3687 | * High-level spec will capture only 3 points: |
| 3688 | * - intial zero-index access by pointer (&s->... is the same as &s[0]...); |
| 3689 | * - field 'a' access (corresponds to '2' in low-level spec); |
| 3690 | * - array element #3 access (corresponds to '3' in low-level spec). |
| 3691 | * |
| 3692 | */ |
| 3693 | static int bpf_core_spec_parse(const struct btf *btf, |
| 3694 | __u32 type_id, |
| 3695 | const char *spec_str, |
| 3696 | struct bpf_core_spec *spec) |
| 3697 | { |
| 3698 | int access_idx, parsed_len, i; |
Andrii Nakryiko | 1b484b3 | 2019-12-14 23:08:43 -0800 | [diff] [blame] | 3699 | struct bpf_core_accessor *acc; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3700 | const struct btf_type *t; |
| 3701 | const char *name; |
| 3702 | __u32 id; |
| 3703 | __s64 sz; |
| 3704 | |
| 3705 | if (str_is_empty(spec_str) || *spec_str == ':') |
| 3706 | return -EINVAL; |
| 3707 | |
| 3708 | memset(spec, 0, sizeof(*spec)); |
| 3709 | spec->btf = btf; |
| 3710 | |
| 3711 | /* parse spec_str="0:1:2:3:4" into array raw_spec=[0, 1, 2, 3, 4] */ |
| 3712 | while (*spec_str) { |
| 3713 | if (*spec_str == ':') |
| 3714 | ++spec_str; |
| 3715 | if (sscanf(spec_str, "%d%n", &access_idx, &parsed_len) != 1) |
| 3716 | return -EINVAL; |
| 3717 | if (spec->raw_len == BPF_CORE_SPEC_MAX_LEN) |
| 3718 | return -E2BIG; |
| 3719 | spec_str += parsed_len; |
| 3720 | spec->raw_spec[spec->raw_len++] = access_idx; |
| 3721 | } |
| 3722 | |
| 3723 | if (spec->raw_len == 0) |
| 3724 | return -EINVAL; |
| 3725 | |
| 3726 | /* first spec value is always reloc type array index */ |
| 3727 | t = skip_mods_and_typedefs(btf, type_id, &id); |
| 3728 | if (!t) |
| 3729 | return -EINVAL; |
| 3730 | |
| 3731 | access_idx = spec->raw_spec[0]; |
| 3732 | spec->spec[0].type_id = id; |
| 3733 | spec->spec[0].idx = access_idx; |
| 3734 | spec->len++; |
| 3735 | |
| 3736 | sz = btf__resolve_size(btf, id); |
| 3737 | if (sz < 0) |
| 3738 | return sz; |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 3739 | spec->bit_offset = access_idx * sz * 8; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3740 | |
| 3741 | for (i = 1; i < spec->raw_len; i++) { |
| 3742 | t = skip_mods_and_typedefs(btf, id, &id); |
| 3743 | if (!t) |
| 3744 | return -EINVAL; |
| 3745 | |
| 3746 | access_idx = spec->raw_spec[i]; |
Andrii Nakryiko | 1b484b3 | 2019-12-14 23:08:43 -0800 | [diff] [blame] | 3747 | acc = &spec->spec[spec->len]; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3748 | |
| 3749 | if (btf_is_composite(t)) { |
| 3750 | const struct btf_member *m; |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 3751 | __u32 bit_offset; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3752 | |
| 3753 | if (access_idx >= btf_vlen(t)) |
| 3754 | return -EINVAL; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3755 | |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 3756 | bit_offset = btf_member_bit_offset(t, access_idx); |
| 3757 | spec->bit_offset += bit_offset; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3758 | |
| 3759 | m = btf_members(t) + access_idx; |
| 3760 | if (m->name_off) { |
| 3761 | name = btf__name_by_offset(btf, m->name_off); |
| 3762 | if (str_is_empty(name)) |
| 3763 | return -EINVAL; |
| 3764 | |
Andrii Nakryiko | 1b484b3 | 2019-12-14 23:08:43 -0800 | [diff] [blame] | 3765 | acc->type_id = id; |
| 3766 | acc->idx = access_idx; |
| 3767 | acc->name = name; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3768 | spec->len++; |
| 3769 | } |
| 3770 | |
| 3771 | id = m->type; |
| 3772 | } else if (btf_is_array(t)) { |
| 3773 | const struct btf_array *a = btf_array(t); |
Andrii Nakryiko | 1b484b3 | 2019-12-14 23:08:43 -0800 | [diff] [blame] | 3774 | bool flex; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3775 | |
| 3776 | t = skip_mods_and_typedefs(btf, a->type, &id); |
Andrii Nakryiko | 1b484b3 | 2019-12-14 23:08:43 -0800 | [diff] [blame] | 3777 | if (!t) |
| 3778 | return -EINVAL; |
| 3779 | |
| 3780 | flex = is_flex_arr(btf, acc - 1, a); |
| 3781 | if (!flex && access_idx >= a->nelems) |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3782 | return -EINVAL; |
| 3783 | |
| 3784 | spec->spec[spec->len].type_id = id; |
| 3785 | spec->spec[spec->len].idx = access_idx; |
| 3786 | spec->len++; |
| 3787 | |
| 3788 | sz = btf__resolve_size(btf, id); |
| 3789 | if (sz < 0) |
| 3790 | return sz; |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 3791 | spec->bit_offset += access_idx * sz * 8; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3792 | } else { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3793 | pr_warn("relo for [%u] %s (at idx %d) captures type [%d] of unexpected kind %d\n", |
| 3794 | type_id, spec_str, i, id, btf_kind(t)); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3795 | return -EINVAL; |
| 3796 | } |
| 3797 | } |
| 3798 | |
| 3799 | return 0; |
| 3800 | } |
| 3801 | |
| 3802 | static bool bpf_core_is_flavor_sep(const char *s) |
| 3803 | { |
| 3804 | /* check X___Y name pattern, where X and Y are not underscores */ |
| 3805 | return s[0] != '_' && /* X */ |
| 3806 | s[1] == '_' && s[2] == '_' && s[3] == '_' && /* ___ */ |
| 3807 | s[4] != '_'; /* Y */ |
| 3808 | } |
| 3809 | |
| 3810 | /* Given 'some_struct_name___with_flavor' return the length of a name prefix |
| 3811 | * before last triple underscore. Struct name part after last triple |
| 3812 | * underscore is ignored by BPF CO-RE relocation during relocation matching. |
| 3813 | */ |
| 3814 | static size_t bpf_core_essential_name_len(const char *name) |
| 3815 | { |
| 3816 | size_t n = strlen(name); |
| 3817 | int i; |
| 3818 | |
| 3819 | for (i = n - 5; i >= 0; i--) { |
| 3820 | if (bpf_core_is_flavor_sep(name + i)) |
| 3821 | return i + 1; |
| 3822 | } |
| 3823 | return n; |
| 3824 | } |
| 3825 | |
| 3826 | /* dynamically sized list of type IDs */ |
| 3827 | struct ids_vec { |
| 3828 | __u32 *data; |
| 3829 | int len; |
| 3830 | }; |
| 3831 | |
| 3832 | static void bpf_core_free_cands(struct ids_vec *cand_ids) |
| 3833 | { |
| 3834 | free(cand_ids->data); |
| 3835 | free(cand_ids); |
| 3836 | } |
| 3837 | |
| 3838 | static struct ids_vec *bpf_core_find_cands(const struct btf *local_btf, |
| 3839 | __u32 local_type_id, |
| 3840 | const struct btf *targ_btf) |
| 3841 | { |
| 3842 | size_t local_essent_len, targ_essent_len; |
| 3843 | const char *local_name, *targ_name; |
| 3844 | const struct btf_type *t; |
| 3845 | struct ids_vec *cand_ids; |
| 3846 | __u32 *new_ids; |
| 3847 | int i, err, n; |
| 3848 | |
| 3849 | t = btf__type_by_id(local_btf, local_type_id); |
| 3850 | if (!t) |
| 3851 | return ERR_PTR(-EINVAL); |
| 3852 | |
| 3853 | local_name = btf__name_by_offset(local_btf, t->name_off); |
| 3854 | if (str_is_empty(local_name)) |
| 3855 | return ERR_PTR(-EINVAL); |
| 3856 | local_essent_len = bpf_core_essential_name_len(local_name); |
| 3857 | |
| 3858 | cand_ids = calloc(1, sizeof(*cand_ids)); |
| 3859 | if (!cand_ids) |
| 3860 | return ERR_PTR(-ENOMEM); |
| 3861 | |
| 3862 | n = btf__get_nr_types(targ_btf); |
| 3863 | for (i = 1; i <= n; i++) { |
| 3864 | t = btf__type_by_id(targ_btf, i); |
| 3865 | targ_name = btf__name_by_offset(targ_btf, t->name_off); |
| 3866 | if (str_is_empty(targ_name)) |
| 3867 | continue; |
| 3868 | |
| 3869 | targ_essent_len = bpf_core_essential_name_len(targ_name); |
| 3870 | if (targ_essent_len != local_essent_len) |
| 3871 | continue; |
| 3872 | |
| 3873 | if (strncmp(local_name, targ_name, local_essent_len) == 0) { |
| 3874 | pr_debug("[%d] %s: found candidate [%d] %s\n", |
| 3875 | local_type_id, local_name, i, targ_name); |
Andrii Nakryiko | 35b9211 | 2020-01-24 12:18:46 -0800 | [diff] [blame] | 3876 | new_ids = reallocarray(cand_ids->data, |
| 3877 | cand_ids->len + 1, |
| 3878 | sizeof(*cand_ids->data)); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3879 | if (!new_ids) { |
| 3880 | err = -ENOMEM; |
| 3881 | goto err_out; |
| 3882 | } |
| 3883 | cand_ids->data = new_ids; |
| 3884 | cand_ids->data[cand_ids->len++] = i; |
| 3885 | } |
| 3886 | } |
| 3887 | return cand_ids; |
| 3888 | err_out: |
| 3889 | bpf_core_free_cands(cand_ids); |
| 3890 | return ERR_PTR(err); |
| 3891 | } |
| 3892 | |
| 3893 | /* Check two types for compatibility, skipping const/volatile/restrict and |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 3894 | * typedefs, to ensure we are relocating compatible entities: |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3895 | * - any two STRUCTs/UNIONs are compatible and can be mixed; |
Andrii Nakryiko | 94f060e | 2019-11-01 15:28:08 -0700 | [diff] [blame] | 3896 | * - any two FWDs are compatible, if their names match (modulo flavor suffix); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3897 | * - any two PTRs are always compatible; |
Andrii Nakryiko | 94f060e | 2019-11-01 15:28:08 -0700 | [diff] [blame] | 3898 | * - for ENUMs, names should be the same (ignoring flavor suffix) or at |
| 3899 | * least one of enums should be anonymous; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3900 | * - for ENUMs, check sizes, names are ignored; |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 3901 | * - for INT, size and signedness are ignored; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3902 | * - for ARRAY, dimensionality is ignored, element types are checked for |
| 3903 | * compatibility recursively; |
| 3904 | * - everything else shouldn't be ever a target of relocation. |
| 3905 | * These rules are not set in stone and probably will be adjusted as we get |
| 3906 | * more experience with using BPF CO-RE relocations. |
| 3907 | */ |
| 3908 | static int bpf_core_fields_are_compat(const struct btf *local_btf, |
| 3909 | __u32 local_id, |
| 3910 | const struct btf *targ_btf, |
| 3911 | __u32 targ_id) |
| 3912 | { |
| 3913 | const struct btf_type *local_type, *targ_type; |
| 3914 | |
| 3915 | recur: |
| 3916 | local_type = skip_mods_and_typedefs(local_btf, local_id, &local_id); |
| 3917 | targ_type = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id); |
| 3918 | if (!local_type || !targ_type) |
| 3919 | return -EINVAL; |
| 3920 | |
| 3921 | if (btf_is_composite(local_type) && btf_is_composite(targ_type)) |
| 3922 | return 1; |
| 3923 | if (btf_kind(local_type) != btf_kind(targ_type)) |
| 3924 | return 0; |
| 3925 | |
| 3926 | switch (btf_kind(local_type)) { |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3927 | case BTF_KIND_PTR: |
| 3928 | return 1; |
Andrii Nakryiko | 94f060e | 2019-11-01 15:28:08 -0700 | [diff] [blame] | 3929 | case BTF_KIND_FWD: |
| 3930 | case BTF_KIND_ENUM: { |
| 3931 | const char *local_name, *targ_name; |
| 3932 | size_t local_len, targ_len; |
| 3933 | |
| 3934 | local_name = btf__name_by_offset(local_btf, |
| 3935 | local_type->name_off); |
| 3936 | targ_name = btf__name_by_offset(targ_btf, targ_type->name_off); |
| 3937 | local_len = bpf_core_essential_name_len(local_name); |
| 3938 | targ_len = bpf_core_essential_name_len(targ_name); |
| 3939 | /* one of them is anonymous or both w/ same flavor-less names */ |
| 3940 | return local_len == 0 || targ_len == 0 || |
| 3941 | (local_len == targ_len && |
| 3942 | strncmp(local_name, targ_name, local_len) == 0); |
| 3943 | } |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3944 | case BTF_KIND_INT: |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 3945 | /* just reject deprecated bitfield-like integers; all other |
| 3946 | * integers are by default compatible between each other |
| 3947 | */ |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3948 | return btf_int_offset(local_type) == 0 && |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 3949 | btf_int_offset(targ_type) == 0; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3950 | case BTF_KIND_ARRAY: |
| 3951 | local_id = btf_array(local_type)->type; |
| 3952 | targ_id = btf_array(targ_type)->type; |
| 3953 | goto recur; |
| 3954 | default: |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3955 | pr_warn("unexpected kind %d relocated, local [%d], target [%d]\n", |
| 3956 | btf_kind(local_type), local_id, targ_id); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3957 | return 0; |
| 3958 | } |
| 3959 | } |
| 3960 | |
| 3961 | /* |
| 3962 | * Given single high-level named field accessor in local type, find |
| 3963 | * corresponding high-level accessor for a target type. Along the way, |
| 3964 | * maintain low-level spec for target as well. Also keep updating target |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 3965 | * bit offset. |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3966 | * |
| 3967 | * Searching is performed through recursive exhaustive enumeration of all |
| 3968 | * fields of a struct/union. If there are any anonymous (embedded) |
| 3969 | * structs/unions, they are recursively searched as well. If field with |
| 3970 | * desired name is found, check compatibility between local and target types, |
| 3971 | * before returning result. |
| 3972 | * |
| 3973 | * 1 is returned, if field is found. |
| 3974 | * 0 is returned if no compatible field is found. |
| 3975 | * <0 is returned on error. |
| 3976 | */ |
| 3977 | static int bpf_core_match_member(const struct btf *local_btf, |
| 3978 | const struct bpf_core_accessor *local_acc, |
| 3979 | const struct btf *targ_btf, |
| 3980 | __u32 targ_id, |
| 3981 | struct bpf_core_spec *spec, |
| 3982 | __u32 *next_targ_id) |
| 3983 | { |
| 3984 | const struct btf_type *local_type, *targ_type; |
| 3985 | const struct btf_member *local_member, *m; |
| 3986 | const char *local_name, *targ_name; |
| 3987 | __u32 local_id; |
| 3988 | int i, n, found; |
| 3989 | |
| 3990 | targ_type = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id); |
| 3991 | if (!targ_type) |
| 3992 | return -EINVAL; |
| 3993 | if (!btf_is_composite(targ_type)) |
| 3994 | return 0; |
| 3995 | |
| 3996 | local_id = local_acc->type_id; |
| 3997 | local_type = btf__type_by_id(local_btf, local_id); |
| 3998 | local_member = btf_members(local_type) + local_acc->idx; |
| 3999 | local_name = btf__name_by_offset(local_btf, local_member->name_off); |
| 4000 | |
| 4001 | n = btf_vlen(targ_type); |
| 4002 | m = btf_members(targ_type); |
| 4003 | for (i = 0; i < n; i++, m++) { |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 4004 | __u32 bit_offset; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4005 | |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 4006 | bit_offset = btf_member_bit_offset(targ_type, i); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4007 | |
| 4008 | /* too deep struct/union/array nesting */ |
| 4009 | if (spec->raw_len == BPF_CORE_SPEC_MAX_LEN) |
| 4010 | return -E2BIG; |
| 4011 | |
| 4012 | /* speculate this member will be the good one */ |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 4013 | spec->bit_offset += bit_offset; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4014 | spec->raw_spec[spec->raw_len++] = i; |
| 4015 | |
| 4016 | targ_name = btf__name_by_offset(targ_btf, m->name_off); |
| 4017 | if (str_is_empty(targ_name)) { |
| 4018 | /* embedded struct/union, we need to go deeper */ |
| 4019 | found = bpf_core_match_member(local_btf, local_acc, |
| 4020 | targ_btf, m->type, |
| 4021 | spec, next_targ_id); |
| 4022 | if (found) /* either found or error */ |
| 4023 | return found; |
| 4024 | } else if (strcmp(local_name, targ_name) == 0) { |
| 4025 | /* matching named field */ |
| 4026 | struct bpf_core_accessor *targ_acc; |
| 4027 | |
| 4028 | targ_acc = &spec->spec[spec->len++]; |
| 4029 | targ_acc->type_id = targ_id; |
| 4030 | targ_acc->idx = i; |
| 4031 | targ_acc->name = targ_name; |
| 4032 | |
| 4033 | *next_targ_id = m->type; |
| 4034 | found = bpf_core_fields_are_compat(local_btf, |
| 4035 | local_member->type, |
| 4036 | targ_btf, m->type); |
| 4037 | if (!found) |
| 4038 | spec->len--; /* pop accessor */ |
| 4039 | return found; |
| 4040 | } |
| 4041 | /* member turned out not to be what we looked for */ |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 4042 | spec->bit_offset -= bit_offset; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4043 | spec->raw_len--; |
| 4044 | } |
| 4045 | |
| 4046 | return 0; |
| 4047 | } |
| 4048 | |
| 4049 | /* |
| 4050 | * Try to match local spec to a target type and, if successful, produce full |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 4051 | * target spec (high-level, low-level + bit offset). |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4052 | */ |
| 4053 | static int bpf_core_spec_match(struct bpf_core_spec *local_spec, |
| 4054 | const struct btf *targ_btf, __u32 targ_id, |
| 4055 | struct bpf_core_spec *targ_spec) |
| 4056 | { |
| 4057 | const struct btf_type *targ_type; |
| 4058 | const struct bpf_core_accessor *local_acc; |
| 4059 | struct bpf_core_accessor *targ_acc; |
| 4060 | int i, sz, matched; |
| 4061 | |
| 4062 | memset(targ_spec, 0, sizeof(*targ_spec)); |
| 4063 | targ_spec->btf = targ_btf; |
| 4064 | |
| 4065 | local_acc = &local_spec->spec[0]; |
| 4066 | targ_acc = &targ_spec->spec[0]; |
| 4067 | |
| 4068 | for (i = 0; i < local_spec->len; i++, local_acc++, targ_acc++) { |
| 4069 | targ_type = skip_mods_and_typedefs(targ_spec->btf, targ_id, |
| 4070 | &targ_id); |
| 4071 | if (!targ_type) |
| 4072 | return -EINVAL; |
| 4073 | |
| 4074 | if (local_acc->name) { |
| 4075 | matched = bpf_core_match_member(local_spec->btf, |
| 4076 | local_acc, |
| 4077 | targ_btf, targ_id, |
| 4078 | targ_spec, &targ_id); |
| 4079 | if (matched <= 0) |
| 4080 | return matched; |
| 4081 | } else { |
| 4082 | /* for i=0, targ_id is already treated as array element |
| 4083 | * type (because it's the original struct), for others |
| 4084 | * we should find array element type first |
| 4085 | */ |
| 4086 | if (i > 0) { |
| 4087 | const struct btf_array *a; |
Andrii Nakryiko | 1b484b3 | 2019-12-14 23:08:43 -0800 | [diff] [blame] | 4088 | bool flex; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4089 | |
| 4090 | if (!btf_is_array(targ_type)) |
| 4091 | return 0; |
| 4092 | |
| 4093 | a = btf_array(targ_type); |
Andrii Nakryiko | 1b484b3 | 2019-12-14 23:08:43 -0800 | [diff] [blame] | 4094 | flex = is_flex_arr(targ_btf, targ_acc - 1, a); |
| 4095 | if (!flex && local_acc->idx >= a->nelems) |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4096 | return 0; |
| 4097 | if (!skip_mods_and_typedefs(targ_btf, a->type, |
| 4098 | &targ_id)) |
| 4099 | return -EINVAL; |
| 4100 | } |
| 4101 | |
| 4102 | /* too deep struct/union/array nesting */ |
| 4103 | if (targ_spec->raw_len == BPF_CORE_SPEC_MAX_LEN) |
| 4104 | return -E2BIG; |
| 4105 | |
| 4106 | targ_acc->type_id = targ_id; |
| 4107 | targ_acc->idx = local_acc->idx; |
| 4108 | targ_acc->name = NULL; |
| 4109 | targ_spec->len++; |
| 4110 | targ_spec->raw_spec[targ_spec->raw_len] = targ_acc->idx; |
| 4111 | targ_spec->raw_len++; |
| 4112 | |
| 4113 | sz = btf__resolve_size(targ_btf, targ_id); |
| 4114 | if (sz < 0) |
| 4115 | return sz; |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 4116 | targ_spec->bit_offset += local_acc->idx * sz * 8; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4117 | } |
| 4118 | } |
| 4119 | |
| 4120 | return 1; |
| 4121 | } |
| 4122 | |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 4123 | static int bpf_core_calc_field_relo(const struct bpf_program *prog, |
| 4124 | const struct bpf_field_reloc *relo, |
| 4125 | const struct bpf_core_spec *spec, |
| 4126 | __u32 *val, bool *validate) |
| 4127 | { |
| 4128 | const struct bpf_core_accessor *acc = &spec->spec[spec->len - 1]; |
| 4129 | const struct btf_type *t = btf__type_by_id(spec->btf, acc->type_id); |
| 4130 | __u32 byte_off, byte_sz, bit_off, bit_sz; |
| 4131 | const struct btf_member *m; |
| 4132 | const struct btf_type *mt; |
| 4133 | bool bitfield; |
Andrii Nakryiko | 94f060e | 2019-11-01 15:28:08 -0700 | [diff] [blame] | 4134 | __s64 sz; |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 4135 | |
| 4136 | /* a[n] accessor needs special handling */ |
| 4137 | if (!acc->name) { |
Andrii Nakryiko | 94f060e | 2019-11-01 15:28:08 -0700 | [diff] [blame] | 4138 | if (relo->kind == BPF_FIELD_BYTE_OFFSET) { |
| 4139 | *val = spec->bit_offset / 8; |
| 4140 | } else if (relo->kind == BPF_FIELD_BYTE_SIZE) { |
| 4141 | sz = btf__resolve_size(spec->btf, acc->type_id); |
| 4142 | if (sz < 0) |
| 4143 | return -EINVAL; |
| 4144 | *val = sz; |
| 4145 | } else { |
| 4146 | pr_warn("prog '%s': relo %d at insn #%d can't be applied to array access\n", |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 4147 | bpf_program__title(prog, false), |
| 4148 | relo->kind, relo->insn_off / 8); |
| 4149 | return -EINVAL; |
| 4150 | } |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 4151 | if (validate) |
| 4152 | *validate = true; |
| 4153 | return 0; |
| 4154 | } |
| 4155 | |
| 4156 | m = btf_members(t) + acc->idx; |
| 4157 | mt = skip_mods_and_typedefs(spec->btf, m->type, NULL); |
| 4158 | bit_off = spec->bit_offset; |
| 4159 | bit_sz = btf_member_bitfield_size(t, acc->idx); |
| 4160 | |
| 4161 | bitfield = bit_sz > 0; |
| 4162 | if (bitfield) { |
| 4163 | byte_sz = mt->size; |
| 4164 | byte_off = bit_off / 8 / byte_sz * byte_sz; |
| 4165 | /* figure out smallest int size necessary for bitfield load */ |
| 4166 | while (bit_off + bit_sz - byte_off * 8 > byte_sz * 8) { |
| 4167 | if (byte_sz >= 8) { |
| 4168 | /* bitfield can't be read with 64-bit read */ |
| 4169 | pr_warn("prog '%s': relo %d at insn #%d can't be satisfied for bitfield\n", |
| 4170 | bpf_program__title(prog, false), |
| 4171 | relo->kind, relo->insn_off / 8); |
| 4172 | return -E2BIG; |
| 4173 | } |
| 4174 | byte_sz *= 2; |
| 4175 | byte_off = bit_off / 8 / byte_sz * byte_sz; |
| 4176 | } |
| 4177 | } else { |
Andrii Nakryiko | 94f060e | 2019-11-01 15:28:08 -0700 | [diff] [blame] | 4178 | sz = btf__resolve_size(spec->btf, m->type); |
| 4179 | if (sz < 0) |
| 4180 | return -EINVAL; |
| 4181 | byte_sz = sz; |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 4182 | byte_off = spec->bit_offset / 8; |
| 4183 | bit_sz = byte_sz * 8; |
| 4184 | } |
| 4185 | |
| 4186 | /* for bitfields, all the relocatable aspects are ambiguous and we |
| 4187 | * might disagree with compiler, so turn off validation of expected |
| 4188 | * value, except for signedness |
| 4189 | */ |
| 4190 | if (validate) |
| 4191 | *validate = !bitfield; |
| 4192 | |
| 4193 | switch (relo->kind) { |
| 4194 | case BPF_FIELD_BYTE_OFFSET: |
| 4195 | *val = byte_off; |
| 4196 | break; |
| 4197 | case BPF_FIELD_BYTE_SIZE: |
| 4198 | *val = byte_sz; |
| 4199 | break; |
| 4200 | case BPF_FIELD_SIGNED: |
| 4201 | /* enums will be assumed unsigned */ |
| 4202 | *val = btf_is_enum(mt) || |
| 4203 | (btf_int_encoding(mt) & BTF_INT_SIGNED); |
| 4204 | if (validate) |
| 4205 | *validate = true; /* signedness is never ambiguous */ |
| 4206 | break; |
| 4207 | case BPF_FIELD_LSHIFT_U64: |
| 4208 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 4209 | *val = 64 - (bit_off + bit_sz - byte_off * 8); |
| 4210 | #else |
| 4211 | *val = (8 - byte_sz) * 8 + (bit_off - byte_off * 8); |
| 4212 | #endif |
| 4213 | break; |
| 4214 | case BPF_FIELD_RSHIFT_U64: |
| 4215 | *val = 64 - bit_sz; |
| 4216 | if (validate) |
| 4217 | *validate = true; /* right shift is never ambiguous */ |
| 4218 | break; |
| 4219 | case BPF_FIELD_EXISTS: |
| 4220 | default: |
| 4221 | pr_warn("prog '%s': unknown relo %d at insn #%d\n", |
| 4222 | bpf_program__title(prog, false), |
| 4223 | relo->kind, relo->insn_off / 8); |
| 4224 | return -EINVAL; |
| 4225 | } |
| 4226 | |
| 4227 | return 0; |
| 4228 | } |
| 4229 | |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4230 | /* |
| 4231 | * Patch relocatable BPF instruction. |
Andrii Nakryiko | 62561eb | 2019-10-15 11:28:47 -0700 | [diff] [blame] | 4232 | * |
| 4233 | * Patched value is determined by relocation kind and target specification. |
| 4234 | * For field existence relocation target spec will be NULL if field is not |
| 4235 | * found. |
| 4236 | * Expected insn->imm value is determined using relocation kind and local |
| 4237 | * spec, and is checked before patching instruction. If actual insn->imm value |
| 4238 | * is wrong, bail out with error. |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4239 | * |
| 4240 | * Currently three kinds of BPF instructions are supported: |
| 4241 | * 1. rX = <imm> (assignment with immediate operand); |
| 4242 | * 2. rX += <imm> (arithmetic operations with immediate operand); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4243 | */ |
Andrii Nakryiko | 62561eb | 2019-10-15 11:28:47 -0700 | [diff] [blame] | 4244 | static int bpf_core_reloc_insn(struct bpf_program *prog, |
| 4245 | const struct bpf_field_reloc *relo, |
Andrii Nakryiko | d7a2527 | 2020-01-23 21:38:37 -0800 | [diff] [blame] | 4246 | int relo_idx, |
Andrii Nakryiko | 62561eb | 2019-10-15 11:28:47 -0700 | [diff] [blame] | 4247 | const struct bpf_core_spec *local_spec, |
| 4248 | const struct bpf_core_spec *targ_spec) |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4249 | { |
Andrii Nakryiko | 62561eb | 2019-10-15 11:28:47 -0700 | [diff] [blame] | 4250 | __u32 orig_val, new_val; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4251 | struct bpf_insn *insn; |
Andrii Nakryiko | d7a2527 | 2020-01-23 21:38:37 -0800 | [diff] [blame] | 4252 | bool validate = true; |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 4253 | int insn_idx, err; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4254 | __u8 class; |
| 4255 | |
Andrii Nakryiko | 62561eb | 2019-10-15 11:28:47 -0700 | [diff] [blame] | 4256 | if (relo->insn_off % sizeof(struct bpf_insn)) |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4257 | return -EINVAL; |
Andrii Nakryiko | 62561eb | 2019-10-15 11:28:47 -0700 | [diff] [blame] | 4258 | insn_idx = relo->insn_off / sizeof(struct bpf_insn); |
Andrii Nakryiko | d7a2527 | 2020-01-23 21:38:37 -0800 | [diff] [blame] | 4259 | insn = &prog->insns[insn_idx]; |
| 4260 | class = BPF_CLASS(insn->code); |
Andrii Nakryiko | 62561eb | 2019-10-15 11:28:47 -0700 | [diff] [blame] | 4261 | |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 4262 | if (relo->kind == BPF_FIELD_EXISTS) { |
Andrii Nakryiko | 62561eb | 2019-10-15 11:28:47 -0700 | [diff] [blame] | 4263 | orig_val = 1; /* can't generate EXISTS relo w/o local field */ |
| 4264 | new_val = targ_spec ? 1 : 0; |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 4265 | } else if (!targ_spec) { |
Andrii Nakryiko | d7a2527 | 2020-01-23 21:38:37 -0800 | [diff] [blame] | 4266 | pr_debug("prog '%s': relo #%d: substituting insn #%d w/ invalid insn\n", |
| 4267 | bpf_program__title(prog, false), relo_idx, insn_idx); |
| 4268 | insn->code = BPF_JMP | BPF_CALL; |
| 4269 | insn->dst_reg = 0; |
| 4270 | insn->src_reg = 0; |
| 4271 | insn->off = 0; |
| 4272 | /* if this instruction is reachable (not a dead code), |
| 4273 | * verifier will complain with the following message: |
| 4274 | * invalid func unknown#195896080 |
| 4275 | */ |
| 4276 | insn->imm = 195896080; /* => 0xbad2310 => "bad relo" */ |
| 4277 | return 0; |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 4278 | } else { |
| 4279 | err = bpf_core_calc_field_relo(prog, relo, local_spec, |
| 4280 | &orig_val, &validate); |
| 4281 | if (err) |
| 4282 | return err; |
| 4283 | err = bpf_core_calc_field_relo(prog, relo, targ_spec, |
| 4284 | &new_val, NULL); |
| 4285 | if (err) |
| 4286 | return err; |
Andrii Nakryiko | 62561eb | 2019-10-15 11:28:47 -0700 | [diff] [blame] | 4287 | } |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4288 | |
Andrii Nakryiko | 8ab9da5 | 2019-12-23 10:03:05 -0800 | [diff] [blame] | 4289 | switch (class) { |
| 4290 | case BPF_ALU: |
| 4291 | case BPF_ALU64: |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4292 | if (BPF_SRC(insn->code) != BPF_K) |
| 4293 | return -EINVAL; |
Andrii Nakryiko | d7a2527 | 2020-01-23 21:38:37 -0800 | [diff] [blame] | 4294 | if (validate && insn->imm != orig_val) { |
| 4295 | pr_warn("prog '%s': relo #%d: unexpected insn #%d (ALU/ALU64) value: got %u, exp %u -> %u\n", |
| 4296 | bpf_program__title(prog, false), relo_idx, |
| 4297 | insn_idx, insn->imm, orig_val, new_val); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4298 | return -EINVAL; |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 4299 | } |
| 4300 | orig_val = insn->imm; |
Andrii Nakryiko | 62561eb | 2019-10-15 11:28:47 -0700 | [diff] [blame] | 4301 | insn->imm = new_val; |
Andrii Nakryiko | d7a2527 | 2020-01-23 21:38:37 -0800 | [diff] [blame] | 4302 | pr_debug("prog '%s': relo #%d: patched insn #%d (ALU/ALU64) imm %u -> %u\n", |
| 4303 | bpf_program__title(prog, false), relo_idx, insn_idx, |
| 4304 | orig_val, new_val); |
Andrii Nakryiko | 8ab9da5 | 2019-12-23 10:03:05 -0800 | [diff] [blame] | 4305 | break; |
| 4306 | case BPF_LDX: |
| 4307 | case BPF_ST: |
| 4308 | case BPF_STX: |
Andrii Nakryiko | d7a2527 | 2020-01-23 21:38:37 -0800 | [diff] [blame] | 4309 | if (validate && insn->off != orig_val) { |
| 4310 | pr_warn("prog '%s': relo #%d: unexpected insn #%d (LD/LDX/ST/STX) value: got %u, exp %u -> %u\n", |
| 4311 | bpf_program__title(prog, false), relo_idx, |
| 4312 | insn_idx, insn->off, orig_val, new_val); |
Andrii Nakryiko | 8ab9da5 | 2019-12-23 10:03:05 -0800 | [diff] [blame] | 4313 | return -EINVAL; |
| 4314 | } |
| 4315 | if (new_val > SHRT_MAX) { |
Andrii Nakryiko | d7a2527 | 2020-01-23 21:38:37 -0800 | [diff] [blame] | 4316 | pr_warn("prog '%s': relo #%d: insn #%d (LDX/ST/STX) value too big: %u\n", |
| 4317 | bpf_program__title(prog, false), relo_idx, |
| 4318 | insn_idx, new_val); |
Andrii Nakryiko | 8ab9da5 | 2019-12-23 10:03:05 -0800 | [diff] [blame] | 4319 | return -ERANGE; |
| 4320 | } |
| 4321 | orig_val = insn->off; |
| 4322 | insn->off = new_val; |
Andrii Nakryiko | d7a2527 | 2020-01-23 21:38:37 -0800 | [diff] [blame] | 4323 | pr_debug("prog '%s': relo #%d: patched insn #%d (LDX/ST/STX) off %u -> %u\n", |
| 4324 | bpf_program__title(prog, false), relo_idx, insn_idx, |
| 4325 | orig_val, new_val); |
Andrii Nakryiko | 8ab9da5 | 2019-12-23 10:03:05 -0800 | [diff] [blame] | 4326 | break; |
| 4327 | default: |
Andrii Nakryiko | d7a2527 | 2020-01-23 21:38:37 -0800 | [diff] [blame] | 4328 | pr_warn("prog '%s': relo #%d: trying to relocate unrecognized insn #%d, code:%x, src:%x, dst:%x, off:%x, imm:%x\n", |
| 4329 | bpf_program__title(prog, false), relo_idx, |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4330 | insn_idx, insn->code, insn->src_reg, insn->dst_reg, |
| 4331 | insn->off, insn->imm); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4332 | return -EINVAL; |
| 4333 | } |
Andrii Nakryiko | 62561eb | 2019-10-15 11:28:47 -0700 | [diff] [blame] | 4334 | |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4335 | return 0; |
| 4336 | } |
| 4337 | |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4338 | /* Output spec definition in the format: |
| 4339 | * [<type-id>] (<type-name>) + <raw-spec> => <offset>@<spec>, |
| 4340 | * where <spec> is a C-syntax view of recorded field access, e.g.: x.a[3].b |
| 4341 | */ |
| 4342 | static void bpf_core_dump_spec(int level, const struct bpf_core_spec *spec) |
| 4343 | { |
| 4344 | const struct btf_type *t; |
| 4345 | const char *s; |
| 4346 | __u32 type_id; |
| 4347 | int i; |
| 4348 | |
| 4349 | type_id = spec->spec[0].type_id; |
| 4350 | t = btf__type_by_id(spec->btf, type_id); |
| 4351 | s = btf__name_by_offset(spec->btf, t->name_off); |
| 4352 | libbpf_print(level, "[%u] %s + ", type_id, s); |
| 4353 | |
| 4354 | for (i = 0; i < spec->raw_len; i++) |
| 4355 | libbpf_print(level, "%d%s", spec->raw_spec[i], |
| 4356 | i == spec->raw_len - 1 ? " => " : ":"); |
| 4357 | |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 4358 | libbpf_print(level, "%u.%u @ &x", |
| 4359 | spec->bit_offset / 8, spec->bit_offset % 8); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4360 | |
| 4361 | for (i = 0; i < spec->len; i++) { |
| 4362 | if (spec->spec[i].name) |
| 4363 | libbpf_print(level, ".%s", spec->spec[i].name); |
| 4364 | else |
| 4365 | libbpf_print(level, "[%u]", spec->spec[i].idx); |
| 4366 | } |
| 4367 | |
| 4368 | } |
| 4369 | |
| 4370 | static size_t bpf_core_hash_fn(const void *key, void *ctx) |
| 4371 | { |
| 4372 | return (size_t)key; |
| 4373 | } |
| 4374 | |
| 4375 | static bool bpf_core_equal_fn(const void *k1, const void *k2, void *ctx) |
| 4376 | { |
| 4377 | return k1 == k2; |
| 4378 | } |
| 4379 | |
| 4380 | static void *u32_as_hash_key(__u32 x) |
| 4381 | { |
| 4382 | return (void *)(uintptr_t)x; |
| 4383 | } |
| 4384 | |
| 4385 | /* |
| 4386 | * CO-RE relocate single instruction. |
| 4387 | * |
| 4388 | * The outline and important points of the algorithm: |
| 4389 | * 1. For given local type, find corresponding candidate target types. |
| 4390 | * Candidate type is a type with the same "essential" name, ignoring |
| 4391 | * everything after last triple underscore (___). E.g., `sample`, |
| 4392 | * `sample___flavor_one`, `sample___flavor_another_one`, are all candidates |
| 4393 | * for each other. Names with triple underscore are referred to as |
| 4394 | * "flavors" and are useful, among other things, to allow to |
| 4395 | * specify/support incompatible variations of the same kernel struct, which |
| 4396 | * might differ between different kernel versions and/or build |
| 4397 | * configurations. |
| 4398 | * |
| 4399 | * N.B. Struct "flavors" could be generated by bpftool's BTF-to-C |
| 4400 | * converter, when deduplicated BTF of a kernel still contains more than |
| 4401 | * one different types with the same name. In that case, ___2, ___3, etc |
| 4402 | * are appended starting from second name conflict. But start flavors are |
| 4403 | * also useful to be defined "locally", in BPF program, to extract same |
| 4404 | * data from incompatible changes between different kernel |
| 4405 | * versions/configurations. For instance, to handle field renames between |
| 4406 | * kernel versions, one can use two flavors of the struct name with the |
| 4407 | * same common name and use conditional relocations to extract that field, |
| 4408 | * depending on target kernel version. |
| 4409 | * 2. For each candidate type, try to match local specification to this |
| 4410 | * candidate target type. Matching involves finding corresponding |
| 4411 | * high-level spec accessors, meaning that all named fields should match, |
| 4412 | * as well as all array accesses should be within the actual bounds. Also, |
| 4413 | * types should be compatible (see bpf_core_fields_are_compat for details). |
| 4414 | * 3. It is supported and expected that there might be multiple flavors |
| 4415 | * matching the spec. As long as all the specs resolve to the same set of |
Andrii Nakryiko | 511bb00 | 2019-10-15 11:28:45 -0700 | [diff] [blame] | 4416 | * offsets across all candidates, there is no error. If there is any |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4417 | * ambiguity, CO-RE relocation will fail. This is necessary to accomodate |
| 4418 | * imprefection of BTF deduplication, which can cause slight duplication of |
| 4419 | * the same BTF type, if some directly or indirectly referenced (by |
| 4420 | * pointer) type gets resolved to different actual types in different |
| 4421 | * object files. If such situation occurs, deduplicated BTF will end up |
| 4422 | * with two (or more) structurally identical types, which differ only in |
| 4423 | * types they refer to through pointer. This should be OK in most cases and |
| 4424 | * is not an error. |
| 4425 | * 4. Candidate types search is performed by linearly scanning through all |
| 4426 | * types in target BTF. It is anticipated that this is overall more |
| 4427 | * efficient memory-wise and not significantly worse (if not better) |
| 4428 | * CPU-wise compared to prebuilding a map from all local type names to |
| 4429 | * a list of candidate type names. It's also sped up by caching resolved |
| 4430 | * list of matching candidates per each local "root" type ID, that has at |
Andrii Nakryiko | 511bb00 | 2019-10-15 11:28:45 -0700 | [diff] [blame] | 4431 | * least one bpf_field_reloc associated with it. This list is shared |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4432 | * between multiple relocations for the same type ID and is updated as some |
| 4433 | * of the candidates are pruned due to structural incompatibility. |
| 4434 | */ |
Andrii Nakryiko | 511bb00 | 2019-10-15 11:28:45 -0700 | [diff] [blame] | 4435 | static int bpf_core_reloc_field(struct bpf_program *prog, |
| 4436 | const struct bpf_field_reloc *relo, |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4437 | int relo_idx, |
| 4438 | const struct btf *local_btf, |
| 4439 | const struct btf *targ_btf, |
| 4440 | struct hashmap *cand_cache) |
| 4441 | { |
| 4442 | const char *prog_name = bpf_program__title(prog, false); |
| 4443 | struct bpf_core_spec local_spec, cand_spec, targ_spec; |
| 4444 | const void *type_key = u32_as_hash_key(relo->type_id); |
| 4445 | const struct btf_type *local_type, *cand_type; |
| 4446 | const char *local_name, *cand_name; |
| 4447 | struct ids_vec *cand_ids; |
| 4448 | __u32 local_id, cand_id; |
| 4449 | const char *spec_str; |
| 4450 | int i, j, err; |
| 4451 | |
| 4452 | local_id = relo->type_id; |
| 4453 | local_type = btf__type_by_id(local_btf, local_id); |
| 4454 | if (!local_type) |
| 4455 | return -EINVAL; |
| 4456 | |
| 4457 | local_name = btf__name_by_offset(local_btf, local_type->name_off); |
| 4458 | if (str_is_empty(local_name)) |
| 4459 | return -EINVAL; |
| 4460 | |
| 4461 | spec_str = btf__name_by_offset(local_btf, relo->access_str_off); |
| 4462 | if (str_is_empty(spec_str)) |
| 4463 | return -EINVAL; |
| 4464 | |
| 4465 | err = bpf_core_spec_parse(local_btf, local_id, spec_str, &local_spec); |
| 4466 | if (err) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4467 | pr_warn("prog '%s': relo #%d: parsing [%d] %s + %s failed: %d\n", |
| 4468 | prog_name, relo_idx, local_id, local_name, spec_str, |
| 4469 | err); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4470 | return -EINVAL; |
| 4471 | } |
| 4472 | |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 4473 | pr_debug("prog '%s': relo #%d: kind %d, spec is ", prog_name, relo_idx, |
| 4474 | relo->kind); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4475 | bpf_core_dump_spec(LIBBPF_DEBUG, &local_spec); |
| 4476 | libbpf_print(LIBBPF_DEBUG, "\n"); |
| 4477 | |
| 4478 | if (!hashmap__find(cand_cache, type_key, (void **)&cand_ids)) { |
| 4479 | cand_ids = bpf_core_find_cands(local_btf, local_id, targ_btf); |
| 4480 | if (IS_ERR(cand_ids)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4481 | pr_warn("prog '%s': relo #%d: target candidate search failed for [%d] %s: %ld", |
| 4482 | prog_name, relo_idx, local_id, local_name, |
| 4483 | PTR_ERR(cand_ids)); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4484 | return PTR_ERR(cand_ids); |
| 4485 | } |
| 4486 | err = hashmap__set(cand_cache, type_key, cand_ids, NULL, NULL); |
| 4487 | if (err) { |
| 4488 | bpf_core_free_cands(cand_ids); |
| 4489 | return err; |
| 4490 | } |
| 4491 | } |
| 4492 | |
| 4493 | for (i = 0, j = 0; i < cand_ids->len; i++) { |
| 4494 | cand_id = cand_ids->data[i]; |
| 4495 | cand_type = btf__type_by_id(targ_btf, cand_id); |
| 4496 | cand_name = btf__name_by_offset(targ_btf, cand_type->name_off); |
| 4497 | |
| 4498 | err = bpf_core_spec_match(&local_spec, targ_btf, |
| 4499 | cand_id, &cand_spec); |
| 4500 | pr_debug("prog '%s': relo #%d: matching candidate #%d %s against spec ", |
| 4501 | prog_name, relo_idx, i, cand_name); |
| 4502 | bpf_core_dump_spec(LIBBPF_DEBUG, &cand_spec); |
| 4503 | libbpf_print(LIBBPF_DEBUG, ": %d\n", err); |
| 4504 | if (err < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4505 | pr_warn("prog '%s': relo #%d: matching error: %d\n", |
| 4506 | prog_name, relo_idx, err); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4507 | return err; |
| 4508 | } |
| 4509 | if (err == 0) |
| 4510 | continue; |
| 4511 | |
| 4512 | if (j == 0) { |
| 4513 | targ_spec = cand_spec; |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 4514 | } else if (cand_spec.bit_offset != targ_spec.bit_offset) { |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4515 | /* if there are many candidates, they should all |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 4516 | * resolve to the same bit offset |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4517 | */ |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4518 | pr_warn("prog '%s': relo #%d: offset ambiguity: %u != %u\n", |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 4519 | prog_name, relo_idx, cand_spec.bit_offset, |
| 4520 | targ_spec.bit_offset); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4521 | return -EINVAL; |
| 4522 | } |
| 4523 | |
| 4524 | cand_ids->data[j++] = cand_spec.spec[0].type_id; |
| 4525 | } |
| 4526 | |
Andrii Nakryiko | 62561eb | 2019-10-15 11:28:47 -0700 | [diff] [blame] | 4527 | /* |
Andrii Nakryiko | d7a2527 | 2020-01-23 21:38:37 -0800 | [diff] [blame] | 4528 | * For BPF_FIELD_EXISTS relo or when used BPF program has field |
| 4529 | * existence checks or kernel version/config checks, it's expected |
| 4530 | * that we might not find any candidates. In this case, if field |
| 4531 | * wasn't found in any candidate, the list of candidates shouldn't |
| 4532 | * change at all, we'll just handle relocating appropriately, |
| 4533 | * depending on relo's kind. |
Andrii Nakryiko | 62561eb | 2019-10-15 11:28:47 -0700 | [diff] [blame] | 4534 | */ |
| 4535 | if (j > 0) |
| 4536 | cand_ids->len = j; |
| 4537 | |
Andrii Nakryiko | d7a2527 | 2020-01-23 21:38:37 -0800 | [diff] [blame] | 4538 | /* |
| 4539 | * If no candidates were found, it might be both a programmer error, |
| 4540 | * as well as expected case, depending whether instruction w/ |
| 4541 | * relocation is guarded in some way that makes it unreachable (dead |
| 4542 | * code) if relocation can't be resolved. This is handled in |
| 4543 | * bpf_core_reloc_insn() uniformly by replacing that instruction with |
| 4544 | * BPF helper call insn (using invalid helper ID). If that instruction |
| 4545 | * is indeed unreachable, then it will be ignored and eliminated by |
| 4546 | * verifier. If it was an error, then verifier will complain and point |
| 4547 | * to a specific instruction number in its log. |
| 4548 | */ |
| 4549 | if (j == 0) |
| 4550 | pr_debug("prog '%s': relo #%d: no matching targets found for [%d] %s + %s\n", |
| 4551 | prog_name, relo_idx, local_id, local_name, spec_str); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4552 | |
Andrii Nakryiko | 62561eb | 2019-10-15 11:28:47 -0700 | [diff] [blame] | 4553 | /* bpf_core_reloc_insn should know how to handle missing targ_spec */ |
Andrii Nakryiko | d7a2527 | 2020-01-23 21:38:37 -0800 | [diff] [blame] | 4554 | err = bpf_core_reloc_insn(prog, relo, relo_idx, &local_spec, |
Andrii Nakryiko | 62561eb | 2019-10-15 11:28:47 -0700 | [diff] [blame] | 4555 | j ? &targ_spec : NULL); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4556 | if (err) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4557 | pr_warn("prog '%s': relo #%d: failed to patch insn at offset %d: %d\n", |
| 4558 | prog_name, relo_idx, relo->insn_off, err); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4559 | return -EINVAL; |
| 4560 | } |
| 4561 | |
| 4562 | return 0; |
| 4563 | } |
| 4564 | |
| 4565 | static int |
Andrii Nakryiko | 511bb00 | 2019-10-15 11:28:45 -0700 | [diff] [blame] | 4566 | bpf_core_reloc_fields(struct bpf_object *obj, const char *targ_btf_path) |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4567 | { |
| 4568 | const struct btf_ext_info_sec *sec; |
Andrii Nakryiko | 511bb00 | 2019-10-15 11:28:45 -0700 | [diff] [blame] | 4569 | const struct bpf_field_reloc *rec; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4570 | const struct btf_ext_info *seg; |
| 4571 | struct hashmap_entry *entry; |
| 4572 | struct hashmap *cand_cache = NULL; |
| 4573 | struct bpf_program *prog; |
| 4574 | struct btf *targ_btf; |
| 4575 | const char *sec_name; |
| 4576 | int i, err = 0; |
| 4577 | |
| 4578 | if (targ_btf_path) |
| 4579 | targ_btf = btf__parse_elf(targ_btf_path, NULL); |
| 4580 | else |
Martin KaFai Lau | fb2426a | 2020-01-15 15:00:31 -0800 | [diff] [blame] | 4581 | targ_btf = libbpf_find_kernel_btf(); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4582 | if (IS_ERR(targ_btf)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4583 | pr_warn("failed to get target BTF: %ld\n", PTR_ERR(targ_btf)); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4584 | return PTR_ERR(targ_btf); |
| 4585 | } |
| 4586 | |
| 4587 | cand_cache = hashmap__new(bpf_core_hash_fn, bpf_core_equal_fn, NULL); |
| 4588 | if (IS_ERR(cand_cache)) { |
| 4589 | err = PTR_ERR(cand_cache); |
| 4590 | goto out; |
| 4591 | } |
| 4592 | |
Andrii Nakryiko | 511bb00 | 2019-10-15 11:28:45 -0700 | [diff] [blame] | 4593 | seg = &obj->btf_ext->field_reloc_info; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4594 | for_each_btf_ext_sec(seg, sec) { |
| 4595 | sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off); |
| 4596 | if (str_is_empty(sec_name)) { |
| 4597 | err = -EINVAL; |
| 4598 | goto out; |
| 4599 | } |
| 4600 | prog = bpf_object__find_program_by_title(obj, sec_name); |
| 4601 | if (!prog) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4602 | pr_warn("failed to find program '%s' for CO-RE offset relocation\n", |
| 4603 | sec_name); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4604 | err = -EINVAL; |
| 4605 | goto out; |
| 4606 | } |
| 4607 | |
| 4608 | pr_debug("prog '%s': performing %d CO-RE offset relocs\n", |
| 4609 | sec_name, sec->num_info); |
| 4610 | |
| 4611 | for_each_btf_ext_rec(seg, sec, i, rec) { |
Andrii Nakryiko | 511bb00 | 2019-10-15 11:28:45 -0700 | [diff] [blame] | 4612 | err = bpf_core_reloc_field(prog, rec, i, obj->btf, |
| 4613 | targ_btf, cand_cache); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4614 | if (err) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4615 | pr_warn("prog '%s': relo #%d: failed to relocate: %d\n", |
| 4616 | sec_name, i, err); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4617 | goto out; |
| 4618 | } |
| 4619 | } |
| 4620 | } |
| 4621 | |
| 4622 | out: |
| 4623 | btf__free(targ_btf); |
| 4624 | if (!IS_ERR_OR_NULL(cand_cache)) { |
| 4625 | hashmap__for_each_entry(cand_cache, entry, i) { |
| 4626 | bpf_core_free_cands(entry->value); |
| 4627 | } |
| 4628 | hashmap__free(cand_cache); |
| 4629 | } |
| 4630 | return err; |
| 4631 | } |
| 4632 | |
| 4633 | static int |
| 4634 | bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path) |
| 4635 | { |
| 4636 | int err = 0; |
| 4637 | |
Andrii Nakryiko | 511bb00 | 2019-10-15 11:28:45 -0700 | [diff] [blame] | 4638 | if (obj->btf_ext->field_reloc_info.len) |
| 4639 | err = bpf_core_reloc_fields(obj, targ_btf_path); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4640 | |
| 4641 | return err; |
| 4642 | } |
| 4643 | |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 4644 | static int |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 4645 | bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj, |
| 4646 | struct reloc_desc *relo) |
| 4647 | { |
| 4648 | struct bpf_insn *insn, *new_insn; |
| 4649 | struct bpf_program *text; |
| 4650 | size_t new_cnt; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 4651 | int err; |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 4652 | |
Andrii Nakryiko | 9173cac | 2020-01-15 11:08:56 -0800 | [diff] [blame] | 4653 | if (prog->idx != obj->efile.text_shndx && prog->main_prog_cnt == 0) { |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 4654 | text = bpf_object__find_prog_by_idx(obj, obj->efile.text_shndx); |
| 4655 | if (!text) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4656 | pr_warn("no .text section found yet relo into text exist\n"); |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 4657 | return -LIBBPF_ERRNO__RELOC; |
| 4658 | } |
| 4659 | new_cnt = prog->insns_cnt + text->insns_cnt; |
Jakub Kicinski | 531b014 | 2018-07-10 14:43:05 -0700 | [diff] [blame] | 4660 | new_insn = reallocarray(prog->insns, new_cnt, sizeof(*insn)); |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 4661 | if (!new_insn) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4662 | pr_warn("oom in prog realloc\n"); |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 4663 | return -ENOMEM; |
| 4664 | } |
Andrii Nakryiko | 3dc5e05 | 2019-11-06 18:08:51 -0800 | [diff] [blame] | 4665 | prog->insns = new_insn; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 4666 | |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 4667 | if (obj->btf_ext) { |
| 4668 | err = bpf_program_reloc_btf_ext(prog, obj, |
| 4669 | text->section_name, |
| 4670 | prog->insns_cnt); |
| 4671 | if (err) |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 4672 | return err; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 4673 | } |
| 4674 | |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 4675 | memcpy(new_insn + prog->insns_cnt, text->insns, |
| 4676 | text->insns_cnt * sizeof(*insn)); |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 4677 | prog->main_prog_cnt = prog->insns_cnt; |
| 4678 | prog->insns_cnt = new_cnt; |
Jeremy Cline | b1a2ce8 | 2018-02-20 01:00:07 +0000 | [diff] [blame] | 4679 | pr_debug("added %zd insn from %s to prog %s\n", |
| 4680 | text->insns_cnt, text->section_name, |
| 4681 | prog->section_name); |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 4682 | } |
Andrii Nakryiko | 9173cac | 2020-01-15 11:08:56 -0800 | [diff] [blame] | 4683 | |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 4684 | insn = &prog->insns[relo->insn_idx]; |
Andrii Nakryiko | 53f8dd4 | 2019-11-27 12:06:50 -0800 | [diff] [blame] | 4685 | insn->imm += relo->sym_off / 8 + prog->main_prog_cnt - relo->insn_idx; |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 4686 | return 0; |
| 4687 | } |
| 4688 | |
| 4689 | static int |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 4690 | bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj) |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 4691 | { |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 4692 | int i, err; |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 4693 | |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 4694 | if (!prog) |
| 4695 | return 0; |
| 4696 | |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 4697 | if (obj->btf_ext) { |
| 4698 | err = bpf_program_reloc_btf_ext(prog, obj, |
| 4699 | prog->section_name, 0); |
| 4700 | if (err) |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 4701 | return err; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 4702 | } |
| 4703 | |
| 4704 | if (!prog->reloc_desc) |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 4705 | return 0; |
| 4706 | |
| 4707 | for (i = 0; i < prog->nr_reloc; i++) { |
Andrii Nakryiko | 53f8dd4 | 2019-11-27 12:06:50 -0800 | [diff] [blame] | 4708 | struct reloc_desc *relo = &prog->reloc_desc[i]; |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 4709 | struct bpf_insn *insn = &prog->insns[relo->insn_idx]; |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 4710 | |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 4711 | if (relo->insn_idx + 1 >= (int)prog->insns_cnt) { |
| 4712 | pr_warn("relocation out of range: '%s'\n", |
| 4713 | prog->section_name); |
| 4714 | return -LIBBPF_ERRNO__RELOC; |
| 4715 | } |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 4716 | |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 4717 | switch (relo->type) { |
| 4718 | case RELO_LD64: |
| 4719 | insn[0].src_reg = BPF_PSEUDO_MAP_FD; |
Andrii Nakryiko | 53f8dd4 | 2019-11-27 12:06:50 -0800 | [diff] [blame] | 4720 | insn[0].imm = obj->maps[relo->map_idx].fd; |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 4721 | break; |
| 4722 | case RELO_DATA: |
| 4723 | insn[0].src_reg = BPF_PSEUDO_MAP_VALUE; |
| 4724 | insn[1].imm = insn[0].imm + relo->sym_off; |
| 4725 | insn[0].imm = obj->maps[relo->map_idx].fd; |
| 4726 | break; |
| 4727 | case RELO_EXTERN: |
| 4728 | insn[0].src_reg = BPF_PSEUDO_MAP_VALUE; |
Andrii Nakryiko | 81bfdd0 | 2019-12-18 16:28:34 -0800 | [diff] [blame] | 4729 | insn[0].imm = obj->maps[obj->kconfig_map_idx].fd; |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 4730 | insn[1].imm = relo->sym_off; |
| 4731 | break; |
| 4732 | case RELO_CALL: |
Andrii Nakryiko | 53f8dd4 | 2019-11-27 12:06:50 -0800 | [diff] [blame] | 4733 | err = bpf_program__reloc_text(prog, obj, relo); |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 4734 | if (err) |
| 4735 | return err; |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 4736 | break; |
| 4737 | default: |
| 4738 | pr_warn("relo #%d: bad relo type %d\n", i, relo->type); |
| 4739 | return -EINVAL; |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 4740 | } |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 4741 | } |
| 4742 | |
| 4743 | zfree(&prog->reloc_desc); |
| 4744 | prog->nr_reloc = 0; |
| 4745 | return 0; |
| 4746 | } |
| 4747 | |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 4748 | static int |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4749 | bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path) |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 4750 | { |
| 4751 | struct bpf_program *prog; |
| 4752 | size_t i; |
| 4753 | int err; |
| 4754 | |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4755 | if (obj->btf_ext) { |
| 4756 | err = bpf_object__relocate_core(obj, targ_btf_path); |
| 4757 | if (err) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4758 | pr_warn("failed to perform CO-RE relocations: %d\n", |
| 4759 | err); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4760 | return err; |
| 4761 | } |
| 4762 | } |
Andrii Nakryiko | 9173cac | 2020-01-15 11:08:56 -0800 | [diff] [blame] | 4763 | /* ensure .text is relocated first, as it's going to be copied as-is |
| 4764 | * later for sub-program calls |
| 4765 | */ |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 4766 | for (i = 0; i < obj->nr_programs; i++) { |
| 4767 | prog = &obj->programs[i]; |
Andrii Nakryiko | 9173cac | 2020-01-15 11:08:56 -0800 | [diff] [blame] | 4768 | if (prog->idx != obj->efile.text_shndx) |
| 4769 | continue; |
| 4770 | |
| 4771 | err = bpf_program__relocate(prog, obj); |
| 4772 | if (err) { |
| 4773 | pr_warn("failed to relocate '%s'\n", prog->section_name); |
| 4774 | return err; |
| 4775 | } |
| 4776 | break; |
| 4777 | } |
| 4778 | /* now relocate everything but .text, which by now is relocated |
| 4779 | * properly, so we can copy raw sub-program instructions as is safely |
| 4780 | */ |
| 4781 | for (i = 0; i < obj->nr_programs; i++) { |
| 4782 | prog = &obj->programs[i]; |
| 4783 | if (prog->idx == obj->efile.text_shndx) |
| 4784 | continue; |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 4785 | |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 4786 | err = bpf_program__relocate(prog, obj); |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 4787 | if (err) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4788 | pr_warn("failed to relocate '%s'\n", prog->section_name); |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 4789 | return err; |
| 4790 | } |
| 4791 | } |
| 4792 | return 0; |
| 4793 | } |
| 4794 | |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 4795 | static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, |
| 4796 | GElf_Shdr *shdr, |
| 4797 | Elf_Data *data); |
| 4798 | |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 4799 | static int bpf_object__collect_reloc(struct bpf_object *obj) |
| 4800 | { |
| 4801 | int i, err; |
| 4802 | |
| 4803 | if (!obj_elf_valid(obj)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4804 | pr_warn("Internal error: elf object is closed\n"); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 4805 | return -LIBBPF_ERRNO__INTERNAL; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 4806 | } |
| 4807 | |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 4808 | for (i = 0; i < obj->efile.nr_reloc_sects; i++) { |
| 4809 | GElf_Shdr *shdr = &obj->efile.reloc_sects[i].shdr; |
| 4810 | Elf_Data *data = obj->efile.reloc_sects[i].data; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 4811 | int idx = shdr->sh_info; |
| 4812 | struct bpf_program *prog; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 4813 | |
| 4814 | if (shdr->sh_type != SHT_REL) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4815 | pr_warn("internal error at %d\n", __LINE__); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 4816 | return -LIBBPF_ERRNO__INTERNAL; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 4817 | } |
| 4818 | |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 4819 | if (idx == obj->efile.st_ops_shndx) { |
| 4820 | err = bpf_object__collect_struct_ops_map_reloc(obj, |
| 4821 | shdr, |
| 4822 | data); |
| 4823 | if (err) |
| 4824 | return err; |
| 4825 | continue; |
| 4826 | } |
| 4827 | |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 4828 | prog = bpf_object__find_prog_by_idx(obj, idx); |
| 4829 | if (!prog) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4830 | pr_warn("relocation failed: no section(%d)\n", idx); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 4831 | return -LIBBPF_ERRNO__RELOC; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 4832 | } |
| 4833 | |
Andrii Nakryiko | 399dc65 | 2019-05-29 10:36:11 -0700 | [diff] [blame] | 4834 | err = bpf_program__collect_reloc(prog, shdr, data, obj); |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 4835 | if (err) |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 4836 | return err; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 4837 | } |
| 4838 | return 0; |
| 4839 | } |
| 4840 | |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 4841 | static int |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 4842 | load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt, |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 4843 | char *license, __u32 kern_version, int *pfd) |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 4844 | { |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 4845 | struct bpf_load_program_attr load_attr; |
Thomas Richter | 1ce6a9f | 2018-07-30 10:53:23 +0200 | [diff] [blame] | 4846 | char *cp, errmsg[STRERR_BUFSIZE]; |
Alexei Starovoitov | da11b41 | 2019-04-01 21:27:47 -0700 | [diff] [blame] | 4847 | int log_buf_size = BPF_LOG_BUF_SIZE; |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 4848 | char *log_buf; |
Andrii Nakryiko | 5d01ab7 | 2019-07-26 14:24:38 -0700 | [diff] [blame] | 4849 | int btf_fd, ret; |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 4850 | |
Andrii Nakryiko | fba01a0 | 2019-05-29 10:36:08 -0700 | [diff] [blame] | 4851 | if (!insns || !insns_cnt) |
| 4852 | return -EINVAL; |
| 4853 | |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 4854 | memset(&load_attr, 0, sizeof(struct bpf_load_program_attr)); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 4855 | load_attr.prog_type = prog->type; |
| 4856 | load_attr.expected_attach_type = prog->expected_attach_type; |
Stanislav Fomichev | 5b32a23 | 2018-11-20 17:11:21 -0800 | [diff] [blame] | 4857 | if (prog->caps->name) |
| 4858 | load_attr.name = prog->name; |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 4859 | load_attr.insns = insns; |
| 4860 | load_attr.insns_cnt = insns_cnt; |
| 4861 | load_attr.license = license; |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 4862 | if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) { |
| 4863 | load_attr.attach_btf_id = prog->attach_btf_id; |
Alexei Starovoitov | 2db6eab | 2020-01-20 16:53:47 -0800 | [diff] [blame] | 4864 | } else if (prog->type == BPF_PROG_TYPE_TRACING || |
| 4865 | prog->type == BPF_PROG_TYPE_EXT) { |
Alexei Starovoitov | e7bf94d | 2019-11-14 10:57:18 -0800 | [diff] [blame] | 4866 | load_attr.attach_prog_fd = prog->attach_prog_fd; |
| 4867 | load_attr.attach_btf_id = prog->attach_btf_id; |
| 4868 | } else { |
| 4869 | load_attr.kern_version = kern_version; |
| 4870 | load_attr.prog_ifindex = prog->prog_ifindex; |
| 4871 | } |
Andrii Nakryiko | 3415ec6 | 2019-08-01 00:24:05 -0700 | [diff] [blame] | 4872 | /* if .BTF.ext was loaded, kernel supports associated BTF for prog */ |
| 4873 | if (prog->obj->btf_ext) |
| 4874 | btf_fd = bpf_object__btf_fd(prog->obj); |
| 4875 | else |
| 4876 | btf_fd = -1; |
Andrii Nakryiko | 5d01ab7 | 2019-07-26 14:24:38 -0700 | [diff] [blame] | 4877 | load_attr.prog_btf_fd = btf_fd >= 0 ? btf_fd : 0; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 4878 | load_attr.func_info = prog->func_info; |
| 4879 | load_attr.func_info_rec_size = prog->func_info_rec_size; |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 4880 | load_attr.func_info_cnt = prog->func_info_cnt; |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 4881 | load_attr.line_info = prog->line_info; |
| 4882 | load_attr.line_info_rec_size = prog->line_info_rec_size; |
| 4883 | load_attr.line_info_cnt = prog->line_info_cnt; |
Alexei Starovoitov | da11b41 | 2019-04-01 21:27:47 -0700 | [diff] [blame] | 4884 | load_attr.log_level = prog->log_level; |
Jiong Wang | 0465619 | 2019-05-24 23:25:19 +0100 | [diff] [blame] | 4885 | load_attr.prog_flags = prog->prog_flags; |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 4886 | |
Alexei Starovoitov | da11b41 | 2019-04-01 21:27:47 -0700 | [diff] [blame] | 4887 | retry_load: |
| 4888 | log_buf = malloc(log_buf_size); |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 4889 | if (!log_buf) |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4890 | pr_warn("Alloc log buffer for bpf loader error, continue without log\n"); |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 4891 | |
Alexei Starovoitov | da11b41 | 2019-04-01 21:27:47 -0700 | [diff] [blame] | 4892 | ret = bpf_load_program_xattr(&load_attr, log_buf, log_buf_size); |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 4893 | |
| 4894 | if (ret >= 0) { |
Alexei Starovoitov | da11b41 | 2019-04-01 21:27:47 -0700 | [diff] [blame] | 4895 | if (load_attr.log_level) |
| 4896 | pr_debug("verifier log:\n%s", log_buf); |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 4897 | *pfd = ret; |
| 4898 | ret = 0; |
| 4899 | goto out; |
| 4900 | } |
| 4901 | |
Alexei Starovoitov | da11b41 | 2019-04-01 21:27:47 -0700 | [diff] [blame] | 4902 | if (errno == ENOSPC) { |
| 4903 | log_buf_size <<= 1; |
| 4904 | free(log_buf); |
| 4905 | goto retry_load; |
| 4906 | } |
Toke Høiland-Jørgensen | 4f33ddb | 2019-11-09 21:37:29 +0100 | [diff] [blame] | 4907 | ret = -errno; |
Andrey Ignatov | 24d6a80 | 2018-10-03 15:26:41 -0700 | [diff] [blame] | 4908 | cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg)); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4909 | pr_warn("load bpf program failed: %s\n", cp); |
Toke Høiland-Jørgensen | dc3a2d2 | 2019-12-16 19:12:04 +0100 | [diff] [blame] | 4910 | pr_perm_msg(ret); |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 4911 | |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 4912 | if (log_buf && log_buf[0] != '\0') { |
| 4913 | ret = -LIBBPF_ERRNO__VERIFY; |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4914 | pr_warn("-- BEGIN DUMP LOG ---\n"); |
| 4915 | pr_warn("\n%s\n", log_buf); |
| 4916 | pr_warn("-- END LOG --\n"); |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 4917 | } else if (load_attr.insns_cnt >= BPF_MAXINSNS) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4918 | pr_warn("Program too large (%zu insns), at most %d insns\n", |
| 4919 | load_attr.insns_cnt, BPF_MAXINSNS); |
Wang Nan | 705fa21 | 2016-07-13 10:44:02 +0000 | [diff] [blame] | 4920 | ret = -LIBBPF_ERRNO__PROG2BIG; |
Toke Høiland-Jørgensen | 4f33ddb | 2019-11-09 21:37:29 +0100 | [diff] [blame] | 4921 | } else if (load_attr.prog_type != BPF_PROG_TYPE_KPROBE) { |
Wang Nan | 705fa21 | 2016-07-13 10:44:02 +0000 | [diff] [blame] | 4922 | /* Wrong program type? */ |
Toke Høiland-Jørgensen | 4f33ddb | 2019-11-09 21:37:29 +0100 | [diff] [blame] | 4923 | int fd; |
Wang Nan | 705fa21 | 2016-07-13 10:44:02 +0000 | [diff] [blame] | 4924 | |
Toke Høiland-Jørgensen | 4f33ddb | 2019-11-09 21:37:29 +0100 | [diff] [blame] | 4925 | load_attr.prog_type = BPF_PROG_TYPE_KPROBE; |
| 4926 | load_attr.expected_attach_type = 0; |
| 4927 | fd = bpf_load_program_xattr(&load_attr, NULL, 0); |
| 4928 | if (fd >= 0) { |
| 4929 | close(fd); |
| 4930 | ret = -LIBBPF_ERRNO__PROGTYPE; |
| 4931 | goto out; |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 4932 | } |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 4933 | } |
| 4934 | |
| 4935 | out: |
| 4936 | free(log_buf); |
| 4937 | return ret; |
| 4938 | } |
| 4939 | |
KP Singh | a6ed02c | 2020-01-17 22:28:25 +0100 | [diff] [blame] | 4940 | static int libbpf_find_attach_btf_id(struct bpf_program *prog); |
Andrii Nakryiko | 13acb50 | 2019-12-13 17:43:34 -0800 | [diff] [blame] | 4941 | |
| 4942 | int bpf_program__load(struct bpf_program *prog, char *license, __u32 kern_ver) |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 4943 | { |
Andrii Nakryiko | 13acb50 | 2019-12-13 17:43:34 -0800 | [diff] [blame] | 4944 | int err = 0, fd, i, btf_id; |
| 4945 | |
Eelco Chaudron | ff26ce5 | 2020-02-20 13:26:35 +0000 | [diff] [blame] | 4946 | if ((prog->type == BPF_PROG_TYPE_TRACING || |
| 4947 | prog->type == BPF_PROG_TYPE_EXT) && !prog->attach_btf_id) { |
KP Singh | a6ed02c | 2020-01-17 22:28:25 +0100 | [diff] [blame] | 4948 | btf_id = libbpf_find_attach_btf_id(prog); |
Andrii Nakryiko | 13acb50 | 2019-12-13 17:43:34 -0800 | [diff] [blame] | 4949 | if (btf_id <= 0) |
| 4950 | return btf_id; |
| 4951 | prog->attach_btf_id = btf_id; |
| 4952 | } |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 4953 | |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 4954 | if (prog->instances.nr < 0 || !prog->instances.fds) { |
| 4955 | if (prog->preprocessor) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4956 | pr_warn("Internal error: can't load program '%s'\n", |
| 4957 | prog->section_name); |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 4958 | return -LIBBPF_ERRNO__INTERNAL; |
| 4959 | } |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 4960 | |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 4961 | prog->instances.fds = malloc(sizeof(int)); |
| 4962 | if (!prog->instances.fds) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4963 | pr_warn("Not enough memory for BPF fds\n"); |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 4964 | return -ENOMEM; |
| 4965 | } |
| 4966 | prog->instances.nr = 1; |
| 4967 | prog->instances.fds[0] = -1; |
| 4968 | } |
| 4969 | |
| 4970 | if (!prog->preprocessor) { |
| 4971 | if (prog->instances.nr != 1) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4972 | pr_warn("Program '%s' is inconsistent: nr(%d) != 1\n", |
| 4973 | prog->section_name, prog->instances.nr); |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 4974 | } |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 4975 | err = load_program(prog, prog->insns, prog->insns_cnt, |
Andrii Nakryiko | 13acb50 | 2019-12-13 17:43:34 -0800 | [diff] [blame] | 4976 | license, kern_ver, &fd); |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 4977 | if (!err) |
| 4978 | prog->instances.fds[0] = fd; |
| 4979 | goto out; |
| 4980 | } |
| 4981 | |
| 4982 | for (i = 0; i < prog->instances.nr; i++) { |
| 4983 | struct bpf_prog_prep_result result; |
| 4984 | bpf_program_prep_t preprocessor = prog->preprocessor; |
| 4985 | |
Andrii Nakryiko | 1ad9cbb | 2019-02-13 10:25:53 -0800 | [diff] [blame] | 4986 | memset(&result, 0, sizeof(result)); |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 4987 | err = preprocessor(prog, i, prog->insns, |
| 4988 | prog->insns_cnt, &result); |
| 4989 | if (err) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4990 | pr_warn("Preprocessing the %dth instance of program '%s' failed\n", |
| 4991 | i, prog->section_name); |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 4992 | goto out; |
| 4993 | } |
| 4994 | |
| 4995 | if (!result.new_insn_ptr || !result.new_insn_cnt) { |
| 4996 | pr_debug("Skip loading the %dth instance of program '%s'\n", |
| 4997 | i, prog->section_name); |
| 4998 | prog->instances.fds[i] = -1; |
| 4999 | if (result.pfd) |
| 5000 | *result.pfd = -1; |
| 5001 | continue; |
| 5002 | } |
| 5003 | |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 5004 | err = load_program(prog, result.new_insn_ptr, |
Andrii Nakryiko | 13acb50 | 2019-12-13 17:43:34 -0800 | [diff] [blame] | 5005 | result.new_insn_cnt, license, kern_ver, &fd); |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 5006 | if (err) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5007 | pr_warn("Loading the %dth instance of program '%s' failed\n", |
| 5008 | i, prog->section_name); |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 5009 | goto out; |
| 5010 | } |
| 5011 | |
| 5012 | if (result.pfd) |
| 5013 | *result.pfd = fd; |
| 5014 | prog->instances.fds[i] = fd; |
| 5015 | } |
| 5016 | out: |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 5017 | if (err) |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5018 | pr_warn("failed to load program '%s'\n", prog->section_name); |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 5019 | zfree(&prog->insns); |
| 5020 | prog->insns_cnt = 0; |
| 5021 | return err; |
| 5022 | } |
| 5023 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 5024 | static bool bpf_program__is_function_storage(const struct bpf_program *prog, |
| 5025 | const struct bpf_object *obj) |
Jakub Kicinski | 9a94f27 | 2018-06-28 14:41:38 -0700 | [diff] [blame] | 5026 | { |
| 5027 | return prog->idx == obj->efile.text_shndx && obj->has_pseudo_calls; |
| 5028 | } |
| 5029 | |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 5030 | static int |
Quentin Monnet | 60276f9 | 2019-05-24 11:36:47 +0100 | [diff] [blame] | 5031 | bpf_object__load_progs(struct bpf_object *obj, int log_level) |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 5032 | { |
| 5033 | size_t i; |
| 5034 | int err; |
| 5035 | |
| 5036 | for (i = 0; i < obj->nr_programs; i++) { |
Jakub Kicinski | 9a94f27 | 2018-06-28 14:41:38 -0700 | [diff] [blame] | 5037 | if (bpf_program__is_function_storage(&obj->programs[i], obj)) |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 5038 | continue; |
Quentin Monnet | 501b125 | 2019-05-29 15:26:41 +0100 | [diff] [blame] | 5039 | obj->programs[i].log_level |= log_level; |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 5040 | err = bpf_program__load(&obj->programs[i], |
| 5041 | obj->license, |
| 5042 | obj->kern_version); |
| 5043 | if (err) |
| 5044 | return err; |
| 5045 | } |
| 5046 | return 0; |
| 5047 | } |
| 5048 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 5049 | static struct bpf_object * |
Andrii Nakryiko | 5e61f27 | 2019-10-04 15:40:34 -0700 | [diff] [blame] | 5050 | __bpf_object__open(const char *path, const void *obj_buf, size_t obj_buf_sz, |
Andrii Nakryiko | 01af3bf | 2019-12-13 17:43:32 -0800 | [diff] [blame] | 5051 | const struct bpf_object_open_opts *opts) |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 5052 | { |
Andrii Nakryiko | 8601fd4 | 2019-12-18 16:28:35 -0800 | [diff] [blame] | 5053 | const char *obj_name, *kconfig; |
Andrii Nakryiko | dd4436b | 2019-10-20 20:38:59 -0700 | [diff] [blame] | 5054 | struct bpf_program *prog; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 5055 | struct bpf_object *obj; |
Andrii Nakryiko | 291ee02 | 2019-10-15 11:28:46 -0700 | [diff] [blame] | 5056 | char tmp_name[64]; |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 5057 | int err; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 5058 | |
| 5059 | if (elf_version(EV_CURRENT) == EV_NONE) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5060 | pr_warn("failed to init libelf for %s\n", |
| 5061 | path ? : "(mem buf)"); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 5062 | return ERR_PTR(-LIBBPF_ERRNO__LIBELF); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 5063 | } |
| 5064 | |
Andrii Nakryiko | 291ee02 | 2019-10-15 11:28:46 -0700 | [diff] [blame] | 5065 | if (!OPTS_VALID(opts, bpf_object_open_opts)) |
| 5066 | return ERR_PTR(-EINVAL); |
| 5067 | |
Andrii Nakryiko | 1aace10 | 2019-11-21 16:35:27 -0800 | [diff] [blame] | 5068 | obj_name = OPTS_GET(opts, object_name, NULL); |
Andrii Nakryiko | 291ee02 | 2019-10-15 11:28:46 -0700 | [diff] [blame] | 5069 | if (obj_buf) { |
| 5070 | if (!obj_name) { |
| 5071 | snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx", |
| 5072 | (unsigned long)obj_buf, |
| 5073 | (unsigned long)obj_buf_sz); |
| 5074 | obj_name = tmp_name; |
| 5075 | } |
| 5076 | path = obj_name; |
| 5077 | pr_debug("loading object '%s' from buffer\n", obj_name); |
| 5078 | } |
| 5079 | |
Andrii Nakryiko | 2ce8450 | 2019-10-04 15:40:35 -0700 | [diff] [blame] | 5080 | obj = bpf_object__new(path, obj_buf, obj_buf_sz, obj_name); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 5081 | if (IS_ERR(obj)) |
| 5082 | return obj; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 5083 | |
Andrii Nakryiko | 8601fd4 | 2019-12-18 16:28:35 -0800 | [diff] [blame] | 5084 | kconfig = OPTS_GET(opts, kconfig, NULL); |
| 5085 | if (kconfig) { |
| 5086 | obj->kconfig = strdup(kconfig); |
| 5087 | if (!obj->kconfig) |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 5088 | return ERR_PTR(-ENOMEM); |
| 5089 | } |
Andrii Nakryiko | 291ee02 | 2019-10-15 11:28:46 -0700 | [diff] [blame] | 5090 | |
Andrii Nakryiko | 0d13bfc | 2019-12-13 17:43:25 -0800 | [diff] [blame] | 5091 | err = bpf_object__elf_init(obj); |
| 5092 | err = err ? : bpf_object__check_endianness(obj); |
| 5093 | err = err ? : bpf_object__elf_collect(obj); |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 5094 | err = err ? : bpf_object__collect_externs(obj); |
| 5095 | err = err ? : bpf_object__finalize_btf(obj); |
Andrii Nakryiko | 0d13bfc | 2019-12-13 17:43:25 -0800 | [diff] [blame] | 5096 | err = err ? : bpf_object__init_maps(obj, opts); |
| 5097 | err = err ? : bpf_object__init_prog_names(obj); |
| 5098 | err = err ? : bpf_object__collect_reloc(obj); |
| 5099 | if (err) |
| 5100 | goto out; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 5101 | bpf_object__elf_finish(obj); |
Andrii Nakryiko | dd4436b | 2019-10-20 20:38:59 -0700 | [diff] [blame] | 5102 | |
| 5103 | bpf_object__for_each_program(prog, obj) { |
| 5104 | enum bpf_prog_type prog_type; |
| 5105 | enum bpf_attach_type attach_type; |
| 5106 | |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 5107 | if (prog->type != BPF_PROG_TYPE_UNSPEC) |
| 5108 | continue; |
| 5109 | |
Andrii Nakryiko | dd4436b | 2019-10-20 20:38:59 -0700 | [diff] [blame] | 5110 | err = libbpf_prog_type_by_name(prog->section_name, &prog_type, |
| 5111 | &attach_type); |
| 5112 | if (err == -ESRCH) |
| 5113 | /* couldn't guess, but user might manually specify */ |
| 5114 | continue; |
| 5115 | if (err) |
| 5116 | goto out; |
| 5117 | |
| 5118 | bpf_program__set_type(prog, prog_type); |
| 5119 | bpf_program__set_expected_attach_type(prog, attach_type); |
Alexei Starovoitov | 2db6eab | 2020-01-20 16:53:47 -0800 | [diff] [blame] | 5120 | if (prog_type == BPF_PROG_TYPE_TRACING || |
| 5121 | prog_type == BPF_PROG_TYPE_EXT) |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 5122 | prog->attach_prog_fd = OPTS_GET(opts, attach_prog_fd, 0); |
Andrii Nakryiko | dd4436b | 2019-10-20 20:38:59 -0700 | [diff] [blame] | 5123 | } |
| 5124 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 5125 | return obj; |
| 5126 | out: |
| 5127 | bpf_object__close(obj); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 5128 | return ERR_PTR(err); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 5129 | } |
| 5130 | |
Andrii Nakryiko | 5e61f27 | 2019-10-04 15:40:34 -0700 | [diff] [blame] | 5131 | static struct bpf_object * |
| 5132 | __bpf_object__open_xattr(struct bpf_object_open_attr *attr, int flags) |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 5133 | { |
Andrii Nakryiko | e00aca6 | 2019-10-22 10:21:00 -0700 | [diff] [blame] | 5134 | DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts, |
Andrii Nakryiko | 291ee02 | 2019-10-15 11:28:46 -0700 | [diff] [blame] | 5135 | .relaxed_maps = flags & MAPS_RELAX_COMPAT, |
| 5136 | ); |
| 5137 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 5138 | /* param validation */ |
Jakub Kicinski | 07f2d4e | 2018-07-10 14:43:02 -0700 | [diff] [blame] | 5139 | if (!attr->file) |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 5140 | return NULL; |
| 5141 | |
Jakub Kicinski | 07f2d4e | 2018-07-10 14:43:02 -0700 | [diff] [blame] | 5142 | pr_debug("loading %s\n", attr->file); |
Andrii Nakryiko | 291ee02 | 2019-10-15 11:28:46 -0700 | [diff] [blame] | 5143 | return __bpf_object__open(attr->file, NULL, 0, &opts); |
John Fastabend | c034a17 | 2018-10-15 11:19:55 -0700 | [diff] [blame] | 5144 | } |
| 5145 | |
| 5146 | struct bpf_object *bpf_object__open_xattr(struct bpf_object_open_attr *attr) |
| 5147 | { |
| 5148 | return __bpf_object__open_xattr(attr, 0); |
Jakub Kicinski | 07f2d4e | 2018-07-10 14:43:02 -0700 | [diff] [blame] | 5149 | } |
| 5150 | |
| 5151 | struct bpf_object *bpf_object__open(const char *path) |
| 5152 | { |
| 5153 | struct bpf_object_open_attr attr = { |
| 5154 | .file = path, |
| 5155 | .prog_type = BPF_PROG_TYPE_UNSPEC, |
| 5156 | }; |
| 5157 | |
| 5158 | return bpf_object__open_xattr(&attr); |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 5159 | } |
| 5160 | |
Andrii Nakryiko | 2ce8450 | 2019-10-04 15:40:35 -0700 | [diff] [blame] | 5161 | struct bpf_object * |
Andrii Nakryiko | 01af3bf | 2019-12-13 17:43:32 -0800 | [diff] [blame] | 5162 | bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts) |
Andrii Nakryiko | 2ce8450 | 2019-10-04 15:40:35 -0700 | [diff] [blame] | 5163 | { |
Andrii Nakryiko | 2ce8450 | 2019-10-04 15:40:35 -0700 | [diff] [blame] | 5164 | if (!path) |
| 5165 | return ERR_PTR(-EINVAL); |
| 5166 | |
| 5167 | pr_debug("loading %s\n", path); |
| 5168 | |
Andrii Nakryiko | 291ee02 | 2019-10-15 11:28:46 -0700 | [diff] [blame] | 5169 | return __bpf_object__open(path, NULL, 0, opts); |
Andrii Nakryiko | 2ce8450 | 2019-10-04 15:40:35 -0700 | [diff] [blame] | 5170 | } |
| 5171 | |
| 5172 | struct bpf_object * |
| 5173 | bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz, |
Andrii Nakryiko | 01af3bf | 2019-12-13 17:43:32 -0800 | [diff] [blame] | 5174 | const struct bpf_object_open_opts *opts) |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 5175 | { |
Andrii Nakryiko | 2ce8450 | 2019-10-04 15:40:35 -0700 | [diff] [blame] | 5176 | if (!obj_buf || obj_buf_sz == 0) |
| 5177 | return ERR_PTR(-EINVAL); |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 5178 | |
Andrii Nakryiko | 291ee02 | 2019-10-15 11:28:46 -0700 | [diff] [blame] | 5179 | return __bpf_object__open(NULL, obj_buf, obj_buf_sz, opts); |
Andrii Nakryiko | 2ce8450 | 2019-10-04 15:40:35 -0700 | [diff] [blame] | 5180 | } |
| 5181 | |
| 5182 | struct bpf_object * |
| 5183 | bpf_object__open_buffer(const void *obj_buf, size_t obj_buf_sz, |
| 5184 | const char *name) |
| 5185 | { |
Andrii Nakryiko | e00aca6 | 2019-10-22 10:21:00 -0700 | [diff] [blame] | 5186 | DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts, |
Andrii Nakryiko | 2ce8450 | 2019-10-04 15:40:35 -0700 | [diff] [blame] | 5187 | .object_name = name, |
| 5188 | /* wrong default, but backwards-compatible */ |
| 5189 | .relaxed_maps = true, |
| 5190 | ); |
| 5191 | |
| 5192 | /* returning NULL is wrong, but backwards-compatible */ |
| 5193 | if (!obj_buf || obj_buf_sz == 0) |
| 5194 | return NULL; |
| 5195 | |
| 5196 | return bpf_object__open_mem(obj_buf, obj_buf_sz, &opts); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 5197 | } |
| 5198 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 5199 | int bpf_object__unload(struct bpf_object *obj) |
| 5200 | { |
| 5201 | size_t i; |
| 5202 | |
| 5203 | if (!obj) |
| 5204 | return -EINVAL; |
| 5205 | |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 5206 | for (i = 0; i < obj->nr_maps; i++) { |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 5207 | zclose(obj->maps[i].fd); |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 5208 | if (obj->maps[i].st_ops) |
| 5209 | zfree(&obj->maps[i].st_ops->kern_vdata); |
| 5210 | } |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 5211 | |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 5212 | for (i = 0; i < obj->nr_programs; i++) |
| 5213 | bpf_program__unload(&obj->programs[i]); |
| 5214 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 5215 | return 0; |
| 5216 | } |
| 5217 | |
Andrii Nakryiko | 0d13bfc | 2019-12-13 17:43:25 -0800 | [diff] [blame] | 5218 | static int bpf_object__sanitize_maps(struct bpf_object *obj) |
| 5219 | { |
| 5220 | struct bpf_map *m; |
| 5221 | |
| 5222 | bpf_object__for_each_map(m, obj) { |
| 5223 | if (!bpf_map__is_internal(m)) |
| 5224 | continue; |
| 5225 | if (!obj->caps.global_data) { |
| 5226 | pr_warn("kernel doesn't support global data\n"); |
| 5227 | return -ENOTSUP; |
| 5228 | } |
| 5229 | if (!obj->caps.array_mmap) |
| 5230 | m->def.map_flags ^= BPF_F_MMAPABLE; |
| 5231 | } |
| 5232 | |
| 5233 | return 0; |
| 5234 | } |
| 5235 | |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 5236 | static int bpf_object__resolve_externs(struct bpf_object *obj, |
Andrii Nakryiko | 8601fd4 | 2019-12-18 16:28:35 -0800 | [diff] [blame] | 5237 | const char *extra_kconfig) |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 5238 | { |
| 5239 | bool need_config = false; |
| 5240 | struct extern_desc *ext; |
| 5241 | int err, i; |
| 5242 | void *data; |
| 5243 | |
| 5244 | if (obj->nr_extern == 0) |
| 5245 | return 0; |
| 5246 | |
Andrii Nakryiko | 81bfdd0 | 2019-12-18 16:28:34 -0800 | [diff] [blame] | 5247 | data = obj->maps[obj->kconfig_map_idx].mmaped; |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 5248 | |
| 5249 | for (i = 0; i < obj->nr_extern; i++) { |
| 5250 | ext = &obj->externs[i]; |
| 5251 | |
| 5252 | if (strcmp(ext->name, "LINUX_KERNEL_VERSION") == 0) { |
| 5253 | void *ext_val = data + ext->data_off; |
| 5254 | __u32 kver = get_kernel_version(); |
| 5255 | |
| 5256 | if (!kver) { |
| 5257 | pr_warn("failed to get kernel version\n"); |
| 5258 | return -EINVAL; |
| 5259 | } |
| 5260 | err = set_ext_value_num(ext, ext_val, kver); |
| 5261 | if (err) |
| 5262 | return err; |
| 5263 | pr_debug("extern %s=0x%x\n", ext->name, kver); |
| 5264 | } else if (strncmp(ext->name, "CONFIG_", 7) == 0) { |
| 5265 | need_config = true; |
| 5266 | } else { |
| 5267 | pr_warn("unrecognized extern '%s'\n", ext->name); |
| 5268 | return -EINVAL; |
| 5269 | } |
| 5270 | } |
Andrii Nakryiko | 8601fd4 | 2019-12-18 16:28:35 -0800 | [diff] [blame] | 5271 | if (need_config && extra_kconfig) { |
| 5272 | err = bpf_object__read_kconfig_mem(obj, extra_kconfig, data); |
| 5273 | if (err) |
| 5274 | return -EINVAL; |
| 5275 | need_config = false; |
| 5276 | for (i = 0; i < obj->nr_extern; i++) { |
| 5277 | ext = &obj->externs[i]; |
| 5278 | if (!ext->is_set) { |
| 5279 | need_config = true; |
| 5280 | break; |
| 5281 | } |
| 5282 | } |
| 5283 | } |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 5284 | if (need_config) { |
Andrii Nakryiko | 8601fd4 | 2019-12-18 16:28:35 -0800 | [diff] [blame] | 5285 | err = bpf_object__read_kconfig_file(obj, data); |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 5286 | if (err) |
| 5287 | return -EINVAL; |
| 5288 | } |
| 5289 | for (i = 0; i < obj->nr_extern; i++) { |
| 5290 | ext = &obj->externs[i]; |
| 5291 | |
| 5292 | if (!ext->is_set && !ext->is_weak) { |
| 5293 | pr_warn("extern %s (strong) not resolved\n", ext->name); |
| 5294 | return -ESRCH; |
| 5295 | } else if (!ext->is_set) { |
| 5296 | pr_debug("extern %s (weak) not resolved, defaulting to zero\n", |
| 5297 | ext->name); |
| 5298 | } |
| 5299 | } |
| 5300 | |
| 5301 | return 0; |
| 5302 | } |
| 5303 | |
Quentin Monnet | 60276f9 | 2019-05-24 11:36:47 +0100 | [diff] [blame] | 5304 | int bpf_object__load_xattr(struct bpf_object_load_attr *attr) |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 5305 | { |
Quentin Monnet | 60276f9 | 2019-05-24 11:36:47 +0100 | [diff] [blame] | 5306 | struct bpf_object *obj; |
Toke Høiland-Jørgensen | ec6d5f47 | 2019-11-09 21:37:27 +0100 | [diff] [blame] | 5307 | int err, i; |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 5308 | |
Quentin Monnet | 60276f9 | 2019-05-24 11:36:47 +0100 | [diff] [blame] | 5309 | if (!attr) |
| 5310 | return -EINVAL; |
| 5311 | obj = attr->obj; |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 5312 | if (!obj) |
| 5313 | return -EINVAL; |
| 5314 | |
| 5315 | if (obj->loaded) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5316 | pr_warn("object should not be loaded twice\n"); |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 5317 | return -EINVAL; |
| 5318 | } |
| 5319 | |
| 5320 | obj->loaded = true; |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 5321 | |
Andrii Nakryiko | 0d13bfc | 2019-12-13 17:43:25 -0800 | [diff] [blame] | 5322 | err = bpf_object__probe_caps(obj); |
Andrii Nakryiko | 8601fd4 | 2019-12-18 16:28:35 -0800 | [diff] [blame] | 5323 | err = err ? : bpf_object__resolve_externs(obj, obj->kconfig); |
Andrii Nakryiko | 0d13bfc | 2019-12-13 17:43:25 -0800 | [diff] [blame] | 5324 | err = err ? : bpf_object__sanitize_and_load_btf(obj); |
| 5325 | err = err ? : bpf_object__sanitize_maps(obj); |
KP Singh | a6ed02c | 2020-01-17 22:28:25 +0100 | [diff] [blame] | 5326 | err = err ? : bpf_object__load_vmlinux_btf(obj); |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 5327 | err = err ? : bpf_object__init_kern_struct_ops_maps(obj); |
Andrii Nakryiko | 0d13bfc | 2019-12-13 17:43:25 -0800 | [diff] [blame] | 5328 | err = err ? : bpf_object__create_maps(obj); |
| 5329 | err = err ? : bpf_object__relocate(obj, attr->target_btf_path); |
| 5330 | err = err ? : bpf_object__load_progs(obj, attr->log_level); |
KP Singh | a6ed02c | 2020-01-17 22:28:25 +0100 | [diff] [blame] | 5331 | |
| 5332 | btf__free(obj->btf_vmlinux); |
| 5333 | obj->btf_vmlinux = NULL; |
| 5334 | |
Andrii Nakryiko | 0d13bfc | 2019-12-13 17:43:25 -0800 | [diff] [blame] | 5335 | if (err) |
| 5336 | goto out; |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 5337 | |
| 5338 | return 0; |
| 5339 | out: |
Toke Høiland-Jørgensen | ec6d5f47 | 2019-11-09 21:37:27 +0100 | [diff] [blame] | 5340 | /* unpin any maps that were auto-pinned during load */ |
| 5341 | for (i = 0; i < obj->nr_maps; i++) |
| 5342 | if (obj->maps[i].pinned && !obj->maps[i].reused) |
| 5343 | bpf_map__unpin(&obj->maps[i], NULL); |
| 5344 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 5345 | bpf_object__unload(obj); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5346 | pr_warn("failed to load object '%s'\n", obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 5347 | return err; |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 5348 | } |
| 5349 | |
Quentin Monnet | 60276f9 | 2019-05-24 11:36:47 +0100 | [diff] [blame] | 5350 | int bpf_object__load(struct bpf_object *obj) |
| 5351 | { |
| 5352 | struct bpf_object_load_attr attr = { |
| 5353 | .obj = obj, |
| 5354 | }; |
| 5355 | |
| 5356 | return bpf_object__load_xattr(&attr); |
| 5357 | } |
| 5358 | |
Toke Høiland-Jørgensen | 196f848 | 2019-11-02 12:09:39 +0100 | [diff] [blame] | 5359 | static int make_parent_dir(const char *path) |
| 5360 | { |
| 5361 | char *cp, errmsg[STRERR_BUFSIZE]; |
| 5362 | char *dname, *dir; |
| 5363 | int err = 0; |
| 5364 | |
| 5365 | dname = strdup(path); |
| 5366 | if (dname == NULL) |
| 5367 | return -ENOMEM; |
| 5368 | |
| 5369 | dir = dirname(dname); |
| 5370 | if (mkdir(dir, 0700) && errno != EEXIST) |
| 5371 | err = -errno; |
| 5372 | |
| 5373 | free(dname); |
| 5374 | if (err) { |
| 5375 | cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg)); |
| 5376 | pr_warn("failed to mkdir %s: %s\n", path, cp); |
| 5377 | } |
| 5378 | return err; |
| 5379 | } |
| 5380 | |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 5381 | static int check_path(const char *path) |
| 5382 | { |
Thomas Richter | 1ce6a9f | 2018-07-30 10:53:23 +0200 | [diff] [blame] | 5383 | char *cp, errmsg[STRERR_BUFSIZE]; |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 5384 | struct statfs st_fs; |
| 5385 | char *dname, *dir; |
| 5386 | int err = 0; |
| 5387 | |
| 5388 | if (path == NULL) |
| 5389 | return -EINVAL; |
| 5390 | |
| 5391 | dname = strdup(path); |
| 5392 | if (dname == NULL) |
| 5393 | return -ENOMEM; |
| 5394 | |
| 5395 | dir = dirname(dname); |
| 5396 | if (statfs(dir, &st_fs)) { |
Andrey Ignatov | 24d6a80 | 2018-10-03 15:26:41 -0700 | [diff] [blame] | 5397 | cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg)); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5398 | pr_warn("failed to statfs %s: %s\n", dir, cp); |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 5399 | err = -errno; |
| 5400 | } |
| 5401 | free(dname); |
| 5402 | |
| 5403 | if (!err && st_fs.f_type != BPF_FS_MAGIC) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5404 | pr_warn("specified path %s is not on BPF FS\n", path); |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 5405 | err = -EINVAL; |
| 5406 | } |
| 5407 | |
| 5408 | return err; |
| 5409 | } |
| 5410 | |
| 5411 | int bpf_program__pin_instance(struct bpf_program *prog, const char *path, |
| 5412 | int instance) |
| 5413 | { |
Thomas Richter | 1ce6a9f | 2018-07-30 10:53:23 +0200 | [diff] [blame] | 5414 | char *cp, errmsg[STRERR_BUFSIZE]; |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 5415 | int err; |
| 5416 | |
Toke Høiland-Jørgensen | 196f848 | 2019-11-02 12:09:39 +0100 | [diff] [blame] | 5417 | err = make_parent_dir(path); |
| 5418 | if (err) |
| 5419 | return err; |
| 5420 | |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 5421 | err = check_path(path); |
| 5422 | if (err) |
| 5423 | return err; |
| 5424 | |
| 5425 | if (prog == NULL) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5426 | pr_warn("invalid program pointer\n"); |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 5427 | return -EINVAL; |
| 5428 | } |
| 5429 | |
| 5430 | if (instance < 0 || instance >= prog->instances.nr) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5431 | pr_warn("invalid prog instance %d of prog %s (max %d)\n", |
| 5432 | instance, prog->section_name, prog->instances.nr); |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 5433 | return -EINVAL; |
| 5434 | } |
| 5435 | |
| 5436 | if (bpf_obj_pin(prog->instances.fds[instance], path)) { |
Andrey Ignatov | 24d6a80 | 2018-10-03 15:26:41 -0700 | [diff] [blame] | 5437 | cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg)); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5438 | pr_warn("failed to pin program: %s\n", cp); |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 5439 | return -errno; |
| 5440 | } |
| 5441 | pr_debug("pinned program '%s'\n", path); |
| 5442 | |
| 5443 | return 0; |
| 5444 | } |
| 5445 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5446 | int bpf_program__unpin_instance(struct bpf_program *prog, const char *path, |
| 5447 | int instance) |
| 5448 | { |
| 5449 | int err; |
| 5450 | |
| 5451 | err = check_path(path); |
| 5452 | if (err) |
| 5453 | return err; |
| 5454 | |
| 5455 | if (prog == NULL) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5456 | pr_warn("invalid program pointer\n"); |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5457 | return -EINVAL; |
| 5458 | } |
| 5459 | |
| 5460 | if (instance < 0 || instance >= prog->instances.nr) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5461 | pr_warn("invalid prog instance %d of prog %s (max %d)\n", |
| 5462 | instance, prog->section_name, prog->instances.nr); |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5463 | return -EINVAL; |
| 5464 | } |
| 5465 | |
| 5466 | err = unlink(path); |
| 5467 | if (err != 0) |
| 5468 | return -errno; |
| 5469 | pr_debug("unpinned program '%s'\n", path); |
| 5470 | |
| 5471 | return 0; |
| 5472 | } |
| 5473 | |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 5474 | int bpf_program__pin(struct bpf_program *prog, const char *path) |
| 5475 | { |
| 5476 | int i, err; |
| 5477 | |
Toke Høiland-Jørgensen | 196f848 | 2019-11-02 12:09:39 +0100 | [diff] [blame] | 5478 | err = make_parent_dir(path); |
| 5479 | if (err) |
| 5480 | return err; |
| 5481 | |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 5482 | err = check_path(path); |
| 5483 | if (err) |
| 5484 | return err; |
| 5485 | |
| 5486 | if (prog == NULL) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5487 | pr_warn("invalid program pointer\n"); |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 5488 | return -EINVAL; |
| 5489 | } |
| 5490 | |
| 5491 | if (prog->instances.nr <= 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5492 | pr_warn("no instances of prog %s to pin\n", |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 5493 | prog->section_name); |
| 5494 | return -EINVAL; |
| 5495 | } |
| 5496 | |
Stanislav Fomichev | fd734c5 | 2018-11-09 08:21:42 -0800 | [diff] [blame] | 5497 | if (prog->instances.nr == 1) { |
| 5498 | /* don't create subdirs when pinning single instance */ |
| 5499 | return bpf_program__pin_instance(prog, path, 0); |
| 5500 | } |
| 5501 | |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 5502 | for (i = 0; i < prog->instances.nr; i++) { |
| 5503 | char buf[PATH_MAX]; |
| 5504 | int len; |
| 5505 | |
| 5506 | len = snprintf(buf, PATH_MAX, "%s/%d", path, i); |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5507 | if (len < 0) { |
| 5508 | err = -EINVAL; |
| 5509 | goto err_unpin; |
| 5510 | } else if (len >= PATH_MAX) { |
| 5511 | err = -ENAMETOOLONG; |
| 5512 | goto err_unpin; |
| 5513 | } |
| 5514 | |
| 5515 | err = bpf_program__pin_instance(prog, buf, i); |
| 5516 | if (err) |
| 5517 | goto err_unpin; |
| 5518 | } |
| 5519 | |
| 5520 | return 0; |
| 5521 | |
| 5522 | err_unpin: |
| 5523 | for (i = i - 1; i >= 0; i--) { |
| 5524 | char buf[PATH_MAX]; |
| 5525 | int len; |
| 5526 | |
| 5527 | len = snprintf(buf, PATH_MAX, "%s/%d", path, i); |
| 5528 | if (len < 0) |
| 5529 | continue; |
| 5530 | else if (len >= PATH_MAX) |
| 5531 | continue; |
| 5532 | |
| 5533 | bpf_program__unpin_instance(prog, buf, i); |
| 5534 | } |
| 5535 | |
| 5536 | rmdir(path); |
| 5537 | |
| 5538 | return err; |
| 5539 | } |
| 5540 | |
| 5541 | int bpf_program__unpin(struct bpf_program *prog, const char *path) |
| 5542 | { |
| 5543 | int i, err; |
| 5544 | |
| 5545 | err = check_path(path); |
| 5546 | if (err) |
| 5547 | return err; |
| 5548 | |
| 5549 | if (prog == NULL) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5550 | pr_warn("invalid program pointer\n"); |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5551 | return -EINVAL; |
| 5552 | } |
| 5553 | |
| 5554 | if (prog->instances.nr <= 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5555 | pr_warn("no instances of prog %s to pin\n", |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5556 | prog->section_name); |
| 5557 | return -EINVAL; |
| 5558 | } |
| 5559 | |
Stanislav Fomichev | fd734c5 | 2018-11-09 08:21:42 -0800 | [diff] [blame] | 5560 | if (prog->instances.nr == 1) { |
| 5561 | /* don't create subdirs when pinning single instance */ |
| 5562 | return bpf_program__unpin_instance(prog, path, 0); |
| 5563 | } |
| 5564 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5565 | for (i = 0; i < prog->instances.nr; i++) { |
| 5566 | char buf[PATH_MAX]; |
| 5567 | int len; |
| 5568 | |
| 5569 | len = snprintf(buf, PATH_MAX, "%s/%d", path, i); |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 5570 | if (len < 0) |
| 5571 | return -EINVAL; |
| 5572 | else if (len >= PATH_MAX) |
| 5573 | return -ENAMETOOLONG; |
| 5574 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5575 | err = bpf_program__unpin_instance(prog, buf, i); |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 5576 | if (err) |
| 5577 | return err; |
| 5578 | } |
| 5579 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5580 | err = rmdir(path); |
| 5581 | if (err) |
| 5582 | return -errno; |
| 5583 | |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 5584 | return 0; |
| 5585 | } |
| 5586 | |
Joe Stringer | b6989f3 | 2017-01-26 13:19:57 -0800 | [diff] [blame] | 5587 | int bpf_map__pin(struct bpf_map *map, const char *path) |
| 5588 | { |
Thomas Richter | 1ce6a9f | 2018-07-30 10:53:23 +0200 | [diff] [blame] | 5589 | char *cp, errmsg[STRERR_BUFSIZE]; |
Joe Stringer | b6989f3 | 2017-01-26 13:19:57 -0800 | [diff] [blame] | 5590 | int err; |
| 5591 | |
Joe Stringer | b6989f3 | 2017-01-26 13:19:57 -0800 | [diff] [blame] | 5592 | if (map == NULL) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5593 | pr_warn("invalid map pointer\n"); |
Joe Stringer | b6989f3 | 2017-01-26 13:19:57 -0800 | [diff] [blame] | 5594 | return -EINVAL; |
| 5595 | } |
| 5596 | |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 5597 | if (map->pin_path) { |
| 5598 | if (path && strcmp(path, map->pin_path)) { |
| 5599 | pr_warn("map '%s' already has pin path '%s' different from '%s'\n", |
| 5600 | bpf_map__name(map), map->pin_path, path); |
| 5601 | return -EINVAL; |
| 5602 | } else if (map->pinned) { |
| 5603 | pr_debug("map '%s' already pinned at '%s'; not re-pinning\n", |
| 5604 | bpf_map__name(map), map->pin_path); |
| 5605 | return 0; |
| 5606 | } |
| 5607 | } else { |
| 5608 | if (!path) { |
| 5609 | pr_warn("missing a path to pin map '%s' at\n", |
| 5610 | bpf_map__name(map)); |
| 5611 | return -EINVAL; |
| 5612 | } else if (map->pinned) { |
| 5613 | pr_warn("map '%s' already pinned\n", bpf_map__name(map)); |
| 5614 | return -EEXIST; |
| 5615 | } |
| 5616 | |
| 5617 | map->pin_path = strdup(path); |
| 5618 | if (!map->pin_path) { |
| 5619 | err = -errno; |
| 5620 | goto out_err; |
| 5621 | } |
Joe Stringer | b6989f3 | 2017-01-26 13:19:57 -0800 | [diff] [blame] | 5622 | } |
| 5623 | |
Toke Høiland-Jørgensen | 196f848 | 2019-11-02 12:09:39 +0100 | [diff] [blame] | 5624 | err = make_parent_dir(map->pin_path); |
| 5625 | if (err) |
| 5626 | return err; |
| 5627 | |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 5628 | err = check_path(map->pin_path); |
| 5629 | if (err) |
| 5630 | return err; |
| 5631 | |
| 5632 | if (bpf_obj_pin(map->fd, map->pin_path)) { |
| 5633 | err = -errno; |
| 5634 | goto out_err; |
| 5635 | } |
| 5636 | |
| 5637 | map->pinned = true; |
| 5638 | pr_debug("pinned map '%s'\n", map->pin_path); |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5639 | |
Joe Stringer | b6989f3 | 2017-01-26 13:19:57 -0800 | [diff] [blame] | 5640 | return 0; |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 5641 | |
| 5642 | out_err: |
| 5643 | cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg)); |
| 5644 | pr_warn("failed to pin map: %s\n", cp); |
| 5645 | return err; |
Joe Stringer | b6989f3 | 2017-01-26 13:19:57 -0800 | [diff] [blame] | 5646 | } |
| 5647 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5648 | int bpf_map__unpin(struct bpf_map *map, const char *path) |
Joe Stringer | d5148d8 | 2017-01-26 13:19:58 -0800 | [diff] [blame] | 5649 | { |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5650 | int err; |
| 5651 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5652 | if (map == NULL) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5653 | pr_warn("invalid map pointer\n"); |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5654 | return -EINVAL; |
| 5655 | } |
| 5656 | |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 5657 | if (map->pin_path) { |
| 5658 | if (path && strcmp(path, map->pin_path)) { |
| 5659 | pr_warn("map '%s' already has pin path '%s' different from '%s'\n", |
| 5660 | bpf_map__name(map), map->pin_path, path); |
| 5661 | return -EINVAL; |
| 5662 | } |
| 5663 | path = map->pin_path; |
| 5664 | } else if (!path) { |
| 5665 | pr_warn("no path to unpin map '%s' from\n", |
| 5666 | bpf_map__name(map)); |
| 5667 | return -EINVAL; |
| 5668 | } |
| 5669 | |
| 5670 | err = check_path(path); |
| 5671 | if (err) |
| 5672 | return err; |
| 5673 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5674 | err = unlink(path); |
| 5675 | if (err != 0) |
| 5676 | return -errno; |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 5677 | |
| 5678 | map->pinned = false; |
| 5679 | pr_debug("unpinned map '%s' from '%s'\n", bpf_map__name(map), path); |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5680 | |
| 5681 | return 0; |
| 5682 | } |
| 5683 | |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 5684 | int bpf_map__set_pin_path(struct bpf_map *map, const char *path) |
| 5685 | { |
| 5686 | char *new = NULL; |
| 5687 | |
| 5688 | if (path) { |
| 5689 | new = strdup(path); |
| 5690 | if (!new) |
| 5691 | return -errno; |
| 5692 | } |
| 5693 | |
| 5694 | free(map->pin_path); |
| 5695 | map->pin_path = new; |
| 5696 | return 0; |
| 5697 | } |
| 5698 | |
| 5699 | const char *bpf_map__get_pin_path(const struct bpf_map *map) |
| 5700 | { |
| 5701 | return map->pin_path; |
| 5702 | } |
| 5703 | |
| 5704 | bool bpf_map__is_pinned(const struct bpf_map *map) |
| 5705 | { |
| 5706 | return map->pinned; |
| 5707 | } |
| 5708 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5709 | int bpf_object__pin_maps(struct bpf_object *obj, const char *path) |
| 5710 | { |
Joe Stringer | d5148d8 | 2017-01-26 13:19:58 -0800 | [diff] [blame] | 5711 | struct bpf_map *map; |
| 5712 | int err; |
| 5713 | |
| 5714 | if (!obj) |
| 5715 | return -ENOENT; |
| 5716 | |
| 5717 | if (!obj->loaded) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5718 | pr_warn("object not yet loaded; load it first\n"); |
Joe Stringer | d5148d8 | 2017-01-26 13:19:58 -0800 | [diff] [blame] | 5719 | return -ENOENT; |
| 5720 | } |
| 5721 | |
Jakub Kicinski | f74a53d9 | 2019-02-27 19:04:12 -0800 | [diff] [blame] | 5722 | bpf_object__for_each_map(map, obj) { |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 5723 | char *pin_path = NULL; |
Joe Stringer | d5148d8 | 2017-01-26 13:19:58 -0800 | [diff] [blame] | 5724 | char buf[PATH_MAX]; |
Joe Stringer | d5148d8 | 2017-01-26 13:19:58 -0800 | [diff] [blame] | 5725 | |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 5726 | if (path) { |
| 5727 | int len; |
| 5728 | |
| 5729 | len = snprintf(buf, PATH_MAX, "%s/%s", path, |
| 5730 | bpf_map__name(map)); |
| 5731 | if (len < 0) { |
| 5732 | err = -EINVAL; |
| 5733 | goto err_unpin_maps; |
| 5734 | } else if (len >= PATH_MAX) { |
| 5735 | err = -ENAMETOOLONG; |
| 5736 | goto err_unpin_maps; |
| 5737 | } |
| 5738 | pin_path = buf; |
| 5739 | } else if (!map->pin_path) { |
| 5740 | continue; |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5741 | } |
| 5742 | |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 5743 | err = bpf_map__pin(map, pin_path); |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5744 | if (err) |
| 5745 | goto err_unpin_maps; |
| 5746 | } |
| 5747 | |
| 5748 | return 0; |
| 5749 | |
| 5750 | err_unpin_maps: |
| 5751 | while ((map = bpf_map__prev(map, obj))) { |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 5752 | if (!map->pin_path) |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5753 | continue; |
| 5754 | |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 5755 | bpf_map__unpin(map, NULL); |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5756 | } |
| 5757 | |
| 5758 | return err; |
| 5759 | } |
| 5760 | |
| 5761 | int bpf_object__unpin_maps(struct bpf_object *obj, const char *path) |
| 5762 | { |
| 5763 | struct bpf_map *map; |
| 5764 | int err; |
| 5765 | |
| 5766 | if (!obj) |
| 5767 | return -ENOENT; |
| 5768 | |
Jakub Kicinski | f74a53d9 | 2019-02-27 19:04:12 -0800 | [diff] [blame] | 5769 | bpf_object__for_each_map(map, obj) { |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 5770 | char *pin_path = NULL; |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5771 | char buf[PATH_MAX]; |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5772 | |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 5773 | if (path) { |
| 5774 | int len; |
Joe Stringer | d5148d8 | 2017-01-26 13:19:58 -0800 | [diff] [blame] | 5775 | |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 5776 | len = snprintf(buf, PATH_MAX, "%s/%s", path, |
| 5777 | bpf_map__name(map)); |
| 5778 | if (len < 0) |
| 5779 | return -EINVAL; |
| 5780 | else if (len >= PATH_MAX) |
| 5781 | return -ENAMETOOLONG; |
| 5782 | pin_path = buf; |
| 5783 | } else if (!map->pin_path) { |
| 5784 | continue; |
| 5785 | } |
| 5786 | |
| 5787 | err = bpf_map__unpin(map, pin_path); |
Joe Stringer | d5148d8 | 2017-01-26 13:19:58 -0800 | [diff] [blame] | 5788 | if (err) |
| 5789 | return err; |
| 5790 | } |
| 5791 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5792 | return 0; |
| 5793 | } |
| 5794 | |
| 5795 | int bpf_object__pin_programs(struct bpf_object *obj, const char *path) |
| 5796 | { |
| 5797 | struct bpf_program *prog; |
| 5798 | int err; |
| 5799 | |
| 5800 | if (!obj) |
| 5801 | return -ENOENT; |
| 5802 | |
| 5803 | if (!obj->loaded) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5804 | pr_warn("object not yet loaded; load it first\n"); |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5805 | return -ENOENT; |
| 5806 | } |
| 5807 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5808 | bpf_object__for_each_program(prog, obj) { |
| 5809 | char buf[PATH_MAX]; |
| 5810 | int len; |
| 5811 | |
| 5812 | len = snprintf(buf, PATH_MAX, "%s/%s", path, |
Stanislav Fomichev | 33a2c75 | 2018-11-09 08:21:43 -0800 | [diff] [blame] | 5813 | prog->pin_name); |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5814 | if (len < 0) { |
| 5815 | err = -EINVAL; |
| 5816 | goto err_unpin_programs; |
| 5817 | } else if (len >= PATH_MAX) { |
| 5818 | err = -ENAMETOOLONG; |
| 5819 | goto err_unpin_programs; |
| 5820 | } |
| 5821 | |
| 5822 | err = bpf_program__pin(prog, buf); |
| 5823 | if (err) |
| 5824 | goto err_unpin_programs; |
| 5825 | } |
| 5826 | |
| 5827 | return 0; |
| 5828 | |
| 5829 | err_unpin_programs: |
| 5830 | while ((prog = bpf_program__prev(prog, obj))) { |
| 5831 | char buf[PATH_MAX]; |
| 5832 | int len; |
| 5833 | |
| 5834 | len = snprintf(buf, PATH_MAX, "%s/%s", path, |
Stanislav Fomichev | 33a2c75 | 2018-11-09 08:21:43 -0800 | [diff] [blame] | 5835 | prog->pin_name); |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5836 | if (len < 0) |
| 5837 | continue; |
| 5838 | else if (len >= PATH_MAX) |
| 5839 | continue; |
| 5840 | |
| 5841 | bpf_program__unpin(prog, buf); |
| 5842 | } |
| 5843 | |
| 5844 | return err; |
| 5845 | } |
| 5846 | |
| 5847 | int bpf_object__unpin_programs(struct bpf_object *obj, const char *path) |
| 5848 | { |
| 5849 | struct bpf_program *prog; |
| 5850 | int err; |
| 5851 | |
| 5852 | if (!obj) |
| 5853 | return -ENOENT; |
| 5854 | |
Joe Stringer | d5148d8 | 2017-01-26 13:19:58 -0800 | [diff] [blame] | 5855 | bpf_object__for_each_program(prog, obj) { |
| 5856 | char buf[PATH_MAX]; |
| 5857 | int len; |
| 5858 | |
| 5859 | len = snprintf(buf, PATH_MAX, "%s/%s", path, |
Stanislav Fomichev | 33a2c75 | 2018-11-09 08:21:43 -0800 | [diff] [blame] | 5860 | prog->pin_name); |
Joe Stringer | d5148d8 | 2017-01-26 13:19:58 -0800 | [diff] [blame] | 5861 | if (len < 0) |
| 5862 | return -EINVAL; |
| 5863 | else if (len >= PATH_MAX) |
| 5864 | return -ENAMETOOLONG; |
| 5865 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5866 | err = bpf_program__unpin(prog, buf); |
Joe Stringer | d5148d8 | 2017-01-26 13:19:58 -0800 | [diff] [blame] | 5867 | if (err) |
| 5868 | return err; |
| 5869 | } |
| 5870 | |
| 5871 | return 0; |
| 5872 | } |
| 5873 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5874 | int bpf_object__pin(struct bpf_object *obj, const char *path) |
| 5875 | { |
| 5876 | int err; |
| 5877 | |
| 5878 | err = bpf_object__pin_maps(obj, path); |
| 5879 | if (err) |
| 5880 | return err; |
| 5881 | |
| 5882 | err = bpf_object__pin_programs(obj, path); |
| 5883 | if (err) { |
| 5884 | bpf_object__unpin_maps(obj, path); |
| 5885 | return err; |
| 5886 | } |
| 5887 | |
| 5888 | return 0; |
| 5889 | } |
| 5890 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 5891 | void bpf_object__close(struct bpf_object *obj) |
| 5892 | { |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 5893 | size_t i; |
| 5894 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 5895 | if (!obj) |
| 5896 | return; |
| 5897 | |
Wang Nan | 10931d2 | 2016-11-26 07:03:26 +0000 | [diff] [blame] | 5898 | if (obj->clear_priv) |
| 5899 | obj->clear_priv(obj, obj->priv); |
| 5900 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 5901 | bpf_object__elf_finish(obj); |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 5902 | bpf_object__unload(obj); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 5903 | btf__free(obj->btf); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 5904 | btf_ext__free(obj->btf_ext); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 5905 | |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 5906 | for (i = 0; i < obj->nr_maps; i++) { |
Andrii Nakryiko | eba9c5f | 2019-12-13 17:43:33 -0800 | [diff] [blame] | 5907 | struct bpf_map *map = &obj->maps[i]; |
| 5908 | |
| 5909 | if (map->clear_priv) |
| 5910 | map->clear_priv(map, map->priv); |
| 5911 | map->priv = NULL; |
| 5912 | map->clear_priv = NULL; |
| 5913 | |
| 5914 | if (map->mmaped) { |
| 5915 | munmap(map->mmaped, bpf_map_mmap_sz(map)); |
| 5916 | map->mmaped = NULL; |
| 5917 | } |
| 5918 | |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 5919 | if (map->st_ops) { |
| 5920 | zfree(&map->st_ops->data); |
| 5921 | zfree(&map->st_ops->progs); |
| 5922 | zfree(&map->st_ops->kern_func_off); |
| 5923 | zfree(&map->st_ops); |
| 5924 | } |
| 5925 | |
Andrii Nakryiko | eba9c5f | 2019-12-13 17:43:33 -0800 | [diff] [blame] | 5926 | zfree(&map->name); |
| 5927 | zfree(&map->pin_path); |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 5928 | } |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 5929 | |
Andrii Nakryiko | 8601fd4 | 2019-12-18 16:28:35 -0800 | [diff] [blame] | 5930 | zfree(&obj->kconfig); |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 5931 | zfree(&obj->externs); |
| 5932 | obj->nr_extern = 0; |
| 5933 | |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 5934 | zfree(&obj->maps); |
| 5935 | obj->nr_maps = 0; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 5936 | |
| 5937 | if (obj->programs && obj->nr_programs) { |
| 5938 | for (i = 0; i < obj->nr_programs; i++) |
| 5939 | bpf_program__exit(&obj->programs[i]); |
| 5940 | } |
| 5941 | zfree(&obj->programs); |
| 5942 | |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 5943 | list_del(&obj->list); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 5944 | free(obj); |
| 5945 | } |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 5946 | |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 5947 | struct bpf_object * |
| 5948 | bpf_object__next(struct bpf_object *prev) |
| 5949 | { |
| 5950 | struct bpf_object *next; |
| 5951 | |
| 5952 | if (!prev) |
| 5953 | next = list_first_entry(&bpf_objects_list, |
| 5954 | struct bpf_object, |
| 5955 | list); |
| 5956 | else |
| 5957 | next = list_next_entry(prev, list); |
| 5958 | |
| 5959 | /* Empty list is noticed here so don't need checking on entry. */ |
| 5960 | if (&next->list == &bpf_objects_list) |
| 5961 | return NULL; |
| 5962 | |
| 5963 | return next; |
| 5964 | } |
| 5965 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 5966 | const char *bpf_object__name(const struct bpf_object *obj) |
Wang Nan | acf860a | 2015-08-27 02:30:55 +0000 | [diff] [blame] | 5967 | { |
Andrii Nakryiko | c9e4c30 | 2019-10-04 15:40:36 -0700 | [diff] [blame] | 5968 | return obj ? obj->name : ERR_PTR(-EINVAL); |
Wang Nan | acf860a | 2015-08-27 02:30:55 +0000 | [diff] [blame] | 5969 | } |
| 5970 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 5971 | unsigned int bpf_object__kversion(const struct bpf_object *obj) |
Wang Nan | 45825d8 | 2015-11-06 13:49:38 +0000 | [diff] [blame] | 5972 | { |
Arnaldo Carvalho de Melo | a7fe045 | 2016-06-03 12:22:51 -0300 | [diff] [blame] | 5973 | return obj ? obj->kern_version : 0; |
Wang Nan | 45825d8 | 2015-11-06 13:49:38 +0000 | [diff] [blame] | 5974 | } |
| 5975 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 5976 | struct btf *bpf_object__btf(const struct bpf_object *obj) |
Andrey Ignatov | 789f6ba | 2019-02-14 15:01:43 -0800 | [diff] [blame] | 5977 | { |
| 5978 | return obj ? obj->btf : NULL; |
| 5979 | } |
| 5980 | |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 5981 | int bpf_object__btf_fd(const struct bpf_object *obj) |
| 5982 | { |
| 5983 | return obj->btf ? btf__fd(obj->btf) : -1; |
| 5984 | } |
| 5985 | |
Wang Nan | 10931d2 | 2016-11-26 07:03:26 +0000 | [diff] [blame] | 5986 | int bpf_object__set_priv(struct bpf_object *obj, void *priv, |
| 5987 | bpf_object_clear_priv_t clear_priv) |
| 5988 | { |
| 5989 | if (obj->priv && obj->clear_priv) |
| 5990 | obj->clear_priv(obj, obj->priv); |
| 5991 | |
| 5992 | obj->priv = priv; |
| 5993 | obj->clear_priv = clear_priv; |
| 5994 | return 0; |
| 5995 | } |
| 5996 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 5997 | void *bpf_object__priv(const struct bpf_object *obj) |
Wang Nan | 10931d2 | 2016-11-26 07:03:26 +0000 | [diff] [blame] | 5998 | { |
| 5999 | return obj ? obj->priv : ERR_PTR(-EINVAL); |
| 6000 | } |
| 6001 | |
Jakub Kicinski | eac7d84 | 2018-06-28 14:41:39 -0700 | [diff] [blame] | 6002 | static struct bpf_program * |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 6003 | __bpf_program__iter(const struct bpf_program *p, const struct bpf_object *obj, |
| 6004 | bool forward) |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 6005 | { |
Martin KaFai Lau | a83d6e7 | 2018-11-12 15:44:53 -0800 | [diff] [blame] | 6006 | size_t nr_programs = obj->nr_programs; |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 6007 | ssize_t idx; |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 6008 | |
Martin KaFai Lau | a83d6e7 | 2018-11-12 15:44:53 -0800 | [diff] [blame] | 6009 | if (!nr_programs) |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 6010 | return NULL; |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 6011 | |
Martin KaFai Lau | a83d6e7 | 2018-11-12 15:44:53 -0800 | [diff] [blame] | 6012 | if (!p) |
| 6013 | /* Iter from the beginning */ |
| 6014 | return forward ? &obj->programs[0] : |
| 6015 | &obj->programs[nr_programs - 1]; |
| 6016 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 6017 | if (p->obj != obj) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6018 | pr_warn("error: program handler doesn't match object\n"); |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 6019 | return NULL; |
| 6020 | } |
| 6021 | |
Martin KaFai Lau | a83d6e7 | 2018-11-12 15:44:53 -0800 | [diff] [blame] | 6022 | idx = (p - obj->programs) + (forward ? 1 : -1); |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 6023 | if (idx >= obj->nr_programs || idx < 0) |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 6024 | return NULL; |
| 6025 | return &obj->programs[idx]; |
| 6026 | } |
| 6027 | |
Jakub Kicinski | eac7d84 | 2018-06-28 14:41:39 -0700 | [diff] [blame] | 6028 | struct bpf_program * |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 6029 | bpf_program__next(struct bpf_program *prev, const struct bpf_object *obj) |
Jakub Kicinski | eac7d84 | 2018-06-28 14:41:39 -0700 | [diff] [blame] | 6030 | { |
| 6031 | struct bpf_program *prog = prev; |
| 6032 | |
| 6033 | do { |
Martin KaFai Lau | a83d6e7 | 2018-11-12 15:44:53 -0800 | [diff] [blame] | 6034 | prog = __bpf_program__iter(prog, obj, true); |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 6035 | } while (prog && bpf_program__is_function_storage(prog, obj)); |
| 6036 | |
| 6037 | return prog; |
| 6038 | } |
| 6039 | |
| 6040 | struct bpf_program * |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 6041 | bpf_program__prev(struct bpf_program *next, const struct bpf_object *obj) |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 6042 | { |
| 6043 | struct bpf_program *prog = next; |
| 6044 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 6045 | do { |
Martin KaFai Lau | a83d6e7 | 2018-11-12 15:44:53 -0800 | [diff] [blame] | 6046 | prog = __bpf_program__iter(prog, obj, false); |
Jakub Kicinski | eac7d84 | 2018-06-28 14:41:39 -0700 | [diff] [blame] | 6047 | } while (prog && bpf_program__is_function_storage(prog, obj)); |
| 6048 | |
| 6049 | return prog; |
| 6050 | } |
| 6051 | |
Arnaldo Carvalho de Melo | edb13ed | 2016-06-03 12:38:21 -0300 | [diff] [blame] | 6052 | int bpf_program__set_priv(struct bpf_program *prog, void *priv, |
| 6053 | bpf_program_clear_priv_t clear_priv) |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 6054 | { |
| 6055 | if (prog->priv && prog->clear_priv) |
| 6056 | prog->clear_priv(prog, prog->priv); |
| 6057 | |
| 6058 | prog->priv = priv; |
| 6059 | prog->clear_priv = clear_priv; |
| 6060 | return 0; |
| 6061 | } |
| 6062 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 6063 | void *bpf_program__priv(const struct bpf_program *prog) |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 6064 | { |
Arnaldo Carvalho de Melo | be834ff | 2016-06-03 12:36:39 -0300 | [diff] [blame] | 6065 | return prog ? prog->priv : ERR_PTR(-EINVAL); |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 6066 | } |
| 6067 | |
Jakub Kicinski | 9aba361 | 2018-06-28 14:41:37 -0700 | [diff] [blame] | 6068 | void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex) |
| 6069 | { |
| 6070 | prog->prog_ifindex = ifindex; |
| 6071 | } |
| 6072 | |
Andrii Nakryiko | 01af3bf | 2019-12-13 17:43:32 -0800 | [diff] [blame] | 6073 | const char *bpf_program__name(const struct bpf_program *prog) |
| 6074 | { |
| 6075 | return prog->name; |
| 6076 | } |
| 6077 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 6078 | const char *bpf_program__title(const struct bpf_program *prog, bool needs_copy) |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 6079 | { |
| 6080 | const char *title; |
| 6081 | |
| 6082 | title = prog->section_name; |
Namhyung Kim | 715f8db | 2015-11-03 20:21:05 +0900 | [diff] [blame] | 6083 | if (needs_copy) { |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 6084 | title = strdup(title); |
| 6085 | if (!title) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6086 | pr_warn("failed to strdup program title\n"); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 6087 | return ERR_PTR(-ENOMEM); |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 6088 | } |
| 6089 | } |
| 6090 | |
| 6091 | return title; |
| 6092 | } |
| 6093 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 6094 | int bpf_program__fd(const struct bpf_program *prog) |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 6095 | { |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 6096 | return bpf_program__nth_fd(prog, 0); |
| 6097 | } |
| 6098 | |
Toke Høiland-Jørgensen | 1a734ef | 2019-11-09 21:37:32 +0100 | [diff] [blame] | 6099 | size_t bpf_program__size(const struct bpf_program *prog) |
| 6100 | { |
| 6101 | return prog->insns_cnt * sizeof(struct bpf_insn); |
| 6102 | } |
| 6103 | |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 6104 | int bpf_program__set_prep(struct bpf_program *prog, int nr_instances, |
| 6105 | bpf_program_prep_t prep) |
| 6106 | { |
| 6107 | int *instances_fds; |
| 6108 | |
| 6109 | if (nr_instances <= 0 || !prep) |
| 6110 | return -EINVAL; |
| 6111 | |
| 6112 | if (prog->instances.nr > 0 || prog->instances.fds) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6113 | pr_warn("Can't set pre-processor after loading\n"); |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 6114 | return -EINVAL; |
| 6115 | } |
| 6116 | |
| 6117 | instances_fds = malloc(sizeof(int) * nr_instances); |
| 6118 | if (!instances_fds) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6119 | pr_warn("alloc memory failed for fds\n"); |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 6120 | return -ENOMEM; |
| 6121 | } |
| 6122 | |
| 6123 | /* fill all fd with -1 */ |
| 6124 | memset(instances_fds, -1, sizeof(int) * nr_instances); |
| 6125 | |
| 6126 | prog->instances.nr = nr_instances; |
| 6127 | prog->instances.fds = instances_fds; |
| 6128 | prog->preprocessor = prep; |
| 6129 | return 0; |
| 6130 | } |
| 6131 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 6132 | int bpf_program__nth_fd(const struct bpf_program *prog, int n) |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 6133 | { |
| 6134 | int fd; |
| 6135 | |
Jakub Kicinski | 1e96004 | 2018-07-26 14:32:18 -0700 | [diff] [blame] | 6136 | if (!prog) |
| 6137 | return -EINVAL; |
| 6138 | |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 6139 | if (n >= prog->instances.nr || n < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6140 | pr_warn("Can't get the %dth fd from program %s: only %d instances\n", |
| 6141 | n, prog->section_name, prog->instances.nr); |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 6142 | return -EINVAL; |
| 6143 | } |
| 6144 | |
| 6145 | fd = prog->instances.fds[n]; |
| 6146 | if (fd < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6147 | pr_warn("%dth instance of program '%s' is invalid\n", |
| 6148 | n, prog->section_name); |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 6149 | return -ENOENT; |
| 6150 | } |
| 6151 | |
| 6152 | return fd; |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 6153 | } |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 6154 | |
Andrii Nakryiko | f1eead9 | 2019-10-20 20:38:57 -0700 | [diff] [blame] | 6155 | enum bpf_prog_type bpf_program__get_type(struct bpf_program *prog) |
| 6156 | { |
| 6157 | return prog->type; |
| 6158 | } |
| 6159 | |
Alexei Starovoitov | dd26b7f | 2017-03-30 21:45:40 -0700 | [diff] [blame] | 6160 | void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type) |
Wang Nan | 5f44e4c8 | 2016-07-13 10:44:01 +0000 | [diff] [blame] | 6161 | { |
| 6162 | prog->type = type; |
| 6163 | } |
| 6164 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 6165 | static bool bpf_program__is_type(const struct bpf_program *prog, |
Wang Nan | 5f44e4c8 | 2016-07-13 10:44:01 +0000 | [diff] [blame] | 6166 | enum bpf_prog_type type) |
| 6167 | { |
| 6168 | return prog ? (prog->type == type) : false; |
| 6169 | } |
| 6170 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 6171 | #define BPF_PROG_TYPE_FNS(NAME, TYPE) \ |
| 6172 | int bpf_program__set_##NAME(struct bpf_program *prog) \ |
| 6173 | { \ |
| 6174 | if (!prog) \ |
| 6175 | return -EINVAL; \ |
| 6176 | bpf_program__set_type(prog, TYPE); \ |
| 6177 | return 0; \ |
| 6178 | } \ |
| 6179 | \ |
| 6180 | bool bpf_program__is_##NAME(const struct bpf_program *prog) \ |
| 6181 | { \ |
| 6182 | return bpf_program__is_type(prog, TYPE); \ |
| 6183 | } \ |
Wang Nan | 5f44e4c8 | 2016-07-13 10:44:01 +0000 | [diff] [blame] | 6184 | |
Joe Stringer | 7803ba7 | 2017-01-22 17:11:24 -0800 | [diff] [blame] | 6185 | BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER); |
Joe Stringer | ed79407 | 2017-01-22 17:11:23 -0800 | [diff] [blame] | 6186 | BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE); |
Joe Stringer | 7803ba7 | 2017-01-22 17:11:24 -0800 | [diff] [blame] | 6187 | BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS); |
| 6188 | BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT); |
Joe Stringer | ed79407 | 2017-01-22 17:11:23 -0800 | [diff] [blame] | 6189 | BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT); |
Andrey Ignatov | e14c93fd | 2018-04-17 10:28:46 -0700 | [diff] [blame] | 6190 | BPF_PROG_TYPE_FNS(raw_tracepoint, BPF_PROG_TYPE_RAW_TRACEPOINT); |
Joe Stringer | 7803ba7 | 2017-01-22 17:11:24 -0800 | [diff] [blame] | 6191 | BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP); |
| 6192 | BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT); |
Alexei Starovoitov | 12a8654 | 2019-10-30 15:32:12 -0700 | [diff] [blame] | 6193 | BPF_PROG_TYPE_FNS(tracing, BPF_PROG_TYPE_TRACING); |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 6194 | BPF_PROG_TYPE_FNS(struct_ops, BPF_PROG_TYPE_STRUCT_OPS); |
Alexei Starovoitov | 2db6eab | 2020-01-20 16:53:47 -0800 | [diff] [blame] | 6195 | BPF_PROG_TYPE_FNS(extension, BPF_PROG_TYPE_EXT); |
Wang Nan | 5f44e4c8 | 2016-07-13 10:44:01 +0000 | [diff] [blame] | 6196 | |
Andrii Nakryiko | f1eead9 | 2019-10-20 20:38:57 -0700 | [diff] [blame] | 6197 | enum bpf_attach_type |
| 6198 | bpf_program__get_expected_attach_type(struct bpf_program *prog) |
| 6199 | { |
| 6200 | return prog->expected_attach_type; |
| 6201 | } |
| 6202 | |
John Fastabend | 16962b2 | 2018-04-23 14:30:38 -0700 | [diff] [blame] | 6203 | void bpf_program__set_expected_attach_type(struct bpf_program *prog, |
| 6204 | enum bpf_attach_type type) |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 6205 | { |
| 6206 | prog->expected_attach_type = type; |
| 6207 | } |
| 6208 | |
Alexei Starovoitov | f75a697 | 2019-10-15 20:24:59 -0700 | [diff] [blame] | 6209 | #define BPF_PROG_SEC_IMPL(string, ptype, eatype, is_attachable, btf, atype) \ |
| 6210 | { string, sizeof(string) - 1, ptype, eatype, is_attachable, btf, atype } |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 6211 | |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 6212 | /* Programs that can NOT be attached. */ |
Alexei Starovoitov | f75a697 | 2019-10-15 20:24:59 -0700 | [diff] [blame] | 6213 | #define BPF_PROG_SEC(string, ptype) BPF_PROG_SEC_IMPL(string, ptype, 0, 0, 0, 0) |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 6214 | |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 6215 | /* Programs that can be attached. */ |
| 6216 | #define BPF_APROG_SEC(string, ptype, atype) \ |
Alexei Starovoitov | f75a697 | 2019-10-15 20:24:59 -0700 | [diff] [blame] | 6217 | BPF_PROG_SEC_IMPL(string, ptype, 0, 1, 0, atype) |
Andrey Ignatov | 81efee7 | 2018-04-17 10:28:45 -0700 | [diff] [blame] | 6218 | |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 6219 | /* Programs that must specify expected attach type at load time. */ |
| 6220 | #define BPF_EAPROG_SEC(string, ptype, eatype) \ |
Alexei Starovoitov | f75a697 | 2019-10-15 20:24:59 -0700 | [diff] [blame] | 6221 | BPF_PROG_SEC_IMPL(string, ptype, eatype, 1, 0, eatype) |
| 6222 | |
| 6223 | /* Programs that use BTF to identify attach point */ |
Alexei Starovoitov | 12a8654 | 2019-10-30 15:32:12 -0700 | [diff] [blame] | 6224 | #define BPF_PROG_BTF(string, ptype, eatype) \ |
| 6225 | BPF_PROG_SEC_IMPL(string, ptype, eatype, 0, 1, 0) |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 6226 | |
| 6227 | /* Programs that can be attached but attach type can't be identified by section |
| 6228 | * name. Kept for backward compatibility. |
| 6229 | */ |
| 6230 | #define BPF_APROG_COMPAT(string, ptype) BPF_PROG_SEC(string, ptype) |
Andrey Ignatov | e50b0a6 | 2018-03-30 15:08:03 -0700 | [diff] [blame] | 6231 | |
Andrii Nakryiko | d7a18ea | 2019-12-13 17:43:26 -0800 | [diff] [blame] | 6232 | #define SEC_DEF(sec_pfx, ptype, ...) { \ |
| 6233 | .sec = sec_pfx, \ |
| 6234 | .len = sizeof(sec_pfx) - 1, \ |
| 6235 | .prog_type = BPF_PROG_TYPE_##ptype, \ |
| 6236 | __VA_ARGS__ \ |
| 6237 | } |
| 6238 | |
| 6239 | struct bpf_sec_def; |
| 6240 | |
| 6241 | typedef struct bpf_link *(*attach_fn_t)(const struct bpf_sec_def *sec, |
| 6242 | struct bpf_program *prog); |
| 6243 | |
| 6244 | static struct bpf_link *attach_kprobe(const struct bpf_sec_def *sec, |
| 6245 | struct bpf_program *prog); |
| 6246 | static struct bpf_link *attach_tp(const struct bpf_sec_def *sec, |
| 6247 | struct bpf_program *prog); |
| 6248 | static struct bpf_link *attach_raw_tp(const struct bpf_sec_def *sec, |
| 6249 | struct bpf_program *prog); |
| 6250 | static struct bpf_link *attach_trace(const struct bpf_sec_def *sec, |
| 6251 | struct bpf_program *prog); |
| 6252 | |
| 6253 | struct bpf_sec_def { |
Roman Gushchin | 583c900 | 2017-12-13 15:18:51 +0000 | [diff] [blame] | 6254 | const char *sec; |
| 6255 | size_t len; |
| 6256 | enum bpf_prog_type prog_type; |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 6257 | enum bpf_attach_type expected_attach_type; |
Alexei Starovoitov | f75a697 | 2019-10-15 20:24:59 -0700 | [diff] [blame] | 6258 | bool is_attachable; |
| 6259 | bool is_attach_btf; |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 6260 | enum bpf_attach_type attach_type; |
Andrii Nakryiko | d7a18ea | 2019-12-13 17:43:26 -0800 | [diff] [blame] | 6261 | attach_fn_t attach_fn; |
| 6262 | }; |
| 6263 | |
| 6264 | static const struct bpf_sec_def section_defs[] = { |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 6265 | BPF_PROG_SEC("socket", BPF_PROG_TYPE_SOCKET_FILTER), |
Jakub Sitnicki | 67d69cc | 2019-12-12 11:22:50 +0100 | [diff] [blame] | 6266 | BPF_PROG_SEC("sk_reuseport", BPF_PROG_TYPE_SK_REUSEPORT), |
Andrii Nakryiko | d7a18ea | 2019-12-13 17:43:26 -0800 | [diff] [blame] | 6267 | SEC_DEF("kprobe/", KPROBE, |
| 6268 | .attach_fn = attach_kprobe), |
Andrii Nakryiko | 32dff6d | 2019-10-20 20:38:58 -0700 | [diff] [blame] | 6269 | BPF_PROG_SEC("uprobe/", BPF_PROG_TYPE_KPROBE), |
Andrii Nakryiko | d7a18ea | 2019-12-13 17:43:26 -0800 | [diff] [blame] | 6270 | SEC_DEF("kretprobe/", KPROBE, |
| 6271 | .attach_fn = attach_kprobe), |
Andrii Nakryiko | 32dff6d | 2019-10-20 20:38:58 -0700 | [diff] [blame] | 6272 | BPF_PROG_SEC("uretprobe/", BPF_PROG_TYPE_KPROBE), |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 6273 | BPF_PROG_SEC("classifier", BPF_PROG_TYPE_SCHED_CLS), |
| 6274 | BPF_PROG_SEC("action", BPF_PROG_TYPE_SCHED_ACT), |
Andrii Nakryiko | d7a18ea | 2019-12-13 17:43:26 -0800 | [diff] [blame] | 6275 | SEC_DEF("tracepoint/", TRACEPOINT, |
| 6276 | .attach_fn = attach_tp), |
| 6277 | SEC_DEF("tp/", TRACEPOINT, |
| 6278 | .attach_fn = attach_tp), |
| 6279 | SEC_DEF("raw_tracepoint/", RAW_TRACEPOINT, |
| 6280 | .attach_fn = attach_raw_tp), |
| 6281 | SEC_DEF("raw_tp/", RAW_TRACEPOINT, |
| 6282 | .attach_fn = attach_raw_tp), |
| 6283 | SEC_DEF("tp_btf/", TRACING, |
| 6284 | .expected_attach_type = BPF_TRACE_RAW_TP, |
| 6285 | .is_attach_btf = true, |
| 6286 | .attach_fn = attach_trace), |
| 6287 | SEC_DEF("fentry/", TRACING, |
| 6288 | .expected_attach_type = BPF_TRACE_FENTRY, |
| 6289 | .is_attach_btf = true, |
| 6290 | .attach_fn = attach_trace), |
KP Singh | aca228c | 2020-03-04 20:18:51 +0100 | [diff] [blame^] | 6291 | SEC_DEF("fmod_ret/", TRACING, |
| 6292 | .expected_attach_type = BPF_MODIFY_RETURN, |
| 6293 | .is_attach_btf = true, |
| 6294 | .attach_fn = attach_trace), |
Andrii Nakryiko | d7a18ea | 2019-12-13 17:43:26 -0800 | [diff] [blame] | 6295 | SEC_DEF("fexit/", TRACING, |
| 6296 | .expected_attach_type = BPF_TRACE_FEXIT, |
| 6297 | .is_attach_btf = true, |
| 6298 | .attach_fn = attach_trace), |
Alexei Starovoitov | 2db6eab | 2020-01-20 16:53:47 -0800 | [diff] [blame] | 6299 | SEC_DEF("freplace/", EXT, |
| 6300 | .is_attach_btf = true, |
| 6301 | .attach_fn = attach_trace), |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 6302 | BPF_PROG_SEC("xdp", BPF_PROG_TYPE_XDP), |
| 6303 | BPF_PROG_SEC("perf_event", BPF_PROG_TYPE_PERF_EVENT), |
| 6304 | BPF_PROG_SEC("lwt_in", BPF_PROG_TYPE_LWT_IN), |
| 6305 | BPF_PROG_SEC("lwt_out", BPF_PROG_TYPE_LWT_OUT), |
| 6306 | BPF_PROG_SEC("lwt_xmit", BPF_PROG_TYPE_LWT_XMIT), |
| 6307 | BPF_PROG_SEC("lwt_seg6local", BPF_PROG_TYPE_LWT_SEG6LOCAL), |
Andrey Ignatov | bafa7af | 2018-09-26 15:24:54 -0700 | [diff] [blame] | 6308 | BPF_APROG_SEC("cgroup_skb/ingress", BPF_PROG_TYPE_CGROUP_SKB, |
| 6309 | BPF_CGROUP_INET_INGRESS), |
| 6310 | BPF_APROG_SEC("cgroup_skb/egress", BPF_PROG_TYPE_CGROUP_SKB, |
| 6311 | BPF_CGROUP_INET_EGRESS), |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 6312 | BPF_APROG_COMPAT("cgroup/skb", BPF_PROG_TYPE_CGROUP_SKB), |
| 6313 | BPF_APROG_SEC("cgroup/sock", BPF_PROG_TYPE_CGROUP_SOCK, |
| 6314 | BPF_CGROUP_INET_SOCK_CREATE), |
| 6315 | BPF_EAPROG_SEC("cgroup/post_bind4", BPF_PROG_TYPE_CGROUP_SOCK, |
| 6316 | BPF_CGROUP_INET4_POST_BIND), |
| 6317 | BPF_EAPROG_SEC("cgroup/post_bind6", BPF_PROG_TYPE_CGROUP_SOCK, |
| 6318 | BPF_CGROUP_INET6_POST_BIND), |
| 6319 | BPF_APROG_SEC("cgroup/dev", BPF_PROG_TYPE_CGROUP_DEVICE, |
| 6320 | BPF_CGROUP_DEVICE), |
| 6321 | BPF_APROG_SEC("sockops", BPF_PROG_TYPE_SOCK_OPS, |
| 6322 | BPF_CGROUP_SOCK_OPS), |
Andrey Ignatov | c6f6851 | 2018-09-26 15:24:55 -0700 | [diff] [blame] | 6323 | BPF_APROG_SEC("sk_skb/stream_parser", BPF_PROG_TYPE_SK_SKB, |
| 6324 | BPF_SK_SKB_STREAM_PARSER), |
| 6325 | BPF_APROG_SEC("sk_skb/stream_verdict", BPF_PROG_TYPE_SK_SKB, |
| 6326 | BPF_SK_SKB_STREAM_VERDICT), |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 6327 | BPF_APROG_COMPAT("sk_skb", BPF_PROG_TYPE_SK_SKB), |
| 6328 | BPF_APROG_SEC("sk_msg", BPF_PROG_TYPE_SK_MSG, |
| 6329 | BPF_SK_MSG_VERDICT), |
| 6330 | BPF_APROG_SEC("lirc_mode2", BPF_PROG_TYPE_LIRC_MODE2, |
| 6331 | BPF_LIRC_MODE2), |
| 6332 | BPF_APROG_SEC("flow_dissector", BPF_PROG_TYPE_FLOW_DISSECTOR, |
| 6333 | BPF_FLOW_DISSECTOR), |
| 6334 | BPF_EAPROG_SEC("cgroup/bind4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, |
| 6335 | BPF_CGROUP_INET4_BIND), |
| 6336 | BPF_EAPROG_SEC("cgroup/bind6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, |
| 6337 | BPF_CGROUP_INET6_BIND), |
| 6338 | BPF_EAPROG_SEC("cgroup/connect4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, |
| 6339 | BPF_CGROUP_INET4_CONNECT), |
| 6340 | BPF_EAPROG_SEC("cgroup/connect6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, |
| 6341 | BPF_CGROUP_INET6_CONNECT), |
| 6342 | BPF_EAPROG_SEC("cgroup/sendmsg4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, |
| 6343 | BPF_CGROUP_UDP4_SENDMSG), |
| 6344 | BPF_EAPROG_SEC("cgroup/sendmsg6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, |
| 6345 | BPF_CGROUP_UDP6_SENDMSG), |
Daniel Borkmann | 9bb59ac | 2019-06-07 01:48:59 +0200 | [diff] [blame] | 6346 | BPF_EAPROG_SEC("cgroup/recvmsg4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, |
| 6347 | BPF_CGROUP_UDP4_RECVMSG), |
| 6348 | BPF_EAPROG_SEC("cgroup/recvmsg6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, |
| 6349 | BPF_CGROUP_UDP6_RECVMSG), |
Andrey Ignatov | 063cc9f | 2019-03-08 09:15:26 -0800 | [diff] [blame] | 6350 | BPF_EAPROG_SEC("cgroup/sysctl", BPF_PROG_TYPE_CGROUP_SYSCTL, |
| 6351 | BPF_CGROUP_SYSCTL), |
Stanislav Fomichev | 4cdbfb5 | 2019-06-27 13:38:49 -0700 | [diff] [blame] | 6352 | BPF_EAPROG_SEC("cgroup/getsockopt", BPF_PROG_TYPE_CGROUP_SOCKOPT, |
| 6353 | BPF_CGROUP_GETSOCKOPT), |
| 6354 | BPF_EAPROG_SEC("cgroup/setsockopt", BPF_PROG_TYPE_CGROUP_SOCKOPT, |
| 6355 | BPF_CGROUP_SETSOCKOPT), |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 6356 | BPF_PROG_SEC("struct_ops", BPF_PROG_TYPE_STRUCT_OPS), |
Roman Gushchin | 583c900 | 2017-12-13 15:18:51 +0000 | [diff] [blame] | 6357 | }; |
Roman Gushchin | 583c900 | 2017-12-13 15:18:51 +0000 | [diff] [blame] | 6358 | |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 6359 | #undef BPF_PROG_SEC_IMPL |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 6360 | #undef BPF_PROG_SEC |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 6361 | #undef BPF_APROG_SEC |
| 6362 | #undef BPF_EAPROG_SEC |
| 6363 | #undef BPF_APROG_COMPAT |
Andrii Nakryiko | d7a18ea | 2019-12-13 17:43:26 -0800 | [diff] [blame] | 6364 | #undef SEC_DEF |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 6365 | |
Taeung Song | c76e4c2 | 2019-01-21 22:06:38 +0900 | [diff] [blame] | 6366 | #define MAX_TYPE_NAME_SIZE 32 |
| 6367 | |
Andrii Nakryiko | d7a18ea | 2019-12-13 17:43:26 -0800 | [diff] [blame] | 6368 | static const struct bpf_sec_def *find_sec_def(const char *sec_name) |
| 6369 | { |
| 6370 | int i, n = ARRAY_SIZE(section_defs); |
| 6371 | |
| 6372 | for (i = 0; i < n; i++) { |
| 6373 | if (strncmp(sec_name, |
| 6374 | section_defs[i].sec, section_defs[i].len)) |
| 6375 | continue; |
| 6376 | return §ion_defs[i]; |
| 6377 | } |
| 6378 | return NULL; |
| 6379 | } |
| 6380 | |
Taeung Song | c76e4c2 | 2019-01-21 22:06:38 +0900 | [diff] [blame] | 6381 | static char *libbpf_get_type_names(bool attach_type) |
| 6382 | { |
Andrii Nakryiko | d7a18ea | 2019-12-13 17:43:26 -0800 | [diff] [blame] | 6383 | int i, len = ARRAY_SIZE(section_defs) * MAX_TYPE_NAME_SIZE; |
Taeung Song | c76e4c2 | 2019-01-21 22:06:38 +0900 | [diff] [blame] | 6384 | char *buf; |
| 6385 | |
| 6386 | buf = malloc(len); |
| 6387 | if (!buf) |
| 6388 | return NULL; |
| 6389 | |
| 6390 | buf[0] = '\0'; |
| 6391 | /* Forge string buf with all available names */ |
Andrii Nakryiko | d7a18ea | 2019-12-13 17:43:26 -0800 | [diff] [blame] | 6392 | for (i = 0; i < ARRAY_SIZE(section_defs); i++) { |
| 6393 | if (attach_type && !section_defs[i].is_attachable) |
Taeung Song | c76e4c2 | 2019-01-21 22:06:38 +0900 | [diff] [blame] | 6394 | continue; |
| 6395 | |
Andrii Nakryiko | d7a18ea | 2019-12-13 17:43:26 -0800 | [diff] [blame] | 6396 | if (strlen(buf) + strlen(section_defs[i].sec) + 2 > len) { |
Taeung Song | c76e4c2 | 2019-01-21 22:06:38 +0900 | [diff] [blame] | 6397 | free(buf); |
| 6398 | return NULL; |
| 6399 | } |
| 6400 | strcat(buf, " "); |
Andrii Nakryiko | d7a18ea | 2019-12-13 17:43:26 -0800 | [diff] [blame] | 6401 | strcat(buf, section_defs[i].sec); |
Taeung Song | c76e4c2 | 2019-01-21 22:06:38 +0900 | [diff] [blame] | 6402 | } |
| 6403 | |
| 6404 | return buf; |
| 6405 | } |
| 6406 | |
Jakub Kicinski | b60df2a | 2018-07-10 14:42:59 -0700 | [diff] [blame] | 6407 | int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type, |
| 6408 | enum bpf_attach_type *expected_attach_type) |
Roman Gushchin | 583c900 | 2017-12-13 15:18:51 +0000 | [diff] [blame] | 6409 | { |
Andrii Nakryiko | d7a18ea | 2019-12-13 17:43:26 -0800 | [diff] [blame] | 6410 | const struct bpf_sec_def *sec_def; |
Taeung Song | c76e4c2 | 2019-01-21 22:06:38 +0900 | [diff] [blame] | 6411 | char *type_names; |
Roman Gushchin | 583c900 | 2017-12-13 15:18:51 +0000 | [diff] [blame] | 6412 | |
Jakub Kicinski | b60df2a | 2018-07-10 14:42:59 -0700 | [diff] [blame] | 6413 | if (!name) |
| 6414 | return -EINVAL; |
Roman Gushchin | 583c900 | 2017-12-13 15:18:51 +0000 | [diff] [blame] | 6415 | |
Andrii Nakryiko | d7a18ea | 2019-12-13 17:43:26 -0800 | [diff] [blame] | 6416 | sec_def = find_sec_def(name); |
| 6417 | if (sec_def) { |
| 6418 | *prog_type = sec_def->prog_type; |
| 6419 | *expected_attach_type = sec_def->expected_attach_type; |
Jakub Kicinski | b60df2a | 2018-07-10 14:42:59 -0700 | [diff] [blame] | 6420 | return 0; |
| 6421 | } |
Andrii Nakryiko | d7a18ea | 2019-12-13 17:43:26 -0800 | [diff] [blame] | 6422 | |
Andrii Nakryiko | 4a3d6c6 | 2019-12-17 15:42:28 -0800 | [diff] [blame] | 6423 | pr_debug("failed to guess program type from ELF section '%s'\n", name); |
Taeung Song | c76e4c2 | 2019-01-21 22:06:38 +0900 | [diff] [blame] | 6424 | type_names = libbpf_get_type_names(false); |
| 6425 | if (type_names != NULL) { |
Andrii Nakryiko | 3f51935 | 2019-12-13 17:43:35 -0800 | [diff] [blame] | 6426 | pr_debug("supported section(type) names are:%s\n", type_names); |
Taeung Song | c76e4c2 | 2019-01-21 22:06:38 +0900 | [diff] [blame] | 6427 | free(type_names); |
| 6428 | } |
| 6429 | |
Andrii Nakryiko | dd4436b | 2019-10-20 20:38:59 -0700 | [diff] [blame] | 6430 | return -ESRCH; |
Jakub Kicinski | b60df2a | 2018-07-10 14:42:59 -0700 | [diff] [blame] | 6431 | } |
Roman Gushchin | 583c900 | 2017-12-13 15:18:51 +0000 | [diff] [blame] | 6432 | |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 6433 | static struct bpf_map *find_struct_ops_map_by_offset(struct bpf_object *obj, |
| 6434 | size_t offset) |
| 6435 | { |
| 6436 | struct bpf_map *map; |
| 6437 | size_t i; |
| 6438 | |
| 6439 | for (i = 0; i < obj->nr_maps; i++) { |
| 6440 | map = &obj->maps[i]; |
| 6441 | if (!bpf_map__is_struct_ops(map)) |
| 6442 | continue; |
| 6443 | if (map->sec_offset <= offset && |
| 6444 | offset - map->sec_offset < map->def.value_size) |
| 6445 | return map; |
| 6446 | } |
| 6447 | |
| 6448 | return NULL; |
| 6449 | } |
| 6450 | |
| 6451 | /* Collect the reloc from ELF and populate the st_ops->progs[] */ |
| 6452 | static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj, |
| 6453 | GElf_Shdr *shdr, |
| 6454 | Elf_Data *data) |
| 6455 | { |
| 6456 | const struct btf_member *member; |
| 6457 | struct bpf_struct_ops *st_ops; |
| 6458 | struct bpf_program *prog; |
| 6459 | unsigned int shdr_idx; |
| 6460 | const struct btf *btf; |
| 6461 | struct bpf_map *map; |
| 6462 | Elf_Data *symbols; |
| 6463 | unsigned int moff; |
| 6464 | const char *name; |
Andrii Nakryiko | 1d1a3bc | 2020-01-10 10:19:16 -0800 | [diff] [blame] | 6465 | __u32 member_idx; |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 6466 | GElf_Sym sym; |
| 6467 | GElf_Rel rel; |
| 6468 | int i, nrels; |
| 6469 | |
| 6470 | symbols = obj->efile.symbols; |
| 6471 | btf = obj->btf; |
| 6472 | nrels = shdr->sh_size / shdr->sh_entsize; |
| 6473 | for (i = 0; i < nrels; i++) { |
| 6474 | if (!gelf_getrel(data, i, &rel)) { |
| 6475 | pr_warn("struct_ops reloc: failed to get %d reloc\n", i); |
| 6476 | return -LIBBPF_ERRNO__FORMAT; |
| 6477 | } |
| 6478 | |
| 6479 | if (!gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym)) { |
| 6480 | pr_warn("struct_ops reloc: symbol %zx not found\n", |
| 6481 | (size_t)GELF_R_SYM(rel.r_info)); |
| 6482 | return -LIBBPF_ERRNO__FORMAT; |
| 6483 | } |
| 6484 | |
| 6485 | name = elf_strptr(obj->efile.elf, obj->efile.strtabidx, |
| 6486 | sym.st_name) ? : "<?>"; |
| 6487 | map = find_struct_ops_map_by_offset(obj, rel.r_offset); |
| 6488 | if (!map) { |
| 6489 | pr_warn("struct_ops reloc: cannot find map at rel.r_offset %zu\n", |
| 6490 | (size_t)rel.r_offset); |
| 6491 | return -EINVAL; |
| 6492 | } |
| 6493 | |
| 6494 | moff = rel.r_offset - map->sec_offset; |
| 6495 | shdr_idx = sym.st_shndx; |
| 6496 | st_ops = map->st_ops; |
| 6497 | pr_debug("struct_ops reloc %s: for %lld value %lld shdr_idx %u rel.r_offset %zu map->sec_offset %zu name %d (\'%s\')\n", |
| 6498 | map->name, |
| 6499 | (long long)(rel.r_info >> 32), |
| 6500 | (long long)sym.st_value, |
| 6501 | shdr_idx, (size_t)rel.r_offset, |
| 6502 | map->sec_offset, sym.st_name, name); |
| 6503 | |
| 6504 | if (shdr_idx >= SHN_LORESERVE) { |
| 6505 | pr_warn("struct_ops reloc %s: rel.r_offset %zu shdr_idx %u unsupported non-static function\n", |
| 6506 | map->name, (size_t)rel.r_offset, shdr_idx); |
| 6507 | return -LIBBPF_ERRNO__RELOC; |
| 6508 | } |
| 6509 | |
| 6510 | member = find_member_by_offset(st_ops->type, moff * 8); |
| 6511 | if (!member) { |
| 6512 | pr_warn("struct_ops reloc %s: cannot find member at moff %u\n", |
| 6513 | map->name, moff); |
| 6514 | return -EINVAL; |
| 6515 | } |
| 6516 | member_idx = member - btf_members(st_ops->type); |
| 6517 | name = btf__name_by_offset(btf, member->name_off); |
| 6518 | |
| 6519 | if (!resolve_func_ptr(btf, member->type, NULL)) { |
| 6520 | pr_warn("struct_ops reloc %s: cannot relocate non func ptr %s\n", |
| 6521 | map->name, name); |
| 6522 | return -EINVAL; |
| 6523 | } |
| 6524 | |
| 6525 | prog = bpf_object__find_prog_by_idx(obj, shdr_idx); |
| 6526 | if (!prog) { |
| 6527 | pr_warn("struct_ops reloc %s: cannot find prog at shdr_idx %u to relocate func ptr %s\n", |
| 6528 | map->name, shdr_idx, name); |
| 6529 | return -EINVAL; |
| 6530 | } |
| 6531 | |
| 6532 | if (prog->type == BPF_PROG_TYPE_UNSPEC) { |
| 6533 | const struct bpf_sec_def *sec_def; |
| 6534 | |
| 6535 | sec_def = find_sec_def(prog->section_name); |
| 6536 | if (sec_def && |
| 6537 | sec_def->prog_type != BPF_PROG_TYPE_STRUCT_OPS) { |
| 6538 | /* for pr_warn */ |
| 6539 | prog->type = sec_def->prog_type; |
| 6540 | goto invalid_prog; |
| 6541 | } |
| 6542 | |
| 6543 | prog->type = BPF_PROG_TYPE_STRUCT_OPS; |
| 6544 | prog->attach_btf_id = st_ops->type_id; |
| 6545 | prog->expected_attach_type = member_idx; |
| 6546 | } else if (prog->type != BPF_PROG_TYPE_STRUCT_OPS || |
| 6547 | prog->attach_btf_id != st_ops->type_id || |
| 6548 | prog->expected_attach_type != member_idx) { |
| 6549 | goto invalid_prog; |
| 6550 | } |
| 6551 | st_ops->progs[member_idx] = prog; |
| 6552 | } |
| 6553 | |
| 6554 | return 0; |
| 6555 | |
| 6556 | invalid_prog: |
| 6557 | pr_warn("struct_ops reloc %s: cannot use prog %s in sec %s with type %u attach_btf_id %u expected_attach_type %u for func ptr %s\n", |
| 6558 | map->name, prog->name, prog->section_name, prog->type, |
| 6559 | prog->attach_btf_id, prog->expected_attach_type, name); |
| 6560 | return -EINVAL; |
| 6561 | } |
| 6562 | |
KP Singh | a6ed02c | 2020-01-17 22:28:25 +0100 | [diff] [blame] | 6563 | #define BTF_TRACE_PREFIX "btf_trace_" |
| 6564 | #define BTF_MAX_NAME_SIZE 128 |
| 6565 | |
| 6566 | static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix, |
| 6567 | const char *name, __u32 kind) |
| 6568 | { |
| 6569 | char btf_type_name[BTF_MAX_NAME_SIZE]; |
| 6570 | int ret; |
| 6571 | |
| 6572 | ret = snprintf(btf_type_name, sizeof(btf_type_name), |
| 6573 | "%s%s", prefix, name); |
| 6574 | /* snprintf returns the number of characters written excluding the |
| 6575 | * the terminating null. So, if >= BTF_MAX_NAME_SIZE are written, it |
| 6576 | * indicates truncation. |
| 6577 | */ |
| 6578 | if (ret < 0 || ret >= sizeof(btf_type_name)) |
| 6579 | return -ENAMETOOLONG; |
| 6580 | return btf__find_by_name_kind(btf, btf_type_name, kind); |
| 6581 | } |
| 6582 | |
| 6583 | static inline int __find_vmlinux_btf_id(struct btf *btf, const char *name, |
| 6584 | enum bpf_attach_type attach_type) |
| 6585 | { |
| 6586 | int err; |
| 6587 | |
| 6588 | if (attach_type == BPF_TRACE_RAW_TP) |
| 6589 | err = find_btf_by_prefix_kind(btf, BTF_TRACE_PREFIX, name, |
| 6590 | BTF_KIND_TYPEDEF); |
| 6591 | else |
| 6592 | err = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC); |
| 6593 | |
Eelco Chaudron | ff26ce5 | 2020-02-20 13:26:35 +0000 | [diff] [blame] | 6594 | if (err <= 0) |
| 6595 | pr_warn("%s is not found in vmlinux BTF\n", name); |
| 6596 | |
KP Singh | a6ed02c | 2020-01-17 22:28:25 +0100 | [diff] [blame] | 6597 | return err; |
| 6598 | } |
| 6599 | |
Alexei Starovoitov | b8c54ea | 2019-11-14 10:57:06 -0800 | [diff] [blame] | 6600 | int libbpf_find_vmlinux_btf_id(const char *name, |
| 6601 | enum bpf_attach_type attach_type) |
Alexei Starovoitov | 12a8654 | 2019-10-30 15:32:12 -0700 | [diff] [blame] | 6602 | { |
KP Singh | a6ed02c | 2020-01-17 22:28:25 +0100 | [diff] [blame] | 6603 | struct btf *btf; |
Alexei Starovoitov | 12a8654 | 2019-10-30 15:32:12 -0700 | [diff] [blame] | 6604 | |
KP Singh | a6ed02c | 2020-01-17 22:28:25 +0100 | [diff] [blame] | 6605 | btf = libbpf_find_kernel_btf(); |
Alexei Starovoitov | 12a8654 | 2019-10-30 15:32:12 -0700 | [diff] [blame] | 6606 | if (IS_ERR(btf)) { |
| 6607 | pr_warn("vmlinux BTF is not found\n"); |
| 6608 | return -EINVAL; |
| 6609 | } |
| 6610 | |
KP Singh | a6ed02c | 2020-01-17 22:28:25 +0100 | [diff] [blame] | 6611 | return __find_vmlinux_btf_id(btf, name, attach_type); |
Alexei Starovoitov | b8c54ea | 2019-11-14 10:57:06 -0800 | [diff] [blame] | 6612 | } |
| 6613 | |
Alexei Starovoitov | e7bf94d | 2019-11-14 10:57:18 -0800 | [diff] [blame] | 6614 | static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd) |
| 6615 | { |
| 6616 | struct bpf_prog_info_linear *info_linear; |
| 6617 | struct bpf_prog_info *info; |
| 6618 | struct btf *btf = NULL; |
| 6619 | int err = -EINVAL; |
| 6620 | |
| 6621 | info_linear = bpf_program__get_prog_info_linear(attach_prog_fd, 0); |
| 6622 | if (IS_ERR_OR_NULL(info_linear)) { |
| 6623 | pr_warn("failed get_prog_info_linear for FD %d\n", |
| 6624 | attach_prog_fd); |
| 6625 | return -EINVAL; |
| 6626 | } |
| 6627 | info = &info_linear->info; |
| 6628 | if (!info->btf_id) { |
| 6629 | pr_warn("The target program doesn't have BTF\n"); |
| 6630 | goto out; |
| 6631 | } |
| 6632 | if (btf__get_from_id(info->btf_id, &btf)) { |
| 6633 | pr_warn("Failed to get BTF of the program\n"); |
| 6634 | goto out; |
| 6635 | } |
| 6636 | err = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC); |
| 6637 | btf__free(btf); |
| 6638 | if (err <= 0) { |
| 6639 | pr_warn("%s is not found in prog's BTF\n", name); |
| 6640 | goto out; |
| 6641 | } |
| 6642 | out: |
| 6643 | free(info_linear); |
| 6644 | return err; |
| 6645 | } |
| 6646 | |
KP Singh | a6ed02c | 2020-01-17 22:28:25 +0100 | [diff] [blame] | 6647 | static int libbpf_find_attach_btf_id(struct bpf_program *prog) |
Alexei Starovoitov | b8c54ea | 2019-11-14 10:57:06 -0800 | [diff] [blame] | 6648 | { |
KP Singh | a6ed02c | 2020-01-17 22:28:25 +0100 | [diff] [blame] | 6649 | enum bpf_attach_type attach_type = prog->expected_attach_type; |
| 6650 | __u32 attach_prog_fd = prog->attach_prog_fd; |
| 6651 | const char *name = prog->section_name; |
Alexei Starovoitov | b8c54ea | 2019-11-14 10:57:06 -0800 | [diff] [blame] | 6652 | int i, err; |
| 6653 | |
Alexei Starovoitov | 12a8654 | 2019-10-30 15:32:12 -0700 | [diff] [blame] | 6654 | if (!name) |
Alexei Starovoitov | b8c54ea | 2019-11-14 10:57:06 -0800 | [diff] [blame] | 6655 | return -EINVAL; |
Alexei Starovoitov | 12a8654 | 2019-10-30 15:32:12 -0700 | [diff] [blame] | 6656 | |
Andrii Nakryiko | d7a18ea | 2019-12-13 17:43:26 -0800 | [diff] [blame] | 6657 | for (i = 0; i < ARRAY_SIZE(section_defs); i++) { |
| 6658 | if (!section_defs[i].is_attach_btf) |
Alexei Starovoitov | 12a8654 | 2019-10-30 15:32:12 -0700 | [diff] [blame] | 6659 | continue; |
Andrii Nakryiko | d7a18ea | 2019-12-13 17:43:26 -0800 | [diff] [blame] | 6660 | if (strncmp(name, section_defs[i].sec, section_defs[i].len)) |
Alexei Starovoitov | 12a8654 | 2019-10-30 15:32:12 -0700 | [diff] [blame] | 6661 | continue; |
Alexei Starovoitov | e7bf94d | 2019-11-14 10:57:18 -0800 | [diff] [blame] | 6662 | if (attach_prog_fd) |
Andrii Nakryiko | d7a18ea | 2019-12-13 17:43:26 -0800 | [diff] [blame] | 6663 | err = libbpf_find_prog_btf_id(name + section_defs[i].len, |
Alexei Starovoitov | e7bf94d | 2019-11-14 10:57:18 -0800 | [diff] [blame] | 6664 | attach_prog_fd); |
| 6665 | else |
KP Singh | a6ed02c | 2020-01-17 22:28:25 +0100 | [diff] [blame] | 6666 | err = __find_vmlinux_btf_id(prog->obj->btf_vmlinux, |
| 6667 | name + section_defs[i].len, |
| 6668 | attach_type); |
Alexei Starovoitov | b8c54ea | 2019-11-14 10:57:06 -0800 | [diff] [blame] | 6669 | return err; |
Alexei Starovoitov | 12a8654 | 2019-10-30 15:32:12 -0700 | [diff] [blame] | 6670 | } |
| 6671 | pr_warn("failed to identify btf_id based on ELF section name '%s'\n", name); |
Alexei Starovoitov | b8c54ea | 2019-11-14 10:57:06 -0800 | [diff] [blame] | 6672 | return -ESRCH; |
Alexei Starovoitov | 12a8654 | 2019-10-30 15:32:12 -0700 | [diff] [blame] | 6673 | } |
| 6674 | |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 6675 | int libbpf_attach_type_by_name(const char *name, |
| 6676 | enum bpf_attach_type *attach_type) |
| 6677 | { |
Taeung Song | c76e4c2 | 2019-01-21 22:06:38 +0900 | [diff] [blame] | 6678 | char *type_names; |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 6679 | int i; |
| 6680 | |
| 6681 | if (!name) |
| 6682 | return -EINVAL; |
| 6683 | |
Andrii Nakryiko | d7a18ea | 2019-12-13 17:43:26 -0800 | [diff] [blame] | 6684 | for (i = 0; i < ARRAY_SIZE(section_defs); i++) { |
| 6685 | if (strncmp(name, section_defs[i].sec, section_defs[i].len)) |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 6686 | continue; |
Andrii Nakryiko | d7a18ea | 2019-12-13 17:43:26 -0800 | [diff] [blame] | 6687 | if (!section_defs[i].is_attachable) |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 6688 | return -EINVAL; |
Andrii Nakryiko | d7a18ea | 2019-12-13 17:43:26 -0800 | [diff] [blame] | 6689 | *attach_type = section_defs[i].attach_type; |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 6690 | return 0; |
| 6691 | } |
Andrii Nakryiko | 4a3d6c6 | 2019-12-17 15:42:28 -0800 | [diff] [blame] | 6692 | pr_debug("failed to guess attach type based on ELF section name '%s'\n", name); |
Taeung Song | c76e4c2 | 2019-01-21 22:06:38 +0900 | [diff] [blame] | 6693 | type_names = libbpf_get_type_names(true); |
| 6694 | if (type_names != NULL) { |
Andrii Nakryiko | 4a3d6c6 | 2019-12-17 15:42:28 -0800 | [diff] [blame] | 6695 | pr_debug("attachable section(type) names are:%s\n", type_names); |
Taeung Song | c76e4c2 | 2019-01-21 22:06:38 +0900 | [diff] [blame] | 6696 | free(type_names); |
| 6697 | } |
| 6698 | |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 6699 | return -EINVAL; |
| 6700 | } |
| 6701 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 6702 | int bpf_map__fd(const struct bpf_map *map) |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 6703 | { |
Arnaldo Carvalho de Melo | 6e009e65 | 2016-06-03 12:15:52 -0300 | [diff] [blame] | 6704 | return map ? map->fd : -EINVAL; |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 6705 | } |
| 6706 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 6707 | const struct bpf_map_def *bpf_map__def(const struct bpf_map *map) |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 6708 | { |
Arnaldo Carvalho de Melo | 53897a7 | 2016-06-02 14:21:06 -0300 | [diff] [blame] | 6709 | return map ? &map->def : ERR_PTR(-EINVAL); |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 6710 | } |
| 6711 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 6712 | const char *bpf_map__name(const struct bpf_map *map) |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 6713 | { |
Arnaldo Carvalho de Melo | 009ad5d | 2016-06-02 11:02:05 -0300 | [diff] [blame] | 6714 | return map ? map->name : NULL; |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 6715 | } |
| 6716 | |
Martin KaFai Lau | 5b891af | 2018-07-24 08:40:21 -0700 | [diff] [blame] | 6717 | __u32 bpf_map__btf_key_type_id(const struct bpf_map *map) |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 6718 | { |
Martin KaFai Lau | 61746dbe | 2018-05-22 15:04:24 -0700 | [diff] [blame] | 6719 | return map ? map->btf_key_type_id : 0; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 6720 | } |
| 6721 | |
Martin KaFai Lau | 5b891af | 2018-07-24 08:40:21 -0700 | [diff] [blame] | 6722 | __u32 bpf_map__btf_value_type_id(const struct bpf_map *map) |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 6723 | { |
Martin KaFai Lau | 61746dbe | 2018-05-22 15:04:24 -0700 | [diff] [blame] | 6724 | return map ? map->btf_value_type_id : 0; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 6725 | } |
| 6726 | |
Arnaldo Carvalho de Melo | edb13ed | 2016-06-03 12:38:21 -0300 | [diff] [blame] | 6727 | int bpf_map__set_priv(struct bpf_map *map, void *priv, |
| 6728 | bpf_map_clear_priv_t clear_priv) |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 6729 | { |
| 6730 | if (!map) |
| 6731 | return -EINVAL; |
| 6732 | |
| 6733 | if (map->priv) { |
| 6734 | if (map->clear_priv) |
| 6735 | map->clear_priv(map, map->priv); |
| 6736 | } |
| 6737 | |
| 6738 | map->priv = priv; |
| 6739 | map->clear_priv = clear_priv; |
| 6740 | return 0; |
| 6741 | } |
| 6742 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 6743 | void *bpf_map__priv(const struct bpf_map *map) |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 6744 | { |
Arnaldo Carvalho de Melo | b4cbfa5 | 2016-06-02 10:51:59 -0300 | [diff] [blame] | 6745 | return map ? map->priv : ERR_PTR(-EINVAL); |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 6746 | } |
| 6747 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 6748 | bool bpf_map__is_offload_neutral(const struct bpf_map *map) |
Jakub Kicinski | f83fb22 | 2018-07-10 14:43:01 -0700 | [diff] [blame] | 6749 | { |
| 6750 | return map->def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY; |
| 6751 | } |
| 6752 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 6753 | bool bpf_map__is_internal(const struct bpf_map *map) |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 6754 | { |
| 6755 | return map->libbpf_type != LIBBPF_MAP_UNSPEC; |
| 6756 | } |
| 6757 | |
Jakub Kicinski | 9aba361 | 2018-06-28 14:41:37 -0700 | [diff] [blame] | 6758 | void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex) |
| 6759 | { |
| 6760 | map->map_ifindex = ifindex; |
| 6761 | } |
| 6762 | |
Nikita V. Shirokov | addb9fc | 2018-11-20 20:55:56 -0800 | [diff] [blame] | 6763 | int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd) |
| 6764 | { |
| 6765 | if (!bpf_map_type__is_map_in_map(map->def.type)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6766 | pr_warn("error: unsupported map type\n"); |
Nikita V. Shirokov | addb9fc | 2018-11-20 20:55:56 -0800 | [diff] [blame] | 6767 | return -EINVAL; |
| 6768 | } |
| 6769 | if (map->inner_map_fd != -1) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6770 | pr_warn("error: inner_map_fd already specified\n"); |
Nikita V. Shirokov | addb9fc | 2018-11-20 20:55:56 -0800 | [diff] [blame] | 6771 | return -EINVAL; |
| 6772 | } |
| 6773 | map->inner_map_fd = fd; |
| 6774 | return 0; |
| 6775 | } |
| 6776 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 6777 | static struct bpf_map * |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 6778 | __bpf_map__iter(const struct bpf_map *m, const struct bpf_object *obj, int i) |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 6779 | { |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 6780 | ssize_t idx; |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 6781 | struct bpf_map *s, *e; |
| 6782 | |
| 6783 | if (!obj || !obj->maps) |
| 6784 | return NULL; |
| 6785 | |
| 6786 | s = obj->maps; |
| 6787 | e = obj->maps + obj->nr_maps; |
| 6788 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 6789 | if ((m < s) || (m >= e)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6790 | pr_warn("error in %s: map handler doesn't belong to object\n", |
| 6791 | __func__); |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 6792 | return NULL; |
| 6793 | } |
| 6794 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 6795 | idx = (m - obj->maps) + i; |
| 6796 | if (idx >= obj->nr_maps || idx < 0) |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 6797 | return NULL; |
| 6798 | return &obj->maps[idx]; |
| 6799 | } |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 6800 | |
| 6801 | struct bpf_map * |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 6802 | bpf_map__next(const struct bpf_map *prev, const struct bpf_object *obj) |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 6803 | { |
| 6804 | if (prev == NULL) |
| 6805 | return obj->maps; |
| 6806 | |
| 6807 | return __bpf_map__iter(prev, obj, 1); |
| 6808 | } |
| 6809 | |
| 6810 | struct bpf_map * |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 6811 | bpf_map__prev(const struct bpf_map *next, const struct bpf_object *obj) |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 6812 | { |
| 6813 | if (next == NULL) { |
| 6814 | if (!obj->nr_maps) |
| 6815 | return NULL; |
| 6816 | return obj->maps + obj->nr_maps - 1; |
| 6817 | } |
| 6818 | |
| 6819 | return __bpf_map__iter(next, obj, -1); |
| 6820 | } |
| 6821 | |
| 6822 | struct bpf_map * |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 6823 | bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name) |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 6824 | { |
| 6825 | struct bpf_map *pos; |
| 6826 | |
Jakub Kicinski | f74a53d9 | 2019-02-27 19:04:12 -0800 | [diff] [blame] | 6827 | bpf_object__for_each_map(pos, obj) { |
Wang Nan | 973170e | 2015-12-08 02:25:29 +0000 | [diff] [blame] | 6828 | if (pos->name && !strcmp(pos->name, name)) |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 6829 | return pos; |
| 6830 | } |
| 6831 | return NULL; |
| 6832 | } |
Wang Nan | 5a6acad | 2016-11-26 07:03:27 +0000 | [diff] [blame] | 6833 | |
Maciej Fijalkowski | f3cea32 | 2019-02-01 22:42:23 +0100 | [diff] [blame] | 6834 | int |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 6835 | bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name) |
Maciej Fijalkowski | f3cea32 | 2019-02-01 22:42:23 +0100 | [diff] [blame] | 6836 | { |
| 6837 | return bpf_map__fd(bpf_object__find_map_by_name(obj, name)); |
| 6838 | } |
| 6839 | |
Wang Nan | 5a6acad | 2016-11-26 07:03:27 +0000 | [diff] [blame] | 6840 | struct bpf_map * |
| 6841 | bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset) |
| 6842 | { |
Andrii Nakryiko | db48814 | 2019-06-17 12:26:54 -0700 | [diff] [blame] | 6843 | return ERR_PTR(-ENOTSUP); |
Wang Nan | 5a6acad | 2016-11-26 07:03:27 +0000 | [diff] [blame] | 6844 | } |
Joe Stringer | e28ff1a | 2017-01-22 17:11:25 -0800 | [diff] [blame] | 6845 | |
| 6846 | long libbpf_get_error(const void *ptr) |
| 6847 | { |
Hariprasad Kelam | d98363b | 2019-05-25 14:32:57 +0530 | [diff] [blame] | 6848 | return PTR_ERR_OR_ZERO(ptr); |
Joe Stringer | e28ff1a | 2017-01-22 17:11:25 -0800 | [diff] [blame] | 6849 | } |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 6850 | |
| 6851 | int bpf_prog_load(const char *file, enum bpf_prog_type type, |
| 6852 | struct bpf_object **pobj, int *prog_fd) |
| 6853 | { |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 6854 | struct bpf_prog_load_attr attr; |
| 6855 | |
| 6856 | memset(&attr, 0, sizeof(struct bpf_prog_load_attr)); |
| 6857 | attr.file = file; |
| 6858 | attr.prog_type = type; |
| 6859 | attr.expected_attach_type = 0; |
| 6860 | |
| 6861 | return bpf_prog_load_xattr(&attr, pobj, prog_fd); |
| 6862 | } |
| 6863 | |
| 6864 | int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr, |
| 6865 | struct bpf_object **pobj, int *prog_fd) |
| 6866 | { |
Leo Yan | 33bae18 | 2019-07-02 18:25:31 +0800 | [diff] [blame] | 6867 | struct bpf_object_open_attr open_attr = {}; |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 6868 | struct bpf_program *prog, *first_prog = NULL; |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 6869 | struct bpf_object *obj; |
David Beckett | f0307a7 | 2018-05-16 14:02:49 -0700 | [diff] [blame] | 6870 | struct bpf_map *map; |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 6871 | int err; |
| 6872 | |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 6873 | if (!attr) |
| 6874 | return -EINVAL; |
Jakub Kicinski | 17387dd | 2018-05-10 10:24:42 -0700 | [diff] [blame] | 6875 | if (!attr->file) |
| 6876 | return -EINVAL; |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 6877 | |
Leo Yan | 33bae18 | 2019-07-02 18:25:31 +0800 | [diff] [blame] | 6878 | open_attr.file = attr->file; |
| 6879 | open_attr.prog_type = attr->prog_type; |
| 6880 | |
Jakub Kicinski | 07f2d4e | 2018-07-10 14:43:02 -0700 | [diff] [blame] | 6881 | obj = bpf_object__open_xattr(&open_attr); |
Jakub Kicinski | 3597683 | 2018-05-10 10:09:34 -0700 | [diff] [blame] | 6882 | if (IS_ERR_OR_NULL(obj)) |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 6883 | return -ENOENT; |
| 6884 | |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 6885 | bpf_object__for_each_program(prog, obj) { |
Andrii Nakryiko | dd4436b | 2019-10-20 20:38:59 -0700 | [diff] [blame] | 6886 | enum bpf_attach_type attach_type = attr->expected_attach_type; |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 6887 | /* |
Andrii Nakryiko | dd4436b | 2019-10-20 20:38:59 -0700 | [diff] [blame] | 6888 | * to preserve backwards compatibility, bpf_prog_load treats |
| 6889 | * attr->prog_type, if specified, as an override to whatever |
| 6890 | * bpf_object__open guessed |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 6891 | */ |
Andrii Nakryiko | dd4436b | 2019-10-20 20:38:59 -0700 | [diff] [blame] | 6892 | if (attr->prog_type != BPF_PROG_TYPE_UNSPEC) { |
| 6893 | bpf_program__set_type(prog, attr->prog_type); |
| 6894 | bpf_program__set_expected_attach_type(prog, |
| 6895 | attach_type); |
| 6896 | } |
| 6897 | if (bpf_program__get_type(prog) == BPF_PROG_TYPE_UNSPEC) { |
| 6898 | /* |
| 6899 | * we haven't guessed from section name and user |
| 6900 | * didn't provide a fallback type, too bad... |
| 6901 | */ |
| 6902 | bpf_object__close(obj); |
| 6903 | return -EINVAL; |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 6904 | } |
| 6905 | |
Andrii Nakryiko | dd4436b | 2019-10-20 20:38:59 -0700 | [diff] [blame] | 6906 | prog->prog_ifindex = attr->ifindex; |
Alexei Starovoitov | da11b41 | 2019-04-01 21:27:47 -0700 | [diff] [blame] | 6907 | prog->log_level = attr->log_level; |
Jiong Wang | 0465619 | 2019-05-24 23:25:19 +0100 | [diff] [blame] | 6908 | prog->prog_flags = attr->prog_flags; |
Taeung Song | 69495d2 | 2018-09-03 08:30:07 +0900 | [diff] [blame] | 6909 | if (!first_prog) |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 6910 | first_prog = prog; |
| 6911 | } |
| 6912 | |
Jakub Kicinski | f74a53d9 | 2019-02-27 19:04:12 -0800 | [diff] [blame] | 6913 | bpf_object__for_each_map(map, obj) { |
Jakub Kicinski | f83fb22 | 2018-07-10 14:43:01 -0700 | [diff] [blame] | 6914 | if (!bpf_map__is_offload_neutral(map)) |
| 6915 | map->map_ifindex = attr->ifindex; |
David Beckett | f0307a7 | 2018-05-16 14:02:49 -0700 | [diff] [blame] | 6916 | } |
| 6917 | |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 6918 | if (!first_prog) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6919 | pr_warn("object file doesn't contain bpf program\n"); |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 6920 | bpf_object__close(obj); |
| 6921 | return -ENOENT; |
| 6922 | } |
| 6923 | |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 6924 | err = bpf_object__load(obj); |
| 6925 | if (err) { |
| 6926 | bpf_object__close(obj); |
| 6927 | return -EINVAL; |
| 6928 | } |
| 6929 | |
| 6930 | *pobj = obj; |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 6931 | *prog_fd = bpf_program__fd(first_prog); |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 6932 | return 0; |
| 6933 | } |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 6934 | |
Andrii Nakryiko | 1c2e9ef | 2019-07-01 16:58:56 -0700 | [diff] [blame] | 6935 | struct bpf_link { |
Andrii Nakryiko | d695870 | 2019-12-18 14:50:39 -0800 | [diff] [blame] | 6936 | int (*detach)(struct bpf_link *link); |
Andrii Nakryiko | 1c2e9ef | 2019-07-01 16:58:56 -0700 | [diff] [blame] | 6937 | int (*destroy)(struct bpf_link *link); |
Andrii Nakryiko | c016b68 | 2020-03-02 20:31:58 -0800 | [diff] [blame] | 6938 | char *pin_path; /* NULL, if not pinned */ |
| 6939 | int fd; /* hook FD, -1 if not applicable */ |
Andrii Nakryiko | d695870 | 2019-12-18 14:50:39 -0800 | [diff] [blame] | 6940 | bool disconnected; |
Andrii Nakryiko | 1c2e9ef | 2019-07-01 16:58:56 -0700 | [diff] [blame] | 6941 | }; |
| 6942 | |
Andrii Nakryiko | d695870 | 2019-12-18 14:50:39 -0800 | [diff] [blame] | 6943 | /* Release "ownership" of underlying BPF resource (typically, BPF program |
| 6944 | * attached to some BPF hook, e.g., tracepoint, kprobe, etc). Disconnected |
| 6945 | * link, when destructed through bpf_link__destroy() call won't attempt to |
| 6946 | * detach/unregisted that BPF resource. This is useful in situations where, |
| 6947 | * say, attached BPF program has to outlive userspace program that attached it |
| 6948 | * in the system. Depending on type of BPF program, though, there might be |
| 6949 | * additional steps (like pinning BPF program in BPF FS) necessary to ensure |
| 6950 | * exit of userspace program doesn't trigger automatic detachment and clean up |
| 6951 | * inside the kernel. |
| 6952 | */ |
| 6953 | void bpf_link__disconnect(struct bpf_link *link) |
| 6954 | { |
| 6955 | link->disconnected = true; |
| 6956 | } |
| 6957 | |
Andrii Nakryiko | 1c2e9ef | 2019-07-01 16:58:56 -0700 | [diff] [blame] | 6958 | int bpf_link__destroy(struct bpf_link *link) |
| 6959 | { |
Andrii Nakryiko | d695870 | 2019-12-18 14:50:39 -0800 | [diff] [blame] | 6960 | int err = 0; |
Andrii Nakryiko | 1c2e9ef | 2019-07-01 16:58:56 -0700 | [diff] [blame] | 6961 | |
| 6962 | if (!link) |
| 6963 | return 0; |
| 6964 | |
Andrii Nakryiko | d695870 | 2019-12-18 14:50:39 -0800 | [diff] [blame] | 6965 | if (!link->disconnected && link->detach) |
| 6966 | err = link->detach(link); |
| 6967 | if (link->destroy) |
| 6968 | link->destroy(link); |
Andrii Nakryiko | c016b68 | 2020-03-02 20:31:58 -0800 | [diff] [blame] | 6969 | if (link->pin_path) |
| 6970 | free(link->pin_path); |
Andrii Nakryiko | 1c2e9ef | 2019-07-01 16:58:56 -0700 | [diff] [blame] | 6971 | free(link); |
| 6972 | |
| 6973 | return err; |
| 6974 | } |
| 6975 | |
Andrii Nakryiko | c016b68 | 2020-03-02 20:31:58 -0800 | [diff] [blame] | 6976 | int bpf_link__fd(const struct bpf_link *link) |
| 6977 | { |
| 6978 | return link->fd; |
| 6979 | } |
| 6980 | |
| 6981 | const char *bpf_link__pin_path(const struct bpf_link *link) |
| 6982 | { |
| 6983 | return link->pin_path; |
| 6984 | } |
| 6985 | |
| 6986 | static int bpf_link__detach_fd(struct bpf_link *link) |
| 6987 | { |
| 6988 | return close(link->fd); |
| 6989 | } |
| 6990 | |
| 6991 | struct bpf_link *bpf_link__open(const char *path) |
| 6992 | { |
| 6993 | struct bpf_link *link; |
| 6994 | int fd; |
| 6995 | |
| 6996 | fd = bpf_obj_get(path); |
| 6997 | if (fd < 0) { |
| 6998 | fd = -errno; |
| 6999 | pr_warn("failed to open link at %s: %d\n", path, fd); |
| 7000 | return ERR_PTR(fd); |
| 7001 | } |
| 7002 | |
| 7003 | link = calloc(1, sizeof(*link)); |
| 7004 | if (!link) { |
| 7005 | close(fd); |
| 7006 | return ERR_PTR(-ENOMEM); |
| 7007 | } |
| 7008 | link->detach = &bpf_link__detach_fd; |
| 7009 | link->fd = fd; |
| 7010 | |
| 7011 | link->pin_path = strdup(path); |
| 7012 | if (!link->pin_path) { |
| 7013 | bpf_link__destroy(link); |
| 7014 | return ERR_PTR(-ENOMEM); |
| 7015 | } |
| 7016 | |
| 7017 | return link; |
| 7018 | } |
| 7019 | |
| 7020 | int bpf_link__pin(struct bpf_link *link, const char *path) |
| 7021 | { |
| 7022 | int err; |
| 7023 | |
| 7024 | if (link->pin_path) |
| 7025 | return -EBUSY; |
| 7026 | err = make_parent_dir(path); |
| 7027 | if (err) |
| 7028 | return err; |
| 7029 | err = check_path(path); |
| 7030 | if (err) |
| 7031 | return err; |
| 7032 | |
| 7033 | link->pin_path = strdup(path); |
| 7034 | if (!link->pin_path) |
| 7035 | return -ENOMEM; |
| 7036 | |
| 7037 | if (bpf_obj_pin(link->fd, link->pin_path)) { |
| 7038 | err = -errno; |
| 7039 | zfree(&link->pin_path); |
| 7040 | return err; |
| 7041 | } |
| 7042 | |
| 7043 | pr_debug("link fd=%d: pinned at %s\n", link->fd, link->pin_path); |
| 7044 | return 0; |
| 7045 | } |
| 7046 | |
| 7047 | int bpf_link__unpin(struct bpf_link *link) |
| 7048 | { |
| 7049 | int err; |
| 7050 | |
| 7051 | if (!link->pin_path) |
| 7052 | return -EINVAL; |
| 7053 | |
| 7054 | err = unlink(link->pin_path); |
| 7055 | if (err != 0) |
| 7056 | return -errno; |
| 7057 | |
| 7058 | pr_debug("link fd=%d: unpinned from %s\n", link->fd, link->pin_path); |
| 7059 | zfree(&link->pin_path); |
| 7060 | return 0; |
| 7061 | } |
Andrii Nakryiko | 63f2f5e | 2019-07-01 16:58:57 -0700 | [diff] [blame] | 7062 | |
Andrii Nakryiko | d695870 | 2019-12-18 14:50:39 -0800 | [diff] [blame] | 7063 | static int bpf_link__detach_perf_event(struct bpf_link *link) |
Andrii Nakryiko | 63f2f5e | 2019-07-01 16:58:57 -0700 | [diff] [blame] | 7064 | { |
Andrii Nakryiko | 63f2f5e | 2019-07-01 16:58:57 -0700 | [diff] [blame] | 7065 | int err; |
| 7066 | |
Andrii Nakryiko | c016b68 | 2020-03-02 20:31:58 -0800 | [diff] [blame] | 7067 | err = ioctl(link->fd, PERF_EVENT_IOC_DISABLE, 0); |
Andrii Nakryiko | 63f2f5e | 2019-07-01 16:58:57 -0700 | [diff] [blame] | 7068 | if (err) |
| 7069 | err = -errno; |
| 7070 | |
Andrii Nakryiko | c016b68 | 2020-03-02 20:31:58 -0800 | [diff] [blame] | 7071 | close(link->fd); |
Andrii Nakryiko | 63f2f5e | 2019-07-01 16:58:57 -0700 | [diff] [blame] | 7072 | return err; |
| 7073 | } |
| 7074 | |
| 7075 | struct bpf_link *bpf_program__attach_perf_event(struct bpf_program *prog, |
| 7076 | int pfd) |
| 7077 | { |
| 7078 | char errmsg[STRERR_BUFSIZE]; |
Andrii Nakryiko | c016b68 | 2020-03-02 20:31:58 -0800 | [diff] [blame] | 7079 | struct bpf_link *link; |
Andrii Nakryiko | 63f2f5e | 2019-07-01 16:58:57 -0700 | [diff] [blame] | 7080 | int prog_fd, err; |
| 7081 | |
| 7082 | if (pfd < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7083 | pr_warn("program '%s': invalid perf event FD %d\n", |
| 7084 | bpf_program__title(prog, false), pfd); |
Andrii Nakryiko | 63f2f5e | 2019-07-01 16:58:57 -0700 | [diff] [blame] | 7085 | return ERR_PTR(-EINVAL); |
| 7086 | } |
| 7087 | prog_fd = bpf_program__fd(prog); |
| 7088 | if (prog_fd < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7089 | pr_warn("program '%s': can't attach BPF program w/o FD (did you load it?)\n", |
| 7090 | bpf_program__title(prog, false)); |
Andrii Nakryiko | 63f2f5e | 2019-07-01 16:58:57 -0700 | [diff] [blame] | 7091 | return ERR_PTR(-EINVAL); |
| 7092 | } |
| 7093 | |
Andrii Nakryiko | d695870 | 2019-12-18 14:50:39 -0800 | [diff] [blame] | 7094 | link = calloc(1, sizeof(*link)); |
Andrii Nakryiko | 63f2f5e | 2019-07-01 16:58:57 -0700 | [diff] [blame] | 7095 | if (!link) |
| 7096 | return ERR_PTR(-ENOMEM); |
Andrii Nakryiko | c016b68 | 2020-03-02 20:31:58 -0800 | [diff] [blame] | 7097 | link->detach = &bpf_link__detach_perf_event; |
Andrii Nakryiko | 63f2f5e | 2019-07-01 16:58:57 -0700 | [diff] [blame] | 7098 | link->fd = pfd; |
| 7099 | |
| 7100 | if (ioctl(pfd, PERF_EVENT_IOC_SET_BPF, prog_fd) < 0) { |
| 7101 | err = -errno; |
| 7102 | free(link); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7103 | pr_warn("program '%s': failed to attach to pfd %d: %s\n", |
| 7104 | bpf_program__title(prog, false), pfd, |
Andrii Nakryiko | 63f2f5e | 2019-07-01 16:58:57 -0700 | [diff] [blame] | 7105 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
| 7106 | return ERR_PTR(err); |
| 7107 | } |
| 7108 | if (ioctl(pfd, PERF_EVENT_IOC_ENABLE, 0) < 0) { |
| 7109 | err = -errno; |
| 7110 | free(link); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7111 | pr_warn("program '%s': failed to enable pfd %d: %s\n", |
| 7112 | bpf_program__title(prog, false), pfd, |
Andrii Nakryiko | 63f2f5e | 2019-07-01 16:58:57 -0700 | [diff] [blame] | 7113 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
| 7114 | return ERR_PTR(err); |
| 7115 | } |
Andrii Nakryiko | c016b68 | 2020-03-02 20:31:58 -0800 | [diff] [blame] | 7116 | return link; |
Andrii Nakryiko | 63f2f5e | 2019-07-01 16:58:57 -0700 | [diff] [blame] | 7117 | } |
| 7118 | |
Andrii Nakryiko | b265002 | 2019-07-01 16:58:58 -0700 | [diff] [blame] | 7119 | /* |
| 7120 | * this function is expected to parse integer in the range of [0, 2^31-1] from |
| 7121 | * given file using scanf format string fmt. If actual parsed value is |
| 7122 | * negative, the result might be indistinguishable from error |
| 7123 | */ |
| 7124 | static int parse_uint_from_file(const char *file, const char *fmt) |
| 7125 | { |
| 7126 | char buf[STRERR_BUFSIZE]; |
| 7127 | int err, ret; |
| 7128 | FILE *f; |
| 7129 | |
| 7130 | f = fopen(file, "r"); |
| 7131 | if (!f) { |
| 7132 | err = -errno; |
| 7133 | pr_debug("failed to open '%s': %s\n", file, |
| 7134 | libbpf_strerror_r(err, buf, sizeof(buf))); |
| 7135 | return err; |
| 7136 | } |
| 7137 | err = fscanf(f, fmt, &ret); |
| 7138 | if (err != 1) { |
| 7139 | err = err == EOF ? -EIO : -errno; |
| 7140 | pr_debug("failed to parse '%s': %s\n", file, |
| 7141 | libbpf_strerror_r(err, buf, sizeof(buf))); |
| 7142 | fclose(f); |
| 7143 | return err; |
| 7144 | } |
| 7145 | fclose(f); |
| 7146 | return ret; |
| 7147 | } |
| 7148 | |
| 7149 | static int determine_kprobe_perf_type(void) |
| 7150 | { |
| 7151 | const char *file = "/sys/bus/event_source/devices/kprobe/type"; |
| 7152 | |
| 7153 | return parse_uint_from_file(file, "%d\n"); |
| 7154 | } |
| 7155 | |
| 7156 | static int determine_uprobe_perf_type(void) |
| 7157 | { |
| 7158 | const char *file = "/sys/bus/event_source/devices/uprobe/type"; |
| 7159 | |
| 7160 | return parse_uint_from_file(file, "%d\n"); |
| 7161 | } |
| 7162 | |
| 7163 | static int determine_kprobe_retprobe_bit(void) |
| 7164 | { |
| 7165 | const char *file = "/sys/bus/event_source/devices/kprobe/format/retprobe"; |
| 7166 | |
| 7167 | return parse_uint_from_file(file, "config:%d\n"); |
| 7168 | } |
| 7169 | |
| 7170 | static int determine_uprobe_retprobe_bit(void) |
| 7171 | { |
| 7172 | const char *file = "/sys/bus/event_source/devices/uprobe/format/retprobe"; |
| 7173 | |
| 7174 | return parse_uint_from_file(file, "config:%d\n"); |
| 7175 | } |
| 7176 | |
| 7177 | static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name, |
| 7178 | uint64_t offset, int pid) |
| 7179 | { |
| 7180 | struct perf_event_attr attr = {}; |
| 7181 | char errmsg[STRERR_BUFSIZE]; |
| 7182 | int type, pfd, err; |
| 7183 | |
| 7184 | type = uprobe ? determine_uprobe_perf_type() |
| 7185 | : determine_kprobe_perf_type(); |
| 7186 | if (type < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7187 | pr_warn("failed to determine %s perf type: %s\n", |
| 7188 | uprobe ? "uprobe" : "kprobe", |
| 7189 | libbpf_strerror_r(type, errmsg, sizeof(errmsg))); |
Andrii Nakryiko | b265002 | 2019-07-01 16:58:58 -0700 | [diff] [blame] | 7190 | return type; |
| 7191 | } |
| 7192 | if (retprobe) { |
| 7193 | int bit = uprobe ? determine_uprobe_retprobe_bit() |
| 7194 | : determine_kprobe_retprobe_bit(); |
| 7195 | |
| 7196 | if (bit < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7197 | pr_warn("failed to determine %s retprobe bit: %s\n", |
| 7198 | uprobe ? "uprobe" : "kprobe", |
| 7199 | libbpf_strerror_r(bit, errmsg, sizeof(errmsg))); |
Andrii Nakryiko | b265002 | 2019-07-01 16:58:58 -0700 | [diff] [blame] | 7200 | return bit; |
| 7201 | } |
| 7202 | attr.config |= 1 << bit; |
| 7203 | } |
| 7204 | attr.size = sizeof(attr); |
| 7205 | attr.type = type; |
Andrii Nakryiko | 36db2a9 | 2019-07-08 21:00:07 -0700 | [diff] [blame] | 7206 | attr.config1 = ptr_to_u64(name); /* kprobe_func or uprobe_path */ |
| 7207 | attr.config2 = offset; /* kprobe_addr or probe_offset */ |
Andrii Nakryiko | b265002 | 2019-07-01 16:58:58 -0700 | [diff] [blame] | 7208 | |
| 7209 | /* pid filter is meaningful only for uprobes */ |
| 7210 | pfd = syscall(__NR_perf_event_open, &attr, |
| 7211 | pid < 0 ? -1 : pid /* pid */, |
| 7212 | pid == -1 ? 0 : -1 /* cpu */, |
| 7213 | -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC); |
| 7214 | if (pfd < 0) { |
| 7215 | err = -errno; |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7216 | pr_warn("%s perf_event_open() failed: %s\n", |
| 7217 | uprobe ? "uprobe" : "kprobe", |
| 7218 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
Andrii Nakryiko | b265002 | 2019-07-01 16:58:58 -0700 | [diff] [blame] | 7219 | return err; |
| 7220 | } |
| 7221 | return pfd; |
| 7222 | } |
| 7223 | |
| 7224 | struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog, |
| 7225 | bool retprobe, |
| 7226 | const char *func_name) |
| 7227 | { |
| 7228 | char errmsg[STRERR_BUFSIZE]; |
| 7229 | struct bpf_link *link; |
| 7230 | int pfd, err; |
| 7231 | |
| 7232 | pfd = perf_event_open_probe(false /* uprobe */, retprobe, func_name, |
| 7233 | 0 /* offset */, -1 /* pid */); |
| 7234 | if (pfd < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7235 | pr_warn("program '%s': failed to create %s '%s' perf event: %s\n", |
| 7236 | bpf_program__title(prog, false), |
| 7237 | retprobe ? "kretprobe" : "kprobe", func_name, |
| 7238 | libbpf_strerror_r(pfd, errmsg, sizeof(errmsg))); |
Andrii Nakryiko | b265002 | 2019-07-01 16:58:58 -0700 | [diff] [blame] | 7239 | return ERR_PTR(pfd); |
| 7240 | } |
| 7241 | link = bpf_program__attach_perf_event(prog, pfd); |
| 7242 | if (IS_ERR(link)) { |
| 7243 | close(pfd); |
| 7244 | err = PTR_ERR(link); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7245 | pr_warn("program '%s': failed to attach to %s '%s': %s\n", |
| 7246 | bpf_program__title(prog, false), |
| 7247 | retprobe ? "kretprobe" : "kprobe", func_name, |
| 7248 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
Andrii Nakryiko | b265002 | 2019-07-01 16:58:58 -0700 | [diff] [blame] | 7249 | return link; |
| 7250 | } |
| 7251 | return link; |
| 7252 | } |
| 7253 | |
Andrii Nakryiko | d7a18ea | 2019-12-13 17:43:26 -0800 | [diff] [blame] | 7254 | static struct bpf_link *attach_kprobe(const struct bpf_sec_def *sec, |
| 7255 | struct bpf_program *prog) |
| 7256 | { |
| 7257 | const char *func_name; |
| 7258 | bool retprobe; |
| 7259 | |
| 7260 | func_name = bpf_program__title(prog, false) + sec->len; |
| 7261 | retprobe = strcmp(sec->sec, "kretprobe/") == 0; |
| 7262 | |
| 7263 | return bpf_program__attach_kprobe(prog, retprobe, func_name); |
| 7264 | } |
| 7265 | |
Andrii Nakryiko | b265002 | 2019-07-01 16:58:58 -0700 | [diff] [blame] | 7266 | struct bpf_link *bpf_program__attach_uprobe(struct bpf_program *prog, |
| 7267 | bool retprobe, pid_t pid, |
| 7268 | const char *binary_path, |
| 7269 | size_t func_offset) |
| 7270 | { |
| 7271 | char errmsg[STRERR_BUFSIZE]; |
| 7272 | struct bpf_link *link; |
| 7273 | int pfd, err; |
| 7274 | |
| 7275 | pfd = perf_event_open_probe(true /* uprobe */, retprobe, |
| 7276 | binary_path, func_offset, pid); |
| 7277 | if (pfd < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7278 | pr_warn("program '%s': failed to create %s '%s:0x%zx' perf event: %s\n", |
| 7279 | bpf_program__title(prog, false), |
| 7280 | retprobe ? "uretprobe" : "uprobe", |
| 7281 | binary_path, func_offset, |
| 7282 | libbpf_strerror_r(pfd, errmsg, sizeof(errmsg))); |
Andrii Nakryiko | b265002 | 2019-07-01 16:58:58 -0700 | [diff] [blame] | 7283 | return ERR_PTR(pfd); |
| 7284 | } |
| 7285 | link = bpf_program__attach_perf_event(prog, pfd); |
| 7286 | if (IS_ERR(link)) { |
| 7287 | close(pfd); |
| 7288 | err = PTR_ERR(link); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7289 | pr_warn("program '%s': failed to attach to %s '%s:0x%zx': %s\n", |
| 7290 | bpf_program__title(prog, false), |
| 7291 | retprobe ? "uretprobe" : "uprobe", |
| 7292 | binary_path, func_offset, |
| 7293 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
Andrii Nakryiko | b265002 | 2019-07-01 16:58:58 -0700 | [diff] [blame] | 7294 | return link; |
| 7295 | } |
| 7296 | return link; |
| 7297 | } |
| 7298 | |
Andrii Nakryiko | f6de59c | 2019-07-01 16:58:59 -0700 | [diff] [blame] | 7299 | static int determine_tracepoint_id(const char *tp_category, |
| 7300 | const char *tp_name) |
| 7301 | { |
| 7302 | char file[PATH_MAX]; |
| 7303 | int ret; |
| 7304 | |
| 7305 | ret = snprintf(file, sizeof(file), |
| 7306 | "/sys/kernel/debug/tracing/events/%s/%s/id", |
| 7307 | tp_category, tp_name); |
| 7308 | if (ret < 0) |
| 7309 | return -errno; |
| 7310 | if (ret >= sizeof(file)) { |
| 7311 | pr_debug("tracepoint %s/%s path is too long\n", |
| 7312 | tp_category, tp_name); |
| 7313 | return -E2BIG; |
| 7314 | } |
| 7315 | return parse_uint_from_file(file, "%d\n"); |
| 7316 | } |
| 7317 | |
| 7318 | static int perf_event_open_tracepoint(const char *tp_category, |
| 7319 | const char *tp_name) |
| 7320 | { |
| 7321 | struct perf_event_attr attr = {}; |
| 7322 | char errmsg[STRERR_BUFSIZE]; |
| 7323 | int tp_id, pfd, err; |
| 7324 | |
| 7325 | tp_id = determine_tracepoint_id(tp_category, tp_name); |
| 7326 | if (tp_id < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7327 | pr_warn("failed to determine tracepoint '%s/%s' perf event ID: %s\n", |
| 7328 | tp_category, tp_name, |
| 7329 | libbpf_strerror_r(tp_id, errmsg, sizeof(errmsg))); |
Andrii Nakryiko | f6de59c | 2019-07-01 16:58:59 -0700 | [diff] [blame] | 7330 | return tp_id; |
| 7331 | } |
| 7332 | |
| 7333 | attr.type = PERF_TYPE_TRACEPOINT; |
| 7334 | attr.size = sizeof(attr); |
| 7335 | attr.config = tp_id; |
| 7336 | |
| 7337 | pfd = syscall(__NR_perf_event_open, &attr, -1 /* pid */, 0 /* cpu */, |
| 7338 | -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC); |
| 7339 | if (pfd < 0) { |
| 7340 | err = -errno; |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7341 | pr_warn("tracepoint '%s/%s' perf_event_open() failed: %s\n", |
| 7342 | tp_category, tp_name, |
| 7343 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
Andrii Nakryiko | f6de59c | 2019-07-01 16:58:59 -0700 | [diff] [blame] | 7344 | return err; |
| 7345 | } |
| 7346 | return pfd; |
| 7347 | } |
| 7348 | |
| 7349 | struct bpf_link *bpf_program__attach_tracepoint(struct bpf_program *prog, |
| 7350 | const char *tp_category, |
| 7351 | const char *tp_name) |
| 7352 | { |
| 7353 | char errmsg[STRERR_BUFSIZE]; |
| 7354 | struct bpf_link *link; |
| 7355 | int pfd, err; |
| 7356 | |
| 7357 | pfd = perf_event_open_tracepoint(tp_category, tp_name); |
| 7358 | if (pfd < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7359 | pr_warn("program '%s': failed to create tracepoint '%s/%s' perf event: %s\n", |
| 7360 | bpf_program__title(prog, false), |
| 7361 | tp_category, tp_name, |
| 7362 | libbpf_strerror_r(pfd, errmsg, sizeof(errmsg))); |
Andrii Nakryiko | f6de59c | 2019-07-01 16:58:59 -0700 | [diff] [blame] | 7363 | return ERR_PTR(pfd); |
| 7364 | } |
| 7365 | link = bpf_program__attach_perf_event(prog, pfd); |
| 7366 | if (IS_ERR(link)) { |
| 7367 | close(pfd); |
| 7368 | err = PTR_ERR(link); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7369 | pr_warn("program '%s': failed to attach to tracepoint '%s/%s': %s\n", |
| 7370 | bpf_program__title(prog, false), |
| 7371 | tp_category, tp_name, |
| 7372 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
Andrii Nakryiko | f6de59c | 2019-07-01 16:58:59 -0700 | [diff] [blame] | 7373 | return link; |
| 7374 | } |
| 7375 | return link; |
| 7376 | } |
| 7377 | |
Andrii Nakryiko | d7a18ea | 2019-12-13 17:43:26 -0800 | [diff] [blame] | 7378 | static struct bpf_link *attach_tp(const struct bpf_sec_def *sec, |
| 7379 | struct bpf_program *prog) |
| 7380 | { |
| 7381 | char *sec_name, *tp_cat, *tp_name; |
| 7382 | struct bpf_link *link; |
| 7383 | |
| 7384 | sec_name = strdup(bpf_program__title(prog, false)); |
| 7385 | if (!sec_name) |
| 7386 | return ERR_PTR(-ENOMEM); |
| 7387 | |
| 7388 | /* extract "tp/<category>/<name>" */ |
| 7389 | tp_cat = sec_name + sec->len; |
| 7390 | tp_name = strchr(tp_cat, '/'); |
| 7391 | if (!tp_name) { |
| 7392 | link = ERR_PTR(-EINVAL); |
| 7393 | goto out; |
| 7394 | } |
| 7395 | *tp_name = '\0'; |
| 7396 | tp_name++; |
| 7397 | |
| 7398 | link = bpf_program__attach_tracepoint(prog, tp_cat, tp_name); |
| 7399 | out: |
| 7400 | free(sec_name); |
| 7401 | return link; |
| 7402 | } |
| 7403 | |
Andrii Nakryiko | 84bf5e1 | 2019-07-01 16:59:00 -0700 | [diff] [blame] | 7404 | struct bpf_link *bpf_program__attach_raw_tracepoint(struct bpf_program *prog, |
| 7405 | const char *tp_name) |
| 7406 | { |
| 7407 | char errmsg[STRERR_BUFSIZE]; |
Andrii Nakryiko | c016b68 | 2020-03-02 20:31:58 -0800 | [diff] [blame] | 7408 | struct bpf_link *link; |
Andrii Nakryiko | 84bf5e1 | 2019-07-01 16:59:00 -0700 | [diff] [blame] | 7409 | int prog_fd, pfd; |
| 7410 | |
| 7411 | prog_fd = bpf_program__fd(prog); |
| 7412 | if (prog_fd < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7413 | pr_warn("program '%s': can't attach before loaded\n", |
| 7414 | bpf_program__title(prog, false)); |
Andrii Nakryiko | 84bf5e1 | 2019-07-01 16:59:00 -0700 | [diff] [blame] | 7415 | return ERR_PTR(-EINVAL); |
| 7416 | } |
| 7417 | |
Andrii Nakryiko | d695870 | 2019-12-18 14:50:39 -0800 | [diff] [blame] | 7418 | link = calloc(1, sizeof(*link)); |
Andrii Nakryiko | 84bf5e1 | 2019-07-01 16:59:00 -0700 | [diff] [blame] | 7419 | if (!link) |
| 7420 | return ERR_PTR(-ENOMEM); |
Andrii Nakryiko | c016b68 | 2020-03-02 20:31:58 -0800 | [diff] [blame] | 7421 | link->detach = &bpf_link__detach_fd; |
Andrii Nakryiko | 84bf5e1 | 2019-07-01 16:59:00 -0700 | [diff] [blame] | 7422 | |
| 7423 | pfd = bpf_raw_tracepoint_open(tp_name, prog_fd); |
| 7424 | if (pfd < 0) { |
| 7425 | pfd = -errno; |
| 7426 | free(link); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7427 | pr_warn("program '%s': failed to attach to raw tracepoint '%s': %s\n", |
| 7428 | bpf_program__title(prog, false), tp_name, |
| 7429 | libbpf_strerror_r(pfd, errmsg, sizeof(errmsg))); |
Andrii Nakryiko | 84bf5e1 | 2019-07-01 16:59:00 -0700 | [diff] [blame] | 7430 | return ERR_PTR(pfd); |
| 7431 | } |
| 7432 | link->fd = pfd; |
Andrii Nakryiko | c016b68 | 2020-03-02 20:31:58 -0800 | [diff] [blame] | 7433 | return link; |
Andrii Nakryiko | 84bf5e1 | 2019-07-01 16:59:00 -0700 | [diff] [blame] | 7434 | } |
| 7435 | |
Andrii Nakryiko | d7a18ea | 2019-12-13 17:43:26 -0800 | [diff] [blame] | 7436 | static struct bpf_link *attach_raw_tp(const struct bpf_sec_def *sec, |
| 7437 | struct bpf_program *prog) |
| 7438 | { |
| 7439 | const char *tp_name = bpf_program__title(prog, false) + sec->len; |
| 7440 | |
| 7441 | return bpf_program__attach_raw_tracepoint(prog, tp_name); |
| 7442 | } |
| 7443 | |
Alexei Starovoitov | b8c54ea | 2019-11-14 10:57:06 -0800 | [diff] [blame] | 7444 | struct bpf_link *bpf_program__attach_trace(struct bpf_program *prog) |
| 7445 | { |
| 7446 | char errmsg[STRERR_BUFSIZE]; |
Andrii Nakryiko | c016b68 | 2020-03-02 20:31:58 -0800 | [diff] [blame] | 7447 | struct bpf_link *link; |
Alexei Starovoitov | b8c54ea | 2019-11-14 10:57:06 -0800 | [diff] [blame] | 7448 | int prog_fd, pfd; |
| 7449 | |
| 7450 | prog_fd = bpf_program__fd(prog); |
| 7451 | if (prog_fd < 0) { |
| 7452 | pr_warn("program '%s': can't attach before loaded\n", |
| 7453 | bpf_program__title(prog, false)); |
| 7454 | return ERR_PTR(-EINVAL); |
| 7455 | } |
| 7456 | |
Andrii Nakryiko | d695870 | 2019-12-18 14:50:39 -0800 | [diff] [blame] | 7457 | link = calloc(1, sizeof(*link)); |
Alexei Starovoitov | b8c54ea | 2019-11-14 10:57:06 -0800 | [diff] [blame] | 7458 | if (!link) |
| 7459 | return ERR_PTR(-ENOMEM); |
Andrii Nakryiko | c016b68 | 2020-03-02 20:31:58 -0800 | [diff] [blame] | 7460 | link->detach = &bpf_link__detach_fd; |
Alexei Starovoitov | b8c54ea | 2019-11-14 10:57:06 -0800 | [diff] [blame] | 7461 | |
| 7462 | pfd = bpf_raw_tracepoint_open(NULL, prog_fd); |
| 7463 | if (pfd < 0) { |
| 7464 | pfd = -errno; |
| 7465 | free(link); |
| 7466 | pr_warn("program '%s': failed to attach to trace: %s\n", |
| 7467 | bpf_program__title(prog, false), |
| 7468 | libbpf_strerror_r(pfd, errmsg, sizeof(errmsg))); |
| 7469 | return ERR_PTR(pfd); |
| 7470 | } |
| 7471 | link->fd = pfd; |
| 7472 | return (struct bpf_link *)link; |
| 7473 | } |
| 7474 | |
Andrii Nakryiko | d7a18ea | 2019-12-13 17:43:26 -0800 | [diff] [blame] | 7475 | static struct bpf_link *attach_trace(const struct bpf_sec_def *sec, |
| 7476 | struct bpf_program *prog) |
| 7477 | { |
| 7478 | return bpf_program__attach_trace(prog); |
| 7479 | } |
| 7480 | |
| 7481 | struct bpf_link *bpf_program__attach(struct bpf_program *prog) |
| 7482 | { |
| 7483 | const struct bpf_sec_def *sec_def; |
| 7484 | |
| 7485 | sec_def = find_sec_def(bpf_program__title(prog, false)); |
| 7486 | if (!sec_def || !sec_def->attach_fn) |
| 7487 | return ERR_PTR(-ESRCH); |
| 7488 | |
| 7489 | return sec_def->attach_fn(sec_def, prog); |
| 7490 | } |
| 7491 | |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 7492 | static int bpf_link__detach_struct_ops(struct bpf_link *link) |
| 7493 | { |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 7494 | __u32 zero = 0; |
| 7495 | |
Andrii Nakryiko | c016b68 | 2020-03-02 20:31:58 -0800 | [diff] [blame] | 7496 | if (bpf_map_delete_elem(link->fd, &zero)) |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 7497 | return -errno; |
| 7498 | |
| 7499 | return 0; |
| 7500 | } |
| 7501 | |
| 7502 | struct bpf_link *bpf_map__attach_struct_ops(struct bpf_map *map) |
| 7503 | { |
| 7504 | struct bpf_struct_ops *st_ops; |
Andrii Nakryiko | c016b68 | 2020-03-02 20:31:58 -0800 | [diff] [blame] | 7505 | struct bpf_link *link; |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 7506 | __u32 i, zero = 0; |
| 7507 | int err; |
| 7508 | |
| 7509 | if (!bpf_map__is_struct_ops(map) || map->fd == -1) |
| 7510 | return ERR_PTR(-EINVAL); |
| 7511 | |
| 7512 | link = calloc(1, sizeof(*link)); |
| 7513 | if (!link) |
| 7514 | return ERR_PTR(-EINVAL); |
| 7515 | |
| 7516 | st_ops = map->st_ops; |
| 7517 | for (i = 0; i < btf_vlen(st_ops->type); i++) { |
| 7518 | struct bpf_program *prog = st_ops->progs[i]; |
| 7519 | void *kern_data; |
| 7520 | int prog_fd; |
| 7521 | |
| 7522 | if (!prog) |
| 7523 | continue; |
| 7524 | |
| 7525 | prog_fd = bpf_program__fd(prog); |
| 7526 | kern_data = st_ops->kern_vdata + st_ops->kern_func_off[i]; |
| 7527 | *(unsigned long *)kern_data = prog_fd; |
| 7528 | } |
| 7529 | |
| 7530 | err = bpf_map_update_elem(map->fd, &zero, st_ops->kern_vdata, 0); |
| 7531 | if (err) { |
| 7532 | err = -errno; |
| 7533 | free(link); |
| 7534 | return ERR_PTR(err); |
| 7535 | } |
| 7536 | |
Andrii Nakryiko | c016b68 | 2020-03-02 20:31:58 -0800 | [diff] [blame] | 7537 | link->detach = bpf_link__detach_struct_ops; |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 7538 | link->fd = map->fd; |
| 7539 | |
Andrii Nakryiko | c016b68 | 2020-03-02 20:31:58 -0800 | [diff] [blame] | 7540 | return link; |
Martin KaFai Lau | 590a008 | 2020-01-08 16:35:14 -0800 | [diff] [blame] | 7541 | } |
| 7542 | |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 7543 | enum bpf_perf_event_ret |
Daniel Borkmann | 3dca211 | 2018-10-21 02:09:28 +0200 | [diff] [blame] | 7544 | bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size, |
| 7545 | void **copy_mem, size_t *copy_size, |
| 7546 | bpf_perf_event_print_t fn, void *private_data) |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 7547 | { |
Daniel Borkmann | 3dca211 | 2018-10-21 02:09:28 +0200 | [diff] [blame] | 7548 | struct perf_event_mmap_page *header = mmap_mem; |
Daniel Borkmann | a64af0e | 2018-10-19 15:51:03 +0200 | [diff] [blame] | 7549 | __u64 data_head = ring_buffer_read_head(header); |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 7550 | __u64 data_tail = header->data_tail; |
Daniel Borkmann | 3dca211 | 2018-10-21 02:09:28 +0200 | [diff] [blame] | 7551 | void *base = ((__u8 *)header) + page_size; |
| 7552 | int ret = LIBBPF_PERF_EVENT_CONT; |
| 7553 | struct perf_event_header *ehdr; |
| 7554 | size_t ehdr_size; |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 7555 | |
Daniel Borkmann | 3dca211 | 2018-10-21 02:09:28 +0200 | [diff] [blame] | 7556 | while (data_head != data_tail) { |
| 7557 | ehdr = base + (data_tail & (mmap_size - 1)); |
| 7558 | ehdr_size = ehdr->size; |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 7559 | |
Daniel Borkmann | 3dca211 | 2018-10-21 02:09:28 +0200 | [diff] [blame] | 7560 | if (((void *)ehdr) + ehdr_size > base + mmap_size) { |
| 7561 | void *copy_start = ehdr; |
| 7562 | size_t len_first = base + mmap_size - copy_start; |
| 7563 | size_t len_secnd = ehdr_size - len_first; |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 7564 | |
Daniel Borkmann | 3dca211 | 2018-10-21 02:09:28 +0200 | [diff] [blame] | 7565 | if (*copy_size < ehdr_size) { |
| 7566 | free(*copy_mem); |
| 7567 | *copy_mem = malloc(ehdr_size); |
| 7568 | if (!*copy_mem) { |
| 7569 | *copy_size = 0; |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 7570 | ret = LIBBPF_PERF_EVENT_ERROR; |
| 7571 | break; |
| 7572 | } |
Daniel Borkmann | 3dca211 | 2018-10-21 02:09:28 +0200 | [diff] [blame] | 7573 | *copy_size = ehdr_size; |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 7574 | } |
| 7575 | |
Daniel Borkmann | 3dca211 | 2018-10-21 02:09:28 +0200 | [diff] [blame] | 7576 | memcpy(*copy_mem, copy_start, len_first); |
| 7577 | memcpy(*copy_mem + len_first, base, len_secnd); |
| 7578 | ehdr = *copy_mem; |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 7579 | } |
| 7580 | |
Daniel Borkmann | 3dca211 | 2018-10-21 02:09:28 +0200 | [diff] [blame] | 7581 | ret = fn(ehdr, private_data); |
| 7582 | data_tail += ehdr_size; |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 7583 | if (ret != LIBBPF_PERF_EVENT_CONT) |
| 7584 | break; |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 7585 | } |
| 7586 | |
Daniel Borkmann | a64af0e | 2018-10-19 15:51:03 +0200 | [diff] [blame] | 7587 | ring_buffer_write_tail(header, data_tail); |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 7588 | return ret; |
| 7589 | } |
Song Liu | 34be1646 | 2019-03-11 22:30:38 -0700 | [diff] [blame] | 7590 | |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 7591 | struct perf_buffer; |
| 7592 | |
| 7593 | struct perf_buffer_params { |
| 7594 | struct perf_event_attr *attr; |
| 7595 | /* if event_cb is specified, it takes precendence */ |
| 7596 | perf_buffer_event_fn event_cb; |
| 7597 | /* sample_cb and lost_cb are higher-level common-case callbacks */ |
| 7598 | perf_buffer_sample_fn sample_cb; |
| 7599 | perf_buffer_lost_fn lost_cb; |
| 7600 | void *ctx; |
| 7601 | int cpu_cnt; |
| 7602 | int *cpus; |
| 7603 | int *map_keys; |
| 7604 | }; |
| 7605 | |
| 7606 | struct perf_cpu_buf { |
| 7607 | struct perf_buffer *pb; |
| 7608 | void *base; /* mmap()'ed memory */ |
| 7609 | void *buf; /* for reconstructing segmented data */ |
| 7610 | size_t buf_size; |
| 7611 | int fd; |
| 7612 | int cpu; |
| 7613 | int map_key; |
| 7614 | }; |
| 7615 | |
| 7616 | struct perf_buffer { |
| 7617 | perf_buffer_event_fn event_cb; |
| 7618 | perf_buffer_sample_fn sample_cb; |
| 7619 | perf_buffer_lost_fn lost_cb; |
| 7620 | void *ctx; /* passed into callbacks */ |
| 7621 | |
| 7622 | size_t page_size; |
| 7623 | size_t mmap_size; |
| 7624 | struct perf_cpu_buf **cpu_bufs; |
| 7625 | struct epoll_event *events; |
Andrii Nakryiko | 783b8f0 | 2019-12-11 17:36:09 -0800 | [diff] [blame] | 7626 | int cpu_cnt; /* number of allocated CPU buffers */ |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 7627 | int epoll_fd; /* perf event FD */ |
| 7628 | int map_fd; /* BPF_MAP_TYPE_PERF_EVENT_ARRAY BPF map FD */ |
| 7629 | }; |
| 7630 | |
| 7631 | static void perf_buffer__free_cpu_buf(struct perf_buffer *pb, |
| 7632 | struct perf_cpu_buf *cpu_buf) |
| 7633 | { |
| 7634 | if (!cpu_buf) |
| 7635 | return; |
| 7636 | if (cpu_buf->base && |
| 7637 | munmap(cpu_buf->base, pb->mmap_size + pb->page_size)) |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7638 | pr_warn("failed to munmap cpu_buf #%d\n", cpu_buf->cpu); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 7639 | if (cpu_buf->fd >= 0) { |
| 7640 | ioctl(cpu_buf->fd, PERF_EVENT_IOC_DISABLE, 0); |
| 7641 | close(cpu_buf->fd); |
| 7642 | } |
| 7643 | free(cpu_buf->buf); |
| 7644 | free(cpu_buf); |
| 7645 | } |
| 7646 | |
| 7647 | void perf_buffer__free(struct perf_buffer *pb) |
| 7648 | { |
| 7649 | int i; |
| 7650 | |
| 7651 | if (!pb) |
| 7652 | return; |
| 7653 | if (pb->cpu_bufs) { |
| 7654 | for (i = 0; i < pb->cpu_cnt && pb->cpu_bufs[i]; i++) { |
| 7655 | struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i]; |
| 7656 | |
| 7657 | bpf_map_delete_elem(pb->map_fd, &cpu_buf->map_key); |
| 7658 | perf_buffer__free_cpu_buf(pb, cpu_buf); |
| 7659 | } |
| 7660 | free(pb->cpu_bufs); |
| 7661 | } |
| 7662 | if (pb->epoll_fd >= 0) |
| 7663 | close(pb->epoll_fd); |
| 7664 | free(pb->events); |
| 7665 | free(pb); |
| 7666 | } |
| 7667 | |
| 7668 | static struct perf_cpu_buf * |
| 7669 | perf_buffer__open_cpu_buf(struct perf_buffer *pb, struct perf_event_attr *attr, |
| 7670 | int cpu, int map_key) |
| 7671 | { |
| 7672 | struct perf_cpu_buf *cpu_buf; |
| 7673 | char msg[STRERR_BUFSIZE]; |
| 7674 | int err; |
| 7675 | |
| 7676 | cpu_buf = calloc(1, sizeof(*cpu_buf)); |
| 7677 | if (!cpu_buf) |
| 7678 | return ERR_PTR(-ENOMEM); |
| 7679 | |
| 7680 | cpu_buf->pb = pb; |
| 7681 | cpu_buf->cpu = cpu; |
| 7682 | cpu_buf->map_key = map_key; |
| 7683 | |
| 7684 | cpu_buf->fd = syscall(__NR_perf_event_open, attr, -1 /* pid */, cpu, |
| 7685 | -1, PERF_FLAG_FD_CLOEXEC); |
| 7686 | if (cpu_buf->fd < 0) { |
| 7687 | err = -errno; |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7688 | pr_warn("failed to open perf buffer event on cpu #%d: %s\n", |
| 7689 | cpu, libbpf_strerror_r(err, msg, sizeof(msg))); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 7690 | goto error; |
| 7691 | } |
| 7692 | |
| 7693 | cpu_buf->base = mmap(NULL, pb->mmap_size + pb->page_size, |
| 7694 | PROT_READ | PROT_WRITE, MAP_SHARED, |
| 7695 | cpu_buf->fd, 0); |
| 7696 | if (cpu_buf->base == MAP_FAILED) { |
| 7697 | cpu_buf->base = NULL; |
| 7698 | err = -errno; |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7699 | pr_warn("failed to mmap perf buffer on cpu #%d: %s\n", |
| 7700 | cpu, libbpf_strerror_r(err, msg, sizeof(msg))); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 7701 | goto error; |
| 7702 | } |
| 7703 | |
| 7704 | if (ioctl(cpu_buf->fd, PERF_EVENT_IOC_ENABLE, 0) < 0) { |
| 7705 | err = -errno; |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7706 | pr_warn("failed to enable perf buffer event on cpu #%d: %s\n", |
| 7707 | cpu, libbpf_strerror_r(err, msg, sizeof(msg))); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 7708 | goto error; |
| 7709 | } |
| 7710 | |
| 7711 | return cpu_buf; |
| 7712 | |
| 7713 | error: |
| 7714 | perf_buffer__free_cpu_buf(pb, cpu_buf); |
| 7715 | return (struct perf_cpu_buf *)ERR_PTR(err); |
| 7716 | } |
| 7717 | |
| 7718 | static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt, |
| 7719 | struct perf_buffer_params *p); |
| 7720 | |
| 7721 | struct perf_buffer *perf_buffer__new(int map_fd, size_t page_cnt, |
| 7722 | const struct perf_buffer_opts *opts) |
| 7723 | { |
| 7724 | struct perf_buffer_params p = {}; |
Arnaldo Carvalho de Melo | 4be6e05 | 2019-07-19 11:34:07 -0300 | [diff] [blame] | 7725 | struct perf_event_attr attr = { 0, }; |
| 7726 | |
| 7727 | attr.config = PERF_COUNT_SW_BPF_OUTPUT, |
| 7728 | attr.type = PERF_TYPE_SOFTWARE; |
| 7729 | attr.sample_type = PERF_SAMPLE_RAW; |
| 7730 | attr.sample_period = 1; |
| 7731 | attr.wakeup_events = 1; |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 7732 | |
| 7733 | p.attr = &attr; |
| 7734 | p.sample_cb = opts ? opts->sample_cb : NULL; |
| 7735 | p.lost_cb = opts ? opts->lost_cb : NULL; |
| 7736 | p.ctx = opts ? opts->ctx : NULL; |
| 7737 | |
| 7738 | return __perf_buffer__new(map_fd, page_cnt, &p); |
| 7739 | } |
| 7740 | |
| 7741 | struct perf_buffer * |
| 7742 | perf_buffer__new_raw(int map_fd, size_t page_cnt, |
| 7743 | const struct perf_buffer_raw_opts *opts) |
| 7744 | { |
| 7745 | struct perf_buffer_params p = {}; |
| 7746 | |
| 7747 | p.attr = opts->attr; |
| 7748 | p.event_cb = opts->event_cb; |
| 7749 | p.ctx = opts->ctx; |
| 7750 | p.cpu_cnt = opts->cpu_cnt; |
| 7751 | p.cpus = opts->cpus; |
| 7752 | p.map_keys = opts->map_keys; |
| 7753 | |
| 7754 | return __perf_buffer__new(map_fd, page_cnt, &p); |
| 7755 | } |
| 7756 | |
| 7757 | static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt, |
| 7758 | struct perf_buffer_params *p) |
| 7759 | { |
Andrii Nakryiko | 783b8f0 | 2019-12-11 17:36:09 -0800 | [diff] [blame] | 7760 | const char *online_cpus_file = "/sys/devices/system/cpu/online"; |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 7761 | struct bpf_map_info map = {}; |
| 7762 | char msg[STRERR_BUFSIZE]; |
| 7763 | struct perf_buffer *pb; |
Andrii Nakryiko | 783b8f0 | 2019-12-11 17:36:09 -0800 | [diff] [blame] | 7764 | bool *online = NULL; |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 7765 | __u32 map_info_len; |
Andrii Nakryiko | 783b8f0 | 2019-12-11 17:36:09 -0800 | [diff] [blame] | 7766 | int err, i, j, n; |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 7767 | |
| 7768 | if (page_cnt & (page_cnt - 1)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7769 | pr_warn("page count should be power of two, but is %zu\n", |
| 7770 | page_cnt); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 7771 | return ERR_PTR(-EINVAL); |
| 7772 | } |
| 7773 | |
| 7774 | map_info_len = sizeof(map); |
| 7775 | err = bpf_obj_get_info_by_fd(map_fd, &map, &map_info_len); |
| 7776 | if (err) { |
| 7777 | err = -errno; |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7778 | pr_warn("failed to get map info for map FD %d: %s\n", |
| 7779 | map_fd, libbpf_strerror_r(err, msg, sizeof(msg))); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 7780 | return ERR_PTR(err); |
| 7781 | } |
| 7782 | |
| 7783 | if (map.type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7784 | pr_warn("map '%s' should be BPF_MAP_TYPE_PERF_EVENT_ARRAY\n", |
| 7785 | map.name); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 7786 | return ERR_PTR(-EINVAL); |
| 7787 | } |
| 7788 | |
| 7789 | pb = calloc(1, sizeof(*pb)); |
| 7790 | if (!pb) |
| 7791 | return ERR_PTR(-ENOMEM); |
| 7792 | |
| 7793 | pb->event_cb = p->event_cb; |
| 7794 | pb->sample_cb = p->sample_cb; |
| 7795 | pb->lost_cb = p->lost_cb; |
| 7796 | pb->ctx = p->ctx; |
| 7797 | |
| 7798 | pb->page_size = getpagesize(); |
| 7799 | pb->mmap_size = pb->page_size * page_cnt; |
| 7800 | pb->map_fd = map_fd; |
| 7801 | |
| 7802 | pb->epoll_fd = epoll_create1(EPOLL_CLOEXEC); |
| 7803 | if (pb->epoll_fd < 0) { |
| 7804 | err = -errno; |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7805 | pr_warn("failed to create epoll instance: %s\n", |
| 7806 | libbpf_strerror_r(err, msg, sizeof(msg))); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 7807 | goto error; |
| 7808 | } |
| 7809 | |
| 7810 | if (p->cpu_cnt > 0) { |
| 7811 | pb->cpu_cnt = p->cpu_cnt; |
| 7812 | } else { |
| 7813 | pb->cpu_cnt = libbpf_num_possible_cpus(); |
| 7814 | if (pb->cpu_cnt < 0) { |
| 7815 | err = pb->cpu_cnt; |
| 7816 | goto error; |
| 7817 | } |
| 7818 | if (map.max_entries < pb->cpu_cnt) |
| 7819 | pb->cpu_cnt = map.max_entries; |
| 7820 | } |
| 7821 | |
| 7822 | pb->events = calloc(pb->cpu_cnt, sizeof(*pb->events)); |
| 7823 | if (!pb->events) { |
| 7824 | err = -ENOMEM; |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7825 | pr_warn("failed to allocate events: out of memory\n"); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 7826 | goto error; |
| 7827 | } |
| 7828 | pb->cpu_bufs = calloc(pb->cpu_cnt, sizeof(*pb->cpu_bufs)); |
| 7829 | if (!pb->cpu_bufs) { |
| 7830 | err = -ENOMEM; |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7831 | pr_warn("failed to allocate buffers: out of memory\n"); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 7832 | goto error; |
| 7833 | } |
| 7834 | |
Andrii Nakryiko | 783b8f0 | 2019-12-11 17:36:09 -0800 | [diff] [blame] | 7835 | err = parse_cpu_mask_file(online_cpus_file, &online, &n); |
| 7836 | if (err) { |
| 7837 | pr_warn("failed to get online CPU mask: %d\n", err); |
| 7838 | goto error; |
| 7839 | } |
| 7840 | |
| 7841 | for (i = 0, j = 0; i < pb->cpu_cnt; i++) { |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 7842 | struct perf_cpu_buf *cpu_buf; |
| 7843 | int cpu, map_key; |
| 7844 | |
| 7845 | cpu = p->cpu_cnt > 0 ? p->cpus[i] : i; |
| 7846 | map_key = p->cpu_cnt > 0 ? p->map_keys[i] : i; |
| 7847 | |
Andrii Nakryiko | 783b8f0 | 2019-12-11 17:36:09 -0800 | [diff] [blame] | 7848 | /* in case user didn't explicitly requested particular CPUs to |
| 7849 | * be attached to, skip offline/not present CPUs |
| 7850 | */ |
| 7851 | if (p->cpu_cnt <= 0 && (cpu >= n || !online[cpu])) |
| 7852 | continue; |
| 7853 | |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 7854 | cpu_buf = perf_buffer__open_cpu_buf(pb, p->attr, cpu, map_key); |
| 7855 | if (IS_ERR(cpu_buf)) { |
| 7856 | err = PTR_ERR(cpu_buf); |
| 7857 | goto error; |
| 7858 | } |
| 7859 | |
Andrii Nakryiko | 783b8f0 | 2019-12-11 17:36:09 -0800 | [diff] [blame] | 7860 | pb->cpu_bufs[j] = cpu_buf; |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 7861 | |
| 7862 | err = bpf_map_update_elem(pb->map_fd, &map_key, |
| 7863 | &cpu_buf->fd, 0); |
| 7864 | if (err) { |
| 7865 | err = -errno; |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7866 | pr_warn("failed to set cpu #%d, key %d -> perf FD %d: %s\n", |
| 7867 | cpu, map_key, cpu_buf->fd, |
| 7868 | libbpf_strerror_r(err, msg, sizeof(msg))); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 7869 | goto error; |
| 7870 | } |
| 7871 | |
Andrii Nakryiko | 783b8f0 | 2019-12-11 17:36:09 -0800 | [diff] [blame] | 7872 | pb->events[j].events = EPOLLIN; |
| 7873 | pb->events[j].data.ptr = cpu_buf; |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 7874 | if (epoll_ctl(pb->epoll_fd, EPOLL_CTL_ADD, cpu_buf->fd, |
Andrii Nakryiko | 783b8f0 | 2019-12-11 17:36:09 -0800 | [diff] [blame] | 7875 | &pb->events[j]) < 0) { |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 7876 | err = -errno; |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7877 | pr_warn("failed to epoll_ctl cpu #%d perf FD %d: %s\n", |
| 7878 | cpu, cpu_buf->fd, |
| 7879 | libbpf_strerror_r(err, msg, sizeof(msg))); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 7880 | goto error; |
| 7881 | } |
Andrii Nakryiko | 783b8f0 | 2019-12-11 17:36:09 -0800 | [diff] [blame] | 7882 | j++; |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 7883 | } |
Andrii Nakryiko | 783b8f0 | 2019-12-11 17:36:09 -0800 | [diff] [blame] | 7884 | pb->cpu_cnt = j; |
| 7885 | free(online); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 7886 | |
| 7887 | return pb; |
| 7888 | |
| 7889 | error: |
Andrii Nakryiko | 783b8f0 | 2019-12-11 17:36:09 -0800 | [diff] [blame] | 7890 | free(online); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 7891 | if (pb) |
| 7892 | perf_buffer__free(pb); |
| 7893 | return ERR_PTR(err); |
| 7894 | } |
| 7895 | |
| 7896 | struct perf_sample_raw { |
| 7897 | struct perf_event_header header; |
| 7898 | uint32_t size; |
| 7899 | char data[0]; |
| 7900 | }; |
| 7901 | |
| 7902 | struct perf_sample_lost { |
| 7903 | struct perf_event_header header; |
| 7904 | uint64_t id; |
| 7905 | uint64_t lost; |
| 7906 | uint64_t sample_id; |
| 7907 | }; |
| 7908 | |
| 7909 | static enum bpf_perf_event_ret |
| 7910 | perf_buffer__process_record(struct perf_event_header *e, void *ctx) |
| 7911 | { |
| 7912 | struct perf_cpu_buf *cpu_buf = ctx; |
| 7913 | struct perf_buffer *pb = cpu_buf->pb; |
| 7914 | void *data = e; |
| 7915 | |
| 7916 | /* user wants full control over parsing perf event */ |
| 7917 | if (pb->event_cb) |
| 7918 | return pb->event_cb(pb->ctx, cpu_buf->cpu, e); |
| 7919 | |
| 7920 | switch (e->type) { |
| 7921 | case PERF_RECORD_SAMPLE: { |
| 7922 | struct perf_sample_raw *s = data; |
| 7923 | |
| 7924 | if (pb->sample_cb) |
| 7925 | pb->sample_cb(pb->ctx, cpu_buf->cpu, s->data, s->size); |
| 7926 | break; |
| 7927 | } |
| 7928 | case PERF_RECORD_LOST: { |
| 7929 | struct perf_sample_lost *s = data; |
| 7930 | |
| 7931 | if (pb->lost_cb) |
| 7932 | pb->lost_cb(pb->ctx, cpu_buf->cpu, s->lost); |
| 7933 | break; |
| 7934 | } |
| 7935 | default: |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7936 | pr_warn("unknown perf sample type %d\n", e->type); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 7937 | return LIBBPF_PERF_EVENT_ERROR; |
| 7938 | } |
| 7939 | return LIBBPF_PERF_EVENT_CONT; |
| 7940 | } |
| 7941 | |
| 7942 | static int perf_buffer__process_records(struct perf_buffer *pb, |
| 7943 | struct perf_cpu_buf *cpu_buf) |
| 7944 | { |
| 7945 | enum bpf_perf_event_ret ret; |
| 7946 | |
| 7947 | ret = bpf_perf_event_read_simple(cpu_buf->base, pb->mmap_size, |
| 7948 | pb->page_size, &cpu_buf->buf, |
| 7949 | &cpu_buf->buf_size, |
| 7950 | perf_buffer__process_record, cpu_buf); |
| 7951 | if (ret != LIBBPF_PERF_EVENT_CONT) |
| 7952 | return ret; |
| 7953 | return 0; |
| 7954 | } |
| 7955 | |
| 7956 | int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms) |
| 7957 | { |
| 7958 | int i, cnt, err; |
| 7959 | |
| 7960 | cnt = epoll_wait(pb->epoll_fd, pb->events, pb->cpu_cnt, timeout_ms); |
| 7961 | for (i = 0; i < cnt; i++) { |
| 7962 | struct perf_cpu_buf *cpu_buf = pb->events[i].data.ptr; |
| 7963 | |
| 7964 | err = perf_buffer__process_records(pb, cpu_buf); |
| 7965 | if (err) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 7966 | pr_warn("error while processing records: %d\n", err); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 7967 | return err; |
| 7968 | } |
| 7969 | } |
| 7970 | return cnt < 0 ? -errno : cnt; |
| 7971 | } |
| 7972 | |
Song Liu | 34be1646 | 2019-03-11 22:30:38 -0700 | [diff] [blame] | 7973 | struct bpf_prog_info_array_desc { |
| 7974 | int array_offset; /* e.g. offset of jited_prog_insns */ |
| 7975 | int count_offset; /* e.g. offset of jited_prog_len */ |
| 7976 | int size_offset; /* > 0: offset of rec size, |
| 7977 | * < 0: fix size of -size_offset |
| 7978 | */ |
| 7979 | }; |
| 7980 | |
| 7981 | static struct bpf_prog_info_array_desc bpf_prog_info_array_desc[] = { |
| 7982 | [BPF_PROG_INFO_JITED_INSNS] = { |
| 7983 | offsetof(struct bpf_prog_info, jited_prog_insns), |
| 7984 | offsetof(struct bpf_prog_info, jited_prog_len), |
| 7985 | -1, |
| 7986 | }, |
| 7987 | [BPF_PROG_INFO_XLATED_INSNS] = { |
| 7988 | offsetof(struct bpf_prog_info, xlated_prog_insns), |
| 7989 | offsetof(struct bpf_prog_info, xlated_prog_len), |
| 7990 | -1, |
| 7991 | }, |
| 7992 | [BPF_PROG_INFO_MAP_IDS] = { |
| 7993 | offsetof(struct bpf_prog_info, map_ids), |
| 7994 | offsetof(struct bpf_prog_info, nr_map_ids), |
| 7995 | -(int)sizeof(__u32), |
| 7996 | }, |
| 7997 | [BPF_PROG_INFO_JITED_KSYMS] = { |
| 7998 | offsetof(struct bpf_prog_info, jited_ksyms), |
| 7999 | offsetof(struct bpf_prog_info, nr_jited_ksyms), |
| 8000 | -(int)sizeof(__u64), |
| 8001 | }, |
| 8002 | [BPF_PROG_INFO_JITED_FUNC_LENS] = { |
| 8003 | offsetof(struct bpf_prog_info, jited_func_lens), |
| 8004 | offsetof(struct bpf_prog_info, nr_jited_func_lens), |
| 8005 | -(int)sizeof(__u32), |
| 8006 | }, |
| 8007 | [BPF_PROG_INFO_FUNC_INFO] = { |
| 8008 | offsetof(struct bpf_prog_info, func_info), |
| 8009 | offsetof(struct bpf_prog_info, nr_func_info), |
| 8010 | offsetof(struct bpf_prog_info, func_info_rec_size), |
| 8011 | }, |
| 8012 | [BPF_PROG_INFO_LINE_INFO] = { |
| 8013 | offsetof(struct bpf_prog_info, line_info), |
| 8014 | offsetof(struct bpf_prog_info, nr_line_info), |
| 8015 | offsetof(struct bpf_prog_info, line_info_rec_size), |
| 8016 | }, |
| 8017 | [BPF_PROG_INFO_JITED_LINE_INFO] = { |
| 8018 | offsetof(struct bpf_prog_info, jited_line_info), |
| 8019 | offsetof(struct bpf_prog_info, nr_jited_line_info), |
| 8020 | offsetof(struct bpf_prog_info, jited_line_info_rec_size), |
| 8021 | }, |
| 8022 | [BPF_PROG_INFO_PROG_TAGS] = { |
| 8023 | offsetof(struct bpf_prog_info, prog_tags), |
| 8024 | offsetof(struct bpf_prog_info, nr_prog_tags), |
| 8025 | -(int)sizeof(__u8) * BPF_TAG_SIZE, |
| 8026 | }, |
| 8027 | |
| 8028 | }; |
| 8029 | |
Andrii Nakryiko | 8983b73 | 2019-11-20 23:07:42 -0800 | [diff] [blame] | 8030 | static __u32 bpf_prog_info_read_offset_u32(struct bpf_prog_info *info, |
| 8031 | int offset) |
Song Liu | 34be1646 | 2019-03-11 22:30:38 -0700 | [diff] [blame] | 8032 | { |
| 8033 | __u32 *array = (__u32 *)info; |
| 8034 | |
| 8035 | if (offset >= 0) |
| 8036 | return array[offset / sizeof(__u32)]; |
| 8037 | return -(int)offset; |
| 8038 | } |
| 8039 | |
Andrii Nakryiko | 8983b73 | 2019-11-20 23:07:42 -0800 | [diff] [blame] | 8040 | static __u64 bpf_prog_info_read_offset_u64(struct bpf_prog_info *info, |
| 8041 | int offset) |
Song Liu | 34be1646 | 2019-03-11 22:30:38 -0700 | [diff] [blame] | 8042 | { |
| 8043 | __u64 *array = (__u64 *)info; |
| 8044 | |
| 8045 | if (offset >= 0) |
| 8046 | return array[offset / sizeof(__u64)]; |
| 8047 | return -(int)offset; |
| 8048 | } |
| 8049 | |
| 8050 | static void bpf_prog_info_set_offset_u32(struct bpf_prog_info *info, int offset, |
| 8051 | __u32 val) |
| 8052 | { |
| 8053 | __u32 *array = (__u32 *)info; |
| 8054 | |
| 8055 | if (offset >= 0) |
| 8056 | array[offset / sizeof(__u32)] = val; |
| 8057 | } |
| 8058 | |
| 8059 | static void bpf_prog_info_set_offset_u64(struct bpf_prog_info *info, int offset, |
| 8060 | __u64 val) |
| 8061 | { |
| 8062 | __u64 *array = (__u64 *)info; |
| 8063 | |
| 8064 | if (offset >= 0) |
| 8065 | array[offset / sizeof(__u64)] = val; |
| 8066 | } |
| 8067 | |
| 8068 | struct bpf_prog_info_linear * |
| 8069 | bpf_program__get_prog_info_linear(int fd, __u64 arrays) |
| 8070 | { |
| 8071 | struct bpf_prog_info_linear *info_linear; |
| 8072 | struct bpf_prog_info info = {}; |
| 8073 | __u32 info_len = sizeof(info); |
| 8074 | __u32 data_len = 0; |
| 8075 | int i, err; |
| 8076 | void *ptr; |
| 8077 | |
| 8078 | if (arrays >> BPF_PROG_INFO_LAST_ARRAY) |
| 8079 | return ERR_PTR(-EINVAL); |
| 8080 | |
| 8081 | /* step 1: get array dimensions */ |
| 8082 | err = bpf_obj_get_info_by_fd(fd, &info, &info_len); |
| 8083 | if (err) { |
| 8084 | pr_debug("can't get prog info: %s", strerror(errno)); |
| 8085 | return ERR_PTR(-EFAULT); |
| 8086 | } |
| 8087 | |
| 8088 | /* step 2: calculate total size of all arrays */ |
| 8089 | for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) { |
| 8090 | bool include_array = (arrays & (1UL << i)) > 0; |
| 8091 | struct bpf_prog_info_array_desc *desc; |
| 8092 | __u32 count, size; |
| 8093 | |
| 8094 | desc = bpf_prog_info_array_desc + i; |
| 8095 | |
| 8096 | /* kernel is too old to support this field */ |
| 8097 | if (info_len < desc->array_offset + sizeof(__u32) || |
| 8098 | info_len < desc->count_offset + sizeof(__u32) || |
| 8099 | (desc->size_offset > 0 && info_len < desc->size_offset)) |
| 8100 | include_array = false; |
| 8101 | |
| 8102 | if (!include_array) { |
| 8103 | arrays &= ~(1UL << i); /* clear the bit */ |
| 8104 | continue; |
| 8105 | } |
| 8106 | |
| 8107 | count = bpf_prog_info_read_offset_u32(&info, desc->count_offset); |
| 8108 | size = bpf_prog_info_read_offset_u32(&info, desc->size_offset); |
| 8109 | |
| 8110 | data_len += count * size; |
| 8111 | } |
| 8112 | |
| 8113 | /* step 3: allocate continuous memory */ |
| 8114 | data_len = roundup(data_len, sizeof(__u64)); |
| 8115 | info_linear = malloc(sizeof(struct bpf_prog_info_linear) + data_len); |
| 8116 | if (!info_linear) |
| 8117 | return ERR_PTR(-ENOMEM); |
| 8118 | |
| 8119 | /* step 4: fill data to info_linear->info */ |
| 8120 | info_linear->arrays = arrays; |
| 8121 | memset(&info_linear->info, 0, sizeof(info)); |
| 8122 | ptr = info_linear->data; |
| 8123 | |
| 8124 | for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) { |
| 8125 | struct bpf_prog_info_array_desc *desc; |
| 8126 | __u32 count, size; |
| 8127 | |
| 8128 | if ((arrays & (1UL << i)) == 0) |
| 8129 | continue; |
| 8130 | |
| 8131 | desc = bpf_prog_info_array_desc + i; |
| 8132 | count = bpf_prog_info_read_offset_u32(&info, desc->count_offset); |
| 8133 | size = bpf_prog_info_read_offset_u32(&info, desc->size_offset); |
| 8134 | bpf_prog_info_set_offset_u32(&info_linear->info, |
| 8135 | desc->count_offset, count); |
| 8136 | bpf_prog_info_set_offset_u32(&info_linear->info, |
| 8137 | desc->size_offset, size); |
| 8138 | bpf_prog_info_set_offset_u64(&info_linear->info, |
| 8139 | desc->array_offset, |
| 8140 | ptr_to_u64(ptr)); |
| 8141 | ptr += count * size; |
| 8142 | } |
| 8143 | |
| 8144 | /* step 5: call syscall again to get required arrays */ |
| 8145 | err = bpf_obj_get_info_by_fd(fd, &info_linear->info, &info_len); |
| 8146 | if (err) { |
| 8147 | pr_debug("can't get prog info: %s", strerror(errno)); |
| 8148 | free(info_linear); |
| 8149 | return ERR_PTR(-EFAULT); |
| 8150 | } |
| 8151 | |
| 8152 | /* step 6: verify the data */ |
| 8153 | for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) { |
| 8154 | struct bpf_prog_info_array_desc *desc; |
| 8155 | __u32 v1, v2; |
| 8156 | |
| 8157 | if ((arrays & (1UL << i)) == 0) |
| 8158 | continue; |
| 8159 | |
| 8160 | desc = bpf_prog_info_array_desc + i; |
| 8161 | v1 = bpf_prog_info_read_offset_u32(&info, desc->count_offset); |
| 8162 | v2 = bpf_prog_info_read_offset_u32(&info_linear->info, |
| 8163 | desc->count_offset); |
| 8164 | if (v1 != v2) |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 8165 | pr_warn("%s: mismatch in element count\n", __func__); |
Song Liu | 34be1646 | 2019-03-11 22:30:38 -0700 | [diff] [blame] | 8166 | |
| 8167 | v1 = bpf_prog_info_read_offset_u32(&info, desc->size_offset); |
| 8168 | v2 = bpf_prog_info_read_offset_u32(&info_linear->info, |
| 8169 | desc->size_offset); |
| 8170 | if (v1 != v2) |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 8171 | pr_warn("%s: mismatch in rec size\n", __func__); |
Song Liu | 34be1646 | 2019-03-11 22:30:38 -0700 | [diff] [blame] | 8172 | } |
| 8173 | |
| 8174 | /* step 7: update info_len and data_len */ |
| 8175 | info_linear->info_len = sizeof(struct bpf_prog_info); |
| 8176 | info_linear->data_len = data_len; |
| 8177 | |
| 8178 | return info_linear; |
| 8179 | } |
| 8180 | |
| 8181 | void bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear) |
| 8182 | { |
| 8183 | int i; |
| 8184 | |
| 8185 | for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) { |
| 8186 | struct bpf_prog_info_array_desc *desc; |
| 8187 | __u64 addr, offs; |
| 8188 | |
| 8189 | if ((info_linear->arrays & (1UL << i)) == 0) |
| 8190 | continue; |
| 8191 | |
| 8192 | desc = bpf_prog_info_array_desc + i; |
| 8193 | addr = bpf_prog_info_read_offset_u64(&info_linear->info, |
| 8194 | desc->array_offset); |
| 8195 | offs = addr - ptr_to_u64(info_linear->data); |
| 8196 | bpf_prog_info_set_offset_u64(&info_linear->info, |
| 8197 | desc->array_offset, offs); |
| 8198 | } |
| 8199 | } |
| 8200 | |
| 8201 | void bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear) |
| 8202 | { |
| 8203 | int i; |
| 8204 | |
| 8205 | for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) { |
| 8206 | struct bpf_prog_info_array_desc *desc; |
| 8207 | __u64 addr, offs; |
| 8208 | |
| 8209 | if ((info_linear->arrays & (1UL << i)) == 0) |
| 8210 | continue; |
| 8211 | |
| 8212 | desc = bpf_prog_info_array_desc + i; |
| 8213 | offs = bpf_prog_info_read_offset_u64(&info_linear->info, |
| 8214 | desc->array_offset); |
| 8215 | addr = offs + ptr_to_u64(info_linear->data); |
| 8216 | bpf_prog_info_set_offset_u64(&info_linear->info, |
| 8217 | desc->array_offset, addr); |
| 8218 | } |
| 8219 | } |
Hechao Li | 6446b31 | 2019-06-10 17:56:50 -0700 | [diff] [blame] | 8220 | |
Eelco Chaudron | ff26ce5 | 2020-02-20 13:26:35 +0000 | [diff] [blame] | 8221 | int bpf_program__set_attach_target(struct bpf_program *prog, |
| 8222 | int attach_prog_fd, |
| 8223 | const char *attach_func_name) |
| 8224 | { |
| 8225 | int btf_id; |
| 8226 | |
| 8227 | if (!prog || attach_prog_fd < 0 || !attach_func_name) |
| 8228 | return -EINVAL; |
| 8229 | |
| 8230 | if (attach_prog_fd) |
| 8231 | btf_id = libbpf_find_prog_btf_id(attach_func_name, |
| 8232 | attach_prog_fd); |
| 8233 | else |
| 8234 | btf_id = __find_vmlinux_btf_id(prog->obj->btf_vmlinux, |
| 8235 | attach_func_name, |
| 8236 | prog->expected_attach_type); |
| 8237 | |
| 8238 | if (btf_id < 0) |
| 8239 | return btf_id; |
| 8240 | |
| 8241 | prog->attach_btf_id = btf_id; |
| 8242 | prog->attach_prog_fd = attach_prog_fd; |
| 8243 | return 0; |
| 8244 | } |
| 8245 | |
Andrii Nakryiko | 6803ee2 | 2019-12-11 17:35:48 -0800 | [diff] [blame] | 8246 | int parse_cpu_mask_str(const char *s, bool **mask, int *mask_sz) |
| 8247 | { |
| 8248 | int err = 0, n, len, start, end = -1; |
| 8249 | bool *tmp; |
| 8250 | |
| 8251 | *mask = NULL; |
| 8252 | *mask_sz = 0; |
| 8253 | |
| 8254 | /* Each sub string separated by ',' has format \d+-\d+ or \d+ */ |
| 8255 | while (*s) { |
| 8256 | if (*s == ',' || *s == '\n') { |
| 8257 | s++; |
| 8258 | continue; |
| 8259 | } |
| 8260 | n = sscanf(s, "%d%n-%d%n", &start, &len, &end, &len); |
| 8261 | if (n <= 0 || n > 2) { |
| 8262 | pr_warn("Failed to get CPU range %s: %d\n", s, n); |
| 8263 | err = -EINVAL; |
| 8264 | goto cleanup; |
| 8265 | } else if (n == 1) { |
| 8266 | end = start; |
| 8267 | } |
| 8268 | if (start < 0 || start > end) { |
| 8269 | pr_warn("Invalid CPU range [%d,%d] in %s\n", |
| 8270 | start, end, s); |
| 8271 | err = -EINVAL; |
| 8272 | goto cleanup; |
| 8273 | } |
| 8274 | tmp = realloc(*mask, end + 1); |
| 8275 | if (!tmp) { |
| 8276 | err = -ENOMEM; |
| 8277 | goto cleanup; |
| 8278 | } |
| 8279 | *mask = tmp; |
| 8280 | memset(tmp + *mask_sz, 0, start - *mask_sz); |
| 8281 | memset(tmp + start, 1, end - start + 1); |
| 8282 | *mask_sz = end + 1; |
| 8283 | s += len; |
| 8284 | } |
| 8285 | if (!*mask_sz) { |
| 8286 | pr_warn("Empty CPU range\n"); |
| 8287 | return -EINVAL; |
| 8288 | } |
| 8289 | return 0; |
| 8290 | cleanup: |
| 8291 | free(*mask); |
| 8292 | *mask = NULL; |
| 8293 | return err; |
| 8294 | } |
| 8295 | |
| 8296 | int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz) |
| 8297 | { |
| 8298 | int fd, err = 0, len; |
| 8299 | char buf[128]; |
| 8300 | |
| 8301 | fd = open(fcpu, O_RDONLY); |
| 8302 | if (fd < 0) { |
| 8303 | err = -errno; |
| 8304 | pr_warn("Failed to open cpu mask file %s: %d\n", fcpu, err); |
| 8305 | return err; |
| 8306 | } |
| 8307 | len = read(fd, buf, sizeof(buf)); |
| 8308 | close(fd); |
| 8309 | if (len <= 0) { |
| 8310 | err = len ? -errno : -EINVAL; |
| 8311 | pr_warn("Failed to read cpu mask from %s: %d\n", fcpu, err); |
| 8312 | return err; |
| 8313 | } |
| 8314 | if (len >= sizeof(buf)) { |
| 8315 | pr_warn("CPU mask is too big in file %s\n", fcpu); |
| 8316 | return -E2BIG; |
| 8317 | } |
| 8318 | buf[len] = '\0'; |
| 8319 | |
| 8320 | return parse_cpu_mask_str(buf, mask, mask_sz); |
| 8321 | } |
| 8322 | |
Hechao Li | 6446b31 | 2019-06-10 17:56:50 -0700 | [diff] [blame] | 8323 | int libbpf_num_possible_cpus(void) |
| 8324 | { |
| 8325 | static const char *fcpu = "/sys/devices/system/cpu/possible"; |
Hechao Li | 6446b31 | 2019-06-10 17:56:50 -0700 | [diff] [blame] | 8326 | static int cpus; |
Andrii Nakryiko | 6803ee2 | 2019-12-11 17:35:48 -0800 | [diff] [blame] | 8327 | int err, n, i, tmp_cpus; |
| 8328 | bool *mask; |
Hechao Li | 6446b31 | 2019-06-10 17:56:50 -0700 | [diff] [blame] | 8329 | |
Takshak Chahande | 56fbc24 | 2019-07-31 15:10:55 -0700 | [diff] [blame] | 8330 | tmp_cpus = READ_ONCE(cpus); |
| 8331 | if (tmp_cpus > 0) |
| 8332 | return tmp_cpus; |
Hechao Li | 6446b31 | 2019-06-10 17:56:50 -0700 | [diff] [blame] | 8333 | |
Andrii Nakryiko | 6803ee2 | 2019-12-11 17:35:48 -0800 | [diff] [blame] | 8334 | err = parse_cpu_mask_file(fcpu, &mask, &n); |
| 8335 | if (err) |
| 8336 | return err; |
Hechao Li | 6446b31 | 2019-06-10 17:56:50 -0700 | [diff] [blame] | 8337 | |
Andrii Nakryiko | 6803ee2 | 2019-12-11 17:35:48 -0800 | [diff] [blame] | 8338 | tmp_cpus = 0; |
| 8339 | for (i = 0; i < n; i++) { |
| 8340 | if (mask[i]) |
| 8341 | tmp_cpus++; |
Hechao Li | 6446b31 | 2019-06-10 17:56:50 -0700 | [diff] [blame] | 8342 | } |
Andrii Nakryiko | 6803ee2 | 2019-12-11 17:35:48 -0800 | [diff] [blame] | 8343 | free(mask); |
Takshak Chahande | 56fbc24 | 2019-07-31 15:10:55 -0700 | [diff] [blame] | 8344 | |
| 8345 | WRITE_ONCE(cpus, tmp_cpus); |
| 8346 | return tmp_cpus; |
Hechao Li | 6446b31 | 2019-06-10 17:56:50 -0700 | [diff] [blame] | 8347 | } |
Andrii Nakryiko | d66562f | 2019-12-13 17:43:36 -0800 | [diff] [blame] | 8348 | |
| 8349 | int bpf_object__open_skeleton(struct bpf_object_skeleton *s, |
| 8350 | const struct bpf_object_open_opts *opts) |
| 8351 | { |
| 8352 | DECLARE_LIBBPF_OPTS(bpf_object_open_opts, skel_opts, |
| 8353 | .object_name = s->name, |
| 8354 | ); |
| 8355 | struct bpf_object *obj; |
| 8356 | int i; |
| 8357 | |
| 8358 | /* Attempt to preserve opts->object_name, unless overriden by user |
| 8359 | * explicitly. Overwriting object name for skeletons is discouraged, |
| 8360 | * as it breaks global data maps, because they contain object name |
| 8361 | * prefix as their own map name prefix. When skeleton is generated, |
| 8362 | * bpftool is making an assumption that this name will stay the same. |
| 8363 | */ |
| 8364 | if (opts) { |
| 8365 | memcpy(&skel_opts, opts, sizeof(*opts)); |
| 8366 | if (!opts->object_name) |
| 8367 | skel_opts.object_name = s->name; |
| 8368 | } |
| 8369 | |
| 8370 | obj = bpf_object__open_mem(s->data, s->data_sz, &skel_opts); |
| 8371 | if (IS_ERR(obj)) { |
| 8372 | pr_warn("failed to initialize skeleton BPF object '%s': %ld\n", |
| 8373 | s->name, PTR_ERR(obj)); |
| 8374 | return PTR_ERR(obj); |
| 8375 | } |
| 8376 | |
| 8377 | *s->obj = obj; |
| 8378 | |
| 8379 | for (i = 0; i < s->map_cnt; i++) { |
| 8380 | struct bpf_map **map = s->maps[i].map; |
| 8381 | const char *name = s->maps[i].name; |
| 8382 | void **mmaped = s->maps[i].mmaped; |
| 8383 | |
| 8384 | *map = bpf_object__find_map_by_name(obj, name); |
| 8385 | if (!*map) { |
| 8386 | pr_warn("failed to find skeleton map '%s'\n", name); |
| 8387 | return -ESRCH; |
| 8388 | } |
| 8389 | |
Andrii Nakryiko | 2ad97d4 | 2019-12-13 17:47:09 -0800 | [diff] [blame] | 8390 | /* externs shouldn't be pre-setup from user code */ |
Andrii Nakryiko | 81bfdd0 | 2019-12-18 16:28:34 -0800 | [diff] [blame] | 8391 | if (mmaped && (*map)->libbpf_type != LIBBPF_MAP_KCONFIG) |
Andrii Nakryiko | d66562f | 2019-12-13 17:43:36 -0800 | [diff] [blame] | 8392 | *mmaped = (*map)->mmaped; |
| 8393 | } |
| 8394 | |
| 8395 | for (i = 0; i < s->prog_cnt; i++) { |
| 8396 | struct bpf_program **prog = s->progs[i].prog; |
| 8397 | const char *name = s->progs[i].name; |
| 8398 | |
| 8399 | *prog = bpf_object__find_program_by_name(obj, name); |
| 8400 | if (!*prog) { |
| 8401 | pr_warn("failed to find skeleton program '%s'\n", name); |
| 8402 | return -ESRCH; |
| 8403 | } |
| 8404 | } |
| 8405 | |
| 8406 | return 0; |
| 8407 | } |
| 8408 | |
| 8409 | int bpf_object__load_skeleton(struct bpf_object_skeleton *s) |
| 8410 | { |
| 8411 | int i, err; |
| 8412 | |
| 8413 | err = bpf_object__load(*s->obj); |
| 8414 | if (err) { |
| 8415 | pr_warn("failed to load BPF skeleton '%s': %d\n", s->name, err); |
| 8416 | return err; |
| 8417 | } |
| 8418 | |
| 8419 | for (i = 0; i < s->map_cnt; i++) { |
| 8420 | struct bpf_map *map = *s->maps[i].map; |
| 8421 | size_t mmap_sz = bpf_map_mmap_sz(map); |
| 8422 | int prot, map_fd = bpf_map__fd(map); |
| 8423 | void **mmaped = s->maps[i].mmaped; |
Andrii Nakryiko | d66562f | 2019-12-13 17:43:36 -0800 | [diff] [blame] | 8424 | |
| 8425 | if (!mmaped) |
| 8426 | continue; |
| 8427 | |
| 8428 | if (!(map->def.map_flags & BPF_F_MMAPABLE)) { |
| 8429 | *mmaped = NULL; |
| 8430 | continue; |
| 8431 | } |
| 8432 | |
| 8433 | if (map->def.map_flags & BPF_F_RDONLY_PROG) |
| 8434 | prot = PROT_READ; |
| 8435 | else |
| 8436 | prot = PROT_READ | PROT_WRITE; |
| 8437 | |
| 8438 | /* Remap anonymous mmap()-ed "map initialization image" as |
| 8439 | * a BPF map-backed mmap()-ed memory, but preserving the same |
| 8440 | * memory address. This will cause kernel to change process' |
| 8441 | * page table to point to a different piece of kernel memory, |
| 8442 | * but from userspace point of view memory address (and its |
| 8443 | * contents, being identical at this point) will stay the |
| 8444 | * same. This mapping will be released by bpf_object__close() |
| 8445 | * as per normal clean up procedure, so we don't need to worry |
| 8446 | * about it from skeleton's clean up perspective. |
| 8447 | */ |
Andrii Nakryiko | 2ad97d4 | 2019-12-13 17:47:09 -0800 | [diff] [blame] | 8448 | *mmaped = mmap(map->mmaped, mmap_sz, prot, |
| 8449 | MAP_SHARED | MAP_FIXED, map_fd, 0); |
| 8450 | if (*mmaped == MAP_FAILED) { |
Andrii Nakryiko | d66562f | 2019-12-13 17:43:36 -0800 | [diff] [blame] | 8451 | err = -errno; |
| 8452 | *mmaped = NULL; |
| 8453 | pr_warn("failed to re-mmap() map '%s': %d\n", |
| 8454 | bpf_map__name(map), err); |
| 8455 | return err; |
| 8456 | } |
| 8457 | } |
| 8458 | |
| 8459 | return 0; |
| 8460 | } |
| 8461 | |
| 8462 | int bpf_object__attach_skeleton(struct bpf_object_skeleton *s) |
| 8463 | { |
| 8464 | int i; |
| 8465 | |
| 8466 | for (i = 0; i < s->prog_cnt; i++) { |
| 8467 | struct bpf_program *prog = *s->progs[i].prog; |
| 8468 | struct bpf_link **link = s->progs[i].link; |
| 8469 | const struct bpf_sec_def *sec_def; |
| 8470 | const char *sec_name = bpf_program__title(prog, false); |
| 8471 | |
| 8472 | sec_def = find_sec_def(sec_name); |
| 8473 | if (!sec_def || !sec_def->attach_fn) |
| 8474 | continue; |
| 8475 | |
| 8476 | *link = sec_def->attach_fn(sec_def, prog); |
| 8477 | if (IS_ERR(*link)) { |
| 8478 | pr_warn("failed to auto-attach program '%s': %ld\n", |
| 8479 | bpf_program__name(prog), PTR_ERR(*link)); |
| 8480 | return PTR_ERR(*link); |
| 8481 | } |
| 8482 | } |
| 8483 | |
| 8484 | return 0; |
| 8485 | } |
| 8486 | |
| 8487 | void bpf_object__detach_skeleton(struct bpf_object_skeleton *s) |
| 8488 | { |
| 8489 | int i; |
| 8490 | |
| 8491 | for (i = 0; i < s->prog_cnt; i++) { |
| 8492 | struct bpf_link **link = s->progs[i].link; |
| 8493 | |
| 8494 | if (!IS_ERR_OR_NULL(*link)) |
| 8495 | bpf_link__destroy(*link); |
| 8496 | *link = NULL; |
| 8497 | } |
| 8498 | } |
| 8499 | |
| 8500 | void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s) |
| 8501 | { |
| 8502 | if (s->progs) |
| 8503 | bpf_object__detach_skeleton(s); |
| 8504 | if (s->obj) |
| 8505 | bpf_object__close(*s->obj); |
| 8506 | free(s->maps); |
| 8507 | free(s->progs); |
| 8508 | free(s); |
| 8509 | } |