]> asedeno.scripts.mit.edu Git - linux.git/blob - tools/perf/util/bpf-loader.h
6be0eec043c64760b64681fe152bad016d593059
[linux.git] / tools / perf / util / bpf-loader.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
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>
12 #include <bpf/libbpf.h>
13 #include "probe-event.h"
14 #include "evlist.h"
15 #include "debug.h"
16
17 enum 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 */
25         BPF_LOADER_ERRNO__PROGCONF_TERM,/* Invalid program config term in config string */
26         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 */
29         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 */
37         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 */
42         BPF_LOADER_ERRNO__OBJCONF_MAP_IDX2BIG,  /* Index too large */
43         __BPF_LOADER_ERRNO__END,
44 };
45
46 struct bpf_object;
47 struct parse_events_term;
48 #define PERF_BPF_PROBE_GROUP "perf_bpf_probe"
49
50 typedef int (*bpf_prog_iter_callback_t)(const char *group, const char *event,
51                                         int fd, void *arg);
52
53 #ifdef HAVE_LIBBPF_SUPPORT
54 struct bpf_object *bpf__prepare_load(const char *filename, bool source);
55 int bpf__strerror_prepare_load(const char *filename, bool source,
56                                int err, char *buf, size_t size);
57
58 struct bpf_object *bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz,
59                                             const char *name);
60
61 void bpf__clear(void);
62
63 int bpf__probe(struct bpf_object *obj);
64 int bpf__unprobe(struct bpf_object *obj);
65 int bpf__strerror_probe(struct bpf_object *obj, int err,
66                         char *buf, size_t size);
67
68 int bpf__load(struct bpf_object *obj);
69 int bpf__strerror_load(struct bpf_object *obj, int err,
70                        char *buf, size_t size);
71 int bpf__foreach_event(struct bpf_object *obj,
72                        bpf_prog_iter_callback_t func, void *arg);
73
74 int bpf__config_obj(struct bpf_object *obj, struct parse_events_term *term,
75                     struct perf_evlist *evlist, int *error_pos);
76 int 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);
81 int bpf__apply_obj_config(void);
82 int bpf__strerror_apply_obj_config(int err, char *buf, size_t size);
83
84 int bpf__setup_stdout(struct perf_evlist *evlist);
85 int bpf__setup_output_event(struct perf_evlist *evlist, const char *name);
86 int bpf__strerror_setup_stdout(struct perf_evlist *evlist, int err,
87                                char *buf, size_t size);
88
89 #else
90 #include <errno.h>
91
92 static inline struct bpf_object *
93 bpf__prepare_load(const char *filename __maybe_unused,
94                   bool source __maybe_unused)
95 {
96         pr_debug("ERROR: eBPF object loading is disabled during compiling.\n");
97         return ERR_PTR(-ENOTSUP);
98 }
99
100 static inline struct bpf_object *
101 bpf__prepare_load_buffer(void *obj_buf __maybe_unused,
102                                            size_t obj_buf_sz __maybe_unused)
103 {
104         return ERR_PTR(-ENOTSUP);
105 }
106
107 static inline void bpf__clear(void) { }
108
109 static inline int bpf__probe(struct bpf_object *obj __maybe_unused) { return 0;}
110 static inline int bpf__unprobe(struct bpf_object *obj __maybe_unused) { return 0;}
111 static inline int bpf__load(struct bpf_object *obj __maybe_unused) { return 0; }
112
113 static inline int
114 bpf__foreach_event(struct bpf_object *obj __maybe_unused,
115                    bpf_prog_iter_callback_t func __maybe_unused,
116                    void *arg __maybe_unused)
117 {
118         return 0;
119 }
120
121 static inline int
122 bpf__config_obj(struct bpf_object *obj __maybe_unused,
123                 struct parse_events_term *term __maybe_unused,
124                 struct perf_evlist *evlist __maybe_unused,
125                 int *error_pos __maybe_unused)
126 {
127         return 0;
128 }
129
130 static inline int
131 bpf__apply_obj_config(void)
132 {
133         return 0;
134 }
135
136 static inline int
137 bpf__setup_stdout(struct perf_evlist *evlist __maybe_unused)
138 {
139         return 0;
140 }
141
142 static inline int
143 bpf__setup_output_event(struct perf_evlist *evlist __maybe_unused, const char *name __maybe_unused)
144 {
145         return 0;
146 }
147
148 static inline int
149 __bpf_strerror(char *buf, size_t size)
150 {
151         if (!size)
152                 return 0;
153         strncpy(buf,
154                 "ERROR: eBPF object loading is disabled during compiling.\n",
155                 size);
156         buf[size - 1] = '\0';
157         return 0;
158 }
159
160 static inline
161 int bpf__strerror_prepare_load(const char *filename __maybe_unused,
162                                bool source __maybe_unused,
163                                int err __maybe_unused,
164                                char *buf, size_t size)
165 {
166         return __bpf_strerror(buf, size);
167 }
168
169 static inline int
170 bpf__strerror_probe(struct bpf_object *obj __maybe_unused,
171                     int err __maybe_unused,
172                     char *buf, size_t size)
173 {
174         return __bpf_strerror(buf, size);
175 }
176
177 static inline int bpf__strerror_load(struct bpf_object *obj __maybe_unused,
178                                      int err __maybe_unused,
179                                      char *buf, size_t size)
180 {
181         return __bpf_strerror(buf, size);
182 }
183
184 static inline int
185 bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused,
186                          struct parse_events_term *term __maybe_unused,
187                          struct perf_evlist *evlist __maybe_unused,
188                          int *error_pos __maybe_unused,
189                          int err __maybe_unused,
190                          char *buf, size_t size)
191 {
192         return __bpf_strerror(buf, size);
193 }
194
195 static inline int
196 bpf__strerror_apply_obj_config(int err __maybe_unused,
197                                char *buf, size_t size)
198 {
199         return __bpf_strerror(buf, size);
200 }
201
202 static inline int
203 bpf__strerror_setup_stdout(struct perf_evlist *evlist __maybe_unused,
204                            int err __maybe_unused, char *buf,
205                            size_t size)
206 {
207         return __bpf_strerror(buf, size);
208 }
209 #endif
210 #endif