Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 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> |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 11 | #include <bpf/libbpf.h> |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 12 | |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 13 | enum bpf_loader_errno { |
| 14 | __BPF_LOADER_ERRNO__START = __LIBBPF_ERRNO__START - 100, |
| 15 | /* Invalid config string */ |
| 16 | BPF_LOADER_ERRNO__CONFIG = __BPF_LOADER_ERRNO__START, |
| 17 | BPF_LOADER_ERRNO__GROUP, /* Invalid group name */ |
| 18 | BPF_LOADER_ERRNO__EVENTNAME, /* Event name is missing */ |
| 19 | BPF_LOADER_ERRNO__INTERNAL, /* BPF loader internal error */ |
| 20 | BPF_LOADER_ERRNO__COMPILE, /* Error when compiling BPF scriptlet */ |
Wang Nan | 0bb9349 | 2015-11-27 08:47:37 +0000 | [diff] [blame] | 21 | BPF_LOADER_ERRNO__PROGCONF_TERM,/* Invalid program config term in config string */ |
He Kuang | bfc077b | 2015-11-16 12:10:12 +0000 | [diff] [blame] | 22 | BPF_LOADER_ERRNO__PROLOGUE, /* Failed to generate prologue */ |
| 23 | BPF_LOADER_ERRNO__PROLOGUE2BIG, /* Prologue too big for program */ |
| 24 | BPF_LOADER_ERRNO__PROLOGUEOOB, /* Offset out of bound for prologue */ |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 25 | BPF_LOADER_ERRNO__OBJCONF_OPT, /* Invalid object config option */ |
| 26 | BPF_LOADER_ERRNO__OBJCONF_CONF, /* Config value not set (lost '=')) */ |
| 27 | BPF_LOADER_ERRNO__OBJCONF_MAP_OPT, /* Invalid object map config option */ |
| 28 | BPF_LOADER_ERRNO__OBJCONF_MAP_NOTEXIST, /* Target map not exist */ |
| 29 | BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE, /* Incorrect value type for map */ |
| 30 | BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE, /* Incorrect map type */ |
| 31 | BPF_LOADER_ERRNO__OBJCONF_MAP_KEYSIZE, /* Incorrect map key size */ |
| 32 | BPF_LOADER_ERRNO__OBJCONF_MAP_VALUESIZE,/* Incorrect map value size */ |
Wang Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame] | 33 | BPF_LOADER_ERRNO__OBJCONF_MAP_NOEVT, /* Event not found for map setting */ |
| 34 | BPF_LOADER_ERRNO__OBJCONF_MAP_MAPSIZE, /* Invalid map size for event setting */ |
| 35 | BPF_LOADER_ERRNO__OBJCONF_MAP_EVTDIM, /* Event dimension too large */ |
| 36 | BPF_LOADER_ERRNO__OBJCONF_MAP_EVTINH, /* Doesn't support inherit event */ |
| 37 | BPF_LOADER_ERRNO__OBJCONF_MAP_EVTTYPE, /* Wrong event type for map */ |
Wang Nan | 2d055bf | 2016-02-22 09:10:34 +0000 | [diff] [blame] | 38 | BPF_LOADER_ERRNO__OBJCONF_MAP_IDX2BIG, /* Index too large */ |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 39 | __BPF_LOADER_ERRNO__END, |
| 40 | }; |
| 41 | |
Arnaldo Carvalho de Melo | 78e890e | 2018-08-07 16:19:05 -0300 | [diff] [blame] | 42 | struct perf_evsel; |
Arnaldo Carvalho de Melo | ebc52ae | 2019-01-29 13:06:18 +0100 | [diff] [blame] | 43 | struct perf_evlist; |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 44 | struct bpf_object; |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 45 | struct parse_events_term; |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 46 | #define PERF_BPF_PROBE_GROUP "perf_bpf_probe" |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 47 | |
Wang Nan | cd102d7 | 2016-07-13 10:44:04 +0000 | [diff] [blame] | 48 | typedef int (*bpf_prog_iter_callback_t)(const char *group, const char *event, |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 49 | int fd, void *arg); |
| 50 | |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 51 | #ifdef HAVE_LIBBPF_SUPPORT |
Wang Nan | d509db0 | 2015-10-14 12:41:20 +0000 | [diff] [blame] | 52 | struct bpf_object *bpf__prepare_load(const char *filename, bool source); |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 53 | int bpf__strerror_prepare_load(const char *filename, bool source, |
| 54 | int err, char *buf, size_t size); |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 55 | |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 56 | struct bpf_object *bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz, |
| 57 | const char *name); |
| 58 | |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 59 | void bpf__clear(void); |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 60 | |
| 61 | int bpf__probe(struct bpf_object *obj); |
| 62 | int bpf__unprobe(struct bpf_object *obj); |
| 63 | int bpf__strerror_probe(struct bpf_object *obj, int err, |
| 64 | char *buf, size_t size); |
| 65 | |
Wang Nan | 1e5e3ee | 2015-10-14 12:41:16 +0000 | [diff] [blame] | 66 | int bpf__load(struct bpf_object *obj); |
| 67 | int bpf__strerror_load(struct bpf_object *obj, int err, |
| 68 | char *buf, size_t size); |
Wang Nan | cd102d7 | 2016-07-13 10:44:04 +0000 | [diff] [blame] | 69 | int bpf__foreach_event(struct bpf_object *obj, |
| 70 | bpf_prog_iter_callback_t func, void *arg); |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 71 | |
| 72 | int bpf__config_obj(struct bpf_object *obj, struct parse_events_term *term, |
| 73 | struct perf_evlist *evlist, int *error_pos); |
| 74 | int bpf__strerror_config_obj(struct bpf_object *obj, |
| 75 | struct parse_events_term *term, |
| 76 | struct perf_evlist *evlist, |
| 77 | int *error_pos, int err, char *buf, |
| 78 | size_t size); |
Wang Nan | 8690a2a | 2016-02-22 09:10:32 +0000 | [diff] [blame] | 79 | int bpf__apply_obj_config(void); |
| 80 | int bpf__strerror_apply_obj_config(int err, char *buf, size_t size); |
Wang Nan | d788857 | 2016-04-08 15:07:24 +0000 | [diff] [blame] | 81 | |
| 82 | int bpf__setup_stdout(struct perf_evlist *evlist); |
Arnaldo Carvalho de Melo | 78e890e | 2018-08-07 16:19:05 -0300 | [diff] [blame] | 83 | struct perf_evsel *bpf__setup_output_event(struct perf_evlist *evlist, const char *name); |
Arnaldo Carvalho de Melo | aa31be3 | 2018-08-06 11:35:37 -0300 | [diff] [blame] | 84 | int bpf__strerror_setup_output_event(struct perf_evlist *evlist, int err, char *buf, size_t size); |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 85 | #else |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 86 | #include <errno.h> |
Arnaldo Carvalho de Melo | ebc52ae | 2019-01-29 13:06:18 +0100 | [diff] [blame] | 87 | #include <string.h> |
| 88 | #include "debug.h" |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 89 | |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 90 | static inline struct bpf_object * |
Wang Nan | d509db0 | 2015-10-14 12:41:20 +0000 | [diff] [blame] | 91 | bpf__prepare_load(const char *filename __maybe_unused, |
| 92 | bool source __maybe_unused) |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 93 | { |
| 94 | pr_debug("ERROR: eBPF object loading is disabled during compiling.\n"); |
| 95 | return ERR_PTR(-ENOTSUP); |
| 96 | } |
| 97 | |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 98 | static inline struct bpf_object * |
| 99 | bpf__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 Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 105 | static inline void bpf__clear(void) { } |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 106 | |
| 107 | static inline int bpf__probe(struct bpf_object *obj __maybe_unused) { return 0;} |
| 108 | static inline int bpf__unprobe(struct bpf_object *obj __maybe_unused) { return 0;} |
Wang Nan | 1e5e3ee | 2015-10-14 12:41:16 +0000 | [diff] [blame] | 109 | static inline int bpf__load(struct bpf_object *obj __maybe_unused) { return 0; } |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 110 | |
| 111 | static inline int |
Wang Nan | cd102d7 | 2016-07-13 10:44:04 +0000 | [diff] [blame] | 112 | bpf__foreach_event(struct bpf_object *obj __maybe_unused, |
| 113 | bpf_prog_iter_callback_t func __maybe_unused, |
| 114 | void *arg __maybe_unused) |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 115 | { |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static inline int |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 120 | bpf__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 | |
| 128 | static inline int |
Wang Nan | 8690a2a | 2016-02-22 09:10:32 +0000 | [diff] [blame] | 129 | bpf__apply_obj_config(void) |
| 130 | { |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | static inline int |
Wang Nan | d788857 | 2016-04-08 15:07:24 +0000 | [diff] [blame] | 135 | bpf__setup_stdout(struct perf_evlist *evlist __maybe_unused) |
| 136 | { |
| 137 | return 0; |
| 138 | } |
| 139 | |
Arnaldo Carvalho de Melo | 78e890e | 2018-08-07 16:19:05 -0300 | [diff] [blame] | 140 | static inline struct perf_evsel * |
Arnaldo Carvalho de Melo | 92bbe8d | 2018-08-06 09:53:35 -0300 | [diff] [blame] | 141 | bpf__setup_output_event(struct perf_evlist *evlist __maybe_unused, const char *name __maybe_unused) |
| 142 | { |
Arnaldo Carvalho de Melo | 78e890e | 2018-08-07 16:19:05 -0300 | [diff] [blame] | 143 | return NULL; |
Arnaldo Carvalho de Melo | 92bbe8d | 2018-08-06 09:53:35 -0300 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static inline int |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 147 | __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 Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 158 | static inline |
| 159 | int 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 Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 167 | static inline int |
| 168 | bpf__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 Nan | 1e5e3ee | 2015-10-14 12:41:16 +0000 | [diff] [blame] | 174 | |
| 175 | static 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 Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 181 | |
| 182 | static inline int |
| 183 | bpf__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 Nan | 8690a2a | 2016-02-22 09:10:32 +0000 | [diff] [blame] | 192 | |
| 193 | static inline int |
| 194 | bpf__strerror_apply_obj_config(int err __maybe_unused, |
| 195 | char *buf, size_t size) |
| 196 | { |
| 197 | return __bpf_strerror(buf, size); |
| 198 | } |
Wang Nan | d788857 | 2016-04-08 15:07:24 +0000 | [diff] [blame] | 199 | |
| 200 | static inline int |
Arnaldo Carvalho de Melo | aa31be3 | 2018-08-06 11:35:37 -0300 | [diff] [blame] | 201 | bpf__strerror_setup_output_event(struct perf_evlist *evlist __maybe_unused, |
| 202 | int err __maybe_unused, char *buf, size_t size) |
Wang Nan | d788857 | 2016-04-08 15:07:24 +0000 | [diff] [blame] | 203 | { |
| 204 | return __bpf_strerror(buf, size); |
| 205 | } |
Arnaldo Carvalho de Melo | aa31be3 | 2018-08-06 11:35:37 -0300 | [diff] [blame] | 206 | |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 207 | #endif |
Arnaldo Carvalho de Melo | aa31be3 | 2018-08-06 11:35:37 -0300 | [diff] [blame] | 208 | |
| 209 | static 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 Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 213 | #endif |