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> |
| 11 | #include <string.h> |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 12 | #include <bpf/libbpf.h> |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 13 | #include "probe-event.h" |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 14 | #include "evlist.h" |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 15 | #include "debug.h" |
| 16 | |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 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 */ |
Wang Nan | 0bb9349 | 2015-11-27 08:47:37 +0000 | [diff] [blame] | 25 | BPF_LOADER_ERRNO__PROGCONF_TERM,/* Invalid program config term in config string */ |
He Kuang | bfc077b | 2015-11-16 12:10:12 +0000 | [diff] [blame] | 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 */ |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 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 */ |
Wang Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame] | 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 */ |
Wang Nan | 2d055bf | 2016-02-22 09:10:34 +0000 | [diff] [blame] | 42 | BPF_LOADER_ERRNO__OBJCONF_MAP_IDX2BIG, /* Index too large */ |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 43 | __BPF_LOADER_ERRNO__END, |
| 44 | }; |
| 45 | |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 46 | struct bpf_object; |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 47 | struct parse_events_term; |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 48 | #define PERF_BPF_PROBE_GROUP "perf_bpf_probe" |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 49 | |
Wang Nan | cd102d70 | 2016-07-13 10:44:04 +0000 | [diff] [blame] | 50 | 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] | 51 | int fd, void *arg); |
| 52 | |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 53 | #ifdef HAVE_LIBBPF_SUPPORT |
Wang Nan | d509db0 | 2015-10-14 12:41:20 +0000 | [diff] [blame] | 54 | struct bpf_object *bpf__prepare_load(const char *filename, bool source); |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 55 | int bpf__strerror_prepare_load(const char *filename, bool source, |
| 56 | int err, char *buf, size_t size); |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 57 | |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 58 | struct bpf_object *bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz, |
| 59 | const char *name); |
| 60 | |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 61 | void bpf__clear(void); |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 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 | |
Wang Nan | 1e5e3ee | 2015-10-14 12:41:16 +0000 | [diff] [blame] | 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); |
Wang Nan | cd102d70 | 2016-07-13 10:44:04 +0000 | [diff] [blame] | 71 | int bpf__foreach_event(struct bpf_object *obj, |
| 72 | bpf_prog_iter_callback_t func, void *arg); |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 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); |
Wang Nan | 8690a2a | 2016-02-22 09:10:32 +0000 | [diff] [blame] | 81 | int bpf__apply_obj_config(void); |
| 82 | 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] | 83 | |
| 84 | int bpf__setup_stdout(struct perf_evlist *evlist); |
| 85 | int bpf__strerror_setup_stdout(struct perf_evlist *evlist, int err, |
| 86 | char *buf, size_t size); |
| 87 | |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 88 | #else |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 89 | #include <errno.h> |
| 90 | |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 91 | static inline struct bpf_object * |
Wang Nan | d509db0 | 2015-10-14 12:41:20 +0000 | [diff] [blame] | 92 | bpf__prepare_load(const char *filename __maybe_unused, |
| 93 | bool source __maybe_unused) |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 94 | { |
| 95 | pr_debug("ERROR: eBPF object loading is disabled during compiling.\n"); |
| 96 | return ERR_PTR(-ENOTSUP); |
| 97 | } |
| 98 | |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 99 | static inline struct bpf_object * |
| 100 | bpf__prepare_load_buffer(void *obj_buf __maybe_unused, |
| 101 | size_t obj_buf_sz __maybe_unused) |
| 102 | { |
| 103 | return ERR_PTR(-ENOTSUP); |
| 104 | } |
| 105 | |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 106 | static inline void bpf__clear(void) { } |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 107 | |
| 108 | static inline int bpf__probe(struct bpf_object *obj __maybe_unused) { return 0;} |
| 109 | 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] | 110 | 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] | 111 | |
| 112 | static inline int |
Wang Nan | cd102d70 | 2016-07-13 10:44:04 +0000 | [diff] [blame] | 113 | bpf__foreach_event(struct bpf_object *obj __maybe_unused, |
| 114 | bpf_prog_iter_callback_t func __maybe_unused, |
| 115 | void *arg __maybe_unused) |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 116 | { |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static inline int |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 121 | bpf__config_obj(struct bpf_object *obj __maybe_unused, |
| 122 | struct parse_events_term *term __maybe_unused, |
| 123 | struct perf_evlist *evlist __maybe_unused, |
| 124 | int *error_pos __maybe_unused) |
| 125 | { |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static inline int |
Wang Nan | 8690a2a | 2016-02-22 09:10:32 +0000 | [diff] [blame] | 130 | bpf__apply_obj_config(void) |
| 131 | { |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static inline int |
Wang Nan | d788857 | 2016-04-08 15:07:24 +0000 | [diff] [blame] | 136 | bpf__setup_stdout(struct perf_evlist *evlist __maybe_unused) |
| 137 | { |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static inline int |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 142 | __bpf_strerror(char *buf, size_t size) |
| 143 | { |
| 144 | if (!size) |
| 145 | return 0; |
| 146 | strncpy(buf, |
| 147 | "ERROR: eBPF object loading is disabled during compiling.\n", |
| 148 | size); |
| 149 | buf[size - 1] = '\0'; |
| 150 | return 0; |
| 151 | } |
| 152 | |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 153 | static inline |
| 154 | int bpf__strerror_prepare_load(const char *filename __maybe_unused, |
| 155 | bool source __maybe_unused, |
| 156 | int err __maybe_unused, |
| 157 | char *buf, size_t size) |
| 158 | { |
| 159 | return __bpf_strerror(buf, size); |
| 160 | } |
| 161 | |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 162 | static inline int |
| 163 | bpf__strerror_probe(struct bpf_object *obj __maybe_unused, |
| 164 | int err __maybe_unused, |
| 165 | char *buf, size_t size) |
| 166 | { |
| 167 | return __bpf_strerror(buf, size); |
| 168 | } |
Wang Nan | 1e5e3ee | 2015-10-14 12:41:16 +0000 | [diff] [blame] | 169 | |
| 170 | static inline int bpf__strerror_load(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 | } |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 176 | |
| 177 | static inline int |
| 178 | bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused, |
| 179 | struct parse_events_term *term __maybe_unused, |
| 180 | struct perf_evlist *evlist __maybe_unused, |
| 181 | int *error_pos __maybe_unused, |
| 182 | int err __maybe_unused, |
| 183 | char *buf, size_t size) |
| 184 | { |
| 185 | return __bpf_strerror(buf, size); |
| 186 | } |
Wang Nan | 8690a2a | 2016-02-22 09:10:32 +0000 | [diff] [blame] | 187 | |
| 188 | static inline int |
| 189 | bpf__strerror_apply_obj_config(int err __maybe_unused, |
| 190 | char *buf, size_t size) |
| 191 | { |
| 192 | return __bpf_strerror(buf, size); |
| 193 | } |
Wang Nan | d788857 | 2016-04-08 15:07:24 +0000 | [diff] [blame] | 194 | |
| 195 | static inline int |
| 196 | bpf__strerror_setup_stdout(struct perf_evlist *evlist __maybe_unused, |
| 197 | int err __maybe_unused, char *buf, |
| 198 | size_t size) |
| 199 | { |
| 200 | return __bpf_strerror(buf, size); |
| 201 | } |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 202 | #endif |
| 203 | #endif |