Skip to content

Commit aa5e2af

Browse files
DanielTimLeeborkmann
authored andcommitted
samples, bpf: Refactor perf_event user program with libbpf bpf_link
The bpf_program__attach of libbpf(using bpf_link) is much more intuitive than the previous method using ioctl. bpf_program__attach_perf_event manages the enable of perf_event and attach of BPF programs to it, so there's no neeed to do this directly with ioctl. In addition, bpf_link provides consistency in the use of API because it allows disable (detach, destroy) for multiple events to be treated as one bpf_link__destroy. Also, bpf_link__destroy manages the close() of perf_event fd. This commit refactors samples that attach the bpf program to perf_event by using libbbpf instead of ioctl. Also the bpf_load in the samples were removed and migrated to use libbbpf API. Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Andrii Nakryiko <andriin@fb.com> Link: https://lore.kernel.org/bpf/20200321100424.1593964-3-danieltimlee@gmail.com
1 parent 24a6034 commit aa5e2af

3 files changed

Lines changed: 159 additions & 82 deletions

File tree

‎samples/bpf/Makefile‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ xdp2-objs := xdp1_user.o
8888
xdp_router_ipv4-objs := xdp_router_ipv4_user.o
8989
test_current_task_under_cgroup-objs := bpf_load.o $(CGROUP_HELPERS) \
9090
test_current_task_under_cgroup_user.o
91-
trace_event-objs := bpf_load.o trace_event_user.o $(TRACE_HELPERS)
92-
sampleip-objs := bpf_load.o sampleip_user.o $(TRACE_HELPERS)
91+
trace_event-objs := trace_event_user.o $(TRACE_HELPERS)
92+
sampleip-objs := sampleip_user.o $(TRACE_HELPERS)
9393
tc_l2_redirect-objs := bpf_load.o tc_l2_redirect_user.o
9494
lwt_len_hist-objs := bpf_load.o lwt_len_hist_user.o
9595
xdp_tx_iptunnel-objs := xdp_tx_iptunnel_user.o

‎samples/bpf/sampleip_user.c‎

Lines changed: 64 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,23 @@
1010
#include <errno.h>
1111
#include <signal.h>
1212
#include <string.h>
13-
#include <assert.h>
1413
#include <linux/perf_event.h>
1514
#include <linux/ptrace.h>
1615
#include <linux/bpf.h>
17-
#include <sys/ioctl.h>
16+
#include <bpf/bpf.h>
1817
#include <bpf/libbpf.h>
19-
#include "bpf_load.h"
2018
#include "perf-sys.h"
2119
#include "trace_helpers.h"
2220

21+
#define __must_check
22+
#include <linux/err.h>
23+
2324
#define DEFAULT_FREQ 99
2425
#define DEFAULT_SECS 5
2526
#define MAX_IPS 8192
2627
#define PAGE_OFFSET 0xffff880000000000
2728

29+
static int map_fd;
2830
static int nr_cpus;
2931

3032
static void usage(void)
@@ -34,9 +36,10 @@ static void usage(void)
3436
printf(" duration # sampling duration (seconds), default 5\n");
3537
}
3638

37-
static int sampling_start(int *pmu_fd, int freq)
39+
static int sampling_start(int freq, struct bpf_program *prog,
40+
struct bpf_link *links[])
3841
{
39-
int i;
42+
int i, pmu_fd;
4043

4144
struct perf_event_attr pe_sample_attr = {
4245
.type = PERF_TYPE_SOFTWARE,
@@ -47,26 +50,30 @@ static int sampling_start(int *pmu_fd, int freq)
4750
};
4851

4952
for (i = 0; i < nr_cpus; i++) {
50-
pmu_fd[i] = sys_perf_event_open(&pe_sample_attr, -1 /* pid */, i,
53+
pmu_fd = sys_perf_event_open(&pe_sample_attr, -1 /* pid */, i,
5154
-1 /* group_fd */, 0 /* flags */);
52-
if (pmu_fd[i] < 0) {
55+
if (pmu_fd < 0) {
5356
fprintf(stderr, "ERROR: Initializing perf sampling\n");
5457
return 1;
5558
}
56-
assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_SET_BPF,
57-
prog_fd[0]) == 0);
58-
assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_ENABLE, 0) == 0);
59+
links[i] = bpf_program__attach_perf_event(prog, pmu_fd);
60+
if (IS_ERR(links[i])) {
61+
fprintf(stderr, "ERROR: Attach perf event\n");
62+
links[i] = NULL;
63+
close(pmu_fd);
64+
return 1;
65+
}
5966
}
6067

6168
return 0;
6269
}
6370

64-
static void sampling_end(int *pmu_fd)
71+
static void sampling_end(struct bpf_link *links[])
6572
{
6673
int i;
6774

6875
for (i = 0; i < nr_cpus; i++)
69-
close(pmu_fd[i]);
76+
bpf_link__destroy(links[i]);
7077
}
7178

7279
struct ipcount {
@@ -128,14 +135,17 @@ static void print_ip_map(int fd)
128135
static void int_exit(int sig)
129136
{
130137
printf("\n");
131-
print_ip_map(map_fd[0]);
138+
print_ip_map(map_fd);
132139
exit(0);
133140
}
134141

135142
int main(int argc, char **argv)
136143
{
144+
int opt, freq = DEFAULT_FREQ, secs = DEFAULT_SECS, error = 1;
145+
struct bpf_object *obj = NULL;
146+
struct bpf_program *prog;
147+
struct bpf_link **links;
137148
char filename[256];
138-
int *pmu_fd, opt, freq = DEFAULT_FREQ, secs = DEFAULT_SECS;
139149

140150
/* process arguments */
141151
while ((opt = getopt(argc, argv, "F:h")) != -1) {
@@ -163,38 +173,58 @@ int main(int argc, char **argv)
163173
}
164174

165175
/* create perf FDs for each CPU */
166-
nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
167-
pmu_fd = malloc(nr_cpus * sizeof(int));
168-
if (pmu_fd == NULL) {
169-
fprintf(stderr, "ERROR: malloc of pmu_fd\n");
170-
return 1;
176+
nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
177+
links = calloc(nr_cpus, sizeof(struct bpf_link *));
178+
if (!links) {
179+
fprintf(stderr, "ERROR: malloc of links\n");
180+
goto cleanup;
171181
}
172182

173-
/* load BPF program */
174183
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
175-
if (load_bpf_file(filename)) {
176-
fprintf(stderr, "ERROR: loading BPF program (errno %d):\n",
177-
errno);
178-
if (strcmp(bpf_log_buf, "") == 0)
179-
fprintf(stderr, "Try: ulimit -l unlimited\n");
180-
else
181-
fprintf(stderr, "%s", bpf_log_buf);
182-
return 1;
184+
obj = bpf_object__open_file(filename, NULL);
185+
if (IS_ERR(obj)) {
186+
fprintf(stderr, "ERROR: opening BPF object file failed\n");
187+
obj = NULL;
188+
goto cleanup;
189+
}
190+
191+
prog = bpf_object__find_program_by_name(obj, "do_sample");
192+
if (!prog) {
193+
fprintf(stderr, "ERROR: finding a prog in obj file failed\n");
194+
goto cleanup;
183195
}
196+
197+
/* load BPF program */
198+
if (bpf_object__load(obj)) {
199+
fprintf(stderr, "ERROR: loading BPF object file failed\n");
200+
goto cleanup;
201+
}
202+
203+
map_fd = bpf_object__find_map_fd_by_name(obj, "ip_map");
204+
if (map_fd < 0) {
205+
fprintf(stderr, "ERROR: finding a map in obj file failed\n");
206+
goto cleanup;
207+
}
208+
184209
signal(SIGINT, int_exit);
185210
signal(SIGTERM, int_exit);
186211

187212
/* do sampling */
188213
printf("Sampling at %d Hertz for %d seconds. Ctrl-C also ends.\n",
189214
freq, secs);
190-
if (sampling_start(pmu_fd, freq) != 0)
191-
return 1;
215+
if (sampling_start(freq, prog, links) != 0)
216+
goto cleanup;
217+
192218
sleep(secs);
193-
sampling_end(pmu_fd);
194-
free(pmu_fd);
219+
error = 0;
195220

221+
cleanup:
222+
sampling_end(links);
196223
/* output sample counts */
197-
print_ip_map(map_fd[0]);
224+
if (!error)
225+
print_ip_map(map_fd);
198226

199-
return 0;
227+
free(links);
228+
bpf_object__close(obj);
229+
return error;
200230
}

0 commit comments

Comments
 (0)