samples: bpf: convert xdp_fwd_user.c to libbpf

Convert xdp_fwd_user.c to use libbpf instead of bpf_load.o.

Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Reviewed-by: Quentin Monnet <quentin.monnet@netronome.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
This commit is contained in:
Jakub Kicinski 2018-07-26 14:32:20 -07:00 committed by Daniel Borkmann
parent 6d4b198b0b
commit e1a40ef418
2 changed files with 24 additions and 12 deletions

View file

@ -106,7 +106,7 @@ syscall_tp-objs := bpf_load.o syscall_tp_user.o
cpustat-objs := bpf_load.o cpustat_user.o cpustat-objs := bpf_load.o cpustat_user.o
xdp_adjust_tail-objs := xdp_adjust_tail_user.o xdp_adjust_tail-objs := xdp_adjust_tail_user.o
xdpsock-objs := bpf_load.o xdpsock_user.o xdpsock-objs := bpf_load.o xdpsock_user.o
xdp_fwd-objs := bpf_load.o xdp_fwd_user.o xdp_fwd-objs := xdp_fwd_user.o
task_fd_query-objs := bpf_load.o task_fd_query_user.o $(TRACE_HELPERS) task_fd_query-objs := bpf_load.o task_fd_query_user.o $(TRACE_HELPERS)
xdp_sample_pkts-objs := xdp_sample_pkts_user.o $(TRACE_HELPERS) xdp_sample_pkts-objs := xdp_sample_pkts_user.o $(TRACE_HELPERS)

View file

@ -24,8 +24,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <libgen.h> #include <libgen.h>
#include "bpf_load.h" #include "bpf/libbpf.h"
#include "bpf_util.h"
#include <bpf/bpf.h> #include <bpf/bpf.h>
@ -63,9 +62,15 @@ static void usage(const char *prog)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
struct bpf_prog_load_attr prog_load_attr = {
.prog_type = BPF_PROG_TYPE_XDP,
};
const char *prog_name = "xdp_fwd";
struct bpf_program *prog;
char filename[PATH_MAX]; char filename[PATH_MAX];
struct bpf_object *obj;
int opt, i, idx, err; int opt, i, idx, err;
int prog_id = 0; int prog_fd, map_fd;
int attach = 1; int attach = 1;
int ret = 0; int ret = 0;
@ -75,7 +80,7 @@ int main(int argc, char **argv)
attach = 0; attach = 0;
break; break;
case 'D': case 'D':
prog_id = 1; prog_name = "xdp_fwd_direct";
break; break;
default: default:
usage(basename(argv[0])); usage(basename(argv[0]));
@ -90,6 +95,7 @@ int main(int argc, char **argv)
if (attach) { if (attach) {
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
prog_load_attr.file = filename;
if (access(filename, O_RDONLY) < 0) { if (access(filename, O_RDONLY) < 0) {
printf("error accessing file %s: %s\n", printf("error accessing file %s: %s\n",
@ -97,19 +103,25 @@ int main(int argc, char **argv)
return 1; return 1;
} }
if (load_bpf_file(filename)) { if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
printf("%s", bpf_log_buf); return 1;
prog = bpf_object__find_program_by_title(obj, prog_name);
prog_fd = bpf_program__fd(prog);
if (prog_fd < 0) {
printf("program not found: %s\n", strerror(prog_fd));
return 1; return 1;
} }
map_fd = bpf_map__fd(bpf_object__find_map_by_name(obj,
if (!prog_fd[prog_id]) { "tx_port"));
printf("load_bpf_file: %s\n", strerror(errno)); if (map_fd < 0) {
printf("map not found: %s\n", strerror(map_fd));
return 1; return 1;
} }
} }
if (attach) { if (attach) {
for (i = 1; i < 64; ++i) for (i = 1; i < 64; ++i)
bpf_map_update_elem(map_fd[0], &i, &i, 0); bpf_map_update_elem(map_fd, &i, &i, 0);
} }
for (i = optind; i < argc; ++i) { for (i = optind; i < argc; ++i) {
@ -126,7 +138,7 @@ int main(int argc, char **argv)
if (err) if (err)
ret = err; ret = err;
} else { } else {
err = do_attach(idx, prog_fd[prog_id], argv[i]); err = do_attach(idx, prog_fd, argv[i]);
if (err) if (err)
ret = err; ret = err;
} }