-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathtrace_load_and_stats.c
275 lines (227 loc) · 6.48 KB
/
trace_load_and_stats.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* SPDX-License-Identifier: GPL-2.0 */
static const char *__doc__ = "XDP loader and stats program\n"
" - Allows selecting BPF section --progname name to XDP-attach to --dev\n";
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <net/if.h>
#include <locale.h>
#include <unistd.h>
#include <time.h>
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
#include <net/if.h>
#include <linux/if_link.h> /* depend on kernel-headers installed */
#include "../common/common_params.h"
#include <linux/perf_event.h>
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <unistd.h>
#include <sys/syscall.h> /* For SYS_xxx definitions */
#include <sys/ioctl.h>
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
static const char *default_filename = "trace_prog_kern.o";
static const struct option_wrapper long_options[] = {
{{"help", no_argument, NULL, 'h' },
"Show help", false},
{{"quiet", no_argument, NULL, 'q' },
"Quiet mode (no output)"},
{{"filename", required_argument, NULL, 1 },
"Load program from <file>", "<file>"},
{{0, 0, NULL, 0 }}
};
int find_map_fd(struct bpf_object *bpf_obj, const char *mapname)
{
struct bpf_map *map;
int map_fd = -1;
/* Lesson#3: bpf_object to bpf_map */
map = bpf_object__find_map_by_name(bpf_obj, mapname);
if (!map) {
fprintf(stderr, "ERR: cannot find map by name: %s\n", mapname);
goto out;
}
map_fd = bpf_map__fd(map);
out:
return map_fd;
}
static void stats_print(int map_fd)
{
/* For percpu maps, userspace gets a value per possible CPU */
unsigned int nr_cpus = libbpf_num_possible_cpus();
__u64 values[nr_cpus];
__s32 key;
void *keyp = &key, *prev_keyp = NULL;
int err;
while (true) {
char dev[IF_NAMESIZE];
__u64 total = 0;
int i;
err = bpf_map_get_next_key(map_fd, prev_keyp, keyp);
if (err) {
if (errno == ENOENT)
err = 0;
break;
}
if ((bpf_map_lookup_elem(map_fd, keyp, values)) != 0) {
fprintf(stderr,
"ERR: bpf_map_lookup_elem failed key:0x%X\n", key);
continue;
}
/* Sum values from each CPU */
for (i = 0; i < nr_cpus; i++)
total += values[i];
printf("%s (%llu) ", if_indextoname(key, dev), total);
prev_keyp = keyp;
}
printf("\n");
}
static void stats_poll(int map_fd, __u32 map_type, int interval)
{
/* Trick to pretty printf with thousands separators use %' */
setlocale(LC_NUMERIC, "en_US");
while (1) {
stats_print(map_fd);
sleep(interval);
}
}
/* Lesson#4: It is userspace responsibility to known what map it is reading and
* know the value size. Here get bpf_map_info and check if it match our expected
* values.
*/
static int __check_map_fd_info(int map_fd, struct bpf_map_info *info,
struct bpf_map_info *exp)
{
__u32 info_len = sizeof(*info);
int err;
if (map_fd < 0)
return EXIT_FAIL;
/* BPF-info via bpf-syscall */
err = bpf_obj_get_info_by_fd(map_fd, info, &info_len);
if (err) {
fprintf(stderr, "ERR: %s() can't get info - %s\n",
__func__, strerror(errno));
return EXIT_FAIL_BPF;
}
if (exp->key_size && exp->key_size != info->key_size) {
fprintf(stderr, "ERR: %s() "
"Map key size(%d) mismatch expected size(%d)\n",
__func__, info->key_size, exp->key_size);
return EXIT_FAIL;
}
if (exp->value_size && exp->value_size != info->value_size) {
fprintf(stderr, "ERR: %s() "
"Map value size(%d) mismatch expected size(%d)\n",
__func__, info->value_size, exp->value_size);
return EXIT_FAIL;
}
if (exp->max_entries && exp->max_entries != info->max_entries) {
fprintf(stderr, "ERR: %s() "
"Map max_entries(%d) mismatch expected size(%d)\n",
__func__, info->max_entries, exp->max_entries);
return EXIT_FAIL;
}
if (exp->type && exp->type != info->type) {
fprintf(stderr, "ERR: %s() "
"Map type(%d) mismatch expected type(%d)\n",
__func__, info->type, exp->type);
return EXIT_FAIL;
}
return 0;
}
int filename__read_int(const char *filename, int *value)
{
char line[64];
int fd = open(filename, O_RDONLY), err = -1;
if (fd < 0)
return -1;
if (read(fd, line, sizeof(line)) > 0) {
*value = atoi(line);
err = 0;
}
close(fd);
return err;
}
static struct bpf_object* load_bpf_and_trace_attach(struct config *cfg)
{
struct bpf_object *obj;
int err;
struct bpf_program *prog;
struct bpf_link *link;
obj = bpf_object__open_file(cfg->filename, NULL);
err = libbpf_get_error(obj);
if (err)
return NULL;
err = bpf_object__load(obj);
if (err) {
fprintf(stderr, "ERR: loading BPF-OBJ file(%s) (%d): %s\n",
cfg->filename, err, strerror(-err));
goto err;
}
prog = bpf_object__next_program(obj, NULL);
if (!prog) {
fprintf(stderr, "ERR: Failed to retrieve program from BPF-OBJ file(%s) (%d): %s\n",
cfg->filename, err, strerror(-err));
goto err;
}
link = bpf_program__attach_tracepoint(prog, "xdp", "xdp_exception");
if (libbpf_get_error(link)) {
printf("bpf_program__attach_tracepoint failed\n");
goto err;
}
/*
* As far as this program is concerned, we don't care about
* the perf event file descriptor, it will get closed when
* the program is terminated. But normally you want to call
* close(fd) when you stop using it.
*/
return obj;
err:
bpf_object__close(obj);
return NULL;
}
int main(int argc, char **argv)
{
struct bpf_map_info map_expect = { 0 };
struct bpf_map_info info = { 0 };
struct bpf_object *bpf_obj;
struct config cfg;
int stats_map_fd;
int interval = 2;
int err;
/* Set default BPF-ELF object file and BPF program name */
strncpy(cfg.filename, default_filename, sizeof(cfg.filename));
/* Cmdline options can change progname */
parse_cmdline_args(argc, argv, long_options, &cfg, __doc__);
bpf_obj = load_bpf_and_trace_attach(&cfg);
if (!bpf_obj)
return EXIT_FAIL_BPF;
if (verbose) {
printf("Success: Loaded BPF-object(%s)\n", cfg.filename);
}
stats_map_fd = find_map_fd(bpf_obj, "xdp_stats_map");
if (stats_map_fd < 0)
return EXIT_FAIL_BPF;
map_expect.key_size = sizeof(__s32);
map_expect.value_size = sizeof(__u64);
err = __check_map_fd_info(stats_map_fd, &info, &map_expect);
if (err) {
fprintf(stderr, "ERR: map via FD not compatible\n");
return err;
}
if (verbose) {
printf("\nCollecting stats from BPF map\n");
printf(" - BPF map (bpf_map_type:%d) id:%d name:%s"
" key_size:%d value_size:%d max_entries:%d\n",
info.type, info.id, info.name,
info.key_size, info.value_size, info.max_entries
);
}
stats_poll(stats_map_fd, info.type, interval);
return EXIT_OK;
}