bpf-loader.h (6690B)
1/* SPDX-License-Identifier: GPL-2.0 */ 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 12#ifdef HAVE_LIBBPF_SUPPORT 13#include <bpf/libbpf.h> 14 15enum 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 */ 23 BPF_LOADER_ERRNO__PROGCONF_TERM,/* Invalid program config term in config string */ 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 */ 27 BPF_LOADER_ERRNO__OBJCONF_OPT, /* Invalid object config option */ 28 BPF_LOADER_ERRNO__OBJCONF_CONF, /* Config value not set (lost '=')) */ 29 BPF_LOADER_ERRNO__OBJCONF_MAP_OPT, /* Invalid object map config option */ 30 BPF_LOADER_ERRNO__OBJCONF_MAP_NOTEXIST, /* Target map not exist */ 31 BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE, /* Incorrect value type for map */ 32 BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE, /* Incorrect map type */ 33 BPF_LOADER_ERRNO__OBJCONF_MAP_KEYSIZE, /* Incorrect map key size */ 34 BPF_LOADER_ERRNO__OBJCONF_MAP_VALUESIZE,/* Incorrect map value size */ 35 BPF_LOADER_ERRNO__OBJCONF_MAP_NOEVT, /* Event not found for map setting */ 36 BPF_LOADER_ERRNO__OBJCONF_MAP_MAPSIZE, /* Invalid map size for event setting */ 37 BPF_LOADER_ERRNO__OBJCONF_MAP_EVTDIM, /* Event dimension too large */ 38 BPF_LOADER_ERRNO__OBJCONF_MAP_EVTINH, /* Doesn't support inherit event */ 39 BPF_LOADER_ERRNO__OBJCONF_MAP_EVTTYPE, /* Wrong event type for map */ 40 BPF_LOADER_ERRNO__OBJCONF_MAP_IDX2BIG, /* Index too large */ 41 __BPF_LOADER_ERRNO__END, 42}; 43#endif // HAVE_LIBBPF_SUPPORT 44 45struct evsel; 46struct evlist; 47struct bpf_object; 48struct parse_events_term; 49#define PERF_BPF_PROBE_GROUP "perf_bpf_probe" 50 51typedef int (*bpf_prog_iter_callback_t)(const char *group, const char *event, 52 int fd, struct bpf_object *obj, void *arg); 53 54#ifdef HAVE_LIBBPF_SUPPORT 55struct bpf_object *bpf__prepare_load(const char *filename, bool source); 56int bpf__strerror_prepare_load(const char *filename, bool source, 57 int err, char *buf, size_t size); 58 59struct bpf_object *bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz, 60 const char *name); 61 62void bpf__clear(void); 63 64int bpf__probe(struct bpf_object *obj); 65int bpf__unprobe(struct bpf_object *obj); 66int bpf__strerror_probe(struct bpf_object *obj, int err, 67 char *buf, size_t size); 68 69int bpf__load(struct bpf_object *obj); 70int bpf__strerror_load(struct bpf_object *obj, int err, 71 char *buf, size_t size); 72int bpf__foreach_event(struct bpf_object *obj, 73 bpf_prog_iter_callback_t func, void *arg); 74 75int bpf__config_obj(struct bpf_object *obj, struct parse_events_term *term, 76 struct evlist *evlist, int *error_pos); 77int bpf__strerror_config_obj(struct bpf_object *obj, 78 struct parse_events_term *term, 79 struct evlist *evlist, 80 int *error_pos, int err, char *buf, 81 size_t size); 82int bpf__apply_obj_config(void); 83int bpf__strerror_apply_obj_config(int err, char *buf, size_t size); 84 85int bpf__setup_stdout(struct evlist *evlist); 86struct evsel *bpf__setup_output_event(struct evlist *evlist, const char *name); 87int bpf__strerror_setup_output_event(struct evlist *evlist, int err, char *buf, size_t size); 88#else 89#include <errno.h> 90#include <string.h> 91#include "debug.h" 92 93static inline struct bpf_object * 94bpf__prepare_load(const char *filename __maybe_unused, 95 bool source __maybe_unused) 96{ 97 pr_debug("ERROR: eBPF object loading is disabled during compiling.\n"); 98 return ERR_PTR(-ENOTSUP); 99} 100 101static inline struct bpf_object * 102bpf__prepare_load_buffer(void *obj_buf __maybe_unused, 103 size_t obj_buf_sz __maybe_unused) 104{ 105 return ERR_PTR(-ENOTSUP); 106} 107 108static inline void bpf__clear(void) { } 109 110static inline int bpf__probe(struct bpf_object *obj __maybe_unused) { return 0;} 111static inline int bpf__unprobe(struct bpf_object *obj __maybe_unused) { return 0;} 112static inline int bpf__load(struct bpf_object *obj __maybe_unused) { return 0; } 113 114static inline int 115bpf__foreach_event(struct bpf_object *obj __maybe_unused, 116 bpf_prog_iter_callback_t func __maybe_unused, 117 void *arg __maybe_unused) 118{ 119 return 0; 120} 121 122static inline int 123bpf__config_obj(struct bpf_object *obj __maybe_unused, 124 struct parse_events_term *term __maybe_unused, 125 struct evlist *evlist __maybe_unused, 126 int *error_pos __maybe_unused) 127{ 128 return 0; 129} 130 131static inline int 132bpf__apply_obj_config(void) 133{ 134 return 0; 135} 136 137static inline int 138bpf__setup_stdout(struct evlist *evlist __maybe_unused) 139{ 140 return 0; 141} 142 143static inline struct evsel * 144bpf__setup_output_event(struct evlist *evlist __maybe_unused, const char *name __maybe_unused) 145{ 146 return NULL; 147} 148 149static inline int 150__bpf_strerror(char *buf, size_t size) 151{ 152 if (!size) 153 return 0; 154 strncpy(buf, 155 "ERROR: eBPF object loading is disabled during compiling.\n", 156 size); 157 buf[size - 1] = '\0'; 158 return 0; 159} 160 161static inline 162int bpf__strerror_prepare_load(const char *filename __maybe_unused, 163 bool source __maybe_unused, 164 int err __maybe_unused, 165 char *buf, size_t size) 166{ 167 return __bpf_strerror(buf, size); 168} 169 170static inline int 171bpf__strerror_probe(struct bpf_object *obj __maybe_unused, 172 int err __maybe_unused, 173 char *buf, size_t size) 174{ 175 return __bpf_strerror(buf, size); 176} 177 178static inline int bpf__strerror_load(struct bpf_object *obj __maybe_unused, 179 int err __maybe_unused, 180 char *buf, size_t size) 181{ 182 return __bpf_strerror(buf, size); 183} 184 185static inline int 186bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused, 187 struct parse_events_term *term __maybe_unused, 188 struct evlist *evlist __maybe_unused, 189 int *error_pos __maybe_unused, 190 int err __maybe_unused, 191 char *buf, size_t size) 192{ 193 return __bpf_strerror(buf, size); 194} 195 196static inline int 197bpf__strerror_apply_obj_config(int err __maybe_unused, 198 char *buf, size_t size) 199{ 200 return __bpf_strerror(buf, size); 201} 202 203static inline int 204bpf__strerror_setup_output_event(struct evlist *evlist __maybe_unused, 205 int err __maybe_unused, char *buf, size_t size) 206{ 207 return __bpf_strerror(buf, size); 208} 209 210#endif 211 212static inline int bpf__strerror_setup_stdout(struct evlist *evlist, int err, char *buf, size_t size) 213{ 214 return bpf__strerror_setup_output_event(evlist, err, buf, size); 215} 216#endif