Skip to content

Commit

Permalink
fix compile as a standalone library
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwei37 committed Jan 23, 2024
1 parent 8a2ca8f commit 29f5e19
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 23 deletions.
9 changes: 9 additions & 0 deletions vm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ build: ## build the package
cmake -Bbuild -DBPFTIME_ENABLE_UNIT_TESTING=1
cmake --build build --config Debug

build-ubpf: ## build the package with ubpf backend
rm -rf build/
cmake -Bbuild
cmake --build build --config Release

build-llvm: ## build with llvm as jit backend
cmake -Bbuild -DBPFTIME_LLVM_JIT=1
cmake --build build --config Release

build-arm64: ## build the package on arm64
rm -rf build/
cmake -Bbuild -DCMAKE_TOOLCHAIN_FILE=../cmake/aarch64-toolchain.cmake -DARCH=aarch64 -DBPFTIME_ENABLE_UNIT_TESTING=1
Expand Down
26 changes: 15 additions & 11 deletions vm/README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
# bpftime vm: userspace eBPF vm with JIT support

The bpf vm and jit for eBPF usersapce runtime.
The bpf vm and JIT/AOT for eBPF usersapce runtime.

you can choose from llvm-jit and a simple-jit/interpreter based on ubpf.
The JIT can be built as a standalone library and integrated into other projects.
You can also try the cli tool to compile and run AOT eBPF programs.

## LLVM jit for eBPF

see [llvm-jit/README.md](llvm-jit/README.md)
see [llvm-jit/README.md](llvm-jit/README.md).

## a simple jit modified from ubpf
You can build the llvm JIT/AOT for eBPF as a standalone library:

see [simple-jit/README.md](simple-jit/README.md)
```sh
sudo apt install llvm-15-dev
cd llvm-jit
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build --target all -j
```

## build
## a simple jit modified from ubpf

The JIT can be built as a standalone library and integrated into other projects.
see [simple-jit/README.md](simple-jit/README.md)

In `vm` directory, run:

```sh
make build
cmake -Bbuild
cmake --build build --config Release
```

## Example Usage
Expand All @@ -38,7 +46,3 @@ Usage: build/vm/cli/bpftime-cli <path to ebpf instructions> [path to memory for
## benchmark

see [github.com/eunomia-bpf/bpf-benchmark](https://github.com/eunomia-bpf/bpf-benchmark) for how we evaluate and details.

## Roadmap

- [ ] AOT support for LLVM JIT
4 changes: 4 additions & 0 deletions vm/cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <unistd.h>
#include <bpf/libbpf.h>
#include <fstream>

using namespace llvm;
using namespace llvm::orc;

Expand Down Expand Up @@ -77,8 +78,10 @@ static int build_ebpf_program(const std::string &ebpf_elf,
}
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 @@ -148,6 +151,7 @@ int main(int argc, const char **argv)
.nargs(0, 1);
program.add_subparser(build_command);
program.add_subparser(run_command);

try {
program.parse_args(argc, argv);
} catch (const std::exception &err) {
Expand Down
1 change: 1 addition & 0 deletions vm/llvm-jit/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ build
/.vscode
/.cache
test*
libvm-bpf.a
16 changes: 13 additions & 3 deletions vm/llvm-jit/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
cmake_minimum_required(VERSION 3.21)

project(
"llvm-bpf-jit"
LANGUAGES C CXX
Expand All @@ -8,8 +10,6 @@ if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Debug")
endif()

cmake_minimum_required(VERSION 3.15)

function(bpftime_setup_target target)
set_property(TARGET ${target} PROPERTY CXX_STANDARD 20)
target_include_directories(${target}
Expand Down Expand Up @@ -54,6 +54,13 @@ llvm_map_components_to_libnames(LLVM_LIBS
nativecodegen
)

if(NOT DEFINED SPDLOG_INCLUDE)
message(INFO "Adding spdlo seperately..")
# spdlog
add_subdirectory(../../third_party/spdlog ${CMAKE_CURRENT_BINARY_DIR}/spdlog)
set(SPDLOG_INCLUDE ../../third_party/spdlog/include)
endif()

message(STATUS "LLVM_LIBS=${LLVM_LIBS}")

target_link_libraries(vm-bpf ${LLVM_LIBS} spdlog::spdlog)
Expand All @@ -75,7 +82,10 @@ target_link_libraries(vm-llvm-bpf-test vm-bpf)

target_include_directories(vm-bpf PUBLIC ../include include)

add_subdirectory(unit-test)
if(BPFTIME_ENABLE_UNIT_TESTING)
message(STATUS "Build unit tests for the project. Tests should always be found in the test folder\n")
add_subdirectory(unit-test)
endif()

include(FetchContent)
FetchContent_Declare(
Expand Down
4 changes: 0 additions & 4 deletions vm/llvm-jit/Makefile

This file was deleted.

10 changes: 5 additions & 5 deletions vm/llvm-jit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ A faster and better support on different architectures jit based on LLVM.

## build

```
sudo apt install llvm-dev
```sh
sudo apt install llvm-15-dev
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build --target all -j
```

## run

```
build/vm-llvm
```sh
build/vm-llvm-example
```

## Test with bpf-conformance
Expand Down Expand Up @@ -51,5 +51,5 @@ PASS: "tests/stxdw.data"
PASS: "tests/stxh.data"
PASS: "tests/stxw.data"
PASS: "tests/subnet.data"
Passed 147 out of 166 tests.
Passed 165 out of 166 tests.
```
1 change: 1 addition & 0 deletions vm/llvm-jit/example/main-bpf-conformance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <iostream>
#include <vector>
#include "ebpf-vm.h"

/**
* @brief Read in a string of hex bytes and return a vector of bytes.
*
Expand Down
4 changes: 4 additions & 0 deletions vm/llvm-jit/src/llvm/llvm_jit_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <picosha2.h>
#include <boost/interprocess/sync/file_lock.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>

using namespace llvm;
using namespace llvm::orc;
using namespace bpftime;
Expand Down Expand Up @@ -144,6 +145,7 @@ ensure_aot_cache_dir_and_cache_file()

return { dir, cache_lock };
}

static std::optional<std::vector<uint8_t> >
load_aot_cache(const std::filesystem::path &path)
{
Expand All @@ -167,6 +169,7 @@ load_aot_cache(const std::filesystem::path &path)
return buffer;
}
}

ebpf_jit_fn llvm_bpf_jit_context::compile()
{
spin_lock_guard guard(compiling.get());
Expand Down Expand Up @@ -386,6 +389,7 @@ llvm_bpf_jit_context::create_and_initialize_lljit_instance()
ExitOnErr(mainDylib.define(absoluteSymbols(lddwSyms)));
return { std::move(jit), extFuncNames, definedLddwHelpers };
}

ebpf_jit_fn llvm_bpf_jit_context::get_entry_address()
{
if (!this->jit.has_value()) {
Expand Down
1 change: 1 addition & 0 deletions vm/llvm-jit/unit-test/llvm-aot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extern "C" uint64_t add_func(uint64_t a, uint64_t b, uint64_t, uint64_t,
{
return a + b;
}

TEST_CASE("Test aot compilation")
{
ebpf_vm *vm = ebpf_create();
Expand Down

0 comments on commit 29f5e19

Please sign in to comment.