af_xdp: update af_xdp driver plugin to depend on libxdp 69/37869/6
authorYulong Pei <yulong.pei@intel.com>
Thu, 5 Jan 2023 02:26:32 +0000 (02:26 +0000)
committerBeno�t Ganne <bganne@cisco.com>
Fri, 13 Jan 2023 09:57:00 +0000 (09:57 +0000)
AF_XDP support is deprecated in libbpf since v0.7.0 [1], the libxdp library
now provides the functionality which once was in libbpf, this commit updates
af_xdp plugin to depend on libxdp, libbpf still remains a dependency even if
libxdp is present, as it need use libbpf APIs for program loading.

libxdp is distributed within xdp-tool [2], xdp-tools package also
include libbpf in it as dependency, so here installed libxdp v1.2.9 and
libbpf v0.8.0, both from xdp-tool-1.2.9 package.

More information about libxdp compatibility can be found in the libxdp
README [3].

In libbpf v0.8.0, The bpf_prog_load function was deprecated and changed to
bpf_object__open_file and bpf_object__next_program and bpf_object__load,
The bpf_get_link_xdp_id and bpf_set_link_xdp_fd functions were deprecated
and changed to bpf_xdp_attach and bpf_xdp_detach, The bpf_object__unload
function was deprecated and changed to bpf_object__close.

[1] https://github.com/libbpf/libbpf/commit/277846bc6c15
[2] https://github.com/xdp-project/xdp-tools/releases/tag/v1.2.9
[3] https://github.com/xdp-project/xdp-tools/blob/master/lib/libxdp/README.org

Type: improvement

Change-Id: Ifbf6e3aa38bc6e0b77561f26311fd11c15ddb47e
Signed-off-by: Yulong Pei <yulong.pei@intel.com>
Makefile
build/external/Makefile
build/external/packages/libbpf.mk [deleted file]
build/external/packages/xdp-tools.mk [new file with mode: 0644]
build/external/patches/xdp-tools_1.2.9/0001-libxdp-add-fPIC-with-static-lib-build.patch [new file with mode: 0644]
build/external/patches/xdp-tools_1.2.9/0002-libxdp-fix-asm-types.h-file-not-found-issue.patch [new file with mode: 0644]
build/external/patches/xdp-tools_1.2.9/0003-libxdp-fix-maybe-uninitialized-compiler-warning.patch [new file with mode: 0644]
src/plugins/af_xdp/CMakeLists.txt
src/plugins/af_xdp/af_xdp.h
src/plugins/af_xdp/device.c

index 743be51..3514e7d 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -75,7 +75,7 @@ DEB_DEPENDS += libnl-3-dev libnl-route-3-dev libmnl-dev
 # DEB_DEPENDS += enchant  # for docs
 DEB_DEPENDS += python3-virtualenv
 DEB_DEPENDS += libssl-dev
-DEB_DEPENDS += libelf-dev # for libbpf (af_xdp)
+DEB_DEPENDS += libelf-dev libpcap-dev # for libxdp (af_xdp)
 DEB_DEPENDS += iperf3 # for 'make test TEST=vcl'
 DEB_DEPENDS += nasm
 DEB_DEPENDS += iperf ethtool  # for 'make test TEST=vm_vpp_interfaces'
@@ -85,14 +85,12 @@ LIBFFI=libffi6 # works on all but 20.04 and debian-testing
 ifeq ($(OS_VERSION_ID),22.04)
        DEB_DEPENDS += python3-virtualenv
        DEB_DEPENDS += libssl-dev
-       DEB_DEPENDS += libelf-dev # for libbpf (af_xdp)
        DEB_DEPENDS += clang clang-format-11
        LIBFFI=libffi7
        DEB_DEPENDS += enchant-2  # for docs
 else ifeq ($(OS_VERSION_ID),20.04)
        DEB_DEPENDS += python3-virtualenv
        DEB_DEPENDS += libssl-dev
-       DEB_DEPENDS += libelf-dev # for libbpf (af_xdp)
        DEB_DEPENDS += clang clang-format-11
        LIBFFI=libffi7
        DEB_DEPENDS += enchant-2  # for docs
@@ -766,4 +764,4 @@ endif
 
 .PHONY: check-dpdk-mlx
 check-dpdk-mlx:
-       @[ $$(make -sC build/external dpdk-show-DPDK_MLX_DEFAULT) = y ]
\ No newline at end of file
+       @[ $$(make -sC build/external dpdk-show-DPDK_MLX_DEFAULT) = y ]
index b036313..d648f4f 100644 (file)
@@ -40,14 +40,14 @@ include packages/ipsec-mb.mk
 include packages/quicly.mk
 include packages/rdma-core.mk
 include packages/dpdk.mk
-include packages/libbpf.mk
+include packages/xdp-tools.mk
 
 .PHONY: clean
 clean:
        @rm -rf $(B) $(I)
 
 .PHONY: install
-install: $(if $(ARCH_X86_64), ipsec-mb-install) dpdk-install rdma-core-install quicly-install libbpf-install
+install: $(if $(ARCH_X86_64), ipsec-mb-install) dpdk-install rdma-core-install quicly-install xdp-tools-install
 
 .PHONY: config
 config: $(if $(ARCH_X86_64), ipsec-mb-config) dpdk-config rdma-core-config quicly-build
diff --git a/build/external/packages/libbpf.mk b/build/external/packages/libbpf.mk
deleted file mode 100644 (file)
index 78b1468..0000000
+++ /dev/null
@@ -1,58 +0,0 @@
-# Copyright (c) 2018 Cisco and/or its affiliates.
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at:
-#
-#     http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-LIBBPF_DEBUG?=n
-
-libbpf_version             := 0.5.0
-libbpf_tarball             := v$(libbpf_version).tar.gz
-libbpf_tarball_md5sum_0.2  := cd0f82d76a9830c1e66b1a249393d5a8
-libbpf_tarball_md5sum_0.5.0:= ba0a514d2775f033c24a6fec37e587bc
-libbpf_tarball_md5sum      := $(libbpf_tarball_md5sum_$(libbpf_version))
-libbpf_tarball_strip_dirs  := 1
-libbpf_url                 := https://github.com/libbpf/libbpf/archive/$(libbpf_tarball)
-
-LIBBPF_CFLAGS:=-g -Werror -Wall -fPIC -fvisibility=hidden
-ifeq ($(LIBBPF_DEBUG),y)
-  LIBBPF_CFLAGS+= -O0
-else
-  LIBBPF_CFLAGS+= -O2
-endif
-
-# check for libelf, zlib and kernel if_xdp.h presence
-LIBBPF_DEPS_CHECK:="\#include <linux/if_xdp.h>\\n\#include <gelf.h>\\n\#include <zlib.h>\\nint main(void){return 0;}"
-LIBBPF_DEPS_CHECK:=$(shell echo -e $(LIBBPF_DEPS_CHECK) | $(CC) -xc -lelf -lz -o /dev/null - > /dev/null 2>&1)
-LIBBPF_DEPS_CHECK:=$(.SHELLSTATUS)
-
-define  libbpf_config_cmds
-       @true
-endef
-
-define  libbpf_build_cmds__
-       BUILD_STATIC_ONLY=y OBJDIR='$(libbpf_build_dir)' PREFIX='' DESTDIR='$(libbpf_install_dir)' CFLAGS='$(LIBBPF_CFLAGS)' make -C '$(libbpf_src_dir)/src' $(1) > $(2)
-endef
-
-define  libbpf_build_cmds
-       $(call libbpf_build_cmds__,,$(libbpf_build_log))
-endef
-
-define  libbpf_install_cmds
-       $(call libbpf_build_cmds__,install,$(libbpf_install_log))
-endef
-
-ifneq ($(LIBBPF_DEPS_CHECK),0)
-  $(warning "Missing libbpf dependencies. libbpf will be skipped.")
-libbpf-install:
-       @true
-else
-  $(eval $(call package,libbpf))
-endif
diff --git a/build/external/packages/xdp-tools.mk b/build/external/packages/xdp-tools.mk
new file mode 100644 (file)
index 0000000..b65ae13
--- /dev/null
@@ -0,0 +1,38 @@
+# Copyright (c) 2023 Intel and/or its affiliates.
+# Copyright (c) 2018 Cisco and/or its affiliates.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+xdp-tools_version             := 1.2.9
+xdp-tools_tarball             := xdp-tools-$(xdp-tools_version).tar.gz
+xdp-tools_tarball_md5sum_1.2.9:= 6e4a49ceea8354bb7bb3b55990e9aed7
+xdp-tools_tarball_md5sum      := $(xdp-tools_tarball_md5sum_$(xdp-tools_version))
+xdp-tools_tarball_strip_dirs  := 1
+xdp-tools_url                 := https://github.com/xdp-project/xdp-tools/releases/download/v$(xdp-tools_version)/$(xdp-tools_tarball)
+
+define  xdp-tools_config_cmds
+       @true
+endef
+
+define  xdp-tools_build_cmds
+       @cd ${xdp-tools_src_dir} && make V=1 BUILD_STATIC_ONLY=y > $(xdp-tools_build_log)
+endef
+
+define  xdp-tools_install_cmds
+       @rm -f $(xdp-tools_install_log)
+       @cd ${xdp-tools_src_dir} && \
+               make -C lib/libbpf/src install V=1 BUILD_STATIC_ONLY=y PREFIX='' DESTDIR='$(xdp-tools_install_dir)' >> $(xdp-tools_install_log)
+       @cd ${xdp-tools_src_dir} && \
+               make libxdp_install V=1 BUILD_STATIC_ONLY=y PREFIX='' DESTDIR='$(xdp-tools_install_dir)' >> $(xdp-tools_install_log)
+endef
+
+$(eval $(call package,xdp-tools))
diff --git a/build/external/patches/xdp-tools_1.2.9/0001-libxdp-add-fPIC-with-static-lib-build.patch b/build/external/patches/xdp-tools_1.2.9/0001-libxdp-add-fPIC-with-static-lib-build.patch
new file mode 100644 (file)
index 0000000..98b85a2
--- /dev/null
@@ -0,0 +1,26 @@
+From e83f80443a2f23a68037bf4c7ba16b3723d193a4 Mon Sep 17 00:00:00 2001
+From: Yulong <yulong.pei@intel.com>
+Date: Tue, 3 Jan 2023 14:16:17 +0000
+Subject: [PATCH] libxdp: add fPIC with static lib build
+
+Signed-off-by: Yulong <yulong.pei@intel.com>
+---
+ lib/libxdp/Makefile | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git a/lib/libxdp/Makefile b/lib/libxdp/Makefile
+index 358b751..a9bb414 100644
+--- a/lib/libxdp/Makefile
++++ b/lib/libxdp/Makefile
+@@ -88,7 +88,7 @@ $(SHARED_OBJDIR):
+       $(Q)mkdir -p $(SHARED_OBJDIR)
+ $(STATIC_OBJDIR)/%.o: %.c $(EXTRA_LIB_DEPS) | $(STATIC_OBJDIR)
+-      $(QUIET_CC)$(CC) $(CFLAGS) $(CPPFLAGS) -D LIBXDP_STATIC=1 -Wall -I../../headers -c $< -o $@
++      $(QUIET_CC)$(CC) $(CFLAGS) $(CPPFLAGS) -fPIC -D LIBXDP_STATIC=1 -Wall -I../../headers -c $< -o $@
+ $(SHARED_OBJDIR)/%.o: %.c $(EXTRA_LIB_DEPS) | $(SHARED_OBJDIR)
+       $(QUIET_CC)$(CC) $(CFLAGS) $(CPPFLAGS) $(SHARED_CFLAGS) -Wall -I../../headers -c $< -o $@
+-- 
+2.25.1
+
diff --git a/build/external/patches/xdp-tools_1.2.9/0002-libxdp-fix-asm-types.h-file-not-found-issue.patch b/build/external/patches/xdp-tools_1.2.9/0002-libxdp-fix-asm-types.h-file-not-found-issue.patch
new file mode 100644 (file)
index 0000000..1ba3cc5
--- /dev/null
@@ -0,0 +1,32 @@
+From 51ea3b590f5e0a6b34b5148af43996029fd60ac6 Mon Sep 17 00:00:00 2001
+From: Yulong <yulong.pei@intel.com>
+Date: Fri, 6 Jan 2023 07:46:10 +0000
+Subject: [PATCH 2/2] libxdp: fix asm/types.h file not found issue
+
+The file asm/types.h located in /usr/include/$(uname -m)-linux-gnu,
+the path string already assigned to ARCH_INCLUDES when run xdp-tools
+configure, so include ARCH_INCLUDES to CFLAGS and BPF_CFLAGS directly.
+
+Signed-off-by: Yulong <yulong.pei@intel.com>
+---
+ lib/defines.mk | 4 ++--
+ 1 file changed, 2 insertions(+), 2 deletions(-)
+
+diff --git a/lib/defines.mk b/lib/defines.mk
+index f134c43..515411c 100644
+--- a/lib/defines.mk
++++ b/lib/defines.mk
+@@ -40,8 +40,8 @@ endif
+ DEFINES += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
+-CFLAGS += -std=gnu11 -Wextra -Werror $(DEFINES)
+-BPF_CFLAGS += $(DEFINES) $(filter -ffile-prefix-map=%,$(CFLAGS))
++CFLAGS += -std=gnu11 -Wextra -Werror $(DEFINES) $(ARCH_INCLUDES)
++BPF_CFLAGS += $(DEFINES) $(filter -ffile-prefix-map=%,$(CFLAGS)) $(ARCH_INCLUDES)
+ CONFIGMK := $(LIB_DIR)/../config.mk
+ LIBMK := Makefile $(CONFIGMK) $(LIB_DIR)/defines.mk $(LIB_DIR)/common.mk $(LIB_DIR)/../version.mk
+-- 
+2.25.1
+
diff --git a/build/external/patches/xdp-tools_1.2.9/0003-libxdp-fix-maybe-uninitialized-compiler-warning.patch b/build/external/patches/xdp-tools_1.2.9/0003-libxdp-fix-maybe-uninitialized-compiler-warning.patch
new file mode 100644 (file)
index 0000000..6ab7417
--- /dev/null
@@ -0,0 +1,26 @@
+From 3033b9bdbcdb270f15373b27933d554f847e01d4 Mon Sep 17 00:00:00 2001
+From: Yulong <yulong.pei@intel.com>
+Date: Fri, 6 Jan 2023 14:31:24 +0000
+Subject: [PATCH 3/3] libxdp: fix maybe-uninitialized compiler warning
+
+Signed-off-by: Yulong <yulong.pei@intel.com>
+---
+ lib/common.mk | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git a/lib/common.mk b/lib/common.mk
+index 56c0406..f7a88a1 100644
+--- a/lib/common.mk
++++ b/lib/common.mk
+@@ -101,7 +101,7 @@ $(LIB_OBJS): %.o: %.c %.h $(LIB_H)
+ ALL_EXEC_TARGETS=$(USER_TARGETS) $(TEST_TARGETS)
+ $(ALL_EXEC_TARGETS): %: %.c  $(OBJECT_LIBBPF) $(OBJECT_LIBXDP) $(LIBMK) $(LIB_OBJS) $(KERN_USER_H) $(EXTRA_DEPS) $(EXTRA_USER_DEPS)
+-      $(QUIET_CC)$(CC) -Wall $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o $@ $(LIB_OBJS) \
++      $(QUIET_CC)$(CC) -Wall -Wno-maybe-uninitialized $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o $@ $(LIB_OBJS) \
+        $< $(LDLIBS)
+ $(XDP_OBJ): %.o: %.c $(KERN_USER_H) $(EXTRA_DEPS) $(BPF_HEADERS) $(LIBMK)
+-- 
+2.25.1
+
index cbe96aa..1097358 100644 (file)
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-vpp_find_path(BPF_INCLUDE_DIR NAMES bpf/xsk.h)
-if (NOT BPF_INCLUDE_DIR)
-  message(WARNING "libbpf headers not found - af_xdp plugin disabled")
+vpp_find_path(XDP_INCLUDE_DIR NAMES xdp/xsk.h)
+if (NOT XDP_INCLUDE_DIR)
+  message(WARNING "libxdp headers not found - af_xdp plugin disabled")
   return()
 endif()
 
 set_property(GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS TRUE)
+vpp_plugin_find_library(af_xdp XDP_LIB libxdp.a)
 vpp_plugin_find_library(af_xdp BPF_LIB libbpf.a)
-vpp_plugin_find_library(af_xdp BPF_ELF_LIB elf)
-vpp_plugin_find_library(af_xdp BPF_Z_LIB z)
-if (NOT BPF_LIB OR NOT BPF_ELF_LIB OR NOT BPF_Z_LIB)
+vpp_plugin_find_library(af_xdp ELF_LIB elf)
+vpp_plugin_find_library(af_xdp Z_LIB z)
+if (NOT XDP_LIB OR NOT BPF_LIB OR NOT ELF_LIB OR NOT Z_LIB)
   message(WARNING "af_xdp plugin - missing libraries - af_xdp plugin disabled")
   return()
 endif()
 
 set(CMAKE_REQUIRED_FLAGS "-fPIC")
-set(CMAKE_REQUIRED_INCLUDES "${BPF_INCLUDE_DIR}")
-set(CMAKE_REQUIRED_LIBRARIES "${BPF_LIB}" "${BPF_ELF_LIB}" "${BPF_Z_LIB}")
+set(CMAKE_REQUIRED_INCLUDES "${XDP_INCLUDE_DIR}")
+set(CMAKE_REQUIRED_LIBRARIES "${XDP_LIB}" "${BPF_LIB}" "${ELF_LIB}" "${Z_LIB}")
 CHECK_C_SOURCE_COMPILES("
-#include <bpf/xsk.h>
+#include <xdp/xsk.h>
 int main(void)
 {
     return xsk_socket__create (0, 0, 0, 0, 0, 0, 0);
-}" BPF_COMPILES_CHECK)
-if (NOT BPF_COMPILES_CHECK)
-  message(WARNING "af_xdp plugins - no working libbpf found - af_xdp plugin disabled")
+}" XDP_COMPILES_CHECK)
+if (NOT XDP_COMPILES_CHECK)
+message(WARNING "af_xdp plugins - no working libxdp found - af_xdp plugin disabled")
   return()
 endif()
 
-include_directories(${BPF_INCLUDE_DIR})
+include_directories(${XDP_INCLUDE_DIR})
 
 add_vpp_plugin(af_xdp
   SOURCES
@@ -65,7 +66,8 @@ add_vpp_plugin(af_xdp
   test_api.c
 
   LINK_LIBRARIES
+  ${XDP_LIB}
   ${BPF_LIB}
-  ${BPF_ELF_LIB}
-  ${BPF_Z_LIB}
+  ${ELF_LIB}
+  ${Z_LIB}
 )
index 84fc65f..cf364fc 100644 (file)
@@ -20,7 +20,7 @@
 
 #include <vlib/log.h>
 #include <vnet/interface.h>
-#include <bpf/xsk.h>
+#include <xdp/xsk.h>
 
 #define AF_XDP_NUM_RX_QUEUES_ALL        ((u16)-1)
 
index cccbf69..385a6e5 100644 (file)
@@ -176,10 +176,10 @@ af_xdp_delete_if (vlib_main_t * vm, af_xdp_device_t * ad)
     {
       int ns_fds[2];
       af_xdp_enter_netns (ad->netns, ns_fds);
-      bpf_set_link_xdp_fd (ad->linux_ifindex, -1, 0);
+      bpf_xdp_detach (ad->linux_ifindex, XDP_FLAGS_UPDATE_IF_NOEXIST, NULL);
       af_xdp_exit_netns (ad->netns, ns_fds);
 
-      bpf_object__unload (ad->bpf_obj);
+      bpf_object__close (ad->bpf_obj);
     }
 
   vec_free (ad->xsk);
@@ -198,6 +198,7 @@ static int
 af_xdp_load_program (af_xdp_create_if_args_t * args, af_xdp_device_t * ad)
 {
   int fd;
+  struct bpf_program *bpf_prog;
   struct rlimit r = { RLIM_INFINITY, RLIM_INFINITY };
 
   if (setrlimit (RLIMIT_MEMLOCK, &r))
@@ -215,27 +216,39 @@ af_xdp_load_program (af_xdp_create_if_args_t * args, af_xdp_device_t * ad)
       goto err0;
     }
 
-  if (bpf_prog_load (args->prog, BPF_PROG_TYPE_XDP, &ad->bpf_obj, &fd))
+  ad->bpf_obj = bpf_object__open_file (args->prog, NULL);
+  if (libbpf_get_error (ad->bpf_obj))
     {
       args->rv = VNET_API_ERROR_SYSCALL_ERROR_5;
-      args->error =
-       clib_error_return_unix (0, "bpf_prog_load(%s) failed", args->prog);
+      args->error = clib_error_return_unix (
+       0, "bpf_object__open_file(%s) failed", args->prog);
       goto err0;
     }
 
-  if (bpf_set_link_xdp_fd (ad->linux_ifindex, fd, 0))
+  bpf_prog = bpf_object__next_program (ad->bpf_obj, NULL);
+  if (!bpf_prog)
+    goto err1;
+
+  bpf_program__set_type (bpf_prog, BPF_PROG_TYPE_XDP);
+
+  if (bpf_object__load (ad->bpf_obj))
+    goto err1;
+
+  fd = bpf_program__fd (bpf_prog);
+
+  if (bpf_xdp_attach (ad->linux_ifindex, fd, XDP_FLAGS_UPDATE_IF_NOEXIST,
+                     NULL))
     {
       args->rv = VNET_API_ERROR_SYSCALL_ERROR_6;
-      args->error =
-       clib_error_return_unix (0, "bpf_set_link_xdp_fd(%s) failed",
-                               ad->linux_ifname);
+      args->error = clib_error_return_unix (0, "bpf_xdp_attach(%s) failed",
+                                           ad->linux_ifname);
       goto err1;
     }
 
   return 0;
 
 err1:
-  bpf_object__unload (ad->bpf_obj);
+  bpf_object__close (ad->bpf_obj);
   ad->bpf_obj = 0;
 err0:
   ad->linux_ifindex = ~0;