blob: 8eca75145ac27b8b122620afd4652570eef9651e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Wang Nan69d262a2015-10-14 12:41:13 +00002/*
3 * Copyright (C) 2015, Wang Nan <wangnan0@huawei.com>
4 * Copyright (C) 2015, Huawei Inc.
5 */
6#ifndef __BPF_LOADER_H
7#define __BPF_LOADER_H
8
9#include <linux/compiler.h>
10#include <linux/err.h>
11#include <string.h>
Wang Nand3e0ce32015-11-06 13:58:09 +000012#include <bpf/libbpf.h>
Wang Nan4edf30e2015-10-14 12:41:17 +000013#include "probe-event.h"
Wang Nan066dacb2016-02-22 09:10:30 +000014#include "evlist.h"
Wang Nan69d262a2015-10-14 12:41:13 +000015#include "debug.h"
16
Wang Nand3e0ce32015-11-06 13:58:09 +000017enum bpf_loader_errno {
18 __BPF_LOADER_ERRNO__START = __LIBBPF_ERRNO__START - 100,
19 /* Invalid config string */
20 BPF_LOADER_ERRNO__CONFIG = __BPF_LOADER_ERRNO__START,
21 BPF_LOADER_ERRNO__GROUP, /* Invalid group name */
22 BPF_LOADER_ERRNO__EVENTNAME, /* Event name is missing */
23 BPF_LOADER_ERRNO__INTERNAL, /* BPF loader internal error */
24 BPF_LOADER_ERRNO__COMPILE, /* Error when compiling BPF scriptlet */
Wang Nan0bb93492015-11-27 08:47:37 +000025 BPF_LOADER_ERRNO__PROGCONF_TERM,/* Invalid program config term in config string */
He Kuangbfc077b2015-11-16 12:10:12 +000026 BPF_LOADER_ERRNO__PROLOGUE, /* Failed to generate prologue */
27 BPF_LOADER_ERRNO__PROLOGUE2BIG, /* Prologue too big for program */
28 BPF_LOADER_ERRNO__PROLOGUEOOB, /* Offset out of bound for prologue */
Wang Nan066dacb2016-02-22 09:10:30 +000029 BPF_LOADER_ERRNO__OBJCONF_OPT, /* Invalid object config option */
30 BPF_LOADER_ERRNO__OBJCONF_CONF, /* Config value not set (lost '=')) */
31 BPF_LOADER_ERRNO__OBJCONF_MAP_OPT, /* Invalid object map config option */
32 BPF_LOADER_ERRNO__OBJCONF_MAP_NOTEXIST, /* Target map not exist */
33 BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE, /* Incorrect value type for map */
34 BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE, /* Incorrect map type */
35 BPF_LOADER_ERRNO__OBJCONF_MAP_KEYSIZE, /* Incorrect map key size */
36 BPF_LOADER_ERRNO__OBJCONF_MAP_VALUESIZE,/* Incorrect map value size */
Wang Nan7630b3e2016-02-22 09:10:33 +000037 BPF_LOADER_ERRNO__OBJCONF_MAP_NOEVT, /* Event not found for map setting */
38 BPF_LOADER_ERRNO__OBJCONF_MAP_MAPSIZE, /* Invalid map size for event setting */
39 BPF_LOADER_ERRNO__OBJCONF_MAP_EVTDIM, /* Event dimension too large */
40 BPF_LOADER_ERRNO__OBJCONF_MAP_EVTINH, /* Doesn't support inherit event */
41 BPF_LOADER_ERRNO__OBJCONF_MAP_EVTTYPE, /* Wrong event type for map */
Wang Nan2d055bf2016-02-22 09:10:34 +000042 BPF_LOADER_ERRNO__OBJCONF_MAP_IDX2BIG, /* Index too large */
Wang Nand3e0ce32015-11-06 13:58:09 +000043 __BPF_LOADER_ERRNO__END,
44};
45
Wang Nan69d262a2015-10-14 12:41:13 +000046struct bpf_object;
Wang Nan066dacb2016-02-22 09:10:30 +000047struct parse_events_term;
Wang Nanaa3abf32015-10-14 12:41:15 +000048#define PERF_BPF_PROBE_GROUP "perf_bpf_probe"
Wang Nan69d262a2015-10-14 12:41:13 +000049
Wang Nancd102d72016-07-13 10:44:04 +000050typedef int (*bpf_prog_iter_callback_t)(const char *group, const char *event,
Wang Nan4edf30e2015-10-14 12:41:17 +000051 int fd, void *arg);
52
Wang Nan69d262a2015-10-14 12:41:13 +000053#ifdef HAVE_LIBBPF_SUPPORT
Wang Nand509db02015-10-14 12:41:20 +000054struct bpf_object *bpf__prepare_load(const char *filename, bool source);
Wang Nand3e0ce32015-11-06 13:58:09 +000055int bpf__strerror_prepare_load(const char *filename, bool source,
56 int err, char *buf, size_t size);
Wang Nan69d262a2015-10-14 12:41:13 +000057
Wang Nanba1fae42015-11-06 13:49:43 +000058struct bpf_object *bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz,
59 const char *name);
60
Wang Nan69d262a2015-10-14 12:41:13 +000061void bpf__clear(void);
Wang Nanaa3abf32015-10-14 12:41:15 +000062
63int bpf__probe(struct bpf_object *obj);
64int bpf__unprobe(struct bpf_object *obj);
65int bpf__strerror_probe(struct bpf_object *obj, int err,
66 char *buf, size_t size);
67
Wang Nan1e5e3ee2015-10-14 12:41:16 +000068int bpf__load(struct bpf_object *obj);
69int bpf__strerror_load(struct bpf_object *obj, int err,
70 char *buf, size_t size);
Wang Nancd102d72016-07-13 10:44:04 +000071int bpf__foreach_event(struct bpf_object *obj,
72 bpf_prog_iter_callback_t func, void *arg);
Wang Nan066dacb2016-02-22 09:10:30 +000073
74int bpf__config_obj(struct bpf_object *obj, struct parse_events_term *term,
75 struct perf_evlist *evlist, int *error_pos);
76int bpf__strerror_config_obj(struct bpf_object *obj,
77 struct parse_events_term *term,
78 struct perf_evlist *evlist,
79 int *error_pos, int err, char *buf,
80 size_t size);
Wang Nan8690a2a2016-02-22 09:10:32 +000081int bpf__apply_obj_config(void);
82int bpf__strerror_apply_obj_config(int err, char *buf, size_t size);
Wang Nand7888572016-04-08 15:07:24 +000083
84int bpf__setup_stdout(struct perf_evlist *evlist);
Arnaldo Carvalho de Melo92bbe8d2018-08-06 09:53:35 -030085int bpf__setup_output_event(struct perf_evlist *evlist, const char *name);
Arnaldo Carvalho de Meloaa31be32018-08-06 11:35:37 -030086int bpf__strerror_setup_output_event(struct perf_evlist *evlist, int err, char *buf, size_t size);
Wang Nan69d262a2015-10-14 12:41:13 +000087#else
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030088#include <errno.h>
89
Wang Nan69d262a2015-10-14 12:41:13 +000090static inline struct bpf_object *
Wang Nand509db02015-10-14 12:41:20 +000091bpf__prepare_load(const char *filename __maybe_unused,
92 bool source __maybe_unused)
Wang Nan69d262a2015-10-14 12:41:13 +000093{
94 pr_debug("ERROR: eBPF object loading is disabled during compiling.\n");
95 return ERR_PTR(-ENOTSUP);
96}
97
Wang Nanba1fae42015-11-06 13:49:43 +000098static inline struct bpf_object *
99bpf__prepare_load_buffer(void *obj_buf __maybe_unused,
100 size_t obj_buf_sz __maybe_unused)
101{
102 return ERR_PTR(-ENOTSUP);
103}
104
Wang Nan69d262a2015-10-14 12:41:13 +0000105static inline void bpf__clear(void) { }
Wang Nanaa3abf32015-10-14 12:41:15 +0000106
107static inline int bpf__probe(struct bpf_object *obj __maybe_unused) { return 0;}
108static inline int bpf__unprobe(struct bpf_object *obj __maybe_unused) { return 0;}
Wang Nan1e5e3ee2015-10-14 12:41:16 +0000109static inline int bpf__load(struct bpf_object *obj __maybe_unused) { return 0; }
Wang Nanaa3abf32015-10-14 12:41:15 +0000110
111static inline int
Wang Nancd102d72016-07-13 10:44:04 +0000112bpf__foreach_event(struct bpf_object *obj __maybe_unused,
113 bpf_prog_iter_callback_t func __maybe_unused,
114 void *arg __maybe_unused)
Wang Nan4edf30e2015-10-14 12:41:17 +0000115{
116 return 0;
117}
118
119static inline int
Wang Nan066dacb2016-02-22 09:10:30 +0000120bpf__config_obj(struct bpf_object *obj __maybe_unused,
121 struct parse_events_term *term __maybe_unused,
122 struct perf_evlist *evlist __maybe_unused,
123 int *error_pos __maybe_unused)
124{
125 return 0;
126}
127
128static inline int
Wang Nan8690a2a2016-02-22 09:10:32 +0000129bpf__apply_obj_config(void)
130{
131 return 0;
132}
133
134static inline int
Wang Nand7888572016-04-08 15:07:24 +0000135bpf__setup_stdout(struct perf_evlist *evlist __maybe_unused)
136{
137 return 0;
138}
139
140static inline int
Arnaldo Carvalho de Melo92bbe8d2018-08-06 09:53:35 -0300141bpf__setup_output_event(struct perf_evlist *evlist __maybe_unused, const char *name __maybe_unused)
142{
143 return 0;
144}
145
146static inline int
Wang Nanaa3abf32015-10-14 12:41:15 +0000147__bpf_strerror(char *buf, size_t size)
148{
149 if (!size)
150 return 0;
151 strncpy(buf,
152 "ERROR: eBPF object loading is disabled during compiling.\n",
153 size);
154 buf[size - 1] = '\0';
155 return 0;
156}
157
Wang Nand3e0ce32015-11-06 13:58:09 +0000158static inline
159int bpf__strerror_prepare_load(const char *filename __maybe_unused,
160 bool source __maybe_unused,
161 int err __maybe_unused,
162 char *buf, size_t size)
163{
164 return __bpf_strerror(buf, size);
165}
166
Wang Nanaa3abf32015-10-14 12:41:15 +0000167static inline int
168bpf__strerror_probe(struct bpf_object *obj __maybe_unused,
169 int err __maybe_unused,
170 char *buf, size_t size)
171{
172 return __bpf_strerror(buf, size);
173}
Wang Nan1e5e3ee2015-10-14 12:41:16 +0000174
175static inline int bpf__strerror_load(struct bpf_object *obj __maybe_unused,
176 int err __maybe_unused,
177 char *buf, size_t size)
178{
179 return __bpf_strerror(buf, size);
180}
Wang Nan066dacb2016-02-22 09:10:30 +0000181
182static inline int
183bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused,
184 struct parse_events_term *term __maybe_unused,
185 struct perf_evlist *evlist __maybe_unused,
186 int *error_pos __maybe_unused,
187 int err __maybe_unused,
188 char *buf, size_t size)
189{
190 return __bpf_strerror(buf, size);
191}
Wang Nan8690a2a2016-02-22 09:10:32 +0000192
193static inline int
194bpf__strerror_apply_obj_config(int err __maybe_unused,
195 char *buf, size_t size)
196{
197 return __bpf_strerror(buf, size);
198}
Wang Nand7888572016-04-08 15:07:24 +0000199
200static inline int
Arnaldo Carvalho de Meloaa31be32018-08-06 11:35:37 -0300201bpf__strerror_setup_output_event(struct perf_evlist *evlist __maybe_unused,
202 int err __maybe_unused, char *buf, size_t size)
Wang Nand7888572016-04-08 15:07:24 +0000203{
204 return __bpf_strerror(buf, size);
205}
Arnaldo Carvalho de Meloaa31be32018-08-06 11:35:37 -0300206
Wang Nan69d262a2015-10-14 12:41:13 +0000207#endif
Arnaldo Carvalho de Meloaa31be32018-08-06 11:35:37 -0300208
209static inline int bpf__strerror_setup_stdout(struct perf_evlist *evlist, int err, char *buf, size_t size)
210{
211 return bpf__strerror_setup_output_event(evlist, err, buf, size);
212}
Wang Nan69d262a2015-10-14 12:41:13 +0000213#endif