Skip to content

Commit

Permalink
samples, bpf: Refactor perf_event user program with libbpf bpf_link
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Daniel Borkmann <[email protected]>
Acked-by: Andrii Nakryiko <[email protected]>
Link: https://lore.kernel.org/bpf/[email protected]
  • Loading branch information
DanielTimLee authored and borkmann committed Mar 23, 2020
1 parent 24a6034 commit aa5e2af
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 82 deletions.
4 changes: 2 additions & 2 deletions samples/bpf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ xdp2-objs := xdp1_user.o
xdp_router_ipv4-objs := xdp_router_ipv4_user.o
test_current_task_under_cgroup-objs := bpf_load.o $(CGROUP_HELPERS) \
test_current_task_under_cgroup_user.o
trace_event-objs := bpf_load.o trace_event_user.o $(TRACE_HELPERS)
sampleip-objs := bpf_load.o sampleip_user.o $(TRACE_HELPERS)
trace_event-objs := trace_event_user.o $(TRACE_HELPERS)
sampleip-objs := sampleip_user.o $(TRACE_HELPERS)
tc_l2_redirect-objs := bpf_load.o tc_l2_redirect_user.o
lwt_len_hist-objs := bpf_load.o lwt_len_hist_user.o
xdp_tx_iptunnel-objs := xdp_tx_iptunnel_user.o
Expand Down
98 changes: 64 additions & 34 deletions samples/bpf/sampleip_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <assert.h>
#include <linux/perf_event.h>
#include <linux/ptrace.h>
#include <linux/bpf.h>
#include <sys/ioctl.h>
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
#include "bpf_load.h"
#include "perf-sys.h"
#include "trace_helpers.h"

#define __must_check
#include <linux/err.h>

#define DEFAULT_FREQ 99
#define DEFAULT_SECS 5
#define MAX_IPS 8192
#define PAGE_OFFSET 0xffff880000000000

static int map_fd;
static int nr_cpus;

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

static int sampling_start(int *pmu_fd, int freq)
static int sampling_start(int freq, struct bpf_program *prog,
struct bpf_link *links[])
{
int i;
int i, pmu_fd;

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

for (i = 0; i < nr_cpus; i++) {
pmu_fd[i] = sys_perf_event_open(&pe_sample_attr, -1 /* pid */, i,
pmu_fd = sys_perf_event_open(&pe_sample_attr, -1 /* pid */, i,
-1 /* group_fd */, 0 /* flags */);
if (pmu_fd[i] < 0) {
if (pmu_fd < 0) {
fprintf(stderr, "ERROR: Initializing perf sampling\n");
return 1;
}
assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_SET_BPF,
prog_fd[0]) == 0);
assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_ENABLE, 0) == 0);
links[i] = bpf_program__attach_perf_event(prog, pmu_fd);
if (IS_ERR(links[i])) {
fprintf(stderr, "ERROR: Attach perf event\n");
links[i] = NULL;
close(pmu_fd);
return 1;
}
}

return 0;
}

static void sampling_end(int *pmu_fd)
static void sampling_end(struct bpf_link *links[])
{
int i;

for (i = 0; i < nr_cpus; i++)
close(pmu_fd[i]);
bpf_link__destroy(links[i]);
}

struct ipcount {
Expand Down Expand Up @@ -128,14 +135,17 @@ static void print_ip_map(int fd)
static void int_exit(int sig)
{
printf("\n");
print_ip_map(map_fd[0]);
print_ip_map(map_fd);
exit(0);
}

int main(int argc, char **argv)
{
int opt, freq = DEFAULT_FREQ, secs = DEFAULT_SECS, error = 1;
struct bpf_object *obj = NULL;
struct bpf_program *prog;
struct bpf_link **links;
char filename[256];
int *pmu_fd, opt, freq = DEFAULT_FREQ, secs = DEFAULT_SECS;

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

/* create perf FDs for each CPU */
nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
pmu_fd = malloc(nr_cpus * sizeof(int));
if (pmu_fd == NULL) {
fprintf(stderr, "ERROR: malloc of pmu_fd\n");
return 1;
nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
links = calloc(nr_cpus, sizeof(struct bpf_link *));
if (!links) {
fprintf(stderr, "ERROR: malloc of links\n");
goto cleanup;
}

/* load BPF program */
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
if (load_bpf_file(filename)) {
fprintf(stderr, "ERROR: loading BPF program (errno %d):\n",
errno);
if (strcmp(bpf_log_buf, "") == 0)
fprintf(stderr, "Try: ulimit -l unlimited\n");
else
fprintf(stderr, "%s", bpf_log_buf);
return 1;
obj = bpf_object__open_file(filename, NULL);
if (IS_ERR(obj)) {
fprintf(stderr, "ERROR: opening BPF object file failed\n");
obj = NULL;
goto cleanup;
}

prog = bpf_object__find_program_by_name(obj, "do_sample");
if (!prog) {
fprintf(stderr, "ERROR: finding a prog in obj file failed\n");
goto cleanup;
}

/* load BPF program */
if (bpf_object__load(obj)) {
fprintf(stderr, "ERROR: loading BPF object file failed\n");
goto cleanup;
}

map_fd = bpf_object__find_map_fd_by_name(obj, "ip_map");
if (map_fd < 0) {
fprintf(stderr, "ERROR: finding a map in obj file failed\n");
goto cleanup;
}

signal(SIGINT, int_exit);
signal(SIGTERM, int_exit);

/* do sampling */
printf("Sampling at %d Hertz for %d seconds. Ctrl-C also ends.\n",
freq, secs);
if (sampling_start(pmu_fd, freq) != 0)
return 1;
if (sampling_start(freq, prog, links) != 0)
goto cleanup;

sleep(secs);
sampling_end(pmu_fd);
free(pmu_fd);
error = 0;

cleanup:
sampling_end(links);
/* output sample counts */
print_ip_map(map_fd[0]);
if (!error)
print_ip_map(map_fd);

return 0;
free(links);
bpf_object__close(obj);
return error;
}
Loading

0 comments on commit aa5e2af

Please sign in to comment.