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