Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 1 | /* |
| 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 Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 11 | #include <bpf/libbpf.h> |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 12 | #include "probe-event.h" |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 13 | #include "evlist.h" |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 14 | #include "debug.h" |
| 15 | |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 16 | enum 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 Nan | 0bb9349 | 2015-11-27 08:47:37 +0000 | [diff] [blame] | 24 | BPF_LOADER_ERRNO__PROGCONF_TERM,/* Invalid program config term in config string */ |
He Kuang | bfc077b | 2015-11-16 12:10:12 +0000 | [diff] [blame] | 25 | 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 Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 28 | 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 Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame^] | 36 | BPF_LOADER_ERRNO__OBJCONF_MAP_NOEVT, /* Event not found for map setting */ |
| 37 | BPF_LOADER_ERRNO__OBJCONF_MAP_MAPSIZE, /* Invalid map size for event setting */ |
| 38 | BPF_LOADER_ERRNO__OBJCONF_MAP_EVTDIM, /* Event dimension too large */ |
| 39 | BPF_LOADER_ERRNO__OBJCONF_MAP_EVTINH, /* Doesn't support inherit event */ |
| 40 | BPF_LOADER_ERRNO__OBJCONF_MAP_EVTTYPE, /* Wrong event type for map */ |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 41 | __BPF_LOADER_ERRNO__END, |
| 42 | }; |
| 43 | |
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 | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 48 | typedef int (*bpf_prog_iter_callback_t)(struct probe_trace_event *tev, |
| 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 | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 69 | int bpf__foreach_tev(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 | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 81 | #else |
| 82 | static inline struct bpf_object * |
Wang Nan | d509db0 | 2015-10-14 12:41:20 +0000 | [diff] [blame] | 83 | bpf__prepare_load(const char *filename __maybe_unused, |
| 84 | bool source __maybe_unused) |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 85 | { |
| 86 | pr_debug("ERROR: eBPF object loading is disabled during compiling.\n"); |
| 87 | return ERR_PTR(-ENOTSUP); |
| 88 | } |
| 89 | |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 90 | static inline struct bpf_object * |
| 91 | bpf__prepare_load_buffer(void *obj_buf __maybe_unused, |
| 92 | size_t obj_buf_sz __maybe_unused) |
| 93 | { |
| 94 | return ERR_PTR(-ENOTSUP); |
| 95 | } |
| 96 | |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 97 | static inline void bpf__clear(void) { } |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 98 | |
| 99 | static inline int bpf__probe(struct bpf_object *obj __maybe_unused) { return 0;} |
| 100 | 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] | 101 | 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] | 102 | |
| 103 | static inline int |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 104 | bpf__foreach_tev(struct bpf_object *obj __maybe_unused, |
| 105 | bpf_prog_iter_callback_t func __maybe_unused, |
| 106 | void *arg __maybe_unused) |
| 107 | { |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static inline int |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 112 | bpf__config_obj(struct bpf_object *obj __maybe_unused, |
| 113 | struct parse_events_term *term __maybe_unused, |
| 114 | struct perf_evlist *evlist __maybe_unused, |
| 115 | int *error_pos __maybe_unused) |
| 116 | { |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static inline int |
Wang Nan | 8690a2a | 2016-02-22 09:10:32 +0000 | [diff] [blame] | 121 | bpf__apply_obj_config(void) |
| 122 | { |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static inline int |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 127 | __bpf_strerror(char *buf, size_t size) |
| 128 | { |
| 129 | if (!size) |
| 130 | return 0; |
| 131 | strncpy(buf, |
| 132 | "ERROR: eBPF object loading is disabled during compiling.\n", |
| 133 | size); |
| 134 | buf[size - 1] = '\0'; |
| 135 | return 0; |
| 136 | } |
| 137 | |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 138 | static inline |
| 139 | int bpf__strerror_prepare_load(const char *filename __maybe_unused, |
| 140 | bool source __maybe_unused, |
| 141 | int err __maybe_unused, |
| 142 | char *buf, size_t size) |
| 143 | { |
| 144 | return __bpf_strerror(buf, size); |
| 145 | } |
| 146 | |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 147 | static inline int |
| 148 | bpf__strerror_probe(struct bpf_object *obj __maybe_unused, |
| 149 | int err __maybe_unused, |
| 150 | char *buf, size_t size) |
| 151 | { |
| 152 | return __bpf_strerror(buf, size); |
| 153 | } |
Wang Nan | 1e5e3ee | 2015-10-14 12:41:16 +0000 | [diff] [blame] | 154 | |
| 155 | static inline int bpf__strerror_load(struct bpf_object *obj __maybe_unused, |
| 156 | int err __maybe_unused, |
| 157 | char *buf, size_t size) |
| 158 | { |
| 159 | return __bpf_strerror(buf, size); |
| 160 | } |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 161 | |
| 162 | static inline int |
| 163 | bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused, |
| 164 | struct parse_events_term *term __maybe_unused, |
| 165 | struct perf_evlist *evlist __maybe_unused, |
| 166 | int *error_pos __maybe_unused, |
| 167 | int err __maybe_unused, |
| 168 | char *buf, size_t size) |
| 169 | { |
| 170 | return __bpf_strerror(buf, size); |
| 171 | } |
Wang Nan | 8690a2a | 2016-02-22 09:10:32 +0000 | [diff] [blame] | 172 | |
| 173 | static inline int |
| 174 | bpf__strerror_apply_obj_config(int err __maybe_unused, |
| 175 | char *buf, size_t size) |
| 176 | { |
| 177 | return __bpf_strerror(buf, size); |
| 178 | } |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 179 | #endif |
| 180 | #endif |