Skip to content

Commit

Permalink
vm: integrate ubpf & add compat layer for different vm implementations (
Browse files Browse the repository at this point in the history
  • Loading branch information
Officeyutong authored Apr 14, 2024
1 parent 8ae9c71 commit 237cbb5
Show file tree
Hide file tree
Showing 66 changed files with 1,001 additions and 4,643 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/test-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ jobs:
executable: ./funclatency -i 1 ./victim:plus
victim: ./victim
syscall_trace: false
expected_str: ": 1"
expected_str: "|*"
- path: libbpf-tools/mountsnoop
executable: ./mountsnoop
victim: ./victim
Expand Down Expand Up @@ -147,7 +147,10 @@ jobs:
with:
name: runtime-package-no-jit-${{matrix.container.name}}
path: ~/.bpftime

- name: Install which(required by funclatency on fedora)
if: ${{matrix.container.name=='fedora' && matrix.examples.path=='libbpf-tools/funclatency'}}
run: |
yum install -y which
- name: Set permissions
run: |
chmod +x ~/.bpftime/*
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test-llvm-jit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ jobs:
- name: build llvm JIT/AOT as a standalone library
run: |
cd vm/llvm-jit &&\
cmake -B build -DCMAKE_BUILD_TYPE=Release &&\
cmake -B build -DCMAKE_BUILD_TYPE=Release -DBPFTIME_BUILD_STANDALONE_LLVM_VM=YES &&\
cmake --build build --target all -j
- name: build vm as a standalone library
run: |
cd vm && make build-llvm -j
cd vm && make build-llvm -j
3 changes: 0 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ add_subdirectory(third_party/argparse)

# main library
add_subdirectory(vm)
if (BPFTIME_LLVM_JIT)
add_subdirectory(vm/cli)
endif()

add_subdirectory(attach)

Expand Down
4 changes: 2 additions & 2 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
add_executable(simple-benchmark-with-embed-ebpf-calling test_embed.c)

add_dependencies(simple-benchmark-with-embed-ebpf-calling vm-bpf libbpf)
add_dependencies(simple-benchmark-with-embed-ebpf-calling bpftime_vm libbpf)
target_compile_definitions(simple-benchmark-with-embed-ebpf-calling
PRIVATE
)
target_link_libraries(simple-benchmark-with-embed-ebpf-calling vm-bpf ${LIBBPF_LIBRARIES} elf z)
target_link_libraries(simple-benchmark-with-embed-ebpf-calling bpftime_vm ${LIBBPF_LIBRARIES} elf z)
target_include_directories(simple-benchmark-with-embed-ebpf-calling
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}../vm/include
Expand Down
9 changes: 6 additions & 3 deletions cmake/libbpf.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# Setup libbpf
#
set(LIBBPF_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/libbpf/)
set(LIBBPF_DIR ${CMAKE_CURRENT_LIST_DIR}/../third_party/libbpf/)
include(ExternalProject)
ExternalProject_Add(libbpf
PREFIX libbpf
Expand All @@ -20,7 +20,6 @@ ExternalProject_Add(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 SRC_DIR TARGET_DIR)
file(GLOB_RECURSE FILES RELATIVE "${SRC_DIR}" "${SRC_DIR}/*")
Expand Down Expand Up @@ -67,8 +66,12 @@ endforeach()

add_dependencies(copy_headers libbpf)

add_custom_target(libbpf_with_headers)

add_dependencies(libbpf_with_headers libbpf copy_headers)

# # Setup bpftool
set(BPFTOOL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/bpftool)
set(BPFTOOL_DIR ${CMAKE_CURRENT_LIST_DIR}/../third_party/bpftool)
set(BPFTOOL_INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/bpftool)
ExternalProject_Add(bpftool
PREFIX bpftool
Expand Down
4 changes: 3 additions & 1 deletion example/libbpf-tools/funclatency/funclatency.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ static void exit(void)
slot = log2l(delta);
if (slot >= MAX_SLOTS)
slot = MAX_SLOTS - 1;
__sync_fetch_and_add(&hist[slot], 1);
// ubpf does not support atomic instructions yet
// __sync_fetch_and_add(&hist[slot], 1);
hist[slot] += 1;
}


Expand Down
1 change: 1 addition & 0 deletions example/libbpf-tools/funclatency/funclatency.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* - support uprobes on libraries without -p PID. (parse ld.so.cache)
* - support regexp pattern matching and per-function histograms
*/
#define _GNU_SOURCE
#include <argp.h>
#include <errno.h>
#include <signal.h>
Expand Down
4 changes: 2 additions & 2 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ target_include_directories(${PROJECT_NAME}

target_link_libraries(${PROJECT_NAME}
PUBLIC
vm-bpf
bpftime_vm
spdlog::spdlog
bpftime_base_attach_impl
)
add_dependencies(${PROJECT_NAME} vm-bpf FridaGum spdlog::spdlog libbpf bpftime_base_attach_impl)
add_dependencies(${PROJECT_NAME} bpftime_vm FridaGum spdlog::spdlog libbpf bpftime_base_attach_impl)

if(BPFTIME_ENABLE_IOURING_EXT)
target_link_libraries(${PROJECT_NAME}
Expand Down
5 changes: 2 additions & 3 deletions runtime/object/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ add_library(bpftime-object OBJECT
bpf_object.cpp
)

add_dependencies(bpftime-object copy_headers)
add_dependencies(bpftime-object libbpf)
add_dependencies(bpftime-object spdlog::spdlog)
add_dependencies(bpftime-object copy_headers libbpf spdlog::spdlog bpftime_vm)

target_link_libraries(bpftime-object
PUBLIC
Expand All @@ -17,6 +15,7 @@ target_link_libraries(bpftime-object
-lelf
-lz
spdlog::spdlog
bpftime_vm
)

target_include_directories(bpftime-object PUBLIC
Expand Down
32 changes: 15 additions & 17 deletions runtime/object/bpf_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ namespace bpftime
{

// ebpf object file
class bpftime_object {
class bpftime_object
{
public:
bpftime_object(std::string_view path);
~bpftime_object() = default;
Expand All @@ -31,7 +32,7 @@ class bpftime_object {
void create_programs();
bpftime_prog *find_program_by_name(std::string_view name) const;
bpftime_prog *find_program_by_secname(std::string_view name) const;
bpftime_prog *next_program(bpftime_prog *prog) const;
bpftime_prog *next_program(bpftime_prog * prog) const;
};

bpftime_prog *bpftime_object::next_program(bpftime_prog *prog) const
Expand Down Expand Up @@ -68,7 +69,7 @@ void bpftime_object::create_programs()
const char *name = bpf_program__name(prog);
if (!insns || !name) {
SPDLOG_ERROR("Failed to get insns or name for prog {}",
name || "<NULL>");
name || "<NULL>");
continue;
}
progs.emplace_back(
Expand All @@ -81,7 +82,7 @@ bpftime_object::find_program_by_secname(std::string_view name) const
{
const char *sec_name;
struct bpf_program *prog = NULL;
struct bpftime_prog *time_prog = progs.front().get();
bpftime_prog *time_prog = progs.front().get();
// iterate through the bpftime_prog from prog and bpf_program
bpf_object__for_each_program(prog, obj.get())
{
Expand Down Expand Up @@ -125,14 +126,14 @@ bpftime_object::bpftime_object(std::string_view path)
}

// open the object elf file and load it into the context
struct bpftime_object *bpftime_object_open(const char *obj_path)
bpftime_object *bpftime_object_open(const char *obj_path)
{
struct bpftime_object *obj = new bpftime_object(obj_path);
bpftime_object *obj = new bpftime_object(obj_path);
return obj;
}

// close and free the object
void bpftime_object_close(struct bpftime_object *obj)
void bpftime_object_close(bpftime_object *obj)
{
if (!obj) {
return;
Expand All @@ -151,31 +152,28 @@ static int libbpf_print_fn(enum libbpf_print_level level, const char *format,
}

// The execution unit or bpf function.
struct bpftime_prog;
class bpftime_prog;
// find the program by section name
struct bpftime_prog *
bpftime_object_find_program_by_name(struct bpftime_object *obj,
const char *name)
class bpftime_prog *bpftime_object_find_program_by_name(bpftime_object *obj,
const char *name)
{
if (!obj || !name) {
return NULL;
}
return obj->find_program_by_name(name);
}

struct bpftime_prog *
bpftime_object_find_program_by_secname(struct bpftime_object *obj,
const char *secname)
class bpftime_prog *bpftime_object_find_program_by_secname(bpftime_object *obj,
const char *secname)
{
if (!obj || !secname) {
return NULL;
}
return obj->find_program_by_secname(secname);
}

struct bpftime_prog *
bpftime_object__next_program(const struct bpftime_object *obj,
struct bpftime_prog *prog)
class bpftime_prog *bpftime_object__next_program(const bpftime_object *obj,
class bpftime_prog *prog)
{
if (!obj) {
return NULL;
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/handler/handler_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ handler_manager::handler_manager(managed_shared_memory &mem,
handler_manager::~handler_manager()
{
for (std::size_t i = 0; i < handlers.size(); i++) {
SPDLOG_ERROR(
SPDLOG_TRACE(
"Handler at {} is not destroyed, but handler_manager is being destroyed",
i);
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/unit-test/assets/filter.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ enum filter_op {
};

static const int (*test_pass_param)(char *str, char c,
long long parm1) = (void *)4097;
long long parm1) = (void *)40;

uint64_t BPF_UPROBE(my_function, char *str, char c, long long parm1)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ TEST_CASE("Test attaching filter program with ebpf, and reverting")
REQUIRE(bpftime_helper_group::get_kernel_utils_helper_group()
.add_helper_group_to_prog(prog) >= 0);
bpftime_helper_info info = {
.index = 4097,
.index = 40,
.name = "test_pass_param",
.fn = (void *)__bpftime_attach_filter_with_ebpf__test_pass_param,
};
Expand Down
2 changes: 1 addition & 1 deletion runtime/unit-test/maps/test_external_map_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ TEST_CASE("Test basic operations of external hash map ops")
uint32_t key = i;
auto val = *(uint64_t *)(map_handler.map_lookup_elem(&key));
REQUIRE(val == i);
spdlog::info("val for {} = {:x}", i, val);
spdlog::debug("val for {} = {:x}", i, val);
}
}
6 changes: 3 additions & 3 deletions runtime/unit-test/maps/test_shm_hash_maps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static void test_lookup_map(int fd, bpftime::handler_manager &manager_ref,
uint32_t key = i;
auto val = *(uint64_t *)(map_handler.map_lookup_elem(&key));
REQUIRE(val == ((((uint64_t)key) << 32) | 0xffffffff));
spdlog::info("val for {} = {:x}", i, val);
spdlog::debug("val for {} = {:x}", i, val);
}
}

Expand All @@ -58,10 +58,10 @@ static void test_get_next_element(int fd, bpftime::handler_manager &manager_ref,
uint32_t key = 0;
uint32_t next_key = 0;
while (map_handler.bpf_map_get_next_key(&key, &next_key) == 0) {
spdlog::info("key = {}, next_key = {}", key, next_key);
spdlog::debug("key = {}, next_key = {}", key, next_key);
key = next_key;
}
spdlog::info("key = {}, next_key = {}", key, next_key);
spdlog::debug("key = {}, next_key = {}", key, next_key);
}

static void handle_sub_process()
Expand Down
4 changes: 2 additions & 2 deletions tools/aot/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ target_include_directories(bpftime-aot-cli PRIVATE
../../runtime/include/
../../runtime/src/
${LIBBPF_INCLUDE_DIRS})
target_link_libraries(bpftime-aot-cli PRIVATE spdlog::spdlog argparse vm-bpf runtime ${LIBBPF_LIBRARIES} elf z)
target_link_libraries(bpftime-aot-cli PRIVATE spdlog::spdlog argparse bpftime_vm_compat bpftime_llvm_jit_vm runtime ${LIBBPF_LIBRARIES} elf z)
set_property(TARGET bpftime-aot-cli PROPERTY CXX_STANDARD 20)

target_compile_definitions(bpftime-aot-cli PRIVATE _GNU_SOURCE)

add_dependencies(bpftime-aot-cli spdlog::spdlog argparse vm-bpf libbpf)
add_dependencies(bpftime-aot-cli spdlog::spdlog argparse bpftime_vm_compat bpftime_llvm_jit_vm libbpf)

install(TARGETS bpftime-aot-cli CONFIGURATIONS Release Debug RelWithDebInfo DESTINATION ~/.bpftime)
Loading

0 comments on commit 237cbb5

Please sign in to comment.