mirror of
https://github.com/xdp-project/bpf-examples.git
synced 2024-05-06 15:54:53 +00:00
pkt-loop-filter: Switch to using BPF skeletons
This makes it easier to populate the global variables we'll need for handling multicast, and also means we don't have to worry about keeping the BPF object file around (since it'll be statically linked). Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
USER_TARGETS += pkt-loop-filter
|
USER_TARGETS += pkt-loop-filter
|
||||||
BPF_TARGETS += pkt-loop-filter.kern
|
BPF_TARGETS += pkt-loop-filter.kern
|
||||||
|
BPF_SKEL_OBJ := pkt-loop-filter.kern.o
|
||||||
EXTRA_DEPS := pkt-loop-filter.h
|
EXTRA_DEPS := pkt-loop-filter.h
|
||||||
|
|
||||||
LIB_DIR = ../lib
|
LIB_DIR = ../lib
|
||||||
|
@@ -11,15 +11,15 @@
|
|||||||
|
|
||||||
#include <bpf/libbpf.h>
|
#include <bpf/libbpf.h>
|
||||||
|
|
||||||
|
#include "pkt-loop-filter.kern.skel.h"
|
||||||
|
|
||||||
#define MAX_IFINDEXES 10
|
#define MAX_IFINDEXES 10
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int err = 0, i, num_ifindexes = 0, _err, ingress_fd, egress_fd;
|
int err = 0, i, num_ifindexes = 0, _err, ingress_fd, egress_fd;
|
||||||
const char *filename = "pkt-loop-filter.kern.o";
|
struct pkt_loop_filter_kern *skel = NULL;
|
||||||
struct bpf_link *trace_link = NULL;
|
struct bpf_link *trace_link = NULL;
|
||||||
struct bpf_program *trace_prog;
|
|
||||||
struct bpf_object *obj = NULL;
|
|
||||||
int ifindex[MAX_IFINDEXES];
|
int ifindex[MAX_IFINDEXES];
|
||||||
bool unload = false;
|
bool unload = false;
|
||||||
char pin_path[100];
|
char pin_path[100];
|
||||||
@@ -60,40 +60,33 @@ int main(int argc, char *argv[])
|
|||||||
if (unload)
|
if (unload)
|
||||||
goto unload;
|
goto unload;
|
||||||
|
|
||||||
obj = bpf_object__open(filename);
|
skel = pkt_loop_filter_kern__open();
|
||||||
err = libbpf_get_error(obj);
|
err = libbpf_get_error(skel);
|
||||||
if (err) {
|
if (err) {
|
||||||
fprintf(stderr, "Couldn't open file: %s\n", filename);
|
fprintf(stderr, "Couldn't open BPF skeleton: %s\n", strerror(errno));
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
err = bpf_object__load(obj);
|
err = pkt_loop_filter_kern__load(skel);
|
||||||
if (err) {
|
if (err) {
|
||||||
fprintf(stderr, "Failed to load object\n");
|
fprintf(stderr, "Failed to load object\n");
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
egress_fd = bpf_program__fd(bpf_object__find_program_by_name(obj, "record_egress_pkt"));
|
egress_fd = bpf_program__fd(skel->progs.record_egress_pkt);
|
||||||
if (egress_fd < 0) {
|
if (egress_fd < 0) {
|
||||||
fprintf(stderr, "Couldn't find program 'record_egress_pkt'\n");
|
fprintf(stderr, "Couldn't find program 'record_egress_pkt'\n");
|
||||||
err = -ENOENT;
|
err = -ENOENT;
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
ingress_fd = bpf_program__fd(bpf_object__find_program_by_name(obj, "filter_ingress_pkt"));
|
ingress_fd = bpf_program__fd(skel->progs.filter_ingress_pkt);
|
||||||
if (ingress_fd < 0) {
|
if (ingress_fd < 0) {
|
||||||
fprintf(stderr, "Couldn't find program 'filter_ingress_pkt'\n");
|
fprintf(stderr, "Couldn't find program 'filter_ingress_pkt'\n");
|
||||||
err = -ENOENT;
|
err = -ENOENT;
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
trace_prog = bpf_object__find_program_by_name(obj, "handle_device_notify");
|
|
||||||
if (!trace_prog) {
|
|
||||||
fprintf(stderr, "Couldn't find program 'handle_device_notify'\n");
|
|
||||||
err = -ENOENT;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < num_ifindexes; i++) {
|
for (i = 0; i < num_ifindexes; i++) {
|
||||||
DECLARE_LIBBPF_OPTS(bpf_tc_opts, attach_egress,
|
DECLARE_LIBBPF_OPTS(bpf_tc_opts, attach_egress,
|
||||||
.prog_fd = egress_fd);
|
.prog_fd = egress_fd);
|
||||||
@@ -130,14 +123,13 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
trace_link = bpf_program__attach(trace_prog);
|
trace_link = bpf_program__attach(skel->progs.handle_device_notify);
|
||||||
if (!trace_link) {
|
if (!trace_link) {
|
||||||
fprintf(stderr, "Couldn't attach tracing prog: %s\n", strerror(errno));
|
fprintf(stderr, "Couldn't attach tracing prog: %s\n", strerror(errno));
|
||||||
err = -EFAULT;
|
err = -EFAULT;
|
||||||
goto unload;
|
goto unload;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
err = bpf_link__pin(trace_link, pin_path);
|
err = bpf_link__pin(trace_link, pin_path);
|
||||||
if (err) {
|
if (err) {
|
||||||
fprintf(stderr, "Couldn't pin bpf_link: %s\n", strerror(errno));
|
fprintf(stderr, "Couldn't pin bpf_link: %s\n", strerror(errno));
|
||||||
@@ -146,7 +138,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
out:
|
out:
|
||||||
bpf_link__destroy(trace_link);
|
bpf_link__destroy(trace_link);
|
||||||
bpf_object__close(obj);
|
pkt_loop_filter_kern__destroy(skel);
|
||||||
return err;
|
return err;
|
||||||
|
|
||||||
unload:
|
unload:
|
||||||
|
Reference in New Issue
Block a user