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