Skip to content

Commit 441650c

Browse files
authored
Merge pull request #26 from xdp-project/libxdp
Embedding libxdp
2 parents 58fcc52 + 24cea38 commit 441650c

File tree

13 files changed

+249
-17
lines changed

13 files changed

+249
-17
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
path = lib/libbpf
33
url = https://github.com/xdp-project/libbpf.git
44
ignore = dirty
5+
[submodule "lib/xdp-tools"]
6+
path = lib/xdp-tools
7+
url = https://github.com/xdp-project/xdp-tools

configure

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ CONFIG=".${CONFIG}.tmp"
1111
TMPDIR=$(mktemp -d config.XXXXXX)
1212
trap 'status=$?; rm -rf $TMPDIR; rm -f $CONFIG; exit $status' EXIT HUP INT QUIT TERM
1313

14+
SUBMODULE_LIBBPF=0
15+
1416
check_toolchain()
1517
{
1618
local clang_version
@@ -123,12 +125,10 @@ EOF
123125
fi
124126

125127
echo submodule
128+
SUBMODULE_LIBBPF=1
126129
echo "SYSTEM_LIBBPF:=n" >> $CONFIG
127-
echo 'CFLAGS += -I$(LIB_DIR)/libbpf-install/usr/include' >>$CONFIG
128-
echo 'BPF_CFLAGS += -I$(LIB_DIR)/libbpf-install/usr/include' >>$CONFIG
129-
echo 'LDFLAGS += -L$(LIB_DIR)/libbpf/src' >>$CONFIG
130130
echo 'LDLIBS += -l:libbpf.a' >>$CONFIG
131-
echo 'OBJECT_LIBBPF = $(LIB_DIR)/libbpf/src/libbpf.a' >>$CONFIG
131+
echo 'OBJECT_LIBBPF = $(LIB_DIR)/install/lib/libbpf.a' >>$CONFIG
132132
if ! [ -d "lib/libbpf/src" ] && [ -f ".gitmodules" ] && [ -e ".git" ]; then
133133
git submodule init && git submodule update
134134
fi
@@ -173,6 +173,39 @@ EOF
173173
fi
174174
}
175175

176+
check_libxdp()
177+
{
178+
if [ "${FORCE_SUBMODULE_LIBXDP:-0}" -ne "1" ] && ${PKG_CONFIG} libxdp --exists; then
179+
180+
LIBXDP_CFLAGS=$(${PKG_CONFIG} libxdp --cflags)
181+
LIBXDP_LDLIBS=$(${PKG_CONFIG} libxdp --libs)
182+
echo "SYSTEM_LIBXDP:=y" >>$CONFIG
183+
echo 'CFLAGS += ' $LIBXDP_CFLAGS >> $CONFIG
184+
echo 'LDLIBS += ' $LIBXDP_LDLIBS >>$CONFIG
185+
echo 'OBJECT_LIBXDP = ' >>$CONFIG
186+
echo system
187+
188+
return 0
189+
fi
190+
191+
echo submodule
192+
echo "SYSTEM_LIBXDP:=n" >> $CONFIG
193+
if [ "$SUBMODULE_LIBBPF" -eq "1" ]; then
194+
echo "Configuring libxdp to use our libbpf submodule"
195+
(export LIBBPF_DIR="$(readlink -m lib/libbpf)" LIBBPF_INCLUDE_DIR="$(readlink -m lib/install/include)";
196+
cd lib/xdp-tools; ./configure)
197+
else
198+
echo "Configuring libxdp without our libbpf"
199+
(cd lib/xdp-tools; ./configure)
200+
fi
201+
echo 'LDFLAGS += -L$(LIB_DIR)/install/lib' >>$CONFIG
202+
echo 'LDLIBS += -l:libxdp.a' >>$CONFIG
203+
echo 'OBJECT_LIBXDP = $(LIB_DIR)/install/lib/libxdp.a' >>$CONFIG
204+
if ! [ -d "lib/xdp-tools/lib" ] && [ -f ".gitmodules" ] && [ -e ".git" ]; then
205+
git submodule init && git submodule update
206+
fi
207+
}
208+
176209
quiet_config()
177210
{
178211
cat <<EOF
@@ -211,6 +244,8 @@ check_toolchain
211244

212245
echo -n "libbpf support: "
213246
check_libbpf
247+
echo -n "libxdp support: "
248+
check_libxdp
214249

215250
check_bpf_use_errno
216251

lib/Makefile

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,31 @@
22
LIBBPF_CFLAGS:=$(if $(CFLAGS),$(CFLAGS),-g -O2 -Werror -Wall) -fPIC
33

44
LIB_DIR = .
5+
LIB_INSTALL := $(LIB_DIR)/install
56
include defines.mk
67

7-
SUBDIRS=
8+
SUBDIRS=util
89

9-
all: $(OBJECT_LIBBPF)
10+
all: $(OBJECT_LIBBPF) $(OBJECT_LIBXDP)
1011
@set -e; \
1112
for i in $(SUBDIRS); \
1213
do echo; echo " $$i"; $(MAKE) -C $$i; done
1314

1415
.PHONY: clean
15-
clean: libbpf_clean
16+
clean: libbpf_clean libxdp_clean
1617
@for i in $(SUBDIRS); \
1718
do $(MAKE) -C $$i clean; done
19+
$(Q)find $(LIB_INSTALL) -type f -not -name .gitignore -delete
20+
$(Q)find $(LIB_INSTALL) -type d -empty -delete
1821

1922
install:
2023
install -m 0755 -d $(DESTDIR)$(HDRDIR)
2124
$(MAKE) -C libxdp install
2225
$(MAKE) -C testing install
2326

27+
2428
libbpf: $(OBJECT_LIBBPF)
29+
libxdp: libbpf $(OBJECT_LIBXDP)
2530

2631
# Handle libbpf as git submodule
2732
ifeq ($(SYSTEM_LIBBPF),n)
@@ -31,21 +36,44 @@ endif
3136

3237
# Detect submodule libbpf source file changes
3338
LIBBPF_SOURCES := $(wildcard libbpf/src/*.[ch])
34-
LIBBPF_INSTALL := libbpf-install
35-
INSTDIR=../../$(LIBBPF_INSTALL)
3639

37-
.PHONY: libbpf_clean
38-
libbpf/src/libbpf.a: $(LIBBPF_SOURCES)
40+
$(LIB_INSTALL)/lib/libbpf.a: $(LIBBPF_SOURCES)
3941
@echo ; echo " libbpf"
4042
$(QUIET_CC)$(MAKE) -C libbpf/src CFLAGS="$(LIBBPF_CFLAGS)" $P
41-
$(QUIET_INSTALL)$(MAKE) -C libbpf/src DESTDIR=$(INSTDIR) install_headers $P
43+
$(QUIET_INSTALL)$(MAKE) -C libbpf/src DESTDIR=../../$(LIB_INSTALL) PREFIX= install_headers $P
44+
$(Q)cp -fp libbpf/src/libbpf.a install/lib/
4245

46+
.PHONY: libbpf_clean
4347
libbpf_clean:
4448
$(Q)$(MAKE) -C libbpf/src clean $P
45-
$(Q)$(RM) -r $(LIBBPF_INSTALL)
4649

4750
else
4851

4952
libbpf_clean:
5053
@echo -n
5154
endif
55+
56+
# Handle libbpf as git submodule
57+
ifeq ($(SYSTEM_LIBXDP),n)
58+
ifeq ($(VERBOSE),0)
59+
P:= >/dev/null
60+
endif
61+
62+
# Detect submodule libbpf source file changes
63+
LIBXDP_SOURCES := $(wildcard xdp-tools/lib/libxdp/libxdp*.[ch]) xdp-tools/lib/libxdp/xsk.c
64+
65+
66+
$(LIB_INSTALL)/lib/libxdp.a: $(LIBXDP_SOURCES)
67+
@echo ; echo " libxdp"
68+
$(QUIET_CC)$(MAKE) -C xdp-tools BUILD_STATIC_ONLY=1 libxdp $P
69+
$(QUIET_INSTALL)$(MAKE) -C xdp-tools DESTDIR=../../../$(LIB_INSTALL) PREFIX= BUILD_STATIC_ONLY=1 libxdp_install $P
70+
71+
.PHONY: libxdp_clean
72+
libxdp_clean:
73+
$(Q)$(MAKE) -C xdp-tools clean $P
74+
75+
else
76+
77+
libxdp_clean:
78+
@echo -n
79+
endif

lib/common.mk

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ BPF_CFLAGS += -D__TARGET_ARCH_$(ARCH)
4747
# BPF-prog kern and userspace shares struct via header file:
4848
KERN_USER_H ?= $(wildcard common_kern_user.h)
4949

50-
CFLAGS += -I$(INCLUDE_DIR) -I$(HEADER_DIR) -I$(LIB_DIR)/util $(EXTRA_CFLAGS)
51-
BPF_CFLAGS += -I$(INCLUDE_DIR) -I$(HEADER_DIR) $(EXTRA_CFLAGS)
50+
CFLAGS += -I$(INCLUDE_DIR) -I$(HEADER_DIR) -I$(LIB_DIR)/util -I$(LIB_DIR)/install/include $(EXTRA_CFLAGS)
51+
BPF_CFLAGS += -I$(INCLUDE_DIR) -I$(HEADER_DIR) -I$(LIB_DIR)/install/include $(EXTRA_CFLAGS)
52+
LDFLAGS += -L$(LIB_DIR)/install/lib
5253

5354
BPF_HEADERS := $(wildcard $(HEADER_DIR)/*/*.h) $(wildcard $(INCLUDE_DIR)/*/*.h)
5455

@@ -61,6 +62,9 @@ clean::
6162
$(OBJECT_LIBBPF): $(LIBBPF_SOURCES)
6263
$(Q)$(MAKE) -C $(LIB_DIR) libbpf
6364

65+
$(OBJECT_LIBXDP): $(LIBXDP_SOURCES)
66+
$(Q)$(MAKE) -C $(LIB_DIR) libxdp
67+
6468
$(CONFIGMK):
6569
$(Q)$(MAKE) -C $(LIB_DIR)/.. config.mk
6670

lib/install/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*
2+
!.gitignore
3+
!include
4+
!lib

lib/install/include/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

lib/install/lib/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

lib/util/Makefile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
include util.mk
2+
3+
LIB_DIR ?= ..
4+
5+
include $(LIB_DIR)/defines.mk
6+
7+
all: $(UTIL_OBJS)
8+
9+
# Create expansions for dependencies
10+
UTIL_H := ${UTIL_OBJS:.o=.h}
11+
12+
CFLAGS+= -I$(LIB_DIR)/install/include
13+
14+
$(UTIL_OBJS): %.o: %.c $(UTIL_H) $(LIBMK)
15+
$(QUIET_CC)$(CC) $(CFLAGS) -Wall -I../../headers -c -o $@ $<
16+
17+
clean:
18+
$(Q)rm -f $(UTIL_OBJS)

lib/util/logging.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
3+
#include <stdio.h>
4+
#include <stdarg.h>
5+
6+
#include <bpf/libbpf.h>
7+
#include <xdp/libxdp.h>
8+
9+
#define __unused __attribute__((unused))
10+
#include "logging.h"
11+
12+
static enum logging_print_level log_level = LOG_INFO;
13+
14+
static int print_func(enum logging_print_level level, const char *format,
15+
va_list args)
16+
{
17+
if (level > log_level)
18+
return 0;
19+
20+
return vfprintf(stderr, format, args);
21+
}
22+
23+
static int libbpf_print_func(enum libbpf_print_level level, const char *format,
24+
va_list args)
25+
{
26+
return print_func(level + 1, format, args);
27+
}
28+
29+
static int libbpf_silent_func(__unused enum libbpf_print_level level,
30+
__unused const char *format,
31+
__unused va_list args)
32+
{
33+
return 0;
34+
}
35+
36+
static int libxdp_print_func(enum libxdp_print_level level, const char *format,
37+
va_list args)
38+
{
39+
return print_func(level + 1, format, args);
40+
}
41+
42+
static int libxdp_silent_func(__unused enum libxdp_print_level level,
43+
__unused const char *format,
44+
__unused va_list args)
45+
{
46+
return 0;
47+
}
48+
49+
#define __printf(a, b) __attribute__((format(printf, a, b)))
50+
51+
__printf(2, 3) void logging_print(enum logging_print_level level,
52+
const char *format, ...)
53+
{
54+
va_list args;
55+
56+
va_start(args, format);
57+
print_func(level, format, args);
58+
va_end(args);
59+
}
60+
61+
void init_lib_logging(void)
62+
{
63+
libbpf_set_print(libbpf_print_func);
64+
libxdp_set_print(libxdp_print_func);
65+
}
66+
67+
void silence_libbpf_logging(void)
68+
{
69+
if (log_level < LOG_VERBOSE)
70+
libbpf_set_print(libbpf_silent_func);
71+
}
72+
73+
void silence_libxdp_logging(void)
74+
{
75+
if (log_level < LOG_VERBOSE)
76+
libxdp_set_print(libxdp_silent_func);
77+
}
78+
79+
enum logging_print_level set_log_level(enum logging_print_level level)
80+
{
81+
enum logging_print_level old_level = log_level;
82+
83+
log_level = level;
84+
return old_level;
85+
}
86+
87+
enum logging_print_level increase_log_level(void)
88+
{
89+
if (log_level < LOG_VERBOSE)
90+
log_level++;
91+
return log_level;
92+
}

lib/util/logging.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
3+
#ifndef __LOGGING_H
4+
#define __LOGGING_H
5+
6+
/* This matches the libbpf logging levels, but with an additional VERBOSE level;
7+
* we demote all libbpf messages by one level so debug messages only show up on
8+
* VERBOSE.
9+
*/
10+
enum logging_print_level {
11+
LOG_WARN,
12+
LOG_INFO,
13+
LOG_DEBUG,
14+
LOG_VERBOSE,
15+
};
16+
17+
extern void logging_print(enum logging_print_level level, const char *format,
18+
...) __attribute__((format(printf, 2, 3)));
19+
20+
#define __pr(level, fmt, ...) \
21+
do { \
22+
logging_print(level, fmt, ##__VA_ARGS__); \
23+
} while (0)
24+
25+
#define pr_warn(fmt, ...) __pr(LOG_WARN, fmt, ##__VA_ARGS__)
26+
#define pr_info(fmt, ...) __pr(LOG_INFO, fmt, ##__VA_ARGS__)
27+
#define pr_debug(fmt, ...) __pr(LOG_DEBUG, fmt, ##__VA_ARGS__)
28+
29+
void init_lib_logging(void);
30+
void silence_libbpf_logging(void);
31+
void silence_libxdp_logging(void);
32+
enum logging_print_level set_log_level(enum logging_print_level level);
33+
enum logging_print_level increase_log_level();
34+
35+
#endif

0 commit comments

Comments
 (0)