From e0c3ae7f5f57a38d00d973537e81d8775dde8045 Mon Sep 17 00:00:00 2001 From: Officeyutong Date: Sun, 29 Oct 2023 00:38:14 +0900 Subject: [PATCH] Fix #48 (libbpf building issue) (#58) * Revert changes Try fixing Update Fix building of old libbpf * Fix CI * Fix CI * Fix CI * Remove bundled libbpf; use bpftool instead --- .gitmodules | 3 --- cmake/libbpf.cmake | 17 +++++++------ daemon/CMakeLists.txt | 1 - daemon/test/CMakeLists.txt | 25 +++++++++---------- daemon/user/bpftime_driver.cpp | 4 +-- daemon/user/handle_bpf_event.cpp | 2 +- example/malloc/malloc.c | 2 +- example/sslsniff/sslsniff.bpf.c | 8 ++++++ runtime/src/bpf_map/array_map_kernel_user.cpp | 2 +- runtime/src/bpf_map/hash_map_kernel_user.cpp | 2 +- runtime/test/bpf/replace.bpf.c | 7 ++++++ runtime/unit-test/assets/filter.bpf.c | 7 ++++++ runtime/unit-test/assets/replace.bpf.c | 7 ++++++ runtime/unit-test/assets/uprobe.bpf.c | 9 +++++++ third_party/bpftool | 2 +- third_party/libbpf | 2 +- 16 files changed, 67 insertions(+), 33 deletions(-) mode change 160000 => 120000 third_party/libbpf diff --git a/.gitmodules b/.gitmodules index 023e70b5..8efb3c8b 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,3 @@ -[submodule "third_party/libbpf"] - path = third_party/libbpf - url = https://github.com/libbpf/libbpf.git [submodule "third_party/bpftool"] path = third_party/bpftool url = https://github.com/libbpf/bpftool diff --git a/cmake/libbpf.cmake b/cmake/libbpf.cmake index 11716824..dbc65e07 100644 --- a/cmake/libbpf.cmake +++ b/cmake/libbpf.cmake @@ -16,40 +16,41 @@ ExternalProject_Add(libbpf # Set BpfObject input parameters -- note this is usually not necessary unless # you're in a highly vendored environment (like libbpf-bootstrap) -set(LIBBPF_INCLUDE_DIRS ${CMAKE_CURRENT_BINARY_DIR}/libbpf) +set(LIBBPF_INCLUDE_DIRS ${CMAKE_CURRENT_BINARY_DIR}/libbpf/) set(LIBBPF_LIBRARIES ${CMAKE_CURRENT_BINARY_DIR}/libbpf/libbpf.a) set(header_output_list) -function(copy_header TARGET SRC_DIR TARGET_DIR) +function(copy_header SRC_DIR TARGET_DIR) file(GLOB_RECURSE FILES RELATIVE "${SRC_DIR}" "${SRC_DIR}/*") message(STATUS "copying ${FILES} from ${SRC_DIR} to ${TARGET_DIR}") foreach(file ${FILES}) get_filename_component(PARENT_DIR "${TARGET_DIR}/${file}" DIRECTORY) add_custom_command( - PRE_BUILD COMMAND ${CMAKE_COMMAND} -E make_directory ${PARENT_DIR} COMMAND ${CMAKE_COMMAND} -E copy ${SRC_DIR}/${file} ${TARGET_DIR}/${file} - COMMENT "Copying file ${HEADER_DIRS}/${file} to ${TARGET_DIR}/${file}" - BYPRODUCTS ${TARGET_DIR}/${file} + COMMENT "Copying file ${SRC_DIR}/${file} to ${TARGET_DIR}/${file}" OUTPUT ${TARGET_DIR}/${file} DEPENDS ${SRC_DIR}/${file} ) list(APPEND header_output_list ${TARGET_DIR}/${file}) + set(header_output_list ${header_output_list} PARENT_SCOPE) endforeach() endfunction() -copy_header(copy_headers "${LIBBPF_DIR}/include/linux" "${LIBBPF_INCLUDE_DIRS}/linux") -copy_header(copy_headers "${LIBBPF_DIR}/include/uapi/linux" "${LIBBPF_INCLUDE_DIRS}/linux") - +copy_header("${LIBBPF_DIR}/include/linux" "${LIBBPF_INCLUDE_DIRS}/linux") +copy_header("${LIBBPF_DIR}/include/uapi/" "${LIBBPF_INCLUDE_DIRS}/uapi") +copy_header("${LIBBPF_DIR}/include/uapi/linux" "${LIBBPF_INCLUDE_DIRS}/linux") add_custom_target(copy_headers ALL COMMENT "Copying headers" DEPENDS ${header_output_list} ) +message(STATUS "All headers to copy: ${header_output_list}") + set(HEADER_FILES relo_core.h hashmap.h nlattr.h libbpf_internal.h) foreach(file ${HEADER_FILES}) diff --git a/daemon/CMakeLists.txt b/daemon/CMakeLists.txt index 2ddd65df..f79000a9 100644 --- a/daemon/CMakeLists.txt +++ b/daemon/CMakeLists.txt @@ -25,7 +25,6 @@ add_dependencies(libbpftime_daemon target_include_directories(libbpftime_daemon PRIVATE ${CMAKE_CURRENT_BINARY_DIR} - ${LIBBPF_INCLUDE_DIRS}/uapi ${LIBBPF_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/../vm/include diff --git a/daemon/test/CMakeLists.txt b/daemon/test/CMakeLists.txt index 726c09ee..be5363d5 100644 --- a/daemon/test/CMakeLists.txt +++ b/daemon/test/CMakeLists.txt @@ -17,17 +17,17 @@ set(TEST_SOURCES add_executable(bpftime_daemon_tests ${TEST_SOURCES}) set_property(TARGET bpftime_daemon_tests PROPERTY CXX_STANDARD 20) add_dependencies(bpftime_daemon_tests runtime bpftime-object) -target_link_libraries(bpftime_daemon_tests PRIVATE -runtime -bpftime-object -Catch2::Catch2WithMain -libbpftime_daemon +target_link_libraries(bpftime_daemon_tests PRIVATE + runtime + bpftime-object + Catch2::Catch2WithMain + libbpftime_daemon ) -target_include_directories(bpftime_daemon_tests +target_include_directories(bpftime_daemon_tests PRIVATE - ${BPFTIME_RUNTIME_INCLUDE} - ${BPFTIME_OBJECT_INCLUDE_DIRS} - ${Catch2_INCLUDE} + ${BPFTIME_RUNTIME_INCLUDE} + ${BPFTIME_OBJECT_INCLUDE_DIRS} + ${Catch2_INCLUDE} ${Boost_INCLUDE} ../ ) @@ -35,10 +35,9 @@ add_test(NAME bpftime_daemon_tests COMMAND bpftime_daemon_tests) # These are necessary ebpf program required by the test # set(used_ebpf_programs -# uprobe -# # replace # Not used now -# filter) - +# uprobe +# # replace # Not used now +# filter) foreach(current ${used_ebpf_programs}) set(curr_target_name bpftime_test_ebpf_prog_${current}) add_ebpf_program_target(${curr_target_name} ${CMAKE_CURRENT_SOURCE_DIR}/assets/${current}.bpf.c ${CMAKE_CURRENT_BINARY_DIR}/${current}.bpf.o) diff --git a/daemon/user/bpftime_driver.cpp b/daemon/user/bpftime_driver.cpp index 377d0c42..2c09a770 100644 --- a/daemon/user/bpftime_driver.cpp +++ b/daemon/user/bpftime_driver.cpp @@ -126,7 +126,7 @@ int bpftime_driver::check_and_create_prog_related_maps( map_ids.resize(info->nr_map_ids); new_info.nr_map_ids = info->nr_map_ids; new_info.map_ids = (unsigned long long)(uintptr_t)map_ids.data(); - int res = bpf_prog_get_info_by_fd(fd, &new_info, &info_len); + int res = bpf_obj_get_info_by_fd(fd, &new_info, &info_len); if (res < 0) { spdlog::error( "Failed to get prog info for fd {} to find related maps", @@ -204,7 +204,7 @@ int bpftime_driver::bpftime_maps_create_server(int kernel_id) } bpf_map_info info = {}; uint32_t info_len = sizeof(info); - int res = bpf_map_get_info_by_fd(map_fd, &info, &info_len); + int res = bpf_obj_get_info_by_fd(map_fd, &info, &info_len); if (res < 0) { spdlog::error("Failed to get map info for id {}", kernel_id); return -1; diff --git a/daemon/user/handle_bpf_event.cpp b/daemon/user/handle_bpf_event.cpp index a9d8f0c9..cc70f840 100644 --- a/daemon/user/handle_bpf_event.cpp +++ b/daemon/user/handle_bpf_event.cpp @@ -380,4 +380,4 @@ bpf_event_handler::bpf_event_handler(struct daemon_config config, } perf_type_id_strings[kprobe_type] = "PERF_TYPE_KPROBE"; -} \ No newline at end of file +} diff --git a/example/malloc/malloc.c b/example/malloc/malloc.c index 29da2230..78604bd1 100644 --- a/example/malloc/malloc.c +++ b/example/malloc/malloc.c @@ -98,7 +98,7 @@ int main(int argc, char **argv) goto cleanup; } LIBBPF_OPTS(bpf_uprobe_opts, attach_opts, .func_name = "malloc", - .retprobe = false, .attach_mode = PROBE_ATTACH_MODE_PERF); + .retprobe = false); struct bpf_link *attach = bpf_program__attach_uprobe_opts( skel->progs.do_count, -1, "/lib/x86_64-linux-gnu/libc.so.6", 0, &attach_opts); diff --git a/example/sslsniff/sslsniff.bpf.c b/example/sslsniff/sslsniff.bpf.c index ed6d2559..28fe966d 100644 --- a/example/sslsniff/sslsniff.bpf.c +++ b/example/sslsniff/sslsniff.bpf.c @@ -10,6 +10,14 @@ #include #include "sslsniff.h" +#ifndef BPF_UPROBE +#define BPF_UPROBE BPF_KPROBE +#endif +#ifndef BPF_URETPROBE +#define BPF_URETPROBE BPF_KRETPROBE +#endif + + struct { __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY); __uint(key_size, sizeof(__u32)); diff --git a/runtime/src/bpf_map/array_map_kernel_user.cpp b/runtime/src/bpf_map/array_map_kernel_user.cpp index 7061e896..359a9b94 100644 --- a/runtime/src/bpf_map/array_map_kernel_user.cpp +++ b/runtime/src/bpf_map/array_map_kernel_user.cpp @@ -34,7 +34,7 @@ void array_map_kernel_user_impl::init_map_fd() } bpf_map_info info = {}; unsigned int info_len = sizeof(info); - int res = bpf_map_get_info_by_fd(map_fd, &info, &info_len); + int res = bpf_obj_get_info_by_fd(map_fd, &info, &info_len); if (res < 0) { spdlog::error("Failed to get info for kernel map id {}", kernel_map_id); diff --git a/runtime/src/bpf_map/hash_map_kernel_user.cpp b/runtime/src/bpf_map/hash_map_kernel_user.cpp index 8bd1630a..9ec4c4df 100644 --- a/runtime/src/bpf_map/hash_map_kernel_user.cpp +++ b/runtime/src/bpf_map/hash_map_kernel_user.cpp @@ -19,7 +19,7 @@ void hash_map_kernel_user_impl::init_map_fd() } bpf_map_info info = {}; unsigned int info_len = sizeof(info); - int res = bpf_map_get_info_by_fd(map_fd, &info, &info_len); + int res = bpf_obj_get_info_by_fd(map_fd, &info, &info_len); if (res < 0) { spdlog::error("Failed to get info for kernel map id {}", kernel_map_id); diff --git a/runtime/test/bpf/replace.bpf.c b/runtime/test/bpf/replace.bpf.c index 706de783..306956cb 100644 --- a/runtime/test/bpf/replace.bpf.c +++ b/runtime/test/bpf/replace.bpf.c @@ -3,6 +3,13 @@ #include "bpf/bpf_helpers.h" #include "ffi.bpf.h" +#ifndef BPF_UPROBE +#define BPF_UPROBE BPF_KPROBE +#endif +#ifndef BPF_URETPROBE +#define BPF_URETPROBE BPF_KRETPROBE +#endif + static inline int add_func(int a, int b) { return FFI_CALL_NAME_2("add_func", a, b); diff --git a/runtime/unit-test/assets/filter.bpf.c b/runtime/unit-test/assets/filter.bpf.c index cef7985f..77ce0e05 100644 --- a/runtime/unit-test/assets/filter.bpf.c +++ b/runtime/unit-test/assets/filter.bpf.c @@ -3,6 +3,13 @@ #include "bpf/bpf_helpers.h" #include "ffi.bpf.h" +#ifndef BPF_UPROBE +#define BPF_UPROBE BPF_KPROBE +#endif +#ifndef BPF_URETPROBE +#define BPF_URETPROBE BPF_KRETPROBE +#endif + enum PatchOp { OP_SKIP, OP_RESUME, diff --git a/runtime/unit-test/assets/replace.bpf.c b/runtime/unit-test/assets/replace.bpf.c index 706de783..306956cb 100644 --- a/runtime/unit-test/assets/replace.bpf.c +++ b/runtime/unit-test/assets/replace.bpf.c @@ -3,6 +3,13 @@ #include "bpf/bpf_helpers.h" #include "ffi.bpf.h" +#ifndef BPF_UPROBE +#define BPF_UPROBE BPF_KPROBE +#endif +#ifndef BPF_URETPROBE +#define BPF_URETPROBE BPF_KRETPROBE +#endif + static inline int add_func(int a, int b) { return FFI_CALL_NAME_2("add_func", a, b); diff --git a/runtime/unit-test/assets/uprobe.bpf.c b/runtime/unit-test/assets/uprobe.bpf.c index 96f50aad..a8c72f1e 100644 --- a/runtime/unit-test/assets/uprobe.bpf.c +++ b/runtime/unit-test/assets/uprobe.bpf.c @@ -3,6 +3,15 @@ #include #include +#ifndef BPF_UPROBE +#define BPF_UPROBE BPF_KPROBE +#endif +#ifndef BPF_URETPROBE +#define BPF_URETPROBE BPF_KRETPROBE +#endif + + + SEC("uprobe") int BPF_UPROBE(my_function_uprobe, int parm1, char* str, char c) { bpf_printk("BPF_UPROBE: %d %s %c\n", parm1, str, c); diff --git a/third_party/bpftool b/third_party/bpftool index 88156afd..b01941c8 160000 --- a/third_party/bpftool +++ b/third_party/bpftool @@ -1 +1 @@ -Subproject commit 88156afd0fb486fe1a54cefe0dd3b0b744fcec61 +Subproject commit b01941c8f7890489f09713348a7d89567538504b diff --git a/third_party/libbpf b/third_party/libbpf deleted file mode 160000 index e26b84dc..00000000 --- a/third_party/libbpf +++ /dev/null @@ -1 +0,0 @@ -Subproject commit e26b84dc330c9644c07428c271ab491b0f01f4e1 diff --git a/third_party/libbpf b/third_party/libbpf new file mode 120000 index 00000000..5573f0b5 --- /dev/null +++ b/third_party/libbpf @@ -0,0 +1 @@ +./bpftool/libbpf/ \ No newline at end of file