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 | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 13 | #include "debug.h" |
| 14 | |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 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 */ |
Wang Nan | 361f2b1 | 2015-11-16 12:10:05 +0000 | [diff] [blame] | 23 | BPF_LOADER_ERRNO__CONFIG_TERM, /* Invalid config term in config term */ |
He Kuang | bfc077b | 2015-11-16 12:10:12 +0000 | [diff] [blame^] | 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 */ |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 27 | __BPF_LOADER_ERRNO__END, |
| 28 | }; |
| 29 | |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 30 | struct bpf_object; |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 31 | #define PERF_BPF_PROBE_GROUP "perf_bpf_probe" |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 32 | |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 33 | typedef int (*bpf_prog_iter_callback_t)(struct probe_trace_event *tev, |
| 34 | int fd, void *arg); |
| 35 | |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 36 | #ifdef HAVE_LIBBPF_SUPPORT |
Wang Nan | d509db0 | 2015-10-14 12:41:20 +0000 | [diff] [blame] | 37 | struct bpf_object *bpf__prepare_load(const char *filename, bool source); |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 38 | int bpf__strerror_prepare_load(const char *filename, bool source, |
| 39 | int err, char *buf, size_t size); |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 40 | |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 41 | struct bpf_object *bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz, |
| 42 | const char *name); |
| 43 | |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 44 | void bpf__clear(void); |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 45 | |
| 46 | int bpf__probe(struct bpf_object *obj); |
| 47 | int bpf__unprobe(struct bpf_object *obj); |
| 48 | int bpf__strerror_probe(struct bpf_object *obj, int err, |
| 49 | char *buf, size_t size); |
| 50 | |
Wang Nan | 1e5e3ee | 2015-10-14 12:41:16 +0000 | [diff] [blame] | 51 | int bpf__load(struct bpf_object *obj); |
| 52 | int bpf__strerror_load(struct bpf_object *obj, int err, |
| 53 | char *buf, size_t size); |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 54 | int bpf__foreach_tev(struct bpf_object *obj, |
| 55 | bpf_prog_iter_callback_t func, void *arg); |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 56 | #else |
| 57 | static inline struct bpf_object * |
Wang Nan | d509db0 | 2015-10-14 12:41:20 +0000 | [diff] [blame] | 58 | bpf__prepare_load(const char *filename __maybe_unused, |
| 59 | bool source __maybe_unused) |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 60 | { |
| 61 | pr_debug("ERROR: eBPF object loading is disabled during compiling.\n"); |
| 62 | return ERR_PTR(-ENOTSUP); |
| 63 | } |
| 64 | |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 65 | static inline struct bpf_object * |
| 66 | bpf__prepare_load_buffer(void *obj_buf __maybe_unused, |
| 67 | size_t obj_buf_sz __maybe_unused) |
| 68 | { |
| 69 | return ERR_PTR(-ENOTSUP); |
| 70 | } |
| 71 | |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 72 | static inline void bpf__clear(void) { } |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 73 | |
| 74 | static inline int bpf__probe(struct bpf_object *obj __maybe_unused) { return 0;} |
| 75 | 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] | 76 | 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] | 77 | |
| 78 | static inline int |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 79 | bpf__foreach_tev(struct bpf_object *obj __maybe_unused, |
| 80 | bpf_prog_iter_callback_t func __maybe_unused, |
| 81 | void *arg __maybe_unused) |
| 82 | { |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static inline int |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 87 | __bpf_strerror(char *buf, size_t size) |
| 88 | { |
| 89 | if (!size) |
| 90 | return 0; |
| 91 | strncpy(buf, |
| 92 | "ERROR: eBPF object loading is disabled during compiling.\n", |
| 93 | size); |
| 94 | buf[size - 1] = '\0'; |
| 95 | return 0; |
| 96 | } |
| 97 | |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 98 | static inline |
| 99 | int bpf__strerror_prepare_load(const char *filename __maybe_unused, |
| 100 | bool source __maybe_unused, |
| 101 | int err __maybe_unused, |
| 102 | char *buf, size_t size) |
| 103 | { |
| 104 | return __bpf_strerror(buf, size); |
| 105 | } |
| 106 | |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 107 | static inline int |
| 108 | bpf__strerror_probe(struct bpf_object *obj __maybe_unused, |
| 109 | int err __maybe_unused, |
| 110 | char *buf, size_t size) |
| 111 | { |
| 112 | return __bpf_strerror(buf, size); |
| 113 | } |
Wang Nan | 1e5e3ee | 2015-10-14 12:41:16 +0000 | [diff] [blame] | 114 | |
| 115 | static inline int bpf__strerror_load(struct bpf_object *obj __maybe_unused, |
| 116 | int err __maybe_unused, |
| 117 | char *buf, size_t size) |
| 118 | { |
| 119 | return __bpf_strerror(buf, size); |
| 120 | } |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 121 | #endif |
| 122 | #endif |