blob: ff9174282a8c6afdfeb79015a7ccbe0c7b33ac7b [file] [log] [blame]
Alexei Starovoitov1bc38b82018-10-05 16:40:00 -07001// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
Eric Leblond6061a3d2018-01-30 21:55:03 +01002
Wang Nan1b76c132015-07-01 02:13:51 +00003/*
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 Stringerf3675402017-01-26 13:19:56 -08009 * Copyright (C) 2017 Nicira, Inc.
Daniel Borkmannd8599002019-04-09 23:20:13 +020010 * Copyright (C) 2019 Isovalent, Inc.
Wang Nan1b76c132015-07-01 02:13:51 +000011 */
12
Yonghong Songb4269952018-11-29 15:31:45 -080013#ifndef _GNU_SOURCE
Jakub Kicinski531b0142018-07-10 14:43:05 -070014#define _GNU_SOURCE
Yonghong Songb4269952018-11-29 15:31:45 -080015#endif
Wang Nan1b76c132015-07-01 02:13:51 +000016#include <stdlib.h>
Wang Nanb3f59d62015-07-01 02:13:52 +000017#include <stdio.h>
18#include <stdarg.h>
Joe Stringerf3675402017-01-26 13:19:56 -080019#include <libgen.h>
Wang Nan34090912015-07-01 02:14:02 +000020#include <inttypes.h>
Andrii Nakryiko8ab9da52019-12-23 10:03:05 -080021#include <limits.h>
Wang Nanb3f59d62015-07-01 02:13:52 +000022#include <string.h>
Wang Nan1b76c132015-07-01 02:13:51 +000023#include <unistd.h>
Arnaldo Carvalho de Melocdb2f922019-07-19 11:34:06 -030024#include <endian.h>
Wang Nan1a5e3fb2015-07-01 02:13:53 +000025#include <fcntl.h>
26#include <errno.h>
Toke Høiland-Jørgensen113e6b72020-02-17 18:17:01 +010027#include <ctype.h>
Wang Nan1b76c132015-07-01 02:13:51 +000028#include <asm/unistd.h>
Joe Stringere28ff1a2017-01-22 17:11:25 -080029#include <linux/err.h>
Wang Nancb1e5e92015-07-01 02:13:57 +000030#include <linux/kernel.h>
Wang Nan1b76c132015-07-01 02:13:51 +000031#include <linux/bpf.h>
Martin KaFai Lau38d5d3b2018-07-24 08:40:22 -070032#include <linux/btf.h>
Stanislav Fomichev47eff612018-11-20 17:11:19 -080033#include <linux/filter.h>
Wang Nan9a208ef2015-07-01 02:14:10 +000034#include <linux/list.h>
Joe Stringerf3675402017-01-26 13:19:56 -080035#include <linux/limits.h>
Yonghong Song438363c2018-10-09 16:14:47 -070036#include <linux/perf_event.h>
Daniel Borkmanna64af0e2018-10-19 15:51:03 +020037#include <linux/ring_buffer.h>
Andrii Nakryiko5e61f272019-10-04 15:40:34 -070038#include <linux/version.h>
Andrii Nakryikofb84b822019-07-06 11:06:24 -070039#include <sys/epoll.h>
Andrii Nakryiko63f2f5e2019-07-01 16:58:57 -070040#include <sys/ioctl.h>
Andrii Nakryikofb84b822019-07-06 11:06:24 -070041#include <sys/mman.h>
Joe Stringerf3675402017-01-26 13:19:56 -080042#include <sys/stat.h>
43#include <sys/types.h>
44#include <sys/vfs.h>
Andrii Nakryikoddc7c302019-08-07 14:39:51 -070045#include <sys/utsname.h>
Toke Høiland-Jørgensendc3a2d22019-12-16 19:12:04 +010046#include <sys/resource.h>
Jakub Kicinski531b0142018-07-10 14:43:05 -070047#include <tools/libc_compat.h>
Wang Nan1a5e3fb2015-07-01 02:13:53 +000048#include <libelf.h>
49#include <gelf.h>
Andrii Nakryiko166750b2019-12-13 17:47:08 -080050#include <zlib.h>
Wang Nan1b76c132015-07-01 02:13:51 +000051
52#include "libbpf.h"
Wang Nan52d33522015-07-01 02:14:04 +000053#include "bpf.h"
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -070054#include "btf.h"
Arnaldo Carvalho de Melo6d419072018-09-14 16:47:14 -030055#include "str_error.h"
Andrii Nakryikod7c4b392019-05-10 14:13:15 -070056#include "libbpf_internal.h"
Andrii Nakryikoddc7c302019-08-07 14:39:51 -070057#include "hashmap.h"
Wang Nanb3f59d62015-07-01 02:13:52 +000058
Andrii Nakryiko1d1a3bc2020-01-10 10:19:16 -080059/* make sure libbpf doesn't use kernel-only integer typedefs */
60#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
61
Wang Nan9b161372016-07-18 06:01:08 +000062#ifndef EM_BPF
63#define EM_BPF 247
64#endif
65
Joe Stringerf3675402017-01-26 13:19:56 -080066#ifndef BPF_FS_MAGIC
67#define BPF_FS_MAGIC 0xcafe4a11
68#endif
69
Andrey Ignatovff466b52019-04-06 22:37:34 -070070/* vsprintf() in __base_pr() uses nonliteral format string. It may break
71 * compilation if user enables corresponding warning. Disable it explicitly.
72 */
73#pragma GCC diagnostic ignored "-Wformat-nonliteral"
74
Wang Nanb3f59d62015-07-01 02:13:52 +000075#define __printf(a, b) __attribute__((format(printf, a, b)))
76
Martin KaFai Lau590a0082020-01-08 16:35:14 -080077static struct bpf_map *bpf_object__add_map(struct bpf_object *obj);
78static struct bpf_program *bpf_object__find_prog_by_idx(struct bpf_object *obj,
79 int idx);
80static const struct btf_type *
81skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id);
82
Stanislav Fomicheva8a1f7d2019-02-04 16:20:55 -080083static int __base_pr(enum libbpf_print_level level, const char *format,
84 va_list args)
Wang Nanb3f59d62015-07-01 02:13:52 +000085{
Yonghong Song6f1ae8b2019-02-01 16:14:17 -080086 if (level == LIBBPF_DEBUG)
87 return 0;
88
Stanislav Fomicheva8a1f7d2019-02-04 16:20:55 -080089 return vfprintf(stderr, format, args);
Wang Nanb3f59d62015-07-01 02:13:52 +000090}
91
Stanislav Fomicheva8a1f7d2019-02-04 16:20:55 -080092static libbpf_print_fn_t __libbpf_pr = __base_pr;
Wang Nanb3f59d62015-07-01 02:13:52 +000093
Andrii Nakryikoe87fd8b2019-07-27 20:25:26 -070094libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn)
Wang Nanb3f59d62015-07-01 02:13:52 +000095{
Andrii Nakryikoe87fd8b2019-07-27 20:25:26 -070096 libbpf_print_fn_t old_print_fn = __libbpf_pr;
97
Yonghong Song6f1ae8b2019-02-01 16:14:17 -080098 __libbpf_pr = fn;
Andrii Nakryikoe87fd8b2019-07-27 20:25:26 -070099 return old_print_fn;
Wang Nanb3f59d62015-07-01 02:13:52 +0000100}
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000101
Yonghong Song8461ef82019-02-01 16:14:14 -0800102__printf(2, 3)
103void libbpf_print(enum libbpf_print_level level, const char *format, ...)
104{
105 va_list args;
106
Yonghong Song6f1ae8b2019-02-01 16:14:17 -0800107 if (!__libbpf_pr)
108 return;
109
Yonghong Song8461ef82019-02-01 16:14:14 -0800110 va_start(args, format);
Yonghong Song6f1ae8b2019-02-01 16:14:17 -0800111 __libbpf_pr(level, format, args);
Yonghong Song8461ef82019-02-01 16:14:14 -0800112 va_end(args);
113}
114
Toke Høiland-Jørgensendc3a2d22019-12-16 19:12:04 +0100115static void pr_perm_msg(int err)
116{
117 struct rlimit limit;
118 char buf[100];
119
120 if (err != -EPERM || geteuid() != 0)
121 return;
122
123 err = getrlimit(RLIMIT_MEMLOCK, &limit);
124 if (err)
125 return;
126
127 if (limit.rlim_cur == RLIM_INFINITY)
128 return;
129
130 if (limit.rlim_cur < 1024)
Toke Høiland-Jørgensenb5c7d0d2019-12-19 10:02:36 +0100131 snprintf(buf, sizeof(buf), "%zu bytes", (size_t)limit.rlim_cur);
Toke Høiland-Jørgensendc3a2d22019-12-16 19:12:04 +0100132 else if (limit.rlim_cur < 1024*1024)
133 snprintf(buf, sizeof(buf), "%.1f KiB", (double)limit.rlim_cur / 1024);
134 else
135 snprintf(buf, sizeof(buf), "%.1f MiB", (double)limit.rlim_cur / (1024*1024));
136
137 pr_warn("permission error while running as root; try raising 'ulimit -l'? current value: %s\n",
138 buf);
139}
140
Wang Nan6371ca3b2015-11-06 13:49:37 +0000141#define STRERR_BUFSIZE 128
142
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000143/* Copied from tools/perf/util/util.h */
144#ifndef zfree
145# define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
146#endif
147
148#ifndef zclose
149# define zclose(fd) ({ \
150 int ___err = 0; \
151 if ((fd) >= 0) \
152 ___err = close((fd)); \
153 fd = -1; \
154 ___err; })
155#endif
156
157#ifdef HAVE_LIBELF_MMAP_SUPPORT
158# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP
159#else
160# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ
161#endif
162
Song Liu34be16462019-03-11 22:30:38 -0700163static inline __u64 ptr_to_u64(const void *ptr)
164{
165 return (__u64) (unsigned long) ptr;
166}
167
Stanislav Fomichev47eff612018-11-20 17:11:19 -0800168struct bpf_capabilities {
169 /* v4.14: kernel support for program & map names. */
170 __u32 name:1;
Daniel Borkmann8837fe52019-04-24 00:45:56 +0200171 /* v5.2: kernel support for global data sections. */
172 __u32 global_data:1;
Andrii Nakryikod7c4b392019-05-10 14:13:15 -0700173 /* BTF_KIND_FUNC and BTF_KIND_FUNC_PROTO support */
174 __u32 btf_func:1;
175 /* BTF_KIND_VAR and BTF_KIND_DATASEC support */
176 __u32 btf_datasec:1;
Andrii Nakryiko7fe74b42019-11-17 09:28:05 -0800177 /* BPF_F_MMAPABLE is supported for arrays */
178 __u32 array_mmap:1;
Alexei Starovoitov2d3eb672020-01-09 22:41:19 -0800179 /* BTF_FUNC_GLOBAL is supported */
180 __u32 btf_func_global:1;
Stanislav Fomichev47eff612018-11-20 17:11:19 -0800181};
182
Andrii Nakryiko166750b2019-12-13 17:47:08 -0800183enum reloc_type {
184 RELO_LD64,
185 RELO_CALL,
186 RELO_DATA,
187 RELO_EXTERN,
188};
189
190struct reloc_desc {
191 enum reloc_type type;
192 int insn_idx;
193 int map_idx;
194 int sym_off;
195};
196
Wang Nana5b8bd42015-07-01 02:14:00 +0000197/*
198 * bpf_prog should be a better name but it has been used in
199 * linux/filter.h.
200 */
201struct bpf_program {
202 /* Index in elf obj file, for relocation use. */
203 int idx;
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700204 char *name;
David Beckettf0307a72018-05-16 14:02:49 -0700205 int prog_ifindex;
Wang Nana5b8bd42015-07-01 02:14:00 +0000206 char *section_name;
Stanislav Fomichev33a2c752018-11-09 08:21:43 -0800207 /* section_name with / replaced by _; makes recursive pinning
208 * in bpf_object__pin_programs easier
209 */
210 char *pin_name;
Wang Nana5b8bd42015-07-01 02:14:00 +0000211 struct bpf_insn *insns;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800212 size_t insns_cnt, main_prog_cnt;
Wang Nan5f44e4c82016-07-13 10:44:01 +0000213 enum bpf_prog_type type;
Wang Nan34090912015-07-01 02:14:02 +0000214
Andrii Nakryiko166750b2019-12-13 17:47:08 -0800215 struct reloc_desc *reloc_desc;
Wang Nan34090912015-07-01 02:14:02 +0000216 int nr_reloc;
Alexei Starovoitovda11b412019-04-01 21:27:47 -0700217 int log_level;
Wang Nan55cffde2015-07-01 02:14:07 +0000218
Wang Nanb5805632015-11-16 12:10:09 +0000219 struct {
220 int nr;
221 int *fds;
222 } instances;
223 bpf_program_prep_t preprocessor;
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000224
225 struct bpf_object *obj;
226 void *priv;
227 bpf_program_clear_priv_t clear_priv;
Andrey Ignatovd7be1432018-03-30 15:08:01 -0700228
229 enum bpf_attach_type expected_attach_type;
Alexei Starovoitov12a86542019-10-30 15:32:12 -0700230 __u32 attach_btf_id;
Alexei Starovoitove7bf94d2019-11-14 10:57:18 -0800231 __u32 attach_prog_fd;
Yonghong Song2993e052018-11-19 15:29:16 -0800232 void *func_info;
233 __u32 func_info_rec_size;
Martin KaFai Lauf0187f02018-12-07 16:42:29 -0800234 __u32 func_info_cnt;
Stanislav Fomichev47eff612018-11-20 17:11:19 -0800235
236 struct bpf_capabilities *caps;
Martin KaFai Lau3d650142018-12-07 16:42:31 -0800237
238 void *line_info;
239 __u32 line_info_rec_size;
240 __u32 line_info_cnt;
Jiong Wang04656192019-05-24 23:25:19 +0100241 __u32 prog_flags;
Wang Nana5b8bd42015-07-01 02:14:00 +0000242};
243
Martin KaFai Lau590a0082020-01-08 16:35:14 -0800244struct bpf_struct_ops {
245 const char *tname;
246 const struct btf_type *type;
247 struct bpf_program **progs;
248 __u32 *kern_func_off;
249 /* e.g. struct tcp_congestion_ops in bpf_prog's btf format */
250 void *data;
251 /* e.g. struct bpf_struct_ops_tcp_congestion_ops in
252 * btf_vmlinux's format.
253 * struct bpf_struct_ops_tcp_congestion_ops {
254 * [... some other kernel fields ...]
255 * struct tcp_congestion_ops data;
256 * }
257 * kern_vdata-size == sizeof(struct bpf_struct_ops_tcp_congestion_ops)
258 * bpf_map__init_kern_struct_ops() will populate the "kern_vdata"
259 * from "data".
260 */
261 void *kern_vdata;
262 __u32 type_id;
263};
264
Andrii Nakryikoac9d1382019-12-13 17:47:07 -0800265#define DATA_SEC ".data"
266#define BSS_SEC ".bss"
267#define RODATA_SEC ".rodata"
Andrii Nakryiko81bfdd02019-12-18 16:28:34 -0800268#define KCONFIG_SEC ".kconfig"
Martin KaFai Lau590a0082020-01-08 16:35:14 -0800269#define STRUCT_OPS_SEC ".struct_ops"
Andrii Nakryikoac9d1382019-12-13 17:47:07 -0800270
Daniel Borkmannd8599002019-04-09 23:20:13 +0200271enum libbpf_map_type {
272 LIBBPF_MAP_UNSPEC,
273 LIBBPF_MAP_DATA,
274 LIBBPF_MAP_BSS,
275 LIBBPF_MAP_RODATA,
Andrii Nakryiko81bfdd02019-12-18 16:28:34 -0800276 LIBBPF_MAP_KCONFIG,
Daniel Borkmannd8599002019-04-09 23:20:13 +0200277};
278
279static const char * const libbpf_type_to_btf_name[] = {
Andrii Nakryikoac9d1382019-12-13 17:47:07 -0800280 [LIBBPF_MAP_DATA] = DATA_SEC,
281 [LIBBPF_MAP_BSS] = BSS_SEC,
282 [LIBBPF_MAP_RODATA] = RODATA_SEC,
Andrii Nakryiko81bfdd02019-12-18 16:28:34 -0800283 [LIBBPF_MAP_KCONFIG] = KCONFIG_SEC,
Daniel Borkmannd8599002019-04-09 23:20:13 +0200284};
285
Wang Nan9d759a92015-11-27 08:47:35 +0000286struct bpf_map {
Wang Nan561bbcc2015-11-27 08:47:36 +0000287 char *name;
Andrii Nakryiko01af3bf2019-12-13 17:43:32 -0800288 int fd;
Andrii Nakryikodb488142019-06-17 12:26:54 -0700289 int sec_idx;
290 size_t sec_offset;
David Beckettf0307a72018-05-16 14:02:49 -0700291 int map_ifindex;
Nikita V. Shirokovaddb9fc2018-11-20 20:55:56 -0800292 int inner_map_fd;
Wang Nan9d759a92015-11-27 08:47:35 +0000293 struct bpf_map_def def;
Martin KaFai Lau5b891af2018-07-24 08:40:21 -0700294 __u32 btf_key_type_id;
295 __u32 btf_value_type_id;
Martin KaFai Lau590a0082020-01-08 16:35:14 -0800296 __u32 btf_vmlinux_value_type_id;
Wang Nan9d759a92015-11-27 08:47:35 +0000297 void *priv;
298 bpf_map_clear_priv_t clear_priv;
Daniel Borkmannd8599002019-04-09 23:20:13 +0200299 enum libbpf_map_type libbpf_type;
Andrii Nakryikoeba9c5f2019-12-13 17:43:33 -0800300 void *mmaped;
Martin KaFai Lau590a0082020-01-08 16:35:14 -0800301 struct bpf_struct_ops *st_ops;
Toke Høiland-Jørgensen4580b252019-11-02 12:09:38 +0100302 char *pin_path;
303 bool pinned;
Toke Høiland-Jørgensenec6d5f472019-11-09 21:37:27 +0100304 bool reused;
Daniel Borkmannd8599002019-04-09 23:20:13 +0200305};
306
Andrii Nakryiko166750b2019-12-13 17:47:08 -0800307enum extern_type {
308 EXT_UNKNOWN,
309 EXT_CHAR,
310 EXT_BOOL,
311 EXT_INT,
312 EXT_TRISTATE,
313 EXT_CHAR_ARR,
314};
315
316struct extern_desc {
317 const char *name;
318 int sym_idx;
319 int btf_id;
320 enum extern_type type;
321 int sz;
322 int align;
323 int data_off;
324 bool is_signed;
325 bool is_weak;
326 bool is_set;
327};
328
Wang Nan9a208ef2015-07-01 02:14:10 +0000329static LIST_HEAD(bpf_objects_list);
330
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000331struct bpf_object {
Daniel Borkmannd8599002019-04-09 23:20:13 +0200332 char name[BPF_OBJ_NAME_LEN];
Wang Nancb1e5e92015-07-01 02:13:57 +0000333 char license[64];
Yonghong Song438363c2018-10-09 16:14:47 -0700334 __u32 kern_version;
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000335
Wang Nana5b8bd42015-07-01 02:14:00 +0000336 struct bpf_program *programs;
337 size_t nr_programs;
Wang Nan9d759a92015-11-27 08:47:35 +0000338 struct bpf_map *maps;
339 size_t nr_maps;
Andrii Nakryikobf829272019-06-17 12:26:53 -0700340 size_t maps_cap;
Wang Nan9d759a92015-11-27 08:47:35 +0000341
Andrii Nakryiko8601fd42019-12-18 16:28:35 -0800342 char *kconfig;
Andrii Nakryiko166750b2019-12-13 17:47:08 -0800343 struct extern_desc *externs;
344 int nr_extern;
Andrii Nakryiko81bfdd02019-12-18 16:28:34 -0800345 int kconfig_map_idx;
Andrii Nakryiko166750b2019-12-13 17:47:08 -0800346
Wang Nan52d33522015-07-01 02:14:04 +0000347 bool loaded;
Jakub Kicinski9a94f272018-06-28 14:41:38 -0700348 bool has_pseudo_calls;
Wang Nana5b8bd42015-07-01 02:14:00 +0000349
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000350 /*
351 * Information when doing elf related work. Only valid if fd
352 * is valid.
353 */
354 struct {
355 int fd;
Andrii Nakryiko5e61f272019-10-04 15:40:34 -0700356 const void *obj_buf;
Wang Nan6c956392015-07-01 02:13:54 +0000357 size_t obj_buf_sz;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000358 Elf *elf;
359 GElf_Ehdr ehdr;
Wang Nanbec7d682015-07-01 02:13:59 +0000360 Elf_Data *symbols;
Daniel Borkmannd8599002019-04-09 23:20:13 +0200361 Elf_Data *data;
362 Elf_Data *rodata;
363 Elf_Data *bss;
Martin KaFai Lau590a0082020-01-08 16:35:14 -0800364 Elf_Data *st_ops_data;
Wang Nan77ba9a52015-12-08 02:25:30 +0000365 size_t strtabidx;
Wang Nanb62f06e2015-07-01 02:14:01 +0000366 struct {
367 GElf_Shdr shdr;
368 Elf_Data *data;
Andrii Nakryiko1f8e2bc2019-11-20 23:07:41 -0800369 } *reloc_sects;
370 int nr_reloc_sects;
Wang Nan666810e2016-01-25 09:55:49 +0000371 int maps_shndx;
Andrii Nakryikoabd29c92019-06-17 12:26:56 -0700372 int btf_maps_shndx;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800373 int text_shndx;
Andrii Nakryiko166750b2019-12-13 17:47:08 -0800374 int symbols_shndx;
Daniel Borkmannd8599002019-04-09 23:20:13 +0200375 int data_shndx;
376 int rodata_shndx;
377 int bss_shndx;
Martin KaFai Lau590a0082020-01-08 16:35:14 -0800378 int st_ops_shndx;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000379 } efile;
Wang Nan9a208ef2015-07-01 02:14:10 +0000380 /*
381 * All loaded bpf_object is linked in a list, which is
382 * hidden to caller. bpf_objects__<func> handlers deal with
383 * all objects.
384 */
385 struct list_head list;
Wang Nan10931d22016-11-26 07:03:26 +0000386
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700387 struct btf *btf;
KP Singha6ed02c2020-01-17 22:28:25 +0100388 /* Parse and load BTF vmlinux if any of the programs in the object need
389 * it at load time.
390 */
391 struct btf *btf_vmlinux;
Yonghong Song2993e052018-11-19 15:29:16 -0800392 struct btf_ext *btf_ext;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700393
Wang Nan10931d22016-11-26 07:03:26 +0000394 void *priv;
395 bpf_object_clear_priv_t clear_priv;
396
Stanislav Fomichev47eff612018-11-20 17:11:19 -0800397 struct bpf_capabilities caps;
398
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000399 char path[];
400};
401#define obj_elf_valid(o) ((o)->efile.elf)
402
Joe Stringer29cd77f2018-10-02 13:35:39 -0700403void bpf_program__unload(struct bpf_program *prog)
Wang Nan55cffde2015-07-01 02:14:07 +0000404{
Wang Nanb5805632015-11-16 12:10:09 +0000405 int i;
406
Wang Nan55cffde2015-07-01 02:14:07 +0000407 if (!prog)
408 return;
409
Wang Nanb5805632015-11-16 12:10:09 +0000410 /*
411 * If the object is opened but the program was never loaded,
412 * it is possible that prog->instances.nr == -1.
413 */
414 if (prog->instances.nr > 0) {
415 for (i = 0; i < prog->instances.nr; i++)
416 zclose(prog->instances.fds[i]);
417 } else if (prog->instances.nr != -1) {
Kefeng Wangbe180102019-10-21 13:55:32 +0800418 pr_warn("Internal error: instances.nr is %d\n",
419 prog->instances.nr);
Wang Nanb5805632015-11-16 12:10:09 +0000420 }
421
422 prog->instances.nr = -1;
423 zfree(&prog->instances.fds);
Yonghong Song2993e052018-11-19 15:29:16 -0800424
Yonghong Song2993e052018-11-19 15:29:16 -0800425 zfree(&prog->func_info);
Prashant Bhole07a09d12018-12-17 16:57:50 +0900426 zfree(&prog->line_info);
Wang Nan55cffde2015-07-01 02:14:07 +0000427}
428
Wang Nana5b8bd42015-07-01 02:14:00 +0000429static void bpf_program__exit(struct bpf_program *prog)
430{
431 if (!prog)
432 return;
433
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000434 if (prog->clear_priv)
435 prog->clear_priv(prog, prog->priv);
436
437 prog->priv = NULL;
438 prog->clear_priv = NULL;
439
Wang Nan55cffde2015-07-01 02:14:07 +0000440 bpf_program__unload(prog);
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700441 zfree(&prog->name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000442 zfree(&prog->section_name);
Stanislav Fomichev33a2c752018-11-09 08:21:43 -0800443 zfree(&prog->pin_name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000444 zfree(&prog->insns);
Wang Nan34090912015-07-01 02:14:02 +0000445 zfree(&prog->reloc_desc);
446
447 prog->nr_reloc = 0;
Wang Nana5b8bd42015-07-01 02:14:00 +0000448 prog->insns_cnt = 0;
449 prog->idx = -1;
450}
451
Stanislav Fomichev33a2c752018-11-09 08:21:43 -0800452static char *__bpf_program__pin_name(struct bpf_program *prog)
453{
454 char *name, *p;
455
456 name = p = strdup(prog->section_name);
457 while ((p = strchr(p, '/')))
458 *p = '_';
459
460 return name;
461}
462
Wang Nana5b8bd42015-07-01 02:14:00 +0000463static int
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700464bpf_program__init(void *data, size_t size, char *section_name, int idx,
465 struct bpf_program *prog)
Wang Nana5b8bd42015-07-01 02:14:00 +0000466{
Andrii Nakryiko8ca990c2019-05-29 10:36:03 -0700467 const size_t bpf_insn_sz = sizeof(struct bpf_insn);
468
469 if (size == 0 || size % bpf_insn_sz) {
Kefeng Wangbe180102019-10-21 13:55:32 +0800470 pr_warn("corrupted section '%s', size: %zu\n",
471 section_name, size);
Wang Nana5b8bd42015-07-01 02:14:00 +0000472 return -EINVAL;
473 }
474
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -0800475 memset(prog, 0, sizeof(*prog));
Wang Nana5b8bd42015-07-01 02:14:00 +0000476
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700477 prog->section_name = strdup(section_name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000478 if (!prog->section_name) {
Kefeng Wangbe180102019-10-21 13:55:32 +0800479 pr_warn("failed to alloc name for prog under section(%d) %s\n",
480 idx, section_name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000481 goto errout;
482 }
483
Stanislav Fomichev33a2c752018-11-09 08:21:43 -0800484 prog->pin_name = __bpf_program__pin_name(prog);
485 if (!prog->pin_name) {
Kefeng Wangbe180102019-10-21 13:55:32 +0800486 pr_warn("failed to alloc pin name for prog under section(%d) %s\n",
487 idx, section_name);
Stanislav Fomichev33a2c752018-11-09 08:21:43 -0800488 goto errout;
489 }
490
Wang Nana5b8bd42015-07-01 02:14:00 +0000491 prog->insns = malloc(size);
492 if (!prog->insns) {
Kefeng Wangbe180102019-10-21 13:55:32 +0800493 pr_warn("failed to alloc insns for prog under section %s\n",
494 section_name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000495 goto errout;
496 }
Andrii Nakryiko8ca990c2019-05-29 10:36:03 -0700497 prog->insns_cnt = size / bpf_insn_sz;
498 memcpy(prog->insns, data, size);
Wang Nana5b8bd42015-07-01 02:14:00 +0000499 prog->idx = idx;
Wang Nanb5805632015-11-16 12:10:09 +0000500 prog->instances.fds = NULL;
501 prog->instances.nr = -1;
Nikita V. Shirokov47ae7e32018-11-23 12:58:12 -0800502 prog->type = BPF_PROG_TYPE_UNSPEC;
Wang Nana5b8bd42015-07-01 02:14:00 +0000503
504 return 0;
505errout:
506 bpf_program__exit(prog);
507 return -ENOMEM;
508}
509
510static int
511bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700512 char *section_name, int idx)
Wang Nana5b8bd42015-07-01 02:14:00 +0000513{
514 struct bpf_program prog, *progs;
515 int nr_progs, err;
516
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700517 err = bpf_program__init(data, size, section_name, idx, &prog);
Wang Nana5b8bd42015-07-01 02:14:00 +0000518 if (err)
519 return err;
520
Stanislav Fomichev47eff612018-11-20 17:11:19 -0800521 prog.caps = &obj->caps;
Wang Nana5b8bd42015-07-01 02:14:00 +0000522 progs = obj->programs;
523 nr_progs = obj->nr_programs;
524
Jakub Kicinski531b0142018-07-10 14:43:05 -0700525 progs = reallocarray(progs, nr_progs + 1, sizeof(progs[0]));
Wang Nana5b8bd42015-07-01 02:14:00 +0000526 if (!progs) {
527 /*
528 * In this case the original obj->programs
529 * is still valid, so don't need special treat for
530 * bpf_close_object().
531 */
Kefeng Wangbe180102019-10-21 13:55:32 +0800532 pr_warn("failed to alloc a new program under section '%s'\n",
533 section_name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000534 bpf_program__exit(&prog);
535 return -ENOMEM;
536 }
537
538 pr_debug("found program %s\n", prog.section_name);
539 obj->programs = progs;
540 obj->nr_programs = nr_progs + 1;
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000541 prog.obj = obj;
Wang Nana5b8bd42015-07-01 02:14:00 +0000542 progs[nr_progs] = prog;
543 return 0;
544}
545
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700546static int
547bpf_object__init_prog_names(struct bpf_object *obj)
548{
549 Elf_Data *symbols = obj->efile.symbols;
550 struct bpf_program *prog;
551 size_t pi, si;
552
553 for (pi = 0; pi < obj->nr_programs; pi++) {
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800554 const char *name = NULL;
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700555
556 prog = &obj->programs[pi];
557
558 for (si = 0; si < symbols->d_size / sizeof(GElf_Sym) && !name;
559 si++) {
560 GElf_Sym sym;
561
562 if (!gelf_getsym(symbols, si, &sym))
563 continue;
564 if (sym.st_shndx != prog->idx)
565 continue;
Roman Gushchinfe4d44b2017-12-13 15:18:52 +0000566 if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL)
567 continue;
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700568
569 name = elf_strptr(obj->efile.elf,
570 obj->efile.strtabidx,
571 sym.st_name);
572 if (!name) {
Kefeng Wangbe180102019-10-21 13:55:32 +0800573 pr_warn("failed to get sym name string for prog %s\n",
574 prog->section_name);
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700575 return -LIBBPF_ERRNO__LIBELF;
576 }
577 }
578
Jakub Kicinski9a94f272018-06-28 14:41:38 -0700579 if (!name && prog->idx == obj->efile.text_shndx)
580 name = ".text";
581
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700582 if (!name) {
Kefeng Wangbe180102019-10-21 13:55:32 +0800583 pr_warn("failed to find sym for prog %s\n",
584 prog->section_name);
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700585 return -EINVAL;
586 }
Jakub Kicinski9a94f272018-06-28 14:41:38 -0700587
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700588 prog->name = strdup(name);
589 if (!prog->name) {
Kefeng Wangbe180102019-10-21 13:55:32 +0800590 pr_warn("failed to allocate memory for prog sym %s\n",
591 name);
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700592 return -ENOMEM;
593 }
594 }
595
596 return 0;
597}
598
Andrii Nakryiko5e61f272019-10-04 15:40:34 -0700599static __u32 get_kernel_version(void)
600{
601 __u32 major, minor, patch;
602 struct utsname info;
603
604 uname(&info);
605 if (sscanf(info.release, "%u.%u.%u", &major, &minor, &patch) != 3)
606 return 0;
607 return KERNEL_VERSION(major, minor, patch);
608}
609
Martin KaFai Lau590a0082020-01-08 16:35:14 -0800610static const struct btf_member *
611find_member_by_offset(const struct btf_type *t, __u32 bit_offset)
612{
613 struct btf_member *m;
614 int i;
615
616 for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) {
617 if (btf_member_bit_offset(t, i) == bit_offset)
618 return m;
619 }
620
621 return NULL;
622}
623
624static const struct btf_member *
625find_member_by_name(const struct btf *btf, const struct btf_type *t,
626 const char *name)
627{
628 struct btf_member *m;
629 int i;
630
631 for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) {
632 if (!strcmp(btf__name_by_offset(btf, m->name_off), name))
633 return m;
634 }
635
636 return NULL;
637}
638
639#define STRUCT_OPS_VALUE_PREFIX "bpf_struct_ops_"
KP Singha6ed02c2020-01-17 22:28:25 +0100640static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix,
641 const char *name, __u32 kind);
Martin KaFai Lau590a0082020-01-08 16:35:14 -0800642
643static int
644find_struct_ops_kern_types(const struct btf *btf, const char *tname,
645 const struct btf_type **type, __u32 *type_id,
646 const struct btf_type **vtype, __u32 *vtype_id,
647 const struct btf_member **data_member)
648{
649 const struct btf_type *kern_type, *kern_vtype;
650 const struct btf_member *kern_data_member;
651 __s32 kern_vtype_id, kern_type_id;
Martin KaFai Lau590a0082020-01-08 16:35:14 -0800652 __u32 i;
653
654 kern_type_id = btf__find_by_name_kind(btf, tname, BTF_KIND_STRUCT);
655 if (kern_type_id < 0) {
656 pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n",
657 tname);
658 return kern_type_id;
659 }
660 kern_type = btf__type_by_id(btf, kern_type_id);
661
662 /* Find the corresponding "map_value" type that will be used
663 * in map_update(BPF_MAP_TYPE_STRUCT_OPS). For example,
664 * find "struct bpf_struct_ops_tcp_congestion_ops" from the
665 * btf_vmlinux.
666 */
KP Singha6ed02c2020-01-17 22:28:25 +0100667 kern_vtype_id = find_btf_by_prefix_kind(btf, STRUCT_OPS_VALUE_PREFIX,
668 tname, BTF_KIND_STRUCT);
Martin KaFai Lau590a0082020-01-08 16:35:14 -0800669 if (kern_vtype_id < 0) {
KP Singha6ed02c2020-01-17 22:28:25 +0100670 pr_warn("struct_ops init_kern: struct %s%s is not found in kernel BTF\n",
671 STRUCT_OPS_VALUE_PREFIX, tname);
Martin KaFai Lau590a0082020-01-08 16:35:14 -0800672 return kern_vtype_id;
673 }
674 kern_vtype = btf__type_by_id(btf, kern_vtype_id);
675
676 /* Find "struct tcp_congestion_ops" from
677 * struct bpf_struct_ops_tcp_congestion_ops {
678 * [ ... ]
679 * struct tcp_congestion_ops data;
680 * }
681 */
682 kern_data_member = btf_members(kern_vtype);
683 for (i = 0; i < btf_vlen(kern_vtype); i++, kern_data_member++) {
684 if (kern_data_member->type == kern_type_id)
685 break;
686 }
687 if (i == btf_vlen(kern_vtype)) {
KP Singha6ed02c2020-01-17 22:28:25 +0100688 pr_warn("struct_ops init_kern: struct %s data is not found in struct %s%s\n",
689 tname, STRUCT_OPS_VALUE_PREFIX, tname);
Martin KaFai Lau590a0082020-01-08 16:35:14 -0800690 return -EINVAL;
691 }
692
693 *type = kern_type;
694 *type_id = kern_type_id;
695 *vtype = kern_vtype;
696 *vtype_id = kern_vtype_id;
697 *data_member = kern_data_member;
698
699 return 0;
700}
701
702static bool bpf_map__is_struct_ops(const struct bpf_map *map)
703{
704 return map->def.type == BPF_MAP_TYPE_STRUCT_OPS;
705}
706
707/* Init the map's fields that depend on kern_btf */
708static int bpf_map__init_kern_struct_ops(struct bpf_map *map,
709 const struct btf *btf,
710 const struct btf *kern_btf)
711{
712 const struct btf_member *member, *kern_member, *kern_data_member;
713 const struct btf_type *type, *kern_type, *kern_vtype;
714 __u32 i, kern_type_id, kern_vtype_id, kern_data_off;
715 struct bpf_struct_ops *st_ops;
716 void *data, *kern_data;
717 const char *tname;
718 int err;
719
720 st_ops = map->st_ops;
721 type = st_ops->type;
722 tname = st_ops->tname;
723 err = find_struct_ops_kern_types(kern_btf, tname,
724 &kern_type, &kern_type_id,
725 &kern_vtype, &kern_vtype_id,
726 &kern_data_member);
727 if (err)
728 return err;
729
730 pr_debug("struct_ops init_kern %s: type_id:%u kern_type_id:%u kern_vtype_id:%u\n",
731 map->name, st_ops->type_id, kern_type_id, kern_vtype_id);
732
733 map->def.value_size = kern_vtype->size;
734 map->btf_vmlinux_value_type_id = kern_vtype_id;
735
736 st_ops->kern_vdata = calloc(1, kern_vtype->size);
737 if (!st_ops->kern_vdata)
738 return -ENOMEM;
739
740 data = st_ops->data;
741 kern_data_off = kern_data_member->offset / 8;
742 kern_data = st_ops->kern_vdata + kern_data_off;
743
744 member = btf_members(type);
745 for (i = 0; i < btf_vlen(type); i++, member++) {
746 const struct btf_type *mtype, *kern_mtype;
747 __u32 mtype_id, kern_mtype_id;
748 void *mdata, *kern_mdata;
749 __s64 msize, kern_msize;
750 __u32 moff, kern_moff;
751 __u32 kern_member_idx;
752 const char *mname;
753
754 mname = btf__name_by_offset(btf, member->name_off);
755 kern_member = find_member_by_name(kern_btf, kern_type, mname);
756 if (!kern_member) {
757 pr_warn("struct_ops init_kern %s: Cannot find member %s in kernel BTF\n",
758 map->name, mname);
759 return -ENOTSUP;
760 }
761
762 kern_member_idx = kern_member - btf_members(kern_type);
763 if (btf_member_bitfield_size(type, i) ||
764 btf_member_bitfield_size(kern_type, kern_member_idx)) {
765 pr_warn("struct_ops init_kern %s: bitfield %s is not supported\n",
766 map->name, mname);
767 return -ENOTSUP;
768 }
769
770 moff = member->offset / 8;
771 kern_moff = kern_member->offset / 8;
772
773 mdata = data + moff;
774 kern_mdata = kern_data + kern_moff;
775
776 mtype = skip_mods_and_typedefs(btf, member->type, &mtype_id);
777 kern_mtype = skip_mods_and_typedefs(kern_btf, kern_member->type,
778 &kern_mtype_id);
779 if (BTF_INFO_KIND(mtype->info) !=
780 BTF_INFO_KIND(kern_mtype->info)) {
781 pr_warn("struct_ops init_kern %s: Unmatched member type %s %u != %u(kernel)\n",
782 map->name, mname, BTF_INFO_KIND(mtype->info),
783 BTF_INFO_KIND(kern_mtype->info));
784 return -ENOTSUP;
785 }
786
787 if (btf_is_ptr(mtype)) {
788 struct bpf_program *prog;
789
790 mtype = skip_mods_and_typedefs(btf, mtype->type, &mtype_id);
791 kern_mtype = skip_mods_and_typedefs(kern_btf,
792 kern_mtype->type,
793 &kern_mtype_id);
794 if (!btf_is_func_proto(mtype) ||
795 !btf_is_func_proto(kern_mtype)) {
796 pr_warn("struct_ops init_kern %s: non func ptr %s is not supported\n",
797 map->name, mname);
798 return -ENOTSUP;
799 }
800
801 prog = st_ops->progs[i];
802 if (!prog) {
803 pr_debug("struct_ops init_kern %s: func ptr %s is not set\n",
804 map->name, mname);
805 continue;
806 }
807
808 prog->attach_btf_id = kern_type_id;
809 prog->expected_attach_type = kern_member_idx;
810
811 st_ops->kern_func_off[i] = kern_data_off + kern_moff;
812
813 pr_debug("struct_ops init_kern %s: func ptr %s is set to prog %s from data(+%u) to kern_data(+%u)\n",
814 map->name, mname, prog->name, moff,
815 kern_moff);
816
817 continue;
818 }
819
820 msize = btf__resolve_size(btf, mtype_id);
821 kern_msize = btf__resolve_size(kern_btf, kern_mtype_id);
822 if (msize < 0 || kern_msize < 0 || msize != kern_msize) {
823 pr_warn("struct_ops init_kern %s: Error in size of member %s: %zd != %zd(kernel)\n",
824 map->name, mname, (ssize_t)msize,
825 (ssize_t)kern_msize);
826 return -ENOTSUP;
827 }
828
829 pr_debug("struct_ops init_kern %s: copy %s %u bytes from data(+%u) to kern_data(+%u)\n",
830 map->name, mname, (unsigned int)msize,
831 moff, kern_moff);
832 memcpy(kern_mdata, mdata, msize);
833 }
834
835 return 0;
836}
837
838static int bpf_object__init_kern_struct_ops_maps(struct bpf_object *obj)
839{
Martin KaFai Lau590a0082020-01-08 16:35:14 -0800840 struct bpf_map *map;
841 size_t i;
842 int err;
843
844 for (i = 0; i < obj->nr_maps; i++) {
845 map = &obj->maps[i];
846
847 if (!bpf_map__is_struct_ops(map))
848 continue;
849
KP Singha6ed02c2020-01-17 22:28:25 +0100850 err = bpf_map__init_kern_struct_ops(map, obj->btf,
851 obj->btf_vmlinux);
852 if (err)
Martin KaFai Lau590a0082020-01-08 16:35:14 -0800853 return err;
Martin KaFai Lau590a0082020-01-08 16:35:14 -0800854 }
855
Martin KaFai Lau590a0082020-01-08 16:35:14 -0800856 return 0;
857}
858
859static int bpf_object__init_struct_ops_maps(struct bpf_object *obj)
860{
861 const struct btf_type *type, *datasec;
862 const struct btf_var_secinfo *vsi;
863 struct bpf_struct_ops *st_ops;
864 const char *tname, *var_name;
865 __s32 type_id, datasec_id;
866 const struct btf *btf;
867 struct bpf_map *map;
868 __u32 i;
869
870 if (obj->efile.st_ops_shndx == -1)
871 return 0;
872
873 btf = obj->btf;
874 datasec_id = btf__find_by_name_kind(btf, STRUCT_OPS_SEC,
875 BTF_KIND_DATASEC);
876 if (datasec_id < 0) {
877 pr_warn("struct_ops init: DATASEC %s not found\n",
878 STRUCT_OPS_SEC);
879 return -EINVAL;
880 }
881
882 datasec = btf__type_by_id(btf, datasec_id);
883 vsi = btf_var_secinfos(datasec);
884 for (i = 0; i < btf_vlen(datasec); i++, vsi++) {
885 type = btf__type_by_id(obj->btf, vsi->type);
886 var_name = btf__name_by_offset(obj->btf, type->name_off);
887
888 type_id = btf__resolve_type(obj->btf, vsi->type);
889 if (type_id < 0) {
890 pr_warn("struct_ops init: Cannot resolve var type_id %u in DATASEC %s\n",
891 vsi->type, STRUCT_OPS_SEC);
892 return -EINVAL;
893 }
894
895 type = btf__type_by_id(obj->btf, type_id);
896 tname = btf__name_by_offset(obj->btf, type->name_off);
897 if (!tname[0]) {
898 pr_warn("struct_ops init: anonymous type is not supported\n");
899 return -ENOTSUP;
900 }
901 if (!btf_is_struct(type)) {
902 pr_warn("struct_ops init: %s is not a struct\n", tname);
903 return -EINVAL;
904 }
905
906 map = bpf_object__add_map(obj);
907 if (IS_ERR(map))
908 return PTR_ERR(map);
909
910 map->sec_idx = obj->efile.st_ops_shndx;
911 map->sec_offset = vsi->offset;
912 map->name = strdup(var_name);
913 if (!map->name)
914 return -ENOMEM;
915
916 map->def.type = BPF_MAP_TYPE_STRUCT_OPS;
917 map->def.key_size = sizeof(int);
918 map->def.value_size = type->size;
919 map->def.max_entries = 1;
920
921 map->st_ops = calloc(1, sizeof(*map->st_ops));
922 if (!map->st_ops)
923 return -ENOMEM;
924 st_ops = map->st_ops;
925 st_ops->data = malloc(type->size);
926 st_ops->progs = calloc(btf_vlen(type), sizeof(*st_ops->progs));
927 st_ops->kern_func_off = malloc(btf_vlen(type) *
928 sizeof(*st_ops->kern_func_off));
929 if (!st_ops->data || !st_ops->progs || !st_ops->kern_func_off)
930 return -ENOMEM;
931
932 if (vsi->offset + type->size > obj->efile.st_ops_data->d_size) {
933 pr_warn("struct_ops init: var %s is beyond the end of DATASEC %s\n",
934 var_name, STRUCT_OPS_SEC);
935 return -EINVAL;
936 }
937
938 memcpy(st_ops->data,
939 obj->efile.st_ops_data->d_buf + vsi->offset,
940 type->size);
941 st_ops->tname = tname;
942 st_ops->type = type;
943 st_ops->type_id = type_id;
944
945 pr_debug("struct_ops init: struct %s(type_id=%u) %s found at offset %u\n",
946 tname, type_id, var_name, vsi->offset);
947 }
948
949 return 0;
950}
951
Wang Nan6c956392015-07-01 02:13:54 +0000952static struct bpf_object *bpf_object__new(const char *path,
Andrii Nakryiko5e61f272019-10-04 15:40:34 -0700953 const void *obj_buf,
Andrii Nakryiko2ce84502019-10-04 15:40:35 -0700954 size_t obj_buf_sz,
955 const char *obj_name)
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000956{
957 struct bpf_object *obj;
Daniel Borkmannd8599002019-04-09 23:20:13 +0200958 char *end;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000959
960 obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
961 if (!obj) {
Kefeng Wangbe180102019-10-21 13:55:32 +0800962 pr_warn("alloc memory failed for %s\n", path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000963 return ERR_PTR(-ENOMEM);
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000964 }
965
966 strcpy(obj->path, path);
Andrii Nakryiko2ce84502019-10-04 15:40:35 -0700967 if (obj_name) {
968 strncpy(obj->name, obj_name, sizeof(obj->name) - 1);
969 obj->name[sizeof(obj->name) - 1] = 0;
970 } else {
971 /* Using basename() GNU version which doesn't modify arg. */
972 strncpy(obj->name, basename((void *)path),
973 sizeof(obj->name) - 1);
974 end = strchr(obj->name, '.');
975 if (end)
976 *end = 0;
977 }
Wang Nan6c956392015-07-01 02:13:54 +0000978
Daniel Borkmannd8599002019-04-09 23:20:13 +0200979 obj->efile.fd = -1;
Wang Nan6c956392015-07-01 02:13:54 +0000980 /*
Andrii Nakryiko76e10222019-05-29 10:36:10 -0700981 * Caller of this function should also call
Wang Nan6c956392015-07-01 02:13:54 +0000982 * bpf_object__elf_finish() after data collection to return
983 * obj_buf to user. If not, we should duplicate the buffer to
984 * avoid user freeing them before elf finish.
985 */
986 obj->efile.obj_buf = obj_buf;
987 obj->efile.obj_buf_sz = obj_buf_sz;
Wang Nan666810e2016-01-25 09:55:49 +0000988 obj->efile.maps_shndx = -1;
Andrii Nakryikoabd29c92019-06-17 12:26:56 -0700989 obj->efile.btf_maps_shndx = -1;
Daniel Borkmannd8599002019-04-09 23:20:13 +0200990 obj->efile.data_shndx = -1;
991 obj->efile.rodata_shndx = -1;
992 obj->efile.bss_shndx = -1;
Martin KaFai Lau590a0082020-01-08 16:35:14 -0800993 obj->efile.st_ops_shndx = -1;
Andrii Nakryiko81bfdd02019-12-18 16:28:34 -0800994 obj->kconfig_map_idx = -1;
Wang Nan6c956392015-07-01 02:13:54 +0000995
Andrii Nakryiko5e61f272019-10-04 15:40:34 -0700996 obj->kern_version = get_kernel_version();
Wang Nan52d33522015-07-01 02:14:04 +0000997 obj->loaded = false;
Wang Nan9a208ef2015-07-01 02:14:10 +0000998
999 INIT_LIST_HEAD(&obj->list);
1000 list_add(&obj->list, &bpf_objects_list);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001001 return obj;
1002}
1003
1004static void bpf_object__elf_finish(struct bpf_object *obj)
1005{
1006 if (!obj_elf_valid(obj))
1007 return;
1008
1009 if (obj->efile.elf) {
1010 elf_end(obj->efile.elf);
1011 obj->efile.elf = NULL;
1012 }
Wang Nanbec7d682015-07-01 02:13:59 +00001013 obj->efile.symbols = NULL;
Daniel Borkmannd8599002019-04-09 23:20:13 +02001014 obj->efile.data = NULL;
1015 obj->efile.rodata = NULL;
1016 obj->efile.bss = NULL;
Martin KaFai Lau590a0082020-01-08 16:35:14 -08001017 obj->efile.st_ops_data = NULL;
Wang Nanb62f06e2015-07-01 02:14:01 +00001018
Andrii Nakryiko1f8e2bc2019-11-20 23:07:41 -08001019 zfree(&obj->efile.reloc_sects);
1020 obj->efile.nr_reloc_sects = 0;
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001021 zclose(obj->efile.fd);
Wang Nan6c956392015-07-01 02:13:54 +00001022 obj->efile.obj_buf = NULL;
1023 obj->efile.obj_buf_sz = 0;
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001024}
1025
1026static int bpf_object__elf_init(struct bpf_object *obj)
1027{
1028 int err = 0;
1029 GElf_Ehdr *ep;
1030
1031 if (obj_elf_valid(obj)) {
Kefeng Wangbe180102019-10-21 13:55:32 +08001032 pr_warn("elf init: internal error\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +00001033 return -LIBBPF_ERRNO__LIBELF;
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001034 }
1035
Wang Nan6c956392015-07-01 02:13:54 +00001036 if (obj->efile.obj_buf_sz > 0) {
1037 /*
1038 * obj_buf should have been validated by
1039 * bpf_object__open_buffer().
1040 */
Andrii Nakryiko5e61f272019-10-04 15:40:34 -07001041 obj->efile.elf = elf_memory((char *)obj->efile.obj_buf,
Wang Nan6c956392015-07-01 02:13:54 +00001042 obj->efile.obj_buf_sz);
1043 } else {
1044 obj->efile.fd = open(obj->path, O_RDONLY);
1045 if (obj->efile.fd < 0) {
Andrii Nakryikobe5c5d42019-05-29 10:36:04 -07001046 char errmsg[STRERR_BUFSIZE], *cp;
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02001047
Andrii Nakryikobe5c5d42019-05-29 10:36:04 -07001048 err = -errno;
1049 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
Kefeng Wangbe180102019-10-21 13:55:32 +08001050 pr_warn("failed to open %s: %s\n", obj->path, cp);
Andrii Nakryikobe5c5d42019-05-29 10:36:04 -07001051 return err;
Wang Nan6c956392015-07-01 02:13:54 +00001052 }
1053
1054 obj->efile.elf = elf_begin(obj->efile.fd,
Andrii Nakryiko76e10222019-05-29 10:36:10 -07001055 LIBBPF_ELF_C_READ_MMAP, NULL);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001056 }
1057
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001058 if (!obj->efile.elf) {
Kefeng Wangbe180102019-10-21 13:55:32 +08001059 pr_warn("failed to open %s as ELF file\n", obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001060 err = -LIBBPF_ERRNO__LIBELF;
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001061 goto errout;
1062 }
1063
1064 if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) {
Kefeng Wangbe180102019-10-21 13:55:32 +08001065 pr_warn("failed to get EHDR from %s\n", obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001066 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001067 goto errout;
1068 }
1069 ep = &obj->efile.ehdr;
1070
Wang Nan9b161372016-07-18 06:01:08 +00001071 /* Old LLVM set e_machine to EM_NONE */
Andrii Nakryiko76e10222019-05-29 10:36:10 -07001072 if (ep->e_type != ET_REL ||
1073 (ep->e_machine && ep->e_machine != EM_BPF)) {
Kefeng Wangbe180102019-10-21 13:55:32 +08001074 pr_warn("%s is not an eBPF object file\n", obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001075 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001076 goto errout;
1077 }
1078
1079 return 0;
1080errout:
1081 bpf_object__elf_finish(obj);
1082 return err;
1083}
1084
Andrii Nakryiko12ef5632019-05-29 10:36:05 -07001085static int bpf_object__check_endianness(struct bpf_object *obj)
Wang Nancc4228d2015-07-01 02:13:55 +00001086{
Arnaldo Carvalho de Melocdb2f922019-07-19 11:34:06 -03001087#if __BYTE_ORDER == __LITTLE_ENDIAN
Andrii Nakryiko12ef5632019-05-29 10:36:05 -07001088 if (obj->efile.ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
1089 return 0;
Arnaldo Carvalho de Melocdb2f922019-07-19 11:34:06 -03001090#elif __BYTE_ORDER == __BIG_ENDIAN
Andrii Nakryiko12ef5632019-05-29 10:36:05 -07001091 if (obj->efile.ehdr.e_ident[EI_DATA] == ELFDATA2MSB)
1092 return 0;
1093#else
1094# error "Unrecognized __BYTE_ORDER__"
1095#endif
Kefeng Wangbe180102019-10-21 13:55:32 +08001096 pr_warn("endianness mismatch.\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +00001097 return -LIBBPF_ERRNO__ENDIAN;
Wang Nancc4228d2015-07-01 02:13:55 +00001098}
1099
Wang Nancb1e5e92015-07-01 02:13:57 +00001100static int
Andrii Nakryiko399dc652019-05-29 10:36:11 -07001101bpf_object__init_license(struct bpf_object *obj, void *data, size_t size)
Wang Nancb1e5e92015-07-01 02:13:57 +00001102{
Andrii Nakryiko399dc652019-05-29 10:36:11 -07001103 memcpy(obj->license, data, min(size, sizeof(obj->license) - 1));
Wang Nancb1e5e92015-07-01 02:13:57 +00001104 pr_debug("license of %s is %s\n", obj->path, obj->license);
1105 return 0;
1106}
1107
John Fastabend54b86252019-10-18 07:41:26 -07001108static int
1109bpf_object__init_kversion(struct bpf_object *obj, void *data, size_t size)
1110{
1111 __u32 kver;
1112
1113 if (size != sizeof(kver)) {
Kefeng Wangbe180102019-10-21 13:55:32 +08001114 pr_warn("invalid kver section in %s\n", obj->path);
John Fastabend54b86252019-10-18 07:41:26 -07001115 return -LIBBPF_ERRNO__FORMAT;
1116 }
1117 memcpy(&kver, data, sizeof(kver));
1118 obj->kern_version = kver;
1119 pr_debug("kernel version of %s is %x\n", obj->path, obj->kern_version);
1120 return 0;
1121}
1122
Nikita V. Shirokovaddb9fc2018-11-20 20:55:56 -08001123static bool bpf_map_type__is_map_in_map(enum bpf_map_type type)
1124{
1125 if (type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
1126 type == BPF_MAP_TYPE_HASH_OF_MAPS)
1127 return true;
1128 return false;
1129}
1130
Daniel Borkmann1713d682019-04-09 23:20:14 +02001131static int bpf_object_search_section_size(const struct bpf_object *obj,
1132 const char *name, size_t *d_size)
1133{
1134 const GElf_Ehdr *ep = &obj->efile.ehdr;
1135 Elf *elf = obj->efile.elf;
1136 Elf_Scn *scn = NULL;
1137 int idx = 0;
1138
1139 while ((scn = elf_nextscn(elf, scn)) != NULL) {
1140 const char *sec_name;
1141 Elf_Data *data;
1142 GElf_Shdr sh;
1143
1144 idx++;
1145 if (gelf_getshdr(scn, &sh) != &sh) {
Kefeng Wangbe180102019-10-21 13:55:32 +08001146 pr_warn("failed to get section(%d) header from %s\n",
1147 idx, obj->path);
Daniel Borkmann1713d682019-04-09 23:20:14 +02001148 return -EIO;
1149 }
1150
1151 sec_name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
1152 if (!sec_name) {
Kefeng Wangbe180102019-10-21 13:55:32 +08001153 pr_warn("failed to get section(%d) name from %s\n",
1154 idx, obj->path);
Daniel Borkmann1713d682019-04-09 23:20:14 +02001155 return -EIO;
1156 }
1157
1158 if (strcmp(name, sec_name))
1159 continue;
1160
1161 data = elf_getdata(scn, 0);
1162 if (!data) {
Kefeng Wangbe180102019-10-21 13:55:32 +08001163 pr_warn("failed to get section(%d) data from %s(%s)\n",
1164 idx, name, obj->path);
Daniel Borkmann1713d682019-04-09 23:20:14 +02001165 return -EIO;
1166 }
1167
1168 *d_size = data->d_size;
1169 return 0;
1170 }
1171
1172 return -ENOENT;
1173}
1174
1175int bpf_object__section_size(const struct bpf_object *obj, const char *name,
1176 __u32 *size)
1177{
1178 int ret = -ENOENT;
1179 size_t d_size;
1180
1181 *size = 0;
1182 if (!name) {
1183 return -EINVAL;
Andrii Nakryikoac9d1382019-12-13 17:47:07 -08001184 } else if (!strcmp(name, DATA_SEC)) {
Daniel Borkmann1713d682019-04-09 23:20:14 +02001185 if (obj->efile.data)
1186 *size = obj->efile.data->d_size;
Andrii Nakryikoac9d1382019-12-13 17:47:07 -08001187 } else if (!strcmp(name, BSS_SEC)) {
Daniel Borkmann1713d682019-04-09 23:20:14 +02001188 if (obj->efile.bss)
1189 *size = obj->efile.bss->d_size;
Andrii Nakryikoac9d1382019-12-13 17:47:07 -08001190 } else if (!strcmp(name, RODATA_SEC)) {
Daniel Borkmann1713d682019-04-09 23:20:14 +02001191 if (obj->efile.rodata)
1192 *size = obj->efile.rodata->d_size;
Martin KaFai Lau590a0082020-01-08 16:35:14 -08001193 } else if (!strcmp(name, STRUCT_OPS_SEC)) {
1194 if (obj->efile.st_ops_data)
1195 *size = obj->efile.st_ops_data->d_size;
Daniel Borkmann1713d682019-04-09 23:20:14 +02001196 } else {
1197 ret = bpf_object_search_section_size(obj, name, &d_size);
1198 if (!ret)
1199 *size = d_size;
1200 }
1201
1202 return *size ? 0 : ret;
1203}
1204
1205int bpf_object__variable_offset(const struct bpf_object *obj, const char *name,
1206 __u32 *off)
1207{
1208 Elf_Data *symbols = obj->efile.symbols;
1209 const char *sname;
1210 size_t si;
1211
1212 if (!name || !off)
1213 return -EINVAL;
1214
1215 for (si = 0; si < symbols->d_size / sizeof(GElf_Sym); si++) {
1216 GElf_Sym sym;
1217
1218 if (!gelf_getsym(symbols, si, &sym))
1219 continue;
1220 if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL ||
1221 GELF_ST_TYPE(sym.st_info) != STT_OBJECT)
1222 continue;
1223
1224 sname = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
1225 sym.st_name);
1226 if (!sname) {
Kefeng Wangbe180102019-10-21 13:55:32 +08001227 pr_warn("failed to get sym name string for var %s\n",
1228 name);
Daniel Borkmann1713d682019-04-09 23:20:14 +02001229 return -EIO;
1230 }
1231 if (strcmp(name, sname) == 0) {
1232 *off = sym.st_value;
1233 return 0;
1234 }
1235 }
1236
1237 return -ENOENT;
1238}
1239
Andrii Nakryikobf829272019-06-17 12:26:53 -07001240static struct bpf_map *bpf_object__add_map(struct bpf_object *obj)
Daniel Borkmannd8599002019-04-09 23:20:13 +02001241{
Andrii Nakryikobf829272019-06-17 12:26:53 -07001242 struct bpf_map *new_maps;
1243 size_t new_cap;
1244 int i;
1245
1246 if (obj->nr_maps < obj->maps_cap)
1247 return &obj->maps[obj->nr_maps++];
1248
Ivan Khoronzhuk95064972019-06-26 13:38:37 +03001249 new_cap = max((size_t)4, obj->maps_cap * 3 / 2);
Andrii Nakryikobf829272019-06-17 12:26:53 -07001250 new_maps = realloc(obj->maps, new_cap * sizeof(*obj->maps));
1251 if (!new_maps) {
Kefeng Wangbe180102019-10-21 13:55:32 +08001252 pr_warn("alloc maps for object failed\n");
Andrii Nakryikobf829272019-06-17 12:26:53 -07001253 return ERR_PTR(-ENOMEM);
1254 }
1255
1256 obj->maps_cap = new_cap;
1257 obj->maps = new_maps;
1258
1259 /* zero out new maps */
1260 memset(obj->maps + obj->nr_maps, 0,
1261 (obj->maps_cap - obj->nr_maps) * sizeof(*obj->maps));
1262 /*
1263 * fill all fd with -1 so won't close incorrect fd (fd=0 is stdin)
1264 * when failure (zclose won't close negative fd)).
1265 */
1266 for (i = obj->nr_maps; i < obj->maps_cap; i++) {
1267 obj->maps[i].fd = -1;
1268 obj->maps[i].inner_map_fd = -1;
1269 }
1270
1271 return &obj->maps[obj->nr_maps++];
Daniel Borkmannd8599002019-04-09 23:20:13 +02001272}
1273
Andrii Nakryikoeba9c5f2019-12-13 17:43:33 -08001274static size_t bpf_map_mmap_sz(const struct bpf_map *map)
1275{
1276 long page_sz = sysconf(_SC_PAGE_SIZE);
1277 size_t map_sz;
1278
Andrii Nakryikoc7019172020-01-16 22:08:00 -08001279 map_sz = (size_t)roundup(map->def.value_size, 8) * map->def.max_entries;
Andrii Nakryikoeba9c5f2019-12-13 17:43:33 -08001280 map_sz = roundup(map_sz, page_sz);
1281 return map_sz;
1282}
1283
Andrii Nakryiko81bfdd02019-12-18 16:28:34 -08001284static char *internal_map_name(struct bpf_object *obj,
1285 enum libbpf_map_type type)
1286{
Toke Høiland-Jørgensen113e6b72020-02-17 18:17:01 +01001287 char map_name[BPF_OBJ_NAME_LEN], *p;
Andrii Nakryiko81bfdd02019-12-18 16:28:34 -08001288 const char *sfx = libbpf_type_to_btf_name[type];
1289 int sfx_len = max((size_t)7, strlen(sfx));
1290 int pfx_len = min((size_t)BPF_OBJ_NAME_LEN - sfx_len - 1,
1291 strlen(obj->name));
1292
1293 snprintf(map_name, sizeof(map_name), "%.*s%.*s", pfx_len, obj->name,
1294 sfx_len, libbpf_type_to_btf_name[type]);
1295
Toke Høiland-Jørgensen113e6b72020-02-17 18:17:01 +01001296 /* sanitise map name to characters allowed by kernel */
1297 for (p = map_name; *p && p < map_name + sizeof(map_name); p++)
1298 if (!isalnum(*p) && *p != '_' && *p != '.')
1299 *p = '_';
1300
Andrii Nakryiko81bfdd02019-12-18 16:28:34 -08001301 return strdup(map_name);
1302}
1303
Daniel Borkmannd8599002019-04-09 23:20:13 +02001304static int
Andrii Nakryikobf829272019-06-17 12:26:53 -07001305bpf_object__init_internal_map(struct bpf_object *obj, enum libbpf_map_type type,
Andrii Nakryikoeba9c5f2019-12-13 17:43:33 -08001306 int sec_idx, void *data, size_t data_sz)
Daniel Borkmannd8599002019-04-09 23:20:13 +02001307{
Andrii Nakryikobf829272019-06-17 12:26:53 -07001308 struct bpf_map_def *def;
1309 struct bpf_map *map;
Andrii Nakryikoeba9c5f2019-12-13 17:43:33 -08001310 int err;
Andrii Nakryikobf829272019-06-17 12:26:53 -07001311
1312 map = bpf_object__add_map(obj);
1313 if (IS_ERR(map))
1314 return PTR_ERR(map);
Daniel Borkmannd8599002019-04-09 23:20:13 +02001315
1316 map->libbpf_type = type;
Andrii Nakryikodb488142019-06-17 12:26:54 -07001317 map->sec_idx = sec_idx;
1318 map->sec_offset = 0;
Andrii Nakryiko81bfdd02019-12-18 16:28:34 -08001319 map->name = internal_map_name(obj, type);
Daniel Borkmannd8599002019-04-09 23:20:13 +02001320 if (!map->name) {
Kefeng Wangbe180102019-10-21 13:55:32 +08001321 pr_warn("failed to alloc map name\n");
Daniel Borkmannd8599002019-04-09 23:20:13 +02001322 return -ENOMEM;
1323 }
1324
Andrii Nakryikobf829272019-06-17 12:26:53 -07001325 def = &map->def;
Daniel Borkmannd8599002019-04-09 23:20:13 +02001326 def->type = BPF_MAP_TYPE_ARRAY;
1327 def->key_size = sizeof(int);
Andrii Nakryikoeba9c5f2019-12-13 17:43:33 -08001328 def->value_size = data_sz;
Daniel Borkmannd8599002019-04-09 23:20:13 +02001329 def->max_entries = 1;
Andrii Nakryiko81bfdd02019-12-18 16:28:34 -08001330 def->map_flags = type == LIBBPF_MAP_RODATA || type == LIBBPF_MAP_KCONFIG
Andrii Nakryiko166750b2019-12-13 17:47:08 -08001331 ? BPF_F_RDONLY_PROG : 0;
Andrii Nakryiko0d13bfc2019-12-13 17:43:25 -08001332 def->map_flags |= BPF_F_MMAPABLE;
Andrii Nakryiko7fe74b42019-11-17 09:28:05 -08001333
1334 pr_debug("map '%s' (global data): at sec_idx %d, offset %zu, flags %x.\n",
Andrii Nakryiko81bfdd02019-12-18 16:28:34 -08001335 map->name, map->sec_idx, map->sec_offset, def->map_flags);
Andrii Nakryiko7fe74b42019-11-17 09:28:05 -08001336
Andrii Nakryikoeba9c5f2019-12-13 17:43:33 -08001337 map->mmaped = mmap(NULL, bpf_map_mmap_sz(map), PROT_READ | PROT_WRITE,
1338 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1339 if (map->mmaped == MAP_FAILED) {
1340 err = -errno;
1341 map->mmaped = NULL;
1342 pr_warn("failed to alloc map '%s' content buffer: %d\n",
1343 map->name, err);
1344 zfree(&map->name);
1345 return err;
Daniel Borkmannd8599002019-04-09 23:20:13 +02001346 }
1347
Andrii Nakryiko166750b2019-12-13 17:47:08 -08001348 if (data)
Andrii Nakryikoeba9c5f2019-12-13 17:43:33 -08001349 memcpy(map->mmaped, data, data_sz);
1350
Andrii Nakryikoe1d1dc42019-04-16 11:47:17 -07001351 pr_debug("map %td is \"%s\"\n", map - obj->maps, map->name);
Daniel Borkmannd8599002019-04-09 23:20:13 +02001352 return 0;
1353}
1354
Andrii Nakryikobf829272019-06-17 12:26:53 -07001355static int bpf_object__init_global_data_maps(struct bpf_object *obj)
Eric Leblond4708bbd2016-11-15 04:05:47 +00001356{
Andrii Nakryikobf829272019-06-17 12:26:53 -07001357 int err;
1358
Andrii Nakryikobf829272019-06-17 12:26:53 -07001359 /*
1360 * Populate obj->maps with libbpf internal maps.
1361 */
1362 if (obj->efile.data_shndx >= 0) {
1363 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_DATA,
Andrii Nakryikodb488142019-06-17 12:26:54 -07001364 obj->efile.data_shndx,
Andrii Nakryikoeba9c5f2019-12-13 17:43:33 -08001365 obj->efile.data->d_buf,
1366 obj->efile.data->d_size);
Andrii Nakryikobf829272019-06-17 12:26:53 -07001367 if (err)
1368 return err;
1369 }
1370 if (obj->efile.rodata_shndx >= 0) {
1371 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_RODATA,
Andrii Nakryikodb488142019-06-17 12:26:54 -07001372 obj->efile.rodata_shndx,
Andrii Nakryikoeba9c5f2019-12-13 17:43:33 -08001373 obj->efile.rodata->d_buf,
1374 obj->efile.rodata->d_size);
Andrii Nakryikobf829272019-06-17 12:26:53 -07001375 if (err)
1376 return err;
1377 }
1378 if (obj->efile.bss_shndx >= 0) {
1379 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_BSS,
Andrii Nakryikodb488142019-06-17 12:26:54 -07001380 obj->efile.bss_shndx,
Andrii Nakryikoeba9c5f2019-12-13 17:43:33 -08001381 NULL,
1382 obj->efile.bss->d_size);
Andrii Nakryikobf829272019-06-17 12:26:53 -07001383 if (err)
1384 return err;
1385 }
1386 return 0;
1387}
1388
Andrii Nakryiko166750b2019-12-13 17:47:08 -08001389
1390static struct extern_desc *find_extern_by_name(const struct bpf_object *obj,
1391 const void *name)
1392{
1393 int i;
1394
1395 for (i = 0; i < obj->nr_extern; i++) {
1396 if (strcmp(obj->externs[i].name, name) == 0)
1397 return &obj->externs[i];
1398 }
1399 return NULL;
1400}
1401
1402static int set_ext_value_tri(struct extern_desc *ext, void *ext_val,
1403 char value)
1404{
1405 switch (ext->type) {
1406 case EXT_BOOL:
1407 if (value == 'm') {
1408 pr_warn("extern %s=%c should be tristate or char\n",
1409 ext->name, value);
1410 return -EINVAL;
1411 }
1412 *(bool *)ext_val = value == 'y' ? true : false;
1413 break;
1414 case EXT_TRISTATE:
1415 if (value == 'y')
1416 *(enum libbpf_tristate *)ext_val = TRI_YES;
1417 else if (value == 'm')
1418 *(enum libbpf_tristate *)ext_val = TRI_MODULE;
1419 else /* value == 'n' */
1420 *(enum libbpf_tristate *)ext_val = TRI_NO;
1421 break;
1422 case EXT_CHAR:
1423 *(char *)ext_val = value;
1424 break;
1425 case EXT_UNKNOWN:
1426 case EXT_INT:
1427 case EXT_CHAR_ARR:
1428 default:
1429 pr_warn("extern %s=%c should be bool, tristate, or char\n",
1430 ext->name, value);
1431 return -EINVAL;
1432 }
1433 ext->is_set = true;
1434 return 0;
1435}
1436
1437static int set_ext_value_str(struct extern_desc *ext, char *ext_val,
1438 const char *value)
1439{
1440 size_t len;
1441
1442 if (ext->type != EXT_CHAR_ARR) {
1443 pr_warn("extern %s=%s should char array\n", ext->name, value);
1444 return -EINVAL;
1445 }
1446
1447 len = strlen(value);
1448 if (value[len - 1] != '"') {
1449 pr_warn("extern '%s': invalid string config '%s'\n",
1450 ext->name, value);
1451 return -EINVAL;
1452 }
1453
1454 /* strip quotes */
1455 len -= 2;
1456 if (len >= ext->sz) {
1457 pr_warn("extern '%s': long string config %s of (%zu bytes) truncated to %d bytes\n",
1458 ext->name, value, len, ext->sz - 1);
1459 len = ext->sz - 1;
1460 }
1461 memcpy(ext_val, value + 1, len);
1462 ext_val[len] = '\0';
1463 ext->is_set = true;
1464 return 0;
1465}
1466
1467static int parse_u64(const char *value, __u64 *res)
1468{
1469 char *value_end;
1470 int err;
1471
1472 errno = 0;
1473 *res = strtoull(value, &value_end, 0);
1474 if (errno) {
1475 err = -errno;
1476 pr_warn("failed to parse '%s' as integer: %d\n", value, err);
1477 return err;
1478 }
1479 if (*value_end) {
1480 pr_warn("failed to parse '%s' as integer completely\n", value);
1481 return -EINVAL;
1482 }
1483 return 0;
1484}
1485
1486static bool is_ext_value_in_range(const struct extern_desc *ext, __u64 v)
1487{
1488 int bit_sz = ext->sz * 8;
1489
1490 if (ext->sz == 8)
1491 return true;
1492
1493 /* Validate that value stored in u64 fits in integer of `ext->sz`
1494 * bytes size without any loss of information. If the target integer
1495 * is signed, we rely on the following limits of integer type of
1496 * Y bits and subsequent transformation:
1497 *
1498 * -2^(Y-1) <= X <= 2^(Y-1) - 1
1499 * 0 <= X + 2^(Y-1) <= 2^Y - 1
1500 * 0 <= X + 2^(Y-1) < 2^Y
1501 *
1502 * For unsigned target integer, check that all the (64 - Y) bits are
1503 * zero.
1504 */
1505 if (ext->is_signed)
1506 return v + (1ULL << (bit_sz - 1)) < (1ULL << bit_sz);
1507 else
1508 return (v >> bit_sz) == 0;
1509}
1510
1511static int set_ext_value_num(struct extern_desc *ext, void *ext_val,
1512 __u64 value)
1513{
1514 if (ext->type != EXT_INT && ext->type != EXT_CHAR) {
1515 pr_warn("extern %s=%llu should be integer\n",
Andrii Nakryiko7745ff92019-12-18 21:21:03 -08001516 ext->name, (unsigned long long)value);
Andrii Nakryiko166750b2019-12-13 17:47:08 -08001517 return -EINVAL;
1518 }
1519 if (!is_ext_value_in_range(ext, value)) {
1520 pr_warn("extern %s=%llu value doesn't fit in %d bytes\n",
Andrii Nakryiko7745ff92019-12-18 21:21:03 -08001521 ext->name, (unsigned long long)value, ext->sz);
Andrii Nakryiko166750b2019-12-13 17:47:08 -08001522 return -ERANGE;
1523 }
1524 switch (ext->sz) {
1525 case 1: *(__u8 *)ext_val = value; break;
1526 case 2: *(__u16 *)ext_val = value; break;
1527 case 4: *(__u32 *)ext_val = value; break;
1528 case 8: *(__u64 *)ext_val = value; break;
1529 default:
1530 return -EINVAL;
1531 }
1532 ext->is_set = true;
1533 return 0;
1534}
1535
Andrii Nakryiko8601fd42019-12-18 16:28:35 -08001536static int bpf_object__process_kconfig_line(struct bpf_object *obj,
1537 char *buf, void *data)
Andrii Nakryiko166750b2019-12-13 17:47:08 -08001538{
Andrii Nakryiko166750b2019-12-13 17:47:08 -08001539 struct extern_desc *ext;
Andrii Nakryiko8601fd42019-12-18 16:28:35 -08001540 char *sep, *value;
Andrii Nakryiko166750b2019-12-13 17:47:08 -08001541 int len, err = 0;
1542 void *ext_val;
1543 __u64 num;
Andrii Nakryiko8601fd42019-12-18 16:28:35 -08001544
1545 if (strncmp(buf, "CONFIG_", 7))
1546 return 0;
1547
1548 sep = strchr(buf, '=');
1549 if (!sep) {
1550 pr_warn("failed to parse '%s': no separator\n", buf);
1551 return -EINVAL;
1552 }
1553
1554 /* Trim ending '\n' */
1555 len = strlen(buf);
1556 if (buf[len - 1] == '\n')
1557 buf[len - 1] = '\0';
1558 /* Split on '=' and ensure that a value is present. */
1559 *sep = '\0';
1560 if (!sep[1]) {
1561 *sep = '=';
1562 pr_warn("failed to parse '%s': no value\n", buf);
1563 return -EINVAL;
1564 }
1565
1566 ext = find_extern_by_name(obj, buf);
1567 if (!ext || ext->is_set)
1568 return 0;
1569
1570 ext_val = data + ext->data_off;
1571 value = sep + 1;
1572
1573 switch (*value) {
1574 case 'y': case 'n': case 'm':
1575 err = set_ext_value_tri(ext, ext_val, *value);
1576 break;
1577 case '"':
1578 err = set_ext_value_str(ext, ext_val, value);
1579 break;
1580 default:
1581 /* assume integer */
1582 err = parse_u64(value, &num);
1583 if (err) {
1584 pr_warn("extern %s=%s should be integer\n",
1585 ext->name, value);
1586 return err;
1587 }
1588 err = set_ext_value_num(ext, ext_val, num);
1589 break;
1590 }
1591 if (err)
1592 return err;
1593 pr_debug("extern %s=%s\n", ext->name, value);
1594 return 0;
1595}
1596
1597static int bpf_object__read_kconfig_file(struct bpf_object *obj, void *data)
1598{
1599 char buf[PATH_MAX];
1600 struct utsname uts;
1601 int len, err = 0;
Andrii Nakryiko166750b2019-12-13 17:47:08 -08001602 gzFile file;
1603
Andrii Nakryiko8601fd42019-12-18 16:28:35 -08001604 uname(&uts);
1605 len = snprintf(buf, PATH_MAX, "/boot/config-%s", uts.release);
1606 if (len < 0)
1607 return -EINVAL;
1608 else if (len >= PATH_MAX)
1609 return -ENAMETOOLONG;
Andrii Nakryiko166750b2019-12-13 17:47:08 -08001610
Andrii Nakryiko8601fd42019-12-18 16:28:35 -08001611 /* gzopen also accepts uncompressed files. */
1612 file = gzopen(buf, "r");
1613 if (!file)
1614 file = gzopen("/proc/config.gz", "r");
1615
Andrii Nakryiko166750b2019-12-13 17:47:08 -08001616 if (!file) {
Andrii Nakryiko8601fd42019-12-18 16:28:35 -08001617 pr_warn("failed to open system Kconfig\n");
Andrii Nakryiko166750b2019-12-13 17:47:08 -08001618 return -ENOENT;
1619 }
1620
1621 while (gzgets(file, buf, sizeof(buf))) {
Andrii Nakryiko8601fd42019-12-18 16:28:35 -08001622 err = bpf_object__process_kconfig_line(obj, buf, data);
1623 if (err) {
1624 pr_warn("error parsing system Kconfig line '%s': %d\n",
1625 buf, err);
Andrii Nakryiko166750b2019-12-13 17:47:08 -08001626 goto out;
1627 }
Andrii Nakryiko166750b2019-12-13 17:47:08 -08001628 }
1629
1630out:
1631 gzclose(file);
1632 return err;
1633}
1634
Andrii Nakryiko8601fd42019-12-18 16:28:35 -08001635static int bpf_object__read_kconfig_mem(struct bpf_object *obj,
1636 const char *config, void *data)
1637{
1638 char buf[PATH_MAX];
1639 int err = 0;
1640 FILE *file;
1641
1642 file = fmemopen((void *)config, strlen(config), "r");
1643 if (!file) {
1644 err = -errno;
1645 pr_warn("failed to open in-memory Kconfig: %d\n", err);
1646 return err;
1647 }
1648
1649 while (fgets(buf, sizeof(buf), file)) {
1650 err = bpf_object__process_kconfig_line(obj, buf, data);
1651 if (err) {
1652 pr_warn("error parsing in-memory Kconfig line '%s': %d\n",
1653 buf, err);
1654 break;
1655 }
1656 }
1657
1658 fclose(file);
1659 return err;
1660}
1661
Andrii Nakryiko81bfdd02019-12-18 16:28:34 -08001662static int bpf_object__init_kconfig_map(struct bpf_object *obj)
Andrii Nakryiko166750b2019-12-13 17:47:08 -08001663{
1664 struct extern_desc *last_ext;
1665 size_t map_sz;
1666 int err;
1667
1668 if (obj->nr_extern == 0)
1669 return 0;
1670
1671 last_ext = &obj->externs[obj->nr_extern - 1];
1672 map_sz = last_ext->data_off + last_ext->sz;
1673
Andrii Nakryiko81bfdd02019-12-18 16:28:34 -08001674 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_KCONFIG,
Andrii Nakryiko166750b2019-12-13 17:47:08 -08001675 obj->efile.symbols_shndx,
1676 NULL, map_sz);
1677 if (err)
1678 return err;
1679
Andrii Nakryiko81bfdd02019-12-18 16:28:34 -08001680 obj->kconfig_map_idx = obj->nr_maps - 1;
Andrii Nakryiko166750b2019-12-13 17:47:08 -08001681
1682 return 0;
1683}
1684
Andrii Nakryikobf829272019-06-17 12:26:53 -07001685static int bpf_object__init_user_maps(struct bpf_object *obj, bool strict)
1686{
Eric Leblond4708bbd2016-11-15 04:05:47 +00001687 Elf_Data *symbols = obj->efile.symbols;
Andrii Nakryikobf829272019-06-17 12:26:53 -07001688 int i, map_def_sz = 0, nr_maps = 0, nr_syms;
Daniel Borkmannd8599002019-04-09 23:20:13 +02001689 Elf_Data *data = NULL;
Andrii Nakryikobf829272019-06-17 12:26:53 -07001690 Elf_Scn *scn;
1691
1692 if (obj->efile.maps_shndx < 0)
1693 return 0;
Eric Leblond4708bbd2016-11-15 04:05:47 +00001694
Eric Leblond4708bbd2016-11-15 04:05:47 +00001695 if (!symbols)
1696 return -EINVAL;
1697
Andrii Nakryikobf829272019-06-17 12:26:53 -07001698 scn = elf_getscn(obj->efile.elf, obj->efile.maps_shndx);
1699 if (scn)
1700 data = elf_getdata(scn, NULL);
1701 if (!scn || !data) {
Kefeng Wangbe180102019-10-21 13:55:32 +08001702 pr_warn("failed to get Elf_Data from map section %d\n",
1703 obj->efile.maps_shndx);
Andrii Nakryikobf829272019-06-17 12:26:53 -07001704 return -EINVAL;
Wang Nan0b3d1ef2015-07-01 02:13:58 +00001705 }
1706
Eric Leblond4708bbd2016-11-15 04:05:47 +00001707 /*
1708 * Count number of maps. Each map has a name.
1709 * Array of maps is not supported: only the first element is
1710 * considered.
1711 *
1712 * TODO: Detect array of map and report error.
1713 */
Andrii Nakryikobf829272019-06-17 12:26:53 -07001714 nr_syms = symbols->d_size / sizeof(GElf_Sym);
1715 for (i = 0; i < nr_syms; i++) {
Eric Leblond4708bbd2016-11-15 04:05:47 +00001716 GElf_Sym sym;
1717
1718 if (!gelf_getsym(symbols, i, &sym))
1719 continue;
1720 if (sym.st_shndx != obj->efile.maps_shndx)
1721 continue;
1722 nr_maps++;
1723 }
Craig Gallekb13c5c12017-10-05 10:41:57 -04001724 /* Assume equally sized map definitions */
Andrii Nakryikobf829272019-06-17 12:26:53 -07001725 pr_debug("maps in %s: %d maps in %zd bytes\n",
1726 obj->path, nr_maps, data->d_size);
Daniel Borkmann4f8827d2019-04-24 00:45:57 +02001727
Andrii Nakryiko98e527af2019-11-06 18:08:55 -08001728 if (!data->d_size || nr_maps == 0 || (data->d_size % nr_maps) != 0) {
Andrii Nakryiko8983b732019-11-20 23:07:42 -08001729 pr_warn("unable to determine map definition size section %s, %d maps in %zd bytes\n",
Kefeng Wangbe180102019-10-21 13:55:32 +08001730 obj->path, nr_maps, data->d_size);
Andrii Nakryikobf829272019-06-17 12:26:53 -07001731 return -EINVAL;
Craig Gallekb13c5c12017-10-05 10:41:57 -04001732 }
Andrii Nakryiko98e527af2019-11-06 18:08:55 -08001733 map_def_sz = data->d_size / nr_maps;
Craig Gallekb13c5c12017-10-05 10:41:57 -04001734
Andrii Nakryikobf829272019-06-17 12:26:53 -07001735 /* Fill obj->maps using data in "maps" section. */
1736 for (i = 0; i < nr_syms; i++) {
Wang Nan561bbcc2015-11-27 08:47:36 +00001737 GElf_Sym sym;
Wang Nan561bbcc2015-11-27 08:47:36 +00001738 const char *map_name;
Eric Leblond4708bbd2016-11-15 04:05:47 +00001739 struct bpf_map_def *def;
Andrii Nakryikobf829272019-06-17 12:26:53 -07001740 struct bpf_map *map;
Wang Nan561bbcc2015-11-27 08:47:36 +00001741
1742 if (!gelf_getsym(symbols, i, &sym))
1743 continue;
Wang Nan666810e2016-01-25 09:55:49 +00001744 if (sym.st_shndx != obj->efile.maps_shndx)
Wang Nan561bbcc2015-11-27 08:47:36 +00001745 continue;
1746
Andrii Nakryikobf829272019-06-17 12:26:53 -07001747 map = bpf_object__add_map(obj);
1748 if (IS_ERR(map))
1749 return PTR_ERR(map);
1750
1751 map_name = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
Wang Nan561bbcc2015-11-27 08:47:36 +00001752 sym.st_name);
Andrii Nakryikoc51829b2019-05-29 10:36:06 -07001753 if (!map_name) {
Kefeng Wangbe180102019-10-21 13:55:32 +08001754 pr_warn("failed to get map #%d name sym string for obj %s\n",
1755 i, obj->path);
Andrii Nakryikoc51829b2019-05-29 10:36:06 -07001756 return -LIBBPF_ERRNO__FORMAT;
1757 }
Daniel Borkmannd8599002019-04-09 23:20:13 +02001758
Andrii Nakryikobf829272019-06-17 12:26:53 -07001759 map->libbpf_type = LIBBPF_MAP_UNSPEC;
Andrii Nakryikodb488142019-06-17 12:26:54 -07001760 map->sec_idx = sym.st_shndx;
1761 map->sec_offset = sym.st_value;
1762 pr_debug("map '%s' (legacy): at sec_idx %d, offset %zu.\n",
1763 map_name, map->sec_idx, map->sec_offset);
Craig Gallekb13c5c12017-10-05 10:41:57 -04001764 if (sym.st_value + map_def_sz > data->d_size) {
Kefeng Wangbe180102019-10-21 13:55:32 +08001765 pr_warn("corrupted maps section in %s: last map \"%s\" too small\n",
1766 obj->path, map_name);
Eric Leblond4708bbd2016-11-15 04:05:47 +00001767 return -EINVAL;
Wang Nan561bbcc2015-11-27 08:47:36 +00001768 }
Eric Leblond4708bbd2016-11-15 04:05:47 +00001769
Andrii Nakryikobf829272019-06-17 12:26:53 -07001770 map->name = strdup(map_name);
1771 if (!map->name) {
Kefeng Wangbe180102019-10-21 13:55:32 +08001772 pr_warn("failed to alloc map name\n");
Wang Nan973170e2015-12-08 02:25:29 +00001773 return -ENOMEM;
1774 }
Andrii Nakryikobf829272019-06-17 12:26:53 -07001775 pr_debug("map %d is \"%s\"\n", i, map->name);
Eric Leblond4708bbd2016-11-15 04:05:47 +00001776 def = (struct bpf_map_def *)(data->d_buf + sym.st_value);
Craig Gallekb13c5c12017-10-05 10:41:57 -04001777 /*
1778 * If the definition of the map in the object file fits in
1779 * bpf_map_def, copy it. Any extra fields in our version
1780 * of bpf_map_def will default to zero as a result of the
1781 * calloc above.
1782 */
1783 if (map_def_sz <= sizeof(struct bpf_map_def)) {
Andrii Nakryikobf829272019-06-17 12:26:53 -07001784 memcpy(&map->def, def, map_def_sz);
Craig Gallekb13c5c12017-10-05 10:41:57 -04001785 } else {
1786 /*
1787 * Here the map structure being read is bigger than what
1788 * we expect, truncate if the excess bits are all zero.
1789 * If they are not zero, reject this map as
1790 * incompatible.
1791 */
1792 char *b;
Andrii Nakryiko8983b732019-11-20 23:07:42 -08001793
Craig Gallekb13c5c12017-10-05 10:41:57 -04001794 for (b = ((char *)def) + sizeof(struct bpf_map_def);
1795 b < ((char *)def) + map_def_sz; b++) {
1796 if (*b != 0) {
Andrii Nakryiko8983b732019-11-20 23:07:42 -08001797 pr_warn("maps section in %s: \"%s\" has unrecognized, non-zero options\n",
Kefeng Wangbe180102019-10-21 13:55:32 +08001798 obj->path, map_name);
John Fastabendc034a172018-10-15 11:19:55 -07001799 if (strict)
1800 return -EINVAL;
Craig Gallekb13c5c12017-10-05 10:41:57 -04001801 }
1802 }
Andrii Nakryikobf829272019-06-17 12:26:53 -07001803 memcpy(&map->def, def, sizeof(struct bpf_map_def));
Craig Gallekb13c5c12017-10-05 10:41:57 -04001804 }
Wang Nan561bbcc2015-11-27 08:47:36 +00001805 }
Andrii Nakryikobf829272019-06-17 12:26:53 -07001806 return 0;
1807}
Eric Leblond4708bbd2016-11-15 04:05:47 +00001808
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07001809static const struct btf_type *
1810skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id)
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001811{
1812 const struct btf_type *t = btf__type_by_id(btf, id);
1813
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07001814 if (res_id)
1815 *res_id = id;
1816
1817 while (btf_is_mod(t) || btf_is_typedef(t)) {
1818 if (res_id)
1819 *res_id = t->type;
1820 t = btf__type_by_id(btf, t->type);
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001821 }
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07001822
1823 return t;
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001824}
1825
Martin KaFai Lau590a0082020-01-08 16:35:14 -08001826static const struct btf_type *
1827resolve_func_ptr(const struct btf *btf, __u32 id, __u32 *res_id)
1828{
1829 const struct btf_type *t;
1830
1831 t = skip_mods_and_typedefs(btf, id, NULL);
1832 if (!btf_is_ptr(t))
1833 return NULL;
1834
1835 t = skip_mods_and_typedefs(btf, t->type, res_id);
1836
1837 return btf_is_func_proto(t) ? t : NULL;
1838}
1839
Andrii Nakryikoef99b022019-07-05 08:50:09 -07001840/*
1841 * Fetch integer attribute of BTF map definition. Such attributes are
1842 * represented using a pointer to an array, in which dimensionality of array
1843 * encodes specified integer value. E.g., int (*type)[BPF_MAP_TYPE_ARRAY];
1844 * encodes `type => BPF_MAP_TYPE_ARRAY` key/value pair completely using BTF
1845 * type definition, while using only sizeof(void *) space in ELF data section.
1846 */
1847static bool get_map_field_int(const char *map_name, const struct btf *btf,
Andrii Nakryiko8983b732019-11-20 23:07:42 -08001848 const struct btf_member *m, __u32 *res)
1849{
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07001850 const struct btf_type *t = skip_mods_and_typedefs(btf, m->type, NULL);
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001851 const char *name = btf__name_by_offset(btf, m->name_off);
Andrii Nakryikoef99b022019-07-05 08:50:09 -07001852 const struct btf_array *arr_info;
1853 const struct btf_type *arr_t;
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001854
Andrii Nakryikob03bc682019-08-07 14:39:49 -07001855 if (!btf_is_ptr(t)) {
Kefeng Wangbe180102019-10-21 13:55:32 +08001856 pr_warn("map '%s': attr '%s': expected PTR, got %u.\n",
1857 map_name, name, btf_kind(t));
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001858 return false;
1859 }
Eric Leblond4708bbd2016-11-15 04:05:47 +00001860
Andrii Nakryikoef99b022019-07-05 08:50:09 -07001861 arr_t = btf__type_by_id(btf, t->type);
1862 if (!arr_t) {
Kefeng Wangbe180102019-10-21 13:55:32 +08001863 pr_warn("map '%s': attr '%s': type [%u] not found.\n",
1864 map_name, name, t->type);
Andrii Nakryikoef99b022019-07-05 08:50:09 -07001865 return false;
1866 }
Andrii Nakryikob03bc682019-08-07 14:39:49 -07001867 if (!btf_is_array(arr_t)) {
Kefeng Wangbe180102019-10-21 13:55:32 +08001868 pr_warn("map '%s': attr '%s': expected ARRAY, got %u.\n",
1869 map_name, name, btf_kind(arr_t));
Andrii Nakryikoef99b022019-07-05 08:50:09 -07001870 return false;
1871 }
Andrii Nakryikob03bc682019-08-07 14:39:49 -07001872 arr_info = btf_array(arr_t);
Andrii Nakryikoef99b022019-07-05 08:50:09 -07001873 *res = arr_info->nelems;
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001874 return true;
1875}
Daniel Borkmann8837fe52019-04-24 00:45:56 +02001876
Toke Høiland-Jørgensen57a00f42019-11-02 12:09:41 +01001877static int build_map_pin_path(struct bpf_map *map, const char *path)
1878{
1879 char buf[PATH_MAX];
1880 int err, len;
1881
1882 if (!path)
1883 path = "/sys/fs/bpf";
1884
1885 len = snprintf(buf, PATH_MAX, "%s/%s", path, bpf_map__name(map));
1886 if (len < 0)
1887 return -EINVAL;
1888 else if (len >= PATH_MAX)
1889 return -ENAMETOOLONG;
1890
1891 err = bpf_map__set_pin_path(map, buf);
1892 if (err)
1893 return err;
1894
1895 return 0;
1896}
1897
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001898static int bpf_object__init_user_btf_map(struct bpf_object *obj,
1899 const struct btf_type *sec,
1900 int var_idx, int sec_idx,
Toke Høiland-Jørgensen57a00f42019-11-02 12:09:41 +01001901 const Elf_Data *data, bool strict,
1902 const char *pin_root_path)
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001903{
1904 const struct btf_type *var, *def, *t;
1905 const struct btf_var_secinfo *vi;
1906 const struct btf_var *var_extra;
1907 const struct btf_member *m;
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001908 const char *map_name;
1909 struct bpf_map *map;
1910 int vlen, i;
1911
Andrii Nakryikob03bc682019-08-07 14:39:49 -07001912 vi = btf_var_secinfos(sec) + var_idx;
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001913 var = btf__type_by_id(obj->btf, vi->type);
Andrii Nakryikob03bc682019-08-07 14:39:49 -07001914 var_extra = btf_var(var);
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001915 map_name = btf__name_by_offset(obj->btf, var->name_off);
Andrii Nakryikob03bc682019-08-07 14:39:49 -07001916 vlen = btf_vlen(var);
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001917
1918 if (map_name == NULL || map_name[0] == '\0') {
Kefeng Wangbe180102019-10-21 13:55:32 +08001919 pr_warn("map #%d: empty name.\n", var_idx);
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001920 return -EINVAL;
1921 }
1922 if ((__u64)vi->offset + vi->size > data->d_size) {
Kefeng Wangbe180102019-10-21 13:55:32 +08001923 pr_warn("map '%s' BTF data is corrupted.\n", map_name);
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001924 return -EINVAL;
1925 }
Andrii Nakryikob03bc682019-08-07 14:39:49 -07001926 if (!btf_is_var(var)) {
Kefeng Wangbe180102019-10-21 13:55:32 +08001927 pr_warn("map '%s': unexpected var kind %u.\n",
1928 map_name, btf_kind(var));
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001929 return -EINVAL;
1930 }
1931 if (var_extra->linkage != BTF_VAR_GLOBAL_ALLOCATED &&
1932 var_extra->linkage != BTF_VAR_STATIC) {
Kefeng Wangbe180102019-10-21 13:55:32 +08001933 pr_warn("map '%s': unsupported var linkage %u.\n",
1934 map_name, var_extra->linkage);
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001935 return -EOPNOTSUPP;
1936 }
1937
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07001938 def = skip_mods_and_typedefs(obj->btf, var->type, NULL);
Andrii Nakryikob03bc682019-08-07 14:39:49 -07001939 if (!btf_is_struct(def)) {
Kefeng Wangbe180102019-10-21 13:55:32 +08001940 pr_warn("map '%s': unexpected def kind %u.\n",
1941 map_name, btf_kind(var));
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001942 return -EINVAL;
1943 }
1944 if (def->size > vi->size) {
Kefeng Wangbe180102019-10-21 13:55:32 +08001945 pr_warn("map '%s': invalid def size.\n", map_name);
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001946 return -EINVAL;
1947 }
1948
1949 map = bpf_object__add_map(obj);
1950 if (IS_ERR(map))
1951 return PTR_ERR(map);
1952 map->name = strdup(map_name);
1953 if (!map->name) {
Kefeng Wangbe180102019-10-21 13:55:32 +08001954 pr_warn("map '%s': failed to alloc map name.\n", map_name);
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001955 return -ENOMEM;
1956 }
1957 map->libbpf_type = LIBBPF_MAP_UNSPEC;
1958 map->def.type = BPF_MAP_TYPE_UNSPEC;
1959 map->sec_idx = sec_idx;
1960 map->sec_offset = vi->offset;
1961 pr_debug("map '%s': at sec_idx %d, offset %zu.\n",
1962 map_name, map->sec_idx, map->sec_offset);
1963
Andrii Nakryikob03bc682019-08-07 14:39:49 -07001964 vlen = btf_vlen(def);
1965 m = btf_members(def);
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001966 for (i = 0; i < vlen; i++, m++) {
1967 const char *name = btf__name_by_offset(obj->btf, m->name_off);
1968
1969 if (!name) {
Kefeng Wangbe180102019-10-21 13:55:32 +08001970 pr_warn("map '%s': invalid field #%d.\n", map_name, i);
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001971 return -EINVAL;
1972 }
1973 if (strcmp(name, "type") == 0) {
Tobias Klauser9fc9aad2020-03-25 12:36:55 +01001974 if (!get_map_field_int(map_name, obj->btf, m,
Andrii Nakryikoef99b022019-07-05 08:50:09 -07001975 &map->def.type))
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001976 return -EINVAL;
1977 pr_debug("map '%s': found type = %u.\n",
1978 map_name, map->def.type);
1979 } else if (strcmp(name, "max_entries") == 0) {
Tobias Klauser9fc9aad2020-03-25 12:36:55 +01001980 if (!get_map_field_int(map_name, obj->btf, m,
Andrii Nakryikoef99b022019-07-05 08:50:09 -07001981 &map->def.max_entries))
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001982 return -EINVAL;
1983 pr_debug("map '%s': found max_entries = %u.\n",
1984 map_name, map->def.max_entries);
1985 } else if (strcmp(name, "map_flags") == 0) {
Tobias Klauser9fc9aad2020-03-25 12:36:55 +01001986 if (!get_map_field_int(map_name, obj->btf, m,
Andrii Nakryikoef99b022019-07-05 08:50:09 -07001987 &map->def.map_flags))
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001988 return -EINVAL;
1989 pr_debug("map '%s': found map_flags = %u.\n",
1990 map_name, map->def.map_flags);
1991 } else if (strcmp(name, "key_size") == 0) {
1992 __u32 sz;
1993
Tobias Klauser9fc9aad2020-03-25 12:36:55 +01001994 if (!get_map_field_int(map_name, obj->btf, m, &sz))
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001995 return -EINVAL;
1996 pr_debug("map '%s': found key_size = %u.\n",
1997 map_name, sz);
1998 if (map->def.key_size && map->def.key_size != sz) {
Kefeng Wangbe180102019-10-21 13:55:32 +08001999 pr_warn("map '%s': conflicting key size %u != %u.\n",
2000 map_name, map->def.key_size, sz);
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002001 return -EINVAL;
2002 }
2003 map->def.key_size = sz;
2004 } else if (strcmp(name, "key") == 0) {
2005 __s64 sz;
2006
2007 t = btf__type_by_id(obj->btf, m->type);
2008 if (!t) {
Kefeng Wangbe180102019-10-21 13:55:32 +08002009 pr_warn("map '%s': key type [%d] not found.\n",
2010 map_name, m->type);
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002011 return -EINVAL;
2012 }
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002013 if (!btf_is_ptr(t)) {
Kefeng Wangbe180102019-10-21 13:55:32 +08002014 pr_warn("map '%s': key spec is not PTR: %u.\n",
2015 map_name, btf_kind(t));
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002016 return -EINVAL;
2017 }
2018 sz = btf__resolve_size(obj->btf, t->type);
2019 if (sz < 0) {
Andrii Nakryiko679152d2019-12-12 09:19:18 -08002020 pr_warn("map '%s': can't determine key size for type [%u]: %zd.\n",
2021 map_name, t->type, (ssize_t)sz);
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002022 return sz;
2023 }
Andrii Nakryiko679152d2019-12-12 09:19:18 -08002024 pr_debug("map '%s': found key [%u], sz = %zd.\n",
2025 map_name, t->type, (ssize_t)sz);
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002026 if (map->def.key_size && map->def.key_size != sz) {
Andrii Nakryiko679152d2019-12-12 09:19:18 -08002027 pr_warn("map '%s': conflicting key size %u != %zd.\n",
2028 map_name, map->def.key_size, (ssize_t)sz);
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002029 return -EINVAL;
2030 }
2031 map->def.key_size = sz;
2032 map->btf_key_type_id = t->type;
2033 } else if (strcmp(name, "value_size") == 0) {
2034 __u32 sz;
2035
Tobias Klauser9fc9aad2020-03-25 12:36:55 +01002036 if (!get_map_field_int(map_name, obj->btf, m, &sz))
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002037 return -EINVAL;
2038 pr_debug("map '%s': found value_size = %u.\n",
2039 map_name, sz);
2040 if (map->def.value_size && map->def.value_size != sz) {
Kefeng Wangbe180102019-10-21 13:55:32 +08002041 pr_warn("map '%s': conflicting value size %u != %u.\n",
2042 map_name, map->def.value_size, sz);
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002043 return -EINVAL;
2044 }
2045 map->def.value_size = sz;
2046 } else if (strcmp(name, "value") == 0) {
2047 __s64 sz;
2048
2049 t = btf__type_by_id(obj->btf, m->type);
2050 if (!t) {
Kefeng Wangbe180102019-10-21 13:55:32 +08002051 pr_warn("map '%s': value type [%d] not found.\n",
2052 map_name, m->type);
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002053 return -EINVAL;
2054 }
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002055 if (!btf_is_ptr(t)) {
Kefeng Wangbe180102019-10-21 13:55:32 +08002056 pr_warn("map '%s': value spec is not PTR: %u.\n",
2057 map_name, btf_kind(t));
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002058 return -EINVAL;
2059 }
2060 sz = btf__resolve_size(obj->btf, t->type);
2061 if (sz < 0) {
Andrii Nakryiko679152d2019-12-12 09:19:18 -08002062 pr_warn("map '%s': can't determine value size for type [%u]: %zd.\n",
2063 map_name, t->type, (ssize_t)sz);
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002064 return sz;
2065 }
Andrii Nakryiko679152d2019-12-12 09:19:18 -08002066 pr_debug("map '%s': found value [%u], sz = %zd.\n",
2067 map_name, t->type, (ssize_t)sz);
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002068 if (map->def.value_size && map->def.value_size != sz) {
Andrii Nakryiko679152d2019-12-12 09:19:18 -08002069 pr_warn("map '%s': conflicting value size %u != %zd.\n",
2070 map_name, map->def.value_size, (ssize_t)sz);
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002071 return -EINVAL;
2072 }
2073 map->def.value_size = sz;
2074 map->btf_value_type_id = t->type;
Toke Høiland-Jørgensen57a00f42019-11-02 12:09:41 +01002075 } else if (strcmp(name, "pinning") == 0) {
2076 __u32 val;
2077 int err;
2078
Tobias Klauser9fc9aad2020-03-25 12:36:55 +01002079 if (!get_map_field_int(map_name, obj->btf, m, &val))
Toke Høiland-Jørgensen57a00f42019-11-02 12:09:41 +01002080 return -EINVAL;
2081 pr_debug("map '%s': found pinning = %u.\n",
2082 map_name, val);
2083
2084 if (val != LIBBPF_PIN_NONE &&
2085 val != LIBBPF_PIN_BY_NAME) {
2086 pr_warn("map '%s': invalid pinning value %u.\n",
2087 map_name, val);
2088 return -EINVAL;
2089 }
2090 if (val == LIBBPF_PIN_BY_NAME) {
2091 err = build_map_pin_path(map, pin_root_path);
2092 if (err) {
2093 pr_warn("map '%s': couldn't build pin path.\n",
2094 map_name);
2095 return err;
2096 }
2097 }
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002098 } else {
2099 if (strict) {
Kefeng Wangbe180102019-10-21 13:55:32 +08002100 pr_warn("map '%s': unknown field '%s'.\n",
2101 map_name, name);
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002102 return -ENOTSUP;
2103 }
2104 pr_debug("map '%s': ignoring unknown field '%s'.\n",
2105 map_name, name);
2106 }
2107 }
2108
2109 if (map->def.type == BPF_MAP_TYPE_UNSPEC) {
Kefeng Wangbe180102019-10-21 13:55:32 +08002110 pr_warn("map '%s': map type isn't specified.\n", map_name);
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002111 return -EINVAL;
2112 }
2113
2114 return 0;
2115}
2116
Toke Høiland-Jørgensen57a00f42019-11-02 12:09:41 +01002117static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict,
2118 const char *pin_root_path)
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002119{
2120 const struct btf_type *sec = NULL;
2121 int nr_types, i, vlen, err;
2122 const struct btf_type *t;
2123 const char *name;
2124 Elf_Data *data;
2125 Elf_Scn *scn;
2126
2127 if (obj->efile.btf_maps_shndx < 0)
2128 return 0;
2129
2130 scn = elf_getscn(obj->efile.elf, obj->efile.btf_maps_shndx);
2131 if (scn)
2132 data = elf_getdata(scn, NULL);
2133 if (!scn || !data) {
Kefeng Wangbe180102019-10-21 13:55:32 +08002134 pr_warn("failed to get Elf_Data from map section %d (%s)\n",
2135 obj->efile.maps_shndx, MAPS_ELF_SEC);
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002136 return -EINVAL;
2137 }
2138
2139 nr_types = btf__get_nr_types(obj->btf);
2140 for (i = 1; i <= nr_types; i++) {
2141 t = btf__type_by_id(obj->btf, i);
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002142 if (!btf_is_datasec(t))
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002143 continue;
2144 name = btf__name_by_offset(obj->btf, t->name_off);
2145 if (strcmp(name, MAPS_ELF_SEC) == 0) {
2146 sec = t;
2147 break;
2148 }
2149 }
2150
2151 if (!sec) {
Kefeng Wangbe180102019-10-21 13:55:32 +08002152 pr_warn("DATASEC '%s' not found.\n", MAPS_ELF_SEC);
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002153 return -ENOENT;
2154 }
2155
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002156 vlen = btf_vlen(sec);
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002157 for (i = 0; i < vlen; i++) {
2158 err = bpf_object__init_user_btf_map(obj, sec, i,
2159 obj->efile.btf_maps_shndx,
Andrii Nakryiko8983b732019-11-20 23:07:42 -08002160 data, strict,
2161 pin_root_path);
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002162 if (err)
2163 return err;
2164 }
2165
2166 return 0;
2167}
2168
Andrii Nakryiko0d13bfc2019-12-13 17:43:25 -08002169static int bpf_object__init_maps(struct bpf_object *obj,
Andrii Nakryiko01af3bf2019-12-13 17:43:32 -08002170 const struct bpf_object_open_opts *opts)
Andrii Nakryikobf829272019-06-17 12:26:53 -07002171{
Andrii Nakryiko166750b2019-12-13 17:47:08 -08002172 const char *pin_root_path;
2173 bool strict;
Andrii Nakryikobf829272019-06-17 12:26:53 -07002174 int err;
Eric Leblond4708bbd2016-11-15 04:05:47 +00002175
Andrii Nakryiko166750b2019-12-13 17:47:08 -08002176 strict = !OPTS_GET(opts, relaxed_maps, false);
2177 pin_root_path = OPTS_GET(opts, pin_root_path, NULL);
2178
Andrii Nakryikobf829272019-06-17 12:26:53 -07002179 err = bpf_object__init_user_maps(obj, strict);
Andrii Nakryiko166750b2019-12-13 17:47:08 -08002180 err = err ?: bpf_object__init_user_btf_maps(obj, strict, pin_root_path);
2181 err = err ?: bpf_object__init_global_data_maps(obj);
Andrii Nakryiko81bfdd02019-12-18 16:28:34 -08002182 err = err ?: bpf_object__init_kconfig_map(obj);
Martin KaFai Lau590a0082020-01-08 16:35:14 -08002183 err = err ?: bpf_object__init_struct_ops_maps(obj);
Andrii Nakryikobf829272019-06-17 12:26:53 -07002184 if (err)
2185 return err;
2186
Andrii Nakryikobf829272019-06-17 12:26:53 -07002187 return 0;
Wang Nan561bbcc2015-11-27 08:47:36 +00002188}
2189
Jesper Dangaard Brouere3d91b02018-02-08 12:48:32 +01002190static bool section_have_execinstr(struct bpf_object *obj, int idx)
2191{
2192 Elf_Scn *scn;
2193 GElf_Shdr sh;
2194
2195 scn = elf_getscn(obj->efile.elf, idx);
2196 if (!scn)
2197 return false;
2198
2199 if (gelf_getshdr(scn, &sh) != &sh)
2200 return false;
2201
2202 if (sh.sh_flags & SHF_EXECINSTR)
2203 return true;
2204
2205 return false;
2206}
2207
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07002208static void bpf_object__sanitize_btf(struct bpf_object *obj)
2209{
Alexei Starovoitov2d3eb672020-01-09 22:41:19 -08002210 bool has_func_global = obj->caps.btf_func_global;
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07002211 bool has_datasec = obj->caps.btf_datasec;
2212 bool has_func = obj->caps.btf_func;
2213 struct btf *btf = obj->btf;
2214 struct btf_type *t;
2215 int i, j, vlen;
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07002216
Alexei Starovoitov2d3eb672020-01-09 22:41:19 -08002217 if (!obj->btf || (has_func && has_datasec && has_func_global))
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07002218 return;
2219
2220 for (i = 1; i <= btf__get_nr_types(btf); i++) {
2221 t = (struct btf_type *)btf__type_by_id(btf, i);
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07002222
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002223 if (!has_datasec && btf_is_var(t)) {
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07002224 /* replace VAR with INT */
2225 t->info = BTF_INFO_ENC(BTF_KIND_INT, 0, 0);
Andrii Nakryiko1d4126c2019-07-19 12:46:03 -07002226 /*
2227 * using size = 1 is the safest choice, 4 will be too
2228 * big and cause kernel BTF validation failure if
2229 * original variable took less than 4 bytes
2230 */
2231 t->size = 1;
Jakub Kicinski708852d2019-08-13 16:24:57 -07002232 *(int *)(t + 1) = BTF_INT_ENC(0, 0, 8);
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002233 } else if (!has_datasec && btf_is_datasec(t)) {
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07002234 /* replace DATASEC with STRUCT */
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002235 const struct btf_var_secinfo *v = btf_var_secinfos(t);
2236 struct btf_member *m = btf_members(t);
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07002237 struct btf_type *vt;
2238 char *name;
2239
2240 name = (char *)btf__name_by_offset(btf, t->name_off);
2241 while (*name) {
2242 if (*name == '.')
2243 *name = '_';
2244 name++;
2245 }
2246
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002247 vlen = btf_vlen(t);
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07002248 t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, vlen);
2249 for (j = 0; j < vlen; j++, v++, m++) {
2250 /* order of field assignments is important */
2251 m->offset = v->offset * 8;
2252 m->type = v->type;
2253 /* preserve variable name as member name */
2254 vt = (void *)btf__type_by_id(btf, v->type);
2255 m->name_off = vt->name_off;
2256 }
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002257 } else if (!has_func && btf_is_func_proto(t)) {
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07002258 /* replace FUNC_PROTO with ENUM */
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002259 vlen = btf_vlen(t);
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07002260 t->info = BTF_INFO_ENC(BTF_KIND_ENUM, 0, vlen);
2261 t->size = sizeof(__u32); /* kernel enforced */
Andrii Nakryikob03bc682019-08-07 14:39:49 -07002262 } else if (!has_func && btf_is_func(t)) {
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07002263 /* replace FUNC with TYPEDEF */
2264 t->info = BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0);
Alexei Starovoitov2d3eb672020-01-09 22:41:19 -08002265 } else if (!has_func_global && btf_is_func(t)) {
2266 /* replace BTF_FUNC_GLOBAL with BTF_FUNC_STATIC */
2267 t->info = BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0);
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07002268 }
2269 }
2270}
2271
2272static void bpf_object__sanitize_btf_ext(struct bpf_object *obj)
2273{
2274 if (!obj->btf_ext)
2275 return;
2276
2277 if (!obj->caps.btf_func) {
2278 btf_ext__free(obj->btf_ext);
2279 obj->btf_ext = NULL;
2280 }
2281}
2282
Andrii Nakryikob35f14f2020-03-12 11:50:33 -07002283static bool libbpf_needs_btf(const struct bpf_object *obj)
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002284{
Andrii Nakryikob35f14f2020-03-12 11:50:33 -07002285 return obj->efile.btf_maps_shndx >= 0 ||
2286 obj->efile.st_ops_shndx >= 0 ||
2287 obj->nr_extern > 0;
2288}
2289
2290static bool kernel_needs_btf(const struct bpf_object *obj)
2291{
2292 return obj->efile.st_ops_shndx >= 0;
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002293}
2294
Andrii Nakryiko063183b2019-06-17 12:26:55 -07002295static int bpf_object__init_btf(struct bpf_object *obj,
Andrii Nakryiko9c6660d2019-06-17 12:26:51 -07002296 Elf_Data *btf_data,
2297 Elf_Data *btf_ext_data)
2298{
Andrii Nakryikob7d7f3e2020-01-16 22:07:59 -08002299 int err = -ENOENT;
Andrii Nakryiko9c6660d2019-06-17 12:26:51 -07002300
2301 if (btf_data) {
2302 obj->btf = btf__new(btf_data->d_buf, btf_data->d_size);
2303 if (IS_ERR(obj->btf)) {
Andrii Nakryikob7d7f3e2020-01-16 22:07:59 -08002304 err = PTR_ERR(obj->btf);
2305 obj->btf = NULL;
Kefeng Wangbe180102019-10-21 13:55:32 +08002306 pr_warn("Error loading ELF section %s: %d.\n",
2307 BTF_ELF_SEC, err);
Andrii Nakryiko9c6660d2019-06-17 12:26:51 -07002308 goto out;
2309 }
Andrii Nakryikob7d7f3e2020-01-16 22:07:59 -08002310 err = 0;
Andrii Nakryiko9c6660d2019-06-17 12:26:51 -07002311 }
2312 if (btf_ext_data) {
2313 if (!obj->btf) {
2314 pr_debug("Ignore ELF section %s because its depending ELF section %s is not found.\n",
2315 BTF_EXT_ELF_SEC, BTF_ELF_SEC);
2316 goto out;
2317 }
2318 obj->btf_ext = btf_ext__new(btf_ext_data->d_buf,
2319 btf_ext_data->d_size);
2320 if (IS_ERR(obj->btf_ext)) {
Kefeng Wangbe180102019-10-21 13:55:32 +08002321 pr_warn("Error loading ELF section %s: %ld. Ignored and continue.\n",
2322 BTF_EXT_ELF_SEC, PTR_ERR(obj->btf_ext));
Andrii Nakryiko9c6660d2019-06-17 12:26:51 -07002323 obj->btf_ext = NULL;
2324 goto out;
2325 }
Andrii Nakryiko9c6660d2019-06-17 12:26:51 -07002326 }
2327out:
Andrii Nakryikob35f14f2020-03-12 11:50:33 -07002328 if (err && libbpf_needs_btf(obj)) {
Kefeng Wangbe180102019-10-21 13:55:32 +08002329 pr_warn("BTF is required, but is missing or corrupted.\n");
Andrii Nakryikob7d7f3e2020-01-16 22:07:59 -08002330 return err;
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002331 }
Andrii Nakryiko9c6660d2019-06-17 12:26:51 -07002332 return 0;
2333}
2334
Andrii Nakryiko166750b2019-12-13 17:47:08 -08002335static int bpf_object__finalize_btf(struct bpf_object *obj)
2336{
2337 int err;
2338
2339 if (!obj->btf)
2340 return 0;
2341
2342 err = btf__finalize_data(obj, obj->btf);
2343 if (!err)
2344 return 0;
2345
2346 pr_warn("Error finalizing %s: %d.\n", BTF_ELF_SEC, err);
2347 btf__free(obj->btf);
2348 obj->btf = NULL;
2349 btf_ext__free(obj->btf_ext);
2350 obj->btf_ext = NULL;
2351
Andrii Nakryikob35f14f2020-03-12 11:50:33 -07002352 if (libbpf_needs_btf(obj)) {
Andrii Nakryiko166750b2019-12-13 17:47:08 -08002353 pr_warn("BTF is required, but is missing or corrupted.\n");
2354 return -ENOENT;
2355 }
2356 return 0;
2357}
2358
KP Singha6ed02c2020-01-17 22:28:25 +01002359static inline bool libbpf_prog_needs_vmlinux_btf(struct bpf_program *prog)
2360{
KP Singh1e092a032020-03-29 01:43:54 +01002361 if (prog->type == BPF_PROG_TYPE_STRUCT_OPS ||
2362 prog->type == BPF_PROG_TYPE_LSM)
KP Singha6ed02c2020-01-17 22:28:25 +01002363 return true;
2364
2365 /* BPF_PROG_TYPE_TRACING programs which do not attach to other programs
2366 * also need vmlinux BTF
2367 */
2368 if (prog->type == BPF_PROG_TYPE_TRACING && !prog->attach_prog_fd)
2369 return true;
2370
2371 return false;
2372}
2373
2374static int bpf_object__load_vmlinux_btf(struct bpf_object *obj)
2375{
2376 struct bpf_program *prog;
2377 int err;
2378
2379 bpf_object__for_each_program(prog, obj) {
2380 if (libbpf_prog_needs_vmlinux_btf(prog)) {
2381 obj->btf_vmlinux = libbpf_find_kernel_btf();
2382 if (IS_ERR(obj->btf_vmlinux)) {
2383 err = PTR_ERR(obj->btf_vmlinux);
2384 pr_warn("Error loading vmlinux BTF: %d\n", err);
2385 obj->btf_vmlinux = NULL;
2386 return err;
2387 }
2388 return 0;
2389 }
2390 }
2391
2392 return 0;
2393}
2394
Andrii Nakryiko063183b2019-06-17 12:26:55 -07002395static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj)
2396{
2397 int err = 0;
2398
2399 if (!obj->btf)
2400 return 0;
2401
2402 bpf_object__sanitize_btf(obj);
2403 bpf_object__sanitize_btf_ext(obj);
2404
2405 err = btf__load(obj->btf);
2406 if (err) {
Kefeng Wangbe180102019-10-21 13:55:32 +08002407 pr_warn("Error loading %s into kernel: %d.\n",
2408 BTF_ELF_SEC, err);
Andrii Nakryiko063183b2019-06-17 12:26:55 -07002409 btf__free(obj->btf);
2410 obj->btf = NULL;
Andrii Nakryiko04efe592019-07-19 12:32:42 -07002411 /* btf_ext can't exist without btf, so free it as well */
2412 if (obj->btf_ext) {
2413 btf_ext__free(obj->btf_ext);
2414 obj->btf_ext = NULL;
2415 }
2416
Andrii Nakryikob35f14f2020-03-12 11:50:33 -07002417 if (kernel_needs_btf(obj))
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002418 return err;
Andrii Nakryiko063183b2019-06-17 12:26:55 -07002419 }
2420 return 0;
2421}
2422
Andrii Nakryiko0d13bfc2019-12-13 17:43:25 -08002423static int bpf_object__elf_collect(struct bpf_object *obj)
Wang Nan29603662015-07-01 02:13:56 +00002424{
2425 Elf *elf = obj->efile.elf;
2426 GElf_Ehdr *ep = &obj->efile.ehdr;
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08002427 Elf_Data *btf_ext_data = NULL;
Daniel Borkmann1713d682019-04-09 23:20:14 +02002428 Elf_Data *btf_data = NULL;
Wang Nan29603662015-07-01 02:13:56 +00002429 Elf_Scn *scn = NULL;
Wang Nan666810e2016-01-25 09:55:49 +00002430 int idx = 0, err = 0;
Wang Nan29603662015-07-01 02:13:56 +00002431
2432 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
2433 if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) {
Kefeng Wangbe180102019-10-21 13:55:32 +08002434 pr_warn("failed to get e_shstrndx from %s\n", obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00002435 return -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +00002436 }
2437
2438 while ((scn = elf_nextscn(elf, scn)) != NULL) {
2439 char *name;
2440 GElf_Shdr sh;
2441 Elf_Data *data;
2442
2443 idx++;
2444 if (gelf_getshdr(scn, &sh) != &sh) {
Kefeng Wangbe180102019-10-21 13:55:32 +08002445 pr_warn("failed to get section(%d) header from %s\n",
2446 idx, obj->path);
Andrii Nakryiko01b29d12019-06-17 12:26:52 -07002447 return -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +00002448 }
2449
2450 name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
2451 if (!name) {
Kefeng Wangbe180102019-10-21 13:55:32 +08002452 pr_warn("failed to get section(%d) name from %s\n",
2453 idx, obj->path);
Andrii Nakryiko01b29d12019-06-17 12:26:52 -07002454 return -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +00002455 }
2456
2457 data = elf_getdata(scn, 0);
2458 if (!data) {
Kefeng Wangbe180102019-10-21 13:55:32 +08002459 pr_warn("failed to get section(%d) data from %s(%s)\n",
2460 idx, name, obj->path);
Andrii Nakryiko01b29d12019-06-17 12:26:52 -07002461 return -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +00002462 }
Jesper Dangaard Brouer077c0662018-02-08 12:48:17 +01002463 pr_debug("section(%d) %s, size %ld, link %d, flags %lx, type=%d\n",
2464 idx, name, (unsigned long)data->d_size,
Wang Nan29603662015-07-01 02:13:56 +00002465 (int)sh.sh_link, (unsigned long)sh.sh_flags,
2466 (int)sh.sh_type);
Wang Nancb1e5e92015-07-01 02:13:57 +00002467
Daniel Borkmann1713d682019-04-09 23:20:14 +02002468 if (strcmp(name, "license") == 0) {
Wang Nancb1e5e92015-07-01 02:13:57 +00002469 err = bpf_object__init_license(obj,
2470 data->d_buf,
2471 data->d_size);
Andrii Nakryiko01b29d12019-06-17 12:26:52 -07002472 if (err)
2473 return err;
Daniel Borkmann1713d682019-04-09 23:20:14 +02002474 } else if (strcmp(name, "version") == 0) {
John Fastabend54b86252019-10-18 07:41:26 -07002475 err = bpf_object__init_kversion(obj,
2476 data->d_buf,
2477 data->d_size);
2478 if (err)
2479 return err;
Daniel Borkmann1713d682019-04-09 23:20:14 +02002480 } else if (strcmp(name, "maps") == 0) {
Wang Nan666810e2016-01-25 09:55:49 +00002481 obj->efile.maps_shndx = idx;
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002482 } else if (strcmp(name, MAPS_ELF_SEC) == 0) {
2483 obj->efile.btf_maps_shndx = idx;
Daniel Borkmann1713d682019-04-09 23:20:14 +02002484 } else if (strcmp(name, BTF_ELF_SEC) == 0) {
2485 btf_data = data;
Yonghong Song2993e052018-11-19 15:29:16 -08002486 } else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) {
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08002487 btf_ext_data = data;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07002488 } else if (sh.sh_type == SHT_SYMTAB) {
Wang Nanbec7d682015-07-01 02:13:59 +00002489 if (obj->efile.symbols) {
Kefeng Wangbe180102019-10-21 13:55:32 +08002490 pr_warn("bpf: multiple SYMTAB in %s\n",
2491 obj->path);
Andrii Nakryiko01b29d12019-06-17 12:26:52 -07002492 return -LIBBPF_ERRNO__FORMAT;
Wang Nan77ba9a52015-12-08 02:25:30 +00002493 }
Andrii Nakryiko01b29d12019-06-17 12:26:52 -07002494 obj->efile.symbols = data;
Andrii Nakryiko166750b2019-12-13 17:47:08 -08002495 obj->efile.symbols_shndx = idx;
Andrii Nakryiko01b29d12019-06-17 12:26:52 -07002496 obj->efile.strtabidx = sh.sh_link;
Joe Stringerf8c7a4d2019-04-09 23:20:12 +02002497 } else if (sh.sh_type == SHT_PROGBITS && data->d_size > 0) {
2498 if (sh.sh_flags & SHF_EXECINSTR) {
2499 if (strcmp(name, ".text") == 0)
2500 obj->efile.text_shndx = idx;
2501 err = bpf_object__add_program(obj, data->d_buf,
Andrii Nakryiko8983b732019-11-20 23:07:42 -08002502 data->d_size,
2503 name, idx);
Joe Stringerf8c7a4d2019-04-09 23:20:12 +02002504 if (err) {
2505 char errmsg[STRERR_BUFSIZE];
Andrii Nakryiko8983b732019-11-20 23:07:42 -08002506 char *cp;
Wang Nan6371ca3b2015-11-06 13:49:37 +00002507
Andrii Nakryiko8983b732019-11-20 23:07:42 -08002508 cp = libbpf_strerror_r(-err, errmsg,
2509 sizeof(errmsg));
Kefeng Wangbe180102019-10-21 13:55:32 +08002510 pr_warn("failed to alloc program %s (%s): %s",
2511 name, obj->path, cp);
Andrii Nakryiko01b29d12019-06-17 12:26:52 -07002512 return err;
Joe Stringerf8c7a4d2019-04-09 23:20:12 +02002513 }
Andrii Nakryikoac9d1382019-12-13 17:47:07 -08002514 } else if (strcmp(name, DATA_SEC) == 0) {
Daniel Borkmannd8599002019-04-09 23:20:13 +02002515 obj->efile.data = data;
2516 obj->efile.data_shndx = idx;
Andrii Nakryikoac9d1382019-12-13 17:47:07 -08002517 } else if (strcmp(name, RODATA_SEC) == 0) {
Daniel Borkmannd8599002019-04-09 23:20:13 +02002518 obj->efile.rodata = data;
2519 obj->efile.rodata_shndx = idx;
Martin KaFai Lau590a0082020-01-08 16:35:14 -08002520 } else if (strcmp(name, STRUCT_OPS_SEC) == 0) {
2521 obj->efile.st_ops_data = data;
2522 obj->efile.st_ops_shndx = idx;
Daniel Borkmannd8599002019-04-09 23:20:13 +02002523 } else {
2524 pr_debug("skip section(%d) %s\n", idx, name);
Wang Nana5b8bd42015-07-01 02:14:00 +00002525 }
Wang Nanb62f06e2015-07-01 02:14:01 +00002526 } else if (sh.sh_type == SHT_REL) {
Andrii Nakryiko1f8e2bc2019-11-20 23:07:41 -08002527 int nr_sects = obj->efile.nr_reloc_sects;
2528 void *sects = obj->efile.reloc_sects;
Jesper Dangaard Brouere3d91b02018-02-08 12:48:32 +01002529 int sec = sh.sh_info; /* points to other section */
2530
2531 /* Only do relo for section with exec instructions */
Martin KaFai Lau590a0082020-01-08 16:35:14 -08002532 if (!section_have_execinstr(obj, sec) &&
2533 strcmp(name, ".rel" STRUCT_OPS_SEC)) {
Jesper Dangaard Brouere3d91b02018-02-08 12:48:32 +01002534 pr_debug("skip relo %s(%d) for section(%d)\n",
2535 name, idx, sec);
2536 continue;
2537 }
Wang Nanb62f06e2015-07-01 02:14:01 +00002538
Andrii Nakryiko1f8e2bc2019-11-20 23:07:41 -08002539 sects = reallocarray(sects, nr_sects + 1,
2540 sizeof(*obj->efile.reloc_sects));
2541 if (!sects) {
2542 pr_warn("reloc_sects realloc failed\n");
Andrii Nakryiko01b29d12019-06-17 12:26:52 -07002543 return -ENOMEM;
Wang Nanb62f06e2015-07-01 02:14:01 +00002544 }
Andrii Nakryiko01b29d12019-06-17 12:26:52 -07002545
Andrii Nakryiko1f8e2bc2019-11-20 23:07:41 -08002546 obj->efile.reloc_sects = sects;
2547 obj->efile.nr_reloc_sects++;
Andrii Nakryiko01b29d12019-06-17 12:26:52 -07002548
Andrii Nakryiko1f8e2bc2019-11-20 23:07:41 -08002549 obj->efile.reloc_sects[nr_sects].shdr = sh;
2550 obj->efile.reloc_sects[nr_sects].data = data;
Andrii Nakryikoac9d1382019-12-13 17:47:07 -08002551 } else if (sh.sh_type == SHT_NOBITS &&
2552 strcmp(name, BSS_SEC) == 0) {
Daniel Borkmannd8599002019-04-09 23:20:13 +02002553 obj->efile.bss = data;
2554 obj->efile.bss_shndx = idx;
Jesper Dangaard Brouer077c0662018-02-08 12:48:17 +01002555 } else {
2556 pr_debug("skip section(%d) %s\n", idx, name);
Wang Nanbec7d682015-07-01 02:13:59 +00002557 }
Wang Nan29603662015-07-01 02:13:56 +00002558 }
Wang Nan561bbcc2015-11-27 08:47:36 +00002559
Andrii Nakryikod3a3aa02019-10-28 16:37:27 -07002560 if (!obj->efile.strtabidx || obj->efile.strtabidx > idx) {
Kefeng Wangbe180102019-10-21 13:55:32 +08002561 pr_warn("Corrupted ELF file: index of strtab invalid\n");
Andrii Nakryikof1021542019-05-29 10:36:07 -07002562 return -LIBBPF_ERRNO__FORMAT;
Wang Nan77ba9a52015-12-08 02:25:30 +00002563 }
Andrii Nakryiko0d13bfc2019-12-13 17:43:25 -08002564 return bpf_object__init_btf(obj, btf_data, btf_ext_data);
Wang Nan29603662015-07-01 02:13:56 +00002565}
2566
Andrii Nakryiko166750b2019-12-13 17:47:08 -08002567static bool sym_is_extern(const GElf_Sym *sym)
2568{
2569 int bind = GELF_ST_BIND(sym->st_info);
2570 /* externs are symbols w/ type=NOTYPE, bind=GLOBAL|WEAK, section=UND */
2571 return sym->st_shndx == SHN_UNDEF &&
2572 (bind == STB_GLOBAL || bind == STB_WEAK) &&
2573 GELF_ST_TYPE(sym->st_info) == STT_NOTYPE;
2574}
2575
2576static int find_extern_btf_id(const struct btf *btf, const char *ext_name)
2577{
2578 const struct btf_type *t;
2579 const char *var_name;
2580 int i, n;
2581
2582 if (!btf)
2583 return -ESRCH;
2584
2585 n = btf__get_nr_types(btf);
2586 for (i = 1; i <= n; i++) {
2587 t = btf__type_by_id(btf, i);
2588
2589 if (!btf_is_var(t))
2590 continue;
2591
2592 var_name = btf__name_by_offset(btf, t->name_off);
2593 if (strcmp(var_name, ext_name))
2594 continue;
2595
2596 if (btf_var(t)->linkage != BTF_VAR_GLOBAL_EXTERN)
2597 return -EINVAL;
2598
2599 return i;
2600 }
2601
2602 return -ENOENT;
2603}
2604
2605static enum extern_type find_extern_type(const struct btf *btf, int id,
2606 bool *is_signed)
2607{
2608 const struct btf_type *t;
2609 const char *name;
2610
2611 t = skip_mods_and_typedefs(btf, id, NULL);
2612 name = btf__name_by_offset(btf, t->name_off);
2613
2614 if (is_signed)
2615 *is_signed = false;
2616 switch (btf_kind(t)) {
2617 case BTF_KIND_INT: {
2618 int enc = btf_int_encoding(t);
2619
2620 if (enc & BTF_INT_BOOL)
2621 return t->size == 1 ? EXT_BOOL : EXT_UNKNOWN;
2622 if (is_signed)
2623 *is_signed = enc & BTF_INT_SIGNED;
2624 if (t->size == 1)
2625 return EXT_CHAR;
2626 if (t->size < 1 || t->size > 8 || (t->size & (t->size - 1)))
2627 return EXT_UNKNOWN;
2628 return EXT_INT;
2629 }
2630 case BTF_KIND_ENUM:
2631 if (t->size != 4)
2632 return EXT_UNKNOWN;
2633 if (strcmp(name, "libbpf_tristate"))
2634 return EXT_UNKNOWN;
2635 return EXT_TRISTATE;
2636 case BTF_KIND_ARRAY:
2637 if (btf_array(t)->nelems == 0)
2638 return EXT_UNKNOWN;
2639 if (find_extern_type(btf, btf_array(t)->type, NULL) != EXT_CHAR)
2640 return EXT_UNKNOWN;
2641 return EXT_CHAR_ARR;
2642 default:
2643 return EXT_UNKNOWN;
2644 }
2645}
2646
2647static int cmp_externs(const void *_a, const void *_b)
2648{
2649 const struct extern_desc *a = _a;
2650 const struct extern_desc *b = _b;
2651
2652 /* descending order by alignment requirements */
2653 if (a->align != b->align)
2654 return a->align > b->align ? -1 : 1;
2655 /* ascending order by size, within same alignment class */
2656 if (a->sz != b->sz)
2657 return a->sz < b->sz ? -1 : 1;
2658 /* resolve ties by name */
2659 return strcmp(a->name, b->name);
2660}
2661
2662static int bpf_object__collect_externs(struct bpf_object *obj)
2663{
2664 const struct btf_type *t;
2665 struct extern_desc *ext;
2666 int i, n, off, btf_id;
2667 struct btf_type *sec;
2668 const char *ext_name;
2669 Elf_Scn *scn;
2670 GElf_Shdr sh;
2671
2672 if (!obj->efile.symbols)
2673 return 0;
2674
2675 scn = elf_getscn(obj->efile.elf, obj->efile.symbols_shndx);
2676 if (!scn)
2677 return -LIBBPF_ERRNO__FORMAT;
2678 if (gelf_getshdr(scn, &sh) != &sh)
2679 return -LIBBPF_ERRNO__FORMAT;
2680 n = sh.sh_size / sh.sh_entsize;
2681
2682 pr_debug("looking for externs among %d symbols...\n", n);
2683 for (i = 0; i < n; i++) {
2684 GElf_Sym sym;
2685
2686 if (!gelf_getsym(obj->efile.symbols, i, &sym))
2687 return -LIBBPF_ERRNO__FORMAT;
2688 if (!sym_is_extern(&sym))
2689 continue;
2690 ext_name = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
2691 sym.st_name);
2692 if (!ext_name || !ext_name[0])
2693 continue;
2694
2695 ext = obj->externs;
2696 ext = reallocarray(ext, obj->nr_extern + 1, sizeof(*ext));
2697 if (!ext)
2698 return -ENOMEM;
2699 obj->externs = ext;
2700 ext = &ext[obj->nr_extern];
2701 memset(ext, 0, sizeof(*ext));
2702 obj->nr_extern++;
2703
2704 ext->btf_id = find_extern_btf_id(obj->btf, ext_name);
2705 if (ext->btf_id <= 0) {
2706 pr_warn("failed to find BTF for extern '%s': %d\n",
2707 ext_name, ext->btf_id);
2708 return ext->btf_id;
2709 }
2710 t = btf__type_by_id(obj->btf, ext->btf_id);
2711 ext->name = btf__name_by_offset(obj->btf, t->name_off);
2712 ext->sym_idx = i;
2713 ext->is_weak = GELF_ST_BIND(sym.st_info) == STB_WEAK;
2714 ext->sz = btf__resolve_size(obj->btf, t->type);
2715 if (ext->sz <= 0) {
2716 pr_warn("failed to resolve size of extern '%s': %d\n",
2717 ext_name, ext->sz);
2718 return ext->sz;
2719 }
2720 ext->align = btf__align_of(obj->btf, t->type);
2721 if (ext->align <= 0) {
2722 pr_warn("failed to determine alignment of extern '%s': %d\n",
2723 ext_name, ext->align);
2724 return -EINVAL;
2725 }
2726 ext->type = find_extern_type(obj->btf, t->type,
2727 &ext->is_signed);
2728 if (ext->type == EXT_UNKNOWN) {
2729 pr_warn("extern '%s' type is unsupported\n", ext_name);
2730 return -ENOTSUP;
2731 }
2732 }
2733 pr_debug("collected %d externs total\n", obj->nr_extern);
2734
2735 if (!obj->nr_extern)
2736 return 0;
2737
2738 /* sort externs by (alignment, size, name) and calculate their offsets
2739 * within a map */
2740 qsort(obj->externs, obj->nr_extern, sizeof(*ext), cmp_externs);
2741 off = 0;
2742 for (i = 0; i < obj->nr_extern; i++) {
2743 ext = &obj->externs[i];
2744 ext->data_off = roundup(off, ext->align);
2745 off = ext->data_off + ext->sz;
2746 pr_debug("extern #%d: symbol %d, off %u, name %s\n",
2747 i, ext->sym_idx, ext->data_off, ext->name);
2748 }
2749
Andrii Nakryiko81bfdd02019-12-18 16:28:34 -08002750 btf_id = btf__find_by_name(obj->btf, KCONFIG_SEC);
Andrii Nakryiko166750b2019-12-13 17:47:08 -08002751 if (btf_id <= 0) {
Andrii Nakryiko81bfdd02019-12-18 16:28:34 -08002752 pr_warn("no BTF info found for '%s' datasec\n", KCONFIG_SEC);
Andrii Nakryiko166750b2019-12-13 17:47:08 -08002753 return -ESRCH;
2754 }
2755
2756 sec = (struct btf_type *)btf__type_by_id(obj->btf, btf_id);
2757 sec->size = off;
2758 n = btf_vlen(sec);
2759 for (i = 0; i < n; i++) {
2760 struct btf_var_secinfo *vs = btf_var_secinfos(sec) + i;
2761
2762 t = btf__type_by_id(obj->btf, vs->type);
2763 ext_name = btf__name_by_offset(obj->btf, t->name_off);
2764 ext = find_extern_by_name(obj, ext_name);
2765 if (!ext) {
2766 pr_warn("failed to find extern definition for BTF var '%s'\n",
2767 ext_name);
2768 return -ESRCH;
2769 }
2770 vs->offset = ext->data_off;
2771 btf_var(t)->linkage = BTF_VAR_GLOBAL_ALLOCATED;
2772 }
2773
2774 return 0;
2775}
2776
Wang Nan34090912015-07-01 02:14:02 +00002777static struct bpf_program *
2778bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx)
2779{
2780 struct bpf_program *prog;
2781 size_t i;
2782
2783 for (i = 0; i < obj->nr_programs; i++) {
2784 prog = &obj->programs[i];
2785 if (prog->idx == idx)
2786 return prog;
2787 }
2788 return NULL;
2789}
2790
Jakub Kicinski6d4b1982018-07-26 14:32:19 -07002791struct bpf_program *
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07002792bpf_object__find_program_by_title(const struct bpf_object *obj,
2793 const char *title)
Jakub Kicinski6d4b1982018-07-26 14:32:19 -07002794{
2795 struct bpf_program *pos;
2796
2797 bpf_object__for_each_program(pos, obj) {
2798 if (pos->section_name && !strcmp(pos->section_name, title))
2799 return pos;
2800 }
2801 return NULL;
2802}
2803
Andrii Nakryiko01af3bf2019-12-13 17:43:32 -08002804struct bpf_program *
2805bpf_object__find_program_by_name(const struct bpf_object *obj,
2806 const char *name)
2807{
2808 struct bpf_program *prog;
2809
2810 bpf_object__for_each_program(prog, obj) {
2811 if (!strcmp(prog->name, name))
2812 return prog;
2813 }
2814 return NULL;
2815}
2816
Daniel Borkmannd8599002019-04-09 23:20:13 +02002817static bool bpf_object__shndx_is_data(const struct bpf_object *obj,
2818 int shndx)
2819{
2820 return shndx == obj->efile.data_shndx ||
2821 shndx == obj->efile.bss_shndx ||
2822 shndx == obj->efile.rodata_shndx;
2823}
2824
2825static bool bpf_object__shndx_is_maps(const struct bpf_object *obj,
2826 int shndx)
2827{
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002828 return shndx == obj->efile.maps_shndx ||
2829 shndx == obj->efile.btf_maps_shndx;
Daniel Borkmannd8599002019-04-09 23:20:13 +02002830}
2831
Daniel Borkmannd8599002019-04-09 23:20:13 +02002832static enum libbpf_map_type
2833bpf_object__section_to_libbpf_map_type(const struct bpf_object *obj, int shndx)
2834{
2835 if (shndx == obj->efile.data_shndx)
2836 return LIBBPF_MAP_DATA;
2837 else if (shndx == obj->efile.bss_shndx)
2838 return LIBBPF_MAP_BSS;
2839 else if (shndx == obj->efile.rodata_shndx)
2840 return LIBBPF_MAP_RODATA;
Andrii Nakryiko166750b2019-12-13 17:47:08 -08002841 else if (shndx == obj->efile.symbols_shndx)
Andrii Nakryiko81bfdd02019-12-18 16:28:34 -08002842 return LIBBPF_MAP_KCONFIG;
Daniel Borkmannd8599002019-04-09 23:20:13 +02002843 else
2844 return LIBBPF_MAP_UNSPEC;
2845}
2846
Andrii Nakryiko1f8e2bc2019-11-20 23:07:41 -08002847static int bpf_program__record_reloc(struct bpf_program *prog,
2848 struct reloc_desc *reloc_desc,
2849 __u32 insn_idx, const char *name,
2850 const GElf_Sym *sym, const GElf_Rel *rel)
2851{
2852 struct bpf_insn *insn = &prog->insns[insn_idx];
2853 size_t map_idx, nr_maps = prog->obj->nr_maps;
2854 struct bpf_object *obj = prog->obj;
2855 __u32 shdr_idx = sym->st_shndx;
2856 enum libbpf_map_type type;
2857 struct bpf_map *map;
2858
2859 /* sub-program call relocation */
2860 if (insn->code == (BPF_JMP | BPF_CALL)) {
2861 if (insn->src_reg != BPF_PSEUDO_CALL) {
2862 pr_warn("incorrect bpf_call opcode\n");
2863 return -LIBBPF_ERRNO__RELOC;
2864 }
2865 /* text_shndx can be 0, if no default "main" program exists */
2866 if (!shdr_idx || shdr_idx != obj->efile.text_shndx) {
2867 pr_warn("bad call relo against section %u\n", shdr_idx);
2868 return -LIBBPF_ERRNO__RELOC;
2869 }
2870 if (sym->st_value % 8) {
Andrii Nakryiko679152d2019-12-12 09:19:18 -08002871 pr_warn("bad call relo offset: %zu\n",
2872 (size_t)sym->st_value);
Andrii Nakryiko1f8e2bc2019-11-20 23:07:41 -08002873 return -LIBBPF_ERRNO__RELOC;
2874 }
2875 reloc_desc->type = RELO_CALL;
2876 reloc_desc->insn_idx = insn_idx;
Andrii Nakryiko53f8dd42019-11-27 12:06:50 -08002877 reloc_desc->sym_off = sym->st_value;
Andrii Nakryiko1f8e2bc2019-11-20 23:07:41 -08002878 obj->has_pseudo_calls = true;
2879 return 0;
2880 }
2881
2882 if (insn->code != (BPF_LD | BPF_IMM | BPF_DW)) {
Andrii Nakryiko8983b732019-11-20 23:07:42 -08002883 pr_warn("invalid relo for insns[%d].code 0x%x\n",
Andrii Nakryiko1f8e2bc2019-11-20 23:07:41 -08002884 insn_idx, insn->code);
2885 return -LIBBPF_ERRNO__RELOC;
2886 }
Andrii Nakryiko166750b2019-12-13 17:47:08 -08002887
2888 if (sym_is_extern(sym)) {
2889 int sym_idx = GELF_R_SYM(rel->r_info);
2890 int i, n = obj->nr_extern;
2891 struct extern_desc *ext;
2892
2893 for (i = 0; i < n; i++) {
2894 ext = &obj->externs[i];
2895 if (ext->sym_idx == sym_idx)
2896 break;
2897 }
2898 if (i >= n) {
2899 pr_warn("extern relo failed to find extern for sym %d\n",
2900 sym_idx);
2901 return -LIBBPF_ERRNO__RELOC;
2902 }
2903 pr_debug("found extern #%d '%s' (sym %d, off %u) for insn %u\n",
2904 i, ext->name, ext->sym_idx, ext->data_off, insn_idx);
2905 reloc_desc->type = RELO_EXTERN;
2906 reloc_desc->insn_idx = insn_idx;
2907 reloc_desc->sym_off = ext->data_off;
2908 return 0;
2909 }
2910
Andrii Nakryiko1f8e2bc2019-11-20 23:07:41 -08002911 if (!shdr_idx || shdr_idx >= SHN_LORESERVE) {
Andrii Nakryiko393cdfb2019-11-20 23:07:43 -08002912 pr_warn("invalid relo for \'%s\' in special section 0x%x; forgot to initialize global var?..\n",
2913 name, shdr_idx);
Andrii Nakryiko1f8e2bc2019-11-20 23:07:41 -08002914 return -LIBBPF_ERRNO__RELOC;
2915 }
2916
2917 type = bpf_object__section_to_libbpf_map_type(obj, shdr_idx);
2918
2919 /* generic map reference relocation */
2920 if (type == LIBBPF_MAP_UNSPEC) {
2921 if (!bpf_object__shndx_is_maps(obj, shdr_idx)) {
2922 pr_warn("bad map relo against section %u\n",
2923 shdr_idx);
2924 return -LIBBPF_ERRNO__RELOC;
2925 }
2926 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
2927 map = &obj->maps[map_idx];
2928 if (map->libbpf_type != type ||
2929 map->sec_idx != sym->st_shndx ||
2930 map->sec_offset != sym->st_value)
2931 continue;
2932 pr_debug("found map %zd (%s, sec %d, off %zu) for insn %u\n",
2933 map_idx, map->name, map->sec_idx,
2934 map->sec_offset, insn_idx);
2935 break;
2936 }
2937 if (map_idx >= nr_maps) {
Andrii Nakryiko679152d2019-12-12 09:19:18 -08002938 pr_warn("map relo failed to find map for sec %u, off %zu\n",
2939 shdr_idx, (size_t)sym->st_value);
Andrii Nakryiko1f8e2bc2019-11-20 23:07:41 -08002940 return -LIBBPF_ERRNO__RELOC;
2941 }
2942 reloc_desc->type = RELO_LD64;
2943 reloc_desc->insn_idx = insn_idx;
2944 reloc_desc->map_idx = map_idx;
Andrii Nakryiko53f8dd42019-11-27 12:06:50 -08002945 reloc_desc->sym_off = 0; /* sym->st_value determines map_idx */
Andrii Nakryiko1f8e2bc2019-11-20 23:07:41 -08002946 return 0;
2947 }
2948
2949 /* global data map relocation */
2950 if (!bpf_object__shndx_is_data(obj, shdr_idx)) {
2951 pr_warn("bad data relo against section %u\n", shdr_idx);
2952 return -LIBBPF_ERRNO__RELOC;
2953 }
Andrii Nakryiko1f8e2bc2019-11-20 23:07:41 -08002954 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
2955 map = &obj->maps[map_idx];
2956 if (map->libbpf_type != type)
2957 continue;
2958 pr_debug("found data map %zd (%s, sec %d, off %zu) for insn %u\n",
2959 map_idx, map->name, map->sec_idx, map->sec_offset,
2960 insn_idx);
2961 break;
2962 }
2963 if (map_idx >= nr_maps) {
2964 pr_warn("data relo failed to find map for sec %u\n",
2965 shdr_idx);
2966 return -LIBBPF_ERRNO__RELOC;
2967 }
2968
2969 reloc_desc->type = RELO_DATA;
2970 reloc_desc->insn_idx = insn_idx;
2971 reloc_desc->map_idx = map_idx;
Andrii Nakryiko53f8dd42019-11-27 12:06:50 -08002972 reloc_desc->sym_off = sym->st_value;
Andrii Nakryiko1f8e2bc2019-11-20 23:07:41 -08002973 return 0;
2974}
2975
Wang Nan34090912015-07-01 02:14:02 +00002976static int
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08002977bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr,
2978 Elf_Data *data, struct bpf_object *obj)
Wang Nan34090912015-07-01 02:14:02 +00002979{
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08002980 Elf_Data *symbols = obj->efile.symbols;
Andrii Nakryiko1f8e2bc2019-11-20 23:07:41 -08002981 int err, i, nrels;
Wang Nan34090912015-07-01 02:14:02 +00002982
Andrii Nakryiko399dc652019-05-29 10:36:11 -07002983 pr_debug("collecting relocating info for: '%s'\n", prog->section_name);
Wang Nan34090912015-07-01 02:14:02 +00002984 nrels = shdr->sh_size / shdr->sh_entsize;
2985
2986 prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels);
2987 if (!prog->reloc_desc) {
Kefeng Wangbe180102019-10-21 13:55:32 +08002988 pr_warn("failed to alloc memory in relocation\n");
Wang Nan34090912015-07-01 02:14:02 +00002989 return -ENOMEM;
2990 }
2991 prog->nr_reloc = nrels;
2992
2993 for (i = 0; i < nrels; i++) {
Daniel Borkmannd8599002019-04-09 23:20:13 +02002994 const char *name;
Andrii Nakryiko1f8e2bc2019-11-20 23:07:41 -08002995 __u32 insn_idx;
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002996 GElf_Sym sym;
2997 GElf_Rel rel;
Wang Nan34090912015-07-01 02:14:02 +00002998
2999 if (!gelf_getrel(data, i, &rel)) {
Kefeng Wangbe180102019-10-21 13:55:32 +08003000 pr_warn("relocation: failed to get %d reloc\n", i);
Wang Nan6371ca3b2015-11-06 13:49:37 +00003001 return -LIBBPF_ERRNO__FORMAT;
Wang Nan34090912015-07-01 02:14:02 +00003002 }
Andrii Nakryiko399dc652019-05-29 10:36:11 -07003003 if (!gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym)) {
Kefeng Wangbe180102019-10-21 13:55:32 +08003004 pr_warn("relocation: symbol %"PRIx64" not found\n",
3005 GELF_R_SYM(rel.r_info));
Wang Nan6371ca3b2015-11-06 13:49:37 +00003006 return -LIBBPF_ERRNO__FORMAT;
Wang Nan34090912015-07-01 02:14:02 +00003007 }
Andrii Nakryiko1f8e2bc2019-11-20 23:07:41 -08003008 if (rel.r_offset % sizeof(struct bpf_insn))
3009 return -LIBBPF_ERRNO__FORMAT;
Wang Nan34090912015-07-01 02:14:02 +00003010
Andrii Nakryiko1f8e2bc2019-11-20 23:07:41 -08003011 insn_idx = rel.r_offset / sizeof(struct bpf_insn);
Daniel Borkmannd8599002019-04-09 23:20:13 +02003012 name = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
3013 sym.st_name) ? : "<?>";
3014
Andrii Nakryiko679152d2019-12-12 09:19:18 -08003015 pr_debug("relo for shdr %u, symb %zu, value %zu, type %d, bind %d, name %d (\'%s\'), insn %u\n",
3016 (__u32)sym.st_shndx, (size_t)GELF_R_SYM(rel.r_info),
3017 (size_t)sym.st_value, GELF_ST_TYPE(sym.st_info),
Andrii Nakryiko1f8e2bc2019-11-20 23:07:41 -08003018 GELF_ST_BIND(sym.st_info), sym.st_name, name,
3019 insn_idx);
Daniel Borkmannd8599002019-04-09 23:20:13 +02003020
Andrii Nakryiko1f8e2bc2019-11-20 23:07:41 -08003021 err = bpf_program__record_reloc(prog, &prog->reloc_desc[i],
3022 insn_idx, name, &sym, &rel);
3023 if (err)
3024 return err;
Wang Nan34090912015-07-01 02:14:02 +00003025 }
3026 return 0;
3027}
3028
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07003029static int bpf_map_find_btf_info(struct bpf_object *obj, struct bpf_map *map)
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07003030{
3031 struct bpf_map_def *def = &map->def;
Daniel Borkmannd8599002019-04-09 23:20:13 +02003032 __u32 key_type_id = 0, value_type_id = 0;
Yonghong Song96408c42019-02-04 11:00:58 -08003033 int ret;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07003034
Martin KaFai Lau590a0082020-01-08 16:35:14 -08003035 /* if it's BTF-defined map, we don't need to search for type IDs.
3036 * For struct_ops map, it does not need btf_key_type_id and
3037 * btf_value_type_id.
3038 */
3039 if (map->sec_idx == obj->efile.btf_maps_shndx ||
3040 bpf_map__is_struct_ops(map))
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07003041 return 0;
3042
Daniel Borkmannd8599002019-04-09 23:20:13 +02003043 if (!bpf_map__is_internal(map)) {
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07003044 ret = btf__get_map_kv_tids(obj->btf, map->name, def->key_size,
Daniel Borkmannd8599002019-04-09 23:20:13 +02003045 def->value_size, &key_type_id,
3046 &value_type_id);
3047 } else {
3048 /*
3049 * LLVM annotates global data differently in BTF, that is,
3050 * only as '.data', '.bss' or '.rodata'.
3051 */
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07003052 ret = btf__find_by_name(obj->btf,
Daniel Borkmannd8599002019-04-09 23:20:13 +02003053 libbpf_type_to_btf_name[map->libbpf_type]);
3054 }
3055 if (ret < 0)
Yonghong Song96408c42019-02-04 11:00:58 -08003056 return ret;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07003057
Yonghong Song96408c42019-02-04 11:00:58 -08003058 map->btf_key_type_id = key_type_id;
Daniel Borkmannd8599002019-04-09 23:20:13 +02003059 map->btf_value_type_id = bpf_map__is_internal(map) ?
3060 ret : value_type_id;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07003061 return 0;
3062}
3063
Jakub Kicinski26736eb2018-07-10 14:43:06 -07003064int bpf_map__reuse_fd(struct bpf_map *map, int fd)
3065{
3066 struct bpf_map_info info = {};
3067 __u32 len = sizeof(info);
3068 int new_fd, err;
3069 char *new_name;
3070
3071 err = bpf_obj_get_info_by_fd(fd, &info, &len);
3072 if (err)
3073 return err;
3074
3075 new_name = strdup(info.name);
3076 if (!new_name)
3077 return -errno;
3078
3079 new_fd = open("/", O_RDONLY | O_CLOEXEC);
Toke Høiland-Jørgensend1b45742019-11-02 12:09:37 +01003080 if (new_fd < 0) {
3081 err = -errno;
Jakub Kicinski26736eb2018-07-10 14:43:06 -07003082 goto err_free_new_name;
Toke Høiland-Jørgensend1b45742019-11-02 12:09:37 +01003083 }
Jakub Kicinski26736eb2018-07-10 14:43:06 -07003084
3085 new_fd = dup3(fd, new_fd, O_CLOEXEC);
Toke Høiland-Jørgensend1b45742019-11-02 12:09:37 +01003086 if (new_fd < 0) {
3087 err = -errno;
Jakub Kicinski26736eb2018-07-10 14:43:06 -07003088 goto err_close_new_fd;
Toke Høiland-Jørgensend1b45742019-11-02 12:09:37 +01003089 }
Jakub Kicinski26736eb2018-07-10 14:43:06 -07003090
3091 err = zclose(map->fd);
Toke Høiland-Jørgensend1b45742019-11-02 12:09:37 +01003092 if (err) {
3093 err = -errno;
Jakub Kicinski26736eb2018-07-10 14:43:06 -07003094 goto err_close_new_fd;
Toke Høiland-Jørgensend1b45742019-11-02 12:09:37 +01003095 }
Jakub Kicinski26736eb2018-07-10 14:43:06 -07003096 free(map->name);
3097
3098 map->fd = new_fd;
3099 map->name = new_name;
3100 map->def.type = info.type;
3101 map->def.key_size = info.key_size;
3102 map->def.value_size = info.value_size;
3103 map->def.max_entries = info.max_entries;
3104 map->def.map_flags = info.map_flags;
3105 map->btf_key_type_id = info.btf_key_type_id;
3106 map->btf_value_type_id = info.btf_value_type_id;
Toke Høiland-Jørgensenec6d5f472019-11-09 21:37:27 +01003107 map->reused = true;
Jakub Kicinski26736eb2018-07-10 14:43:06 -07003108
3109 return 0;
3110
3111err_close_new_fd:
3112 close(new_fd);
3113err_free_new_name:
3114 free(new_name);
Toke Høiland-Jørgensend1b45742019-11-02 12:09:37 +01003115 return err;
Jakub Kicinski26736eb2018-07-10 14:43:06 -07003116}
3117
Andrey Ignatov1a11a4c2019-02-14 15:01:42 -08003118int bpf_map__resize(struct bpf_map *map, __u32 max_entries)
3119{
3120 if (!map || !max_entries)
3121 return -EINVAL;
3122
3123 /* If map already created, its attributes can't be changed. */
3124 if (map->fd >= 0)
3125 return -EBUSY;
3126
3127 map->def.max_entries = max_entries;
3128
3129 return 0;
3130}
3131
Wang Nan52d33522015-07-01 02:14:04 +00003132static int
Stanislav Fomichev47eff612018-11-20 17:11:19 -08003133bpf_object__probe_name(struct bpf_object *obj)
3134{
3135 struct bpf_load_program_attr attr;
3136 char *cp, errmsg[STRERR_BUFSIZE];
3137 struct bpf_insn insns[] = {
3138 BPF_MOV64_IMM(BPF_REG_0, 0),
3139 BPF_EXIT_INSN(),
3140 };
3141 int ret;
3142
3143 /* make sure basic loading works */
3144
3145 memset(&attr, 0, sizeof(attr));
3146 attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
3147 attr.insns = insns;
3148 attr.insns_cnt = ARRAY_SIZE(insns);
3149 attr.license = "GPL";
3150
3151 ret = bpf_load_program_xattr(&attr, NULL, 0);
3152 if (ret < 0) {
3153 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
Kefeng Wangbe180102019-10-21 13:55:32 +08003154 pr_warn("Error in %s():%s(%d). Couldn't load basic 'r0 = 0' BPF program.\n",
3155 __func__, cp, errno);
Stanislav Fomichev47eff612018-11-20 17:11:19 -08003156 return -errno;
3157 }
3158 close(ret);
3159
3160 /* now try the same program, but with the name */
3161
3162 attr.name = "test";
3163 ret = bpf_load_program_xattr(&attr, NULL, 0);
3164 if (ret >= 0) {
3165 obj->caps.name = 1;
3166 close(ret);
3167 }
3168
3169 return 0;
3170}
3171
3172static int
Daniel Borkmann8837fe52019-04-24 00:45:56 +02003173bpf_object__probe_global_data(struct bpf_object *obj)
3174{
3175 struct bpf_load_program_attr prg_attr;
3176 struct bpf_create_map_attr map_attr;
3177 char *cp, errmsg[STRERR_BUFSIZE];
3178 struct bpf_insn insns[] = {
3179 BPF_LD_MAP_VALUE(BPF_REG_1, 0, 16),
3180 BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 42),
3181 BPF_MOV64_IMM(BPF_REG_0, 0),
3182 BPF_EXIT_INSN(),
3183 };
3184 int ret, map;
3185
3186 memset(&map_attr, 0, sizeof(map_attr));
3187 map_attr.map_type = BPF_MAP_TYPE_ARRAY;
3188 map_attr.key_size = sizeof(int);
3189 map_attr.value_size = 32;
3190 map_attr.max_entries = 1;
3191
3192 map = bpf_create_map_xattr(&map_attr);
3193 if (map < 0) {
3194 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
Kefeng Wangbe180102019-10-21 13:55:32 +08003195 pr_warn("Error in %s():%s(%d). Couldn't create simple array map.\n",
3196 __func__, cp, errno);
Daniel Borkmann8837fe52019-04-24 00:45:56 +02003197 return -errno;
3198 }
3199
3200 insns[0].imm = map;
3201
3202 memset(&prg_attr, 0, sizeof(prg_attr));
3203 prg_attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
3204 prg_attr.insns = insns;
3205 prg_attr.insns_cnt = ARRAY_SIZE(insns);
3206 prg_attr.license = "GPL";
3207
3208 ret = bpf_load_program_xattr(&prg_attr, NULL, 0);
3209 if (ret >= 0) {
3210 obj->caps.global_data = 1;
3211 close(ret);
3212 }
3213
3214 close(map);
3215 return 0;
3216}
3217
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07003218static int bpf_object__probe_btf_func(struct bpf_object *obj)
3219{
Andrii Nakryiko8983b732019-11-20 23:07:42 -08003220 static const char strs[] = "\0int\0x\0a";
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07003221 /* void x(int a) {} */
3222 __u32 types[] = {
3223 /* int */
3224 BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3225 /* FUNC_PROTO */ /* [2] */
3226 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 0),
3227 BTF_PARAM_ENC(7, 1),
3228 /* FUNC x */ /* [3] */
3229 BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0), 2),
3230 };
Michal Rosteckicfd49212019-05-29 20:31:09 +02003231 int btf_fd;
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07003232
Michal Rosteckicfd49212019-05-29 20:31:09 +02003233 btf_fd = libbpf__load_raw_btf((char *)types, sizeof(types),
3234 strs, sizeof(strs));
3235 if (btf_fd >= 0) {
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07003236 obj->caps.btf_func = 1;
Michal Rosteckicfd49212019-05-29 20:31:09 +02003237 close(btf_fd);
3238 return 1;
3239 }
3240
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07003241 return 0;
3242}
3243
Alexei Starovoitov2d3eb672020-01-09 22:41:19 -08003244static int bpf_object__probe_btf_func_global(struct bpf_object *obj)
3245{
3246 static const char strs[] = "\0int\0x\0a";
3247 /* static void x(int a) {} */
3248 __u32 types[] = {
3249 /* int */
3250 BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3251 /* FUNC_PROTO */ /* [2] */
3252 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 0),
3253 BTF_PARAM_ENC(7, 1),
3254 /* FUNC x BTF_FUNC_GLOBAL */ /* [3] */
3255 BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, BTF_FUNC_GLOBAL), 2),
3256 };
3257 int btf_fd;
3258
3259 btf_fd = libbpf__load_raw_btf((char *)types, sizeof(types),
3260 strs, sizeof(strs));
3261 if (btf_fd >= 0) {
3262 obj->caps.btf_func_global = 1;
3263 close(btf_fd);
3264 return 1;
3265 }
3266
3267 return 0;
3268}
3269
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07003270static int bpf_object__probe_btf_datasec(struct bpf_object *obj)
3271{
Andrii Nakryiko8983b732019-11-20 23:07:42 -08003272 static const char strs[] = "\0x\0.data";
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07003273 /* static int a; */
3274 __u32 types[] = {
3275 /* int */
3276 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3277 /* VAR x */ /* [2] */
3278 BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1),
3279 BTF_VAR_STATIC,
3280 /* DATASEC val */ /* [3] */
3281 BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
3282 BTF_VAR_SECINFO_ENC(2, 0, 4),
3283 };
Michal Rosteckicfd49212019-05-29 20:31:09 +02003284 int btf_fd;
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07003285
Michal Rosteckicfd49212019-05-29 20:31:09 +02003286 btf_fd = libbpf__load_raw_btf((char *)types, sizeof(types),
3287 strs, sizeof(strs));
3288 if (btf_fd >= 0) {
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07003289 obj->caps.btf_datasec = 1;
Michal Rosteckicfd49212019-05-29 20:31:09 +02003290 close(btf_fd);
3291 return 1;
3292 }
3293
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07003294 return 0;
3295}
3296
Andrii Nakryiko7fe74b42019-11-17 09:28:05 -08003297static int bpf_object__probe_array_mmap(struct bpf_object *obj)
3298{
3299 struct bpf_create_map_attr attr = {
3300 .map_type = BPF_MAP_TYPE_ARRAY,
3301 .map_flags = BPF_F_MMAPABLE,
3302 .key_size = sizeof(int),
3303 .value_size = sizeof(int),
3304 .max_entries = 1,
3305 };
3306 int fd;
3307
3308 fd = bpf_create_map_xattr(&attr);
3309 if (fd >= 0) {
3310 obj->caps.array_mmap = 1;
3311 close(fd);
3312 return 1;
3313 }
3314
3315 return 0;
3316}
3317
Daniel Borkmann8837fe52019-04-24 00:45:56 +02003318static int
Stanislav Fomichev47eff612018-11-20 17:11:19 -08003319bpf_object__probe_caps(struct bpf_object *obj)
3320{
Daniel Borkmann8837fe52019-04-24 00:45:56 +02003321 int (*probe_fn[])(struct bpf_object *obj) = {
3322 bpf_object__probe_name,
3323 bpf_object__probe_global_data,
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07003324 bpf_object__probe_btf_func,
Alexei Starovoitov2d3eb672020-01-09 22:41:19 -08003325 bpf_object__probe_btf_func_global,
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07003326 bpf_object__probe_btf_datasec,
Andrii Nakryiko7fe74b42019-11-17 09:28:05 -08003327 bpf_object__probe_array_mmap,
Daniel Borkmann8837fe52019-04-24 00:45:56 +02003328 };
3329 int i, ret;
3330
3331 for (i = 0; i < ARRAY_SIZE(probe_fn); i++) {
3332 ret = probe_fn[i](obj);
3333 if (ret < 0)
Stanislav Fomichev15ea1642019-05-14 20:38:49 -07003334 pr_debug("Probe #%d failed with %d.\n", i, ret);
Daniel Borkmann8837fe52019-04-24 00:45:56 +02003335 }
3336
3337 return 0;
Stanislav Fomichev47eff612018-11-20 17:11:19 -08003338}
3339
Toke Høiland-Jørgensen57a00f42019-11-02 12:09:41 +01003340static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd)
3341{
3342 struct bpf_map_info map_info = {};
3343 char msg[STRERR_BUFSIZE];
3344 __u32 map_info_len;
3345
3346 map_info_len = sizeof(map_info);
3347
3348 if (bpf_obj_get_info_by_fd(map_fd, &map_info, &map_info_len)) {
3349 pr_warn("failed to get map info for map FD %d: %s\n",
3350 map_fd, libbpf_strerror_r(errno, msg, sizeof(msg)));
3351 return false;
3352 }
3353
3354 return (map_info.type == map->def.type &&
3355 map_info.key_size == map->def.key_size &&
3356 map_info.value_size == map->def.value_size &&
3357 map_info.max_entries == map->def.max_entries &&
3358 map_info.map_flags == map->def.map_flags);
3359}
3360
3361static int
3362bpf_object__reuse_map(struct bpf_map *map)
3363{
3364 char *cp, errmsg[STRERR_BUFSIZE];
3365 int err, pin_fd;
3366
3367 pin_fd = bpf_obj_get(map->pin_path);
3368 if (pin_fd < 0) {
3369 err = -errno;
3370 if (err == -ENOENT) {
3371 pr_debug("found no pinned map to reuse at '%s'\n",
3372 map->pin_path);
3373 return 0;
3374 }
3375
3376 cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
3377 pr_warn("couldn't retrieve pinned map '%s': %s\n",
3378 map->pin_path, cp);
3379 return err;
3380 }
3381
3382 if (!map_is_reuse_compat(map, pin_fd)) {
3383 pr_warn("couldn't reuse pinned map at '%s': parameter mismatch\n",
3384 map->pin_path);
3385 close(pin_fd);
3386 return -EINVAL;
3387 }
3388
3389 err = bpf_map__reuse_fd(map, pin_fd);
3390 if (err) {
3391 close(pin_fd);
3392 return err;
3393 }
3394 map->pinned = true;
3395 pr_debug("reused pinned map at '%s'\n", map->pin_path);
3396
3397 return 0;
3398}
3399
Stanislav Fomichev47eff612018-11-20 17:11:19 -08003400static int
Daniel Borkmannd8599002019-04-09 23:20:13 +02003401bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map)
3402{
Andrii Nakryiko166750b2019-12-13 17:47:08 -08003403 enum libbpf_map_type map_type = map->libbpf_type;
Daniel Borkmannd8599002019-04-09 23:20:13 +02003404 char *cp, errmsg[STRERR_BUFSIZE];
3405 int err, zero = 0;
Daniel Borkmannd8599002019-04-09 23:20:13 +02003406
Andrii Nakryiko166750b2019-12-13 17:47:08 -08003407 /* kernel already zero-initializes .bss map. */
3408 if (map_type == LIBBPF_MAP_BSS)
Daniel Borkmannd8599002019-04-09 23:20:13 +02003409 return 0;
3410
Andrii Nakryikoeba9c5f2019-12-13 17:43:33 -08003411 err = bpf_map_update_elem(map->fd, &zero, map->mmaped, 0);
3412 if (err) {
3413 err = -errno;
3414 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
3415 pr_warn("Error setting initial map(%s) contents: %s\n",
3416 map->name, cp);
3417 return err;
3418 }
Daniel Borkmannd8599002019-04-09 23:20:13 +02003419
Andrii Nakryiko81bfdd02019-12-18 16:28:34 -08003420 /* Freeze .rodata and .kconfig map as read-only from syscall side. */
3421 if (map_type == LIBBPF_MAP_RODATA || map_type == LIBBPF_MAP_KCONFIG) {
Daniel Borkmannd8599002019-04-09 23:20:13 +02003422 err = bpf_map_freeze(map->fd);
3423 if (err) {
Andrii Nakryikoeba9c5f2019-12-13 17:43:33 -08003424 err = -errno;
3425 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
Kefeng Wangbe180102019-10-21 13:55:32 +08003426 pr_warn("Error freezing map(%s) as read-only: %s\n",
3427 map->name, cp);
Andrii Nakryikoeba9c5f2019-12-13 17:43:33 -08003428 return err;
Daniel Borkmannd8599002019-04-09 23:20:13 +02003429 }
3430 }
Andrii Nakryikoeba9c5f2019-12-13 17:43:33 -08003431 return 0;
Daniel Borkmannd8599002019-04-09 23:20:13 +02003432}
3433
3434static int
Wang Nan52d33522015-07-01 02:14:04 +00003435bpf_object__create_maps(struct bpf_object *obj)
3436{
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07003437 struct bpf_create_map_attr create_attr = {};
Andrii Nakryikod7ff34d2019-07-06 11:06:25 -07003438 int nr_cpus = 0;
Wang Nan52d33522015-07-01 02:14:04 +00003439 unsigned int i;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07003440 int err;
Wang Nan52d33522015-07-01 02:14:04 +00003441
Wang Nan9d759a92015-11-27 08:47:35 +00003442 for (i = 0; i < obj->nr_maps; i++) {
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07003443 struct bpf_map *map = &obj->maps[i];
3444 struct bpf_map_def *def = &map->def;
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02003445 char *cp, errmsg[STRERR_BUFSIZE];
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07003446 int *pfd = &map->fd;
Wang Nan52d33522015-07-01 02:14:04 +00003447
Toke Høiland-Jørgensen57a00f42019-11-02 12:09:41 +01003448 if (map->pin_path) {
3449 err = bpf_object__reuse_map(map);
3450 if (err) {
3451 pr_warn("error reusing pinned map %s\n",
3452 map->name);
3453 return err;
3454 }
3455 }
3456
Jakub Kicinski26736eb2018-07-10 14:43:06 -07003457 if (map->fd >= 0) {
3458 pr_debug("skip map create (preset) %s: fd=%d\n",
3459 map->name, map->fd);
3460 continue;
3461 }
3462
Stanislav Fomichev94cb3102018-11-20 17:11:20 -08003463 if (obj->caps.name)
3464 create_attr.name = map->name;
David Beckettf0307a72018-05-16 14:02:49 -07003465 create_attr.map_ifindex = map->map_ifindex;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07003466 create_attr.map_type = def->type;
3467 create_attr.map_flags = def->map_flags;
3468 create_attr.key_size = def->key_size;
3469 create_attr.value_size = def->value_size;
Andrii Nakryikod7ff34d2019-07-06 11:06:25 -07003470 if (def->type == BPF_MAP_TYPE_PERF_EVENT_ARRAY &&
3471 !def->max_entries) {
3472 if (!nr_cpus)
3473 nr_cpus = libbpf_num_possible_cpus();
3474 if (nr_cpus < 0) {
Kefeng Wangbe180102019-10-21 13:55:32 +08003475 pr_warn("failed to determine number of system CPUs: %d\n",
3476 nr_cpus);
Andrii Nakryikod7ff34d2019-07-06 11:06:25 -07003477 err = nr_cpus;
3478 goto err_out;
3479 }
3480 pr_debug("map '%s': setting size to %d\n",
3481 map->name, nr_cpus);
3482 create_attr.max_entries = nr_cpus;
3483 } else {
3484 create_attr.max_entries = def->max_entries;
3485 }
Andrii Nakryikoe55d54f2019-06-12 22:04:57 -07003486 create_attr.btf_fd = 0;
Martin KaFai Lau61746dbe2018-05-22 15:04:24 -07003487 create_attr.btf_key_type_id = 0;
3488 create_attr.btf_value_type_id = 0;
Nikita V. Shirokovaddb9fc2018-11-20 20:55:56 -08003489 if (bpf_map_type__is_map_in_map(def->type) &&
3490 map->inner_map_fd >= 0)
3491 create_attr.inner_map_fd = map->inner_map_fd;
Martin KaFai Lau590a0082020-01-08 16:35:14 -08003492 if (bpf_map__is_struct_ops(map))
3493 create_attr.btf_vmlinux_value_type_id =
3494 map->btf_vmlinux_value_type_id;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07003495
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07003496 if (obj->btf && !bpf_map_find_btf_info(obj, map)) {
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07003497 create_attr.btf_fd = btf__fd(obj->btf);
Martin KaFai Lau61746dbe2018-05-22 15:04:24 -07003498 create_attr.btf_key_type_id = map->btf_key_type_id;
3499 create_attr.btf_value_type_id = map->btf_value_type_id;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07003500 }
3501
3502 *pfd = bpf_create_map_xattr(&create_attr);
Andrii Nakryikoe55d54f2019-06-12 22:04:57 -07003503 if (*pfd < 0 && (create_attr.btf_key_type_id ||
3504 create_attr.btf_value_type_id)) {
Andrii Nakryikod7ff34d2019-07-06 11:06:25 -07003505 err = -errno;
3506 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
Kefeng Wangbe180102019-10-21 13:55:32 +08003507 pr_warn("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n",
3508 map->name, cp, err);
Andrii Nakryikoe55d54f2019-06-12 22:04:57 -07003509 create_attr.btf_fd = 0;
Martin KaFai Lau61746dbe2018-05-22 15:04:24 -07003510 create_attr.btf_key_type_id = 0;
3511 create_attr.btf_value_type_id = 0;
3512 map->btf_key_type_id = 0;
3513 map->btf_value_type_id = 0;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07003514 *pfd = bpf_create_map_xattr(&create_attr);
3515 }
3516
Wang Nan52d33522015-07-01 02:14:04 +00003517 if (*pfd < 0) {
3518 size_t j;
Wang Nan52d33522015-07-01 02:14:04 +00003519
Andrii Nakryikod7ff34d2019-07-06 11:06:25 -07003520 err = -errno;
Daniel Borkmannd8599002019-04-09 23:20:13 +02003521err_out:
Andrii Nakryikod7ff34d2019-07-06 11:06:25 -07003522 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
Kefeng Wangbe180102019-10-21 13:55:32 +08003523 pr_warn("failed to create map (name: '%s'): %s(%d)\n",
3524 map->name, cp, err);
Toke Høiland-Jørgensendc3a2d22019-12-16 19:12:04 +01003525 pr_perm_msg(err);
Wang Nan52d33522015-07-01 02:14:04 +00003526 for (j = 0; j < i; j++)
Wang Nan9d759a92015-11-27 08:47:35 +00003527 zclose(obj->maps[j].fd);
Wang Nan52d33522015-07-01 02:14:04 +00003528 return err;
3529 }
Daniel Borkmannd8599002019-04-09 23:20:13 +02003530
3531 if (bpf_map__is_internal(map)) {
3532 err = bpf_object__populate_internal_map(obj, map);
3533 if (err < 0) {
3534 zclose(*pfd);
3535 goto err_out;
3536 }
3537 }
3538
Toke Høiland-Jørgensen57a00f42019-11-02 12:09:41 +01003539 if (map->pin_path && !map->pinned) {
3540 err = bpf_map__pin(map, NULL);
3541 if (err) {
3542 pr_warn("failed to auto-pin map name '%s' at '%s'\n",
3543 map->name, map->pin_path);
3544 return err;
3545 }
3546 }
3547
Andrii Nakryiko76e10222019-05-29 10:36:10 -07003548 pr_debug("created map %s: fd=%d\n", map->name, *pfd);
Wang Nan52d33522015-07-01 02:14:04 +00003549 }
3550
Wang Nan52d33522015-07-01 02:14:04 +00003551 return 0;
3552}
3553
Wang Nan8a47a6c2015-07-01 02:14:05 +00003554static int
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08003555check_btf_ext_reloc_err(struct bpf_program *prog, int err,
3556 void *btf_prog_info, const char *info_name)
3557{
3558 if (err != -ENOENT) {
Kefeng Wangbe180102019-10-21 13:55:32 +08003559 pr_warn("Error in loading %s for sec %s.\n",
3560 info_name, prog->section_name);
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08003561 return err;
3562 }
3563
3564 /* err == -ENOENT (i.e. prog->section_name not found in btf_ext) */
3565
3566 if (btf_prog_info) {
3567 /*
3568 * Some info has already been found but has problem
Andrii Nakryiko399dc652019-05-29 10:36:11 -07003569 * in the last btf_ext reloc. Must have to error out.
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08003570 */
Kefeng Wangbe180102019-10-21 13:55:32 +08003571 pr_warn("Error in relocating %s for sec %s.\n",
3572 info_name, prog->section_name);
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08003573 return err;
3574 }
3575
Andrii Nakryiko399dc652019-05-29 10:36:11 -07003576 /* Have problem loading the very first info. Ignore the rest. */
Kefeng Wangbe180102019-10-21 13:55:32 +08003577 pr_warn("Cannot find %s for main program sec %s. Ignore all %s.\n",
3578 info_name, prog->section_name, info_name);
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08003579 return 0;
3580}
3581
3582static int
3583bpf_program_reloc_btf_ext(struct bpf_program *prog, struct bpf_object *obj,
3584 const char *section_name, __u32 insn_offset)
3585{
3586 int err;
3587
3588 if (!insn_offset || prog->func_info) {
3589 /*
3590 * !insn_offset => main program
3591 *
3592 * For sub prog, the main program's func_info has to
3593 * be loaded first (i.e. prog->func_info != NULL)
3594 */
3595 err = btf_ext__reloc_func_info(obj->btf, obj->btf_ext,
3596 section_name, insn_offset,
3597 &prog->func_info,
3598 &prog->func_info_cnt);
3599 if (err)
3600 return check_btf_ext_reloc_err(prog, err,
3601 prog->func_info,
3602 "bpf_func_info");
3603
3604 prog->func_info_rec_size = btf_ext__func_info_rec_size(obj->btf_ext);
3605 }
3606
Martin KaFai Lau3d650142018-12-07 16:42:31 -08003607 if (!insn_offset || prog->line_info) {
3608 err = btf_ext__reloc_line_info(obj->btf, obj->btf_ext,
3609 section_name, insn_offset,
3610 &prog->line_info,
3611 &prog->line_info_cnt);
3612 if (err)
3613 return check_btf_ext_reloc_err(prog, err,
3614 prog->line_info,
3615 "bpf_line_info");
3616
3617 prog->line_info_rec_size = btf_ext__line_info_rec_size(obj->btf_ext);
3618 }
3619
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08003620 return 0;
3621}
3622
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07003623#define BPF_CORE_SPEC_MAX_LEN 64
3624
3625/* represents BPF CO-RE field or array element accessor */
3626struct bpf_core_accessor {
3627 __u32 type_id; /* struct/union type or array element type */
3628 __u32 idx; /* field index or array index */
3629 const char *name; /* field name or NULL for array accessor */
3630};
3631
3632struct bpf_core_spec {
3633 const struct btf *btf;
3634 /* high-level spec: named fields and array indices only */
3635 struct bpf_core_accessor spec[BPF_CORE_SPEC_MAX_LEN];
3636 /* high-level spec length */
3637 int len;
3638 /* raw, low-level spec: 1-to-1 with accessor spec string */
3639 int raw_spec[BPF_CORE_SPEC_MAX_LEN];
3640 /* raw spec length */
3641 int raw_len;
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07003642 /* field bit offset represented by spec */
3643 __u32 bit_offset;
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07003644};
3645
3646static bool str_is_empty(const char *s)
3647{
3648 return !s || !s[0];
3649}
3650
Andrii Nakryiko1b484b32019-12-14 23:08:43 -08003651static bool is_flex_arr(const struct btf *btf,
3652 const struct bpf_core_accessor *acc,
3653 const struct btf_array *arr)
3654{
3655 const struct btf_type *t;
3656
3657 /* not a flexible array, if not inside a struct or has non-zero size */
3658 if (!acc->name || arr->nelems > 0)
3659 return false;
3660
3661 /* has to be the last member of enclosing struct */
3662 t = btf__type_by_id(btf, acc->type_id);
3663 return acc->idx == btf_vlen(t) - 1;
3664}
3665
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07003666/*
Andrii Nakryiko511bb002019-10-15 11:28:45 -07003667 * Turn bpf_field_reloc into a low- and high-level spec representation,
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07003668 * validating correctness along the way, as well as calculating resulting
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07003669 * field bit offset, specified by accessor string. Low-level spec captures
3670 * every single level of nestedness, including traversing anonymous
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07003671 * struct/union members. High-level one only captures semantically meaningful
3672 * "turning points": named fields and array indicies.
3673 * E.g., for this case:
3674 *
3675 * struct sample {
3676 * int __unimportant;
3677 * struct {
3678 * int __1;
3679 * int __2;
3680 * int a[7];
3681 * };
3682 * };
3683 *
3684 * struct sample *s = ...;
3685 *
3686 * int x = &s->a[3]; // access string = '0:1:2:3'
3687 *
3688 * Low-level spec has 1:1 mapping with each element of access string (it's
3689 * just a parsed access string representation): [0, 1, 2, 3].
3690 *
3691 * High-level spec will capture only 3 points:
3692 * - intial zero-index access by pointer (&s->... is the same as &s[0]...);
3693 * - field 'a' access (corresponds to '2' in low-level spec);
3694 * - array element #3 access (corresponds to '3' in low-level spec).
3695 *
3696 */
3697static int bpf_core_spec_parse(const struct btf *btf,
3698 __u32 type_id,
3699 const char *spec_str,
3700 struct bpf_core_spec *spec)
3701{
3702 int access_idx, parsed_len, i;
Andrii Nakryiko1b484b32019-12-14 23:08:43 -08003703 struct bpf_core_accessor *acc;
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07003704 const struct btf_type *t;
3705 const char *name;
3706 __u32 id;
3707 __s64 sz;
3708
3709 if (str_is_empty(spec_str) || *spec_str == ':')
3710 return -EINVAL;
3711
3712 memset(spec, 0, sizeof(*spec));
3713 spec->btf = btf;
3714
3715 /* parse spec_str="0:1:2:3:4" into array raw_spec=[0, 1, 2, 3, 4] */
3716 while (*spec_str) {
3717 if (*spec_str == ':')
3718 ++spec_str;
3719 if (sscanf(spec_str, "%d%n", &access_idx, &parsed_len) != 1)
3720 return -EINVAL;
3721 if (spec->raw_len == BPF_CORE_SPEC_MAX_LEN)
3722 return -E2BIG;
3723 spec_str += parsed_len;
3724 spec->raw_spec[spec->raw_len++] = access_idx;
3725 }
3726
3727 if (spec->raw_len == 0)
3728 return -EINVAL;
3729
3730 /* first spec value is always reloc type array index */
3731 t = skip_mods_and_typedefs(btf, type_id, &id);
3732 if (!t)
3733 return -EINVAL;
3734
3735 access_idx = spec->raw_spec[0];
3736 spec->spec[0].type_id = id;
3737 spec->spec[0].idx = access_idx;
3738 spec->len++;
3739
3740 sz = btf__resolve_size(btf, id);
3741 if (sz < 0)
3742 return sz;
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07003743 spec->bit_offset = access_idx * sz * 8;
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07003744
3745 for (i = 1; i < spec->raw_len; i++) {
3746 t = skip_mods_and_typedefs(btf, id, &id);
3747 if (!t)
3748 return -EINVAL;
3749
3750 access_idx = spec->raw_spec[i];
Andrii Nakryiko1b484b32019-12-14 23:08:43 -08003751 acc = &spec->spec[spec->len];
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07003752
3753 if (btf_is_composite(t)) {
3754 const struct btf_member *m;
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07003755 __u32 bit_offset;
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07003756
3757 if (access_idx >= btf_vlen(t))
3758 return -EINVAL;
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07003759
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07003760 bit_offset = btf_member_bit_offset(t, access_idx);
3761 spec->bit_offset += bit_offset;
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07003762
3763 m = btf_members(t) + access_idx;
3764 if (m->name_off) {
3765 name = btf__name_by_offset(btf, m->name_off);
3766 if (str_is_empty(name))
3767 return -EINVAL;
3768
Andrii Nakryiko1b484b32019-12-14 23:08:43 -08003769 acc->type_id = id;
3770 acc->idx = access_idx;
3771 acc->name = name;
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07003772 spec->len++;
3773 }
3774
3775 id = m->type;
3776 } else if (btf_is_array(t)) {
3777 const struct btf_array *a = btf_array(t);
Andrii Nakryiko1b484b32019-12-14 23:08:43 -08003778 bool flex;
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07003779
3780 t = skip_mods_and_typedefs(btf, a->type, &id);
Andrii Nakryiko1b484b32019-12-14 23:08:43 -08003781 if (!t)
3782 return -EINVAL;
3783
3784 flex = is_flex_arr(btf, acc - 1, a);
3785 if (!flex && access_idx >= a->nelems)
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07003786 return -EINVAL;
3787
3788 spec->spec[spec->len].type_id = id;
3789 spec->spec[spec->len].idx = access_idx;
3790 spec->len++;
3791
3792 sz = btf__resolve_size(btf, id);
3793 if (sz < 0)
3794 return sz;
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07003795 spec->bit_offset += access_idx * sz * 8;
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07003796 } else {
Kefeng Wangbe180102019-10-21 13:55:32 +08003797 pr_warn("relo for [%u] %s (at idx %d) captures type [%d] of unexpected kind %d\n",
3798 type_id, spec_str, i, id, btf_kind(t));
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07003799 return -EINVAL;
3800 }
3801 }
3802
3803 return 0;
3804}
3805
3806static bool bpf_core_is_flavor_sep(const char *s)
3807{
3808 /* check X___Y name pattern, where X and Y are not underscores */
3809 return s[0] != '_' && /* X */
3810 s[1] == '_' && s[2] == '_' && s[3] == '_' && /* ___ */
3811 s[4] != '_'; /* Y */
3812}
3813
3814/* Given 'some_struct_name___with_flavor' return the length of a name prefix
3815 * before last triple underscore. Struct name part after last triple
3816 * underscore is ignored by BPF CO-RE relocation during relocation matching.
3817 */
3818static size_t bpf_core_essential_name_len(const char *name)
3819{
3820 size_t n = strlen(name);
3821 int i;
3822
3823 for (i = n - 5; i >= 0; i--) {
3824 if (bpf_core_is_flavor_sep(name + i))
3825 return i + 1;
3826 }
3827 return n;
3828}
3829
3830/* dynamically sized list of type IDs */
3831struct ids_vec {
3832 __u32 *data;
3833 int len;
3834};
3835
3836static void bpf_core_free_cands(struct ids_vec *cand_ids)
3837{
3838 free(cand_ids->data);
3839 free(cand_ids);
3840}
3841
3842static struct ids_vec *bpf_core_find_cands(const struct btf *local_btf,
3843 __u32 local_type_id,
3844 const struct btf *targ_btf)
3845{
3846 size_t local_essent_len, targ_essent_len;
3847 const char *local_name, *targ_name;
3848 const struct btf_type *t;
3849 struct ids_vec *cand_ids;
3850 __u32 *new_ids;
3851 int i, err, n;
3852
3853 t = btf__type_by_id(local_btf, local_type_id);
3854 if (!t)
3855 return ERR_PTR(-EINVAL);
3856
3857 local_name = btf__name_by_offset(local_btf, t->name_off);
3858 if (str_is_empty(local_name))
3859 return ERR_PTR(-EINVAL);
3860 local_essent_len = bpf_core_essential_name_len(local_name);
3861
3862 cand_ids = calloc(1, sizeof(*cand_ids));
3863 if (!cand_ids)
3864 return ERR_PTR(-ENOMEM);
3865
3866 n = btf__get_nr_types(targ_btf);
3867 for (i = 1; i <= n; i++) {
3868 t = btf__type_by_id(targ_btf, i);
3869 targ_name = btf__name_by_offset(targ_btf, t->name_off);
3870 if (str_is_empty(targ_name))
3871 continue;
3872
Andrii Nakryikod121e1d2020-03-13 10:23:34 -07003873 t = skip_mods_and_typedefs(targ_btf, i, NULL);
3874 if (!btf_is_composite(t) && !btf_is_array(t))
3875 continue;
3876
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07003877 targ_essent_len = bpf_core_essential_name_len(targ_name);
3878 if (targ_essent_len != local_essent_len)
3879 continue;
3880
3881 if (strncmp(local_name, targ_name, local_essent_len) == 0) {
3882 pr_debug("[%d] %s: found candidate [%d] %s\n",
3883 local_type_id, local_name, i, targ_name);
Andrii Nakryiko35b92112020-01-24 12:18:46 -08003884 new_ids = reallocarray(cand_ids->data,
3885 cand_ids->len + 1,
3886 sizeof(*cand_ids->data));
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07003887 if (!new_ids) {
3888 err = -ENOMEM;
3889 goto err_out;
3890 }
3891 cand_ids->data = new_ids;
3892 cand_ids->data[cand_ids->len++] = i;
3893 }
3894 }
3895 return cand_ids;
3896err_out:
3897 bpf_core_free_cands(cand_ids);
3898 return ERR_PTR(err);
3899}
3900
3901/* Check two types for compatibility, skipping const/volatile/restrict and
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07003902 * typedefs, to ensure we are relocating compatible entities:
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07003903 * - any two STRUCTs/UNIONs are compatible and can be mixed;
Andrii Nakryiko94f060e2019-11-01 15:28:08 -07003904 * - any two FWDs are compatible, if their names match (modulo flavor suffix);
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07003905 * - any two PTRs are always compatible;
Andrii Nakryiko94f060e2019-11-01 15:28:08 -07003906 * - for ENUMs, names should be the same (ignoring flavor suffix) or at
3907 * least one of enums should be anonymous;
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07003908 * - for ENUMs, check sizes, names are ignored;
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07003909 * - for INT, size and signedness are ignored;
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07003910 * - for ARRAY, dimensionality is ignored, element types are checked for
3911 * compatibility recursively;
3912 * - everything else shouldn't be ever a target of relocation.
3913 * These rules are not set in stone and probably will be adjusted as we get
3914 * more experience with using BPF CO-RE relocations.
3915 */
3916static int bpf_core_fields_are_compat(const struct btf *local_btf,
3917 __u32 local_id,
3918 const struct btf *targ_btf,
3919 __u32 targ_id)
3920{
3921 const struct btf_type *local_type, *targ_type;
3922
3923recur:
3924 local_type = skip_mods_and_typedefs(local_btf, local_id, &local_id);
3925 targ_type = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id);
3926 if (!local_type || !targ_type)
3927 return -EINVAL;
3928
3929 if (btf_is_composite(local_type) && btf_is_composite(targ_type))
3930 return 1;
3931 if (btf_kind(local_type) != btf_kind(targ_type))
3932 return 0;
3933
3934 switch (btf_kind(local_type)) {
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07003935 case BTF_KIND_PTR:
3936 return 1;
Andrii Nakryiko94f060e2019-11-01 15:28:08 -07003937 case BTF_KIND_FWD:
3938 case BTF_KIND_ENUM: {
3939 const char *local_name, *targ_name;
3940 size_t local_len, targ_len;
3941
3942 local_name = btf__name_by_offset(local_btf,
3943 local_type->name_off);
3944 targ_name = btf__name_by_offset(targ_btf, targ_type->name_off);
3945 local_len = bpf_core_essential_name_len(local_name);
3946 targ_len = bpf_core_essential_name_len(targ_name);
3947 /* one of them is anonymous or both w/ same flavor-less names */
3948 return local_len == 0 || targ_len == 0 ||
3949 (local_len == targ_len &&
3950 strncmp(local_name, targ_name, local_len) == 0);
3951 }
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07003952 case BTF_KIND_INT:
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07003953 /* just reject deprecated bitfield-like integers; all other
3954 * integers are by default compatible between each other
3955 */
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07003956 return btf_int_offset(local_type) == 0 &&
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07003957 btf_int_offset(targ_type) == 0;
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07003958 case BTF_KIND_ARRAY:
3959 local_id = btf_array(local_type)->type;
3960 targ_id = btf_array(targ_type)->type;
3961 goto recur;
3962 default:
Kefeng Wangbe180102019-10-21 13:55:32 +08003963 pr_warn("unexpected kind %d relocated, local [%d], target [%d]\n",
3964 btf_kind(local_type), local_id, targ_id);
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07003965 return 0;
3966 }
3967}
3968
3969/*
3970 * Given single high-level named field accessor in local type, find
3971 * corresponding high-level accessor for a target type. Along the way,
3972 * maintain low-level spec for target as well. Also keep updating target
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07003973 * bit offset.
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07003974 *
3975 * Searching is performed through recursive exhaustive enumeration of all
3976 * fields of a struct/union. If there are any anonymous (embedded)
3977 * structs/unions, they are recursively searched as well. If field with
3978 * desired name is found, check compatibility between local and target types,
3979 * before returning result.
3980 *
3981 * 1 is returned, if field is found.
3982 * 0 is returned if no compatible field is found.
3983 * <0 is returned on error.
3984 */
3985static int bpf_core_match_member(const struct btf *local_btf,
3986 const struct bpf_core_accessor *local_acc,
3987 const struct btf *targ_btf,
3988 __u32 targ_id,
3989 struct bpf_core_spec *spec,
3990 __u32 *next_targ_id)
3991{
3992 const struct btf_type *local_type, *targ_type;
3993 const struct btf_member *local_member, *m;
3994 const char *local_name, *targ_name;
3995 __u32 local_id;
3996 int i, n, found;
3997
3998 targ_type = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id);
3999 if (!targ_type)
4000 return -EINVAL;
4001 if (!btf_is_composite(targ_type))
4002 return 0;
4003
4004 local_id = local_acc->type_id;
4005 local_type = btf__type_by_id(local_btf, local_id);
4006 local_member = btf_members(local_type) + local_acc->idx;
4007 local_name = btf__name_by_offset(local_btf, local_member->name_off);
4008
4009 n = btf_vlen(targ_type);
4010 m = btf_members(targ_type);
4011 for (i = 0; i < n; i++, m++) {
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07004012 __u32 bit_offset;
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004013
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07004014 bit_offset = btf_member_bit_offset(targ_type, i);
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004015
4016 /* too deep struct/union/array nesting */
4017 if (spec->raw_len == BPF_CORE_SPEC_MAX_LEN)
4018 return -E2BIG;
4019
4020 /* speculate this member will be the good one */
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07004021 spec->bit_offset += bit_offset;
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004022 spec->raw_spec[spec->raw_len++] = i;
4023
4024 targ_name = btf__name_by_offset(targ_btf, m->name_off);
4025 if (str_is_empty(targ_name)) {
4026 /* embedded struct/union, we need to go deeper */
4027 found = bpf_core_match_member(local_btf, local_acc,
4028 targ_btf, m->type,
4029 spec, next_targ_id);
4030 if (found) /* either found or error */
4031 return found;
4032 } else if (strcmp(local_name, targ_name) == 0) {
4033 /* matching named field */
4034 struct bpf_core_accessor *targ_acc;
4035
4036 targ_acc = &spec->spec[spec->len++];
4037 targ_acc->type_id = targ_id;
4038 targ_acc->idx = i;
4039 targ_acc->name = targ_name;
4040
4041 *next_targ_id = m->type;
4042 found = bpf_core_fields_are_compat(local_btf,
4043 local_member->type,
4044 targ_btf, m->type);
4045 if (!found)
4046 spec->len--; /* pop accessor */
4047 return found;
4048 }
4049 /* member turned out not to be what we looked for */
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07004050 spec->bit_offset -= bit_offset;
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004051 spec->raw_len--;
4052 }
4053
4054 return 0;
4055}
4056
4057/*
4058 * Try to match local spec to a target type and, if successful, produce full
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07004059 * target spec (high-level, low-level + bit offset).
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004060 */
4061static int bpf_core_spec_match(struct bpf_core_spec *local_spec,
4062 const struct btf *targ_btf, __u32 targ_id,
4063 struct bpf_core_spec *targ_spec)
4064{
4065 const struct btf_type *targ_type;
4066 const struct bpf_core_accessor *local_acc;
4067 struct bpf_core_accessor *targ_acc;
4068 int i, sz, matched;
4069
4070 memset(targ_spec, 0, sizeof(*targ_spec));
4071 targ_spec->btf = targ_btf;
4072
4073 local_acc = &local_spec->spec[0];
4074 targ_acc = &targ_spec->spec[0];
4075
4076 for (i = 0; i < local_spec->len; i++, local_acc++, targ_acc++) {
4077 targ_type = skip_mods_and_typedefs(targ_spec->btf, targ_id,
4078 &targ_id);
4079 if (!targ_type)
4080 return -EINVAL;
4081
4082 if (local_acc->name) {
4083 matched = bpf_core_match_member(local_spec->btf,
4084 local_acc,
4085 targ_btf, targ_id,
4086 targ_spec, &targ_id);
4087 if (matched <= 0)
4088 return matched;
4089 } else {
4090 /* for i=0, targ_id is already treated as array element
4091 * type (because it's the original struct), for others
4092 * we should find array element type first
4093 */
4094 if (i > 0) {
4095 const struct btf_array *a;
Andrii Nakryiko1b484b32019-12-14 23:08:43 -08004096 bool flex;
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004097
4098 if (!btf_is_array(targ_type))
4099 return 0;
4100
4101 a = btf_array(targ_type);
Andrii Nakryiko1b484b32019-12-14 23:08:43 -08004102 flex = is_flex_arr(targ_btf, targ_acc - 1, a);
4103 if (!flex && local_acc->idx >= a->nelems)
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004104 return 0;
4105 if (!skip_mods_and_typedefs(targ_btf, a->type,
4106 &targ_id))
4107 return -EINVAL;
4108 }
4109
4110 /* too deep struct/union/array nesting */
4111 if (targ_spec->raw_len == BPF_CORE_SPEC_MAX_LEN)
4112 return -E2BIG;
4113
4114 targ_acc->type_id = targ_id;
4115 targ_acc->idx = local_acc->idx;
4116 targ_acc->name = NULL;
4117 targ_spec->len++;
4118 targ_spec->raw_spec[targ_spec->raw_len] = targ_acc->idx;
4119 targ_spec->raw_len++;
4120
4121 sz = btf__resolve_size(targ_btf, targ_id);
4122 if (sz < 0)
4123 return sz;
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07004124 targ_spec->bit_offset += local_acc->idx * sz * 8;
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004125 }
4126 }
4127
4128 return 1;
4129}
4130
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07004131static int bpf_core_calc_field_relo(const struct bpf_program *prog,
4132 const struct bpf_field_reloc *relo,
4133 const struct bpf_core_spec *spec,
4134 __u32 *val, bool *validate)
4135{
4136 const struct bpf_core_accessor *acc = &spec->spec[spec->len - 1];
4137 const struct btf_type *t = btf__type_by_id(spec->btf, acc->type_id);
4138 __u32 byte_off, byte_sz, bit_off, bit_sz;
4139 const struct btf_member *m;
4140 const struct btf_type *mt;
4141 bool bitfield;
Andrii Nakryiko94f060e2019-11-01 15:28:08 -07004142 __s64 sz;
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07004143
4144 /* a[n] accessor needs special handling */
4145 if (!acc->name) {
Andrii Nakryiko94f060e2019-11-01 15:28:08 -07004146 if (relo->kind == BPF_FIELD_BYTE_OFFSET) {
4147 *val = spec->bit_offset / 8;
4148 } else if (relo->kind == BPF_FIELD_BYTE_SIZE) {
4149 sz = btf__resolve_size(spec->btf, acc->type_id);
4150 if (sz < 0)
4151 return -EINVAL;
4152 *val = sz;
4153 } else {
4154 pr_warn("prog '%s': relo %d at insn #%d can't be applied to array access\n",
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07004155 bpf_program__title(prog, false),
4156 relo->kind, relo->insn_off / 8);
4157 return -EINVAL;
4158 }
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07004159 if (validate)
4160 *validate = true;
4161 return 0;
4162 }
4163
4164 m = btf_members(t) + acc->idx;
4165 mt = skip_mods_and_typedefs(spec->btf, m->type, NULL);
4166 bit_off = spec->bit_offset;
4167 bit_sz = btf_member_bitfield_size(t, acc->idx);
4168
4169 bitfield = bit_sz > 0;
4170 if (bitfield) {
4171 byte_sz = mt->size;
4172 byte_off = bit_off / 8 / byte_sz * byte_sz;
4173 /* figure out smallest int size necessary for bitfield load */
4174 while (bit_off + bit_sz - byte_off * 8 > byte_sz * 8) {
4175 if (byte_sz >= 8) {
4176 /* bitfield can't be read with 64-bit read */
4177 pr_warn("prog '%s': relo %d at insn #%d can't be satisfied for bitfield\n",
4178 bpf_program__title(prog, false),
4179 relo->kind, relo->insn_off / 8);
4180 return -E2BIG;
4181 }
4182 byte_sz *= 2;
4183 byte_off = bit_off / 8 / byte_sz * byte_sz;
4184 }
4185 } else {
Andrii Nakryiko94f060e2019-11-01 15:28:08 -07004186 sz = btf__resolve_size(spec->btf, m->type);
4187 if (sz < 0)
4188 return -EINVAL;
4189 byte_sz = sz;
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07004190 byte_off = spec->bit_offset / 8;
4191 bit_sz = byte_sz * 8;
4192 }
4193
4194 /* for bitfields, all the relocatable aspects are ambiguous and we
4195 * might disagree with compiler, so turn off validation of expected
4196 * value, except for signedness
4197 */
4198 if (validate)
4199 *validate = !bitfield;
4200
4201 switch (relo->kind) {
4202 case BPF_FIELD_BYTE_OFFSET:
4203 *val = byte_off;
4204 break;
4205 case BPF_FIELD_BYTE_SIZE:
4206 *val = byte_sz;
4207 break;
4208 case BPF_FIELD_SIGNED:
4209 /* enums will be assumed unsigned */
4210 *val = btf_is_enum(mt) ||
4211 (btf_int_encoding(mt) & BTF_INT_SIGNED);
4212 if (validate)
4213 *validate = true; /* signedness is never ambiguous */
4214 break;
4215 case BPF_FIELD_LSHIFT_U64:
4216#if __BYTE_ORDER == __LITTLE_ENDIAN
4217 *val = 64 - (bit_off + bit_sz - byte_off * 8);
4218#else
4219 *val = (8 - byte_sz) * 8 + (bit_off - byte_off * 8);
4220#endif
4221 break;
4222 case BPF_FIELD_RSHIFT_U64:
4223 *val = 64 - bit_sz;
4224 if (validate)
4225 *validate = true; /* right shift is never ambiguous */
4226 break;
4227 case BPF_FIELD_EXISTS:
4228 default:
4229 pr_warn("prog '%s': unknown relo %d at insn #%d\n",
4230 bpf_program__title(prog, false),
4231 relo->kind, relo->insn_off / 8);
4232 return -EINVAL;
4233 }
4234
4235 return 0;
4236}
4237
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004238/*
4239 * Patch relocatable BPF instruction.
Andrii Nakryiko62561eb2019-10-15 11:28:47 -07004240 *
4241 * Patched value is determined by relocation kind and target specification.
4242 * For field existence relocation target spec will be NULL if field is not
4243 * found.
4244 * Expected insn->imm value is determined using relocation kind and local
4245 * spec, and is checked before patching instruction. If actual insn->imm value
4246 * is wrong, bail out with error.
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004247 *
4248 * Currently three kinds of BPF instructions are supported:
4249 * 1. rX = <imm> (assignment with immediate operand);
4250 * 2. rX += <imm> (arithmetic operations with immediate operand);
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004251 */
Andrii Nakryiko62561eb2019-10-15 11:28:47 -07004252static int bpf_core_reloc_insn(struct bpf_program *prog,
4253 const struct bpf_field_reloc *relo,
Andrii Nakryikod7a25272020-01-23 21:38:37 -08004254 int relo_idx,
Andrii Nakryiko62561eb2019-10-15 11:28:47 -07004255 const struct bpf_core_spec *local_spec,
4256 const struct bpf_core_spec *targ_spec)
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004257{
Andrii Nakryiko62561eb2019-10-15 11:28:47 -07004258 __u32 orig_val, new_val;
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004259 struct bpf_insn *insn;
Andrii Nakryikod7a25272020-01-23 21:38:37 -08004260 bool validate = true;
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07004261 int insn_idx, err;
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004262 __u8 class;
4263
Andrii Nakryiko62561eb2019-10-15 11:28:47 -07004264 if (relo->insn_off % sizeof(struct bpf_insn))
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004265 return -EINVAL;
Andrii Nakryiko62561eb2019-10-15 11:28:47 -07004266 insn_idx = relo->insn_off / sizeof(struct bpf_insn);
Andrii Nakryikod7a25272020-01-23 21:38:37 -08004267 insn = &prog->insns[insn_idx];
4268 class = BPF_CLASS(insn->code);
Andrii Nakryiko62561eb2019-10-15 11:28:47 -07004269
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07004270 if (relo->kind == BPF_FIELD_EXISTS) {
Andrii Nakryiko62561eb2019-10-15 11:28:47 -07004271 orig_val = 1; /* can't generate EXISTS relo w/o local field */
4272 new_val = targ_spec ? 1 : 0;
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07004273 } else if (!targ_spec) {
Andrii Nakryikod7a25272020-01-23 21:38:37 -08004274 pr_debug("prog '%s': relo #%d: substituting insn #%d w/ invalid insn\n",
4275 bpf_program__title(prog, false), relo_idx, insn_idx);
4276 insn->code = BPF_JMP | BPF_CALL;
4277 insn->dst_reg = 0;
4278 insn->src_reg = 0;
4279 insn->off = 0;
4280 /* if this instruction is reachable (not a dead code),
4281 * verifier will complain with the following message:
4282 * invalid func unknown#195896080
4283 */
4284 insn->imm = 195896080; /* => 0xbad2310 => "bad relo" */
4285 return 0;
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07004286 } else {
4287 err = bpf_core_calc_field_relo(prog, relo, local_spec,
4288 &orig_val, &validate);
4289 if (err)
4290 return err;
4291 err = bpf_core_calc_field_relo(prog, relo, targ_spec,
4292 &new_val, NULL);
4293 if (err)
4294 return err;
Andrii Nakryiko62561eb2019-10-15 11:28:47 -07004295 }
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004296
Andrii Nakryiko8ab9da52019-12-23 10:03:05 -08004297 switch (class) {
4298 case BPF_ALU:
4299 case BPF_ALU64:
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004300 if (BPF_SRC(insn->code) != BPF_K)
4301 return -EINVAL;
Andrii Nakryikod7a25272020-01-23 21:38:37 -08004302 if (validate && insn->imm != orig_val) {
4303 pr_warn("prog '%s': relo #%d: unexpected insn #%d (ALU/ALU64) value: got %u, exp %u -> %u\n",
4304 bpf_program__title(prog, false), relo_idx,
4305 insn_idx, insn->imm, orig_val, new_val);
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004306 return -EINVAL;
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07004307 }
4308 orig_val = insn->imm;
Andrii Nakryiko62561eb2019-10-15 11:28:47 -07004309 insn->imm = new_val;
Andrii Nakryikod7a25272020-01-23 21:38:37 -08004310 pr_debug("prog '%s': relo #%d: patched insn #%d (ALU/ALU64) imm %u -> %u\n",
4311 bpf_program__title(prog, false), relo_idx, insn_idx,
4312 orig_val, new_val);
Andrii Nakryiko8ab9da52019-12-23 10:03:05 -08004313 break;
4314 case BPF_LDX:
4315 case BPF_ST:
4316 case BPF_STX:
Andrii Nakryikod7a25272020-01-23 21:38:37 -08004317 if (validate && insn->off != orig_val) {
4318 pr_warn("prog '%s': relo #%d: unexpected insn #%d (LD/LDX/ST/STX) value: got %u, exp %u -> %u\n",
4319 bpf_program__title(prog, false), relo_idx,
4320 insn_idx, insn->off, orig_val, new_val);
Andrii Nakryiko8ab9da52019-12-23 10:03:05 -08004321 return -EINVAL;
4322 }
4323 if (new_val > SHRT_MAX) {
Andrii Nakryikod7a25272020-01-23 21:38:37 -08004324 pr_warn("prog '%s': relo #%d: insn #%d (LDX/ST/STX) value too big: %u\n",
4325 bpf_program__title(prog, false), relo_idx,
4326 insn_idx, new_val);
Andrii Nakryiko8ab9da52019-12-23 10:03:05 -08004327 return -ERANGE;
4328 }
4329 orig_val = insn->off;
4330 insn->off = new_val;
Andrii Nakryikod7a25272020-01-23 21:38:37 -08004331 pr_debug("prog '%s': relo #%d: patched insn #%d (LDX/ST/STX) off %u -> %u\n",
4332 bpf_program__title(prog, false), relo_idx, insn_idx,
4333 orig_val, new_val);
Andrii Nakryiko8ab9da52019-12-23 10:03:05 -08004334 break;
4335 default:
Andrii Nakryikod7a25272020-01-23 21:38:37 -08004336 pr_warn("prog '%s': relo #%d: trying to relocate unrecognized insn #%d, code:%x, src:%x, dst:%x, off:%x, imm:%x\n",
4337 bpf_program__title(prog, false), relo_idx,
Kefeng Wangbe180102019-10-21 13:55:32 +08004338 insn_idx, insn->code, insn->src_reg, insn->dst_reg,
4339 insn->off, insn->imm);
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004340 return -EINVAL;
4341 }
Andrii Nakryiko62561eb2019-10-15 11:28:47 -07004342
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004343 return 0;
4344}
4345
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004346/* Output spec definition in the format:
4347 * [<type-id>] (<type-name>) + <raw-spec> => <offset>@<spec>,
4348 * where <spec> is a C-syntax view of recorded field access, e.g.: x.a[3].b
4349 */
4350static void bpf_core_dump_spec(int level, const struct bpf_core_spec *spec)
4351{
4352 const struct btf_type *t;
4353 const char *s;
4354 __u32 type_id;
4355 int i;
4356
4357 type_id = spec->spec[0].type_id;
4358 t = btf__type_by_id(spec->btf, type_id);
4359 s = btf__name_by_offset(spec->btf, t->name_off);
4360 libbpf_print(level, "[%u] %s + ", type_id, s);
4361
4362 for (i = 0; i < spec->raw_len; i++)
4363 libbpf_print(level, "%d%s", spec->raw_spec[i],
4364 i == spec->raw_len - 1 ? " => " : ":");
4365
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07004366 libbpf_print(level, "%u.%u @ &x",
4367 spec->bit_offset / 8, spec->bit_offset % 8);
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004368
4369 for (i = 0; i < spec->len; i++) {
4370 if (spec->spec[i].name)
4371 libbpf_print(level, ".%s", spec->spec[i].name);
4372 else
4373 libbpf_print(level, "[%u]", spec->spec[i].idx);
4374 }
4375
4376}
4377
4378static size_t bpf_core_hash_fn(const void *key, void *ctx)
4379{
4380 return (size_t)key;
4381}
4382
4383static bool bpf_core_equal_fn(const void *k1, const void *k2, void *ctx)
4384{
4385 return k1 == k2;
4386}
4387
4388static void *u32_as_hash_key(__u32 x)
4389{
4390 return (void *)(uintptr_t)x;
4391}
4392
4393/*
4394 * CO-RE relocate single instruction.
4395 *
4396 * The outline and important points of the algorithm:
4397 * 1. For given local type, find corresponding candidate target types.
4398 * Candidate type is a type with the same "essential" name, ignoring
4399 * everything after last triple underscore (___). E.g., `sample`,
4400 * `sample___flavor_one`, `sample___flavor_another_one`, are all candidates
4401 * for each other. Names with triple underscore are referred to as
4402 * "flavors" and are useful, among other things, to allow to
4403 * specify/support incompatible variations of the same kernel struct, which
4404 * might differ between different kernel versions and/or build
4405 * configurations.
4406 *
4407 * N.B. Struct "flavors" could be generated by bpftool's BTF-to-C
4408 * converter, when deduplicated BTF of a kernel still contains more than
4409 * one different types with the same name. In that case, ___2, ___3, etc
4410 * are appended starting from second name conflict. But start flavors are
4411 * also useful to be defined "locally", in BPF program, to extract same
4412 * data from incompatible changes between different kernel
4413 * versions/configurations. For instance, to handle field renames between
4414 * kernel versions, one can use two flavors of the struct name with the
4415 * same common name and use conditional relocations to extract that field,
4416 * depending on target kernel version.
4417 * 2. For each candidate type, try to match local specification to this
4418 * candidate target type. Matching involves finding corresponding
4419 * high-level spec accessors, meaning that all named fields should match,
4420 * as well as all array accesses should be within the actual bounds. Also,
4421 * types should be compatible (see bpf_core_fields_are_compat for details).
4422 * 3. It is supported and expected that there might be multiple flavors
4423 * matching the spec. As long as all the specs resolve to the same set of
Andrii Nakryiko511bb002019-10-15 11:28:45 -07004424 * offsets across all candidates, there is no error. If there is any
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004425 * ambiguity, CO-RE relocation will fail. This is necessary to accomodate
4426 * imprefection of BTF deduplication, which can cause slight duplication of
4427 * the same BTF type, if some directly or indirectly referenced (by
4428 * pointer) type gets resolved to different actual types in different
4429 * object files. If such situation occurs, deduplicated BTF will end up
4430 * with two (or more) structurally identical types, which differ only in
4431 * types they refer to through pointer. This should be OK in most cases and
4432 * is not an error.
4433 * 4. Candidate types search is performed by linearly scanning through all
4434 * types in target BTF. It is anticipated that this is overall more
4435 * efficient memory-wise and not significantly worse (if not better)
4436 * CPU-wise compared to prebuilding a map from all local type names to
4437 * a list of candidate type names. It's also sped up by caching resolved
4438 * list of matching candidates per each local "root" type ID, that has at
Andrii Nakryiko511bb002019-10-15 11:28:45 -07004439 * least one bpf_field_reloc associated with it. This list is shared
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004440 * between multiple relocations for the same type ID and is updated as some
4441 * of the candidates are pruned due to structural incompatibility.
4442 */
Andrii Nakryiko511bb002019-10-15 11:28:45 -07004443static int bpf_core_reloc_field(struct bpf_program *prog,
4444 const struct bpf_field_reloc *relo,
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004445 int relo_idx,
4446 const struct btf *local_btf,
4447 const struct btf *targ_btf,
4448 struct hashmap *cand_cache)
4449{
4450 const char *prog_name = bpf_program__title(prog, false);
4451 struct bpf_core_spec local_spec, cand_spec, targ_spec;
4452 const void *type_key = u32_as_hash_key(relo->type_id);
4453 const struct btf_type *local_type, *cand_type;
4454 const char *local_name, *cand_name;
4455 struct ids_vec *cand_ids;
4456 __u32 local_id, cand_id;
4457 const char *spec_str;
4458 int i, j, err;
4459
4460 local_id = relo->type_id;
4461 local_type = btf__type_by_id(local_btf, local_id);
4462 if (!local_type)
4463 return -EINVAL;
4464
4465 local_name = btf__name_by_offset(local_btf, local_type->name_off);
4466 if (str_is_empty(local_name))
4467 return -EINVAL;
4468
4469 spec_str = btf__name_by_offset(local_btf, relo->access_str_off);
4470 if (str_is_empty(spec_str))
4471 return -EINVAL;
4472
4473 err = bpf_core_spec_parse(local_btf, local_id, spec_str, &local_spec);
4474 if (err) {
Kefeng Wangbe180102019-10-21 13:55:32 +08004475 pr_warn("prog '%s': relo #%d: parsing [%d] %s + %s failed: %d\n",
4476 prog_name, relo_idx, local_id, local_name, spec_str,
4477 err);
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004478 return -EINVAL;
4479 }
4480
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07004481 pr_debug("prog '%s': relo #%d: kind %d, spec is ", prog_name, relo_idx,
4482 relo->kind);
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004483 bpf_core_dump_spec(LIBBPF_DEBUG, &local_spec);
4484 libbpf_print(LIBBPF_DEBUG, "\n");
4485
4486 if (!hashmap__find(cand_cache, type_key, (void **)&cand_ids)) {
4487 cand_ids = bpf_core_find_cands(local_btf, local_id, targ_btf);
4488 if (IS_ERR(cand_ids)) {
Kefeng Wangbe180102019-10-21 13:55:32 +08004489 pr_warn("prog '%s': relo #%d: target candidate search failed for [%d] %s: %ld",
4490 prog_name, relo_idx, local_id, local_name,
4491 PTR_ERR(cand_ids));
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004492 return PTR_ERR(cand_ids);
4493 }
4494 err = hashmap__set(cand_cache, type_key, cand_ids, NULL, NULL);
4495 if (err) {
4496 bpf_core_free_cands(cand_ids);
4497 return err;
4498 }
4499 }
4500
4501 for (i = 0, j = 0; i < cand_ids->len; i++) {
4502 cand_id = cand_ids->data[i];
4503 cand_type = btf__type_by_id(targ_btf, cand_id);
4504 cand_name = btf__name_by_offset(targ_btf, cand_type->name_off);
4505
4506 err = bpf_core_spec_match(&local_spec, targ_btf,
4507 cand_id, &cand_spec);
4508 pr_debug("prog '%s': relo #%d: matching candidate #%d %s against spec ",
4509 prog_name, relo_idx, i, cand_name);
4510 bpf_core_dump_spec(LIBBPF_DEBUG, &cand_spec);
4511 libbpf_print(LIBBPF_DEBUG, ": %d\n", err);
4512 if (err < 0) {
Kefeng Wangbe180102019-10-21 13:55:32 +08004513 pr_warn("prog '%s': relo #%d: matching error: %d\n",
4514 prog_name, relo_idx, err);
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004515 return err;
4516 }
4517 if (err == 0)
4518 continue;
4519
4520 if (j == 0) {
4521 targ_spec = cand_spec;
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07004522 } else if (cand_spec.bit_offset != targ_spec.bit_offset) {
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004523 /* if there are many candidates, they should all
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07004524 * resolve to the same bit offset
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004525 */
Kefeng Wangbe180102019-10-21 13:55:32 +08004526 pr_warn("prog '%s': relo #%d: offset ambiguity: %u != %u\n",
Andrii Nakryikoee26dad2019-11-01 15:28:07 -07004527 prog_name, relo_idx, cand_spec.bit_offset,
4528 targ_spec.bit_offset);
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004529 return -EINVAL;
4530 }
4531
4532 cand_ids->data[j++] = cand_spec.spec[0].type_id;
4533 }
4534
Andrii Nakryiko62561eb2019-10-15 11:28:47 -07004535 /*
Andrii Nakryikod7a25272020-01-23 21:38:37 -08004536 * For BPF_FIELD_EXISTS relo or when used BPF program has field
4537 * existence checks or kernel version/config checks, it's expected
4538 * that we might not find any candidates. In this case, if field
4539 * wasn't found in any candidate, the list of candidates shouldn't
4540 * change at all, we'll just handle relocating appropriately,
4541 * depending on relo's kind.
Andrii Nakryiko62561eb2019-10-15 11:28:47 -07004542 */
4543 if (j > 0)
4544 cand_ids->len = j;
4545
Andrii Nakryikod7a25272020-01-23 21:38:37 -08004546 /*
4547 * If no candidates were found, it might be both a programmer error,
4548 * as well as expected case, depending whether instruction w/
4549 * relocation is guarded in some way that makes it unreachable (dead
4550 * code) if relocation can't be resolved. This is handled in
4551 * bpf_core_reloc_insn() uniformly by replacing that instruction with
4552 * BPF helper call insn (using invalid helper ID). If that instruction
4553 * is indeed unreachable, then it will be ignored and eliminated by
4554 * verifier. If it was an error, then verifier will complain and point
4555 * to a specific instruction number in its log.
4556 */
4557 if (j == 0)
4558 pr_debug("prog '%s': relo #%d: no matching targets found for [%d] %s + %s\n",
4559 prog_name, relo_idx, local_id, local_name, spec_str);
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004560
Andrii Nakryiko62561eb2019-10-15 11:28:47 -07004561 /* bpf_core_reloc_insn should know how to handle missing targ_spec */
Andrii Nakryikod7a25272020-01-23 21:38:37 -08004562 err = bpf_core_reloc_insn(prog, relo, relo_idx, &local_spec,
Andrii Nakryiko62561eb2019-10-15 11:28:47 -07004563 j ? &targ_spec : NULL);
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004564 if (err) {
Kefeng Wangbe180102019-10-21 13:55:32 +08004565 pr_warn("prog '%s': relo #%d: failed to patch insn at offset %d: %d\n",
4566 prog_name, relo_idx, relo->insn_off, err);
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004567 return -EINVAL;
4568 }
4569
4570 return 0;
4571}
4572
4573static int
Andrii Nakryiko511bb002019-10-15 11:28:45 -07004574bpf_core_reloc_fields(struct bpf_object *obj, const char *targ_btf_path)
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004575{
4576 const struct btf_ext_info_sec *sec;
Andrii Nakryiko511bb002019-10-15 11:28:45 -07004577 const struct bpf_field_reloc *rec;
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004578 const struct btf_ext_info *seg;
4579 struct hashmap_entry *entry;
4580 struct hashmap *cand_cache = NULL;
4581 struct bpf_program *prog;
4582 struct btf *targ_btf;
4583 const char *sec_name;
4584 int i, err = 0;
4585
4586 if (targ_btf_path)
4587 targ_btf = btf__parse_elf(targ_btf_path, NULL);
4588 else
Martin KaFai Laufb2426a2020-01-15 15:00:31 -08004589 targ_btf = libbpf_find_kernel_btf();
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004590 if (IS_ERR(targ_btf)) {
Kefeng Wangbe180102019-10-21 13:55:32 +08004591 pr_warn("failed to get target BTF: %ld\n", PTR_ERR(targ_btf));
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004592 return PTR_ERR(targ_btf);
4593 }
4594
4595 cand_cache = hashmap__new(bpf_core_hash_fn, bpf_core_equal_fn, NULL);
4596 if (IS_ERR(cand_cache)) {
4597 err = PTR_ERR(cand_cache);
4598 goto out;
4599 }
4600
Andrii Nakryiko511bb002019-10-15 11:28:45 -07004601 seg = &obj->btf_ext->field_reloc_info;
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004602 for_each_btf_ext_sec(seg, sec) {
4603 sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off);
4604 if (str_is_empty(sec_name)) {
4605 err = -EINVAL;
4606 goto out;
4607 }
4608 prog = bpf_object__find_program_by_title(obj, sec_name);
4609 if (!prog) {
Kefeng Wangbe180102019-10-21 13:55:32 +08004610 pr_warn("failed to find program '%s' for CO-RE offset relocation\n",
4611 sec_name);
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004612 err = -EINVAL;
4613 goto out;
4614 }
4615
4616 pr_debug("prog '%s': performing %d CO-RE offset relocs\n",
4617 sec_name, sec->num_info);
4618
4619 for_each_btf_ext_rec(seg, sec, i, rec) {
Andrii Nakryiko511bb002019-10-15 11:28:45 -07004620 err = bpf_core_reloc_field(prog, rec, i, obj->btf,
4621 targ_btf, cand_cache);
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004622 if (err) {
Kefeng Wangbe180102019-10-21 13:55:32 +08004623 pr_warn("prog '%s': relo #%d: failed to relocate: %d\n",
4624 sec_name, i, err);
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004625 goto out;
4626 }
4627 }
4628 }
4629
4630out:
4631 btf__free(targ_btf);
4632 if (!IS_ERR_OR_NULL(cand_cache)) {
4633 hashmap__for_each_entry(cand_cache, entry, i) {
4634 bpf_core_free_cands(entry->value);
4635 }
4636 hashmap__free(cand_cache);
4637 }
4638 return err;
4639}
4640
4641static int
4642bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path)
4643{
4644 int err = 0;
4645
Andrii Nakryiko511bb002019-10-15 11:28:45 -07004646 if (obj->btf_ext->field_reloc_info.len)
4647 err = bpf_core_reloc_fields(obj, targ_btf_path);
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004648
4649 return err;
4650}
4651
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08004652static int
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08004653bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj,
4654 struct reloc_desc *relo)
4655{
4656 struct bpf_insn *insn, *new_insn;
4657 struct bpf_program *text;
4658 size_t new_cnt;
Yonghong Song2993e052018-11-19 15:29:16 -08004659 int err;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08004660
Andrii Nakryiko9173cac2020-01-15 11:08:56 -08004661 if (prog->idx != obj->efile.text_shndx && prog->main_prog_cnt == 0) {
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08004662 text = bpf_object__find_prog_by_idx(obj, obj->efile.text_shndx);
4663 if (!text) {
Kefeng Wangbe180102019-10-21 13:55:32 +08004664 pr_warn("no .text section found yet relo into text exist\n");
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08004665 return -LIBBPF_ERRNO__RELOC;
4666 }
4667 new_cnt = prog->insns_cnt + text->insns_cnt;
Jakub Kicinski531b0142018-07-10 14:43:05 -07004668 new_insn = reallocarray(prog->insns, new_cnt, sizeof(*insn));
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08004669 if (!new_insn) {
Kefeng Wangbe180102019-10-21 13:55:32 +08004670 pr_warn("oom in prog realloc\n");
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08004671 return -ENOMEM;
4672 }
Andrii Nakryiko3dc5e052019-11-06 18:08:51 -08004673 prog->insns = new_insn;
Yonghong Song2993e052018-11-19 15:29:16 -08004674
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08004675 if (obj->btf_ext) {
4676 err = bpf_program_reloc_btf_ext(prog, obj,
4677 text->section_name,
4678 prog->insns_cnt);
4679 if (err)
Yonghong Song2993e052018-11-19 15:29:16 -08004680 return err;
Yonghong Song2993e052018-11-19 15:29:16 -08004681 }
4682
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08004683 memcpy(new_insn + prog->insns_cnt, text->insns,
4684 text->insns_cnt * sizeof(*insn));
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08004685 prog->main_prog_cnt = prog->insns_cnt;
4686 prog->insns_cnt = new_cnt;
Jeremy Clineb1a2ce82018-02-20 01:00:07 +00004687 pr_debug("added %zd insn from %s to prog %s\n",
4688 text->insns_cnt, text->section_name,
4689 prog->section_name);
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08004690 }
Andrii Nakryiko9173cac2020-01-15 11:08:56 -08004691
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08004692 insn = &prog->insns[relo->insn_idx];
Andrii Nakryiko53f8dd42019-11-27 12:06:50 -08004693 insn->imm += relo->sym_off / 8 + prog->main_prog_cnt - relo->insn_idx;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08004694 return 0;
4695}
4696
4697static int
Wang Nan9d759a92015-11-27 08:47:35 +00004698bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj)
Wang Nan8a47a6c2015-07-01 02:14:05 +00004699{
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08004700 int i, err;
Wang Nan8a47a6c2015-07-01 02:14:05 +00004701
Yonghong Song2993e052018-11-19 15:29:16 -08004702 if (!prog)
4703 return 0;
4704
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08004705 if (obj->btf_ext) {
4706 err = bpf_program_reloc_btf_ext(prog, obj,
4707 prog->section_name, 0);
4708 if (err)
Yonghong Song2993e052018-11-19 15:29:16 -08004709 return err;
Yonghong Song2993e052018-11-19 15:29:16 -08004710 }
4711
4712 if (!prog->reloc_desc)
Wang Nan8a47a6c2015-07-01 02:14:05 +00004713 return 0;
4714
4715 for (i = 0; i < prog->nr_reloc; i++) {
Andrii Nakryiko53f8dd42019-11-27 12:06:50 -08004716 struct reloc_desc *relo = &prog->reloc_desc[i];
Andrii Nakryiko166750b2019-12-13 17:47:08 -08004717 struct bpf_insn *insn = &prog->insns[relo->insn_idx];
Wang Nan8a47a6c2015-07-01 02:14:05 +00004718
Andrii Nakryiko166750b2019-12-13 17:47:08 -08004719 if (relo->insn_idx + 1 >= (int)prog->insns_cnt) {
4720 pr_warn("relocation out of range: '%s'\n",
4721 prog->section_name);
4722 return -LIBBPF_ERRNO__RELOC;
4723 }
Wang Nan8a47a6c2015-07-01 02:14:05 +00004724
Andrii Nakryiko166750b2019-12-13 17:47:08 -08004725 switch (relo->type) {
4726 case RELO_LD64:
4727 insn[0].src_reg = BPF_PSEUDO_MAP_FD;
Andrii Nakryiko53f8dd42019-11-27 12:06:50 -08004728 insn[0].imm = obj->maps[relo->map_idx].fd;
Andrii Nakryiko166750b2019-12-13 17:47:08 -08004729 break;
4730 case RELO_DATA:
4731 insn[0].src_reg = BPF_PSEUDO_MAP_VALUE;
4732 insn[1].imm = insn[0].imm + relo->sym_off;
4733 insn[0].imm = obj->maps[relo->map_idx].fd;
4734 break;
4735 case RELO_EXTERN:
4736 insn[0].src_reg = BPF_PSEUDO_MAP_VALUE;
Andrii Nakryiko81bfdd02019-12-18 16:28:34 -08004737 insn[0].imm = obj->maps[obj->kconfig_map_idx].fd;
Andrii Nakryiko166750b2019-12-13 17:47:08 -08004738 insn[1].imm = relo->sym_off;
4739 break;
4740 case RELO_CALL:
Andrii Nakryiko53f8dd42019-11-27 12:06:50 -08004741 err = bpf_program__reloc_text(prog, obj, relo);
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08004742 if (err)
4743 return err;
Andrii Nakryiko166750b2019-12-13 17:47:08 -08004744 break;
4745 default:
4746 pr_warn("relo #%d: bad relo type %d\n", i, relo->type);
4747 return -EINVAL;
Wang Nan8a47a6c2015-07-01 02:14:05 +00004748 }
Wang Nan8a47a6c2015-07-01 02:14:05 +00004749 }
4750
4751 zfree(&prog->reloc_desc);
4752 prog->nr_reloc = 0;
4753 return 0;
4754}
4755
Wang Nan8a47a6c2015-07-01 02:14:05 +00004756static int
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004757bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path)
Wang Nan8a47a6c2015-07-01 02:14:05 +00004758{
4759 struct bpf_program *prog;
4760 size_t i;
4761 int err;
4762
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004763 if (obj->btf_ext) {
4764 err = bpf_object__relocate_core(obj, targ_btf_path);
4765 if (err) {
Kefeng Wangbe180102019-10-21 13:55:32 +08004766 pr_warn("failed to perform CO-RE relocations: %d\n",
4767 err);
Andrii Nakryikoddc7c302019-08-07 14:39:51 -07004768 return err;
4769 }
4770 }
Andrii Nakryiko9173cac2020-01-15 11:08:56 -08004771 /* ensure .text is relocated first, as it's going to be copied as-is
4772 * later for sub-program calls
4773 */
Wang Nan8a47a6c2015-07-01 02:14:05 +00004774 for (i = 0; i < obj->nr_programs; i++) {
4775 prog = &obj->programs[i];
Andrii Nakryiko9173cac2020-01-15 11:08:56 -08004776 if (prog->idx != obj->efile.text_shndx)
4777 continue;
4778
4779 err = bpf_program__relocate(prog, obj);
4780 if (err) {
4781 pr_warn("failed to relocate '%s'\n", prog->section_name);
4782 return err;
4783 }
4784 break;
4785 }
4786 /* now relocate everything but .text, which by now is relocated
4787 * properly, so we can copy raw sub-program instructions as is safely
4788 */
4789 for (i = 0; i < obj->nr_programs; i++) {
4790 prog = &obj->programs[i];
4791 if (prog->idx == obj->efile.text_shndx)
4792 continue;
Wang Nan8a47a6c2015-07-01 02:14:05 +00004793
Wang Nan9d759a92015-11-27 08:47:35 +00004794 err = bpf_program__relocate(prog, obj);
Wang Nan8a47a6c2015-07-01 02:14:05 +00004795 if (err) {
Kefeng Wangbe180102019-10-21 13:55:32 +08004796 pr_warn("failed to relocate '%s'\n", prog->section_name);
Wang Nan8a47a6c2015-07-01 02:14:05 +00004797 return err;
4798 }
4799 }
4800 return 0;
4801}
4802
Martin KaFai Lau590a0082020-01-08 16:35:14 -08004803static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj,
4804 GElf_Shdr *shdr,
4805 Elf_Data *data);
4806
Wang Nan34090912015-07-01 02:14:02 +00004807static int bpf_object__collect_reloc(struct bpf_object *obj)
4808{
4809 int i, err;
4810
4811 if (!obj_elf_valid(obj)) {
Kefeng Wangbe180102019-10-21 13:55:32 +08004812 pr_warn("Internal error: elf object is closed\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +00004813 return -LIBBPF_ERRNO__INTERNAL;
Wang Nan34090912015-07-01 02:14:02 +00004814 }
4815
Andrii Nakryiko1f8e2bc2019-11-20 23:07:41 -08004816 for (i = 0; i < obj->efile.nr_reloc_sects; i++) {
4817 GElf_Shdr *shdr = &obj->efile.reloc_sects[i].shdr;
4818 Elf_Data *data = obj->efile.reloc_sects[i].data;
Wang Nan34090912015-07-01 02:14:02 +00004819 int idx = shdr->sh_info;
4820 struct bpf_program *prog;
Wang Nan34090912015-07-01 02:14:02 +00004821
4822 if (shdr->sh_type != SHT_REL) {
Kefeng Wangbe180102019-10-21 13:55:32 +08004823 pr_warn("internal error at %d\n", __LINE__);
Wang Nan6371ca3b2015-11-06 13:49:37 +00004824 return -LIBBPF_ERRNO__INTERNAL;
Wang Nan34090912015-07-01 02:14:02 +00004825 }
4826
Martin KaFai Lau590a0082020-01-08 16:35:14 -08004827 if (idx == obj->efile.st_ops_shndx) {
4828 err = bpf_object__collect_struct_ops_map_reloc(obj,
4829 shdr,
4830 data);
4831 if (err)
4832 return err;
4833 continue;
4834 }
4835
Wang Nan34090912015-07-01 02:14:02 +00004836 prog = bpf_object__find_prog_by_idx(obj, idx);
4837 if (!prog) {
Kefeng Wangbe180102019-10-21 13:55:32 +08004838 pr_warn("relocation failed: no section(%d)\n", idx);
Wang Nan6371ca3b2015-11-06 13:49:37 +00004839 return -LIBBPF_ERRNO__RELOC;
Wang Nan34090912015-07-01 02:14:02 +00004840 }
4841
Andrii Nakryiko399dc652019-05-29 10:36:11 -07004842 err = bpf_program__collect_reloc(prog, shdr, data, obj);
Wang Nan34090912015-07-01 02:14:02 +00004843 if (err)
Wang Nan6371ca3b2015-11-06 13:49:37 +00004844 return err;
Wang Nan34090912015-07-01 02:14:02 +00004845 }
4846 return 0;
4847}
4848
Wang Nan55cffde2015-07-01 02:14:07 +00004849static int
Yonghong Song2993e052018-11-19 15:29:16 -08004850load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08004851 char *license, __u32 kern_version, int *pfd)
Wang Nan55cffde2015-07-01 02:14:07 +00004852{
Andrey Ignatovd7be1432018-03-30 15:08:01 -07004853 struct bpf_load_program_attr load_attr;
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02004854 char *cp, errmsg[STRERR_BUFSIZE];
Stanislav Fomichev8395f322020-03-25 12:55:21 -07004855 size_t log_buf_size = 0;
4856 char *log_buf = NULL;
Andrii Nakryiko5d01ab72019-07-26 14:24:38 -07004857 int btf_fd, ret;
Wang Nan55cffde2015-07-01 02:14:07 +00004858
Andrii Nakryikofba01a02019-05-29 10:36:08 -07004859 if (!insns || !insns_cnt)
4860 return -EINVAL;
4861
Andrey Ignatovd7be1432018-03-30 15:08:01 -07004862 memset(&load_attr, 0, sizeof(struct bpf_load_program_attr));
Yonghong Song2993e052018-11-19 15:29:16 -08004863 load_attr.prog_type = prog->type;
4864 load_attr.expected_attach_type = prog->expected_attach_type;
Stanislav Fomichev5b32a232018-11-20 17:11:21 -08004865 if (prog->caps->name)
4866 load_attr.name = prog->name;
Andrey Ignatovd7be1432018-03-30 15:08:01 -07004867 load_attr.insns = insns;
4868 load_attr.insns_cnt = insns_cnt;
4869 load_attr.license = license;
KP Singh1e092a032020-03-29 01:43:54 +01004870 if (prog->type == BPF_PROG_TYPE_STRUCT_OPS ||
4871 prog->type == BPF_PROG_TYPE_LSM) {
Martin KaFai Lau590a0082020-01-08 16:35:14 -08004872 load_attr.attach_btf_id = prog->attach_btf_id;
Alexei Starovoitov2db6eab2020-01-20 16:53:47 -08004873 } else if (prog->type == BPF_PROG_TYPE_TRACING ||
4874 prog->type == BPF_PROG_TYPE_EXT) {
Alexei Starovoitove7bf94d2019-11-14 10:57:18 -08004875 load_attr.attach_prog_fd = prog->attach_prog_fd;
4876 load_attr.attach_btf_id = prog->attach_btf_id;
4877 } else {
4878 load_attr.kern_version = kern_version;
4879 load_attr.prog_ifindex = prog->prog_ifindex;
4880 }
Andrii Nakryiko3415ec62019-08-01 00:24:05 -07004881 /* if .BTF.ext was loaded, kernel supports associated BTF for prog */
4882 if (prog->obj->btf_ext)
4883 btf_fd = bpf_object__btf_fd(prog->obj);
4884 else
4885 btf_fd = -1;
Andrii Nakryiko5d01ab72019-07-26 14:24:38 -07004886 load_attr.prog_btf_fd = btf_fd >= 0 ? btf_fd : 0;
Yonghong Song2993e052018-11-19 15:29:16 -08004887 load_attr.func_info = prog->func_info;
4888 load_attr.func_info_rec_size = prog->func_info_rec_size;
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08004889 load_attr.func_info_cnt = prog->func_info_cnt;
Martin KaFai Lau3d650142018-12-07 16:42:31 -08004890 load_attr.line_info = prog->line_info;
4891 load_attr.line_info_rec_size = prog->line_info_rec_size;
4892 load_attr.line_info_cnt = prog->line_info_cnt;
Alexei Starovoitovda11b412019-04-01 21:27:47 -07004893 load_attr.log_level = prog->log_level;
Jiong Wang04656192019-05-24 23:25:19 +01004894 load_attr.prog_flags = prog->prog_flags;
Wang Nan55cffde2015-07-01 02:14:07 +00004895
Alexei Starovoitovda11b412019-04-01 21:27:47 -07004896retry_load:
Stanislav Fomichev8395f322020-03-25 12:55:21 -07004897 if (log_buf_size) {
4898 log_buf = malloc(log_buf_size);
4899 if (!log_buf)
4900 return -ENOMEM;
4901
4902 *log_buf = 0;
4903 }
Wang Nan55cffde2015-07-01 02:14:07 +00004904
Alexei Starovoitovda11b412019-04-01 21:27:47 -07004905 ret = bpf_load_program_xattr(&load_attr, log_buf, log_buf_size);
Wang Nan55cffde2015-07-01 02:14:07 +00004906
4907 if (ret >= 0) {
Stanislav Fomichev8395f322020-03-25 12:55:21 -07004908 if (log_buf && load_attr.log_level)
Alexei Starovoitovda11b412019-04-01 21:27:47 -07004909 pr_debug("verifier log:\n%s", log_buf);
Wang Nan55cffde2015-07-01 02:14:07 +00004910 *pfd = ret;
4911 ret = 0;
4912 goto out;
4913 }
4914
Stanislav Fomichev8395f322020-03-25 12:55:21 -07004915 if (!log_buf || errno == ENOSPC) {
4916 log_buf_size = max((size_t)BPF_LOG_BUF_SIZE,
4917 log_buf_size << 1);
4918
Alexei Starovoitovda11b412019-04-01 21:27:47 -07004919 free(log_buf);
4920 goto retry_load;
4921 }
Toke Høiland-Jørgensen4f33ddb2019-11-09 21:37:29 +01004922 ret = -errno;
Andrey Ignatov24d6a802018-10-03 15:26:41 -07004923 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
Kefeng Wangbe180102019-10-21 13:55:32 +08004924 pr_warn("load bpf program failed: %s\n", cp);
Toke Høiland-Jørgensendc3a2d22019-12-16 19:12:04 +01004925 pr_perm_msg(ret);
Wang Nan55cffde2015-07-01 02:14:07 +00004926
Wang Nan6371ca3b2015-11-06 13:49:37 +00004927 if (log_buf && log_buf[0] != '\0') {
4928 ret = -LIBBPF_ERRNO__VERIFY;
Kefeng Wangbe180102019-10-21 13:55:32 +08004929 pr_warn("-- BEGIN DUMP LOG ---\n");
4930 pr_warn("\n%s\n", log_buf);
4931 pr_warn("-- END LOG --\n");
Andrey Ignatovd7be1432018-03-30 15:08:01 -07004932 } else if (load_attr.insns_cnt >= BPF_MAXINSNS) {
Kefeng Wangbe180102019-10-21 13:55:32 +08004933 pr_warn("Program too large (%zu insns), at most %d insns\n",
4934 load_attr.insns_cnt, BPF_MAXINSNS);
Wang Nan705fa212016-07-13 10:44:02 +00004935 ret = -LIBBPF_ERRNO__PROG2BIG;
Toke Høiland-Jørgensen4f33ddb2019-11-09 21:37:29 +01004936 } else if (load_attr.prog_type != BPF_PROG_TYPE_KPROBE) {
Wang Nan705fa212016-07-13 10:44:02 +00004937 /* Wrong program type? */
Toke Høiland-Jørgensen4f33ddb2019-11-09 21:37:29 +01004938 int fd;
Wang Nan705fa212016-07-13 10:44:02 +00004939
Toke Høiland-Jørgensen4f33ddb2019-11-09 21:37:29 +01004940 load_attr.prog_type = BPF_PROG_TYPE_KPROBE;
4941 load_attr.expected_attach_type = 0;
4942 fd = bpf_load_program_xattr(&load_attr, NULL, 0);
4943 if (fd >= 0) {
4944 close(fd);
4945 ret = -LIBBPF_ERRNO__PROGTYPE;
4946 goto out;
Wang Nan6371ca3b2015-11-06 13:49:37 +00004947 }
Wang Nan55cffde2015-07-01 02:14:07 +00004948 }
4949
4950out:
4951 free(log_buf);
4952 return ret;
4953}
4954
KP Singha6ed02c2020-01-17 22:28:25 +01004955static int libbpf_find_attach_btf_id(struct bpf_program *prog);
Andrii Nakryiko13acb502019-12-13 17:43:34 -08004956
4957int bpf_program__load(struct bpf_program *prog, char *license, __u32 kern_ver)
Wang Nan55cffde2015-07-01 02:14:07 +00004958{
Andrii Nakryiko13acb502019-12-13 17:43:34 -08004959 int err = 0, fd, i, btf_id;
4960
Eelco Chaudronff26ce52020-02-20 13:26:35 +00004961 if ((prog->type == BPF_PROG_TYPE_TRACING ||
KP Singh1e092a032020-03-29 01:43:54 +01004962 prog->type == BPF_PROG_TYPE_LSM ||
Eelco Chaudronff26ce52020-02-20 13:26:35 +00004963 prog->type == BPF_PROG_TYPE_EXT) && !prog->attach_btf_id) {
KP Singha6ed02c2020-01-17 22:28:25 +01004964 btf_id = libbpf_find_attach_btf_id(prog);
Andrii Nakryiko13acb502019-12-13 17:43:34 -08004965 if (btf_id <= 0)
4966 return btf_id;
4967 prog->attach_btf_id = btf_id;
4968 }
Wang Nan55cffde2015-07-01 02:14:07 +00004969
Wang Nanb5805632015-11-16 12:10:09 +00004970 if (prog->instances.nr < 0 || !prog->instances.fds) {
4971 if (prog->preprocessor) {
Kefeng Wangbe180102019-10-21 13:55:32 +08004972 pr_warn("Internal error: can't load program '%s'\n",
4973 prog->section_name);
Wang Nanb5805632015-11-16 12:10:09 +00004974 return -LIBBPF_ERRNO__INTERNAL;
4975 }
Wang Nan55cffde2015-07-01 02:14:07 +00004976
Wang Nanb5805632015-11-16 12:10:09 +00004977 prog->instances.fds = malloc(sizeof(int));
4978 if (!prog->instances.fds) {
Kefeng Wangbe180102019-10-21 13:55:32 +08004979 pr_warn("Not enough memory for BPF fds\n");
Wang Nanb5805632015-11-16 12:10:09 +00004980 return -ENOMEM;
4981 }
4982 prog->instances.nr = 1;
4983 prog->instances.fds[0] = -1;
4984 }
4985
4986 if (!prog->preprocessor) {
4987 if (prog->instances.nr != 1) {
Kefeng Wangbe180102019-10-21 13:55:32 +08004988 pr_warn("Program '%s' is inconsistent: nr(%d) != 1\n",
4989 prog->section_name, prog->instances.nr);
Wang Nanb5805632015-11-16 12:10:09 +00004990 }
Yonghong Song2993e052018-11-19 15:29:16 -08004991 err = load_program(prog, prog->insns, prog->insns_cnt,
Andrii Nakryiko13acb502019-12-13 17:43:34 -08004992 license, kern_ver, &fd);
Wang Nanb5805632015-11-16 12:10:09 +00004993 if (!err)
4994 prog->instances.fds[0] = fd;
4995 goto out;
4996 }
4997
4998 for (i = 0; i < prog->instances.nr; i++) {
4999 struct bpf_prog_prep_result result;
5000 bpf_program_prep_t preprocessor = prog->preprocessor;
5001
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -08005002 memset(&result, 0, sizeof(result));
Wang Nanb5805632015-11-16 12:10:09 +00005003 err = preprocessor(prog, i, prog->insns,
5004 prog->insns_cnt, &result);
5005 if (err) {
Kefeng Wangbe180102019-10-21 13:55:32 +08005006 pr_warn("Preprocessing the %dth instance of program '%s' failed\n",
5007 i, prog->section_name);
Wang Nanb5805632015-11-16 12:10:09 +00005008 goto out;
5009 }
5010
5011 if (!result.new_insn_ptr || !result.new_insn_cnt) {
5012 pr_debug("Skip loading the %dth instance of program '%s'\n",
5013 i, prog->section_name);
5014 prog->instances.fds[i] = -1;
5015 if (result.pfd)
5016 *result.pfd = -1;
5017 continue;
5018 }
5019
Yonghong Song2993e052018-11-19 15:29:16 -08005020 err = load_program(prog, result.new_insn_ptr,
Andrii Nakryiko13acb502019-12-13 17:43:34 -08005021 result.new_insn_cnt, license, kern_ver, &fd);
Wang Nanb5805632015-11-16 12:10:09 +00005022 if (err) {
Kefeng Wangbe180102019-10-21 13:55:32 +08005023 pr_warn("Loading the %dth instance of program '%s' failed\n",
5024 i, prog->section_name);
Wang Nanb5805632015-11-16 12:10:09 +00005025 goto out;
5026 }
5027
5028 if (result.pfd)
5029 *result.pfd = fd;
5030 prog->instances.fds[i] = fd;
5031 }
5032out:
Wang Nan55cffde2015-07-01 02:14:07 +00005033 if (err)
Kefeng Wangbe180102019-10-21 13:55:32 +08005034 pr_warn("failed to load program '%s'\n", prog->section_name);
Wang Nan55cffde2015-07-01 02:14:07 +00005035 zfree(&prog->insns);
5036 prog->insns_cnt = 0;
5037 return err;
5038}
5039
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07005040static bool bpf_program__is_function_storage(const struct bpf_program *prog,
5041 const struct bpf_object *obj)
Jakub Kicinski9a94f272018-06-28 14:41:38 -07005042{
5043 return prog->idx == obj->efile.text_shndx && obj->has_pseudo_calls;
5044}
5045
Wang Nan55cffde2015-07-01 02:14:07 +00005046static int
Quentin Monnet60276f92019-05-24 11:36:47 +01005047bpf_object__load_progs(struct bpf_object *obj, int log_level)
Wang Nan55cffde2015-07-01 02:14:07 +00005048{
5049 size_t i;
5050 int err;
5051
5052 for (i = 0; i < obj->nr_programs; i++) {
Jakub Kicinski9a94f272018-06-28 14:41:38 -07005053 if (bpf_program__is_function_storage(&obj->programs[i], obj))
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08005054 continue;
Quentin Monnet501b1252019-05-29 15:26:41 +01005055 obj->programs[i].log_level |= log_level;
Wang Nan55cffde2015-07-01 02:14:07 +00005056 err = bpf_program__load(&obj->programs[i],
5057 obj->license,
5058 obj->kern_version);
5059 if (err)
5060 return err;
5061 }
5062 return 0;
5063}
5064
Wang Nan1a5e3fb2015-07-01 02:13:53 +00005065static struct bpf_object *
Andrii Nakryiko5e61f272019-10-04 15:40:34 -07005066__bpf_object__open(const char *path, const void *obj_buf, size_t obj_buf_sz,
Andrii Nakryiko01af3bf2019-12-13 17:43:32 -08005067 const struct bpf_object_open_opts *opts)
Wang Nan1a5e3fb2015-07-01 02:13:53 +00005068{
Andrii Nakryiko8601fd42019-12-18 16:28:35 -08005069 const char *obj_name, *kconfig;
Andrii Nakryikodd4436b2019-10-20 20:38:59 -07005070 struct bpf_program *prog;
Wang Nan1a5e3fb2015-07-01 02:13:53 +00005071 struct bpf_object *obj;
Andrii Nakryiko291ee022019-10-15 11:28:46 -07005072 char tmp_name[64];
Wang Nan6371ca3b2015-11-06 13:49:37 +00005073 int err;
Wang Nan1a5e3fb2015-07-01 02:13:53 +00005074
5075 if (elf_version(EV_CURRENT) == EV_NONE) {
Kefeng Wangbe180102019-10-21 13:55:32 +08005076 pr_warn("failed to init libelf for %s\n",
5077 path ? : "(mem buf)");
Wang Nan6371ca3b2015-11-06 13:49:37 +00005078 return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00005079 }
5080
Andrii Nakryiko291ee022019-10-15 11:28:46 -07005081 if (!OPTS_VALID(opts, bpf_object_open_opts))
5082 return ERR_PTR(-EINVAL);
5083
Andrii Nakryiko1aace102019-11-21 16:35:27 -08005084 obj_name = OPTS_GET(opts, object_name, NULL);
Andrii Nakryiko291ee022019-10-15 11:28:46 -07005085 if (obj_buf) {
5086 if (!obj_name) {
5087 snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
5088 (unsigned long)obj_buf,
5089 (unsigned long)obj_buf_sz);
5090 obj_name = tmp_name;
5091 }
5092 path = obj_name;
5093 pr_debug("loading object '%s' from buffer\n", obj_name);
5094 }
5095
Andrii Nakryiko2ce84502019-10-04 15:40:35 -07005096 obj = bpf_object__new(path, obj_buf, obj_buf_sz, obj_name);
Wang Nan6371ca3b2015-11-06 13:49:37 +00005097 if (IS_ERR(obj))
5098 return obj;
Wang Nan1a5e3fb2015-07-01 02:13:53 +00005099
Andrii Nakryiko8601fd42019-12-18 16:28:35 -08005100 kconfig = OPTS_GET(opts, kconfig, NULL);
5101 if (kconfig) {
5102 obj->kconfig = strdup(kconfig);
5103 if (!obj->kconfig)
Andrii Nakryiko166750b2019-12-13 17:47:08 -08005104 return ERR_PTR(-ENOMEM);
5105 }
Andrii Nakryiko291ee022019-10-15 11:28:46 -07005106
Andrii Nakryiko0d13bfc2019-12-13 17:43:25 -08005107 err = bpf_object__elf_init(obj);
5108 err = err ? : bpf_object__check_endianness(obj);
5109 err = err ? : bpf_object__elf_collect(obj);
Andrii Nakryiko166750b2019-12-13 17:47:08 -08005110 err = err ? : bpf_object__collect_externs(obj);
5111 err = err ? : bpf_object__finalize_btf(obj);
Andrii Nakryiko0d13bfc2019-12-13 17:43:25 -08005112 err = err ? : bpf_object__init_maps(obj, opts);
5113 err = err ? : bpf_object__init_prog_names(obj);
5114 err = err ? : bpf_object__collect_reloc(obj);
5115 if (err)
5116 goto out;
Wang Nan1a5e3fb2015-07-01 02:13:53 +00005117 bpf_object__elf_finish(obj);
Andrii Nakryikodd4436b2019-10-20 20:38:59 -07005118
5119 bpf_object__for_each_program(prog, obj) {
5120 enum bpf_prog_type prog_type;
5121 enum bpf_attach_type attach_type;
5122
Martin KaFai Lau590a0082020-01-08 16:35:14 -08005123 if (prog->type != BPF_PROG_TYPE_UNSPEC)
5124 continue;
5125
Andrii Nakryikodd4436b2019-10-20 20:38:59 -07005126 err = libbpf_prog_type_by_name(prog->section_name, &prog_type,
5127 &attach_type);
5128 if (err == -ESRCH)
5129 /* couldn't guess, but user might manually specify */
5130 continue;
5131 if (err)
5132 goto out;
5133
5134 bpf_program__set_type(prog, prog_type);
5135 bpf_program__set_expected_attach_type(prog, attach_type);
Alexei Starovoitov2db6eab2020-01-20 16:53:47 -08005136 if (prog_type == BPF_PROG_TYPE_TRACING ||
5137 prog_type == BPF_PROG_TYPE_EXT)
Andrii Nakryiko166750b2019-12-13 17:47:08 -08005138 prog->attach_prog_fd = OPTS_GET(opts, attach_prog_fd, 0);
Andrii Nakryikodd4436b2019-10-20 20:38:59 -07005139 }
5140
Wang Nan1a5e3fb2015-07-01 02:13:53 +00005141 return obj;
5142out:
5143 bpf_object__close(obj);
Wang Nan6371ca3b2015-11-06 13:49:37 +00005144 return ERR_PTR(err);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00005145}
5146
Andrii Nakryiko5e61f272019-10-04 15:40:34 -07005147static struct bpf_object *
5148__bpf_object__open_xattr(struct bpf_object_open_attr *attr, int flags)
Wang Nan1a5e3fb2015-07-01 02:13:53 +00005149{
Andrii Nakryikoe00aca62019-10-22 10:21:00 -07005150 DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts,
Andrii Nakryiko291ee022019-10-15 11:28:46 -07005151 .relaxed_maps = flags & MAPS_RELAX_COMPAT,
5152 );
5153
Wang Nan1a5e3fb2015-07-01 02:13:53 +00005154 /* param validation */
Jakub Kicinski07f2d4e2018-07-10 14:43:02 -07005155 if (!attr->file)
Wang Nan1a5e3fb2015-07-01 02:13:53 +00005156 return NULL;
5157
Jakub Kicinski07f2d4e2018-07-10 14:43:02 -07005158 pr_debug("loading %s\n", attr->file);
Andrii Nakryiko291ee022019-10-15 11:28:46 -07005159 return __bpf_object__open(attr->file, NULL, 0, &opts);
John Fastabendc034a172018-10-15 11:19:55 -07005160}
5161
5162struct bpf_object *bpf_object__open_xattr(struct bpf_object_open_attr *attr)
5163{
5164 return __bpf_object__open_xattr(attr, 0);
Jakub Kicinski07f2d4e2018-07-10 14:43:02 -07005165}
5166
5167struct bpf_object *bpf_object__open(const char *path)
5168{
5169 struct bpf_object_open_attr attr = {
5170 .file = path,
5171 .prog_type = BPF_PROG_TYPE_UNSPEC,
5172 };
5173
5174 return bpf_object__open_xattr(&attr);
Wang Nan6c956392015-07-01 02:13:54 +00005175}
5176
Andrii Nakryiko2ce84502019-10-04 15:40:35 -07005177struct bpf_object *
Andrii Nakryiko01af3bf2019-12-13 17:43:32 -08005178bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts)
Andrii Nakryiko2ce84502019-10-04 15:40:35 -07005179{
Andrii Nakryiko2ce84502019-10-04 15:40:35 -07005180 if (!path)
5181 return ERR_PTR(-EINVAL);
5182
5183 pr_debug("loading %s\n", path);
5184
Andrii Nakryiko291ee022019-10-15 11:28:46 -07005185 return __bpf_object__open(path, NULL, 0, opts);
Andrii Nakryiko2ce84502019-10-04 15:40:35 -07005186}
5187
5188struct bpf_object *
5189bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
Andrii Nakryiko01af3bf2019-12-13 17:43:32 -08005190 const struct bpf_object_open_opts *opts)
Wang Nan6c956392015-07-01 02:13:54 +00005191{
Andrii Nakryiko2ce84502019-10-04 15:40:35 -07005192 if (!obj_buf || obj_buf_sz == 0)
5193 return ERR_PTR(-EINVAL);
Wang Nan6c956392015-07-01 02:13:54 +00005194
Andrii Nakryiko291ee022019-10-15 11:28:46 -07005195 return __bpf_object__open(NULL, obj_buf, obj_buf_sz, opts);
Andrii Nakryiko2ce84502019-10-04 15:40:35 -07005196}
5197
5198struct bpf_object *
5199bpf_object__open_buffer(const void *obj_buf, size_t obj_buf_sz,
5200 const char *name)
5201{
Andrii Nakryikoe00aca62019-10-22 10:21:00 -07005202 DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts,
Andrii Nakryiko2ce84502019-10-04 15:40:35 -07005203 .object_name = name,
5204 /* wrong default, but backwards-compatible */
5205 .relaxed_maps = true,
5206 );
5207
5208 /* returning NULL is wrong, but backwards-compatible */
5209 if (!obj_buf || obj_buf_sz == 0)
5210 return NULL;
5211
5212 return bpf_object__open_mem(obj_buf, obj_buf_sz, &opts);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00005213}
5214
Wang Nan52d33522015-07-01 02:14:04 +00005215int bpf_object__unload(struct bpf_object *obj)
5216{
5217 size_t i;
5218
5219 if (!obj)
5220 return -EINVAL;
5221
Martin KaFai Lau590a0082020-01-08 16:35:14 -08005222 for (i = 0; i < obj->nr_maps; i++) {
Wang Nan9d759a92015-11-27 08:47:35 +00005223 zclose(obj->maps[i].fd);
Martin KaFai Lau590a0082020-01-08 16:35:14 -08005224 if (obj->maps[i].st_ops)
5225 zfree(&obj->maps[i].st_ops->kern_vdata);
5226 }
Wang Nan52d33522015-07-01 02:14:04 +00005227
Wang Nan55cffde2015-07-01 02:14:07 +00005228 for (i = 0; i < obj->nr_programs; i++)
5229 bpf_program__unload(&obj->programs[i]);
5230
Wang Nan52d33522015-07-01 02:14:04 +00005231 return 0;
5232}
5233
Andrii Nakryiko0d13bfc2019-12-13 17:43:25 -08005234static int bpf_object__sanitize_maps(struct bpf_object *obj)
5235{
5236 struct bpf_map *m;
5237
5238 bpf_object__for_each_map(m, obj) {
5239 if (!bpf_map__is_internal(m))
5240 continue;
5241 if (!obj->caps.global_data) {
5242 pr_warn("kernel doesn't support global data\n");
5243 return -ENOTSUP;
5244 }
5245 if (!obj->caps.array_mmap)
5246 m->def.map_flags ^= BPF_F_MMAPABLE;
5247 }
5248
5249 return 0;
5250}
5251
Andrii Nakryiko166750b2019-12-13 17:47:08 -08005252static int bpf_object__resolve_externs(struct bpf_object *obj,
Andrii Nakryiko8601fd42019-12-18 16:28:35 -08005253 const char *extra_kconfig)
Andrii Nakryiko166750b2019-12-13 17:47:08 -08005254{
5255 bool need_config = false;
5256 struct extern_desc *ext;
5257 int err, i;
5258 void *data;
5259
5260 if (obj->nr_extern == 0)
5261 return 0;
5262
Andrii Nakryiko81bfdd02019-12-18 16:28:34 -08005263 data = obj->maps[obj->kconfig_map_idx].mmaped;
Andrii Nakryiko166750b2019-12-13 17:47:08 -08005264
5265 for (i = 0; i < obj->nr_extern; i++) {
5266 ext = &obj->externs[i];
5267
5268 if (strcmp(ext->name, "LINUX_KERNEL_VERSION") == 0) {
5269 void *ext_val = data + ext->data_off;
5270 __u32 kver = get_kernel_version();
5271
5272 if (!kver) {
5273 pr_warn("failed to get kernel version\n");
5274 return -EINVAL;
5275 }
5276 err = set_ext_value_num(ext, ext_val, kver);
5277 if (err)
5278 return err;
5279 pr_debug("extern %s=0x%x\n", ext->name, kver);
5280 } else if (strncmp(ext->name, "CONFIG_", 7) == 0) {
5281 need_config = true;
5282 } else {
5283 pr_warn("unrecognized extern '%s'\n", ext->name);
5284 return -EINVAL;
5285 }
5286 }
Andrii Nakryiko8601fd42019-12-18 16:28:35 -08005287 if (need_config && extra_kconfig) {
5288 err = bpf_object__read_kconfig_mem(obj, extra_kconfig, data);
5289 if (err)
5290 return -EINVAL;
5291 need_config = false;
5292 for (i = 0; i < obj->nr_extern; i++) {
5293 ext = &obj->externs[i];
5294 if (!ext->is_set) {
5295 need_config = true;
5296 break;
5297 }
5298 }
5299 }
Andrii Nakryiko166750b2019-12-13 17:47:08 -08005300 if (need_config) {
Andrii Nakryiko8601fd42019-12-18 16:28:35 -08005301 err = bpf_object__read_kconfig_file(obj, data);
Andrii Nakryiko166750b2019-12-13 17:47:08 -08005302 if (err)
5303 return -EINVAL;
5304 }
5305 for (i = 0; i < obj->nr_extern; i++) {
5306 ext = &obj->externs[i];
5307
5308 if (!ext->is_set && !ext->is_weak) {
5309 pr_warn("extern %s (strong) not resolved\n", ext->name);
5310 return -ESRCH;
5311 } else if (!ext->is_set) {
5312 pr_debug("extern %s (weak) not resolved, defaulting to zero\n",
5313 ext->name);
5314 }
5315 }
5316
5317 return 0;
5318}
5319
Quentin Monnet60276f92019-05-24 11:36:47 +01005320int bpf_object__load_xattr(struct bpf_object_load_attr *attr)
Wang Nan52d33522015-07-01 02:14:04 +00005321{
Quentin Monnet60276f92019-05-24 11:36:47 +01005322 struct bpf_object *obj;
Toke Høiland-Jørgensenec6d5f472019-11-09 21:37:27 +01005323 int err, i;
Wang Nan6371ca3b2015-11-06 13:49:37 +00005324
Quentin Monnet60276f92019-05-24 11:36:47 +01005325 if (!attr)
5326 return -EINVAL;
5327 obj = attr->obj;
Wang Nan52d33522015-07-01 02:14:04 +00005328 if (!obj)
5329 return -EINVAL;
5330
5331 if (obj->loaded) {
Kefeng Wangbe180102019-10-21 13:55:32 +08005332 pr_warn("object should not be loaded twice\n");
Wang Nan52d33522015-07-01 02:14:04 +00005333 return -EINVAL;
5334 }
5335
5336 obj->loaded = true;
Wang Nan6371ca3b2015-11-06 13:49:37 +00005337
Andrii Nakryiko0d13bfc2019-12-13 17:43:25 -08005338 err = bpf_object__probe_caps(obj);
Andrii Nakryiko8601fd42019-12-18 16:28:35 -08005339 err = err ? : bpf_object__resolve_externs(obj, obj->kconfig);
Andrii Nakryiko0d13bfc2019-12-13 17:43:25 -08005340 err = err ? : bpf_object__sanitize_and_load_btf(obj);
5341 err = err ? : bpf_object__sanitize_maps(obj);
KP Singha6ed02c2020-01-17 22:28:25 +01005342 err = err ? : bpf_object__load_vmlinux_btf(obj);
Martin KaFai Lau590a0082020-01-08 16:35:14 -08005343 err = err ? : bpf_object__init_kern_struct_ops_maps(obj);
Andrii Nakryiko0d13bfc2019-12-13 17:43:25 -08005344 err = err ? : bpf_object__create_maps(obj);
5345 err = err ? : bpf_object__relocate(obj, attr->target_btf_path);
5346 err = err ? : bpf_object__load_progs(obj, attr->log_level);
KP Singha6ed02c2020-01-17 22:28:25 +01005347
5348 btf__free(obj->btf_vmlinux);
5349 obj->btf_vmlinux = NULL;
5350
Andrii Nakryiko0d13bfc2019-12-13 17:43:25 -08005351 if (err)
5352 goto out;
Wang Nan52d33522015-07-01 02:14:04 +00005353
5354 return 0;
5355out:
Toke Høiland-Jørgensenec6d5f472019-11-09 21:37:27 +01005356 /* unpin any maps that were auto-pinned during load */
5357 for (i = 0; i < obj->nr_maps; i++)
5358 if (obj->maps[i].pinned && !obj->maps[i].reused)
5359 bpf_map__unpin(&obj->maps[i], NULL);
5360
Wang Nan52d33522015-07-01 02:14:04 +00005361 bpf_object__unload(obj);
Kefeng Wangbe180102019-10-21 13:55:32 +08005362 pr_warn("failed to load object '%s'\n", obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00005363 return err;
Wang Nan52d33522015-07-01 02:14:04 +00005364}
5365
Quentin Monnet60276f92019-05-24 11:36:47 +01005366int bpf_object__load(struct bpf_object *obj)
5367{
5368 struct bpf_object_load_attr attr = {
5369 .obj = obj,
5370 };
5371
5372 return bpf_object__load_xattr(&attr);
5373}
5374
Toke Høiland-Jørgensen196f8482019-11-02 12:09:39 +01005375static int make_parent_dir(const char *path)
5376{
5377 char *cp, errmsg[STRERR_BUFSIZE];
5378 char *dname, *dir;
5379 int err = 0;
5380
5381 dname = strdup(path);
5382 if (dname == NULL)
5383 return -ENOMEM;
5384
5385 dir = dirname(dname);
5386 if (mkdir(dir, 0700) && errno != EEXIST)
5387 err = -errno;
5388
5389 free(dname);
5390 if (err) {
5391 cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
5392 pr_warn("failed to mkdir %s: %s\n", path, cp);
5393 }
5394 return err;
5395}
5396
Joe Stringerf3675402017-01-26 13:19:56 -08005397static int check_path(const char *path)
5398{
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02005399 char *cp, errmsg[STRERR_BUFSIZE];
Joe Stringerf3675402017-01-26 13:19:56 -08005400 struct statfs st_fs;
5401 char *dname, *dir;
5402 int err = 0;
5403
5404 if (path == NULL)
5405 return -EINVAL;
5406
5407 dname = strdup(path);
5408 if (dname == NULL)
5409 return -ENOMEM;
5410
5411 dir = dirname(dname);
5412 if (statfs(dir, &st_fs)) {
Andrey Ignatov24d6a802018-10-03 15:26:41 -07005413 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
Kefeng Wangbe180102019-10-21 13:55:32 +08005414 pr_warn("failed to statfs %s: %s\n", dir, cp);
Joe Stringerf3675402017-01-26 13:19:56 -08005415 err = -errno;
5416 }
5417 free(dname);
5418
5419 if (!err && st_fs.f_type != BPF_FS_MAGIC) {
Kefeng Wangbe180102019-10-21 13:55:32 +08005420 pr_warn("specified path %s is not on BPF FS\n", path);
Joe Stringerf3675402017-01-26 13:19:56 -08005421 err = -EINVAL;
5422 }
5423
5424 return err;
5425}
5426
5427int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
5428 int instance)
5429{
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02005430 char *cp, errmsg[STRERR_BUFSIZE];
Joe Stringerf3675402017-01-26 13:19:56 -08005431 int err;
5432
Toke Høiland-Jørgensen196f8482019-11-02 12:09:39 +01005433 err = make_parent_dir(path);
5434 if (err)
5435 return err;
5436
Joe Stringerf3675402017-01-26 13:19:56 -08005437 err = check_path(path);
5438 if (err)
5439 return err;
5440
5441 if (prog == NULL) {
Kefeng Wangbe180102019-10-21 13:55:32 +08005442 pr_warn("invalid program pointer\n");
Joe Stringerf3675402017-01-26 13:19:56 -08005443 return -EINVAL;
5444 }
5445
5446 if (instance < 0 || instance >= prog->instances.nr) {
Kefeng Wangbe180102019-10-21 13:55:32 +08005447 pr_warn("invalid prog instance %d of prog %s (max %d)\n",
5448 instance, prog->section_name, prog->instances.nr);
Joe Stringerf3675402017-01-26 13:19:56 -08005449 return -EINVAL;
5450 }
5451
5452 if (bpf_obj_pin(prog->instances.fds[instance], path)) {
Andrey Ignatov24d6a802018-10-03 15:26:41 -07005453 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
Kefeng Wangbe180102019-10-21 13:55:32 +08005454 pr_warn("failed to pin program: %s\n", cp);
Joe Stringerf3675402017-01-26 13:19:56 -08005455 return -errno;
5456 }
5457 pr_debug("pinned program '%s'\n", path);
5458
5459 return 0;
5460}
5461
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005462int bpf_program__unpin_instance(struct bpf_program *prog, const char *path,
5463 int instance)
5464{
5465 int err;
5466
5467 err = check_path(path);
5468 if (err)
5469 return err;
5470
5471 if (prog == NULL) {
Kefeng Wangbe180102019-10-21 13:55:32 +08005472 pr_warn("invalid program pointer\n");
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005473 return -EINVAL;
5474 }
5475
5476 if (instance < 0 || instance >= prog->instances.nr) {
Kefeng Wangbe180102019-10-21 13:55:32 +08005477 pr_warn("invalid prog instance %d of prog %s (max %d)\n",
5478 instance, prog->section_name, prog->instances.nr);
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005479 return -EINVAL;
5480 }
5481
5482 err = unlink(path);
5483 if (err != 0)
5484 return -errno;
5485 pr_debug("unpinned program '%s'\n", path);
5486
5487 return 0;
5488}
5489
Joe Stringerf3675402017-01-26 13:19:56 -08005490int bpf_program__pin(struct bpf_program *prog, const char *path)
5491{
5492 int i, err;
5493
Toke Høiland-Jørgensen196f8482019-11-02 12:09:39 +01005494 err = make_parent_dir(path);
5495 if (err)
5496 return err;
5497
Joe Stringerf3675402017-01-26 13:19:56 -08005498 err = check_path(path);
5499 if (err)
5500 return err;
5501
5502 if (prog == NULL) {
Kefeng Wangbe180102019-10-21 13:55:32 +08005503 pr_warn("invalid program pointer\n");
Joe Stringerf3675402017-01-26 13:19:56 -08005504 return -EINVAL;
5505 }
5506
5507 if (prog->instances.nr <= 0) {
Kefeng Wangbe180102019-10-21 13:55:32 +08005508 pr_warn("no instances of prog %s to pin\n",
Joe Stringerf3675402017-01-26 13:19:56 -08005509 prog->section_name);
5510 return -EINVAL;
5511 }
5512
Stanislav Fomichevfd734c52018-11-09 08:21:42 -08005513 if (prog->instances.nr == 1) {
5514 /* don't create subdirs when pinning single instance */
5515 return bpf_program__pin_instance(prog, path, 0);
5516 }
5517
Joe Stringerf3675402017-01-26 13:19:56 -08005518 for (i = 0; i < prog->instances.nr; i++) {
5519 char buf[PATH_MAX];
5520 int len;
5521
5522 len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005523 if (len < 0) {
5524 err = -EINVAL;
5525 goto err_unpin;
5526 } else if (len >= PATH_MAX) {
5527 err = -ENAMETOOLONG;
5528 goto err_unpin;
5529 }
5530
5531 err = bpf_program__pin_instance(prog, buf, i);
5532 if (err)
5533 goto err_unpin;
5534 }
5535
5536 return 0;
5537
5538err_unpin:
5539 for (i = i - 1; i >= 0; i--) {
5540 char buf[PATH_MAX];
5541 int len;
5542
5543 len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
5544 if (len < 0)
5545 continue;
5546 else if (len >= PATH_MAX)
5547 continue;
5548
5549 bpf_program__unpin_instance(prog, buf, i);
5550 }
5551
5552 rmdir(path);
5553
5554 return err;
5555}
5556
5557int bpf_program__unpin(struct bpf_program *prog, const char *path)
5558{
5559 int i, err;
5560
5561 err = check_path(path);
5562 if (err)
5563 return err;
5564
5565 if (prog == NULL) {
Kefeng Wangbe180102019-10-21 13:55:32 +08005566 pr_warn("invalid program pointer\n");
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005567 return -EINVAL;
5568 }
5569
5570 if (prog->instances.nr <= 0) {
Kefeng Wangbe180102019-10-21 13:55:32 +08005571 pr_warn("no instances of prog %s to pin\n",
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005572 prog->section_name);
5573 return -EINVAL;
5574 }
5575
Stanislav Fomichevfd734c52018-11-09 08:21:42 -08005576 if (prog->instances.nr == 1) {
5577 /* don't create subdirs when pinning single instance */
5578 return bpf_program__unpin_instance(prog, path, 0);
5579 }
5580
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005581 for (i = 0; i < prog->instances.nr; i++) {
5582 char buf[PATH_MAX];
5583 int len;
5584
5585 len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
Joe Stringerf3675402017-01-26 13:19:56 -08005586 if (len < 0)
5587 return -EINVAL;
5588 else if (len >= PATH_MAX)
5589 return -ENAMETOOLONG;
5590
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005591 err = bpf_program__unpin_instance(prog, buf, i);
Joe Stringerf3675402017-01-26 13:19:56 -08005592 if (err)
5593 return err;
5594 }
5595
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005596 err = rmdir(path);
5597 if (err)
5598 return -errno;
5599
Joe Stringerf3675402017-01-26 13:19:56 -08005600 return 0;
5601}
5602
Joe Stringerb6989f32017-01-26 13:19:57 -08005603int bpf_map__pin(struct bpf_map *map, const char *path)
5604{
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02005605 char *cp, errmsg[STRERR_BUFSIZE];
Joe Stringerb6989f32017-01-26 13:19:57 -08005606 int err;
5607
Joe Stringerb6989f32017-01-26 13:19:57 -08005608 if (map == NULL) {
Kefeng Wangbe180102019-10-21 13:55:32 +08005609 pr_warn("invalid map pointer\n");
Joe Stringerb6989f32017-01-26 13:19:57 -08005610 return -EINVAL;
5611 }
5612
Toke Høiland-Jørgensen4580b252019-11-02 12:09:38 +01005613 if (map->pin_path) {
5614 if (path && strcmp(path, map->pin_path)) {
5615 pr_warn("map '%s' already has pin path '%s' different from '%s'\n",
5616 bpf_map__name(map), map->pin_path, path);
5617 return -EINVAL;
5618 } else if (map->pinned) {
5619 pr_debug("map '%s' already pinned at '%s'; not re-pinning\n",
5620 bpf_map__name(map), map->pin_path);
5621 return 0;
5622 }
5623 } else {
5624 if (!path) {
5625 pr_warn("missing a path to pin map '%s' at\n",
5626 bpf_map__name(map));
5627 return -EINVAL;
5628 } else if (map->pinned) {
5629 pr_warn("map '%s' already pinned\n", bpf_map__name(map));
5630 return -EEXIST;
5631 }
5632
5633 map->pin_path = strdup(path);
5634 if (!map->pin_path) {
5635 err = -errno;
5636 goto out_err;
5637 }
Joe Stringerb6989f32017-01-26 13:19:57 -08005638 }
5639
Toke Høiland-Jørgensen196f8482019-11-02 12:09:39 +01005640 err = make_parent_dir(map->pin_path);
5641 if (err)
5642 return err;
5643
Toke Høiland-Jørgensen4580b252019-11-02 12:09:38 +01005644 err = check_path(map->pin_path);
5645 if (err)
5646 return err;
5647
5648 if (bpf_obj_pin(map->fd, map->pin_path)) {
5649 err = -errno;
5650 goto out_err;
5651 }
5652
5653 map->pinned = true;
5654 pr_debug("pinned map '%s'\n", map->pin_path);
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005655
Joe Stringerb6989f32017-01-26 13:19:57 -08005656 return 0;
Toke Høiland-Jørgensen4580b252019-11-02 12:09:38 +01005657
5658out_err:
5659 cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
5660 pr_warn("failed to pin map: %s\n", cp);
5661 return err;
Joe Stringerb6989f32017-01-26 13:19:57 -08005662}
5663
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005664int bpf_map__unpin(struct bpf_map *map, const char *path)
Joe Stringerd5148d82017-01-26 13:19:58 -08005665{
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005666 int err;
5667
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005668 if (map == NULL) {
Kefeng Wangbe180102019-10-21 13:55:32 +08005669 pr_warn("invalid map pointer\n");
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005670 return -EINVAL;
5671 }
5672
Toke Høiland-Jørgensen4580b252019-11-02 12:09:38 +01005673 if (map->pin_path) {
5674 if (path && strcmp(path, map->pin_path)) {
5675 pr_warn("map '%s' already has pin path '%s' different from '%s'\n",
5676 bpf_map__name(map), map->pin_path, path);
5677 return -EINVAL;
5678 }
5679 path = map->pin_path;
5680 } else if (!path) {
5681 pr_warn("no path to unpin map '%s' from\n",
5682 bpf_map__name(map));
5683 return -EINVAL;
5684 }
5685
5686 err = check_path(path);
5687 if (err)
5688 return err;
5689
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005690 err = unlink(path);
5691 if (err != 0)
5692 return -errno;
Toke Høiland-Jørgensen4580b252019-11-02 12:09:38 +01005693
5694 map->pinned = false;
5695 pr_debug("unpinned map '%s' from '%s'\n", bpf_map__name(map), path);
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005696
5697 return 0;
5698}
5699
Toke Høiland-Jørgensen4580b252019-11-02 12:09:38 +01005700int bpf_map__set_pin_path(struct bpf_map *map, const char *path)
5701{
5702 char *new = NULL;
5703
5704 if (path) {
5705 new = strdup(path);
5706 if (!new)
5707 return -errno;
5708 }
5709
5710 free(map->pin_path);
5711 map->pin_path = new;
5712 return 0;
5713}
5714
5715const char *bpf_map__get_pin_path(const struct bpf_map *map)
5716{
5717 return map->pin_path;
5718}
5719
5720bool bpf_map__is_pinned(const struct bpf_map *map)
5721{
5722 return map->pinned;
5723}
5724
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005725int bpf_object__pin_maps(struct bpf_object *obj, const char *path)
5726{
Joe Stringerd5148d82017-01-26 13:19:58 -08005727 struct bpf_map *map;
5728 int err;
5729
5730 if (!obj)
5731 return -ENOENT;
5732
5733 if (!obj->loaded) {
Kefeng Wangbe180102019-10-21 13:55:32 +08005734 pr_warn("object not yet loaded; load it first\n");
Joe Stringerd5148d82017-01-26 13:19:58 -08005735 return -ENOENT;
5736 }
5737
Jakub Kicinskif74a53d92019-02-27 19:04:12 -08005738 bpf_object__for_each_map(map, obj) {
Toke Høiland-Jørgensen4580b252019-11-02 12:09:38 +01005739 char *pin_path = NULL;
Joe Stringerd5148d82017-01-26 13:19:58 -08005740 char buf[PATH_MAX];
Joe Stringerd5148d82017-01-26 13:19:58 -08005741
Toke Høiland-Jørgensen4580b252019-11-02 12:09:38 +01005742 if (path) {
5743 int len;
5744
5745 len = snprintf(buf, PATH_MAX, "%s/%s", path,
5746 bpf_map__name(map));
5747 if (len < 0) {
5748 err = -EINVAL;
5749 goto err_unpin_maps;
5750 } else if (len >= PATH_MAX) {
5751 err = -ENAMETOOLONG;
5752 goto err_unpin_maps;
5753 }
5754 pin_path = buf;
5755 } else if (!map->pin_path) {
5756 continue;
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005757 }
5758
Toke Høiland-Jørgensen4580b252019-11-02 12:09:38 +01005759 err = bpf_map__pin(map, pin_path);
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005760 if (err)
5761 goto err_unpin_maps;
5762 }
5763
5764 return 0;
5765
5766err_unpin_maps:
5767 while ((map = bpf_map__prev(map, obj))) {
Toke Høiland-Jørgensen4580b252019-11-02 12:09:38 +01005768 if (!map->pin_path)
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005769 continue;
5770
Toke Høiland-Jørgensen4580b252019-11-02 12:09:38 +01005771 bpf_map__unpin(map, NULL);
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005772 }
5773
5774 return err;
5775}
5776
5777int bpf_object__unpin_maps(struct bpf_object *obj, const char *path)
5778{
5779 struct bpf_map *map;
5780 int err;
5781
5782 if (!obj)
5783 return -ENOENT;
5784
Jakub Kicinskif74a53d92019-02-27 19:04:12 -08005785 bpf_object__for_each_map(map, obj) {
Toke Høiland-Jørgensen4580b252019-11-02 12:09:38 +01005786 char *pin_path = NULL;
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005787 char buf[PATH_MAX];
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005788
Toke Høiland-Jørgensen4580b252019-11-02 12:09:38 +01005789 if (path) {
5790 int len;
Joe Stringerd5148d82017-01-26 13:19:58 -08005791
Toke Høiland-Jørgensen4580b252019-11-02 12:09:38 +01005792 len = snprintf(buf, PATH_MAX, "%s/%s", path,
5793 bpf_map__name(map));
5794 if (len < 0)
5795 return -EINVAL;
5796 else if (len >= PATH_MAX)
5797 return -ENAMETOOLONG;
5798 pin_path = buf;
5799 } else if (!map->pin_path) {
5800 continue;
5801 }
5802
5803 err = bpf_map__unpin(map, pin_path);
Joe Stringerd5148d82017-01-26 13:19:58 -08005804 if (err)
5805 return err;
5806 }
5807
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005808 return 0;
5809}
5810
5811int bpf_object__pin_programs(struct bpf_object *obj, const char *path)
5812{
5813 struct bpf_program *prog;
5814 int err;
5815
5816 if (!obj)
5817 return -ENOENT;
5818
5819 if (!obj->loaded) {
Kefeng Wangbe180102019-10-21 13:55:32 +08005820 pr_warn("object not yet loaded; load it first\n");
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005821 return -ENOENT;
5822 }
5823
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005824 bpf_object__for_each_program(prog, obj) {
5825 char buf[PATH_MAX];
5826 int len;
5827
5828 len = snprintf(buf, PATH_MAX, "%s/%s", path,
Stanislav Fomichev33a2c752018-11-09 08:21:43 -08005829 prog->pin_name);
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005830 if (len < 0) {
5831 err = -EINVAL;
5832 goto err_unpin_programs;
5833 } else if (len >= PATH_MAX) {
5834 err = -ENAMETOOLONG;
5835 goto err_unpin_programs;
5836 }
5837
5838 err = bpf_program__pin(prog, buf);
5839 if (err)
5840 goto err_unpin_programs;
5841 }
5842
5843 return 0;
5844
5845err_unpin_programs:
5846 while ((prog = bpf_program__prev(prog, obj))) {
5847 char buf[PATH_MAX];
5848 int len;
5849
5850 len = snprintf(buf, PATH_MAX, "%s/%s", path,
Stanislav Fomichev33a2c752018-11-09 08:21:43 -08005851 prog->pin_name);
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005852 if (len < 0)
5853 continue;
5854 else if (len >= PATH_MAX)
5855 continue;
5856
5857 bpf_program__unpin(prog, buf);
5858 }
5859
5860 return err;
5861}
5862
5863int bpf_object__unpin_programs(struct bpf_object *obj, const char *path)
5864{
5865 struct bpf_program *prog;
5866 int err;
5867
5868 if (!obj)
5869 return -ENOENT;
5870
Joe Stringerd5148d82017-01-26 13:19:58 -08005871 bpf_object__for_each_program(prog, obj) {
5872 char buf[PATH_MAX];
5873 int len;
5874
5875 len = snprintf(buf, PATH_MAX, "%s/%s", path,
Stanislav Fomichev33a2c752018-11-09 08:21:43 -08005876 prog->pin_name);
Joe Stringerd5148d82017-01-26 13:19:58 -08005877 if (len < 0)
5878 return -EINVAL;
5879 else if (len >= PATH_MAX)
5880 return -ENAMETOOLONG;
5881
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005882 err = bpf_program__unpin(prog, buf);
Joe Stringerd5148d82017-01-26 13:19:58 -08005883 if (err)
5884 return err;
5885 }
5886
5887 return 0;
5888}
5889
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08005890int bpf_object__pin(struct bpf_object *obj, const char *path)
5891{
5892 int err;
5893
5894 err = bpf_object__pin_maps(obj, path);
5895 if (err)
5896 return err;
5897
5898 err = bpf_object__pin_programs(obj, path);
5899 if (err) {
5900 bpf_object__unpin_maps(obj, path);
5901 return err;
5902 }
5903
5904 return 0;
5905}
5906
Wang Nan1a5e3fb2015-07-01 02:13:53 +00005907void bpf_object__close(struct bpf_object *obj)
5908{
Wang Nana5b8bd42015-07-01 02:14:00 +00005909 size_t i;
5910
Wang Nan1a5e3fb2015-07-01 02:13:53 +00005911 if (!obj)
5912 return;
5913
Wang Nan10931d22016-11-26 07:03:26 +00005914 if (obj->clear_priv)
5915 obj->clear_priv(obj, obj->priv);
5916
Wang Nan1a5e3fb2015-07-01 02:13:53 +00005917 bpf_object__elf_finish(obj);
Wang Nan52d33522015-07-01 02:14:04 +00005918 bpf_object__unload(obj);
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07005919 btf__free(obj->btf);
Yonghong Song2993e052018-11-19 15:29:16 -08005920 btf_ext__free(obj->btf_ext);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00005921
Wang Nan9d759a92015-11-27 08:47:35 +00005922 for (i = 0; i < obj->nr_maps; i++) {
Andrii Nakryikoeba9c5f2019-12-13 17:43:33 -08005923 struct bpf_map *map = &obj->maps[i];
5924
5925 if (map->clear_priv)
5926 map->clear_priv(map, map->priv);
5927 map->priv = NULL;
5928 map->clear_priv = NULL;
5929
5930 if (map->mmaped) {
5931 munmap(map->mmaped, bpf_map_mmap_sz(map));
5932 map->mmaped = NULL;
5933 }
5934
Martin KaFai Lau590a0082020-01-08 16:35:14 -08005935 if (map->st_ops) {
5936 zfree(&map->st_ops->data);
5937 zfree(&map->st_ops->progs);
5938 zfree(&map->st_ops->kern_func_off);
5939 zfree(&map->st_ops);
5940 }
5941
Andrii Nakryikoeba9c5f2019-12-13 17:43:33 -08005942 zfree(&map->name);
5943 zfree(&map->pin_path);
Wang Nan9d759a92015-11-27 08:47:35 +00005944 }
Daniel Borkmannd8599002019-04-09 23:20:13 +02005945
Andrii Nakryiko8601fd42019-12-18 16:28:35 -08005946 zfree(&obj->kconfig);
Andrii Nakryiko166750b2019-12-13 17:47:08 -08005947 zfree(&obj->externs);
5948 obj->nr_extern = 0;
5949
Wang Nan9d759a92015-11-27 08:47:35 +00005950 zfree(&obj->maps);
5951 obj->nr_maps = 0;
Wang Nana5b8bd42015-07-01 02:14:00 +00005952
5953 if (obj->programs && obj->nr_programs) {
5954 for (i = 0; i < obj->nr_programs; i++)
5955 bpf_program__exit(&obj->programs[i]);
5956 }
5957 zfree(&obj->programs);
5958
Wang Nan9a208ef2015-07-01 02:14:10 +00005959 list_del(&obj->list);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00005960 free(obj);
5961}
Wang Nanaa9b1ac2015-07-01 02:14:08 +00005962
Wang Nan9a208ef2015-07-01 02:14:10 +00005963struct bpf_object *
5964bpf_object__next(struct bpf_object *prev)
5965{
5966 struct bpf_object *next;
5967
5968 if (!prev)
5969 next = list_first_entry(&bpf_objects_list,
5970 struct bpf_object,
5971 list);
5972 else
5973 next = list_next_entry(prev, list);
5974
5975 /* Empty list is noticed here so don't need checking on entry. */
5976 if (&next->list == &bpf_objects_list)
5977 return NULL;
5978
5979 return next;
5980}
5981
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07005982const char *bpf_object__name(const struct bpf_object *obj)
Wang Nanacf860a2015-08-27 02:30:55 +00005983{
Andrii Nakryikoc9e4c302019-10-04 15:40:36 -07005984 return obj ? obj->name : ERR_PTR(-EINVAL);
Wang Nanacf860a2015-08-27 02:30:55 +00005985}
5986
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07005987unsigned int bpf_object__kversion(const struct bpf_object *obj)
Wang Nan45825d82015-11-06 13:49:38 +00005988{
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03005989 return obj ? obj->kern_version : 0;
Wang Nan45825d82015-11-06 13:49:38 +00005990}
5991
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07005992struct btf *bpf_object__btf(const struct bpf_object *obj)
Andrey Ignatov789f6ba2019-02-14 15:01:43 -08005993{
5994 return obj ? obj->btf : NULL;
5995}
5996
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07005997int bpf_object__btf_fd(const struct bpf_object *obj)
5998{
5999 return obj->btf ? btf__fd(obj->btf) : -1;
6000}
6001
Wang Nan10931d22016-11-26 07:03:26 +00006002int bpf_object__set_priv(struct bpf_object *obj, void *priv,
6003 bpf_object_clear_priv_t clear_priv)
6004{
6005 if (obj->priv && obj->clear_priv)
6006 obj->clear_priv(obj, obj->priv);
6007
6008 obj->priv = priv;
6009 obj->clear_priv = clear_priv;
6010 return 0;
6011}
6012
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07006013void *bpf_object__priv(const struct bpf_object *obj)
Wang Nan10931d22016-11-26 07:03:26 +00006014{
6015 return obj ? obj->priv : ERR_PTR(-EINVAL);
6016}
6017
Jakub Kicinskieac7d842018-06-28 14:41:39 -07006018static struct bpf_program *
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07006019__bpf_program__iter(const struct bpf_program *p, const struct bpf_object *obj,
6020 bool forward)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00006021{
Martin KaFai Laua83d6e72018-11-12 15:44:53 -08006022 size_t nr_programs = obj->nr_programs;
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08006023 ssize_t idx;
Wang Nanaa9b1ac2015-07-01 02:14:08 +00006024
Martin KaFai Laua83d6e72018-11-12 15:44:53 -08006025 if (!nr_programs)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00006026 return NULL;
Wang Nanaa9b1ac2015-07-01 02:14:08 +00006027
Martin KaFai Laua83d6e72018-11-12 15:44:53 -08006028 if (!p)
6029 /* Iter from the beginning */
6030 return forward ? &obj->programs[0] :
6031 &obj->programs[nr_programs - 1];
6032
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08006033 if (p->obj != obj) {
Kefeng Wangbe180102019-10-21 13:55:32 +08006034 pr_warn("error: program handler doesn't match object\n");
Wang Nanaa9b1ac2015-07-01 02:14:08 +00006035 return NULL;
6036 }
6037
Martin KaFai Laua83d6e72018-11-12 15:44:53 -08006038 idx = (p - obj->programs) + (forward ? 1 : -1);
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08006039 if (idx >= obj->nr_programs || idx < 0)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00006040 return NULL;
6041 return &obj->programs[idx];
6042}
6043
Jakub Kicinskieac7d842018-06-28 14:41:39 -07006044struct bpf_program *
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07006045bpf_program__next(struct bpf_program *prev, const struct bpf_object *obj)
Jakub Kicinskieac7d842018-06-28 14:41:39 -07006046{
6047 struct bpf_program *prog = prev;
6048
6049 do {
Martin KaFai Laua83d6e72018-11-12 15:44:53 -08006050 prog = __bpf_program__iter(prog, obj, true);
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08006051 } while (prog && bpf_program__is_function_storage(prog, obj));
6052
6053 return prog;
6054}
6055
6056struct bpf_program *
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07006057bpf_program__prev(struct bpf_program *next, const struct bpf_object *obj)
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08006058{
6059 struct bpf_program *prog = next;
6060
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08006061 do {
Martin KaFai Laua83d6e72018-11-12 15:44:53 -08006062 prog = __bpf_program__iter(prog, obj, false);
Jakub Kicinskieac7d842018-06-28 14:41:39 -07006063 } while (prog && bpf_program__is_function_storage(prog, obj));
6064
6065 return prog;
6066}
6067
Arnaldo Carvalho de Meloedb13ed2016-06-03 12:38:21 -03006068int bpf_program__set_priv(struct bpf_program *prog, void *priv,
6069 bpf_program_clear_priv_t clear_priv)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00006070{
6071 if (prog->priv && prog->clear_priv)
6072 prog->clear_priv(prog, prog->priv);
6073
6074 prog->priv = priv;
6075 prog->clear_priv = clear_priv;
6076 return 0;
6077}
6078
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07006079void *bpf_program__priv(const struct bpf_program *prog)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00006080{
Arnaldo Carvalho de Melobe834ff2016-06-03 12:36:39 -03006081 return prog ? prog->priv : ERR_PTR(-EINVAL);
Wang Nanaa9b1ac2015-07-01 02:14:08 +00006082}
6083
Jakub Kicinski9aba3612018-06-28 14:41:37 -07006084void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex)
6085{
6086 prog->prog_ifindex = ifindex;
6087}
6088
Andrii Nakryiko01af3bf2019-12-13 17:43:32 -08006089const char *bpf_program__name(const struct bpf_program *prog)
6090{
6091 return prog->name;
6092}
6093
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07006094const char *bpf_program__title(const struct bpf_program *prog, bool needs_copy)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00006095{
6096 const char *title;
6097
6098 title = prog->section_name;
Namhyung Kim715f8db2015-11-03 20:21:05 +09006099 if (needs_copy) {
Wang Nanaa9b1ac2015-07-01 02:14:08 +00006100 title = strdup(title);
6101 if (!title) {
Kefeng Wangbe180102019-10-21 13:55:32 +08006102 pr_warn("failed to strdup program title\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +00006103 return ERR_PTR(-ENOMEM);
Wang Nanaa9b1ac2015-07-01 02:14:08 +00006104 }
6105 }
6106
6107 return title;
6108}
6109
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07006110int bpf_program__fd(const struct bpf_program *prog)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00006111{
Wang Nanb5805632015-11-16 12:10:09 +00006112 return bpf_program__nth_fd(prog, 0);
6113}
6114
Toke Høiland-Jørgensen1a734ef2019-11-09 21:37:32 +01006115size_t bpf_program__size(const struct bpf_program *prog)
6116{
6117 return prog->insns_cnt * sizeof(struct bpf_insn);
6118}
6119
Wang Nanb5805632015-11-16 12:10:09 +00006120int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
6121 bpf_program_prep_t prep)
6122{
6123 int *instances_fds;
6124
6125 if (nr_instances <= 0 || !prep)
6126 return -EINVAL;
6127
6128 if (prog->instances.nr > 0 || prog->instances.fds) {
Kefeng Wangbe180102019-10-21 13:55:32 +08006129 pr_warn("Can't set pre-processor after loading\n");
Wang Nanb5805632015-11-16 12:10:09 +00006130 return -EINVAL;
6131 }
6132
6133 instances_fds = malloc(sizeof(int) * nr_instances);
6134 if (!instances_fds) {
Kefeng Wangbe180102019-10-21 13:55:32 +08006135 pr_warn("alloc memory failed for fds\n");
Wang Nanb5805632015-11-16 12:10:09 +00006136 return -ENOMEM;
6137 }
6138
6139 /* fill all fd with -1 */
6140 memset(instances_fds, -1, sizeof(int) * nr_instances);
6141
6142 prog->instances.nr = nr_instances;
6143 prog->instances.fds = instances_fds;
6144 prog->preprocessor = prep;
6145 return 0;
6146}
6147
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07006148int bpf_program__nth_fd(const struct bpf_program *prog, int n)
Wang Nanb5805632015-11-16 12:10:09 +00006149{
6150 int fd;
6151
Jakub Kicinski1e960042018-07-26 14:32:18 -07006152 if (!prog)
6153 return -EINVAL;
6154
Wang Nanb5805632015-11-16 12:10:09 +00006155 if (n >= prog->instances.nr || n < 0) {
Kefeng Wangbe180102019-10-21 13:55:32 +08006156 pr_warn("Can't get the %dth fd from program %s: only %d instances\n",
6157 n, prog->section_name, prog->instances.nr);
Wang Nanb5805632015-11-16 12:10:09 +00006158 return -EINVAL;
6159 }
6160
6161 fd = prog->instances.fds[n];
6162 if (fd < 0) {
Kefeng Wangbe180102019-10-21 13:55:32 +08006163 pr_warn("%dth instance of program '%s' is invalid\n",
6164 n, prog->section_name);
Wang Nanb5805632015-11-16 12:10:09 +00006165 return -ENOENT;
6166 }
6167
6168 return fd;
Wang Nanaa9b1ac2015-07-01 02:14:08 +00006169}
Wang Nan9d759a92015-11-27 08:47:35 +00006170
Andrii Nakryikof1eead92019-10-20 20:38:57 -07006171enum bpf_prog_type bpf_program__get_type(struct bpf_program *prog)
6172{
6173 return prog->type;
6174}
6175
Alexei Starovoitovdd26b7f2017-03-30 21:45:40 -07006176void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
Wang Nan5f44e4c82016-07-13 10:44:01 +00006177{
6178 prog->type = type;
6179}
6180
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07006181static bool bpf_program__is_type(const struct bpf_program *prog,
Wang Nan5f44e4c82016-07-13 10:44:01 +00006182 enum bpf_prog_type type)
6183{
6184 return prog ? (prog->type == type) : false;
6185}
6186
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07006187#define BPF_PROG_TYPE_FNS(NAME, TYPE) \
6188int bpf_program__set_##NAME(struct bpf_program *prog) \
6189{ \
6190 if (!prog) \
6191 return -EINVAL; \
6192 bpf_program__set_type(prog, TYPE); \
6193 return 0; \
6194} \
6195 \
6196bool bpf_program__is_##NAME(const struct bpf_program *prog) \
6197{ \
6198 return bpf_program__is_type(prog, TYPE); \
6199} \
Wang Nan5f44e4c82016-07-13 10:44:01 +00006200
Joe Stringer7803ba72017-01-22 17:11:24 -08006201BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
KP Singh1e092a032020-03-29 01:43:54 +01006202BPF_PROG_TYPE_FNS(lsm, BPF_PROG_TYPE_LSM);
Joe Stringered794072017-01-22 17:11:23 -08006203BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE);
Joe Stringer7803ba72017-01-22 17:11:24 -08006204BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS);
6205BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT);
Joe Stringered794072017-01-22 17:11:23 -08006206BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT);
Andrey Ignatove14c93fd2018-04-17 10:28:46 -07006207BPF_PROG_TYPE_FNS(raw_tracepoint, BPF_PROG_TYPE_RAW_TRACEPOINT);
Joe Stringer7803ba72017-01-22 17:11:24 -08006208BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP);
6209BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT);
Alexei Starovoitov12a86542019-10-30 15:32:12 -07006210BPF_PROG_TYPE_FNS(tracing, BPF_PROG_TYPE_TRACING);
Martin KaFai Lau590a0082020-01-08 16:35:14 -08006211BPF_PROG_TYPE_FNS(struct_ops, BPF_PROG_TYPE_STRUCT_OPS);
Alexei Starovoitov2db6eab2020-01-20 16:53:47 -08006212BPF_PROG_TYPE_FNS(extension, BPF_PROG_TYPE_EXT);
Wang Nan5f44e4c82016-07-13 10:44:01 +00006213
Andrii Nakryikof1eead92019-10-20 20:38:57 -07006214enum bpf_attach_type
6215bpf_program__get_expected_attach_type(struct bpf_program *prog)
6216{
6217 return prog->expected_attach_type;
6218}
6219
John Fastabend16962b22018-04-23 14:30:38 -07006220void bpf_program__set_expected_attach_type(struct bpf_program *prog,
6221 enum bpf_attach_type type)
Andrey Ignatovd7be1432018-03-30 15:08:01 -07006222{
6223 prog->expected_attach_type = type;
6224}
6225
Alexei Starovoitovf75a6972019-10-15 20:24:59 -07006226#define BPF_PROG_SEC_IMPL(string, ptype, eatype, is_attachable, btf, atype) \
6227 { string, sizeof(string) - 1, ptype, eatype, is_attachable, btf, atype }
Andrey Ignatovd7be1432018-03-30 15:08:01 -07006228
Andrey Ignatov956b6202018-09-26 15:24:53 -07006229/* Programs that can NOT be attached. */
Alexei Starovoitovf75a6972019-10-15 20:24:59 -07006230#define BPF_PROG_SEC(string, ptype) BPF_PROG_SEC_IMPL(string, ptype, 0, 0, 0, 0)
Andrey Ignatovd7be1432018-03-30 15:08:01 -07006231
Andrey Ignatov956b6202018-09-26 15:24:53 -07006232/* Programs that can be attached. */
6233#define BPF_APROG_SEC(string, ptype, atype) \
Alexei Starovoitovf75a6972019-10-15 20:24:59 -07006234 BPF_PROG_SEC_IMPL(string, ptype, 0, 1, 0, atype)
Andrey Ignatov81efee72018-04-17 10:28:45 -07006235
Andrey Ignatov956b6202018-09-26 15:24:53 -07006236/* Programs that must specify expected attach type at load time. */
6237#define BPF_EAPROG_SEC(string, ptype, eatype) \
Alexei Starovoitovf75a6972019-10-15 20:24:59 -07006238 BPF_PROG_SEC_IMPL(string, ptype, eatype, 1, 0, eatype)
6239
6240/* Programs that use BTF to identify attach point */
Alexei Starovoitov12a86542019-10-30 15:32:12 -07006241#define BPF_PROG_BTF(string, ptype, eatype) \
6242 BPF_PROG_SEC_IMPL(string, ptype, eatype, 0, 1, 0)
Andrey Ignatov956b6202018-09-26 15:24:53 -07006243
6244/* Programs that can be attached but attach type can't be identified by section
6245 * name. Kept for backward compatibility.
6246 */
6247#define BPF_APROG_COMPAT(string, ptype) BPF_PROG_SEC(string, ptype)
Andrey Ignatove50b0a62018-03-30 15:08:03 -07006248
Andrii Nakryikod7a18ea2019-12-13 17:43:26 -08006249#define SEC_DEF(sec_pfx, ptype, ...) { \
6250 .sec = sec_pfx, \
6251 .len = sizeof(sec_pfx) - 1, \
6252 .prog_type = BPF_PROG_TYPE_##ptype, \
6253 __VA_ARGS__ \
6254}
6255
6256struct bpf_sec_def;
6257
6258typedef struct bpf_link *(*attach_fn_t)(const struct bpf_sec_def *sec,
6259 struct bpf_program *prog);
6260
6261static struct bpf_link *attach_kprobe(const struct bpf_sec_def *sec,
6262 struct bpf_program *prog);
6263static struct bpf_link *attach_tp(const struct bpf_sec_def *sec,
6264 struct bpf_program *prog);
6265static struct bpf_link *attach_raw_tp(const struct bpf_sec_def *sec,
6266 struct bpf_program *prog);
6267static struct bpf_link *attach_trace(const struct bpf_sec_def *sec,
6268 struct bpf_program *prog);
KP Singh1e092a032020-03-29 01:43:54 +01006269static struct bpf_link *attach_lsm(const struct bpf_sec_def *sec,
6270 struct bpf_program *prog);
Andrii Nakryikod7a18ea2019-12-13 17:43:26 -08006271
6272struct bpf_sec_def {
Roman Gushchin583c9002017-12-13 15:18:51 +00006273 const char *sec;
6274 size_t len;
6275 enum bpf_prog_type prog_type;
Andrey Ignatovd7be1432018-03-30 15:08:01 -07006276 enum bpf_attach_type expected_attach_type;
Alexei Starovoitovf75a6972019-10-15 20:24:59 -07006277 bool is_attachable;
6278 bool is_attach_btf;
Andrey Ignatov956b6202018-09-26 15:24:53 -07006279 enum bpf_attach_type attach_type;
Andrii Nakryikod7a18ea2019-12-13 17:43:26 -08006280 attach_fn_t attach_fn;
6281};
6282
6283static const struct bpf_sec_def section_defs[] = {
Andrey Ignatov956b6202018-09-26 15:24:53 -07006284 BPF_PROG_SEC("socket", BPF_PROG_TYPE_SOCKET_FILTER),
Jakub Sitnicki67d69cc2019-12-12 11:22:50 +01006285 BPF_PROG_SEC("sk_reuseport", BPF_PROG_TYPE_SK_REUSEPORT),
Andrii Nakryikod7a18ea2019-12-13 17:43:26 -08006286 SEC_DEF("kprobe/", KPROBE,
6287 .attach_fn = attach_kprobe),
Andrii Nakryiko32dff6d2019-10-20 20:38:58 -07006288 BPF_PROG_SEC("uprobe/", BPF_PROG_TYPE_KPROBE),
Andrii Nakryikod7a18ea2019-12-13 17:43:26 -08006289 SEC_DEF("kretprobe/", KPROBE,
6290 .attach_fn = attach_kprobe),
Andrii Nakryiko32dff6d2019-10-20 20:38:58 -07006291 BPF_PROG_SEC("uretprobe/", BPF_PROG_TYPE_KPROBE),
Andrey Ignatov956b6202018-09-26 15:24:53 -07006292 BPF_PROG_SEC("classifier", BPF_PROG_TYPE_SCHED_CLS),
6293 BPF_PROG_SEC("action", BPF_PROG_TYPE_SCHED_ACT),
Andrii Nakryikod7a18ea2019-12-13 17:43:26 -08006294 SEC_DEF("tracepoint/", TRACEPOINT,
6295 .attach_fn = attach_tp),
6296 SEC_DEF("tp/", TRACEPOINT,
6297 .attach_fn = attach_tp),
6298 SEC_DEF("raw_tracepoint/", RAW_TRACEPOINT,
6299 .attach_fn = attach_raw_tp),
6300 SEC_DEF("raw_tp/", RAW_TRACEPOINT,
6301 .attach_fn = attach_raw_tp),
6302 SEC_DEF("tp_btf/", TRACING,
6303 .expected_attach_type = BPF_TRACE_RAW_TP,
6304 .is_attach_btf = true,
6305 .attach_fn = attach_trace),
6306 SEC_DEF("fentry/", TRACING,
6307 .expected_attach_type = BPF_TRACE_FENTRY,
6308 .is_attach_btf = true,
6309 .attach_fn = attach_trace),
KP Singhaca228c2020-03-04 20:18:51 +01006310 SEC_DEF("fmod_ret/", TRACING,
6311 .expected_attach_type = BPF_MODIFY_RETURN,
6312 .is_attach_btf = true,
6313 .attach_fn = attach_trace),
Andrii Nakryikod7a18ea2019-12-13 17:43:26 -08006314 SEC_DEF("fexit/", TRACING,
6315 .expected_attach_type = BPF_TRACE_FEXIT,
6316 .is_attach_btf = true,
6317 .attach_fn = attach_trace),
Alexei Starovoitov2db6eab2020-01-20 16:53:47 -08006318 SEC_DEF("freplace/", EXT,
6319 .is_attach_btf = true,
6320 .attach_fn = attach_trace),
KP Singh1e092a032020-03-29 01:43:54 +01006321 SEC_DEF("lsm/", LSM,
6322 .is_attach_btf = true,
6323 .expected_attach_type = BPF_LSM_MAC,
6324 .attach_fn = attach_lsm),
Andrey Ignatov956b6202018-09-26 15:24:53 -07006325 BPF_PROG_SEC("xdp", BPF_PROG_TYPE_XDP),
6326 BPF_PROG_SEC("perf_event", BPF_PROG_TYPE_PERF_EVENT),
6327 BPF_PROG_SEC("lwt_in", BPF_PROG_TYPE_LWT_IN),
6328 BPF_PROG_SEC("lwt_out", BPF_PROG_TYPE_LWT_OUT),
6329 BPF_PROG_SEC("lwt_xmit", BPF_PROG_TYPE_LWT_XMIT),
6330 BPF_PROG_SEC("lwt_seg6local", BPF_PROG_TYPE_LWT_SEG6LOCAL),
Andrey Ignatovbafa7af2018-09-26 15:24:54 -07006331 BPF_APROG_SEC("cgroup_skb/ingress", BPF_PROG_TYPE_CGROUP_SKB,
6332 BPF_CGROUP_INET_INGRESS),
6333 BPF_APROG_SEC("cgroup_skb/egress", BPF_PROG_TYPE_CGROUP_SKB,
6334 BPF_CGROUP_INET_EGRESS),
Andrey Ignatov956b6202018-09-26 15:24:53 -07006335 BPF_APROG_COMPAT("cgroup/skb", BPF_PROG_TYPE_CGROUP_SKB),
6336 BPF_APROG_SEC("cgroup/sock", BPF_PROG_TYPE_CGROUP_SOCK,
6337 BPF_CGROUP_INET_SOCK_CREATE),
6338 BPF_EAPROG_SEC("cgroup/post_bind4", BPF_PROG_TYPE_CGROUP_SOCK,
6339 BPF_CGROUP_INET4_POST_BIND),
6340 BPF_EAPROG_SEC("cgroup/post_bind6", BPF_PROG_TYPE_CGROUP_SOCK,
6341 BPF_CGROUP_INET6_POST_BIND),
6342 BPF_APROG_SEC("cgroup/dev", BPF_PROG_TYPE_CGROUP_DEVICE,
6343 BPF_CGROUP_DEVICE),
6344 BPF_APROG_SEC("sockops", BPF_PROG_TYPE_SOCK_OPS,
6345 BPF_CGROUP_SOCK_OPS),
Andrey Ignatovc6f68512018-09-26 15:24:55 -07006346 BPF_APROG_SEC("sk_skb/stream_parser", BPF_PROG_TYPE_SK_SKB,
6347 BPF_SK_SKB_STREAM_PARSER),
6348 BPF_APROG_SEC("sk_skb/stream_verdict", BPF_PROG_TYPE_SK_SKB,
6349 BPF_SK_SKB_STREAM_VERDICT),
Andrey Ignatov956b6202018-09-26 15:24:53 -07006350 BPF_APROG_COMPAT("sk_skb", BPF_PROG_TYPE_SK_SKB),
6351 BPF_APROG_SEC("sk_msg", BPF_PROG_TYPE_SK_MSG,
6352 BPF_SK_MSG_VERDICT),
6353 BPF_APROG_SEC("lirc_mode2", BPF_PROG_TYPE_LIRC_MODE2,
6354 BPF_LIRC_MODE2),
6355 BPF_APROG_SEC("flow_dissector", BPF_PROG_TYPE_FLOW_DISSECTOR,
6356 BPF_FLOW_DISSECTOR),
6357 BPF_EAPROG_SEC("cgroup/bind4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
6358 BPF_CGROUP_INET4_BIND),
6359 BPF_EAPROG_SEC("cgroup/bind6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
6360 BPF_CGROUP_INET6_BIND),
6361 BPF_EAPROG_SEC("cgroup/connect4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
6362 BPF_CGROUP_INET4_CONNECT),
6363 BPF_EAPROG_SEC("cgroup/connect6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
6364 BPF_CGROUP_INET6_CONNECT),
6365 BPF_EAPROG_SEC("cgroup/sendmsg4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
6366 BPF_CGROUP_UDP4_SENDMSG),
6367 BPF_EAPROG_SEC("cgroup/sendmsg6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
6368 BPF_CGROUP_UDP6_SENDMSG),
Daniel Borkmann9bb59ac2019-06-07 01:48:59 +02006369 BPF_EAPROG_SEC("cgroup/recvmsg4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
6370 BPF_CGROUP_UDP4_RECVMSG),
6371 BPF_EAPROG_SEC("cgroup/recvmsg6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
6372 BPF_CGROUP_UDP6_RECVMSG),
Andrey Ignatov063cc9f2019-03-08 09:15:26 -08006373 BPF_EAPROG_SEC("cgroup/sysctl", BPF_PROG_TYPE_CGROUP_SYSCTL,
6374 BPF_CGROUP_SYSCTL),
Stanislav Fomichev4cdbfb52019-06-27 13:38:49 -07006375 BPF_EAPROG_SEC("cgroup/getsockopt", BPF_PROG_TYPE_CGROUP_SOCKOPT,
6376 BPF_CGROUP_GETSOCKOPT),
6377 BPF_EAPROG_SEC("cgroup/setsockopt", BPF_PROG_TYPE_CGROUP_SOCKOPT,
6378 BPF_CGROUP_SETSOCKOPT),
Martin KaFai Lau590a0082020-01-08 16:35:14 -08006379 BPF_PROG_SEC("struct_ops", BPF_PROG_TYPE_STRUCT_OPS),
Roman Gushchin583c9002017-12-13 15:18:51 +00006380};
Roman Gushchin583c9002017-12-13 15:18:51 +00006381
Andrey Ignatov956b6202018-09-26 15:24:53 -07006382#undef BPF_PROG_SEC_IMPL
Andrey Ignatovd7be1432018-03-30 15:08:01 -07006383#undef BPF_PROG_SEC
Andrey Ignatov956b6202018-09-26 15:24:53 -07006384#undef BPF_APROG_SEC
6385#undef BPF_EAPROG_SEC
6386#undef BPF_APROG_COMPAT
Andrii Nakryikod7a18ea2019-12-13 17:43:26 -08006387#undef SEC_DEF
Andrey Ignatovd7be1432018-03-30 15:08:01 -07006388
Taeung Songc76e4c22019-01-21 22:06:38 +09006389#define MAX_TYPE_NAME_SIZE 32
6390
Andrii Nakryikod7a18ea2019-12-13 17:43:26 -08006391static const struct bpf_sec_def *find_sec_def(const char *sec_name)
6392{
6393 int i, n = ARRAY_SIZE(section_defs);
6394
6395 for (i = 0; i < n; i++) {
6396 if (strncmp(sec_name,
6397 section_defs[i].sec, section_defs[i].len))
6398 continue;
6399 return &section_defs[i];
6400 }
6401 return NULL;
6402}
6403
Taeung Songc76e4c22019-01-21 22:06:38 +09006404static char *libbpf_get_type_names(bool attach_type)
6405{
Andrii Nakryikod7a18ea2019-12-13 17:43:26 -08006406 int i, len = ARRAY_SIZE(section_defs) * MAX_TYPE_NAME_SIZE;
Taeung Songc76e4c22019-01-21 22:06:38 +09006407 char *buf;
6408
6409 buf = malloc(len);
6410 if (!buf)
6411 return NULL;
6412
6413 buf[0] = '\0';
6414 /* Forge string buf with all available names */
Andrii Nakryikod7a18ea2019-12-13 17:43:26 -08006415 for (i = 0; i < ARRAY_SIZE(section_defs); i++) {
6416 if (attach_type && !section_defs[i].is_attachable)
Taeung Songc76e4c22019-01-21 22:06:38 +09006417 continue;
6418
Andrii Nakryikod7a18ea2019-12-13 17:43:26 -08006419 if (strlen(buf) + strlen(section_defs[i].sec) + 2 > len) {
Taeung Songc76e4c22019-01-21 22:06:38 +09006420 free(buf);
6421 return NULL;
6422 }
6423 strcat(buf, " ");
Andrii Nakryikod7a18ea2019-12-13 17:43:26 -08006424 strcat(buf, section_defs[i].sec);
Taeung Songc76e4c22019-01-21 22:06:38 +09006425 }
6426
6427 return buf;
6428}
6429
Jakub Kicinskib60df2a2018-07-10 14:42:59 -07006430int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
6431 enum bpf_attach_type *expected_attach_type)
Roman Gushchin583c9002017-12-13 15:18:51 +00006432{
Andrii Nakryikod7a18ea2019-12-13 17:43:26 -08006433 const struct bpf_sec_def *sec_def;
Taeung Songc76e4c22019-01-21 22:06:38 +09006434 char *type_names;
Roman Gushchin583c9002017-12-13 15:18:51 +00006435
Jakub Kicinskib60df2a2018-07-10 14:42:59 -07006436 if (!name)
6437 return -EINVAL;
Roman Gushchin583c9002017-12-13 15:18:51 +00006438
Andrii Nakryikod7a18ea2019-12-13 17:43:26 -08006439 sec_def = find_sec_def(name);
6440 if (sec_def) {
6441 *prog_type = sec_def->prog_type;
6442 *expected_attach_type = sec_def->expected_attach_type;
Jakub Kicinskib60df2a2018-07-10 14:42:59 -07006443 return 0;
6444 }
Andrii Nakryikod7a18ea2019-12-13 17:43:26 -08006445
Andrii Nakryiko4a3d6c62019-12-17 15:42:28 -08006446 pr_debug("failed to guess program type from ELF section '%s'\n", name);
Taeung Songc76e4c22019-01-21 22:06:38 +09006447 type_names = libbpf_get_type_names(false);
6448 if (type_names != NULL) {
Andrii Nakryiko3f519352019-12-13 17:43:35 -08006449 pr_debug("supported section(type) names are:%s\n", type_names);
Taeung Songc76e4c22019-01-21 22:06:38 +09006450 free(type_names);
6451 }
6452
Andrii Nakryikodd4436b2019-10-20 20:38:59 -07006453 return -ESRCH;
Jakub Kicinskib60df2a2018-07-10 14:42:59 -07006454}
Roman Gushchin583c9002017-12-13 15:18:51 +00006455
Martin KaFai Lau590a0082020-01-08 16:35:14 -08006456static struct bpf_map *find_struct_ops_map_by_offset(struct bpf_object *obj,
6457 size_t offset)
6458{
6459 struct bpf_map *map;
6460 size_t i;
6461
6462 for (i = 0; i < obj->nr_maps; i++) {
6463 map = &obj->maps[i];
6464 if (!bpf_map__is_struct_ops(map))
6465 continue;
6466 if (map->sec_offset <= offset &&
6467 offset - map->sec_offset < map->def.value_size)
6468 return map;
6469 }
6470
6471 return NULL;
6472}
6473
6474/* Collect the reloc from ELF and populate the st_ops->progs[] */
6475static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj,
6476 GElf_Shdr *shdr,
6477 Elf_Data *data)
6478{
6479 const struct btf_member *member;
6480 struct bpf_struct_ops *st_ops;
6481 struct bpf_program *prog;
6482 unsigned int shdr_idx;
6483 const struct btf *btf;
6484 struct bpf_map *map;
6485 Elf_Data *symbols;
6486 unsigned int moff;
6487 const char *name;
Andrii Nakryiko1d1a3bc2020-01-10 10:19:16 -08006488 __u32 member_idx;
Martin KaFai Lau590a0082020-01-08 16:35:14 -08006489 GElf_Sym sym;
6490 GElf_Rel rel;
6491 int i, nrels;
6492
6493 symbols = obj->efile.symbols;
6494 btf = obj->btf;
6495 nrels = shdr->sh_size / shdr->sh_entsize;
6496 for (i = 0; i < nrels; i++) {
6497 if (!gelf_getrel(data, i, &rel)) {
6498 pr_warn("struct_ops reloc: failed to get %d reloc\n", i);
6499 return -LIBBPF_ERRNO__FORMAT;
6500 }
6501
6502 if (!gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym)) {
6503 pr_warn("struct_ops reloc: symbol %zx not found\n",
6504 (size_t)GELF_R_SYM(rel.r_info));
6505 return -LIBBPF_ERRNO__FORMAT;
6506 }
6507
6508 name = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
6509 sym.st_name) ? : "<?>";
6510 map = find_struct_ops_map_by_offset(obj, rel.r_offset);
6511 if (!map) {
6512 pr_warn("struct_ops reloc: cannot find map at rel.r_offset %zu\n",
6513 (size_t)rel.r_offset);
6514 return -EINVAL;
6515 }
6516
6517 moff = rel.r_offset - map->sec_offset;
6518 shdr_idx = sym.st_shndx;
6519 st_ops = map->st_ops;
6520 pr_debug("struct_ops reloc %s: for %lld value %lld shdr_idx %u rel.r_offset %zu map->sec_offset %zu name %d (\'%s\')\n",
6521 map->name,
6522 (long long)(rel.r_info >> 32),
6523 (long long)sym.st_value,
6524 shdr_idx, (size_t)rel.r_offset,
6525 map->sec_offset, sym.st_name, name);
6526
6527 if (shdr_idx >= SHN_LORESERVE) {
6528 pr_warn("struct_ops reloc %s: rel.r_offset %zu shdr_idx %u unsupported non-static function\n",
6529 map->name, (size_t)rel.r_offset, shdr_idx);
6530 return -LIBBPF_ERRNO__RELOC;
6531 }
6532
6533 member = find_member_by_offset(st_ops->type, moff * 8);
6534 if (!member) {
6535 pr_warn("struct_ops reloc %s: cannot find member at moff %u\n",
6536 map->name, moff);
6537 return -EINVAL;
6538 }
6539 member_idx = member - btf_members(st_ops->type);
6540 name = btf__name_by_offset(btf, member->name_off);
6541
6542 if (!resolve_func_ptr(btf, member->type, NULL)) {
6543 pr_warn("struct_ops reloc %s: cannot relocate non func ptr %s\n",
6544 map->name, name);
6545 return -EINVAL;
6546 }
6547
6548 prog = bpf_object__find_prog_by_idx(obj, shdr_idx);
6549 if (!prog) {
6550 pr_warn("struct_ops reloc %s: cannot find prog at shdr_idx %u to relocate func ptr %s\n",
6551 map->name, shdr_idx, name);
6552 return -EINVAL;
6553 }
6554
6555 if (prog->type == BPF_PROG_TYPE_UNSPEC) {
6556 const struct bpf_sec_def *sec_def;
6557
6558 sec_def = find_sec_def(prog->section_name);
6559 if (sec_def &&
6560 sec_def->prog_type != BPF_PROG_TYPE_STRUCT_OPS) {
6561 /* for pr_warn */
6562 prog->type = sec_def->prog_type;
6563 goto invalid_prog;
6564 }
6565
6566 prog->type = BPF_PROG_TYPE_STRUCT_OPS;
6567 prog->attach_btf_id = st_ops->type_id;
6568 prog->expected_attach_type = member_idx;
6569 } else if (prog->type != BPF_PROG_TYPE_STRUCT_OPS ||
6570 prog->attach_btf_id != st_ops->type_id ||
6571 prog->expected_attach_type != member_idx) {
6572 goto invalid_prog;
6573 }
6574 st_ops->progs[member_idx] = prog;
6575 }
6576
6577 return 0;
6578
6579invalid_prog:
6580 pr_warn("struct_ops reloc %s: cannot use prog %s in sec %s with type %u attach_btf_id %u expected_attach_type %u for func ptr %s\n",
6581 map->name, prog->name, prog->section_name, prog->type,
6582 prog->attach_btf_id, prog->expected_attach_type, name);
6583 return -EINVAL;
6584}
6585
KP Singha6ed02c2020-01-17 22:28:25 +01006586#define BTF_TRACE_PREFIX "btf_trace_"
KP Singh1e092a032020-03-29 01:43:54 +01006587#define BTF_LSM_PREFIX "bpf_lsm_"
KP Singha6ed02c2020-01-17 22:28:25 +01006588#define BTF_MAX_NAME_SIZE 128
6589
6590static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix,
6591 const char *name, __u32 kind)
6592{
6593 char btf_type_name[BTF_MAX_NAME_SIZE];
6594 int ret;
6595
6596 ret = snprintf(btf_type_name, sizeof(btf_type_name),
6597 "%s%s", prefix, name);
6598 /* snprintf returns the number of characters written excluding the
6599 * the terminating null. So, if >= BTF_MAX_NAME_SIZE are written, it
6600 * indicates truncation.
6601 */
6602 if (ret < 0 || ret >= sizeof(btf_type_name))
6603 return -ENAMETOOLONG;
6604 return btf__find_by_name_kind(btf, btf_type_name, kind);
6605}
6606
6607static inline int __find_vmlinux_btf_id(struct btf *btf, const char *name,
6608 enum bpf_attach_type attach_type)
6609{
6610 int err;
6611
6612 if (attach_type == BPF_TRACE_RAW_TP)
6613 err = find_btf_by_prefix_kind(btf, BTF_TRACE_PREFIX, name,
6614 BTF_KIND_TYPEDEF);
KP Singh1e092a032020-03-29 01:43:54 +01006615 else if (attach_type == BPF_LSM_MAC)
6616 err = find_btf_by_prefix_kind(btf, BTF_LSM_PREFIX, name,
6617 BTF_KIND_FUNC);
KP Singha6ed02c2020-01-17 22:28:25 +01006618 else
6619 err = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
6620
Eelco Chaudronff26ce52020-02-20 13:26:35 +00006621 if (err <= 0)
6622 pr_warn("%s is not found in vmlinux BTF\n", name);
6623
KP Singha6ed02c2020-01-17 22:28:25 +01006624 return err;
6625}
6626
Alexei Starovoitovb8c54ea2019-11-14 10:57:06 -08006627int libbpf_find_vmlinux_btf_id(const char *name,
6628 enum bpf_attach_type attach_type)
Alexei Starovoitov12a86542019-10-30 15:32:12 -07006629{
KP Singha6ed02c2020-01-17 22:28:25 +01006630 struct btf *btf;
Alexei Starovoitov12a86542019-10-30 15:32:12 -07006631
KP Singha6ed02c2020-01-17 22:28:25 +01006632 btf = libbpf_find_kernel_btf();
Alexei Starovoitov12a86542019-10-30 15:32:12 -07006633 if (IS_ERR(btf)) {
6634 pr_warn("vmlinux BTF is not found\n");
6635 return -EINVAL;
6636 }
6637
KP Singha6ed02c2020-01-17 22:28:25 +01006638 return __find_vmlinux_btf_id(btf, name, attach_type);
Alexei Starovoitovb8c54ea2019-11-14 10:57:06 -08006639}
6640
Alexei Starovoitove7bf94d2019-11-14 10:57:18 -08006641static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd)
6642{
6643 struct bpf_prog_info_linear *info_linear;
6644 struct bpf_prog_info *info;
6645 struct btf *btf = NULL;
6646 int err = -EINVAL;
6647
6648 info_linear = bpf_program__get_prog_info_linear(attach_prog_fd, 0);
6649 if (IS_ERR_OR_NULL(info_linear)) {
6650 pr_warn("failed get_prog_info_linear for FD %d\n",
6651 attach_prog_fd);
6652 return -EINVAL;
6653 }
6654 info = &info_linear->info;
6655 if (!info->btf_id) {
6656 pr_warn("The target program doesn't have BTF\n");
6657 goto out;
6658 }
6659 if (btf__get_from_id(info->btf_id, &btf)) {
6660 pr_warn("Failed to get BTF of the program\n");
6661 goto out;
6662 }
6663 err = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
6664 btf__free(btf);
6665 if (err <= 0) {
6666 pr_warn("%s is not found in prog's BTF\n", name);
6667 goto out;
6668 }
6669out:
6670 free(info_linear);
6671 return err;
6672}
6673
KP Singha6ed02c2020-01-17 22:28:25 +01006674static int libbpf_find_attach_btf_id(struct bpf_program *prog)
Alexei Starovoitovb8c54ea2019-11-14 10:57:06 -08006675{
KP Singha6ed02c2020-01-17 22:28:25 +01006676 enum bpf_attach_type attach_type = prog->expected_attach_type;
6677 __u32 attach_prog_fd = prog->attach_prog_fd;
6678 const char *name = prog->section_name;
Alexei Starovoitovb8c54ea2019-11-14 10:57:06 -08006679 int i, err;
6680
Alexei Starovoitov12a86542019-10-30 15:32:12 -07006681 if (!name)
Alexei Starovoitovb8c54ea2019-11-14 10:57:06 -08006682 return -EINVAL;
Alexei Starovoitov12a86542019-10-30 15:32:12 -07006683
Andrii Nakryikod7a18ea2019-12-13 17:43:26 -08006684 for (i = 0; i < ARRAY_SIZE(section_defs); i++) {
6685 if (!section_defs[i].is_attach_btf)
Alexei Starovoitov12a86542019-10-30 15:32:12 -07006686 continue;
Andrii Nakryikod7a18ea2019-12-13 17:43:26 -08006687 if (strncmp(name, section_defs[i].sec, section_defs[i].len))
Alexei Starovoitov12a86542019-10-30 15:32:12 -07006688 continue;
Alexei Starovoitove7bf94d2019-11-14 10:57:18 -08006689 if (attach_prog_fd)
Andrii Nakryikod7a18ea2019-12-13 17:43:26 -08006690 err = libbpf_find_prog_btf_id(name + section_defs[i].len,
Alexei Starovoitove7bf94d2019-11-14 10:57:18 -08006691 attach_prog_fd);
6692 else
KP Singha6ed02c2020-01-17 22:28:25 +01006693 err = __find_vmlinux_btf_id(prog->obj->btf_vmlinux,
6694 name + section_defs[i].len,
6695 attach_type);
Alexei Starovoitovb8c54ea2019-11-14 10:57:06 -08006696 return err;
Alexei Starovoitov12a86542019-10-30 15:32:12 -07006697 }
6698 pr_warn("failed to identify btf_id based on ELF section name '%s'\n", name);
Alexei Starovoitovb8c54ea2019-11-14 10:57:06 -08006699 return -ESRCH;
Alexei Starovoitov12a86542019-10-30 15:32:12 -07006700}
6701
Andrey Ignatov956b6202018-09-26 15:24:53 -07006702int libbpf_attach_type_by_name(const char *name,
6703 enum bpf_attach_type *attach_type)
6704{
Taeung Songc76e4c22019-01-21 22:06:38 +09006705 char *type_names;
Andrey Ignatov956b6202018-09-26 15:24:53 -07006706 int i;
6707
6708 if (!name)
6709 return -EINVAL;
6710
Andrii Nakryikod7a18ea2019-12-13 17:43:26 -08006711 for (i = 0; i < ARRAY_SIZE(section_defs); i++) {
6712 if (strncmp(name, section_defs[i].sec, section_defs[i].len))
Andrey Ignatov956b6202018-09-26 15:24:53 -07006713 continue;
Andrii Nakryikod7a18ea2019-12-13 17:43:26 -08006714 if (!section_defs[i].is_attachable)
Andrey Ignatov956b6202018-09-26 15:24:53 -07006715 return -EINVAL;
Andrii Nakryikod7a18ea2019-12-13 17:43:26 -08006716 *attach_type = section_defs[i].attach_type;
Andrey Ignatov956b6202018-09-26 15:24:53 -07006717 return 0;
6718 }
Andrii Nakryiko4a3d6c62019-12-17 15:42:28 -08006719 pr_debug("failed to guess attach type based on ELF section name '%s'\n", name);
Taeung Songc76e4c22019-01-21 22:06:38 +09006720 type_names = libbpf_get_type_names(true);
6721 if (type_names != NULL) {
Andrii Nakryiko4a3d6c62019-12-17 15:42:28 -08006722 pr_debug("attachable section(type) names are:%s\n", type_names);
Taeung Songc76e4c22019-01-21 22:06:38 +09006723 free(type_names);
6724 }
6725
Andrey Ignatov956b6202018-09-26 15:24:53 -07006726 return -EINVAL;
6727}
6728
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07006729int bpf_map__fd(const struct bpf_map *map)
Wang Nan9d759a92015-11-27 08:47:35 +00006730{
Arnaldo Carvalho de Melo6e009e652016-06-03 12:15:52 -03006731 return map ? map->fd : -EINVAL;
Wang Nan9d759a92015-11-27 08:47:35 +00006732}
6733
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07006734const struct bpf_map_def *bpf_map__def(const struct bpf_map *map)
Wang Nan9d759a92015-11-27 08:47:35 +00006735{
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03006736 return map ? &map->def : ERR_PTR(-EINVAL);
Wang Nan9d759a92015-11-27 08:47:35 +00006737}
6738
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07006739const char *bpf_map__name(const struct bpf_map *map)
Wang Nan561bbcc2015-11-27 08:47:36 +00006740{
Arnaldo Carvalho de Melo009ad5d2016-06-02 11:02:05 -03006741 return map ? map->name : NULL;
Wang Nan561bbcc2015-11-27 08:47:36 +00006742}
6743
Martin KaFai Lau5b891af2018-07-24 08:40:21 -07006744__u32 bpf_map__btf_key_type_id(const struct bpf_map *map)
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07006745{
Martin KaFai Lau61746dbe2018-05-22 15:04:24 -07006746 return map ? map->btf_key_type_id : 0;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07006747}
6748
Martin KaFai Lau5b891af2018-07-24 08:40:21 -07006749__u32 bpf_map__btf_value_type_id(const struct bpf_map *map)
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07006750{
Martin KaFai Lau61746dbe2018-05-22 15:04:24 -07006751 return map ? map->btf_value_type_id : 0;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07006752}
6753
Arnaldo Carvalho de Meloedb13ed2016-06-03 12:38:21 -03006754int bpf_map__set_priv(struct bpf_map *map, void *priv,
6755 bpf_map_clear_priv_t clear_priv)
Wang Nan9d759a92015-11-27 08:47:35 +00006756{
6757 if (!map)
6758 return -EINVAL;
6759
6760 if (map->priv) {
6761 if (map->clear_priv)
6762 map->clear_priv(map, map->priv);
6763 }
6764
6765 map->priv = priv;
6766 map->clear_priv = clear_priv;
6767 return 0;
6768}
6769
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07006770void *bpf_map__priv(const struct bpf_map *map)
Wang Nan9d759a92015-11-27 08:47:35 +00006771{
Arnaldo Carvalho de Melob4cbfa52016-06-02 10:51:59 -03006772 return map ? map->priv : ERR_PTR(-EINVAL);
Wang Nan9d759a92015-11-27 08:47:35 +00006773}
6774
Toke Høiland-Jørgensene2842be2020-03-29 15:22:52 +02006775int bpf_map__set_initial_value(struct bpf_map *map,
6776 const void *data, size_t size)
6777{
6778 if (!map->mmaped || map->libbpf_type == LIBBPF_MAP_KCONFIG ||
6779 size != map->def.value_size || map->fd >= 0)
6780 return -EINVAL;
6781
6782 memcpy(map->mmaped, data, size);
6783 return 0;
6784}
6785
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07006786bool bpf_map__is_offload_neutral(const struct bpf_map *map)
Jakub Kicinskif83fb222018-07-10 14:43:01 -07006787{
6788 return map->def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
6789}
6790
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07006791bool bpf_map__is_internal(const struct bpf_map *map)
Daniel Borkmannd8599002019-04-09 23:20:13 +02006792{
6793 return map->libbpf_type != LIBBPF_MAP_UNSPEC;
6794}
6795
Jakub Kicinski9aba3612018-06-28 14:41:37 -07006796void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex)
6797{
6798 map->map_ifindex = ifindex;
6799}
6800
Nikita V. Shirokovaddb9fc2018-11-20 20:55:56 -08006801int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd)
6802{
6803 if (!bpf_map_type__is_map_in_map(map->def.type)) {
Kefeng Wangbe180102019-10-21 13:55:32 +08006804 pr_warn("error: unsupported map type\n");
Nikita V. Shirokovaddb9fc2018-11-20 20:55:56 -08006805 return -EINVAL;
6806 }
6807 if (map->inner_map_fd != -1) {
Kefeng Wangbe180102019-10-21 13:55:32 +08006808 pr_warn("error: inner_map_fd already specified\n");
Nikita V. Shirokovaddb9fc2018-11-20 20:55:56 -08006809 return -EINVAL;
6810 }
6811 map->inner_map_fd = fd;
6812 return 0;
6813}
6814
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08006815static struct bpf_map *
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07006816__bpf_map__iter(const struct bpf_map *m, const struct bpf_object *obj, int i)
Wang Nan9d759a92015-11-27 08:47:35 +00006817{
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08006818 ssize_t idx;
Wang Nan9d759a92015-11-27 08:47:35 +00006819 struct bpf_map *s, *e;
6820
6821 if (!obj || !obj->maps)
6822 return NULL;
6823
6824 s = obj->maps;
6825 e = obj->maps + obj->nr_maps;
6826
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08006827 if ((m < s) || (m >= e)) {
Kefeng Wangbe180102019-10-21 13:55:32 +08006828 pr_warn("error in %s: map handler doesn't belong to object\n",
6829 __func__);
Wang Nan9d759a92015-11-27 08:47:35 +00006830 return NULL;
6831 }
6832
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08006833 idx = (m - obj->maps) + i;
6834 if (idx >= obj->nr_maps || idx < 0)
Wang Nan9d759a92015-11-27 08:47:35 +00006835 return NULL;
6836 return &obj->maps[idx];
6837}
Wang Nan561bbcc2015-11-27 08:47:36 +00006838
6839struct bpf_map *
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07006840bpf_map__next(const struct bpf_map *prev, const struct bpf_object *obj)
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08006841{
6842 if (prev == NULL)
6843 return obj->maps;
6844
6845 return __bpf_map__iter(prev, obj, 1);
6846}
6847
6848struct bpf_map *
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07006849bpf_map__prev(const struct bpf_map *next, const struct bpf_object *obj)
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08006850{
6851 if (next == NULL) {
6852 if (!obj->nr_maps)
6853 return NULL;
6854 return obj->maps + obj->nr_maps - 1;
6855 }
6856
6857 return __bpf_map__iter(next, obj, -1);
6858}
6859
6860struct bpf_map *
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07006861bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name)
Wang Nan561bbcc2015-11-27 08:47:36 +00006862{
6863 struct bpf_map *pos;
6864
Jakub Kicinskif74a53d92019-02-27 19:04:12 -08006865 bpf_object__for_each_map(pos, obj) {
Wang Nan973170e2015-12-08 02:25:29 +00006866 if (pos->name && !strcmp(pos->name, name))
Wang Nan561bbcc2015-11-27 08:47:36 +00006867 return pos;
6868 }
6869 return NULL;
6870}
Wang Nan5a6acad2016-11-26 07:03:27 +00006871
Maciej Fijalkowskif3cea322019-02-01 22:42:23 +01006872int
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07006873bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name)
Maciej Fijalkowskif3cea322019-02-01 22:42:23 +01006874{
6875 return bpf_map__fd(bpf_object__find_map_by_name(obj, name));
6876}
6877
Wang Nan5a6acad2016-11-26 07:03:27 +00006878struct bpf_map *
6879bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
6880{
Andrii Nakryikodb488142019-06-17 12:26:54 -07006881 return ERR_PTR(-ENOTSUP);
Wang Nan5a6acad2016-11-26 07:03:27 +00006882}
Joe Stringere28ff1a2017-01-22 17:11:25 -08006883
6884long libbpf_get_error(const void *ptr)
6885{
Hariprasad Kelamd98363b2019-05-25 14:32:57 +05306886 return PTR_ERR_OR_ZERO(ptr);
Joe Stringere28ff1a2017-01-22 17:11:25 -08006887}
John Fastabend6f6d33f2017-08-15 22:34:22 -07006888
6889int bpf_prog_load(const char *file, enum bpf_prog_type type,
6890 struct bpf_object **pobj, int *prog_fd)
6891{
Andrey Ignatovd7be1432018-03-30 15:08:01 -07006892 struct bpf_prog_load_attr attr;
6893
6894 memset(&attr, 0, sizeof(struct bpf_prog_load_attr));
6895 attr.file = file;
6896 attr.prog_type = type;
6897 attr.expected_attach_type = 0;
6898
6899 return bpf_prog_load_xattr(&attr, pobj, prog_fd);
6900}
6901
6902int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
6903 struct bpf_object **pobj, int *prog_fd)
6904{
Leo Yan33bae182019-07-02 18:25:31 +08006905 struct bpf_object_open_attr open_attr = {};
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08006906 struct bpf_program *prog, *first_prog = NULL;
John Fastabend6f6d33f2017-08-15 22:34:22 -07006907 struct bpf_object *obj;
David Beckettf0307a72018-05-16 14:02:49 -07006908 struct bpf_map *map;
John Fastabend6f6d33f2017-08-15 22:34:22 -07006909 int err;
6910
Andrey Ignatovd7be1432018-03-30 15:08:01 -07006911 if (!attr)
6912 return -EINVAL;
Jakub Kicinski17387dd2018-05-10 10:24:42 -07006913 if (!attr->file)
6914 return -EINVAL;
Andrey Ignatovd7be1432018-03-30 15:08:01 -07006915
Leo Yan33bae182019-07-02 18:25:31 +08006916 open_attr.file = attr->file;
6917 open_attr.prog_type = attr->prog_type;
6918
Jakub Kicinski07f2d4e2018-07-10 14:43:02 -07006919 obj = bpf_object__open_xattr(&open_attr);
Jakub Kicinski35976832018-05-10 10:09:34 -07006920 if (IS_ERR_OR_NULL(obj))
John Fastabend6f6d33f2017-08-15 22:34:22 -07006921 return -ENOENT;
6922
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08006923 bpf_object__for_each_program(prog, obj) {
Andrii Nakryikodd4436b2019-10-20 20:38:59 -07006924 enum bpf_attach_type attach_type = attr->expected_attach_type;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08006925 /*
Andrii Nakryikodd4436b2019-10-20 20:38:59 -07006926 * to preserve backwards compatibility, bpf_prog_load treats
6927 * attr->prog_type, if specified, as an override to whatever
6928 * bpf_object__open guessed
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08006929 */
Andrii Nakryikodd4436b2019-10-20 20:38:59 -07006930 if (attr->prog_type != BPF_PROG_TYPE_UNSPEC) {
6931 bpf_program__set_type(prog, attr->prog_type);
6932 bpf_program__set_expected_attach_type(prog,
6933 attach_type);
6934 }
6935 if (bpf_program__get_type(prog) == BPF_PROG_TYPE_UNSPEC) {
6936 /*
6937 * we haven't guessed from section name and user
6938 * didn't provide a fallback type, too bad...
6939 */
6940 bpf_object__close(obj);
6941 return -EINVAL;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08006942 }
6943
Andrii Nakryikodd4436b2019-10-20 20:38:59 -07006944 prog->prog_ifindex = attr->ifindex;
Alexei Starovoitovda11b412019-04-01 21:27:47 -07006945 prog->log_level = attr->log_level;
Jiong Wang04656192019-05-24 23:25:19 +01006946 prog->prog_flags = attr->prog_flags;
Taeung Song69495d22018-09-03 08:30:07 +09006947 if (!first_prog)
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08006948 first_prog = prog;
6949 }
6950
Jakub Kicinskif74a53d92019-02-27 19:04:12 -08006951 bpf_object__for_each_map(map, obj) {
Jakub Kicinskif83fb222018-07-10 14:43:01 -07006952 if (!bpf_map__is_offload_neutral(map))
6953 map->map_ifindex = attr->ifindex;
David Beckettf0307a72018-05-16 14:02:49 -07006954 }
6955
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08006956 if (!first_prog) {
Kefeng Wangbe180102019-10-21 13:55:32 +08006957 pr_warn("object file doesn't contain bpf program\n");
John Fastabend6f6d33f2017-08-15 22:34:22 -07006958 bpf_object__close(obj);
6959 return -ENOENT;
6960 }
6961
John Fastabend6f6d33f2017-08-15 22:34:22 -07006962 err = bpf_object__load(obj);
6963 if (err) {
6964 bpf_object__close(obj);
6965 return -EINVAL;
6966 }
6967
6968 *pobj = obj;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08006969 *prog_fd = bpf_program__fd(first_prog);
John Fastabend6f6d33f2017-08-15 22:34:22 -07006970 return 0;
6971}
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07006972
Andrii Nakryiko1c2e9ef2019-07-01 16:58:56 -07006973struct bpf_link {
Andrii Nakryikod6958702019-12-18 14:50:39 -08006974 int (*detach)(struct bpf_link *link);
Andrii Nakryiko1c2e9ef2019-07-01 16:58:56 -07006975 int (*destroy)(struct bpf_link *link);
Andrii Nakryikoc016b682020-03-02 20:31:58 -08006976 char *pin_path; /* NULL, if not pinned */
6977 int fd; /* hook FD, -1 if not applicable */
Andrii Nakryikod6958702019-12-18 14:50:39 -08006978 bool disconnected;
Andrii Nakryiko1c2e9ef2019-07-01 16:58:56 -07006979};
6980
Andrii Nakryikocc4f8642020-03-29 20:00:00 -07006981/* Replace link's underlying BPF program with the new one */
6982int bpf_link__update_program(struct bpf_link *link, struct bpf_program *prog)
6983{
6984 return bpf_link_update(bpf_link__fd(link), bpf_program__fd(prog), NULL);
6985}
6986
Andrii Nakryikod6958702019-12-18 14:50:39 -08006987/* Release "ownership" of underlying BPF resource (typically, BPF program
6988 * attached to some BPF hook, e.g., tracepoint, kprobe, etc). Disconnected
6989 * link, when destructed through bpf_link__destroy() call won't attempt to
6990 * detach/unregisted that BPF resource. This is useful in situations where,
6991 * say, attached BPF program has to outlive userspace program that attached it
6992 * in the system. Depending on type of BPF program, though, there might be
6993 * additional steps (like pinning BPF program in BPF FS) necessary to ensure
6994 * exit of userspace program doesn't trigger automatic detachment and clean up
6995 * inside the kernel.
6996 */
6997void bpf_link__disconnect(struct bpf_link *link)
6998{
6999 link->disconnected = true;
7000}
7001
Andrii Nakryiko1c2e9ef2019-07-01 16:58:56 -07007002int bpf_link__destroy(struct bpf_link *link)
7003{
Andrii Nakryikod6958702019-12-18 14:50:39 -08007004 int err = 0;
Andrii Nakryiko1c2e9ef2019-07-01 16:58:56 -07007005
7006 if (!link)
7007 return 0;
7008
Andrii Nakryikod6958702019-12-18 14:50:39 -08007009 if (!link->disconnected && link->detach)
7010 err = link->detach(link);
7011 if (link->destroy)
7012 link->destroy(link);
Andrii Nakryikoc016b682020-03-02 20:31:58 -08007013 if (link->pin_path)
7014 free(link->pin_path);
Andrii Nakryiko1c2e9ef2019-07-01 16:58:56 -07007015 free(link);
7016
7017 return err;
7018}
7019
Andrii Nakryikoc016b682020-03-02 20:31:58 -08007020int bpf_link__fd(const struct bpf_link *link)
7021{
7022 return link->fd;
7023}
7024
7025const char *bpf_link__pin_path(const struct bpf_link *link)
7026{
7027 return link->pin_path;
7028}
7029
7030static int bpf_link__detach_fd(struct bpf_link *link)
7031{
7032 return close(link->fd);
7033}
7034
7035struct bpf_link *bpf_link__open(const char *path)
7036{
7037 struct bpf_link *link;
7038 int fd;
7039
7040 fd = bpf_obj_get(path);
7041 if (fd < 0) {
7042 fd = -errno;
7043 pr_warn("failed to open link at %s: %d\n", path, fd);
7044 return ERR_PTR(fd);
7045 }
7046
7047 link = calloc(1, sizeof(*link));
7048 if (!link) {
7049 close(fd);
7050 return ERR_PTR(-ENOMEM);
7051 }
7052 link->detach = &bpf_link__detach_fd;
7053 link->fd = fd;
7054
7055 link->pin_path = strdup(path);
7056 if (!link->pin_path) {
7057 bpf_link__destroy(link);
7058 return ERR_PTR(-ENOMEM);
7059 }
7060
7061 return link;
7062}
7063
7064int bpf_link__pin(struct bpf_link *link, const char *path)
7065{
7066 int err;
7067
7068 if (link->pin_path)
7069 return -EBUSY;
7070 err = make_parent_dir(path);
7071 if (err)
7072 return err;
7073 err = check_path(path);
7074 if (err)
7075 return err;
7076
7077 link->pin_path = strdup(path);
7078 if (!link->pin_path)
7079 return -ENOMEM;
7080
7081 if (bpf_obj_pin(link->fd, link->pin_path)) {
7082 err = -errno;
7083 zfree(&link->pin_path);
7084 return err;
7085 }
7086
7087 pr_debug("link fd=%d: pinned at %s\n", link->fd, link->pin_path);
7088 return 0;
7089}
7090
7091int bpf_link__unpin(struct bpf_link *link)
7092{
7093 int err;
7094
7095 if (!link->pin_path)
7096 return -EINVAL;
7097
7098 err = unlink(link->pin_path);
7099 if (err != 0)
7100 return -errno;
7101
7102 pr_debug("link fd=%d: unpinned from %s\n", link->fd, link->pin_path);
7103 zfree(&link->pin_path);
7104 return 0;
7105}
Andrii Nakryiko63f2f5e2019-07-01 16:58:57 -07007106
Andrii Nakryikod6958702019-12-18 14:50:39 -08007107static int bpf_link__detach_perf_event(struct bpf_link *link)
Andrii Nakryiko63f2f5e2019-07-01 16:58:57 -07007108{
Andrii Nakryiko63f2f5e2019-07-01 16:58:57 -07007109 int err;
7110
Andrii Nakryikoc016b682020-03-02 20:31:58 -08007111 err = ioctl(link->fd, PERF_EVENT_IOC_DISABLE, 0);
Andrii Nakryiko63f2f5e2019-07-01 16:58:57 -07007112 if (err)
7113 err = -errno;
7114
Andrii Nakryikoc016b682020-03-02 20:31:58 -08007115 close(link->fd);
Andrii Nakryiko63f2f5e2019-07-01 16:58:57 -07007116 return err;
7117}
7118
7119struct bpf_link *bpf_program__attach_perf_event(struct bpf_program *prog,
7120 int pfd)
7121{
7122 char errmsg[STRERR_BUFSIZE];
Andrii Nakryikoc016b682020-03-02 20:31:58 -08007123 struct bpf_link *link;
Andrii Nakryiko63f2f5e2019-07-01 16:58:57 -07007124 int prog_fd, err;
7125
7126 if (pfd < 0) {
Kefeng Wangbe180102019-10-21 13:55:32 +08007127 pr_warn("program '%s': invalid perf event FD %d\n",
7128 bpf_program__title(prog, false), pfd);
Andrii Nakryiko63f2f5e2019-07-01 16:58:57 -07007129 return ERR_PTR(-EINVAL);
7130 }
7131 prog_fd = bpf_program__fd(prog);
7132 if (prog_fd < 0) {
Kefeng Wangbe180102019-10-21 13:55:32 +08007133 pr_warn("program '%s': can't attach BPF program w/o FD (did you load it?)\n",
7134 bpf_program__title(prog, false));
Andrii Nakryiko63f2f5e2019-07-01 16:58:57 -07007135 return ERR_PTR(-EINVAL);
7136 }
7137
Andrii Nakryikod6958702019-12-18 14:50:39 -08007138 link = calloc(1, sizeof(*link));
Andrii Nakryiko63f2f5e2019-07-01 16:58:57 -07007139 if (!link)
7140 return ERR_PTR(-ENOMEM);
Andrii Nakryikoc016b682020-03-02 20:31:58 -08007141 link->detach = &bpf_link__detach_perf_event;
Andrii Nakryiko63f2f5e2019-07-01 16:58:57 -07007142 link->fd = pfd;
7143
7144 if (ioctl(pfd, PERF_EVENT_IOC_SET_BPF, prog_fd) < 0) {
7145 err = -errno;
7146 free(link);
Kefeng Wangbe180102019-10-21 13:55:32 +08007147 pr_warn("program '%s': failed to attach to pfd %d: %s\n",
7148 bpf_program__title(prog, false), pfd,
Andrii Nakryiko63f2f5e2019-07-01 16:58:57 -07007149 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
7150 return ERR_PTR(err);
7151 }
7152 if (ioctl(pfd, PERF_EVENT_IOC_ENABLE, 0) < 0) {
7153 err = -errno;
7154 free(link);
Kefeng Wangbe180102019-10-21 13:55:32 +08007155 pr_warn("program '%s': failed to enable pfd %d: %s\n",
7156 bpf_program__title(prog, false), pfd,
Andrii Nakryiko63f2f5e2019-07-01 16:58:57 -07007157 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
7158 return ERR_PTR(err);
7159 }
Andrii Nakryikoc016b682020-03-02 20:31:58 -08007160 return link;
Andrii Nakryiko63f2f5e2019-07-01 16:58:57 -07007161}
7162
Andrii Nakryikob2650022019-07-01 16:58:58 -07007163/*
7164 * this function is expected to parse integer in the range of [0, 2^31-1] from
7165 * given file using scanf format string fmt. If actual parsed value is
7166 * negative, the result might be indistinguishable from error
7167 */
7168static int parse_uint_from_file(const char *file, const char *fmt)
7169{
7170 char buf[STRERR_BUFSIZE];
7171 int err, ret;
7172 FILE *f;
7173
7174 f = fopen(file, "r");
7175 if (!f) {
7176 err = -errno;
7177 pr_debug("failed to open '%s': %s\n", file,
7178 libbpf_strerror_r(err, buf, sizeof(buf)));
7179 return err;
7180 }
7181 err = fscanf(f, fmt, &ret);
7182 if (err != 1) {
7183 err = err == EOF ? -EIO : -errno;
7184 pr_debug("failed to parse '%s': %s\n", file,
7185 libbpf_strerror_r(err, buf, sizeof(buf)));
7186 fclose(f);
7187 return err;
7188 }
7189 fclose(f);
7190 return ret;
7191}
7192
7193static int determine_kprobe_perf_type(void)
7194{
7195 const char *file = "/sys/bus/event_source/devices/kprobe/type";
7196
7197 return parse_uint_from_file(file, "%d\n");
7198}
7199
7200static int determine_uprobe_perf_type(void)
7201{
7202 const char *file = "/sys/bus/event_source/devices/uprobe/type";
7203
7204 return parse_uint_from_file(file, "%d\n");
7205}
7206
7207static int determine_kprobe_retprobe_bit(void)
7208{
7209 const char *file = "/sys/bus/event_source/devices/kprobe/format/retprobe";
7210
7211 return parse_uint_from_file(file, "config:%d\n");
7212}
7213
7214static int determine_uprobe_retprobe_bit(void)
7215{
7216 const char *file = "/sys/bus/event_source/devices/uprobe/format/retprobe";
7217
7218 return parse_uint_from_file(file, "config:%d\n");
7219}
7220
7221static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
7222 uint64_t offset, int pid)
7223{
7224 struct perf_event_attr attr = {};
7225 char errmsg[STRERR_BUFSIZE];
7226 int type, pfd, err;
7227
7228 type = uprobe ? determine_uprobe_perf_type()
7229 : determine_kprobe_perf_type();
7230 if (type < 0) {
Kefeng Wangbe180102019-10-21 13:55:32 +08007231 pr_warn("failed to determine %s perf type: %s\n",
7232 uprobe ? "uprobe" : "kprobe",
7233 libbpf_strerror_r(type, errmsg, sizeof(errmsg)));
Andrii Nakryikob2650022019-07-01 16:58:58 -07007234 return type;
7235 }
7236 if (retprobe) {
7237 int bit = uprobe ? determine_uprobe_retprobe_bit()
7238 : determine_kprobe_retprobe_bit();
7239
7240 if (bit < 0) {
Kefeng Wangbe180102019-10-21 13:55:32 +08007241 pr_warn("failed to determine %s retprobe bit: %s\n",
7242 uprobe ? "uprobe" : "kprobe",
7243 libbpf_strerror_r(bit, errmsg, sizeof(errmsg)));
Andrii Nakryikob2650022019-07-01 16:58:58 -07007244 return bit;
7245 }
7246 attr.config |= 1 << bit;
7247 }
7248 attr.size = sizeof(attr);
7249 attr.type = type;
Andrii Nakryiko36db2a92019-07-08 21:00:07 -07007250 attr.config1 = ptr_to_u64(name); /* kprobe_func or uprobe_path */
7251 attr.config2 = offset; /* kprobe_addr or probe_offset */
Andrii Nakryikob2650022019-07-01 16:58:58 -07007252
7253 /* pid filter is meaningful only for uprobes */
7254 pfd = syscall(__NR_perf_event_open, &attr,
7255 pid < 0 ? -1 : pid /* pid */,
7256 pid == -1 ? 0 : -1 /* cpu */,
7257 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
7258 if (pfd < 0) {
7259 err = -errno;
Kefeng Wangbe180102019-10-21 13:55:32 +08007260 pr_warn("%s perf_event_open() failed: %s\n",
7261 uprobe ? "uprobe" : "kprobe",
7262 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
Andrii Nakryikob2650022019-07-01 16:58:58 -07007263 return err;
7264 }
7265 return pfd;
7266}
7267
7268struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog,
7269 bool retprobe,
7270 const char *func_name)
7271{
7272 char errmsg[STRERR_BUFSIZE];
7273 struct bpf_link *link;
7274 int pfd, err;
7275
7276 pfd = perf_event_open_probe(false /* uprobe */, retprobe, func_name,
7277 0 /* offset */, -1 /* pid */);
7278 if (pfd < 0) {
Kefeng Wangbe180102019-10-21 13:55:32 +08007279 pr_warn("program '%s': failed to create %s '%s' perf event: %s\n",
7280 bpf_program__title(prog, false),
7281 retprobe ? "kretprobe" : "kprobe", func_name,
7282 libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
Andrii Nakryikob2650022019-07-01 16:58:58 -07007283 return ERR_PTR(pfd);
7284 }
7285 link = bpf_program__attach_perf_event(prog, pfd);
7286 if (IS_ERR(link)) {
7287 close(pfd);
7288 err = PTR_ERR(link);
Kefeng Wangbe180102019-10-21 13:55:32 +08007289 pr_warn("program '%s': failed to attach to %s '%s': %s\n",
7290 bpf_program__title(prog, false),
7291 retprobe ? "kretprobe" : "kprobe", func_name,
7292 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
Andrii Nakryikob2650022019-07-01 16:58:58 -07007293 return link;
7294 }
7295 return link;
7296}
7297
Andrii Nakryikod7a18ea2019-12-13 17:43:26 -08007298static struct bpf_link *attach_kprobe(const struct bpf_sec_def *sec,
7299 struct bpf_program *prog)
7300{
7301 const char *func_name;
7302 bool retprobe;
7303
7304 func_name = bpf_program__title(prog, false) + sec->len;
7305 retprobe = strcmp(sec->sec, "kretprobe/") == 0;
7306
7307 return bpf_program__attach_kprobe(prog, retprobe, func_name);
7308}
7309
Andrii Nakryikob2650022019-07-01 16:58:58 -07007310struct bpf_link *bpf_program__attach_uprobe(struct bpf_program *prog,
7311 bool retprobe, pid_t pid,
7312 const char *binary_path,
7313 size_t func_offset)
7314{
7315 char errmsg[STRERR_BUFSIZE];
7316 struct bpf_link *link;
7317 int pfd, err;
7318
7319 pfd = perf_event_open_probe(true /* uprobe */, retprobe,
7320 binary_path, func_offset, pid);
7321 if (pfd < 0) {
Kefeng Wangbe180102019-10-21 13:55:32 +08007322 pr_warn("program '%s': failed to create %s '%s:0x%zx' perf event: %s\n",
7323 bpf_program__title(prog, false),
7324 retprobe ? "uretprobe" : "uprobe",
7325 binary_path, func_offset,
7326 libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
Andrii Nakryikob2650022019-07-01 16:58:58 -07007327 return ERR_PTR(pfd);
7328 }
7329 link = bpf_program__attach_perf_event(prog, pfd);
7330 if (IS_ERR(link)) {
7331 close(pfd);
7332 err = PTR_ERR(link);
Kefeng Wangbe180102019-10-21 13:55:32 +08007333 pr_warn("program '%s': failed to attach to %s '%s:0x%zx': %s\n",
7334 bpf_program__title(prog, false),
7335 retprobe ? "uretprobe" : "uprobe",
7336 binary_path, func_offset,
7337 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
Andrii Nakryikob2650022019-07-01 16:58:58 -07007338 return link;
7339 }
7340 return link;
7341}
7342
Andrii Nakryikof6de59c2019-07-01 16:58:59 -07007343static int determine_tracepoint_id(const char *tp_category,
7344 const char *tp_name)
7345{
7346 char file[PATH_MAX];
7347 int ret;
7348
7349 ret = snprintf(file, sizeof(file),
7350 "/sys/kernel/debug/tracing/events/%s/%s/id",
7351 tp_category, tp_name);
7352 if (ret < 0)
7353 return -errno;
7354 if (ret >= sizeof(file)) {
7355 pr_debug("tracepoint %s/%s path is too long\n",
7356 tp_category, tp_name);
7357 return -E2BIG;
7358 }
7359 return parse_uint_from_file(file, "%d\n");
7360}
7361
7362static int perf_event_open_tracepoint(const char *tp_category,
7363 const char *tp_name)
7364{
7365 struct perf_event_attr attr = {};
7366 char errmsg[STRERR_BUFSIZE];
7367 int tp_id, pfd, err;
7368
7369 tp_id = determine_tracepoint_id(tp_category, tp_name);
7370 if (tp_id < 0) {
Kefeng Wangbe180102019-10-21 13:55:32 +08007371 pr_warn("failed to determine tracepoint '%s/%s' perf event ID: %s\n",
7372 tp_category, tp_name,
7373 libbpf_strerror_r(tp_id, errmsg, sizeof(errmsg)));
Andrii Nakryikof6de59c2019-07-01 16:58:59 -07007374 return tp_id;
7375 }
7376
7377 attr.type = PERF_TYPE_TRACEPOINT;
7378 attr.size = sizeof(attr);
7379 attr.config = tp_id;
7380
7381 pfd = syscall(__NR_perf_event_open, &attr, -1 /* pid */, 0 /* cpu */,
7382 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
7383 if (pfd < 0) {
7384 err = -errno;
Kefeng Wangbe180102019-10-21 13:55:32 +08007385 pr_warn("tracepoint '%s/%s' perf_event_open() failed: %s\n",
7386 tp_category, tp_name,
7387 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
Andrii Nakryikof6de59c2019-07-01 16:58:59 -07007388 return err;
7389 }
7390 return pfd;
7391}
7392
7393struct bpf_link *bpf_program__attach_tracepoint(struct bpf_program *prog,
7394 const char *tp_category,
7395 const char *tp_name)
7396{
7397 char errmsg[STRERR_BUFSIZE];
7398 struct bpf_link *link;
7399 int pfd, err;
7400
7401 pfd = perf_event_open_tracepoint(tp_category, tp_name);
7402 if (pfd < 0) {
Kefeng Wangbe180102019-10-21 13:55:32 +08007403 pr_warn("program '%s': failed to create tracepoint '%s/%s' perf event: %s\n",
7404 bpf_program__title(prog, false),
7405 tp_category, tp_name,
7406 libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
Andrii Nakryikof6de59c2019-07-01 16:58:59 -07007407 return ERR_PTR(pfd);
7408 }
7409 link = bpf_program__attach_perf_event(prog, pfd);
7410 if (IS_ERR(link)) {
7411 close(pfd);
7412 err = PTR_ERR(link);
Kefeng Wangbe180102019-10-21 13:55:32 +08007413 pr_warn("program '%s': failed to attach to tracepoint '%s/%s': %s\n",
7414 bpf_program__title(prog, false),
7415 tp_category, tp_name,
7416 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
Andrii Nakryikof6de59c2019-07-01 16:58:59 -07007417 return link;
7418 }
7419 return link;
7420}
7421
Andrii Nakryikod7a18ea2019-12-13 17:43:26 -08007422static struct bpf_link *attach_tp(const struct bpf_sec_def *sec,
7423 struct bpf_program *prog)
7424{
7425 char *sec_name, *tp_cat, *tp_name;
7426 struct bpf_link *link;
7427
7428 sec_name = strdup(bpf_program__title(prog, false));
7429 if (!sec_name)
7430 return ERR_PTR(-ENOMEM);
7431
7432 /* extract "tp/<category>/<name>" */
7433 tp_cat = sec_name + sec->len;
7434 tp_name = strchr(tp_cat, '/');
7435 if (!tp_name) {
7436 link = ERR_PTR(-EINVAL);
7437 goto out;
7438 }
7439 *tp_name = '\0';
7440 tp_name++;
7441
7442 link = bpf_program__attach_tracepoint(prog, tp_cat, tp_name);
7443out:
7444 free(sec_name);
7445 return link;
7446}
7447
Andrii Nakryiko84bf5e12019-07-01 16:59:00 -07007448struct bpf_link *bpf_program__attach_raw_tracepoint(struct bpf_program *prog,
7449 const char *tp_name)
7450{
7451 char errmsg[STRERR_BUFSIZE];
Andrii Nakryikoc016b682020-03-02 20:31:58 -08007452 struct bpf_link *link;
Andrii Nakryiko84bf5e12019-07-01 16:59:00 -07007453 int prog_fd, pfd;
7454
7455 prog_fd = bpf_program__fd(prog);
7456 if (prog_fd < 0) {
Kefeng Wangbe180102019-10-21 13:55:32 +08007457 pr_warn("program '%s': can't attach before loaded\n",
7458 bpf_program__title(prog, false));
Andrii Nakryiko84bf5e12019-07-01 16:59:00 -07007459 return ERR_PTR(-EINVAL);
7460 }
7461
Andrii Nakryikod6958702019-12-18 14:50:39 -08007462 link = calloc(1, sizeof(*link));
Andrii Nakryiko84bf5e12019-07-01 16:59:00 -07007463 if (!link)
7464 return ERR_PTR(-ENOMEM);
Andrii Nakryikoc016b682020-03-02 20:31:58 -08007465 link->detach = &bpf_link__detach_fd;
Andrii Nakryiko84bf5e12019-07-01 16:59:00 -07007466
7467 pfd = bpf_raw_tracepoint_open(tp_name, prog_fd);
7468 if (pfd < 0) {
7469 pfd = -errno;
7470 free(link);
Kefeng Wangbe180102019-10-21 13:55:32 +08007471 pr_warn("program '%s': failed to attach to raw tracepoint '%s': %s\n",
7472 bpf_program__title(prog, false), tp_name,
7473 libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
Andrii Nakryiko84bf5e12019-07-01 16:59:00 -07007474 return ERR_PTR(pfd);
7475 }
7476 link->fd = pfd;
Andrii Nakryikoc016b682020-03-02 20:31:58 -08007477 return link;
Andrii Nakryiko84bf5e12019-07-01 16:59:00 -07007478}
7479
Andrii Nakryikod7a18ea2019-12-13 17:43:26 -08007480static struct bpf_link *attach_raw_tp(const struct bpf_sec_def *sec,
7481 struct bpf_program *prog)
7482{
7483 const char *tp_name = bpf_program__title(prog, false) + sec->len;
7484
7485 return bpf_program__attach_raw_tracepoint(prog, tp_name);
7486}
7487
KP Singh1e092a032020-03-29 01:43:54 +01007488/* Common logic for all BPF program types that attach to a btf_id */
7489static struct bpf_link *bpf_program__attach_btf_id(struct bpf_program *prog)
Alexei Starovoitovb8c54ea2019-11-14 10:57:06 -08007490{
7491 char errmsg[STRERR_BUFSIZE];
Andrii Nakryikoc016b682020-03-02 20:31:58 -08007492 struct bpf_link *link;
Alexei Starovoitovb8c54ea2019-11-14 10:57:06 -08007493 int prog_fd, pfd;
7494
7495 prog_fd = bpf_program__fd(prog);
7496 if (prog_fd < 0) {
7497 pr_warn("program '%s': can't attach before loaded\n",
7498 bpf_program__title(prog, false));
7499 return ERR_PTR(-EINVAL);
7500 }
7501
Andrii Nakryikod6958702019-12-18 14:50:39 -08007502 link = calloc(1, sizeof(*link));
Alexei Starovoitovb8c54ea2019-11-14 10:57:06 -08007503 if (!link)
7504 return ERR_PTR(-ENOMEM);
Andrii Nakryikoc016b682020-03-02 20:31:58 -08007505 link->detach = &bpf_link__detach_fd;
Alexei Starovoitovb8c54ea2019-11-14 10:57:06 -08007506
7507 pfd = bpf_raw_tracepoint_open(NULL, prog_fd);
7508 if (pfd < 0) {
7509 pfd = -errno;
7510 free(link);
KP Singh1e092a032020-03-29 01:43:54 +01007511 pr_warn("program '%s': failed to attach: %s\n",
Alexei Starovoitovb8c54ea2019-11-14 10:57:06 -08007512 bpf_program__title(prog, false),
7513 libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
7514 return ERR_PTR(pfd);
7515 }
7516 link->fd = pfd;
7517 return (struct bpf_link *)link;
7518}
7519
KP Singh1e092a032020-03-29 01:43:54 +01007520struct bpf_link *bpf_program__attach_trace(struct bpf_program *prog)
7521{
7522 return bpf_program__attach_btf_id(prog);
7523}
7524
7525struct bpf_link *bpf_program__attach_lsm(struct bpf_program *prog)
7526{
7527 return bpf_program__attach_btf_id(prog);
7528}
7529
Andrii Nakryikod7a18ea2019-12-13 17:43:26 -08007530static struct bpf_link *attach_trace(const struct bpf_sec_def *sec,
7531 struct bpf_program *prog)
7532{
7533 return bpf_program__attach_trace(prog);
7534}
7535
KP Singh1e092a032020-03-29 01:43:54 +01007536static struct bpf_link *attach_lsm(const struct bpf_sec_def *sec,
7537 struct bpf_program *prog)
7538{
7539 return bpf_program__attach_lsm(prog);
7540}
7541
Andrii Nakryikocc4f8642020-03-29 20:00:00 -07007542struct bpf_link *
7543bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd)
7544{
7545 const struct bpf_sec_def *sec_def;
7546 enum bpf_attach_type attach_type;
7547 char errmsg[STRERR_BUFSIZE];
7548 struct bpf_link *link;
7549 int prog_fd, link_fd;
7550
7551 prog_fd = bpf_program__fd(prog);
7552 if (prog_fd < 0) {
7553 pr_warn("program '%s': can't attach before loaded\n",
7554 bpf_program__title(prog, false));
7555 return ERR_PTR(-EINVAL);
7556 }
7557
7558 link = calloc(1, sizeof(*link));
7559 if (!link)
7560 return ERR_PTR(-ENOMEM);
7561 link->detach = &bpf_link__detach_fd;
7562
7563 attach_type = bpf_program__get_expected_attach_type(prog);
7564 if (!attach_type) {
7565 sec_def = find_sec_def(bpf_program__title(prog, false));
7566 if (sec_def)
7567 attach_type = sec_def->attach_type;
7568 }
7569 link_fd = bpf_link_create(prog_fd, cgroup_fd, attach_type, NULL);
7570 if (link_fd < 0) {
7571 link_fd = -errno;
7572 free(link);
7573 pr_warn("program '%s': failed to attach to cgroup: %s\n",
7574 bpf_program__title(prog, false),
7575 libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg)));
7576 return ERR_PTR(link_fd);
7577 }
7578 link->fd = link_fd;
7579 return link;
7580}
7581
Andrii Nakryikod7a18ea2019-12-13 17:43:26 -08007582struct bpf_link *bpf_program__attach(struct bpf_program *prog)
7583{
7584 const struct bpf_sec_def *sec_def;
7585
7586 sec_def = find_sec_def(bpf_program__title(prog, false));
7587 if (!sec_def || !sec_def->attach_fn)
7588 return ERR_PTR(-ESRCH);
7589
7590 return sec_def->attach_fn(sec_def, prog);
7591}
7592
Martin KaFai Lau590a0082020-01-08 16:35:14 -08007593static int bpf_link__detach_struct_ops(struct bpf_link *link)
7594{
Martin KaFai Lau590a0082020-01-08 16:35:14 -08007595 __u32 zero = 0;
7596
Andrii Nakryikoc016b682020-03-02 20:31:58 -08007597 if (bpf_map_delete_elem(link->fd, &zero))
Martin KaFai Lau590a0082020-01-08 16:35:14 -08007598 return -errno;
7599
7600 return 0;
7601}
7602
7603struct bpf_link *bpf_map__attach_struct_ops(struct bpf_map *map)
7604{
7605 struct bpf_struct_ops *st_ops;
Andrii Nakryikoc016b682020-03-02 20:31:58 -08007606 struct bpf_link *link;
Martin KaFai Lau590a0082020-01-08 16:35:14 -08007607 __u32 i, zero = 0;
7608 int err;
7609
7610 if (!bpf_map__is_struct_ops(map) || map->fd == -1)
7611 return ERR_PTR(-EINVAL);
7612
7613 link = calloc(1, sizeof(*link));
7614 if (!link)
7615 return ERR_PTR(-EINVAL);
7616
7617 st_ops = map->st_ops;
7618 for (i = 0; i < btf_vlen(st_ops->type); i++) {
7619 struct bpf_program *prog = st_ops->progs[i];
7620 void *kern_data;
7621 int prog_fd;
7622
7623 if (!prog)
7624 continue;
7625
7626 prog_fd = bpf_program__fd(prog);
7627 kern_data = st_ops->kern_vdata + st_ops->kern_func_off[i];
7628 *(unsigned long *)kern_data = prog_fd;
7629 }
7630
7631 err = bpf_map_update_elem(map->fd, &zero, st_ops->kern_vdata, 0);
7632 if (err) {
7633 err = -errno;
7634 free(link);
7635 return ERR_PTR(err);
7636 }
7637
Andrii Nakryikoc016b682020-03-02 20:31:58 -08007638 link->detach = bpf_link__detach_struct_ops;
Martin KaFai Lau590a0082020-01-08 16:35:14 -08007639 link->fd = map->fd;
7640
Andrii Nakryikoc016b682020-03-02 20:31:58 -08007641 return link;
Martin KaFai Lau590a0082020-01-08 16:35:14 -08007642}
7643
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07007644enum bpf_perf_event_ret
Daniel Borkmann3dca2112018-10-21 02:09:28 +02007645bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
7646 void **copy_mem, size_t *copy_size,
7647 bpf_perf_event_print_t fn, void *private_data)
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07007648{
Daniel Borkmann3dca2112018-10-21 02:09:28 +02007649 struct perf_event_mmap_page *header = mmap_mem;
Daniel Borkmanna64af0e2018-10-19 15:51:03 +02007650 __u64 data_head = ring_buffer_read_head(header);
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07007651 __u64 data_tail = header->data_tail;
Daniel Borkmann3dca2112018-10-21 02:09:28 +02007652 void *base = ((__u8 *)header) + page_size;
7653 int ret = LIBBPF_PERF_EVENT_CONT;
7654 struct perf_event_header *ehdr;
7655 size_t ehdr_size;
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07007656
Daniel Borkmann3dca2112018-10-21 02:09:28 +02007657 while (data_head != data_tail) {
7658 ehdr = base + (data_tail & (mmap_size - 1));
7659 ehdr_size = ehdr->size;
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07007660
Daniel Borkmann3dca2112018-10-21 02:09:28 +02007661 if (((void *)ehdr) + ehdr_size > base + mmap_size) {
7662 void *copy_start = ehdr;
7663 size_t len_first = base + mmap_size - copy_start;
7664 size_t len_secnd = ehdr_size - len_first;
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07007665
Daniel Borkmann3dca2112018-10-21 02:09:28 +02007666 if (*copy_size < ehdr_size) {
7667 free(*copy_mem);
7668 *copy_mem = malloc(ehdr_size);
7669 if (!*copy_mem) {
7670 *copy_size = 0;
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07007671 ret = LIBBPF_PERF_EVENT_ERROR;
7672 break;
7673 }
Daniel Borkmann3dca2112018-10-21 02:09:28 +02007674 *copy_size = ehdr_size;
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07007675 }
7676
Daniel Borkmann3dca2112018-10-21 02:09:28 +02007677 memcpy(*copy_mem, copy_start, len_first);
7678 memcpy(*copy_mem + len_first, base, len_secnd);
7679 ehdr = *copy_mem;
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07007680 }
7681
Daniel Borkmann3dca2112018-10-21 02:09:28 +02007682 ret = fn(ehdr, private_data);
7683 data_tail += ehdr_size;
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07007684 if (ret != LIBBPF_PERF_EVENT_CONT)
7685 break;
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07007686 }
7687
Daniel Borkmanna64af0e2018-10-19 15:51:03 +02007688 ring_buffer_write_tail(header, data_tail);
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07007689 return ret;
7690}
Song Liu34be16462019-03-11 22:30:38 -07007691
Andrii Nakryikofb84b822019-07-06 11:06:24 -07007692struct perf_buffer;
7693
7694struct perf_buffer_params {
7695 struct perf_event_attr *attr;
7696 /* if event_cb is specified, it takes precendence */
7697 perf_buffer_event_fn event_cb;
7698 /* sample_cb and lost_cb are higher-level common-case callbacks */
7699 perf_buffer_sample_fn sample_cb;
7700 perf_buffer_lost_fn lost_cb;
7701 void *ctx;
7702 int cpu_cnt;
7703 int *cpus;
7704 int *map_keys;
7705};
7706
7707struct perf_cpu_buf {
7708 struct perf_buffer *pb;
7709 void *base; /* mmap()'ed memory */
7710 void *buf; /* for reconstructing segmented data */
7711 size_t buf_size;
7712 int fd;
7713 int cpu;
7714 int map_key;
7715};
7716
7717struct perf_buffer {
7718 perf_buffer_event_fn event_cb;
7719 perf_buffer_sample_fn sample_cb;
7720 perf_buffer_lost_fn lost_cb;
7721 void *ctx; /* passed into callbacks */
7722
7723 size_t page_size;
7724 size_t mmap_size;
7725 struct perf_cpu_buf **cpu_bufs;
7726 struct epoll_event *events;
Andrii Nakryiko783b8f02019-12-11 17:36:09 -08007727 int cpu_cnt; /* number of allocated CPU buffers */
Andrii Nakryikofb84b822019-07-06 11:06:24 -07007728 int epoll_fd; /* perf event FD */
7729 int map_fd; /* BPF_MAP_TYPE_PERF_EVENT_ARRAY BPF map FD */
7730};
7731
7732static void perf_buffer__free_cpu_buf(struct perf_buffer *pb,
7733 struct perf_cpu_buf *cpu_buf)
7734{
7735 if (!cpu_buf)
7736 return;
7737 if (cpu_buf->base &&
7738 munmap(cpu_buf->base, pb->mmap_size + pb->page_size))
Kefeng Wangbe180102019-10-21 13:55:32 +08007739 pr_warn("failed to munmap cpu_buf #%d\n", cpu_buf->cpu);
Andrii Nakryikofb84b822019-07-06 11:06:24 -07007740 if (cpu_buf->fd >= 0) {
7741 ioctl(cpu_buf->fd, PERF_EVENT_IOC_DISABLE, 0);
7742 close(cpu_buf->fd);
7743 }
7744 free(cpu_buf->buf);
7745 free(cpu_buf);
7746}
7747
7748void perf_buffer__free(struct perf_buffer *pb)
7749{
7750 int i;
7751
7752 if (!pb)
7753 return;
7754 if (pb->cpu_bufs) {
7755 for (i = 0; i < pb->cpu_cnt && pb->cpu_bufs[i]; i++) {
7756 struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i];
7757
7758 bpf_map_delete_elem(pb->map_fd, &cpu_buf->map_key);
7759 perf_buffer__free_cpu_buf(pb, cpu_buf);
7760 }
7761 free(pb->cpu_bufs);
7762 }
7763 if (pb->epoll_fd >= 0)
7764 close(pb->epoll_fd);
7765 free(pb->events);
7766 free(pb);
7767}
7768
7769static struct perf_cpu_buf *
7770perf_buffer__open_cpu_buf(struct perf_buffer *pb, struct perf_event_attr *attr,
7771 int cpu, int map_key)
7772{
7773 struct perf_cpu_buf *cpu_buf;
7774 char msg[STRERR_BUFSIZE];
7775 int err;
7776
7777 cpu_buf = calloc(1, sizeof(*cpu_buf));
7778 if (!cpu_buf)
7779 return ERR_PTR(-ENOMEM);
7780
7781 cpu_buf->pb = pb;
7782 cpu_buf->cpu = cpu;
7783 cpu_buf->map_key = map_key;
7784
7785 cpu_buf->fd = syscall(__NR_perf_event_open, attr, -1 /* pid */, cpu,
7786 -1, PERF_FLAG_FD_CLOEXEC);
7787 if (cpu_buf->fd < 0) {
7788 err = -errno;
Kefeng Wangbe180102019-10-21 13:55:32 +08007789 pr_warn("failed to open perf buffer event on cpu #%d: %s\n",
7790 cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
Andrii Nakryikofb84b822019-07-06 11:06:24 -07007791 goto error;
7792 }
7793
7794 cpu_buf->base = mmap(NULL, pb->mmap_size + pb->page_size,
7795 PROT_READ | PROT_WRITE, MAP_SHARED,
7796 cpu_buf->fd, 0);
7797 if (cpu_buf->base == MAP_FAILED) {
7798 cpu_buf->base = NULL;
7799 err = -errno;
Kefeng Wangbe180102019-10-21 13:55:32 +08007800 pr_warn("failed to mmap perf buffer on cpu #%d: %s\n",
7801 cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
Andrii Nakryikofb84b822019-07-06 11:06:24 -07007802 goto error;
7803 }
7804
7805 if (ioctl(cpu_buf->fd, PERF_EVENT_IOC_ENABLE, 0) < 0) {
7806 err = -errno;
Kefeng Wangbe180102019-10-21 13:55:32 +08007807 pr_warn("failed to enable perf buffer event on cpu #%d: %s\n",
7808 cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
Andrii Nakryikofb84b822019-07-06 11:06:24 -07007809 goto error;
7810 }
7811
7812 return cpu_buf;
7813
7814error:
7815 perf_buffer__free_cpu_buf(pb, cpu_buf);
7816 return (struct perf_cpu_buf *)ERR_PTR(err);
7817}
7818
7819static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,
7820 struct perf_buffer_params *p);
7821
7822struct perf_buffer *perf_buffer__new(int map_fd, size_t page_cnt,
7823 const struct perf_buffer_opts *opts)
7824{
7825 struct perf_buffer_params p = {};
Arnaldo Carvalho de Melo4be6e052019-07-19 11:34:07 -03007826 struct perf_event_attr attr = { 0, };
7827
7828 attr.config = PERF_COUNT_SW_BPF_OUTPUT,
7829 attr.type = PERF_TYPE_SOFTWARE;
7830 attr.sample_type = PERF_SAMPLE_RAW;
7831 attr.sample_period = 1;
7832 attr.wakeup_events = 1;
Andrii Nakryikofb84b822019-07-06 11:06:24 -07007833
7834 p.attr = &attr;
7835 p.sample_cb = opts ? opts->sample_cb : NULL;
7836 p.lost_cb = opts ? opts->lost_cb : NULL;
7837 p.ctx = opts ? opts->ctx : NULL;
7838
7839 return __perf_buffer__new(map_fd, page_cnt, &p);
7840}
7841
7842struct perf_buffer *
7843perf_buffer__new_raw(int map_fd, size_t page_cnt,
7844 const struct perf_buffer_raw_opts *opts)
7845{
7846 struct perf_buffer_params p = {};
7847
7848 p.attr = opts->attr;
7849 p.event_cb = opts->event_cb;
7850 p.ctx = opts->ctx;
7851 p.cpu_cnt = opts->cpu_cnt;
7852 p.cpus = opts->cpus;
7853 p.map_keys = opts->map_keys;
7854
7855 return __perf_buffer__new(map_fd, page_cnt, &p);
7856}
7857
7858static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,
7859 struct perf_buffer_params *p)
7860{
Andrii Nakryiko783b8f02019-12-11 17:36:09 -08007861 const char *online_cpus_file = "/sys/devices/system/cpu/online";
Andrii Nakryikofb84b822019-07-06 11:06:24 -07007862 struct bpf_map_info map = {};
7863 char msg[STRERR_BUFSIZE];
7864 struct perf_buffer *pb;
Andrii Nakryiko783b8f02019-12-11 17:36:09 -08007865 bool *online = NULL;
Andrii Nakryikofb84b822019-07-06 11:06:24 -07007866 __u32 map_info_len;
Andrii Nakryiko783b8f02019-12-11 17:36:09 -08007867 int err, i, j, n;
Andrii Nakryikofb84b822019-07-06 11:06:24 -07007868
7869 if (page_cnt & (page_cnt - 1)) {
Kefeng Wangbe180102019-10-21 13:55:32 +08007870 pr_warn("page count should be power of two, but is %zu\n",
7871 page_cnt);
Andrii Nakryikofb84b822019-07-06 11:06:24 -07007872 return ERR_PTR(-EINVAL);
7873 }
7874
7875 map_info_len = sizeof(map);
7876 err = bpf_obj_get_info_by_fd(map_fd, &map, &map_info_len);
7877 if (err) {
7878 err = -errno;
Kefeng Wangbe180102019-10-21 13:55:32 +08007879 pr_warn("failed to get map info for map FD %d: %s\n",
7880 map_fd, libbpf_strerror_r(err, msg, sizeof(msg)));
Andrii Nakryikofb84b822019-07-06 11:06:24 -07007881 return ERR_PTR(err);
7882 }
7883
7884 if (map.type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) {
Kefeng Wangbe180102019-10-21 13:55:32 +08007885 pr_warn("map '%s' should be BPF_MAP_TYPE_PERF_EVENT_ARRAY\n",
7886 map.name);
Andrii Nakryikofb84b822019-07-06 11:06:24 -07007887 return ERR_PTR(-EINVAL);
7888 }
7889
7890 pb = calloc(1, sizeof(*pb));
7891 if (!pb)
7892 return ERR_PTR(-ENOMEM);
7893
7894 pb->event_cb = p->event_cb;
7895 pb->sample_cb = p->sample_cb;
7896 pb->lost_cb = p->lost_cb;
7897 pb->ctx = p->ctx;
7898
7899 pb->page_size = getpagesize();
7900 pb->mmap_size = pb->page_size * page_cnt;
7901 pb->map_fd = map_fd;
7902
7903 pb->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
7904 if (pb->epoll_fd < 0) {
7905 err = -errno;
Kefeng Wangbe180102019-10-21 13:55:32 +08007906 pr_warn("failed to create epoll instance: %s\n",
7907 libbpf_strerror_r(err, msg, sizeof(msg)));
Andrii Nakryikofb84b822019-07-06 11:06:24 -07007908 goto error;
7909 }
7910
7911 if (p->cpu_cnt > 0) {
7912 pb->cpu_cnt = p->cpu_cnt;
7913 } else {
7914 pb->cpu_cnt = libbpf_num_possible_cpus();
7915 if (pb->cpu_cnt < 0) {
7916 err = pb->cpu_cnt;
7917 goto error;
7918 }
7919 if (map.max_entries < pb->cpu_cnt)
7920 pb->cpu_cnt = map.max_entries;
7921 }
7922
7923 pb->events = calloc(pb->cpu_cnt, sizeof(*pb->events));
7924 if (!pb->events) {
7925 err = -ENOMEM;
Kefeng Wangbe180102019-10-21 13:55:32 +08007926 pr_warn("failed to allocate events: out of memory\n");
Andrii Nakryikofb84b822019-07-06 11:06:24 -07007927 goto error;
7928 }
7929 pb->cpu_bufs = calloc(pb->cpu_cnt, sizeof(*pb->cpu_bufs));
7930 if (!pb->cpu_bufs) {
7931 err = -ENOMEM;
Kefeng Wangbe180102019-10-21 13:55:32 +08007932 pr_warn("failed to allocate buffers: out of memory\n");
Andrii Nakryikofb84b822019-07-06 11:06:24 -07007933 goto error;
7934 }
7935
Andrii Nakryiko783b8f02019-12-11 17:36:09 -08007936 err = parse_cpu_mask_file(online_cpus_file, &online, &n);
7937 if (err) {
7938 pr_warn("failed to get online CPU mask: %d\n", err);
7939 goto error;
7940 }
7941
7942 for (i = 0, j = 0; i < pb->cpu_cnt; i++) {
Andrii Nakryikofb84b822019-07-06 11:06:24 -07007943 struct perf_cpu_buf *cpu_buf;
7944 int cpu, map_key;
7945
7946 cpu = p->cpu_cnt > 0 ? p->cpus[i] : i;
7947 map_key = p->cpu_cnt > 0 ? p->map_keys[i] : i;
7948
Andrii Nakryiko783b8f02019-12-11 17:36:09 -08007949 /* in case user didn't explicitly requested particular CPUs to
7950 * be attached to, skip offline/not present CPUs
7951 */
7952 if (p->cpu_cnt <= 0 && (cpu >= n || !online[cpu]))
7953 continue;
7954
Andrii Nakryikofb84b822019-07-06 11:06:24 -07007955 cpu_buf = perf_buffer__open_cpu_buf(pb, p->attr, cpu, map_key);
7956 if (IS_ERR(cpu_buf)) {
7957 err = PTR_ERR(cpu_buf);
7958 goto error;
7959 }
7960
Andrii Nakryiko783b8f02019-12-11 17:36:09 -08007961 pb->cpu_bufs[j] = cpu_buf;
Andrii Nakryikofb84b822019-07-06 11:06:24 -07007962
7963 err = bpf_map_update_elem(pb->map_fd, &map_key,
7964 &cpu_buf->fd, 0);
7965 if (err) {
7966 err = -errno;
Kefeng Wangbe180102019-10-21 13:55:32 +08007967 pr_warn("failed to set cpu #%d, key %d -> perf FD %d: %s\n",
7968 cpu, map_key, cpu_buf->fd,
7969 libbpf_strerror_r(err, msg, sizeof(msg)));
Andrii Nakryikofb84b822019-07-06 11:06:24 -07007970 goto error;
7971 }
7972
Andrii Nakryiko783b8f02019-12-11 17:36:09 -08007973 pb->events[j].events = EPOLLIN;
7974 pb->events[j].data.ptr = cpu_buf;
Andrii Nakryikofb84b822019-07-06 11:06:24 -07007975 if (epoll_ctl(pb->epoll_fd, EPOLL_CTL_ADD, cpu_buf->fd,
Andrii Nakryiko783b8f02019-12-11 17:36:09 -08007976 &pb->events[j]) < 0) {
Andrii Nakryikofb84b822019-07-06 11:06:24 -07007977 err = -errno;
Kefeng Wangbe180102019-10-21 13:55:32 +08007978 pr_warn("failed to epoll_ctl cpu #%d perf FD %d: %s\n",
7979 cpu, cpu_buf->fd,
7980 libbpf_strerror_r(err, msg, sizeof(msg)));
Andrii Nakryikofb84b822019-07-06 11:06:24 -07007981 goto error;
7982 }
Andrii Nakryiko783b8f02019-12-11 17:36:09 -08007983 j++;
Andrii Nakryikofb84b822019-07-06 11:06:24 -07007984 }
Andrii Nakryiko783b8f02019-12-11 17:36:09 -08007985 pb->cpu_cnt = j;
7986 free(online);
Andrii Nakryikofb84b822019-07-06 11:06:24 -07007987
7988 return pb;
7989
7990error:
Andrii Nakryiko783b8f02019-12-11 17:36:09 -08007991 free(online);
Andrii Nakryikofb84b822019-07-06 11:06:24 -07007992 if (pb)
7993 perf_buffer__free(pb);
7994 return ERR_PTR(err);
7995}
7996
7997struct perf_sample_raw {
7998 struct perf_event_header header;
7999 uint32_t size;
8000 char data[0];
8001};
8002
8003struct perf_sample_lost {
8004 struct perf_event_header header;
8005 uint64_t id;
8006 uint64_t lost;
8007 uint64_t sample_id;
8008};
8009
8010static enum bpf_perf_event_ret
8011perf_buffer__process_record(struct perf_event_header *e, void *ctx)
8012{
8013 struct perf_cpu_buf *cpu_buf = ctx;
8014 struct perf_buffer *pb = cpu_buf->pb;
8015 void *data = e;
8016
8017 /* user wants full control over parsing perf event */
8018 if (pb->event_cb)
8019 return pb->event_cb(pb->ctx, cpu_buf->cpu, e);
8020
8021 switch (e->type) {
8022 case PERF_RECORD_SAMPLE: {
8023 struct perf_sample_raw *s = data;
8024
8025 if (pb->sample_cb)
8026 pb->sample_cb(pb->ctx, cpu_buf->cpu, s->data, s->size);
8027 break;
8028 }
8029 case PERF_RECORD_LOST: {
8030 struct perf_sample_lost *s = data;
8031
8032 if (pb->lost_cb)
8033 pb->lost_cb(pb->ctx, cpu_buf->cpu, s->lost);
8034 break;
8035 }
8036 default:
Kefeng Wangbe180102019-10-21 13:55:32 +08008037 pr_warn("unknown perf sample type %d\n", e->type);
Andrii Nakryikofb84b822019-07-06 11:06:24 -07008038 return LIBBPF_PERF_EVENT_ERROR;
8039 }
8040 return LIBBPF_PERF_EVENT_CONT;
8041}
8042
8043static int perf_buffer__process_records(struct perf_buffer *pb,
8044 struct perf_cpu_buf *cpu_buf)
8045{
8046 enum bpf_perf_event_ret ret;
8047
8048 ret = bpf_perf_event_read_simple(cpu_buf->base, pb->mmap_size,
8049 pb->page_size, &cpu_buf->buf,
8050 &cpu_buf->buf_size,
8051 perf_buffer__process_record, cpu_buf);
8052 if (ret != LIBBPF_PERF_EVENT_CONT)
8053 return ret;
8054 return 0;
8055}
8056
8057int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms)
8058{
8059 int i, cnt, err;
8060
8061 cnt = epoll_wait(pb->epoll_fd, pb->events, pb->cpu_cnt, timeout_ms);
8062 for (i = 0; i < cnt; i++) {
8063 struct perf_cpu_buf *cpu_buf = pb->events[i].data.ptr;
8064
8065 err = perf_buffer__process_records(pb, cpu_buf);
8066 if (err) {
Kefeng Wangbe180102019-10-21 13:55:32 +08008067 pr_warn("error while processing records: %d\n", err);
Andrii Nakryikofb84b822019-07-06 11:06:24 -07008068 return err;
8069 }
8070 }
8071 return cnt < 0 ? -errno : cnt;
8072}
8073
Song Liu34be16462019-03-11 22:30:38 -07008074struct bpf_prog_info_array_desc {
8075 int array_offset; /* e.g. offset of jited_prog_insns */
8076 int count_offset; /* e.g. offset of jited_prog_len */
8077 int size_offset; /* > 0: offset of rec size,
8078 * < 0: fix size of -size_offset
8079 */
8080};
8081
8082static struct bpf_prog_info_array_desc bpf_prog_info_array_desc[] = {
8083 [BPF_PROG_INFO_JITED_INSNS] = {
8084 offsetof(struct bpf_prog_info, jited_prog_insns),
8085 offsetof(struct bpf_prog_info, jited_prog_len),
8086 -1,
8087 },
8088 [BPF_PROG_INFO_XLATED_INSNS] = {
8089 offsetof(struct bpf_prog_info, xlated_prog_insns),
8090 offsetof(struct bpf_prog_info, xlated_prog_len),
8091 -1,
8092 },
8093 [BPF_PROG_INFO_MAP_IDS] = {
8094 offsetof(struct bpf_prog_info, map_ids),
8095 offsetof(struct bpf_prog_info, nr_map_ids),
8096 -(int)sizeof(__u32),
8097 },
8098 [BPF_PROG_INFO_JITED_KSYMS] = {
8099 offsetof(struct bpf_prog_info, jited_ksyms),
8100 offsetof(struct bpf_prog_info, nr_jited_ksyms),
8101 -(int)sizeof(__u64),
8102 },
8103 [BPF_PROG_INFO_JITED_FUNC_LENS] = {
8104 offsetof(struct bpf_prog_info, jited_func_lens),
8105 offsetof(struct bpf_prog_info, nr_jited_func_lens),
8106 -(int)sizeof(__u32),
8107 },
8108 [BPF_PROG_INFO_FUNC_INFO] = {
8109 offsetof(struct bpf_prog_info, func_info),
8110 offsetof(struct bpf_prog_info, nr_func_info),
8111 offsetof(struct bpf_prog_info, func_info_rec_size),
8112 },
8113 [BPF_PROG_INFO_LINE_INFO] = {
8114 offsetof(struct bpf_prog_info, line_info),
8115 offsetof(struct bpf_prog_info, nr_line_info),
8116 offsetof(struct bpf_prog_info, line_info_rec_size),
8117 },
8118 [BPF_PROG_INFO_JITED_LINE_INFO] = {
8119 offsetof(struct bpf_prog_info, jited_line_info),
8120 offsetof(struct bpf_prog_info, nr_jited_line_info),
8121 offsetof(struct bpf_prog_info, jited_line_info_rec_size),
8122 },
8123 [BPF_PROG_INFO_PROG_TAGS] = {
8124 offsetof(struct bpf_prog_info, prog_tags),
8125 offsetof(struct bpf_prog_info, nr_prog_tags),
8126 -(int)sizeof(__u8) * BPF_TAG_SIZE,
8127 },
8128
8129};
8130
Andrii Nakryiko8983b732019-11-20 23:07:42 -08008131static __u32 bpf_prog_info_read_offset_u32(struct bpf_prog_info *info,
8132 int offset)
Song Liu34be16462019-03-11 22:30:38 -07008133{
8134 __u32 *array = (__u32 *)info;
8135
8136 if (offset >= 0)
8137 return array[offset / sizeof(__u32)];
8138 return -(int)offset;
8139}
8140
Andrii Nakryiko8983b732019-11-20 23:07:42 -08008141static __u64 bpf_prog_info_read_offset_u64(struct bpf_prog_info *info,
8142 int offset)
Song Liu34be16462019-03-11 22:30:38 -07008143{
8144 __u64 *array = (__u64 *)info;
8145
8146 if (offset >= 0)
8147 return array[offset / sizeof(__u64)];
8148 return -(int)offset;
8149}
8150
8151static void bpf_prog_info_set_offset_u32(struct bpf_prog_info *info, int offset,
8152 __u32 val)
8153{
8154 __u32 *array = (__u32 *)info;
8155
8156 if (offset >= 0)
8157 array[offset / sizeof(__u32)] = val;
8158}
8159
8160static void bpf_prog_info_set_offset_u64(struct bpf_prog_info *info, int offset,
8161 __u64 val)
8162{
8163 __u64 *array = (__u64 *)info;
8164
8165 if (offset >= 0)
8166 array[offset / sizeof(__u64)] = val;
8167}
8168
8169struct bpf_prog_info_linear *
8170bpf_program__get_prog_info_linear(int fd, __u64 arrays)
8171{
8172 struct bpf_prog_info_linear *info_linear;
8173 struct bpf_prog_info info = {};
8174 __u32 info_len = sizeof(info);
8175 __u32 data_len = 0;
8176 int i, err;
8177 void *ptr;
8178
8179 if (arrays >> BPF_PROG_INFO_LAST_ARRAY)
8180 return ERR_PTR(-EINVAL);
8181
8182 /* step 1: get array dimensions */
8183 err = bpf_obj_get_info_by_fd(fd, &info, &info_len);
8184 if (err) {
8185 pr_debug("can't get prog info: %s", strerror(errno));
8186 return ERR_PTR(-EFAULT);
8187 }
8188
8189 /* step 2: calculate total size of all arrays */
8190 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
8191 bool include_array = (arrays & (1UL << i)) > 0;
8192 struct bpf_prog_info_array_desc *desc;
8193 __u32 count, size;
8194
8195 desc = bpf_prog_info_array_desc + i;
8196
8197 /* kernel is too old to support this field */
8198 if (info_len < desc->array_offset + sizeof(__u32) ||
8199 info_len < desc->count_offset + sizeof(__u32) ||
8200 (desc->size_offset > 0 && info_len < desc->size_offset))
8201 include_array = false;
8202
8203 if (!include_array) {
8204 arrays &= ~(1UL << i); /* clear the bit */
8205 continue;
8206 }
8207
8208 count = bpf_prog_info_read_offset_u32(&info, desc->count_offset);
8209 size = bpf_prog_info_read_offset_u32(&info, desc->size_offset);
8210
8211 data_len += count * size;
8212 }
8213
8214 /* step 3: allocate continuous memory */
8215 data_len = roundup(data_len, sizeof(__u64));
8216 info_linear = malloc(sizeof(struct bpf_prog_info_linear) + data_len);
8217 if (!info_linear)
8218 return ERR_PTR(-ENOMEM);
8219
8220 /* step 4: fill data to info_linear->info */
8221 info_linear->arrays = arrays;
8222 memset(&info_linear->info, 0, sizeof(info));
8223 ptr = info_linear->data;
8224
8225 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
8226 struct bpf_prog_info_array_desc *desc;
8227 __u32 count, size;
8228
8229 if ((arrays & (1UL << i)) == 0)
8230 continue;
8231
8232 desc = bpf_prog_info_array_desc + i;
8233 count = bpf_prog_info_read_offset_u32(&info, desc->count_offset);
8234 size = bpf_prog_info_read_offset_u32(&info, desc->size_offset);
8235 bpf_prog_info_set_offset_u32(&info_linear->info,
8236 desc->count_offset, count);
8237 bpf_prog_info_set_offset_u32(&info_linear->info,
8238 desc->size_offset, size);
8239 bpf_prog_info_set_offset_u64(&info_linear->info,
8240 desc->array_offset,
8241 ptr_to_u64(ptr));
8242 ptr += count * size;
8243 }
8244
8245 /* step 5: call syscall again to get required arrays */
8246 err = bpf_obj_get_info_by_fd(fd, &info_linear->info, &info_len);
8247 if (err) {
8248 pr_debug("can't get prog info: %s", strerror(errno));
8249 free(info_linear);
8250 return ERR_PTR(-EFAULT);
8251 }
8252
8253 /* step 6: verify the data */
8254 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
8255 struct bpf_prog_info_array_desc *desc;
8256 __u32 v1, v2;
8257
8258 if ((arrays & (1UL << i)) == 0)
8259 continue;
8260
8261 desc = bpf_prog_info_array_desc + i;
8262 v1 = bpf_prog_info_read_offset_u32(&info, desc->count_offset);
8263 v2 = bpf_prog_info_read_offset_u32(&info_linear->info,
8264 desc->count_offset);
8265 if (v1 != v2)
Kefeng Wangbe180102019-10-21 13:55:32 +08008266 pr_warn("%s: mismatch in element count\n", __func__);
Song Liu34be16462019-03-11 22:30:38 -07008267
8268 v1 = bpf_prog_info_read_offset_u32(&info, desc->size_offset);
8269 v2 = bpf_prog_info_read_offset_u32(&info_linear->info,
8270 desc->size_offset);
8271 if (v1 != v2)
Kefeng Wangbe180102019-10-21 13:55:32 +08008272 pr_warn("%s: mismatch in rec size\n", __func__);
Song Liu34be16462019-03-11 22:30:38 -07008273 }
8274
8275 /* step 7: update info_len and data_len */
8276 info_linear->info_len = sizeof(struct bpf_prog_info);
8277 info_linear->data_len = data_len;
8278
8279 return info_linear;
8280}
8281
8282void bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear)
8283{
8284 int i;
8285
8286 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
8287 struct bpf_prog_info_array_desc *desc;
8288 __u64 addr, offs;
8289
8290 if ((info_linear->arrays & (1UL << i)) == 0)
8291 continue;
8292
8293 desc = bpf_prog_info_array_desc + i;
8294 addr = bpf_prog_info_read_offset_u64(&info_linear->info,
8295 desc->array_offset);
8296 offs = addr - ptr_to_u64(info_linear->data);
8297 bpf_prog_info_set_offset_u64(&info_linear->info,
8298 desc->array_offset, offs);
8299 }
8300}
8301
8302void bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear)
8303{
8304 int i;
8305
8306 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
8307 struct bpf_prog_info_array_desc *desc;
8308 __u64 addr, offs;
8309
8310 if ((info_linear->arrays & (1UL << i)) == 0)
8311 continue;
8312
8313 desc = bpf_prog_info_array_desc + i;
8314 offs = bpf_prog_info_read_offset_u64(&info_linear->info,
8315 desc->array_offset);
8316 addr = offs + ptr_to_u64(info_linear->data);
8317 bpf_prog_info_set_offset_u64(&info_linear->info,
8318 desc->array_offset, addr);
8319 }
8320}
Hechao Li6446b312019-06-10 17:56:50 -07008321
Eelco Chaudronff26ce52020-02-20 13:26:35 +00008322int bpf_program__set_attach_target(struct bpf_program *prog,
8323 int attach_prog_fd,
8324 const char *attach_func_name)
8325{
8326 int btf_id;
8327
8328 if (!prog || attach_prog_fd < 0 || !attach_func_name)
8329 return -EINVAL;
8330
8331 if (attach_prog_fd)
8332 btf_id = libbpf_find_prog_btf_id(attach_func_name,
8333 attach_prog_fd);
8334 else
8335 btf_id = __find_vmlinux_btf_id(prog->obj->btf_vmlinux,
8336 attach_func_name,
8337 prog->expected_attach_type);
8338
8339 if (btf_id < 0)
8340 return btf_id;
8341
8342 prog->attach_btf_id = btf_id;
8343 prog->attach_prog_fd = attach_prog_fd;
8344 return 0;
8345}
8346
Andrii Nakryiko6803ee22019-12-11 17:35:48 -08008347int parse_cpu_mask_str(const char *s, bool **mask, int *mask_sz)
8348{
8349 int err = 0, n, len, start, end = -1;
8350 bool *tmp;
8351
8352 *mask = NULL;
8353 *mask_sz = 0;
8354
8355 /* Each sub string separated by ',' has format \d+-\d+ or \d+ */
8356 while (*s) {
8357 if (*s == ',' || *s == '\n') {
8358 s++;
8359 continue;
8360 }
8361 n = sscanf(s, "%d%n-%d%n", &start, &len, &end, &len);
8362 if (n <= 0 || n > 2) {
8363 pr_warn("Failed to get CPU range %s: %d\n", s, n);
8364 err = -EINVAL;
8365 goto cleanup;
8366 } else if (n == 1) {
8367 end = start;
8368 }
8369 if (start < 0 || start > end) {
8370 pr_warn("Invalid CPU range [%d,%d] in %s\n",
8371 start, end, s);
8372 err = -EINVAL;
8373 goto cleanup;
8374 }
8375 tmp = realloc(*mask, end + 1);
8376 if (!tmp) {
8377 err = -ENOMEM;
8378 goto cleanup;
8379 }
8380 *mask = tmp;
8381 memset(tmp + *mask_sz, 0, start - *mask_sz);
8382 memset(tmp + start, 1, end - start + 1);
8383 *mask_sz = end + 1;
8384 s += len;
8385 }
8386 if (!*mask_sz) {
8387 pr_warn("Empty CPU range\n");
8388 return -EINVAL;
8389 }
8390 return 0;
8391cleanup:
8392 free(*mask);
8393 *mask = NULL;
8394 return err;
8395}
8396
8397int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz)
8398{
8399 int fd, err = 0, len;
8400 char buf[128];
8401
8402 fd = open(fcpu, O_RDONLY);
8403 if (fd < 0) {
8404 err = -errno;
8405 pr_warn("Failed to open cpu mask file %s: %d\n", fcpu, err);
8406 return err;
8407 }
8408 len = read(fd, buf, sizeof(buf));
8409 close(fd);
8410 if (len <= 0) {
8411 err = len ? -errno : -EINVAL;
8412 pr_warn("Failed to read cpu mask from %s: %d\n", fcpu, err);
8413 return err;
8414 }
8415 if (len >= sizeof(buf)) {
8416 pr_warn("CPU mask is too big in file %s\n", fcpu);
8417 return -E2BIG;
8418 }
8419 buf[len] = '\0';
8420
8421 return parse_cpu_mask_str(buf, mask, mask_sz);
8422}
8423
Hechao Li6446b312019-06-10 17:56:50 -07008424int libbpf_num_possible_cpus(void)
8425{
8426 static const char *fcpu = "/sys/devices/system/cpu/possible";
Hechao Li6446b312019-06-10 17:56:50 -07008427 static int cpus;
Andrii Nakryiko6803ee22019-12-11 17:35:48 -08008428 int err, n, i, tmp_cpus;
8429 bool *mask;
Hechao Li6446b312019-06-10 17:56:50 -07008430
Takshak Chahande56fbc242019-07-31 15:10:55 -07008431 tmp_cpus = READ_ONCE(cpus);
8432 if (tmp_cpus > 0)
8433 return tmp_cpus;
Hechao Li6446b312019-06-10 17:56:50 -07008434
Andrii Nakryiko6803ee22019-12-11 17:35:48 -08008435 err = parse_cpu_mask_file(fcpu, &mask, &n);
8436 if (err)
8437 return err;
Hechao Li6446b312019-06-10 17:56:50 -07008438
Andrii Nakryiko6803ee22019-12-11 17:35:48 -08008439 tmp_cpus = 0;
8440 for (i = 0; i < n; i++) {
8441 if (mask[i])
8442 tmp_cpus++;
Hechao Li6446b312019-06-10 17:56:50 -07008443 }
Andrii Nakryiko6803ee22019-12-11 17:35:48 -08008444 free(mask);
Takshak Chahande56fbc242019-07-31 15:10:55 -07008445
8446 WRITE_ONCE(cpus, tmp_cpus);
8447 return tmp_cpus;
Hechao Li6446b312019-06-10 17:56:50 -07008448}
Andrii Nakryikod66562f2019-12-13 17:43:36 -08008449
8450int bpf_object__open_skeleton(struct bpf_object_skeleton *s,
8451 const struct bpf_object_open_opts *opts)
8452{
8453 DECLARE_LIBBPF_OPTS(bpf_object_open_opts, skel_opts,
8454 .object_name = s->name,
8455 );
8456 struct bpf_object *obj;
8457 int i;
8458
8459 /* Attempt to preserve opts->object_name, unless overriden by user
8460 * explicitly. Overwriting object name for skeletons is discouraged,
8461 * as it breaks global data maps, because they contain object name
8462 * prefix as their own map name prefix. When skeleton is generated,
8463 * bpftool is making an assumption that this name will stay the same.
8464 */
8465 if (opts) {
8466 memcpy(&skel_opts, opts, sizeof(*opts));
8467 if (!opts->object_name)
8468 skel_opts.object_name = s->name;
8469 }
8470
8471 obj = bpf_object__open_mem(s->data, s->data_sz, &skel_opts);
8472 if (IS_ERR(obj)) {
8473 pr_warn("failed to initialize skeleton BPF object '%s': %ld\n",
8474 s->name, PTR_ERR(obj));
8475 return PTR_ERR(obj);
8476 }
8477
8478 *s->obj = obj;
8479
8480 for (i = 0; i < s->map_cnt; i++) {
8481 struct bpf_map **map = s->maps[i].map;
8482 const char *name = s->maps[i].name;
8483 void **mmaped = s->maps[i].mmaped;
8484
8485 *map = bpf_object__find_map_by_name(obj, name);
8486 if (!*map) {
8487 pr_warn("failed to find skeleton map '%s'\n", name);
8488 return -ESRCH;
8489 }
8490
Andrii Nakryiko2ad97d42019-12-13 17:47:09 -08008491 /* externs shouldn't be pre-setup from user code */
Andrii Nakryiko81bfdd02019-12-18 16:28:34 -08008492 if (mmaped && (*map)->libbpf_type != LIBBPF_MAP_KCONFIG)
Andrii Nakryikod66562f2019-12-13 17:43:36 -08008493 *mmaped = (*map)->mmaped;
8494 }
8495
8496 for (i = 0; i < s->prog_cnt; i++) {
8497 struct bpf_program **prog = s->progs[i].prog;
8498 const char *name = s->progs[i].name;
8499
8500 *prog = bpf_object__find_program_by_name(obj, name);
8501 if (!*prog) {
8502 pr_warn("failed to find skeleton program '%s'\n", name);
8503 return -ESRCH;
8504 }
8505 }
8506
8507 return 0;
8508}
8509
8510int bpf_object__load_skeleton(struct bpf_object_skeleton *s)
8511{
8512 int i, err;
8513
8514 err = bpf_object__load(*s->obj);
8515 if (err) {
8516 pr_warn("failed to load BPF skeleton '%s': %d\n", s->name, err);
8517 return err;
8518 }
8519
8520 for (i = 0; i < s->map_cnt; i++) {
8521 struct bpf_map *map = *s->maps[i].map;
8522 size_t mmap_sz = bpf_map_mmap_sz(map);
8523 int prot, map_fd = bpf_map__fd(map);
8524 void **mmaped = s->maps[i].mmaped;
Andrii Nakryikod66562f2019-12-13 17:43:36 -08008525
8526 if (!mmaped)
8527 continue;
8528
8529 if (!(map->def.map_flags & BPF_F_MMAPABLE)) {
8530 *mmaped = NULL;
8531 continue;
8532 }
8533
8534 if (map->def.map_flags & BPF_F_RDONLY_PROG)
8535 prot = PROT_READ;
8536 else
8537 prot = PROT_READ | PROT_WRITE;
8538
8539 /* Remap anonymous mmap()-ed "map initialization image" as
8540 * a BPF map-backed mmap()-ed memory, but preserving the same
8541 * memory address. This will cause kernel to change process'
8542 * page table to point to a different piece of kernel memory,
8543 * but from userspace point of view memory address (and its
8544 * contents, being identical at this point) will stay the
8545 * same. This mapping will be released by bpf_object__close()
8546 * as per normal clean up procedure, so we don't need to worry
8547 * about it from skeleton's clean up perspective.
8548 */
Andrii Nakryiko2ad97d42019-12-13 17:47:09 -08008549 *mmaped = mmap(map->mmaped, mmap_sz, prot,
8550 MAP_SHARED | MAP_FIXED, map_fd, 0);
8551 if (*mmaped == MAP_FAILED) {
Andrii Nakryikod66562f2019-12-13 17:43:36 -08008552 err = -errno;
8553 *mmaped = NULL;
8554 pr_warn("failed to re-mmap() map '%s': %d\n",
8555 bpf_map__name(map), err);
8556 return err;
8557 }
8558 }
8559
8560 return 0;
8561}
8562
8563int bpf_object__attach_skeleton(struct bpf_object_skeleton *s)
8564{
8565 int i;
8566
8567 for (i = 0; i < s->prog_cnt; i++) {
8568 struct bpf_program *prog = *s->progs[i].prog;
8569 struct bpf_link **link = s->progs[i].link;
8570 const struct bpf_sec_def *sec_def;
8571 const char *sec_name = bpf_program__title(prog, false);
8572
8573 sec_def = find_sec_def(sec_name);
8574 if (!sec_def || !sec_def->attach_fn)
8575 continue;
8576
8577 *link = sec_def->attach_fn(sec_def, prog);
8578 if (IS_ERR(*link)) {
8579 pr_warn("failed to auto-attach program '%s': %ld\n",
8580 bpf_program__name(prog), PTR_ERR(*link));
8581 return PTR_ERR(*link);
8582 }
8583 }
8584
8585 return 0;
8586}
8587
8588void bpf_object__detach_skeleton(struct bpf_object_skeleton *s)
8589{
8590 int i;
8591
8592 for (i = 0; i < s->prog_cnt; i++) {
8593 struct bpf_link **link = s->progs[i].link;
8594
8595 if (!IS_ERR_OR_NULL(*link))
8596 bpf_link__destroy(*link);
8597 *link = NULL;
8598 }
8599}
8600
8601void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s)
8602{
8603 if (s->progs)
8604 bpf_object__detach_skeleton(s);
8605 if (s->obj)
8606 bpf_object__close(*s->obj);
8607 free(s->maps);
8608 free(s->progs);
8609 free(s);
8610}