blob: 794dd5064ae8a01d9628c3baade0605a24fa4f44 [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>
Wang Nanb3f59d62015-07-01 02:13:52 +000021#include <string.h>
Wang Nan1b76c132015-07-01 02:13:51 +000022#include <unistd.h>
Wang Nan1a5e3fb2015-07-01 02:13:53 +000023#include <fcntl.h>
24#include <errno.h>
Wang Nan1b76c132015-07-01 02:13:51 +000025#include <asm/unistd.h>
Joe Stringere28ff1a2017-01-22 17:11:25 -080026#include <linux/err.h>
Wang Nancb1e5e92015-07-01 02:13:57 +000027#include <linux/kernel.h>
Wang Nan1b76c132015-07-01 02:13:51 +000028#include <linux/bpf.h>
Martin KaFai Lau38d5d3b2018-07-24 08:40:22 -070029#include <linux/btf.h>
Stanislav Fomichev47eff612018-11-20 17:11:19 -080030#include <linux/filter.h>
Wang Nan9a208ef2015-07-01 02:14:10 +000031#include <linux/list.h>
Joe Stringerf3675402017-01-26 13:19:56 -080032#include <linux/limits.h>
Yonghong Song438363c2018-10-09 16:14:47 -070033#include <linux/perf_event.h>
Daniel Borkmanna64af0e2018-10-19 15:51:03 +020034#include <linux/ring_buffer.h>
Andrii Nakryikofb84b822019-07-06 11:06:24 -070035#include <sys/epoll.h>
Andrii Nakryiko63f2f5e2019-07-01 16:58:57 -070036#include <sys/ioctl.h>
Andrii Nakryikofb84b822019-07-06 11:06:24 -070037#include <sys/mman.h>
Joe Stringerf3675402017-01-26 13:19:56 -080038#include <sys/stat.h>
39#include <sys/types.h>
40#include <sys/vfs.h>
Jakub Kicinski531b0142018-07-10 14:43:05 -070041#include <tools/libc_compat.h>
Wang Nan1a5e3fb2015-07-01 02:13:53 +000042#include <libelf.h>
43#include <gelf.h>
Wang Nan1b76c132015-07-01 02:13:51 +000044
45#include "libbpf.h"
Wang Nan52d33522015-07-01 02:14:04 +000046#include "bpf.h"
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -070047#include "btf.h"
Arnaldo Carvalho de Melo6d419072018-09-14 16:47:14 -030048#include "str_error.h"
Andrii Nakryikod7c4b392019-05-10 14:13:15 -070049#include "libbpf_internal.h"
Wang Nanb3f59d62015-07-01 02:13:52 +000050
Wang Nan9b161372016-07-18 06:01:08 +000051#ifndef EM_BPF
52#define EM_BPF 247
53#endif
54
Joe Stringerf3675402017-01-26 13:19:56 -080055#ifndef BPF_FS_MAGIC
56#define BPF_FS_MAGIC 0xcafe4a11
57#endif
58
Andrey Ignatovff466b52019-04-06 22:37:34 -070059/* vsprintf() in __base_pr() uses nonliteral format string. It may break
60 * compilation if user enables corresponding warning. Disable it explicitly.
61 */
62#pragma GCC diagnostic ignored "-Wformat-nonliteral"
63
Wang Nanb3f59d62015-07-01 02:13:52 +000064#define __printf(a, b) __attribute__((format(printf, a, b)))
65
Stanislav Fomicheva8a1f7d2019-02-04 16:20:55 -080066static int __base_pr(enum libbpf_print_level level, const char *format,
67 va_list args)
Wang Nanb3f59d62015-07-01 02:13:52 +000068{
Yonghong Song6f1ae8b2019-02-01 16:14:17 -080069 if (level == LIBBPF_DEBUG)
70 return 0;
71
Stanislav Fomicheva8a1f7d2019-02-04 16:20:55 -080072 return vfprintf(stderr, format, args);
Wang Nanb3f59d62015-07-01 02:13:52 +000073}
74
Stanislav Fomicheva8a1f7d2019-02-04 16:20:55 -080075static libbpf_print_fn_t __libbpf_pr = __base_pr;
Wang Nanb3f59d62015-07-01 02:13:52 +000076
Yonghong Song6f1ae8b2019-02-01 16:14:17 -080077void libbpf_set_print(libbpf_print_fn_t fn)
Wang Nanb3f59d62015-07-01 02:13:52 +000078{
Yonghong Song6f1ae8b2019-02-01 16:14:17 -080079 __libbpf_pr = fn;
Wang Nanb3f59d62015-07-01 02:13:52 +000080}
Wang Nan1a5e3fb2015-07-01 02:13:53 +000081
Yonghong Song8461ef82019-02-01 16:14:14 -080082__printf(2, 3)
83void libbpf_print(enum libbpf_print_level level, const char *format, ...)
84{
85 va_list args;
86
Yonghong Song6f1ae8b2019-02-01 16:14:17 -080087 if (!__libbpf_pr)
88 return;
89
Yonghong Song8461ef82019-02-01 16:14:14 -080090 va_start(args, format);
Yonghong Song6f1ae8b2019-02-01 16:14:17 -080091 __libbpf_pr(level, format, args);
Yonghong Song8461ef82019-02-01 16:14:14 -080092 va_end(args);
93}
94
Wang Nan6371ca3b2015-11-06 13:49:37 +000095#define STRERR_BUFSIZE 128
96
Wang Nan6371ca3b2015-11-06 13:49:37 +000097#define CHECK_ERR(action, err, out) do { \
98 err = action; \
99 if (err) \
100 goto out; \
101} while(0)
102
103
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000104/* Copied from tools/perf/util/util.h */
105#ifndef zfree
106# define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
107#endif
108
109#ifndef zclose
110# define zclose(fd) ({ \
111 int ___err = 0; \
112 if ((fd) >= 0) \
113 ___err = close((fd)); \
114 fd = -1; \
115 ___err; })
116#endif
117
118#ifdef HAVE_LIBELF_MMAP_SUPPORT
119# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP
120#else
121# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ
122#endif
123
Song Liu34be16462019-03-11 22:30:38 -0700124static inline __u64 ptr_to_u64(const void *ptr)
125{
126 return (__u64) (unsigned long) ptr;
127}
128
Stanislav Fomichev47eff612018-11-20 17:11:19 -0800129struct bpf_capabilities {
130 /* v4.14: kernel support for program & map names. */
131 __u32 name:1;
Daniel Borkmann8837fe52019-04-24 00:45:56 +0200132 /* v5.2: kernel support for global data sections. */
133 __u32 global_data:1;
Andrii Nakryikod7c4b392019-05-10 14:13:15 -0700134 /* BTF_KIND_FUNC and BTF_KIND_FUNC_PROTO support */
135 __u32 btf_func:1;
136 /* BTF_KIND_VAR and BTF_KIND_DATASEC support */
137 __u32 btf_datasec:1;
Stanislav Fomichev47eff612018-11-20 17:11:19 -0800138};
139
Wang Nana5b8bd42015-07-01 02:14:00 +0000140/*
141 * bpf_prog should be a better name but it has been used in
142 * linux/filter.h.
143 */
144struct bpf_program {
145 /* Index in elf obj file, for relocation use. */
146 int idx;
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700147 char *name;
David Beckettf0307a72018-05-16 14:02:49 -0700148 int prog_ifindex;
Wang Nana5b8bd42015-07-01 02:14:00 +0000149 char *section_name;
Stanislav Fomichev33a2c752018-11-09 08:21:43 -0800150 /* section_name with / replaced by _; makes recursive pinning
151 * in bpf_object__pin_programs easier
152 */
153 char *pin_name;
Wang Nana5b8bd42015-07-01 02:14:00 +0000154 struct bpf_insn *insns;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800155 size_t insns_cnt, main_prog_cnt;
Wang Nan5f44e4c82016-07-13 10:44:01 +0000156 enum bpf_prog_type type;
Wang Nan34090912015-07-01 02:14:02 +0000157
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800158 struct reloc_desc {
159 enum {
160 RELO_LD64,
161 RELO_CALL,
Daniel Borkmannd8599002019-04-09 23:20:13 +0200162 RELO_DATA,
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800163 } type;
Wang Nan34090912015-07-01 02:14:02 +0000164 int insn_idx;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800165 union {
166 int map_idx;
167 int text_off;
168 };
Wang Nan34090912015-07-01 02:14:02 +0000169 } *reloc_desc;
170 int nr_reloc;
Alexei Starovoitovda11b412019-04-01 21:27:47 -0700171 int log_level;
Wang Nan55cffde2015-07-01 02:14:07 +0000172
Wang Nanb5805632015-11-16 12:10:09 +0000173 struct {
174 int nr;
175 int *fds;
176 } instances;
177 bpf_program_prep_t preprocessor;
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000178
179 struct bpf_object *obj;
180 void *priv;
181 bpf_program_clear_priv_t clear_priv;
Andrey Ignatovd7be1432018-03-30 15:08:01 -0700182
183 enum bpf_attach_type expected_attach_type;
Yonghong Song2993e052018-11-19 15:29:16 -0800184 int btf_fd;
185 void *func_info;
186 __u32 func_info_rec_size;
Martin KaFai Lauf0187f02018-12-07 16:42:29 -0800187 __u32 func_info_cnt;
Stanislav Fomichev47eff612018-11-20 17:11:19 -0800188
189 struct bpf_capabilities *caps;
Martin KaFai Lau3d650142018-12-07 16:42:31 -0800190
191 void *line_info;
192 __u32 line_info_rec_size;
193 __u32 line_info_cnt;
Jiong Wang04656192019-05-24 23:25:19 +0100194 __u32 prog_flags;
Wang Nana5b8bd42015-07-01 02:14:00 +0000195};
196
Daniel Borkmannd8599002019-04-09 23:20:13 +0200197enum libbpf_map_type {
198 LIBBPF_MAP_UNSPEC,
199 LIBBPF_MAP_DATA,
200 LIBBPF_MAP_BSS,
201 LIBBPF_MAP_RODATA,
202};
203
204static const char * const libbpf_type_to_btf_name[] = {
205 [LIBBPF_MAP_DATA] = ".data",
206 [LIBBPF_MAP_BSS] = ".bss",
207 [LIBBPF_MAP_RODATA] = ".rodata",
208};
209
Wang Nan9d759a92015-11-27 08:47:35 +0000210struct bpf_map {
211 int fd;
Wang Nan561bbcc2015-11-27 08:47:36 +0000212 char *name;
Andrii Nakryikodb488142019-06-17 12:26:54 -0700213 int sec_idx;
214 size_t sec_offset;
David Beckettf0307a72018-05-16 14:02:49 -0700215 int map_ifindex;
Nikita V. Shirokovaddb9fc2018-11-20 20:55:56 -0800216 int inner_map_fd;
Wang Nan9d759a92015-11-27 08:47:35 +0000217 struct bpf_map_def def;
Martin KaFai Lau5b891af2018-07-24 08:40:21 -0700218 __u32 btf_key_type_id;
219 __u32 btf_value_type_id;
Wang Nan9d759a92015-11-27 08:47:35 +0000220 void *priv;
221 bpf_map_clear_priv_t clear_priv;
Daniel Borkmannd8599002019-04-09 23:20:13 +0200222 enum libbpf_map_type libbpf_type;
223};
224
225struct bpf_secdata {
226 void *rodata;
227 void *data;
Wang Nan9d759a92015-11-27 08:47:35 +0000228};
229
Wang Nan9a208ef2015-07-01 02:14:10 +0000230static LIST_HEAD(bpf_objects_list);
231
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000232struct bpf_object {
Daniel Borkmannd8599002019-04-09 23:20:13 +0200233 char name[BPF_OBJ_NAME_LEN];
Wang Nancb1e5e92015-07-01 02:13:57 +0000234 char license[64];
Yonghong Song438363c2018-10-09 16:14:47 -0700235 __u32 kern_version;
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000236
Wang Nana5b8bd42015-07-01 02:14:00 +0000237 struct bpf_program *programs;
238 size_t nr_programs;
Wang Nan9d759a92015-11-27 08:47:35 +0000239 struct bpf_map *maps;
240 size_t nr_maps;
Andrii Nakryikobf829272019-06-17 12:26:53 -0700241 size_t maps_cap;
Daniel Borkmannd8599002019-04-09 23:20:13 +0200242 struct bpf_secdata sections;
Wang Nan9d759a92015-11-27 08:47:35 +0000243
Wang Nan52d33522015-07-01 02:14:04 +0000244 bool loaded;
Jakub Kicinski9a94f272018-06-28 14:41:38 -0700245 bool has_pseudo_calls;
Wang Nana5b8bd42015-07-01 02:14:00 +0000246
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000247 /*
248 * Information when doing elf related work. Only valid if fd
249 * is valid.
250 */
251 struct {
252 int fd;
Wang Nan6c956392015-07-01 02:13:54 +0000253 void *obj_buf;
254 size_t obj_buf_sz;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000255 Elf *elf;
256 GElf_Ehdr ehdr;
Wang Nanbec7d682015-07-01 02:13:59 +0000257 Elf_Data *symbols;
Daniel Borkmannd8599002019-04-09 23:20:13 +0200258 Elf_Data *data;
259 Elf_Data *rodata;
260 Elf_Data *bss;
Wang Nan77ba9a52015-12-08 02:25:30 +0000261 size_t strtabidx;
Wang Nanb62f06e2015-07-01 02:14:01 +0000262 struct {
263 GElf_Shdr shdr;
264 Elf_Data *data;
265 } *reloc;
266 int nr_reloc;
Wang Nan666810e2016-01-25 09:55:49 +0000267 int maps_shndx;
Andrii Nakryikoabd29c92019-06-17 12:26:56 -0700268 int btf_maps_shndx;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800269 int text_shndx;
Daniel Borkmannd8599002019-04-09 23:20:13 +0200270 int data_shndx;
271 int rodata_shndx;
272 int bss_shndx;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000273 } efile;
Wang Nan9a208ef2015-07-01 02:14:10 +0000274 /*
275 * All loaded bpf_object is linked in a list, which is
276 * hidden to caller. bpf_objects__<func> handlers deal with
277 * all objects.
278 */
279 struct list_head list;
Wang Nan10931d22016-11-26 07:03:26 +0000280
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700281 struct btf *btf;
Yonghong Song2993e052018-11-19 15:29:16 -0800282 struct btf_ext *btf_ext;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700283
Wang Nan10931d22016-11-26 07:03:26 +0000284 void *priv;
285 bpf_object_clear_priv_t clear_priv;
286
Stanislav Fomichev47eff612018-11-20 17:11:19 -0800287 struct bpf_capabilities caps;
288
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000289 char path[];
290};
291#define obj_elf_valid(o) ((o)->efile.elf)
292
Joe Stringer29cd77f2018-10-02 13:35:39 -0700293void bpf_program__unload(struct bpf_program *prog)
Wang Nan55cffde2015-07-01 02:14:07 +0000294{
Wang Nanb5805632015-11-16 12:10:09 +0000295 int i;
296
Wang Nan55cffde2015-07-01 02:14:07 +0000297 if (!prog)
298 return;
299
Wang Nanb5805632015-11-16 12:10:09 +0000300 /*
301 * If the object is opened but the program was never loaded,
302 * it is possible that prog->instances.nr == -1.
303 */
304 if (prog->instances.nr > 0) {
305 for (i = 0; i < prog->instances.nr; i++)
306 zclose(prog->instances.fds[i]);
307 } else if (prog->instances.nr != -1) {
308 pr_warning("Internal error: instances.nr is %d\n",
309 prog->instances.nr);
310 }
311
312 prog->instances.nr = -1;
313 zfree(&prog->instances.fds);
Yonghong Song2993e052018-11-19 15:29:16 -0800314
315 zclose(prog->btf_fd);
316 zfree(&prog->func_info);
Prashant Bhole07a09d12018-12-17 16:57:50 +0900317 zfree(&prog->line_info);
Wang Nan55cffde2015-07-01 02:14:07 +0000318}
319
Wang Nana5b8bd42015-07-01 02:14:00 +0000320static void bpf_program__exit(struct bpf_program *prog)
321{
322 if (!prog)
323 return;
324
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000325 if (prog->clear_priv)
326 prog->clear_priv(prog, prog->priv);
327
328 prog->priv = NULL;
329 prog->clear_priv = NULL;
330
Wang Nan55cffde2015-07-01 02:14:07 +0000331 bpf_program__unload(prog);
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700332 zfree(&prog->name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000333 zfree(&prog->section_name);
Stanislav Fomichev33a2c752018-11-09 08:21:43 -0800334 zfree(&prog->pin_name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000335 zfree(&prog->insns);
Wang Nan34090912015-07-01 02:14:02 +0000336 zfree(&prog->reloc_desc);
337
338 prog->nr_reloc = 0;
Wang Nana5b8bd42015-07-01 02:14:00 +0000339 prog->insns_cnt = 0;
340 prog->idx = -1;
341}
342
Stanislav Fomichev33a2c752018-11-09 08:21:43 -0800343static char *__bpf_program__pin_name(struct bpf_program *prog)
344{
345 char *name, *p;
346
347 name = p = strdup(prog->section_name);
348 while ((p = strchr(p, '/')))
349 *p = '_';
350
351 return name;
352}
353
Wang Nana5b8bd42015-07-01 02:14:00 +0000354static int
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700355bpf_program__init(void *data, size_t size, char *section_name, int idx,
356 struct bpf_program *prog)
Wang Nana5b8bd42015-07-01 02:14:00 +0000357{
Andrii Nakryiko8ca990c2019-05-29 10:36:03 -0700358 const size_t bpf_insn_sz = sizeof(struct bpf_insn);
359
360 if (size == 0 || size % bpf_insn_sz) {
361 pr_warning("corrupted section '%s', size: %zu\n",
362 section_name, size);
Wang Nana5b8bd42015-07-01 02:14:00 +0000363 return -EINVAL;
364 }
365
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -0800366 memset(prog, 0, sizeof(*prog));
Wang Nana5b8bd42015-07-01 02:14:00 +0000367
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700368 prog->section_name = strdup(section_name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000369 if (!prog->section_name) {
Jesper Dangaard Brouer077c0662018-02-08 12:48:17 +0100370 pr_warning("failed to alloc name for prog under section(%d) %s\n",
371 idx, section_name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000372 goto errout;
373 }
374
Stanislav Fomichev33a2c752018-11-09 08:21:43 -0800375 prog->pin_name = __bpf_program__pin_name(prog);
376 if (!prog->pin_name) {
377 pr_warning("failed to alloc pin name for prog under section(%d) %s\n",
378 idx, section_name);
379 goto errout;
380 }
381
Wang Nana5b8bd42015-07-01 02:14:00 +0000382 prog->insns = malloc(size);
383 if (!prog->insns) {
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700384 pr_warning("failed to alloc insns for prog under section %s\n",
385 section_name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000386 goto errout;
387 }
Andrii Nakryiko8ca990c2019-05-29 10:36:03 -0700388 prog->insns_cnt = size / bpf_insn_sz;
389 memcpy(prog->insns, data, size);
Wang Nana5b8bd42015-07-01 02:14:00 +0000390 prog->idx = idx;
Wang Nanb5805632015-11-16 12:10:09 +0000391 prog->instances.fds = NULL;
392 prog->instances.nr = -1;
Nikita V. Shirokov47ae7e32018-11-23 12:58:12 -0800393 prog->type = BPF_PROG_TYPE_UNSPEC;
Yonghong Song2993e052018-11-19 15:29:16 -0800394 prog->btf_fd = -1;
Wang Nana5b8bd42015-07-01 02:14:00 +0000395
396 return 0;
397errout:
398 bpf_program__exit(prog);
399 return -ENOMEM;
400}
401
402static int
403bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700404 char *section_name, int idx)
Wang Nana5b8bd42015-07-01 02:14:00 +0000405{
406 struct bpf_program prog, *progs;
407 int nr_progs, err;
408
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700409 err = bpf_program__init(data, size, section_name, idx, &prog);
Wang Nana5b8bd42015-07-01 02:14:00 +0000410 if (err)
411 return err;
412
Stanislav Fomichev47eff612018-11-20 17:11:19 -0800413 prog.caps = &obj->caps;
Wang Nana5b8bd42015-07-01 02:14:00 +0000414 progs = obj->programs;
415 nr_progs = obj->nr_programs;
416
Jakub Kicinski531b0142018-07-10 14:43:05 -0700417 progs = reallocarray(progs, nr_progs + 1, sizeof(progs[0]));
Wang Nana5b8bd42015-07-01 02:14:00 +0000418 if (!progs) {
419 /*
420 * In this case the original obj->programs
421 * is still valid, so don't need special treat for
422 * bpf_close_object().
423 */
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700424 pr_warning("failed to alloc a new program under section '%s'\n",
425 section_name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000426 bpf_program__exit(&prog);
427 return -ENOMEM;
428 }
429
430 pr_debug("found program %s\n", prog.section_name);
431 obj->programs = progs;
432 obj->nr_programs = nr_progs + 1;
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000433 prog.obj = obj;
Wang Nana5b8bd42015-07-01 02:14:00 +0000434 progs[nr_progs] = prog;
435 return 0;
436}
437
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700438static int
439bpf_object__init_prog_names(struct bpf_object *obj)
440{
441 Elf_Data *symbols = obj->efile.symbols;
442 struct bpf_program *prog;
443 size_t pi, si;
444
445 for (pi = 0; pi < obj->nr_programs; pi++) {
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800446 const char *name = NULL;
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700447
448 prog = &obj->programs[pi];
449
450 for (si = 0; si < symbols->d_size / sizeof(GElf_Sym) && !name;
451 si++) {
452 GElf_Sym sym;
453
454 if (!gelf_getsym(symbols, si, &sym))
455 continue;
456 if (sym.st_shndx != prog->idx)
457 continue;
Roman Gushchinfe4d44b2017-12-13 15:18:52 +0000458 if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL)
459 continue;
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700460
461 name = elf_strptr(obj->efile.elf,
462 obj->efile.strtabidx,
463 sym.st_name);
464 if (!name) {
465 pr_warning("failed to get sym name string for prog %s\n",
466 prog->section_name);
467 return -LIBBPF_ERRNO__LIBELF;
468 }
469 }
470
Jakub Kicinski9a94f272018-06-28 14:41:38 -0700471 if (!name && prog->idx == obj->efile.text_shndx)
472 name = ".text";
473
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700474 if (!name) {
475 pr_warning("failed to find sym for prog %s\n",
476 prog->section_name);
477 return -EINVAL;
478 }
Jakub Kicinski9a94f272018-06-28 14:41:38 -0700479
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700480 prog->name = strdup(name);
481 if (!prog->name) {
482 pr_warning("failed to allocate memory for prog sym %s\n",
483 name);
484 return -ENOMEM;
485 }
486 }
487
488 return 0;
489}
490
Wang Nan6c956392015-07-01 02:13:54 +0000491static struct bpf_object *bpf_object__new(const char *path,
492 void *obj_buf,
493 size_t obj_buf_sz)
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000494{
495 struct bpf_object *obj;
Daniel Borkmannd8599002019-04-09 23:20:13 +0200496 char *end;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000497
498 obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
499 if (!obj) {
500 pr_warning("alloc memory failed for %s\n", path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000501 return ERR_PTR(-ENOMEM);
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000502 }
503
504 strcpy(obj->path, path);
Daniel Borkmannd8599002019-04-09 23:20:13 +0200505 /* Using basename() GNU version which doesn't modify arg. */
Andrii Nakryiko399dc652019-05-29 10:36:11 -0700506 strncpy(obj->name, basename((void *)path), sizeof(obj->name) - 1);
Daniel Borkmannd8599002019-04-09 23:20:13 +0200507 end = strchr(obj->name, '.');
508 if (end)
509 *end = 0;
Wang Nan6c956392015-07-01 02:13:54 +0000510
Daniel Borkmannd8599002019-04-09 23:20:13 +0200511 obj->efile.fd = -1;
Wang Nan6c956392015-07-01 02:13:54 +0000512 /*
Andrii Nakryiko76e10222019-05-29 10:36:10 -0700513 * Caller of this function should also call
Wang Nan6c956392015-07-01 02:13:54 +0000514 * bpf_object__elf_finish() after data collection to return
515 * obj_buf to user. If not, we should duplicate the buffer to
516 * avoid user freeing them before elf finish.
517 */
518 obj->efile.obj_buf = obj_buf;
519 obj->efile.obj_buf_sz = obj_buf_sz;
Wang Nan666810e2016-01-25 09:55:49 +0000520 obj->efile.maps_shndx = -1;
Andrii Nakryikoabd29c92019-06-17 12:26:56 -0700521 obj->efile.btf_maps_shndx = -1;
Daniel Borkmannd8599002019-04-09 23:20:13 +0200522 obj->efile.data_shndx = -1;
523 obj->efile.rodata_shndx = -1;
524 obj->efile.bss_shndx = -1;
Wang Nan6c956392015-07-01 02:13:54 +0000525
Wang Nan52d33522015-07-01 02:14:04 +0000526 obj->loaded = false;
Wang Nan9a208ef2015-07-01 02:14:10 +0000527
528 INIT_LIST_HEAD(&obj->list);
529 list_add(&obj->list, &bpf_objects_list);
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000530 return obj;
531}
532
533static void bpf_object__elf_finish(struct bpf_object *obj)
534{
535 if (!obj_elf_valid(obj))
536 return;
537
538 if (obj->efile.elf) {
539 elf_end(obj->efile.elf);
540 obj->efile.elf = NULL;
541 }
Wang Nanbec7d682015-07-01 02:13:59 +0000542 obj->efile.symbols = NULL;
Daniel Borkmannd8599002019-04-09 23:20:13 +0200543 obj->efile.data = NULL;
544 obj->efile.rodata = NULL;
545 obj->efile.bss = NULL;
Wang Nanb62f06e2015-07-01 02:14:01 +0000546
547 zfree(&obj->efile.reloc);
548 obj->efile.nr_reloc = 0;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000549 zclose(obj->efile.fd);
Wang Nan6c956392015-07-01 02:13:54 +0000550 obj->efile.obj_buf = NULL;
551 obj->efile.obj_buf_sz = 0;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000552}
553
554static int bpf_object__elf_init(struct bpf_object *obj)
555{
556 int err = 0;
557 GElf_Ehdr *ep;
558
559 if (obj_elf_valid(obj)) {
560 pr_warning("elf init: internal error\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +0000561 return -LIBBPF_ERRNO__LIBELF;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000562 }
563
Wang Nan6c956392015-07-01 02:13:54 +0000564 if (obj->efile.obj_buf_sz > 0) {
565 /*
566 * obj_buf should have been validated by
567 * bpf_object__open_buffer().
568 */
569 obj->efile.elf = elf_memory(obj->efile.obj_buf,
570 obj->efile.obj_buf_sz);
571 } else {
572 obj->efile.fd = open(obj->path, O_RDONLY);
573 if (obj->efile.fd < 0) {
Andrii Nakryikobe5c5d42019-05-29 10:36:04 -0700574 char errmsg[STRERR_BUFSIZE], *cp;
Thomas Richter1ce6a9f2018-07-30 10:53:23 +0200575
Andrii Nakryikobe5c5d42019-05-29 10:36:04 -0700576 err = -errno;
577 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
Thomas Richter1ce6a9f2018-07-30 10:53:23 +0200578 pr_warning("failed to open %s: %s\n", obj->path, cp);
Andrii Nakryikobe5c5d42019-05-29 10:36:04 -0700579 return err;
Wang Nan6c956392015-07-01 02:13:54 +0000580 }
581
582 obj->efile.elf = elf_begin(obj->efile.fd,
Andrii Nakryiko76e10222019-05-29 10:36:10 -0700583 LIBBPF_ELF_C_READ_MMAP, NULL);
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000584 }
585
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000586 if (!obj->efile.elf) {
Andrii Nakryiko399dc652019-05-29 10:36:11 -0700587 pr_warning("failed to open %s as ELF file\n", obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000588 err = -LIBBPF_ERRNO__LIBELF;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000589 goto errout;
590 }
591
592 if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) {
Andrii Nakryiko399dc652019-05-29 10:36:11 -0700593 pr_warning("failed to get EHDR from %s\n", obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000594 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000595 goto errout;
596 }
597 ep = &obj->efile.ehdr;
598
Wang Nan9b161372016-07-18 06:01:08 +0000599 /* Old LLVM set e_machine to EM_NONE */
Andrii Nakryiko76e10222019-05-29 10:36:10 -0700600 if (ep->e_type != ET_REL ||
601 (ep->e_machine && ep->e_machine != EM_BPF)) {
602 pr_warning("%s is not an eBPF object file\n", obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000603 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000604 goto errout;
605 }
606
607 return 0;
608errout:
609 bpf_object__elf_finish(obj);
610 return err;
611}
612
Andrii Nakryiko12ef5632019-05-29 10:36:05 -0700613static int bpf_object__check_endianness(struct bpf_object *obj)
Wang Nancc4228d2015-07-01 02:13:55 +0000614{
Andrii Nakryiko12ef5632019-05-29 10:36:05 -0700615#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
616 if (obj->efile.ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
617 return 0;
618#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
619 if (obj->efile.ehdr.e_ident[EI_DATA] == ELFDATA2MSB)
620 return 0;
621#else
622# error "Unrecognized __BYTE_ORDER__"
623#endif
624 pr_warning("endianness mismatch.\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +0000625 return -LIBBPF_ERRNO__ENDIAN;
Wang Nancc4228d2015-07-01 02:13:55 +0000626}
627
Wang Nancb1e5e92015-07-01 02:13:57 +0000628static int
Andrii Nakryiko399dc652019-05-29 10:36:11 -0700629bpf_object__init_license(struct bpf_object *obj, void *data, size_t size)
Wang Nancb1e5e92015-07-01 02:13:57 +0000630{
Andrii Nakryiko399dc652019-05-29 10:36:11 -0700631 memcpy(obj->license, data, min(size, sizeof(obj->license) - 1));
Wang Nancb1e5e92015-07-01 02:13:57 +0000632 pr_debug("license of %s is %s\n", obj->path, obj->license);
633 return 0;
634}
635
636static int
Andrii Nakryiko399dc652019-05-29 10:36:11 -0700637bpf_object__init_kversion(struct bpf_object *obj, void *data, size_t size)
Wang Nancb1e5e92015-07-01 02:13:57 +0000638{
Yonghong Song438363c2018-10-09 16:14:47 -0700639 __u32 kver;
Wang Nancb1e5e92015-07-01 02:13:57 +0000640
641 if (size != sizeof(kver)) {
642 pr_warning("invalid kver section in %s\n", obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000643 return -LIBBPF_ERRNO__FORMAT;
Wang Nancb1e5e92015-07-01 02:13:57 +0000644 }
645 memcpy(&kver, data, sizeof(kver));
646 obj->kern_version = kver;
Andrii Nakryiko399dc652019-05-29 10:36:11 -0700647 pr_debug("kernel version of %s is %x\n", obj->path, obj->kern_version);
Wang Nancb1e5e92015-07-01 02:13:57 +0000648 return 0;
649}
650
Eric Leblond4708bbd2016-11-15 04:05:47 +0000651static int compare_bpf_map(const void *_a, const void *_b)
652{
653 const struct bpf_map *a = _a;
654 const struct bpf_map *b = _b;
655
Andrii Nakryikodb488142019-06-17 12:26:54 -0700656 if (a->sec_idx != b->sec_idx)
657 return a->sec_idx - b->sec_idx;
658 return a->sec_offset - b->sec_offset;
Eric Leblond4708bbd2016-11-15 04:05:47 +0000659}
660
Nikita V. Shirokovaddb9fc2018-11-20 20:55:56 -0800661static bool bpf_map_type__is_map_in_map(enum bpf_map_type type)
662{
663 if (type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
664 type == BPF_MAP_TYPE_HASH_OF_MAPS)
665 return true;
666 return false;
667}
668
Daniel Borkmann1713d682019-04-09 23:20:14 +0200669static int bpf_object_search_section_size(const struct bpf_object *obj,
670 const char *name, size_t *d_size)
671{
672 const GElf_Ehdr *ep = &obj->efile.ehdr;
673 Elf *elf = obj->efile.elf;
674 Elf_Scn *scn = NULL;
675 int idx = 0;
676
677 while ((scn = elf_nextscn(elf, scn)) != NULL) {
678 const char *sec_name;
679 Elf_Data *data;
680 GElf_Shdr sh;
681
682 idx++;
683 if (gelf_getshdr(scn, &sh) != &sh) {
684 pr_warning("failed to get section(%d) header from %s\n",
685 idx, obj->path);
686 return -EIO;
687 }
688
689 sec_name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
690 if (!sec_name) {
691 pr_warning("failed to get section(%d) name from %s\n",
692 idx, obj->path);
693 return -EIO;
694 }
695
696 if (strcmp(name, sec_name))
697 continue;
698
699 data = elf_getdata(scn, 0);
700 if (!data) {
701 pr_warning("failed to get section(%d) data from %s(%s)\n",
702 idx, name, obj->path);
703 return -EIO;
704 }
705
706 *d_size = data->d_size;
707 return 0;
708 }
709
710 return -ENOENT;
711}
712
713int bpf_object__section_size(const struct bpf_object *obj, const char *name,
714 __u32 *size)
715{
716 int ret = -ENOENT;
717 size_t d_size;
718
719 *size = 0;
720 if (!name) {
721 return -EINVAL;
722 } else if (!strcmp(name, ".data")) {
723 if (obj->efile.data)
724 *size = obj->efile.data->d_size;
725 } else if (!strcmp(name, ".bss")) {
726 if (obj->efile.bss)
727 *size = obj->efile.bss->d_size;
728 } else if (!strcmp(name, ".rodata")) {
729 if (obj->efile.rodata)
730 *size = obj->efile.rodata->d_size;
731 } else {
732 ret = bpf_object_search_section_size(obj, name, &d_size);
733 if (!ret)
734 *size = d_size;
735 }
736
737 return *size ? 0 : ret;
738}
739
740int bpf_object__variable_offset(const struct bpf_object *obj, const char *name,
741 __u32 *off)
742{
743 Elf_Data *symbols = obj->efile.symbols;
744 const char *sname;
745 size_t si;
746
747 if (!name || !off)
748 return -EINVAL;
749
750 for (si = 0; si < symbols->d_size / sizeof(GElf_Sym); si++) {
751 GElf_Sym sym;
752
753 if (!gelf_getsym(symbols, si, &sym))
754 continue;
755 if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL ||
756 GELF_ST_TYPE(sym.st_info) != STT_OBJECT)
757 continue;
758
759 sname = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
760 sym.st_name);
761 if (!sname) {
762 pr_warning("failed to get sym name string for var %s\n",
763 name);
764 return -EIO;
765 }
766 if (strcmp(name, sname) == 0) {
767 *off = sym.st_value;
768 return 0;
769 }
770 }
771
772 return -ENOENT;
773}
774
Andrii Nakryikobf829272019-06-17 12:26:53 -0700775static struct bpf_map *bpf_object__add_map(struct bpf_object *obj)
Daniel Borkmannd8599002019-04-09 23:20:13 +0200776{
Andrii Nakryikobf829272019-06-17 12:26:53 -0700777 struct bpf_map *new_maps;
778 size_t new_cap;
779 int i;
780
781 if (obj->nr_maps < obj->maps_cap)
782 return &obj->maps[obj->nr_maps++];
783
Ivan Khoronzhuk95064972019-06-26 13:38:37 +0300784 new_cap = max((size_t)4, obj->maps_cap * 3 / 2);
Andrii Nakryikobf829272019-06-17 12:26:53 -0700785 new_maps = realloc(obj->maps, new_cap * sizeof(*obj->maps));
786 if (!new_maps) {
787 pr_warning("alloc maps for object failed\n");
788 return ERR_PTR(-ENOMEM);
789 }
790
791 obj->maps_cap = new_cap;
792 obj->maps = new_maps;
793
794 /* zero out new maps */
795 memset(obj->maps + obj->nr_maps, 0,
796 (obj->maps_cap - obj->nr_maps) * sizeof(*obj->maps));
797 /*
798 * fill all fd with -1 so won't close incorrect fd (fd=0 is stdin)
799 * when failure (zclose won't close negative fd)).
800 */
801 for (i = obj->nr_maps; i < obj->maps_cap; i++) {
802 obj->maps[i].fd = -1;
803 obj->maps[i].inner_map_fd = -1;
804 }
805
806 return &obj->maps[obj->nr_maps++];
Daniel Borkmannd8599002019-04-09 23:20:13 +0200807}
808
809static int
Andrii Nakryikobf829272019-06-17 12:26:53 -0700810bpf_object__init_internal_map(struct bpf_object *obj, enum libbpf_map_type type,
Andrii Nakryikodb488142019-06-17 12:26:54 -0700811 int sec_idx, Elf_Data *data, void **data_buff)
Daniel Borkmannd8599002019-04-09 23:20:13 +0200812{
Daniel Borkmannd8599002019-04-09 23:20:13 +0200813 char map_name[BPF_OBJ_NAME_LEN];
Andrii Nakryikobf829272019-06-17 12:26:53 -0700814 struct bpf_map_def *def;
815 struct bpf_map *map;
816
817 map = bpf_object__add_map(obj);
818 if (IS_ERR(map))
819 return PTR_ERR(map);
Daniel Borkmannd8599002019-04-09 23:20:13 +0200820
821 map->libbpf_type = type;
Andrii Nakryikodb488142019-06-17 12:26:54 -0700822 map->sec_idx = sec_idx;
823 map->sec_offset = 0;
Daniel Borkmannd8599002019-04-09 23:20:13 +0200824 snprintf(map_name, sizeof(map_name), "%.8s%.7s", obj->name,
825 libbpf_type_to_btf_name[type]);
826 map->name = strdup(map_name);
827 if (!map->name) {
828 pr_warning("failed to alloc map name\n");
829 return -ENOMEM;
830 }
Andrii Nakryikodb488142019-06-17 12:26:54 -0700831 pr_debug("map '%s' (global data): at sec_idx %d, offset %zu.\n",
832 map_name, map->sec_idx, map->sec_offset);
Daniel Borkmannd8599002019-04-09 23:20:13 +0200833
Andrii Nakryikobf829272019-06-17 12:26:53 -0700834 def = &map->def;
Daniel Borkmannd8599002019-04-09 23:20:13 +0200835 def->type = BPF_MAP_TYPE_ARRAY;
836 def->key_size = sizeof(int);
837 def->value_size = data->d_size;
838 def->max_entries = 1;
Andrii Nakryiko399dc652019-05-29 10:36:11 -0700839 def->map_flags = type == LIBBPF_MAP_RODATA ? BPF_F_RDONLY_PROG : 0;
Daniel Borkmannd8599002019-04-09 23:20:13 +0200840 if (data_buff) {
841 *data_buff = malloc(data->d_size);
842 if (!*data_buff) {
843 zfree(&map->name);
844 pr_warning("failed to alloc map content buffer\n");
845 return -ENOMEM;
846 }
847 memcpy(*data_buff, data->d_buf, data->d_size);
848 }
849
Andrii Nakryikoe1d1dc42019-04-16 11:47:17 -0700850 pr_debug("map %td is \"%s\"\n", map - obj->maps, map->name);
Daniel Borkmannd8599002019-04-09 23:20:13 +0200851 return 0;
852}
853
Andrii Nakryikobf829272019-06-17 12:26:53 -0700854static int bpf_object__init_global_data_maps(struct bpf_object *obj)
Eric Leblond4708bbd2016-11-15 04:05:47 +0000855{
Andrii Nakryikobf829272019-06-17 12:26:53 -0700856 int err;
857
858 if (!obj->caps.global_data)
859 return 0;
860 /*
861 * Populate obj->maps with libbpf internal maps.
862 */
863 if (obj->efile.data_shndx >= 0) {
864 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_DATA,
Andrii Nakryikodb488142019-06-17 12:26:54 -0700865 obj->efile.data_shndx,
Andrii Nakryikobf829272019-06-17 12:26:53 -0700866 obj->efile.data,
867 &obj->sections.data);
868 if (err)
869 return err;
870 }
871 if (obj->efile.rodata_shndx >= 0) {
872 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_RODATA,
Andrii Nakryikodb488142019-06-17 12:26:54 -0700873 obj->efile.rodata_shndx,
Andrii Nakryikobf829272019-06-17 12:26:53 -0700874 obj->efile.rodata,
875 &obj->sections.rodata);
876 if (err)
877 return err;
878 }
879 if (obj->efile.bss_shndx >= 0) {
880 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_BSS,
Andrii Nakryikodb488142019-06-17 12:26:54 -0700881 obj->efile.bss_shndx,
Andrii Nakryikobf829272019-06-17 12:26:53 -0700882 obj->efile.bss, NULL);
883 if (err)
884 return err;
885 }
886 return 0;
887}
888
889static int bpf_object__init_user_maps(struct bpf_object *obj, bool strict)
890{
Eric Leblond4708bbd2016-11-15 04:05:47 +0000891 Elf_Data *symbols = obj->efile.symbols;
Andrii Nakryikobf829272019-06-17 12:26:53 -0700892 int i, map_def_sz = 0, nr_maps = 0, nr_syms;
Daniel Borkmannd8599002019-04-09 23:20:13 +0200893 Elf_Data *data = NULL;
Andrii Nakryikobf829272019-06-17 12:26:53 -0700894 Elf_Scn *scn;
895
896 if (obj->efile.maps_shndx < 0)
897 return 0;
Eric Leblond4708bbd2016-11-15 04:05:47 +0000898
Eric Leblond4708bbd2016-11-15 04:05:47 +0000899 if (!symbols)
900 return -EINVAL;
901
Andrii Nakryikobf829272019-06-17 12:26:53 -0700902 scn = elf_getscn(obj->efile.elf, obj->efile.maps_shndx);
903 if (scn)
904 data = elf_getdata(scn, NULL);
905 if (!scn || !data) {
906 pr_warning("failed to get Elf_Data from map section %d\n",
907 obj->efile.maps_shndx);
908 return -EINVAL;
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000909 }
910
Eric Leblond4708bbd2016-11-15 04:05:47 +0000911 /*
912 * Count number of maps. Each map has a name.
913 * Array of maps is not supported: only the first element is
914 * considered.
915 *
916 * TODO: Detect array of map and report error.
917 */
Andrii Nakryikobf829272019-06-17 12:26:53 -0700918 nr_syms = symbols->d_size / sizeof(GElf_Sym);
919 for (i = 0; i < nr_syms; i++) {
Eric Leblond4708bbd2016-11-15 04:05:47 +0000920 GElf_Sym sym;
921
922 if (!gelf_getsym(symbols, i, &sym))
923 continue;
924 if (sym.st_shndx != obj->efile.maps_shndx)
925 continue;
926 nr_maps++;
927 }
Craig Gallekb13c5c12017-10-05 10:41:57 -0400928 /* Assume equally sized map definitions */
Andrii Nakryikobf829272019-06-17 12:26:53 -0700929 pr_debug("maps in %s: %d maps in %zd bytes\n",
930 obj->path, nr_maps, data->d_size);
Daniel Borkmann4f8827d2019-04-24 00:45:57 +0200931
Andrii Nakryikobf829272019-06-17 12:26:53 -0700932 map_def_sz = data->d_size / nr_maps;
933 if (!data->d_size || (data->d_size % nr_maps) != 0) {
934 pr_warning("unable to determine map definition size "
935 "section %s, %d maps in %zd bytes\n",
936 obj->path, nr_maps, data->d_size);
937 return -EINVAL;
Craig Gallekb13c5c12017-10-05 10:41:57 -0400938 }
939
Andrii Nakryikobf829272019-06-17 12:26:53 -0700940 /* Fill obj->maps using data in "maps" section. */
941 for (i = 0; i < nr_syms; i++) {
Wang Nan561bbcc2015-11-27 08:47:36 +0000942 GElf_Sym sym;
Wang Nan561bbcc2015-11-27 08:47:36 +0000943 const char *map_name;
Eric Leblond4708bbd2016-11-15 04:05:47 +0000944 struct bpf_map_def *def;
Andrii Nakryikobf829272019-06-17 12:26:53 -0700945 struct bpf_map *map;
Wang Nan561bbcc2015-11-27 08:47:36 +0000946
947 if (!gelf_getsym(symbols, i, &sym))
948 continue;
Wang Nan666810e2016-01-25 09:55:49 +0000949 if (sym.st_shndx != obj->efile.maps_shndx)
Wang Nan561bbcc2015-11-27 08:47:36 +0000950 continue;
951
Andrii Nakryikobf829272019-06-17 12:26:53 -0700952 map = bpf_object__add_map(obj);
953 if (IS_ERR(map))
954 return PTR_ERR(map);
955
956 map_name = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
Wang Nan561bbcc2015-11-27 08:47:36 +0000957 sym.st_name);
Andrii Nakryikoc51829b2019-05-29 10:36:06 -0700958 if (!map_name) {
959 pr_warning("failed to get map #%d name sym string for obj %s\n",
Andrii Nakryikobf829272019-06-17 12:26:53 -0700960 i, obj->path);
Andrii Nakryikoc51829b2019-05-29 10:36:06 -0700961 return -LIBBPF_ERRNO__FORMAT;
962 }
Daniel Borkmannd8599002019-04-09 23:20:13 +0200963
Andrii Nakryikobf829272019-06-17 12:26:53 -0700964 map->libbpf_type = LIBBPF_MAP_UNSPEC;
Andrii Nakryikodb488142019-06-17 12:26:54 -0700965 map->sec_idx = sym.st_shndx;
966 map->sec_offset = sym.st_value;
967 pr_debug("map '%s' (legacy): at sec_idx %d, offset %zu.\n",
968 map_name, map->sec_idx, map->sec_offset);
Craig Gallekb13c5c12017-10-05 10:41:57 -0400969 if (sym.st_value + map_def_sz > data->d_size) {
Eric Leblond4708bbd2016-11-15 04:05:47 +0000970 pr_warning("corrupted maps section in %s: last map \"%s\" too small\n",
971 obj->path, map_name);
972 return -EINVAL;
Wang Nan561bbcc2015-11-27 08:47:36 +0000973 }
Eric Leblond4708bbd2016-11-15 04:05:47 +0000974
Andrii Nakryikobf829272019-06-17 12:26:53 -0700975 map->name = strdup(map_name);
976 if (!map->name) {
Wang Nan973170e2015-12-08 02:25:29 +0000977 pr_warning("failed to alloc map name\n");
978 return -ENOMEM;
979 }
Andrii Nakryikobf829272019-06-17 12:26:53 -0700980 pr_debug("map %d is \"%s\"\n", i, map->name);
Eric Leblond4708bbd2016-11-15 04:05:47 +0000981 def = (struct bpf_map_def *)(data->d_buf + sym.st_value);
Craig Gallekb13c5c12017-10-05 10:41:57 -0400982 /*
983 * If the definition of the map in the object file fits in
984 * bpf_map_def, copy it. Any extra fields in our version
985 * of bpf_map_def will default to zero as a result of the
986 * calloc above.
987 */
988 if (map_def_sz <= sizeof(struct bpf_map_def)) {
Andrii Nakryikobf829272019-06-17 12:26:53 -0700989 memcpy(&map->def, def, map_def_sz);
Craig Gallekb13c5c12017-10-05 10:41:57 -0400990 } else {
991 /*
992 * Here the map structure being read is bigger than what
993 * we expect, truncate if the excess bits are all zero.
994 * If they are not zero, reject this map as
995 * incompatible.
996 */
997 char *b;
998 for (b = ((char *)def) + sizeof(struct bpf_map_def);
999 b < ((char *)def) + map_def_sz; b++) {
1000 if (*b != 0) {
1001 pr_warning("maps section in %s: \"%s\" "
1002 "has unrecognized, non-zero "
1003 "options\n",
1004 obj->path, map_name);
John Fastabendc034a172018-10-15 11:19:55 -07001005 if (strict)
1006 return -EINVAL;
Craig Gallekb13c5c12017-10-05 10:41:57 -04001007 }
1008 }
Andrii Nakryikobf829272019-06-17 12:26:53 -07001009 memcpy(&map->def, def, sizeof(struct bpf_map_def));
Craig Gallekb13c5c12017-10-05 10:41:57 -04001010 }
Wang Nan561bbcc2015-11-27 08:47:36 +00001011 }
Andrii Nakryikobf829272019-06-17 12:26:53 -07001012 return 0;
1013}
Eric Leblond4708bbd2016-11-15 04:05:47 +00001014
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001015static const struct btf_type *skip_mods_and_typedefs(const struct btf *btf,
1016 __u32 id)
1017{
1018 const struct btf_type *t = btf__type_by_id(btf, id);
1019
1020 while (true) {
1021 switch (BTF_INFO_KIND(t->info)) {
1022 case BTF_KIND_VOLATILE:
1023 case BTF_KIND_CONST:
1024 case BTF_KIND_RESTRICT:
1025 case BTF_KIND_TYPEDEF:
1026 t = btf__type_by_id(btf, t->type);
1027 break;
1028 default:
1029 return t;
1030 }
1031 }
1032}
1033
Andrii Nakryikoef99b022019-07-05 08:50:09 -07001034/*
1035 * Fetch integer attribute of BTF map definition. Such attributes are
1036 * represented using a pointer to an array, in which dimensionality of array
1037 * encodes specified integer value. E.g., int (*type)[BPF_MAP_TYPE_ARRAY];
1038 * encodes `type => BPF_MAP_TYPE_ARRAY` key/value pair completely using BTF
1039 * type definition, while using only sizeof(void *) space in ELF data section.
1040 */
1041static bool get_map_field_int(const char *map_name, const struct btf *btf,
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001042 const struct btf_type *def,
Andrii Nakryikoef99b022019-07-05 08:50:09 -07001043 const struct btf_member *m, __u32 *res) {
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001044 const struct btf_type *t = skip_mods_and_typedefs(btf, m->type);
1045 const char *name = btf__name_by_offset(btf, m->name_off);
Andrii Nakryikoef99b022019-07-05 08:50:09 -07001046 const struct btf_array *arr_info;
1047 const struct btf_type *arr_t;
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001048
Andrii Nakryikoef99b022019-07-05 08:50:09 -07001049 if (BTF_INFO_KIND(t->info) != BTF_KIND_PTR) {
1050 pr_warning("map '%s': attr '%s': expected PTR, got %u.\n",
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001051 map_name, name, BTF_INFO_KIND(t->info));
1052 return false;
1053 }
Eric Leblond4708bbd2016-11-15 04:05:47 +00001054
Andrii Nakryikoef99b022019-07-05 08:50:09 -07001055 arr_t = btf__type_by_id(btf, t->type);
1056 if (!arr_t) {
1057 pr_warning("map '%s': attr '%s': type [%u] not found.\n",
1058 map_name, name, t->type);
1059 return false;
1060 }
1061 if (BTF_INFO_KIND(arr_t->info) != BTF_KIND_ARRAY) {
1062 pr_warning("map '%s': attr '%s': expected ARRAY, got %u.\n",
1063 map_name, name, BTF_INFO_KIND(arr_t->info));
1064 return false;
1065 }
1066 arr_info = (const void *)(arr_t + 1);
1067 *res = arr_info->nelems;
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001068 return true;
1069}
Daniel Borkmann8837fe52019-04-24 00:45:56 +02001070
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001071static int bpf_object__init_user_btf_map(struct bpf_object *obj,
1072 const struct btf_type *sec,
1073 int var_idx, int sec_idx,
1074 const Elf_Data *data, bool strict)
1075{
1076 const struct btf_type *var, *def, *t;
1077 const struct btf_var_secinfo *vi;
1078 const struct btf_var *var_extra;
1079 const struct btf_member *m;
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001080 const char *map_name;
1081 struct bpf_map *map;
1082 int vlen, i;
1083
1084 vi = (const struct btf_var_secinfo *)(const void *)(sec + 1) + var_idx;
1085 var = btf__type_by_id(obj->btf, vi->type);
1086 var_extra = (const void *)(var + 1);
1087 map_name = btf__name_by_offset(obj->btf, var->name_off);
1088 vlen = BTF_INFO_VLEN(var->info);
1089
1090 if (map_name == NULL || map_name[0] == '\0') {
1091 pr_warning("map #%d: empty name.\n", var_idx);
1092 return -EINVAL;
1093 }
1094 if ((__u64)vi->offset + vi->size > data->d_size) {
1095 pr_warning("map '%s' BTF data is corrupted.\n", map_name);
1096 return -EINVAL;
1097 }
1098 if (BTF_INFO_KIND(var->info) != BTF_KIND_VAR) {
1099 pr_warning("map '%s': unexpected var kind %u.\n",
1100 map_name, BTF_INFO_KIND(var->info));
1101 return -EINVAL;
1102 }
1103 if (var_extra->linkage != BTF_VAR_GLOBAL_ALLOCATED &&
1104 var_extra->linkage != BTF_VAR_STATIC) {
1105 pr_warning("map '%s': unsupported var linkage %u.\n",
1106 map_name, var_extra->linkage);
1107 return -EOPNOTSUPP;
1108 }
1109
1110 def = skip_mods_and_typedefs(obj->btf, var->type);
1111 if (BTF_INFO_KIND(def->info) != BTF_KIND_STRUCT) {
1112 pr_warning("map '%s': unexpected def kind %u.\n",
1113 map_name, BTF_INFO_KIND(var->info));
1114 return -EINVAL;
1115 }
1116 if (def->size > vi->size) {
1117 pr_warning("map '%s': invalid def size.\n", map_name);
1118 return -EINVAL;
1119 }
1120
1121 map = bpf_object__add_map(obj);
1122 if (IS_ERR(map))
1123 return PTR_ERR(map);
1124 map->name = strdup(map_name);
1125 if (!map->name) {
1126 pr_warning("map '%s': failed to alloc map name.\n", map_name);
1127 return -ENOMEM;
1128 }
1129 map->libbpf_type = LIBBPF_MAP_UNSPEC;
1130 map->def.type = BPF_MAP_TYPE_UNSPEC;
1131 map->sec_idx = sec_idx;
1132 map->sec_offset = vi->offset;
1133 pr_debug("map '%s': at sec_idx %d, offset %zu.\n",
1134 map_name, map->sec_idx, map->sec_offset);
1135
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001136 vlen = BTF_INFO_VLEN(def->info);
1137 m = (const void *)(def + 1);
1138 for (i = 0; i < vlen; i++, m++) {
1139 const char *name = btf__name_by_offset(obj->btf, m->name_off);
1140
1141 if (!name) {
1142 pr_warning("map '%s': invalid field #%d.\n",
1143 map_name, i);
1144 return -EINVAL;
1145 }
1146 if (strcmp(name, "type") == 0) {
1147 if (!get_map_field_int(map_name, obj->btf, def, m,
Andrii Nakryikoef99b022019-07-05 08:50:09 -07001148 &map->def.type))
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001149 return -EINVAL;
1150 pr_debug("map '%s': found type = %u.\n",
1151 map_name, map->def.type);
1152 } else if (strcmp(name, "max_entries") == 0) {
1153 if (!get_map_field_int(map_name, obj->btf, def, m,
Andrii Nakryikoef99b022019-07-05 08:50:09 -07001154 &map->def.max_entries))
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001155 return -EINVAL;
1156 pr_debug("map '%s': found max_entries = %u.\n",
1157 map_name, map->def.max_entries);
1158 } else if (strcmp(name, "map_flags") == 0) {
1159 if (!get_map_field_int(map_name, obj->btf, def, m,
Andrii Nakryikoef99b022019-07-05 08:50:09 -07001160 &map->def.map_flags))
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001161 return -EINVAL;
1162 pr_debug("map '%s': found map_flags = %u.\n",
1163 map_name, map->def.map_flags);
1164 } else if (strcmp(name, "key_size") == 0) {
1165 __u32 sz;
1166
1167 if (!get_map_field_int(map_name, obj->btf, def, m,
Andrii Nakryikoef99b022019-07-05 08:50:09 -07001168 &sz))
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001169 return -EINVAL;
1170 pr_debug("map '%s': found key_size = %u.\n",
1171 map_name, sz);
1172 if (map->def.key_size && map->def.key_size != sz) {
Colin Ian King900de4a2019-06-19 17:27:42 +01001173 pr_warning("map '%s': conflicting key size %u != %u.\n",
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001174 map_name, map->def.key_size, sz);
1175 return -EINVAL;
1176 }
1177 map->def.key_size = sz;
1178 } else if (strcmp(name, "key") == 0) {
1179 __s64 sz;
1180
1181 t = btf__type_by_id(obj->btf, m->type);
1182 if (!t) {
1183 pr_warning("map '%s': key type [%d] not found.\n",
1184 map_name, m->type);
1185 return -EINVAL;
1186 }
1187 if (BTF_INFO_KIND(t->info) != BTF_KIND_PTR) {
1188 pr_warning("map '%s': key spec is not PTR: %u.\n",
1189 map_name, BTF_INFO_KIND(t->info));
1190 return -EINVAL;
1191 }
1192 sz = btf__resolve_size(obj->btf, t->type);
1193 if (sz < 0) {
1194 pr_warning("map '%s': can't determine key size for type [%u]: %lld.\n",
1195 map_name, t->type, sz);
1196 return sz;
1197 }
1198 pr_debug("map '%s': found key [%u], sz = %lld.\n",
1199 map_name, t->type, sz);
1200 if (map->def.key_size && map->def.key_size != sz) {
Colin Ian King900de4a2019-06-19 17:27:42 +01001201 pr_warning("map '%s': conflicting key size %u != %lld.\n",
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001202 map_name, map->def.key_size, sz);
1203 return -EINVAL;
1204 }
1205 map->def.key_size = sz;
1206 map->btf_key_type_id = t->type;
1207 } else if (strcmp(name, "value_size") == 0) {
1208 __u32 sz;
1209
1210 if (!get_map_field_int(map_name, obj->btf, def, m,
Andrii Nakryikoef99b022019-07-05 08:50:09 -07001211 &sz))
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001212 return -EINVAL;
1213 pr_debug("map '%s': found value_size = %u.\n",
1214 map_name, sz);
1215 if (map->def.value_size && map->def.value_size != sz) {
Colin Ian King900de4a2019-06-19 17:27:42 +01001216 pr_warning("map '%s': conflicting value size %u != %u.\n",
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001217 map_name, map->def.value_size, sz);
1218 return -EINVAL;
1219 }
1220 map->def.value_size = sz;
1221 } else if (strcmp(name, "value") == 0) {
1222 __s64 sz;
1223
1224 t = btf__type_by_id(obj->btf, m->type);
1225 if (!t) {
1226 pr_warning("map '%s': value type [%d] not found.\n",
1227 map_name, m->type);
1228 return -EINVAL;
1229 }
1230 if (BTF_INFO_KIND(t->info) != BTF_KIND_PTR) {
1231 pr_warning("map '%s': value spec is not PTR: %u.\n",
1232 map_name, BTF_INFO_KIND(t->info));
1233 return -EINVAL;
1234 }
1235 sz = btf__resolve_size(obj->btf, t->type);
1236 if (sz < 0) {
1237 pr_warning("map '%s': can't determine value size for type [%u]: %lld.\n",
1238 map_name, t->type, sz);
1239 return sz;
1240 }
1241 pr_debug("map '%s': found value [%u], sz = %lld.\n",
1242 map_name, t->type, sz);
1243 if (map->def.value_size && map->def.value_size != sz) {
Colin Ian King900de4a2019-06-19 17:27:42 +01001244 pr_warning("map '%s': conflicting value size %u != %lld.\n",
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001245 map_name, map->def.value_size, sz);
1246 return -EINVAL;
1247 }
1248 map->def.value_size = sz;
1249 map->btf_value_type_id = t->type;
1250 } else {
1251 if (strict) {
1252 pr_warning("map '%s': unknown field '%s'.\n",
1253 map_name, name);
1254 return -ENOTSUP;
1255 }
1256 pr_debug("map '%s': ignoring unknown field '%s'.\n",
1257 map_name, name);
1258 }
1259 }
1260
1261 if (map->def.type == BPF_MAP_TYPE_UNSPEC) {
1262 pr_warning("map '%s': map type isn't specified.\n", map_name);
1263 return -EINVAL;
1264 }
1265
1266 return 0;
1267}
1268
1269static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict)
1270{
1271 const struct btf_type *sec = NULL;
1272 int nr_types, i, vlen, err;
1273 const struct btf_type *t;
1274 const char *name;
1275 Elf_Data *data;
1276 Elf_Scn *scn;
1277
1278 if (obj->efile.btf_maps_shndx < 0)
1279 return 0;
1280
1281 scn = elf_getscn(obj->efile.elf, obj->efile.btf_maps_shndx);
1282 if (scn)
1283 data = elf_getdata(scn, NULL);
1284 if (!scn || !data) {
1285 pr_warning("failed to get Elf_Data from map section %d (%s)\n",
1286 obj->efile.maps_shndx, MAPS_ELF_SEC);
1287 return -EINVAL;
1288 }
1289
1290 nr_types = btf__get_nr_types(obj->btf);
1291 for (i = 1; i <= nr_types; i++) {
1292 t = btf__type_by_id(obj->btf, i);
1293 if (BTF_INFO_KIND(t->info) != BTF_KIND_DATASEC)
1294 continue;
1295 name = btf__name_by_offset(obj->btf, t->name_off);
1296 if (strcmp(name, MAPS_ELF_SEC) == 0) {
1297 sec = t;
1298 break;
1299 }
1300 }
1301
1302 if (!sec) {
1303 pr_warning("DATASEC '%s' not found.\n", MAPS_ELF_SEC);
1304 return -ENOENT;
1305 }
1306
1307 vlen = BTF_INFO_VLEN(sec->info);
1308 for (i = 0; i < vlen; i++) {
1309 err = bpf_object__init_user_btf_map(obj, sec, i,
1310 obj->efile.btf_maps_shndx,
1311 data, strict);
1312 if (err)
1313 return err;
1314 }
1315
1316 return 0;
1317}
1318
Andrii Nakryikobf829272019-06-17 12:26:53 -07001319static int bpf_object__init_maps(struct bpf_object *obj, int flags)
1320{
1321 bool strict = !(flags & MAPS_RELAX_COMPAT);
1322 int err;
Eric Leblond4708bbd2016-11-15 04:05:47 +00001323
Andrii Nakryikobf829272019-06-17 12:26:53 -07001324 err = bpf_object__init_user_maps(obj, strict);
1325 if (err)
1326 return err;
1327
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001328 err = bpf_object__init_user_btf_maps(obj, strict);
1329 if (err)
1330 return err;
1331
Andrii Nakryikobf829272019-06-17 12:26:53 -07001332 err = bpf_object__init_global_data_maps(obj);
1333 if (err)
1334 return err;
1335
1336 if (obj->nr_maps) {
Daniel Borkmannd8599002019-04-09 23:20:13 +02001337 qsort(obj->maps, obj->nr_maps, sizeof(obj->maps[0]),
1338 compare_bpf_map);
Andrii Nakryikobf829272019-06-17 12:26:53 -07001339 }
1340 return 0;
Wang Nan561bbcc2015-11-27 08:47:36 +00001341}
1342
Jesper Dangaard Brouere3d91b02018-02-08 12:48:32 +01001343static bool section_have_execinstr(struct bpf_object *obj, int idx)
1344{
1345 Elf_Scn *scn;
1346 GElf_Shdr sh;
1347
1348 scn = elf_getscn(obj->efile.elf, idx);
1349 if (!scn)
1350 return false;
1351
1352 if (gelf_getshdr(scn, &sh) != &sh)
1353 return false;
1354
1355 if (sh.sh_flags & SHF_EXECINSTR)
1356 return true;
1357
1358 return false;
1359}
1360
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07001361static void bpf_object__sanitize_btf(struct bpf_object *obj)
1362{
1363 bool has_datasec = obj->caps.btf_datasec;
1364 bool has_func = obj->caps.btf_func;
1365 struct btf *btf = obj->btf;
1366 struct btf_type *t;
1367 int i, j, vlen;
1368 __u16 kind;
1369
1370 if (!obj->btf || (has_func && has_datasec))
1371 return;
1372
1373 for (i = 1; i <= btf__get_nr_types(btf); i++) {
1374 t = (struct btf_type *)btf__type_by_id(btf, i);
1375 kind = BTF_INFO_KIND(t->info);
1376
1377 if (!has_datasec && kind == BTF_KIND_VAR) {
1378 /* replace VAR with INT */
1379 t->info = BTF_INFO_ENC(BTF_KIND_INT, 0, 0);
1380 t->size = sizeof(int);
1381 *(int *)(t+1) = BTF_INT_ENC(0, 0, 32);
1382 } else if (!has_datasec && kind == BTF_KIND_DATASEC) {
1383 /* replace DATASEC with STRUCT */
1384 struct btf_var_secinfo *v = (void *)(t + 1);
1385 struct btf_member *m = (void *)(t + 1);
1386 struct btf_type *vt;
1387 char *name;
1388
1389 name = (char *)btf__name_by_offset(btf, t->name_off);
1390 while (*name) {
1391 if (*name == '.')
1392 *name = '_';
1393 name++;
1394 }
1395
1396 vlen = BTF_INFO_VLEN(t->info);
1397 t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, vlen);
1398 for (j = 0; j < vlen; j++, v++, m++) {
1399 /* order of field assignments is important */
1400 m->offset = v->offset * 8;
1401 m->type = v->type;
1402 /* preserve variable name as member name */
1403 vt = (void *)btf__type_by_id(btf, v->type);
1404 m->name_off = vt->name_off;
1405 }
1406 } else if (!has_func && kind == BTF_KIND_FUNC_PROTO) {
1407 /* replace FUNC_PROTO with ENUM */
1408 vlen = BTF_INFO_VLEN(t->info);
1409 t->info = BTF_INFO_ENC(BTF_KIND_ENUM, 0, vlen);
1410 t->size = sizeof(__u32); /* kernel enforced */
1411 } else if (!has_func && kind == BTF_KIND_FUNC) {
1412 /* replace FUNC with TYPEDEF */
1413 t->info = BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0);
1414 }
1415 }
1416}
1417
1418static void bpf_object__sanitize_btf_ext(struct bpf_object *obj)
1419{
1420 if (!obj->btf_ext)
1421 return;
1422
1423 if (!obj->caps.btf_func) {
1424 btf_ext__free(obj->btf_ext);
1425 obj->btf_ext = NULL;
1426 }
1427}
1428
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001429static bool bpf_object__is_btf_mandatory(const struct bpf_object *obj)
1430{
1431 return obj->efile.btf_maps_shndx >= 0;
1432}
1433
Andrii Nakryiko063183b2019-06-17 12:26:55 -07001434static int bpf_object__init_btf(struct bpf_object *obj,
Andrii Nakryiko9c6660d2019-06-17 12:26:51 -07001435 Elf_Data *btf_data,
1436 Elf_Data *btf_ext_data)
1437{
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001438 bool btf_required = bpf_object__is_btf_mandatory(obj);
Andrii Nakryiko9c6660d2019-06-17 12:26:51 -07001439 int err = 0;
1440
1441 if (btf_data) {
1442 obj->btf = btf__new(btf_data->d_buf, btf_data->d_size);
1443 if (IS_ERR(obj->btf)) {
1444 pr_warning("Error loading ELF section %s: %d.\n",
1445 BTF_ELF_SEC, err);
1446 goto out;
1447 }
1448 err = btf__finalize_data(obj, obj->btf);
1449 if (err) {
1450 pr_warning("Error finalizing %s: %d.\n",
1451 BTF_ELF_SEC, err);
1452 goto out;
1453 }
Andrii Nakryiko9c6660d2019-06-17 12:26:51 -07001454 }
1455 if (btf_ext_data) {
1456 if (!obj->btf) {
1457 pr_debug("Ignore ELF section %s because its depending ELF section %s is not found.\n",
1458 BTF_EXT_ELF_SEC, BTF_ELF_SEC);
1459 goto out;
1460 }
1461 obj->btf_ext = btf_ext__new(btf_ext_data->d_buf,
1462 btf_ext_data->d_size);
1463 if (IS_ERR(obj->btf_ext)) {
1464 pr_warning("Error loading ELF section %s: %ld. Ignored and continue.\n",
1465 BTF_EXT_ELF_SEC, PTR_ERR(obj->btf_ext));
1466 obj->btf_ext = NULL;
1467 goto out;
1468 }
Andrii Nakryiko9c6660d2019-06-17 12:26:51 -07001469 }
1470out:
1471 if (err || IS_ERR(obj->btf)) {
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001472 if (btf_required)
1473 err = err ? : PTR_ERR(obj->btf);
1474 else
1475 err = 0;
Andrii Nakryiko9c6660d2019-06-17 12:26:51 -07001476 if (!IS_ERR_OR_NULL(obj->btf))
1477 btf__free(obj->btf);
1478 obj->btf = NULL;
1479 }
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001480 if (btf_required && !obj->btf) {
1481 pr_warning("BTF is required, but is missing or corrupted.\n");
1482 return err == 0 ? -ENOENT : err;
1483 }
Andrii Nakryiko9c6660d2019-06-17 12:26:51 -07001484 return 0;
1485}
1486
Andrii Nakryiko063183b2019-06-17 12:26:55 -07001487static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj)
1488{
1489 int err = 0;
1490
1491 if (!obj->btf)
1492 return 0;
1493
1494 bpf_object__sanitize_btf(obj);
1495 bpf_object__sanitize_btf_ext(obj);
1496
1497 err = btf__load(obj->btf);
1498 if (err) {
1499 pr_warning("Error loading %s into kernel: %d.\n",
1500 BTF_ELF_SEC, err);
1501 btf__free(obj->btf);
1502 obj->btf = NULL;
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001503 if (bpf_object__is_btf_mandatory(obj))
1504 return err;
Andrii Nakryiko063183b2019-06-17 12:26:55 -07001505 }
1506 return 0;
1507}
1508
John Fastabendc034a172018-10-15 11:19:55 -07001509static int bpf_object__elf_collect(struct bpf_object *obj, int flags)
Wang Nan29603662015-07-01 02:13:56 +00001510{
1511 Elf *elf = obj->efile.elf;
1512 GElf_Ehdr *ep = &obj->efile.ehdr;
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08001513 Elf_Data *btf_ext_data = NULL;
Daniel Borkmann1713d682019-04-09 23:20:14 +02001514 Elf_Data *btf_data = NULL;
Wang Nan29603662015-07-01 02:13:56 +00001515 Elf_Scn *scn = NULL;
Wang Nan666810e2016-01-25 09:55:49 +00001516 int idx = 0, err = 0;
Wang Nan29603662015-07-01 02:13:56 +00001517
1518 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
1519 if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) {
Andrii Nakryiko399dc652019-05-29 10:36:11 -07001520 pr_warning("failed to get e_shstrndx from %s\n", obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001521 return -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +00001522 }
1523
1524 while ((scn = elf_nextscn(elf, scn)) != NULL) {
1525 char *name;
1526 GElf_Shdr sh;
1527 Elf_Data *data;
1528
1529 idx++;
1530 if (gelf_getshdr(scn, &sh) != &sh) {
Jesper Dangaard Brouer077c0662018-02-08 12:48:17 +01001531 pr_warning("failed to get section(%d) header from %s\n",
1532 idx, obj->path);
Andrii Nakryiko01b29d12019-06-17 12:26:52 -07001533 return -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +00001534 }
1535
1536 name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
1537 if (!name) {
Jesper Dangaard Brouer077c0662018-02-08 12:48:17 +01001538 pr_warning("failed to get section(%d) name from %s\n",
1539 idx, obj->path);
Andrii Nakryiko01b29d12019-06-17 12:26:52 -07001540 return -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +00001541 }
1542
1543 data = elf_getdata(scn, 0);
1544 if (!data) {
Jesper Dangaard Brouer077c0662018-02-08 12:48:17 +01001545 pr_warning("failed to get section(%d) data from %s(%s)\n",
1546 idx, name, obj->path);
Andrii Nakryiko01b29d12019-06-17 12:26:52 -07001547 return -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +00001548 }
Jesper Dangaard Brouer077c0662018-02-08 12:48:17 +01001549 pr_debug("section(%d) %s, size %ld, link %d, flags %lx, type=%d\n",
1550 idx, name, (unsigned long)data->d_size,
Wang Nan29603662015-07-01 02:13:56 +00001551 (int)sh.sh_link, (unsigned long)sh.sh_flags,
1552 (int)sh.sh_type);
Wang Nancb1e5e92015-07-01 02:13:57 +00001553
Daniel Borkmann1713d682019-04-09 23:20:14 +02001554 if (strcmp(name, "license") == 0) {
Wang Nancb1e5e92015-07-01 02:13:57 +00001555 err = bpf_object__init_license(obj,
1556 data->d_buf,
1557 data->d_size);
Andrii Nakryiko01b29d12019-06-17 12:26:52 -07001558 if (err)
1559 return err;
Daniel Borkmann1713d682019-04-09 23:20:14 +02001560 } else if (strcmp(name, "version") == 0) {
Wang Nancb1e5e92015-07-01 02:13:57 +00001561 err = bpf_object__init_kversion(obj,
1562 data->d_buf,
1563 data->d_size);
Andrii Nakryiko01b29d12019-06-17 12:26:52 -07001564 if (err)
1565 return err;
Daniel Borkmann1713d682019-04-09 23:20:14 +02001566 } else if (strcmp(name, "maps") == 0) {
Wang Nan666810e2016-01-25 09:55:49 +00001567 obj->efile.maps_shndx = idx;
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001568 } else if (strcmp(name, MAPS_ELF_SEC) == 0) {
1569 obj->efile.btf_maps_shndx = idx;
Daniel Borkmann1713d682019-04-09 23:20:14 +02001570 } else if (strcmp(name, BTF_ELF_SEC) == 0) {
1571 btf_data = data;
Yonghong Song2993e052018-11-19 15:29:16 -08001572 } else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) {
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08001573 btf_ext_data = data;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07001574 } else if (sh.sh_type == SHT_SYMTAB) {
Wang Nanbec7d682015-07-01 02:13:59 +00001575 if (obj->efile.symbols) {
1576 pr_warning("bpf: multiple SYMTAB in %s\n",
1577 obj->path);
Andrii Nakryiko01b29d12019-06-17 12:26:52 -07001578 return -LIBBPF_ERRNO__FORMAT;
Wang Nan77ba9a52015-12-08 02:25:30 +00001579 }
Andrii Nakryiko01b29d12019-06-17 12:26:52 -07001580 obj->efile.symbols = data;
1581 obj->efile.strtabidx = sh.sh_link;
Joe Stringerf8c7a4d2019-04-09 23:20:12 +02001582 } else if (sh.sh_type == SHT_PROGBITS && data->d_size > 0) {
1583 if (sh.sh_flags & SHF_EXECINSTR) {
1584 if (strcmp(name, ".text") == 0)
1585 obj->efile.text_shndx = idx;
1586 err = bpf_object__add_program(obj, data->d_buf,
1587 data->d_size, name, idx);
1588 if (err) {
1589 char errmsg[STRERR_BUFSIZE];
1590 char *cp = libbpf_strerror_r(-err, errmsg,
1591 sizeof(errmsg));
Wang Nan6371ca3b2015-11-06 13:49:37 +00001592
Joe Stringerf8c7a4d2019-04-09 23:20:12 +02001593 pr_warning("failed to alloc program %s (%s): %s",
1594 name, obj->path, cp);
Andrii Nakryiko01b29d12019-06-17 12:26:52 -07001595 return err;
Joe Stringerf8c7a4d2019-04-09 23:20:12 +02001596 }
Daniel Borkmannd8599002019-04-09 23:20:13 +02001597 } else if (strcmp(name, ".data") == 0) {
1598 obj->efile.data = data;
1599 obj->efile.data_shndx = idx;
1600 } else if (strcmp(name, ".rodata") == 0) {
1601 obj->efile.rodata = data;
1602 obj->efile.rodata_shndx = idx;
1603 } else {
1604 pr_debug("skip section(%d) %s\n", idx, name);
Wang Nana5b8bd42015-07-01 02:14:00 +00001605 }
Wang Nanb62f06e2015-07-01 02:14:01 +00001606 } else if (sh.sh_type == SHT_REL) {
Andrii Nakryiko01b29d12019-06-17 12:26:52 -07001607 int nr_reloc = obj->efile.nr_reloc;
Wang Nanb62f06e2015-07-01 02:14:01 +00001608 void *reloc = obj->efile.reloc;
Jesper Dangaard Brouere3d91b02018-02-08 12:48:32 +01001609 int sec = sh.sh_info; /* points to other section */
1610
1611 /* Only do relo for section with exec instructions */
1612 if (!section_have_execinstr(obj, sec)) {
1613 pr_debug("skip relo %s(%d) for section(%d)\n",
1614 name, idx, sec);
1615 continue;
1616 }
Wang Nanb62f06e2015-07-01 02:14:01 +00001617
Andrii Nakryiko01b29d12019-06-17 12:26:52 -07001618 reloc = reallocarray(reloc, nr_reloc + 1,
Jakub Kicinski531b0142018-07-10 14:43:05 -07001619 sizeof(*obj->efile.reloc));
Wang Nanb62f06e2015-07-01 02:14:01 +00001620 if (!reloc) {
1621 pr_warning("realloc failed\n");
Andrii Nakryiko01b29d12019-06-17 12:26:52 -07001622 return -ENOMEM;
Wang Nanb62f06e2015-07-01 02:14:01 +00001623 }
Andrii Nakryiko01b29d12019-06-17 12:26:52 -07001624
1625 obj->efile.reloc = reloc;
1626 obj->efile.nr_reloc++;
1627
1628 obj->efile.reloc[nr_reloc].shdr = sh;
1629 obj->efile.reloc[nr_reloc].data = data;
Daniel Borkmannd8599002019-04-09 23:20:13 +02001630 } else if (sh.sh_type == SHT_NOBITS && strcmp(name, ".bss") == 0) {
1631 obj->efile.bss = data;
1632 obj->efile.bss_shndx = idx;
Jesper Dangaard Brouer077c0662018-02-08 12:48:17 +01001633 } else {
1634 pr_debug("skip section(%d) %s\n", idx, name);
Wang Nanbec7d682015-07-01 02:13:59 +00001635 }
Wang Nan29603662015-07-01 02:13:56 +00001636 }
Wang Nan561bbcc2015-11-27 08:47:36 +00001637
Wang Nan77ba9a52015-12-08 02:25:30 +00001638 if (!obj->efile.strtabidx || obj->efile.strtabidx >= idx) {
1639 pr_warning("Corrupted ELF file: index of strtab invalid\n");
Andrii Nakryikof1021542019-05-29 10:36:07 -07001640 return -LIBBPF_ERRNO__FORMAT;
Wang Nan77ba9a52015-12-08 02:25:30 +00001641 }
Andrii Nakryiko063183b2019-06-17 12:26:55 -07001642 err = bpf_object__init_btf(obj, btf_data, btf_ext_data);
Andrii Nakryikobf829272019-06-17 12:26:53 -07001643 if (!err)
John Fastabendc034a172018-10-15 11:19:55 -07001644 err = bpf_object__init_maps(obj, flags);
Andrii Nakryikobf829272019-06-17 12:26:53 -07001645 if (!err)
Andrii Nakryiko063183b2019-06-17 12:26:55 -07001646 err = bpf_object__sanitize_and_load_btf(obj);
1647 if (!err)
Andrii Nakryikobf829272019-06-17 12:26:53 -07001648 err = bpf_object__init_prog_names(obj);
Wang Nan29603662015-07-01 02:13:56 +00001649 return err;
1650}
1651
Wang Nan34090912015-07-01 02:14:02 +00001652static struct bpf_program *
1653bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx)
1654{
1655 struct bpf_program *prog;
1656 size_t i;
1657
1658 for (i = 0; i < obj->nr_programs; i++) {
1659 prog = &obj->programs[i];
1660 if (prog->idx == idx)
1661 return prog;
1662 }
1663 return NULL;
1664}
1665
Jakub Kicinski6d4b1982018-07-26 14:32:19 -07001666struct bpf_program *
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07001667bpf_object__find_program_by_title(const struct bpf_object *obj,
1668 const char *title)
Jakub Kicinski6d4b1982018-07-26 14:32:19 -07001669{
1670 struct bpf_program *pos;
1671
1672 bpf_object__for_each_program(pos, obj) {
1673 if (pos->section_name && !strcmp(pos->section_name, title))
1674 return pos;
1675 }
1676 return NULL;
1677}
1678
Daniel Borkmannd8599002019-04-09 23:20:13 +02001679static bool bpf_object__shndx_is_data(const struct bpf_object *obj,
1680 int shndx)
1681{
1682 return shndx == obj->efile.data_shndx ||
1683 shndx == obj->efile.bss_shndx ||
1684 shndx == obj->efile.rodata_shndx;
1685}
1686
1687static bool bpf_object__shndx_is_maps(const struct bpf_object *obj,
1688 int shndx)
1689{
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001690 return shndx == obj->efile.maps_shndx ||
1691 shndx == obj->efile.btf_maps_shndx;
Daniel Borkmannd8599002019-04-09 23:20:13 +02001692}
1693
1694static bool bpf_object__relo_in_known_section(const struct bpf_object *obj,
1695 int shndx)
1696{
1697 return shndx == obj->efile.text_shndx ||
1698 bpf_object__shndx_is_maps(obj, shndx) ||
1699 bpf_object__shndx_is_data(obj, shndx);
1700}
1701
1702static enum libbpf_map_type
1703bpf_object__section_to_libbpf_map_type(const struct bpf_object *obj, int shndx)
1704{
1705 if (shndx == obj->efile.data_shndx)
1706 return LIBBPF_MAP_DATA;
1707 else if (shndx == obj->efile.bss_shndx)
1708 return LIBBPF_MAP_BSS;
1709 else if (shndx == obj->efile.rodata_shndx)
1710 return LIBBPF_MAP_RODATA;
1711 else
1712 return LIBBPF_MAP_UNSPEC;
1713}
1714
Wang Nan34090912015-07-01 02:14:02 +00001715static int
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001716bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr,
1717 Elf_Data *data, struct bpf_object *obj)
Wang Nan34090912015-07-01 02:14:02 +00001718{
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001719 Elf_Data *symbols = obj->efile.symbols;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001720 struct bpf_map *maps = obj->maps;
1721 size_t nr_maps = obj->nr_maps;
Wang Nan34090912015-07-01 02:14:02 +00001722 int i, nrels;
1723
Andrii Nakryiko399dc652019-05-29 10:36:11 -07001724 pr_debug("collecting relocating info for: '%s'\n", prog->section_name);
Wang Nan34090912015-07-01 02:14:02 +00001725 nrels = shdr->sh_size / shdr->sh_entsize;
1726
1727 prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels);
1728 if (!prog->reloc_desc) {
1729 pr_warning("failed to alloc memory in relocation\n");
1730 return -ENOMEM;
1731 }
1732 prog->nr_reloc = nrels;
1733
1734 for (i = 0; i < nrels; i++) {
Wang Nan34090912015-07-01 02:14:02 +00001735 struct bpf_insn *insns = prog->insns;
Daniel Borkmannd8599002019-04-09 23:20:13 +02001736 enum libbpf_map_type type;
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001737 unsigned int insn_idx;
1738 unsigned int shdr_idx;
Daniel Borkmannd8599002019-04-09 23:20:13 +02001739 const char *name;
Wang Nan34090912015-07-01 02:14:02 +00001740 size_t map_idx;
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001741 GElf_Sym sym;
1742 GElf_Rel rel;
Wang Nan34090912015-07-01 02:14:02 +00001743
1744 if (!gelf_getrel(data, i, &rel)) {
1745 pr_warning("relocation: failed to get %d reloc\n", i);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001746 return -LIBBPF_ERRNO__FORMAT;
Wang Nan34090912015-07-01 02:14:02 +00001747 }
1748
Andrii Nakryiko399dc652019-05-29 10:36:11 -07001749 if (!gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym)) {
Wang Nan34090912015-07-01 02:14:02 +00001750 pr_warning("relocation: symbol %"PRIx64" not found\n",
1751 GELF_R_SYM(rel.r_info));
Wang Nan6371ca3b2015-11-06 13:49:37 +00001752 return -LIBBPF_ERRNO__FORMAT;
Wang Nan34090912015-07-01 02:14:02 +00001753 }
1754
Daniel Borkmannd8599002019-04-09 23:20:13 +02001755 name = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
1756 sym.st_name) ? : "<?>";
1757
1758 pr_debug("relo for %lld value %lld name %d (\'%s\')\n",
1759 (long long) (rel.r_info >> 32),
1760 (long long) sym.st_value, sym.st_name, name);
1761
1762 shdr_idx = sym.st_shndx;
1763 if (!bpf_object__relo_in_known_section(obj, shdr_idx)) {
1764 pr_warning("Program '%s' contains unrecognized relo data pointing to section %u\n",
1765 prog->section_name, shdr_idx);
Wang Nan666810e2016-01-25 09:55:49 +00001766 return -LIBBPF_ERRNO__RELOC;
1767 }
1768
1769 insn_idx = rel.r_offset / sizeof(struct bpf_insn);
1770 pr_debug("relocation: insn_idx=%u\n", insn_idx);
1771
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001772 if (insns[insn_idx].code == (BPF_JMP | BPF_CALL)) {
1773 if (insns[insn_idx].src_reg != BPF_PSEUDO_CALL) {
1774 pr_warning("incorrect bpf_call opcode\n");
1775 return -LIBBPF_ERRNO__RELOC;
1776 }
1777 prog->reloc_desc[i].type = RELO_CALL;
1778 prog->reloc_desc[i].insn_idx = insn_idx;
1779 prog->reloc_desc[i].text_off = sym.st_value;
Jakub Kicinski9a94f272018-06-28 14:41:38 -07001780 obj->has_pseudo_calls = true;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001781 continue;
1782 }
1783
Wang Nan34090912015-07-01 02:14:02 +00001784 if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
1785 pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
1786 insn_idx, insns[insn_idx].code);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001787 return -LIBBPF_ERRNO__RELOC;
Wang Nan34090912015-07-01 02:14:02 +00001788 }
1789
Daniel Borkmannd8599002019-04-09 23:20:13 +02001790 if (bpf_object__shndx_is_maps(obj, shdr_idx) ||
1791 bpf_object__shndx_is_data(obj, shdr_idx)) {
1792 type = bpf_object__section_to_libbpf_map_type(obj, shdr_idx);
Daniel Borkmann8837fe52019-04-24 00:45:56 +02001793 if (type != LIBBPF_MAP_UNSPEC) {
1794 if (GELF_ST_BIND(sym.st_info) == STB_GLOBAL) {
1795 pr_warning("bpf: relocation: not yet supported relo for non-static global \'%s\' variable found in insns[%d].code 0x%x\n",
1796 name, insn_idx, insns[insn_idx].code);
1797 return -LIBBPF_ERRNO__RELOC;
1798 }
1799 if (!obj->caps.global_data) {
1800 pr_warning("bpf: relocation: kernel does not support global \'%s\' variable access in insns[%d]\n",
1801 name, insn_idx);
1802 return -LIBBPF_ERRNO__RELOC;
1803 }
Daniel Borkmannd8599002019-04-09 23:20:13 +02001804 }
1805
Joe Stringerf8c7a4d2019-04-09 23:20:12 +02001806 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
Daniel Borkmannd8599002019-04-09 23:20:13 +02001807 if (maps[map_idx].libbpf_type != type)
1808 continue;
1809 if (type != LIBBPF_MAP_UNSPEC ||
Andrii Nakryikodb488142019-06-17 12:26:54 -07001810 (maps[map_idx].sec_idx == sym.st_shndx &&
1811 maps[map_idx].sec_offset == sym.st_value)) {
1812 pr_debug("relocation: found map %zd (%s, sec_idx %d, offset %zu) for insn %u\n",
1813 map_idx, maps[map_idx].name,
1814 maps[map_idx].sec_idx,
1815 maps[map_idx].sec_offset,
1816 insn_idx);
Joe Stringerf8c7a4d2019-04-09 23:20:12 +02001817 break;
1818 }
Joe Stringer94e5ade2017-01-22 17:11:22 -08001819 }
Joe Stringer94e5ade2017-01-22 17:11:22 -08001820
Joe Stringerf8c7a4d2019-04-09 23:20:12 +02001821 if (map_idx >= nr_maps) {
Andrii Nakryiko76e10222019-05-29 10:36:10 -07001822 pr_warning("bpf relocation: map_idx %d larger than %d\n",
Joe Stringerf8c7a4d2019-04-09 23:20:12 +02001823 (int)map_idx, (int)nr_maps - 1);
1824 return -LIBBPF_ERRNO__RELOC;
1825 }
Wang Nan34090912015-07-01 02:14:02 +00001826
Daniel Borkmannd8599002019-04-09 23:20:13 +02001827 prog->reloc_desc[i].type = type != LIBBPF_MAP_UNSPEC ?
1828 RELO_DATA : RELO_LD64;
Joe Stringerf8c7a4d2019-04-09 23:20:12 +02001829 prog->reloc_desc[i].insn_idx = insn_idx;
1830 prog->reloc_desc[i].map_idx = map_idx;
1831 }
Wang Nan34090912015-07-01 02:14:02 +00001832 }
1833 return 0;
1834}
1835
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001836static int bpf_map_find_btf_info(struct bpf_object *obj, struct bpf_map *map)
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07001837{
1838 struct bpf_map_def *def = &map->def;
Daniel Borkmannd8599002019-04-09 23:20:13 +02001839 __u32 key_type_id = 0, value_type_id = 0;
Yonghong Song96408c42019-02-04 11:00:58 -08001840 int ret;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07001841
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001842 /* if it's BTF-defined map, we don't need to search for type IDs */
1843 if (map->sec_idx == obj->efile.btf_maps_shndx)
1844 return 0;
1845
Daniel Borkmannd8599002019-04-09 23:20:13 +02001846 if (!bpf_map__is_internal(map)) {
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001847 ret = btf__get_map_kv_tids(obj->btf, map->name, def->key_size,
Daniel Borkmannd8599002019-04-09 23:20:13 +02001848 def->value_size, &key_type_id,
1849 &value_type_id);
1850 } else {
1851 /*
1852 * LLVM annotates global data differently in BTF, that is,
1853 * only as '.data', '.bss' or '.rodata'.
1854 */
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07001855 ret = btf__find_by_name(obj->btf,
Daniel Borkmannd8599002019-04-09 23:20:13 +02001856 libbpf_type_to_btf_name[map->libbpf_type]);
1857 }
1858 if (ret < 0)
Yonghong Song96408c42019-02-04 11:00:58 -08001859 return ret;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07001860
Yonghong Song96408c42019-02-04 11:00:58 -08001861 map->btf_key_type_id = key_type_id;
Daniel Borkmannd8599002019-04-09 23:20:13 +02001862 map->btf_value_type_id = bpf_map__is_internal(map) ?
1863 ret : value_type_id;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07001864 return 0;
1865}
1866
Jakub Kicinski26736eb2018-07-10 14:43:06 -07001867int bpf_map__reuse_fd(struct bpf_map *map, int fd)
1868{
1869 struct bpf_map_info info = {};
1870 __u32 len = sizeof(info);
1871 int new_fd, err;
1872 char *new_name;
1873
1874 err = bpf_obj_get_info_by_fd(fd, &info, &len);
1875 if (err)
1876 return err;
1877
1878 new_name = strdup(info.name);
1879 if (!new_name)
1880 return -errno;
1881
1882 new_fd = open("/", O_RDONLY | O_CLOEXEC);
1883 if (new_fd < 0)
1884 goto err_free_new_name;
1885
1886 new_fd = dup3(fd, new_fd, O_CLOEXEC);
1887 if (new_fd < 0)
1888 goto err_close_new_fd;
1889
1890 err = zclose(map->fd);
1891 if (err)
1892 goto err_close_new_fd;
1893 free(map->name);
1894
1895 map->fd = new_fd;
1896 map->name = new_name;
1897 map->def.type = info.type;
1898 map->def.key_size = info.key_size;
1899 map->def.value_size = info.value_size;
1900 map->def.max_entries = info.max_entries;
1901 map->def.map_flags = info.map_flags;
1902 map->btf_key_type_id = info.btf_key_type_id;
1903 map->btf_value_type_id = info.btf_value_type_id;
1904
1905 return 0;
1906
1907err_close_new_fd:
1908 close(new_fd);
1909err_free_new_name:
1910 free(new_name);
1911 return -errno;
1912}
1913
Andrey Ignatov1a11a4c2019-02-14 15:01:42 -08001914int bpf_map__resize(struct bpf_map *map, __u32 max_entries)
1915{
1916 if (!map || !max_entries)
1917 return -EINVAL;
1918
1919 /* If map already created, its attributes can't be changed. */
1920 if (map->fd >= 0)
1921 return -EBUSY;
1922
1923 map->def.max_entries = max_entries;
1924
1925 return 0;
1926}
1927
Wang Nan52d33522015-07-01 02:14:04 +00001928static int
Stanislav Fomichev47eff612018-11-20 17:11:19 -08001929bpf_object__probe_name(struct bpf_object *obj)
1930{
1931 struct bpf_load_program_attr attr;
1932 char *cp, errmsg[STRERR_BUFSIZE];
1933 struct bpf_insn insns[] = {
1934 BPF_MOV64_IMM(BPF_REG_0, 0),
1935 BPF_EXIT_INSN(),
1936 };
1937 int ret;
1938
1939 /* make sure basic loading works */
1940
1941 memset(&attr, 0, sizeof(attr));
1942 attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
1943 attr.insns = insns;
1944 attr.insns_cnt = ARRAY_SIZE(insns);
1945 attr.license = "GPL";
1946
1947 ret = bpf_load_program_xattr(&attr, NULL, 0);
1948 if (ret < 0) {
1949 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
1950 pr_warning("Error in %s():%s(%d). Couldn't load basic 'r0 = 0' BPF program.\n",
1951 __func__, cp, errno);
1952 return -errno;
1953 }
1954 close(ret);
1955
1956 /* now try the same program, but with the name */
1957
1958 attr.name = "test";
1959 ret = bpf_load_program_xattr(&attr, NULL, 0);
1960 if (ret >= 0) {
1961 obj->caps.name = 1;
1962 close(ret);
1963 }
1964
1965 return 0;
1966}
1967
1968static int
Daniel Borkmann8837fe52019-04-24 00:45:56 +02001969bpf_object__probe_global_data(struct bpf_object *obj)
1970{
1971 struct bpf_load_program_attr prg_attr;
1972 struct bpf_create_map_attr map_attr;
1973 char *cp, errmsg[STRERR_BUFSIZE];
1974 struct bpf_insn insns[] = {
1975 BPF_LD_MAP_VALUE(BPF_REG_1, 0, 16),
1976 BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 42),
1977 BPF_MOV64_IMM(BPF_REG_0, 0),
1978 BPF_EXIT_INSN(),
1979 };
1980 int ret, map;
1981
1982 memset(&map_attr, 0, sizeof(map_attr));
1983 map_attr.map_type = BPF_MAP_TYPE_ARRAY;
1984 map_attr.key_size = sizeof(int);
1985 map_attr.value_size = 32;
1986 map_attr.max_entries = 1;
1987
1988 map = bpf_create_map_xattr(&map_attr);
1989 if (map < 0) {
1990 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
1991 pr_warning("Error in %s():%s(%d). Couldn't create simple array map.\n",
1992 __func__, cp, errno);
1993 return -errno;
1994 }
1995
1996 insns[0].imm = map;
1997
1998 memset(&prg_attr, 0, sizeof(prg_attr));
1999 prg_attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
2000 prg_attr.insns = insns;
2001 prg_attr.insns_cnt = ARRAY_SIZE(insns);
2002 prg_attr.license = "GPL";
2003
2004 ret = bpf_load_program_xattr(&prg_attr, NULL, 0);
2005 if (ret >= 0) {
2006 obj->caps.global_data = 1;
2007 close(ret);
2008 }
2009
2010 close(map);
2011 return 0;
2012}
2013
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07002014static int bpf_object__probe_btf_func(struct bpf_object *obj)
2015{
2016 const char strs[] = "\0int\0x\0a";
2017 /* void x(int a) {} */
2018 __u32 types[] = {
2019 /* int */
2020 BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2021 /* FUNC_PROTO */ /* [2] */
2022 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 0),
2023 BTF_PARAM_ENC(7, 1),
2024 /* FUNC x */ /* [3] */
2025 BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0), 2),
2026 };
Michal Rosteckicfd49212019-05-29 20:31:09 +02002027 int btf_fd;
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07002028
Michal Rosteckicfd49212019-05-29 20:31:09 +02002029 btf_fd = libbpf__load_raw_btf((char *)types, sizeof(types),
2030 strs, sizeof(strs));
2031 if (btf_fd >= 0) {
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07002032 obj->caps.btf_func = 1;
Michal Rosteckicfd49212019-05-29 20:31:09 +02002033 close(btf_fd);
2034 return 1;
2035 }
2036
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07002037 return 0;
2038}
2039
2040static int bpf_object__probe_btf_datasec(struct bpf_object *obj)
2041{
2042 const char strs[] = "\0x\0.data";
2043 /* static int a; */
2044 __u32 types[] = {
2045 /* int */
2046 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2047 /* VAR x */ /* [2] */
2048 BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1),
2049 BTF_VAR_STATIC,
2050 /* DATASEC val */ /* [3] */
2051 BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
2052 BTF_VAR_SECINFO_ENC(2, 0, 4),
2053 };
Michal Rosteckicfd49212019-05-29 20:31:09 +02002054 int btf_fd;
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07002055
Michal Rosteckicfd49212019-05-29 20:31:09 +02002056 btf_fd = libbpf__load_raw_btf((char *)types, sizeof(types),
2057 strs, sizeof(strs));
2058 if (btf_fd >= 0) {
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07002059 obj->caps.btf_datasec = 1;
Michal Rosteckicfd49212019-05-29 20:31:09 +02002060 close(btf_fd);
2061 return 1;
2062 }
2063
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07002064 return 0;
2065}
2066
Daniel Borkmann8837fe52019-04-24 00:45:56 +02002067static int
Stanislav Fomichev47eff612018-11-20 17:11:19 -08002068bpf_object__probe_caps(struct bpf_object *obj)
2069{
Daniel Borkmann8837fe52019-04-24 00:45:56 +02002070 int (*probe_fn[])(struct bpf_object *obj) = {
2071 bpf_object__probe_name,
2072 bpf_object__probe_global_data,
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07002073 bpf_object__probe_btf_func,
2074 bpf_object__probe_btf_datasec,
Daniel Borkmann8837fe52019-04-24 00:45:56 +02002075 };
2076 int i, ret;
2077
2078 for (i = 0; i < ARRAY_SIZE(probe_fn); i++) {
2079 ret = probe_fn[i](obj);
2080 if (ret < 0)
Stanislav Fomichev15ea1642019-05-14 20:38:49 -07002081 pr_debug("Probe #%d failed with %d.\n", i, ret);
Daniel Borkmann8837fe52019-04-24 00:45:56 +02002082 }
2083
2084 return 0;
Stanislav Fomichev47eff612018-11-20 17:11:19 -08002085}
2086
2087static int
Daniel Borkmannd8599002019-04-09 23:20:13 +02002088bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map)
2089{
2090 char *cp, errmsg[STRERR_BUFSIZE];
2091 int err, zero = 0;
2092 __u8 *data;
2093
2094 /* Nothing to do here since kernel already zero-initializes .bss map. */
2095 if (map->libbpf_type == LIBBPF_MAP_BSS)
2096 return 0;
2097
2098 data = map->libbpf_type == LIBBPF_MAP_DATA ?
2099 obj->sections.data : obj->sections.rodata;
2100
2101 err = bpf_map_update_elem(map->fd, &zero, data, 0);
2102 /* Freeze .rodata map as read-only from syscall side. */
2103 if (!err && map->libbpf_type == LIBBPF_MAP_RODATA) {
2104 err = bpf_map_freeze(map->fd);
2105 if (err) {
2106 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
2107 pr_warning("Error freezing map(%s) as read-only: %s\n",
2108 map->name, cp);
2109 err = 0;
2110 }
2111 }
2112 return err;
2113}
2114
2115static int
Wang Nan52d33522015-07-01 02:14:04 +00002116bpf_object__create_maps(struct bpf_object *obj)
2117{
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07002118 struct bpf_create_map_attr create_attr = {};
Andrii Nakryikod7ff34d2019-07-06 11:06:25 -07002119 int nr_cpus = 0;
Wang Nan52d33522015-07-01 02:14:04 +00002120 unsigned int i;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07002121 int err;
Wang Nan52d33522015-07-01 02:14:04 +00002122
Wang Nan9d759a92015-11-27 08:47:35 +00002123 for (i = 0; i < obj->nr_maps; i++) {
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07002124 struct bpf_map *map = &obj->maps[i];
2125 struct bpf_map_def *def = &map->def;
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02002126 char *cp, errmsg[STRERR_BUFSIZE];
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07002127 int *pfd = &map->fd;
Wang Nan52d33522015-07-01 02:14:04 +00002128
Jakub Kicinski26736eb2018-07-10 14:43:06 -07002129 if (map->fd >= 0) {
2130 pr_debug("skip map create (preset) %s: fd=%d\n",
2131 map->name, map->fd);
2132 continue;
2133 }
2134
Stanislav Fomichev94cb3102018-11-20 17:11:20 -08002135 if (obj->caps.name)
2136 create_attr.name = map->name;
David Beckettf0307a72018-05-16 14:02:49 -07002137 create_attr.map_ifindex = map->map_ifindex;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07002138 create_attr.map_type = def->type;
2139 create_attr.map_flags = def->map_flags;
2140 create_attr.key_size = def->key_size;
2141 create_attr.value_size = def->value_size;
Andrii Nakryikod7ff34d2019-07-06 11:06:25 -07002142 if (def->type == BPF_MAP_TYPE_PERF_EVENT_ARRAY &&
2143 !def->max_entries) {
2144 if (!nr_cpus)
2145 nr_cpus = libbpf_num_possible_cpus();
2146 if (nr_cpus < 0) {
2147 pr_warning("failed to determine number of system CPUs: %d\n",
2148 nr_cpus);
2149 err = nr_cpus;
2150 goto err_out;
2151 }
2152 pr_debug("map '%s': setting size to %d\n",
2153 map->name, nr_cpus);
2154 create_attr.max_entries = nr_cpus;
2155 } else {
2156 create_attr.max_entries = def->max_entries;
2157 }
Andrii Nakryikoe55d54f2019-06-12 22:04:57 -07002158 create_attr.btf_fd = 0;
Martin KaFai Lau61746dbe2018-05-22 15:04:24 -07002159 create_attr.btf_key_type_id = 0;
2160 create_attr.btf_value_type_id = 0;
Nikita V. Shirokovaddb9fc2018-11-20 20:55:56 -08002161 if (bpf_map_type__is_map_in_map(def->type) &&
2162 map->inner_map_fd >= 0)
2163 create_attr.inner_map_fd = map->inner_map_fd;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07002164
Andrii Nakryikoabd29c92019-06-17 12:26:56 -07002165 if (obj->btf && !bpf_map_find_btf_info(obj, map)) {
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07002166 create_attr.btf_fd = btf__fd(obj->btf);
Martin KaFai Lau61746dbe2018-05-22 15:04:24 -07002167 create_attr.btf_key_type_id = map->btf_key_type_id;
2168 create_attr.btf_value_type_id = map->btf_value_type_id;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07002169 }
2170
2171 *pfd = bpf_create_map_xattr(&create_attr);
Andrii Nakryikoe55d54f2019-06-12 22:04:57 -07002172 if (*pfd < 0 && (create_attr.btf_key_type_id ||
2173 create_attr.btf_value_type_id)) {
Andrii Nakryikod7ff34d2019-07-06 11:06:25 -07002174 err = -errno;
2175 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07002176 pr_warning("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n",
Andrii Nakryikod7ff34d2019-07-06 11:06:25 -07002177 map->name, cp, err);
Andrii Nakryikoe55d54f2019-06-12 22:04:57 -07002178 create_attr.btf_fd = 0;
Martin KaFai Lau61746dbe2018-05-22 15:04:24 -07002179 create_attr.btf_key_type_id = 0;
2180 create_attr.btf_value_type_id = 0;
2181 map->btf_key_type_id = 0;
2182 map->btf_value_type_id = 0;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07002183 *pfd = bpf_create_map_xattr(&create_attr);
2184 }
2185
Wang Nan52d33522015-07-01 02:14:04 +00002186 if (*pfd < 0) {
2187 size_t j;
Wang Nan52d33522015-07-01 02:14:04 +00002188
Andrii Nakryikod7ff34d2019-07-06 11:06:25 -07002189 err = -errno;
Daniel Borkmannd8599002019-04-09 23:20:13 +02002190err_out:
Andrii Nakryikod7ff34d2019-07-06 11:06:25 -07002191 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
2192 pr_warning("failed to create map (name: '%s'): %s(%d)\n",
2193 map->name, cp, err);
Wang Nan52d33522015-07-01 02:14:04 +00002194 for (j = 0; j < i; j++)
Wang Nan9d759a92015-11-27 08:47:35 +00002195 zclose(obj->maps[j].fd);
Wang Nan52d33522015-07-01 02:14:04 +00002196 return err;
2197 }
Daniel Borkmannd8599002019-04-09 23:20:13 +02002198
2199 if (bpf_map__is_internal(map)) {
2200 err = bpf_object__populate_internal_map(obj, map);
2201 if (err < 0) {
2202 zclose(*pfd);
2203 goto err_out;
2204 }
2205 }
2206
Andrii Nakryiko76e10222019-05-29 10:36:10 -07002207 pr_debug("created map %s: fd=%d\n", map->name, *pfd);
Wang Nan52d33522015-07-01 02:14:04 +00002208 }
2209
Wang Nan52d33522015-07-01 02:14:04 +00002210 return 0;
2211}
2212
Wang Nan8a47a6c2015-07-01 02:14:05 +00002213static int
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08002214check_btf_ext_reloc_err(struct bpf_program *prog, int err,
2215 void *btf_prog_info, const char *info_name)
2216{
2217 if (err != -ENOENT) {
2218 pr_warning("Error in loading %s for sec %s.\n",
2219 info_name, prog->section_name);
2220 return err;
2221 }
2222
2223 /* err == -ENOENT (i.e. prog->section_name not found in btf_ext) */
2224
2225 if (btf_prog_info) {
2226 /*
2227 * Some info has already been found but has problem
Andrii Nakryiko399dc652019-05-29 10:36:11 -07002228 * in the last btf_ext reloc. Must have to error out.
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08002229 */
2230 pr_warning("Error in relocating %s for sec %s.\n",
2231 info_name, prog->section_name);
2232 return err;
2233 }
2234
Andrii Nakryiko399dc652019-05-29 10:36:11 -07002235 /* Have problem loading the very first info. Ignore the rest. */
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08002236 pr_warning("Cannot find %s for main program sec %s. Ignore all %s.\n",
2237 info_name, prog->section_name, info_name);
2238 return 0;
2239}
2240
2241static int
2242bpf_program_reloc_btf_ext(struct bpf_program *prog, struct bpf_object *obj,
2243 const char *section_name, __u32 insn_offset)
2244{
2245 int err;
2246
2247 if (!insn_offset || prog->func_info) {
2248 /*
2249 * !insn_offset => main program
2250 *
2251 * For sub prog, the main program's func_info has to
2252 * be loaded first (i.e. prog->func_info != NULL)
2253 */
2254 err = btf_ext__reloc_func_info(obj->btf, obj->btf_ext,
2255 section_name, insn_offset,
2256 &prog->func_info,
2257 &prog->func_info_cnt);
2258 if (err)
2259 return check_btf_ext_reloc_err(prog, err,
2260 prog->func_info,
2261 "bpf_func_info");
2262
2263 prog->func_info_rec_size = btf_ext__func_info_rec_size(obj->btf_ext);
2264 }
2265
Martin KaFai Lau3d650142018-12-07 16:42:31 -08002266 if (!insn_offset || prog->line_info) {
2267 err = btf_ext__reloc_line_info(obj->btf, obj->btf_ext,
2268 section_name, insn_offset,
2269 &prog->line_info,
2270 &prog->line_info_cnt);
2271 if (err)
2272 return check_btf_ext_reloc_err(prog, err,
2273 prog->line_info,
2274 "bpf_line_info");
2275
2276 prog->line_info_rec_size = btf_ext__line_info_rec_size(obj->btf_ext);
2277 }
2278
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08002279 if (!insn_offset)
2280 prog->btf_fd = btf__fd(obj->btf);
2281
2282 return 0;
2283}
2284
2285static int
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08002286bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj,
2287 struct reloc_desc *relo)
2288{
2289 struct bpf_insn *insn, *new_insn;
2290 struct bpf_program *text;
2291 size_t new_cnt;
Yonghong Song2993e052018-11-19 15:29:16 -08002292 int err;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08002293
2294 if (relo->type != RELO_CALL)
2295 return -LIBBPF_ERRNO__RELOC;
2296
2297 if (prog->idx == obj->efile.text_shndx) {
2298 pr_warning("relo in .text insn %d into off %d\n",
2299 relo->insn_idx, relo->text_off);
2300 return -LIBBPF_ERRNO__RELOC;
2301 }
2302
2303 if (prog->main_prog_cnt == 0) {
2304 text = bpf_object__find_prog_by_idx(obj, obj->efile.text_shndx);
2305 if (!text) {
2306 pr_warning("no .text section found yet relo into text exist\n");
2307 return -LIBBPF_ERRNO__RELOC;
2308 }
2309 new_cnt = prog->insns_cnt + text->insns_cnt;
Jakub Kicinski531b0142018-07-10 14:43:05 -07002310 new_insn = reallocarray(prog->insns, new_cnt, sizeof(*insn));
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08002311 if (!new_insn) {
2312 pr_warning("oom in prog realloc\n");
2313 return -ENOMEM;
2314 }
Yonghong Song2993e052018-11-19 15:29:16 -08002315
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08002316 if (obj->btf_ext) {
2317 err = bpf_program_reloc_btf_ext(prog, obj,
2318 text->section_name,
2319 prog->insns_cnt);
2320 if (err)
Yonghong Song2993e052018-11-19 15:29:16 -08002321 return err;
Yonghong Song2993e052018-11-19 15:29:16 -08002322 }
2323
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08002324 memcpy(new_insn + prog->insns_cnt, text->insns,
2325 text->insns_cnt * sizeof(*insn));
2326 prog->insns = new_insn;
2327 prog->main_prog_cnt = prog->insns_cnt;
2328 prog->insns_cnt = new_cnt;
Jeremy Clineb1a2ce82018-02-20 01:00:07 +00002329 pr_debug("added %zd insn from %s to prog %s\n",
2330 text->insns_cnt, text->section_name,
2331 prog->section_name);
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08002332 }
2333 insn = &prog->insns[relo->insn_idx];
2334 insn->imm += prog->main_prog_cnt - relo->insn_idx;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08002335 return 0;
2336}
2337
2338static int
Wang Nan9d759a92015-11-27 08:47:35 +00002339bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj)
Wang Nan8a47a6c2015-07-01 02:14:05 +00002340{
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08002341 int i, err;
Wang Nan8a47a6c2015-07-01 02:14:05 +00002342
Yonghong Song2993e052018-11-19 15:29:16 -08002343 if (!prog)
2344 return 0;
2345
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08002346 if (obj->btf_ext) {
2347 err = bpf_program_reloc_btf_ext(prog, obj,
2348 prog->section_name, 0);
2349 if (err)
Yonghong Song2993e052018-11-19 15:29:16 -08002350 return err;
Yonghong Song2993e052018-11-19 15:29:16 -08002351 }
2352
2353 if (!prog->reloc_desc)
Wang Nan8a47a6c2015-07-01 02:14:05 +00002354 return 0;
2355
2356 for (i = 0; i < prog->nr_reloc; i++) {
Daniel Borkmannd8599002019-04-09 23:20:13 +02002357 if (prog->reloc_desc[i].type == RELO_LD64 ||
2358 prog->reloc_desc[i].type == RELO_DATA) {
2359 bool relo_data = prog->reloc_desc[i].type == RELO_DATA;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08002360 struct bpf_insn *insns = prog->insns;
2361 int insn_idx, map_idx;
Wang Nan8a47a6c2015-07-01 02:14:05 +00002362
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08002363 insn_idx = prog->reloc_desc[i].insn_idx;
2364 map_idx = prog->reloc_desc[i].map_idx;
Wang Nan8a47a6c2015-07-01 02:14:05 +00002365
Daniel Borkmannd8599002019-04-09 23:20:13 +02002366 if (insn_idx + 1 >= (int)prog->insns_cnt) {
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08002367 pr_warning("relocation out of range: '%s'\n",
2368 prog->section_name);
2369 return -LIBBPF_ERRNO__RELOC;
2370 }
Daniel Borkmannd8599002019-04-09 23:20:13 +02002371
2372 if (!relo_data) {
2373 insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
2374 } else {
2375 insns[insn_idx].src_reg = BPF_PSEUDO_MAP_VALUE;
2376 insns[insn_idx + 1].imm = insns[insn_idx].imm;
2377 }
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08002378 insns[insn_idx].imm = obj->maps[map_idx].fd;
Joe Stringerf8c7a4d2019-04-09 23:20:12 +02002379 } else if (prog->reloc_desc[i].type == RELO_CALL) {
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08002380 err = bpf_program__reloc_text(prog, obj,
2381 &prog->reloc_desc[i]);
2382 if (err)
2383 return err;
Wang Nan8a47a6c2015-07-01 02:14:05 +00002384 }
Wang Nan8a47a6c2015-07-01 02:14:05 +00002385 }
2386
2387 zfree(&prog->reloc_desc);
2388 prog->nr_reloc = 0;
2389 return 0;
2390}
2391
2392
2393static int
2394bpf_object__relocate(struct bpf_object *obj)
2395{
2396 struct bpf_program *prog;
2397 size_t i;
2398 int err;
2399
2400 for (i = 0; i < obj->nr_programs; i++) {
2401 prog = &obj->programs[i];
2402
Wang Nan9d759a92015-11-27 08:47:35 +00002403 err = bpf_program__relocate(prog, obj);
Wang Nan8a47a6c2015-07-01 02:14:05 +00002404 if (err) {
2405 pr_warning("failed to relocate '%s'\n",
2406 prog->section_name);
2407 return err;
2408 }
2409 }
2410 return 0;
2411}
2412
Wang Nan34090912015-07-01 02:14:02 +00002413static int bpf_object__collect_reloc(struct bpf_object *obj)
2414{
2415 int i, err;
2416
2417 if (!obj_elf_valid(obj)) {
2418 pr_warning("Internal error: elf object is closed\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +00002419 return -LIBBPF_ERRNO__INTERNAL;
Wang Nan34090912015-07-01 02:14:02 +00002420 }
2421
2422 for (i = 0; i < obj->efile.nr_reloc; i++) {
2423 GElf_Shdr *shdr = &obj->efile.reloc[i].shdr;
2424 Elf_Data *data = obj->efile.reloc[i].data;
2425 int idx = shdr->sh_info;
2426 struct bpf_program *prog;
Wang Nan34090912015-07-01 02:14:02 +00002427
2428 if (shdr->sh_type != SHT_REL) {
2429 pr_warning("internal error at %d\n", __LINE__);
Wang Nan6371ca3b2015-11-06 13:49:37 +00002430 return -LIBBPF_ERRNO__INTERNAL;
Wang Nan34090912015-07-01 02:14:02 +00002431 }
2432
2433 prog = bpf_object__find_prog_by_idx(obj, idx);
2434 if (!prog) {
Jesper Dangaard Brouer077c0662018-02-08 12:48:17 +01002435 pr_warning("relocation failed: no section(%d)\n", idx);
Wang Nan6371ca3b2015-11-06 13:49:37 +00002436 return -LIBBPF_ERRNO__RELOC;
Wang Nan34090912015-07-01 02:14:02 +00002437 }
2438
Andrii Nakryiko399dc652019-05-29 10:36:11 -07002439 err = bpf_program__collect_reloc(prog, shdr, data, obj);
Wang Nan34090912015-07-01 02:14:02 +00002440 if (err)
Wang Nan6371ca3b2015-11-06 13:49:37 +00002441 return err;
Wang Nan34090912015-07-01 02:14:02 +00002442 }
2443 return 0;
2444}
2445
Wang Nan55cffde2015-07-01 02:14:07 +00002446static int
Yonghong Song2993e052018-11-19 15:29:16 -08002447load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08002448 char *license, __u32 kern_version, int *pfd)
Wang Nan55cffde2015-07-01 02:14:07 +00002449{
Andrey Ignatovd7be1432018-03-30 15:08:01 -07002450 struct bpf_load_program_attr load_attr;
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02002451 char *cp, errmsg[STRERR_BUFSIZE];
Alexei Starovoitovda11b412019-04-01 21:27:47 -07002452 int log_buf_size = BPF_LOG_BUF_SIZE;
Wang Nan55cffde2015-07-01 02:14:07 +00002453 char *log_buf;
Andrey Ignatovd7be1432018-03-30 15:08:01 -07002454 int ret;
Wang Nan55cffde2015-07-01 02:14:07 +00002455
Andrii Nakryikofba01a02019-05-29 10:36:08 -07002456 if (!insns || !insns_cnt)
2457 return -EINVAL;
2458
Andrey Ignatovd7be1432018-03-30 15:08:01 -07002459 memset(&load_attr, 0, sizeof(struct bpf_load_program_attr));
Yonghong Song2993e052018-11-19 15:29:16 -08002460 load_attr.prog_type = prog->type;
2461 load_attr.expected_attach_type = prog->expected_attach_type;
Stanislav Fomichev5b32a232018-11-20 17:11:21 -08002462 if (prog->caps->name)
2463 load_attr.name = prog->name;
Andrey Ignatovd7be1432018-03-30 15:08:01 -07002464 load_attr.insns = insns;
2465 load_attr.insns_cnt = insns_cnt;
2466 load_attr.license = license;
2467 load_attr.kern_version = kern_version;
Yonghong Song2993e052018-11-19 15:29:16 -08002468 load_attr.prog_ifindex = prog->prog_ifindex;
Andrii Nakryikoe55d54f2019-06-12 22:04:57 -07002469 load_attr.prog_btf_fd = prog->btf_fd >= 0 ? prog->btf_fd : 0;
Yonghong Song2993e052018-11-19 15:29:16 -08002470 load_attr.func_info = prog->func_info;
2471 load_attr.func_info_rec_size = prog->func_info_rec_size;
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08002472 load_attr.func_info_cnt = prog->func_info_cnt;
Martin KaFai Lau3d650142018-12-07 16:42:31 -08002473 load_attr.line_info = prog->line_info;
2474 load_attr.line_info_rec_size = prog->line_info_rec_size;
2475 load_attr.line_info_cnt = prog->line_info_cnt;
Alexei Starovoitovda11b412019-04-01 21:27:47 -07002476 load_attr.log_level = prog->log_level;
Jiong Wang04656192019-05-24 23:25:19 +01002477 load_attr.prog_flags = prog->prog_flags;
Wang Nan55cffde2015-07-01 02:14:07 +00002478
Alexei Starovoitovda11b412019-04-01 21:27:47 -07002479retry_load:
2480 log_buf = malloc(log_buf_size);
Wang Nan55cffde2015-07-01 02:14:07 +00002481 if (!log_buf)
2482 pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
2483
Alexei Starovoitovda11b412019-04-01 21:27:47 -07002484 ret = bpf_load_program_xattr(&load_attr, log_buf, log_buf_size);
Wang Nan55cffde2015-07-01 02:14:07 +00002485
2486 if (ret >= 0) {
Alexei Starovoitovda11b412019-04-01 21:27:47 -07002487 if (load_attr.log_level)
2488 pr_debug("verifier log:\n%s", log_buf);
Wang Nan55cffde2015-07-01 02:14:07 +00002489 *pfd = ret;
2490 ret = 0;
2491 goto out;
2492 }
2493
Alexei Starovoitovda11b412019-04-01 21:27:47 -07002494 if (errno == ENOSPC) {
2495 log_buf_size <<= 1;
2496 free(log_buf);
2497 goto retry_load;
2498 }
Wang Nan6371ca3b2015-11-06 13:49:37 +00002499 ret = -LIBBPF_ERRNO__LOAD;
Andrey Ignatov24d6a802018-10-03 15:26:41 -07002500 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02002501 pr_warning("load bpf program failed: %s\n", cp);
Wang Nan55cffde2015-07-01 02:14:07 +00002502
Wang Nan6371ca3b2015-11-06 13:49:37 +00002503 if (log_buf && log_buf[0] != '\0') {
2504 ret = -LIBBPF_ERRNO__VERIFY;
Wang Nan55cffde2015-07-01 02:14:07 +00002505 pr_warning("-- BEGIN DUMP LOG ---\n");
2506 pr_warning("\n%s\n", log_buf);
2507 pr_warning("-- END LOG --\n");
Andrey Ignatovd7be1432018-03-30 15:08:01 -07002508 } else if (load_attr.insns_cnt >= BPF_MAXINSNS) {
2509 pr_warning("Program too large (%zu insns), at most %d insns\n",
2510 load_attr.insns_cnt, BPF_MAXINSNS);
Wang Nan705fa212016-07-13 10:44:02 +00002511 ret = -LIBBPF_ERRNO__PROG2BIG;
Wang Nan6371ca3b2015-11-06 13:49:37 +00002512 } else {
Wang Nan705fa212016-07-13 10:44:02 +00002513 /* Wrong program type? */
Andrey Ignatovd7be1432018-03-30 15:08:01 -07002514 if (load_attr.prog_type != BPF_PROG_TYPE_KPROBE) {
Wang Nan705fa212016-07-13 10:44:02 +00002515 int fd;
2516
Andrey Ignatovd7be1432018-03-30 15:08:01 -07002517 load_attr.prog_type = BPF_PROG_TYPE_KPROBE;
2518 load_attr.expected_attach_type = 0;
2519 fd = bpf_load_program_xattr(&load_attr, NULL, 0);
Wang Nan705fa212016-07-13 10:44:02 +00002520 if (fd >= 0) {
2521 close(fd);
2522 ret = -LIBBPF_ERRNO__PROGTYPE;
2523 goto out;
2524 }
Wang Nan6371ca3b2015-11-06 13:49:37 +00002525 }
Wang Nan705fa212016-07-13 10:44:02 +00002526
2527 if (log_buf)
2528 ret = -LIBBPF_ERRNO__KVER;
Wang Nan55cffde2015-07-01 02:14:07 +00002529 }
2530
2531out:
2532 free(log_buf);
2533 return ret;
2534}
2535
Joe Stringer29cd77f2018-10-02 13:35:39 -07002536int
Wang Nan55cffde2015-07-01 02:14:07 +00002537bpf_program__load(struct bpf_program *prog,
Andrey Ignatove5b08632018-10-03 15:26:43 -07002538 char *license, __u32 kern_version)
Wang Nan55cffde2015-07-01 02:14:07 +00002539{
Wang Nanb5805632015-11-16 12:10:09 +00002540 int err = 0, fd, i;
Wang Nan55cffde2015-07-01 02:14:07 +00002541
Wang Nanb5805632015-11-16 12:10:09 +00002542 if (prog->instances.nr < 0 || !prog->instances.fds) {
2543 if (prog->preprocessor) {
2544 pr_warning("Internal error: can't load program '%s'\n",
2545 prog->section_name);
2546 return -LIBBPF_ERRNO__INTERNAL;
2547 }
Wang Nan55cffde2015-07-01 02:14:07 +00002548
Wang Nanb5805632015-11-16 12:10:09 +00002549 prog->instances.fds = malloc(sizeof(int));
2550 if (!prog->instances.fds) {
2551 pr_warning("Not enough memory for BPF fds\n");
2552 return -ENOMEM;
2553 }
2554 prog->instances.nr = 1;
2555 prog->instances.fds[0] = -1;
2556 }
2557
2558 if (!prog->preprocessor) {
2559 if (prog->instances.nr != 1) {
2560 pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
2561 prog->section_name, prog->instances.nr);
2562 }
Yonghong Song2993e052018-11-19 15:29:16 -08002563 err = load_program(prog, prog->insns, prog->insns_cnt,
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08002564 license, kern_version, &fd);
Wang Nanb5805632015-11-16 12:10:09 +00002565 if (!err)
2566 prog->instances.fds[0] = fd;
2567 goto out;
2568 }
2569
2570 for (i = 0; i < prog->instances.nr; i++) {
2571 struct bpf_prog_prep_result result;
2572 bpf_program_prep_t preprocessor = prog->preprocessor;
2573
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -08002574 memset(&result, 0, sizeof(result));
Wang Nanb5805632015-11-16 12:10:09 +00002575 err = preprocessor(prog, i, prog->insns,
2576 prog->insns_cnt, &result);
2577 if (err) {
2578 pr_warning("Preprocessing the %dth instance of program '%s' failed\n",
2579 i, prog->section_name);
2580 goto out;
2581 }
2582
2583 if (!result.new_insn_ptr || !result.new_insn_cnt) {
2584 pr_debug("Skip loading the %dth instance of program '%s'\n",
2585 i, prog->section_name);
2586 prog->instances.fds[i] = -1;
2587 if (result.pfd)
2588 *result.pfd = -1;
2589 continue;
2590 }
2591
Yonghong Song2993e052018-11-19 15:29:16 -08002592 err = load_program(prog, result.new_insn_ptr,
Wang Nanb5805632015-11-16 12:10:09 +00002593 result.new_insn_cnt,
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08002594 license, kern_version, &fd);
Wang Nanb5805632015-11-16 12:10:09 +00002595
2596 if (err) {
2597 pr_warning("Loading the %dth instance of program '%s' failed\n",
2598 i, prog->section_name);
2599 goto out;
2600 }
2601
2602 if (result.pfd)
2603 *result.pfd = fd;
2604 prog->instances.fds[i] = fd;
2605 }
2606out:
Wang Nan55cffde2015-07-01 02:14:07 +00002607 if (err)
2608 pr_warning("failed to load program '%s'\n",
2609 prog->section_name);
2610 zfree(&prog->insns);
2611 prog->insns_cnt = 0;
2612 return err;
2613}
2614
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07002615static bool bpf_program__is_function_storage(const struct bpf_program *prog,
2616 const struct bpf_object *obj)
Jakub Kicinski9a94f272018-06-28 14:41:38 -07002617{
2618 return prog->idx == obj->efile.text_shndx && obj->has_pseudo_calls;
2619}
2620
Wang Nan55cffde2015-07-01 02:14:07 +00002621static int
Quentin Monnet60276f92019-05-24 11:36:47 +01002622bpf_object__load_progs(struct bpf_object *obj, int log_level)
Wang Nan55cffde2015-07-01 02:14:07 +00002623{
2624 size_t i;
2625 int err;
2626
2627 for (i = 0; i < obj->nr_programs; i++) {
Jakub Kicinski9a94f272018-06-28 14:41:38 -07002628 if (bpf_program__is_function_storage(&obj->programs[i], obj))
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08002629 continue;
Quentin Monnet501b1252019-05-29 15:26:41 +01002630 obj->programs[i].log_level |= log_level;
Wang Nan55cffde2015-07-01 02:14:07 +00002631 err = bpf_program__load(&obj->programs[i],
2632 obj->license,
2633 obj->kern_version);
2634 if (err)
2635 return err;
2636 }
2637 return 0;
2638}
2639
Jakub Kicinski17387dd2018-05-10 10:24:42 -07002640static bool bpf_prog_type__needs_kver(enum bpf_prog_type type)
Wang Nancb1e5e92015-07-01 02:13:57 +00002641{
Jakub Kicinski17387dd2018-05-10 10:24:42 -07002642 switch (type) {
2643 case BPF_PROG_TYPE_SOCKET_FILTER:
2644 case BPF_PROG_TYPE_SCHED_CLS:
2645 case BPF_PROG_TYPE_SCHED_ACT:
2646 case BPF_PROG_TYPE_XDP:
2647 case BPF_PROG_TYPE_CGROUP_SKB:
2648 case BPF_PROG_TYPE_CGROUP_SOCK:
2649 case BPF_PROG_TYPE_LWT_IN:
2650 case BPF_PROG_TYPE_LWT_OUT:
2651 case BPF_PROG_TYPE_LWT_XMIT:
Mathieu Xhonneux004d4b22018-05-20 14:58:16 +01002652 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
Jakub Kicinski17387dd2018-05-10 10:24:42 -07002653 case BPF_PROG_TYPE_SOCK_OPS:
2654 case BPF_PROG_TYPE_SK_SKB:
2655 case BPF_PROG_TYPE_CGROUP_DEVICE:
2656 case BPF_PROG_TYPE_SK_MSG:
2657 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
Sean Young6bdd5332018-05-27 12:24:10 +01002658 case BPF_PROG_TYPE_LIRC_MODE2:
Martin KaFai Lau6bc8529c2018-08-08 01:01:30 -07002659 case BPF_PROG_TYPE_SK_REUSEPORT:
Petar Penkovc22fbae2018-09-14 07:46:20 -07002660 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Jakub Kicinski17387dd2018-05-10 10:24:42 -07002661 case BPF_PROG_TYPE_UNSPEC:
Jakub Kicinski17387dd2018-05-10 10:24:42 -07002662 case BPF_PROG_TYPE_TRACEPOINT:
Jakub Kicinski17387dd2018-05-10 10:24:42 -07002663 case BPF_PROG_TYPE_RAW_TRACEPOINT:
Matt Mullins4635b0a2019-04-26 11:49:50 -07002664 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
Nikita V. Shirokov47ae7e32018-11-23 12:58:12 -08002665 case BPF_PROG_TYPE_PERF_EVENT:
Andrey Ignatov063cc9f2019-03-08 09:15:26 -08002666 case BPF_PROG_TYPE_CGROUP_SYSCTL:
Stanislav Fomichev4cdbfb52019-06-27 13:38:49 -07002667 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
Nikita V. Shirokov47ae7e32018-11-23 12:58:12 -08002668 return false;
2669 case BPF_PROG_TYPE_KPROBE:
Jakub Kicinski17387dd2018-05-10 10:24:42 -07002670 default:
2671 return true;
2672 }
2673}
2674
2675static int bpf_object__validate(struct bpf_object *obj, bool needs_kver)
2676{
2677 if (needs_kver && obj->kern_version == 0) {
Wang Nancb1e5e92015-07-01 02:13:57 +00002678 pr_warning("%s doesn't provide kernel version\n",
2679 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00002680 return -LIBBPF_ERRNO__KVERSION;
Wang Nancb1e5e92015-07-01 02:13:57 +00002681 }
2682 return 0;
2683}
2684
Wang Nan1a5e3fb2015-07-01 02:13:53 +00002685static struct bpf_object *
Jakub Kicinski17387dd2018-05-10 10:24:42 -07002686__bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz,
John Fastabendc034a172018-10-15 11:19:55 -07002687 bool needs_kver, int flags)
Wang Nan1a5e3fb2015-07-01 02:13:53 +00002688{
2689 struct bpf_object *obj;
Wang Nan6371ca3b2015-11-06 13:49:37 +00002690 int err;
Wang Nan1a5e3fb2015-07-01 02:13:53 +00002691
2692 if (elf_version(EV_CURRENT) == EV_NONE) {
2693 pr_warning("failed to init libelf for %s\n", path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00002694 return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00002695 }
2696
Wang Nan6c956392015-07-01 02:13:54 +00002697 obj = bpf_object__new(path, obj_buf, obj_buf_sz);
Wang Nan6371ca3b2015-11-06 13:49:37 +00002698 if (IS_ERR(obj))
2699 return obj;
Wang Nan1a5e3fb2015-07-01 02:13:53 +00002700
Wang Nan6371ca3b2015-11-06 13:49:37 +00002701 CHECK_ERR(bpf_object__elf_init(obj), err, out);
2702 CHECK_ERR(bpf_object__check_endianness(obj), err, out);
Daniel Borkmann8837fe52019-04-24 00:45:56 +02002703 CHECK_ERR(bpf_object__probe_caps(obj), err, out);
John Fastabendc034a172018-10-15 11:19:55 -07002704 CHECK_ERR(bpf_object__elf_collect(obj, flags), err, out);
Wang Nan6371ca3b2015-11-06 13:49:37 +00002705 CHECK_ERR(bpf_object__collect_reloc(obj), err, out);
Jakub Kicinski17387dd2018-05-10 10:24:42 -07002706 CHECK_ERR(bpf_object__validate(obj, needs_kver), err, out);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00002707
2708 bpf_object__elf_finish(obj);
2709 return obj;
2710out:
2711 bpf_object__close(obj);
Wang Nan6371ca3b2015-11-06 13:49:37 +00002712 return ERR_PTR(err);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00002713}
2714
John Fastabendc034a172018-10-15 11:19:55 -07002715struct bpf_object *__bpf_object__open_xattr(struct bpf_object_open_attr *attr,
2716 int flags)
Wang Nan1a5e3fb2015-07-01 02:13:53 +00002717{
2718 /* param validation */
Jakub Kicinski07f2d4e2018-07-10 14:43:02 -07002719 if (!attr->file)
Wang Nan1a5e3fb2015-07-01 02:13:53 +00002720 return NULL;
2721
Jakub Kicinski07f2d4e2018-07-10 14:43:02 -07002722 pr_debug("loading %s\n", attr->file);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00002723
Jakub Kicinski07f2d4e2018-07-10 14:43:02 -07002724 return __bpf_object__open(attr->file, NULL, 0,
John Fastabendc034a172018-10-15 11:19:55 -07002725 bpf_prog_type__needs_kver(attr->prog_type),
2726 flags);
2727}
2728
2729struct bpf_object *bpf_object__open_xattr(struct bpf_object_open_attr *attr)
2730{
2731 return __bpf_object__open_xattr(attr, 0);
Jakub Kicinski07f2d4e2018-07-10 14:43:02 -07002732}
2733
2734struct bpf_object *bpf_object__open(const char *path)
2735{
2736 struct bpf_object_open_attr attr = {
2737 .file = path,
2738 .prog_type = BPF_PROG_TYPE_UNSPEC,
2739 };
2740
2741 return bpf_object__open_xattr(&attr);
Wang Nan6c956392015-07-01 02:13:54 +00002742}
2743
2744struct bpf_object *bpf_object__open_buffer(void *obj_buf,
Wang Nanacf860a2015-08-27 02:30:55 +00002745 size_t obj_buf_sz,
2746 const char *name)
Wang Nan6c956392015-07-01 02:13:54 +00002747{
Wang Nanacf860a2015-08-27 02:30:55 +00002748 char tmp_name[64];
2749
Wang Nan6c956392015-07-01 02:13:54 +00002750 /* param validation */
2751 if (!obj_buf || obj_buf_sz <= 0)
2752 return NULL;
2753
Wang Nanacf860a2015-08-27 02:30:55 +00002754 if (!name) {
2755 snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
2756 (unsigned long)obj_buf,
2757 (unsigned long)obj_buf_sz);
Wang Nanacf860a2015-08-27 02:30:55 +00002758 name = tmp_name;
2759 }
Andrii Nakryiko399dc652019-05-29 10:36:11 -07002760 pr_debug("loading object '%s' from buffer\n", name);
Wang Nan6c956392015-07-01 02:13:54 +00002761
John Fastabendc034a172018-10-15 11:19:55 -07002762 return __bpf_object__open(name, obj_buf, obj_buf_sz, true, true);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00002763}
2764
Wang Nan52d33522015-07-01 02:14:04 +00002765int bpf_object__unload(struct bpf_object *obj)
2766{
2767 size_t i;
2768
2769 if (!obj)
2770 return -EINVAL;
2771
Wang Nan9d759a92015-11-27 08:47:35 +00002772 for (i = 0; i < obj->nr_maps; i++)
2773 zclose(obj->maps[i].fd);
Wang Nan52d33522015-07-01 02:14:04 +00002774
Wang Nan55cffde2015-07-01 02:14:07 +00002775 for (i = 0; i < obj->nr_programs; i++)
2776 bpf_program__unload(&obj->programs[i]);
2777
Wang Nan52d33522015-07-01 02:14:04 +00002778 return 0;
2779}
2780
Quentin Monnet60276f92019-05-24 11:36:47 +01002781int bpf_object__load_xattr(struct bpf_object_load_attr *attr)
Wang Nan52d33522015-07-01 02:14:04 +00002782{
Quentin Monnet60276f92019-05-24 11:36:47 +01002783 struct bpf_object *obj;
Wang Nan6371ca3b2015-11-06 13:49:37 +00002784 int err;
2785
Quentin Monnet60276f92019-05-24 11:36:47 +01002786 if (!attr)
2787 return -EINVAL;
2788 obj = attr->obj;
Wang Nan52d33522015-07-01 02:14:04 +00002789 if (!obj)
2790 return -EINVAL;
2791
2792 if (obj->loaded) {
2793 pr_warning("object should not be loaded twice\n");
2794 return -EINVAL;
2795 }
2796
2797 obj->loaded = true;
Wang Nan6371ca3b2015-11-06 13:49:37 +00002798
2799 CHECK_ERR(bpf_object__create_maps(obj), err, out);
2800 CHECK_ERR(bpf_object__relocate(obj), err, out);
Quentin Monnet60276f92019-05-24 11:36:47 +01002801 CHECK_ERR(bpf_object__load_progs(obj, attr->log_level), err, out);
Wang Nan52d33522015-07-01 02:14:04 +00002802
2803 return 0;
2804out:
2805 bpf_object__unload(obj);
2806 pr_warning("failed to load object '%s'\n", obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00002807 return err;
Wang Nan52d33522015-07-01 02:14:04 +00002808}
2809
Quentin Monnet60276f92019-05-24 11:36:47 +01002810int bpf_object__load(struct bpf_object *obj)
2811{
2812 struct bpf_object_load_attr attr = {
2813 .obj = obj,
2814 };
2815
2816 return bpf_object__load_xattr(&attr);
2817}
2818
Joe Stringerf3675402017-01-26 13:19:56 -08002819static int check_path(const char *path)
2820{
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02002821 char *cp, errmsg[STRERR_BUFSIZE];
Joe Stringerf3675402017-01-26 13:19:56 -08002822 struct statfs st_fs;
2823 char *dname, *dir;
2824 int err = 0;
2825
2826 if (path == NULL)
2827 return -EINVAL;
2828
2829 dname = strdup(path);
2830 if (dname == NULL)
2831 return -ENOMEM;
2832
2833 dir = dirname(dname);
2834 if (statfs(dir, &st_fs)) {
Andrey Ignatov24d6a802018-10-03 15:26:41 -07002835 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02002836 pr_warning("failed to statfs %s: %s\n", dir, cp);
Joe Stringerf3675402017-01-26 13:19:56 -08002837 err = -errno;
2838 }
2839 free(dname);
2840
2841 if (!err && st_fs.f_type != BPF_FS_MAGIC) {
2842 pr_warning("specified path %s is not on BPF FS\n", path);
2843 err = -EINVAL;
2844 }
2845
2846 return err;
2847}
2848
2849int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
2850 int instance)
2851{
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02002852 char *cp, errmsg[STRERR_BUFSIZE];
Joe Stringerf3675402017-01-26 13:19:56 -08002853 int err;
2854
2855 err = check_path(path);
2856 if (err)
2857 return err;
2858
2859 if (prog == NULL) {
2860 pr_warning("invalid program pointer\n");
2861 return -EINVAL;
2862 }
2863
2864 if (instance < 0 || instance >= prog->instances.nr) {
2865 pr_warning("invalid prog instance %d of prog %s (max %d)\n",
2866 instance, prog->section_name, prog->instances.nr);
2867 return -EINVAL;
2868 }
2869
2870 if (bpf_obj_pin(prog->instances.fds[instance], path)) {
Andrey Ignatov24d6a802018-10-03 15:26:41 -07002871 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02002872 pr_warning("failed to pin program: %s\n", cp);
Joe Stringerf3675402017-01-26 13:19:56 -08002873 return -errno;
2874 }
2875 pr_debug("pinned program '%s'\n", path);
2876
2877 return 0;
2878}
2879
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08002880int bpf_program__unpin_instance(struct bpf_program *prog, const char *path,
2881 int instance)
2882{
2883 int err;
2884
2885 err = check_path(path);
2886 if (err)
2887 return err;
2888
2889 if (prog == NULL) {
2890 pr_warning("invalid program pointer\n");
2891 return -EINVAL;
2892 }
2893
2894 if (instance < 0 || instance >= prog->instances.nr) {
2895 pr_warning("invalid prog instance %d of prog %s (max %d)\n",
2896 instance, prog->section_name, prog->instances.nr);
2897 return -EINVAL;
2898 }
2899
2900 err = unlink(path);
2901 if (err != 0)
2902 return -errno;
2903 pr_debug("unpinned program '%s'\n", path);
2904
2905 return 0;
2906}
2907
Joe Stringerf3675402017-01-26 13:19:56 -08002908static int make_dir(const char *path)
2909{
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02002910 char *cp, errmsg[STRERR_BUFSIZE];
Joe Stringerf3675402017-01-26 13:19:56 -08002911 int err = 0;
2912
2913 if (mkdir(path, 0700) && errno != EEXIST)
2914 err = -errno;
2915
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02002916 if (err) {
Andrey Ignatov24d6a802018-10-03 15:26:41 -07002917 cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02002918 pr_warning("failed to mkdir %s: %s\n", path, cp);
2919 }
Joe Stringerf3675402017-01-26 13:19:56 -08002920 return err;
2921}
2922
2923int bpf_program__pin(struct bpf_program *prog, const char *path)
2924{
2925 int i, err;
2926
2927 err = check_path(path);
2928 if (err)
2929 return err;
2930
2931 if (prog == NULL) {
2932 pr_warning("invalid program pointer\n");
2933 return -EINVAL;
2934 }
2935
2936 if (prog->instances.nr <= 0) {
2937 pr_warning("no instances of prog %s to pin\n",
2938 prog->section_name);
2939 return -EINVAL;
2940 }
2941
Stanislav Fomichevfd734c52018-11-09 08:21:42 -08002942 if (prog->instances.nr == 1) {
2943 /* don't create subdirs when pinning single instance */
2944 return bpf_program__pin_instance(prog, path, 0);
2945 }
2946
Joe Stringerf3675402017-01-26 13:19:56 -08002947 err = make_dir(path);
2948 if (err)
2949 return err;
2950
2951 for (i = 0; i < prog->instances.nr; i++) {
2952 char buf[PATH_MAX];
2953 int len;
2954
2955 len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08002956 if (len < 0) {
2957 err = -EINVAL;
2958 goto err_unpin;
2959 } else if (len >= PATH_MAX) {
2960 err = -ENAMETOOLONG;
2961 goto err_unpin;
2962 }
2963
2964 err = bpf_program__pin_instance(prog, buf, i);
2965 if (err)
2966 goto err_unpin;
2967 }
2968
2969 return 0;
2970
2971err_unpin:
2972 for (i = i - 1; i >= 0; i--) {
2973 char buf[PATH_MAX];
2974 int len;
2975
2976 len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
2977 if (len < 0)
2978 continue;
2979 else if (len >= PATH_MAX)
2980 continue;
2981
2982 bpf_program__unpin_instance(prog, buf, i);
2983 }
2984
2985 rmdir(path);
2986
2987 return err;
2988}
2989
2990int bpf_program__unpin(struct bpf_program *prog, const char *path)
2991{
2992 int i, err;
2993
2994 err = check_path(path);
2995 if (err)
2996 return err;
2997
2998 if (prog == NULL) {
2999 pr_warning("invalid program pointer\n");
3000 return -EINVAL;
3001 }
3002
3003 if (prog->instances.nr <= 0) {
3004 pr_warning("no instances of prog %s to pin\n",
3005 prog->section_name);
3006 return -EINVAL;
3007 }
3008
Stanislav Fomichevfd734c52018-11-09 08:21:42 -08003009 if (prog->instances.nr == 1) {
3010 /* don't create subdirs when pinning single instance */
3011 return bpf_program__unpin_instance(prog, path, 0);
3012 }
3013
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003014 for (i = 0; i < prog->instances.nr; i++) {
3015 char buf[PATH_MAX];
3016 int len;
3017
3018 len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
Joe Stringerf3675402017-01-26 13:19:56 -08003019 if (len < 0)
3020 return -EINVAL;
3021 else if (len >= PATH_MAX)
3022 return -ENAMETOOLONG;
3023
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003024 err = bpf_program__unpin_instance(prog, buf, i);
Joe Stringerf3675402017-01-26 13:19:56 -08003025 if (err)
3026 return err;
3027 }
3028
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003029 err = rmdir(path);
3030 if (err)
3031 return -errno;
3032
Joe Stringerf3675402017-01-26 13:19:56 -08003033 return 0;
3034}
3035
Joe Stringerb6989f32017-01-26 13:19:57 -08003036int bpf_map__pin(struct bpf_map *map, const char *path)
3037{
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02003038 char *cp, errmsg[STRERR_BUFSIZE];
Joe Stringerb6989f32017-01-26 13:19:57 -08003039 int err;
3040
3041 err = check_path(path);
3042 if (err)
3043 return err;
3044
3045 if (map == NULL) {
3046 pr_warning("invalid map pointer\n");
3047 return -EINVAL;
3048 }
3049
3050 if (bpf_obj_pin(map->fd, path)) {
Andrey Ignatov24d6a802018-10-03 15:26:41 -07003051 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02003052 pr_warning("failed to pin map: %s\n", cp);
Joe Stringerb6989f32017-01-26 13:19:57 -08003053 return -errno;
3054 }
3055
3056 pr_debug("pinned map '%s'\n", path);
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003057
Joe Stringerb6989f32017-01-26 13:19:57 -08003058 return 0;
3059}
3060
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003061int bpf_map__unpin(struct bpf_map *map, const char *path)
Joe Stringerd5148d82017-01-26 13:19:58 -08003062{
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003063 int err;
3064
3065 err = check_path(path);
3066 if (err)
3067 return err;
3068
3069 if (map == NULL) {
3070 pr_warning("invalid map pointer\n");
3071 return -EINVAL;
3072 }
3073
3074 err = unlink(path);
3075 if (err != 0)
3076 return -errno;
3077 pr_debug("unpinned map '%s'\n", path);
3078
3079 return 0;
3080}
3081
3082int bpf_object__pin_maps(struct bpf_object *obj, const char *path)
3083{
Joe Stringerd5148d82017-01-26 13:19:58 -08003084 struct bpf_map *map;
3085 int err;
3086
3087 if (!obj)
3088 return -ENOENT;
3089
3090 if (!obj->loaded) {
3091 pr_warning("object not yet loaded; load it first\n");
3092 return -ENOENT;
3093 }
3094
3095 err = make_dir(path);
3096 if (err)
3097 return err;
3098
Jakub Kicinskif74a53d92019-02-27 19:04:12 -08003099 bpf_object__for_each_map(map, obj) {
Joe Stringerd5148d82017-01-26 13:19:58 -08003100 char buf[PATH_MAX];
3101 int len;
3102
3103 len = snprintf(buf, PATH_MAX, "%s/%s", path,
3104 bpf_map__name(map));
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003105 if (len < 0) {
3106 err = -EINVAL;
3107 goto err_unpin_maps;
3108 } else if (len >= PATH_MAX) {
3109 err = -ENAMETOOLONG;
3110 goto err_unpin_maps;
3111 }
3112
3113 err = bpf_map__pin(map, buf);
3114 if (err)
3115 goto err_unpin_maps;
3116 }
3117
3118 return 0;
3119
3120err_unpin_maps:
3121 while ((map = bpf_map__prev(map, obj))) {
3122 char buf[PATH_MAX];
3123 int len;
3124
3125 len = snprintf(buf, PATH_MAX, "%s/%s", path,
3126 bpf_map__name(map));
3127 if (len < 0)
3128 continue;
3129 else if (len >= PATH_MAX)
3130 continue;
3131
3132 bpf_map__unpin(map, buf);
3133 }
3134
3135 return err;
3136}
3137
3138int bpf_object__unpin_maps(struct bpf_object *obj, const char *path)
3139{
3140 struct bpf_map *map;
3141 int err;
3142
3143 if (!obj)
3144 return -ENOENT;
3145
Jakub Kicinskif74a53d92019-02-27 19:04:12 -08003146 bpf_object__for_each_map(map, obj) {
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003147 char buf[PATH_MAX];
3148 int len;
3149
3150 len = snprintf(buf, PATH_MAX, "%s/%s", path,
3151 bpf_map__name(map));
Joe Stringerd5148d82017-01-26 13:19:58 -08003152 if (len < 0)
3153 return -EINVAL;
3154 else if (len >= PATH_MAX)
3155 return -ENAMETOOLONG;
3156
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003157 err = bpf_map__unpin(map, buf);
Joe Stringerd5148d82017-01-26 13:19:58 -08003158 if (err)
3159 return err;
3160 }
3161
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003162 return 0;
3163}
3164
3165int bpf_object__pin_programs(struct bpf_object *obj, const char *path)
3166{
3167 struct bpf_program *prog;
3168 int err;
3169
3170 if (!obj)
3171 return -ENOENT;
3172
3173 if (!obj->loaded) {
3174 pr_warning("object not yet loaded; load it first\n");
3175 return -ENOENT;
3176 }
3177
3178 err = make_dir(path);
3179 if (err)
3180 return err;
3181
3182 bpf_object__for_each_program(prog, obj) {
3183 char buf[PATH_MAX];
3184 int len;
3185
3186 len = snprintf(buf, PATH_MAX, "%s/%s", path,
Stanislav Fomichev33a2c752018-11-09 08:21:43 -08003187 prog->pin_name);
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003188 if (len < 0) {
3189 err = -EINVAL;
3190 goto err_unpin_programs;
3191 } else if (len >= PATH_MAX) {
3192 err = -ENAMETOOLONG;
3193 goto err_unpin_programs;
3194 }
3195
3196 err = bpf_program__pin(prog, buf);
3197 if (err)
3198 goto err_unpin_programs;
3199 }
3200
3201 return 0;
3202
3203err_unpin_programs:
3204 while ((prog = bpf_program__prev(prog, obj))) {
3205 char buf[PATH_MAX];
3206 int len;
3207
3208 len = snprintf(buf, PATH_MAX, "%s/%s", path,
Stanislav Fomichev33a2c752018-11-09 08:21:43 -08003209 prog->pin_name);
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003210 if (len < 0)
3211 continue;
3212 else if (len >= PATH_MAX)
3213 continue;
3214
3215 bpf_program__unpin(prog, buf);
3216 }
3217
3218 return err;
3219}
3220
3221int bpf_object__unpin_programs(struct bpf_object *obj, const char *path)
3222{
3223 struct bpf_program *prog;
3224 int err;
3225
3226 if (!obj)
3227 return -ENOENT;
3228
Joe Stringerd5148d82017-01-26 13:19:58 -08003229 bpf_object__for_each_program(prog, obj) {
3230 char buf[PATH_MAX];
3231 int len;
3232
3233 len = snprintf(buf, PATH_MAX, "%s/%s", path,
Stanislav Fomichev33a2c752018-11-09 08:21:43 -08003234 prog->pin_name);
Joe Stringerd5148d82017-01-26 13:19:58 -08003235 if (len < 0)
3236 return -EINVAL;
3237 else if (len >= PATH_MAX)
3238 return -ENAMETOOLONG;
3239
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003240 err = bpf_program__unpin(prog, buf);
Joe Stringerd5148d82017-01-26 13:19:58 -08003241 if (err)
3242 return err;
3243 }
3244
3245 return 0;
3246}
3247
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003248int bpf_object__pin(struct bpf_object *obj, const char *path)
3249{
3250 int err;
3251
3252 err = bpf_object__pin_maps(obj, path);
3253 if (err)
3254 return err;
3255
3256 err = bpf_object__pin_programs(obj, path);
3257 if (err) {
3258 bpf_object__unpin_maps(obj, path);
3259 return err;
3260 }
3261
3262 return 0;
3263}
3264
Wang Nan1a5e3fb2015-07-01 02:13:53 +00003265void bpf_object__close(struct bpf_object *obj)
3266{
Wang Nana5b8bd42015-07-01 02:14:00 +00003267 size_t i;
3268
Wang Nan1a5e3fb2015-07-01 02:13:53 +00003269 if (!obj)
3270 return;
3271
Wang Nan10931d22016-11-26 07:03:26 +00003272 if (obj->clear_priv)
3273 obj->clear_priv(obj, obj->priv);
3274
Wang Nan1a5e3fb2015-07-01 02:13:53 +00003275 bpf_object__elf_finish(obj);
Wang Nan52d33522015-07-01 02:14:04 +00003276 bpf_object__unload(obj);
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07003277 btf__free(obj->btf);
Yonghong Song2993e052018-11-19 15:29:16 -08003278 btf_ext__free(obj->btf_ext);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00003279
Wang Nan9d759a92015-11-27 08:47:35 +00003280 for (i = 0; i < obj->nr_maps; i++) {
Wang Nan561bbcc2015-11-27 08:47:36 +00003281 zfree(&obj->maps[i].name);
Wang Nan9d759a92015-11-27 08:47:35 +00003282 if (obj->maps[i].clear_priv)
3283 obj->maps[i].clear_priv(&obj->maps[i],
3284 obj->maps[i].priv);
3285 obj->maps[i].priv = NULL;
3286 obj->maps[i].clear_priv = NULL;
3287 }
Daniel Borkmannd8599002019-04-09 23:20:13 +02003288
3289 zfree(&obj->sections.rodata);
3290 zfree(&obj->sections.data);
Wang Nan9d759a92015-11-27 08:47:35 +00003291 zfree(&obj->maps);
3292 obj->nr_maps = 0;
Wang Nana5b8bd42015-07-01 02:14:00 +00003293
3294 if (obj->programs && obj->nr_programs) {
3295 for (i = 0; i < obj->nr_programs; i++)
3296 bpf_program__exit(&obj->programs[i]);
3297 }
3298 zfree(&obj->programs);
3299
Wang Nan9a208ef2015-07-01 02:14:10 +00003300 list_del(&obj->list);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00003301 free(obj);
3302}
Wang Nanaa9b1ac2015-07-01 02:14:08 +00003303
Wang Nan9a208ef2015-07-01 02:14:10 +00003304struct bpf_object *
3305bpf_object__next(struct bpf_object *prev)
3306{
3307 struct bpf_object *next;
3308
3309 if (!prev)
3310 next = list_first_entry(&bpf_objects_list,
3311 struct bpf_object,
3312 list);
3313 else
3314 next = list_next_entry(prev, list);
3315
3316 /* Empty list is noticed here so don't need checking on entry. */
3317 if (&next->list == &bpf_objects_list)
3318 return NULL;
3319
3320 return next;
3321}
3322
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07003323const char *bpf_object__name(const struct bpf_object *obj)
Wang Nanacf860a2015-08-27 02:30:55 +00003324{
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03003325 return obj ? obj->path : ERR_PTR(-EINVAL);
Wang Nanacf860a2015-08-27 02:30:55 +00003326}
3327
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07003328unsigned int bpf_object__kversion(const struct bpf_object *obj)
Wang Nan45825d82015-11-06 13:49:38 +00003329{
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03003330 return obj ? obj->kern_version : 0;
Wang Nan45825d82015-11-06 13:49:38 +00003331}
3332
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07003333struct btf *bpf_object__btf(const struct bpf_object *obj)
Andrey Ignatov789f6ba2019-02-14 15:01:43 -08003334{
3335 return obj ? obj->btf : NULL;
3336}
3337
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07003338int bpf_object__btf_fd(const struct bpf_object *obj)
3339{
3340 return obj->btf ? btf__fd(obj->btf) : -1;
3341}
3342
Wang Nan10931d22016-11-26 07:03:26 +00003343int bpf_object__set_priv(struct bpf_object *obj, void *priv,
3344 bpf_object_clear_priv_t clear_priv)
3345{
3346 if (obj->priv && obj->clear_priv)
3347 obj->clear_priv(obj, obj->priv);
3348
3349 obj->priv = priv;
3350 obj->clear_priv = clear_priv;
3351 return 0;
3352}
3353
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07003354void *bpf_object__priv(const struct bpf_object *obj)
Wang Nan10931d22016-11-26 07:03:26 +00003355{
3356 return obj ? obj->priv : ERR_PTR(-EINVAL);
3357}
3358
Jakub Kicinskieac7d842018-06-28 14:41:39 -07003359static struct bpf_program *
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07003360__bpf_program__iter(const struct bpf_program *p, const struct bpf_object *obj,
3361 bool forward)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00003362{
Martin KaFai Laua83d6e72018-11-12 15:44:53 -08003363 size_t nr_programs = obj->nr_programs;
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003364 ssize_t idx;
Wang Nanaa9b1ac2015-07-01 02:14:08 +00003365
Martin KaFai Laua83d6e72018-11-12 15:44:53 -08003366 if (!nr_programs)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00003367 return NULL;
Wang Nanaa9b1ac2015-07-01 02:14:08 +00003368
Martin KaFai Laua83d6e72018-11-12 15:44:53 -08003369 if (!p)
3370 /* Iter from the beginning */
3371 return forward ? &obj->programs[0] :
3372 &obj->programs[nr_programs - 1];
3373
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003374 if (p->obj != obj) {
Wang Nanaa9b1ac2015-07-01 02:14:08 +00003375 pr_warning("error: program handler doesn't match object\n");
3376 return NULL;
3377 }
3378
Martin KaFai Laua83d6e72018-11-12 15:44:53 -08003379 idx = (p - obj->programs) + (forward ? 1 : -1);
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003380 if (idx >= obj->nr_programs || idx < 0)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00003381 return NULL;
3382 return &obj->programs[idx];
3383}
3384
Jakub Kicinskieac7d842018-06-28 14:41:39 -07003385struct bpf_program *
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07003386bpf_program__next(struct bpf_program *prev, const struct bpf_object *obj)
Jakub Kicinskieac7d842018-06-28 14:41:39 -07003387{
3388 struct bpf_program *prog = prev;
3389
3390 do {
Martin KaFai Laua83d6e72018-11-12 15:44:53 -08003391 prog = __bpf_program__iter(prog, obj, true);
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003392 } while (prog && bpf_program__is_function_storage(prog, obj));
3393
3394 return prog;
3395}
3396
3397struct bpf_program *
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07003398bpf_program__prev(struct bpf_program *next, const struct bpf_object *obj)
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003399{
3400 struct bpf_program *prog = next;
3401
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003402 do {
Martin KaFai Laua83d6e72018-11-12 15:44:53 -08003403 prog = __bpf_program__iter(prog, obj, false);
Jakub Kicinskieac7d842018-06-28 14:41:39 -07003404 } while (prog && bpf_program__is_function_storage(prog, obj));
3405
3406 return prog;
3407}
3408
Arnaldo Carvalho de Meloedb13ed2016-06-03 12:38:21 -03003409int bpf_program__set_priv(struct bpf_program *prog, void *priv,
3410 bpf_program_clear_priv_t clear_priv)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00003411{
3412 if (prog->priv && prog->clear_priv)
3413 prog->clear_priv(prog, prog->priv);
3414
3415 prog->priv = priv;
3416 prog->clear_priv = clear_priv;
3417 return 0;
3418}
3419
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07003420void *bpf_program__priv(const struct bpf_program *prog)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00003421{
Arnaldo Carvalho de Melobe834ff2016-06-03 12:36:39 -03003422 return prog ? prog->priv : ERR_PTR(-EINVAL);
Wang Nanaa9b1ac2015-07-01 02:14:08 +00003423}
3424
Jakub Kicinski9aba3612018-06-28 14:41:37 -07003425void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex)
3426{
3427 prog->prog_ifindex = ifindex;
3428}
3429
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07003430const char *bpf_program__title(const struct bpf_program *prog, bool needs_copy)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00003431{
3432 const char *title;
3433
3434 title = prog->section_name;
Namhyung Kim715f8db2015-11-03 20:21:05 +09003435 if (needs_copy) {
Wang Nanaa9b1ac2015-07-01 02:14:08 +00003436 title = strdup(title);
3437 if (!title) {
3438 pr_warning("failed to strdup program title\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +00003439 return ERR_PTR(-ENOMEM);
Wang Nanaa9b1ac2015-07-01 02:14:08 +00003440 }
3441 }
3442
3443 return title;
3444}
3445
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07003446int bpf_program__fd(const struct bpf_program *prog)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00003447{
Wang Nanb5805632015-11-16 12:10:09 +00003448 return bpf_program__nth_fd(prog, 0);
3449}
3450
3451int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
3452 bpf_program_prep_t prep)
3453{
3454 int *instances_fds;
3455
3456 if (nr_instances <= 0 || !prep)
3457 return -EINVAL;
3458
3459 if (prog->instances.nr > 0 || prog->instances.fds) {
3460 pr_warning("Can't set pre-processor after loading\n");
3461 return -EINVAL;
3462 }
3463
3464 instances_fds = malloc(sizeof(int) * nr_instances);
3465 if (!instances_fds) {
3466 pr_warning("alloc memory failed for fds\n");
3467 return -ENOMEM;
3468 }
3469
3470 /* fill all fd with -1 */
3471 memset(instances_fds, -1, sizeof(int) * nr_instances);
3472
3473 prog->instances.nr = nr_instances;
3474 prog->instances.fds = instances_fds;
3475 prog->preprocessor = prep;
3476 return 0;
3477}
3478
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07003479int bpf_program__nth_fd(const struct bpf_program *prog, int n)
Wang Nanb5805632015-11-16 12:10:09 +00003480{
3481 int fd;
3482
Jakub Kicinski1e960042018-07-26 14:32:18 -07003483 if (!prog)
3484 return -EINVAL;
3485
Wang Nanb5805632015-11-16 12:10:09 +00003486 if (n >= prog->instances.nr || n < 0) {
3487 pr_warning("Can't get the %dth fd from program %s: only %d instances\n",
3488 n, prog->section_name, prog->instances.nr);
3489 return -EINVAL;
3490 }
3491
3492 fd = prog->instances.fds[n];
3493 if (fd < 0) {
3494 pr_warning("%dth instance of program '%s' is invalid\n",
3495 n, prog->section_name);
3496 return -ENOENT;
3497 }
3498
3499 return fd;
Wang Nanaa9b1ac2015-07-01 02:14:08 +00003500}
Wang Nan9d759a92015-11-27 08:47:35 +00003501
Alexei Starovoitovdd26b7f2017-03-30 21:45:40 -07003502void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
Wang Nan5f44e4c82016-07-13 10:44:01 +00003503{
3504 prog->type = type;
3505}
3506
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07003507static bool bpf_program__is_type(const struct bpf_program *prog,
Wang Nan5f44e4c82016-07-13 10:44:01 +00003508 enum bpf_prog_type type)
3509{
3510 return prog ? (prog->type == type) : false;
3511}
3512
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07003513#define BPF_PROG_TYPE_FNS(NAME, TYPE) \
3514int bpf_program__set_##NAME(struct bpf_program *prog) \
3515{ \
3516 if (!prog) \
3517 return -EINVAL; \
3518 bpf_program__set_type(prog, TYPE); \
3519 return 0; \
3520} \
3521 \
3522bool bpf_program__is_##NAME(const struct bpf_program *prog) \
3523{ \
3524 return bpf_program__is_type(prog, TYPE); \
3525} \
Wang Nan5f44e4c82016-07-13 10:44:01 +00003526
Joe Stringer7803ba72017-01-22 17:11:24 -08003527BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
Joe Stringered794072017-01-22 17:11:23 -08003528BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE);
Joe Stringer7803ba72017-01-22 17:11:24 -08003529BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS);
3530BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT);
Joe Stringered794072017-01-22 17:11:23 -08003531BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT);
Andrey Ignatove14c93fd2018-04-17 10:28:46 -07003532BPF_PROG_TYPE_FNS(raw_tracepoint, BPF_PROG_TYPE_RAW_TRACEPOINT);
Joe Stringer7803ba72017-01-22 17:11:24 -08003533BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP);
3534BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT);
Wang Nan5f44e4c82016-07-13 10:44:01 +00003535
John Fastabend16962b22018-04-23 14:30:38 -07003536void bpf_program__set_expected_attach_type(struct bpf_program *prog,
3537 enum bpf_attach_type type)
Andrey Ignatovd7be1432018-03-30 15:08:01 -07003538{
3539 prog->expected_attach_type = type;
3540}
3541
Andrey Ignatov36153532018-10-31 12:57:18 -07003542#define BPF_PROG_SEC_IMPL(string, ptype, eatype, is_attachable, atype) \
3543 { string, sizeof(string) - 1, ptype, eatype, is_attachable, atype }
Andrey Ignatovd7be1432018-03-30 15:08:01 -07003544
Andrey Ignatov956b6202018-09-26 15:24:53 -07003545/* Programs that can NOT be attached. */
Andrey Ignatov36153532018-10-31 12:57:18 -07003546#define BPF_PROG_SEC(string, ptype) BPF_PROG_SEC_IMPL(string, ptype, 0, 0, 0)
Andrey Ignatovd7be1432018-03-30 15:08:01 -07003547
Andrey Ignatov956b6202018-09-26 15:24:53 -07003548/* Programs that can be attached. */
3549#define BPF_APROG_SEC(string, ptype, atype) \
Andrey Ignatov36153532018-10-31 12:57:18 -07003550 BPF_PROG_SEC_IMPL(string, ptype, 0, 1, atype)
Andrey Ignatov81efee72018-04-17 10:28:45 -07003551
Andrey Ignatov956b6202018-09-26 15:24:53 -07003552/* Programs that must specify expected attach type at load time. */
3553#define BPF_EAPROG_SEC(string, ptype, eatype) \
Andrey Ignatov36153532018-10-31 12:57:18 -07003554 BPF_PROG_SEC_IMPL(string, ptype, eatype, 1, eatype)
Andrey Ignatov956b6202018-09-26 15:24:53 -07003555
3556/* Programs that can be attached but attach type can't be identified by section
3557 * name. Kept for backward compatibility.
3558 */
3559#define BPF_APROG_COMPAT(string, ptype) BPF_PROG_SEC(string, ptype)
Andrey Ignatove50b0a62018-03-30 15:08:03 -07003560
Roman Gushchin583c9002017-12-13 15:18:51 +00003561static const struct {
3562 const char *sec;
3563 size_t len;
3564 enum bpf_prog_type prog_type;
Andrey Ignatovd7be1432018-03-30 15:08:01 -07003565 enum bpf_attach_type expected_attach_type;
Andrey Ignatov36153532018-10-31 12:57:18 -07003566 int is_attachable;
Andrey Ignatov956b6202018-09-26 15:24:53 -07003567 enum bpf_attach_type attach_type;
Roman Gushchin583c9002017-12-13 15:18:51 +00003568} section_names[] = {
Andrey Ignatov956b6202018-09-26 15:24:53 -07003569 BPF_PROG_SEC("socket", BPF_PROG_TYPE_SOCKET_FILTER),
3570 BPF_PROG_SEC("kprobe/", BPF_PROG_TYPE_KPROBE),
3571 BPF_PROG_SEC("kretprobe/", BPF_PROG_TYPE_KPROBE),
3572 BPF_PROG_SEC("classifier", BPF_PROG_TYPE_SCHED_CLS),
3573 BPF_PROG_SEC("action", BPF_PROG_TYPE_SCHED_ACT),
3574 BPF_PROG_SEC("tracepoint/", BPF_PROG_TYPE_TRACEPOINT),
3575 BPF_PROG_SEC("raw_tracepoint/", BPF_PROG_TYPE_RAW_TRACEPOINT),
3576 BPF_PROG_SEC("xdp", BPF_PROG_TYPE_XDP),
3577 BPF_PROG_SEC("perf_event", BPF_PROG_TYPE_PERF_EVENT),
3578 BPF_PROG_SEC("lwt_in", BPF_PROG_TYPE_LWT_IN),
3579 BPF_PROG_SEC("lwt_out", BPF_PROG_TYPE_LWT_OUT),
3580 BPF_PROG_SEC("lwt_xmit", BPF_PROG_TYPE_LWT_XMIT),
3581 BPF_PROG_SEC("lwt_seg6local", BPF_PROG_TYPE_LWT_SEG6LOCAL),
Andrey Ignatovbafa7af2018-09-26 15:24:54 -07003582 BPF_APROG_SEC("cgroup_skb/ingress", BPF_PROG_TYPE_CGROUP_SKB,
3583 BPF_CGROUP_INET_INGRESS),
3584 BPF_APROG_SEC("cgroup_skb/egress", BPF_PROG_TYPE_CGROUP_SKB,
3585 BPF_CGROUP_INET_EGRESS),
Andrey Ignatov956b6202018-09-26 15:24:53 -07003586 BPF_APROG_COMPAT("cgroup/skb", BPF_PROG_TYPE_CGROUP_SKB),
3587 BPF_APROG_SEC("cgroup/sock", BPF_PROG_TYPE_CGROUP_SOCK,
3588 BPF_CGROUP_INET_SOCK_CREATE),
3589 BPF_EAPROG_SEC("cgroup/post_bind4", BPF_PROG_TYPE_CGROUP_SOCK,
3590 BPF_CGROUP_INET4_POST_BIND),
3591 BPF_EAPROG_SEC("cgroup/post_bind6", BPF_PROG_TYPE_CGROUP_SOCK,
3592 BPF_CGROUP_INET6_POST_BIND),
3593 BPF_APROG_SEC("cgroup/dev", BPF_PROG_TYPE_CGROUP_DEVICE,
3594 BPF_CGROUP_DEVICE),
3595 BPF_APROG_SEC("sockops", BPF_PROG_TYPE_SOCK_OPS,
3596 BPF_CGROUP_SOCK_OPS),
Andrey Ignatovc6f68512018-09-26 15:24:55 -07003597 BPF_APROG_SEC("sk_skb/stream_parser", BPF_PROG_TYPE_SK_SKB,
3598 BPF_SK_SKB_STREAM_PARSER),
3599 BPF_APROG_SEC("sk_skb/stream_verdict", BPF_PROG_TYPE_SK_SKB,
3600 BPF_SK_SKB_STREAM_VERDICT),
Andrey Ignatov956b6202018-09-26 15:24:53 -07003601 BPF_APROG_COMPAT("sk_skb", BPF_PROG_TYPE_SK_SKB),
3602 BPF_APROG_SEC("sk_msg", BPF_PROG_TYPE_SK_MSG,
3603 BPF_SK_MSG_VERDICT),
3604 BPF_APROG_SEC("lirc_mode2", BPF_PROG_TYPE_LIRC_MODE2,
3605 BPF_LIRC_MODE2),
3606 BPF_APROG_SEC("flow_dissector", BPF_PROG_TYPE_FLOW_DISSECTOR,
3607 BPF_FLOW_DISSECTOR),
3608 BPF_EAPROG_SEC("cgroup/bind4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
3609 BPF_CGROUP_INET4_BIND),
3610 BPF_EAPROG_SEC("cgroup/bind6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
3611 BPF_CGROUP_INET6_BIND),
3612 BPF_EAPROG_SEC("cgroup/connect4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
3613 BPF_CGROUP_INET4_CONNECT),
3614 BPF_EAPROG_SEC("cgroup/connect6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
3615 BPF_CGROUP_INET6_CONNECT),
3616 BPF_EAPROG_SEC("cgroup/sendmsg4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
3617 BPF_CGROUP_UDP4_SENDMSG),
3618 BPF_EAPROG_SEC("cgroup/sendmsg6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
3619 BPF_CGROUP_UDP6_SENDMSG),
Daniel Borkmann9bb59ac2019-06-07 01:48:59 +02003620 BPF_EAPROG_SEC("cgroup/recvmsg4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
3621 BPF_CGROUP_UDP4_RECVMSG),
3622 BPF_EAPROG_SEC("cgroup/recvmsg6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
3623 BPF_CGROUP_UDP6_RECVMSG),
Andrey Ignatov063cc9f2019-03-08 09:15:26 -08003624 BPF_EAPROG_SEC("cgroup/sysctl", BPF_PROG_TYPE_CGROUP_SYSCTL,
3625 BPF_CGROUP_SYSCTL),
Stanislav Fomichev4cdbfb52019-06-27 13:38:49 -07003626 BPF_EAPROG_SEC("cgroup/getsockopt", BPF_PROG_TYPE_CGROUP_SOCKOPT,
3627 BPF_CGROUP_GETSOCKOPT),
3628 BPF_EAPROG_SEC("cgroup/setsockopt", BPF_PROG_TYPE_CGROUP_SOCKOPT,
3629 BPF_CGROUP_SETSOCKOPT),
Roman Gushchin583c9002017-12-13 15:18:51 +00003630};
Roman Gushchin583c9002017-12-13 15:18:51 +00003631
Andrey Ignatov956b6202018-09-26 15:24:53 -07003632#undef BPF_PROG_SEC_IMPL
Andrey Ignatovd7be1432018-03-30 15:08:01 -07003633#undef BPF_PROG_SEC
Andrey Ignatov956b6202018-09-26 15:24:53 -07003634#undef BPF_APROG_SEC
3635#undef BPF_EAPROG_SEC
3636#undef BPF_APROG_COMPAT
Andrey Ignatovd7be1432018-03-30 15:08:01 -07003637
Taeung Songc76e4c22019-01-21 22:06:38 +09003638#define MAX_TYPE_NAME_SIZE 32
3639
3640static char *libbpf_get_type_names(bool attach_type)
3641{
3642 int i, len = ARRAY_SIZE(section_names) * MAX_TYPE_NAME_SIZE;
3643 char *buf;
3644
3645 buf = malloc(len);
3646 if (!buf)
3647 return NULL;
3648
3649 buf[0] = '\0';
3650 /* Forge string buf with all available names */
3651 for (i = 0; i < ARRAY_SIZE(section_names); i++) {
3652 if (attach_type && !section_names[i].is_attachable)
3653 continue;
3654
3655 if (strlen(buf) + strlen(section_names[i].sec) + 2 > len) {
3656 free(buf);
3657 return NULL;
3658 }
3659 strcat(buf, " ");
3660 strcat(buf, section_names[i].sec);
3661 }
3662
3663 return buf;
3664}
3665
Jakub Kicinskib60df2a2018-07-10 14:42:59 -07003666int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
3667 enum bpf_attach_type *expected_attach_type)
Roman Gushchin583c9002017-12-13 15:18:51 +00003668{
Taeung Songc76e4c22019-01-21 22:06:38 +09003669 char *type_names;
Roman Gushchin583c9002017-12-13 15:18:51 +00003670 int i;
3671
Jakub Kicinskib60df2a2018-07-10 14:42:59 -07003672 if (!name)
3673 return -EINVAL;
Roman Gushchin583c9002017-12-13 15:18:51 +00003674
Jakub Kicinskib60df2a2018-07-10 14:42:59 -07003675 for (i = 0; i < ARRAY_SIZE(section_names); i++) {
3676 if (strncmp(name, section_names[i].sec, section_names[i].len))
3677 continue;
3678 *prog_type = section_names[i].prog_type;
3679 *expected_attach_type = section_names[i].expected_attach_type;
3680 return 0;
3681 }
Taeung Songc76e4c22019-01-21 22:06:38 +09003682 pr_warning("failed to guess program type based on ELF section name '%s'\n", name);
3683 type_names = libbpf_get_type_names(false);
3684 if (type_names != NULL) {
3685 pr_info("supported section(type) names are:%s\n", type_names);
3686 free(type_names);
3687 }
3688
Jakub Kicinskib60df2a2018-07-10 14:42:59 -07003689 return -EINVAL;
3690}
Roman Gushchin583c9002017-12-13 15:18:51 +00003691
Andrey Ignatov956b6202018-09-26 15:24:53 -07003692int libbpf_attach_type_by_name(const char *name,
3693 enum bpf_attach_type *attach_type)
3694{
Taeung Songc76e4c22019-01-21 22:06:38 +09003695 char *type_names;
Andrey Ignatov956b6202018-09-26 15:24:53 -07003696 int i;
3697
3698 if (!name)
3699 return -EINVAL;
3700
3701 for (i = 0; i < ARRAY_SIZE(section_names); i++) {
3702 if (strncmp(name, section_names[i].sec, section_names[i].len))
3703 continue;
Andrey Ignatov36153532018-10-31 12:57:18 -07003704 if (!section_names[i].is_attachable)
Andrey Ignatov956b6202018-09-26 15:24:53 -07003705 return -EINVAL;
3706 *attach_type = section_names[i].attach_type;
3707 return 0;
3708 }
Taeung Songc76e4c22019-01-21 22:06:38 +09003709 pr_warning("failed to guess attach type based on ELF section name '%s'\n", name);
3710 type_names = libbpf_get_type_names(true);
3711 if (type_names != NULL) {
3712 pr_info("attachable section(type) names are:%s\n", type_names);
3713 free(type_names);
3714 }
3715
Andrey Ignatov956b6202018-09-26 15:24:53 -07003716 return -EINVAL;
3717}
3718
Jakub Kicinskib60df2a2018-07-10 14:42:59 -07003719static int
3720bpf_program__identify_section(struct bpf_program *prog,
3721 enum bpf_prog_type *prog_type,
3722 enum bpf_attach_type *expected_attach_type)
3723{
3724 return libbpf_prog_type_by_name(prog->section_name, prog_type,
3725 expected_attach_type);
Roman Gushchin583c9002017-12-13 15:18:51 +00003726}
3727
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07003728int bpf_map__fd(const struct bpf_map *map)
Wang Nan9d759a92015-11-27 08:47:35 +00003729{
Arnaldo Carvalho de Melo6e009e652016-06-03 12:15:52 -03003730 return map ? map->fd : -EINVAL;
Wang Nan9d759a92015-11-27 08:47:35 +00003731}
3732
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07003733const struct bpf_map_def *bpf_map__def(const struct bpf_map *map)
Wang Nan9d759a92015-11-27 08:47:35 +00003734{
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03003735 return map ? &map->def : ERR_PTR(-EINVAL);
Wang Nan9d759a92015-11-27 08:47:35 +00003736}
3737
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07003738const char *bpf_map__name(const struct bpf_map *map)
Wang Nan561bbcc2015-11-27 08:47:36 +00003739{
Arnaldo Carvalho de Melo009ad5d2016-06-02 11:02:05 -03003740 return map ? map->name : NULL;
Wang Nan561bbcc2015-11-27 08:47:36 +00003741}
3742
Martin KaFai Lau5b891af2018-07-24 08:40:21 -07003743__u32 bpf_map__btf_key_type_id(const struct bpf_map *map)
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07003744{
Martin KaFai Lau61746dbe2018-05-22 15:04:24 -07003745 return map ? map->btf_key_type_id : 0;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07003746}
3747
Martin KaFai Lau5b891af2018-07-24 08:40:21 -07003748__u32 bpf_map__btf_value_type_id(const struct bpf_map *map)
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07003749{
Martin KaFai Lau61746dbe2018-05-22 15:04:24 -07003750 return map ? map->btf_value_type_id : 0;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07003751}
3752
Arnaldo Carvalho de Meloedb13ed2016-06-03 12:38:21 -03003753int bpf_map__set_priv(struct bpf_map *map, void *priv,
3754 bpf_map_clear_priv_t clear_priv)
Wang Nan9d759a92015-11-27 08:47:35 +00003755{
3756 if (!map)
3757 return -EINVAL;
3758
3759 if (map->priv) {
3760 if (map->clear_priv)
3761 map->clear_priv(map, map->priv);
3762 }
3763
3764 map->priv = priv;
3765 map->clear_priv = clear_priv;
3766 return 0;
3767}
3768
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07003769void *bpf_map__priv(const struct bpf_map *map)
Wang Nan9d759a92015-11-27 08:47:35 +00003770{
Arnaldo Carvalho de Melob4cbfa52016-06-02 10:51:59 -03003771 return map ? map->priv : ERR_PTR(-EINVAL);
Wang Nan9d759a92015-11-27 08:47:35 +00003772}
3773
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07003774bool bpf_map__is_offload_neutral(const struct bpf_map *map)
Jakub Kicinskif83fb222018-07-10 14:43:01 -07003775{
3776 return map->def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
3777}
3778
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07003779bool bpf_map__is_internal(const struct bpf_map *map)
Daniel Borkmannd8599002019-04-09 23:20:13 +02003780{
3781 return map->libbpf_type != LIBBPF_MAP_UNSPEC;
3782}
3783
Jakub Kicinski9aba3612018-06-28 14:41:37 -07003784void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex)
3785{
3786 map->map_ifindex = ifindex;
3787}
3788
Nikita V. Shirokovaddb9fc2018-11-20 20:55:56 -08003789int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd)
3790{
3791 if (!bpf_map_type__is_map_in_map(map->def.type)) {
3792 pr_warning("error: unsupported map type\n");
3793 return -EINVAL;
3794 }
3795 if (map->inner_map_fd != -1) {
3796 pr_warning("error: inner_map_fd already specified\n");
3797 return -EINVAL;
3798 }
3799 map->inner_map_fd = fd;
3800 return 0;
3801}
3802
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003803static struct bpf_map *
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07003804__bpf_map__iter(const struct bpf_map *m, const struct bpf_object *obj, int i)
Wang Nan9d759a92015-11-27 08:47:35 +00003805{
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003806 ssize_t idx;
Wang Nan9d759a92015-11-27 08:47:35 +00003807 struct bpf_map *s, *e;
3808
3809 if (!obj || !obj->maps)
3810 return NULL;
3811
3812 s = obj->maps;
3813 e = obj->maps + obj->nr_maps;
3814
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003815 if ((m < s) || (m >= e)) {
Wang Nan9d759a92015-11-27 08:47:35 +00003816 pr_warning("error in %s: map handler doesn't belong to object\n",
3817 __func__);
3818 return NULL;
3819 }
3820
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003821 idx = (m - obj->maps) + i;
3822 if (idx >= obj->nr_maps || idx < 0)
Wang Nan9d759a92015-11-27 08:47:35 +00003823 return NULL;
3824 return &obj->maps[idx];
3825}
Wang Nan561bbcc2015-11-27 08:47:36 +00003826
3827struct bpf_map *
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07003828bpf_map__next(const struct bpf_map *prev, const struct bpf_object *obj)
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003829{
3830 if (prev == NULL)
3831 return obj->maps;
3832
3833 return __bpf_map__iter(prev, obj, 1);
3834}
3835
3836struct bpf_map *
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07003837bpf_map__prev(const struct bpf_map *next, const struct bpf_object *obj)
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003838{
3839 if (next == NULL) {
3840 if (!obj->nr_maps)
3841 return NULL;
3842 return obj->maps + obj->nr_maps - 1;
3843 }
3844
3845 return __bpf_map__iter(next, obj, -1);
3846}
3847
3848struct bpf_map *
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07003849bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name)
Wang Nan561bbcc2015-11-27 08:47:36 +00003850{
3851 struct bpf_map *pos;
3852
Jakub Kicinskif74a53d92019-02-27 19:04:12 -08003853 bpf_object__for_each_map(pos, obj) {
Wang Nan973170e2015-12-08 02:25:29 +00003854 if (pos->name && !strcmp(pos->name, name))
Wang Nan561bbcc2015-11-27 08:47:36 +00003855 return pos;
3856 }
3857 return NULL;
3858}
Wang Nan5a6acad2016-11-26 07:03:27 +00003859
Maciej Fijalkowskif3cea322019-02-01 22:42:23 +01003860int
Andrii Nakryikoa324aae2019-06-17 15:48:58 -07003861bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name)
Maciej Fijalkowskif3cea322019-02-01 22:42:23 +01003862{
3863 return bpf_map__fd(bpf_object__find_map_by_name(obj, name));
3864}
3865
Wang Nan5a6acad2016-11-26 07:03:27 +00003866struct bpf_map *
3867bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
3868{
Andrii Nakryikodb488142019-06-17 12:26:54 -07003869 return ERR_PTR(-ENOTSUP);
Wang Nan5a6acad2016-11-26 07:03:27 +00003870}
Joe Stringere28ff1a2017-01-22 17:11:25 -08003871
3872long libbpf_get_error(const void *ptr)
3873{
Hariprasad Kelamd98363b2019-05-25 14:32:57 +05303874 return PTR_ERR_OR_ZERO(ptr);
Joe Stringere28ff1a2017-01-22 17:11:25 -08003875}
John Fastabend6f6d33f2017-08-15 22:34:22 -07003876
3877int bpf_prog_load(const char *file, enum bpf_prog_type type,
3878 struct bpf_object **pobj, int *prog_fd)
3879{
Andrey Ignatovd7be1432018-03-30 15:08:01 -07003880 struct bpf_prog_load_attr attr;
3881
3882 memset(&attr, 0, sizeof(struct bpf_prog_load_attr));
3883 attr.file = file;
3884 attr.prog_type = type;
3885 attr.expected_attach_type = 0;
3886
3887 return bpf_prog_load_xattr(&attr, pobj, prog_fd);
3888}
3889
3890int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
3891 struct bpf_object **pobj, int *prog_fd)
3892{
Leo Yan33bae182019-07-02 18:25:31 +08003893 struct bpf_object_open_attr open_attr = {};
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08003894 struct bpf_program *prog, *first_prog = NULL;
Andrey Ignatovd7be1432018-03-30 15:08:01 -07003895 enum bpf_attach_type expected_attach_type;
3896 enum bpf_prog_type prog_type;
John Fastabend6f6d33f2017-08-15 22:34:22 -07003897 struct bpf_object *obj;
David Beckettf0307a72018-05-16 14:02:49 -07003898 struct bpf_map *map;
John Fastabend6f6d33f2017-08-15 22:34:22 -07003899 int err;
3900
Andrey Ignatovd7be1432018-03-30 15:08:01 -07003901 if (!attr)
3902 return -EINVAL;
Jakub Kicinski17387dd2018-05-10 10:24:42 -07003903 if (!attr->file)
3904 return -EINVAL;
Andrey Ignatovd7be1432018-03-30 15:08:01 -07003905
Leo Yan33bae182019-07-02 18:25:31 +08003906 open_attr.file = attr->file;
3907 open_attr.prog_type = attr->prog_type;
3908
Jakub Kicinski07f2d4e2018-07-10 14:43:02 -07003909 obj = bpf_object__open_xattr(&open_attr);
Jakub Kicinski35976832018-05-10 10:09:34 -07003910 if (IS_ERR_OR_NULL(obj))
John Fastabend6f6d33f2017-08-15 22:34:22 -07003911 return -ENOENT;
3912
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08003913 bpf_object__for_each_program(prog, obj) {
3914 /*
3915 * If type is not specified, try to guess it based on
3916 * section name.
3917 */
Andrey Ignatovd7be1432018-03-30 15:08:01 -07003918 prog_type = attr->prog_type;
David Beckettf0307a72018-05-16 14:02:49 -07003919 prog->prog_ifindex = attr->ifindex;
Andrey Ignatovd7be1432018-03-30 15:08:01 -07003920 expected_attach_type = attr->expected_attach_type;
3921 if (prog_type == BPF_PROG_TYPE_UNSPEC) {
Jakub Kicinskib60df2a2018-07-10 14:42:59 -07003922 err = bpf_program__identify_section(prog, &prog_type,
3923 &expected_attach_type);
3924 if (err < 0) {
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08003925 bpf_object__close(obj);
3926 return -EINVAL;
3927 }
3928 }
3929
Andrey Ignatovd7be1432018-03-30 15:08:01 -07003930 bpf_program__set_type(prog, prog_type);
3931 bpf_program__set_expected_attach_type(prog,
3932 expected_attach_type);
3933
Alexei Starovoitovda11b412019-04-01 21:27:47 -07003934 prog->log_level = attr->log_level;
Jiong Wang04656192019-05-24 23:25:19 +01003935 prog->prog_flags = attr->prog_flags;
Taeung Song69495d22018-09-03 08:30:07 +09003936 if (!first_prog)
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08003937 first_prog = prog;
3938 }
3939
Jakub Kicinskif74a53d92019-02-27 19:04:12 -08003940 bpf_object__for_each_map(map, obj) {
Jakub Kicinskif83fb222018-07-10 14:43:01 -07003941 if (!bpf_map__is_offload_neutral(map))
3942 map->map_ifindex = attr->ifindex;
David Beckettf0307a72018-05-16 14:02:49 -07003943 }
3944
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08003945 if (!first_prog) {
3946 pr_warning("object file doesn't contain bpf program\n");
John Fastabend6f6d33f2017-08-15 22:34:22 -07003947 bpf_object__close(obj);
3948 return -ENOENT;
3949 }
3950
John Fastabend6f6d33f2017-08-15 22:34:22 -07003951 err = bpf_object__load(obj);
3952 if (err) {
3953 bpf_object__close(obj);
3954 return -EINVAL;
3955 }
3956
3957 *pobj = obj;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08003958 *prog_fd = bpf_program__fd(first_prog);
John Fastabend6f6d33f2017-08-15 22:34:22 -07003959 return 0;
3960}
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07003961
Andrii Nakryiko1c2e9ef2019-07-01 16:58:56 -07003962struct bpf_link {
3963 int (*destroy)(struct bpf_link *link);
3964};
3965
3966int bpf_link__destroy(struct bpf_link *link)
3967{
3968 int err;
3969
3970 if (!link)
3971 return 0;
3972
3973 err = link->destroy(link);
3974 free(link);
3975
3976 return err;
3977}
3978
Andrii Nakryiko63f2f5e2019-07-01 16:58:57 -07003979struct bpf_link_fd {
3980 struct bpf_link link; /* has to be at the top of struct */
3981 int fd; /* hook FD */
3982};
3983
3984static int bpf_link__destroy_perf_event(struct bpf_link *link)
3985{
3986 struct bpf_link_fd *l = (void *)link;
3987 int err;
3988
3989 err = ioctl(l->fd, PERF_EVENT_IOC_DISABLE, 0);
3990 if (err)
3991 err = -errno;
3992
3993 close(l->fd);
3994 return err;
3995}
3996
3997struct bpf_link *bpf_program__attach_perf_event(struct bpf_program *prog,
3998 int pfd)
3999{
4000 char errmsg[STRERR_BUFSIZE];
4001 struct bpf_link_fd *link;
4002 int prog_fd, err;
4003
4004 if (pfd < 0) {
4005 pr_warning("program '%s': invalid perf event FD %d\n",
4006 bpf_program__title(prog, false), pfd);
4007 return ERR_PTR(-EINVAL);
4008 }
4009 prog_fd = bpf_program__fd(prog);
4010 if (prog_fd < 0) {
4011 pr_warning("program '%s': can't attach BPF program w/o FD (did you load it?)\n",
4012 bpf_program__title(prog, false));
4013 return ERR_PTR(-EINVAL);
4014 }
4015
4016 link = malloc(sizeof(*link));
4017 if (!link)
4018 return ERR_PTR(-ENOMEM);
4019 link->link.destroy = &bpf_link__destroy_perf_event;
4020 link->fd = pfd;
4021
4022 if (ioctl(pfd, PERF_EVENT_IOC_SET_BPF, prog_fd) < 0) {
4023 err = -errno;
4024 free(link);
4025 pr_warning("program '%s': failed to attach to pfd %d: %s\n",
4026 bpf_program__title(prog, false), pfd,
4027 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
4028 return ERR_PTR(err);
4029 }
4030 if (ioctl(pfd, PERF_EVENT_IOC_ENABLE, 0) < 0) {
4031 err = -errno;
4032 free(link);
4033 pr_warning("program '%s': failed to enable pfd %d: %s\n",
4034 bpf_program__title(prog, false), pfd,
4035 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
4036 return ERR_PTR(err);
4037 }
4038 return (struct bpf_link *)link;
4039}
4040
Andrii Nakryikob2650022019-07-01 16:58:58 -07004041/*
4042 * this function is expected to parse integer in the range of [0, 2^31-1] from
4043 * given file using scanf format string fmt. If actual parsed value is
4044 * negative, the result might be indistinguishable from error
4045 */
4046static int parse_uint_from_file(const char *file, const char *fmt)
4047{
4048 char buf[STRERR_BUFSIZE];
4049 int err, ret;
4050 FILE *f;
4051
4052 f = fopen(file, "r");
4053 if (!f) {
4054 err = -errno;
4055 pr_debug("failed to open '%s': %s\n", file,
4056 libbpf_strerror_r(err, buf, sizeof(buf)));
4057 return err;
4058 }
4059 err = fscanf(f, fmt, &ret);
4060 if (err != 1) {
4061 err = err == EOF ? -EIO : -errno;
4062 pr_debug("failed to parse '%s': %s\n", file,
4063 libbpf_strerror_r(err, buf, sizeof(buf)));
4064 fclose(f);
4065 return err;
4066 }
4067 fclose(f);
4068 return ret;
4069}
4070
4071static int determine_kprobe_perf_type(void)
4072{
4073 const char *file = "/sys/bus/event_source/devices/kprobe/type";
4074
4075 return parse_uint_from_file(file, "%d\n");
4076}
4077
4078static int determine_uprobe_perf_type(void)
4079{
4080 const char *file = "/sys/bus/event_source/devices/uprobe/type";
4081
4082 return parse_uint_from_file(file, "%d\n");
4083}
4084
4085static int determine_kprobe_retprobe_bit(void)
4086{
4087 const char *file = "/sys/bus/event_source/devices/kprobe/format/retprobe";
4088
4089 return parse_uint_from_file(file, "config:%d\n");
4090}
4091
4092static int determine_uprobe_retprobe_bit(void)
4093{
4094 const char *file = "/sys/bus/event_source/devices/uprobe/format/retprobe";
4095
4096 return parse_uint_from_file(file, "config:%d\n");
4097}
4098
4099static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
4100 uint64_t offset, int pid)
4101{
4102 struct perf_event_attr attr = {};
4103 char errmsg[STRERR_BUFSIZE];
4104 int type, pfd, err;
4105
4106 type = uprobe ? determine_uprobe_perf_type()
4107 : determine_kprobe_perf_type();
4108 if (type < 0) {
4109 pr_warning("failed to determine %s perf type: %s\n",
4110 uprobe ? "uprobe" : "kprobe",
4111 libbpf_strerror_r(type, errmsg, sizeof(errmsg)));
4112 return type;
4113 }
4114 if (retprobe) {
4115 int bit = uprobe ? determine_uprobe_retprobe_bit()
4116 : determine_kprobe_retprobe_bit();
4117
4118 if (bit < 0) {
4119 pr_warning("failed to determine %s retprobe bit: %s\n",
4120 uprobe ? "uprobe" : "kprobe",
4121 libbpf_strerror_r(bit, errmsg,
4122 sizeof(errmsg)));
4123 return bit;
4124 }
4125 attr.config |= 1 << bit;
4126 }
4127 attr.size = sizeof(attr);
4128 attr.type = type;
Andrii Nakryiko36db2a92019-07-08 21:00:07 -07004129 attr.config1 = ptr_to_u64(name); /* kprobe_func or uprobe_path */
4130 attr.config2 = offset; /* kprobe_addr or probe_offset */
Andrii Nakryikob2650022019-07-01 16:58:58 -07004131
4132 /* pid filter is meaningful only for uprobes */
4133 pfd = syscall(__NR_perf_event_open, &attr,
4134 pid < 0 ? -1 : pid /* pid */,
4135 pid == -1 ? 0 : -1 /* cpu */,
4136 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
4137 if (pfd < 0) {
4138 err = -errno;
4139 pr_warning("%s perf_event_open() failed: %s\n",
4140 uprobe ? "uprobe" : "kprobe",
4141 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
4142 return err;
4143 }
4144 return pfd;
4145}
4146
4147struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog,
4148 bool retprobe,
4149 const char *func_name)
4150{
4151 char errmsg[STRERR_BUFSIZE];
4152 struct bpf_link *link;
4153 int pfd, err;
4154
4155 pfd = perf_event_open_probe(false /* uprobe */, retprobe, func_name,
4156 0 /* offset */, -1 /* pid */);
4157 if (pfd < 0) {
4158 pr_warning("program '%s': failed to create %s '%s' perf event: %s\n",
4159 bpf_program__title(prog, false),
4160 retprobe ? "kretprobe" : "kprobe", func_name,
4161 libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
4162 return ERR_PTR(pfd);
4163 }
4164 link = bpf_program__attach_perf_event(prog, pfd);
4165 if (IS_ERR(link)) {
4166 close(pfd);
4167 err = PTR_ERR(link);
4168 pr_warning("program '%s': failed to attach to %s '%s': %s\n",
4169 bpf_program__title(prog, false),
4170 retprobe ? "kretprobe" : "kprobe", func_name,
4171 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
4172 return link;
4173 }
4174 return link;
4175}
4176
4177struct bpf_link *bpf_program__attach_uprobe(struct bpf_program *prog,
4178 bool retprobe, pid_t pid,
4179 const char *binary_path,
4180 size_t func_offset)
4181{
4182 char errmsg[STRERR_BUFSIZE];
4183 struct bpf_link *link;
4184 int pfd, err;
4185
4186 pfd = perf_event_open_probe(true /* uprobe */, retprobe,
4187 binary_path, func_offset, pid);
4188 if (pfd < 0) {
4189 pr_warning("program '%s': failed to create %s '%s:0x%zx' perf event: %s\n",
4190 bpf_program__title(prog, false),
4191 retprobe ? "uretprobe" : "uprobe",
4192 binary_path, func_offset,
4193 libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
4194 return ERR_PTR(pfd);
4195 }
4196 link = bpf_program__attach_perf_event(prog, pfd);
4197 if (IS_ERR(link)) {
4198 close(pfd);
4199 err = PTR_ERR(link);
4200 pr_warning("program '%s': failed to attach to %s '%s:0x%zx': %s\n",
4201 bpf_program__title(prog, false),
4202 retprobe ? "uretprobe" : "uprobe",
4203 binary_path, func_offset,
4204 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
4205 return link;
4206 }
4207 return link;
4208}
4209
Andrii Nakryikof6de59c2019-07-01 16:58:59 -07004210static int determine_tracepoint_id(const char *tp_category,
4211 const char *tp_name)
4212{
4213 char file[PATH_MAX];
4214 int ret;
4215
4216 ret = snprintf(file, sizeof(file),
4217 "/sys/kernel/debug/tracing/events/%s/%s/id",
4218 tp_category, tp_name);
4219 if (ret < 0)
4220 return -errno;
4221 if (ret >= sizeof(file)) {
4222 pr_debug("tracepoint %s/%s path is too long\n",
4223 tp_category, tp_name);
4224 return -E2BIG;
4225 }
4226 return parse_uint_from_file(file, "%d\n");
4227}
4228
4229static int perf_event_open_tracepoint(const char *tp_category,
4230 const char *tp_name)
4231{
4232 struct perf_event_attr attr = {};
4233 char errmsg[STRERR_BUFSIZE];
4234 int tp_id, pfd, err;
4235
4236 tp_id = determine_tracepoint_id(tp_category, tp_name);
4237 if (tp_id < 0) {
4238 pr_warning("failed to determine tracepoint '%s/%s' perf event ID: %s\n",
4239 tp_category, tp_name,
4240 libbpf_strerror_r(tp_id, errmsg, sizeof(errmsg)));
4241 return tp_id;
4242 }
4243
4244 attr.type = PERF_TYPE_TRACEPOINT;
4245 attr.size = sizeof(attr);
4246 attr.config = tp_id;
4247
4248 pfd = syscall(__NR_perf_event_open, &attr, -1 /* pid */, 0 /* cpu */,
4249 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
4250 if (pfd < 0) {
4251 err = -errno;
4252 pr_warning("tracepoint '%s/%s' perf_event_open() failed: %s\n",
4253 tp_category, tp_name,
4254 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
4255 return err;
4256 }
4257 return pfd;
4258}
4259
4260struct bpf_link *bpf_program__attach_tracepoint(struct bpf_program *prog,
4261 const char *tp_category,
4262 const char *tp_name)
4263{
4264 char errmsg[STRERR_BUFSIZE];
4265 struct bpf_link *link;
4266 int pfd, err;
4267
4268 pfd = perf_event_open_tracepoint(tp_category, tp_name);
4269 if (pfd < 0) {
4270 pr_warning("program '%s': failed to create tracepoint '%s/%s' perf event: %s\n",
4271 bpf_program__title(prog, false),
4272 tp_category, tp_name,
4273 libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
4274 return ERR_PTR(pfd);
4275 }
4276 link = bpf_program__attach_perf_event(prog, pfd);
4277 if (IS_ERR(link)) {
4278 close(pfd);
4279 err = PTR_ERR(link);
4280 pr_warning("program '%s': failed to attach to tracepoint '%s/%s': %s\n",
4281 bpf_program__title(prog, false),
4282 tp_category, tp_name,
4283 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
4284 return link;
4285 }
4286 return link;
4287}
4288
Andrii Nakryiko84bf5e12019-07-01 16:59:00 -07004289static int bpf_link__destroy_fd(struct bpf_link *link)
4290{
4291 struct bpf_link_fd *l = (void *)link;
4292
4293 return close(l->fd);
4294}
4295
4296struct bpf_link *bpf_program__attach_raw_tracepoint(struct bpf_program *prog,
4297 const char *tp_name)
4298{
4299 char errmsg[STRERR_BUFSIZE];
4300 struct bpf_link_fd *link;
4301 int prog_fd, pfd;
4302
4303 prog_fd = bpf_program__fd(prog);
4304 if (prog_fd < 0) {
4305 pr_warning("program '%s': can't attach before loaded\n",
4306 bpf_program__title(prog, false));
4307 return ERR_PTR(-EINVAL);
4308 }
4309
4310 link = malloc(sizeof(*link));
4311 if (!link)
4312 return ERR_PTR(-ENOMEM);
4313 link->link.destroy = &bpf_link__destroy_fd;
4314
4315 pfd = bpf_raw_tracepoint_open(tp_name, prog_fd);
4316 if (pfd < 0) {
4317 pfd = -errno;
4318 free(link);
4319 pr_warning("program '%s': failed to attach to raw tracepoint '%s': %s\n",
4320 bpf_program__title(prog, false), tp_name,
4321 libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
4322 return ERR_PTR(pfd);
4323 }
4324 link->fd = pfd;
4325 return (struct bpf_link *)link;
4326}
4327
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07004328enum bpf_perf_event_ret
Daniel Borkmann3dca2112018-10-21 02:09:28 +02004329bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
4330 void **copy_mem, size_t *copy_size,
4331 bpf_perf_event_print_t fn, void *private_data)
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07004332{
Daniel Borkmann3dca2112018-10-21 02:09:28 +02004333 struct perf_event_mmap_page *header = mmap_mem;
Daniel Borkmanna64af0e2018-10-19 15:51:03 +02004334 __u64 data_head = ring_buffer_read_head(header);
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07004335 __u64 data_tail = header->data_tail;
Daniel Borkmann3dca2112018-10-21 02:09:28 +02004336 void *base = ((__u8 *)header) + page_size;
4337 int ret = LIBBPF_PERF_EVENT_CONT;
4338 struct perf_event_header *ehdr;
4339 size_t ehdr_size;
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07004340
Daniel Borkmann3dca2112018-10-21 02:09:28 +02004341 while (data_head != data_tail) {
4342 ehdr = base + (data_tail & (mmap_size - 1));
4343 ehdr_size = ehdr->size;
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07004344
Daniel Borkmann3dca2112018-10-21 02:09:28 +02004345 if (((void *)ehdr) + ehdr_size > base + mmap_size) {
4346 void *copy_start = ehdr;
4347 size_t len_first = base + mmap_size - copy_start;
4348 size_t len_secnd = ehdr_size - len_first;
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07004349
Daniel Borkmann3dca2112018-10-21 02:09:28 +02004350 if (*copy_size < ehdr_size) {
4351 free(*copy_mem);
4352 *copy_mem = malloc(ehdr_size);
4353 if (!*copy_mem) {
4354 *copy_size = 0;
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07004355 ret = LIBBPF_PERF_EVENT_ERROR;
4356 break;
4357 }
Daniel Borkmann3dca2112018-10-21 02:09:28 +02004358 *copy_size = ehdr_size;
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07004359 }
4360
Daniel Borkmann3dca2112018-10-21 02:09:28 +02004361 memcpy(*copy_mem, copy_start, len_first);
4362 memcpy(*copy_mem + len_first, base, len_secnd);
4363 ehdr = *copy_mem;
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07004364 }
4365
Daniel Borkmann3dca2112018-10-21 02:09:28 +02004366 ret = fn(ehdr, private_data);
4367 data_tail += ehdr_size;
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07004368 if (ret != LIBBPF_PERF_EVENT_CONT)
4369 break;
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07004370 }
4371
Daniel Borkmanna64af0e2018-10-19 15:51:03 +02004372 ring_buffer_write_tail(header, data_tail);
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07004373 return ret;
4374}
Song Liu34be16462019-03-11 22:30:38 -07004375
Andrii Nakryikofb84b822019-07-06 11:06:24 -07004376struct perf_buffer;
4377
4378struct perf_buffer_params {
4379 struct perf_event_attr *attr;
4380 /* if event_cb is specified, it takes precendence */
4381 perf_buffer_event_fn event_cb;
4382 /* sample_cb and lost_cb are higher-level common-case callbacks */
4383 perf_buffer_sample_fn sample_cb;
4384 perf_buffer_lost_fn lost_cb;
4385 void *ctx;
4386 int cpu_cnt;
4387 int *cpus;
4388 int *map_keys;
4389};
4390
4391struct perf_cpu_buf {
4392 struct perf_buffer *pb;
4393 void *base; /* mmap()'ed memory */
4394 void *buf; /* for reconstructing segmented data */
4395 size_t buf_size;
4396 int fd;
4397 int cpu;
4398 int map_key;
4399};
4400
4401struct perf_buffer {
4402 perf_buffer_event_fn event_cb;
4403 perf_buffer_sample_fn sample_cb;
4404 perf_buffer_lost_fn lost_cb;
4405 void *ctx; /* passed into callbacks */
4406
4407 size_t page_size;
4408 size_t mmap_size;
4409 struct perf_cpu_buf **cpu_bufs;
4410 struct epoll_event *events;
4411 int cpu_cnt;
4412 int epoll_fd; /* perf event FD */
4413 int map_fd; /* BPF_MAP_TYPE_PERF_EVENT_ARRAY BPF map FD */
4414};
4415
4416static void perf_buffer__free_cpu_buf(struct perf_buffer *pb,
4417 struct perf_cpu_buf *cpu_buf)
4418{
4419 if (!cpu_buf)
4420 return;
4421 if (cpu_buf->base &&
4422 munmap(cpu_buf->base, pb->mmap_size + pb->page_size))
4423 pr_warning("failed to munmap cpu_buf #%d\n", cpu_buf->cpu);
4424 if (cpu_buf->fd >= 0) {
4425 ioctl(cpu_buf->fd, PERF_EVENT_IOC_DISABLE, 0);
4426 close(cpu_buf->fd);
4427 }
4428 free(cpu_buf->buf);
4429 free(cpu_buf);
4430}
4431
4432void perf_buffer__free(struct perf_buffer *pb)
4433{
4434 int i;
4435
4436 if (!pb)
4437 return;
4438 if (pb->cpu_bufs) {
4439 for (i = 0; i < pb->cpu_cnt && pb->cpu_bufs[i]; i++) {
4440 struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i];
4441
4442 bpf_map_delete_elem(pb->map_fd, &cpu_buf->map_key);
4443 perf_buffer__free_cpu_buf(pb, cpu_buf);
4444 }
4445 free(pb->cpu_bufs);
4446 }
4447 if (pb->epoll_fd >= 0)
4448 close(pb->epoll_fd);
4449 free(pb->events);
4450 free(pb);
4451}
4452
4453static struct perf_cpu_buf *
4454perf_buffer__open_cpu_buf(struct perf_buffer *pb, struct perf_event_attr *attr,
4455 int cpu, int map_key)
4456{
4457 struct perf_cpu_buf *cpu_buf;
4458 char msg[STRERR_BUFSIZE];
4459 int err;
4460
4461 cpu_buf = calloc(1, sizeof(*cpu_buf));
4462 if (!cpu_buf)
4463 return ERR_PTR(-ENOMEM);
4464
4465 cpu_buf->pb = pb;
4466 cpu_buf->cpu = cpu;
4467 cpu_buf->map_key = map_key;
4468
4469 cpu_buf->fd = syscall(__NR_perf_event_open, attr, -1 /* pid */, cpu,
4470 -1, PERF_FLAG_FD_CLOEXEC);
4471 if (cpu_buf->fd < 0) {
4472 err = -errno;
4473 pr_warning("failed to open perf buffer event on cpu #%d: %s\n",
4474 cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
4475 goto error;
4476 }
4477
4478 cpu_buf->base = mmap(NULL, pb->mmap_size + pb->page_size,
4479 PROT_READ | PROT_WRITE, MAP_SHARED,
4480 cpu_buf->fd, 0);
4481 if (cpu_buf->base == MAP_FAILED) {
4482 cpu_buf->base = NULL;
4483 err = -errno;
4484 pr_warning("failed to mmap perf buffer on cpu #%d: %s\n",
4485 cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
4486 goto error;
4487 }
4488
4489 if (ioctl(cpu_buf->fd, PERF_EVENT_IOC_ENABLE, 0) < 0) {
4490 err = -errno;
4491 pr_warning("failed to enable perf buffer event on cpu #%d: %s\n",
4492 cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
4493 goto error;
4494 }
4495
4496 return cpu_buf;
4497
4498error:
4499 perf_buffer__free_cpu_buf(pb, cpu_buf);
4500 return (struct perf_cpu_buf *)ERR_PTR(err);
4501}
4502
4503static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,
4504 struct perf_buffer_params *p);
4505
4506struct perf_buffer *perf_buffer__new(int map_fd, size_t page_cnt,
4507 const struct perf_buffer_opts *opts)
4508{
4509 struct perf_buffer_params p = {};
4510 struct perf_event_attr attr = {
4511 .config = PERF_COUNT_SW_BPF_OUTPUT,
4512 .type = PERF_TYPE_SOFTWARE,
4513 .sample_type = PERF_SAMPLE_RAW,
4514 .sample_period = 1,
4515 .wakeup_events = 1,
4516 };
4517
4518 p.attr = &attr;
4519 p.sample_cb = opts ? opts->sample_cb : NULL;
4520 p.lost_cb = opts ? opts->lost_cb : NULL;
4521 p.ctx = opts ? opts->ctx : NULL;
4522
4523 return __perf_buffer__new(map_fd, page_cnt, &p);
4524}
4525
4526struct perf_buffer *
4527perf_buffer__new_raw(int map_fd, size_t page_cnt,
4528 const struct perf_buffer_raw_opts *opts)
4529{
4530 struct perf_buffer_params p = {};
4531
4532 p.attr = opts->attr;
4533 p.event_cb = opts->event_cb;
4534 p.ctx = opts->ctx;
4535 p.cpu_cnt = opts->cpu_cnt;
4536 p.cpus = opts->cpus;
4537 p.map_keys = opts->map_keys;
4538
4539 return __perf_buffer__new(map_fd, page_cnt, &p);
4540}
4541
4542static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,
4543 struct perf_buffer_params *p)
4544{
4545 struct bpf_map_info map = {};
4546 char msg[STRERR_BUFSIZE];
4547 struct perf_buffer *pb;
4548 __u32 map_info_len;
4549 int err, i;
4550
4551 if (page_cnt & (page_cnt - 1)) {
4552 pr_warning("page count should be power of two, but is %zu\n",
4553 page_cnt);
4554 return ERR_PTR(-EINVAL);
4555 }
4556
4557 map_info_len = sizeof(map);
4558 err = bpf_obj_get_info_by_fd(map_fd, &map, &map_info_len);
4559 if (err) {
4560 err = -errno;
4561 pr_warning("failed to get map info for map FD %d: %s\n",
4562 map_fd, libbpf_strerror_r(err, msg, sizeof(msg)));
4563 return ERR_PTR(err);
4564 }
4565
4566 if (map.type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) {
4567 pr_warning("map '%s' should be BPF_MAP_TYPE_PERF_EVENT_ARRAY\n",
4568 map.name);
4569 return ERR_PTR(-EINVAL);
4570 }
4571
4572 pb = calloc(1, sizeof(*pb));
4573 if (!pb)
4574 return ERR_PTR(-ENOMEM);
4575
4576 pb->event_cb = p->event_cb;
4577 pb->sample_cb = p->sample_cb;
4578 pb->lost_cb = p->lost_cb;
4579 pb->ctx = p->ctx;
4580
4581 pb->page_size = getpagesize();
4582 pb->mmap_size = pb->page_size * page_cnt;
4583 pb->map_fd = map_fd;
4584
4585 pb->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
4586 if (pb->epoll_fd < 0) {
4587 err = -errno;
4588 pr_warning("failed to create epoll instance: %s\n",
4589 libbpf_strerror_r(err, msg, sizeof(msg)));
4590 goto error;
4591 }
4592
4593 if (p->cpu_cnt > 0) {
4594 pb->cpu_cnt = p->cpu_cnt;
4595 } else {
4596 pb->cpu_cnt = libbpf_num_possible_cpus();
4597 if (pb->cpu_cnt < 0) {
4598 err = pb->cpu_cnt;
4599 goto error;
4600 }
4601 if (map.max_entries < pb->cpu_cnt)
4602 pb->cpu_cnt = map.max_entries;
4603 }
4604
4605 pb->events = calloc(pb->cpu_cnt, sizeof(*pb->events));
4606 if (!pb->events) {
4607 err = -ENOMEM;
4608 pr_warning("failed to allocate events: out of memory\n");
4609 goto error;
4610 }
4611 pb->cpu_bufs = calloc(pb->cpu_cnt, sizeof(*pb->cpu_bufs));
4612 if (!pb->cpu_bufs) {
4613 err = -ENOMEM;
4614 pr_warning("failed to allocate buffers: out of memory\n");
4615 goto error;
4616 }
4617
4618 for (i = 0; i < pb->cpu_cnt; i++) {
4619 struct perf_cpu_buf *cpu_buf;
4620 int cpu, map_key;
4621
4622 cpu = p->cpu_cnt > 0 ? p->cpus[i] : i;
4623 map_key = p->cpu_cnt > 0 ? p->map_keys[i] : i;
4624
4625 cpu_buf = perf_buffer__open_cpu_buf(pb, p->attr, cpu, map_key);
4626 if (IS_ERR(cpu_buf)) {
4627 err = PTR_ERR(cpu_buf);
4628 goto error;
4629 }
4630
4631 pb->cpu_bufs[i] = cpu_buf;
4632
4633 err = bpf_map_update_elem(pb->map_fd, &map_key,
4634 &cpu_buf->fd, 0);
4635 if (err) {
4636 err = -errno;
4637 pr_warning("failed to set cpu #%d, key %d -> perf FD %d: %s\n",
4638 cpu, map_key, cpu_buf->fd,
4639 libbpf_strerror_r(err, msg, sizeof(msg)));
4640 goto error;
4641 }
4642
4643 pb->events[i].events = EPOLLIN;
4644 pb->events[i].data.ptr = cpu_buf;
4645 if (epoll_ctl(pb->epoll_fd, EPOLL_CTL_ADD, cpu_buf->fd,
4646 &pb->events[i]) < 0) {
4647 err = -errno;
4648 pr_warning("failed to epoll_ctl cpu #%d perf FD %d: %s\n",
4649 cpu, cpu_buf->fd,
4650 libbpf_strerror_r(err, msg, sizeof(msg)));
4651 goto error;
4652 }
4653 }
4654
4655 return pb;
4656
4657error:
4658 if (pb)
4659 perf_buffer__free(pb);
4660 return ERR_PTR(err);
4661}
4662
4663struct perf_sample_raw {
4664 struct perf_event_header header;
4665 uint32_t size;
4666 char data[0];
4667};
4668
4669struct perf_sample_lost {
4670 struct perf_event_header header;
4671 uint64_t id;
4672 uint64_t lost;
4673 uint64_t sample_id;
4674};
4675
4676static enum bpf_perf_event_ret
4677perf_buffer__process_record(struct perf_event_header *e, void *ctx)
4678{
4679 struct perf_cpu_buf *cpu_buf = ctx;
4680 struct perf_buffer *pb = cpu_buf->pb;
4681 void *data = e;
4682
4683 /* user wants full control over parsing perf event */
4684 if (pb->event_cb)
4685 return pb->event_cb(pb->ctx, cpu_buf->cpu, e);
4686
4687 switch (e->type) {
4688 case PERF_RECORD_SAMPLE: {
4689 struct perf_sample_raw *s = data;
4690
4691 if (pb->sample_cb)
4692 pb->sample_cb(pb->ctx, cpu_buf->cpu, s->data, s->size);
4693 break;
4694 }
4695 case PERF_RECORD_LOST: {
4696 struct perf_sample_lost *s = data;
4697
4698 if (pb->lost_cb)
4699 pb->lost_cb(pb->ctx, cpu_buf->cpu, s->lost);
4700 break;
4701 }
4702 default:
4703 pr_warning("unknown perf sample type %d\n", e->type);
4704 return LIBBPF_PERF_EVENT_ERROR;
4705 }
4706 return LIBBPF_PERF_EVENT_CONT;
4707}
4708
4709static int perf_buffer__process_records(struct perf_buffer *pb,
4710 struct perf_cpu_buf *cpu_buf)
4711{
4712 enum bpf_perf_event_ret ret;
4713
4714 ret = bpf_perf_event_read_simple(cpu_buf->base, pb->mmap_size,
4715 pb->page_size, &cpu_buf->buf,
4716 &cpu_buf->buf_size,
4717 perf_buffer__process_record, cpu_buf);
4718 if (ret != LIBBPF_PERF_EVENT_CONT)
4719 return ret;
4720 return 0;
4721}
4722
4723int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms)
4724{
4725 int i, cnt, err;
4726
4727 cnt = epoll_wait(pb->epoll_fd, pb->events, pb->cpu_cnt, timeout_ms);
4728 for (i = 0; i < cnt; i++) {
4729 struct perf_cpu_buf *cpu_buf = pb->events[i].data.ptr;
4730
4731 err = perf_buffer__process_records(pb, cpu_buf);
4732 if (err) {
4733 pr_warning("error while processing records: %d\n", err);
4734 return err;
4735 }
4736 }
4737 return cnt < 0 ? -errno : cnt;
4738}
4739
Song Liu34be16462019-03-11 22:30:38 -07004740struct bpf_prog_info_array_desc {
4741 int array_offset; /* e.g. offset of jited_prog_insns */
4742 int count_offset; /* e.g. offset of jited_prog_len */
4743 int size_offset; /* > 0: offset of rec size,
4744 * < 0: fix size of -size_offset
4745 */
4746};
4747
4748static struct bpf_prog_info_array_desc bpf_prog_info_array_desc[] = {
4749 [BPF_PROG_INFO_JITED_INSNS] = {
4750 offsetof(struct bpf_prog_info, jited_prog_insns),
4751 offsetof(struct bpf_prog_info, jited_prog_len),
4752 -1,
4753 },
4754 [BPF_PROG_INFO_XLATED_INSNS] = {
4755 offsetof(struct bpf_prog_info, xlated_prog_insns),
4756 offsetof(struct bpf_prog_info, xlated_prog_len),
4757 -1,
4758 },
4759 [BPF_PROG_INFO_MAP_IDS] = {
4760 offsetof(struct bpf_prog_info, map_ids),
4761 offsetof(struct bpf_prog_info, nr_map_ids),
4762 -(int)sizeof(__u32),
4763 },
4764 [BPF_PROG_INFO_JITED_KSYMS] = {
4765 offsetof(struct bpf_prog_info, jited_ksyms),
4766 offsetof(struct bpf_prog_info, nr_jited_ksyms),
4767 -(int)sizeof(__u64),
4768 },
4769 [BPF_PROG_INFO_JITED_FUNC_LENS] = {
4770 offsetof(struct bpf_prog_info, jited_func_lens),
4771 offsetof(struct bpf_prog_info, nr_jited_func_lens),
4772 -(int)sizeof(__u32),
4773 },
4774 [BPF_PROG_INFO_FUNC_INFO] = {
4775 offsetof(struct bpf_prog_info, func_info),
4776 offsetof(struct bpf_prog_info, nr_func_info),
4777 offsetof(struct bpf_prog_info, func_info_rec_size),
4778 },
4779 [BPF_PROG_INFO_LINE_INFO] = {
4780 offsetof(struct bpf_prog_info, line_info),
4781 offsetof(struct bpf_prog_info, nr_line_info),
4782 offsetof(struct bpf_prog_info, line_info_rec_size),
4783 },
4784 [BPF_PROG_INFO_JITED_LINE_INFO] = {
4785 offsetof(struct bpf_prog_info, jited_line_info),
4786 offsetof(struct bpf_prog_info, nr_jited_line_info),
4787 offsetof(struct bpf_prog_info, jited_line_info_rec_size),
4788 },
4789 [BPF_PROG_INFO_PROG_TAGS] = {
4790 offsetof(struct bpf_prog_info, prog_tags),
4791 offsetof(struct bpf_prog_info, nr_prog_tags),
4792 -(int)sizeof(__u8) * BPF_TAG_SIZE,
4793 },
4794
4795};
4796
4797static __u32 bpf_prog_info_read_offset_u32(struct bpf_prog_info *info, int offset)
4798{
4799 __u32 *array = (__u32 *)info;
4800
4801 if (offset >= 0)
4802 return array[offset / sizeof(__u32)];
4803 return -(int)offset;
4804}
4805
4806static __u64 bpf_prog_info_read_offset_u64(struct bpf_prog_info *info, int offset)
4807{
4808 __u64 *array = (__u64 *)info;
4809
4810 if (offset >= 0)
4811 return array[offset / sizeof(__u64)];
4812 return -(int)offset;
4813}
4814
4815static void bpf_prog_info_set_offset_u32(struct bpf_prog_info *info, int offset,
4816 __u32 val)
4817{
4818 __u32 *array = (__u32 *)info;
4819
4820 if (offset >= 0)
4821 array[offset / sizeof(__u32)] = val;
4822}
4823
4824static void bpf_prog_info_set_offset_u64(struct bpf_prog_info *info, int offset,
4825 __u64 val)
4826{
4827 __u64 *array = (__u64 *)info;
4828
4829 if (offset >= 0)
4830 array[offset / sizeof(__u64)] = val;
4831}
4832
4833struct bpf_prog_info_linear *
4834bpf_program__get_prog_info_linear(int fd, __u64 arrays)
4835{
4836 struct bpf_prog_info_linear *info_linear;
4837 struct bpf_prog_info info = {};
4838 __u32 info_len = sizeof(info);
4839 __u32 data_len = 0;
4840 int i, err;
4841 void *ptr;
4842
4843 if (arrays >> BPF_PROG_INFO_LAST_ARRAY)
4844 return ERR_PTR(-EINVAL);
4845
4846 /* step 1: get array dimensions */
4847 err = bpf_obj_get_info_by_fd(fd, &info, &info_len);
4848 if (err) {
4849 pr_debug("can't get prog info: %s", strerror(errno));
4850 return ERR_PTR(-EFAULT);
4851 }
4852
4853 /* step 2: calculate total size of all arrays */
4854 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
4855 bool include_array = (arrays & (1UL << i)) > 0;
4856 struct bpf_prog_info_array_desc *desc;
4857 __u32 count, size;
4858
4859 desc = bpf_prog_info_array_desc + i;
4860
4861 /* kernel is too old to support this field */
4862 if (info_len < desc->array_offset + sizeof(__u32) ||
4863 info_len < desc->count_offset + sizeof(__u32) ||
4864 (desc->size_offset > 0 && info_len < desc->size_offset))
4865 include_array = false;
4866
4867 if (!include_array) {
4868 arrays &= ~(1UL << i); /* clear the bit */
4869 continue;
4870 }
4871
4872 count = bpf_prog_info_read_offset_u32(&info, desc->count_offset);
4873 size = bpf_prog_info_read_offset_u32(&info, desc->size_offset);
4874
4875 data_len += count * size;
4876 }
4877
4878 /* step 3: allocate continuous memory */
4879 data_len = roundup(data_len, sizeof(__u64));
4880 info_linear = malloc(sizeof(struct bpf_prog_info_linear) + data_len);
4881 if (!info_linear)
4882 return ERR_PTR(-ENOMEM);
4883
4884 /* step 4: fill data to info_linear->info */
4885 info_linear->arrays = arrays;
4886 memset(&info_linear->info, 0, sizeof(info));
4887 ptr = info_linear->data;
4888
4889 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
4890 struct bpf_prog_info_array_desc *desc;
4891 __u32 count, size;
4892
4893 if ((arrays & (1UL << i)) == 0)
4894 continue;
4895
4896 desc = bpf_prog_info_array_desc + i;
4897 count = bpf_prog_info_read_offset_u32(&info, desc->count_offset);
4898 size = bpf_prog_info_read_offset_u32(&info, desc->size_offset);
4899 bpf_prog_info_set_offset_u32(&info_linear->info,
4900 desc->count_offset, count);
4901 bpf_prog_info_set_offset_u32(&info_linear->info,
4902 desc->size_offset, size);
4903 bpf_prog_info_set_offset_u64(&info_linear->info,
4904 desc->array_offset,
4905 ptr_to_u64(ptr));
4906 ptr += count * size;
4907 }
4908
4909 /* step 5: call syscall again to get required arrays */
4910 err = bpf_obj_get_info_by_fd(fd, &info_linear->info, &info_len);
4911 if (err) {
4912 pr_debug("can't get prog info: %s", strerror(errno));
4913 free(info_linear);
4914 return ERR_PTR(-EFAULT);
4915 }
4916
4917 /* step 6: verify the data */
4918 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
4919 struct bpf_prog_info_array_desc *desc;
4920 __u32 v1, v2;
4921
4922 if ((arrays & (1UL << i)) == 0)
4923 continue;
4924
4925 desc = bpf_prog_info_array_desc + i;
4926 v1 = bpf_prog_info_read_offset_u32(&info, desc->count_offset);
4927 v2 = bpf_prog_info_read_offset_u32(&info_linear->info,
4928 desc->count_offset);
4929 if (v1 != v2)
4930 pr_warning("%s: mismatch in element count\n", __func__);
4931
4932 v1 = bpf_prog_info_read_offset_u32(&info, desc->size_offset);
4933 v2 = bpf_prog_info_read_offset_u32(&info_linear->info,
4934 desc->size_offset);
4935 if (v1 != v2)
4936 pr_warning("%s: mismatch in rec size\n", __func__);
4937 }
4938
4939 /* step 7: update info_len and data_len */
4940 info_linear->info_len = sizeof(struct bpf_prog_info);
4941 info_linear->data_len = data_len;
4942
4943 return info_linear;
4944}
4945
4946void bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear)
4947{
4948 int i;
4949
4950 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
4951 struct bpf_prog_info_array_desc *desc;
4952 __u64 addr, offs;
4953
4954 if ((info_linear->arrays & (1UL << i)) == 0)
4955 continue;
4956
4957 desc = bpf_prog_info_array_desc + i;
4958 addr = bpf_prog_info_read_offset_u64(&info_linear->info,
4959 desc->array_offset);
4960 offs = addr - ptr_to_u64(info_linear->data);
4961 bpf_prog_info_set_offset_u64(&info_linear->info,
4962 desc->array_offset, offs);
4963 }
4964}
4965
4966void bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear)
4967{
4968 int i;
4969
4970 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
4971 struct bpf_prog_info_array_desc *desc;
4972 __u64 addr, offs;
4973
4974 if ((info_linear->arrays & (1UL << i)) == 0)
4975 continue;
4976
4977 desc = bpf_prog_info_array_desc + i;
4978 offs = bpf_prog_info_read_offset_u64(&info_linear->info,
4979 desc->array_offset);
4980 addr = offs + ptr_to_u64(info_linear->data);
4981 bpf_prog_info_set_offset_u64(&info_linear->info,
4982 desc->array_offset, addr);
4983 }
4984}
Hechao Li6446b312019-06-10 17:56:50 -07004985
4986int libbpf_num_possible_cpus(void)
4987{
4988 static const char *fcpu = "/sys/devices/system/cpu/possible";
4989 int len = 0, n = 0, il = 0, ir = 0;
4990 unsigned int start = 0, end = 0;
4991 static int cpus;
4992 char buf[128];
4993 int error = 0;
4994 int fd = -1;
4995
4996 if (cpus > 0)
4997 return cpus;
4998
4999 fd = open(fcpu, O_RDONLY);
5000 if (fd < 0) {
5001 error = errno;
5002 pr_warning("Failed to open file %s: %s\n",
5003 fcpu, strerror(error));
5004 return -error;
5005 }
5006 len = read(fd, buf, sizeof(buf));
5007 close(fd);
5008 if (len <= 0) {
5009 error = len ? errno : EINVAL;
5010 pr_warning("Failed to read # of possible cpus from %s: %s\n",
5011 fcpu, strerror(error));
5012 return -error;
5013 }
5014 if (len == sizeof(buf)) {
5015 pr_warning("File %s size overflow\n", fcpu);
5016 return -EOVERFLOW;
5017 }
5018 buf[len] = '\0';
5019
5020 for (ir = 0, cpus = 0; ir <= len; ir++) {
5021 /* Each sub string separated by ',' has format \d+-\d+ or \d+ */
5022 if (buf[ir] == ',' || buf[ir] == '\0') {
5023 buf[ir] = '\0';
5024 n = sscanf(&buf[il], "%u-%u", &start, &end);
5025 if (n <= 0) {
5026 pr_warning("Failed to get # CPUs from %s\n",
5027 &buf[il]);
5028 return -EINVAL;
5029 } else if (n == 1) {
5030 end = start;
5031 }
5032 cpus += end - start + 1;
5033 il = ir + 1;
5034 }
5035 }
5036 if (cpus <= 0) {
5037 pr_warning("Invalid #CPUs %d from %s\n", cpus, fcpu);
5038 return -EINVAL;
5039 }
5040 return cpus;
5041}