Skip to content

Commit

Permalink
fix compile the llvm and ubpf jit
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwei37 committed Aug 20, 2024
1 parent f849c9d commit 64d297a
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 53 deletions.
11 changes: 5 additions & 6 deletions example/libbpftime_example/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
## Example program
# Example program

This is a sample program showcasing the use of libbpftime.

I am using headers from runtime and I am linking `libbpftime.a` which is installed in `~/.bpftime`


### Prerequisites
## Prerequisites

following command should have been run, so that `libbpftime.a` exists in `~/.bpftime` directory

Expand All @@ -15,8 +14,7 @@ cmake -Bbuild -DCMAKE_BUILD_TYPE:STRING=Release \
cmake --build build --config Release --target install
```


### Build
## Build

- Makefile

Expand All @@ -34,6 +32,7 @@ Available targets:

command to execute the code:
when not using llvm-jit

```shell
g++ -o example main.cpp -I../../runtime/include -I../../vm/compat/include/ -I../../third_party/spdlog/include -I../../vm/vm-core/include -L~/.bpftime -lbpftime -lboost_system -lrt -lbpf
```
```
3 changes: 1 addition & 2 deletions tools/aot/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ set_target_properties(bpftime-aot-cli PROPERTIES OUTPUT_NAME "bpftime-aot")
target_include_directories(bpftime-aot-cli PRIVATE
${SPDLOG_INCLUDE}
${argparse_INCLUDE}
${CMAKE_CURRENT_SOURCE_DIR}/../../vm/llvm-jit/src
${CMAKE_CURRENT_SOURCE_DIR}/../vm/llvm-jit/include
${CMAKE_CURRENT_SOURCE_DIR}/../../vm/compat/include
../../runtime/include/
../../runtime/src/
${LIBBPF_INCLUDE_DIRS})
Expand Down
46 changes: 18 additions & 28 deletions tools/aot/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "compat_llvm.hpp"
#include "ebpf-vm.h"
#include "bpftime_helper_group.hpp"
#include "bpftime_prog.hpp"
Expand All @@ -9,14 +8,12 @@
#include <argparse/argparse.hpp>
#include <cstdarg>
#include <cstdint>
#include <cassert>
#include <cstdio>
#include <fcntl.h>
#include <filesystem>
#include <iostream>
#include <libelf.h>
#include <llvm/ExecutionEngine/Orc/LLJIT.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/llvm_jit_context.hpp>
#include <string>
#include <unistd.h>
#include <bpf/libbpf.h>
Expand All @@ -28,9 +25,6 @@
#include <vector>
#include <bpftime_vm_compat.hpp>

using namespace llvm;
using namespace llvm::orc;

extern "C" int _libbpf_print(libbpf_print_level level, const char *fmt,
va_list ap)
{
Expand Down Expand Up @@ -96,10 +90,10 @@ static int build_ebpf_program(const std::string &ebpf_elf,
auto helper_group = create_all_helpers();
helper_group.add_helper_group_to_prog(&bpftime_prog);
bpftime_prog.bpftime_prog_load(true);
llvm_bpf_jit_context ctx(
dynamic_cast<bpftime::vm::llvm::bpftime_llvm_vm *>(
bpftime_prog.get_vm()->vm_instance.get()));
auto result = ctx.do_aot_compile(emit_llvm_ir);
auto vm = bpftime_prog.get_vm();
// The vm instance should not be empty
assert(vm && vm->vm_instance);
auto result = vm->vm_instance->do_aot_compile(emit_llvm_ir);
auto out_path = output / (std::string(name) + ".o");
std::ofstream ofs(out_path, std::ios::binary);
ofs.write((const char *)result.data(), result.size());
Expand Down Expand Up @@ -127,11 +121,11 @@ static int compile_ebpf_program(const std::filesystem::path &output)
auto helper_group = create_all_helpers();
helper_group.add_helper_group_to_prog(&new_prog);
new_prog.bpftime_prog_load(true);
llvm_bpf_jit_context ctx(
dynamic_cast<
bpftime::vm::llvm::bpftime_llvm_vm *>(
new_prog.get_vm()->vm_instance.get()));
auto result = ctx.do_aot_compile(emit_llvm_ir);
auto vm = new_prog.get_vm();
// The vm instance should not be empty
assert(vm && vm->vm_instance);
auto result =
vm->vm_instance->do_aot_compile(emit_llvm_ir);
auto out_path = output /
(std::string(prog.name.c_str()) + ".o");
std::ofstream ofs(out_path, std::ios::binary);
Expand Down Expand Up @@ -187,9 +181,6 @@ static int load_ebpf_program(const std::filesystem::path &elf, int id)
return 0;
}

static ExitOnError exit_on_error;
using bpf_func = uint64_t (*)(const void *, uint64_t);

static int run_ebpf_program(const std::filesystem::path &elf,
std::optional<std::string> memory)
{
Expand Down Expand Up @@ -229,22 +220,21 @@ static int run_ebpf_program(const std::filesystem::path &elf,
.add_helper_group_to_prog(&bpftime_prog);
bpftime::bpftime_helper_group::get_shm_maps_helper_group()
.add_helper_group_to_prog(&bpftime_prog);
auto vm = bpftime_prog.get_vm();
auto &jit_ctx = *dynamic_cast<bpftime::vm::llvm::bpftime_llvm_vm &>(
*vm->vm_instance)
.get_jit_context();
jit_ctx.load_aot_object(file_buf);
int ret = jit_ctx.get_entry_address()(&mem, sizeof(mem));
SPDLOG_INFO("Output: {}", ret);
bpftime_prog.load_aot_object(file_buf);
uint64_t retval;
int ret = bpftime_prog.bpftime_prog_exec(&mem, sizeof(mem), &retval);
if (ret < 0) {
SPDLOG_ERROR("Failed to exec the eBPF program: {}", ret);
return 0;
}
SPDLOG_INFO("Output: {}", retval);
return 0;
}

int main(int argc, const char **argv)
{
spdlog::cfg::load_env_levels();
libbpf_set_print(_libbpf_print);
InitializeNativeTarget();
InitializeNativeTargetAsmPrinter();
argparse::ArgumentParser program(argv[0]);

argparse::ArgumentParser build_command("build");
Expand Down
11 changes: 6 additions & 5 deletions vm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@ if(NOT DEFINED SPDLOG_INCLUDE)
add_subdirectory(../third_party/spdlog ${CMAKE_CURRENT_BINARY_DIR}/spdlog)
set(SPDLOG_INCLUDE ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/spdlog/include)
endif()
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_definitions(-DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_DEBUG)
else()
add_definitions(-DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_INFO)
endif()

if(${BPFTIME_BUILD_STANDALONE_VM})
include(../cmake/libbpf.cmake)
add_subdirectory(../third_party/argparse ${CMAKE_CURRENT_BINARY_DIR}/argparse)
endif()

if(${BPFTIME_LLVM_JIT})
set(BPFTIME_BPF_RUNTIME_NAME "bpftime_llvm_vm")
else()
set(BPFTIME_BPF_RUNTIME_NAME "bpftime_ubpf_vm")
endif()

add_subdirectory(compat)
add_subdirectory(vm-core)

Expand Down
2 changes: 0 additions & 2 deletions vm/compat/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ target_include_directories(bpftime_vm_compat INTERFACE ${CMAKE_CURRENT_SOURCE_DI
if(${BPFTIME_LLVM_JIT})
message(STATUS "Using llvm-jit")
add_subdirectory(llvm-vm)
set(BPFTIME_BPF_RUNTIME_NAME "bpftime_llvm_vm")
else()
message(STATUS "Using ubpf-jit")
add_subdirectory(ubpf-vm)
set(BPFTIME_BPF_RUNTIME_NAME "bpftime_ubpf_vm")
endif()
6 changes: 3 additions & 3 deletions vm/compat/llvm-vm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
add_library(bpftime_llvm_vm STATIC ./compat_llvm.cpp)
add_library(bpftime_llvm_vm STATIC compat_llvm.cpp)

add_dependencies(bpftime_llvm_vm llvmbpf_vm spdlog::spdlog bpftime_vm_compat)

target_link_libraries(bpftime_llvm_vm PUBLIC bpftime_vm_compat spdlog::spdlog)
target_link_libraries(bpftime_llvm_vm PUBLIC bpftime_vm_compat spdlog::spdlog llvmbpf_vm)

target_include_directories(bpftime_llvm_vm PRIVATE ../../llvm-jit/inc ${CMAKE_CURRENT_SOURCE_DIR} ${SPDLOG_INCLUDE})
target_include_directories(bpftime_llvm_vm PRIVATE ../../llvm-jit/include ${CMAKE_CURRENT_SOURCE_DIR} ${SPDLOG_INCLUDE})

add_subdirectory(../../llvm-jit ${CMAKE_CURRENT_BINARY_DIR}/llvm-jit)

46 changes: 46 additions & 0 deletions vm/compat/llvm-vm/compat_llvm.cpp
Original file line number Diff line number Diff line change
@@ -1,2 +1,48 @@
#include <bpftime_vm_compat.hpp>
#include <compat_llvm.hpp>

namespace bpftime::vm::compat
{

std::unique_ptr<bpftime_vm_impl> create_vm_instance()
{
return std::make_unique<llvm::bpftime_llvm_vm>();
}

} // namespace bpftime::vm::compat

namespace bpftime::vm::llvm {

int bpftime_llvm_vm::register_external_function(size_t index, const std::string &name, void *fn) {
return bpftime::llvmbpf_vm::register_external_function(index, name, fn);
}

void bpftime_llvm_vm::unload_code() {
bpftime::llvmbpf_vm::unload_code();
}

int bpftime_llvm_vm::exec(void *mem, size_t mem_len, uint64_t &bpf_return_value) {
return bpftime::llvmbpf_vm::exec(mem, mem_len, bpf_return_value);
}

std::vector<uint8_t> bpftime_llvm_vm::do_aot_compile(bool print_ir) {
return bpftime::llvmbpf_vm::do_aot_compile(print_ir);
}

std::optional<compat::precompiled_ebpf_function> bpftime_llvm_vm::load_aot_object(const std::vector<uint8_t> &object) {
return bpftime::llvmbpf_vm::load_aot_object(object);
}

std::optional<compat::precompiled_ebpf_function> bpftime_llvm_vm::compile() {
return bpftime::llvmbpf_vm::compile();
}

void bpftime_llvm_vm::set_lddw_helpers(uint64_t (*map_by_fd)(uint32_t),
uint64_t (*map_by_idx)(uint32_t),
uint64_t (*map_val)(uint64_t),
uint64_t (*var_addr)(uint32_t),
uint64_t (*code_addr)(uint32_t)) {
bpftime::llvmbpf_vm::set_lddw_helpers(map_by_fd, map_by_idx, map_val, var_addr, code_addr);
}

} // namespace bpftime::vm::llvm
34 changes: 28 additions & 6 deletions vm/compat/llvm-vm/compat_llvm.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#ifndef _BPFTIME_VM_COMPAT_LLVM_HPP
#define _BPFTIME_VM_COMPAT_LLVM_HPP

#include <bpftime_vm_compat.hpp>
#include <memory>
#include <optional>
Expand All @@ -10,12 +11,33 @@
namespace bpftime::vm::llvm
{

class bpftime_llvm_vm : public bpftime::vm::compat::bpftime_vm_impl,
public bpftime::llvmbpf_vm {
bpftime_llvm_vm() : bpftime::llvmbpf_vm()
{
}
class bpftime_llvm_vm : public bpftime::llvmbpf_vm, public bpftime::vm::compat::bpftime_vm_impl {
public:
bpftime_llvm_vm() : bpftime::llvmbpf_vm() {}
virtual ~bpftime_llvm_vm() = default;

std::string get_error_message() override {
return bpftime::llvmbpf_vm::get_error_message();
}
int load_code(const void *code, size_t code_len) override {
return bpftime::llvmbpf_vm::load_code(code, code_len);
}
int register_external_function(size_t index, const std::string &name,
void *fn) override;
void unload_code() override;
int exec(void *mem, size_t mem_len,
uint64_t &bpf_return_value) override;
std::vector<uint8_t> do_aot_compile(bool print_ir = false) override;
std::optional<compat::precompiled_ebpf_function>
load_aot_object(const std::vector<uint8_t> &object) override;
std::optional<compat::precompiled_ebpf_function> compile() override;
void set_lddw_helpers(uint64_t (*map_by_fd)(uint32_t),
uint64_t (*map_by_idx)(uint32_t),
uint64_t (*map_val)(uint64_t),
uint64_t (*var_addr)(uint32_t),
uint64_t (*code_addr)(uint32_t)) override;
};

} // namespace bpftime::vm::llvm

#endif
#endif // _BPFTIME_VM_COMPAT_LLVM_HPP
2 changes: 1 addition & 1 deletion vm/compat/ubpf-vm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set(UBPF_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/ubpf)
include(ExternalProject)
ExternalProject_Add(
ubpf
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../third_party/ubpf
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../third_party/ubpf
BINARY_DIR ${UBPF_BUILD_DIR}
BUILD_BYPRODUCTS ${UBPF_BUILD_DIR}/lib/libubpf.a ${UBPF_BUILD_DIR}/vm/ubpf_config.h
# CMAKE_ARGS -DCMAKE_BUILD_TYPE=Debug
Expand Down
1 change: 1 addition & 0 deletions vm/compat/ubpf-vm/compat_ubpf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ std::unique_ptr<bpftime_vm_impl> create_vm_instance()
}

} // namespace bpftime::vm::compat

using namespace bpftime::vm::ubpf;

bpftime_ubpf_vm::bpftime_ubpf_vm()
Expand Down
2 changes: 2 additions & 0 deletions vm/vm-core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ add_library(bpftime_vm STATIC ./src/ebpf-vm.cpp)

target_include_directories(bpftime_vm PRIVATE ./include INTERFACE ./include)

message("The runtime is ${BPFTIME_BPF_RUNTIME_NAME}")

add_dependencies(bpftime_vm bpftime_vm_compat spdlog ${BPFTIME_BPF_RUNTIME_NAME})
target_link_libraries(bpftime_vm PUBLIC bpftime_vm_compat spdlog ${BPFTIME_BPF_RUNTIME_NAME})

0 comments on commit 64d297a

Please sign in to comment.