Skip to content

Commit

Permalink
cli: auto detect libxdp at build time & fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hack3ric committed Dec 11, 2024
1 parent 1efa7e3 commit f34d836
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions src/libxdp.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <errno.h>
#include <xdp/libxdp.h>

#include "common/try.h"
#include "common/log.h"
#include "libxdp.h"

static void *libxdp_dl = NULL;
Expand All @@ -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;
}

Expand All @@ -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);

Expand Down
8 changes: 6 additions & 2 deletions src/run.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f34d836

Please sign in to comment.