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