blob: 5d3b931f9baa372c5635f81269ae6cbf5f09d32b [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 Nand3e0ce32015-11-06 13:58:09 +000036 __BPF_LOADER_ERRNO__END,
37};
38
Wang Nan69d262a2015-10-14 12:41:13 +000039struct bpf_object;
Wang Nan066dacb2016-02-22 09:10:30 +000040struct parse_events_term;
Wang Nanaa3abf32015-10-14 12:41:15 +000041#define PERF_BPF_PROBE_GROUP "perf_bpf_probe"
Wang Nan69d262a2015-10-14 12:41:13 +000042
Wang Nan4edf30e2015-10-14 12:41:17 +000043typedef int (*bpf_prog_iter_callback_t)(struct probe_trace_event *tev,
44 int fd, void *arg);
45
Wang Nan69d262a2015-10-14 12:41:13 +000046#ifdef HAVE_LIBBPF_SUPPORT
Wang Nand509db02015-10-14 12:41:20 +000047struct bpf_object *bpf__prepare_load(const char *filename, bool source);
Wang Nand3e0ce32015-11-06 13:58:09 +000048int bpf__strerror_prepare_load(const char *filename, bool source,
49 int err, char *buf, size_t size);
Wang Nan69d262a2015-10-14 12:41:13 +000050
Wang Nanba1fae42015-11-06 13:49:43 +000051struct bpf_object *bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz,
52 const char *name);
53
Wang Nan69d262a2015-10-14 12:41:13 +000054void bpf__clear(void);
Wang Nanaa3abf32015-10-14 12:41:15 +000055
56int bpf__probe(struct bpf_object *obj);
57int bpf__unprobe(struct bpf_object *obj);
58int bpf__strerror_probe(struct bpf_object *obj, int err,
59 char *buf, size_t size);
60
Wang Nan1e5e3ee2015-10-14 12:41:16 +000061int bpf__load(struct bpf_object *obj);
62int bpf__strerror_load(struct bpf_object *obj, int err,
63 char *buf, size_t size);
Wang Nan4edf30e2015-10-14 12:41:17 +000064int bpf__foreach_tev(struct bpf_object *obj,
65 bpf_prog_iter_callback_t func, void *arg);
Wang Nan066dacb2016-02-22 09:10:30 +000066
67int bpf__config_obj(struct bpf_object *obj, struct parse_events_term *term,
68 struct perf_evlist *evlist, int *error_pos);
69int bpf__strerror_config_obj(struct bpf_object *obj,
70 struct parse_events_term *term,
71 struct perf_evlist *evlist,
72 int *error_pos, int err, char *buf,
73 size_t size);
Wang Nan8690a2a2016-02-22 09:10:32 +000074int bpf__apply_obj_config(void);
75int bpf__strerror_apply_obj_config(int err, char *buf, size_t size);
Wang Nan69d262a2015-10-14 12:41:13 +000076#else
77static inline struct bpf_object *
Wang Nand509db02015-10-14 12:41:20 +000078bpf__prepare_load(const char *filename __maybe_unused,
79 bool source __maybe_unused)
Wang Nan69d262a2015-10-14 12:41:13 +000080{
81 pr_debug("ERROR: eBPF object loading is disabled during compiling.\n");
82 return ERR_PTR(-ENOTSUP);
83}
84
Wang Nanba1fae42015-11-06 13:49:43 +000085static inline struct bpf_object *
86bpf__prepare_load_buffer(void *obj_buf __maybe_unused,
87 size_t obj_buf_sz __maybe_unused)
88{
89 return ERR_PTR(-ENOTSUP);
90}
91
Wang Nan69d262a2015-10-14 12:41:13 +000092static inline void bpf__clear(void) { }
Wang Nanaa3abf32015-10-14 12:41:15 +000093
94static inline int bpf__probe(struct bpf_object *obj __maybe_unused) { return 0;}
95static inline int bpf__unprobe(struct bpf_object *obj __maybe_unused) { return 0;}
Wang Nan1e5e3ee2015-10-14 12:41:16 +000096static inline int bpf__load(struct bpf_object *obj __maybe_unused) { return 0; }
Wang Nanaa3abf32015-10-14 12:41:15 +000097
98static inline int
Wang Nan4edf30e2015-10-14 12:41:17 +000099bpf__foreach_tev(struct bpf_object *obj __maybe_unused,
100 bpf_prog_iter_callback_t func __maybe_unused,
101 void *arg __maybe_unused)
102{
103 return 0;
104}
105
106static inline int
Wang Nan066dacb2016-02-22 09:10:30 +0000107bpf__config_obj(struct bpf_object *obj __maybe_unused,
108 struct parse_events_term *term __maybe_unused,
109 struct perf_evlist *evlist __maybe_unused,
110 int *error_pos __maybe_unused)
111{
112 return 0;
113}
114
115static inline int
Wang Nan8690a2a2016-02-22 09:10:32 +0000116bpf__apply_obj_config(void)
117{
118 return 0;
119}
120
121static inline int
Wang Nanaa3abf32015-10-14 12:41:15 +0000122__bpf_strerror(char *buf, size_t size)
123{
124 if (!size)
125 return 0;
126 strncpy(buf,
127 "ERROR: eBPF object loading is disabled during compiling.\n",
128 size);
129 buf[size - 1] = '\0';
130 return 0;
131}
132
Wang Nand3e0ce32015-11-06 13:58:09 +0000133static inline
134int bpf__strerror_prepare_load(const char *filename __maybe_unused,
135 bool source __maybe_unused,
136 int err __maybe_unused,
137 char *buf, size_t size)
138{
139 return __bpf_strerror(buf, size);
140}
141
Wang Nanaa3abf32015-10-14 12:41:15 +0000142static inline int
143bpf__strerror_probe(struct bpf_object *obj __maybe_unused,
144 int err __maybe_unused,
145 char *buf, size_t size)
146{
147 return __bpf_strerror(buf, size);
148}
Wang Nan1e5e3ee2015-10-14 12:41:16 +0000149
150static inline int bpf__strerror_load(struct bpf_object *obj __maybe_unused,
151 int err __maybe_unused,
152 char *buf, size_t size)
153{
154 return __bpf_strerror(buf, size);
155}
Wang Nan066dacb2016-02-22 09:10:30 +0000156
157static inline int
158bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused,
159 struct parse_events_term *term __maybe_unused,
160 struct perf_evlist *evlist __maybe_unused,
161 int *error_pos __maybe_unused,
162 int err __maybe_unused,
163 char *buf, size_t size)
164{
165 return __bpf_strerror(buf, size);
166}
Wang Nan8690a2a2016-02-22 09:10:32 +0000167
168static inline int
169bpf__strerror_apply_obj_config(int err __maybe_unused,
170 char *buf, size_t size)
171{
172 return __bpf_strerror(buf, size);
173}
Wang Nan69d262a2015-10-14 12:41:13 +0000174#endif
175#endif