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> |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame] | 21 | #include <string.h> |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 22 | #include <unistd.h> |
Arnaldo Carvalho de Melo | cdb2f92 | 2019-07-19 11:34:06 -0300 | [diff] [blame] | 23 | #include <endian.h> |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 24 | #include <fcntl.h> |
| 25 | #include <errno.h> |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 26 | #include <asm/unistd.h> |
Joe Stringer | e28ff1a | 2017-01-22 17:11:25 -0800 | [diff] [blame] | 27 | #include <linux/err.h> |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 28 | #include <linux/kernel.h> |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 29 | #include <linux/bpf.h> |
Martin KaFai Lau | 38d5d3b | 2018-07-24 08:40:22 -0700 | [diff] [blame] | 30 | #include <linux/btf.h> |
Stanislav Fomichev | 47eff61 | 2018-11-20 17:11:19 -0800 | [diff] [blame] | 31 | #include <linux/filter.h> |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 32 | #include <linux/list.h> |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 33 | #include <linux/limits.h> |
Yonghong Song | 438363c | 2018-10-09 16:14:47 -0700 | [diff] [blame] | 34 | #include <linux/perf_event.h> |
Daniel Borkmann | a64af0e | 2018-10-19 15:51:03 +0200 | [diff] [blame] | 35 | #include <linux/ring_buffer.h> |
Andrii Nakryiko | 5e61f27 | 2019-10-04 15:40:34 -0700 | [diff] [blame] | 36 | #include <linux/version.h> |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 37 | #include <sys/epoll.h> |
Andrii Nakryiko | 63f2f5e | 2019-07-01 16:58:57 -0700 | [diff] [blame] | 38 | #include <sys/ioctl.h> |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 39 | #include <sys/mman.h> |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 40 | #include <sys/stat.h> |
| 41 | #include <sys/types.h> |
| 42 | #include <sys/vfs.h> |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 43 | #include <sys/utsname.h> |
Jakub Kicinski | 531b014 | 2018-07-10 14:43:05 -0700 | [diff] [blame] | 44 | #include <tools/libc_compat.h> |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 45 | #include <libelf.h> |
| 46 | #include <gelf.h> |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 47 | |
| 48 | #include "libbpf.h" |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 49 | #include "bpf.h" |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 50 | #include "btf.h" |
Arnaldo Carvalho de Melo | 6d41907 | 2018-09-14 16:47:14 -0300 | [diff] [blame] | 51 | #include "str_error.h" |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 52 | #include "libbpf_internal.h" |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 53 | #include "hashmap.h" |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame] | 54 | |
Wang Nan | 9b16137 | 2016-07-18 06:01:08 +0000 | [diff] [blame] | 55 | #ifndef EM_BPF |
| 56 | #define EM_BPF 247 |
| 57 | #endif |
| 58 | |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 59 | #ifndef BPF_FS_MAGIC |
| 60 | #define BPF_FS_MAGIC 0xcafe4a11 |
| 61 | #endif |
| 62 | |
Andrey Ignatov | ff466b5 | 2019-04-06 22:37:34 -0700 | [diff] [blame] | 63 | /* vsprintf() in __base_pr() uses nonliteral format string. It may break |
| 64 | * compilation if user enables corresponding warning. Disable it explicitly. |
| 65 | */ |
| 66 | #pragma GCC diagnostic ignored "-Wformat-nonliteral" |
| 67 | |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame] | 68 | #define __printf(a, b) __attribute__((format(printf, a, b))) |
| 69 | |
Stanislav Fomichev | a8a1f7d | 2019-02-04 16:20:55 -0800 | [diff] [blame] | 70 | static int __base_pr(enum libbpf_print_level level, const char *format, |
| 71 | va_list args) |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame] | 72 | { |
Yonghong Song | 6f1ae8b | 2019-02-01 16:14:17 -0800 | [diff] [blame] | 73 | if (level == LIBBPF_DEBUG) |
| 74 | return 0; |
| 75 | |
Stanislav Fomichev | a8a1f7d | 2019-02-04 16:20:55 -0800 | [diff] [blame] | 76 | return vfprintf(stderr, format, args); |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Stanislav Fomichev | a8a1f7d | 2019-02-04 16:20:55 -0800 | [diff] [blame] | 79 | static libbpf_print_fn_t __libbpf_pr = __base_pr; |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame] | 80 | |
Andrii Nakryiko | e87fd8b | 2019-07-27 20:25:26 -0700 | [diff] [blame] | 81 | libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn) |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame] | 82 | { |
Andrii Nakryiko | e87fd8b | 2019-07-27 20:25:26 -0700 | [diff] [blame] | 83 | libbpf_print_fn_t old_print_fn = __libbpf_pr; |
| 84 | |
Yonghong Song | 6f1ae8b | 2019-02-01 16:14:17 -0800 | [diff] [blame] | 85 | __libbpf_pr = fn; |
Andrii Nakryiko | e87fd8b | 2019-07-27 20:25:26 -0700 | [diff] [blame] | 86 | return old_print_fn; |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame] | 87 | } |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 88 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 89 | __printf(2, 3) |
| 90 | void libbpf_print(enum libbpf_print_level level, const char *format, ...) |
| 91 | { |
| 92 | va_list args; |
| 93 | |
Yonghong Song | 6f1ae8b | 2019-02-01 16:14:17 -0800 | [diff] [blame] | 94 | if (!__libbpf_pr) |
| 95 | return; |
| 96 | |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 97 | va_start(args, format); |
Yonghong Song | 6f1ae8b | 2019-02-01 16:14:17 -0800 | [diff] [blame] | 98 | __libbpf_pr(level, format, args); |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 99 | va_end(args); |
| 100 | } |
| 101 | |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 102 | #define STRERR_BUFSIZE 128 |
| 103 | |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 104 | #define CHECK_ERR(action, err, out) do { \ |
| 105 | err = action; \ |
| 106 | if (err) \ |
| 107 | goto out; \ |
Andrii Nakryiko | 8983b73 | 2019-11-20 23:07:42 -0800 | [diff] [blame] | 108 | } while (0) |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 109 | |
| 110 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 111 | /* Copied from tools/perf/util/util.h */ |
| 112 | #ifndef zfree |
| 113 | # define zfree(ptr) ({ free(*ptr); *ptr = NULL; }) |
| 114 | #endif |
| 115 | |
| 116 | #ifndef zclose |
| 117 | # define zclose(fd) ({ \ |
| 118 | int ___err = 0; \ |
| 119 | if ((fd) >= 0) \ |
| 120 | ___err = close((fd)); \ |
| 121 | fd = -1; \ |
| 122 | ___err; }) |
| 123 | #endif |
| 124 | |
| 125 | #ifdef HAVE_LIBELF_MMAP_SUPPORT |
| 126 | # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP |
| 127 | #else |
| 128 | # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ |
| 129 | #endif |
| 130 | |
Song Liu | 34be1646 | 2019-03-11 22:30:38 -0700 | [diff] [blame] | 131 | static inline __u64 ptr_to_u64(const void *ptr) |
| 132 | { |
| 133 | return (__u64) (unsigned long) ptr; |
| 134 | } |
| 135 | |
Stanislav Fomichev | 47eff61 | 2018-11-20 17:11:19 -0800 | [diff] [blame] | 136 | struct bpf_capabilities { |
| 137 | /* v4.14: kernel support for program & map names. */ |
| 138 | __u32 name:1; |
Daniel Borkmann | 8837fe5 | 2019-04-24 00:45:56 +0200 | [diff] [blame] | 139 | /* v5.2: kernel support for global data sections. */ |
| 140 | __u32 global_data:1; |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 141 | /* BTF_KIND_FUNC and BTF_KIND_FUNC_PROTO support */ |
| 142 | __u32 btf_func:1; |
| 143 | /* BTF_KIND_VAR and BTF_KIND_DATASEC support */ |
| 144 | __u32 btf_datasec:1; |
Andrii Nakryiko | 7fe74b4 | 2019-11-17 09:28:05 -0800 | [diff] [blame] | 145 | /* BPF_F_MMAPABLE is supported for arrays */ |
| 146 | __u32 array_mmap:1; |
Stanislav Fomichev | 47eff61 | 2018-11-20 17:11:19 -0800 | [diff] [blame] | 147 | }; |
| 148 | |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 149 | /* |
| 150 | * bpf_prog should be a better name but it has been used in |
| 151 | * linux/filter.h. |
| 152 | */ |
| 153 | struct bpf_program { |
| 154 | /* Index in elf obj file, for relocation use. */ |
| 155 | int idx; |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 156 | char *name; |
David Beckett | f0307a7 | 2018-05-16 14:02:49 -0700 | [diff] [blame] | 157 | int prog_ifindex; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 158 | char *section_name; |
Stanislav Fomichev | 33a2c75 | 2018-11-09 08:21:43 -0800 | [diff] [blame] | 159 | /* section_name with / replaced by _; makes recursive pinning |
| 160 | * in bpf_object__pin_programs easier |
| 161 | */ |
| 162 | char *pin_name; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 163 | struct bpf_insn *insns; |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 164 | size_t insns_cnt, main_prog_cnt; |
Wang Nan | 5f44e4c8 | 2016-07-13 10:44:01 +0000 | [diff] [blame] | 165 | enum bpf_prog_type type; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 166 | |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 167 | struct reloc_desc { |
| 168 | enum { |
| 169 | RELO_LD64, |
| 170 | RELO_CALL, |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 171 | RELO_DATA, |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 172 | } type; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 173 | int insn_idx; |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 174 | union { |
| 175 | int map_idx; |
| 176 | int text_off; |
| 177 | }; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 178 | } *reloc_desc; |
| 179 | int nr_reloc; |
Alexei Starovoitov | da11b41 | 2019-04-01 21:27:47 -0700 | [diff] [blame] | 180 | int log_level; |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 181 | |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 182 | struct { |
| 183 | int nr; |
| 184 | int *fds; |
| 185 | } instances; |
| 186 | bpf_program_prep_t preprocessor; |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 187 | |
| 188 | struct bpf_object *obj; |
| 189 | void *priv; |
| 190 | bpf_program_clear_priv_t clear_priv; |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 191 | |
| 192 | enum bpf_attach_type expected_attach_type; |
Alexei Starovoitov | 12a8654 | 2019-10-30 15:32:12 -0700 | [diff] [blame] | 193 | __u32 attach_btf_id; |
Alexei Starovoitov | e7bf94d | 2019-11-14 10:57:18 -0800 | [diff] [blame] | 194 | __u32 attach_prog_fd; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 195 | void *func_info; |
| 196 | __u32 func_info_rec_size; |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 197 | __u32 func_info_cnt; |
Stanislav Fomichev | 47eff61 | 2018-11-20 17:11:19 -0800 | [diff] [blame] | 198 | |
| 199 | struct bpf_capabilities *caps; |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 200 | |
| 201 | void *line_info; |
| 202 | __u32 line_info_rec_size; |
| 203 | __u32 line_info_cnt; |
Jiong Wang | 0465619 | 2019-05-24 23:25:19 +0100 | [diff] [blame] | 204 | __u32 prog_flags; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 205 | }; |
| 206 | |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 207 | enum libbpf_map_type { |
| 208 | LIBBPF_MAP_UNSPEC, |
| 209 | LIBBPF_MAP_DATA, |
| 210 | LIBBPF_MAP_BSS, |
| 211 | LIBBPF_MAP_RODATA, |
| 212 | }; |
| 213 | |
| 214 | static const char * const libbpf_type_to_btf_name[] = { |
| 215 | [LIBBPF_MAP_DATA] = ".data", |
| 216 | [LIBBPF_MAP_BSS] = ".bss", |
| 217 | [LIBBPF_MAP_RODATA] = ".rodata", |
| 218 | }; |
| 219 | |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 220 | struct bpf_map { |
| 221 | int fd; |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 222 | char *name; |
Andrii Nakryiko | db48814 | 2019-06-17 12:26:54 -0700 | [diff] [blame] | 223 | int sec_idx; |
| 224 | size_t sec_offset; |
David Beckett | f0307a7 | 2018-05-16 14:02:49 -0700 | [diff] [blame] | 225 | int map_ifindex; |
Nikita V. Shirokov | addb9fc | 2018-11-20 20:55:56 -0800 | [diff] [blame] | 226 | int inner_map_fd; |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 227 | struct bpf_map_def def; |
Martin KaFai Lau | 5b891af | 2018-07-24 08:40:21 -0700 | [diff] [blame] | 228 | __u32 btf_key_type_id; |
| 229 | __u32 btf_value_type_id; |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 230 | void *priv; |
| 231 | bpf_map_clear_priv_t clear_priv; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 232 | enum libbpf_map_type libbpf_type; |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 233 | char *pin_path; |
| 234 | bool pinned; |
Toke Høiland-Jørgensen | ec6d5f47 | 2019-11-09 21:37:27 +0100 | [diff] [blame] | 235 | bool reused; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 236 | }; |
| 237 | |
| 238 | struct bpf_secdata { |
| 239 | void *rodata; |
| 240 | void *data; |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 241 | }; |
| 242 | |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 243 | static LIST_HEAD(bpf_objects_list); |
| 244 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 245 | struct bpf_object { |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 246 | char name[BPF_OBJ_NAME_LEN]; |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 247 | char license[64]; |
Yonghong Song | 438363c | 2018-10-09 16:14:47 -0700 | [diff] [blame] | 248 | __u32 kern_version; |
Wang Nan | 0b3d1ef | 2015-07-01 02:13:58 +0000 | [diff] [blame] | 249 | |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 250 | struct bpf_program *programs; |
| 251 | size_t nr_programs; |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 252 | struct bpf_map *maps; |
| 253 | size_t nr_maps; |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 254 | size_t maps_cap; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 255 | struct bpf_secdata sections; |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 256 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 257 | bool loaded; |
Jakub Kicinski | 9a94f27 | 2018-06-28 14:41:38 -0700 | [diff] [blame] | 258 | bool has_pseudo_calls; |
Andrii Nakryiko | 62561eb | 2019-10-15 11:28:47 -0700 | [diff] [blame] | 259 | bool relaxed_core_relocs; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 260 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 261 | /* |
| 262 | * Information when doing elf related work. Only valid if fd |
| 263 | * is valid. |
| 264 | */ |
| 265 | struct { |
| 266 | int fd; |
Andrii Nakryiko | 5e61f27 | 2019-10-04 15:40:34 -0700 | [diff] [blame] | 267 | const void *obj_buf; |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 268 | size_t obj_buf_sz; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 269 | Elf *elf; |
| 270 | GElf_Ehdr ehdr; |
Wang Nan | bec7d68 | 2015-07-01 02:13:59 +0000 | [diff] [blame] | 271 | Elf_Data *symbols; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 272 | Elf_Data *data; |
| 273 | Elf_Data *rodata; |
| 274 | Elf_Data *bss; |
Wang Nan | 77ba9a5 | 2015-12-08 02:25:30 +0000 | [diff] [blame] | 275 | size_t strtabidx; |
Wang Nan | b62f06e | 2015-07-01 02:14:01 +0000 | [diff] [blame] | 276 | struct { |
| 277 | GElf_Shdr shdr; |
| 278 | Elf_Data *data; |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 279 | } *reloc_sects; |
| 280 | int nr_reloc_sects; |
Wang Nan | 666810e | 2016-01-25 09:55:49 +0000 | [diff] [blame] | 281 | int maps_shndx; |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 282 | int btf_maps_shndx; |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 283 | int text_shndx; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 284 | int data_shndx; |
| 285 | int rodata_shndx; |
| 286 | int bss_shndx; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 287 | } efile; |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 288 | /* |
| 289 | * All loaded bpf_object is linked in a list, which is |
| 290 | * hidden to caller. bpf_objects__<func> handlers deal with |
| 291 | * all objects. |
| 292 | */ |
| 293 | struct list_head list; |
Wang Nan | 10931d2 | 2016-11-26 07:03:26 +0000 | [diff] [blame] | 294 | |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 295 | struct btf *btf; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 296 | struct btf_ext *btf_ext; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 297 | |
Wang Nan | 10931d2 | 2016-11-26 07:03:26 +0000 | [diff] [blame] | 298 | void *priv; |
| 299 | bpf_object_clear_priv_t clear_priv; |
| 300 | |
Stanislav Fomichev | 47eff61 | 2018-11-20 17:11:19 -0800 | [diff] [blame] | 301 | struct bpf_capabilities caps; |
| 302 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 303 | char path[]; |
| 304 | }; |
| 305 | #define obj_elf_valid(o) ((o)->efile.elf) |
| 306 | |
Joe Stringer | 29cd77f | 2018-10-02 13:35:39 -0700 | [diff] [blame] | 307 | void bpf_program__unload(struct bpf_program *prog) |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 308 | { |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 309 | int i; |
| 310 | |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 311 | if (!prog) |
| 312 | return; |
| 313 | |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 314 | /* |
| 315 | * If the object is opened but the program was never loaded, |
| 316 | * it is possible that prog->instances.nr == -1. |
| 317 | */ |
| 318 | if (prog->instances.nr > 0) { |
| 319 | for (i = 0; i < prog->instances.nr; i++) |
| 320 | zclose(prog->instances.fds[i]); |
| 321 | } else if (prog->instances.nr != -1) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 322 | pr_warn("Internal error: instances.nr is %d\n", |
| 323 | prog->instances.nr); |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | prog->instances.nr = -1; |
| 327 | zfree(&prog->instances.fds); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 328 | |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 329 | zfree(&prog->func_info); |
Prashant Bhole | 07a09d1 | 2018-12-17 16:57:50 +0900 | [diff] [blame] | 330 | zfree(&prog->line_info); |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 333 | static void bpf_program__exit(struct bpf_program *prog) |
| 334 | { |
| 335 | if (!prog) |
| 336 | return; |
| 337 | |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 338 | if (prog->clear_priv) |
| 339 | prog->clear_priv(prog, prog->priv); |
| 340 | |
| 341 | prog->priv = NULL; |
| 342 | prog->clear_priv = NULL; |
| 343 | |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 344 | bpf_program__unload(prog); |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 345 | zfree(&prog->name); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 346 | zfree(&prog->section_name); |
Stanislav Fomichev | 33a2c75 | 2018-11-09 08:21:43 -0800 | [diff] [blame] | 347 | zfree(&prog->pin_name); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 348 | zfree(&prog->insns); |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 349 | zfree(&prog->reloc_desc); |
| 350 | |
| 351 | prog->nr_reloc = 0; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 352 | prog->insns_cnt = 0; |
| 353 | prog->idx = -1; |
| 354 | } |
| 355 | |
Stanislav Fomichev | 33a2c75 | 2018-11-09 08:21:43 -0800 | [diff] [blame] | 356 | static char *__bpf_program__pin_name(struct bpf_program *prog) |
| 357 | { |
| 358 | char *name, *p; |
| 359 | |
| 360 | name = p = strdup(prog->section_name); |
| 361 | while ((p = strchr(p, '/'))) |
| 362 | *p = '_'; |
| 363 | |
| 364 | return name; |
| 365 | } |
| 366 | |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 367 | static int |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 368 | bpf_program__init(void *data, size_t size, char *section_name, int idx, |
| 369 | struct bpf_program *prog) |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 370 | { |
Andrii Nakryiko | 8ca990c | 2019-05-29 10:36:03 -0700 | [diff] [blame] | 371 | const size_t bpf_insn_sz = sizeof(struct bpf_insn); |
| 372 | |
| 373 | if (size == 0 || size % bpf_insn_sz) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 374 | pr_warn("corrupted section '%s', size: %zu\n", |
| 375 | section_name, size); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 376 | return -EINVAL; |
| 377 | } |
| 378 | |
Andrii Nakryiko | 1ad9cbb | 2019-02-13 10:25:53 -0800 | [diff] [blame] | 379 | memset(prog, 0, sizeof(*prog)); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 380 | |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 381 | prog->section_name = strdup(section_name); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 382 | if (!prog->section_name) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 383 | pr_warn("failed to alloc name for prog under section(%d) %s\n", |
| 384 | idx, section_name); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 385 | goto errout; |
| 386 | } |
| 387 | |
Stanislav Fomichev | 33a2c75 | 2018-11-09 08:21:43 -0800 | [diff] [blame] | 388 | prog->pin_name = __bpf_program__pin_name(prog); |
| 389 | if (!prog->pin_name) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 390 | pr_warn("failed to alloc pin name for prog under section(%d) %s\n", |
| 391 | idx, section_name); |
Stanislav Fomichev | 33a2c75 | 2018-11-09 08:21:43 -0800 | [diff] [blame] | 392 | goto errout; |
| 393 | } |
| 394 | |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 395 | prog->insns = malloc(size); |
| 396 | if (!prog->insns) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 397 | pr_warn("failed to alloc insns for prog under section %s\n", |
| 398 | section_name); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 399 | goto errout; |
| 400 | } |
Andrii Nakryiko | 8ca990c | 2019-05-29 10:36:03 -0700 | [diff] [blame] | 401 | prog->insns_cnt = size / bpf_insn_sz; |
| 402 | memcpy(prog->insns, data, size); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 403 | prog->idx = idx; |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 404 | prog->instances.fds = NULL; |
| 405 | prog->instances.nr = -1; |
Nikita V. Shirokov | 47ae7e3 | 2018-11-23 12:58:12 -0800 | [diff] [blame] | 406 | prog->type = BPF_PROG_TYPE_UNSPEC; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 407 | |
| 408 | return 0; |
| 409 | errout: |
| 410 | bpf_program__exit(prog); |
| 411 | return -ENOMEM; |
| 412 | } |
| 413 | |
| 414 | static int |
| 415 | 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] | 416 | char *section_name, int idx) |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 417 | { |
| 418 | struct bpf_program prog, *progs; |
| 419 | int nr_progs, err; |
| 420 | |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 421 | err = bpf_program__init(data, size, section_name, idx, &prog); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 422 | if (err) |
| 423 | return err; |
| 424 | |
Stanislav Fomichev | 47eff61 | 2018-11-20 17:11:19 -0800 | [diff] [blame] | 425 | prog.caps = &obj->caps; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 426 | progs = obj->programs; |
| 427 | nr_progs = obj->nr_programs; |
| 428 | |
Jakub Kicinski | 531b014 | 2018-07-10 14:43:05 -0700 | [diff] [blame] | 429 | progs = reallocarray(progs, nr_progs + 1, sizeof(progs[0])); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 430 | if (!progs) { |
| 431 | /* |
| 432 | * In this case the original obj->programs |
| 433 | * is still valid, so don't need special treat for |
| 434 | * bpf_close_object(). |
| 435 | */ |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 436 | pr_warn("failed to alloc a new program under section '%s'\n", |
| 437 | section_name); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 438 | bpf_program__exit(&prog); |
| 439 | return -ENOMEM; |
| 440 | } |
| 441 | |
| 442 | pr_debug("found program %s\n", prog.section_name); |
| 443 | obj->programs = progs; |
| 444 | obj->nr_programs = nr_progs + 1; |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 445 | prog.obj = obj; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 446 | progs[nr_progs] = prog; |
| 447 | return 0; |
| 448 | } |
| 449 | |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 450 | static int |
| 451 | bpf_object__init_prog_names(struct bpf_object *obj) |
| 452 | { |
| 453 | Elf_Data *symbols = obj->efile.symbols; |
| 454 | struct bpf_program *prog; |
| 455 | size_t pi, si; |
| 456 | |
| 457 | for (pi = 0; pi < obj->nr_programs; pi++) { |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 458 | const char *name = NULL; |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 459 | |
| 460 | prog = &obj->programs[pi]; |
| 461 | |
| 462 | for (si = 0; si < symbols->d_size / sizeof(GElf_Sym) && !name; |
| 463 | si++) { |
| 464 | GElf_Sym sym; |
| 465 | |
| 466 | if (!gelf_getsym(symbols, si, &sym)) |
| 467 | continue; |
| 468 | if (sym.st_shndx != prog->idx) |
| 469 | continue; |
Roman Gushchin | fe4d44b | 2017-12-13 15:18:52 +0000 | [diff] [blame] | 470 | if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL) |
| 471 | continue; |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 472 | |
| 473 | name = elf_strptr(obj->efile.elf, |
| 474 | obj->efile.strtabidx, |
| 475 | sym.st_name); |
| 476 | if (!name) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 477 | pr_warn("failed to get sym name string for prog %s\n", |
| 478 | prog->section_name); |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 479 | return -LIBBPF_ERRNO__LIBELF; |
| 480 | } |
| 481 | } |
| 482 | |
Jakub Kicinski | 9a94f27 | 2018-06-28 14:41:38 -0700 | [diff] [blame] | 483 | if (!name && prog->idx == obj->efile.text_shndx) |
| 484 | name = ".text"; |
| 485 | |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 486 | if (!name) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 487 | pr_warn("failed to find sym for prog %s\n", |
| 488 | prog->section_name); |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 489 | return -EINVAL; |
| 490 | } |
Jakub Kicinski | 9a94f27 | 2018-06-28 14:41:38 -0700 | [diff] [blame] | 491 | |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 492 | prog->name = strdup(name); |
| 493 | if (!prog->name) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 494 | pr_warn("failed to allocate memory for prog sym %s\n", |
| 495 | name); |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 496 | return -ENOMEM; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | |
Andrii Nakryiko | 5e61f27 | 2019-10-04 15:40:34 -0700 | [diff] [blame] | 503 | static __u32 get_kernel_version(void) |
| 504 | { |
| 505 | __u32 major, minor, patch; |
| 506 | struct utsname info; |
| 507 | |
| 508 | uname(&info); |
| 509 | if (sscanf(info.release, "%u.%u.%u", &major, &minor, &patch) != 3) |
| 510 | return 0; |
| 511 | return KERNEL_VERSION(major, minor, patch); |
| 512 | } |
| 513 | |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 514 | static struct bpf_object *bpf_object__new(const char *path, |
Andrii Nakryiko | 5e61f27 | 2019-10-04 15:40:34 -0700 | [diff] [blame] | 515 | const void *obj_buf, |
Andrii Nakryiko | 2ce8450 | 2019-10-04 15:40:35 -0700 | [diff] [blame] | 516 | size_t obj_buf_sz, |
| 517 | const char *obj_name) |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 518 | { |
| 519 | struct bpf_object *obj; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 520 | char *end; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 521 | |
| 522 | obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1); |
| 523 | if (!obj) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 524 | pr_warn("alloc memory failed for %s\n", path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 525 | return ERR_PTR(-ENOMEM); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | strcpy(obj->path, path); |
Andrii Nakryiko | 2ce8450 | 2019-10-04 15:40:35 -0700 | [diff] [blame] | 529 | if (obj_name) { |
| 530 | strncpy(obj->name, obj_name, sizeof(obj->name) - 1); |
| 531 | obj->name[sizeof(obj->name) - 1] = 0; |
| 532 | } else { |
| 533 | /* Using basename() GNU version which doesn't modify arg. */ |
| 534 | strncpy(obj->name, basename((void *)path), |
| 535 | sizeof(obj->name) - 1); |
| 536 | end = strchr(obj->name, '.'); |
| 537 | if (end) |
| 538 | *end = 0; |
| 539 | } |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 540 | |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 541 | obj->efile.fd = -1; |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 542 | /* |
Andrii Nakryiko | 76e1022 | 2019-05-29 10:36:10 -0700 | [diff] [blame] | 543 | * Caller of this function should also call |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 544 | * bpf_object__elf_finish() after data collection to return |
| 545 | * obj_buf to user. If not, we should duplicate the buffer to |
| 546 | * avoid user freeing them before elf finish. |
| 547 | */ |
| 548 | obj->efile.obj_buf = obj_buf; |
| 549 | obj->efile.obj_buf_sz = obj_buf_sz; |
Wang Nan | 666810e | 2016-01-25 09:55:49 +0000 | [diff] [blame] | 550 | obj->efile.maps_shndx = -1; |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 551 | obj->efile.btf_maps_shndx = -1; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 552 | obj->efile.data_shndx = -1; |
| 553 | obj->efile.rodata_shndx = -1; |
| 554 | obj->efile.bss_shndx = -1; |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 555 | |
Andrii Nakryiko | 5e61f27 | 2019-10-04 15:40:34 -0700 | [diff] [blame] | 556 | obj->kern_version = get_kernel_version(); |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 557 | obj->loaded = false; |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 558 | |
| 559 | INIT_LIST_HEAD(&obj->list); |
| 560 | list_add(&obj->list, &bpf_objects_list); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 561 | return obj; |
| 562 | } |
| 563 | |
| 564 | static void bpf_object__elf_finish(struct bpf_object *obj) |
| 565 | { |
| 566 | if (!obj_elf_valid(obj)) |
| 567 | return; |
| 568 | |
| 569 | if (obj->efile.elf) { |
| 570 | elf_end(obj->efile.elf); |
| 571 | obj->efile.elf = NULL; |
| 572 | } |
Wang Nan | bec7d68 | 2015-07-01 02:13:59 +0000 | [diff] [blame] | 573 | obj->efile.symbols = NULL; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 574 | obj->efile.data = NULL; |
| 575 | obj->efile.rodata = NULL; |
| 576 | obj->efile.bss = NULL; |
Wang Nan | b62f06e | 2015-07-01 02:14:01 +0000 | [diff] [blame] | 577 | |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 578 | zfree(&obj->efile.reloc_sects); |
| 579 | obj->efile.nr_reloc_sects = 0; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 580 | zclose(obj->efile.fd); |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 581 | obj->efile.obj_buf = NULL; |
| 582 | obj->efile.obj_buf_sz = 0; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | static int bpf_object__elf_init(struct bpf_object *obj) |
| 586 | { |
| 587 | int err = 0; |
| 588 | GElf_Ehdr *ep; |
| 589 | |
| 590 | if (obj_elf_valid(obj)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 591 | pr_warn("elf init: internal error\n"); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 592 | return -LIBBPF_ERRNO__LIBELF; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 595 | if (obj->efile.obj_buf_sz > 0) { |
| 596 | /* |
| 597 | * obj_buf should have been validated by |
| 598 | * bpf_object__open_buffer(). |
| 599 | */ |
Andrii Nakryiko | 5e61f27 | 2019-10-04 15:40:34 -0700 | [diff] [blame] | 600 | obj->efile.elf = elf_memory((char *)obj->efile.obj_buf, |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 601 | obj->efile.obj_buf_sz); |
| 602 | } else { |
| 603 | obj->efile.fd = open(obj->path, O_RDONLY); |
| 604 | if (obj->efile.fd < 0) { |
Andrii Nakryiko | be5c5d4 | 2019-05-29 10:36:04 -0700 | [diff] [blame] | 605 | char errmsg[STRERR_BUFSIZE], *cp; |
Thomas Richter | 1ce6a9f | 2018-07-30 10:53:23 +0200 | [diff] [blame] | 606 | |
Andrii Nakryiko | be5c5d4 | 2019-05-29 10:36:04 -0700 | [diff] [blame] | 607 | err = -errno; |
| 608 | cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg)); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 609 | pr_warn("failed to open %s: %s\n", obj->path, cp); |
Andrii Nakryiko | be5c5d4 | 2019-05-29 10:36:04 -0700 | [diff] [blame] | 610 | return err; |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | obj->efile.elf = elf_begin(obj->efile.fd, |
Andrii Nakryiko | 76e1022 | 2019-05-29 10:36:10 -0700 | [diff] [blame] | 614 | LIBBPF_ELF_C_READ_MMAP, NULL); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 617 | if (!obj->efile.elf) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 618 | pr_warn("failed to open %s as ELF file\n", obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 619 | err = -LIBBPF_ERRNO__LIBELF; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 620 | goto errout; |
| 621 | } |
| 622 | |
| 623 | if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 624 | pr_warn("failed to get EHDR from %s\n", obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 625 | err = -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 626 | goto errout; |
| 627 | } |
| 628 | ep = &obj->efile.ehdr; |
| 629 | |
Wang Nan | 9b16137 | 2016-07-18 06:01:08 +0000 | [diff] [blame] | 630 | /* Old LLVM set e_machine to EM_NONE */ |
Andrii Nakryiko | 76e1022 | 2019-05-29 10:36:10 -0700 | [diff] [blame] | 631 | if (ep->e_type != ET_REL || |
| 632 | (ep->e_machine && ep->e_machine != EM_BPF)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 633 | pr_warn("%s is not an eBPF object file\n", obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 634 | err = -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 635 | goto errout; |
| 636 | } |
| 637 | |
| 638 | return 0; |
| 639 | errout: |
| 640 | bpf_object__elf_finish(obj); |
| 641 | return err; |
| 642 | } |
| 643 | |
Andrii Nakryiko | 12ef563 | 2019-05-29 10:36:05 -0700 | [diff] [blame] | 644 | static int bpf_object__check_endianness(struct bpf_object *obj) |
Wang Nan | cc4228d | 2015-07-01 02:13:55 +0000 | [diff] [blame] | 645 | { |
Arnaldo Carvalho de Melo | cdb2f92 | 2019-07-19 11:34:06 -0300 | [diff] [blame] | 646 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
Andrii Nakryiko | 12ef563 | 2019-05-29 10:36:05 -0700 | [diff] [blame] | 647 | if (obj->efile.ehdr.e_ident[EI_DATA] == ELFDATA2LSB) |
| 648 | return 0; |
Arnaldo Carvalho de Melo | cdb2f92 | 2019-07-19 11:34:06 -0300 | [diff] [blame] | 649 | #elif __BYTE_ORDER == __BIG_ENDIAN |
Andrii Nakryiko | 12ef563 | 2019-05-29 10:36:05 -0700 | [diff] [blame] | 650 | if (obj->efile.ehdr.e_ident[EI_DATA] == ELFDATA2MSB) |
| 651 | return 0; |
| 652 | #else |
| 653 | # error "Unrecognized __BYTE_ORDER__" |
| 654 | #endif |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 655 | pr_warn("endianness mismatch.\n"); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 656 | return -LIBBPF_ERRNO__ENDIAN; |
Wang Nan | cc4228d | 2015-07-01 02:13:55 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 659 | static int |
Andrii Nakryiko | 399dc65 | 2019-05-29 10:36:11 -0700 | [diff] [blame] | 660 | 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] | 661 | { |
Andrii Nakryiko | 399dc65 | 2019-05-29 10:36:11 -0700 | [diff] [blame] | 662 | memcpy(obj->license, data, min(size, sizeof(obj->license) - 1)); |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 663 | pr_debug("license of %s is %s\n", obj->path, obj->license); |
| 664 | return 0; |
| 665 | } |
| 666 | |
John Fastabend | 54b8625 | 2019-10-18 07:41:26 -0700 | [diff] [blame] | 667 | static int |
| 668 | bpf_object__init_kversion(struct bpf_object *obj, void *data, size_t size) |
| 669 | { |
| 670 | __u32 kver; |
| 671 | |
| 672 | if (size != sizeof(kver)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 673 | pr_warn("invalid kver section in %s\n", obj->path); |
John Fastabend | 54b8625 | 2019-10-18 07:41:26 -0700 | [diff] [blame] | 674 | return -LIBBPF_ERRNO__FORMAT; |
| 675 | } |
| 676 | memcpy(&kver, data, sizeof(kver)); |
| 677 | obj->kern_version = kver; |
| 678 | pr_debug("kernel version of %s is %x\n", obj->path, obj->kern_version); |
| 679 | return 0; |
| 680 | } |
| 681 | |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 682 | static int compare_bpf_map(const void *_a, const void *_b) |
| 683 | { |
| 684 | const struct bpf_map *a = _a; |
| 685 | const struct bpf_map *b = _b; |
| 686 | |
Andrii Nakryiko | db48814 | 2019-06-17 12:26:54 -0700 | [diff] [blame] | 687 | if (a->sec_idx != b->sec_idx) |
| 688 | return a->sec_idx - b->sec_idx; |
| 689 | return a->sec_offset - b->sec_offset; |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Nikita V. Shirokov | addb9fc | 2018-11-20 20:55:56 -0800 | [diff] [blame] | 692 | static bool bpf_map_type__is_map_in_map(enum bpf_map_type type) |
| 693 | { |
| 694 | if (type == BPF_MAP_TYPE_ARRAY_OF_MAPS || |
| 695 | type == BPF_MAP_TYPE_HASH_OF_MAPS) |
| 696 | return true; |
| 697 | return false; |
| 698 | } |
| 699 | |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 700 | static int bpf_object_search_section_size(const struct bpf_object *obj, |
| 701 | const char *name, size_t *d_size) |
| 702 | { |
| 703 | const GElf_Ehdr *ep = &obj->efile.ehdr; |
| 704 | Elf *elf = obj->efile.elf; |
| 705 | Elf_Scn *scn = NULL; |
| 706 | int idx = 0; |
| 707 | |
| 708 | while ((scn = elf_nextscn(elf, scn)) != NULL) { |
| 709 | const char *sec_name; |
| 710 | Elf_Data *data; |
| 711 | GElf_Shdr sh; |
| 712 | |
| 713 | idx++; |
| 714 | if (gelf_getshdr(scn, &sh) != &sh) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 715 | pr_warn("failed to get section(%d) header from %s\n", |
| 716 | idx, obj->path); |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 717 | return -EIO; |
| 718 | } |
| 719 | |
| 720 | sec_name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name); |
| 721 | if (!sec_name) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 722 | pr_warn("failed to get section(%d) name from %s\n", |
| 723 | idx, obj->path); |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 724 | return -EIO; |
| 725 | } |
| 726 | |
| 727 | if (strcmp(name, sec_name)) |
| 728 | continue; |
| 729 | |
| 730 | data = elf_getdata(scn, 0); |
| 731 | if (!data) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 732 | pr_warn("failed to get section(%d) data from %s(%s)\n", |
| 733 | idx, name, obj->path); |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 734 | return -EIO; |
| 735 | } |
| 736 | |
| 737 | *d_size = data->d_size; |
| 738 | return 0; |
| 739 | } |
| 740 | |
| 741 | return -ENOENT; |
| 742 | } |
| 743 | |
| 744 | int bpf_object__section_size(const struct bpf_object *obj, const char *name, |
| 745 | __u32 *size) |
| 746 | { |
| 747 | int ret = -ENOENT; |
| 748 | size_t d_size; |
| 749 | |
| 750 | *size = 0; |
| 751 | if (!name) { |
| 752 | return -EINVAL; |
| 753 | } else if (!strcmp(name, ".data")) { |
| 754 | if (obj->efile.data) |
| 755 | *size = obj->efile.data->d_size; |
| 756 | } else if (!strcmp(name, ".bss")) { |
| 757 | if (obj->efile.bss) |
| 758 | *size = obj->efile.bss->d_size; |
| 759 | } else if (!strcmp(name, ".rodata")) { |
| 760 | if (obj->efile.rodata) |
| 761 | *size = obj->efile.rodata->d_size; |
| 762 | } else { |
| 763 | ret = bpf_object_search_section_size(obj, name, &d_size); |
| 764 | if (!ret) |
| 765 | *size = d_size; |
| 766 | } |
| 767 | |
| 768 | return *size ? 0 : ret; |
| 769 | } |
| 770 | |
| 771 | int bpf_object__variable_offset(const struct bpf_object *obj, const char *name, |
| 772 | __u32 *off) |
| 773 | { |
| 774 | Elf_Data *symbols = obj->efile.symbols; |
| 775 | const char *sname; |
| 776 | size_t si; |
| 777 | |
| 778 | if (!name || !off) |
| 779 | return -EINVAL; |
| 780 | |
| 781 | for (si = 0; si < symbols->d_size / sizeof(GElf_Sym); si++) { |
| 782 | GElf_Sym sym; |
| 783 | |
| 784 | if (!gelf_getsym(symbols, si, &sym)) |
| 785 | continue; |
| 786 | if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL || |
| 787 | GELF_ST_TYPE(sym.st_info) != STT_OBJECT) |
| 788 | continue; |
| 789 | |
| 790 | sname = elf_strptr(obj->efile.elf, obj->efile.strtabidx, |
| 791 | sym.st_name); |
| 792 | if (!sname) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 793 | pr_warn("failed to get sym name string for var %s\n", |
| 794 | name); |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 795 | return -EIO; |
| 796 | } |
| 797 | if (strcmp(name, sname) == 0) { |
| 798 | *off = sym.st_value; |
| 799 | return 0; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | return -ENOENT; |
| 804 | } |
| 805 | |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 806 | static struct bpf_map *bpf_object__add_map(struct bpf_object *obj) |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 807 | { |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 808 | struct bpf_map *new_maps; |
| 809 | size_t new_cap; |
| 810 | int i; |
| 811 | |
| 812 | if (obj->nr_maps < obj->maps_cap) |
| 813 | return &obj->maps[obj->nr_maps++]; |
| 814 | |
Ivan Khoronzhuk | 9506497 | 2019-06-26 13:38:37 +0300 | [diff] [blame] | 815 | new_cap = max((size_t)4, obj->maps_cap * 3 / 2); |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 816 | new_maps = realloc(obj->maps, new_cap * sizeof(*obj->maps)); |
| 817 | if (!new_maps) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 818 | pr_warn("alloc maps for object failed\n"); |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 819 | return ERR_PTR(-ENOMEM); |
| 820 | } |
| 821 | |
| 822 | obj->maps_cap = new_cap; |
| 823 | obj->maps = new_maps; |
| 824 | |
| 825 | /* zero out new maps */ |
| 826 | memset(obj->maps + obj->nr_maps, 0, |
| 827 | (obj->maps_cap - obj->nr_maps) * sizeof(*obj->maps)); |
| 828 | /* |
| 829 | * fill all fd with -1 so won't close incorrect fd (fd=0 is stdin) |
| 830 | * when failure (zclose won't close negative fd)). |
| 831 | */ |
| 832 | for (i = obj->nr_maps; i < obj->maps_cap; i++) { |
| 833 | obj->maps[i].fd = -1; |
| 834 | obj->maps[i].inner_map_fd = -1; |
| 835 | } |
| 836 | |
| 837 | return &obj->maps[obj->nr_maps++]; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | static int |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 841 | bpf_object__init_internal_map(struct bpf_object *obj, enum libbpf_map_type type, |
Andrii Nakryiko | db48814 | 2019-06-17 12:26:54 -0700 | [diff] [blame] | 842 | int sec_idx, Elf_Data *data, void **data_buff) |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 843 | { |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 844 | char map_name[BPF_OBJ_NAME_LEN]; |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 845 | struct bpf_map_def *def; |
| 846 | struct bpf_map *map; |
| 847 | |
| 848 | map = bpf_object__add_map(obj); |
| 849 | if (IS_ERR(map)) |
| 850 | return PTR_ERR(map); |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 851 | |
| 852 | map->libbpf_type = type; |
Andrii Nakryiko | db48814 | 2019-06-17 12:26:54 -0700 | [diff] [blame] | 853 | map->sec_idx = sec_idx; |
| 854 | map->sec_offset = 0; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 855 | snprintf(map_name, sizeof(map_name), "%.8s%.7s", obj->name, |
| 856 | libbpf_type_to_btf_name[type]); |
| 857 | map->name = strdup(map_name); |
| 858 | if (!map->name) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 859 | pr_warn("failed to alloc map name\n"); |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 860 | return -ENOMEM; |
| 861 | } |
| 862 | |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 863 | def = &map->def; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 864 | def->type = BPF_MAP_TYPE_ARRAY; |
| 865 | def->key_size = sizeof(int); |
| 866 | def->value_size = data->d_size; |
| 867 | def->max_entries = 1; |
Andrii Nakryiko | 399dc65 | 2019-05-29 10:36:11 -0700 | [diff] [blame] | 868 | def->map_flags = type == LIBBPF_MAP_RODATA ? BPF_F_RDONLY_PROG : 0; |
Andrii Nakryiko | 7fe74b4 | 2019-11-17 09:28:05 -0800 | [diff] [blame] | 869 | if (obj->caps.array_mmap) |
| 870 | def->map_flags |= BPF_F_MMAPABLE; |
| 871 | |
| 872 | pr_debug("map '%s' (global data): at sec_idx %d, offset %zu, flags %x.\n", |
| 873 | map_name, map->sec_idx, map->sec_offset, def->map_flags); |
| 874 | |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 875 | if (data_buff) { |
| 876 | *data_buff = malloc(data->d_size); |
| 877 | if (!*data_buff) { |
| 878 | zfree(&map->name); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 879 | pr_warn("failed to alloc map content buffer\n"); |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 880 | return -ENOMEM; |
| 881 | } |
| 882 | memcpy(*data_buff, data->d_buf, data->d_size); |
| 883 | } |
| 884 | |
Andrii Nakryiko | e1d1dc4 | 2019-04-16 11:47:17 -0700 | [diff] [blame] | 885 | pr_debug("map %td is \"%s\"\n", map - obj->maps, map->name); |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 886 | return 0; |
| 887 | } |
| 888 | |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 889 | static int bpf_object__init_global_data_maps(struct bpf_object *obj) |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 890 | { |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 891 | int err; |
| 892 | |
| 893 | if (!obj->caps.global_data) |
| 894 | return 0; |
| 895 | /* |
| 896 | * Populate obj->maps with libbpf internal maps. |
| 897 | */ |
| 898 | if (obj->efile.data_shndx >= 0) { |
| 899 | err = bpf_object__init_internal_map(obj, LIBBPF_MAP_DATA, |
Andrii Nakryiko | db48814 | 2019-06-17 12:26:54 -0700 | [diff] [blame] | 900 | obj->efile.data_shndx, |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 901 | obj->efile.data, |
| 902 | &obj->sections.data); |
| 903 | if (err) |
| 904 | return err; |
| 905 | } |
| 906 | if (obj->efile.rodata_shndx >= 0) { |
| 907 | err = bpf_object__init_internal_map(obj, LIBBPF_MAP_RODATA, |
Andrii Nakryiko | db48814 | 2019-06-17 12:26:54 -0700 | [diff] [blame] | 908 | obj->efile.rodata_shndx, |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 909 | obj->efile.rodata, |
| 910 | &obj->sections.rodata); |
| 911 | if (err) |
| 912 | return err; |
| 913 | } |
| 914 | if (obj->efile.bss_shndx >= 0) { |
| 915 | err = bpf_object__init_internal_map(obj, LIBBPF_MAP_BSS, |
Andrii Nakryiko | db48814 | 2019-06-17 12:26:54 -0700 | [diff] [blame] | 916 | obj->efile.bss_shndx, |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 917 | obj->efile.bss, NULL); |
| 918 | if (err) |
| 919 | return err; |
| 920 | } |
| 921 | return 0; |
| 922 | } |
| 923 | |
| 924 | static int bpf_object__init_user_maps(struct bpf_object *obj, bool strict) |
| 925 | { |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 926 | Elf_Data *symbols = obj->efile.symbols; |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 927 | int i, map_def_sz = 0, nr_maps = 0, nr_syms; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 928 | Elf_Data *data = NULL; |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 929 | Elf_Scn *scn; |
| 930 | |
| 931 | if (obj->efile.maps_shndx < 0) |
| 932 | return 0; |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 933 | |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 934 | if (!symbols) |
| 935 | return -EINVAL; |
| 936 | |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 937 | scn = elf_getscn(obj->efile.elf, obj->efile.maps_shndx); |
| 938 | if (scn) |
| 939 | data = elf_getdata(scn, NULL); |
| 940 | if (!scn || !data) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 941 | pr_warn("failed to get Elf_Data from map section %d\n", |
| 942 | obj->efile.maps_shndx); |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 943 | return -EINVAL; |
Wang Nan | 0b3d1ef | 2015-07-01 02:13:58 +0000 | [diff] [blame] | 944 | } |
| 945 | |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 946 | /* |
| 947 | * Count number of maps. Each map has a name. |
| 948 | * Array of maps is not supported: only the first element is |
| 949 | * considered. |
| 950 | * |
| 951 | * TODO: Detect array of map and report error. |
| 952 | */ |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 953 | nr_syms = symbols->d_size / sizeof(GElf_Sym); |
| 954 | for (i = 0; i < nr_syms; i++) { |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 955 | GElf_Sym sym; |
| 956 | |
| 957 | if (!gelf_getsym(symbols, i, &sym)) |
| 958 | continue; |
| 959 | if (sym.st_shndx != obj->efile.maps_shndx) |
| 960 | continue; |
| 961 | nr_maps++; |
| 962 | } |
Craig Gallek | b13c5c1 | 2017-10-05 10:41:57 -0400 | [diff] [blame] | 963 | /* Assume equally sized map definitions */ |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 964 | pr_debug("maps in %s: %d maps in %zd bytes\n", |
| 965 | obj->path, nr_maps, data->d_size); |
Daniel Borkmann | 4f8827d | 2019-04-24 00:45:57 +0200 | [diff] [blame] | 966 | |
Andrii Nakryiko | 98e527af | 2019-11-06 18:08:55 -0800 | [diff] [blame] | 967 | 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] | 968 | 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] | 969 | obj->path, nr_maps, data->d_size); |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 970 | return -EINVAL; |
Craig Gallek | b13c5c1 | 2017-10-05 10:41:57 -0400 | [diff] [blame] | 971 | } |
Andrii Nakryiko | 98e527af | 2019-11-06 18:08:55 -0800 | [diff] [blame] | 972 | map_def_sz = data->d_size / nr_maps; |
Craig Gallek | b13c5c1 | 2017-10-05 10:41:57 -0400 | [diff] [blame] | 973 | |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 974 | /* Fill obj->maps using data in "maps" section. */ |
| 975 | for (i = 0; i < nr_syms; i++) { |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 976 | GElf_Sym sym; |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 977 | const char *map_name; |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 978 | struct bpf_map_def *def; |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 979 | struct bpf_map *map; |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 980 | |
| 981 | if (!gelf_getsym(symbols, i, &sym)) |
| 982 | continue; |
Wang Nan | 666810e | 2016-01-25 09:55:49 +0000 | [diff] [blame] | 983 | if (sym.st_shndx != obj->efile.maps_shndx) |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 984 | continue; |
| 985 | |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 986 | map = bpf_object__add_map(obj); |
| 987 | if (IS_ERR(map)) |
| 988 | return PTR_ERR(map); |
| 989 | |
| 990 | map_name = elf_strptr(obj->efile.elf, obj->efile.strtabidx, |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 991 | sym.st_name); |
Andrii Nakryiko | c51829b | 2019-05-29 10:36:06 -0700 | [diff] [blame] | 992 | if (!map_name) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 993 | pr_warn("failed to get map #%d name sym string for obj %s\n", |
| 994 | i, obj->path); |
Andrii Nakryiko | c51829b | 2019-05-29 10:36:06 -0700 | [diff] [blame] | 995 | return -LIBBPF_ERRNO__FORMAT; |
| 996 | } |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 997 | |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 998 | map->libbpf_type = LIBBPF_MAP_UNSPEC; |
Andrii Nakryiko | db48814 | 2019-06-17 12:26:54 -0700 | [diff] [blame] | 999 | map->sec_idx = sym.st_shndx; |
| 1000 | map->sec_offset = sym.st_value; |
| 1001 | pr_debug("map '%s' (legacy): at sec_idx %d, offset %zu.\n", |
| 1002 | map_name, map->sec_idx, map->sec_offset); |
Craig Gallek | b13c5c1 | 2017-10-05 10:41:57 -0400 | [diff] [blame] | 1003 | if (sym.st_value + map_def_sz > data->d_size) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1004 | pr_warn("corrupted maps section in %s: last map \"%s\" too small\n", |
| 1005 | obj->path, map_name); |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 1006 | return -EINVAL; |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 1007 | } |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 1008 | |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1009 | map->name = strdup(map_name); |
| 1010 | if (!map->name) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1011 | pr_warn("failed to alloc map name\n"); |
Wang Nan | 973170e | 2015-12-08 02:25:29 +0000 | [diff] [blame] | 1012 | return -ENOMEM; |
| 1013 | } |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1014 | pr_debug("map %d is \"%s\"\n", i, map->name); |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 1015 | def = (struct bpf_map_def *)(data->d_buf + sym.st_value); |
Craig Gallek | b13c5c1 | 2017-10-05 10:41:57 -0400 | [diff] [blame] | 1016 | /* |
| 1017 | * If the definition of the map in the object file fits in |
| 1018 | * bpf_map_def, copy it. Any extra fields in our version |
| 1019 | * of bpf_map_def will default to zero as a result of the |
| 1020 | * calloc above. |
| 1021 | */ |
| 1022 | if (map_def_sz <= sizeof(struct bpf_map_def)) { |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1023 | memcpy(&map->def, def, map_def_sz); |
Craig Gallek | b13c5c1 | 2017-10-05 10:41:57 -0400 | [diff] [blame] | 1024 | } else { |
| 1025 | /* |
| 1026 | * Here the map structure being read is bigger than what |
| 1027 | * we expect, truncate if the excess bits are all zero. |
| 1028 | * If they are not zero, reject this map as |
| 1029 | * incompatible. |
| 1030 | */ |
| 1031 | char *b; |
Andrii Nakryiko | 8983b73 | 2019-11-20 23:07:42 -0800 | [diff] [blame] | 1032 | |
Craig Gallek | b13c5c1 | 2017-10-05 10:41:57 -0400 | [diff] [blame] | 1033 | for (b = ((char *)def) + sizeof(struct bpf_map_def); |
| 1034 | b < ((char *)def) + map_def_sz; b++) { |
| 1035 | if (*b != 0) { |
Andrii Nakryiko | 8983b73 | 2019-11-20 23:07:42 -0800 | [diff] [blame] | 1036 | 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] | 1037 | obj->path, map_name); |
John Fastabend | c034a17 | 2018-10-15 11:19:55 -0700 | [diff] [blame] | 1038 | if (strict) |
| 1039 | return -EINVAL; |
Craig Gallek | b13c5c1 | 2017-10-05 10:41:57 -0400 | [diff] [blame] | 1040 | } |
| 1041 | } |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1042 | memcpy(&map->def, def, sizeof(struct bpf_map_def)); |
Craig Gallek | b13c5c1 | 2017-10-05 10:41:57 -0400 | [diff] [blame] | 1043 | } |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 1044 | } |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1045 | return 0; |
| 1046 | } |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 1047 | |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 1048 | static const struct btf_type * |
| 1049 | 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] | 1050 | { |
| 1051 | const struct btf_type *t = btf__type_by_id(btf, id); |
| 1052 | |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 1053 | if (res_id) |
| 1054 | *res_id = id; |
| 1055 | |
| 1056 | while (btf_is_mod(t) || btf_is_typedef(t)) { |
| 1057 | if (res_id) |
| 1058 | *res_id = t->type; |
| 1059 | t = btf__type_by_id(btf, t->type); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1060 | } |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 1061 | |
| 1062 | return t; |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Andrii Nakryiko | ef99b02 | 2019-07-05 08:50:09 -0700 | [diff] [blame] | 1065 | /* |
| 1066 | * Fetch integer attribute of BTF map definition. Such attributes are |
| 1067 | * represented using a pointer to an array, in which dimensionality of array |
| 1068 | * encodes specified integer value. E.g., int (*type)[BPF_MAP_TYPE_ARRAY]; |
| 1069 | * encodes `type => BPF_MAP_TYPE_ARRAY` key/value pair completely using BTF |
| 1070 | * type definition, while using only sizeof(void *) space in ELF data section. |
| 1071 | */ |
| 1072 | 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] | 1073 | const struct btf_type *def, |
Andrii Nakryiko | 8983b73 | 2019-11-20 23:07:42 -0800 | [diff] [blame] | 1074 | const struct btf_member *m, __u32 *res) |
| 1075 | { |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 1076 | 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] | 1077 | const char *name = btf__name_by_offset(btf, m->name_off); |
Andrii Nakryiko | ef99b02 | 2019-07-05 08:50:09 -0700 | [diff] [blame] | 1078 | const struct btf_array *arr_info; |
| 1079 | const struct btf_type *arr_t; |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1080 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1081 | if (!btf_is_ptr(t)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1082 | pr_warn("map '%s': attr '%s': expected PTR, got %u.\n", |
| 1083 | map_name, name, btf_kind(t)); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1084 | return false; |
| 1085 | } |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 1086 | |
Andrii Nakryiko | ef99b02 | 2019-07-05 08:50:09 -0700 | [diff] [blame] | 1087 | arr_t = btf__type_by_id(btf, t->type); |
| 1088 | if (!arr_t) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1089 | pr_warn("map '%s': attr '%s': type [%u] not found.\n", |
| 1090 | map_name, name, t->type); |
Andrii Nakryiko | ef99b02 | 2019-07-05 08:50:09 -0700 | [diff] [blame] | 1091 | return false; |
| 1092 | } |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1093 | if (!btf_is_array(arr_t)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1094 | pr_warn("map '%s': attr '%s': expected ARRAY, got %u.\n", |
| 1095 | map_name, name, btf_kind(arr_t)); |
Andrii Nakryiko | ef99b02 | 2019-07-05 08:50:09 -0700 | [diff] [blame] | 1096 | return false; |
| 1097 | } |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1098 | arr_info = btf_array(arr_t); |
Andrii Nakryiko | ef99b02 | 2019-07-05 08:50:09 -0700 | [diff] [blame] | 1099 | *res = arr_info->nelems; |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1100 | return true; |
| 1101 | } |
Daniel Borkmann | 8837fe5 | 2019-04-24 00:45:56 +0200 | [diff] [blame] | 1102 | |
Toke Høiland-Jørgensen | 57a00f4 | 2019-11-02 12:09:41 +0100 | [diff] [blame] | 1103 | static int build_map_pin_path(struct bpf_map *map, const char *path) |
| 1104 | { |
| 1105 | char buf[PATH_MAX]; |
| 1106 | int err, len; |
| 1107 | |
| 1108 | if (!path) |
| 1109 | path = "/sys/fs/bpf"; |
| 1110 | |
| 1111 | len = snprintf(buf, PATH_MAX, "%s/%s", path, bpf_map__name(map)); |
| 1112 | if (len < 0) |
| 1113 | return -EINVAL; |
| 1114 | else if (len >= PATH_MAX) |
| 1115 | return -ENAMETOOLONG; |
| 1116 | |
| 1117 | err = bpf_map__set_pin_path(map, buf); |
| 1118 | if (err) |
| 1119 | return err; |
| 1120 | |
| 1121 | return 0; |
| 1122 | } |
| 1123 | |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1124 | static int bpf_object__init_user_btf_map(struct bpf_object *obj, |
| 1125 | const struct btf_type *sec, |
| 1126 | int var_idx, int sec_idx, |
Toke Høiland-Jørgensen | 57a00f4 | 2019-11-02 12:09:41 +0100 | [diff] [blame] | 1127 | const Elf_Data *data, bool strict, |
| 1128 | const char *pin_root_path) |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1129 | { |
| 1130 | const struct btf_type *var, *def, *t; |
| 1131 | const struct btf_var_secinfo *vi; |
| 1132 | const struct btf_var *var_extra; |
| 1133 | const struct btf_member *m; |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1134 | const char *map_name; |
| 1135 | struct bpf_map *map; |
| 1136 | int vlen, i; |
| 1137 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1138 | vi = btf_var_secinfos(sec) + var_idx; |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1139 | var = btf__type_by_id(obj->btf, vi->type); |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1140 | var_extra = btf_var(var); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1141 | map_name = btf__name_by_offset(obj->btf, var->name_off); |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1142 | vlen = btf_vlen(var); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1143 | |
| 1144 | if (map_name == NULL || map_name[0] == '\0') { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1145 | pr_warn("map #%d: empty name.\n", var_idx); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1146 | return -EINVAL; |
| 1147 | } |
| 1148 | if ((__u64)vi->offset + vi->size > data->d_size) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1149 | pr_warn("map '%s' BTF data is corrupted.\n", map_name); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1150 | return -EINVAL; |
| 1151 | } |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1152 | if (!btf_is_var(var)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1153 | pr_warn("map '%s': unexpected var kind %u.\n", |
| 1154 | map_name, btf_kind(var)); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1155 | return -EINVAL; |
| 1156 | } |
| 1157 | if (var_extra->linkage != BTF_VAR_GLOBAL_ALLOCATED && |
| 1158 | var_extra->linkage != BTF_VAR_STATIC) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1159 | pr_warn("map '%s': unsupported var linkage %u.\n", |
| 1160 | map_name, var_extra->linkage); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1161 | return -EOPNOTSUPP; |
| 1162 | } |
| 1163 | |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 1164 | def = skip_mods_and_typedefs(obj->btf, var->type, NULL); |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1165 | if (!btf_is_struct(def)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1166 | pr_warn("map '%s': unexpected def kind %u.\n", |
| 1167 | map_name, btf_kind(var)); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1168 | return -EINVAL; |
| 1169 | } |
| 1170 | if (def->size > vi->size) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1171 | pr_warn("map '%s': invalid def size.\n", map_name); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1172 | return -EINVAL; |
| 1173 | } |
| 1174 | |
| 1175 | map = bpf_object__add_map(obj); |
| 1176 | if (IS_ERR(map)) |
| 1177 | return PTR_ERR(map); |
| 1178 | map->name = strdup(map_name); |
| 1179 | if (!map->name) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1180 | pr_warn("map '%s': failed to alloc map name.\n", map_name); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1181 | return -ENOMEM; |
| 1182 | } |
| 1183 | map->libbpf_type = LIBBPF_MAP_UNSPEC; |
| 1184 | map->def.type = BPF_MAP_TYPE_UNSPEC; |
| 1185 | map->sec_idx = sec_idx; |
| 1186 | map->sec_offset = vi->offset; |
| 1187 | pr_debug("map '%s': at sec_idx %d, offset %zu.\n", |
| 1188 | map_name, map->sec_idx, map->sec_offset); |
| 1189 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1190 | vlen = btf_vlen(def); |
| 1191 | m = btf_members(def); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1192 | for (i = 0; i < vlen; i++, m++) { |
| 1193 | const char *name = btf__name_by_offset(obj->btf, m->name_off); |
| 1194 | |
| 1195 | if (!name) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1196 | pr_warn("map '%s': invalid field #%d.\n", map_name, i); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1197 | return -EINVAL; |
| 1198 | } |
| 1199 | if (strcmp(name, "type") == 0) { |
| 1200 | if (!get_map_field_int(map_name, obj->btf, def, m, |
Andrii Nakryiko | ef99b02 | 2019-07-05 08:50:09 -0700 | [diff] [blame] | 1201 | &map->def.type)) |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1202 | return -EINVAL; |
| 1203 | pr_debug("map '%s': found type = %u.\n", |
| 1204 | map_name, map->def.type); |
| 1205 | } else if (strcmp(name, "max_entries") == 0) { |
| 1206 | if (!get_map_field_int(map_name, obj->btf, def, m, |
Andrii Nakryiko | ef99b02 | 2019-07-05 08:50:09 -0700 | [diff] [blame] | 1207 | &map->def.max_entries)) |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1208 | return -EINVAL; |
| 1209 | pr_debug("map '%s': found max_entries = %u.\n", |
| 1210 | map_name, map->def.max_entries); |
| 1211 | } else if (strcmp(name, "map_flags") == 0) { |
| 1212 | if (!get_map_field_int(map_name, obj->btf, def, m, |
Andrii Nakryiko | ef99b02 | 2019-07-05 08:50:09 -0700 | [diff] [blame] | 1213 | &map->def.map_flags)) |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1214 | return -EINVAL; |
| 1215 | pr_debug("map '%s': found map_flags = %u.\n", |
| 1216 | map_name, map->def.map_flags); |
| 1217 | } else if (strcmp(name, "key_size") == 0) { |
| 1218 | __u32 sz; |
| 1219 | |
| 1220 | if (!get_map_field_int(map_name, obj->btf, def, m, |
Andrii Nakryiko | ef99b02 | 2019-07-05 08:50:09 -0700 | [diff] [blame] | 1221 | &sz)) |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1222 | return -EINVAL; |
| 1223 | pr_debug("map '%s': found key_size = %u.\n", |
| 1224 | map_name, sz); |
| 1225 | if (map->def.key_size && map->def.key_size != sz) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1226 | pr_warn("map '%s': conflicting key size %u != %u.\n", |
| 1227 | map_name, map->def.key_size, sz); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1228 | return -EINVAL; |
| 1229 | } |
| 1230 | map->def.key_size = sz; |
| 1231 | } else if (strcmp(name, "key") == 0) { |
| 1232 | __s64 sz; |
| 1233 | |
| 1234 | t = btf__type_by_id(obj->btf, m->type); |
| 1235 | if (!t) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1236 | pr_warn("map '%s': key type [%d] not found.\n", |
| 1237 | map_name, m->type); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1238 | return -EINVAL; |
| 1239 | } |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1240 | if (!btf_is_ptr(t)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1241 | pr_warn("map '%s': key spec is not PTR: %u.\n", |
| 1242 | map_name, btf_kind(t)); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1243 | return -EINVAL; |
| 1244 | } |
| 1245 | sz = btf__resolve_size(obj->btf, t->type); |
| 1246 | if (sz < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1247 | pr_warn("map '%s': can't determine key size for type [%u]: %lld.\n", |
| 1248 | map_name, t->type, sz); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1249 | return sz; |
| 1250 | } |
| 1251 | pr_debug("map '%s': found key [%u], sz = %lld.\n", |
| 1252 | map_name, t->type, sz); |
| 1253 | if (map->def.key_size && map->def.key_size != sz) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1254 | pr_warn("map '%s': conflicting key size %u != %lld.\n", |
| 1255 | map_name, map->def.key_size, sz); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1256 | return -EINVAL; |
| 1257 | } |
| 1258 | map->def.key_size = sz; |
| 1259 | map->btf_key_type_id = t->type; |
| 1260 | } else if (strcmp(name, "value_size") == 0) { |
| 1261 | __u32 sz; |
| 1262 | |
| 1263 | if (!get_map_field_int(map_name, obj->btf, def, m, |
Andrii Nakryiko | ef99b02 | 2019-07-05 08:50:09 -0700 | [diff] [blame] | 1264 | &sz)) |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1265 | return -EINVAL; |
| 1266 | pr_debug("map '%s': found value_size = %u.\n", |
| 1267 | map_name, sz); |
| 1268 | if (map->def.value_size && map->def.value_size != sz) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1269 | pr_warn("map '%s': conflicting value size %u != %u.\n", |
| 1270 | map_name, map->def.value_size, sz); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1271 | return -EINVAL; |
| 1272 | } |
| 1273 | map->def.value_size = sz; |
| 1274 | } else if (strcmp(name, "value") == 0) { |
| 1275 | __s64 sz; |
| 1276 | |
| 1277 | t = btf__type_by_id(obj->btf, m->type); |
| 1278 | if (!t) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1279 | pr_warn("map '%s': value type [%d] not found.\n", |
| 1280 | map_name, m->type); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1281 | return -EINVAL; |
| 1282 | } |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1283 | if (!btf_is_ptr(t)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1284 | pr_warn("map '%s': value spec is not PTR: %u.\n", |
| 1285 | map_name, btf_kind(t)); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1286 | return -EINVAL; |
| 1287 | } |
| 1288 | sz = btf__resolve_size(obj->btf, t->type); |
| 1289 | if (sz < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1290 | pr_warn("map '%s': can't determine value size for type [%u]: %lld.\n", |
| 1291 | map_name, t->type, sz); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1292 | return sz; |
| 1293 | } |
| 1294 | pr_debug("map '%s': found value [%u], sz = %lld.\n", |
| 1295 | map_name, t->type, sz); |
| 1296 | if (map->def.value_size && map->def.value_size != sz) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1297 | pr_warn("map '%s': conflicting value size %u != %lld.\n", |
| 1298 | map_name, map->def.value_size, sz); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1299 | return -EINVAL; |
| 1300 | } |
| 1301 | map->def.value_size = sz; |
| 1302 | map->btf_value_type_id = t->type; |
Toke Høiland-Jørgensen | 57a00f4 | 2019-11-02 12:09:41 +0100 | [diff] [blame] | 1303 | } else if (strcmp(name, "pinning") == 0) { |
| 1304 | __u32 val; |
| 1305 | int err; |
| 1306 | |
| 1307 | if (!get_map_field_int(map_name, obj->btf, def, m, |
| 1308 | &val)) |
| 1309 | return -EINVAL; |
| 1310 | pr_debug("map '%s': found pinning = %u.\n", |
| 1311 | map_name, val); |
| 1312 | |
| 1313 | if (val != LIBBPF_PIN_NONE && |
| 1314 | val != LIBBPF_PIN_BY_NAME) { |
| 1315 | pr_warn("map '%s': invalid pinning value %u.\n", |
| 1316 | map_name, val); |
| 1317 | return -EINVAL; |
| 1318 | } |
| 1319 | if (val == LIBBPF_PIN_BY_NAME) { |
| 1320 | err = build_map_pin_path(map, pin_root_path); |
| 1321 | if (err) { |
| 1322 | pr_warn("map '%s': couldn't build pin path.\n", |
| 1323 | map_name); |
| 1324 | return err; |
| 1325 | } |
| 1326 | } |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1327 | } else { |
| 1328 | if (strict) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1329 | pr_warn("map '%s': unknown field '%s'.\n", |
| 1330 | map_name, name); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1331 | return -ENOTSUP; |
| 1332 | } |
| 1333 | pr_debug("map '%s': ignoring unknown field '%s'.\n", |
| 1334 | map_name, name); |
| 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | if (map->def.type == BPF_MAP_TYPE_UNSPEC) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1339 | pr_warn("map '%s': map type isn't specified.\n", map_name); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1340 | return -EINVAL; |
| 1341 | } |
| 1342 | |
| 1343 | return 0; |
| 1344 | } |
| 1345 | |
Toke Høiland-Jørgensen | 57a00f4 | 2019-11-02 12:09:41 +0100 | [diff] [blame] | 1346 | static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict, |
| 1347 | const char *pin_root_path) |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1348 | { |
| 1349 | const struct btf_type *sec = NULL; |
| 1350 | int nr_types, i, vlen, err; |
| 1351 | const struct btf_type *t; |
| 1352 | const char *name; |
| 1353 | Elf_Data *data; |
| 1354 | Elf_Scn *scn; |
| 1355 | |
| 1356 | if (obj->efile.btf_maps_shndx < 0) |
| 1357 | return 0; |
| 1358 | |
| 1359 | scn = elf_getscn(obj->efile.elf, obj->efile.btf_maps_shndx); |
| 1360 | if (scn) |
| 1361 | data = elf_getdata(scn, NULL); |
| 1362 | if (!scn || !data) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1363 | pr_warn("failed to get Elf_Data from map section %d (%s)\n", |
| 1364 | obj->efile.maps_shndx, MAPS_ELF_SEC); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1365 | return -EINVAL; |
| 1366 | } |
| 1367 | |
| 1368 | nr_types = btf__get_nr_types(obj->btf); |
| 1369 | for (i = 1; i <= nr_types; i++) { |
| 1370 | t = btf__type_by_id(obj->btf, i); |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1371 | if (!btf_is_datasec(t)) |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1372 | continue; |
| 1373 | name = btf__name_by_offset(obj->btf, t->name_off); |
| 1374 | if (strcmp(name, MAPS_ELF_SEC) == 0) { |
| 1375 | sec = t; |
| 1376 | break; |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | if (!sec) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1381 | pr_warn("DATASEC '%s' not found.\n", MAPS_ELF_SEC); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1382 | return -ENOENT; |
| 1383 | } |
| 1384 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1385 | vlen = btf_vlen(sec); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1386 | for (i = 0; i < vlen; i++) { |
| 1387 | err = bpf_object__init_user_btf_map(obj, sec, i, |
| 1388 | obj->efile.btf_maps_shndx, |
Andrii Nakryiko | 8983b73 | 2019-11-20 23:07:42 -0800 | [diff] [blame] | 1389 | data, strict, |
| 1390 | pin_root_path); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1391 | if (err) |
| 1392 | return err; |
| 1393 | } |
| 1394 | |
| 1395 | return 0; |
| 1396 | } |
| 1397 | |
Toke Høiland-Jørgensen | 57a00f4 | 2019-11-02 12:09:41 +0100 | [diff] [blame] | 1398 | static int bpf_object__init_maps(struct bpf_object *obj, bool relaxed_maps, |
| 1399 | const char *pin_root_path) |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1400 | { |
Andrii Nakryiko | 291ee02 | 2019-10-15 11:28:46 -0700 | [diff] [blame] | 1401 | bool strict = !relaxed_maps; |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1402 | int err; |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 1403 | |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1404 | err = bpf_object__init_user_maps(obj, strict); |
| 1405 | if (err) |
| 1406 | return err; |
| 1407 | |
Toke Høiland-Jørgensen | 57a00f4 | 2019-11-02 12:09:41 +0100 | [diff] [blame] | 1408 | err = bpf_object__init_user_btf_maps(obj, strict, pin_root_path); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1409 | if (err) |
| 1410 | return err; |
| 1411 | |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1412 | err = bpf_object__init_global_data_maps(obj); |
| 1413 | if (err) |
| 1414 | return err; |
| 1415 | |
| 1416 | if (obj->nr_maps) { |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 1417 | qsort(obj->maps, obj->nr_maps, sizeof(obj->maps[0]), |
| 1418 | compare_bpf_map); |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1419 | } |
| 1420 | return 0; |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
Jesper Dangaard Brouer | e3d91b0 | 2018-02-08 12:48:32 +0100 | [diff] [blame] | 1423 | static bool section_have_execinstr(struct bpf_object *obj, int idx) |
| 1424 | { |
| 1425 | Elf_Scn *scn; |
| 1426 | GElf_Shdr sh; |
| 1427 | |
| 1428 | scn = elf_getscn(obj->efile.elf, idx); |
| 1429 | if (!scn) |
| 1430 | return false; |
| 1431 | |
| 1432 | if (gelf_getshdr(scn, &sh) != &sh) |
| 1433 | return false; |
| 1434 | |
| 1435 | if (sh.sh_flags & SHF_EXECINSTR) |
| 1436 | return true; |
| 1437 | |
| 1438 | return false; |
| 1439 | } |
| 1440 | |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 1441 | static void bpf_object__sanitize_btf(struct bpf_object *obj) |
| 1442 | { |
| 1443 | bool has_datasec = obj->caps.btf_datasec; |
| 1444 | bool has_func = obj->caps.btf_func; |
| 1445 | struct btf *btf = obj->btf; |
| 1446 | struct btf_type *t; |
| 1447 | int i, j, vlen; |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 1448 | |
| 1449 | if (!obj->btf || (has_func && has_datasec)) |
| 1450 | return; |
| 1451 | |
| 1452 | for (i = 1; i <= btf__get_nr_types(btf); i++) { |
| 1453 | t = (struct btf_type *)btf__type_by_id(btf, i); |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 1454 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1455 | if (!has_datasec && btf_is_var(t)) { |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 1456 | /* replace VAR with INT */ |
| 1457 | t->info = BTF_INFO_ENC(BTF_KIND_INT, 0, 0); |
Andrii Nakryiko | 1d4126c | 2019-07-19 12:46:03 -0700 | [diff] [blame] | 1458 | /* |
| 1459 | * using size = 1 is the safest choice, 4 will be too |
| 1460 | * big and cause kernel BTF validation failure if |
| 1461 | * original variable took less than 4 bytes |
| 1462 | */ |
| 1463 | t->size = 1; |
Jakub Kicinski | 708852d | 2019-08-13 16:24:57 -0700 | [diff] [blame] | 1464 | *(int *)(t + 1) = BTF_INT_ENC(0, 0, 8); |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1465 | } else if (!has_datasec && btf_is_datasec(t)) { |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 1466 | /* replace DATASEC with STRUCT */ |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1467 | const struct btf_var_secinfo *v = btf_var_secinfos(t); |
| 1468 | struct btf_member *m = btf_members(t); |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 1469 | struct btf_type *vt; |
| 1470 | char *name; |
| 1471 | |
| 1472 | name = (char *)btf__name_by_offset(btf, t->name_off); |
| 1473 | while (*name) { |
| 1474 | if (*name == '.') |
| 1475 | *name = '_'; |
| 1476 | name++; |
| 1477 | } |
| 1478 | |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1479 | vlen = btf_vlen(t); |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 1480 | t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, vlen); |
| 1481 | for (j = 0; j < vlen; j++, v++, m++) { |
| 1482 | /* order of field assignments is important */ |
| 1483 | m->offset = v->offset * 8; |
| 1484 | m->type = v->type; |
| 1485 | /* preserve variable name as member name */ |
| 1486 | vt = (void *)btf__type_by_id(btf, v->type); |
| 1487 | m->name_off = vt->name_off; |
| 1488 | } |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1489 | } else if (!has_func && btf_is_func_proto(t)) { |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 1490 | /* replace FUNC_PROTO with ENUM */ |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1491 | vlen = btf_vlen(t); |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 1492 | t->info = BTF_INFO_ENC(BTF_KIND_ENUM, 0, vlen); |
| 1493 | t->size = sizeof(__u32); /* kernel enforced */ |
Andrii Nakryiko | b03bc68 | 2019-08-07 14:39:49 -0700 | [diff] [blame] | 1494 | } else if (!has_func && btf_is_func(t)) { |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 1495 | /* replace FUNC with TYPEDEF */ |
| 1496 | t->info = BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0); |
| 1497 | } |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | static void bpf_object__sanitize_btf_ext(struct bpf_object *obj) |
| 1502 | { |
| 1503 | if (!obj->btf_ext) |
| 1504 | return; |
| 1505 | |
| 1506 | if (!obj->caps.btf_func) { |
| 1507 | btf_ext__free(obj->btf_ext); |
| 1508 | obj->btf_ext = NULL; |
| 1509 | } |
| 1510 | } |
| 1511 | |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1512 | static bool bpf_object__is_btf_mandatory(const struct bpf_object *obj) |
| 1513 | { |
| 1514 | return obj->efile.btf_maps_shndx >= 0; |
| 1515 | } |
| 1516 | |
Andrii Nakryiko | 063183b | 2019-06-17 12:26:55 -0700 | [diff] [blame] | 1517 | static int bpf_object__init_btf(struct bpf_object *obj, |
Andrii Nakryiko | 9c6660d | 2019-06-17 12:26:51 -0700 | [diff] [blame] | 1518 | Elf_Data *btf_data, |
| 1519 | Elf_Data *btf_ext_data) |
| 1520 | { |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1521 | bool btf_required = bpf_object__is_btf_mandatory(obj); |
Andrii Nakryiko | 9c6660d | 2019-06-17 12:26:51 -0700 | [diff] [blame] | 1522 | int err = 0; |
| 1523 | |
| 1524 | if (btf_data) { |
| 1525 | obj->btf = btf__new(btf_data->d_buf, btf_data->d_size); |
| 1526 | if (IS_ERR(obj->btf)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1527 | pr_warn("Error loading ELF section %s: %d.\n", |
| 1528 | BTF_ELF_SEC, err); |
Andrii Nakryiko | 9c6660d | 2019-06-17 12:26:51 -0700 | [diff] [blame] | 1529 | goto out; |
| 1530 | } |
| 1531 | err = btf__finalize_data(obj, obj->btf); |
| 1532 | if (err) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1533 | pr_warn("Error finalizing %s: %d.\n", BTF_ELF_SEC, err); |
Andrii Nakryiko | 9c6660d | 2019-06-17 12:26:51 -0700 | [diff] [blame] | 1534 | goto out; |
| 1535 | } |
Andrii Nakryiko | 9c6660d | 2019-06-17 12:26:51 -0700 | [diff] [blame] | 1536 | } |
| 1537 | if (btf_ext_data) { |
| 1538 | if (!obj->btf) { |
| 1539 | pr_debug("Ignore ELF section %s because its depending ELF section %s is not found.\n", |
| 1540 | BTF_EXT_ELF_SEC, BTF_ELF_SEC); |
| 1541 | goto out; |
| 1542 | } |
| 1543 | obj->btf_ext = btf_ext__new(btf_ext_data->d_buf, |
| 1544 | btf_ext_data->d_size); |
| 1545 | if (IS_ERR(obj->btf_ext)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1546 | pr_warn("Error loading ELF section %s: %ld. Ignored and continue.\n", |
| 1547 | BTF_EXT_ELF_SEC, PTR_ERR(obj->btf_ext)); |
Andrii Nakryiko | 9c6660d | 2019-06-17 12:26:51 -0700 | [diff] [blame] | 1548 | obj->btf_ext = NULL; |
| 1549 | goto out; |
| 1550 | } |
Andrii Nakryiko | 9c6660d | 2019-06-17 12:26:51 -0700 | [diff] [blame] | 1551 | } |
| 1552 | out: |
| 1553 | if (err || IS_ERR(obj->btf)) { |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1554 | if (btf_required) |
| 1555 | err = err ? : PTR_ERR(obj->btf); |
| 1556 | else |
| 1557 | err = 0; |
Andrii Nakryiko | 9c6660d | 2019-06-17 12:26:51 -0700 | [diff] [blame] | 1558 | if (!IS_ERR_OR_NULL(obj->btf)) |
| 1559 | btf__free(obj->btf); |
| 1560 | obj->btf = NULL; |
| 1561 | } |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1562 | if (btf_required && !obj->btf) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1563 | pr_warn("BTF is required, but is missing or corrupted.\n"); |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1564 | return err == 0 ? -ENOENT : err; |
| 1565 | } |
Andrii Nakryiko | 9c6660d | 2019-06-17 12:26:51 -0700 | [diff] [blame] | 1566 | return 0; |
| 1567 | } |
| 1568 | |
Andrii Nakryiko | 063183b | 2019-06-17 12:26:55 -0700 | [diff] [blame] | 1569 | static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj) |
| 1570 | { |
| 1571 | int err = 0; |
| 1572 | |
| 1573 | if (!obj->btf) |
| 1574 | return 0; |
| 1575 | |
| 1576 | bpf_object__sanitize_btf(obj); |
| 1577 | bpf_object__sanitize_btf_ext(obj); |
| 1578 | |
| 1579 | err = btf__load(obj->btf); |
| 1580 | if (err) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1581 | pr_warn("Error loading %s into kernel: %d.\n", |
| 1582 | BTF_ELF_SEC, err); |
Andrii Nakryiko | 063183b | 2019-06-17 12:26:55 -0700 | [diff] [blame] | 1583 | btf__free(obj->btf); |
| 1584 | obj->btf = NULL; |
Andrii Nakryiko | 04efe59 | 2019-07-19 12:32:42 -0700 | [diff] [blame] | 1585 | /* btf_ext can't exist without btf, so free it as well */ |
| 1586 | if (obj->btf_ext) { |
| 1587 | btf_ext__free(obj->btf_ext); |
| 1588 | obj->btf_ext = NULL; |
| 1589 | } |
| 1590 | |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1591 | if (bpf_object__is_btf_mandatory(obj)) |
| 1592 | return err; |
Andrii Nakryiko | 063183b | 2019-06-17 12:26:55 -0700 | [diff] [blame] | 1593 | } |
| 1594 | return 0; |
| 1595 | } |
| 1596 | |
Toke Høiland-Jørgensen | 57a00f4 | 2019-11-02 12:09:41 +0100 | [diff] [blame] | 1597 | static int bpf_object__elf_collect(struct bpf_object *obj, bool relaxed_maps, |
| 1598 | const char *pin_root_path) |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 1599 | { |
| 1600 | Elf *elf = obj->efile.elf; |
| 1601 | GElf_Ehdr *ep = &obj->efile.ehdr; |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 1602 | Elf_Data *btf_ext_data = NULL; |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 1603 | Elf_Data *btf_data = NULL; |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 1604 | Elf_Scn *scn = NULL; |
Wang Nan | 666810e | 2016-01-25 09:55:49 +0000 | [diff] [blame] | 1605 | int idx = 0, err = 0; |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 1606 | |
| 1607 | /* Elf is corrupted/truncated, avoid calling elf_strptr. */ |
| 1608 | if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1609 | pr_warn("failed to get e_shstrndx from %s\n", obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1610 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | while ((scn = elf_nextscn(elf, scn)) != NULL) { |
| 1614 | char *name; |
| 1615 | GElf_Shdr sh; |
| 1616 | Elf_Data *data; |
| 1617 | |
| 1618 | idx++; |
| 1619 | if (gelf_getshdr(scn, &sh) != &sh) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1620 | pr_warn("failed to get section(%d) header from %s\n", |
| 1621 | idx, obj->path); |
Andrii Nakryiko | 01b29d1 | 2019-06-17 12:26:52 -0700 | [diff] [blame] | 1622 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 1623 | } |
| 1624 | |
| 1625 | name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name); |
| 1626 | if (!name) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1627 | pr_warn("failed to get section(%d) name from %s\n", |
| 1628 | idx, obj->path); |
Andrii Nakryiko | 01b29d1 | 2019-06-17 12:26:52 -0700 | [diff] [blame] | 1629 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | data = elf_getdata(scn, 0); |
| 1633 | if (!data) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1634 | pr_warn("failed to get section(%d) data from %s(%s)\n", |
| 1635 | idx, name, obj->path); |
Andrii Nakryiko | 01b29d1 | 2019-06-17 12:26:52 -0700 | [diff] [blame] | 1636 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 1637 | } |
Jesper Dangaard Brouer | 077c066 | 2018-02-08 12:48:17 +0100 | [diff] [blame] | 1638 | pr_debug("section(%d) %s, size %ld, link %d, flags %lx, type=%d\n", |
| 1639 | idx, name, (unsigned long)data->d_size, |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 1640 | (int)sh.sh_link, (unsigned long)sh.sh_flags, |
| 1641 | (int)sh.sh_type); |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 1642 | |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 1643 | if (strcmp(name, "license") == 0) { |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 1644 | err = bpf_object__init_license(obj, |
| 1645 | data->d_buf, |
| 1646 | data->d_size); |
Andrii Nakryiko | 01b29d1 | 2019-06-17 12:26:52 -0700 | [diff] [blame] | 1647 | if (err) |
| 1648 | return err; |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 1649 | } else if (strcmp(name, "version") == 0) { |
John Fastabend | 54b8625 | 2019-10-18 07:41:26 -0700 | [diff] [blame] | 1650 | err = bpf_object__init_kversion(obj, |
| 1651 | data->d_buf, |
| 1652 | data->d_size); |
| 1653 | if (err) |
| 1654 | return err; |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 1655 | } else if (strcmp(name, "maps") == 0) { |
Wang Nan | 666810e | 2016-01-25 09:55:49 +0000 | [diff] [blame] | 1656 | obj->efile.maps_shndx = idx; |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1657 | } else if (strcmp(name, MAPS_ELF_SEC) == 0) { |
| 1658 | obj->efile.btf_maps_shndx = idx; |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 1659 | } else if (strcmp(name, BTF_ELF_SEC) == 0) { |
| 1660 | btf_data = data; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 1661 | } else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) { |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 1662 | btf_ext_data = data; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 1663 | } else if (sh.sh_type == SHT_SYMTAB) { |
Wang Nan | bec7d68 | 2015-07-01 02:13:59 +0000 | [diff] [blame] | 1664 | if (obj->efile.symbols) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1665 | pr_warn("bpf: multiple SYMTAB in %s\n", |
| 1666 | obj->path); |
Andrii Nakryiko | 01b29d1 | 2019-06-17 12:26:52 -0700 | [diff] [blame] | 1667 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 77ba9a5 | 2015-12-08 02:25:30 +0000 | [diff] [blame] | 1668 | } |
Andrii Nakryiko | 01b29d1 | 2019-06-17 12:26:52 -0700 | [diff] [blame] | 1669 | obj->efile.symbols = data; |
| 1670 | obj->efile.strtabidx = sh.sh_link; |
Joe Stringer | f8c7a4d | 2019-04-09 23:20:12 +0200 | [diff] [blame] | 1671 | } else if (sh.sh_type == SHT_PROGBITS && data->d_size > 0) { |
| 1672 | if (sh.sh_flags & SHF_EXECINSTR) { |
| 1673 | if (strcmp(name, ".text") == 0) |
| 1674 | obj->efile.text_shndx = idx; |
| 1675 | err = bpf_object__add_program(obj, data->d_buf, |
Andrii Nakryiko | 8983b73 | 2019-11-20 23:07:42 -0800 | [diff] [blame] | 1676 | data->d_size, |
| 1677 | name, idx); |
Joe Stringer | f8c7a4d | 2019-04-09 23:20:12 +0200 | [diff] [blame] | 1678 | if (err) { |
| 1679 | char errmsg[STRERR_BUFSIZE]; |
Andrii Nakryiko | 8983b73 | 2019-11-20 23:07:42 -0800 | [diff] [blame] | 1680 | char *cp; |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1681 | |
Andrii Nakryiko | 8983b73 | 2019-11-20 23:07:42 -0800 | [diff] [blame] | 1682 | cp = libbpf_strerror_r(-err, errmsg, |
| 1683 | sizeof(errmsg)); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1684 | pr_warn("failed to alloc program %s (%s): %s", |
| 1685 | name, obj->path, cp); |
Andrii Nakryiko | 01b29d1 | 2019-06-17 12:26:52 -0700 | [diff] [blame] | 1686 | return err; |
Joe Stringer | f8c7a4d | 2019-04-09 23:20:12 +0200 | [diff] [blame] | 1687 | } |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 1688 | } else if (strcmp(name, ".data") == 0) { |
| 1689 | obj->efile.data = data; |
| 1690 | obj->efile.data_shndx = idx; |
| 1691 | } else if (strcmp(name, ".rodata") == 0) { |
| 1692 | obj->efile.rodata = data; |
| 1693 | obj->efile.rodata_shndx = idx; |
| 1694 | } else { |
| 1695 | pr_debug("skip section(%d) %s\n", idx, name); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 1696 | } |
Wang Nan | b62f06e | 2015-07-01 02:14:01 +0000 | [diff] [blame] | 1697 | } else if (sh.sh_type == SHT_REL) { |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 1698 | int nr_sects = obj->efile.nr_reloc_sects; |
| 1699 | void *sects = obj->efile.reloc_sects; |
Jesper Dangaard Brouer | e3d91b0 | 2018-02-08 12:48:32 +0100 | [diff] [blame] | 1700 | int sec = sh.sh_info; /* points to other section */ |
| 1701 | |
| 1702 | /* Only do relo for section with exec instructions */ |
| 1703 | if (!section_have_execinstr(obj, sec)) { |
| 1704 | pr_debug("skip relo %s(%d) for section(%d)\n", |
| 1705 | name, idx, sec); |
| 1706 | continue; |
| 1707 | } |
Wang Nan | b62f06e | 2015-07-01 02:14:01 +0000 | [diff] [blame] | 1708 | |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 1709 | sects = reallocarray(sects, nr_sects + 1, |
| 1710 | sizeof(*obj->efile.reloc_sects)); |
| 1711 | if (!sects) { |
| 1712 | pr_warn("reloc_sects realloc failed\n"); |
Andrii Nakryiko | 01b29d1 | 2019-06-17 12:26:52 -0700 | [diff] [blame] | 1713 | return -ENOMEM; |
Wang Nan | b62f06e | 2015-07-01 02:14:01 +0000 | [diff] [blame] | 1714 | } |
Andrii Nakryiko | 01b29d1 | 2019-06-17 12:26:52 -0700 | [diff] [blame] | 1715 | |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 1716 | obj->efile.reloc_sects = sects; |
| 1717 | obj->efile.nr_reloc_sects++; |
Andrii Nakryiko | 01b29d1 | 2019-06-17 12:26:52 -0700 | [diff] [blame] | 1718 | |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 1719 | obj->efile.reloc_sects[nr_sects].shdr = sh; |
| 1720 | obj->efile.reloc_sects[nr_sects].data = data; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 1721 | } else if (sh.sh_type == SHT_NOBITS && strcmp(name, ".bss") == 0) { |
| 1722 | obj->efile.bss = data; |
| 1723 | obj->efile.bss_shndx = idx; |
Jesper Dangaard Brouer | 077c066 | 2018-02-08 12:48:17 +0100 | [diff] [blame] | 1724 | } else { |
| 1725 | pr_debug("skip section(%d) %s\n", idx, name); |
Wang Nan | bec7d68 | 2015-07-01 02:13:59 +0000 | [diff] [blame] | 1726 | } |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 1727 | } |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 1728 | |
Andrii Nakryiko | d3a3aa0 | 2019-10-28 16:37:27 -0700 | [diff] [blame] | 1729 | if (!obj->efile.strtabidx || obj->efile.strtabidx > idx) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1730 | pr_warn("Corrupted ELF file: index of strtab invalid\n"); |
Andrii Nakryiko | f102154 | 2019-05-29 10:36:07 -0700 | [diff] [blame] | 1731 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 77ba9a5 | 2015-12-08 02:25:30 +0000 | [diff] [blame] | 1732 | } |
Andrii Nakryiko | 063183b | 2019-06-17 12:26:55 -0700 | [diff] [blame] | 1733 | err = bpf_object__init_btf(obj, btf_data, btf_ext_data); |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1734 | if (!err) |
Toke Høiland-Jørgensen | 57a00f4 | 2019-11-02 12:09:41 +0100 | [diff] [blame] | 1735 | err = bpf_object__init_maps(obj, relaxed_maps, pin_root_path); |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1736 | if (!err) |
Andrii Nakryiko | 063183b | 2019-06-17 12:26:55 -0700 | [diff] [blame] | 1737 | err = bpf_object__sanitize_and_load_btf(obj); |
| 1738 | if (!err) |
Andrii Nakryiko | bf82927 | 2019-06-17 12:26:53 -0700 | [diff] [blame] | 1739 | err = bpf_object__init_prog_names(obj); |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 1740 | return err; |
| 1741 | } |
| 1742 | |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 1743 | static struct bpf_program * |
| 1744 | bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx) |
| 1745 | { |
| 1746 | struct bpf_program *prog; |
| 1747 | size_t i; |
| 1748 | |
| 1749 | for (i = 0; i < obj->nr_programs; i++) { |
| 1750 | prog = &obj->programs[i]; |
| 1751 | if (prog->idx == idx) |
| 1752 | return prog; |
| 1753 | } |
| 1754 | return NULL; |
| 1755 | } |
| 1756 | |
Jakub Kicinski | 6d4b198 | 2018-07-26 14:32:19 -0700 | [diff] [blame] | 1757 | struct bpf_program * |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 1758 | bpf_object__find_program_by_title(const struct bpf_object *obj, |
| 1759 | const char *title) |
Jakub Kicinski | 6d4b198 | 2018-07-26 14:32:19 -0700 | [diff] [blame] | 1760 | { |
| 1761 | struct bpf_program *pos; |
| 1762 | |
| 1763 | bpf_object__for_each_program(pos, obj) { |
| 1764 | if (pos->section_name && !strcmp(pos->section_name, title)) |
| 1765 | return pos; |
| 1766 | } |
| 1767 | return NULL; |
| 1768 | } |
| 1769 | |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 1770 | static bool bpf_object__shndx_is_data(const struct bpf_object *obj, |
| 1771 | int shndx) |
| 1772 | { |
| 1773 | return shndx == obj->efile.data_shndx || |
| 1774 | shndx == obj->efile.bss_shndx || |
| 1775 | shndx == obj->efile.rodata_shndx; |
| 1776 | } |
| 1777 | |
| 1778 | static bool bpf_object__shndx_is_maps(const struct bpf_object *obj, |
| 1779 | int shndx) |
| 1780 | { |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1781 | return shndx == obj->efile.maps_shndx || |
| 1782 | shndx == obj->efile.btf_maps_shndx; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 1783 | } |
| 1784 | |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 1785 | static enum libbpf_map_type |
| 1786 | bpf_object__section_to_libbpf_map_type(const struct bpf_object *obj, int shndx) |
| 1787 | { |
| 1788 | if (shndx == obj->efile.data_shndx) |
| 1789 | return LIBBPF_MAP_DATA; |
| 1790 | else if (shndx == obj->efile.bss_shndx) |
| 1791 | return LIBBPF_MAP_BSS; |
| 1792 | else if (shndx == obj->efile.rodata_shndx) |
| 1793 | return LIBBPF_MAP_RODATA; |
| 1794 | else |
| 1795 | return LIBBPF_MAP_UNSPEC; |
| 1796 | } |
| 1797 | |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 1798 | static int bpf_program__record_reloc(struct bpf_program *prog, |
| 1799 | struct reloc_desc *reloc_desc, |
| 1800 | __u32 insn_idx, const char *name, |
| 1801 | const GElf_Sym *sym, const GElf_Rel *rel) |
| 1802 | { |
| 1803 | struct bpf_insn *insn = &prog->insns[insn_idx]; |
| 1804 | size_t map_idx, nr_maps = prog->obj->nr_maps; |
| 1805 | struct bpf_object *obj = prog->obj; |
| 1806 | __u32 shdr_idx = sym->st_shndx; |
| 1807 | enum libbpf_map_type type; |
| 1808 | struct bpf_map *map; |
| 1809 | |
| 1810 | /* sub-program call relocation */ |
| 1811 | if (insn->code == (BPF_JMP | BPF_CALL)) { |
| 1812 | if (insn->src_reg != BPF_PSEUDO_CALL) { |
| 1813 | pr_warn("incorrect bpf_call opcode\n"); |
| 1814 | return -LIBBPF_ERRNO__RELOC; |
| 1815 | } |
| 1816 | /* text_shndx can be 0, if no default "main" program exists */ |
| 1817 | if (!shdr_idx || shdr_idx != obj->efile.text_shndx) { |
| 1818 | pr_warn("bad call relo against section %u\n", shdr_idx); |
| 1819 | return -LIBBPF_ERRNO__RELOC; |
| 1820 | } |
| 1821 | if (sym->st_value % 8) { |
| 1822 | pr_warn("bad call relo offset: %lu\n", sym->st_value); |
| 1823 | return -LIBBPF_ERRNO__RELOC; |
| 1824 | } |
| 1825 | reloc_desc->type = RELO_CALL; |
| 1826 | reloc_desc->insn_idx = insn_idx; |
| 1827 | reloc_desc->text_off = sym->st_value / 8; |
| 1828 | obj->has_pseudo_calls = true; |
| 1829 | return 0; |
| 1830 | } |
| 1831 | |
| 1832 | if (insn->code != (BPF_LD | BPF_IMM | BPF_DW)) { |
Andrii Nakryiko | 8983b73 | 2019-11-20 23:07:42 -0800 | [diff] [blame] | 1833 | pr_warn("invalid relo for insns[%d].code 0x%x\n", |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 1834 | insn_idx, insn->code); |
| 1835 | return -LIBBPF_ERRNO__RELOC; |
| 1836 | } |
| 1837 | if (!shdr_idx || shdr_idx >= SHN_LORESERVE) { |
Andrii Nakryiko | 393cdfb | 2019-11-20 23:07:43 -0800 | [diff] [blame] | 1838 | pr_warn("invalid relo for \'%s\' in special section 0x%x; forgot to initialize global var?..\n", |
| 1839 | name, shdr_idx); |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 1840 | return -LIBBPF_ERRNO__RELOC; |
| 1841 | } |
| 1842 | |
| 1843 | type = bpf_object__section_to_libbpf_map_type(obj, shdr_idx); |
| 1844 | |
| 1845 | /* generic map reference relocation */ |
| 1846 | if (type == LIBBPF_MAP_UNSPEC) { |
| 1847 | if (!bpf_object__shndx_is_maps(obj, shdr_idx)) { |
| 1848 | pr_warn("bad map relo against section %u\n", |
| 1849 | shdr_idx); |
| 1850 | return -LIBBPF_ERRNO__RELOC; |
| 1851 | } |
| 1852 | for (map_idx = 0; map_idx < nr_maps; map_idx++) { |
| 1853 | map = &obj->maps[map_idx]; |
| 1854 | if (map->libbpf_type != type || |
| 1855 | map->sec_idx != sym->st_shndx || |
| 1856 | map->sec_offset != sym->st_value) |
| 1857 | continue; |
| 1858 | pr_debug("found map %zd (%s, sec %d, off %zu) for insn %u\n", |
| 1859 | map_idx, map->name, map->sec_idx, |
| 1860 | map->sec_offset, insn_idx); |
| 1861 | break; |
| 1862 | } |
| 1863 | if (map_idx >= nr_maps) { |
| 1864 | pr_warn("map relo failed to find map for sec %u, off %llu\n", |
| 1865 | shdr_idx, (__u64)sym->st_value); |
| 1866 | return -LIBBPF_ERRNO__RELOC; |
| 1867 | } |
| 1868 | reloc_desc->type = RELO_LD64; |
| 1869 | reloc_desc->insn_idx = insn_idx; |
| 1870 | reloc_desc->map_idx = map_idx; |
| 1871 | return 0; |
| 1872 | } |
| 1873 | |
| 1874 | /* global data map relocation */ |
| 1875 | if (!bpf_object__shndx_is_data(obj, shdr_idx)) { |
| 1876 | pr_warn("bad data relo against section %u\n", shdr_idx); |
| 1877 | return -LIBBPF_ERRNO__RELOC; |
| 1878 | } |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 1879 | if (!obj->caps.global_data) { |
| 1880 | pr_warn("relocation: kernel does not support global \'%s\' variable access in insns[%d]\n", |
| 1881 | name, insn_idx); |
| 1882 | return -LIBBPF_ERRNO__RELOC; |
| 1883 | } |
| 1884 | for (map_idx = 0; map_idx < nr_maps; map_idx++) { |
| 1885 | map = &obj->maps[map_idx]; |
| 1886 | if (map->libbpf_type != type) |
| 1887 | continue; |
| 1888 | pr_debug("found data map %zd (%s, sec %d, off %zu) for insn %u\n", |
| 1889 | map_idx, map->name, map->sec_idx, map->sec_offset, |
| 1890 | insn_idx); |
| 1891 | break; |
| 1892 | } |
| 1893 | if (map_idx >= nr_maps) { |
| 1894 | pr_warn("data relo failed to find map for sec %u\n", |
| 1895 | shdr_idx); |
| 1896 | return -LIBBPF_ERRNO__RELOC; |
| 1897 | } |
| 1898 | |
| 1899 | reloc_desc->type = RELO_DATA; |
| 1900 | reloc_desc->insn_idx = insn_idx; |
| 1901 | reloc_desc->map_idx = map_idx; |
| 1902 | return 0; |
| 1903 | } |
| 1904 | |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 1905 | static int |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 1906 | bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr, |
| 1907 | Elf_Data *data, struct bpf_object *obj) |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 1908 | { |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 1909 | Elf_Data *symbols = obj->efile.symbols; |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 1910 | int err, i, nrels; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 1911 | |
Andrii Nakryiko | 399dc65 | 2019-05-29 10:36:11 -0700 | [diff] [blame] | 1912 | pr_debug("collecting relocating info for: '%s'\n", prog->section_name); |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 1913 | nrels = shdr->sh_size / shdr->sh_entsize; |
| 1914 | |
| 1915 | prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels); |
| 1916 | if (!prog->reloc_desc) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1917 | pr_warn("failed to alloc memory in relocation\n"); |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 1918 | return -ENOMEM; |
| 1919 | } |
| 1920 | prog->nr_reloc = nrels; |
| 1921 | |
| 1922 | for (i = 0; i < nrels; i++) { |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 1923 | const char *name; |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 1924 | __u32 insn_idx; |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1925 | GElf_Sym sym; |
| 1926 | GElf_Rel rel; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 1927 | |
| 1928 | if (!gelf_getrel(data, i, &rel)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1929 | pr_warn("relocation: failed to get %d reloc\n", i); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1930 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 1931 | } |
Andrii Nakryiko | 399dc65 | 2019-05-29 10:36:11 -0700 | [diff] [blame] | 1932 | if (!gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 1933 | pr_warn("relocation: symbol %"PRIx64" not found\n", |
| 1934 | GELF_R_SYM(rel.r_info)); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1935 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 1936 | } |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 1937 | if (rel.r_offset % sizeof(struct bpf_insn)) |
| 1938 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 1939 | |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 1940 | insn_idx = rel.r_offset / sizeof(struct bpf_insn); |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 1941 | name = elf_strptr(obj->efile.elf, obj->efile.strtabidx, |
| 1942 | sym.st_name) ? : "<?>"; |
| 1943 | |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 1944 | pr_debug("relo for shdr %u, symb %llu, value %llu, type %d, bind %d, name %d (\'%s\'), insn %u\n", |
| 1945 | (__u32)sym.st_shndx, (__u64)GELF_R_SYM(rel.r_info), |
| 1946 | (__u64)sym.st_value, GELF_ST_TYPE(sym.st_info), |
| 1947 | GELF_ST_BIND(sym.st_info), sym.st_name, name, |
| 1948 | insn_idx); |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 1949 | |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 1950 | err = bpf_program__record_reloc(prog, &prog->reloc_desc[i], |
| 1951 | insn_idx, name, &sym, &rel); |
| 1952 | if (err) |
| 1953 | return err; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 1954 | } |
| 1955 | return 0; |
| 1956 | } |
| 1957 | |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1958 | 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] | 1959 | { |
| 1960 | struct bpf_map_def *def = &map->def; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 1961 | __u32 key_type_id = 0, value_type_id = 0; |
Yonghong Song | 96408c4 | 2019-02-04 11:00:58 -0800 | [diff] [blame] | 1962 | int ret; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 1963 | |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1964 | /* if it's BTF-defined map, we don't need to search for type IDs */ |
| 1965 | if (map->sec_idx == obj->efile.btf_maps_shndx) |
| 1966 | return 0; |
| 1967 | |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 1968 | if (!bpf_map__is_internal(map)) { |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1969 | 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] | 1970 | def->value_size, &key_type_id, |
| 1971 | &value_type_id); |
| 1972 | } else { |
| 1973 | /* |
| 1974 | * LLVM annotates global data differently in BTF, that is, |
| 1975 | * only as '.data', '.bss' or '.rodata'. |
| 1976 | */ |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 1977 | ret = btf__find_by_name(obj->btf, |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 1978 | libbpf_type_to_btf_name[map->libbpf_type]); |
| 1979 | } |
| 1980 | if (ret < 0) |
Yonghong Song | 96408c4 | 2019-02-04 11:00:58 -0800 | [diff] [blame] | 1981 | return ret; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 1982 | |
Yonghong Song | 96408c4 | 2019-02-04 11:00:58 -0800 | [diff] [blame] | 1983 | map->btf_key_type_id = key_type_id; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 1984 | map->btf_value_type_id = bpf_map__is_internal(map) ? |
| 1985 | ret : value_type_id; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 1986 | return 0; |
| 1987 | } |
| 1988 | |
Jakub Kicinski | 26736eb | 2018-07-10 14:43:06 -0700 | [diff] [blame] | 1989 | int bpf_map__reuse_fd(struct bpf_map *map, int fd) |
| 1990 | { |
| 1991 | struct bpf_map_info info = {}; |
| 1992 | __u32 len = sizeof(info); |
| 1993 | int new_fd, err; |
| 1994 | char *new_name; |
| 1995 | |
| 1996 | err = bpf_obj_get_info_by_fd(fd, &info, &len); |
| 1997 | if (err) |
| 1998 | return err; |
| 1999 | |
| 2000 | new_name = strdup(info.name); |
| 2001 | if (!new_name) |
| 2002 | return -errno; |
| 2003 | |
| 2004 | new_fd = open("/", O_RDONLY | O_CLOEXEC); |
Toke Høiland-Jørgensen | d1b4574 | 2019-11-02 12:09:37 +0100 | [diff] [blame] | 2005 | if (new_fd < 0) { |
| 2006 | err = -errno; |
Jakub Kicinski | 26736eb | 2018-07-10 14:43:06 -0700 | [diff] [blame] | 2007 | goto err_free_new_name; |
Toke Høiland-Jørgensen | d1b4574 | 2019-11-02 12:09:37 +0100 | [diff] [blame] | 2008 | } |
Jakub Kicinski | 26736eb | 2018-07-10 14:43:06 -0700 | [diff] [blame] | 2009 | |
| 2010 | new_fd = dup3(fd, new_fd, O_CLOEXEC); |
Toke Høiland-Jørgensen | d1b4574 | 2019-11-02 12:09:37 +0100 | [diff] [blame] | 2011 | if (new_fd < 0) { |
| 2012 | err = -errno; |
Jakub Kicinski | 26736eb | 2018-07-10 14:43:06 -0700 | [diff] [blame] | 2013 | goto err_close_new_fd; |
Toke Høiland-Jørgensen | d1b4574 | 2019-11-02 12:09:37 +0100 | [diff] [blame] | 2014 | } |
Jakub Kicinski | 26736eb | 2018-07-10 14:43:06 -0700 | [diff] [blame] | 2015 | |
| 2016 | err = zclose(map->fd); |
Toke Høiland-Jørgensen | d1b4574 | 2019-11-02 12:09:37 +0100 | [diff] [blame] | 2017 | if (err) { |
| 2018 | err = -errno; |
Jakub Kicinski | 26736eb | 2018-07-10 14:43:06 -0700 | [diff] [blame] | 2019 | goto err_close_new_fd; |
Toke Høiland-Jørgensen | d1b4574 | 2019-11-02 12:09:37 +0100 | [diff] [blame] | 2020 | } |
Jakub Kicinski | 26736eb | 2018-07-10 14:43:06 -0700 | [diff] [blame] | 2021 | free(map->name); |
| 2022 | |
| 2023 | map->fd = new_fd; |
| 2024 | map->name = new_name; |
| 2025 | map->def.type = info.type; |
| 2026 | map->def.key_size = info.key_size; |
| 2027 | map->def.value_size = info.value_size; |
| 2028 | map->def.max_entries = info.max_entries; |
| 2029 | map->def.map_flags = info.map_flags; |
| 2030 | map->btf_key_type_id = info.btf_key_type_id; |
| 2031 | 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] | 2032 | map->reused = true; |
Jakub Kicinski | 26736eb | 2018-07-10 14:43:06 -0700 | [diff] [blame] | 2033 | |
| 2034 | return 0; |
| 2035 | |
| 2036 | err_close_new_fd: |
| 2037 | close(new_fd); |
| 2038 | err_free_new_name: |
| 2039 | free(new_name); |
Toke Høiland-Jørgensen | d1b4574 | 2019-11-02 12:09:37 +0100 | [diff] [blame] | 2040 | return err; |
Jakub Kicinski | 26736eb | 2018-07-10 14:43:06 -0700 | [diff] [blame] | 2041 | } |
| 2042 | |
Andrey Ignatov | 1a11a4c | 2019-02-14 15:01:42 -0800 | [diff] [blame] | 2043 | int bpf_map__resize(struct bpf_map *map, __u32 max_entries) |
| 2044 | { |
| 2045 | if (!map || !max_entries) |
| 2046 | return -EINVAL; |
| 2047 | |
| 2048 | /* If map already created, its attributes can't be changed. */ |
| 2049 | if (map->fd >= 0) |
| 2050 | return -EBUSY; |
| 2051 | |
| 2052 | map->def.max_entries = max_entries; |
| 2053 | |
| 2054 | return 0; |
| 2055 | } |
| 2056 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 2057 | static int |
Stanislav Fomichev | 47eff61 | 2018-11-20 17:11:19 -0800 | [diff] [blame] | 2058 | bpf_object__probe_name(struct bpf_object *obj) |
| 2059 | { |
| 2060 | struct bpf_load_program_attr attr; |
| 2061 | char *cp, errmsg[STRERR_BUFSIZE]; |
| 2062 | struct bpf_insn insns[] = { |
| 2063 | BPF_MOV64_IMM(BPF_REG_0, 0), |
| 2064 | BPF_EXIT_INSN(), |
| 2065 | }; |
| 2066 | int ret; |
| 2067 | |
| 2068 | /* make sure basic loading works */ |
| 2069 | |
| 2070 | memset(&attr, 0, sizeof(attr)); |
| 2071 | attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER; |
| 2072 | attr.insns = insns; |
| 2073 | attr.insns_cnt = ARRAY_SIZE(insns); |
| 2074 | attr.license = "GPL"; |
| 2075 | |
| 2076 | ret = bpf_load_program_xattr(&attr, NULL, 0); |
| 2077 | if (ret < 0) { |
| 2078 | cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg)); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2079 | pr_warn("Error in %s():%s(%d). Couldn't load basic 'r0 = 0' BPF program.\n", |
| 2080 | __func__, cp, errno); |
Stanislav Fomichev | 47eff61 | 2018-11-20 17:11:19 -0800 | [diff] [blame] | 2081 | return -errno; |
| 2082 | } |
| 2083 | close(ret); |
| 2084 | |
| 2085 | /* now try the same program, but with the name */ |
| 2086 | |
| 2087 | attr.name = "test"; |
| 2088 | ret = bpf_load_program_xattr(&attr, NULL, 0); |
| 2089 | if (ret >= 0) { |
| 2090 | obj->caps.name = 1; |
| 2091 | close(ret); |
| 2092 | } |
| 2093 | |
| 2094 | return 0; |
| 2095 | } |
| 2096 | |
| 2097 | static int |
Daniel Borkmann | 8837fe5 | 2019-04-24 00:45:56 +0200 | [diff] [blame] | 2098 | bpf_object__probe_global_data(struct bpf_object *obj) |
| 2099 | { |
| 2100 | struct bpf_load_program_attr prg_attr; |
| 2101 | struct bpf_create_map_attr map_attr; |
| 2102 | char *cp, errmsg[STRERR_BUFSIZE]; |
| 2103 | struct bpf_insn insns[] = { |
| 2104 | BPF_LD_MAP_VALUE(BPF_REG_1, 0, 16), |
| 2105 | BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 42), |
| 2106 | BPF_MOV64_IMM(BPF_REG_0, 0), |
| 2107 | BPF_EXIT_INSN(), |
| 2108 | }; |
| 2109 | int ret, map; |
| 2110 | |
| 2111 | memset(&map_attr, 0, sizeof(map_attr)); |
| 2112 | map_attr.map_type = BPF_MAP_TYPE_ARRAY; |
| 2113 | map_attr.key_size = sizeof(int); |
| 2114 | map_attr.value_size = 32; |
| 2115 | map_attr.max_entries = 1; |
| 2116 | |
| 2117 | map = bpf_create_map_xattr(&map_attr); |
| 2118 | if (map < 0) { |
| 2119 | cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg)); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2120 | pr_warn("Error in %s():%s(%d). Couldn't create simple array map.\n", |
| 2121 | __func__, cp, errno); |
Daniel Borkmann | 8837fe5 | 2019-04-24 00:45:56 +0200 | [diff] [blame] | 2122 | return -errno; |
| 2123 | } |
| 2124 | |
| 2125 | insns[0].imm = map; |
| 2126 | |
| 2127 | memset(&prg_attr, 0, sizeof(prg_attr)); |
| 2128 | prg_attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER; |
| 2129 | prg_attr.insns = insns; |
| 2130 | prg_attr.insns_cnt = ARRAY_SIZE(insns); |
| 2131 | prg_attr.license = "GPL"; |
| 2132 | |
| 2133 | ret = bpf_load_program_xattr(&prg_attr, NULL, 0); |
| 2134 | if (ret >= 0) { |
| 2135 | obj->caps.global_data = 1; |
| 2136 | close(ret); |
| 2137 | } |
| 2138 | |
| 2139 | close(map); |
| 2140 | return 0; |
| 2141 | } |
| 2142 | |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 2143 | static int bpf_object__probe_btf_func(struct bpf_object *obj) |
| 2144 | { |
Andrii Nakryiko | 8983b73 | 2019-11-20 23:07:42 -0800 | [diff] [blame] | 2145 | static const char strs[] = "\0int\0x\0a"; |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 2146 | /* void x(int a) {} */ |
| 2147 | __u32 types[] = { |
| 2148 | /* int */ |
| 2149 | BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ |
| 2150 | /* FUNC_PROTO */ /* [2] */ |
| 2151 | BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 0), |
| 2152 | BTF_PARAM_ENC(7, 1), |
| 2153 | /* FUNC x */ /* [3] */ |
| 2154 | BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0), 2), |
| 2155 | }; |
Michal Rostecki | cfd4921 | 2019-05-29 20:31:09 +0200 | [diff] [blame] | 2156 | int btf_fd; |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 2157 | |
Michal Rostecki | cfd4921 | 2019-05-29 20:31:09 +0200 | [diff] [blame] | 2158 | btf_fd = libbpf__load_raw_btf((char *)types, sizeof(types), |
| 2159 | strs, sizeof(strs)); |
| 2160 | if (btf_fd >= 0) { |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 2161 | obj->caps.btf_func = 1; |
Michal Rostecki | cfd4921 | 2019-05-29 20:31:09 +0200 | [diff] [blame] | 2162 | close(btf_fd); |
| 2163 | return 1; |
| 2164 | } |
| 2165 | |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 2166 | return 0; |
| 2167 | } |
| 2168 | |
| 2169 | static int bpf_object__probe_btf_datasec(struct bpf_object *obj) |
| 2170 | { |
Andrii Nakryiko | 8983b73 | 2019-11-20 23:07:42 -0800 | [diff] [blame] | 2171 | static const char strs[] = "\0x\0.data"; |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 2172 | /* static int a; */ |
| 2173 | __u32 types[] = { |
| 2174 | /* int */ |
| 2175 | BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ |
| 2176 | /* VAR x */ /* [2] */ |
| 2177 | BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1), |
| 2178 | BTF_VAR_STATIC, |
| 2179 | /* DATASEC val */ /* [3] */ |
| 2180 | BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4), |
| 2181 | BTF_VAR_SECINFO_ENC(2, 0, 4), |
| 2182 | }; |
Michal Rostecki | cfd4921 | 2019-05-29 20:31:09 +0200 | [diff] [blame] | 2183 | int btf_fd; |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 2184 | |
Michal Rostecki | cfd4921 | 2019-05-29 20:31:09 +0200 | [diff] [blame] | 2185 | btf_fd = libbpf__load_raw_btf((char *)types, sizeof(types), |
| 2186 | strs, sizeof(strs)); |
| 2187 | if (btf_fd >= 0) { |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 2188 | obj->caps.btf_datasec = 1; |
Michal Rostecki | cfd4921 | 2019-05-29 20:31:09 +0200 | [diff] [blame] | 2189 | close(btf_fd); |
| 2190 | return 1; |
| 2191 | } |
| 2192 | |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 2193 | return 0; |
| 2194 | } |
| 2195 | |
Andrii Nakryiko | 7fe74b4 | 2019-11-17 09:28:05 -0800 | [diff] [blame] | 2196 | static int bpf_object__probe_array_mmap(struct bpf_object *obj) |
| 2197 | { |
| 2198 | struct bpf_create_map_attr attr = { |
| 2199 | .map_type = BPF_MAP_TYPE_ARRAY, |
| 2200 | .map_flags = BPF_F_MMAPABLE, |
| 2201 | .key_size = sizeof(int), |
| 2202 | .value_size = sizeof(int), |
| 2203 | .max_entries = 1, |
| 2204 | }; |
| 2205 | int fd; |
| 2206 | |
| 2207 | fd = bpf_create_map_xattr(&attr); |
| 2208 | if (fd >= 0) { |
| 2209 | obj->caps.array_mmap = 1; |
| 2210 | close(fd); |
| 2211 | return 1; |
| 2212 | } |
| 2213 | |
| 2214 | return 0; |
| 2215 | } |
| 2216 | |
Daniel Borkmann | 8837fe5 | 2019-04-24 00:45:56 +0200 | [diff] [blame] | 2217 | static int |
Stanislav Fomichev | 47eff61 | 2018-11-20 17:11:19 -0800 | [diff] [blame] | 2218 | bpf_object__probe_caps(struct bpf_object *obj) |
| 2219 | { |
Daniel Borkmann | 8837fe5 | 2019-04-24 00:45:56 +0200 | [diff] [blame] | 2220 | int (*probe_fn[])(struct bpf_object *obj) = { |
| 2221 | bpf_object__probe_name, |
| 2222 | bpf_object__probe_global_data, |
Andrii Nakryiko | d7c4b39 | 2019-05-10 14:13:15 -0700 | [diff] [blame] | 2223 | bpf_object__probe_btf_func, |
| 2224 | bpf_object__probe_btf_datasec, |
Andrii Nakryiko | 7fe74b4 | 2019-11-17 09:28:05 -0800 | [diff] [blame] | 2225 | bpf_object__probe_array_mmap, |
Daniel Borkmann | 8837fe5 | 2019-04-24 00:45:56 +0200 | [diff] [blame] | 2226 | }; |
| 2227 | int i, ret; |
| 2228 | |
| 2229 | for (i = 0; i < ARRAY_SIZE(probe_fn); i++) { |
| 2230 | ret = probe_fn[i](obj); |
| 2231 | if (ret < 0) |
Stanislav Fomichev | 15ea164 | 2019-05-14 20:38:49 -0700 | [diff] [blame] | 2232 | pr_debug("Probe #%d failed with %d.\n", i, ret); |
Daniel Borkmann | 8837fe5 | 2019-04-24 00:45:56 +0200 | [diff] [blame] | 2233 | } |
| 2234 | |
| 2235 | return 0; |
Stanislav Fomichev | 47eff61 | 2018-11-20 17:11:19 -0800 | [diff] [blame] | 2236 | } |
| 2237 | |
Toke Høiland-Jørgensen | 57a00f4 | 2019-11-02 12:09:41 +0100 | [diff] [blame] | 2238 | static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd) |
| 2239 | { |
| 2240 | struct bpf_map_info map_info = {}; |
| 2241 | char msg[STRERR_BUFSIZE]; |
| 2242 | __u32 map_info_len; |
| 2243 | |
| 2244 | map_info_len = sizeof(map_info); |
| 2245 | |
| 2246 | if (bpf_obj_get_info_by_fd(map_fd, &map_info, &map_info_len)) { |
| 2247 | pr_warn("failed to get map info for map FD %d: %s\n", |
| 2248 | map_fd, libbpf_strerror_r(errno, msg, sizeof(msg))); |
| 2249 | return false; |
| 2250 | } |
| 2251 | |
| 2252 | return (map_info.type == map->def.type && |
| 2253 | map_info.key_size == map->def.key_size && |
| 2254 | map_info.value_size == map->def.value_size && |
| 2255 | map_info.max_entries == map->def.max_entries && |
| 2256 | map_info.map_flags == map->def.map_flags); |
| 2257 | } |
| 2258 | |
| 2259 | static int |
| 2260 | bpf_object__reuse_map(struct bpf_map *map) |
| 2261 | { |
| 2262 | char *cp, errmsg[STRERR_BUFSIZE]; |
| 2263 | int err, pin_fd; |
| 2264 | |
| 2265 | pin_fd = bpf_obj_get(map->pin_path); |
| 2266 | if (pin_fd < 0) { |
| 2267 | err = -errno; |
| 2268 | if (err == -ENOENT) { |
| 2269 | pr_debug("found no pinned map to reuse at '%s'\n", |
| 2270 | map->pin_path); |
| 2271 | return 0; |
| 2272 | } |
| 2273 | |
| 2274 | cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg)); |
| 2275 | pr_warn("couldn't retrieve pinned map '%s': %s\n", |
| 2276 | map->pin_path, cp); |
| 2277 | return err; |
| 2278 | } |
| 2279 | |
| 2280 | if (!map_is_reuse_compat(map, pin_fd)) { |
| 2281 | pr_warn("couldn't reuse pinned map at '%s': parameter mismatch\n", |
| 2282 | map->pin_path); |
| 2283 | close(pin_fd); |
| 2284 | return -EINVAL; |
| 2285 | } |
| 2286 | |
| 2287 | err = bpf_map__reuse_fd(map, pin_fd); |
| 2288 | if (err) { |
| 2289 | close(pin_fd); |
| 2290 | return err; |
| 2291 | } |
| 2292 | map->pinned = true; |
| 2293 | pr_debug("reused pinned map at '%s'\n", map->pin_path); |
| 2294 | |
| 2295 | return 0; |
| 2296 | } |
| 2297 | |
Stanislav Fomichev | 47eff61 | 2018-11-20 17:11:19 -0800 | [diff] [blame] | 2298 | static int |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 2299 | bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map) |
| 2300 | { |
| 2301 | char *cp, errmsg[STRERR_BUFSIZE]; |
| 2302 | int err, zero = 0; |
| 2303 | __u8 *data; |
| 2304 | |
| 2305 | /* Nothing to do here since kernel already zero-initializes .bss map. */ |
| 2306 | if (map->libbpf_type == LIBBPF_MAP_BSS) |
| 2307 | return 0; |
| 2308 | |
| 2309 | data = map->libbpf_type == LIBBPF_MAP_DATA ? |
| 2310 | obj->sections.data : obj->sections.rodata; |
| 2311 | |
| 2312 | err = bpf_map_update_elem(map->fd, &zero, data, 0); |
| 2313 | /* Freeze .rodata map as read-only from syscall side. */ |
| 2314 | if (!err && map->libbpf_type == LIBBPF_MAP_RODATA) { |
| 2315 | err = bpf_map_freeze(map->fd); |
| 2316 | if (err) { |
| 2317 | cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg)); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2318 | pr_warn("Error freezing map(%s) as read-only: %s\n", |
| 2319 | map->name, cp); |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 2320 | err = 0; |
| 2321 | } |
| 2322 | } |
| 2323 | return err; |
| 2324 | } |
| 2325 | |
| 2326 | static int |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 2327 | bpf_object__create_maps(struct bpf_object *obj) |
| 2328 | { |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 2329 | struct bpf_create_map_attr create_attr = {}; |
Andrii Nakryiko | d7ff34d | 2019-07-06 11:06:25 -0700 | [diff] [blame] | 2330 | int nr_cpus = 0; |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 2331 | unsigned int i; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 2332 | int err; |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 2333 | |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 2334 | for (i = 0; i < obj->nr_maps; i++) { |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 2335 | struct bpf_map *map = &obj->maps[i]; |
| 2336 | struct bpf_map_def *def = &map->def; |
Thomas Richter | 1ce6a9f | 2018-07-30 10:53:23 +0200 | [diff] [blame] | 2337 | char *cp, errmsg[STRERR_BUFSIZE]; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 2338 | int *pfd = &map->fd; |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 2339 | |
Toke Høiland-Jørgensen | 57a00f4 | 2019-11-02 12:09:41 +0100 | [diff] [blame] | 2340 | if (map->pin_path) { |
| 2341 | err = bpf_object__reuse_map(map); |
| 2342 | if (err) { |
| 2343 | pr_warn("error reusing pinned map %s\n", |
| 2344 | map->name); |
| 2345 | return err; |
| 2346 | } |
| 2347 | } |
| 2348 | |
Jakub Kicinski | 26736eb | 2018-07-10 14:43:06 -0700 | [diff] [blame] | 2349 | if (map->fd >= 0) { |
| 2350 | pr_debug("skip map create (preset) %s: fd=%d\n", |
| 2351 | map->name, map->fd); |
| 2352 | continue; |
| 2353 | } |
| 2354 | |
Stanislav Fomichev | 94cb310 | 2018-11-20 17:11:20 -0800 | [diff] [blame] | 2355 | if (obj->caps.name) |
| 2356 | create_attr.name = map->name; |
David Beckett | f0307a7 | 2018-05-16 14:02:49 -0700 | [diff] [blame] | 2357 | create_attr.map_ifindex = map->map_ifindex; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 2358 | create_attr.map_type = def->type; |
| 2359 | create_attr.map_flags = def->map_flags; |
| 2360 | create_attr.key_size = def->key_size; |
| 2361 | create_attr.value_size = def->value_size; |
Andrii Nakryiko | d7ff34d | 2019-07-06 11:06:25 -0700 | [diff] [blame] | 2362 | if (def->type == BPF_MAP_TYPE_PERF_EVENT_ARRAY && |
| 2363 | !def->max_entries) { |
| 2364 | if (!nr_cpus) |
| 2365 | nr_cpus = libbpf_num_possible_cpus(); |
| 2366 | if (nr_cpus < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2367 | pr_warn("failed to determine number of system CPUs: %d\n", |
| 2368 | nr_cpus); |
Andrii Nakryiko | d7ff34d | 2019-07-06 11:06:25 -0700 | [diff] [blame] | 2369 | err = nr_cpus; |
| 2370 | goto err_out; |
| 2371 | } |
| 2372 | pr_debug("map '%s': setting size to %d\n", |
| 2373 | map->name, nr_cpus); |
| 2374 | create_attr.max_entries = nr_cpus; |
| 2375 | } else { |
| 2376 | create_attr.max_entries = def->max_entries; |
| 2377 | } |
Andrii Nakryiko | e55d54f | 2019-06-12 22:04:57 -0700 | [diff] [blame] | 2378 | create_attr.btf_fd = 0; |
Martin KaFai Lau | 61746dbe | 2018-05-22 15:04:24 -0700 | [diff] [blame] | 2379 | create_attr.btf_key_type_id = 0; |
| 2380 | create_attr.btf_value_type_id = 0; |
Nikita V. Shirokov | addb9fc | 2018-11-20 20:55:56 -0800 | [diff] [blame] | 2381 | if (bpf_map_type__is_map_in_map(def->type) && |
| 2382 | map->inner_map_fd >= 0) |
| 2383 | create_attr.inner_map_fd = map->inner_map_fd; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 2384 | |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 2385 | if (obj->btf && !bpf_map_find_btf_info(obj, map)) { |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 2386 | create_attr.btf_fd = btf__fd(obj->btf); |
Martin KaFai Lau | 61746dbe | 2018-05-22 15:04:24 -0700 | [diff] [blame] | 2387 | create_attr.btf_key_type_id = map->btf_key_type_id; |
| 2388 | create_attr.btf_value_type_id = map->btf_value_type_id; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | *pfd = bpf_create_map_xattr(&create_attr); |
Andrii Nakryiko | e55d54f | 2019-06-12 22:04:57 -0700 | [diff] [blame] | 2392 | if (*pfd < 0 && (create_attr.btf_key_type_id || |
| 2393 | create_attr.btf_value_type_id)) { |
Andrii Nakryiko | d7ff34d | 2019-07-06 11:06:25 -0700 | [diff] [blame] | 2394 | err = -errno; |
| 2395 | cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg)); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2396 | pr_warn("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n", |
| 2397 | map->name, cp, err); |
Andrii Nakryiko | e55d54f | 2019-06-12 22:04:57 -0700 | [diff] [blame] | 2398 | create_attr.btf_fd = 0; |
Martin KaFai Lau | 61746dbe | 2018-05-22 15:04:24 -0700 | [diff] [blame] | 2399 | create_attr.btf_key_type_id = 0; |
| 2400 | create_attr.btf_value_type_id = 0; |
| 2401 | map->btf_key_type_id = 0; |
| 2402 | map->btf_value_type_id = 0; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 2403 | *pfd = bpf_create_map_xattr(&create_attr); |
| 2404 | } |
| 2405 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 2406 | if (*pfd < 0) { |
| 2407 | size_t j; |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 2408 | |
Andrii Nakryiko | d7ff34d | 2019-07-06 11:06:25 -0700 | [diff] [blame] | 2409 | err = -errno; |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 2410 | err_out: |
Andrii Nakryiko | d7ff34d | 2019-07-06 11:06:25 -0700 | [diff] [blame] | 2411 | cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg)); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2412 | pr_warn("failed to create map (name: '%s'): %s(%d)\n", |
| 2413 | map->name, cp, err); |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 2414 | for (j = 0; j < i; j++) |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 2415 | zclose(obj->maps[j].fd); |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 2416 | return err; |
| 2417 | } |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 2418 | |
| 2419 | if (bpf_map__is_internal(map)) { |
| 2420 | err = bpf_object__populate_internal_map(obj, map); |
| 2421 | if (err < 0) { |
| 2422 | zclose(*pfd); |
| 2423 | goto err_out; |
| 2424 | } |
| 2425 | } |
| 2426 | |
Toke Høiland-Jørgensen | 57a00f4 | 2019-11-02 12:09:41 +0100 | [diff] [blame] | 2427 | if (map->pin_path && !map->pinned) { |
| 2428 | err = bpf_map__pin(map, NULL); |
| 2429 | if (err) { |
| 2430 | pr_warn("failed to auto-pin map name '%s' at '%s'\n", |
| 2431 | map->name, map->pin_path); |
| 2432 | return err; |
| 2433 | } |
| 2434 | } |
| 2435 | |
Andrii Nakryiko | 76e1022 | 2019-05-29 10:36:10 -0700 | [diff] [blame] | 2436 | pr_debug("created map %s: fd=%d\n", map->name, *pfd); |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 2437 | } |
| 2438 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 2439 | return 0; |
| 2440 | } |
| 2441 | |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 2442 | static int |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 2443 | check_btf_ext_reloc_err(struct bpf_program *prog, int err, |
| 2444 | void *btf_prog_info, const char *info_name) |
| 2445 | { |
| 2446 | if (err != -ENOENT) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2447 | pr_warn("Error in loading %s for sec %s.\n", |
| 2448 | info_name, prog->section_name); |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 2449 | return err; |
| 2450 | } |
| 2451 | |
| 2452 | /* err == -ENOENT (i.e. prog->section_name not found in btf_ext) */ |
| 2453 | |
| 2454 | if (btf_prog_info) { |
| 2455 | /* |
| 2456 | * Some info has already been found but has problem |
Andrii Nakryiko | 399dc65 | 2019-05-29 10:36:11 -0700 | [diff] [blame] | 2457 | * in the last btf_ext reloc. Must have to error out. |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 2458 | */ |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2459 | pr_warn("Error in relocating %s for sec %s.\n", |
| 2460 | info_name, prog->section_name); |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 2461 | return err; |
| 2462 | } |
| 2463 | |
Andrii Nakryiko | 399dc65 | 2019-05-29 10:36:11 -0700 | [diff] [blame] | 2464 | /* Have problem loading the very first info. Ignore the rest. */ |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2465 | pr_warn("Cannot find %s for main program sec %s. Ignore all %s.\n", |
| 2466 | info_name, prog->section_name, info_name); |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 2467 | return 0; |
| 2468 | } |
| 2469 | |
| 2470 | static int |
| 2471 | bpf_program_reloc_btf_ext(struct bpf_program *prog, struct bpf_object *obj, |
| 2472 | const char *section_name, __u32 insn_offset) |
| 2473 | { |
| 2474 | int err; |
| 2475 | |
| 2476 | if (!insn_offset || prog->func_info) { |
| 2477 | /* |
| 2478 | * !insn_offset => main program |
| 2479 | * |
| 2480 | * For sub prog, the main program's func_info has to |
| 2481 | * be loaded first (i.e. prog->func_info != NULL) |
| 2482 | */ |
| 2483 | err = btf_ext__reloc_func_info(obj->btf, obj->btf_ext, |
| 2484 | section_name, insn_offset, |
| 2485 | &prog->func_info, |
| 2486 | &prog->func_info_cnt); |
| 2487 | if (err) |
| 2488 | return check_btf_ext_reloc_err(prog, err, |
| 2489 | prog->func_info, |
| 2490 | "bpf_func_info"); |
| 2491 | |
| 2492 | prog->func_info_rec_size = btf_ext__func_info_rec_size(obj->btf_ext); |
| 2493 | } |
| 2494 | |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 2495 | if (!insn_offset || prog->line_info) { |
| 2496 | err = btf_ext__reloc_line_info(obj->btf, obj->btf_ext, |
| 2497 | section_name, insn_offset, |
| 2498 | &prog->line_info, |
| 2499 | &prog->line_info_cnt); |
| 2500 | if (err) |
| 2501 | return check_btf_ext_reloc_err(prog, err, |
| 2502 | prog->line_info, |
| 2503 | "bpf_line_info"); |
| 2504 | |
| 2505 | prog->line_info_rec_size = btf_ext__line_info_rec_size(obj->btf_ext); |
| 2506 | } |
| 2507 | |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 2508 | return 0; |
| 2509 | } |
| 2510 | |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 2511 | #define BPF_CORE_SPEC_MAX_LEN 64 |
| 2512 | |
| 2513 | /* represents BPF CO-RE field or array element accessor */ |
| 2514 | struct bpf_core_accessor { |
| 2515 | __u32 type_id; /* struct/union type or array element type */ |
| 2516 | __u32 idx; /* field index or array index */ |
| 2517 | const char *name; /* field name or NULL for array accessor */ |
| 2518 | }; |
| 2519 | |
| 2520 | struct bpf_core_spec { |
| 2521 | const struct btf *btf; |
| 2522 | /* high-level spec: named fields and array indices only */ |
| 2523 | struct bpf_core_accessor spec[BPF_CORE_SPEC_MAX_LEN]; |
| 2524 | /* high-level spec length */ |
| 2525 | int len; |
| 2526 | /* raw, low-level spec: 1-to-1 with accessor spec string */ |
| 2527 | int raw_spec[BPF_CORE_SPEC_MAX_LEN]; |
| 2528 | /* raw spec length */ |
| 2529 | int raw_len; |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 2530 | /* field bit offset represented by spec */ |
| 2531 | __u32 bit_offset; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 2532 | }; |
| 2533 | |
| 2534 | static bool str_is_empty(const char *s) |
| 2535 | { |
| 2536 | return !s || !s[0]; |
| 2537 | } |
| 2538 | |
| 2539 | /* |
Andrii Nakryiko | 511bb00 | 2019-10-15 11:28:45 -0700 | [diff] [blame] | 2540 | * Turn bpf_field_reloc into a low- and high-level spec representation, |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 2541 | * validating correctness along the way, as well as calculating resulting |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 2542 | * field bit offset, specified by accessor string. Low-level spec captures |
| 2543 | * every single level of nestedness, including traversing anonymous |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 2544 | * struct/union members. High-level one only captures semantically meaningful |
| 2545 | * "turning points": named fields and array indicies. |
| 2546 | * E.g., for this case: |
| 2547 | * |
| 2548 | * struct sample { |
| 2549 | * int __unimportant; |
| 2550 | * struct { |
| 2551 | * int __1; |
| 2552 | * int __2; |
| 2553 | * int a[7]; |
| 2554 | * }; |
| 2555 | * }; |
| 2556 | * |
| 2557 | * struct sample *s = ...; |
| 2558 | * |
| 2559 | * int x = &s->a[3]; // access string = '0:1:2:3' |
| 2560 | * |
| 2561 | * Low-level spec has 1:1 mapping with each element of access string (it's |
| 2562 | * just a parsed access string representation): [0, 1, 2, 3]. |
| 2563 | * |
| 2564 | * High-level spec will capture only 3 points: |
| 2565 | * - intial zero-index access by pointer (&s->... is the same as &s[0]...); |
| 2566 | * - field 'a' access (corresponds to '2' in low-level spec); |
| 2567 | * - array element #3 access (corresponds to '3' in low-level spec). |
| 2568 | * |
| 2569 | */ |
| 2570 | static int bpf_core_spec_parse(const struct btf *btf, |
| 2571 | __u32 type_id, |
| 2572 | const char *spec_str, |
| 2573 | struct bpf_core_spec *spec) |
| 2574 | { |
| 2575 | int access_idx, parsed_len, i; |
| 2576 | const struct btf_type *t; |
| 2577 | const char *name; |
| 2578 | __u32 id; |
| 2579 | __s64 sz; |
| 2580 | |
| 2581 | if (str_is_empty(spec_str) || *spec_str == ':') |
| 2582 | return -EINVAL; |
| 2583 | |
| 2584 | memset(spec, 0, sizeof(*spec)); |
| 2585 | spec->btf = btf; |
| 2586 | |
| 2587 | /* parse spec_str="0:1:2:3:4" into array raw_spec=[0, 1, 2, 3, 4] */ |
| 2588 | while (*spec_str) { |
| 2589 | if (*spec_str == ':') |
| 2590 | ++spec_str; |
| 2591 | if (sscanf(spec_str, "%d%n", &access_idx, &parsed_len) != 1) |
| 2592 | return -EINVAL; |
| 2593 | if (spec->raw_len == BPF_CORE_SPEC_MAX_LEN) |
| 2594 | return -E2BIG; |
| 2595 | spec_str += parsed_len; |
| 2596 | spec->raw_spec[spec->raw_len++] = access_idx; |
| 2597 | } |
| 2598 | |
| 2599 | if (spec->raw_len == 0) |
| 2600 | return -EINVAL; |
| 2601 | |
| 2602 | /* first spec value is always reloc type array index */ |
| 2603 | t = skip_mods_and_typedefs(btf, type_id, &id); |
| 2604 | if (!t) |
| 2605 | return -EINVAL; |
| 2606 | |
| 2607 | access_idx = spec->raw_spec[0]; |
| 2608 | spec->spec[0].type_id = id; |
| 2609 | spec->spec[0].idx = access_idx; |
| 2610 | spec->len++; |
| 2611 | |
| 2612 | sz = btf__resolve_size(btf, id); |
| 2613 | if (sz < 0) |
| 2614 | return sz; |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 2615 | spec->bit_offset = access_idx * sz * 8; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 2616 | |
| 2617 | for (i = 1; i < spec->raw_len; i++) { |
| 2618 | t = skip_mods_and_typedefs(btf, id, &id); |
| 2619 | if (!t) |
| 2620 | return -EINVAL; |
| 2621 | |
| 2622 | access_idx = spec->raw_spec[i]; |
| 2623 | |
| 2624 | if (btf_is_composite(t)) { |
| 2625 | const struct btf_member *m; |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 2626 | __u32 bit_offset; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 2627 | |
| 2628 | if (access_idx >= btf_vlen(t)) |
| 2629 | return -EINVAL; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 2630 | |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 2631 | bit_offset = btf_member_bit_offset(t, access_idx); |
| 2632 | spec->bit_offset += bit_offset; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 2633 | |
| 2634 | m = btf_members(t) + access_idx; |
| 2635 | if (m->name_off) { |
| 2636 | name = btf__name_by_offset(btf, m->name_off); |
| 2637 | if (str_is_empty(name)) |
| 2638 | return -EINVAL; |
| 2639 | |
| 2640 | spec->spec[spec->len].type_id = id; |
| 2641 | spec->spec[spec->len].idx = access_idx; |
| 2642 | spec->spec[spec->len].name = name; |
| 2643 | spec->len++; |
| 2644 | } |
| 2645 | |
| 2646 | id = m->type; |
| 2647 | } else if (btf_is_array(t)) { |
| 2648 | const struct btf_array *a = btf_array(t); |
| 2649 | |
| 2650 | t = skip_mods_and_typedefs(btf, a->type, &id); |
| 2651 | if (!t || access_idx >= a->nelems) |
| 2652 | return -EINVAL; |
| 2653 | |
| 2654 | spec->spec[spec->len].type_id = id; |
| 2655 | spec->spec[spec->len].idx = access_idx; |
| 2656 | spec->len++; |
| 2657 | |
| 2658 | sz = btf__resolve_size(btf, id); |
| 2659 | if (sz < 0) |
| 2660 | return sz; |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 2661 | spec->bit_offset += access_idx * sz * 8; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 2662 | } else { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2663 | pr_warn("relo for [%u] %s (at idx %d) captures type [%d] of unexpected kind %d\n", |
| 2664 | type_id, spec_str, i, id, btf_kind(t)); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 2665 | return -EINVAL; |
| 2666 | } |
| 2667 | } |
| 2668 | |
| 2669 | return 0; |
| 2670 | } |
| 2671 | |
| 2672 | static bool bpf_core_is_flavor_sep(const char *s) |
| 2673 | { |
| 2674 | /* check X___Y name pattern, where X and Y are not underscores */ |
| 2675 | return s[0] != '_' && /* X */ |
| 2676 | s[1] == '_' && s[2] == '_' && s[3] == '_' && /* ___ */ |
| 2677 | s[4] != '_'; /* Y */ |
| 2678 | } |
| 2679 | |
| 2680 | /* Given 'some_struct_name___with_flavor' return the length of a name prefix |
| 2681 | * before last triple underscore. Struct name part after last triple |
| 2682 | * underscore is ignored by BPF CO-RE relocation during relocation matching. |
| 2683 | */ |
| 2684 | static size_t bpf_core_essential_name_len(const char *name) |
| 2685 | { |
| 2686 | size_t n = strlen(name); |
| 2687 | int i; |
| 2688 | |
| 2689 | for (i = n - 5; i >= 0; i--) { |
| 2690 | if (bpf_core_is_flavor_sep(name + i)) |
| 2691 | return i + 1; |
| 2692 | } |
| 2693 | return n; |
| 2694 | } |
| 2695 | |
| 2696 | /* dynamically sized list of type IDs */ |
| 2697 | struct ids_vec { |
| 2698 | __u32 *data; |
| 2699 | int len; |
| 2700 | }; |
| 2701 | |
| 2702 | static void bpf_core_free_cands(struct ids_vec *cand_ids) |
| 2703 | { |
| 2704 | free(cand_ids->data); |
| 2705 | free(cand_ids); |
| 2706 | } |
| 2707 | |
| 2708 | static struct ids_vec *bpf_core_find_cands(const struct btf *local_btf, |
| 2709 | __u32 local_type_id, |
| 2710 | const struct btf *targ_btf) |
| 2711 | { |
| 2712 | size_t local_essent_len, targ_essent_len; |
| 2713 | const char *local_name, *targ_name; |
| 2714 | const struct btf_type *t; |
| 2715 | struct ids_vec *cand_ids; |
| 2716 | __u32 *new_ids; |
| 2717 | int i, err, n; |
| 2718 | |
| 2719 | t = btf__type_by_id(local_btf, local_type_id); |
| 2720 | if (!t) |
| 2721 | return ERR_PTR(-EINVAL); |
| 2722 | |
| 2723 | local_name = btf__name_by_offset(local_btf, t->name_off); |
| 2724 | if (str_is_empty(local_name)) |
| 2725 | return ERR_PTR(-EINVAL); |
| 2726 | local_essent_len = bpf_core_essential_name_len(local_name); |
| 2727 | |
| 2728 | cand_ids = calloc(1, sizeof(*cand_ids)); |
| 2729 | if (!cand_ids) |
| 2730 | return ERR_PTR(-ENOMEM); |
| 2731 | |
| 2732 | n = btf__get_nr_types(targ_btf); |
| 2733 | for (i = 1; i <= n; i++) { |
| 2734 | t = btf__type_by_id(targ_btf, i); |
| 2735 | targ_name = btf__name_by_offset(targ_btf, t->name_off); |
| 2736 | if (str_is_empty(targ_name)) |
| 2737 | continue; |
| 2738 | |
| 2739 | targ_essent_len = bpf_core_essential_name_len(targ_name); |
| 2740 | if (targ_essent_len != local_essent_len) |
| 2741 | continue; |
| 2742 | |
| 2743 | if (strncmp(local_name, targ_name, local_essent_len) == 0) { |
| 2744 | pr_debug("[%d] %s: found candidate [%d] %s\n", |
| 2745 | local_type_id, local_name, i, targ_name); |
| 2746 | new_ids = realloc(cand_ids->data, cand_ids->len + 1); |
| 2747 | if (!new_ids) { |
| 2748 | err = -ENOMEM; |
| 2749 | goto err_out; |
| 2750 | } |
| 2751 | cand_ids->data = new_ids; |
| 2752 | cand_ids->data[cand_ids->len++] = i; |
| 2753 | } |
| 2754 | } |
| 2755 | return cand_ids; |
| 2756 | err_out: |
| 2757 | bpf_core_free_cands(cand_ids); |
| 2758 | return ERR_PTR(err); |
| 2759 | } |
| 2760 | |
| 2761 | /* Check two types for compatibility, skipping const/volatile/restrict and |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 2762 | * typedefs, to ensure we are relocating compatible entities: |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 2763 | * - any two STRUCTs/UNIONs are compatible and can be mixed; |
Andrii Nakryiko | 94f060e | 2019-11-01 15:28:08 -0700 | [diff] [blame] | 2764 | * - any two FWDs are compatible, if their names match (modulo flavor suffix); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 2765 | * - any two PTRs are always compatible; |
Andrii Nakryiko | 94f060e | 2019-11-01 15:28:08 -0700 | [diff] [blame] | 2766 | * - for ENUMs, names should be the same (ignoring flavor suffix) or at |
| 2767 | * least one of enums should be anonymous; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 2768 | * - for ENUMs, check sizes, names are ignored; |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 2769 | * - for INT, size and signedness are ignored; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 2770 | * - for ARRAY, dimensionality is ignored, element types are checked for |
| 2771 | * compatibility recursively; |
| 2772 | * - everything else shouldn't be ever a target of relocation. |
| 2773 | * These rules are not set in stone and probably will be adjusted as we get |
| 2774 | * more experience with using BPF CO-RE relocations. |
| 2775 | */ |
| 2776 | static int bpf_core_fields_are_compat(const struct btf *local_btf, |
| 2777 | __u32 local_id, |
| 2778 | const struct btf *targ_btf, |
| 2779 | __u32 targ_id) |
| 2780 | { |
| 2781 | const struct btf_type *local_type, *targ_type; |
| 2782 | |
| 2783 | recur: |
| 2784 | local_type = skip_mods_and_typedefs(local_btf, local_id, &local_id); |
| 2785 | targ_type = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id); |
| 2786 | if (!local_type || !targ_type) |
| 2787 | return -EINVAL; |
| 2788 | |
| 2789 | if (btf_is_composite(local_type) && btf_is_composite(targ_type)) |
| 2790 | return 1; |
| 2791 | if (btf_kind(local_type) != btf_kind(targ_type)) |
| 2792 | return 0; |
| 2793 | |
| 2794 | switch (btf_kind(local_type)) { |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 2795 | case BTF_KIND_PTR: |
| 2796 | return 1; |
Andrii Nakryiko | 94f060e | 2019-11-01 15:28:08 -0700 | [diff] [blame] | 2797 | case BTF_KIND_FWD: |
| 2798 | case BTF_KIND_ENUM: { |
| 2799 | const char *local_name, *targ_name; |
| 2800 | size_t local_len, targ_len; |
| 2801 | |
| 2802 | local_name = btf__name_by_offset(local_btf, |
| 2803 | local_type->name_off); |
| 2804 | targ_name = btf__name_by_offset(targ_btf, targ_type->name_off); |
| 2805 | local_len = bpf_core_essential_name_len(local_name); |
| 2806 | targ_len = bpf_core_essential_name_len(targ_name); |
| 2807 | /* one of them is anonymous or both w/ same flavor-less names */ |
| 2808 | return local_len == 0 || targ_len == 0 || |
| 2809 | (local_len == targ_len && |
| 2810 | strncmp(local_name, targ_name, local_len) == 0); |
| 2811 | } |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 2812 | case BTF_KIND_INT: |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 2813 | /* just reject deprecated bitfield-like integers; all other |
| 2814 | * integers are by default compatible between each other |
| 2815 | */ |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 2816 | return btf_int_offset(local_type) == 0 && |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 2817 | btf_int_offset(targ_type) == 0; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 2818 | case BTF_KIND_ARRAY: |
| 2819 | local_id = btf_array(local_type)->type; |
| 2820 | targ_id = btf_array(targ_type)->type; |
| 2821 | goto recur; |
| 2822 | default: |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 2823 | pr_warn("unexpected kind %d relocated, local [%d], target [%d]\n", |
| 2824 | btf_kind(local_type), local_id, targ_id); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 2825 | return 0; |
| 2826 | } |
| 2827 | } |
| 2828 | |
| 2829 | /* |
| 2830 | * Given single high-level named field accessor in local type, find |
| 2831 | * corresponding high-level accessor for a target type. Along the way, |
| 2832 | * maintain low-level spec for target as well. Also keep updating target |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 2833 | * bit offset. |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 2834 | * |
| 2835 | * Searching is performed through recursive exhaustive enumeration of all |
| 2836 | * fields of a struct/union. If there are any anonymous (embedded) |
| 2837 | * structs/unions, they are recursively searched as well. If field with |
| 2838 | * desired name is found, check compatibility between local and target types, |
| 2839 | * before returning result. |
| 2840 | * |
| 2841 | * 1 is returned, if field is found. |
| 2842 | * 0 is returned if no compatible field is found. |
| 2843 | * <0 is returned on error. |
| 2844 | */ |
| 2845 | static int bpf_core_match_member(const struct btf *local_btf, |
| 2846 | const struct bpf_core_accessor *local_acc, |
| 2847 | const struct btf *targ_btf, |
| 2848 | __u32 targ_id, |
| 2849 | struct bpf_core_spec *spec, |
| 2850 | __u32 *next_targ_id) |
| 2851 | { |
| 2852 | const struct btf_type *local_type, *targ_type; |
| 2853 | const struct btf_member *local_member, *m; |
| 2854 | const char *local_name, *targ_name; |
| 2855 | __u32 local_id; |
| 2856 | int i, n, found; |
| 2857 | |
| 2858 | targ_type = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id); |
| 2859 | if (!targ_type) |
| 2860 | return -EINVAL; |
| 2861 | if (!btf_is_composite(targ_type)) |
| 2862 | return 0; |
| 2863 | |
| 2864 | local_id = local_acc->type_id; |
| 2865 | local_type = btf__type_by_id(local_btf, local_id); |
| 2866 | local_member = btf_members(local_type) + local_acc->idx; |
| 2867 | local_name = btf__name_by_offset(local_btf, local_member->name_off); |
| 2868 | |
| 2869 | n = btf_vlen(targ_type); |
| 2870 | m = btf_members(targ_type); |
| 2871 | for (i = 0; i < n; i++, m++) { |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 2872 | __u32 bit_offset; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 2873 | |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 2874 | bit_offset = btf_member_bit_offset(targ_type, i); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 2875 | |
| 2876 | /* too deep struct/union/array nesting */ |
| 2877 | if (spec->raw_len == BPF_CORE_SPEC_MAX_LEN) |
| 2878 | return -E2BIG; |
| 2879 | |
| 2880 | /* speculate this member will be the good one */ |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 2881 | spec->bit_offset += bit_offset; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 2882 | spec->raw_spec[spec->raw_len++] = i; |
| 2883 | |
| 2884 | targ_name = btf__name_by_offset(targ_btf, m->name_off); |
| 2885 | if (str_is_empty(targ_name)) { |
| 2886 | /* embedded struct/union, we need to go deeper */ |
| 2887 | found = bpf_core_match_member(local_btf, local_acc, |
| 2888 | targ_btf, m->type, |
| 2889 | spec, next_targ_id); |
| 2890 | if (found) /* either found or error */ |
| 2891 | return found; |
| 2892 | } else if (strcmp(local_name, targ_name) == 0) { |
| 2893 | /* matching named field */ |
| 2894 | struct bpf_core_accessor *targ_acc; |
| 2895 | |
| 2896 | targ_acc = &spec->spec[spec->len++]; |
| 2897 | targ_acc->type_id = targ_id; |
| 2898 | targ_acc->idx = i; |
| 2899 | targ_acc->name = targ_name; |
| 2900 | |
| 2901 | *next_targ_id = m->type; |
| 2902 | found = bpf_core_fields_are_compat(local_btf, |
| 2903 | local_member->type, |
| 2904 | targ_btf, m->type); |
| 2905 | if (!found) |
| 2906 | spec->len--; /* pop accessor */ |
| 2907 | return found; |
| 2908 | } |
| 2909 | /* member turned out not to be what we looked for */ |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 2910 | spec->bit_offset -= bit_offset; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 2911 | spec->raw_len--; |
| 2912 | } |
| 2913 | |
| 2914 | return 0; |
| 2915 | } |
| 2916 | |
| 2917 | /* |
| 2918 | * 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] | 2919 | * target spec (high-level, low-level + bit offset). |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 2920 | */ |
| 2921 | static int bpf_core_spec_match(struct bpf_core_spec *local_spec, |
| 2922 | const struct btf *targ_btf, __u32 targ_id, |
| 2923 | struct bpf_core_spec *targ_spec) |
| 2924 | { |
| 2925 | const struct btf_type *targ_type; |
| 2926 | const struct bpf_core_accessor *local_acc; |
| 2927 | struct bpf_core_accessor *targ_acc; |
| 2928 | int i, sz, matched; |
| 2929 | |
| 2930 | memset(targ_spec, 0, sizeof(*targ_spec)); |
| 2931 | targ_spec->btf = targ_btf; |
| 2932 | |
| 2933 | local_acc = &local_spec->spec[0]; |
| 2934 | targ_acc = &targ_spec->spec[0]; |
| 2935 | |
| 2936 | for (i = 0; i < local_spec->len; i++, local_acc++, targ_acc++) { |
| 2937 | targ_type = skip_mods_and_typedefs(targ_spec->btf, targ_id, |
| 2938 | &targ_id); |
| 2939 | if (!targ_type) |
| 2940 | return -EINVAL; |
| 2941 | |
| 2942 | if (local_acc->name) { |
| 2943 | matched = bpf_core_match_member(local_spec->btf, |
| 2944 | local_acc, |
| 2945 | targ_btf, targ_id, |
| 2946 | targ_spec, &targ_id); |
| 2947 | if (matched <= 0) |
| 2948 | return matched; |
| 2949 | } else { |
| 2950 | /* for i=0, targ_id is already treated as array element |
| 2951 | * type (because it's the original struct), for others |
| 2952 | * we should find array element type first |
| 2953 | */ |
| 2954 | if (i > 0) { |
| 2955 | const struct btf_array *a; |
| 2956 | |
| 2957 | if (!btf_is_array(targ_type)) |
| 2958 | return 0; |
| 2959 | |
| 2960 | a = btf_array(targ_type); |
| 2961 | if (local_acc->idx >= a->nelems) |
| 2962 | return 0; |
| 2963 | if (!skip_mods_and_typedefs(targ_btf, a->type, |
| 2964 | &targ_id)) |
| 2965 | return -EINVAL; |
| 2966 | } |
| 2967 | |
| 2968 | /* too deep struct/union/array nesting */ |
| 2969 | if (targ_spec->raw_len == BPF_CORE_SPEC_MAX_LEN) |
| 2970 | return -E2BIG; |
| 2971 | |
| 2972 | targ_acc->type_id = targ_id; |
| 2973 | targ_acc->idx = local_acc->idx; |
| 2974 | targ_acc->name = NULL; |
| 2975 | targ_spec->len++; |
| 2976 | targ_spec->raw_spec[targ_spec->raw_len] = targ_acc->idx; |
| 2977 | targ_spec->raw_len++; |
| 2978 | |
| 2979 | sz = btf__resolve_size(targ_btf, targ_id); |
| 2980 | if (sz < 0) |
| 2981 | return sz; |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 2982 | targ_spec->bit_offset += local_acc->idx * sz * 8; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 2983 | } |
| 2984 | } |
| 2985 | |
| 2986 | return 1; |
| 2987 | } |
| 2988 | |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 2989 | static int bpf_core_calc_field_relo(const struct bpf_program *prog, |
| 2990 | const struct bpf_field_reloc *relo, |
| 2991 | const struct bpf_core_spec *spec, |
| 2992 | __u32 *val, bool *validate) |
| 2993 | { |
| 2994 | const struct bpf_core_accessor *acc = &spec->spec[spec->len - 1]; |
| 2995 | const struct btf_type *t = btf__type_by_id(spec->btf, acc->type_id); |
| 2996 | __u32 byte_off, byte_sz, bit_off, bit_sz; |
| 2997 | const struct btf_member *m; |
| 2998 | const struct btf_type *mt; |
| 2999 | bool bitfield; |
Andrii Nakryiko | 94f060e | 2019-11-01 15:28:08 -0700 | [diff] [blame] | 3000 | __s64 sz; |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 3001 | |
| 3002 | /* a[n] accessor needs special handling */ |
| 3003 | if (!acc->name) { |
Andrii Nakryiko | 94f060e | 2019-11-01 15:28:08 -0700 | [diff] [blame] | 3004 | if (relo->kind == BPF_FIELD_BYTE_OFFSET) { |
| 3005 | *val = spec->bit_offset / 8; |
| 3006 | } else if (relo->kind == BPF_FIELD_BYTE_SIZE) { |
| 3007 | sz = btf__resolve_size(spec->btf, acc->type_id); |
| 3008 | if (sz < 0) |
| 3009 | return -EINVAL; |
| 3010 | *val = sz; |
| 3011 | } else { |
| 3012 | 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] | 3013 | bpf_program__title(prog, false), |
| 3014 | relo->kind, relo->insn_off / 8); |
| 3015 | return -EINVAL; |
| 3016 | } |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 3017 | if (validate) |
| 3018 | *validate = true; |
| 3019 | return 0; |
| 3020 | } |
| 3021 | |
| 3022 | m = btf_members(t) + acc->idx; |
| 3023 | mt = skip_mods_and_typedefs(spec->btf, m->type, NULL); |
| 3024 | bit_off = spec->bit_offset; |
| 3025 | bit_sz = btf_member_bitfield_size(t, acc->idx); |
| 3026 | |
| 3027 | bitfield = bit_sz > 0; |
| 3028 | if (bitfield) { |
| 3029 | byte_sz = mt->size; |
| 3030 | byte_off = bit_off / 8 / byte_sz * byte_sz; |
| 3031 | /* figure out smallest int size necessary for bitfield load */ |
| 3032 | while (bit_off + bit_sz - byte_off * 8 > byte_sz * 8) { |
| 3033 | if (byte_sz >= 8) { |
| 3034 | /* bitfield can't be read with 64-bit read */ |
| 3035 | pr_warn("prog '%s': relo %d at insn #%d can't be satisfied for bitfield\n", |
| 3036 | bpf_program__title(prog, false), |
| 3037 | relo->kind, relo->insn_off / 8); |
| 3038 | return -E2BIG; |
| 3039 | } |
| 3040 | byte_sz *= 2; |
| 3041 | byte_off = bit_off / 8 / byte_sz * byte_sz; |
| 3042 | } |
| 3043 | } else { |
Andrii Nakryiko | 94f060e | 2019-11-01 15:28:08 -0700 | [diff] [blame] | 3044 | sz = btf__resolve_size(spec->btf, m->type); |
| 3045 | if (sz < 0) |
| 3046 | return -EINVAL; |
| 3047 | byte_sz = sz; |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 3048 | byte_off = spec->bit_offset / 8; |
| 3049 | bit_sz = byte_sz * 8; |
| 3050 | } |
| 3051 | |
| 3052 | /* for bitfields, all the relocatable aspects are ambiguous and we |
| 3053 | * might disagree with compiler, so turn off validation of expected |
| 3054 | * value, except for signedness |
| 3055 | */ |
| 3056 | if (validate) |
| 3057 | *validate = !bitfield; |
| 3058 | |
| 3059 | switch (relo->kind) { |
| 3060 | case BPF_FIELD_BYTE_OFFSET: |
| 3061 | *val = byte_off; |
| 3062 | break; |
| 3063 | case BPF_FIELD_BYTE_SIZE: |
| 3064 | *val = byte_sz; |
| 3065 | break; |
| 3066 | case BPF_FIELD_SIGNED: |
| 3067 | /* enums will be assumed unsigned */ |
| 3068 | *val = btf_is_enum(mt) || |
| 3069 | (btf_int_encoding(mt) & BTF_INT_SIGNED); |
| 3070 | if (validate) |
| 3071 | *validate = true; /* signedness is never ambiguous */ |
| 3072 | break; |
| 3073 | case BPF_FIELD_LSHIFT_U64: |
| 3074 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 3075 | *val = 64 - (bit_off + bit_sz - byte_off * 8); |
| 3076 | #else |
| 3077 | *val = (8 - byte_sz) * 8 + (bit_off - byte_off * 8); |
| 3078 | #endif |
| 3079 | break; |
| 3080 | case BPF_FIELD_RSHIFT_U64: |
| 3081 | *val = 64 - bit_sz; |
| 3082 | if (validate) |
| 3083 | *validate = true; /* right shift is never ambiguous */ |
| 3084 | break; |
| 3085 | case BPF_FIELD_EXISTS: |
| 3086 | default: |
| 3087 | pr_warn("prog '%s': unknown relo %d at insn #%d\n", |
| 3088 | bpf_program__title(prog, false), |
| 3089 | relo->kind, relo->insn_off / 8); |
| 3090 | return -EINVAL; |
| 3091 | } |
| 3092 | |
| 3093 | return 0; |
| 3094 | } |
| 3095 | |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3096 | /* |
| 3097 | * Patch relocatable BPF instruction. |
Andrii Nakryiko | 62561eb | 2019-10-15 11:28:47 -0700 | [diff] [blame] | 3098 | * |
| 3099 | * Patched value is determined by relocation kind and target specification. |
| 3100 | * For field existence relocation target spec will be NULL if field is not |
| 3101 | * found. |
| 3102 | * Expected insn->imm value is determined using relocation kind and local |
| 3103 | * spec, and is checked before patching instruction. If actual insn->imm value |
| 3104 | * is wrong, bail out with error. |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3105 | * |
| 3106 | * Currently three kinds of BPF instructions are supported: |
| 3107 | * 1. rX = <imm> (assignment with immediate operand); |
| 3108 | * 2. rX += <imm> (arithmetic operations with immediate operand); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3109 | */ |
Andrii Nakryiko | 62561eb | 2019-10-15 11:28:47 -0700 | [diff] [blame] | 3110 | static int bpf_core_reloc_insn(struct bpf_program *prog, |
| 3111 | const struct bpf_field_reloc *relo, |
| 3112 | const struct bpf_core_spec *local_spec, |
| 3113 | const struct bpf_core_spec *targ_spec) |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3114 | { |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 3115 | bool failed = false, validate = true; |
Andrii Nakryiko | 62561eb | 2019-10-15 11:28:47 -0700 | [diff] [blame] | 3116 | __u32 orig_val, new_val; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3117 | struct bpf_insn *insn; |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 3118 | int insn_idx, err; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3119 | __u8 class; |
| 3120 | |
Andrii Nakryiko | 62561eb | 2019-10-15 11:28:47 -0700 | [diff] [blame] | 3121 | if (relo->insn_off % sizeof(struct bpf_insn)) |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3122 | return -EINVAL; |
Andrii Nakryiko | 62561eb | 2019-10-15 11:28:47 -0700 | [diff] [blame] | 3123 | insn_idx = relo->insn_off / sizeof(struct bpf_insn); |
| 3124 | |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 3125 | if (relo->kind == BPF_FIELD_EXISTS) { |
Andrii Nakryiko | 62561eb | 2019-10-15 11:28:47 -0700 | [diff] [blame] | 3126 | orig_val = 1; /* can't generate EXISTS relo w/o local field */ |
| 3127 | new_val = targ_spec ? 1 : 0; |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 3128 | } else if (!targ_spec) { |
| 3129 | failed = true; |
| 3130 | new_val = (__u32)-1; |
| 3131 | } else { |
| 3132 | err = bpf_core_calc_field_relo(prog, relo, local_spec, |
| 3133 | &orig_val, &validate); |
| 3134 | if (err) |
| 3135 | return err; |
| 3136 | err = bpf_core_calc_field_relo(prog, relo, targ_spec, |
| 3137 | &new_val, NULL); |
| 3138 | if (err) |
| 3139 | return err; |
Andrii Nakryiko | 62561eb | 2019-10-15 11:28:47 -0700 | [diff] [blame] | 3140 | } |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3141 | |
| 3142 | insn = &prog->insns[insn_idx]; |
| 3143 | class = BPF_CLASS(insn->code); |
| 3144 | |
| 3145 | if (class == BPF_ALU || class == BPF_ALU64) { |
| 3146 | if (BPF_SRC(insn->code) != BPF_K) |
| 3147 | return -EINVAL; |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 3148 | if (!failed && validate && insn->imm != orig_val) { |
| 3149 | pr_warn("prog '%s': unexpected insn #%d value: got %u, exp %u -> %u\n", |
| 3150 | bpf_program__title(prog, false), insn_idx, |
| 3151 | insn->imm, orig_val, new_val); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3152 | return -EINVAL; |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 3153 | } |
| 3154 | orig_val = insn->imm; |
Andrii Nakryiko | 62561eb | 2019-10-15 11:28:47 -0700 | [diff] [blame] | 3155 | insn->imm = new_val; |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 3156 | pr_debug("prog '%s': patched insn #%d (ALU/ALU64)%s imm %u -> %u\n", |
| 3157 | bpf_program__title(prog, false), insn_idx, |
| 3158 | failed ? " w/ failed reloc" : "", orig_val, new_val); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3159 | } else { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3160 | pr_warn("prog '%s': trying to relocate unrecognized insn #%d, code:%x, src:%x, dst:%x, off:%x, imm:%x\n", |
| 3161 | bpf_program__title(prog, false), |
| 3162 | insn_idx, insn->code, insn->src_reg, insn->dst_reg, |
| 3163 | insn->off, insn->imm); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3164 | return -EINVAL; |
| 3165 | } |
Andrii Nakryiko | 62561eb | 2019-10-15 11:28:47 -0700 | [diff] [blame] | 3166 | |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3167 | return 0; |
| 3168 | } |
| 3169 | |
Andrii Nakryiko | a1916a1 | 2019-08-13 11:54:43 -0700 | [diff] [blame] | 3170 | static struct btf *btf_load_raw(const char *path) |
| 3171 | { |
| 3172 | struct btf *btf; |
| 3173 | size_t read_cnt; |
| 3174 | struct stat st; |
| 3175 | void *data; |
| 3176 | FILE *f; |
| 3177 | |
| 3178 | if (stat(path, &st)) |
| 3179 | return ERR_PTR(-errno); |
| 3180 | |
| 3181 | data = malloc(st.st_size); |
| 3182 | if (!data) |
| 3183 | return ERR_PTR(-ENOMEM); |
| 3184 | |
| 3185 | f = fopen(path, "rb"); |
| 3186 | if (!f) { |
| 3187 | btf = ERR_PTR(-errno); |
| 3188 | goto cleanup; |
| 3189 | } |
| 3190 | |
| 3191 | read_cnt = fread(data, 1, st.st_size, f); |
| 3192 | fclose(f); |
| 3193 | if (read_cnt < st.st_size) { |
| 3194 | btf = ERR_PTR(-EBADF); |
| 3195 | goto cleanup; |
| 3196 | } |
| 3197 | |
| 3198 | btf = btf__new(data, read_cnt); |
| 3199 | |
| 3200 | cleanup: |
| 3201 | free(data); |
| 3202 | return btf; |
| 3203 | } |
| 3204 | |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3205 | /* |
| 3206 | * Probe few well-known locations for vmlinux kernel image and try to load BTF |
| 3207 | * data out of it to use for target BTF. |
| 3208 | */ |
| 3209 | static struct btf *bpf_core_find_kernel_btf(void) |
| 3210 | { |
Andrii Nakryiko | a1916a1 | 2019-08-13 11:54:43 -0700 | [diff] [blame] | 3211 | struct { |
| 3212 | const char *path_fmt; |
| 3213 | bool raw_btf; |
| 3214 | } locations[] = { |
| 3215 | /* try canonical vmlinux BTF through sysfs first */ |
| 3216 | { "/sys/kernel/btf/vmlinux", true /* raw BTF */ }, |
| 3217 | /* fall back to trying to find vmlinux ELF on disk otherwise */ |
| 3218 | { "/boot/vmlinux-%1$s" }, |
| 3219 | { "/lib/modules/%1$s/vmlinux-%1$s" }, |
| 3220 | { "/lib/modules/%1$s/build/vmlinux" }, |
| 3221 | { "/usr/lib/modules/%1$s/kernel/vmlinux" }, |
| 3222 | { "/usr/lib/debug/boot/vmlinux-%1$s" }, |
| 3223 | { "/usr/lib/debug/boot/vmlinux-%1$s.debug" }, |
| 3224 | { "/usr/lib/debug/lib/modules/%1$s/vmlinux" }, |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3225 | }; |
| 3226 | char path[PATH_MAX + 1]; |
| 3227 | struct utsname buf; |
| 3228 | struct btf *btf; |
| 3229 | int i; |
| 3230 | |
| 3231 | uname(&buf); |
| 3232 | |
| 3233 | for (i = 0; i < ARRAY_SIZE(locations); i++) { |
Andrii Nakryiko | a1916a1 | 2019-08-13 11:54:43 -0700 | [diff] [blame] | 3234 | snprintf(path, PATH_MAX, locations[i].path_fmt, buf.release); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3235 | |
| 3236 | if (access(path, R_OK)) |
| 3237 | continue; |
| 3238 | |
Andrii Nakryiko | a1916a1 | 2019-08-13 11:54:43 -0700 | [diff] [blame] | 3239 | if (locations[i].raw_btf) |
| 3240 | btf = btf_load_raw(path); |
| 3241 | else |
| 3242 | btf = btf__parse_elf(path, NULL); |
| 3243 | |
| 3244 | pr_debug("loading kernel BTF '%s': %ld\n", |
| 3245 | path, IS_ERR(btf) ? PTR_ERR(btf) : 0); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3246 | if (IS_ERR(btf)) |
| 3247 | continue; |
| 3248 | |
| 3249 | return btf; |
| 3250 | } |
| 3251 | |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3252 | pr_warn("failed to find valid kernel BTF\n"); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3253 | return ERR_PTR(-ESRCH); |
| 3254 | } |
| 3255 | |
| 3256 | /* Output spec definition in the format: |
| 3257 | * [<type-id>] (<type-name>) + <raw-spec> => <offset>@<spec>, |
| 3258 | * where <spec> is a C-syntax view of recorded field access, e.g.: x.a[3].b |
| 3259 | */ |
| 3260 | static void bpf_core_dump_spec(int level, const struct bpf_core_spec *spec) |
| 3261 | { |
| 3262 | const struct btf_type *t; |
| 3263 | const char *s; |
| 3264 | __u32 type_id; |
| 3265 | int i; |
| 3266 | |
| 3267 | type_id = spec->spec[0].type_id; |
| 3268 | t = btf__type_by_id(spec->btf, type_id); |
| 3269 | s = btf__name_by_offset(spec->btf, t->name_off); |
| 3270 | libbpf_print(level, "[%u] %s + ", type_id, s); |
| 3271 | |
| 3272 | for (i = 0; i < spec->raw_len; i++) |
| 3273 | libbpf_print(level, "%d%s", spec->raw_spec[i], |
| 3274 | i == spec->raw_len - 1 ? " => " : ":"); |
| 3275 | |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 3276 | libbpf_print(level, "%u.%u @ &x", |
| 3277 | spec->bit_offset / 8, spec->bit_offset % 8); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3278 | |
| 3279 | for (i = 0; i < spec->len; i++) { |
| 3280 | if (spec->spec[i].name) |
| 3281 | libbpf_print(level, ".%s", spec->spec[i].name); |
| 3282 | else |
| 3283 | libbpf_print(level, "[%u]", spec->spec[i].idx); |
| 3284 | } |
| 3285 | |
| 3286 | } |
| 3287 | |
| 3288 | static size_t bpf_core_hash_fn(const void *key, void *ctx) |
| 3289 | { |
| 3290 | return (size_t)key; |
| 3291 | } |
| 3292 | |
| 3293 | static bool bpf_core_equal_fn(const void *k1, const void *k2, void *ctx) |
| 3294 | { |
| 3295 | return k1 == k2; |
| 3296 | } |
| 3297 | |
| 3298 | static void *u32_as_hash_key(__u32 x) |
| 3299 | { |
| 3300 | return (void *)(uintptr_t)x; |
| 3301 | } |
| 3302 | |
| 3303 | /* |
| 3304 | * CO-RE relocate single instruction. |
| 3305 | * |
| 3306 | * The outline and important points of the algorithm: |
| 3307 | * 1. For given local type, find corresponding candidate target types. |
| 3308 | * Candidate type is a type with the same "essential" name, ignoring |
| 3309 | * everything after last triple underscore (___). E.g., `sample`, |
| 3310 | * `sample___flavor_one`, `sample___flavor_another_one`, are all candidates |
| 3311 | * for each other. Names with triple underscore are referred to as |
| 3312 | * "flavors" and are useful, among other things, to allow to |
| 3313 | * specify/support incompatible variations of the same kernel struct, which |
| 3314 | * might differ between different kernel versions and/or build |
| 3315 | * configurations. |
| 3316 | * |
| 3317 | * N.B. Struct "flavors" could be generated by bpftool's BTF-to-C |
| 3318 | * converter, when deduplicated BTF of a kernel still contains more than |
| 3319 | * one different types with the same name. In that case, ___2, ___3, etc |
| 3320 | * are appended starting from second name conflict. But start flavors are |
| 3321 | * also useful to be defined "locally", in BPF program, to extract same |
| 3322 | * data from incompatible changes between different kernel |
| 3323 | * versions/configurations. For instance, to handle field renames between |
| 3324 | * kernel versions, one can use two flavors of the struct name with the |
| 3325 | * same common name and use conditional relocations to extract that field, |
| 3326 | * depending on target kernel version. |
| 3327 | * 2. For each candidate type, try to match local specification to this |
| 3328 | * candidate target type. Matching involves finding corresponding |
| 3329 | * high-level spec accessors, meaning that all named fields should match, |
| 3330 | * as well as all array accesses should be within the actual bounds. Also, |
| 3331 | * types should be compatible (see bpf_core_fields_are_compat for details). |
| 3332 | * 3. It is supported and expected that there might be multiple flavors |
| 3333 | * 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] | 3334 | * offsets across all candidates, there is no error. If there is any |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3335 | * ambiguity, CO-RE relocation will fail. This is necessary to accomodate |
| 3336 | * imprefection of BTF deduplication, which can cause slight duplication of |
| 3337 | * the same BTF type, if some directly or indirectly referenced (by |
| 3338 | * pointer) type gets resolved to different actual types in different |
| 3339 | * object files. If such situation occurs, deduplicated BTF will end up |
| 3340 | * with two (or more) structurally identical types, which differ only in |
| 3341 | * types they refer to through pointer. This should be OK in most cases and |
| 3342 | * is not an error. |
| 3343 | * 4. Candidate types search is performed by linearly scanning through all |
| 3344 | * types in target BTF. It is anticipated that this is overall more |
| 3345 | * efficient memory-wise and not significantly worse (if not better) |
| 3346 | * CPU-wise compared to prebuilding a map from all local type names to |
| 3347 | * a list of candidate type names. It's also sped up by caching resolved |
| 3348 | * 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] | 3349 | * least one bpf_field_reloc associated with it. This list is shared |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3350 | * between multiple relocations for the same type ID and is updated as some |
| 3351 | * of the candidates are pruned due to structural incompatibility. |
| 3352 | */ |
Andrii Nakryiko | 511bb00 | 2019-10-15 11:28:45 -0700 | [diff] [blame] | 3353 | static int bpf_core_reloc_field(struct bpf_program *prog, |
| 3354 | const struct bpf_field_reloc *relo, |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3355 | int relo_idx, |
| 3356 | const struct btf *local_btf, |
| 3357 | const struct btf *targ_btf, |
| 3358 | struct hashmap *cand_cache) |
| 3359 | { |
| 3360 | const char *prog_name = bpf_program__title(prog, false); |
| 3361 | struct bpf_core_spec local_spec, cand_spec, targ_spec; |
| 3362 | const void *type_key = u32_as_hash_key(relo->type_id); |
| 3363 | const struct btf_type *local_type, *cand_type; |
| 3364 | const char *local_name, *cand_name; |
| 3365 | struct ids_vec *cand_ids; |
| 3366 | __u32 local_id, cand_id; |
| 3367 | const char *spec_str; |
| 3368 | int i, j, err; |
| 3369 | |
| 3370 | local_id = relo->type_id; |
| 3371 | local_type = btf__type_by_id(local_btf, local_id); |
| 3372 | if (!local_type) |
| 3373 | return -EINVAL; |
| 3374 | |
| 3375 | local_name = btf__name_by_offset(local_btf, local_type->name_off); |
| 3376 | if (str_is_empty(local_name)) |
| 3377 | return -EINVAL; |
| 3378 | |
| 3379 | spec_str = btf__name_by_offset(local_btf, relo->access_str_off); |
| 3380 | if (str_is_empty(spec_str)) |
| 3381 | return -EINVAL; |
| 3382 | |
| 3383 | err = bpf_core_spec_parse(local_btf, local_id, spec_str, &local_spec); |
| 3384 | if (err) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3385 | pr_warn("prog '%s': relo #%d: parsing [%d] %s + %s failed: %d\n", |
| 3386 | prog_name, relo_idx, local_id, local_name, spec_str, |
| 3387 | err); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3388 | return -EINVAL; |
| 3389 | } |
| 3390 | |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 3391 | pr_debug("prog '%s': relo #%d: kind %d, spec is ", prog_name, relo_idx, |
| 3392 | relo->kind); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3393 | bpf_core_dump_spec(LIBBPF_DEBUG, &local_spec); |
| 3394 | libbpf_print(LIBBPF_DEBUG, "\n"); |
| 3395 | |
| 3396 | if (!hashmap__find(cand_cache, type_key, (void **)&cand_ids)) { |
| 3397 | cand_ids = bpf_core_find_cands(local_btf, local_id, targ_btf); |
| 3398 | if (IS_ERR(cand_ids)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3399 | pr_warn("prog '%s': relo #%d: target candidate search failed for [%d] %s: %ld", |
| 3400 | prog_name, relo_idx, local_id, local_name, |
| 3401 | PTR_ERR(cand_ids)); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3402 | return PTR_ERR(cand_ids); |
| 3403 | } |
| 3404 | err = hashmap__set(cand_cache, type_key, cand_ids, NULL, NULL); |
| 3405 | if (err) { |
| 3406 | bpf_core_free_cands(cand_ids); |
| 3407 | return err; |
| 3408 | } |
| 3409 | } |
| 3410 | |
| 3411 | for (i = 0, j = 0; i < cand_ids->len; i++) { |
| 3412 | cand_id = cand_ids->data[i]; |
| 3413 | cand_type = btf__type_by_id(targ_btf, cand_id); |
| 3414 | cand_name = btf__name_by_offset(targ_btf, cand_type->name_off); |
| 3415 | |
| 3416 | err = bpf_core_spec_match(&local_spec, targ_btf, |
| 3417 | cand_id, &cand_spec); |
| 3418 | pr_debug("prog '%s': relo #%d: matching candidate #%d %s against spec ", |
| 3419 | prog_name, relo_idx, i, cand_name); |
| 3420 | bpf_core_dump_spec(LIBBPF_DEBUG, &cand_spec); |
| 3421 | libbpf_print(LIBBPF_DEBUG, ": %d\n", err); |
| 3422 | if (err < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3423 | pr_warn("prog '%s': relo #%d: matching error: %d\n", |
| 3424 | prog_name, relo_idx, err); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3425 | return err; |
| 3426 | } |
| 3427 | if (err == 0) |
| 3428 | continue; |
| 3429 | |
| 3430 | if (j == 0) { |
| 3431 | targ_spec = cand_spec; |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 3432 | } else if (cand_spec.bit_offset != targ_spec.bit_offset) { |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3433 | /* if there are many candidates, they should all |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 3434 | * resolve to the same bit offset |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3435 | */ |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3436 | pr_warn("prog '%s': relo #%d: offset ambiguity: %u != %u\n", |
Andrii Nakryiko | ee26dad | 2019-11-01 15:28:07 -0700 | [diff] [blame] | 3437 | prog_name, relo_idx, cand_spec.bit_offset, |
| 3438 | targ_spec.bit_offset); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3439 | return -EINVAL; |
| 3440 | } |
| 3441 | |
| 3442 | cand_ids->data[j++] = cand_spec.spec[0].type_id; |
| 3443 | } |
| 3444 | |
Andrii Nakryiko | 62561eb | 2019-10-15 11:28:47 -0700 | [diff] [blame] | 3445 | /* |
| 3446 | * For BPF_FIELD_EXISTS relo or when relaxed CO-RE reloc mode is |
| 3447 | * requested, it's expected that we might not find any candidates. |
| 3448 | * In this case, if field wasn't found in any candidate, the list of |
| 3449 | * candidates shouldn't change at all, we'll just handle relocating |
| 3450 | * appropriately, depending on relo's kind. |
| 3451 | */ |
| 3452 | if (j > 0) |
| 3453 | cand_ids->len = j; |
| 3454 | |
| 3455 | if (j == 0 && !prog->obj->relaxed_core_relocs && |
| 3456 | relo->kind != BPF_FIELD_EXISTS) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3457 | pr_warn("prog '%s': relo #%d: no matching targets found for [%d] %s + %s\n", |
| 3458 | prog_name, relo_idx, local_id, local_name, spec_str); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3459 | return -ESRCH; |
| 3460 | } |
| 3461 | |
Andrii Nakryiko | 62561eb | 2019-10-15 11:28:47 -0700 | [diff] [blame] | 3462 | /* bpf_core_reloc_insn should know how to handle missing targ_spec */ |
| 3463 | err = bpf_core_reloc_insn(prog, relo, &local_spec, |
| 3464 | j ? &targ_spec : NULL); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3465 | if (err) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3466 | pr_warn("prog '%s': relo #%d: failed to patch insn at offset %d: %d\n", |
| 3467 | prog_name, relo_idx, relo->insn_off, err); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3468 | return -EINVAL; |
| 3469 | } |
| 3470 | |
| 3471 | return 0; |
| 3472 | } |
| 3473 | |
| 3474 | static int |
Andrii Nakryiko | 511bb00 | 2019-10-15 11:28:45 -0700 | [diff] [blame] | 3475 | 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] | 3476 | { |
| 3477 | const struct btf_ext_info_sec *sec; |
Andrii Nakryiko | 511bb00 | 2019-10-15 11:28:45 -0700 | [diff] [blame] | 3478 | const struct bpf_field_reloc *rec; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3479 | const struct btf_ext_info *seg; |
| 3480 | struct hashmap_entry *entry; |
| 3481 | struct hashmap *cand_cache = NULL; |
| 3482 | struct bpf_program *prog; |
| 3483 | struct btf *targ_btf; |
| 3484 | const char *sec_name; |
| 3485 | int i, err = 0; |
| 3486 | |
| 3487 | if (targ_btf_path) |
| 3488 | targ_btf = btf__parse_elf(targ_btf_path, NULL); |
| 3489 | else |
| 3490 | targ_btf = bpf_core_find_kernel_btf(); |
| 3491 | if (IS_ERR(targ_btf)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3492 | 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] | 3493 | return PTR_ERR(targ_btf); |
| 3494 | } |
| 3495 | |
| 3496 | cand_cache = hashmap__new(bpf_core_hash_fn, bpf_core_equal_fn, NULL); |
| 3497 | if (IS_ERR(cand_cache)) { |
| 3498 | err = PTR_ERR(cand_cache); |
| 3499 | goto out; |
| 3500 | } |
| 3501 | |
Andrii Nakryiko | 511bb00 | 2019-10-15 11:28:45 -0700 | [diff] [blame] | 3502 | seg = &obj->btf_ext->field_reloc_info; |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3503 | for_each_btf_ext_sec(seg, sec) { |
| 3504 | sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off); |
| 3505 | if (str_is_empty(sec_name)) { |
| 3506 | err = -EINVAL; |
| 3507 | goto out; |
| 3508 | } |
| 3509 | prog = bpf_object__find_program_by_title(obj, sec_name); |
| 3510 | if (!prog) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3511 | pr_warn("failed to find program '%s' for CO-RE offset relocation\n", |
| 3512 | sec_name); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3513 | err = -EINVAL; |
| 3514 | goto out; |
| 3515 | } |
| 3516 | |
| 3517 | pr_debug("prog '%s': performing %d CO-RE offset relocs\n", |
| 3518 | sec_name, sec->num_info); |
| 3519 | |
| 3520 | for_each_btf_ext_rec(seg, sec, i, rec) { |
Andrii Nakryiko | 511bb00 | 2019-10-15 11:28:45 -0700 | [diff] [blame] | 3521 | err = bpf_core_reloc_field(prog, rec, i, obj->btf, |
| 3522 | targ_btf, cand_cache); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3523 | if (err) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3524 | pr_warn("prog '%s': relo #%d: failed to relocate: %d\n", |
| 3525 | sec_name, i, err); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3526 | goto out; |
| 3527 | } |
| 3528 | } |
| 3529 | } |
| 3530 | |
| 3531 | out: |
| 3532 | btf__free(targ_btf); |
| 3533 | if (!IS_ERR_OR_NULL(cand_cache)) { |
| 3534 | hashmap__for_each_entry(cand_cache, entry, i) { |
| 3535 | bpf_core_free_cands(entry->value); |
| 3536 | } |
| 3537 | hashmap__free(cand_cache); |
| 3538 | } |
| 3539 | return err; |
| 3540 | } |
| 3541 | |
| 3542 | static int |
| 3543 | bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path) |
| 3544 | { |
| 3545 | int err = 0; |
| 3546 | |
Andrii Nakryiko | 511bb00 | 2019-10-15 11:28:45 -0700 | [diff] [blame] | 3547 | if (obj->btf_ext->field_reloc_info.len) |
| 3548 | err = bpf_core_reloc_fields(obj, targ_btf_path); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3549 | |
| 3550 | return err; |
| 3551 | } |
| 3552 | |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 3553 | static int |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 3554 | bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj, |
| 3555 | struct reloc_desc *relo) |
| 3556 | { |
| 3557 | struct bpf_insn *insn, *new_insn; |
| 3558 | struct bpf_program *text; |
| 3559 | size_t new_cnt; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 3560 | int err; |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 3561 | |
| 3562 | if (relo->type != RELO_CALL) |
| 3563 | return -LIBBPF_ERRNO__RELOC; |
| 3564 | |
| 3565 | if (prog->idx == obj->efile.text_shndx) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3566 | pr_warn("relo in .text insn %d into off %d\n", |
| 3567 | relo->insn_idx, relo->text_off); |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 3568 | return -LIBBPF_ERRNO__RELOC; |
| 3569 | } |
| 3570 | |
| 3571 | if (prog->main_prog_cnt == 0) { |
| 3572 | text = bpf_object__find_prog_by_idx(obj, obj->efile.text_shndx); |
| 3573 | if (!text) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3574 | pr_warn("no .text section found yet relo into text exist\n"); |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 3575 | return -LIBBPF_ERRNO__RELOC; |
| 3576 | } |
| 3577 | new_cnt = prog->insns_cnt + text->insns_cnt; |
Jakub Kicinski | 531b014 | 2018-07-10 14:43:05 -0700 | [diff] [blame] | 3578 | new_insn = reallocarray(prog->insns, new_cnt, sizeof(*insn)); |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 3579 | if (!new_insn) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3580 | pr_warn("oom in prog realloc\n"); |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 3581 | return -ENOMEM; |
| 3582 | } |
Andrii Nakryiko | 3dc5e05 | 2019-11-06 18:08:51 -0800 | [diff] [blame] | 3583 | prog->insns = new_insn; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 3584 | |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 3585 | if (obj->btf_ext) { |
| 3586 | err = bpf_program_reloc_btf_ext(prog, obj, |
| 3587 | text->section_name, |
| 3588 | prog->insns_cnt); |
| 3589 | if (err) |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 3590 | return err; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 3591 | } |
| 3592 | |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 3593 | memcpy(new_insn + prog->insns_cnt, text->insns, |
| 3594 | text->insns_cnt * sizeof(*insn)); |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 3595 | prog->main_prog_cnt = prog->insns_cnt; |
| 3596 | prog->insns_cnt = new_cnt; |
Jeremy Cline | b1a2ce8 | 2018-02-20 01:00:07 +0000 | [diff] [blame] | 3597 | pr_debug("added %zd insn from %s to prog %s\n", |
| 3598 | text->insns_cnt, text->section_name, |
| 3599 | prog->section_name); |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 3600 | } |
| 3601 | insn = &prog->insns[relo->insn_idx]; |
Andrii Nakryiko | a0d7da2 | 2019-11-19 14:44:47 -0800 | [diff] [blame] | 3602 | insn->imm += relo->text_off + prog->main_prog_cnt - relo->insn_idx; |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 3603 | return 0; |
| 3604 | } |
| 3605 | |
| 3606 | static int |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 3607 | bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj) |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 3608 | { |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 3609 | int i, err; |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 3610 | |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 3611 | if (!prog) |
| 3612 | return 0; |
| 3613 | |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 3614 | if (obj->btf_ext) { |
| 3615 | err = bpf_program_reloc_btf_ext(prog, obj, |
| 3616 | prog->section_name, 0); |
| 3617 | if (err) |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 3618 | return err; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 3619 | } |
| 3620 | |
| 3621 | if (!prog->reloc_desc) |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 3622 | return 0; |
| 3623 | |
| 3624 | for (i = 0; i < prog->nr_reloc; i++) { |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 3625 | if (prog->reloc_desc[i].type == RELO_LD64 || |
| 3626 | prog->reloc_desc[i].type == RELO_DATA) { |
| 3627 | bool relo_data = prog->reloc_desc[i].type == RELO_DATA; |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 3628 | struct bpf_insn *insns = prog->insns; |
| 3629 | int insn_idx, map_idx; |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 3630 | |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 3631 | insn_idx = prog->reloc_desc[i].insn_idx; |
| 3632 | map_idx = prog->reloc_desc[i].map_idx; |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 3633 | |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 3634 | if (insn_idx + 1 >= (int)prog->insns_cnt) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3635 | pr_warn("relocation out of range: '%s'\n", |
| 3636 | prog->section_name); |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 3637 | return -LIBBPF_ERRNO__RELOC; |
| 3638 | } |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 3639 | |
| 3640 | if (!relo_data) { |
| 3641 | insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD; |
| 3642 | } else { |
| 3643 | insns[insn_idx].src_reg = BPF_PSEUDO_MAP_VALUE; |
| 3644 | insns[insn_idx + 1].imm = insns[insn_idx].imm; |
| 3645 | } |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 3646 | insns[insn_idx].imm = obj->maps[map_idx].fd; |
Joe Stringer | f8c7a4d | 2019-04-09 23:20:12 +0200 | [diff] [blame] | 3647 | } else if (prog->reloc_desc[i].type == RELO_CALL) { |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 3648 | err = bpf_program__reloc_text(prog, obj, |
| 3649 | &prog->reloc_desc[i]); |
| 3650 | if (err) |
| 3651 | return err; |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 3652 | } |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 3653 | } |
| 3654 | |
| 3655 | zfree(&prog->reloc_desc); |
| 3656 | prog->nr_reloc = 0; |
| 3657 | return 0; |
| 3658 | } |
| 3659 | |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 3660 | static int |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3661 | bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path) |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 3662 | { |
| 3663 | struct bpf_program *prog; |
| 3664 | size_t i; |
| 3665 | int err; |
| 3666 | |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3667 | if (obj->btf_ext) { |
| 3668 | err = bpf_object__relocate_core(obj, targ_btf_path); |
| 3669 | if (err) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3670 | pr_warn("failed to perform CO-RE relocations: %d\n", |
| 3671 | err); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 3672 | return err; |
| 3673 | } |
| 3674 | } |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 3675 | for (i = 0; i < obj->nr_programs; i++) { |
| 3676 | prog = &obj->programs[i]; |
| 3677 | |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 3678 | err = bpf_program__relocate(prog, obj); |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 3679 | if (err) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3680 | pr_warn("failed to relocate '%s'\n", prog->section_name); |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 3681 | return err; |
| 3682 | } |
| 3683 | } |
| 3684 | return 0; |
| 3685 | } |
| 3686 | |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 3687 | static int bpf_object__collect_reloc(struct bpf_object *obj) |
| 3688 | { |
| 3689 | int i, err; |
| 3690 | |
| 3691 | if (!obj_elf_valid(obj)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3692 | pr_warn("Internal error: elf object is closed\n"); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 3693 | return -LIBBPF_ERRNO__INTERNAL; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 3694 | } |
| 3695 | |
Andrii Nakryiko | 1f8e2bc | 2019-11-20 23:07:41 -0800 | [diff] [blame] | 3696 | for (i = 0; i < obj->efile.nr_reloc_sects; i++) { |
| 3697 | GElf_Shdr *shdr = &obj->efile.reloc_sects[i].shdr; |
| 3698 | Elf_Data *data = obj->efile.reloc_sects[i].data; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 3699 | int idx = shdr->sh_info; |
| 3700 | struct bpf_program *prog; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 3701 | |
| 3702 | if (shdr->sh_type != SHT_REL) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3703 | pr_warn("internal error at %d\n", __LINE__); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 3704 | return -LIBBPF_ERRNO__INTERNAL; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 3705 | } |
| 3706 | |
| 3707 | prog = bpf_object__find_prog_by_idx(obj, idx); |
| 3708 | if (!prog) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3709 | pr_warn("relocation failed: no section(%d)\n", idx); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 3710 | return -LIBBPF_ERRNO__RELOC; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 3711 | } |
| 3712 | |
Andrii Nakryiko | 399dc65 | 2019-05-29 10:36:11 -0700 | [diff] [blame] | 3713 | err = bpf_program__collect_reloc(prog, shdr, data, obj); |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 3714 | if (err) |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 3715 | return err; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 3716 | } |
| 3717 | return 0; |
| 3718 | } |
| 3719 | |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 3720 | static int |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 3721 | 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] | 3722 | char *license, __u32 kern_version, int *pfd) |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 3723 | { |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 3724 | struct bpf_load_program_attr load_attr; |
Thomas Richter | 1ce6a9f | 2018-07-30 10:53:23 +0200 | [diff] [blame] | 3725 | char *cp, errmsg[STRERR_BUFSIZE]; |
Alexei Starovoitov | da11b41 | 2019-04-01 21:27:47 -0700 | [diff] [blame] | 3726 | int log_buf_size = BPF_LOG_BUF_SIZE; |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 3727 | char *log_buf; |
Andrii Nakryiko | 5d01ab7 | 2019-07-26 14:24:38 -0700 | [diff] [blame] | 3728 | int btf_fd, ret; |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 3729 | |
Andrii Nakryiko | fba01a0 | 2019-05-29 10:36:08 -0700 | [diff] [blame] | 3730 | if (!insns || !insns_cnt) |
| 3731 | return -EINVAL; |
| 3732 | |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 3733 | memset(&load_attr, 0, sizeof(struct bpf_load_program_attr)); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 3734 | load_attr.prog_type = prog->type; |
| 3735 | load_attr.expected_attach_type = prog->expected_attach_type; |
Stanislav Fomichev | 5b32a23 | 2018-11-20 17:11:21 -0800 | [diff] [blame] | 3736 | if (prog->caps->name) |
| 3737 | load_attr.name = prog->name; |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 3738 | load_attr.insns = insns; |
| 3739 | load_attr.insns_cnt = insns_cnt; |
| 3740 | load_attr.license = license; |
Alexei Starovoitov | e7bf94d | 2019-11-14 10:57:18 -0800 | [diff] [blame] | 3741 | if (prog->type == BPF_PROG_TYPE_TRACING) { |
| 3742 | load_attr.attach_prog_fd = prog->attach_prog_fd; |
| 3743 | load_attr.attach_btf_id = prog->attach_btf_id; |
| 3744 | } else { |
| 3745 | load_attr.kern_version = kern_version; |
| 3746 | load_attr.prog_ifindex = prog->prog_ifindex; |
| 3747 | } |
Andrii Nakryiko | 3415ec6 | 2019-08-01 00:24:05 -0700 | [diff] [blame] | 3748 | /* if .BTF.ext was loaded, kernel supports associated BTF for prog */ |
| 3749 | if (prog->obj->btf_ext) |
| 3750 | btf_fd = bpf_object__btf_fd(prog->obj); |
| 3751 | else |
| 3752 | btf_fd = -1; |
Andrii Nakryiko | 5d01ab7 | 2019-07-26 14:24:38 -0700 | [diff] [blame] | 3753 | load_attr.prog_btf_fd = btf_fd >= 0 ? btf_fd : 0; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 3754 | load_attr.func_info = prog->func_info; |
| 3755 | load_attr.func_info_rec_size = prog->func_info_rec_size; |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 3756 | load_attr.func_info_cnt = prog->func_info_cnt; |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 3757 | load_attr.line_info = prog->line_info; |
| 3758 | load_attr.line_info_rec_size = prog->line_info_rec_size; |
| 3759 | load_attr.line_info_cnt = prog->line_info_cnt; |
Alexei Starovoitov | da11b41 | 2019-04-01 21:27:47 -0700 | [diff] [blame] | 3760 | load_attr.log_level = prog->log_level; |
Jiong Wang | 0465619 | 2019-05-24 23:25:19 +0100 | [diff] [blame] | 3761 | load_attr.prog_flags = prog->prog_flags; |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 3762 | |
Alexei Starovoitov | da11b41 | 2019-04-01 21:27:47 -0700 | [diff] [blame] | 3763 | retry_load: |
| 3764 | log_buf = malloc(log_buf_size); |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 3765 | if (!log_buf) |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3766 | 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] | 3767 | |
Alexei Starovoitov | da11b41 | 2019-04-01 21:27:47 -0700 | [diff] [blame] | 3768 | ret = bpf_load_program_xattr(&load_attr, log_buf, log_buf_size); |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 3769 | |
| 3770 | if (ret >= 0) { |
Alexei Starovoitov | da11b41 | 2019-04-01 21:27:47 -0700 | [diff] [blame] | 3771 | if (load_attr.log_level) |
| 3772 | pr_debug("verifier log:\n%s", log_buf); |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 3773 | *pfd = ret; |
| 3774 | ret = 0; |
| 3775 | goto out; |
| 3776 | } |
| 3777 | |
Alexei Starovoitov | da11b41 | 2019-04-01 21:27:47 -0700 | [diff] [blame] | 3778 | if (errno == ENOSPC) { |
| 3779 | log_buf_size <<= 1; |
| 3780 | free(log_buf); |
| 3781 | goto retry_load; |
| 3782 | } |
Toke Høiland-Jørgensen | 4f33ddb | 2019-11-09 21:37:29 +0100 | [diff] [blame] | 3783 | ret = -errno; |
Andrey Ignatov | 24d6a80 | 2018-10-03 15:26:41 -0700 | [diff] [blame] | 3784 | cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg)); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3785 | pr_warn("load bpf program failed: %s\n", cp); |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 3786 | |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 3787 | if (log_buf && log_buf[0] != '\0') { |
| 3788 | ret = -LIBBPF_ERRNO__VERIFY; |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3789 | pr_warn("-- BEGIN DUMP LOG ---\n"); |
| 3790 | pr_warn("\n%s\n", log_buf); |
| 3791 | pr_warn("-- END LOG --\n"); |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 3792 | } else if (load_attr.insns_cnt >= BPF_MAXINSNS) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3793 | pr_warn("Program too large (%zu insns), at most %d insns\n", |
| 3794 | load_attr.insns_cnt, BPF_MAXINSNS); |
Wang Nan | 705fa21 | 2016-07-13 10:44:02 +0000 | [diff] [blame] | 3795 | ret = -LIBBPF_ERRNO__PROG2BIG; |
Toke Høiland-Jørgensen | 4f33ddb | 2019-11-09 21:37:29 +0100 | [diff] [blame] | 3796 | } else if (load_attr.prog_type != BPF_PROG_TYPE_KPROBE) { |
Wang Nan | 705fa21 | 2016-07-13 10:44:02 +0000 | [diff] [blame] | 3797 | /* Wrong program type? */ |
Toke Høiland-Jørgensen | 4f33ddb | 2019-11-09 21:37:29 +0100 | [diff] [blame] | 3798 | int fd; |
Wang Nan | 705fa21 | 2016-07-13 10:44:02 +0000 | [diff] [blame] | 3799 | |
Toke Høiland-Jørgensen | 4f33ddb | 2019-11-09 21:37:29 +0100 | [diff] [blame] | 3800 | load_attr.prog_type = BPF_PROG_TYPE_KPROBE; |
| 3801 | load_attr.expected_attach_type = 0; |
| 3802 | fd = bpf_load_program_xattr(&load_attr, NULL, 0); |
| 3803 | if (fd >= 0) { |
| 3804 | close(fd); |
| 3805 | ret = -LIBBPF_ERRNO__PROGTYPE; |
| 3806 | goto out; |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 3807 | } |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 3808 | } |
| 3809 | |
| 3810 | out: |
| 3811 | free(log_buf); |
| 3812 | return ret; |
| 3813 | } |
| 3814 | |
Joe Stringer | 29cd77f | 2018-10-02 13:35:39 -0700 | [diff] [blame] | 3815 | int |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 3816 | bpf_program__load(struct bpf_program *prog, |
Andrey Ignatov | e5b0863 | 2018-10-03 15:26:43 -0700 | [diff] [blame] | 3817 | char *license, __u32 kern_version) |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 3818 | { |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 3819 | int err = 0, fd, i; |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 3820 | |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 3821 | if (prog->instances.nr < 0 || !prog->instances.fds) { |
| 3822 | if (prog->preprocessor) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3823 | pr_warn("Internal error: can't load program '%s'\n", |
| 3824 | prog->section_name); |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 3825 | return -LIBBPF_ERRNO__INTERNAL; |
| 3826 | } |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 3827 | |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 3828 | prog->instances.fds = malloc(sizeof(int)); |
| 3829 | if (!prog->instances.fds) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3830 | pr_warn("Not enough memory for BPF fds\n"); |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 3831 | return -ENOMEM; |
| 3832 | } |
| 3833 | prog->instances.nr = 1; |
| 3834 | prog->instances.fds[0] = -1; |
| 3835 | } |
| 3836 | |
| 3837 | if (!prog->preprocessor) { |
| 3838 | if (prog->instances.nr != 1) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3839 | pr_warn("Program '%s' is inconsistent: nr(%d) != 1\n", |
| 3840 | prog->section_name, prog->instances.nr); |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 3841 | } |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 3842 | err = load_program(prog, prog->insns, prog->insns_cnt, |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 3843 | license, kern_version, &fd); |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 3844 | if (!err) |
| 3845 | prog->instances.fds[0] = fd; |
| 3846 | goto out; |
| 3847 | } |
| 3848 | |
| 3849 | for (i = 0; i < prog->instances.nr; i++) { |
| 3850 | struct bpf_prog_prep_result result; |
| 3851 | bpf_program_prep_t preprocessor = prog->preprocessor; |
| 3852 | |
Andrii Nakryiko | 1ad9cbb | 2019-02-13 10:25:53 -0800 | [diff] [blame] | 3853 | memset(&result, 0, sizeof(result)); |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 3854 | err = preprocessor(prog, i, prog->insns, |
| 3855 | prog->insns_cnt, &result); |
| 3856 | if (err) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3857 | pr_warn("Preprocessing the %dth instance of program '%s' failed\n", |
| 3858 | i, prog->section_name); |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 3859 | goto out; |
| 3860 | } |
| 3861 | |
| 3862 | if (!result.new_insn_ptr || !result.new_insn_cnt) { |
| 3863 | pr_debug("Skip loading the %dth instance of program '%s'\n", |
| 3864 | i, prog->section_name); |
| 3865 | prog->instances.fds[i] = -1; |
| 3866 | if (result.pfd) |
| 3867 | *result.pfd = -1; |
| 3868 | continue; |
| 3869 | } |
| 3870 | |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 3871 | err = load_program(prog, result.new_insn_ptr, |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 3872 | result.new_insn_cnt, |
Martin KaFai Lau | f0187f0 | 2018-12-07 16:42:29 -0800 | [diff] [blame] | 3873 | license, kern_version, &fd); |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 3874 | |
| 3875 | if (err) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3876 | pr_warn("Loading the %dth instance of program '%s' failed\n", |
| 3877 | i, prog->section_name); |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 3878 | goto out; |
| 3879 | } |
| 3880 | |
| 3881 | if (result.pfd) |
| 3882 | *result.pfd = fd; |
| 3883 | prog->instances.fds[i] = fd; |
| 3884 | } |
| 3885 | out: |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 3886 | if (err) |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3887 | pr_warn("failed to load program '%s'\n", prog->section_name); |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 3888 | zfree(&prog->insns); |
| 3889 | prog->insns_cnt = 0; |
| 3890 | return err; |
| 3891 | } |
| 3892 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 3893 | static bool bpf_program__is_function_storage(const struct bpf_program *prog, |
| 3894 | const struct bpf_object *obj) |
Jakub Kicinski | 9a94f27 | 2018-06-28 14:41:38 -0700 | [diff] [blame] | 3895 | { |
| 3896 | return prog->idx == obj->efile.text_shndx && obj->has_pseudo_calls; |
| 3897 | } |
| 3898 | |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 3899 | static int |
Quentin Monnet | 60276f9 | 2019-05-24 11:36:47 +0100 | [diff] [blame] | 3900 | bpf_object__load_progs(struct bpf_object *obj, int log_level) |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 3901 | { |
| 3902 | size_t i; |
| 3903 | int err; |
| 3904 | |
| 3905 | for (i = 0; i < obj->nr_programs; i++) { |
Jakub Kicinski | 9a94f27 | 2018-06-28 14:41:38 -0700 | [diff] [blame] | 3906 | if (bpf_program__is_function_storage(&obj->programs[i], obj)) |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 3907 | continue; |
Quentin Monnet | 501b125 | 2019-05-29 15:26:41 +0100 | [diff] [blame] | 3908 | obj->programs[i].log_level |= log_level; |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 3909 | err = bpf_program__load(&obj->programs[i], |
| 3910 | obj->license, |
| 3911 | obj->kern_version); |
| 3912 | if (err) |
| 3913 | return err; |
| 3914 | } |
| 3915 | return 0; |
| 3916 | } |
| 3917 | |
Alexei Starovoitov | e7bf94d | 2019-11-14 10:57:18 -0800 | [diff] [blame] | 3918 | static int libbpf_find_attach_btf_id(const char *name, |
| 3919 | enum bpf_attach_type attach_type, |
| 3920 | __u32 attach_prog_fd); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 3921 | static struct bpf_object * |
Andrii Nakryiko | 5e61f27 | 2019-10-04 15:40:34 -0700 | [diff] [blame] | 3922 | __bpf_object__open(const char *path, const void *obj_buf, size_t obj_buf_sz, |
Andrii Nakryiko | 291ee02 | 2019-10-15 11:28:46 -0700 | [diff] [blame] | 3923 | struct bpf_object_open_opts *opts) |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 3924 | { |
Toke Høiland-Jørgensen | 57a00f4 | 2019-11-02 12:09:41 +0100 | [diff] [blame] | 3925 | const char *pin_root_path; |
Andrii Nakryiko | dd4436b | 2019-10-20 20:38:59 -0700 | [diff] [blame] | 3926 | struct bpf_program *prog; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 3927 | struct bpf_object *obj; |
Andrii Nakryiko | 291ee02 | 2019-10-15 11:28:46 -0700 | [diff] [blame] | 3928 | const char *obj_name; |
| 3929 | char tmp_name[64]; |
| 3930 | bool relaxed_maps; |
Alexei Starovoitov | e7bf94d | 2019-11-14 10:57:18 -0800 | [diff] [blame] | 3931 | __u32 attach_prog_fd; |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 3932 | int err; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 3933 | |
| 3934 | if (elf_version(EV_CURRENT) == EV_NONE) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 3935 | pr_warn("failed to init libelf for %s\n", |
| 3936 | path ? : "(mem buf)"); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 3937 | return ERR_PTR(-LIBBPF_ERRNO__LIBELF); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 3938 | } |
| 3939 | |
Andrii Nakryiko | 291ee02 | 2019-10-15 11:28:46 -0700 | [diff] [blame] | 3940 | if (!OPTS_VALID(opts, bpf_object_open_opts)) |
| 3941 | return ERR_PTR(-EINVAL); |
| 3942 | |
Andrii Nakryiko | 1aace10 | 2019-11-21 16:35:27 -0800 | [diff] [blame] | 3943 | obj_name = OPTS_GET(opts, object_name, NULL); |
Andrii Nakryiko | 291ee02 | 2019-10-15 11:28:46 -0700 | [diff] [blame] | 3944 | if (obj_buf) { |
| 3945 | if (!obj_name) { |
| 3946 | snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx", |
| 3947 | (unsigned long)obj_buf, |
| 3948 | (unsigned long)obj_buf_sz); |
| 3949 | obj_name = tmp_name; |
| 3950 | } |
| 3951 | path = obj_name; |
| 3952 | pr_debug("loading object '%s' from buffer\n", obj_name); |
| 3953 | } |
| 3954 | |
Andrii Nakryiko | 2ce8450 | 2019-10-04 15:40:35 -0700 | [diff] [blame] | 3955 | obj = bpf_object__new(path, obj_buf, obj_buf_sz, obj_name); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 3956 | if (IS_ERR(obj)) |
| 3957 | return obj; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 3958 | |
Andrii Nakryiko | 62561eb | 2019-10-15 11:28:47 -0700 | [diff] [blame] | 3959 | obj->relaxed_core_relocs = OPTS_GET(opts, relaxed_core_relocs, false); |
Andrii Nakryiko | 291ee02 | 2019-10-15 11:28:46 -0700 | [diff] [blame] | 3960 | relaxed_maps = OPTS_GET(opts, relaxed_maps, false); |
Toke Høiland-Jørgensen | 57a00f4 | 2019-11-02 12:09:41 +0100 | [diff] [blame] | 3961 | pin_root_path = OPTS_GET(opts, pin_root_path, NULL); |
Alexei Starovoitov | e7bf94d | 2019-11-14 10:57:18 -0800 | [diff] [blame] | 3962 | attach_prog_fd = OPTS_GET(opts, attach_prog_fd, 0); |
Andrii Nakryiko | 291ee02 | 2019-10-15 11:28:46 -0700 | [diff] [blame] | 3963 | |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 3964 | CHECK_ERR(bpf_object__elf_init(obj), err, out); |
| 3965 | CHECK_ERR(bpf_object__check_endianness(obj), err, out); |
Daniel Borkmann | 8837fe5 | 2019-04-24 00:45:56 +0200 | [diff] [blame] | 3966 | CHECK_ERR(bpf_object__probe_caps(obj), err, out); |
Toke Høiland-Jørgensen | 57a00f4 | 2019-11-02 12:09:41 +0100 | [diff] [blame] | 3967 | CHECK_ERR(bpf_object__elf_collect(obj, relaxed_maps, pin_root_path), |
| 3968 | err, out); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 3969 | CHECK_ERR(bpf_object__collect_reloc(obj), err, out); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 3970 | bpf_object__elf_finish(obj); |
Andrii Nakryiko | dd4436b | 2019-10-20 20:38:59 -0700 | [diff] [blame] | 3971 | |
| 3972 | bpf_object__for_each_program(prog, obj) { |
| 3973 | enum bpf_prog_type prog_type; |
| 3974 | enum bpf_attach_type attach_type; |
| 3975 | |
| 3976 | err = libbpf_prog_type_by_name(prog->section_name, &prog_type, |
| 3977 | &attach_type); |
| 3978 | if (err == -ESRCH) |
| 3979 | /* couldn't guess, but user might manually specify */ |
| 3980 | continue; |
| 3981 | if (err) |
| 3982 | goto out; |
| 3983 | |
| 3984 | bpf_program__set_type(prog, prog_type); |
| 3985 | bpf_program__set_expected_attach_type(prog, attach_type); |
Alexei Starovoitov | 12a8654 | 2019-10-30 15:32:12 -0700 | [diff] [blame] | 3986 | if (prog_type == BPF_PROG_TYPE_TRACING) { |
Alexei Starovoitov | e7bf94d | 2019-11-14 10:57:18 -0800 | [diff] [blame] | 3987 | err = libbpf_find_attach_btf_id(prog->section_name, |
| 3988 | attach_type, |
| 3989 | attach_prog_fd); |
Alexei Starovoitov | b8c54ea | 2019-11-14 10:57:06 -0800 | [diff] [blame] | 3990 | if (err <= 0) |
Alexei Starovoitov | 12a8654 | 2019-10-30 15:32:12 -0700 | [diff] [blame] | 3991 | goto out; |
Alexei Starovoitov | b8c54ea | 2019-11-14 10:57:06 -0800 | [diff] [blame] | 3992 | prog->attach_btf_id = err; |
Alexei Starovoitov | e7bf94d | 2019-11-14 10:57:18 -0800 | [diff] [blame] | 3993 | prog->attach_prog_fd = attach_prog_fd; |
Alexei Starovoitov | 12a8654 | 2019-10-30 15:32:12 -0700 | [diff] [blame] | 3994 | } |
Andrii Nakryiko | dd4436b | 2019-10-20 20:38:59 -0700 | [diff] [blame] | 3995 | } |
| 3996 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 3997 | return obj; |
| 3998 | out: |
| 3999 | bpf_object__close(obj); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 4000 | return ERR_PTR(err); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 4001 | } |
| 4002 | |
Andrii Nakryiko | 5e61f27 | 2019-10-04 15:40:34 -0700 | [diff] [blame] | 4003 | static struct bpf_object * |
| 4004 | __bpf_object__open_xattr(struct bpf_object_open_attr *attr, int flags) |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 4005 | { |
Andrii Nakryiko | e00aca6 | 2019-10-22 10:21:00 -0700 | [diff] [blame] | 4006 | DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts, |
Andrii Nakryiko | 291ee02 | 2019-10-15 11:28:46 -0700 | [diff] [blame] | 4007 | .relaxed_maps = flags & MAPS_RELAX_COMPAT, |
| 4008 | ); |
| 4009 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 4010 | /* param validation */ |
Jakub Kicinski | 07f2d4e | 2018-07-10 14:43:02 -0700 | [diff] [blame] | 4011 | if (!attr->file) |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 4012 | return NULL; |
| 4013 | |
Jakub Kicinski | 07f2d4e | 2018-07-10 14:43:02 -0700 | [diff] [blame] | 4014 | pr_debug("loading %s\n", attr->file); |
Andrii Nakryiko | 291ee02 | 2019-10-15 11:28:46 -0700 | [diff] [blame] | 4015 | return __bpf_object__open(attr->file, NULL, 0, &opts); |
John Fastabend | c034a17 | 2018-10-15 11:19:55 -0700 | [diff] [blame] | 4016 | } |
| 4017 | |
| 4018 | struct bpf_object *bpf_object__open_xattr(struct bpf_object_open_attr *attr) |
| 4019 | { |
| 4020 | return __bpf_object__open_xattr(attr, 0); |
Jakub Kicinski | 07f2d4e | 2018-07-10 14:43:02 -0700 | [diff] [blame] | 4021 | } |
| 4022 | |
| 4023 | struct bpf_object *bpf_object__open(const char *path) |
| 4024 | { |
| 4025 | struct bpf_object_open_attr attr = { |
| 4026 | .file = path, |
| 4027 | .prog_type = BPF_PROG_TYPE_UNSPEC, |
| 4028 | }; |
| 4029 | |
| 4030 | return bpf_object__open_xattr(&attr); |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 4031 | } |
| 4032 | |
Andrii Nakryiko | 2ce8450 | 2019-10-04 15:40:35 -0700 | [diff] [blame] | 4033 | struct bpf_object * |
| 4034 | bpf_object__open_file(const char *path, struct bpf_object_open_opts *opts) |
| 4035 | { |
Andrii Nakryiko | 2ce8450 | 2019-10-04 15:40:35 -0700 | [diff] [blame] | 4036 | if (!path) |
| 4037 | return ERR_PTR(-EINVAL); |
| 4038 | |
| 4039 | pr_debug("loading %s\n", path); |
| 4040 | |
Andrii Nakryiko | 291ee02 | 2019-10-15 11:28:46 -0700 | [diff] [blame] | 4041 | return __bpf_object__open(path, NULL, 0, opts); |
Andrii Nakryiko | 2ce8450 | 2019-10-04 15:40:35 -0700 | [diff] [blame] | 4042 | } |
| 4043 | |
| 4044 | struct bpf_object * |
| 4045 | bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz, |
| 4046 | struct bpf_object_open_opts *opts) |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 4047 | { |
Andrii Nakryiko | 2ce8450 | 2019-10-04 15:40:35 -0700 | [diff] [blame] | 4048 | if (!obj_buf || obj_buf_sz == 0) |
| 4049 | return ERR_PTR(-EINVAL); |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 4050 | |
Andrii Nakryiko | 291ee02 | 2019-10-15 11:28:46 -0700 | [diff] [blame] | 4051 | return __bpf_object__open(NULL, obj_buf, obj_buf_sz, opts); |
Andrii Nakryiko | 2ce8450 | 2019-10-04 15:40:35 -0700 | [diff] [blame] | 4052 | } |
| 4053 | |
| 4054 | struct bpf_object * |
| 4055 | bpf_object__open_buffer(const void *obj_buf, size_t obj_buf_sz, |
| 4056 | const char *name) |
| 4057 | { |
Andrii Nakryiko | e00aca6 | 2019-10-22 10:21:00 -0700 | [diff] [blame] | 4058 | DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts, |
Andrii Nakryiko | 2ce8450 | 2019-10-04 15:40:35 -0700 | [diff] [blame] | 4059 | .object_name = name, |
| 4060 | /* wrong default, but backwards-compatible */ |
| 4061 | .relaxed_maps = true, |
| 4062 | ); |
| 4063 | |
| 4064 | /* returning NULL is wrong, but backwards-compatible */ |
| 4065 | if (!obj_buf || obj_buf_sz == 0) |
| 4066 | return NULL; |
| 4067 | |
| 4068 | return bpf_object__open_mem(obj_buf, obj_buf_sz, &opts); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 4069 | } |
| 4070 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 4071 | int bpf_object__unload(struct bpf_object *obj) |
| 4072 | { |
| 4073 | size_t i; |
| 4074 | |
| 4075 | if (!obj) |
| 4076 | return -EINVAL; |
| 4077 | |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 4078 | for (i = 0; i < obj->nr_maps; i++) |
| 4079 | zclose(obj->maps[i].fd); |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 4080 | |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 4081 | for (i = 0; i < obj->nr_programs; i++) |
| 4082 | bpf_program__unload(&obj->programs[i]); |
| 4083 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 4084 | return 0; |
| 4085 | } |
| 4086 | |
Quentin Monnet | 60276f9 | 2019-05-24 11:36:47 +0100 | [diff] [blame] | 4087 | int bpf_object__load_xattr(struct bpf_object_load_attr *attr) |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 4088 | { |
Quentin Monnet | 60276f9 | 2019-05-24 11:36:47 +0100 | [diff] [blame] | 4089 | struct bpf_object *obj; |
Toke Høiland-Jørgensen | ec6d5f47 | 2019-11-09 21:37:27 +0100 | [diff] [blame] | 4090 | int err, i; |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 4091 | |
Quentin Monnet | 60276f9 | 2019-05-24 11:36:47 +0100 | [diff] [blame] | 4092 | if (!attr) |
| 4093 | return -EINVAL; |
| 4094 | obj = attr->obj; |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 4095 | if (!obj) |
| 4096 | return -EINVAL; |
| 4097 | |
| 4098 | if (obj->loaded) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4099 | pr_warn("object should not be loaded twice\n"); |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 4100 | return -EINVAL; |
| 4101 | } |
| 4102 | |
| 4103 | obj->loaded = true; |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 4104 | |
| 4105 | CHECK_ERR(bpf_object__create_maps(obj), err, out); |
Andrii Nakryiko | ddc7c30 | 2019-08-07 14:39:51 -0700 | [diff] [blame] | 4106 | CHECK_ERR(bpf_object__relocate(obj, attr->target_btf_path), err, out); |
Quentin Monnet | 60276f9 | 2019-05-24 11:36:47 +0100 | [diff] [blame] | 4107 | CHECK_ERR(bpf_object__load_progs(obj, attr->log_level), err, out); |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 4108 | |
| 4109 | return 0; |
| 4110 | out: |
Toke Høiland-Jørgensen | ec6d5f47 | 2019-11-09 21:37:27 +0100 | [diff] [blame] | 4111 | /* unpin any maps that were auto-pinned during load */ |
| 4112 | for (i = 0; i < obj->nr_maps; i++) |
| 4113 | if (obj->maps[i].pinned && !obj->maps[i].reused) |
| 4114 | bpf_map__unpin(&obj->maps[i], NULL); |
| 4115 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 4116 | bpf_object__unload(obj); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4117 | pr_warn("failed to load object '%s'\n", obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 4118 | return err; |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 4119 | } |
| 4120 | |
Quentin Monnet | 60276f9 | 2019-05-24 11:36:47 +0100 | [diff] [blame] | 4121 | int bpf_object__load(struct bpf_object *obj) |
| 4122 | { |
| 4123 | struct bpf_object_load_attr attr = { |
| 4124 | .obj = obj, |
| 4125 | }; |
| 4126 | |
| 4127 | return bpf_object__load_xattr(&attr); |
| 4128 | } |
| 4129 | |
Toke Høiland-Jørgensen | 196f848 | 2019-11-02 12:09:39 +0100 | [diff] [blame] | 4130 | static int make_parent_dir(const char *path) |
| 4131 | { |
| 4132 | char *cp, errmsg[STRERR_BUFSIZE]; |
| 4133 | char *dname, *dir; |
| 4134 | int err = 0; |
| 4135 | |
| 4136 | dname = strdup(path); |
| 4137 | if (dname == NULL) |
| 4138 | return -ENOMEM; |
| 4139 | |
| 4140 | dir = dirname(dname); |
| 4141 | if (mkdir(dir, 0700) && errno != EEXIST) |
| 4142 | err = -errno; |
| 4143 | |
| 4144 | free(dname); |
| 4145 | if (err) { |
| 4146 | cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg)); |
| 4147 | pr_warn("failed to mkdir %s: %s\n", path, cp); |
| 4148 | } |
| 4149 | return err; |
| 4150 | } |
| 4151 | |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 4152 | static int check_path(const char *path) |
| 4153 | { |
Thomas Richter | 1ce6a9f | 2018-07-30 10:53:23 +0200 | [diff] [blame] | 4154 | char *cp, errmsg[STRERR_BUFSIZE]; |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 4155 | struct statfs st_fs; |
| 4156 | char *dname, *dir; |
| 4157 | int err = 0; |
| 4158 | |
| 4159 | if (path == NULL) |
| 4160 | return -EINVAL; |
| 4161 | |
| 4162 | dname = strdup(path); |
| 4163 | if (dname == NULL) |
| 4164 | return -ENOMEM; |
| 4165 | |
| 4166 | dir = dirname(dname); |
| 4167 | if (statfs(dir, &st_fs)) { |
Andrey Ignatov | 24d6a80 | 2018-10-03 15:26:41 -0700 | [diff] [blame] | 4168 | cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg)); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4169 | pr_warn("failed to statfs %s: %s\n", dir, cp); |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 4170 | err = -errno; |
| 4171 | } |
| 4172 | free(dname); |
| 4173 | |
| 4174 | if (!err && st_fs.f_type != BPF_FS_MAGIC) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4175 | pr_warn("specified path %s is not on BPF FS\n", path); |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 4176 | err = -EINVAL; |
| 4177 | } |
| 4178 | |
| 4179 | return err; |
| 4180 | } |
| 4181 | |
| 4182 | int bpf_program__pin_instance(struct bpf_program *prog, const char *path, |
| 4183 | int instance) |
| 4184 | { |
Thomas Richter | 1ce6a9f | 2018-07-30 10:53:23 +0200 | [diff] [blame] | 4185 | char *cp, errmsg[STRERR_BUFSIZE]; |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 4186 | int err; |
| 4187 | |
Toke Høiland-Jørgensen | 196f848 | 2019-11-02 12:09:39 +0100 | [diff] [blame] | 4188 | err = make_parent_dir(path); |
| 4189 | if (err) |
| 4190 | return err; |
| 4191 | |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 4192 | err = check_path(path); |
| 4193 | if (err) |
| 4194 | return err; |
| 4195 | |
| 4196 | if (prog == NULL) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4197 | pr_warn("invalid program pointer\n"); |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 4198 | return -EINVAL; |
| 4199 | } |
| 4200 | |
| 4201 | if (instance < 0 || instance >= prog->instances.nr) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4202 | pr_warn("invalid prog instance %d of prog %s (max %d)\n", |
| 4203 | instance, prog->section_name, prog->instances.nr); |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 4204 | return -EINVAL; |
| 4205 | } |
| 4206 | |
| 4207 | if (bpf_obj_pin(prog->instances.fds[instance], path)) { |
Andrey Ignatov | 24d6a80 | 2018-10-03 15:26:41 -0700 | [diff] [blame] | 4208 | cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg)); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4209 | pr_warn("failed to pin program: %s\n", cp); |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 4210 | return -errno; |
| 4211 | } |
| 4212 | pr_debug("pinned program '%s'\n", path); |
| 4213 | |
| 4214 | return 0; |
| 4215 | } |
| 4216 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4217 | int bpf_program__unpin_instance(struct bpf_program *prog, const char *path, |
| 4218 | int instance) |
| 4219 | { |
| 4220 | int err; |
| 4221 | |
| 4222 | err = check_path(path); |
| 4223 | if (err) |
| 4224 | return err; |
| 4225 | |
| 4226 | if (prog == NULL) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4227 | pr_warn("invalid program pointer\n"); |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4228 | return -EINVAL; |
| 4229 | } |
| 4230 | |
| 4231 | if (instance < 0 || instance >= prog->instances.nr) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4232 | pr_warn("invalid prog instance %d of prog %s (max %d)\n", |
| 4233 | instance, prog->section_name, prog->instances.nr); |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4234 | return -EINVAL; |
| 4235 | } |
| 4236 | |
| 4237 | err = unlink(path); |
| 4238 | if (err != 0) |
| 4239 | return -errno; |
| 4240 | pr_debug("unpinned program '%s'\n", path); |
| 4241 | |
| 4242 | return 0; |
| 4243 | } |
| 4244 | |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 4245 | int bpf_program__pin(struct bpf_program *prog, const char *path) |
| 4246 | { |
| 4247 | int i, err; |
| 4248 | |
Toke Høiland-Jørgensen | 196f848 | 2019-11-02 12:09:39 +0100 | [diff] [blame] | 4249 | err = make_parent_dir(path); |
| 4250 | if (err) |
| 4251 | return err; |
| 4252 | |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 4253 | err = check_path(path); |
| 4254 | if (err) |
| 4255 | return err; |
| 4256 | |
| 4257 | if (prog == NULL) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4258 | pr_warn("invalid program pointer\n"); |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 4259 | return -EINVAL; |
| 4260 | } |
| 4261 | |
| 4262 | if (prog->instances.nr <= 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4263 | pr_warn("no instances of prog %s to pin\n", |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 4264 | prog->section_name); |
| 4265 | return -EINVAL; |
| 4266 | } |
| 4267 | |
Stanislav Fomichev | fd734c5 | 2018-11-09 08:21:42 -0800 | [diff] [blame] | 4268 | if (prog->instances.nr == 1) { |
| 4269 | /* don't create subdirs when pinning single instance */ |
| 4270 | return bpf_program__pin_instance(prog, path, 0); |
| 4271 | } |
| 4272 | |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 4273 | for (i = 0; i < prog->instances.nr; i++) { |
| 4274 | char buf[PATH_MAX]; |
| 4275 | int len; |
| 4276 | |
| 4277 | len = snprintf(buf, PATH_MAX, "%s/%d", path, i); |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4278 | if (len < 0) { |
| 4279 | err = -EINVAL; |
| 4280 | goto err_unpin; |
| 4281 | } else if (len >= PATH_MAX) { |
| 4282 | err = -ENAMETOOLONG; |
| 4283 | goto err_unpin; |
| 4284 | } |
| 4285 | |
| 4286 | err = bpf_program__pin_instance(prog, buf, i); |
| 4287 | if (err) |
| 4288 | goto err_unpin; |
| 4289 | } |
| 4290 | |
| 4291 | return 0; |
| 4292 | |
| 4293 | err_unpin: |
| 4294 | for (i = i - 1; i >= 0; i--) { |
| 4295 | char buf[PATH_MAX]; |
| 4296 | int len; |
| 4297 | |
| 4298 | len = snprintf(buf, PATH_MAX, "%s/%d", path, i); |
| 4299 | if (len < 0) |
| 4300 | continue; |
| 4301 | else if (len >= PATH_MAX) |
| 4302 | continue; |
| 4303 | |
| 4304 | bpf_program__unpin_instance(prog, buf, i); |
| 4305 | } |
| 4306 | |
| 4307 | rmdir(path); |
| 4308 | |
| 4309 | return err; |
| 4310 | } |
| 4311 | |
| 4312 | int bpf_program__unpin(struct bpf_program *prog, const char *path) |
| 4313 | { |
| 4314 | int i, err; |
| 4315 | |
| 4316 | err = check_path(path); |
| 4317 | if (err) |
| 4318 | return err; |
| 4319 | |
| 4320 | if (prog == NULL) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4321 | pr_warn("invalid program pointer\n"); |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4322 | return -EINVAL; |
| 4323 | } |
| 4324 | |
| 4325 | if (prog->instances.nr <= 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4326 | pr_warn("no instances of prog %s to pin\n", |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4327 | prog->section_name); |
| 4328 | return -EINVAL; |
| 4329 | } |
| 4330 | |
Stanislav Fomichev | fd734c5 | 2018-11-09 08:21:42 -0800 | [diff] [blame] | 4331 | if (prog->instances.nr == 1) { |
| 4332 | /* don't create subdirs when pinning single instance */ |
| 4333 | return bpf_program__unpin_instance(prog, path, 0); |
| 4334 | } |
| 4335 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4336 | for (i = 0; i < prog->instances.nr; i++) { |
| 4337 | char buf[PATH_MAX]; |
| 4338 | int len; |
| 4339 | |
| 4340 | len = snprintf(buf, PATH_MAX, "%s/%d", path, i); |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 4341 | if (len < 0) |
| 4342 | return -EINVAL; |
| 4343 | else if (len >= PATH_MAX) |
| 4344 | return -ENAMETOOLONG; |
| 4345 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4346 | err = bpf_program__unpin_instance(prog, buf, i); |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 4347 | if (err) |
| 4348 | return err; |
| 4349 | } |
| 4350 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4351 | err = rmdir(path); |
| 4352 | if (err) |
| 4353 | return -errno; |
| 4354 | |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 4355 | return 0; |
| 4356 | } |
| 4357 | |
Joe Stringer | b6989f3 | 2017-01-26 13:19:57 -0800 | [diff] [blame] | 4358 | int bpf_map__pin(struct bpf_map *map, const char *path) |
| 4359 | { |
Thomas Richter | 1ce6a9f | 2018-07-30 10:53:23 +0200 | [diff] [blame] | 4360 | char *cp, errmsg[STRERR_BUFSIZE]; |
Joe Stringer | b6989f3 | 2017-01-26 13:19:57 -0800 | [diff] [blame] | 4361 | int err; |
| 4362 | |
Joe Stringer | b6989f3 | 2017-01-26 13:19:57 -0800 | [diff] [blame] | 4363 | if (map == NULL) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4364 | pr_warn("invalid map pointer\n"); |
Joe Stringer | b6989f3 | 2017-01-26 13:19:57 -0800 | [diff] [blame] | 4365 | return -EINVAL; |
| 4366 | } |
| 4367 | |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 4368 | if (map->pin_path) { |
| 4369 | if (path && strcmp(path, map->pin_path)) { |
| 4370 | pr_warn("map '%s' already has pin path '%s' different from '%s'\n", |
| 4371 | bpf_map__name(map), map->pin_path, path); |
| 4372 | return -EINVAL; |
| 4373 | } else if (map->pinned) { |
| 4374 | pr_debug("map '%s' already pinned at '%s'; not re-pinning\n", |
| 4375 | bpf_map__name(map), map->pin_path); |
| 4376 | return 0; |
| 4377 | } |
| 4378 | } else { |
| 4379 | if (!path) { |
| 4380 | pr_warn("missing a path to pin map '%s' at\n", |
| 4381 | bpf_map__name(map)); |
| 4382 | return -EINVAL; |
| 4383 | } else if (map->pinned) { |
| 4384 | pr_warn("map '%s' already pinned\n", bpf_map__name(map)); |
| 4385 | return -EEXIST; |
| 4386 | } |
| 4387 | |
| 4388 | map->pin_path = strdup(path); |
| 4389 | if (!map->pin_path) { |
| 4390 | err = -errno; |
| 4391 | goto out_err; |
| 4392 | } |
Joe Stringer | b6989f3 | 2017-01-26 13:19:57 -0800 | [diff] [blame] | 4393 | } |
| 4394 | |
Toke Høiland-Jørgensen | 196f848 | 2019-11-02 12:09:39 +0100 | [diff] [blame] | 4395 | err = make_parent_dir(map->pin_path); |
| 4396 | if (err) |
| 4397 | return err; |
| 4398 | |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 4399 | err = check_path(map->pin_path); |
| 4400 | if (err) |
| 4401 | return err; |
| 4402 | |
| 4403 | if (bpf_obj_pin(map->fd, map->pin_path)) { |
| 4404 | err = -errno; |
| 4405 | goto out_err; |
| 4406 | } |
| 4407 | |
| 4408 | map->pinned = true; |
| 4409 | pr_debug("pinned map '%s'\n", map->pin_path); |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4410 | |
Joe Stringer | b6989f3 | 2017-01-26 13:19:57 -0800 | [diff] [blame] | 4411 | return 0; |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 4412 | |
| 4413 | out_err: |
| 4414 | cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg)); |
| 4415 | pr_warn("failed to pin map: %s\n", cp); |
| 4416 | return err; |
Joe Stringer | b6989f3 | 2017-01-26 13:19:57 -0800 | [diff] [blame] | 4417 | } |
| 4418 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4419 | int bpf_map__unpin(struct bpf_map *map, const char *path) |
Joe Stringer | d5148d8 | 2017-01-26 13:19:58 -0800 | [diff] [blame] | 4420 | { |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4421 | int err; |
| 4422 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4423 | if (map == NULL) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4424 | pr_warn("invalid map pointer\n"); |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4425 | return -EINVAL; |
| 4426 | } |
| 4427 | |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 4428 | if (map->pin_path) { |
| 4429 | if (path && strcmp(path, map->pin_path)) { |
| 4430 | pr_warn("map '%s' already has pin path '%s' different from '%s'\n", |
| 4431 | bpf_map__name(map), map->pin_path, path); |
| 4432 | return -EINVAL; |
| 4433 | } |
| 4434 | path = map->pin_path; |
| 4435 | } else if (!path) { |
| 4436 | pr_warn("no path to unpin map '%s' from\n", |
| 4437 | bpf_map__name(map)); |
| 4438 | return -EINVAL; |
| 4439 | } |
| 4440 | |
| 4441 | err = check_path(path); |
| 4442 | if (err) |
| 4443 | return err; |
| 4444 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4445 | err = unlink(path); |
| 4446 | if (err != 0) |
| 4447 | return -errno; |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 4448 | |
| 4449 | map->pinned = false; |
| 4450 | 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] | 4451 | |
| 4452 | return 0; |
| 4453 | } |
| 4454 | |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 4455 | int bpf_map__set_pin_path(struct bpf_map *map, const char *path) |
| 4456 | { |
| 4457 | char *new = NULL; |
| 4458 | |
| 4459 | if (path) { |
| 4460 | new = strdup(path); |
| 4461 | if (!new) |
| 4462 | return -errno; |
| 4463 | } |
| 4464 | |
| 4465 | free(map->pin_path); |
| 4466 | map->pin_path = new; |
| 4467 | return 0; |
| 4468 | } |
| 4469 | |
| 4470 | const char *bpf_map__get_pin_path(const struct bpf_map *map) |
| 4471 | { |
| 4472 | return map->pin_path; |
| 4473 | } |
| 4474 | |
| 4475 | bool bpf_map__is_pinned(const struct bpf_map *map) |
| 4476 | { |
| 4477 | return map->pinned; |
| 4478 | } |
| 4479 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4480 | int bpf_object__pin_maps(struct bpf_object *obj, const char *path) |
| 4481 | { |
Joe Stringer | d5148d8 | 2017-01-26 13:19:58 -0800 | [diff] [blame] | 4482 | struct bpf_map *map; |
| 4483 | int err; |
| 4484 | |
| 4485 | if (!obj) |
| 4486 | return -ENOENT; |
| 4487 | |
| 4488 | if (!obj->loaded) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4489 | pr_warn("object not yet loaded; load it first\n"); |
Joe Stringer | d5148d8 | 2017-01-26 13:19:58 -0800 | [diff] [blame] | 4490 | return -ENOENT; |
| 4491 | } |
| 4492 | |
Jakub Kicinski | f74a53d9 | 2019-02-27 19:04:12 -0800 | [diff] [blame] | 4493 | bpf_object__for_each_map(map, obj) { |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 4494 | char *pin_path = NULL; |
Joe Stringer | d5148d8 | 2017-01-26 13:19:58 -0800 | [diff] [blame] | 4495 | char buf[PATH_MAX]; |
Joe Stringer | d5148d8 | 2017-01-26 13:19:58 -0800 | [diff] [blame] | 4496 | |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 4497 | if (path) { |
| 4498 | int len; |
| 4499 | |
| 4500 | len = snprintf(buf, PATH_MAX, "%s/%s", path, |
| 4501 | bpf_map__name(map)); |
| 4502 | if (len < 0) { |
| 4503 | err = -EINVAL; |
| 4504 | goto err_unpin_maps; |
| 4505 | } else if (len >= PATH_MAX) { |
| 4506 | err = -ENAMETOOLONG; |
| 4507 | goto err_unpin_maps; |
| 4508 | } |
| 4509 | pin_path = buf; |
| 4510 | } else if (!map->pin_path) { |
| 4511 | continue; |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4512 | } |
| 4513 | |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 4514 | err = bpf_map__pin(map, pin_path); |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4515 | if (err) |
| 4516 | goto err_unpin_maps; |
| 4517 | } |
| 4518 | |
| 4519 | return 0; |
| 4520 | |
| 4521 | err_unpin_maps: |
| 4522 | while ((map = bpf_map__prev(map, obj))) { |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 4523 | if (!map->pin_path) |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4524 | continue; |
| 4525 | |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 4526 | bpf_map__unpin(map, NULL); |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4527 | } |
| 4528 | |
| 4529 | return err; |
| 4530 | } |
| 4531 | |
| 4532 | int bpf_object__unpin_maps(struct bpf_object *obj, const char *path) |
| 4533 | { |
| 4534 | struct bpf_map *map; |
| 4535 | int err; |
| 4536 | |
| 4537 | if (!obj) |
| 4538 | return -ENOENT; |
| 4539 | |
Jakub Kicinski | f74a53d9 | 2019-02-27 19:04:12 -0800 | [diff] [blame] | 4540 | bpf_object__for_each_map(map, obj) { |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 4541 | char *pin_path = NULL; |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4542 | char buf[PATH_MAX]; |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4543 | |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 4544 | if (path) { |
| 4545 | int len; |
Joe Stringer | d5148d8 | 2017-01-26 13:19:58 -0800 | [diff] [blame] | 4546 | |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 4547 | len = snprintf(buf, PATH_MAX, "%s/%s", path, |
| 4548 | bpf_map__name(map)); |
| 4549 | if (len < 0) |
| 4550 | return -EINVAL; |
| 4551 | else if (len >= PATH_MAX) |
| 4552 | return -ENAMETOOLONG; |
| 4553 | pin_path = buf; |
| 4554 | } else if (!map->pin_path) { |
| 4555 | continue; |
| 4556 | } |
| 4557 | |
| 4558 | err = bpf_map__unpin(map, pin_path); |
Joe Stringer | d5148d8 | 2017-01-26 13:19:58 -0800 | [diff] [blame] | 4559 | if (err) |
| 4560 | return err; |
| 4561 | } |
| 4562 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4563 | return 0; |
| 4564 | } |
| 4565 | |
| 4566 | int bpf_object__pin_programs(struct bpf_object *obj, const char *path) |
| 4567 | { |
| 4568 | struct bpf_program *prog; |
| 4569 | int err; |
| 4570 | |
| 4571 | if (!obj) |
| 4572 | return -ENOENT; |
| 4573 | |
| 4574 | if (!obj->loaded) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4575 | pr_warn("object not yet loaded; load it first\n"); |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4576 | return -ENOENT; |
| 4577 | } |
| 4578 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4579 | bpf_object__for_each_program(prog, obj) { |
| 4580 | char buf[PATH_MAX]; |
| 4581 | int len; |
| 4582 | |
| 4583 | len = snprintf(buf, PATH_MAX, "%s/%s", path, |
Stanislav Fomichev | 33a2c75 | 2018-11-09 08:21:43 -0800 | [diff] [blame] | 4584 | prog->pin_name); |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4585 | if (len < 0) { |
| 4586 | err = -EINVAL; |
| 4587 | goto err_unpin_programs; |
| 4588 | } else if (len >= PATH_MAX) { |
| 4589 | err = -ENAMETOOLONG; |
| 4590 | goto err_unpin_programs; |
| 4591 | } |
| 4592 | |
| 4593 | err = bpf_program__pin(prog, buf); |
| 4594 | if (err) |
| 4595 | goto err_unpin_programs; |
| 4596 | } |
| 4597 | |
| 4598 | return 0; |
| 4599 | |
| 4600 | err_unpin_programs: |
| 4601 | while ((prog = bpf_program__prev(prog, obj))) { |
| 4602 | char buf[PATH_MAX]; |
| 4603 | int len; |
| 4604 | |
| 4605 | len = snprintf(buf, PATH_MAX, "%s/%s", path, |
Stanislav Fomichev | 33a2c75 | 2018-11-09 08:21:43 -0800 | [diff] [blame] | 4606 | prog->pin_name); |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4607 | if (len < 0) |
| 4608 | continue; |
| 4609 | else if (len >= PATH_MAX) |
| 4610 | continue; |
| 4611 | |
| 4612 | bpf_program__unpin(prog, buf); |
| 4613 | } |
| 4614 | |
| 4615 | return err; |
| 4616 | } |
| 4617 | |
| 4618 | int bpf_object__unpin_programs(struct bpf_object *obj, const char *path) |
| 4619 | { |
| 4620 | struct bpf_program *prog; |
| 4621 | int err; |
| 4622 | |
| 4623 | if (!obj) |
| 4624 | return -ENOENT; |
| 4625 | |
Joe Stringer | d5148d8 | 2017-01-26 13:19:58 -0800 | [diff] [blame] | 4626 | bpf_object__for_each_program(prog, obj) { |
| 4627 | char buf[PATH_MAX]; |
| 4628 | int len; |
| 4629 | |
| 4630 | len = snprintf(buf, PATH_MAX, "%s/%s", path, |
Stanislav Fomichev | 33a2c75 | 2018-11-09 08:21:43 -0800 | [diff] [blame] | 4631 | prog->pin_name); |
Joe Stringer | d5148d8 | 2017-01-26 13:19:58 -0800 | [diff] [blame] | 4632 | if (len < 0) |
| 4633 | return -EINVAL; |
| 4634 | else if (len >= PATH_MAX) |
| 4635 | return -ENAMETOOLONG; |
| 4636 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4637 | err = bpf_program__unpin(prog, buf); |
Joe Stringer | d5148d8 | 2017-01-26 13:19:58 -0800 | [diff] [blame] | 4638 | if (err) |
| 4639 | return err; |
| 4640 | } |
| 4641 | |
| 4642 | return 0; |
| 4643 | } |
| 4644 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4645 | int bpf_object__pin(struct bpf_object *obj, const char *path) |
| 4646 | { |
| 4647 | int err; |
| 4648 | |
| 4649 | err = bpf_object__pin_maps(obj, path); |
| 4650 | if (err) |
| 4651 | return err; |
| 4652 | |
| 4653 | err = bpf_object__pin_programs(obj, path); |
| 4654 | if (err) { |
| 4655 | bpf_object__unpin_maps(obj, path); |
| 4656 | return err; |
| 4657 | } |
| 4658 | |
| 4659 | return 0; |
| 4660 | } |
| 4661 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 4662 | void bpf_object__close(struct bpf_object *obj) |
| 4663 | { |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 4664 | size_t i; |
| 4665 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 4666 | if (!obj) |
| 4667 | return; |
| 4668 | |
Wang Nan | 10931d2 | 2016-11-26 07:03:26 +0000 | [diff] [blame] | 4669 | if (obj->clear_priv) |
| 4670 | obj->clear_priv(obj, obj->priv); |
| 4671 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 4672 | bpf_object__elf_finish(obj); |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 4673 | bpf_object__unload(obj); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 4674 | btf__free(obj->btf); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 4675 | btf_ext__free(obj->btf_ext); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 4676 | |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 4677 | for (i = 0; i < obj->nr_maps; i++) { |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 4678 | zfree(&obj->maps[i].name); |
Toke Høiland-Jørgensen | 4580b25 | 2019-11-02 12:09:38 +0100 | [diff] [blame] | 4679 | zfree(&obj->maps[i].pin_path); |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 4680 | if (obj->maps[i].clear_priv) |
| 4681 | obj->maps[i].clear_priv(&obj->maps[i], |
| 4682 | obj->maps[i].priv); |
| 4683 | obj->maps[i].priv = NULL; |
| 4684 | obj->maps[i].clear_priv = NULL; |
| 4685 | } |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 4686 | |
| 4687 | zfree(&obj->sections.rodata); |
| 4688 | zfree(&obj->sections.data); |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 4689 | zfree(&obj->maps); |
| 4690 | obj->nr_maps = 0; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 4691 | |
| 4692 | if (obj->programs && obj->nr_programs) { |
| 4693 | for (i = 0; i < obj->nr_programs; i++) |
| 4694 | bpf_program__exit(&obj->programs[i]); |
| 4695 | } |
| 4696 | zfree(&obj->programs); |
| 4697 | |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 4698 | list_del(&obj->list); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 4699 | free(obj); |
| 4700 | } |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 4701 | |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 4702 | struct bpf_object * |
| 4703 | bpf_object__next(struct bpf_object *prev) |
| 4704 | { |
| 4705 | struct bpf_object *next; |
| 4706 | |
| 4707 | if (!prev) |
| 4708 | next = list_first_entry(&bpf_objects_list, |
| 4709 | struct bpf_object, |
| 4710 | list); |
| 4711 | else |
| 4712 | next = list_next_entry(prev, list); |
| 4713 | |
| 4714 | /* Empty list is noticed here so don't need checking on entry. */ |
| 4715 | if (&next->list == &bpf_objects_list) |
| 4716 | return NULL; |
| 4717 | |
| 4718 | return next; |
| 4719 | } |
| 4720 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 4721 | const char *bpf_object__name(const struct bpf_object *obj) |
Wang Nan | acf860a | 2015-08-27 02:30:55 +0000 | [diff] [blame] | 4722 | { |
Andrii Nakryiko | c9e4c30 | 2019-10-04 15:40:36 -0700 | [diff] [blame] | 4723 | return obj ? obj->name : ERR_PTR(-EINVAL); |
Wang Nan | acf860a | 2015-08-27 02:30:55 +0000 | [diff] [blame] | 4724 | } |
| 4725 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 4726 | unsigned int bpf_object__kversion(const struct bpf_object *obj) |
Wang Nan | 45825d8 | 2015-11-06 13:49:38 +0000 | [diff] [blame] | 4727 | { |
Arnaldo Carvalho de Melo | a7fe045 | 2016-06-03 12:22:51 -0300 | [diff] [blame] | 4728 | return obj ? obj->kern_version : 0; |
Wang Nan | 45825d8 | 2015-11-06 13:49:38 +0000 | [diff] [blame] | 4729 | } |
| 4730 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 4731 | struct btf *bpf_object__btf(const struct bpf_object *obj) |
Andrey Ignatov | 789f6ba | 2019-02-14 15:01:43 -0800 | [diff] [blame] | 4732 | { |
| 4733 | return obj ? obj->btf : NULL; |
| 4734 | } |
| 4735 | |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 4736 | int bpf_object__btf_fd(const struct bpf_object *obj) |
| 4737 | { |
| 4738 | return obj->btf ? btf__fd(obj->btf) : -1; |
| 4739 | } |
| 4740 | |
Wang Nan | 10931d2 | 2016-11-26 07:03:26 +0000 | [diff] [blame] | 4741 | int bpf_object__set_priv(struct bpf_object *obj, void *priv, |
| 4742 | bpf_object_clear_priv_t clear_priv) |
| 4743 | { |
| 4744 | if (obj->priv && obj->clear_priv) |
| 4745 | obj->clear_priv(obj, obj->priv); |
| 4746 | |
| 4747 | obj->priv = priv; |
| 4748 | obj->clear_priv = clear_priv; |
| 4749 | return 0; |
| 4750 | } |
| 4751 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 4752 | void *bpf_object__priv(const struct bpf_object *obj) |
Wang Nan | 10931d2 | 2016-11-26 07:03:26 +0000 | [diff] [blame] | 4753 | { |
| 4754 | return obj ? obj->priv : ERR_PTR(-EINVAL); |
| 4755 | } |
| 4756 | |
Jakub Kicinski | eac7d84 | 2018-06-28 14:41:39 -0700 | [diff] [blame] | 4757 | static struct bpf_program * |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 4758 | __bpf_program__iter(const struct bpf_program *p, const struct bpf_object *obj, |
| 4759 | bool forward) |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 4760 | { |
Martin KaFai Lau | a83d6e7 | 2018-11-12 15:44:53 -0800 | [diff] [blame] | 4761 | size_t nr_programs = obj->nr_programs; |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4762 | ssize_t idx; |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 4763 | |
Martin KaFai Lau | a83d6e7 | 2018-11-12 15:44:53 -0800 | [diff] [blame] | 4764 | if (!nr_programs) |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 4765 | return NULL; |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 4766 | |
Martin KaFai Lau | a83d6e7 | 2018-11-12 15:44:53 -0800 | [diff] [blame] | 4767 | if (!p) |
| 4768 | /* Iter from the beginning */ |
| 4769 | return forward ? &obj->programs[0] : |
| 4770 | &obj->programs[nr_programs - 1]; |
| 4771 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4772 | if (p->obj != obj) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4773 | pr_warn("error: program handler doesn't match object\n"); |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 4774 | return NULL; |
| 4775 | } |
| 4776 | |
Martin KaFai Lau | a83d6e7 | 2018-11-12 15:44:53 -0800 | [diff] [blame] | 4777 | idx = (p - obj->programs) + (forward ? 1 : -1); |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4778 | if (idx >= obj->nr_programs || idx < 0) |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 4779 | return NULL; |
| 4780 | return &obj->programs[idx]; |
| 4781 | } |
| 4782 | |
Jakub Kicinski | eac7d84 | 2018-06-28 14:41:39 -0700 | [diff] [blame] | 4783 | struct bpf_program * |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 4784 | bpf_program__next(struct bpf_program *prev, const struct bpf_object *obj) |
Jakub Kicinski | eac7d84 | 2018-06-28 14:41:39 -0700 | [diff] [blame] | 4785 | { |
| 4786 | struct bpf_program *prog = prev; |
| 4787 | |
| 4788 | do { |
Martin KaFai Lau | a83d6e7 | 2018-11-12 15:44:53 -0800 | [diff] [blame] | 4789 | prog = __bpf_program__iter(prog, obj, true); |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4790 | } while (prog && bpf_program__is_function_storage(prog, obj)); |
| 4791 | |
| 4792 | return prog; |
| 4793 | } |
| 4794 | |
| 4795 | struct bpf_program * |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 4796 | bpf_program__prev(struct bpf_program *next, const struct bpf_object *obj) |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4797 | { |
| 4798 | struct bpf_program *prog = next; |
| 4799 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 4800 | do { |
Martin KaFai Lau | a83d6e7 | 2018-11-12 15:44:53 -0800 | [diff] [blame] | 4801 | prog = __bpf_program__iter(prog, obj, false); |
Jakub Kicinski | eac7d84 | 2018-06-28 14:41:39 -0700 | [diff] [blame] | 4802 | } while (prog && bpf_program__is_function_storage(prog, obj)); |
| 4803 | |
| 4804 | return prog; |
| 4805 | } |
| 4806 | |
Arnaldo Carvalho de Melo | edb13ed | 2016-06-03 12:38:21 -0300 | [diff] [blame] | 4807 | int bpf_program__set_priv(struct bpf_program *prog, void *priv, |
| 4808 | bpf_program_clear_priv_t clear_priv) |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 4809 | { |
| 4810 | if (prog->priv && prog->clear_priv) |
| 4811 | prog->clear_priv(prog, prog->priv); |
| 4812 | |
| 4813 | prog->priv = priv; |
| 4814 | prog->clear_priv = clear_priv; |
| 4815 | return 0; |
| 4816 | } |
| 4817 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 4818 | void *bpf_program__priv(const struct bpf_program *prog) |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 4819 | { |
Arnaldo Carvalho de Melo | be834ff | 2016-06-03 12:36:39 -0300 | [diff] [blame] | 4820 | return prog ? prog->priv : ERR_PTR(-EINVAL); |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 4821 | } |
| 4822 | |
Jakub Kicinski | 9aba361 | 2018-06-28 14:41:37 -0700 | [diff] [blame] | 4823 | void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex) |
| 4824 | { |
| 4825 | prog->prog_ifindex = ifindex; |
| 4826 | } |
| 4827 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 4828 | 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] | 4829 | { |
| 4830 | const char *title; |
| 4831 | |
| 4832 | title = prog->section_name; |
Namhyung Kim | 715f8db | 2015-11-03 20:21:05 +0900 | [diff] [blame] | 4833 | if (needs_copy) { |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 4834 | title = strdup(title); |
| 4835 | if (!title) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4836 | pr_warn("failed to strdup program title\n"); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 4837 | return ERR_PTR(-ENOMEM); |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 4838 | } |
| 4839 | } |
| 4840 | |
| 4841 | return title; |
| 4842 | } |
| 4843 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 4844 | int bpf_program__fd(const struct bpf_program *prog) |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 4845 | { |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 4846 | return bpf_program__nth_fd(prog, 0); |
| 4847 | } |
| 4848 | |
Toke Høiland-Jørgensen | 1a734ef | 2019-11-09 21:37:32 +0100 | [diff] [blame] | 4849 | size_t bpf_program__size(const struct bpf_program *prog) |
| 4850 | { |
| 4851 | return prog->insns_cnt * sizeof(struct bpf_insn); |
| 4852 | } |
| 4853 | |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 4854 | int bpf_program__set_prep(struct bpf_program *prog, int nr_instances, |
| 4855 | bpf_program_prep_t prep) |
| 4856 | { |
| 4857 | int *instances_fds; |
| 4858 | |
| 4859 | if (nr_instances <= 0 || !prep) |
| 4860 | return -EINVAL; |
| 4861 | |
| 4862 | if (prog->instances.nr > 0 || prog->instances.fds) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4863 | pr_warn("Can't set pre-processor after loading\n"); |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 4864 | return -EINVAL; |
| 4865 | } |
| 4866 | |
| 4867 | instances_fds = malloc(sizeof(int) * nr_instances); |
| 4868 | if (!instances_fds) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4869 | pr_warn("alloc memory failed for fds\n"); |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 4870 | return -ENOMEM; |
| 4871 | } |
| 4872 | |
| 4873 | /* fill all fd with -1 */ |
| 4874 | memset(instances_fds, -1, sizeof(int) * nr_instances); |
| 4875 | |
| 4876 | prog->instances.nr = nr_instances; |
| 4877 | prog->instances.fds = instances_fds; |
| 4878 | prog->preprocessor = prep; |
| 4879 | return 0; |
| 4880 | } |
| 4881 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 4882 | int bpf_program__nth_fd(const struct bpf_program *prog, int n) |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 4883 | { |
| 4884 | int fd; |
| 4885 | |
Jakub Kicinski | 1e96004 | 2018-07-26 14:32:18 -0700 | [diff] [blame] | 4886 | if (!prog) |
| 4887 | return -EINVAL; |
| 4888 | |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 4889 | if (n >= prog->instances.nr || n < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4890 | pr_warn("Can't get the %dth fd from program %s: only %d instances\n", |
| 4891 | n, prog->section_name, prog->instances.nr); |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 4892 | return -EINVAL; |
| 4893 | } |
| 4894 | |
| 4895 | fd = prog->instances.fds[n]; |
| 4896 | if (fd < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 4897 | pr_warn("%dth instance of program '%s' is invalid\n", |
| 4898 | n, prog->section_name); |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 4899 | return -ENOENT; |
| 4900 | } |
| 4901 | |
| 4902 | return fd; |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 4903 | } |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 4904 | |
Andrii Nakryiko | f1eead9 | 2019-10-20 20:38:57 -0700 | [diff] [blame] | 4905 | enum bpf_prog_type bpf_program__get_type(struct bpf_program *prog) |
| 4906 | { |
| 4907 | return prog->type; |
| 4908 | } |
| 4909 | |
Alexei Starovoitov | dd26b7f | 2017-03-30 21:45:40 -0700 | [diff] [blame] | 4910 | 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] | 4911 | { |
| 4912 | prog->type = type; |
| 4913 | } |
| 4914 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 4915 | static bool bpf_program__is_type(const struct bpf_program *prog, |
Wang Nan | 5f44e4c8 | 2016-07-13 10:44:01 +0000 | [diff] [blame] | 4916 | enum bpf_prog_type type) |
| 4917 | { |
| 4918 | return prog ? (prog->type == type) : false; |
| 4919 | } |
| 4920 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 4921 | #define BPF_PROG_TYPE_FNS(NAME, TYPE) \ |
| 4922 | int bpf_program__set_##NAME(struct bpf_program *prog) \ |
| 4923 | { \ |
| 4924 | if (!prog) \ |
| 4925 | return -EINVAL; \ |
| 4926 | bpf_program__set_type(prog, TYPE); \ |
| 4927 | return 0; \ |
| 4928 | } \ |
| 4929 | \ |
| 4930 | bool bpf_program__is_##NAME(const struct bpf_program *prog) \ |
| 4931 | { \ |
| 4932 | return bpf_program__is_type(prog, TYPE); \ |
| 4933 | } \ |
Wang Nan | 5f44e4c8 | 2016-07-13 10:44:01 +0000 | [diff] [blame] | 4934 | |
Joe Stringer | 7803ba7 | 2017-01-22 17:11:24 -0800 | [diff] [blame] | 4935 | BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER); |
Joe Stringer | ed79407 | 2017-01-22 17:11:23 -0800 | [diff] [blame] | 4936 | BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE); |
Joe Stringer | 7803ba7 | 2017-01-22 17:11:24 -0800 | [diff] [blame] | 4937 | BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS); |
| 4938 | BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT); |
Joe Stringer | ed79407 | 2017-01-22 17:11:23 -0800 | [diff] [blame] | 4939 | BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT); |
Andrey Ignatov | e14c93fd | 2018-04-17 10:28:46 -0700 | [diff] [blame] | 4940 | BPF_PROG_TYPE_FNS(raw_tracepoint, BPF_PROG_TYPE_RAW_TRACEPOINT); |
Joe Stringer | 7803ba7 | 2017-01-22 17:11:24 -0800 | [diff] [blame] | 4941 | BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP); |
| 4942 | BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT); |
Alexei Starovoitov | 12a8654 | 2019-10-30 15:32:12 -0700 | [diff] [blame] | 4943 | BPF_PROG_TYPE_FNS(tracing, BPF_PROG_TYPE_TRACING); |
Wang Nan | 5f44e4c8 | 2016-07-13 10:44:01 +0000 | [diff] [blame] | 4944 | |
Andrii Nakryiko | f1eead9 | 2019-10-20 20:38:57 -0700 | [diff] [blame] | 4945 | enum bpf_attach_type |
| 4946 | bpf_program__get_expected_attach_type(struct bpf_program *prog) |
| 4947 | { |
| 4948 | return prog->expected_attach_type; |
| 4949 | } |
| 4950 | |
John Fastabend | 16962b2 | 2018-04-23 14:30:38 -0700 | [diff] [blame] | 4951 | void bpf_program__set_expected_attach_type(struct bpf_program *prog, |
| 4952 | enum bpf_attach_type type) |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 4953 | { |
| 4954 | prog->expected_attach_type = type; |
| 4955 | } |
| 4956 | |
Alexei Starovoitov | f75a697 | 2019-10-15 20:24:59 -0700 | [diff] [blame] | 4957 | #define BPF_PROG_SEC_IMPL(string, ptype, eatype, is_attachable, btf, atype) \ |
| 4958 | { string, sizeof(string) - 1, ptype, eatype, is_attachable, btf, atype } |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 4959 | |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 4960 | /* Programs that can NOT be attached. */ |
Alexei Starovoitov | f75a697 | 2019-10-15 20:24:59 -0700 | [diff] [blame] | 4961 | #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] | 4962 | |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 4963 | /* Programs that can be attached. */ |
| 4964 | #define BPF_APROG_SEC(string, ptype, atype) \ |
Alexei Starovoitov | f75a697 | 2019-10-15 20:24:59 -0700 | [diff] [blame] | 4965 | BPF_PROG_SEC_IMPL(string, ptype, 0, 1, 0, atype) |
Andrey Ignatov | 81efee7 | 2018-04-17 10:28:45 -0700 | [diff] [blame] | 4966 | |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 4967 | /* Programs that must specify expected attach type at load time. */ |
| 4968 | #define BPF_EAPROG_SEC(string, ptype, eatype) \ |
Alexei Starovoitov | f75a697 | 2019-10-15 20:24:59 -0700 | [diff] [blame] | 4969 | BPF_PROG_SEC_IMPL(string, ptype, eatype, 1, 0, eatype) |
| 4970 | |
| 4971 | /* Programs that use BTF to identify attach point */ |
Alexei Starovoitov | 12a8654 | 2019-10-30 15:32:12 -0700 | [diff] [blame] | 4972 | #define BPF_PROG_BTF(string, ptype, eatype) \ |
| 4973 | BPF_PROG_SEC_IMPL(string, ptype, eatype, 0, 1, 0) |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 4974 | |
| 4975 | /* Programs that can be attached but attach type can't be identified by section |
| 4976 | * name. Kept for backward compatibility. |
| 4977 | */ |
| 4978 | #define BPF_APROG_COMPAT(string, ptype) BPF_PROG_SEC(string, ptype) |
Andrey Ignatov | e50b0a6 | 2018-03-30 15:08:03 -0700 | [diff] [blame] | 4979 | |
Roman Gushchin | 583c900 | 2017-12-13 15:18:51 +0000 | [diff] [blame] | 4980 | static const struct { |
| 4981 | const char *sec; |
| 4982 | size_t len; |
| 4983 | enum bpf_prog_type prog_type; |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 4984 | enum bpf_attach_type expected_attach_type; |
Alexei Starovoitov | f75a697 | 2019-10-15 20:24:59 -0700 | [diff] [blame] | 4985 | bool is_attachable; |
| 4986 | bool is_attach_btf; |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 4987 | enum bpf_attach_type attach_type; |
Roman Gushchin | 583c900 | 2017-12-13 15:18:51 +0000 | [diff] [blame] | 4988 | } section_names[] = { |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 4989 | BPF_PROG_SEC("socket", BPF_PROG_TYPE_SOCKET_FILTER), |
| 4990 | BPF_PROG_SEC("kprobe/", BPF_PROG_TYPE_KPROBE), |
Andrii Nakryiko | 32dff6d | 2019-10-20 20:38:58 -0700 | [diff] [blame] | 4991 | BPF_PROG_SEC("uprobe/", BPF_PROG_TYPE_KPROBE), |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 4992 | BPF_PROG_SEC("kretprobe/", BPF_PROG_TYPE_KPROBE), |
Andrii Nakryiko | 32dff6d | 2019-10-20 20:38:58 -0700 | [diff] [blame] | 4993 | BPF_PROG_SEC("uretprobe/", BPF_PROG_TYPE_KPROBE), |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 4994 | BPF_PROG_SEC("classifier", BPF_PROG_TYPE_SCHED_CLS), |
| 4995 | BPF_PROG_SEC("action", BPF_PROG_TYPE_SCHED_ACT), |
| 4996 | BPF_PROG_SEC("tracepoint/", BPF_PROG_TYPE_TRACEPOINT), |
Andrii Nakryiko | 32dff6d | 2019-10-20 20:38:58 -0700 | [diff] [blame] | 4997 | BPF_PROG_SEC("tp/", BPF_PROG_TYPE_TRACEPOINT), |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 4998 | BPF_PROG_SEC("raw_tracepoint/", BPF_PROG_TYPE_RAW_TRACEPOINT), |
Andrii Nakryiko | 32dff6d | 2019-10-20 20:38:58 -0700 | [diff] [blame] | 4999 | BPF_PROG_SEC("raw_tp/", BPF_PROG_TYPE_RAW_TRACEPOINT), |
Alexei Starovoitov | 12a8654 | 2019-10-30 15:32:12 -0700 | [diff] [blame] | 5000 | BPF_PROG_BTF("tp_btf/", BPF_PROG_TYPE_TRACING, |
| 5001 | BPF_TRACE_RAW_TP), |
Alexei Starovoitov | b8c54ea | 2019-11-14 10:57:06 -0800 | [diff] [blame] | 5002 | BPF_PROG_BTF("fentry/", BPF_PROG_TYPE_TRACING, |
| 5003 | BPF_TRACE_FENTRY), |
| 5004 | BPF_PROG_BTF("fexit/", BPF_PROG_TYPE_TRACING, |
| 5005 | BPF_TRACE_FEXIT), |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 5006 | BPF_PROG_SEC("xdp", BPF_PROG_TYPE_XDP), |
| 5007 | BPF_PROG_SEC("perf_event", BPF_PROG_TYPE_PERF_EVENT), |
| 5008 | BPF_PROG_SEC("lwt_in", BPF_PROG_TYPE_LWT_IN), |
| 5009 | BPF_PROG_SEC("lwt_out", BPF_PROG_TYPE_LWT_OUT), |
| 5010 | BPF_PROG_SEC("lwt_xmit", BPF_PROG_TYPE_LWT_XMIT), |
| 5011 | BPF_PROG_SEC("lwt_seg6local", BPF_PROG_TYPE_LWT_SEG6LOCAL), |
Andrey Ignatov | bafa7af | 2018-09-26 15:24:54 -0700 | [diff] [blame] | 5012 | BPF_APROG_SEC("cgroup_skb/ingress", BPF_PROG_TYPE_CGROUP_SKB, |
| 5013 | BPF_CGROUP_INET_INGRESS), |
| 5014 | BPF_APROG_SEC("cgroup_skb/egress", BPF_PROG_TYPE_CGROUP_SKB, |
| 5015 | BPF_CGROUP_INET_EGRESS), |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 5016 | BPF_APROG_COMPAT("cgroup/skb", BPF_PROG_TYPE_CGROUP_SKB), |
| 5017 | BPF_APROG_SEC("cgroup/sock", BPF_PROG_TYPE_CGROUP_SOCK, |
| 5018 | BPF_CGROUP_INET_SOCK_CREATE), |
| 5019 | BPF_EAPROG_SEC("cgroup/post_bind4", BPF_PROG_TYPE_CGROUP_SOCK, |
| 5020 | BPF_CGROUP_INET4_POST_BIND), |
| 5021 | BPF_EAPROG_SEC("cgroup/post_bind6", BPF_PROG_TYPE_CGROUP_SOCK, |
| 5022 | BPF_CGROUP_INET6_POST_BIND), |
| 5023 | BPF_APROG_SEC("cgroup/dev", BPF_PROG_TYPE_CGROUP_DEVICE, |
| 5024 | BPF_CGROUP_DEVICE), |
| 5025 | BPF_APROG_SEC("sockops", BPF_PROG_TYPE_SOCK_OPS, |
| 5026 | BPF_CGROUP_SOCK_OPS), |
Andrey Ignatov | c6f6851 | 2018-09-26 15:24:55 -0700 | [diff] [blame] | 5027 | BPF_APROG_SEC("sk_skb/stream_parser", BPF_PROG_TYPE_SK_SKB, |
| 5028 | BPF_SK_SKB_STREAM_PARSER), |
| 5029 | BPF_APROG_SEC("sk_skb/stream_verdict", BPF_PROG_TYPE_SK_SKB, |
| 5030 | BPF_SK_SKB_STREAM_VERDICT), |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 5031 | BPF_APROG_COMPAT("sk_skb", BPF_PROG_TYPE_SK_SKB), |
| 5032 | BPF_APROG_SEC("sk_msg", BPF_PROG_TYPE_SK_MSG, |
| 5033 | BPF_SK_MSG_VERDICT), |
| 5034 | BPF_APROG_SEC("lirc_mode2", BPF_PROG_TYPE_LIRC_MODE2, |
| 5035 | BPF_LIRC_MODE2), |
| 5036 | BPF_APROG_SEC("flow_dissector", BPF_PROG_TYPE_FLOW_DISSECTOR, |
| 5037 | BPF_FLOW_DISSECTOR), |
| 5038 | BPF_EAPROG_SEC("cgroup/bind4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, |
| 5039 | BPF_CGROUP_INET4_BIND), |
| 5040 | BPF_EAPROG_SEC("cgroup/bind6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, |
| 5041 | BPF_CGROUP_INET6_BIND), |
| 5042 | BPF_EAPROG_SEC("cgroup/connect4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, |
| 5043 | BPF_CGROUP_INET4_CONNECT), |
| 5044 | BPF_EAPROG_SEC("cgroup/connect6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, |
| 5045 | BPF_CGROUP_INET6_CONNECT), |
| 5046 | BPF_EAPROG_SEC("cgroup/sendmsg4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, |
| 5047 | BPF_CGROUP_UDP4_SENDMSG), |
| 5048 | BPF_EAPROG_SEC("cgroup/sendmsg6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, |
| 5049 | BPF_CGROUP_UDP6_SENDMSG), |
Daniel Borkmann | 9bb59ac | 2019-06-07 01:48:59 +0200 | [diff] [blame] | 5050 | BPF_EAPROG_SEC("cgroup/recvmsg4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, |
| 5051 | BPF_CGROUP_UDP4_RECVMSG), |
| 5052 | BPF_EAPROG_SEC("cgroup/recvmsg6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR, |
| 5053 | BPF_CGROUP_UDP6_RECVMSG), |
Andrey Ignatov | 063cc9f | 2019-03-08 09:15:26 -0800 | [diff] [blame] | 5054 | BPF_EAPROG_SEC("cgroup/sysctl", BPF_PROG_TYPE_CGROUP_SYSCTL, |
| 5055 | BPF_CGROUP_SYSCTL), |
Stanislav Fomichev | 4cdbfb5 | 2019-06-27 13:38:49 -0700 | [diff] [blame] | 5056 | BPF_EAPROG_SEC("cgroup/getsockopt", BPF_PROG_TYPE_CGROUP_SOCKOPT, |
| 5057 | BPF_CGROUP_GETSOCKOPT), |
| 5058 | BPF_EAPROG_SEC("cgroup/setsockopt", BPF_PROG_TYPE_CGROUP_SOCKOPT, |
| 5059 | BPF_CGROUP_SETSOCKOPT), |
Roman Gushchin | 583c900 | 2017-12-13 15:18:51 +0000 | [diff] [blame] | 5060 | }; |
Roman Gushchin | 583c900 | 2017-12-13 15:18:51 +0000 | [diff] [blame] | 5061 | |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 5062 | #undef BPF_PROG_SEC_IMPL |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 5063 | #undef BPF_PROG_SEC |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 5064 | #undef BPF_APROG_SEC |
| 5065 | #undef BPF_EAPROG_SEC |
| 5066 | #undef BPF_APROG_COMPAT |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 5067 | |
Taeung Song | c76e4c2 | 2019-01-21 22:06:38 +0900 | [diff] [blame] | 5068 | #define MAX_TYPE_NAME_SIZE 32 |
| 5069 | |
| 5070 | static char *libbpf_get_type_names(bool attach_type) |
| 5071 | { |
| 5072 | int i, len = ARRAY_SIZE(section_names) * MAX_TYPE_NAME_SIZE; |
| 5073 | char *buf; |
| 5074 | |
| 5075 | buf = malloc(len); |
| 5076 | if (!buf) |
| 5077 | return NULL; |
| 5078 | |
| 5079 | buf[0] = '\0'; |
| 5080 | /* Forge string buf with all available names */ |
| 5081 | for (i = 0; i < ARRAY_SIZE(section_names); i++) { |
| 5082 | if (attach_type && !section_names[i].is_attachable) |
| 5083 | continue; |
| 5084 | |
| 5085 | if (strlen(buf) + strlen(section_names[i].sec) + 2 > len) { |
| 5086 | free(buf); |
| 5087 | return NULL; |
| 5088 | } |
| 5089 | strcat(buf, " "); |
| 5090 | strcat(buf, section_names[i].sec); |
| 5091 | } |
| 5092 | |
| 5093 | return buf; |
| 5094 | } |
| 5095 | |
Jakub Kicinski | b60df2a | 2018-07-10 14:42:59 -0700 | [diff] [blame] | 5096 | int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type, |
| 5097 | enum bpf_attach_type *expected_attach_type) |
Roman Gushchin | 583c900 | 2017-12-13 15:18:51 +0000 | [diff] [blame] | 5098 | { |
Taeung Song | c76e4c2 | 2019-01-21 22:06:38 +0900 | [diff] [blame] | 5099 | char *type_names; |
Roman Gushchin | 583c900 | 2017-12-13 15:18:51 +0000 | [diff] [blame] | 5100 | int i; |
| 5101 | |
Jakub Kicinski | b60df2a | 2018-07-10 14:42:59 -0700 | [diff] [blame] | 5102 | if (!name) |
| 5103 | return -EINVAL; |
Roman Gushchin | 583c900 | 2017-12-13 15:18:51 +0000 | [diff] [blame] | 5104 | |
Jakub Kicinski | b60df2a | 2018-07-10 14:42:59 -0700 | [diff] [blame] | 5105 | for (i = 0; i < ARRAY_SIZE(section_names); i++) { |
| 5106 | if (strncmp(name, section_names[i].sec, section_names[i].len)) |
| 5107 | continue; |
| 5108 | *prog_type = section_names[i].prog_type; |
| 5109 | *expected_attach_type = section_names[i].expected_attach_type; |
| 5110 | return 0; |
| 5111 | } |
Andrii Nakryiko | 8983b73 | 2019-11-20 23:07:42 -0800 | [diff] [blame] | 5112 | pr_warn("failed to guess program type from ELF section '%s'\n", name); |
Taeung Song | c76e4c2 | 2019-01-21 22:06:38 +0900 | [diff] [blame] | 5113 | type_names = libbpf_get_type_names(false); |
| 5114 | if (type_names != NULL) { |
| 5115 | pr_info("supported section(type) names are:%s\n", type_names); |
| 5116 | free(type_names); |
| 5117 | } |
| 5118 | |
Andrii Nakryiko | dd4436b | 2019-10-20 20:38:59 -0700 | [diff] [blame] | 5119 | return -ESRCH; |
Jakub Kicinski | b60df2a | 2018-07-10 14:42:59 -0700 | [diff] [blame] | 5120 | } |
Roman Gushchin | 583c900 | 2017-12-13 15:18:51 +0000 | [diff] [blame] | 5121 | |
Alexei Starovoitov | 12a8654 | 2019-10-30 15:32:12 -0700 | [diff] [blame] | 5122 | #define BTF_PREFIX "btf_trace_" |
Alexei Starovoitov | b8c54ea | 2019-11-14 10:57:06 -0800 | [diff] [blame] | 5123 | int libbpf_find_vmlinux_btf_id(const char *name, |
| 5124 | enum bpf_attach_type attach_type) |
Alexei Starovoitov | 12a8654 | 2019-10-30 15:32:12 -0700 | [diff] [blame] | 5125 | { |
| 5126 | struct btf *btf = bpf_core_find_kernel_btf(); |
Alexei Starovoitov | b8c54ea | 2019-11-14 10:57:06 -0800 | [diff] [blame] | 5127 | char raw_tp_btf[128] = BTF_PREFIX; |
| 5128 | char *dst = raw_tp_btf + sizeof(BTF_PREFIX) - 1; |
| 5129 | const char *btf_name; |
| 5130 | int err = -EINVAL; |
Andrii Nakryiko | b615e5a | 2019-11-25 13:29:48 -0800 | [diff] [blame^] | 5131 | __u32 kind; |
Alexei Starovoitov | 12a8654 | 2019-10-30 15:32:12 -0700 | [diff] [blame] | 5132 | |
| 5133 | if (IS_ERR(btf)) { |
| 5134 | pr_warn("vmlinux BTF is not found\n"); |
| 5135 | return -EINVAL; |
| 5136 | } |
| 5137 | |
Alexei Starovoitov | b8c54ea | 2019-11-14 10:57:06 -0800 | [diff] [blame] | 5138 | if (attach_type == BPF_TRACE_RAW_TP) { |
| 5139 | /* prepend "btf_trace_" prefix per kernel convention */ |
| 5140 | strncat(dst, name, sizeof(raw_tp_btf) - sizeof(BTF_PREFIX)); |
| 5141 | btf_name = raw_tp_btf; |
| 5142 | kind = BTF_KIND_TYPEDEF; |
| 5143 | } else { |
| 5144 | btf_name = name; |
| 5145 | kind = BTF_KIND_FUNC; |
| 5146 | } |
| 5147 | err = btf__find_by_name_kind(btf, btf_name, kind); |
| 5148 | btf__free(btf); |
| 5149 | return err; |
| 5150 | } |
| 5151 | |
Alexei Starovoitov | e7bf94d | 2019-11-14 10:57:18 -0800 | [diff] [blame] | 5152 | static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd) |
| 5153 | { |
| 5154 | struct bpf_prog_info_linear *info_linear; |
| 5155 | struct bpf_prog_info *info; |
| 5156 | struct btf *btf = NULL; |
| 5157 | int err = -EINVAL; |
| 5158 | |
| 5159 | info_linear = bpf_program__get_prog_info_linear(attach_prog_fd, 0); |
| 5160 | if (IS_ERR_OR_NULL(info_linear)) { |
| 5161 | pr_warn("failed get_prog_info_linear for FD %d\n", |
| 5162 | attach_prog_fd); |
| 5163 | return -EINVAL; |
| 5164 | } |
| 5165 | info = &info_linear->info; |
| 5166 | if (!info->btf_id) { |
| 5167 | pr_warn("The target program doesn't have BTF\n"); |
| 5168 | goto out; |
| 5169 | } |
| 5170 | if (btf__get_from_id(info->btf_id, &btf)) { |
| 5171 | pr_warn("Failed to get BTF of the program\n"); |
| 5172 | goto out; |
| 5173 | } |
| 5174 | err = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC); |
| 5175 | btf__free(btf); |
| 5176 | if (err <= 0) { |
| 5177 | pr_warn("%s is not found in prog's BTF\n", name); |
| 5178 | goto out; |
| 5179 | } |
| 5180 | out: |
| 5181 | free(info_linear); |
| 5182 | return err; |
| 5183 | } |
| 5184 | |
| 5185 | static int libbpf_find_attach_btf_id(const char *name, |
| 5186 | enum bpf_attach_type attach_type, |
| 5187 | __u32 attach_prog_fd) |
Alexei Starovoitov | b8c54ea | 2019-11-14 10:57:06 -0800 | [diff] [blame] | 5188 | { |
| 5189 | int i, err; |
| 5190 | |
Alexei Starovoitov | 12a8654 | 2019-10-30 15:32:12 -0700 | [diff] [blame] | 5191 | if (!name) |
Alexei Starovoitov | b8c54ea | 2019-11-14 10:57:06 -0800 | [diff] [blame] | 5192 | return -EINVAL; |
Alexei Starovoitov | 12a8654 | 2019-10-30 15:32:12 -0700 | [diff] [blame] | 5193 | |
| 5194 | for (i = 0; i < ARRAY_SIZE(section_names); i++) { |
| 5195 | if (!section_names[i].is_attach_btf) |
| 5196 | continue; |
| 5197 | if (strncmp(name, section_names[i].sec, section_names[i].len)) |
| 5198 | continue; |
Alexei Starovoitov | e7bf94d | 2019-11-14 10:57:18 -0800 | [diff] [blame] | 5199 | if (attach_prog_fd) |
| 5200 | err = libbpf_find_prog_btf_id(name + section_names[i].len, |
| 5201 | attach_prog_fd); |
| 5202 | else |
| 5203 | err = libbpf_find_vmlinux_btf_id(name + section_names[i].len, |
| 5204 | attach_type); |
Alexei Starovoitov | b8c54ea | 2019-11-14 10:57:06 -0800 | [diff] [blame] | 5205 | if (err <= 0) |
| 5206 | pr_warn("%s is not found in vmlinux BTF\n", name); |
| 5207 | return err; |
Alexei Starovoitov | 12a8654 | 2019-10-30 15:32:12 -0700 | [diff] [blame] | 5208 | } |
| 5209 | 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] | 5210 | return -ESRCH; |
Alexei Starovoitov | 12a8654 | 2019-10-30 15:32:12 -0700 | [diff] [blame] | 5211 | } |
| 5212 | |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 5213 | int libbpf_attach_type_by_name(const char *name, |
| 5214 | enum bpf_attach_type *attach_type) |
| 5215 | { |
Taeung Song | c76e4c2 | 2019-01-21 22:06:38 +0900 | [diff] [blame] | 5216 | char *type_names; |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 5217 | int i; |
| 5218 | |
| 5219 | if (!name) |
| 5220 | return -EINVAL; |
| 5221 | |
| 5222 | for (i = 0; i < ARRAY_SIZE(section_names); i++) { |
| 5223 | if (strncmp(name, section_names[i].sec, section_names[i].len)) |
| 5224 | continue; |
Andrey Ignatov | 3615353 | 2018-10-31 12:57:18 -0700 | [diff] [blame] | 5225 | if (!section_names[i].is_attachable) |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 5226 | return -EINVAL; |
| 5227 | *attach_type = section_names[i].attach_type; |
| 5228 | return 0; |
| 5229 | } |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5230 | pr_warn("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] | 5231 | type_names = libbpf_get_type_names(true); |
| 5232 | if (type_names != NULL) { |
| 5233 | pr_info("attachable section(type) names are:%s\n", type_names); |
| 5234 | free(type_names); |
| 5235 | } |
| 5236 | |
Andrey Ignatov | 956b620 | 2018-09-26 15:24:53 -0700 | [diff] [blame] | 5237 | return -EINVAL; |
| 5238 | } |
| 5239 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 5240 | int bpf_map__fd(const struct bpf_map *map) |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 5241 | { |
Arnaldo Carvalho de Melo | 6e009e65 | 2016-06-03 12:15:52 -0300 | [diff] [blame] | 5242 | return map ? map->fd : -EINVAL; |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 5243 | } |
| 5244 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 5245 | 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] | 5246 | { |
Arnaldo Carvalho de Melo | 53897a7 | 2016-06-02 14:21:06 -0300 | [diff] [blame] | 5247 | return map ? &map->def : ERR_PTR(-EINVAL); |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 5248 | } |
| 5249 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 5250 | const char *bpf_map__name(const struct bpf_map *map) |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 5251 | { |
Arnaldo Carvalho de Melo | 009ad5d | 2016-06-02 11:02:05 -0300 | [diff] [blame] | 5252 | return map ? map->name : NULL; |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 5253 | } |
| 5254 | |
Martin KaFai Lau | 5b891af | 2018-07-24 08:40:21 -0700 | [diff] [blame] | 5255 | __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] | 5256 | { |
Martin KaFai Lau | 61746dbe | 2018-05-22 15:04:24 -0700 | [diff] [blame] | 5257 | return map ? map->btf_key_type_id : 0; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 5258 | } |
| 5259 | |
Martin KaFai Lau | 5b891af | 2018-07-24 08:40:21 -0700 | [diff] [blame] | 5260 | __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] | 5261 | { |
Martin KaFai Lau | 61746dbe | 2018-05-22 15:04:24 -0700 | [diff] [blame] | 5262 | return map ? map->btf_value_type_id : 0; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 5263 | } |
| 5264 | |
Arnaldo Carvalho de Melo | edb13ed | 2016-06-03 12:38:21 -0300 | [diff] [blame] | 5265 | int bpf_map__set_priv(struct bpf_map *map, void *priv, |
| 5266 | bpf_map_clear_priv_t clear_priv) |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 5267 | { |
| 5268 | if (!map) |
| 5269 | return -EINVAL; |
| 5270 | |
| 5271 | if (map->priv) { |
| 5272 | if (map->clear_priv) |
| 5273 | map->clear_priv(map, map->priv); |
| 5274 | } |
| 5275 | |
| 5276 | map->priv = priv; |
| 5277 | map->clear_priv = clear_priv; |
| 5278 | return 0; |
| 5279 | } |
| 5280 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 5281 | void *bpf_map__priv(const struct bpf_map *map) |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 5282 | { |
Arnaldo Carvalho de Melo | b4cbfa5 | 2016-06-02 10:51:59 -0300 | [diff] [blame] | 5283 | return map ? map->priv : ERR_PTR(-EINVAL); |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 5284 | } |
| 5285 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 5286 | bool bpf_map__is_offload_neutral(const struct bpf_map *map) |
Jakub Kicinski | f83fb22 | 2018-07-10 14:43:01 -0700 | [diff] [blame] | 5287 | { |
| 5288 | return map->def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY; |
| 5289 | } |
| 5290 | |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 5291 | bool bpf_map__is_internal(const struct bpf_map *map) |
Daniel Borkmann | d859900 | 2019-04-09 23:20:13 +0200 | [diff] [blame] | 5292 | { |
| 5293 | return map->libbpf_type != LIBBPF_MAP_UNSPEC; |
| 5294 | } |
| 5295 | |
Jakub Kicinski | 9aba361 | 2018-06-28 14:41:37 -0700 | [diff] [blame] | 5296 | void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex) |
| 5297 | { |
| 5298 | map->map_ifindex = ifindex; |
| 5299 | } |
| 5300 | |
Nikita V. Shirokov | addb9fc | 2018-11-20 20:55:56 -0800 | [diff] [blame] | 5301 | int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd) |
| 5302 | { |
| 5303 | if (!bpf_map_type__is_map_in_map(map->def.type)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5304 | pr_warn("error: unsupported map type\n"); |
Nikita V. Shirokov | addb9fc | 2018-11-20 20:55:56 -0800 | [diff] [blame] | 5305 | return -EINVAL; |
| 5306 | } |
| 5307 | if (map->inner_map_fd != -1) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5308 | pr_warn("error: inner_map_fd already specified\n"); |
Nikita V. Shirokov | addb9fc | 2018-11-20 20:55:56 -0800 | [diff] [blame] | 5309 | return -EINVAL; |
| 5310 | } |
| 5311 | map->inner_map_fd = fd; |
| 5312 | return 0; |
| 5313 | } |
| 5314 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5315 | static struct bpf_map * |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 5316 | __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] | 5317 | { |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5318 | ssize_t idx; |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 5319 | struct bpf_map *s, *e; |
| 5320 | |
| 5321 | if (!obj || !obj->maps) |
| 5322 | return NULL; |
| 5323 | |
| 5324 | s = obj->maps; |
| 5325 | e = obj->maps + obj->nr_maps; |
| 5326 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5327 | if ((m < s) || (m >= e)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5328 | pr_warn("error in %s: map handler doesn't belong to object\n", |
| 5329 | __func__); |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 5330 | return NULL; |
| 5331 | } |
| 5332 | |
Stanislav Fomichev | 0c19a9f | 2018-11-09 08:21:41 -0800 | [diff] [blame] | 5333 | idx = (m - obj->maps) + i; |
| 5334 | if (idx >= obj->nr_maps || idx < 0) |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 5335 | return NULL; |
| 5336 | return &obj->maps[idx]; |
| 5337 | } |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 5338 | |
| 5339 | struct bpf_map * |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 5340 | 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] | 5341 | { |
| 5342 | if (prev == NULL) |
| 5343 | return obj->maps; |
| 5344 | |
| 5345 | return __bpf_map__iter(prev, obj, 1); |
| 5346 | } |
| 5347 | |
| 5348 | struct bpf_map * |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 5349 | 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] | 5350 | { |
| 5351 | if (next == NULL) { |
| 5352 | if (!obj->nr_maps) |
| 5353 | return NULL; |
| 5354 | return obj->maps + obj->nr_maps - 1; |
| 5355 | } |
| 5356 | |
| 5357 | return __bpf_map__iter(next, obj, -1); |
| 5358 | } |
| 5359 | |
| 5360 | struct bpf_map * |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 5361 | 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] | 5362 | { |
| 5363 | struct bpf_map *pos; |
| 5364 | |
Jakub Kicinski | f74a53d9 | 2019-02-27 19:04:12 -0800 | [diff] [blame] | 5365 | bpf_object__for_each_map(pos, obj) { |
Wang Nan | 973170e | 2015-12-08 02:25:29 +0000 | [diff] [blame] | 5366 | if (pos->name && !strcmp(pos->name, name)) |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 5367 | return pos; |
| 5368 | } |
| 5369 | return NULL; |
| 5370 | } |
Wang Nan | 5a6acad | 2016-11-26 07:03:27 +0000 | [diff] [blame] | 5371 | |
Maciej Fijalkowski | f3cea32 | 2019-02-01 22:42:23 +0100 | [diff] [blame] | 5372 | int |
Andrii Nakryiko | a324aae | 2019-06-17 15:48:58 -0700 | [diff] [blame] | 5373 | 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] | 5374 | { |
| 5375 | return bpf_map__fd(bpf_object__find_map_by_name(obj, name)); |
| 5376 | } |
| 5377 | |
Wang Nan | 5a6acad | 2016-11-26 07:03:27 +0000 | [diff] [blame] | 5378 | struct bpf_map * |
| 5379 | bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset) |
| 5380 | { |
Andrii Nakryiko | db48814 | 2019-06-17 12:26:54 -0700 | [diff] [blame] | 5381 | return ERR_PTR(-ENOTSUP); |
Wang Nan | 5a6acad | 2016-11-26 07:03:27 +0000 | [diff] [blame] | 5382 | } |
Joe Stringer | e28ff1a | 2017-01-22 17:11:25 -0800 | [diff] [blame] | 5383 | |
| 5384 | long libbpf_get_error(const void *ptr) |
| 5385 | { |
Hariprasad Kelam | d98363b | 2019-05-25 14:32:57 +0530 | [diff] [blame] | 5386 | return PTR_ERR_OR_ZERO(ptr); |
Joe Stringer | e28ff1a | 2017-01-22 17:11:25 -0800 | [diff] [blame] | 5387 | } |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 5388 | |
| 5389 | int bpf_prog_load(const char *file, enum bpf_prog_type type, |
| 5390 | struct bpf_object **pobj, int *prog_fd) |
| 5391 | { |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 5392 | struct bpf_prog_load_attr attr; |
| 5393 | |
| 5394 | memset(&attr, 0, sizeof(struct bpf_prog_load_attr)); |
| 5395 | attr.file = file; |
| 5396 | attr.prog_type = type; |
| 5397 | attr.expected_attach_type = 0; |
| 5398 | |
| 5399 | return bpf_prog_load_xattr(&attr, pobj, prog_fd); |
| 5400 | } |
| 5401 | |
| 5402 | int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr, |
| 5403 | struct bpf_object **pobj, int *prog_fd) |
| 5404 | { |
Leo Yan | 33bae18 | 2019-07-02 18:25:31 +0800 | [diff] [blame] | 5405 | struct bpf_object_open_attr open_attr = {}; |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 5406 | struct bpf_program *prog, *first_prog = NULL; |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 5407 | struct bpf_object *obj; |
David Beckett | f0307a7 | 2018-05-16 14:02:49 -0700 | [diff] [blame] | 5408 | struct bpf_map *map; |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 5409 | int err; |
| 5410 | |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 5411 | if (!attr) |
| 5412 | return -EINVAL; |
Jakub Kicinski | 17387dd | 2018-05-10 10:24:42 -0700 | [diff] [blame] | 5413 | if (!attr->file) |
| 5414 | return -EINVAL; |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 5415 | |
Leo Yan | 33bae18 | 2019-07-02 18:25:31 +0800 | [diff] [blame] | 5416 | open_attr.file = attr->file; |
| 5417 | open_attr.prog_type = attr->prog_type; |
| 5418 | |
Jakub Kicinski | 07f2d4e | 2018-07-10 14:43:02 -0700 | [diff] [blame] | 5419 | obj = bpf_object__open_xattr(&open_attr); |
Jakub Kicinski | 3597683 | 2018-05-10 10:09:34 -0700 | [diff] [blame] | 5420 | if (IS_ERR_OR_NULL(obj)) |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 5421 | return -ENOENT; |
| 5422 | |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 5423 | bpf_object__for_each_program(prog, obj) { |
Andrii Nakryiko | dd4436b | 2019-10-20 20:38:59 -0700 | [diff] [blame] | 5424 | enum bpf_attach_type attach_type = attr->expected_attach_type; |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 5425 | /* |
Andrii Nakryiko | dd4436b | 2019-10-20 20:38:59 -0700 | [diff] [blame] | 5426 | * to preserve backwards compatibility, bpf_prog_load treats |
| 5427 | * attr->prog_type, if specified, as an override to whatever |
| 5428 | * bpf_object__open guessed |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 5429 | */ |
Andrii Nakryiko | dd4436b | 2019-10-20 20:38:59 -0700 | [diff] [blame] | 5430 | if (attr->prog_type != BPF_PROG_TYPE_UNSPEC) { |
| 5431 | bpf_program__set_type(prog, attr->prog_type); |
| 5432 | bpf_program__set_expected_attach_type(prog, |
| 5433 | attach_type); |
| 5434 | } |
| 5435 | if (bpf_program__get_type(prog) == BPF_PROG_TYPE_UNSPEC) { |
| 5436 | /* |
| 5437 | * we haven't guessed from section name and user |
| 5438 | * didn't provide a fallback type, too bad... |
| 5439 | */ |
| 5440 | bpf_object__close(obj); |
| 5441 | return -EINVAL; |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 5442 | } |
| 5443 | |
Andrii Nakryiko | dd4436b | 2019-10-20 20:38:59 -0700 | [diff] [blame] | 5444 | prog->prog_ifindex = attr->ifindex; |
Alexei Starovoitov | da11b41 | 2019-04-01 21:27:47 -0700 | [diff] [blame] | 5445 | prog->log_level = attr->log_level; |
Jiong Wang | 0465619 | 2019-05-24 23:25:19 +0100 | [diff] [blame] | 5446 | prog->prog_flags = attr->prog_flags; |
Taeung Song | 69495d2 | 2018-09-03 08:30:07 +0900 | [diff] [blame] | 5447 | if (!first_prog) |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 5448 | first_prog = prog; |
| 5449 | } |
| 5450 | |
Jakub Kicinski | f74a53d9 | 2019-02-27 19:04:12 -0800 | [diff] [blame] | 5451 | bpf_object__for_each_map(map, obj) { |
Jakub Kicinski | f83fb22 | 2018-07-10 14:43:01 -0700 | [diff] [blame] | 5452 | if (!bpf_map__is_offload_neutral(map)) |
| 5453 | map->map_ifindex = attr->ifindex; |
David Beckett | f0307a7 | 2018-05-16 14:02:49 -0700 | [diff] [blame] | 5454 | } |
| 5455 | |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 5456 | if (!first_prog) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5457 | pr_warn("object file doesn't contain bpf program\n"); |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 5458 | bpf_object__close(obj); |
| 5459 | return -ENOENT; |
| 5460 | } |
| 5461 | |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 5462 | err = bpf_object__load(obj); |
| 5463 | if (err) { |
| 5464 | bpf_object__close(obj); |
| 5465 | return -EINVAL; |
| 5466 | } |
| 5467 | |
| 5468 | *pobj = obj; |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 5469 | *prog_fd = bpf_program__fd(first_prog); |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 5470 | return 0; |
| 5471 | } |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 5472 | |
Andrii Nakryiko | 1c2e9ef | 2019-07-01 16:58:56 -0700 | [diff] [blame] | 5473 | struct bpf_link { |
| 5474 | int (*destroy)(struct bpf_link *link); |
| 5475 | }; |
| 5476 | |
| 5477 | int bpf_link__destroy(struct bpf_link *link) |
| 5478 | { |
| 5479 | int err; |
| 5480 | |
| 5481 | if (!link) |
| 5482 | return 0; |
| 5483 | |
| 5484 | err = link->destroy(link); |
| 5485 | free(link); |
| 5486 | |
| 5487 | return err; |
| 5488 | } |
| 5489 | |
Andrii Nakryiko | 63f2f5e | 2019-07-01 16:58:57 -0700 | [diff] [blame] | 5490 | struct bpf_link_fd { |
| 5491 | struct bpf_link link; /* has to be at the top of struct */ |
| 5492 | int fd; /* hook FD */ |
| 5493 | }; |
| 5494 | |
| 5495 | static int bpf_link__destroy_perf_event(struct bpf_link *link) |
| 5496 | { |
| 5497 | struct bpf_link_fd *l = (void *)link; |
| 5498 | int err; |
| 5499 | |
| 5500 | err = ioctl(l->fd, PERF_EVENT_IOC_DISABLE, 0); |
| 5501 | if (err) |
| 5502 | err = -errno; |
| 5503 | |
| 5504 | close(l->fd); |
| 5505 | return err; |
| 5506 | } |
| 5507 | |
| 5508 | struct bpf_link *bpf_program__attach_perf_event(struct bpf_program *prog, |
| 5509 | int pfd) |
| 5510 | { |
| 5511 | char errmsg[STRERR_BUFSIZE]; |
| 5512 | struct bpf_link_fd *link; |
| 5513 | int prog_fd, err; |
| 5514 | |
| 5515 | if (pfd < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5516 | pr_warn("program '%s': invalid perf event FD %d\n", |
| 5517 | bpf_program__title(prog, false), pfd); |
Andrii Nakryiko | 63f2f5e | 2019-07-01 16:58:57 -0700 | [diff] [blame] | 5518 | return ERR_PTR(-EINVAL); |
| 5519 | } |
| 5520 | prog_fd = bpf_program__fd(prog); |
| 5521 | if (prog_fd < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5522 | pr_warn("program '%s': can't attach BPF program w/o FD (did you load it?)\n", |
| 5523 | bpf_program__title(prog, false)); |
Andrii Nakryiko | 63f2f5e | 2019-07-01 16:58:57 -0700 | [diff] [blame] | 5524 | return ERR_PTR(-EINVAL); |
| 5525 | } |
| 5526 | |
| 5527 | link = malloc(sizeof(*link)); |
| 5528 | if (!link) |
| 5529 | return ERR_PTR(-ENOMEM); |
| 5530 | link->link.destroy = &bpf_link__destroy_perf_event; |
| 5531 | link->fd = pfd; |
| 5532 | |
| 5533 | if (ioctl(pfd, PERF_EVENT_IOC_SET_BPF, prog_fd) < 0) { |
| 5534 | err = -errno; |
| 5535 | free(link); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5536 | pr_warn("program '%s': failed to attach to pfd %d: %s\n", |
| 5537 | bpf_program__title(prog, false), pfd, |
Andrii Nakryiko | 63f2f5e | 2019-07-01 16:58:57 -0700 | [diff] [blame] | 5538 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
| 5539 | return ERR_PTR(err); |
| 5540 | } |
| 5541 | if (ioctl(pfd, PERF_EVENT_IOC_ENABLE, 0) < 0) { |
| 5542 | err = -errno; |
| 5543 | free(link); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5544 | pr_warn("program '%s': failed to enable pfd %d: %s\n", |
| 5545 | bpf_program__title(prog, false), pfd, |
Andrii Nakryiko | 63f2f5e | 2019-07-01 16:58:57 -0700 | [diff] [blame] | 5546 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
| 5547 | return ERR_PTR(err); |
| 5548 | } |
| 5549 | return (struct bpf_link *)link; |
| 5550 | } |
| 5551 | |
Andrii Nakryiko | b265002 | 2019-07-01 16:58:58 -0700 | [diff] [blame] | 5552 | /* |
| 5553 | * this function is expected to parse integer in the range of [0, 2^31-1] from |
| 5554 | * given file using scanf format string fmt. If actual parsed value is |
| 5555 | * negative, the result might be indistinguishable from error |
| 5556 | */ |
| 5557 | static int parse_uint_from_file(const char *file, const char *fmt) |
| 5558 | { |
| 5559 | char buf[STRERR_BUFSIZE]; |
| 5560 | int err, ret; |
| 5561 | FILE *f; |
| 5562 | |
| 5563 | f = fopen(file, "r"); |
| 5564 | if (!f) { |
| 5565 | err = -errno; |
| 5566 | pr_debug("failed to open '%s': %s\n", file, |
| 5567 | libbpf_strerror_r(err, buf, sizeof(buf))); |
| 5568 | return err; |
| 5569 | } |
| 5570 | err = fscanf(f, fmt, &ret); |
| 5571 | if (err != 1) { |
| 5572 | err = err == EOF ? -EIO : -errno; |
| 5573 | pr_debug("failed to parse '%s': %s\n", file, |
| 5574 | libbpf_strerror_r(err, buf, sizeof(buf))); |
| 5575 | fclose(f); |
| 5576 | return err; |
| 5577 | } |
| 5578 | fclose(f); |
| 5579 | return ret; |
| 5580 | } |
| 5581 | |
| 5582 | static int determine_kprobe_perf_type(void) |
| 5583 | { |
| 5584 | const char *file = "/sys/bus/event_source/devices/kprobe/type"; |
| 5585 | |
| 5586 | return parse_uint_from_file(file, "%d\n"); |
| 5587 | } |
| 5588 | |
| 5589 | static int determine_uprobe_perf_type(void) |
| 5590 | { |
| 5591 | const char *file = "/sys/bus/event_source/devices/uprobe/type"; |
| 5592 | |
| 5593 | return parse_uint_from_file(file, "%d\n"); |
| 5594 | } |
| 5595 | |
| 5596 | static int determine_kprobe_retprobe_bit(void) |
| 5597 | { |
| 5598 | const char *file = "/sys/bus/event_source/devices/kprobe/format/retprobe"; |
| 5599 | |
| 5600 | return parse_uint_from_file(file, "config:%d\n"); |
| 5601 | } |
| 5602 | |
| 5603 | static int determine_uprobe_retprobe_bit(void) |
| 5604 | { |
| 5605 | const char *file = "/sys/bus/event_source/devices/uprobe/format/retprobe"; |
| 5606 | |
| 5607 | return parse_uint_from_file(file, "config:%d\n"); |
| 5608 | } |
| 5609 | |
| 5610 | static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name, |
| 5611 | uint64_t offset, int pid) |
| 5612 | { |
| 5613 | struct perf_event_attr attr = {}; |
| 5614 | char errmsg[STRERR_BUFSIZE]; |
| 5615 | int type, pfd, err; |
| 5616 | |
| 5617 | type = uprobe ? determine_uprobe_perf_type() |
| 5618 | : determine_kprobe_perf_type(); |
| 5619 | if (type < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5620 | pr_warn("failed to determine %s perf type: %s\n", |
| 5621 | uprobe ? "uprobe" : "kprobe", |
| 5622 | libbpf_strerror_r(type, errmsg, sizeof(errmsg))); |
Andrii Nakryiko | b265002 | 2019-07-01 16:58:58 -0700 | [diff] [blame] | 5623 | return type; |
| 5624 | } |
| 5625 | if (retprobe) { |
| 5626 | int bit = uprobe ? determine_uprobe_retprobe_bit() |
| 5627 | : determine_kprobe_retprobe_bit(); |
| 5628 | |
| 5629 | if (bit < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5630 | pr_warn("failed to determine %s retprobe bit: %s\n", |
| 5631 | uprobe ? "uprobe" : "kprobe", |
| 5632 | libbpf_strerror_r(bit, errmsg, sizeof(errmsg))); |
Andrii Nakryiko | b265002 | 2019-07-01 16:58:58 -0700 | [diff] [blame] | 5633 | return bit; |
| 5634 | } |
| 5635 | attr.config |= 1 << bit; |
| 5636 | } |
| 5637 | attr.size = sizeof(attr); |
| 5638 | attr.type = type; |
Andrii Nakryiko | 36db2a9 | 2019-07-08 21:00:07 -0700 | [diff] [blame] | 5639 | attr.config1 = ptr_to_u64(name); /* kprobe_func or uprobe_path */ |
| 5640 | attr.config2 = offset; /* kprobe_addr or probe_offset */ |
Andrii Nakryiko | b265002 | 2019-07-01 16:58:58 -0700 | [diff] [blame] | 5641 | |
| 5642 | /* pid filter is meaningful only for uprobes */ |
| 5643 | pfd = syscall(__NR_perf_event_open, &attr, |
| 5644 | pid < 0 ? -1 : pid /* pid */, |
| 5645 | pid == -1 ? 0 : -1 /* cpu */, |
| 5646 | -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC); |
| 5647 | if (pfd < 0) { |
| 5648 | err = -errno; |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5649 | pr_warn("%s perf_event_open() failed: %s\n", |
| 5650 | uprobe ? "uprobe" : "kprobe", |
| 5651 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
Andrii Nakryiko | b265002 | 2019-07-01 16:58:58 -0700 | [diff] [blame] | 5652 | return err; |
| 5653 | } |
| 5654 | return pfd; |
| 5655 | } |
| 5656 | |
| 5657 | struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog, |
| 5658 | bool retprobe, |
| 5659 | const char *func_name) |
| 5660 | { |
| 5661 | char errmsg[STRERR_BUFSIZE]; |
| 5662 | struct bpf_link *link; |
| 5663 | int pfd, err; |
| 5664 | |
| 5665 | pfd = perf_event_open_probe(false /* uprobe */, retprobe, func_name, |
| 5666 | 0 /* offset */, -1 /* pid */); |
| 5667 | if (pfd < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5668 | pr_warn("program '%s': failed to create %s '%s' perf event: %s\n", |
| 5669 | bpf_program__title(prog, false), |
| 5670 | retprobe ? "kretprobe" : "kprobe", func_name, |
| 5671 | libbpf_strerror_r(pfd, errmsg, sizeof(errmsg))); |
Andrii Nakryiko | b265002 | 2019-07-01 16:58:58 -0700 | [diff] [blame] | 5672 | return ERR_PTR(pfd); |
| 5673 | } |
| 5674 | link = bpf_program__attach_perf_event(prog, pfd); |
| 5675 | if (IS_ERR(link)) { |
| 5676 | close(pfd); |
| 5677 | err = PTR_ERR(link); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5678 | pr_warn("program '%s': failed to attach to %s '%s': %s\n", |
| 5679 | bpf_program__title(prog, false), |
| 5680 | retprobe ? "kretprobe" : "kprobe", func_name, |
| 5681 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
Andrii Nakryiko | b265002 | 2019-07-01 16:58:58 -0700 | [diff] [blame] | 5682 | return link; |
| 5683 | } |
| 5684 | return link; |
| 5685 | } |
| 5686 | |
| 5687 | struct bpf_link *bpf_program__attach_uprobe(struct bpf_program *prog, |
| 5688 | bool retprobe, pid_t pid, |
| 5689 | const char *binary_path, |
| 5690 | size_t func_offset) |
| 5691 | { |
| 5692 | char errmsg[STRERR_BUFSIZE]; |
| 5693 | struct bpf_link *link; |
| 5694 | int pfd, err; |
| 5695 | |
| 5696 | pfd = perf_event_open_probe(true /* uprobe */, retprobe, |
| 5697 | binary_path, func_offset, pid); |
| 5698 | if (pfd < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5699 | pr_warn("program '%s': failed to create %s '%s:0x%zx' perf event: %s\n", |
| 5700 | bpf_program__title(prog, false), |
| 5701 | retprobe ? "uretprobe" : "uprobe", |
| 5702 | binary_path, func_offset, |
| 5703 | libbpf_strerror_r(pfd, errmsg, sizeof(errmsg))); |
Andrii Nakryiko | b265002 | 2019-07-01 16:58:58 -0700 | [diff] [blame] | 5704 | return ERR_PTR(pfd); |
| 5705 | } |
| 5706 | link = bpf_program__attach_perf_event(prog, pfd); |
| 5707 | if (IS_ERR(link)) { |
| 5708 | close(pfd); |
| 5709 | err = PTR_ERR(link); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5710 | pr_warn("program '%s': failed to attach to %s '%s:0x%zx': %s\n", |
| 5711 | bpf_program__title(prog, false), |
| 5712 | retprobe ? "uretprobe" : "uprobe", |
| 5713 | binary_path, func_offset, |
| 5714 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
Andrii Nakryiko | b265002 | 2019-07-01 16:58:58 -0700 | [diff] [blame] | 5715 | return link; |
| 5716 | } |
| 5717 | return link; |
| 5718 | } |
| 5719 | |
Andrii Nakryiko | f6de59c | 2019-07-01 16:58:59 -0700 | [diff] [blame] | 5720 | static int determine_tracepoint_id(const char *tp_category, |
| 5721 | const char *tp_name) |
| 5722 | { |
| 5723 | char file[PATH_MAX]; |
| 5724 | int ret; |
| 5725 | |
| 5726 | ret = snprintf(file, sizeof(file), |
| 5727 | "/sys/kernel/debug/tracing/events/%s/%s/id", |
| 5728 | tp_category, tp_name); |
| 5729 | if (ret < 0) |
| 5730 | return -errno; |
| 5731 | if (ret >= sizeof(file)) { |
| 5732 | pr_debug("tracepoint %s/%s path is too long\n", |
| 5733 | tp_category, tp_name); |
| 5734 | return -E2BIG; |
| 5735 | } |
| 5736 | return parse_uint_from_file(file, "%d\n"); |
| 5737 | } |
| 5738 | |
| 5739 | static int perf_event_open_tracepoint(const char *tp_category, |
| 5740 | const char *tp_name) |
| 5741 | { |
| 5742 | struct perf_event_attr attr = {}; |
| 5743 | char errmsg[STRERR_BUFSIZE]; |
| 5744 | int tp_id, pfd, err; |
| 5745 | |
| 5746 | tp_id = determine_tracepoint_id(tp_category, tp_name); |
| 5747 | if (tp_id < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5748 | pr_warn("failed to determine tracepoint '%s/%s' perf event ID: %s\n", |
| 5749 | tp_category, tp_name, |
| 5750 | libbpf_strerror_r(tp_id, errmsg, sizeof(errmsg))); |
Andrii Nakryiko | f6de59c | 2019-07-01 16:58:59 -0700 | [diff] [blame] | 5751 | return tp_id; |
| 5752 | } |
| 5753 | |
| 5754 | attr.type = PERF_TYPE_TRACEPOINT; |
| 5755 | attr.size = sizeof(attr); |
| 5756 | attr.config = tp_id; |
| 5757 | |
| 5758 | pfd = syscall(__NR_perf_event_open, &attr, -1 /* pid */, 0 /* cpu */, |
| 5759 | -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC); |
| 5760 | if (pfd < 0) { |
| 5761 | err = -errno; |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5762 | pr_warn("tracepoint '%s/%s' perf_event_open() failed: %s\n", |
| 5763 | tp_category, tp_name, |
| 5764 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
Andrii Nakryiko | f6de59c | 2019-07-01 16:58:59 -0700 | [diff] [blame] | 5765 | return err; |
| 5766 | } |
| 5767 | return pfd; |
| 5768 | } |
| 5769 | |
| 5770 | struct bpf_link *bpf_program__attach_tracepoint(struct bpf_program *prog, |
| 5771 | const char *tp_category, |
| 5772 | const char *tp_name) |
| 5773 | { |
| 5774 | char errmsg[STRERR_BUFSIZE]; |
| 5775 | struct bpf_link *link; |
| 5776 | int pfd, err; |
| 5777 | |
| 5778 | pfd = perf_event_open_tracepoint(tp_category, tp_name); |
| 5779 | if (pfd < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5780 | pr_warn("program '%s': failed to create tracepoint '%s/%s' perf event: %s\n", |
| 5781 | bpf_program__title(prog, false), |
| 5782 | tp_category, tp_name, |
| 5783 | libbpf_strerror_r(pfd, errmsg, sizeof(errmsg))); |
Andrii Nakryiko | f6de59c | 2019-07-01 16:58:59 -0700 | [diff] [blame] | 5784 | return ERR_PTR(pfd); |
| 5785 | } |
| 5786 | link = bpf_program__attach_perf_event(prog, pfd); |
| 5787 | if (IS_ERR(link)) { |
| 5788 | close(pfd); |
| 5789 | err = PTR_ERR(link); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5790 | pr_warn("program '%s': failed to attach to tracepoint '%s/%s': %s\n", |
| 5791 | bpf_program__title(prog, false), |
| 5792 | tp_category, tp_name, |
| 5793 | libbpf_strerror_r(err, errmsg, sizeof(errmsg))); |
Andrii Nakryiko | f6de59c | 2019-07-01 16:58:59 -0700 | [diff] [blame] | 5794 | return link; |
| 5795 | } |
| 5796 | return link; |
| 5797 | } |
| 5798 | |
Andrii Nakryiko | 84bf5e1 | 2019-07-01 16:59:00 -0700 | [diff] [blame] | 5799 | static int bpf_link__destroy_fd(struct bpf_link *link) |
| 5800 | { |
| 5801 | struct bpf_link_fd *l = (void *)link; |
| 5802 | |
| 5803 | return close(l->fd); |
| 5804 | } |
| 5805 | |
| 5806 | struct bpf_link *bpf_program__attach_raw_tracepoint(struct bpf_program *prog, |
| 5807 | const char *tp_name) |
| 5808 | { |
| 5809 | char errmsg[STRERR_BUFSIZE]; |
| 5810 | struct bpf_link_fd *link; |
| 5811 | int prog_fd, pfd; |
| 5812 | |
| 5813 | prog_fd = bpf_program__fd(prog); |
| 5814 | if (prog_fd < 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5815 | pr_warn("program '%s': can't attach before loaded\n", |
| 5816 | bpf_program__title(prog, false)); |
Andrii Nakryiko | 84bf5e1 | 2019-07-01 16:59:00 -0700 | [diff] [blame] | 5817 | return ERR_PTR(-EINVAL); |
| 5818 | } |
| 5819 | |
| 5820 | link = malloc(sizeof(*link)); |
| 5821 | if (!link) |
| 5822 | return ERR_PTR(-ENOMEM); |
| 5823 | link->link.destroy = &bpf_link__destroy_fd; |
| 5824 | |
| 5825 | pfd = bpf_raw_tracepoint_open(tp_name, prog_fd); |
| 5826 | if (pfd < 0) { |
| 5827 | pfd = -errno; |
| 5828 | free(link); |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5829 | pr_warn("program '%s': failed to attach to raw tracepoint '%s': %s\n", |
| 5830 | bpf_program__title(prog, false), tp_name, |
| 5831 | libbpf_strerror_r(pfd, errmsg, sizeof(errmsg))); |
Andrii Nakryiko | 84bf5e1 | 2019-07-01 16:59:00 -0700 | [diff] [blame] | 5832 | return ERR_PTR(pfd); |
| 5833 | } |
| 5834 | link->fd = pfd; |
| 5835 | return (struct bpf_link *)link; |
| 5836 | } |
| 5837 | |
Alexei Starovoitov | b8c54ea | 2019-11-14 10:57:06 -0800 | [diff] [blame] | 5838 | struct bpf_link *bpf_program__attach_trace(struct bpf_program *prog) |
| 5839 | { |
| 5840 | char errmsg[STRERR_BUFSIZE]; |
| 5841 | struct bpf_link_fd *link; |
| 5842 | int prog_fd, pfd; |
| 5843 | |
| 5844 | prog_fd = bpf_program__fd(prog); |
| 5845 | if (prog_fd < 0) { |
| 5846 | pr_warn("program '%s': can't attach before loaded\n", |
| 5847 | bpf_program__title(prog, false)); |
| 5848 | return ERR_PTR(-EINVAL); |
| 5849 | } |
| 5850 | |
| 5851 | link = malloc(sizeof(*link)); |
| 5852 | if (!link) |
| 5853 | return ERR_PTR(-ENOMEM); |
| 5854 | link->link.destroy = &bpf_link__destroy_fd; |
| 5855 | |
| 5856 | pfd = bpf_raw_tracepoint_open(NULL, prog_fd); |
| 5857 | if (pfd < 0) { |
| 5858 | pfd = -errno; |
| 5859 | free(link); |
| 5860 | pr_warn("program '%s': failed to attach to trace: %s\n", |
| 5861 | bpf_program__title(prog, false), |
| 5862 | libbpf_strerror_r(pfd, errmsg, sizeof(errmsg))); |
| 5863 | return ERR_PTR(pfd); |
| 5864 | } |
| 5865 | link->fd = pfd; |
| 5866 | return (struct bpf_link *)link; |
| 5867 | } |
| 5868 | |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 5869 | enum bpf_perf_event_ret |
Daniel Borkmann | 3dca211 | 2018-10-21 02:09:28 +0200 | [diff] [blame] | 5870 | bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size, |
| 5871 | void **copy_mem, size_t *copy_size, |
| 5872 | bpf_perf_event_print_t fn, void *private_data) |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 5873 | { |
Daniel Borkmann | 3dca211 | 2018-10-21 02:09:28 +0200 | [diff] [blame] | 5874 | struct perf_event_mmap_page *header = mmap_mem; |
Daniel Borkmann | a64af0e | 2018-10-19 15:51:03 +0200 | [diff] [blame] | 5875 | __u64 data_head = ring_buffer_read_head(header); |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 5876 | __u64 data_tail = header->data_tail; |
Daniel Borkmann | 3dca211 | 2018-10-21 02:09:28 +0200 | [diff] [blame] | 5877 | void *base = ((__u8 *)header) + page_size; |
| 5878 | int ret = LIBBPF_PERF_EVENT_CONT; |
| 5879 | struct perf_event_header *ehdr; |
| 5880 | size_t ehdr_size; |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 5881 | |
Daniel Borkmann | 3dca211 | 2018-10-21 02:09:28 +0200 | [diff] [blame] | 5882 | while (data_head != data_tail) { |
| 5883 | ehdr = base + (data_tail & (mmap_size - 1)); |
| 5884 | ehdr_size = ehdr->size; |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 5885 | |
Daniel Borkmann | 3dca211 | 2018-10-21 02:09:28 +0200 | [diff] [blame] | 5886 | if (((void *)ehdr) + ehdr_size > base + mmap_size) { |
| 5887 | void *copy_start = ehdr; |
| 5888 | size_t len_first = base + mmap_size - copy_start; |
| 5889 | size_t len_secnd = ehdr_size - len_first; |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 5890 | |
Daniel Borkmann | 3dca211 | 2018-10-21 02:09:28 +0200 | [diff] [blame] | 5891 | if (*copy_size < ehdr_size) { |
| 5892 | free(*copy_mem); |
| 5893 | *copy_mem = malloc(ehdr_size); |
| 5894 | if (!*copy_mem) { |
| 5895 | *copy_size = 0; |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 5896 | ret = LIBBPF_PERF_EVENT_ERROR; |
| 5897 | break; |
| 5898 | } |
Daniel Borkmann | 3dca211 | 2018-10-21 02:09:28 +0200 | [diff] [blame] | 5899 | *copy_size = ehdr_size; |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 5900 | } |
| 5901 | |
Daniel Borkmann | 3dca211 | 2018-10-21 02:09:28 +0200 | [diff] [blame] | 5902 | memcpy(*copy_mem, copy_start, len_first); |
| 5903 | memcpy(*copy_mem + len_first, base, len_secnd); |
| 5904 | ehdr = *copy_mem; |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 5905 | } |
| 5906 | |
Daniel Borkmann | 3dca211 | 2018-10-21 02:09:28 +0200 | [diff] [blame] | 5907 | ret = fn(ehdr, private_data); |
| 5908 | data_tail += ehdr_size; |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 5909 | if (ret != LIBBPF_PERF_EVENT_CONT) |
| 5910 | break; |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 5911 | } |
| 5912 | |
Daniel Borkmann | a64af0e | 2018-10-19 15:51:03 +0200 | [diff] [blame] | 5913 | ring_buffer_write_tail(header, data_tail); |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 5914 | return ret; |
| 5915 | } |
Song Liu | 34be1646 | 2019-03-11 22:30:38 -0700 | [diff] [blame] | 5916 | |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 5917 | struct perf_buffer; |
| 5918 | |
| 5919 | struct perf_buffer_params { |
| 5920 | struct perf_event_attr *attr; |
| 5921 | /* if event_cb is specified, it takes precendence */ |
| 5922 | perf_buffer_event_fn event_cb; |
| 5923 | /* sample_cb and lost_cb are higher-level common-case callbacks */ |
| 5924 | perf_buffer_sample_fn sample_cb; |
| 5925 | perf_buffer_lost_fn lost_cb; |
| 5926 | void *ctx; |
| 5927 | int cpu_cnt; |
| 5928 | int *cpus; |
| 5929 | int *map_keys; |
| 5930 | }; |
| 5931 | |
| 5932 | struct perf_cpu_buf { |
| 5933 | struct perf_buffer *pb; |
| 5934 | void *base; /* mmap()'ed memory */ |
| 5935 | void *buf; /* for reconstructing segmented data */ |
| 5936 | size_t buf_size; |
| 5937 | int fd; |
| 5938 | int cpu; |
| 5939 | int map_key; |
| 5940 | }; |
| 5941 | |
| 5942 | struct perf_buffer { |
| 5943 | perf_buffer_event_fn event_cb; |
| 5944 | perf_buffer_sample_fn sample_cb; |
| 5945 | perf_buffer_lost_fn lost_cb; |
| 5946 | void *ctx; /* passed into callbacks */ |
| 5947 | |
| 5948 | size_t page_size; |
| 5949 | size_t mmap_size; |
| 5950 | struct perf_cpu_buf **cpu_bufs; |
| 5951 | struct epoll_event *events; |
| 5952 | int cpu_cnt; |
| 5953 | int epoll_fd; /* perf event FD */ |
| 5954 | int map_fd; /* BPF_MAP_TYPE_PERF_EVENT_ARRAY BPF map FD */ |
| 5955 | }; |
| 5956 | |
| 5957 | static void perf_buffer__free_cpu_buf(struct perf_buffer *pb, |
| 5958 | struct perf_cpu_buf *cpu_buf) |
| 5959 | { |
| 5960 | if (!cpu_buf) |
| 5961 | return; |
| 5962 | if (cpu_buf->base && |
| 5963 | munmap(cpu_buf->base, pb->mmap_size + pb->page_size)) |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 5964 | pr_warn("failed to munmap cpu_buf #%d\n", cpu_buf->cpu); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 5965 | if (cpu_buf->fd >= 0) { |
| 5966 | ioctl(cpu_buf->fd, PERF_EVENT_IOC_DISABLE, 0); |
| 5967 | close(cpu_buf->fd); |
| 5968 | } |
| 5969 | free(cpu_buf->buf); |
| 5970 | free(cpu_buf); |
| 5971 | } |
| 5972 | |
| 5973 | void perf_buffer__free(struct perf_buffer *pb) |
| 5974 | { |
| 5975 | int i; |
| 5976 | |
| 5977 | if (!pb) |
| 5978 | return; |
| 5979 | if (pb->cpu_bufs) { |
| 5980 | for (i = 0; i < pb->cpu_cnt && pb->cpu_bufs[i]; i++) { |
| 5981 | struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i]; |
| 5982 | |
| 5983 | bpf_map_delete_elem(pb->map_fd, &cpu_buf->map_key); |
| 5984 | perf_buffer__free_cpu_buf(pb, cpu_buf); |
| 5985 | } |
| 5986 | free(pb->cpu_bufs); |
| 5987 | } |
| 5988 | if (pb->epoll_fd >= 0) |
| 5989 | close(pb->epoll_fd); |
| 5990 | free(pb->events); |
| 5991 | free(pb); |
| 5992 | } |
| 5993 | |
| 5994 | static struct perf_cpu_buf * |
| 5995 | perf_buffer__open_cpu_buf(struct perf_buffer *pb, struct perf_event_attr *attr, |
| 5996 | int cpu, int map_key) |
| 5997 | { |
| 5998 | struct perf_cpu_buf *cpu_buf; |
| 5999 | char msg[STRERR_BUFSIZE]; |
| 6000 | int err; |
| 6001 | |
| 6002 | cpu_buf = calloc(1, sizeof(*cpu_buf)); |
| 6003 | if (!cpu_buf) |
| 6004 | return ERR_PTR(-ENOMEM); |
| 6005 | |
| 6006 | cpu_buf->pb = pb; |
| 6007 | cpu_buf->cpu = cpu; |
| 6008 | cpu_buf->map_key = map_key; |
| 6009 | |
| 6010 | cpu_buf->fd = syscall(__NR_perf_event_open, attr, -1 /* pid */, cpu, |
| 6011 | -1, PERF_FLAG_FD_CLOEXEC); |
| 6012 | if (cpu_buf->fd < 0) { |
| 6013 | err = -errno; |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6014 | pr_warn("failed to open perf buffer event on cpu #%d: %s\n", |
| 6015 | cpu, libbpf_strerror_r(err, msg, sizeof(msg))); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 6016 | goto error; |
| 6017 | } |
| 6018 | |
| 6019 | cpu_buf->base = mmap(NULL, pb->mmap_size + pb->page_size, |
| 6020 | PROT_READ | PROT_WRITE, MAP_SHARED, |
| 6021 | cpu_buf->fd, 0); |
| 6022 | if (cpu_buf->base == MAP_FAILED) { |
| 6023 | cpu_buf->base = NULL; |
| 6024 | err = -errno; |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6025 | pr_warn("failed to mmap perf buffer on cpu #%d: %s\n", |
| 6026 | cpu, libbpf_strerror_r(err, msg, sizeof(msg))); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 6027 | goto error; |
| 6028 | } |
| 6029 | |
| 6030 | if (ioctl(cpu_buf->fd, PERF_EVENT_IOC_ENABLE, 0) < 0) { |
| 6031 | err = -errno; |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6032 | pr_warn("failed to enable perf buffer event on cpu #%d: %s\n", |
| 6033 | cpu, libbpf_strerror_r(err, msg, sizeof(msg))); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 6034 | goto error; |
| 6035 | } |
| 6036 | |
| 6037 | return cpu_buf; |
| 6038 | |
| 6039 | error: |
| 6040 | perf_buffer__free_cpu_buf(pb, cpu_buf); |
| 6041 | return (struct perf_cpu_buf *)ERR_PTR(err); |
| 6042 | } |
| 6043 | |
| 6044 | static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt, |
| 6045 | struct perf_buffer_params *p); |
| 6046 | |
| 6047 | struct perf_buffer *perf_buffer__new(int map_fd, size_t page_cnt, |
| 6048 | const struct perf_buffer_opts *opts) |
| 6049 | { |
| 6050 | struct perf_buffer_params p = {}; |
Arnaldo Carvalho de Melo | 4be6e05 | 2019-07-19 11:34:07 -0300 | [diff] [blame] | 6051 | struct perf_event_attr attr = { 0, }; |
| 6052 | |
| 6053 | attr.config = PERF_COUNT_SW_BPF_OUTPUT, |
| 6054 | attr.type = PERF_TYPE_SOFTWARE; |
| 6055 | attr.sample_type = PERF_SAMPLE_RAW; |
| 6056 | attr.sample_period = 1; |
| 6057 | attr.wakeup_events = 1; |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 6058 | |
| 6059 | p.attr = &attr; |
| 6060 | p.sample_cb = opts ? opts->sample_cb : NULL; |
| 6061 | p.lost_cb = opts ? opts->lost_cb : NULL; |
| 6062 | p.ctx = opts ? opts->ctx : NULL; |
| 6063 | |
| 6064 | return __perf_buffer__new(map_fd, page_cnt, &p); |
| 6065 | } |
| 6066 | |
| 6067 | struct perf_buffer * |
| 6068 | perf_buffer__new_raw(int map_fd, size_t page_cnt, |
| 6069 | const struct perf_buffer_raw_opts *opts) |
| 6070 | { |
| 6071 | struct perf_buffer_params p = {}; |
| 6072 | |
| 6073 | p.attr = opts->attr; |
| 6074 | p.event_cb = opts->event_cb; |
| 6075 | p.ctx = opts->ctx; |
| 6076 | p.cpu_cnt = opts->cpu_cnt; |
| 6077 | p.cpus = opts->cpus; |
| 6078 | p.map_keys = opts->map_keys; |
| 6079 | |
| 6080 | return __perf_buffer__new(map_fd, page_cnt, &p); |
| 6081 | } |
| 6082 | |
| 6083 | static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt, |
| 6084 | struct perf_buffer_params *p) |
| 6085 | { |
| 6086 | struct bpf_map_info map = {}; |
| 6087 | char msg[STRERR_BUFSIZE]; |
| 6088 | struct perf_buffer *pb; |
| 6089 | __u32 map_info_len; |
| 6090 | int err, i; |
| 6091 | |
| 6092 | if (page_cnt & (page_cnt - 1)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6093 | pr_warn("page count should be power of two, but is %zu\n", |
| 6094 | page_cnt); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 6095 | return ERR_PTR(-EINVAL); |
| 6096 | } |
| 6097 | |
| 6098 | map_info_len = sizeof(map); |
| 6099 | err = bpf_obj_get_info_by_fd(map_fd, &map, &map_info_len); |
| 6100 | if (err) { |
| 6101 | err = -errno; |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6102 | pr_warn("failed to get map info for map FD %d: %s\n", |
| 6103 | map_fd, libbpf_strerror_r(err, msg, sizeof(msg))); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 6104 | return ERR_PTR(err); |
| 6105 | } |
| 6106 | |
| 6107 | if (map.type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6108 | pr_warn("map '%s' should be BPF_MAP_TYPE_PERF_EVENT_ARRAY\n", |
| 6109 | map.name); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 6110 | return ERR_PTR(-EINVAL); |
| 6111 | } |
| 6112 | |
| 6113 | pb = calloc(1, sizeof(*pb)); |
| 6114 | if (!pb) |
| 6115 | return ERR_PTR(-ENOMEM); |
| 6116 | |
| 6117 | pb->event_cb = p->event_cb; |
| 6118 | pb->sample_cb = p->sample_cb; |
| 6119 | pb->lost_cb = p->lost_cb; |
| 6120 | pb->ctx = p->ctx; |
| 6121 | |
| 6122 | pb->page_size = getpagesize(); |
| 6123 | pb->mmap_size = pb->page_size * page_cnt; |
| 6124 | pb->map_fd = map_fd; |
| 6125 | |
| 6126 | pb->epoll_fd = epoll_create1(EPOLL_CLOEXEC); |
| 6127 | if (pb->epoll_fd < 0) { |
| 6128 | err = -errno; |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6129 | pr_warn("failed to create epoll instance: %s\n", |
| 6130 | libbpf_strerror_r(err, msg, sizeof(msg))); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 6131 | goto error; |
| 6132 | } |
| 6133 | |
| 6134 | if (p->cpu_cnt > 0) { |
| 6135 | pb->cpu_cnt = p->cpu_cnt; |
| 6136 | } else { |
| 6137 | pb->cpu_cnt = libbpf_num_possible_cpus(); |
| 6138 | if (pb->cpu_cnt < 0) { |
| 6139 | err = pb->cpu_cnt; |
| 6140 | goto error; |
| 6141 | } |
| 6142 | if (map.max_entries < pb->cpu_cnt) |
| 6143 | pb->cpu_cnt = map.max_entries; |
| 6144 | } |
| 6145 | |
| 6146 | pb->events = calloc(pb->cpu_cnt, sizeof(*pb->events)); |
| 6147 | if (!pb->events) { |
| 6148 | err = -ENOMEM; |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6149 | pr_warn("failed to allocate events: out of memory\n"); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 6150 | goto error; |
| 6151 | } |
| 6152 | pb->cpu_bufs = calloc(pb->cpu_cnt, sizeof(*pb->cpu_bufs)); |
| 6153 | if (!pb->cpu_bufs) { |
| 6154 | err = -ENOMEM; |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6155 | pr_warn("failed to allocate buffers: out of memory\n"); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 6156 | goto error; |
| 6157 | } |
| 6158 | |
| 6159 | for (i = 0; i < pb->cpu_cnt; i++) { |
| 6160 | struct perf_cpu_buf *cpu_buf; |
| 6161 | int cpu, map_key; |
| 6162 | |
| 6163 | cpu = p->cpu_cnt > 0 ? p->cpus[i] : i; |
| 6164 | map_key = p->cpu_cnt > 0 ? p->map_keys[i] : i; |
| 6165 | |
| 6166 | cpu_buf = perf_buffer__open_cpu_buf(pb, p->attr, cpu, map_key); |
| 6167 | if (IS_ERR(cpu_buf)) { |
| 6168 | err = PTR_ERR(cpu_buf); |
| 6169 | goto error; |
| 6170 | } |
| 6171 | |
| 6172 | pb->cpu_bufs[i] = cpu_buf; |
| 6173 | |
| 6174 | err = bpf_map_update_elem(pb->map_fd, &map_key, |
| 6175 | &cpu_buf->fd, 0); |
| 6176 | if (err) { |
| 6177 | err = -errno; |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6178 | pr_warn("failed to set cpu #%d, key %d -> perf FD %d: %s\n", |
| 6179 | cpu, map_key, cpu_buf->fd, |
| 6180 | libbpf_strerror_r(err, msg, sizeof(msg))); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 6181 | goto error; |
| 6182 | } |
| 6183 | |
| 6184 | pb->events[i].events = EPOLLIN; |
| 6185 | pb->events[i].data.ptr = cpu_buf; |
| 6186 | if (epoll_ctl(pb->epoll_fd, EPOLL_CTL_ADD, cpu_buf->fd, |
| 6187 | &pb->events[i]) < 0) { |
| 6188 | err = -errno; |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6189 | pr_warn("failed to epoll_ctl cpu #%d perf FD %d: %s\n", |
| 6190 | cpu, cpu_buf->fd, |
| 6191 | libbpf_strerror_r(err, msg, sizeof(msg))); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 6192 | goto error; |
| 6193 | } |
| 6194 | } |
| 6195 | |
| 6196 | return pb; |
| 6197 | |
| 6198 | error: |
| 6199 | if (pb) |
| 6200 | perf_buffer__free(pb); |
| 6201 | return ERR_PTR(err); |
| 6202 | } |
| 6203 | |
| 6204 | struct perf_sample_raw { |
| 6205 | struct perf_event_header header; |
| 6206 | uint32_t size; |
| 6207 | char data[0]; |
| 6208 | }; |
| 6209 | |
| 6210 | struct perf_sample_lost { |
| 6211 | struct perf_event_header header; |
| 6212 | uint64_t id; |
| 6213 | uint64_t lost; |
| 6214 | uint64_t sample_id; |
| 6215 | }; |
| 6216 | |
| 6217 | static enum bpf_perf_event_ret |
| 6218 | perf_buffer__process_record(struct perf_event_header *e, void *ctx) |
| 6219 | { |
| 6220 | struct perf_cpu_buf *cpu_buf = ctx; |
| 6221 | struct perf_buffer *pb = cpu_buf->pb; |
| 6222 | void *data = e; |
| 6223 | |
| 6224 | /* user wants full control over parsing perf event */ |
| 6225 | if (pb->event_cb) |
| 6226 | return pb->event_cb(pb->ctx, cpu_buf->cpu, e); |
| 6227 | |
| 6228 | switch (e->type) { |
| 6229 | case PERF_RECORD_SAMPLE: { |
| 6230 | struct perf_sample_raw *s = data; |
| 6231 | |
| 6232 | if (pb->sample_cb) |
| 6233 | pb->sample_cb(pb->ctx, cpu_buf->cpu, s->data, s->size); |
| 6234 | break; |
| 6235 | } |
| 6236 | case PERF_RECORD_LOST: { |
| 6237 | struct perf_sample_lost *s = data; |
| 6238 | |
| 6239 | if (pb->lost_cb) |
| 6240 | pb->lost_cb(pb->ctx, cpu_buf->cpu, s->lost); |
| 6241 | break; |
| 6242 | } |
| 6243 | default: |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6244 | pr_warn("unknown perf sample type %d\n", e->type); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 6245 | return LIBBPF_PERF_EVENT_ERROR; |
| 6246 | } |
| 6247 | return LIBBPF_PERF_EVENT_CONT; |
| 6248 | } |
| 6249 | |
| 6250 | static int perf_buffer__process_records(struct perf_buffer *pb, |
| 6251 | struct perf_cpu_buf *cpu_buf) |
| 6252 | { |
| 6253 | enum bpf_perf_event_ret ret; |
| 6254 | |
| 6255 | ret = bpf_perf_event_read_simple(cpu_buf->base, pb->mmap_size, |
| 6256 | pb->page_size, &cpu_buf->buf, |
| 6257 | &cpu_buf->buf_size, |
| 6258 | perf_buffer__process_record, cpu_buf); |
| 6259 | if (ret != LIBBPF_PERF_EVENT_CONT) |
| 6260 | return ret; |
| 6261 | return 0; |
| 6262 | } |
| 6263 | |
| 6264 | int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms) |
| 6265 | { |
| 6266 | int i, cnt, err; |
| 6267 | |
| 6268 | cnt = epoll_wait(pb->epoll_fd, pb->events, pb->cpu_cnt, timeout_ms); |
| 6269 | for (i = 0; i < cnt; i++) { |
| 6270 | struct perf_cpu_buf *cpu_buf = pb->events[i].data.ptr; |
| 6271 | |
| 6272 | err = perf_buffer__process_records(pb, cpu_buf); |
| 6273 | if (err) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6274 | pr_warn("error while processing records: %d\n", err); |
Andrii Nakryiko | fb84b82 | 2019-07-06 11:06:24 -0700 | [diff] [blame] | 6275 | return err; |
| 6276 | } |
| 6277 | } |
| 6278 | return cnt < 0 ? -errno : cnt; |
| 6279 | } |
| 6280 | |
Song Liu | 34be1646 | 2019-03-11 22:30:38 -0700 | [diff] [blame] | 6281 | struct bpf_prog_info_array_desc { |
| 6282 | int array_offset; /* e.g. offset of jited_prog_insns */ |
| 6283 | int count_offset; /* e.g. offset of jited_prog_len */ |
| 6284 | int size_offset; /* > 0: offset of rec size, |
| 6285 | * < 0: fix size of -size_offset |
| 6286 | */ |
| 6287 | }; |
| 6288 | |
| 6289 | static struct bpf_prog_info_array_desc bpf_prog_info_array_desc[] = { |
| 6290 | [BPF_PROG_INFO_JITED_INSNS] = { |
| 6291 | offsetof(struct bpf_prog_info, jited_prog_insns), |
| 6292 | offsetof(struct bpf_prog_info, jited_prog_len), |
| 6293 | -1, |
| 6294 | }, |
| 6295 | [BPF_PROG_INFO_XLATED_INSNS] = { |
| 6296 | offsetof(struct bpf_prog_info, xlated_prog_insns), |
| 6297 | offsetof(struct bpf_prog_info, xlated_prog_len), |
| 6298 | -1, |
| 6299 | }, |
| 6300 | [BPF_PROG_INFO_MAP_IDS] = { |
| 6301 | offsetof(struct bpf_prog_info, map_ids), |
| 6302 | offsetof(struct bpf_prog_info, nr_map_ids), |
| 6303 | -(int)sizeof(__u32), |
| 6304 | }, |
| 6305 | [BPF_PROG_INFO_JITED_KSYMS] = { |
| 6306 | offsetof(struct bpf_prog_info, jited_ksyms), |
| 6307 | offsetof(struct bpf_prog_info, nr_jited_ksyms), |
| 6308 | -(int)sizeof(__u64), |
| 6309 | }, |
| 6310 | [BPF_PROG_INFO_JITED_FUNC_LENS] = { |
| 6311 | offsetof(struct bpf_prog_info, jited_func_lens), |
| 6312 | offsetof(struct bpf_prog_info, nr_jited_func_lens), |
| 6313 | -(int)sizeof(__u32), |
| 6314 | }, |
| 6315 | [BPF_PROG_INFO_FUNC_INFO] = { |
| 6316 | offsetof(struct bpf_prog_info, func_info), |
| 6317 | offsetof(struct bpf_prog_info, nr_func_info), |
| 6318 | offsetof(struct bpf_prog_info, func_info_rec_size), |
| 6319 | }, |
| 6320 | [BPF_PROG_INFO_LINE_INFO] = { |
| 6321 | offsetof(struct bpf_prog_info, line_info), |
| 6322 | offsetof(struct bpf_prog_info, nr_line_info), |
| 6323 | offsetof(struct bpf_prog_info, line_info_rec_size), |
| 6324 | }, |
| 6325 | [BPF_PROG_INFO_JITED_LINE_INFO] = { |
| 6326 | offsetof(struct bpf_prog_info, jited_line_info), |
| 6327 | offsetof(struct bpf_prog_info, nr_jited_line_info), |
| 6328 | offsetof(struct bpf_prog_info, jited_line_info_rec_size), |
| 6329 | }, |
| 6330 | [BPF_PROG_INFO_PROG_TAGS] = { |
| 6331 | offsetof(struct bpf_prog_info, prog_tags), |
| 6332 | offsetof(struct bpf_prog_info, nr_prog_tags), |
| 6333 | -(int)sizeof(__u8) * BPF_TAG_SIZE, |
| 6334 | }, |
| 6335 | |
| 6336 | }; |
| 6337 | |
Andrii Nakryiko | 8983b73 | 2019-11-20 23:07:42 -0800 | [diff] [blame] | 6338 | static __u32 bpf_prog_info_read_offset_u32(struct bpf_prog_info *info, |
| 6339 | int offset) |
Song Liu | 34be1646 | 2019-03-11 22:30:38 -0700 | [diff] [blame] | 6340 | { |
| 6341 | __u32 *array = (__u32 *)info; |
| 6342 | |
| 6343 | if (offset >= 0) |
| 6344 | return array[offset / sizeof(__u32)]; |
| 6345 | return -(int)offset; |
| 6346 | } |
| 6347 | |
Andrii Nakryiko | 8983b73 | 2019-11-20 23:07:42 -0800 | [diff] [blame] | 6348 | static __u64 bpf_prog_info_read_offset_u64(struct bpf_prog_info *info, |
| 6349 | int offset) |
Song Liu | 34be1646 | 2019-03-11 22:30:38 -0700 | [diff] [blame] | 6350 | { |
| 6351 | __u64 *array = (__u64 *)info; |
| 6352 | |
| 6353 | if (offset >= 0) |
| 6354 | return array[offset / sizeof(__u64)]; |
| 6355 | return -(int)offset; |
| 6356 | } |
| 6357 | |
| 6358 | static void bpf_prog_info_set_offset_u32(struct bpf_prog_info *info, int offset, |
| 6359 | __u32 val) |
| 6360 | { |
| 6361 | __u32 *array = (__u32 *)info; |
| 6362 | |
| 6363 | if (offset >= 0) |
| 6364 | array[offset / sizeof(__u32)] = val; |
| 6365 | } |
| 6366 | |
| 6367 | static void bpf_prog_info_set_offset_u64(struct bpf_prog_info *info, int offset, |
| 6368 | __u64 val) |
| 6369 | { |
| 6370 | __u64 *array = (__u64 *)info; |
| 6371 | |
| 6372 | if (offset >= 0) |
| 6373 | array[offset / sizeof(__u64)] = val; |
| 6374 | } |
| 6375 | |
| 6376 | struct bpf_prog_info_linear * |
| 6377 | bpf_program__get_prog_info_linear(int fd, __u64 arrays) |
| 6378 | { |
| 6379 | struct bpf_prog_info_linear *info_linear; |
| 6380 | struct bpf_prog_info info = {}; |
| 6381 | __u32 info_len = sizeof(info); |
| 6382 | __u32 data_len = 0; |
| 6383 | int i, err; |
| 6384 | void *ptr; |
| 6385 | |
| 6386 | if (arrays >> BPF_PROG_INFO_LAST_ARRAY) |
| 6387 | return ERR_PTR(-EINVAL); |
| 6388 | |
| 6389 | /* step 1: get array dimensions */ |
| 6390 | err = bpf_obj_get_info_by_fd(fd, &info, &info_len); |
| 6391 | if (err) { |
| 6392 | pr_debug("can't get prog info: %s", strerror(errno)); |
| 6393 | return ERR_PTR(-EFAULT); |
| 6394 | } |
| 6395 | |
| 6396 | /* step 2: calculate total size of all arrays */ |
| 6397 | for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) { |
| 6398 | bool include_array = (arrays & (1UL << i)) > 0; |
| 6399 | struct bpf_prog_info_array_desc *desc; |
| 6400 | __u32 count, size; |
| 6401 | |
| 6402 | desc = bpf_prog_info_array_desc + i; |
| 6403 | |
| 6404 | /* kernel is too old to support this field */ |
| 6405 | if (info_len < desc->array_offset + sizeof(__u32) || |
| 6406 | info_len < desc->count_offset + sizeof(__u32) || |
| 6407 | (desc->size_offset > 0 && info_len < desc->size_offset)) |
| 6408 | include_array = false; |
| 6409 | |
| 6410 | if (!include_array) { |
| 6411 | arrays &= ~(1UL << i); /* clear the bit */ |
| 6412 | continue; |
| 6413 | } |
| 6414 | |
| 6415 | count = bpf_prog_info_read_offset_u32(&info, desc->count_offset); |
| 6416 | size = bpf_prog_info_read_offset_u32(&info, desc->size_offset); |
| 6417 | |
| 6418 | data_len += count * size; |
| 6419 | } |
| 6420 | |
| 6421 | /* step 3: allocate continuous memory */ |
| 6422 | data_len = roundup(data_len, sizeof(__u64)); |
| 6423 | info_linear = malloc(sizeof(struct bpf_prog_info_linear) + data_len); |
| 6424 | if (!info_linear) |
| 6425 | return ERR_PTR(-ENOMEM); |
| 6426 | |
| 6427 | /* step 4: fill data to info_linear->info */ |
| 6428 | info_linear->arrays = arrays; |
| 6429 | memset(&info_linear->info, 0, sizeof(info)); |
| 6430 | ptr = info_linear->data; |
| 6431 | |
| 6432 | for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) { |
| 6433 | struct bpf_prog_info_array_desc *desc; |
| 6434 | __u32 count, size; |
| 6435 | |
| 6436 | if ((arrays & (1UL << i)) == 0) |
| 6437 | continue; |
| 6438 | |
| 6439 | desc = bpf_prog_info_array_desc + i; |
| 6440 | count = bpf_prog_info_read_offset_u32(&info, desc->count_offset); |
| 6441 | size = bpf_prog_info_read_offset_u32(&info, desc->size_offset); |
| 6442 | bpf_prog_info_set_offset_u32(&info_linear->info, |
| 6443 | desc->count_offset, count); |
| 6444 | bpf_prog_info_set_offset_u32(&info_linear->info, |
| 6445 | desc->size_offset, size); |
| 6446 | bpf_prog_info_set_offset_u64(&info_linear->info, |
| 6447 | desc->array_offset, |
| 6448 | ptr_to_u64(ptr)); |
| 6449 | ptr += count * size; |
| 6450 | } |
| 6451 | |
| 6452 | /* step 5: call syscall again to get required arrays */ |
| 6453 | err = bpf_obj_get_info_by_fd(fd, &info_linear->info, &info_len); |
| 6454 | if (err) { |
| 6455 | pr_debug("can't get prog info: %s", strerror(errno)); |
| 6456 | free(info_linear); |
| 6457 | return ERR_PTR(-EFAULT); |
| 6458 | } |
| 6459 | |
| 6460 | /* step 6: verify the data */ |
| 6461 | for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) { |
| 6462 | struct bpf_prog_info_array_desc *desc; |
| 6463 | __u32 v1, v2; |
| 6464 | |
| 6465 | if ((arrays & (1UL << i)) == 0) |
| 6466 | continue; |
| 6467 | |
| 6468 | desc = bpf_prog_info_array_desc + i; |
| 6469 | v1 = bpf_prog_info_read_offset_u32(&info, desc->count_offset); |
| 6470 | v2 = bpf_prog_info_read_offset_u32(&info_linear->info, |
| 6471 | desc->count_offset); |
| 6472 | if (v1 != v2) |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6473 | pr_warn("%s: mismatch in element count\n", __func__); |
Song Liu | 34be1646 | 2019-03-11 22:30:38 -0700 | [diff] [blame] | 6474 | |
| 6475 | v1 = bpf_prog_info_read_offset_u32(&info, desc->size_offset); |
| 6476 | v2 = bpf_prog_info_read_offset_u32(&info_linear->info, |
| 6477 | desc->size_offset); |
| 6478 | if (v1 != v2) |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6479 | pr_warn("%s: mismatch in rec size\n", __func__); |
Song Liu | 34be1646 | 2019-03-11 22:30:38 -0700 | [diff] [blame] | 6480 | } |
| 6481 | |
| 6482 | /* step 7: update info_len and data_len */ |
| 6483 | info_linear->info_len = sizeof(struct bpf_prog_info); |
| 6484 | info_linear->data_len = data_len; |
| 6485 | |
| 6486 | return info_linear; |
| 6487 | } |
| 6488 | |
| 6489 | void bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear) |
| 6490 | { |
| 6491 | int i; |
| 6492 | |
| 6493 | for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) { |
| 6494 | struct bpf_prog_info_array_desc *desc; |
| 6495 | __u64 addr, offs; |
| 6496 | |
| 6497 | if ((info_linear->arrays & (1UL << i)) == 0) |
| 6498 | continue; |
| 6499 | |
| 6500 | desc = bpf_prog_info_array_desc + i; |
| 6501 | addr = bpf_prog_info_read_offset_u64(&info_linear->info, |
| 6502 | desc->array_offset); |
| 6503 | offs = addr - ptr_to_u64(info_linear->data); |
| 6504 | bpf_prog_info_set_offset_u64(&info_linear->info, |
| 6505 | desc->array_offset, offs); |
| 6506 | } |
| 6507 | } |
| 6508 | |
| 6509 | void bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear) |
| 6510 | { |
| 6511 | int i; |
| 6512 | |
| 6513 | for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) { |
| 6514 | struct bpf_prog_info_array_desc *desc; |
| 6515 | __u64 addr, offs; |
| 6516 | |
| 6517 | if ((info_linear->arrays & (1UL << i)) == 0) |
| 6518 | continue; |
| 6519 | |
| 6520 | desc = bpf_prog_info_array_desc + i; |
| 6521 | offs = bpf_prog_info_read_offset_u64(&info_linear->info, |
| 6522 | desc->array_offset); |
| 6523 | addr = offs + ptr_to_u64(info_linear->data); |
| 6524 | bpf_prog_info_set_offset_u64(&info_linear->info, |
| 6525 | desc->array_offset, addr); |
| 6526 | } |
| 6527 | } |
Hechao Li | 6446b31 | 2019-06-10 17:56:50 -0700 | [diff] [blame] | 6528 | |
| 6529 | int libbpf_num_possible_cpus(void) |
| 6530 | { |
| 6531 | static const char *fcpu = "/sys/devices/system/cpu/possible"; |
| 6532 | int len = 0, n = 0, il = 0, ir = 0; |
| 6533 | unsigned int start = 0, end = 0; |
Takshak Chahande | 56fbc24 | 2019-07-31 15:10:55 -0700 | [diff] [blame] | 6534 | int tmp_cpus = 0; |
Hechao Li | 6446b31 | 2019-06-10 17:56:50 -0700 | [diff] [blame] | 6535 | static int cpus; |
| 6536 | char buf[128]; |
| 6537 | int error = 0; |
| 6538 | int fd = -1; |
| 6539 | |
Takshak Chahande | 56fbc24 | 2019-07-31 15:10:55 -0700 | [diff] [blame] | 6540 | tmp_cpus = READ_ONCE(cpus); |
| 6541 | if (tmp_cpus > 0) |
| 6542 | return tmp_cpus; |
Hechao Li | 6446b31 | 2019-06-10 17:56:50 -0700 | [diff] [blame] | 6543 | |
| 6544 | fd = open(fcpu, O_RDONLY); |
| 6545 | if (fd < 0) { |
| 6546 | error = errno; |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6547 | pr_warn("Failed to open file %s: %s\n", fcpu, strerror(error)); |
Hechao Li | 6446b31 | 2019-06-10 17:56:50 -0700 | [diff] [blame] | 6548 | return -error; |
| 6549 | } |
| 6550 | len = read(fd, buf, sizeof(buf)); |
| 6551 | close(fd); |
| 6552 | if (len <= 0) { |
| 6553 | error = len ? errno : EINVAL; |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6554 | pr_warn("Failed to read # of possible cpus from %s: %s\n", |
| 6555 | fcpu, strerror(error)); |
Hechao Li | 6446b31 | 2019-06-10 17:56:50 -0700 | [diff] [blame] | 6556 | return -error; |
| 6557 | } |
| 6558 | if (len == sizeof(buf)) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6559 | pr_warn("File %s size overflow\n", fcpu); |
Hechao Li | 6446b31 | 2019-06-10 17:56:50 -0700 | [diff] [blame] | 6560 | return -EOVERFLOW; |
| 6561 | } |
| 6562 | buf[len] = '\0'; |
| 6563 | |
Takshak Chahande | 56fbc24 | 2019-07-31 15:10:55 -0700 | [diff] [blame] | 6564 | for (ir = 0, tmp_cpus = 0; ir <= len; ir++) { |
Hechao Li | 6446b31 | 2019-06-10 17:56:50 -0700 | [diff] [blame] | 6565 | /* Each sub string separated by ',' has format \d+-\d+ or \d+ */ |
| 6566 | if (buf[ir] == ',' || buf[ir] == '\0') { |
| 6567 | buf[ir] = '\0'; |
| 6568 | n = sscanf(&buf[il], "%u-%u", &start, &end); |
| 6569 | if (n <= 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6570 | pr_warn("Failed to get # CPUs from %s\n", |
| 6571 | &buf[il]); |
Hechao Li | 6446b31 | 2019-06-10 17:56:50 -0700 | [diff] [blame] | 6572 | return -EINVAL; |
| 6573 | } else if (n == 1) { |
| 6574 | end = start; |
| 6575 | } |
Takshak Chahande | 56fbc24 | 2019-07-31 15:10:55 -0700 | [diff] [blame] | 6576 | tmp_cpus += end - start + 1; |
Hechao Li | 6446b31 | 2019-06-10 17:56:50 -0700 | [diff] [blame] | 6577 | il = ir + 1; |
| 6578 | } |
| 6579 | } |
Takshak Chahande | 56fbc24 | 2019-07-31 15:10:55 -0700 | [diff] [blame] | 6580 | if (tmp_cpus <= 0) { |
Kefeng Wang | be18010 | 2019-10-21 13:55:32 +0800 | [diff] [blame] | 6581 | pr_warn("Invalid #CPUs %d from %s\n", tmp_cpus, fcpu); |
Hechao Li | 6446b31 | 2019-06-10 17:56:50 -0700 | [diff] [blame] | 6582 | return -EINVAL; |
| 6583 | } |
Takshak Chahande | 56fbc24 | 2019-07-31 15:10:55 -0700 | [diff] [blame] | 6584 | |
| 6585 | WRITE_ONCE(cpus, tmp_cpus); |
| 6586 | return tmp_cpus; |
Hechao Li | 6446b31 | 2019-06-10 17:56:50 -0700 | [diff] [blame] | 6587 | } |