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