Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lcore_monitor: use libbpf skeletons for the obj load #727

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tools/ebpf/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# generated file
et
fentry.skel.h
*.skel.h
vmlinux.h
*.o
lcore_monitor
8 changes: 6 additions & 2 deletions tools/ebpf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# Copyright 2023 Intel Corporation

.PHONY: all
all: et xsk.xdp.o lcore_monitor_kern.o lcore_monitor
all: et xsk.xdp.o lcore_monitor_kern.o lcore_monitor_kern.skel.h lcore_monitor

.PHONY: clean
clean:
rm -rf et *.o *.skel.h lcore_monitor
rm -rf et *.o *.skel.h lcore_monitor vmlinux.h

vmlinux.h:
bpftool btf dump file /sys/kernel/btf/vmlinux format c > $@
Expand Down Expand Up @@ -36,6 +36,10 @@ lcore_monitor_kern.o: lcore_monitor_kern.c lcore_monitor.h
clang -g -O2 -target bpf -c lcore_monitor_kern.c -o $@
llvm-strip -g $@

# Generate lcore_monitor_kern skeletons
lcore_monitor_kern.skel.h: lcore_monitor_kern.o
bpftool gen skeleton $< > $@

# Build lcore_monitor user prog
lcore_monitor: lcore_monitor.c lcore_monitor.h
gcc -Wall -o lcore_monitor lcore_monitor.c -lbpf -lelf
92 changes: 25 additions & 67 deletions tools/ebpf/lcore_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <stdlib.h>
#include <unistd.h>

#include "lcore_monitor_kern.skel.h"
#include "log.h"

struct lcore_monitor_ctx {
Expand Down Expand Up @@ -184,78 +185,34 @@ int main(int argc, char** argv) {
return -1;
}

struct bpf_object* obj;
struct bpf_program* prog;
struct bpf_link* link;
struct lcore_monitor_kern* skel = NULL;
struct ring_buffer* rb = NULL;

obj = bpf_object__open(ctx.bpf_prog);
if (libbpf_get_error(obj)) {
err("%s, open bpf object %s fail\n", __func__, ctx.bpf_prog);
return -1;
}
if (bpf_object__load(obj)) {
err("%s, load bpf object %s fail\n", __func__, ctx.bpf_prog);
return -1;
skel = lcore_monitor_kern__open_and_load();
if (!skel) {
err("%s, Failed to open and load skeleton\n", __func__);
goto exit;
}
info("%s, load bpf object %s succ\n", __func__, ctx.bpf_prog);

int map_fd = bpf_map__fd(skel->maps.lm_cfg_map);
uint32_t key = 0;
int map_fd = bpf_object__find_map_fd_by_name(obj, "lm_cfg_map");
if (map_fd < 0) {
err("%s, get lm_cfg_map fail\n", __func__);
return -1;
}
if (bpf_map_update_elem(map_fd, &key, &ctx.cfg, BPF_ANY) != 0) {
err("%s, update core_id_map fail\n", __func__);
return -1;
ret = bpf_map_update_elem(map_fd, &key, &ctx.cfg, BPF_ANY);
if (ret < 0) {
err("%s, update lm_cfg_map fail\n", __func__);
goto exit;
}

/* attach bpf_prog_sched_switch */
prog = bpf_object__find_program_by_name(obj, "bpf_prog_sched_switch");
if (!prog) {
err("%s, finding bpf_prog_sched_switch failed\n", __func__);
return -1;
}
link = bpf_program__attach_tracepoint(prog, "sched", "sched_switch");
if (libbpf_get_error(link)) {
err("%s, attaching bpf_prog_sched_switch to tracepoint failed\n", __func__);
return -1;
}

/* attach bpf_prog_irq_handler_entry */
prog = bpf_object__find_program_by_name(obj, "bpf_prog_irq_handler_entry");
if (!prog) {
err("%s, finding bpf_prog_irq_handler_entry failed\n", __func__);
return -1;
}
link = bpf_program__attach_tracepoint(prog, "irq", "irq_handler_entry");
if (libbpf_get_error(link)) {
err("%s, attaching bpf_prog_irq_handler_entry to tracepoint failed\n", __func__);
return -1;
}

/* attach bpf_prog_irq_handler_exit */
prog = bpf_object__find_program_by_name(obj, "bpf_prog_irq_handler_exit");
if (!prog) {
err("%s, finding bpf_prog_irq_handler_exit failed\n", __func__);
return -1;
}
link = bpf_program__attach_tracepoint(prog, "irq", "irq_handler_exit");
if (libbpf_get_error(link)) {
err("%s, attaching bpf_prog_irq_handler_exit to tracepoint failed\n", __func__);
return -1;
}

int lm_events_fd = bpf_object__find_map_fd_by_name(obj, "lm_events_map");
if (lm_events_fd < 0) {
err("%s, get lm_events_map fail\n", __func__);
return -1;
}
struct ring_buffer* rb = ring_buffer__new(lm_events_fd, lm_event_handler, &ctx, NULL);
int lm_events_fd = bpf_map__fd(skel->maps.lm_events_map);
rb = ring_buffer__new(lm_events_fd, lm_event_handler, &ctx, NULL);
if (!rb) {
err("%s, create ring buffer fail\n", __func__);
return -1;
goto exit;
}
ret = lcore_monitor_kern__attach(skel);
if (ret) {
err("%s, Failed to attach skeleton\n", __func__);
lcore_monitor_kern__destroy(skel);
goto exit;
}
info("%s, attach skeleton succ\n", __func__);

signal(SIGINT, lm_sig_handler);

Expand All @@ -272,7 +229,8 @@ int main(int argc, char** argv) {
}

info("%s, stop now\n", __func__);
bpf_link__destroy(link);
bpf_object__close(obj);
exit:
if (rb) ring_buffer__free(rb);
if (skel) lcore_monitor_kern__destroy(skel);
return 0;
}
Loading