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