blob: d19f5c5d6d74ea2ee0d298e3186c20162792a937 [file] [log] [blame]
Wang Nan69d262a2015-10-14 12:41:13 +00001/*
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 Nand3e0ce32015-11-06 13:58:09 +000011#include <bpf/libbpf.h>
Wang Nan4edf30e2015-10-14 12:41:17 +000012#include "probe-event.h"
Wang Nan69d262a2015-10-14 12:41:13 +000013#include "debug.h"
14
Wang Nand3e0ce32015-11-06 13:58:09 +000015enum 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 Nan361f2b12015-11-16 12:10:05 +000023 BPF_LOADER_ERRNO__CONFIG_TERM, /* Invalid config term in config term */
Wang Nand3e0ce32015-11-06 13:58:09 +000024 __BPF_LOADER_ERRNO__END,
25};
26
Wang Nan69d262a2015-10-14 12:41:13 +000027struct bpf_object;
Wang Nanaa3abf32015-10-14 12:41:15 +000028#define PERF_BPF_PROBE_GROUP "perf_bpf_probe"
Wang Nan69d262a2015-10-14 12:41:13 +000029
Wang Nan4edf30e2015-10-14 12:41:17 +000030typedef int (*bpf_prog_iter_callback_t)(struct probe_trace_event *tev,
31 int fd, void *arg);
32
Wang Nan69d262a2015-10-14 12:41:13 +000033#ifdef HAVE_LIBBPF_SUPPORT
Wang Nand509db02015-10-14 12:41:20 +000034struct bpf_object *bpf__prepare_load(const char *filename, bool source);
Wang Nand3e0ce32015-11-06 13:58:09 +000035int bpf__strerror_prepare_load(const char *filename, bool source,
36 int err, char *buf, size_t size);
Wang Nan69d262a2015-10-14 12:41:13 +000037
Wang Nanba1fae42015-11-06 13:49:43 +000038struct bpf_object *bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz,
39 const char *name);
40
Wang Nan69d262a2015-10-14 12:41:13 +000041void bpf__clear(void);
Wang Nanaa3abf32015-10-14 12:41:15 +000042
43int bpf__probe(struct bpf_object *obj);
44int bpf__unprobe(struct bpf_object *obj);
45int bpf__strerror_probe(struct bpf_object *obj, int err,
46 char *buf, size_t size);
47
Wang Nan1e5e3ee2015-10-14 12:41:16 +000048int bpf__load(struct bpf_object *obj);
49int bpf__strerror_load(struct bpf_object *obj, int err,
50 char *buf, size_t size);
Wang Nan4edf30e2015-10-14 12:41:17 +000051int bpf__foreach_tev(struct bpf_object *obj,
52 bpf_prog_iter_callback_t func, void *arg);
Wang Nan69d262a2015-10-14 12:41:13 +000053#else
54static inline struct bpf_object *
Wang Nand509db02015-10-14 12:41:20 +000055bpf__prepare_load(const char *filename __maybe_unused,
56 bool source __maybe_unused)
Wang Nan69d262a2015-10-14 12:41:13 +000057{
58 pr_debug("ERROR: eBPF object loading is disabled during compiling.\n");
59 return ERR_PTR(-ENOTSUP);
60}
61
Wang Nanba1fae42015-11-06 13:49:43 +000062static inline struct bpf_object *
63bpf__prepare_load_buffer(void *obj_buf __maybe_unused,
64 size_t obj_buf_sz __maybe_unused)
65{
66 return ERR_PTR(-ENOTSUP);
67}
68
Wang Nan69d262a2015-10-14 12:41:13 +000069static inline void bpf__clear(void) { }
Wang Nanaa3abf32015-10-14 12:41:15 +000070
71static inline int bpf__probe(struct bpf_object *obj __maybe_unused) { return 0;}
72static inline int bpf__unprobe(struct bpf_object *obj __maybe_unused) { return 0;}
Wang Nan1e5e3ee2015-10-14 12:41:16 +000073static inline int bpf__load(struct bpf_object *obj __maybe_unused) { return 0; }
Wang Nanaa3abf32015-10-14 12:41:15 +000074
75static inline int
Wang Nan4edf30e2015-10-14 12:41:17 +000076bpf__foreach_tev(struct bpf_object *obj __maybe_unused,
77 bpf_prog_iter_callback_t func __maybe_unused,
78 void *arg __maybe_unused)
79{
80 return 0;
81}
82
83static inline int
Wang Nanaa3abf32015-10-14 12:41:15 +000084__bpf_strerror(char *buf, size_t size)
85{
86 if (!size)
87 return 0;
88 strncpy(buf,
89 "ERROR: eBPF object loading is disabled during compiling.\n",
90 size);
91 buf[size - 1] = '\0';
92 return 0;
93}
94
Wang Nand3e0ce32015-11-06 13:58:09 +000095static inline
96int bpf__strerror_prepare_load(const char *filename __maybe_unused,
97 bool source __maybe_unused,
98 int err __maybe_unused,
99 char *buf, size_t size)
100{
101 return __bpf_strerror(buf, size);
102}
103
Wang Nanaa3abf32015-10-14 12:41:15 +0000104static inline int
105bpf__strerror_probe(struct bpf_object *obj __maybe_unused,
106 int err __maybe_unused,
107 char *buf, size_t size)
108{
109 return __bpf_strerror(buf, size);
110}
Wang Nan1e5e3ee2015-10-14 12:41:16 +0000111
112static inline int bpf__strerror_load(struct bpf_object *obj __maybe_unused,
113 int err __maybe_unused,
114 char *buf, size_t size)
115{
116 return __bpf_strerror(buf, size);
117}
Wang Nan69d262a2015-10-14 12:41:13 +0000118#endif
119#endif