Eric Leblond | 6061a3d | 2018-01-30 21:55:03 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: LGPL-2.1 |
| 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. |
Wang Nan | 203d1ca | 2016-07-04 11:02:42 +0000 | [diff] [blame] | 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU Lesser General Public |
| 13 | * License as published by the Free Software Foundation; |
| 14 | * version 2.1 of the License (not later!) |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU Lesser General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU Lesser General Public |
| 22 | * License along with this program; if not, see <http://www.gnu.org/licenses> |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 23 | */ |
| 24 | |
Jakub Kicinski | 531b014 | 2018-07-10 14:43:05 -0700 | [diff] [blame] | 25 | #define _GNU_SOURCE |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 26 | #include <stdlib.h> |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <stdarg.h> |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 29 | #include <libgen.h> |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 30 | #include <inttypes.h> |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame] | 31 | #include <string.h> |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 32 | #include <unistd.h> |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 33 | #include <fcntl.h> |
| 34 | #include <errno.h> |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 35 | #include <perf-sys.h> |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 36 | #include <asm/unistd.h> |
Joe Stringer | e28ff1a | 2017-01-22 17:11:25 -0800 | [diff] [blame] | 37 | #include <linux/err.h> |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 38 | #include <linux/kernel.h> |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 39 | #include <linux/bpf.h> |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 40 | #include <linux/list.h> |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 41 | #include <linux/limits.h> |
| 42 | #include <sys/stat.h> |
| 43 | #include <sys/types.h> |
| 44 | #include <sys/vfs.h> |
Jakub Kicinski | 531b014 | 2018-07-10 14:43:05 -0700 | [diff] [blame] | 45 | #include <tools/libc_compat.h> |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 46 | #include <libelf.h> |
| 47 | #include <gelf.h> |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 48 | |
| 49 | #include "libbpf.h" |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 50 | #include "bpf.h" |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 51 | #include "btf.h" |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame] | 52 | |
Wang Nan | 9b16137 | 2016-07-18 06:01:08 +0000 | [diff] [blame] | 53 | #ifndef EM_BPF |
| 54 | #define EM_BPF 247 |
| 55 | #endif |
| 56 | |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 57 | #ifndef BPF_FS_MAGIC |
| 58 | #define BPF_FS_MAGIC 0xcafe4a11 |
| 59 | #endif |
| 60 | |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame] | 61 | #define __printf(a, b) __attribute__((format(printf, a, b))) |
| 62 | |
| 63 | __printf(1, 2) |
| 64 | static int __base_pr(const char *format, ...) |
| 65 | { |
| 66 | va_list args; |
| 67 | int err; |
| 68 | |
| 69 | va_start(args, format); |
| 70 | err = vfprintf(stderr, format, args); |
| 71 | va_end(args); |
| 72 | return err; |
| 73 | } |
| 74 | |
| 75 | static __printf(1, 2) libbpf_print_fn_t __pr_warning = __base_pr; |
| 76 | static __printf(1, 2) libbpf_print_fn_t __pr_info = __base_pr; |
| 77 | static __printf(1, 2) libbpf_print_fn_t __pr_debug; |
| 78 | |
| 79 | #define __pr(func, fmt, ...) \ |
| 80 | do { \ |
| 81 | if ((func)) \ |
| 82 | (func)("libbpf: " fmt, ##__VA_ARGS__); \ |
| 83 | } while (0) |
| 84 | |
| 85 | #define pr_warning(fmt, ...) __pr(__pr_warning, fmt, ##__VA_ARGS__) |
| 86 | #define pr_info(fmt, ...) __pr(__pr_info, fmt, ##__VA_ARGS__) |
| 87 | #define pr_debug(fmt, ...) __pr(__pr_debug, fmt, ##__VA_ARGS__) |
| 88 | |
| 89 | void libbpf_set_print(libbpf_print_fn_t warn, |
| 90 | libbpf_print_fn_t info, |
| 91 | libbpf_print_fn_t debug) |
| 92 | { |
| 93 | __pr_warning = warn; |
| 94 | __pr_info = info; |
| 95 | __pr_debug = debug; |
| 96 | } |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 97 | |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 98 | #define STRERR_BUFSIZE 128 |
| 99 | |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 100 | #define CHECK_ERR(action, err, out) do { \ |
| 101 | err = action; \ |
| 102 | if (err) \ |
| 103 | goto out; \ |
| 104 | } while(0) |
| 105 | |
| 106 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 107 | /* Copied from tools/perf/util/util.h */ |
| 108 | #ifndef zfree |
| 109 | # define zfree(ptr) ({ free(*ptr); *ptr = NULL; }) |
| 110 | #endif |
| 111 | |
| 112 | #ifndef zclose |
| 113 | # define zclose(fd) ({ \ |
| 114 | int ___err = 0; \ |
| 115 | if ((fd) >= 0) \ |
| 116 | ___err = close((fd)); \ |
| 117 | fd = -1; \ |
| 118 | ___err; }) |
| 119 | #endif |
| 120 | |
| 121 | #ifdef HAVE_LIBELF_MMAP_SUPPORT |
| 122 | # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP |
| 123 | #else |
| 124 | # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ |
| 125 | #endif |
| 126 | |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 127 | /* |
| 128 | * bpf_prog should be a better name but it has been used in |
| 129 | * linux/filter.h. |
| 130 | */ |
| 131 | struct bpf_program { |
| 132 | /* Index in elf obj file, for relocation use. */ |
| 133 | int idx; |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 134 | char *name; |
David Beckett | f0307a7 | 2018-05-16 14:02:49 -0700 | [diff] [blame] | 135 | int prog_ifindex; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 136 | char *section_name; |
| 137 | struct bpf_insn *insns; |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 138 | size_t insns_cnt, main_prog_cnt; |
Wang Nan | 5f44e4c8 | 2016-07-13 10:44:01 +0000 | [diff] [blame] | 139 | enum bpf_prog_type type; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 140 | |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 141 | struct reloc_desc { |
| 142 | enum { |
| 143 | RELO_LD64, |
| 144 | RELO_CALL, |
| 145 | } type; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 146 | int insn_idx; |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 147 | union { |
| 148 | int map_idx; |
| 149 | int text_off; |
| 150 | }; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 151 | } *reloc_desc; |
| 152 | int nr_reloc; |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 153 | |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 154 | struct { |
| 155 | int nr; |
| 156 | int *fds; |
| 157 | } instances; |
| 158 | bpf_program_prep_t preprocessor; |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 159 | |
| 160 | struct bpf_object *obj; |
| 161 | void *priv; |
| 162 | bpf_program_clear_priv_t clear_priv; |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 163 | |
| 164 | enum bpf_attach_type expected_attach_type; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 165 | }; |
| 166 | |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 167 | struct bpf_map { |
| 168 | int fd; |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 169 | char *name; |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 170 | size_t offset; |
David Beckett | f0307a7 | 2018-05-16 14:02:49 -0700 | [diff] [blame] | 171 | int map_ifindex; |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 172 | struct bpf_map_def def; |
Martin KaFai Lau | 61746dbe | 2018-05-22 15:04:24 -0700 | [diff] [blame] | 173 | uint32_t btf_key_type_id; |
| 174 | uint32_t btf_value_type_id; |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 175 | void *priv; |
| 176 | bpf_map_clear_priv_t clear_priv; |
| 177 | }; |
| 178 | |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 179 | static LIST_HEAD(bpf_objects_list); |
| 180 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 181 | struct bpf_object { |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 182 | char license[64]; |
| 183 | u32 kern_version; |
Wang Nan | 0b3d1ef | 2015-07-01 02:13:58 +0000 | [diff] [blame] | 184 | |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 185 | struct bpf_program *programs; |
| 186 | size_t nr_programs; |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 187 | struct bpf_map *maps; |
| 188 | size_t nr_maps; |
| 189 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 190 | bool loaded; |
Jakub Kicinski | 9a94f27 | 2018-06-28 14:41:38 -0700 | [diff] [blame] | 191 | bool has_pseudo_calls; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 192 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 193 | /* |
| 194 | * Information when doing elf related work. Only valid if fd |
| 195 | * is valid. |
| 196 | */ |
| 197 | struct { |
| 198 | int fd; |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 199 | void *obj_buf; |
| 200 | size_t obj_buf_sz; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 201 | Elf *elf; |
| 202 | GElf_Ehdr ehdr; |
Wang Nan | bec7d68 | 2015-07-01 02:13:59 +0000 | [diff] [blame] | 203 | Elf_Data *symbols; |
Wang Nan | 77ba9a5 | 2015-12-08 02:25:30 +0000 | [diff] [blame] | 204 | size_t strtabidx; |
Wang Nan | b62f06e | 2015-07-01 02:14:01 +0000 | [diff] [blame] | 205 | struct { |
| 206 | GElf_Shdr shdr; |
| 207 | Elf_Data *data; |
| 208 | } *reloc; |
| 209 | int nr_reloc; |
Wang Nan | 666810e | 2016-01-25 09:55:49 +0000 | [diff] [blame] | 210 | int maps_shndx; |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 211 | int text_shndx; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 212 | } efile; |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 213 | /* |
| 214 | * All loaded bpf_object is linked in a list, which is |
| 215 | * hidden to caller. bpf_objects__<func> handlers deal with |
| 216 | * all objects. |
| 217 | */ |
| 218 | struct list_head list; |
Wang Nan | 10931d2 | 2016-11-26 07:03:26 +0000 | [diff] [blame] | 219 | |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 220 | struct btf *btf; |
| 221 | |
Wang Nan | 10931d2 | 2016-11-26 07:03:26 +0000 | [diff] [blame] | 222 | void *priv; |
| 223 | bpf_object_clear_priv_t clear_priv; |
| 224 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 225 | char path[]; |
| 226 | }; |
| 227 | #define obj_elf_valid(o) ((o)->efile.elf) |
| 228 | |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 229 | static void bpf_program__unload(struct bpf_program *prog) |
| 230 | { |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 231 | int i; |
| 232 | |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 233 | if (!prog) |
| 234 | return; |
| 235 | |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 236 | /* |
| 237 | * If the object is opened but the program was never loaded, |
| 238 | * it is possible that prog->instances.nr == -1. |
| 239 | */ |
| 240 | if (prog->instances.nr > 0) { |
| 241 | for (i = 0; i < prog->instances.nr; i++) |
| 242 | zclose(prog->instances.fds[i]); |
| 243 | } else if (prog->instances.nr != -1) { |
| 244 | pr_warning("Internal error: instances.nr is %d\n", |
| 245 | prog->instances.nr); |
| 246 | } |
| 247 | |
| 248 | prog->instances.nr = -1; |
| 249 | zfree(&prog->instances.fds); |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 252 | static void bpf_program__exit(struct bpf_program *prog) |
| 253 | { |
| 254 | if (!prog) |
| 255 | return; |
| 256 | |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 257 | if (prog->clear_priv) |
| 258 | prog->clear_priv(prog, prog->priv); |
| 259 | |
| 260 | prog->priv = NULL; |
| 261 | prog->clear_priv = NULL; |
| 262 | |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 263 | bpf_program__unload(prog); |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 264 | zfree(&prog->name); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 265 | zfree(&prog->section_name); |
| 266 | zfree(&prog->insns); |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 267 | zfree(&prog->reloc_desc); |
| 268 | |
| 269 | prog->nr_reloc = 0; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 270 | prog->insns_cnt = 0; |
| 271 | prog->idx = -1; |
| 272 | } |
| 273 | |
| 274 | static int |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 275 | bpf_program__init(void *data, size_t size, char *section_name, int idx, |
| 276 | struct bpf_program *prog) |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 277 | { |
| 278 | if (size < sizeof(struct bpf_insn)) { |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 279 | pr_warning("corrupted section '%s'\n", section_name); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 280 | return -EINVAL; |
| 281 | } |
| 282 | |
| 283 | bzero(prog, sizeof(*prog)); |
| 284 | |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 285 | prog->section_name = strdup(section_name); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 286 | if (!prog->section_name) { |
Jesper Dangaard Brouer | 077c066 | 2018-02-08 12:48:17 +0100 | [diff] [blame] | 287 | pr_warning("failed to alloc name for prog under section(%d) %s\n", |
| 288 | idx, section_name); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 289 | goto errout; |
| 290 | } |
| 291 | |
| 292 | prog->insns = malloc(size); |
| 293 | if (!prog->insns) { |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 294 | pr_warning("failed to alloc insns for prog under section %s\n", |
| 295 | section_name); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 296 | goto errout; |
| 297 | } |
| 298 | prog->insns_cnt = size / sizeof(struct bpf_insn); |
| 299 | memcpy(prog->insns, data, |
| 300 | prog->insns_cnt * sizeof(struct bpf_insn)); |
| 301 | prog->idx = idx; |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 302 | prog->instances.fds = NULL; |
| 303 | prog->instances.nr = -1; |
Wang Nan | 5f44e4c8 | 2016-07-13 10:44:01 +0000 | [diff] [blame] | 304 | prog->type = BPF_PROG_TYPE_KPROBE; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 305 | |
| 306 | return 0; |
| 307 | errout: |
| 308 | bpf_program__exit(prog); |
| 309 | return -ENOMEM; |
| 310 | } |
| 311 | |
| 312 | static int |
| 313 | 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] | 314 | char *section_name, int idx) |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 315 | { |
| 316 | struct bpf_program prog, *progs; |
| 317 | int nr_progs, err; |
| 318 | |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 319 | err = bpf_program__init(data, size, section_name, idx, &prog); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 320 | if (err) |
| 321 | return err; |
| 322 | |
| 323 | progs = obj->programs; |
| 324 | nr_progs = obj->nr_programs; |
| 325 | |
Jakub Kicinski | 531b014 | 2018-07-10 14:43:05 -0700 | [diff] [blame] | 326 | progs = reallocarray(progs, nr_progs + 1, sizeof(progs[0])); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 327 | if (!progs) { |
| 328 | /* |
| 329 | * In this case the original obj->programs |
| 330 | * is still valid, so don't need special treat for |
| 331 | * bpf_close_object(). |
| 332 | */ |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 333 | pr_warning("failed to alloc a new program under section '%s'\n", |
| 334 | section_name); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 335 | bpf_program__exit(&prog); |
| 336 | return -ENOMEM; |
| 337 | } |
| 338 | |
| 339 | pr_debug("found program %s\n", prog.section_name); |
| 340 | obj->programs = progs; |
| 341 | obj->nr_programs = nr_progs + 1; |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 342 | prog.obj = obj; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 343 | progs[nr_progs] = prog; |
| 344 | return 0; |
| 345 | } |
| 346 | |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 347 | static int |
| 348 | bpf_object__init_prog_names(struct bpf_object *obj) |
| 349 | { |
| 350 | Elf_Data *symbols = obj->efile.symbols; |
| 351 | struct bpf_program *prog; |
| 352 | size_t pi, si; |
| 353 | |
| 354 | for (pi = 0; pi < obj->nr_programs; pi++) { |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 355 | const char *name = NULL; |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 356 | |
| 357 | prog = &obj->programs[pi]; |
| 358 | |
| 359 | for (si = 0; si < symbols->d_size / sizeof(GElf_Sym) && !name; |
| 360 | si++) { |
| 361 | GElf_Sym sym; |
| 362 | |
| 363 | if (!gelf_getsym(symbols, si, &sym)) |
| 364 | continue; |
| 365 | if (sym.st_shndx != prog->idx) |
| 366 | continue; |
Roman Gushchin | fe4d44b | 2017-12-13 15:18:52 +0000 | [diff] [blame] | 367 | if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL) |
| 368 | continue; |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 369 | |
| 370 | name = elf_strptr(obj->efile.elf, |
| 371 | obj->efile.strtabidx, |
| 372 | sym.st_name); |
| 373 | if (!name) { |
| 374 | pr_warning("failed to get sym name string for prog %s\n", |
| 375 | prog->section_name); |
| 376 | return -LIBBPF_ERRNO__LIBELF; |
| 377 | } |
| 378 | } |
| 379 | |
Jakub Kicinski | 9a94f27 | 2018-06-28 14:41:38 -0700 | [diff] [blame] | 380 | if (!name && prog->idx == obj->efile.text_shndx) |
| 381 | name = ".text"; |
| 382 | |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 383 | if (!name) { |
| 384 | pr_warning("failed to find sym for prog %s\n", |
| 385 | prog->section_name); |
| 386 | return -EINVAL; |
| 387 | } |
Jakub Kicinski | 9a94f27 | 2018-06-28 14:41:38 -0700 | [diff] [blame] | 388 | |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 389 | prog->name = strdup(name); |
| 390 | if (!prog->name) { |
| 391 | pr_warning("failed to allocate memory for prog sym %s\n", |
| 392 | name); |
| 393 | return -ENOMEM; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | return 0; |
| 398 | } |
| 399 | |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 400 | static struct bpf_object *bpf_object__new(const char *path, |
| 401 | void *obj_buf, |
| 402 | size_t obj_buf_sz) |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 403 | { |
| 404 | struct bpf_object *obj; |
| 405 | |
| 406 | obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1); |
| 407 | if (!obj) { |
| 408 | pr_warning("alloc memory failed for %s\n", path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 409 | return ERR_PTR(-ENOMEM); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | strcpy(obj->path, path); |
| 413 | obj->efile.fd = -1; |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 414 | |
| 415 | /* |
| 416 | * Caller of this function should also calls |
| 417 | * bpf_object__elf_finish() after data collection to return |
| 418 | * obj_buf to user. If not, we should duplicate the buffer to |
| 419 | * avoid user freeing them before elf finish. |
| 420 | */ |
| 421 | obj->efile.obj_buf = obj_buf; |
| 422 | obj->efile.obj_buf_sz = obj_buf_sz; |
Wang Nan | 666810e | 2016-01-25 09:55:49 +0000 | [diff] [blame] | 423 | obj->efile.maps_shndx = -1; |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 424 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 425 | obj->loaded = false; |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 426 | |
| 427 | INIT_LIST_HEAD(&obj->list); |
| 428 | list_add(&obj->list, &bpf_objects_list); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 429 | return obj; |
| 430 | } |
| 431 | |
| 432 | static void bpf_object__elf_finish(struct bpf_object *obj) |
| 433 | { |
| 434 | if (!obj_elf_valid(obj)) |
| 435 | return; |
| 436 | |
| 437 | if (obj->efile.elf) { |
| 438 | elf_end(obj->efile.elf); |
| 439 | obj->efile.elf = NULL; |
| 440 | } |
Wang Nan | bec7d68 | 2015-07-01 02:13:59 +0000 | [diff] [blame] | 441 | obj->efile.symbols = NULL; |
Wang Nan | b62f06e | 2015-07-01 02:14:01 +0000 | [diff] [blame] | 442 | |
| 443 | zfree(&obj->efile.reloc); |
| 444 | obj->efile.nr_reloc = 0; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 445 | zclose(obj->efile.fd); |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 446 | obj->efile.obj_buf = NULL; |
| 447 | obj->efile.obj_buf_sz = 0; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | static int bpf_object__elf_init(struct bpf_object *obj) |
| 451 | { |
| 452 | int err = 0; |
| 453 | GElf_Ehdr *ep; |
| 454 | |
| 455 | if (obj_elf_valid(obj)) { |
| 456 | pr_warning("elf init: internal error\n"); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 457 | return -LIBBPF_ERRNO__LIBELF; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 460 | if (obj->efile.obj_buf_sz > 0) { |
| 461 | /* |
| 462 | * obj_buf should have been validated by |
| 463 | * bpf_object__open_buffer(). |
| 464 | */ |
| 465 | obj->efile.elf = elf_memory(obj->efile.obj_buf, |
| 466 | obj->efile.obj_buf_sz); |
| 467 | } else { |
| 468 | obj->efile.fd = open(obj->path, O_RDONLY); |
| 469 | if (obj->efile.fd < 0) { |
| 470 | pr_warning("failed to open %s: %s\n", obj->path, |
| 471 | strerror(errno)); |
| 472 | return -errno; |
| 473 | } |
| 474 | |
| 475 | obj->efile.elf = elf_begin(obj->efile.fd, |
| 476 | LIBBPF_ELF_C_READ_MMAP, |
| 477 | NULL); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 480 | if (!obj->efile.elf) { |
| 481 | pr_warning("failed to open %s as ELF file\n", |
| 482 | obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 483 | err = -LIBBPF_ERRNO__LIBELF; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 484 | goto errout; |
| 485 | } |
| 486 | |
| 487 | if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) { |
| 488 | pr_warning("failed to get EHDR from %s\n", |
| 489 | obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 490 | err = -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 491 | goto errout; |
| 492 | } |
| 493 | ep = &obj->efile.ehdr; |
| 494 | |
Wang Nan | 9b16137 | 2016-07-18 06:01:08 +0000 | [diff] [blame] | 495 | /* Old LLVM set e_machine to EM_NONE */ |
| 496 | if ((ep->e_type != ET_REL) || (ep->e_machine && (ep->e_machine != EM_BPF))) { |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 497 | pr_warning("%s is not an eBPF object file\n", |
| 498 | obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 499 | err = -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 500 | goto errout; |
| 501 | } |
| 502 | |
| 503 | return 0; |
| 504 | errout: |
| 505 | bpf_object__elf_finish(obj); |
| 506 | return err; |
| 507 | } |
| 508 | |
Wang Nan | cc4228d | 2015-07-01 02:13:55 +0000 | [diff] [blame] | 509 | static int |
| 510 | bpf_object__check_endianness(struct bpf_object *obj) |
| 511 | { |
| 512 | static unsigned int const endian = 1; |
| 513 | |
| 514 | switch (obj->efile.ehdr.e_ident[EI_DATA]) { |
| 515 | case ELFDATA2LSB: |
| 516 | /* We are big endian, BPF obj is little endian. */ |
| 517 | if (*(unsigned char const *)&endian != 1) |
| 518 | goto mismatch; |
| 519 | break; |
| 520 | |
| 521 | case ELFDATA2MSB: |
| 522 | /* We are little endian, BPF obj is big endian. */ |
| 523 | if (*(unsigned char const *)&endian != 0) |
| 524 | goto mismatch; |
| 525 | break; |
| 526 | default: |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 527 | return -LIBBPF_ERRNO__ENDIAN; |
Wang Nan | cc4228d | 2015-07-01 02:13:55 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | return 0; |
| 531 | |
| 532 | mismatch: |
| 533 | pr_warning("Error: endianness mismatch.\n"); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 534 | return -LIBBPF_ERRNO__ENDIAN; |
Wang Nan | cc4228d | 2015-07-01 02:13:55 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 537 | static int |
| 538 | bpf_object__init_license(struct bpf_object *obj, |
| 539 | void *data, size_t size) |
| 540 | { |
| 541 | memcpy(obj->license, data, |
| 542 | min(size, sizeof(obj->license) - 1)); |
| 543 | pr_debug("license of %s is %s\n", obj->path, obj->license); |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | static int |
| 548 | bpf_object__init_kversion(struct bpf_object *obj, |
| 549 | void *data, size_t size) |
| 550 | { |
| 551 | u32 kver; |
| 552 | |
| 553 | if (size != sizeof(kver)) { |
| 554 | pr_warning("invalid kver section in %s\n", obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 555 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 556 | } |
| 557 | memcpy(&kver, data, sizeof(kver)); |
| 558 | obj->kern_version = kver; |
| 559 | pr_debug("kernel version of %s is %x\n", obj->path, |
| 560 | obj->kern_version); |
| 561 | return 0; |
| 562 | } |
| 563 | |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 564 | static int compare_bpf_map(const void *_a, const void *_b) |
| 565 | { |
| 566 | const struct bpf_map *a = _a; |
| 567 | const struct bpf_map *b = _b; |
| 568 | |
| 569 | return a->offset - b->offset; |
| 570 | } |
| 571 | |
| 572 | static int |
| 573 | bpf_object__init_maps(struct bpf_object *obj) |
| 574 | { |
Craig Gallek | b13c5c1 | 2017-10-05 10:41:57 -0400 | [diff] [blame] | 575 | int i, map_idx, map_def_sz, nr_maps = 0; |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 576 | Elf_Scn *scn; |
| 577 | Elf_Data *data; |
| 578 | Elf_Data *symbols = obj->efile.symbols; |
| 579 | |
| 580 | if (obj->efile.maps_shndx < 0) |
| 581 | return -EINVAL; |
| 582 | if (!symbols) |
| 583 | return -EINVAL; |
| 584 | |
| 585 | scn = elf_getscn(obj->efile.elf, obj->efile.maps_shndx); |
| 586 | if (scn) |
| 587 | data = elf_getdata(scn, NULL); |
| 588 | if (!scn || !data) { |
| 589 | pr_warning("failed to get Elf_Data from map section %d\n", |
| 590 | obj->efile.maps_shndx); |
| 591 | return -EINVAL; |
Wang Nan | 0b3d1ef | 2015-07-01 02:13:58 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 594 | /* |
| 595 | * Count number of maps. Each map has a name. |
| 596 | * Array of maps is not supported: only the first element is |
| 597 | * considered. |
| 598 | * |
| 599 | * TODO: Detect array of map and report error. |
| 600 | */ |
| 601 | for (i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) { |
| 602 | GElf_Sym sym; |
| 603 | |
| 604 | if (!gelf_getsym(symbols, i, &sym)) |
| 605 | continue; |
| 606 | if (sym.st_shndx != obj->efile.maps_shndx) |
| 607 | continue; |
| 608 | nr_maps++; |
| 609 | } |
| 610 | |
| 611 | /* Alloc obj->maps and fill nr_maps. */ |
| 612 | pr_debug("maps in %s: %d maps in %zd bytes\n", obj->path, |
| 613 | nr_maps, data->d_size); |
| 614 | |
| 615 | if (!nr_maps) |
| 616 | return 0; |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 617 | |
Craig Gallek | b13c5c1 | 2017-10-05 10:41:57 -0400 | [diff] [blame] | 618 | /* Assume equally sized map definitions */ |
| 619 | map_def_sz = data->d_size / nr_maps; |
| 620 | if (!data->d_size || (data->d_size % nr_maps) != 0) { |
| 621 | pr_warning("unable to determine map definition size " |
| 622 | "section %s, %d maps in %zd bytes\n", |
| 623 | obj->path, nr_maps, data->d_size); |
| 624 | return -EINVAL; |
| 625 | } |
| 626 | |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 627 | obj->maps = calloc(nr_maps, sizeof(obj->maps[0])); |
| 628 | if (!obj->maps) { |
| 629 | pr_warning("alloc maps for object failed\n"); |
Wang Nan | 0b3d1ef | 2015-07-01 02:13:58 +0000 | [diff] [blame] | 630 | return -ENOMEM; |
| 631 | } |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 632 | obj->nr_maps = nr_maps; |
Wang Nan | 0b3d1ef | 2015-07-01 02:13:58 +0000 | [diff] [blame] | 633 | |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 634 | /* |
| 635 | * fill all fd with -1 so won't close incorrect |
| 636 | * fd (fd=0 is stdin) when failure (zclose won't close |
| 637 | * negative fd)). |
| 638 | */ |
| 639 | for (i = 0; i < nr_maps; i++) |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 640 | obj->maps[i].fd = -1; |
| 641 | |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 642 | /* |
| 643 | * Fill obj->maps using data in "maps" section. |
| 644 | */ |
| 645 | for (i = 0, map_idx = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) { |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 646 | GElf_Sym sym; |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 647 | const char *map_name; |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 648 | struct bpf_map_def *def; |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 649 | |
| 650 | if (!gelf_getsym(symbols, i, &sym)) |
| 651 | continue; |
Wang Nan | 666810e | 2016-01-25 09:55:49 +0000 | [diff] [blame] | 652 | if (sym.st_shndx != obj->efile.maps_shndx) |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 653 | continue; |
| 654 | |
| 655 | map_name = elf_strptr(obj->efile.elf, |
Wang Nan | 77ba9a5 | 2015-12-08 02:25:30 +0000 | [diff] [blame] | 656 | obj->efile.strtabidx, |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 657 | sym.st_name); |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 658 | obj->maps[map_idx].offset = sym.st_value; |
Craig Gallek | b13c5c1 | 2017-10-05 10:41:57 -0400 | [diff] [blame] | 659 | if (sym.st_value + map_def_sz > data->d_size) { |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 660 | pr_warning("corrupted maps section in %s: last map \"%s\" too small\n", |
| 661 | obj->path, map_name); |
| 662 | return -EINVAL; |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 663 | } |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 664 | |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 665 | obj->maps[map_idx].name = strdup(map_name); |
Wang Nan | 973170e | 2015-12-08 02:25:29 +0000 | [diff] [blame] | 666 | if (!obj->maps[map_idx].name) { |
| 667 | pr_warning("failed to alloc map name\n"); |
| 668 | return -ENOMEM; |
| 669 | } |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 670 | pr_debug("map %d is \"%s\"\n", map_idx, |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 671 | obj->maps[map_idx].name); |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 672 | def = (struct bpf_map_def *)(data->d_buf + sym.st_value); |
Craig Gallek | b13c5c1 | 2017-10-05 10:41:57 -0400 | [diff] [blame] | 673 | /* |
| 674 | * If the definition of the map in the object file fits in |
| 675 | * bpf_map_def, copy it. Any extra fields in our version |
| 676 | * of bpf_map_def will default to zero as a result of the |
| 677 | * calloc above. |
| 678 | */ |
| 679 | if (map_def_sz <= sizeof(struct bpf_map_def)) { |
| 680 | memcpy(&obj->maps[map_idx].def, def, map_def_sz); |
| 681 | } else { |
| 682 | /* |
| 683 | * Here the map structure being read is bigger than what |
| 684 | * we expect, truncate if the excess bits are all zero. |
| 685 | * If they are not zero, reject this map as |
| 686 | * incompatible. |
| 687 | */ |
| 688 | char *b; |
| 689 | for (b = ((char *)def) + sizeof(struct bpf_map_def); |
| 690 | b < ((char *)def) + map_def_sz; b++) { |
| 691 | if (*b != 0) { |
| 692 | pr_warning("maps section in %s: \"%s\" " |
| 693 | "has unrecognized, non-zero " |
| 694 | "options\n", |
| 695 | obj->path, map_name); |
| 696 | return -EINVAL; |
| 697 | } |
| 698 | } |
| 699 | memcpy(&obj->maps[map_idx].def, def, |
| 700 | sizeof(struct bpf_map_def)); |
| 701 | } |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 702 | map_idx++; |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 703 | } |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 704 | |
| 705 | qsort(obj->maps, obj->nr_maps, sizeof(obj->maps[0]), compare_bpf_map); |
Craig Gallek | b13c5c1 | 2017-10-05 10:41:57 -0400 | [diff] [blame] | 706 | return 0; |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Jesper Dangaard Brouer | e3d91b0 | 2018-02-08 12:48:32 +0100 | [diff] [blame] | 709 | static bool section_have_execinstr(struct bpf_object *obj, int idx) |
| 710 | { |
| 711 | Elf_Scn *scn; |
| 712 | GElf_Shdr sh; |
| 713 | |
| 714 | scn = elf_getscn(obj->efile.elf, idx); |
| 715 | if (!scn) |
| 716 | return false; |
| 717 | |
| 718 | if (gelf_getshdr(scn, &sh) != &sh) |
| 719 | return false; |
| 720 | |
| 721 | if (sh.sh_flags & SHF_EXECINSTR) |
| 722 | return true; |
| 723 | |
| 724 | return false; |
| 725 | } |
| 726 | |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 727 | static int bpf_object__elf_collect(struct bpf_object *obj) |
| 728 | { |
| 729 | Elf *elf = obj->efile.elf; |
| 730 | GElf_Ehdr *ep = &obj->efile.ehdr; |
| 731 | Elf_Scn *scn = NULL; |
Wang Nan | 666810e | 2016-01-25 09:55:49 +0000 | [diff] [blame] | 732 | int idx = 0, err = 0; |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 733 | |
| 734 | /* Elf is corrupted/truncated, avoid calling elf_strptr. */ |
| 735 | if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) { |
| 736 | pr_warning("failed to get e_shstrndx from %s\n", |
| 737 | obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 738 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | while ((scn = elf_nextscn(elf, scn)) != NULL) { |
| 742 | char *name; |
| 743 | GElf_Shdr sh; |
| 744 | Elf_Data *data; |
| 745 | |
| 746 | idx++; |
| 747 | if (gelf_getshdr(scn, &sh) != &sh) { |
Jesper Dangaard Brouer | 077c066 | 2018-02-08 12:48:17 +0100 | [diff] [blame] | 748 | pr_warning("failed to get section(%d) header from %s\n", |
| 749 | idx, obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 750 | err = -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 751 | goto out; |
| 752 | } |
| 753 | |
| 754 | name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name); |
| 755 | if (!name) { |
Jesper Dangaard Brouer | 077c066 | 2018-02-08 12:48:17 +0100 | [diff] [blame] | 756 | pr_warning("failed to get section(%d) name from %s\n", |
| 757 | idx, obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 758 | err = -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 759 | goto out; |
| 760 | } |
| 761 | |
| 762 | data = elf_getdata(scn, 0); |
| 763 | if (!data) { |
Jesper Dangaard Brouer | 077c066 | 2018-02-08 12:48:17 +0100 | [diff] [blame] | 764 | pr_warning("failed to get section(%d) data from %s(%s)\n", |
| 765 | idx, name, obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 766 | err = -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 767 | goto out; |
| 768 | } |
Jesper Dangaard Brouer | 077c066 | 2018-02-08 12:48:17 +0100 | [diff] [blame] | 769 | pr_debug("section(%d) %s, size %ld, link %d, flags %lx, type=%d\n", |
| 770 | idx, name, (unsigned long)data->d_size, |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 771 | (int)sh.sh_link, (unsigned long)sh.sh_flags, |
| 772 | (int)sh.sh_type); |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 773 | |
| 774 | if (strcmp(name, "license") == 0) |
| 775 | err = bpf_object__init_license(obj, |
| 776 | data->d_buf, |
| 777 | data->d_size); |
| 778 | else if (strcmp(name, "version") == 0) |
| 779 | err = bpf_object__init_kversion(obj, |
| 780 | data->d_buf, |
| 781 | data->d_size); |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 782 | else if (strcmp(name, "maps") == 0) |
Wang Nan | 666810e | 2016-01-25 09:55:49 +0000 | [diff] [blame] | 783 | obj->efile.maps_shndx = idx; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 784 | else if (strcmp(name, BTF_ELF_SEC) == 0) { |
| 785 | obj->btf = btf__new(data->d_buf, data->d_size, |
| 786 | __pr_debug); |
| 787 | if (IS_ERR(obj->btf)) { |
| 788 | pr_warning("Error loading ELF section %s: %ld. Ignored and continue.\n", |
| 789 | BTF_ELF_SEC, PTR_ERR(obj->btf)); |
| 790 | obj->btf = NULL; |
| 791 | } |
| 792 | } else if (sh.sh_type == SHT_SYMTAB) { |
Wang Nan | bec7d68 | 2015-07-01 02:13:59 +0000 | [diff] [blame] | 793 | if (obj->efile.symbols) { |
| 794 | pr_warning("bpf: multiple SYMTAB in %s\n", |
| 795 | obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 796 | err = -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 77ba9a5 | 2015-12-08 02:25:30 +0000 | [diff] [blame] | 797 | } else { |
Wang Nan | bec7d68 | 2015-07-01 02:13:59 +0000 | [diff] [blame] | 798 | obj->efile.symbols = data; |
Wang Nan | 77ba9a5 | 2015-12-08 02:25:30 +0000 | [diff] [blame] | 799 | obj->efile.strtabidx = sh.sh_link; |
| 800 | } |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 801 | } else if ((sh.sh_type == SHT_PROGBITS) && |
| 802 | (sh.sh_flags & SHF_EXECINSTR) && |
| 803 | (data->d_size > 0)) { |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 804 | if (strcmp(name, ".text") == 0) |
| 805 | obj->efile.text_shndx = idx; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 806 | err = bpf_object__add_program(obj, data->d_buf, |
| 807 | data->d_size, name, idx); |
| 808 | if (err) { |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 809 | char errmsg[STRERR_BUFSIZE]; |
| 810 | |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 811 | strerror_r(-err, errmsg, sizeof(errmsg)); |
| 812 | pr_warning("failed to alloc program %s (%s): %s", |
| 813 | name, obj->path, errmsg); |
| 814 | } |
Wang Nan | b62f06e | 2015-07-01 02:14:01 +0000 | [diff] [blame] | 815 | } else if (sh.sh_type == SHT_REL) { |
| 816 | void *reloc = obj->efile.reloc; |
| 817 | int nr_reloc = obj->efile.nr_reloc + 1; |
Jesper Dangaard Brouer | e3d91b0 | 2018-02-08 12:48:32 +0100 | [diff] [blame] | 818 | int sec = sh.sh_info; /* points to other section */ |
| 819 | |
| 820 | /* Only do relo for section with exec instructions */ |
| 821 | if (!section_have_execinstr(obj, sec)) { |
| 822 | pr_debug("skip relo %s(%d) for section(%d)\n", |
| 823 | name, idx, sec); |
| 824 | continue; |
| 825 | } |
Wang Nan | b62f06e | 2015-07-01 02:14:01 +0000 | [diff] [blame] | 826 | |
Jakub Kicinski | 531b014 | 2018-07-10 14:43:05 -0700 | [diff] [blame] | 827 | reloc = reallocarray(reloc, nr_reloc, |
| 828 | sizeof(*obj->efile.reloc)); |
Wang Nan | b62f06e | 2015-07-01 02:14:01 +0000 | [diff] [blame] | 829 | if (!reloc) { |
| 830 | pr_warning("realloc failed\n"); |
| 831 | err = -ENOMEM; |
| 832 | } else { |
| 833 | int n = nr_reloc - 1; |
| 834 | |
| 835 | obj->efile.reloc = reloc; |
| 836 | obj->efile.nr_reloc = nr_reloc; |
| 837 | |
| 838 | obj->efile.reloc[n].shdr = sh; |
| 839 | obj->efile.reloc[n].data = data; |
| 840 | } |
Jesper Dangaard Brouer | 077c066 | 2018-02-08 12:48:17 +0100 | [diff] [blame] | 841 | } else { |
| 842 | pr_debug("skip section(%d) %s\n", idx, name); |
Wang Nan | bec7d68 | 2015-07-01 02:13:59 +0000 | [diff] [blame] | 843 | } |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 844 | if (err) |
| 845 | goto out; |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 846 | } |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 847 | |
Wang Nan | 77ba9a5 | 2015-12-08 02:25:30 +0000 | [diff] [blame] | 848 | if (!obj->efile.strtabidx || obj->efile.strtabidx >= idx) { |
| 849 | pr_warning("Corrupted ELF file: index of strtab invalid\n"); |
| 850 | return LIBBPF_ERRNO__FORMAT; |
| 851 | } |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 852 | if (obj->efile.maps_shndx >= 0) { |
Eric Leblond | 4708bbd | 2016-11-15 04:05:47 +0000 | [diff] [blame] | 853 | err = bpf_object__init_maps(obj); |
Martin KaFai Lau | 88cda1c | 2017-09-27 14:37:54 -0700 | [diff] [blame] | 854 | if (err) |
| 855 | goto out; |
| 856 | } |
| 857 | err = bpf_object__init_prog_names(obj); |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 858 | out: |
| 859 | return err; |
| 860 | } |
| 861 | |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 862 | static struct bpf_program * |
| 863 | bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx) |
| 864 | { |
| 865 | struct bpf_program *prog; |
| 866 | size_t i; |
| 867 | |
| 868 | for (i = 0; i < obj->nr_programs; i++) { |
| 869 | prog = &obj->programs[i]; |
| 870 | if (prog->idx == idx) |
| 871 | return prog; |
| 872 | } |
| 873 | return NULL; |
| 874 | } |
| 875 | |
Jakub Kicinski | 6d4b198 | 2018-07-26 14:32:19 -0700 | [diff] [blame^] | 876 | struct bpf_program * |
| 877 | bpf_object__find_program_by_title(struct bpf_object *obj, const char *title) |
| 878 | { |
| 879 | struct bpf_program *pos; |
| 880 | |
| 881 | bpf_object__for_each_program(pos, obj) { |
| 882 | if (pos->section_name && !strcmp(pos->section_name, title)) |
| 883 | return pos; |
| 884 | } |
| 885 | return NULL; |
| 886 | } |
| 887 | |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 888 | static int |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 889 | bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr, |
| 890 | Elf_Data *data, struct bpf_object *obj) |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 891 | { |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 892 | Elf_Data *symbols = obj->efile.symbols; |
| 893 | int text_shndx = obj->efile.text_shndx; |
| 894 | int maps_shndx = obj->efile.maps_shndx; |
| 895 | struct bpf_map *maps = obj->maps; |
| 896 | size_t nr_maps = obj->nr_maps; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 897 | int i, nrels; |
| 898 | |
| 899 | pr_debug("collecting relocating info for: '%s'\n", |
| 900 | prog->section_name); |
| 901 | nrels = shdr->sh_size / shdr->sh_entsize; |
| 902 | |
| 903 | prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels); |
| 904 | if (!prog->reloc_desc) { |
| 905 | pr_warning("failed to alloc memory in relocation\n"); |
| 906 | return -ENOMEM; |
| 907 | } |
| 908 | prog->nr_reloc = nrels; |
| 909 | |
| 910 | for (i = 0; i < nrels; i++) { |
| 911 | GElf_Sym sym; |
| 912 | GElf_Rel rel; |
| 913 | unsigned int insn_idx; |
| 914 | struct bpf_insn *insns = prog->insns; |
| 915 | size_t map_idx; |
| 916 | |
| 917 | if (!gelf_getrel(data, i, &rel)) { |
| 918 | pr_warning("relocation: failed to get %d reloc\n", i); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 919 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 922 | if (!gelf_getsym(symbols, |
| 923 | GELF_R_SYM(rel.r_info), |
| 924 | &sym)) { |
| 925 | pr_warning("relocation: symbol %"PRIx64" not found\n", |
| 926 | GELF_R_SYM(rel.r_info)); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 927 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 928 | } |
David Miller | 7d9890e | 2017-12-19 15:53:11 -0500 | [diff] [blame] | 929 | pr_debug("relo for %lld value %lld name %d\n", |
| 930 | (long long) (rel.r_info >> 32), |
| 931 | (long long) sym.st_value, sym.st_name); |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 932 | |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 933 | if (sym.st_shndx != maps_shndx && sym.st_shndx != text_shndx) { |
Wang Nan | 666810e | 2016-01-25 09:55:49 +0000 | [diff] [blame] | 934 | pr_warning("Program '%s' contains non-map related relo data pointing to section %u\n", |
| 935 | prog->section_name, sym.st_shndx); |
| 936 | return -LIBBPF_ERRNO__RELOC; |
| 937 | } |
| 938 | |
| 939 | insn_idx = rel.r_offset / sizeof(struct bpf_insn); |
| 940 | pr_debug("relocation: insn_idx=%u\n", insn_idx); |
| 941 | |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 942 | if (insns[insn_idx].code == (BPF_JMP | BPF_CALL)) { |
| 943 | if (insns[insn_idx].src_reg != BPF_PSEUDO_CALL) { |
| 944 | pr_warning("incorrect bpf_call opcode\n"); |
| 945 | return -LIBBPF_ERRNO__RELOC; |
| 946 | } |
| 947 | prog->reloc_desc[i].type = RELO_CALL; |
| 948 | prog->reloc_desc[i].insn_idx = insn_idx; |
| 949 | prog->reloc_desc[i].text_off = sym.st_value; |
Jakub Kicinski | 9a94f27 | 2018-06-28 14:41:38 -0700 | [diff] [blame] | 950 | obj->has_pseudo_calls = true; |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 951 | continue; |
| 952 | } |
| 953 | |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 954 | if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) { |
| 955 | pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n", |
| 956 | insn_idx, insns[insn_idx].code); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 957 | return -LIBBPF_ERRNO__RELOC; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Joe Stringer | 94e5ade | 2017-01-22 17:11:22 -0800 | [diff] [blame] | 960 | /* TODO: 'maps' is sorted. We can use bsearch to make it faster. */ |
| 961 | for (map_idx = 0; map_idx < nr_maps; map_idx++) { |
| 962 | if (maps[map_idx].offset == sym.st_value) { |
| 963 | pr_debug("relocation: find map %zd (%s) for insn %u\n", |
| 964 | map_idx, maps[map_idx].name, insn_idx); |
| 965 | break; |
| 966 | } |
| 967 | } |
| 968 | |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 969 | if (map_idx >= nr_maps) { |
| 970 | pr_warning("bpf relocation: map_idx %d large than %d\n", |
| 971 | (int)map_idx, (int)nr_maps - 1); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 972 | return -LIBBPF_ERRNO__RELOC; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 973 | } |
| 974 | |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 975 | prog->reloc_desc[i].type = RELO_LD64; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 976 | prog->reloc_desc[i].insn_idx = insn_idx; |
| 977 | prog->reloc_desc[i].map_idx = map_idx; |
| 978 | } |
| 979 | return 0; |
| 980 | } |
| 981 | |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 982 | static int bpf_map_find_btf_info(struct bpf_map *map, const struct btf *btf) |
| 983 | { |
| 984 | struct bpf_map_def *def = &map->def; |
| 985 | const size_t max_name = 256; |
| 986 | int64_t key_size, value_size; |
| 987 | int32_t key_id, value_id; |
| 988 | char name[max_name]; |
| 989 | |
| 990 | /* Find key type by name from BTF */ |
| 991 | if (snprintf(name, max_name, "%s_key", map->name) == max_name) { |
| 992 | pr_warning("map:%s length of BTF key_type:%s_key is too long\n", |
| 993 | map->name, map->name); |
| 994 | return -EINVAL; |
| 995 | } |
| 996 | |
| 997 | key_id = btf__find_by_name(btf, name); |
| 998 | if (key_id < 0) { |
| 999 | pr_debug("map:%s key_type:%s cannot be found in BTF\n", |
| 1000 | map->name, name); |
| 1001 | return key_id; |
| 1002 | } |
| 1003 | |
| 1004 | key_size = btf__resolve_size(btf, key_id); |
| 1005 | if (key_size < 0) { |
| 1006 | pr_warning("map:%s key_type:%s cannot get the BTF type_size\n", |
| 1007 | map->name, name); |
| 1008 | return key_size; |
| 1009 | } |
| 1010 | |
| 1011 | if (def->key_size != key_size) { |
Sirio Balmelli | a1c8181 | 2018-05-23 18:17:07 +0200 | [diff] [blame] | 1012 | pr_warning("map:%s key_type:%s has BTF type_size:%u != key_size:%u\n", |
| 1013 | map->name, name, (unsigned int)key_size, def->key_size); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 1014 | return -EINVAL; |
| 1015 | } |
| 1016 | |
| 1017 | /* Find value type from BTF */ |
| 1018 | if (snprintf(name, max_name, "%s_value", map->name) == max_name) { |
| 1019 | pr_warning("map:%s length of BTF value_type:%s_value is too long\n", |
| 1020 | map->name, map->name); |
| 1021 | return -EINVAL; |
| 1022 | } |
| 1023 | |
| 1024 | value_id = btf__find_by_name(btf, name); |
| 1025 | if (value_id < 0) { |
| 1026 | pr_debug("map:%s value_type:%s cannot be found in BTF\n", |
| 1027 | map->name, name); |
| 1028 | return value_id; |
| 1029 | } |
| 1030 | |
| 1031 | value_size = btf__resolve_size(btf, value_id); |
| 1032 | if (value_size < 0) { |
| 1033 | pr_warning("map:%s value_type:%s cannot get the BTF type_size\n", |
| 1034 | map->name, name); |
| 1035 | return value_size; |
| 1036 | } |
| 1037 | |
| 1038 | if (def->value_size != value_size) { |
Sirio Balmelli | a1c8181 | 2018-05-23 18:17:07 +0200 | [diff] [blame] | 1039 | pr_warning("map:%s value_type:%s has BTF type_size:%u != value_size:%u\n", |
| 1040 | map->name, name, (unsigned int)value_size, def->value_size); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 1041 | return -EINVAL; |
| 1042 | } |
| 1043 | |
Martin KaFai Lau | 61746dbe | 2018-05-22 15:04:24 -0700 | [diff] [blame] | 1044 | map->btf_key_type_id = key_id; |
| 1045 | map->btf_value_type_id = value_id; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 1046 | |
| 1047 | return 0; |
| 1048 | } |
| 1049 | |
Jakub Kicinski | 26736eb | 2018-07-10 14:43:06 -0700 | [diff] [blame] | 1050 | int bpf_map__reuse_fd(struct bpf_map *map, int fd) |
| 1051 | { |
| 1052 | struct bpf_map_info info = {}; |
| 1053 | __u32 len = sizeof(info); |
| 1054 | int new_fd, err; |
| 1055 | char *new_name; |
| 1056 | |
| 1057 | err = bpf_obj_get_info_by_fd(fd, &info, &len); |
| 1058 | if (err) |
| 1059 | return err; |
| 1060 | |
| 1061 | new_name = strdup(info.name); |
| 1062 | if (!new_name) |
| 1063 | return -errno; |
| 1064 | |
| 1065 | new_fd = open("/", O_RDONLY | O_CLOEXEC); |
| 1066 | if (new_fd < 0) |
| 1067 | goto err_free_new_name; |
| 1068 | |
| 1069 | new_fd = dup3(fd, new_fd, O_CLOEXEC); |
| 1070 | if (new_fd < 0) |
| 1071 | goto err_close_new_fd; |
| 1072 | |
| 1073 | err = zclose(map->fd); |
| 1074 | if (err) |
| 1075 | goto err_close_new_fd; |
| 1076 | free(map->name); |
| 1077 | |
| 1078 | map->fd = new_fd; |
| 1079 | map->name = new_name; |
| 1080 | map->def.type = info.type; |
| 1081 | map->def.key_size = info.key_size; |
| 1082 | map->def.value_size = info.value_size; |
| 1083 | map->def.max_entries = info.max_entries; |
| 1084 | map->def.map_flags = info.map_flags; |
| 1085 | map->btf_key_type_id = info.btf_key_type_id; |
| 1086 | map->btf_value_type_id = info.btf_value_type_id; |
| 1087 | |
| 1088 | return 0; |
| 1089 | |
| 1090 | err_close_new_fd: |
| 1091 | close(new_fd); |
| 1092 | err_free_new_name: |
| 1093 | free(new_name); |
| 1094 | return -errno; |
| 1095 | } |
| 1096 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1097 | static int |
| 1098 | bpf_object__create_maps(struct bpf_object *obj) |
| 1099 | { |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 1100 | struct bpf_create_map_attr create_attr = {}; |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1101 | unsigned int i; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 1102 | int err; |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1103 | |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 1104 | for (i = 0; i < obj->nr_maps; i++) { |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 1105 | struct bpf_map *map = &obj->maps[i]; |
| 1106 | struct bpf_map_def *def = &map->def; |
| 1107 | int *pfd = &map->fd; |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1108 | |
Jakub Kicinski | 26736eb | 2018-07-10 14:43:06 -0700 | [diff] [blame] | 1109 | if (map->fd >= 0) { |
| 1110 | pr_debug("skip map create (preset) %s: fd=%d\n", |
| 1111 | map->name, map->fd); |
| 1112 | continue; |
| 1113 | } |
| 1114 | |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 1115 | create_attr.name = map->name; |
David Beckett | f0307a7 | 2018-05-16 14:02:49 -0700 | [diff] [blame] | 1116 | create_attr.map_ifindex = map->map_ifindex; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 1117 | create_attr.map_type = def->type; |
| 1118 | create_attr.map_flags = def->map_flags; |
| 1119 | create_attr.key_size = def->key_size; |
| 1120 | create_attr.value_size = def->value_size; |
| 1121 | create_attr.max_entries = def->max_entries; |
| 1122 | create_attr.btf_fd = 0; |
Martin KaFai Lau | 61746dbe | 2018-05-22 15:04:24 -0700 | [diff] [blame] | 1123 | create_attr.btf_key_type_id = 0; |
| 1124 | create_attr.btf_value_type_id = 0; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 1125 | |
| 1126 | if (obj->btf && !bpf_map_find_btf_info(map, obj->btf)) { |
| 1127 | create_attr.btf_fd = btf__fd(obj->btf); |
Martin KaFai Lau | 61746dbe | 2018-05-22 15:04:24 -0700 | [diff] [blame] | 1128 | create_attr.btf_key_type_id = map->btf_key_type_id; |
| 1129 | create_attr.btf_value_type_id = map->btf_value_type_id; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | *pfd = bpf_create_map_xattr(&create_attr); |
Martin KaFai Lau | 61746dbe | 2018-05-22 15:04:24 -0700 | [diff] [blame] | 1133 | if (*pfd < 0 && create_attr.btf_key_type_id) { |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 1134 | pr_warning("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n", |
| 1135 | map->name, strerror(errno), errno); |
| 1136 | create_attr.btf_fd = 0; |
Martin KaFai Lau | 61746dbe | 2018-05-22 15:04:24 -0700 | [diff] [blame] | 1137 | create_attr.btf_key_type_id = 0; |
| 1138 | create_attr.btf_value_type_id = 0; |
| 1139 | map->btf_key_type_id = 0; |
| 1140 | map->btf_value_type_id = 0; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 1141 | *pfd = bpf_create_map_xattr(&create_attr); |
| 1142 | } |
| 1143 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1144 | if (*pfd < 0) { |
| 1145 | size_t j; |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1146 | |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 1147 | err = *pfd; |
Eric Leblond | 49bf4b3 | 2017-08-20 21:48:14 +0200 | [diff] [blame] | 1148 | pr_warning("failed to create map (name: '%s'): %s\n", |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 1149 | map->name, |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1150 | strerror(errno)); |
| 1151 | for (j = 0; j < i; j++) |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 1152 | zclose(obj->maps[j].fd); |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1153 | return err; |
| 1154 | } |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 1155 | pr_debug("create map %s: fd=%d\n", map->name, *pfd); |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1158 | return 0; |
| 1159 | } |
| 1160 | |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 1161 | static int |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 1162 | bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj, |
| 1163 | struct reloc_desc *relo) |
| 1164 | { |
| 1165 | struct bpf_insn *insn, *new_insn; |
| 1166 | struct bpf_program *text; |
| 1167 | size_t new_cnt; |
| 1168 | |
| 1169 | if (relo->type != RELO_CALL) |
| 1170 | return -LIBBPF_ERRNO__RELOC; |
| 1171 | |
| 1172 | if (prog->idx == obj->efile.text_shndx) { |
| 1173 | pr_warning("relo in .text insn %d into off %d\n", |
| 1174 | relo->insn_idx, relo->text_off); |
| 1175 | return -LIBBPF_ERRNO__RELOC; |
| 1176 | } |
| 1177 | |
| 1178 | if (prog->main_prog_cnt == 0) { |
| 1179 | text = bpf_object__find_prog_by_idx(obj, obj->efile.text_shndx); |
| 1180 | if (!text) { |
| 1181 | pr_warning("no .text section found yet relo into text exist\n"); |
| 1182 | return -LIBBPF_ERRNO__RELOC; |
| 1183 | } |
| 1184 | new_cnt = prog->insns_cnt + text->insns_cnt; |
Jakub Kicinski | 531b014 | 2018-07-10 14:43:05 -0700 | [diff] [blame] | 1185 | new_insn = reallocarray(prog->insns, new_cnt, sizeof(*insn)); |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 1186 | if (!new_insn) { |
| 1187 | pr_warning("oom in prog realloc\n"); |
| 1188 | return -ENOMEM; |
| 1189 | } |
| 1190 | memcpy(new_insn + prog->insns_cnt, text->insns, |
| 1191 | text->insns_cnt * sizeof(*insn)); |
| 1192 | prog->insns = new_insn; |
| 1193 | prog->main_prog_cnt = prog->insns_cnt; |
| 1194 | prog->insns_cnt = new_cnt; |
Jeremy Cline | b1a2ce8 | 2018-02-20 01:00:07 +0000 | [diff] [blame] | 1195 | pr_debug("added %zd insn from %s to prog %s\n", |
| 1196 | text->insns_cnt, text->section_name, |
| 1197 | prog->section_name); |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 1198 | } |
| 1199 | insn = &prog->insns[relo->insn_idx]; |
| 1200 | insn->imm += prog->main_prog_cnt - relo->insn_idx; |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 1201 | return 0; |
| 1202 | } |
| 1203 | |
| 1204 | static int |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 1205 | bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj) |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 1206 | { |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 1207 | int i, err; |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 1208 | |
| 1209 | if (!prog || !prog->reloc_desc) |
| 1210 | return 0; |
| 1211 | |
| 1212 | for (i = 0; i < prog->nr_reloc; i++) { |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 1213 | if (prog->reloc_desc[i].type == RELO_LD64) { |
| 1214 | struct bpf_insn *insns = prog->insns; |
| 1215 | int insn_idx, map_idx; |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 1216 | |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 1217 | insn_idx = prog->reloc_desc[i].insn_idx; |
| 1218 | map_idx = prog->reloc_desc[i].map_idx; |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 1219 | |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 1220 | if (insn_idx >= (int)prog->insns_cnt) { |
| 1221 | pr_warning("relocation out of range: '%s'\n", |
| 1222 | prog->section_name); |
| 1223 | return -LIBBPF_ERRNO__RELOC; |
| 1224 | } |
| 1225 | insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD; |
| 1226 | insns[insn_idx].imm = obj->maps[map_idx].fd; |
| 1227 | } else { |
| 1228 | err = bpf_program__reloc_text(prog, obj, |
| 1229 | &prog->reloc_desc[i]); |
| 1230 | if (err) |
| 1231 | return err; |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 1232 | } |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | zfree(&prog->reloc_desc); |
| 1236 | prog->nr_reloc = 0; |
| 1237 | return 0; |
| 1238 | } |
| 1239 | |
| 1240 | |
| 1241 | static int |
| 1242 | bpf_object__relocate(struct bpf_object *obj) |
| 1243 | { |
| 1244 | struct bpf_program *prog; |
| 1245 | size_t i; |
| 1246 | int err; |
| 1247 | |
| 1248 | for (i = 0; i < obj->nr_programs; i++) { |
| 1249 | prog = &obj->programs[i]; |
| 1250 | |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 1251 | err = bpf_program__relocate(prog, obj); |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 1252 | if (err) { |
| 1253 | pr_warning("failed to relocate '%s'\n", |
| 1254 | prog->section_name); |
| 1255 | return err; |
| 1256 | } |
| 1257 | } |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 1261 | static int bpf_object__collect_reloc(struct bpf_object *obj) |
| 1262 | { |
| 1263 | int i, err; |
| 1264 | |
| 1265 | if (!obj_elf_valid(obj)) { |
| 1266 | pr_warning("Internal error: elf object is closed\n"); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1267 | return -LIBBPF_ERRNO__INTERNAL; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | for (i = 0; i < obj->efile.nr_reloc; i++) { |
| 1271 | GElf_Shdr *shdr = &obj->efile.reloc[i].shdr; |
| 1272 | Elf_Data *data = obj->efile.reloc[i].data; |
| 1273 | int idx = shdr->sh_info; |
| 1274 | struct bpf_program *prog; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 1275 | |
| 1276 | if (shdr->sh_type != SHT_REL) { |
| 1277 | pr_warning("internal error at %d\n", __LINE__); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1278 | return -LIBBPF_ERRNO__INTERNAL; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | prog = bpf_object__find_prog_by_idx(obj, idx); |
| 1282 | if (!prog) { |
Jesper Dangaard Brouer | 077c066 | 2018-02-08 12:48:17 +0100 | [diff] [blame] | 1283 | pr_warning("relocation failed: no section(%d)\n", idx); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1284 | return -LIBBPF_ERRNO__RELOC; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 1287 | err = bpf_program__collect_reloc(prog, |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 1288 | shdr, data, |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 1289 | obj); |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 1290 | if (err) |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1291 | return err; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 1292 | } |
| 1293 | return 0; |
| 1294 | } |
| 1295 | |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 1296 | static int |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 1297 | load_program(enum bpf_prog_type type, enum bpf_attach_type expected_attach_type, |
| 1298 | const char *name, struct bpf_insn *insns, int insns_cnt, |
David Beckett | f0307a7 | 2018-05-16 14:02:49 -0700 | [diff] [blame] | 1299 | char *license, u32 kern_version, int *pfd, int prog_ifindex) |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 1300 | { |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 1301 | struct bpf_load_program_attr load_attr; |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 1302 | char *log_buf; |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 1303 | int ret; |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 1304 | |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 1305 | memset(&load_attr, 0, sizeof(struct bpf_load_program_attr)); |
| 1306 | load_attr.prog_type = type; |
| 1307 | load_attr.expected_attach_type = expected_attach_type; |
| 1308 | load_attr.name = name; |
| 1309 | load_attr.insns = insns; |
| 1310 | load_attr.insns_cnt = insns_cnt; |
| 1311 | load_attr.license = license; |
| 1312 | load_attr.kern_version = kern_version; |
David Beckett | f0307a7 | 2018-05-16 14:02:49 -0700 | [diff] [blame] | 1313 | load_attr.prog_ifindex = prog_ifindex; |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 1314 | |
| 1315 | if (!load_attr.insns || !load_attr.insns_cnt) |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 1316 | return -EINVAL; |
| 1317 | |
| 1318 | log_buf = malloc(BPF_LOG_BUF_SIZE); |
| 1319 | if (!log_buf) |
| 1320 | pr_warning("Alloc log buffer for bpf loader error, continue without log\n"); |
| 1321 | |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 1322 | ret = bpf_load_program_xattr(&load_attr, log_buf, BPF_LOG_BUF_SIZE); |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 1323 | |
| 1324 | if (ret >= 0) { |
| 1325 | *pfd = ret; |
| 1326 | ret = 0; |
| 1327 | goto out; |
| 1328 | } |
| 1329 | |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1330 | ret = -LIBBPF_ERRNO__LOAD; |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 1331 | pr_warning("load bpf program failed: %s\n", strerror(errno)); |
| 1332 | |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1333 | if (log_buf && log_buf[0] != '\0') { |
| 1334 | ret = -LIBBPF_ERRNO__VERIFY; |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 1335 | pr_warning("-- BEGIN DUMP LOG ---\n"); |
| 1336 | pr_warning("\n%s\n", log_buf); |
| 1337 | pr_warning("-- END LOG --\n"); |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 1338 | } else if (load_attr.insns_cnt >= BPF_MAXINSNS) { |
| 1339 | pr_warning("Program too large (%zu insns), at most %d insns\n", |
| 1340 | load_attr.insns_cnt, BPF_MAXINSNS); |
Wang Nan | 705fa21 | 2016-07-13 10:44:02 +0000 | [diff] [blame] | 1341 | ret = -LIBBPF_ERRNO__PROG2BIG; |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1342 | } else { |
Wang Nan | 705fa21 | 2016-07-13 10:44:02 +0000 | [diff] [blame] | 1343 | /* Wrong program type? */ |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 1344 | if (load_attr.prog_type != BPF_PROG_TYPE_KPROBE) { |
Wang Nan | 705fa21 | 2016-07-13 10:44:02 +0000 | [diff] [blame] | 1345 | int fd; |
| 1346 | |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 1347 | load_attr.prog_type = BPF_PROG_TYPE_KPROBE; |
| 1348 | load_attr.expected_attach_type = 0; |
| 1349 | fd = bpf_load_program_xattr(&load_attr, NULL, 0); |
Wang Nan | 705fa21 | 2016-07-13 10:44:02 +0000 | [diff] [blame] | 1350 | if (fd >= 0) { |
| 1351 | close(fd); |
| 1352 | ret = -LIBBPF_ERRNO__PROGTYPE; |
| 1353 | goto out; |
| 1354 | } |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1355 | } |
Wang Nan | 705fa21 | 2016-07-13 10:44:02 +0000 | [diff] [blame] | 1356 | |
| 1357 | if (log_buf) |
| 1358 | ret = -LIBBPF_ERRNO__KVER; |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | out: |
| 1362 | free(log_buf); |
| 1363 | return ret; |
| 1364 | } |
| 1365 | |
| 1366 | static int |
| 1367 | bpf_program__load(struct bpf_program *prog, |
| 1368 | char *license, u32 kern_version) |
| 1369 | { |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 1370 | int err = 0, fd, i; |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 1371 | |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 1372 | if (prog->instances.nr < 0 || !prog->instances.fds) { |
| 1373 | if (prog->preprocessor) { |
| 1374 | pr_warning("Internal error: can't load program '%s'\n", |
| 1375 | prog->section_name); |
| 1376 | return -LIBBPF_ERRNO__INTERNAL; |
| 1377 | } |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 1378 | |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 1379 | prog->instances.fds = malloc(sizeof(int)); |
| 1380 | if (!prog->instances.fds) { |
| 1381 | pr_warning("Not enough memory for BPF fds\n"); |
| 1382 | return -ENOMEM; |
| 1383 | } |
| 1384 | prog->instances.nr = 1; |
| 1385 | prog->instances.fds[0] = -1; |
| 1386 | } |
| 1387 | |
| 1388 | if (!prog->preprocessor) { |
| 1389 | if (prog->instances.nr != 1) { |
| 1390 | pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n", |
| 1391 | prog->section_name, prog->instances.nr); |
| 1392 | } |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 1393 | err = load_program(prog->type, prog->expected_attach_type, |
| 1394 | prog->name, prog->insns, prog->insns_cnt, |
David Beckett | f0307a7 | 2018-05-16 14:02:49 -0700 | [diff] [blame] | 1395 | license, kern_version, &fd, |
| 1396 | prog->prog_ifindex); |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 1397 | if (!err) |
| 1398 | prog->instances.fds[0] = fd; |
| 1399 | goto out; |
| 1400 | } |
| 1401 | |
| 1402 | for (i = 0; i < prog->instances.nr; i++) { |
| 1403 | struct bpf_prog_prep_result result; |
| 1404 | bpf_program_prep_t preprocessor = prog->preprocessor; |
| 1405 | |
| 1406 | bzero(&result, sizeof(result)); |
| 1407 | err = preprocessor(prog, i, prog->insns, |
| 1408 | prog->insns_cnt, &result); |
| 1409 | if (err) { |
| 1410 | pr_warning("Preprocessing the %dth instance of program '%s' failed\n", |
| 1411 | i, prog->section_name); |
| 1412 | goto out; |
| 1413 | } |
| 1414 | |
| 1415 | if (!result.new_insn_ptr || !result.new_insn_cnt) { |
| 1416 | pr_debug("Skip loading the %dth instance of program '%s'\n", |
| 1417 | i, prog->section_name); |
| 1418 | prog->instances.fds[i] = -1; |
| 1419 | if (result.pfd) |
| 1420 | *result.pfd = -1; |
| 1421 | continue; |
| 1422 | } |
| 1423 | |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 1424 | err = load_program(prog->type, prog->expected_attach_type, |
| 1425 | prog->name, result.new_insn_ptr, |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 1426 | result.new_insn_cnt, |
David Beckett | f0307a7 | 2018-05-16 14:02:49 -0700 | [diff] [blame] | 1427 | license, kern_version, &fd, |
| 1428 | prog->prog_ifindex); |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 1429 | |
| 1430 | if (err) { |
| 1431 | pr_warning("Loading the %dth instance of program '%s' failed\n", |
| 1432 | i, prog->section_name); |
| 1433 | goto out; |
| 1434 | } |
| 1435 | |
| 1436 | if (result.pfd) |
| 1437 | *result.pfd = fd; |
| 1438 | prog->instances.fds[i] = fd; |
| 1439 | } |
| 1440 | out: |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 1441 | if (err) |
| 1442 | pr_warning("failed to load program '%s'\n", |
| 1443 | prog->section_name); |
| 1444 | zfree(&prog->insns); |
| 1445 | prog->insns_cnt = 0; |
| 1446 | return err; |
| 1447 | } |
| 1448 | |
Jakub Kicinski | 9a94f27 | 2018-06-28 14:41:38 -0700 | [diff] [blame] | 1449 | static bool bpf_program__is_function_storage(struct bpf_program *prog, |
| 1450 | struct bpf_object *obj) |
| 1451 | { |
| 1452 | return prog->idx == obj->efile.text_shndx && obj->has_pseudo_calls; |
| 1453 | } |
| 1454 | |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 1455 | static int |
| 1456 | bpf_object__load_progs(struct bpf_object *obj) |
| 1457 | { |
| 1458 | size_t i; |
| 1459 | int err; |
| 1460 | |
| 1461 | for (i = 0; i < obj->nr_programs; i++) { |
Jakub Kicinski | 9a94f27 | 2018-06-28 14:41:38 -0700 | [diff] [blame] | 1462 | if (bpf_program__is_function_storage(&obj->programs[i], obj)) |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 1463 | continue; |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 1464 | err = bpf_program__load(&obj->programs[i], |
| 1465 | obj->license, |
| 1466 | obj->kern_version); |
| 1467 | if (err) |
| 1468 | return err; |
| 1469 | } |
| 1470 | return 0; |
| 1471 | } |
| 1472 | |
Jakub Kicinski | 17387dd | 2018-05-10 10:24:42 -0700 | [diff] [blame] | 1473 | static bool bpf_prog_type__needs_kver(enum bpf_prog_type type) |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 1474 | { |
Jakub Kicinski | 17387dd | 2018-05-10 10:24:42 -0700 | [diff] [blame] | 1475 | switch (type) { |
| 1476 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 1477 | case BPF_PROG_TYPE_SCHED_CLS: |
| 1478 | case BPF_PROG_TYPE_SCHED_ACT: |
| 1479 | case BPF_PROG_TYPE_XDP: |
| 1480 | case BPF_PROG_TYPE_CGROUP_SKB: |
| 1481 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 1482 | case BPF_PROG_TYPE_LWT_IN: |
| 1483 | case BPF_PROG_TYPE_LWT_OUT: |
| 1484 | case BPF_PROG_TYPE_LWT_XMIT: |
Mathieu Xhonneux | 004d4b2 | 2018-05-20 14:58:16 +0100 | [diff] [blame] | 1485 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
Jakub Kicinski | 17387dd | 2018-05-10 10:24:42 -0700 | [diff] [blame] | 1486 | case BPF_PROG_TYPE_SOCK_OPS: |
| 1487 | case BPF_PROG_TYPE_SK_SKB: |
| 1488 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
| 1489 | case BPF_PROG_TYPE_SK_MSG: |
| 1490 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
Sean Young | 6bdd533 | 2018-05-27 12:24:10 +0100 | [diff] [blame] | 1491 | case BPF_PROG_TYPE_LIRC_MODE2: |
Jakub Kicinski | 17387dd | 2018-05-10 10:24:42 -0700 | [diff] [blame] | 1492 | return false; |
| 1493 | case BPF_PROG_TYPE_UNSPEC: |
| 1494 | case BPF_PROG_TYPE_KPROBE: |
| 1495 | case BPF_PROG_TYPE_TRACEPOINT: |
| 1496 | case BPF_PROG_TYPE_PERF_EVENT: |
| 1497 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
| 1498 | default: |
| 1499 | return true; |
| 1500 | } |
| 1501 | } |
| 1502 | |
| 1503 | static int bpf_object__validate(struct bpf_object *obj, bool needs_kver) |
| 1504 | { |
| 1505 | if (needs_kver && obj->kern_version == 0) { |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 1506 | pr_warning("%s doesn't provide kernel version\n", |
| 1507 | obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1508 | return -LIBBPF_ERRNO__KVERSION; |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 1509 | } |
| 1510 | return 0; |
| 1511 | } |
| 1512 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1513 | static struct bpf_object * |
Jakub Kicinski | 17387dd | 2018-05-10 10:24:42 -0700 | [diff] [blame] | 1514 | __bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz, |
| 1515 | bool needs_kver) |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1516 | { |
| 1517 | struct bpf_object *obj; |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1518 | int err; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1519 | |
| 1520 | if (elf_version(EV_CURRENT) == EV_NONE) { |
| 1521 | pr_warning("failed to init libelf for %s\n", path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1522 | return ERR_PTR(-LIBBPF_ERRNO__LIBELF); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 1525 | obj = bpf_object__new(path, obj_buf, obj_buf_sz); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1526 | if (IS_ERR(obj)) |
| 1527 | return obj; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1528 | |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1529 | CHECK_ERR(bpf_object__elf_init(obj), err, out); |
| 1530 | CHECK_ERR(bpf_object__check_endianness(obj), err, out); |
| 1531 | CHECK_ERR(bpf_object__elf_collect(obj), err, out); |
| 1532 | CHECK_ERR(bpf_object__collect_reloc(obj), err, out); |
Jakub Kicinski | 17387dd | 2018-05-10 10:24:42 -0700 | [diff] [blame] | 1533 | CHECK_ERR(bpf_object__validate(obj, needs_kver), err, out); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1534 | |
| 1535 | bpf_object__elf_finish(obj); |
| 1536 | return obj; |
| 1537 | out: |
| 1538 | bpf_object__close(obj); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1539 | return ERR_PTR(err); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
Jakub Kicinski | 07f2d4e | 2018-07-10 14:43:02 -0700 | [diff] [blame] | 1542 | struct bpf_object *bpf_object__open_xattr(struct bpf_object_open_attr *attr) |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1543 | { |
| 1544 | /* param validation */ |
Jakub Kicinski | 07f2d4e | 2018-07-10 14:43:02 -0700 | [diff] [blame] | 1545 | if (!attr->file) |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1546 | return NULL; |
| 1547 | |
Jakub Kicinski | 07f2d4e | 2018-07-10 14:43:02 -0700 | [diff] [blame] | 1548 | pr_debug("loading %s\n", attr->file); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1549 | |
Jakub Kicinski | 07f2d4e | 2018-07-10 14:43:02 -0700 | [diff] [blame] | 1550 | return __bpf_object__open(attr->file, NULL, 0, |
| 1551 | bpf_prog_type__needs_kver(attr->prog_type)); |
| 1552 | } |
| 1553 | |
| 1554 | struct bpf_object *bpf_object__open(const char *path) |
| 1555 | { |
| 1556 | struct bpf_object_open_attr attr = { |
| 1557 | .file = path, |
| 1558 | .prog_type = BPF_PROG_TYPE_UNSPEC, |
| 1559 | }; |
| 1560 | |
| 1561 | return bpf_object__open_xattr(&attr); |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | struct bpf_object *bpf_object__open_buffer(void *obj_buf, |
Wang Nan | acf860a | 2015-08-27 02:30:55 +0000 | [diff] [blame] | 1565 | size_t obj_buf_sz, |
| 1566 | const char *name) |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 1567 | { |
Wang Nan | acf860a | 2015-08-27 02:30:55 +0000 | [diff] [blame] | 1568 | char tmp_name[64]; |
| 1569 | |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 1570 | /* param validation */ |
| 1571 | if (!obj_buf || obj_buf_sz <= 0) |
| 1572 | return NULL; |
| 1573 | |
Wang Nan | acf860a | 2015-08-27 02:30:55 +0000 | [diff] [blame] | 1574 | if (!name) { |
| 1575 | snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx", |
| 1576 | (unsigned long)obj_buf, |
| 1577 | (unsigned long)obj_buf_sz); |
| 1578 | tmp_name[sizeof(tmp_name) - 1] = '\0'; |
| 1579 | name = tmp_name; |
| 1580 | } |
| 1581 | pr_debug("loading object '%s' from buffer\n", |
| 1582 | name); |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 1583 | |
Jakub Kicinski | 17387dd | 2018-05-10 10:24:42 -0700 | [diff] [blame] | 1584 | return __bpf_object__open(name, obj_buf, obj_buf_sz, true); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1585 | } |
| 1586 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1587 | int bpf_object__unload(struct bpf_object *obj) |
| 1588 | { |
| 1589 | size_t i; |
| 1590 | |
| 1591 | if (!obj) |
| 1592 | return -EINVAL; |
| 1593 | |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 1594 | for (i = 0; i < obj->nr_maps; i++) |
| 1595 | zclose(obj->maps[i].fd); |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1596 | |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 1597 | for (i = 0; i < obj->nr_programs; i++) |
| 1598 | bpf_program__unload(&obj->programs[i]); |
| 1599 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1600 | return 0; |
| 1601 | } |
| 1602 | |
| 1603 | int bpf_object__load(struct bpf_object *obj) |
| 1604 | { |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1605 | int err; |
| 1606 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1607 | if (!obj) |
| 1608 | return -EINVAL; |
| 1609 | |
| 1610 | if (obj->loaded) { |
| 1611 | pr_warning("object should not be loaded twice\n"); |
| 1612 | return -EINVAL; |
| 1613 | } |
| 1614 | |
| 1615 | obj->loaded = true; |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1616 | |
| 1617 | CHECK_ERR(bpf_object__create_maps(obj), err, out); |
| 1618 | CHECK_ERR(bpf_object__relocate(obj), err, out); |
| 1619 | CHECK_ERR(bpf_object__load_progs(obj), err, out); |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1620 | |
| 1621 | return 0; |
| 1622 | out: |
| 1623 | bpf_object__unload(obj); |
| 1624 | pr_warning("failed to load object '%s'\n", obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1625 | return err; |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
Joe Stringer | f367540 | 2017-01-26 13:19:56 -0800 | [diff] [blame] | 1628 | static int check_path(const char *path) |
| 1629 | { |
| 1630 | struct statfs st_fs; |
| 1631 | char *dname, *dir; |
| 1632 | int err = 0; |
| 1633 | |
| 1634 | if (path == NULL) |
| 1635 | return -EINVAL; |
| 1636 | |
| 1637 | dname = strdup(path); |
| 1638 | if (dname == NULL) |
| 1639 | return -ENOMEM; |
| 1640 | |
| 1641 | dir = dirname(dname); |
| 1642 | if (statfs(dir, &st_fs)) { |
| 1643 | pr_warning("failed to statfs %s: %s\n", dir, strerror(errno)); |
| 1644 | err = -errno; |
| 1645 | } |
| 1646 | free(dname); |
| 1647 | |
| 1648 | if (!err && st_fs.f_type != BPF_FS_MAGIC) { |
| 1649 | pr_warning("specified path %s is not on BPF FS\n", path); |
| 1650 | err = -EINVAL; |
| 1651 | } |
| 1652 | |
| 1653 | return err; |
| 1654 | } |
| 1655 | |
| 1656 | int bpf_program__pin_instance(struct bpf_program *prog, const char *path, |
| 1657 | int instance) |
| 1658 | { |
| 1659 | int err; |
| 1660 | |
| 1661 | err = check_path(path); |
| 1662 | if (err) |
| 1663 | return err; |
| 1664 | |
| 1665 | if (prog == NULL) { |
| 1666 | pr_warning("invalid program pointer\n"); |
| 1667 | return -EINVAL; |
| 1668 | } |
| 1669 | |
| 1670 | if (instance < 0 || instance >= prog->instances.nr) { |
| 1671 | pr_warning("invalid prog instance %d of prog %s (max %d)\n", |
| 1672 | instance, prog->section_name, prog->instances.nr); |
| 1673 | return -EINVAL; |
| 1674 | } |
| 1675 | |
| 1676 | if (bpf_obj_pin(prog->instances.fds[instance], path)) { |
| 1677 | pr_warning("failed to pin program: %s\n", strerror(errno)); |
| 1678 | return -errno; |
| 1679 | } |
| 1680 | pr_debug("pinned program '%s'\n", path); |
| 1681 | |
| 1682 | return 0; |
| 1683 | } |
| 1684 | |
| 1685 | static int make_dir(const char *path) |
| 1686 | { |
| 1687 | int err = 0; |
| 1688 | |
| 1689 | if (mkdir(path, 0700) && errno != EEXIST) |
| 1690 | err = -errno; |
| 1691 | |
| 1692 | if (err) |
| 1693 | pr_warning("failed to mkdir %s: %s\n", path, strerror(-err)); |
| 1694 | return err; |
| 1695 | } |
| 1696 | |
| 1697 | int bpf_program__pin(struct bpf_program *prog, const char *path) |
| 1698 | { |
| 1699 | int i, err; |
| 1700 | |
| 1701 | err = check_path(path); |
| 1702 | if (err) |
| 1703 | return err; |
| 1704 | |
| 1705 | if (prog == NULL) { |
| 1706 | pr_warning("invalid program pointer\n"); |
| 1707 | return -EINVAL; |
| 1708 | } |
| 1709 | |
| 1710 | if (prog->instances.nr <= 0) { |
| 1711 | pr_warning("no instances of prog %s to pin\n", |
| 1712 | prog->section_name); |
| 1713 | return -EINVAL; |
| 1714 | } |
| 1715 | |
| 1716 | err = make_dir(path); |
| 1717 | if (err) |
| 1718 | return err; |
| 1719 | |
| 1720 | for (i = 0; i < prog->instances.nr; i++) { |
| 1721 | char buf[PATH_MAX]; |
| 1722 | int len; |
| 1723 | |
| 1724 | len = snprintf(buf, PATH_MAX, "%s/%d", path, i); |
| 1725 | if (len < 0) |
| 1726 | return -EINVAL; |
| 1727 | else if (len >= PATH_MAX) |
| 1728 | return -ENAMETOOLONG; |
| 1729 | |
| 1730 | err = bpf_program__pin_instance(prog, buf, i); |
| 1731 | if (err) |
| 1732 | return err; |
| 1733 | } |
| 1734 | |
| 1735 | return 0; |
| 1736 | } |
| 1737 | |
Joe Stringer | b6989f3 | 2017-01-26 13:19:57 -0800 | [diff] [blame] | 1738 | int bpf_map__pin(struct bpf_map *map, const char *path) |
| 1739 | { |
| 1740 | int err; |
| 1741 | |
| 1742 | err = check_path(path); |
| 1743 | if (err) |
| 1744 | return err; |
| 1745 | |
| 1746 | if (map == NULL) { |
| 1747 | pr_warning("invalid map pointer\n"); |
| 1748 | return -EINVAL; |
| 1749 | } |
| 1750 | |
| 1751 | if (bpf_obj_pin(map->fd, path)) { |
| 1752 | pr_warning("failed to pin map: %s\n", strerror(errno)); |
| 1753 | return -errno; |
| 1754 | } |
| 1755 | |
| 1756 | pr_debug("pinned map '%s'\n", path); |
| 1757 | return 0; |
| 1758 | } |
| 1759 | |
Joe Stringer | d5148d8 | 2017-01-26 13:19:58 -0800 | [diff] [blame] | 1760 | int bpf_object__pin(struct bpf_object *obj, const char *path) |
| 1761 | { |
| 1762 | struct bpf_program *prog; |
| 1763 | struct bpf_map *map; |
| 1764 | int err; |
| 1765 | |
| 1766 | if (!obj) |
| 1767 | return -ENOENT; |
| 1768 | |
| 1769 | if (!obj->loaded) { |
| 1770 | pr_warning("object not yet loaded; load it first\n"); |
| 1771 | return -ENOENT; |
| 1772 | } |
| 1773 | |
| 1774 | err = make_dir(path); |
| 1775 | if (err) |
| 1776 | return err; |
| 1777 | |
| 1778 | bpf_map__for_each(map, obj) { |
| 1779 | char buf[PATH_MAX]; |
| 1780 | int len; |
| 1781 | |
| 1782 | len = snprintf(buf, PATH_MAX, "%s/%s", path, |
| 1783 | bpf_map__name(map)); |
| 1784 | if (len < 0) |
| 1785 | return -EINVAL; |
| 1786 | else if (len >= PATH_MAX) |
| 1787 | return -ENAMETOOLONG; |
| 1788 | |
| 1789 | err = bpf_map__pin(map, buf); |
| 1790 | if (err) |
| 1791 | return err; |
| 1792 | } |
| 1793 | |
| 1794 | bpf_object__for_each_program(prog, obj) { |
| 1795 | char buf[PATH_MAX]; |
| 1796 | int len; |
| 1797 | |
| 1798 | len = snprintf(buf, PATH_MAX, "%s/%s", path, |
| 1799 | prog->section_name); |
| 1800 | if (len < 0) |
| 1801 | return -EINVAL; |
| 1802 | else if (len >= PATH_MAX) |
| 1803 | return -ENAMETOOLONG; |
| 1804 | |
| 1805 | err = bpf_program__pin(prog, buf); |
| 1806 | if (err) |
| 1807 | return err; |
| 1808 | } |
| 1809 | |
| 1810 | return 0; |
| 1811 | } |
| 1812 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1813 | void bpf_object__close(struct bpf_object *obj) |
| 1814 | { |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 1815 | size_t i; |
| 1816 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1817 | if (!obj) |
| 1818 | return; |
| 1819 | |
Wang Nan | 10931d2 | 2016-11-26 07:03:26 +0000 | [diff] [blame] | 1820 | if (obj->clear_priv) |
| 1821 | obj->clear_priv(obj, obj->priv); |
| 1822 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1823 | bpf_object__elf_finish(obj); |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1824 | bpf_object__unload(obj); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 1825 | btf__free(obj->btf); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1826 | |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 1827 | for (i = 0; i < obj->nr_maps; i++) { |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 1828 | zfree(&obj->maps[i].name); |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 1829 | if (obj->maps[i].clear_priv) |
| 1830 | obj->maps[i].clear_priv(&obj->maps[i], |
| 1831 | obj->maps[i].priv); |
| 1832 | obj->maps[i].priv = NULL; |
| 1833 | obj->maps[i].clear_priv = NULL; |
| 1834 | } |
| 1835 | zfree(&obj->maps); |
| 1836 | obj->nr_maps = 0; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 1837 | |
| 1838 | if (obj->programs && obj->nr_programs) { |
| 1839 | for (i = 0; i < obj->nr_programs; i++) |
| 1840 | bpf_program__exit(&obj->programs[i]); |
| 1841 | } |
| 1842 | zfree(&obj->programs); |
| 1843 | |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 1844 | list_del(&obj->list); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1845 | free(obj); |
| 1846 | } |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 1847 | |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 1848 | struct bpf_object * |
| 1849 | bpf_object__next(struct bpf_object *prev) |
| 1850 | { |
| 1851 | struct bpf_object *next; |
| 1852 | |
| 1853 | if (!prev) |
| 1854 | next = list_first_entry(&bpf_objects_list, |
| 1855 | struct bpf_object, |
| 1856 | list); |
| 1857 | else |
| 1858 | next = list_next_entry(prev, list); |
| 1859 | |
| 1860 | /* Empty list is noticed here so don't need checking on entry. */ |
| 1861 | if (&next->list == &bpf_objects_list) |
| 1862 | return NULL; |
| 1863 | |
| 1864 | return next; |
| 1865 | } |
| 1866 | |
Arnaldo Carvalho de Melo | a7fe045 | 2016-06-03 12:22:51 -0300 | [diff] [blame] | 1867 | const char *bpf_object__name(struct bpf_object *obj) |
Wang Nan | acf860a | 2015-08-27 02:30:55 +0000 | [diff] [blame] | 1868 | { |
Arnaldo Carvalho de Melo | a7fe045 | 2016-06-03 12:22:51 -0300 | [diff] [blame] | 1869 | return obj ? obj->path : ERR_PTR(-EINVAL); |
Wang Nan | acf860a | 2015-08-27 02:30:55 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
Arnaldo Carvalho de Melo | a7fe045 | 2016-06-03 12:22:51 -0300 | [diff] [blame] | 1872 | unsigned int bpf_object__kversion(struct bpf_object *obj) |
Wang Nan | 45825d8 | 2015-11-06 13:49:38 +0000 | [diff] [blame] | 1873 | { |
Arnaldo Carvalho de Melo | a7fe045 | 2016-06-03 12:22:51 -0300 | [diff] [blame] | 1874 | return obj ? obj->kern_version : 0; |
Wang Nan | 45825d8 | 2015-11-06 13:49:38 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 1877 | int bpf_object__btf_fd(const struct bpf_object *obj) |
| 1878 | { |
| 1879 | return obj->btf ? btf__fd(obj->btf) : -1; |
| 1880 | } |
| 1881 | |
Wang Nan | 10931d2 | 2016-11-26 07:03:26 +0000 | [diff] [blame] | 1882 | int bpf_object__set_priv(struct bpf_object *obj, void *priv, |
| 1883 | bpf_object_clear_priv_t clear_priv) |
| 1884 | { |
| 1885 | if (obj->priv && obj->clear_priv) |
| 1886 | obj->clear_priv(obj, obj->priv); |
| 1887 | |
| 1888 | obj->priv = priv; |
| 1889 | obj->clear_priv = clear_priv; |
| 1890 | return 0; |
| 1891 | } |
| 1892 | |
| 1893 | void *bpf_object__priv(struct bpf_object *obj) |
| 1894 | { |
| 1895 | return obj ? obj->priv : ERR_PTR(-EINVAL); |
| 1896 | } |
| 1897 | |
Jakub Kicinski | eac7d84 | 2018-06-28 14:41:39 -0700 | [diff] [blame] | 1898 | static struct bpf_program * |
| 1899 | __bpf_program__next(struct bpf_program *prev, struct bpf_object *obj) |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 1900 | { |
| 1901 | size_t idx; |
| 1902 | |
| 1903 | if (!obj->programs) |
| 1904 | return NULL; |
| 1905 | /* First handler */ |
| 1906 | if (prev == NULL) |
| 1907 | return &obj->programs[0]; |
| 1908 | |
| 1909 | if (prev->obj != obj) { |
| 1910 | pr_warning("error: program handler doesn't match object\n"); |
| 1911 | return NULL; |
| 1912 | } |
| 1913 | |
| 1914 | idx = (prev - obj->programs) + 1; |
| 1915 | if (idx >= obj->nr_programs) |
| 1916 | return NULL; |
| 1917 | return &obj->programs[idx]; |
| 1918 | } |
| 1919 | |
Jakub Kicinski | eac7d84 | 2018-06-28 14:41:39 -0700 | [diff] [blame] | 1920 | struct bpf_program * |
| 1921 | bpf_program__next(struct bpf_program *prev, struct bpf_object *obj) |
| 1922 | { |
| 1923 | struct bpf_program *prog = prev; |
| 1924 | |
| 1925 | do { |
| 1926 | prog = __bpf_program__next(prog, obj); |
| 1927 | } while (prog && bpf_program__is_function_storage(prog, obj)); |
| 1928 | |
| 1929 | return prog; |
| 1930 | } |
| 1931 | |
Arnaldo Carvalho de Melo | edb13ed | 2016-06-03 12:38:21 -0300 | [diff] [blame] | 1932 | int bpf_program__set_priv(struct bpf_program *prog, void *priv, |
| 1933 | bpf_program_clear_priv_t clear_priv) |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 1934 | { |
| 1935 | if (prog->priv && prog->clear_priv) |
| 1936 | prog->clear_priv(prog, prog->priv); |
| 1937 | |
| 1938 | prog->priv = priv; |
| 1939 | prog->clear_priv = clear_priv; |
| 1940 | return 0; |
| 1941 | } |
| 1942 | |
Arnaldo Carvalho de Melo | be834ff | 2016-06-03 12:36:39 -0300 | [diff] [blame] | 1943 | void *bpf_program__priv(struct bpf_program *prog) |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 1944 | { |
Arnaldo Carvalho de Melo | be834ff | 2016-06-03 12:36:39 -0300 | [diff] [blame] | 1945 | return prog ? prog->priv : ERR_PTR(-EINVAL); |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
Jakub Kicinski | 9aba361 | 2018-06-28 14:41:37 -0700 | [diff] [blame] | 1948 | void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex) |
| 1949 | { |
| 1950 | prog->prog_ifindex = ifindex; |
| 1951 | } |
| 1952 | |
Namhyung Kim | 715f8db | 2015-11-03 20:21:05 +0900 | [diff] [blame] | 1953 | const char *bpf_program__title(struct bpf_program *prog, bool needs_copy) |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 1954 | { |
| 1955 | const char *title; |
| 1956 | |
| 1957 | title = prog->section_name; |
Namhyung Kim | 715f8db | 2015-11-03 20:21:05 +0900 | [diff] [blame] | 1958 | if (needs_copy) { |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 1959 | title = strdup(title); |
| 1960 | if (!title) { |
| 1961 | pr_warning("failed to strdup program title\n"); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1962 | return ERR_PTR(-ENOMEM); |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 1963 | } |
| 1964 | } |
| 1965 | |
| 1966 | return title; |
| 1967 | } |
| 1968 | |
| 1969 | int bpf_program__fd(struct bpf_program *prog) |
| 1970 | { |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 1971 | return bpf_program__nth_fd(prog, 0); |
| 1972 | } |
| 1973 | |
| 1974 | int bpf_program__set_prep(struct bpf_program *prog, int nr_instances, |
| 1975 | bpf_program_prep_t prep) |
| 1976 | { |
| 1977 | int *instances_fds; |
| 1978 | |
| 1979 | if (nr_instances <= 0 || !prep) |
| 1980 | return -EINVAL; |
| 1981 | |
| 1982 | if (prog->instances.nr > 0 || prog->instances.fds) { |
| 1983 | pr_warning("Can't set pre-processor after loading\n"); |
| 1984 | return -EINVAL; |
| 1985 | } |
| 1986 | |
| 1987 | instances_fds = malloc(sizeof(int) * nr_instances); |
| 1988 | if (!instances_fds) { |
| 1989 | pr_warning("alloc memory failed for fds\n"); |
| 1990 | return -ENOMEM; |
| 1991 | } |
| 1992 | |
| 1993 | /* fill all fd with -1 */ |
| 1994 | memset(instances_fds, -1, sizeof(int) * nr_instances); |
| 1995 | |
| 1996 | prog->instances.nr = nr_instances; |
| 1997 | prog->instances.fds = instances_fds; |
| 1998 | prog->preprocessor = prep; |
| 1999 | return 0; |
| 2000 | } |
| 2001 | |
| 2002 | int bpf_program__nth_fd(struct bpf_program *prog, int n) |
| 2003 | { |
| 2004 | int fd; |
| 2005 | |
Jakub Kicinski | 1e96004 | 2018-07-26 14:32:18 -0700 | [diff] [blame] | 2006 | if (!prog) |
| 2007 | return -EINVAL; |
| 2008 | |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 2009 | if (n >= prog->instances.nr || n < 0) { |
| 2010 | pr_warning("Can't get the %dth fd from program %s: only %d instances\n", |
| 2011 | n, prog->section_name, prog->instances.nr); |
| 2012 | return -EINVAL; |
| 2013 | } |
| 2014 | |
| 2015 | fd = prog->instances.fds[n]; |
| 2016 | if (fd < 0) { |
| 2017 | pr_warning("%dth instance of program '%s' is invalid\n", |
| 2018 | n, prog->section_name); |
| 2019 | return -ENOENT; |
| 2020 | } |
| 2021 | |
| 2022 | return fd; |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 2023 | } |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 2024 | |
Alexei Starovoitov | dd26b7f | 2017-03-30 21:45:40 -0700 | [diff] [blame] | 2025 | 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] | 2026 | { |
| 2027 | prog->type = type; |
| 2028 | } |
| 2029 | |
Wang Nan | 5f44e4c8 | 2016-07-13 10:44:01 +0000 | [diff] [blame] | 2030 | static bool bpf_program__is_type(struct bpf_program *prog, |
| 2031 | enum bpf_prog_type type) |
| 2032 | { |
| 2033 | return prog ? (prog->type == type) : false; |
| 2034 | } |
| 2035 | |
Joe Stringer | ed79407 | 2017-01-22 17:11:23 -0800 | [diff] [blame] | 2036 | #define BPF_PROG_TYPE_FNS(NAME, TYPE) \ |
| 2037 | int bpf_program__set_##NAME(struct bpf_program *prog) \ |
| 2038 | { \ |
| 2039 | if (!prog) \ |
| 2040 | return -EINVAL; \ |
| 2041 | bpf_program__set_type(prog, TYPE); \ |
| 2042 | return 0; \ |
| 2043 | } \ |
| 2044 | \ |
| 2045 | bool bpf_program__is_##NAME(struct bpf_program *prog) \ |
| 2046 | { \ |
| 2047 | return bpf_program__is_type(prog, TYPE); \ |
| 2048 | } \ |
Wang Nan | 5f44e4c8 | 2016-07-13 10:44:01 +0000 | [diff] [blame] | 2049 | |
Joe Stringer | 7803ba7 | 2017-01-22 17:11:24 -0800 | [diff] [blame] | 2050 | BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER); |
Joe Stringer | ed79407 | 2017-01-22 17:11:23 -0800 | [diff] [blame] | 2051 | BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE); |
Joe Stringer | 7803ba7 | 2017-01-22 17:11:24 -0800 | [diff] [blame] | 2052 | BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS); |
| 2053 | BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT); |
Joe Stringer | ed79407 | 2017-01-22 17:11:23 -0800 | [diff] [blame] | 2054 | BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT); |
Andrey Ignatov | e14c93fd | 2018-04-17 10:28:46 -0700 | [diff] [blame] | 2055 | BPF_PROG_TYPE_FNS(raw_tracepoint, BPF_PROG_TYPE_RAW_TRACEPOINT); |
Joe Stringer | 7803ba7 | 2017-01-22 17:11:24 -0800 | [diff] [blame] | 2056 | BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP); |
| 2057 | BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT); |
Wang Nan | 5f44e4c8 | 2016-07-13 10:44:01 +0000 | [diff] [blame] | 2058 | |
John Fastabend | 16962b2 | 2018-04-23 14:30:38 -0700 | [diff] [blame] | 2059 | void bpf_program__set_expected_attach_type(struct bpf_program *prog, |
| 2060 | enum bpf_attach_type type) |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 2061 | { |
| 2062 | prog->expected_attach_type = type; |
| 2063 | } |
| 2064 | |
| 2065 | #define BPF_PROG_SEC_FULL(string, ptype, atype) \ |
| 2066 | { string, sizeof(string) - 1, ptype, atype } |
| 2067 | |
| 2068 | #define BPF_PROG_SEC(string, ptype) BPF_PROG_SEC_FULL(string, ptype, 0) |
| 2069 | |
Andrey Ignatov | 81efee7 | 2018-04-17 10:28:45 -0700 | [diff] [blame] | 2070 | #define BPF_S_PROG_SEC(string, ptype) \ |
| 2071 | BPF_PROG_SEC_FULL(string, BPF_PROG_TYPE_CGROUP_SOCK, ptype) |
| 2072 | |
Andrey Ignatov | e50b0a6 | 2018-03-30 15:08:03 -0700 | [diff] [blame] | 2073 | #define BPF_SA_PROG_SEC(string, ptype) \ |
| 2074 | BPF_PROG_SEC_FULL(string, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, ptype) |
| 2075 | |
Roman Gushchin | 583c900 | 2017-12-13 15:18:51 +0000 | [diff] [blame] | 2076 | static const struct { |
| 2077 | const char *sec; |
| 2078 | size_t len; |
| 2079 | enum bpf_prog_type prog_type; |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 2080 | enum bpf_attach_type expected_attach_type; |
Roman Gushchin | 583c900 | 2017-12-13 15:18:51 +0000 | [diff] [blame] | 2081 | } section_names[] = { |
| 2082 | BPF_PROG_SEC("socket", BPF_PROG_TYPE_SOCKET_FILTER), |
| 2083 | BPF_PROG_SEC("kprobe/", BPF_PROG_TYPE_KPROBE), |
| 2084 | BPF_PROG_SEC("kretprobe/", BPF_PROG_TYPE_KPROBE), |
Quentin Monnet | 0badd33 | 2018-02-07 20:27:13 -0800 | [diff] [blame] | 2085 | BPF_PROG_SEC("classifier", BPF_PROG_TYPE_SCHED_CLS), |
| 2086 | BPF_PROG_SEC("action", BPF_PROG_TYPE_SCHED_ACT), |
Roman Gushchin | 583c900 | 2017-12-13 15:18:51 +0000 | [diff] [blame] | 2087 | BPF_PROG_SEC("tracepoint/", BPF_PROG_TYPE_TRACEPOINT), |
Andrey Ignatov | e14c93fd | 2018-04-17 10:28:46 -0700 | [diff] [blame] | 2088 | BPF_PROG_SEC("raw_tracepoint/", BPF_PROG_TYPE_RAW_TRACEPOINT), |
Roman Gushchin | 583c900 | 2017-12-13 15:18:51 +0000 | [diff] [blame] | 2089 | BPF_PROG_SEC("xdp", BPF_PROG_TYPE_XDP), |
| 2090 | BPF_PROG_SEC("perf_event", BPF_PROG_TYPE_PERF_EVENT), |
| 2091 | BPF_PROG_SEC("cgroup/skb", BPF_PROG_TYPE_CGROUP_SKB), |
| 2092 | BPF_PROG_SEC("cgroup/sock", BPF_PROG_TYPE_CGROUP_SOCK), |
| 2093 | BPF_PROG_SEC("cgroup/dev", BPF_PROG_TYPE_CGROUP_DEVICE), |
Quentin Monnet | 0badd33 | 2018-02-07 20:27:13 -0800 | [diff] [blame] | 2094 | BPF_PROG_SEC("lwt_in", BPF_PROG_TYPE_LWT_IN), |
| 2095 | BPF_PROG_SEC("lwt_out", BPF_PROG_TYPE_LWT_OUT), |
| 2096 | BPF_PROG_SEC("lwt_xmit", BPF_PROG_TYPE_LWT_XMIT), |
Jakub Kicinski | d9b683d | 2018-06-28 14:41:36 -0700 | [diff] [blame] | 2097 | BPF_PROG_SEC("lwt_seg6local", BPF_PROG_TYPE_LWT_SEG6LOCAL), |
Roman Gushchin | 583c900 | 2017-12-13 15:18:51 +0000 | [diff] [blame] | 2098 | BPF_PROG_SEC("sockops", BPF_PROG_TYPE_SOCK_OPS), |
| 2099 | BPF_PROG_SEC("sk_skb", BPF_PROG_TYPE_SK_SKB), |
John Fastabend | 4c4c3c2 | 2018-03-18 12:57:41 -0700 | [diff] [blame] | 2100 | BPF_PROG_SEC("sk_msg", BPF_PROG_TYPE_SK_MSG), |
Jakub Kicinski | d9b683d | 2018-06-28 14:41:36 -0700 | [diff] [blame] | 2101 | BPF_PROG_SEC("lirc_mode2", BPF_PROG_TYPE_LIRC_MODE2), |
Andrey Ignatov | e50b0a6 | 2018-03-30 15:08:03 -0700 | [diff] [blame] | 2102 | BPF_SA_PROG_SEC("cgroup/bind4", BPF_CGROUP_INET4_BIND), |
| 2103 | BPF_SA_PROG_SEC("cgroup/bind6", BPF_CGROUP_INET6_BIND), |
Andrey Ignatov | 622adaf | 2018-03-30 15:08:06 -0700 | [diff] [blame] | 2104 | BPF_SA_PROG_SEC("cgroup/connect4", BPF_CGROUP_INET4_CONNECT), |
| 2105 | BPF_SA_PROG_SEC("cgroup/connect6", BPF_CGROUP_INET6_CONNECT), |
Andrey Ignatov | 72481f3 | 2018-05-25 08:55:25 -0700 | [diff] [blame] | 2106 | BPF_SA_PROG_SEC("cgroup/sendmsg4", BPF_CGROUP_UDP4_SENDMSG), |
| 2107 | BPF_SA_PROG_SEC("cgroup/sendmsg6", BPF_CGROUP_UDP6_SENDMSG), |
Andrey Ignatov | 81efee7 | 2018-04-17 10:28:45 -0700 | [diff] [blame] | 2108 | BPF_S_PROG_SEC("cgroup/post_bind4", BPF_CGROUP_INET4_POST_BIND), |
| 2109 | BPF_S_PROG_SEC("cgroup/post_bind6", BPF_CGROUP_INET6_POST_BIND), |
Roman Gushchin | 583c900 | 2017-12-13 15:18:51 +0000 | [diff] [blame] | 2110 | }; |
Roman Gushchin | 583c900 | 2017-12-13 15:18:51 +0000 | [diff] [blame] | 2111 | |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 2112 | #undef BPF_PROG_SEC |
| 2113 | #undef BPF_PROG_SEC_FULL |
Andrey Ignatov | 81efee7 | 2018-04-17 10:28:45 -0700 | [diff] [blame] | 2114 | #undef BPF_S_PROG_SEC |
Andrey Ignatov | e50b0a6 | 2018-03-30 15:08:03 -0700 | [diff] [blame] | 2115 | #undef BPF_SA_PROG_SEC |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 2116 | |
Jakub Kicinski | b60df2a | 2018-07-10 14:42:59 -0700 | [diff] [blame] | 2117 | int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type, |
| 2118 | enum bpf_attach_type *expected_attach_type) |
Roman Gushchin | 583c900 | 2017-12-13 15:18:51 +0000 | [diff] [blame] | 2119 | { |
| 2120 | int i; |
| 2121 | |
Jakub Kicinski | b60df2a | 2018-07-10 14:42:59 -0700 | [diff] [blame] | 2122 | if (!name) |
| 2123 | return -EINVAL; |
Roman Gushchin | 583c900 | 2017-12-13 15:18:51 +0000 | [diff] [blame] | 2124 | |
Jakub Kicinski | b60df2a | 2018-07-10 14:42:59 -0700 | [diff] [blame] | 2125 | for (i = 0; i < ARRAY_SIZE(section_names); i++) { |
| 2126 | if (strncmp(name, section_names[i].sec, section_names[i].len)) |
| 2127 | continue; |
| 2128 | *prog_type = section_names[i].prog_type; |
| 2129 | *expected_attach_type = section_names[i].expected_attach_type; |
| 2130 | return 0; |
| 2131 | } |
| 2132 | return -EINVAL; |
| 2133 | } |
Roman Gushchin | 583c900 | 2017-12-13 15:18:51 +0000 | [diff] [blame] | 2134 | |
Jakub Kicinski | b60df2a | 2018-07-10 14:42:59 -0700 | [diff] [blame] | 2135 | static int |
| 2136 | bpf_program__identify_section(struct bpf_program *prog, |
| 2137 | enum bpf_prog_type *prog_type, |
| 2138 | enum bpf_attach_type *expected_attach_type) |
| 2139 | { |
| 2140 | return libbpf_prog_type_by_name(prog->section_name, prog_type, |
| 2141 | expected_attach_type); |
Roman Gushchin | 583c900 | 2017-12-13 15:18:51 +0000 | [diff] [blame] | 2142 | } |
| 2143 | |
Arnaldo Carvalho de Melo | 6e009e65 | 2016-06-03 12:15:52 -0300 | [diff] [blame] | 2144 | int bpf_map__fd(struct bpf_map *map) |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 2145 | { |
Arnaldo Carvalho de Melo | 6e009e65 | 2016-06-03 12:15:52 -0300 | [diff] [blame] | 2146 | return map ? map->fd : -EINVAL; |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 2147 | } |
| 2148 | |
Arnaldo Carvalho de Melo | 53897a7 | 2016-06-02 14:21:06 -0300 | [diff] [blame] | 2149 | const struct bpf_map_def *bpf_map__def(struct bpf_map *map) |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 2150 | { |
Arnaldo Carvalho de Melo | 53897a7 | 2016-06-02 14:21:06 -0300 | [diff] [blame] | 2151 | return map ? &map->def : ERR_PTR(-EINVAL); |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 2152 | } |
| 2153 | |
Arnaldo Carvalho de Melo | 009ad5d | 2016-06-02 11:02:05 -0300 | [diff] [blame] | 2154 | const char *bpf_map__name(struct bpf_map *map) |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 2155 | { |
Arnaldo Carvalho de Melo | 009ad5d | 2016-06-02 11:02:05 -0300 | [diff] [blame] | 2156 | return map ? map->name : NULL; |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
Martin KaFai Lau | 61746dbe | 2018-05-22 15:04:24 -0700 | [diff] [blame] | 2159 | uint32_t bpf_map__btf_key_type_id(const struct bpf_map *map) |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 2160 | { |
Martin KaFai Lau | 61746dbe | 2018-05-22 15:04:24 -0700 | [diff] [blame] | 2161 | return map ? map->btf_key_type_id : 0; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 2162 | } |
| 2163 | |
Martin KaFai Lau | 61746dbe | 2018-05-22 15:04:24 -0700 | [diff] [blame] | 2164 | uint32_t bpf_map__btf_value_type_id(const struct bpf_map *map) |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 2165 | { |
Martin KaFai Lau | 61746dbe | 2018-05-22 15:04:24 -0700 | [diff] [blame] | 2166 | return map ? map->btf_value_type_id : 0; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 2167 | } |
| 2168 | |
Arnaldo Carvalho de Melo | edb13ed | 2016-06-03 12:38:21 -0300 | [diff] [blame] | 2169 | int bpf_map__set_priv(struct bpf_map *map, void *priv, |
| 2170 | bpf_map_clear_priv_t clear_priv) |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 2171 | { |
| 2172 | if (!map) |
| 2173 | return -EINVAL; |
| 2174 | |
| 2175 | if (map->priv) { |
| 2176 | if (map->clear_priv) |
| 2177 | map->clear_priv(map, map->priv); |
| 2178 | } |
| 2179 | |
| 2180 | map->priv = priv; |
| 2181 | map->clear_priv = clear_priv; |
| 2182 | return 0; |
| 2183 | } |
| 2184 | |
Arnaldo Carvalho de Melo | b4cbfa5 | 2016-06-02 10:51:59 -0300 | [diff] [blame] | 2185 | void *bpf_map__priv(struct bpf_map *map) |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 2186 | { |
Arnaldo Carvalho de Melo | b4cbfa5 | 2016-06-02 10:51:59 -0300 | [diff] [blame] | 2187 | return map ? map->priv : ERR_PTR(-EINVAL); |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 2188 | } |
| 2189 | |
Jakub Kicinski | f83fb22 | 2018-07-10 14:43:01 -0700 | [diff] [blame] | 2190 | bool bpf_map__is_offload_neutral(struct bpf_map *map) |
| 2191 | { |
| 2192 | return map->def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY; |
| 2193 | } |
| 2194 | |
Jakub Kicinski | 9aba361 | 2018-06-28 14:41:37 -0700 | [diff] [blame] | 2195 | void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex) |
| 2196 | { |
| 2197 | map->map_ifindex = ifindex; |
| 2198 | } |
| 2199 | |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 2200 | struct bpf_map * |
| 2201 | bpf_map__next(struct bpf_map *prev, struct bpf_object *obj) |
| 2202 | { |
| 2203 | size_t idx; |
| 2204 | struct bpf_map *s, *e; |
| 2205 | |
| 2206 | if (!obj || !obj->maps) |
| 2207 | return NULL; |
| 2208 | |
| 2209 | s = obj->maps; |
| 2210 | e = obj->maps + obj->nr_maps; |
| 2211 | |
| 2212 | if (prev == NULL) |
| 2213 | return s; |
| 2214 | |
| 2215 | if ((prev < s) || (prev >= e)) { |
| 2216 | pr_warning("error in %s: map handler doesn't belong to object\n", |
| 2217 | __func__); |
| 2218 | return NULL; |
| 2219 | } |
| 2220 | |
| 2221 | idx = (prev - obj->maps) + 1; |
| 2222 | if (idx >= obj->nr_maps) |
| 2223 | return NULL; |
| 2224 | return &obj->maps[idx]; |
| 2225 | } |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 2226 | |
| 2227 | struct bpf_map * |
Arnaldo Carvalho de Melo | a7fe045 | 2016-06-03 12:22:51 -0300 | [diff] [blame] | 2228 | bpf_object__find_map_by_name(struct bpf_object *obj, const char *name) |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 2229 | { |
| 2230 | struct bpf_map *pos; |
| 2231 | |
| 2232 | bpf_map__for_each(pos, obj) { |
Wang Nan | 973170e | 2015-12-08 02:25:29 +0000 | [diff] [blame] | 2233 | if (pos->name && !strcmp(pos->name, name)) |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 2234 | return pos; |
| 2235 | } |
| 2236 | return NULL; |
| 2237 | } |
Wang Nan | 5a6acad | 2016-11-26 07:03:27 +0000 | [diff] [blame] | 2238 | |
| 2239 | struct bpf_map * |
| 2240 | bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset) |
| 2241 | { |
| 2242 | int i; |
| 2243 | |
| 2244 | for (i = 0; i < obj->nr_maps; i++) { |
| 2245 | if (obj->maps[i].offset == offset) |
| 2246 | return &obj->maps[i]; |
| 2247 | } |
| 2248 | return ERR_PTR(-ENOENT); |
| 2249 | } |
Joe Stringer | e28ff1a | 2017-01-22 17:11:25 -0800 | [diff] [blame] | 2250 | |
| 2251 | long libbpf_get_error(const void *ptr) |
| 2252 | { |
| 2253 | if (IS_ERR(ptr)) |
| 2254 | return PTR_ERR(ptr); |
| 2255 | return 0; |
| 2256 | } |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 2257 | |
| 2258 | int bpf_prog_load(const char *file, enum bpf_prog_type type, |
| 2259 | struct bpf_object **pobj, int *prog_fd) |
| 2260 | { |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 2261 | struct bpf_prog_load_attr attr; |
| 2262 | |
| 2263 | memset(&attr, 0, sizeof(struct bpf_prog_load_attr)); |
| 2264 | attr.file = file; |
| 2265 | attr.prog_type = type; |
| 2266 | attr.expected_attach_type = 0; |
| 2267 | |
| 2268 | return bpf_prog_load_xattr(&attr, pobj, prog_fd); |
| 2269 | } |
| 2270 | |
| 2271 | int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr, |
| 2272 | struct bpf_object **pobj, int *prog_fd) |
| 2273 | { |
Jakub Kicinski | 07f2d4e | 2018-07-10 14:43:02 -0700 | [diff] [blame] | 2274 | struct bpf_object_open_attr open_attr = { |
| 2275 | .file = attr->file, |
| 2276 | .prog_type = attr->prog_type, |
| 2277 | }; |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 2278 | struct bpf_program *prog, *first_prog = NULL; |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 2279 | enum bpf_attach_type expected_attach_type; |
| 2280 | enum bpf_prog_type prog_type; |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 2281 | struct bpf_object *obj; |
David Beckett | f0307a7 | 2018-05-16 14:02:49 -0700 | [diff] [blame] | 2282 | struct bpf_map *map; |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 2283 | int err; |
| 2284 | |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 2285 | if (!attr) |
| 2286 | return -EINVAL; |
Jakub Kicinski | 17387dd | 2018-05-10 10:24:42 -0700 | [diff] [blame] | 2287 | if (!attr->file) |
| 2288 | return -EINVAL; |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 2289 | |
Jakub Kicinski | 07f2d4e | 2018-07-10 14:43:02 -0700 | [diff] [blame] | 2290 | obj = bpf_object__open_xattr(&open_attr); |
Jakub Kicinski | 3597683 | 2018-05-10 10:09:34 -0700 | [diff] [blame] | 2291 | if (IS_ERR_OR_NULL(obj)) |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 2292 | return -ENOENT; |
| 2293 | |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 2294 | bpf_object__for_each_program(prog, obj) { |
| 2295 | /* |
| 2296 | * If type is not specified, try to guess it based on |
| 2297 | * section name. |
| 2298 | */ |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 2299 | prog_type = attr->prog_type; |
David Beckett | f0307a7 | 2018-05-16 14:02:49 -0700 | [diff] [blame] | 2300 | prog->prog_ifindex = attr->ifindex; |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 2301 | expected_attach_type = attr->expected_attach_type; |
| 2302 | if (prog_type == BPF_PROG_TYPE_UNSPEC) { |
Jakub Kicinski | b60df2a | 2018-07-10 14:42:59 -0700 | [diff] [blame] | 2303 | err = bpf_program__identify_section(prog, &prog_type, |
| 2304 | &expected_attach_type); |
| 2305 | if (err < 0) { |
| 2306 | pr_warning("failed to guess program type based on section name %s\n", |
| 2307 | prog->section_name); |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 2308 | bpf_object__close(obj); |
| 2309 | return -EINVAL; |
| 2310 | } |
| 2311 | } |
| 2312 | |
Andrey Ignatov | d7be143 | 2018-03-30 15:08:01 -0700 | [diff] [blame] | 2313 | bpf_program__set_type(prog, prog_type); |
| 2314 | bpf_program__set_expected_attach_type(prog, |
| 2315 | expected_attach_type); |
| 2316 | |
Jakub Kicinski | 9a94f27 | 2018-06-28 14:41:38 -0700 | [diff] [blame] | 2317 | if (!bpf_program__is_function_storage(prog, obj) && !first_prog) |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 2318 | first_prog = prog; |
| 2319 | } |
| 2320 | |
David Beckett | f0307a7 | 2018-05-16 14:02:49 -0700 | [diff] [blame] | 2321 | bpf_map__for_each(map, obj) { |
Jakub Kicinski | f83fb22 | 2018-07-10 14:43:01 -0700 | [diff] [blame] | 2322 | if (!bpf_map__is_offload_neutral(map)) |
| 2323 | map->map_ifindex = attr->ifindex; |
David Beckett | f0307a7 | 2018-05-16 14:02:49 -0700 | [diff] [blame] | 2324 | } |
| 2325 | |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 2326 | if (!first_prog) { |
| 2327 | pr_warning("object file doesn't contain bpf program\n"); |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 2328 | bpf_object__close(obj); |
| 2329 | return -ENOENT; |
| 2330 | } |
| 2331 | |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 2332 | err = bpf_object__load(obj); |
| 2333 | if (err) { |
| 2334 | bpf_object__close(obj); |
| 2335 | return -EINVAL; |
| 2336 | } |
| 2337 | |
| 2338 | *pobj = obj; |
Alexei Starovoitov | 48cca7e | 2017-12-14 17:55:10 -0800 | [diff] [blame] | 2339 | *prog_fd = bpf_program__fd(first_prog); |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 2340 | return 0; |
| 2341 | } |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 2342 | |
| 2343 | enum bpf_perf_event_ret |
| 2344 | bpf_perf_event_read_simple(void *mem, unsigned long size, |
| 2345 | unsigned long page_size, void **buf, size_t *buf_len, |
| 2346 | bpf_perf_event_print_t fn, void *priv) |
| 2347 | { |
| 2348 | volatile struct perf_event_mmap_page *header = mem; |
| 2349 | __u64 data_tail = header->data_tail; |
| 2350 | __u64 data_head = header->data_head; |
| 2351 | void *base, *begin, *end; |
| 2352 | int ret; |
| 2353 | |
| 2354 | asm volatile("" ::: "memory"); /* in real code it should be smp_rmb() */ |
| 2355 | if (data_head == data_tail) |
| 2356 | return LIBBPF_PERF_EVENT_CONT; |
| 2357 | |
| 2358 | base = ((char *)header) + page_size; |
| 2359 | |
| 2360 | begin = base + data_tail % size; |
| 2361 | end = base + data_head % size; |
| 2362 | |
| 2363 | while (begin != end) { |
| 2364 | struct perf_event_header *ehdr; |
| 2365 | |
| 2366 | ehdr = begin; |
| 2367 | if (begin + ehdr->size > base + size) { |
| 2368 | long len = base + size - begin; |
| 2369 | |
| 2370 | if (*buf_len < ehdr->size) { |
| 2371 | free(*buf); |
| 2372 | *buf = malloc(ehdr->size); |
| 2373 | if (!*buf) { |
| 2374 | ret = LIBBPF_PERF_EVENT_ERROR; |
| 2375 | break; |
| 2376 | } |
| 2377 | *buf_len = ehdr->size; |
| 2378 | } |
| 2379 | |
| 2380 | memcpy(*buf, begin, len); |
| 2381 | memcpy(*buf + len, base, ehdr->size - len); |
| 2382 | ehdr = (void *)*buf; |
| 2383 | begin = base + ehdr->size - len; |
| 2384 | } else if (begin + ehdr->size == base + size) { |
| 2385 | begin = base; |
| 2386 | } else { |
| 2387 | begin += ehdr->size; |
| 2388 | } |
| 2389 | |
| 2390 | ret = fn(ehdr, priv); |
| 2391 | if (ret != LIBBPF_PERF_EVENT_CONT) |
| 2392 | break; |
| 2393 | |
| 2394 | data_tail += ehdr->size; |
| 2395 | } |
| 2396 | |
| 2397 | __sync_synchronize(); /* smp_mb() */ |
| 2398 | header->data_tail = data_tail; |
| 2399 | |
| 2400 | return ret; |
| 2401 | } |