diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 6892c87..8817f35 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -30,7 +30,7 @@ jobs: - name: Install LibXDP. run: sudo make libxdp_install - name: Make project - run: LIBBPF_LIBXDP_STATIC=1 make + run: LIBXDP_STATIC=1 make - name: Install project run: sudo make install - name: Store build artifacts diff --git a/Makefile b/Makefile index 1d797f3..d29bbdd 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC = clang -LIBBPF_LIBXDP_STATIC ?= 0 +LIBXDP_STATIC ?= 0 # Top-level directories. BUILD_DIR = build @@ -61,7 +61,7 @@ LOADER_UTILS_HELPERS_OBJ = helpers.o # Loader objects. LOADER_OBJS = $(BUILD_LOADER_DIR)/$(LOADER_UTILS_CONFIG_OBJ) $(BUILD_LOADER_DIR)/$(LOADER_UTILS_CMDLINE_OBJ) $(BUILD_LOADER_DIR)/$(LOADER_UTILS_XDP_OBJ) $(BUILD_LOADER_DIR)/$(LOADER_UTILS_STATS_OBJ) $(BUILD_LOADER_DIR)/$(LOADER_UTILS_HELPERS_OBJ) -ifeq ($(LIBBPF_LIBXDP_STATIC), 1) +ifeq ($(LIBXDP_STATIC), 1) LOADER_OBJS := $(LIBBPF_OBJS) $(LIBXDP_OBJS) $(LOADER_OBJS) endif @@ -70,13 +70,21 @@ XDP_SRC = prog.c XDP_OBJ = xdp_prog.o # Includes. -INCS = -I $(SRC_DIR) -I $(LIBBPF_SRC) -I /usr/include -I /usr/local/include +INCS = -I $(SRC_DIR) + +ifeq ($(LIBXDP_STATIC), 1) + INCS += -I $(XDP_TOOLS_HEADERS) -I $(LIBBPF_SRC) +else + INCS += -I /usr/include -I /usr/local/include +endif # Flags. FLAGS = -O2 -g FLAGS_LOADER = -lconfig -lelf -lz -ifeq ($(LIBBPF_LIBXDP_STATIC), 0) +ifeq ($(LIBXDP_STATIC), 1) + FLAGS += -D__LIBXDP_STATIC__ +else FLAGS_LOADER += -lbpf -lxdp endif diff --git a/README.md b/README.md index 8bd598a..da76db1 100644 --- a/README.md +++ b/README.md @@ -293,13 +293,13 @@ If you receive an error similar to the one below when running the program, make ./xdpfw: error while loading shared libraries: libxdp.so.1: cannot open shared object file: No such file or directory ``` -If you don't want to have LibXDP installed on your system after building the program, you can set the `LIBBPF_LIBXDP_STATIC` environmental variable to `1` while building the project with `make` or pass the `--static` flag to the [`install.sh`](./install.sh) Bash script. This will link all of the LibBPF and LibXDP object files while building the loader so you shouldn't need LibXDP installed globally. +If you don't want to have LibXDP installed on your system after building the program, you can set the `LIBXDP_STATIC` environmental variable to `1` while building the project with `make` or pass the `--static` flag to the [`install.sh`](./install.sh) Bash script. This will link all of the LibBPF and LibXDP object files while building the loader so you shouldn't need LibXDP installed globally. For example: ```bash # Build with LibBPF and LibXDP object files linked directly from modules/xdp-tools directories. -LIBBPF_LIBXDP_STATIC=1 make +LIBXDP_STATIC=1 make # Install onto system. sudo make install diff --git a/scripts/build.sh b/scripts/build.sh index ff6fd1b..2ec6527 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -7,7 +7,7 @@ if [ -z "$STATIC" ]; then fi if [ -z "$ROOT" ]; then - LIBBPF_LIBXDP_STATIC=$STATIC make + LIBXDP_STATIC=$STATIC make else - cd $ROOT && LIBBPF_LIBXDP_STATIC=$STATIC make + cd $ROOT && LIBXDP_STATIC=$STATIC make fi \ No newline at end of file diff --git a/src/loader/utils/config.h b/src/loader/utils/config.h index 04d767b..72c169d 100644 --- a/src/loader/utils/config.h +++ b/src/loader/utils/config.h @@ -6,7 +6,6 @@ #include #include #include -#include #include diff --git a/src/loader/utils/stats.h b/src/loader/utils/stats.h index 5330910..71cbc91 100644 --- a/src/loader/utils/stats.h +++ b/src/loader/utils/stats.h @@ -1,7 +1,5 @@ #pragma once -#include -#include #include #include diff --git a/src/loader/utils/xdp.h b/src/loader/utils/xdp.h index e823f5e..9b7b127 100644 --- a/src/loader/utils/xdp.h +++ b/src/loader/utils/xdp.h @@ -1,7 +1,5 @@ #pragma once -#include -#include #include #include diff --git a/src/xdp/utils/helpers.h b/src/xdp/utils/helpers.h index 1e53ab5..14b99e5 100644 --- a/src/xdp/utils/helpers.h +++ b/src/xdp/utils/helpers.h @@ -3,12 +3,16 @@ #include #include -#include -#include #include #include +#ifdef __LIBXDP_STATIC__ +#include +#else +#include +#endif + #define likely(x) __builtin_expect(!!(x), 1) #define unlikely(x) __builtin_expect(!!(x), 0) diff --git a/src/xdp/utils/rl.h b/src/xdp/utils/rl.h index d87bd42..85ec5eb 100644 --- a/src/xdp/utils/rl.h +++ b/src/xdp/utils/rl.h @@ -9,7 +9,6 @@ static __always_inline void UpdateIpStats(u64 *pps, u64 *bps, u32 ip, u16 port, u8 protocol, u16 pkt_len, u64 now); static __always_inline void UpdateIp6Stats(u64 *pps, u64 *bps, u128 *ip, u16 port, u8 protocol, u16 pkt_len, u64 now); - // The source file is included directly below instead of compiled and linked as an object because when linking, there is no guarantee the compiler will inline the function (which is crucial for performance). // I'd prefer not to include the function logic inside of the header file. // More Info: https://stackoverflow.com/questions/24289599/always-inline-does-not-work-when-function-is-implemented-in-different-file