Merge tag 'printk-for-5.13-fixup' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.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
12 #ifdef HAVE_LIBBPF_SUPPORT
13 #include <bpf/libbpf.h>
14
15 enum bpf_loader_errno {
16         __BPF_LOADER_ERRNO__START = __LIBBPF_ERRNO__START - 100,
17         /* Invalid config string */
18         BPF_LOADER_ERRNO__CONFIG = __BPF_LOADER_ERRNO__START,
19         BPF_LOADER_ERRNO__GROUP,        /* Invalid group name */
20         BPF_LOADER_ERRNO__EVENTNAME,    /* Event name is missing */
21         BPF_LOADER_ERRNO__INTERNAL,     /* BPF loader internal error */
22         BPF_LOADER_ERRNO__COMPILE,      /* Error when compiling BPF scriptlet */
23         BPF_LOADER_ERRNO__PROGCONF_TERM,/* Invalid program config term in config string */
24         BPF_LOADER_ERRNO__PROLOGUE,     /* Failed to generate prologue */
25         BPF_LOADER_ERRNO__PROLOGUE2BIG, /* Prologue too big for program */
26         BPF_LOADER_ERRNO__PROLOGUEOOB,  /* Offset out of bound for prologue */
27         BPF_LOADER_ERRNO__OBJCONF_OPT,  /* Invalid object config option */
28         BPF_LOADER_ERRNO__OBJCONF_CONF, /* Config value not set (lost '=')) */
29         BPF_LOADER_ERRNO__OBJCONF_MAP_OPT,      /* Invalid object map config option */
30         BPF_LOADER_ERRNO__OBJCONF_MAP_NOTEXIST, /* Target map not exist */
31         BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE,    /* Incorrect value type for map */
32         BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE,     /* Incorrect map type */
33         BPF_LOADER_ERRNO__OBJCONF_MAP_KEYSIZE,  /* Incorrect map key size */
34         BPF_LOADER_ERRNO__OBJCONF_MAP_VALUESIZE,/* Incorrect map value size */
35         BPF_LOADER_ERRNO__OBJCONF_MAP_NOEVT,    /* Event not found for map setting */
36         BPF_LOADER_ERRNO__OBJCONF_MAP_MAPSIZE,  /* Invalid map size for event setting */
37         BPF_LOADER_ERRNO__OBJCONF_MAP_EVTDIM,   /* Event dimension too large */
38         BPF_LOADER_ERRNO__OBJCONF_MAP_EVTINH,   /* Doesn't support inherit event */
39         BPF_LOADER_ERRNO__OBJCONF_MAP_EVTTYPE,  /* Wrong event type for map */
40         BPF_LOADER_ERRNO__OBJCONF_MAP_IDX2BIG,  /* Index too large */
41         __BPF_LOADER_ERRNO__END,
42 };
43 #endif // HAVE_LIBBPF_SUPPORT
44
45 struct evsel;
46 struct evlist;
47 struct bpf_object;
48 struct parse_events_term;
49 #define PERF_BPF_PROBE_GROUP "perf_bpf_probe"
50
51 typedef int (*bpf_prog_iter_callback_t)(const char *group, const char *event,
52                                         int fd, struct bpf_object *obj, void *arg);
53
54 #ifdef HAVE_LIBBPF_SUPPORT
55 struct bpf_object *bpf__prepare_load(const char *filename, bool source);
56 int bpf__strerror_prepare_load(const char *filename, bool source,
57                                int err, char *buf, size_t size);
58
59 struct bpf_object *bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz,
60                                             const char *name);
61
62 void bpf__clear(void);
63
64 int bpf__probe(struct bpf_object *obj);
65 int bpf__unprobe(struct bpf_object *obj);
66 int bpf__strerror_probe(struct bpf_object *obj, int err,
67                         char *buf, size_t size);
68
69 int bpf__load(struct bpf_object *obj);
70 int bpf__strerror_load(struct bpf_object *obj, int err,
71                        char *buf, size_t size);
72 int bpf__foreach_event(struct bpf_object *obj,
73                        bpf_prog_iter_callback_t func, void *arg);
74
75 int bpf__config_obj(struct bpf_object *obj, struct parse_events_term *term,
76                     struct evlist *evlist, int *error_pos);
77 int bpf__strerror_config_obj(struct bpf_object *obj,
78                              struct parse_events_term *term,
79                              struct evlist *evlist,
80                              int *error_pos, int err, char *buf,
81                              size_t size);
82 int bpf__apply_obj_config(void);
83 int bpf__strerror_apply_obj_config(int err, char *buf, size_t size);
84
85 int bpf__setup_stdout(struct evlist *evlist);
86 struct evsel *bpf__setup_output_event(struct evlist *evlist, const char *name);
87 int bpf__strerror_setup_output_event(struct evlist *evlist, int err, char *buf, size_t size);
88 #else
89 #include <errno.h>
90 #include <string.h>
91 #include "debug.h"
92
93 static inline struct bpf_object *
94 bpf__prepare_load(const char *filename __maybe_unused,
95                   bool source __maybe_unused)
96 {
97         pr_debug("ERROR: eBPF object loading is disabled during compiling.\n");
98         return ERR_PTR(-ENOTSUP);
99 }
100
101 static inline struct bpf_object *
102 bpf__prepare_load_buffer(void *obj_buf __maybe_unused,
103                                            size_t obj_buf_sz __maybe_unused)
104 {
105         return ERR_PTR(-ENOTSUP);
106 }
107
108 static inline void bpf__clear(void) { }
109
110 static inline int bpf__probe(struct bpf_object *obj __maybe_unused) { return 0;}
111 static inline int bpf__unprobe(struct bpf_object *obj __maybe_unused) { return 0;}
112 static inline int bpf__load(struct bpf_object *obj __maybe_unused) { return 0; }
113
114 static inline int
115 bpf__foreach_event(struct bpf_object *obj __maybe_unused,
116                    bpf_prog_iter_callback_t func __maybe_unused,
117                    void *arg __maybe_unused)
118 {
119         return 0;
120 }
121
122 static inline int
123 bpf__config_obj(struct bpf_object *obj __maybe_unused,
124                 struct parse_events_term *term __maybe_unused,
125                 struct evlist *evlist __maybe_unused,
126                 int *error_pos __maybe_unused)
127 {
128         return 0;
129 }
130
131 static inline int
132 bpf__apply_obj_config(void)
133 {
134         return 0;
135 }
136
137 static inline int
138 bpf__setup_stdout(struct evlist *evlist __maybe_unused)
139 {
140         return 0;
141 }
142
143 static inline struct evsel *
144 bpf__setup_output_event(struct evlist *evlist __maybe_unused, const char *name __maybe_unused)
145 {
146         return NULL;
147 }
148
149 static inline int
150 __bpf_strerror(char *buf, size_t size)
151 {
152         if (!size)
153                 return 0;
154         strncpy(buf,
155                 "ERROR: eBPF object loading is disabled during compiling.\n",
156                 size);
157         buf[size - 1] = '\0';
158         return 0;
159 }
160
161 static inline
162 int bpf__strerror_prepare_load(const char *filename __maybe_unused,
163                                bool source __maybe_unused,
164                                int err __maybe_unused,
165                                char *buf, size_t size)
166 {
167         return __bpf_strerror(buf, size);
168 }
169
170 static inline int
171 bpf__strerror_probe(struct bpf_object *obj __maybe_unused,
172                     int err __maybe_unused,
173                     char *buf, size_t size)
174 {
175         return __bpf_strerror(buf, size);
176 }
177
178 static inline int bpf__strerror_load(struct bpf_object *obj __maybe_unused,
179                                      int err __maybe_unused,
180                                      char *buf, size_t size)
181 {
182         return __bpf_strerror(buf, size);
183 }
184
185 static inline int
186 bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused,
187                          struct parse_events_term *term __maybe_unused,
188                          struct evlist *evlist __maybe_unused,
189                          int *error_pos __maybe_unused,
190                          int err __maybe_unused,
191                          char *buf, size_t size)
192 {
193         return __bpf_strerror(buf, size);
194 }
195
196 static inline int
197 bpf__strerror_apply_obj_config(int err __maybe_unused,
198                                char *buf, size_t size)
199 {
200         return __bpf_strerror(buf, size);
201 }
202
203 static inline int
204 bpf__strerror_setup_output_event(struct evlist *evlist __maybe_unused,
205                                  int err __maybe_unused, char *buf, size_t size)
206 {
207         return __bpf_strerror(buf, size);
208 }
209
210 #endif
211
212 static inline int bpf__strerror_setup_stdout(struct evlist *evlist, int err, char *buf, size_t size)
213 {
214         return bpf__strerror_setup_output_event(evlist, err, buf, size);
215 }
216 #endif