From f34d836a9aeb01debb3c24530826d63eba0b346d Mon Sep 17 00:00:00 2001 From: Eric Long Date: Wed, 11 Dec 2024 20:40:42 +0800 Subject: [PATCH] cli: auto detect libxdp at build time & fixes --- Makefile | 5 +++++ src/libxdp.c | 12 +++++++++--- src/run.c | 8 ++++++-- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index fba1a21..db64e14 100644 --- a/Makefile +++ b/Makefile @@ -33,6 +33,11 @@ else ifeq ($(MODE), release) CFLAGS += -O2 endif +ifeq ($(USE_LIBXDP),) +ifeq ($(shell pkg-config --exists 'libxdp >= 1' 'libxdp < 2' >/dev/null 2>&1; echo $$?),0) +USE_LIBXDP := 1 +endif +endif ifeq ($(USE_LIBXDP),1) BPF_CFLAGS += -DMIMIC_USE_LIBXDP CFLAGS += -DMIMIC_USE_LIBXDP diff --git a/src/libxdp.c b/src/libxdp.c index 6a45077..76bd3ad 100644 --- a/src/libxdp.c +++ b/src/libxdp.c @@ -4,7 +4,7 @@ #include #include -#include "common/try.h" +#include "common/log.h" #include "libxdp.h" static void *libxdp_dl = NULL; @@ -25,7 +25,10 @@ static int dlsym_many_or_warnv(void *dl, va_list ap) { symbol = va_arg(ap, typeof(symbol)); tfn = (typeof(tfn))dlsym(dl, symbol); - if (!tfn) ret(-ELIBBAD, "can't find symbol '%s': %s", symbol, dlerror()); + if (!tfn) { + log_warn(_("cannot find symbol '%s': %s"), symbol, dlerror()); + return -ELIBBAD; + } *fn = tfn; } @@ -39,7 +42,10 @@ static int dlopen_many_sym_or_warn_sentinel(void **dlp, const char *filename, .. if (*dlp) return 0; dl = dlopen(filename, RTLD_NOW | RTLD_NODELETE); - if (!dl) ret(-EOPNOTSUPP, "%s is not installed: %s", filename, dlerror()); + if (!dl) { + log_warn(_("%s is not installed: %s"), filename, dlerror()); + return -EOPNOTSUPP; + } log_debug("loaded '%s' via dlopen()", filename); diff --git a/src/run.c b/src/run.c index 669f5e7..b535292 100644 --- a/src/run.c +++ b/src/run.c @@ -766,8 +766,12 @@ int subcmd_run(struct run_args* args) { libbpf_set_print(libbpf_print_fn); #ifdef MIMIC_USE_LIBXDP if (args->use_libxdp) { - dlopen_libxdp(); - sym_libxdp_set_print((libxdp_print_fn_t)libbpf_print_fn); + if (dlopen_libxdp() < 0) { + log_warn(_("fall back to using libbpf for loading XDP programs")); + args->use_libxdp = false; + } else { + sym_libxdp_set_print((libxdp_print_fn_t)libbpf_print_fn); + } } #endif retcode = run_bpf(args, lock_fd, args->ifname, ifindex);