Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extension: add iouring helpers #115

Merged
merged 3 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
5 changes: 5 additions & 0 deletions cmake/StandardSettings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
9 changes: 9 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
192 changes: 192 additions & 0 deletions runtime/extension/extension_helper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
#include "bpftime_helper_group.hpp"
#include <cassert>
#include <cerrno>
#include <sched.h>
#ifdef ENABLE_BPFTIME_VERIFIER
#include "bpftime-verifier.hpp"
#endif
#include "spdlog/spdlog.h"
#include <map>
#include <stdio.h>
#include <stdarg.h>
#include <cstring>
#include <time.h>
#include <unistd.h>
#include <ctime>
#include <filesystem>
#include "bpftime.hpp"
#include "bpftime_shm.hpp"
#include "bpftime_internal.h"
#include <spdlog/spdlog.h>
#include <vector>

#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
6 changes: 6 additions & 0 deletions runtime/include/bpftime_ffi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
44 changes: 1 addition & 43 deletions runtime/src/bpf_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion runtime/unit-test/attach/test_uprobe_uretprobe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading