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