diff --git a/Makefile b/Makefile index 277554c6..a7781027 100644 --- a/Makefile +++ b/Makefile @@ -48,6 +48,12 @@ build: ## build the package cmake -Bbuild -DBPFTIME_ENABLE_UNIT_TESTING=1 cmake --build build --config Debug cd tools/cli-rs && cargo build + +build-iouring: ## build the package + cmake -Bbuild -DBPFTIME_ENABLE_UNIT_TESTING=0 -DBPFTIME_ENABLE_IOURING_EXT=1 -DCMAKE_BUILD_TYPE:STRING=Release + cmake --build build --config Release + cd tools/cli-rs && cargo build + release-without-cli: cmake -Bbuild -DBPFTIME_ENABLE_UNIT_TESTING=0 \ -DCMAKE_BUILD_TYPE:STRING=Release \ diff --git a/cmake/StandardSettings.cmake b/cmake/StandardSettings.cmake index a5cab558..4e81cead 100644 --- a/cmake/StandardSettings.cmake +++ b/cmake/StandardSettings.cmake @@ -68,3 +68,8 @@ option(BPFTIME_ENABLE_MPK "Enable Memory Protection Keys to detect memory error. if(BPFTIME_ENABLE_MPK) add_definitions(-DBPFTIME_ENABLE_MPK) endif() + +option(BPFTIME_ENABLE_IOURING_EXT "Enable iouring helpers." OFF) +if(BPFTIME_ENABLE_IOURING_EXT) + add_definitions(-DBPFTIME_ENABLE_IOURING_EXT) +endif() diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index e6c9645b..1e19a141 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -78,6 +78,8 @@ set(sources src/bpf_map/shared/hash_map_kernel_user.cpp src/bpf_map/shared/percpu_array_map_kernel_user.cpp src/bpf_map/shared/perf_event_array_kernel_user.cpp + + extension/extension_helper.cpp ) # list(APPEND sources @@ -126,6 +128,13 @@ target_link_libraries(${PROJECT_NAME} ) add_dependencies(${PROJECT_NAME} vm-bpf FridaGum syscall_id_table spdlog::spdlog libbpf) +if (BPFTIME_ENABLE_IOURING_EXT) +target_link_libraries(${PROJECT_NAME} + PUBLIC + uring +) +endif() + if(${ENABLE_EBPF_VERIFIER}) target_include_directories(${PROJECT_NAME} PRIVATE ${BPFTIME_VERIFIER_INCLUDE}) target_link_libraries(${PROJECT_NAME} PRIVATE bpftime-verifier) diff --git a/runtime/extension/extension_helper.cpp b/runtime/extension/extension_helper.cpp new file mode 100644 index 00000000..765ea04f --- /dev/null +++ b/runtime/extension/extension_helper.cpp @@ -0,0 +1,192 @@ +#include "bpftime_helper_group.hpp" +#include +#include +#include +#ifdef ENABLE_BPFTIME_VERIFIER +#include "bpftime-verifier.hpp" +#endif +#include "spdlog/spdlog.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include "bpftime.hpp" +#include "bpftime_shm.hpp" +#include "bpftime_internal.h" +#include +#include + +#ifdef BPFTIME_ENABLE_IOURING_EXT +#include "liburing.h" +#endif + +using namespace std; + +#if BPFTIME_ENABLE_FS_HELPER +uint64_t bpftime_get_abs_path(const char *filename, const char *buffer, + uint64_t size) +{ + auto path = std::filesystem::absolute(filename); + return (uint64_t)(uintptr_t)strncpy((char *)(uintptr_t)buffer, + path.c_str(), size); +} + +uint64_t bpftime_path_join(const char *filename1, const char *filename2, + const char *buffer, uint64_t size) +{ + auto path = std::filesystem::path(filename1) / + std::filesystem::path(filename2); + return (uint64_t)(uintptr_t)strncpy((char *)(uintptr_t)buffer, + path.c_str(), size); +} +#endif + +namespace bpftime +{ +#ifdef BPFTIME_ENABLE_IOURING_EXT + +static int submit_io_uring_write(struct io_uring *ring, int fd, char *buf, + size_t size) +{ + struct io_uring_sqe *sqe; + + sqe = io_uring_get_sqe(ring); + if (!sqe) { + return 1; + } + io_uring_prep_write(sqe, fd, buf, size, -1); + sqe->user_data = 1; + + return 0; +} + +static int submit_io_uring_fsync(struct io_uring *ring, int fd) +{ + struct io_uring_sqe *sqe; + + sqe = io_uring_get_sqe(ring); + if (!sqe) { + return 1; + } + + io_uring_prep_fsync(sqe, fd, IORING_FSYNC_DATASYNC); + sqe->user_data = 2; + + return 0; +} + +static int io_uring_init(struct io_uring *ring) +{ + int ret = io_uring_queue_init(1024, ring, IORING_SETUP_SINGLE_ISSUER); + if (ret) { + return 1; + } + return 0; +} + +static int io_uring_wait_and_seen(struct io_uring *ring, + struct io_uring_cqe *cqe) +{ + int ret = io_uring_wait_cqe(ring, &cqe); + if (ret < 0) { + return ret; + } + io_uring_cqe_seen(ring, cqe); + return 0; +} + +static struct io_uring ring; + +uint64_t io_uring_init_global(void) +{ + return io_uring_init(&ring); +} + +uint64_t bpftime_io_uring_submit_write(int fd, char *buf, size_t size) +{ + return submit_io_uring_write(&ring, fd, buf, size); +} + +uint64_t bpftime_io_uring_submit_fsync(int fd) +{ + return submit_io_uring_fsync(&ring, fd); +} + +uint64_t bpftime_io_uring_wait_and_seen(void) +{ + struct io_uring_cqe *cqe = nullptr; + return io_uring_wait_and_seen(&ring, cqe); +} + +uint64_t bpftime_io_uring_submit(void) +{ + return io_uring_submit(&ring); +} +#endif + +extern const bpftime_helper_group extesion_group = { { + { FFI_HELPER_ID_FIND_ID, + bpftime_helper_info{ + .index = FFI_HELPER_ID_FIND_ID, + .name = "__ebpf_call_find_ffi_id", + .fn = (void *)__ebpf_call_find_ffi_id, + } }, + { FFI_HELPER_ID_DISPATCHER, + bpftime_helper_info{ + .index = FFI_HELPER_ID_DISPATCHER, + .name = "__ebpf_call_ffi_dispatcher", + .fn = (void *)__ebpf_call_ffi_dispatcher, + } }, +#if BPFTIME_ENABLE_FS_HELPER + { EXTENDED_HELPER_GET_ABS_PATH_ID, + bpftime_helper_info{ + .index = EXTENDED_HELPER_GET_ABS_PATH_ID, + .name = "bpftime_get_abs_path", + .fn = (void *)bpftime_get_abs_path, + } }, + { EXTENDED_HELPER_PATH_JOIN_ID, + bpftime_helper_info{ + .index = EXTENDED_HELPER_PATH_JOIN_ID, + .name = "bpftime_path_join", + .fn = (void *)bpftime_path_join, + } }, +#endif +#ifdef BPFTIME_ENABLE_IOURING_EXT + { EXTENDED_HELPER_IOURING_INIT, + bpftime_helper_info{ + .index = EXTENDED_HELPER_IOURING_INIT, + .name = "io_uring_init", + .fn = (void *)io_uring_init_global, + } }, + { EXTENDED_HELPER_IOURING_SUBMIT_WRITE, + bpftime_helper_info{ + .index = EXTENDED_HELPER_IOURING_SUBMIT_WRITE, + .name = "io_uring_submit_write", + .fn = (void *)bpftime_io_uring_submit_write, + } }, + { EXTENDED_HELPER_IOURING_SUBMIT_FSYNC, + bpftime_helper_info{ + .index = EXTENDED_HELPER_IOURING_SUBMIT_FSYNC, + .name = "io_uring_submit_fsync", + .fn = (void *)bpftime_io_uring_submit_fsync, + } }, + { EXTENDED_HELPER_IOURING_WAIT_AND_SEEN, + bpftime_helper_info{ + .index = EXTENDED_HELPER_IOURING_WAIT_AND_SEEN, + .name = "io_uring_wait_and_seen", + .fn = (void *)bpftime_io_uring_wait_and_seen, + } }, + { EXTENDED_HELPER_IOURING_SUBMIT, + bpftime_helper_info{ + .index = EXTENDED_HELPER_IOURING_SUBMIT, + .name = "io_uring_submit", + .fn = (void *)bpftime_io_uring_submit, + } }, +#endif +} }; + +} // namespace bpftime diff --git a/runtime/include/bpftime_ffi.hpp b/runtime/include/bpftime_ffi.hpp index 51360260..3ce57336 100644 --- a/runtime/include/bpftime_ffi.hpp +++ b/runtime/include/bpftime_ffi.hpp @@ -8,6 +8,12 @@ #define EXTENDED_HELPER_GET_ABS_PATH_ID 1003 #define EXTENDED_HELPER_PATH_JOIN_ID 1004 +#define EXTENDED_HELPER_IOURING_INIT 1006 +#define EXTENDED_HELPER_IOURING_SUBMIT_WRITE 1007 +#define EXTENDED_HELPER_IOURING_SUBMIT_FSYNC 1008 +#define EXTENDED_HELPER_IOURING_WAIT_AND_SEEN 1009 +#define EXTENDED_HELPER_IOURING_SUBMIT 1010 + namespace bpftime { class base_attach_manager; diff --git a/runtime/src/bpf_helper.cpp b/runtime/src/bpf_helper.cpp index a4afef27..2d27fd12 100644 --- a/runtime/src/bpf_helper.cpp +++ b/runtime/src/bpf_helper.cpp @@ -581,49 +581,7 @@ const bpftime_helper_group shm_maps_group = { { } }, } }; -uint64_t bpftime_get_abs_path(const char *filename, const char *buffer, - uint64_t size) -{ - auto path = std::filesystem::absolute(filename); - return (uint64_t)(uintptr_t)strncpy((char *)(uintptr_t)buffer, - path.c_str(), size); -} - -uint64_t bpftime_path_join(const char *filename1, const char *filename2, - const char *buffer, uint64_t size) -{ - auto path = std::filesystem::path(filename1) / - std::filesystem::path(filename2); - return (uint64_t)(uintptr_t)strncpy((char *)(uintptr_t)buffer, - path.c_str(), size); -} - -const bpftime_helper_group extesion_group = { { - { FFI_HELPER_ID_FIND_ID, - bpftime_helper_info{ - .index = FFI_HELPER_ID_FIND_ID, - .name = "__ebpf_call_find_ffi_id", - .fn = (void *)__ebpf_call_find_ffi_id, - } }, - { FFI_HELPER_ID_DISPATCHER, - bpftime_helper_info{ - .index = FFI_HELPER_ID_DISPATCHER, - .name = "__ebpf_call_ffi_dispatcher", - .fn = (void *)__ebpf_call_ffi_dispatcher, - } }, - { EXTENDED_HELPER_GET_ABS_PATH_ID, - bpftime_helper_info{ - .index = EXTENDED_HELPER_GET_ABS_PATH_ID, - .name = "bpftime_get_abs_path", - .fn = (void *)bpftime_get_abs_path, - } }, - { EXTENDED_HELPER_PATH_JOIN_ID, - bpftime_helper_info{ - .index = EXTENDED_HELPER_PATH_JOIN_ID, - .name = "bpftime_path_join", - .fn = (void *)bpftime_path_join, - } }, -} }; +extern const bpftime_helper_group extesion_group; const bpftime_helper_group kernel_helper_group = { { { BPF_FUNC_probe_read, diff --git a/runtime/unit-test/attach/test_uprobe_uretprobe.cpp b/runtime/unit-test/attach/test_uprobe_uretprobe.cpp index 245df171..9e296123 100644 --- a/runtime/unit-test/attach/test_uprobe_uretprobe.cpp +++ b/runtime/unit-test/attach/test_uprobe_uretprobe.cpp @@ -12,7 +12,7 @@ using namespace bpftime; -__attribute__((__noinline__)) extern "C" uint64_t __test_simple_add(uint64_t a, +extern "C" __attribute__((__noinline__)) uint64_t __test_simple_add(uint64_t a, uint64_t b) { // Forbid inline