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 | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 36 | __BPF_LOADER_ERRNO__END, |
| 37 | }; |
| 38 | |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 39 | struct bpf_object; |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame^] | 40 | struct parse_events_term; |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 41 | #define PERF_BPF_PROBE_GROUP "perf_bpf_probe" |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 42 | |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 43 | typedef int (*bpf_prog_iter_callback_t)(struct probe_trace_event *tev, |
| 44 | int fd, void *arg); |
| 45 | |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 46 | #ifdef HAVE_LIBBPF_SUPPORT |
Wang Nan | d509db0 | 2015-10-14 12:41:20 +0000 | [diff] [blame] | 47 | struct bpf_object *bpf__prepare_load(const char *filename, bool source); |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 48 | int bpf__strerror_prepare_load(const char *filename, bool source, |
| 49 | int err, char *buf, size_t size); |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 50 | |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 51 | struct bpf_object *bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz, |
| 52 | const char *name); |
| 53 | |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 54 | void bpf__clear(void); |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 55 | |
| 56 | int bpf__probe(struct bpf_object *obj); |
| 57 | int bpf__unprobe(struct bpf_object *obj); |
| 58 | int bpf__strerror_probe(struct bpf_object *obj, int err, |
| 59 | char *buf, size_t size); |
| 60 | |
Wang Nan | 1e5e3ee | 2015-10-14 12:41:16 +0000 | [diff] [blame] | 61 | int bpf__load(struct bpf_object *obj); |
| 62 | int bpf__strerror_load(struct bpf_object *obj, int err, |
| 63 | char *buf, size_t size); |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 64 | int bpf__foreach_tev(struct bpf_object *obj, |
| 65 | bpf_prog_iter_callback_t func, void *arg); |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame^] | 66 | |
| 67 | int bpf__config_obj(struct bpf_object *obj, struct parse_events_term *term, |
| 68 | struct perf_evlist *evlist, int *error_pos); |
| 69 | int bpf__strerror_config_obj(struct bpf_object *obj, |
| 70 | struct parse_events_term *term, |
| 71 | struct perf_evlist *evlist, |
| 72 | int *error_pos, int err, char *buf, |
| 73 | size_t size); |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 74 | #else |
| 75 | static inline struct bpf_object * |
Wang Nan | d509db0 | 2015-10-14 12:41:20 +0000 | [diff] [blame] | 76 | bpf__prepare_load(const char *filename __maybe_unused, |
| 77 | bool source __maybe_unused) |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 78 | { |
| 79 | pr_debug("ERROR: eBPF object loading is disabled during compiling.\n"); |
| 80 | return ERR_PTR(-ENOTSUP); |
| 81 | } |
| 82 | |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 83 | static inline struct bpf_object * |
| 84 | bpf__prepare_load_buffer(void *obj_buf __maybe_unused, |
| 85 | size_t obj_buf_sz __maybe_unused) |
| 86 | { |
| 87 | return ERR_PTR(-ENOTSUP); |
| 88 | } |
| 89 | |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 90 | static inline void bpf__clear(void) { } |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 91 | |
| 92 | static inline int bpf__probe(struct bpf_object *obj __maybe_unused) { return 0;} |
| 93 | 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] | 94 | 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] | 95 | |
| 96 | static inline int |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 97 | bpf__foreach_tev(struct bpf_object *obj __maybe_unused, |
| 98 | bpf_prog_iter_callback_t func __maybe_unused, |
| 99 | void *arg __maybe_unused) |
| 100 | { |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static inline int |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame^] | 105 | bpf__config_obj(struct bpf_object *obj __maybe_unused, |
| 106 | struct parse_events_term *term __maybe_unused, |
| 107 | struct perf_evlist *evlist __maybe_unused, |
| 108 | int *error_pos __maybe_unused) |
| 109 | { |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static inline int |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 114 | __bpf_strerror(char *buf, size_t size) |
| 115 | { |
| 116 | if (!size) |
| 117 | return 0; |
| 118 | strncpy(buf, |
| 119 | "ERROR: eBPF object loading is disabled during compiling.\n", |
| 120 | size); |
| 121 | buf[size - 1] = '\0'; |
| 122 | return 0; |
| 123 | } |
| 124 | |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 125 | static inline |
| 126 | int bpf__strerror_prepare_load(const char *filename __maybe_unused, |
| 127 | bool source __maybe_unused, |
| 128 | int err __maybe_unused, |
| 129 | char *buf, size_t size) |
| 130 | { |
| 131 | return __bpf_strerror(buf, size); |
| 132 | } |
| 133 | |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 134 | static inline int |
| 135 | bpf__strerror_probe(struct bpf_object *obj __maybe_unused, |
| 136 | int err __maybe_unused, |
| 137 | char *buf, size_t size) |
| 138 | { |
| 139 | return __bpf_strerror(buf, size); |
| 140 | } |
Wang Nan | 1e5e3ee | 2015-10-14 12:41:16 +0000 | [diff] [blame] | 141 | |
| 142 | static inline int bpf__strerror_load(struct bpf_object *obj __maybe_unused, |
| 143 | int err __maybe_unused, |
| 144 | char *buf, size_t size) |
| 145 | { |
| 146 | return __bpf_strerror(buf, size); |
| 147 | } |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame^] | 148 | |
| 149 | static inline int |
| 150 | bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused, |
| 151 | struct parse_events_term *term __maybe_unused, |
| 152 | struct perf_evlist *evlist __maybe_unused, |
| 153 | int *error_pos __maybe_unused, |
| 154 | int err __maybe_unused, |
| 155 | char *buf, size_t size) |
| 156 | { |
| 157 | return __bpf_strerror(buf, size); |
| 158 | } |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 159 | #endif |
| 160 | #endif |