blob: 941e17275aa7d747dfeef655c21a364e627f6f95 [file] [log] [blame]
Wang Nan69d262a2015-10-14 12:41:13 +00001/*
2 * Copyright (C) 2015, Wang Nan <wangnan0@huawei.com>
3 * Copyright (C) 2015, Huawei Inc.
4 */
5#ifndef __BPF_LOADER_H
6#define __BPF_LOADER_H
7
8#include <linux/compiler.h>
9#include <linux/err.h>
10#include <string.h>
Wang Nand3e0ce32015-11-06 13:58:09 +000011#include <bpf/libbpf.h>
Wang Nan4edf30e2015-10-14 12:41:17 +000012#include "probe-event.h"
Wang Nan066dacb2016-02-22 09:10:30 +000013#include "evlist.h"
Wang Nan69d262a2015-10-14 12:41:13 +000014#include "debug.h"
15
Wang Nand3e0ce32015-11-06 13:58:09 +000016enum bpf_loader_errno {
17 __BPF_LOADER_ERRNO__START = __LIBBPF_ERRNO__START - 100,
18 /* Invalid config string */
19 BPF_LOADER_ERRNO__CONFIG = __BPF_LOADER_ERRNO__START,
20 BPF_LOADER_ERRNO__GROUP, /* Invalid group name */
21 BPF_LOADER_ERRNO__EVENTNAME, /* Event name is missing */
22 BPF_LOADER_ERRNO__INTERNAL, /* BPF loader internal error */
23 BPF_LOADER_ERRNO__COMPILE, /* Error when compiling BPF scriptlet */
Wang Nan0bb93492015-11-27 08:47:37 +000024 BPF_LOADER_ERRNO__PROGCONF_TERM,/* Invalid program config term in config string */
He Kuangbfc077b2015-11-16 12:10:12 +000025 BPF_LOADER_ERRNO__PROLOGUE, /* Failed to generate prologue */
26 BPF_LOADER_ERRNO__PROLOGUE2BIG, /* Prologue too big for program */
27 BPF_LOADER_ERRNO__PROLOGUEOOB, /* Offset out of bound for prologue */
Wang Nan066dacb2016-02-22 09:10:30 +000028 BPF_LOADER_ERRNO__OBJCONF_OPT, /* Invalid object config option */
29 BPF_LOADER_ERRNO__OBJCONF_CONF, /* Config value not set (lost '=')) */
30 BPF_LOADER_ERRNO__OBJCONF_MAP_OPT, /* Invalid object map config option */
31 BPF_LOADER_ERRNO__OBJCONF_MAP_NOTEXIST, /* Target map not exist */
32 BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE, /* Incorrect value type for map */
33 BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE, /* Incorrect map type */
34 BPF_LOADER_ERRNO__OBJCONF_MAP_KEYSIZE, /* Incorrect map key size */
35 BPF_LOADER_ERRNO__OBJCONF_MAP_VALUESIZE,/* Incorrect map value size */
Wang Nan7630b3e2016-02-22 09:10:33 +000036 BPF_LOADER_ERRNO__OBJCONF_MAP_NOEVT, /* Event not found for map setting */
37 BPF_LOADER_ERRNO__OBJCONF_MAP_MAPSIZE, /* Invalid map size for event setting */
38 BPF_LOADER_ERRNO__OBJCONF_MAP_EVTDIM, /* Event dimension too large */
39 BPF_LOADER_ERRNO__OBJCONF_MAP_EVTINH, /* Doesn't support inherit event */
40 BPF_LOADER_ERRNO__OBJCONF_MAP_EVTTYPE, /* Wrong event type for map */
Wang Nan2d055bf2016-02-22 09:10:34 +000041 BPF_LOADER_ERRNO__OBJCONF_MAP_IDX2BIG, /* Index too large */
Wang Nand3e0ce32015-11-06 13:58:09 +000042 __BPF_LOADER_ERRNO__END,
43};
44
Wang Nan69d262a2015-10-14 12:41:13 +000045struct bpf_object;
Wang Nan066dacb2016-02-22 09:10:30 +000046struct parse_events_term;
Wang Nanaa3abf32015-10-14 12:41:15 +000047#define PERF_BPF_PROBE_GROUP "perf_bpf_probe"
Wang Nan69d262a2015-10-14 12:41:13 +000048
Wang Nan4edf30e2015-10-14 12:41:17 +000049typedef int (*bpf_prog_iter_callback_t)(struct probe_trace_event *tev,
50 int fd, void *arg);
51
Wang Nan69d262a2015-10-14 12:41:13 +000052#ifdef HAVE_LIBBPF_SUPPORT
Wang Nand509db02015-10-14 12:41:20 +000053struct bpf_object *bpf__prepare_load(const char *filename, bool source);
Wang Nand3e0ce32015-11-06 13:58:09 +000054int bpf__strerror_prepare_load(const char *filename, bool source,
55 int err, char *buf, size_t size);
Wang Nan69d262a2015-10-14 12:41:13 +000056
Wang Nanba1fae42015-11-06 13:49:43 +000057struct bpf_object *bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz,
58 const char *name);
59
Wang Nan69d262a2015-10-14 12:41:13 +000060void bpf__clear(void);
Wang Nanaa3abf32015-10-14 12:41:15 +000061
62int bpf__probe(struct bpf_object *obj);
63int bpf__unprobe(struct bpf_object *obj);
64int bpf__strerror_probe(struct bpf_object *obj, int err,
65 char *buf, size_t size);
66
Wang Nan1e5e3ee2015-10-14 12:41:16 +000067int bpf__load(struct bpf_object *obj);
68int bpf__strerror_load(struct bpf_object *obj, int err,
69 char *buf, size_t size);
Wang Nan4edf30e2015-10-14 12:41:17 +000070int bpf__foreach_tev(struct bpf_object *obj,
71 bpf_prog_iter_callback_t func, void *arg);
Wang Nan066dacb2016-02-22 09:10:30 +000072
73int bpf__config_obj(struct bpf_object *obj, struct parse_events_term *term,
74 struct perf_evlist *evlist, int *error_pos);
75int bpf__strerror_config_obj(struct bpf_object *obj,
76 struct parse_events_term *term,
77 struct perf_evlist *evlist,
78 int *error_pos, int err, char *buf,
79 size_t size);
Wang Nan8690a2a2016-02-22 09:10:32 +000080int bpf__apply_obj_config(void);
81int bpf__strerror_apply_obj_config(int err, char *buf, size_t size);
Wang Nand7888572016-04-08 15:07:24 +000082
83int bpf__setup_stdout(struct perf_evlist *evlist);
84int bpf__strerror_setup_stdout(struct perf_evlist *evlist, int err,
85 char *buf, size_t size);
86
Wang Nan69d262a2015-10-14 12:41:13 +000087#else
88static inline struct bpf_object *
Wang Nand509db02015-10-14 12:41:20 +000089bpf__prepare_load(const char *filename __maybe_unused,
90 bool source __maybe_unused)
Wang Nan69d262a2015-10-14 12:41:13 +000091{
92 pr_debug("ERROR: eBPF object loading is disabled during compiling.\n");
93 return ERR_PTR(-ENOTSUP);
94}
95
Wang Nanba1fae42015-11-06 13:49:43 +000096static inline struct bpf_object *
97bpf__prepare_load_buffer(void *obj_buf __maybe_unused,
98 size_t obj_buf_sz __maybe_unused)
99{
100 return ERR_PTR(-ENOTSUP);
101}
102
Wang Nan69d262a2015-10-14 12:41:13 +0000103static inline void bpf__clear(void) { }
Wang Nanaa3abf32015-10-14 12:41:15 +0000104
105static inline int bpf__probe(struct bpf_object *obj __maybe_unused) { return 0;}
106static inline int bpf__unprobe(struct bpf_object *obj __maybe_unused) { return 0;}
Wang Nan1e5e3ee2015-10-14 12:41:16 +0000107static inline int bpf__load(struct bpf_object *obj __maybe_unused) { return 0; }
Wang Nanaa3abf32015-10-14 12:41:15 +0000108
109static inline int
Wang Nan4edf30e2015-10-14 12:41:17 +0000110bpf__foreach_tev(struct bpf_object *obj __maybe_unused,
111 bpf_prog_iter_callback_t func __maybe_unused,
112 void *arg __maybe_unused)
113{
114 return 0;
115}
116
117static inline int
Wang Nan066dacb2016-02-22 09:10:30 +0000118bpf__config_obj(struct bpf_object *obj __maybe_unused,
119 struct parse_events_term *term __maybe_unused,
120 struct perf_evlist *evlist __maybe_unused,
121 int *error_pos __maybe_unused)
122{
123 return 0;
124}
125
126static inline int
Wang Nan8690a2a2016-02-22 09:10:32 +0000127bpf__apply_obj_config(void)
128{
129 return 0;
130}
131
132static inline int
Wang Nand7888572016-04-08 15:07:24 +0000133bpf__setup_stdout(struct perf_evlist *evlist __maybe_unused)
134{
135 return 0;
136}
137
138static inline int
Wang Nanaa3abf32015-10-14 12:41:15 +0000139__bpf_strerror(char *buf, size_t size)
140{
141 if (!size)
142 return 0;
143 strncpy(buf,
144 "ERROR: eBPF object loading is disabled during compiling.\n",
145 size);
146 buf[size - 1] = '\0';
147 return 0;
148}
149
Wang Nand3e0ce32015-11-06 13:58:09 +0000150static inline
151int bpf__strerror_prepare_load(const char *filename __maybe_unused,
152 bool source __maybe_unused,
153 int err __maybe_unused,
154 char *buf, size_t size)
155{
156 return __bpf_strerror(buf, size);
157}
158
Wang Nanaa3abf32015-10-14 12:41:15 +0000159static inline int
160bpf__strerror_probe(struct bpf_object *obj __maybe_unused,
161 int err __maybe_unused,
162 char *buf, size_t size)
163{
164 return __bpf_strerror(buf, size);
165}
Wang Nan1e5e3ee2015-10-14 12:41:16 +0000166
167static inline int bpf__strerror_load(struct bpf_object *obj __maybe_unused,
168 int err __maybe_unused,
169 char *buf, size_t size)
170{
171 return __bpf_strerror(buf, size);
172}
Wang Nan066dacb2016-02-22 09:10:30 +0000173
174static inline int
175bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused,
176 struct parse_events_term *term __maybe_unused,
177 struct perf_evlist *evlist __maybe_unused,
178 int *error_pos __maybe_unused,
179 int err __maybe_unused,
180 char *buf, size_t size)
181{
182 return __bpf_strerror(buf, size);
183}
Wang Nan8690a2a2016-02-22 09:10:32 +0000184
185static inline int
186bpf__strerror_apply_obj_config(int err __maybe_unused,
187 char *buf, size_t size)
188{
189 return __bpf_strerror(buf, size);
190}
Wang Nand7888572016-04-08 15:07:24 +0000191
192static inline int
193bpf__strerror_setup_stdout(struct perf_evlist *evlist __maybe_unused,
194 int err __maybe_unused, char *buf,
195 size_t size)
196{
197 return __bpf_strerror(buf, size);
198}
Wang Nan69d262a2015-10-14 12:41:13 +0000199#endif
200#endif