Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Copyright (C) 2015, Wang Nan <wangnan0@huawei.com>
0004  * Copyright (C) 2015, Huawei Inc.
0005  */
0006 #ifndef __BPF_LOADER_H
0007 #define __BPF_LOADER_H
0008 
0009 #include <linux/compiler.h>
0010 #include <linux/err.h>
0011 
0012 #ifdef HAVE_LIBBPF_SUPPORT
0013 #include <bpf/libbpf.h>
0014 
0015 enum bpf_loader_errno {
0016     __BPF_LOADER_ERRNO__START = __LIBBPF_ERRNO__START - 100,
0017     /* Invalid config string */
0018     BPF_LOADER_ERRNO__CONFIG = __BPF_LOADER_ERRNO__START,
0019     BPF_LOADER_ERRNO__GROUP,    /* Invalid group name */
0020     BPF_LOADER_ERRNO__EVENTNAME,    /* Event name is missing */
0021     BPF_LOADER_ERRNO__INTERNAL, /* BPF loader internal error */
0022     BPF_LOADER_ERRNO__COMPILE,  /* Error when compiling BPF scriptlet */
0023     BPF_LOADER_ERRNO__PROGCONF_TERM,/* Invalid program config term in config string */
0024     BPF_LOADER_ERRNO__PROLOGUE, /* Failed to generate prologue */
0025     BPF_LOADER_ERRNO__PROLOGUE2BIG, /* Prologue too big for program */
0026     BPF_LOADER_ERRNO__PROLOGUEOOB,  /* Offset out of bound for prologue */
0027     BPF_LOADER_ERRNO__OBJCONF_OPT,  /* Invalid object config option */
0028     BPF_LOADER_ERRNO__OBJCONF_CONF, /* Config value not set (lost '=')) */
0029     BPF_LOADER_ERRNO__OBJCONF_MAP_OPT,  /* Invalid object map config option */
0030     BPF_LOADER_ERRNO__OBJCONF_MAP_NOTEXIST, /* Target map not exist */
0031     BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE,    /* Incorrect value type for map */
0032     BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE, /* Incorrect map type */
0033     BPF_LOADER_ERRNO__OBJCONF_MAP_KEYSIZE,  /* Incorrect map key size */
0034     BPF_LOADER_ERRNO__OBJCONF_MAP_VALUESIZE,/* Incorrect map value size */
0035     BPF_LOADER_ERRNO__OBJCONF_MAP_NOEVT,    /* Event not found for map setting */
0036     BPF_LOADER_ERRNO__OBJCONF_MAP_MAPSIZE,  /* Invalid map size for event setting */
0037     BPF_LOADER_ERRNO__OBJCONF_MAP_EVTDIM,   /* Event dimension too large */
0038     BPF_LOADER_ERRNO__OBJCONF_MAP_EVTINH,   /* Doesn't support inherit event */
0039     BPF_LOADER_ERRNO__OBJCONF_MAP_EVTTYPE,  /* Wrong event type for map */
0040     BPF_LOADER_ERRNO__OBJCONF_MAP_IDX2BIG,  /* Index too large */
0041     __BPF_LOADER_ERRNO__END,
0042 };
0043 #endif // HAVE_LIBBPF_SUPPORT
0044 
0045 struct evsel;
0046 struct evlist;
0047 struct bpf_object;
0048 struct parse_events_term;
0049 #define PERF_BPF_PROBE_GROUP "perf_bpf_probe"
0050 
0051 typedef int (*bpf_prog_iter_callback_t)(const char *group, const char *event,
0052                     int fd, struct bpf_object *obj, void *arg);
0053 
0054 #ifdef HAVE_LIBBPF_SUPPORT
0055 struct bpf_object *bpf__prepare_load(const char *filename, bool source);
0056 int bpf__strerror_prepare_load(const char *filename, bool source,
0057                    int err, char *buf, size_t size);
0058 
0059 struct bpf_object *bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz,
0060                         const char *name);
0061 
0062 void bpf__clear(void);
0063 
0064 int bpf__probe(struct bpf_object *obj);
0065 int bpf__unprobe(struct bpf_object *obj);
0066 int bpf__strerror_probe(struct bpf_object *obj, int err,
0067             char *buf, size_t size);
0068 
0069 int bpf__load(struct bpf_object *obj);
0070 int bpf__strerror_load(struct bpf_object *obj, int err,
0071                char *buf, size_t size);
0072 int bpf__foreach_event(struct bpf_object *obj,
0073                bpf_prog_iter_callback_t func, void *arg);
0074 
0075 int bpf__config_obj(struct bpf_object *obj, struct parse_events_term *term,
0076             struct evlist *evlist, int *error_pos);
0077 int bpf__strerror_config_obj(struct bpf_object *obj,
0078                  struct parse_events_term *term,
0079                  struct evlist *evlist,
0080                  int *error_pos, int err, char *buf,
0081                  size_t size);
0082 int bpf__apply_obj_config(void);
0083 int bpf__strerror_apply_obj_config(int err, char *buf, size_t size);
0084 
0085 int bpf__setup_stdout(struct evlist *evlist);
0086 struct evsel *bpf__setup_output_event(struct evlist *evlist, const char *name);
0087 int bpf__strerror_setup_output_event(struct evlist *evlist, int err, char *buf, size_t size);
0088 #else
0089 #include <errno.h>
0090 #include <string.h>
0091 #include "debug.h"
0092 
0093 static inline struct bpf_object *
0094 bpf__prepare_load(const char *filename __maybe_unused,
0095           bool source __maybe_unused)
0096 {
0097     pr_debug("ERROR: eBPF object loading is disabled during compiling.\n");
0098     return ERR_PTR(-ENOTSUP);
0099 }
0100 
0101 static inline struct bpf_object *
0102 bpf__prepare_load_buffer(void *obj_buf __maybe_unused,
0103                        size_t obj_buf_sz __maybe_unused)
0104 {
0105     return ERR_PTR(-ENOTSUP);
0106 }
0107 
0108 static inline void bpf__clear(void) { }
0109 
0110 static inline int bpf__probe(struct bpf_object *obj __maybe_unused) { return 0;}
0111 static inline int bpf__unprobe(struct bpf_object *obj __maybe_unused) { return 0;}
0112 static inline int bpf__load(struct bpf_object *obj __maybe_unused) { return 0; }
0113 
0114 static inline int
0115 bpf__foreach_event(struct bpf_object *obj __maybe_unused,
0116            bpf_prog_iter_callback_t func __maybe_unused,
0117            void *arg __maybe_unused)
0118 {
0119     return 0;
0120 }
0121 
0122 static inline int
0123 bpf__config_obj(struct bpf_object *obj __maybe_unused,
0124         struct parse_events_term *term __maybe_unused,
0125         struct evlist *evlist __maybe_unused,
0126         int *error_pos __maybe_unused)
0127 {
0128     return 0;
0129 }
0130 
0131 static inline int
0132 bpf__apply_obj_config(void)
0133 {
0134     return 0;
0135 }
0136 
0137 static inline int
0138 bpf__setup_stdout(struct evlist *evlist __maybe_unused)
0139 {
0140     return 0;
0141 }
0142 
0143 static inline struct evsel *
0144 bpf__setup_output_event(struct evlist *evlist __maybe_unused, const char *name __maybe_unused)
0145 {
0146     return NULL;
0147 }
0148 
0149 static inline int
0150 __bpf_strerror(char *buf, size_t size)
0151 {
0152     if (!size)
0153         return 0;
0154     strncpy(buf,
0155         "ERROR: eBPF object loading is disabled during compiling.\n",
0156         size);
0157     buf[size - 1] = '\0';
0158     return 0;
0159 }
0160 
0161 static inline
0162 int bpf__strerror_prepare_load(const char *filename __maybe_unused,
0163                    bool source __maybe_unused,
0164                    int err __maybe_unused,
0165                    char *buf, size_t size)
0166 {
0167     return __bpf_strerror(buf, size);
0168 }
0169 
0170 static inline int
0171 bpf__strerror_probe(struct bpf_object *obj __maybe_unused,
0172             int err __maybe_unused,
0173             char *buf, size_t size)
0174 {
0175     return __bpf_strerror(buf, size);
0176 }
0177 
0178 static inline int bpf__strerror_load(struct bpf_object *obj __maybe_unused,
0179                      int err __maybe_unused,
0180                      char *buf, size_t size)
0181 {
0182     return __bpf_strerror(buf, size);
0183 }
0184 
0185 static inline int
0186 bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused,
0187              struct parse_events_term *term __maybe_unused,
0188              struct evlist *evlist __maybe_unused,
0189              int *error_pos __maybe_unused,
0190              int err __maybe_unused,
0191              char *buf, size_t size)
0192 {
0193     return __bpf_strerror(buf, size);
0194 }
0195 
0196 static inline int
0197 bpf__strerror_apply_obj_config(int err __maybe_unused,
0198                    char *buf, size_t size)
0199 {
0200     return __bpf_strerror(buf, size);
0201 }
0202 
0203 static inline int
0204 bpf__strerror_setup_output_event(struct evlist *evlist __maybe_unused,
0205                  int err __maybe_unused, char *buf, size_t size)
0206 {
0207     return __bpf_strerror(buf, size);
0208 }
0209 
0210 #endif
0211 
0212 static inline int bpf__strerror_setup_stdout(struct evlist *evlist, int err, char *buf, size_t size)
0213 {
0214     return bpf__strerror_setup_output_event(evlist, err, buf, size);
0215 }
0216 #endif