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

Update directory structure & CI #33

Merged
merged 7 commits into from
Feb 21, 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
14 changes: 10 additions & 4 deletions .github/workflows/c-cpp.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Ubuntu
name: "c-cpp"

on:
push:
Expand All @@ -20,8 +20,14 @@ jobs:
sudo make install-deps
make /opt/wasi-sdk
- name: make
run: make
run: make -C runtime/cpp
- name: make build-lib
run: make build-lib
run: make -C runtime/cpp build-lib
- name: make test
run: make test
run: make -C runtime/cpp test
- name: Build wasm-bpf
run: sudo make build-cpp
- name: Upload build result
uses: actions/[email protected]
with:
path: "wasm-bpf"
13 changes: 10 additions & 3 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ jobs:
sudo make install-deps
make /opt/wasi-sdk
- name: make
run: make
- name: examples
run: make -C examples
run: make -C runtime/cpp
- name: Install rust stable
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
- name: examples with rust runtime
run: IMPL=rust make -C examples
- name: examples with cpp runtime
run: IMPL=cpp make -C examples
26 changes: 26 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Rust

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

env:
CARGO_TERM_COLOR: always

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Install dependencies
run: sudo make install-deps
- name: Build
run: make build-rust
- name: Upload build result
uses: actions/[email protected]
with:
path: "wasm-bpf-rs"
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
build/
examples/wasm-bpf
wasi-sdk-*-linux.tar.gz

wasm-bpf
wasm-bpf-rs
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[submodule "third_party/bpftool"]
path = third_party/bpftool
path = runtime/cpp/third_party/bpftool
url = https://github.com/eunomia-bpf/bpftool
[submodule "third_party/wasm-micro-runtime"]
path = third_party/wasm-micro-runtime
path = runtime/cpp/third_party/wasm-micro-runtime
url = https://github.com/bytecodealliance/wasm-micro-runtime
90 changes: 15 additions & 75 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,85 +1,25 @@
.PHONY: install coverage test docs help generate_tools build
.DEFAULT_GOAL := build
build: build-rust build-cpp

define BROWSER_PYSCRIPT
import os, webbrowser, sys
build-rust:
$(MAKE) -C runtime/rust build
cp ./runtime/rust/target/release/wasm-bpf-rs .

try:
from urllib import pathname2url
except:
from urllib.request import pathname2url
build-cpp:
$(MAKE) -C runtime/cpp build
cp ./runtime/cpp/build/bin/Release/wasm-bpf .

webbrowser.open("file://" + pathname2url(os.path.abspath(sys.argv[1])))
endef
export BROWSER_PYSCRIPT

define PRINT_HELP_PYSCRIPT
import re, sys

for line in sys.stdin:
match = re.match(r'^([a-zA-Z_-]+):.*?## (.*)$$', line)
if match:
target, help = match.groups()
print("%-20s %s" % (target, help))
endef
export PRINT_HELP_PYSCRIPT

BROWSER := python -c "$$BROWSER_PYSCRIPT"
INSTALL_LOCATION := ~/.local

build: ## build as a tool
rm -rf build/
cmake -Bbuild -DCMAKE_BUILD_TYPE=Release # -Dwasm-bpf_ENABLE_ASAN=1
cmake --build build --config Release

build-lib: ## build as a library
rm -rf build/
cmake -Bbuild -DCMAKE_BUILD_TYPE=Release -Dwasm-bpf_BUILD_EXECUTABLE=0 # -Dwasm-bpf_ENABLE_ASAN=1
cmake --build build --config Release

build-lib: ## build as a library
rm -rf build/
cmake -Bbuild -DCMAKE_BUILD_TYPE=Release -Dwasm-bpf_BUILD_EXECUTABLE=0 # -Dwasm-bpf_ENABLE_ASAN=1
cmake --build build --config Release

help:
@python -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST)
clean:
$(MAKE) -C examples clean
$(MAKE) -C runtime/cpp clean
$(MAKE) -C runtime/rust clean
rm -rf wasm-bpf-rs
rm -rf wasm-bpf

install-deps: ## install deps
apt update
apt-get install libcurl4-openssl-dev libelf-dev clang llvm -y ## libgtest-dev

test: ## run tests quickly with ctest
sudo rm -rf build/
cmake -Bbuild -DCMAKE_INSTALL_PREFIX=$(INSTALL_LOCATION) -Dwasm-bpf_ENABLE_UNIT_TESTING=1 -Dwasm-bpf_ENABLE_ASAN=1 -Dwasm-bpf_ENABLE_CODE_COVERAGE=1
cmake --build build
cd build/ && sudo ctest -VV
apt-get install libcurl4-openssl-dev libelf-dev clang llvm pahole -y ## libgtest-dev

/opt/wasi-sdk:
wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-17/wasi-sdk-17.0-linux.tar.gz
tar -zxf wasi-sdk-17.0-linux.tar.gz
sudo mkdir -p /opt/wasi-sdk/ && sudo mv wasi-sdk-17.0/* /opt/wasi-sdk/

test-wasm: /opt/wasi-sdk
cd test/wasm-apps && ./build.sh

coverage: ## check code coverage quickly GCC
rm -rf build/
cmake -Bbuild -DCMAKE_INSTALL_PREFIX=$(INSTALL_LOCATION) -Dwasm-bpf_ENABLE_CODE_COVERAGE=1
cmake --build build --config Release
cd build/ && ctest -C Release -VV
cd .. && (bash -c "find . -type f -name '*.gcno' -exec gcov -pb {} +" || true)

install: ## install the package to the `INSTALL_LOCATION`
rm -rf build/
cmake -Bbuild -DCMAKE_INSTALL_PREFIX=$(INSTALL_LOCATION) -DCMAKE_BUILD_TYPE=Release -Dwasm-bpf_ENABLE_UNIT_TESTING=0
cmake --build build --config Release
cmake --build build --target install --config Release

format: ## format the project sources
cmake -Bbuild
cmake --build build --target clang-format

clean: ## clean the project build files
rm -rf build/
rm -rf docs/
sudo mkdir -p /opt/wasi-sdk/ && sudo mv wasi-sdk-17.0/* /opt/wasi-sdk/
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![logo](test/asserts/logo.png)
![logo](docs/logo.png)

# 📦 Wasm-bpf: Wasm library and toolchain for eBPF

Expand Down Expand Up @@ -26,16 +26,16 @@ For tools to distribute Wasm-eBPF programs in [`OCI`](https://opencontainers.org
The wasm-bpf runtime require two parts: `the host side`(Outside the Wasm runtime) and the `Wasm guest side`(Inside the Wasm runtime).

- host side: A simple runtime implementation example
- see [src](src) and [include](include) directories, which would be a sample runtime built on the top of [libbpf](https://github.com/libbpf/libbpf) and [WAMR](https://github.com/bytecodealliance/wasm-micro-runtime).
- see [src](runtime/cpp/src) and [include](runtime/cpp/include) directories, which would be a sample runtime built on the top of [libbpf](https://github.com/libbpf/libbpf) and [WAMR](https://github.com/bytecodealliance/wasm-micro-runtime).
- You can easily build your own Wasm-eBPF runtime in `any` languages, `any` eBPF libraries and `any` Wasm runtimes with the same System interface.
- wasm side: toolchains and libraries
- a [`libbpf-wasm`](wasm-sdk/libbpf-wasm.h) header only library to provide libbpf APIs for Wasm guest `C/C++` code.
- a [`libbpf-wasm`](wasm-sdk/c/libbpf-wasm.h) header only library to provide libbpf APIs for Wasm guest `C/C++` code.
- a [`bpftool`](https://github.com/eunomia-bpf/bpftool/tree/wasm-bpftool) tool to generate the Wasm-eBPF `skeleton` headers, and `C struct definitions` for passing data between the host and Wasm guest without serialization.
- More languages support(`Rust`, `Go`, etc) is on the way.

For details compile process, please refer to the [examples/bootstrap/README.md](examples/bootstrap/README.md). The figure below shows the overall interaction between the eBPF and Wasm runtimes:

![wasi-bpf](test/asserts/wasm-bpf-no-bcc.png)
![wasi-bpf](docs/wasm-bpf-no-bcc.png)

A Wasm module could load and control multiple eBPF programs at the same time, and can call another Wasm module written in other languages to process the data or control with [the component model](https://github.com/WebAssembly/component-model).

Expand Down
File renamed without changes
File renamed without changes
File renamed without changes
35 changes: 29 additions & 6 deletions examples/Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
.PHONY: all bootstrap runqlat
all: test

clean:
$(MAKE) -C bootstrap clean
$(MAKE) -C runqlat clean
$(MAKE) -C sockfilter clean

TEST_TIME := 3

Expand All @@ -14,7 +10,34 @@ test: $(TEST_CASES_DIRS)

.PHONY:$(TEST_CASES_DIRS)
# build the test cases
$(TEST_CASES_DIRS):
cp ../build/bin/Release/wasm-bpf .
$(TEST_CASES_DIRS): wasm-bpf
make -C $@
make -C $@ test

clean:
for name in $(TEST_CASES_DIRS); do \
$(MAKE) -C $$name clean; \
done
rm -rf wasm-bpf

wasm-bpf:
case $$IMPL in \
cpp) \
make wasm-bpf-cpp \
;; \
rust) \
make wasm-bpf-rs \
;; \
*) \
echo "\e[31mNo runtime specified. Set env variable IMPL to either \"rust\" or \"cpp\" to specify which runtime to use\e[0m\n" \
exit 1 \
;; \
esac

wasm-bpf-cpp:
make -C ../runtime/cpp
cp ../runtime/cpp/build/bin/Release/wasm-bpf .

wasm-bpf-rs:
cd ../runtime/rust && cargo build --release
cp ../runtime/rust/target/release/wasm-bpf-rs wasm-bpf
13 changes: 8 additions & 5 deletions examples/bootstrap/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
.PHONY: all

ARCH ?= $(shell uname -m | sed 's/x86_64/x86/' | sed 's/aarch64/arm64/' | sed 's/ppc64le/powerpc/' | sed 's/mips.*/mips/')
VMLINUX := ../../third_party/vmlinux/$(ARCH)/vmlinux.h
BPF_HEADERS := ../../third_party/
THIRD_PARTY := ../../runtime/cpp/third_party

VMLINUX := $(THIRD_PARTY)/vmlinux/$(ARCH)/vmlinux.h
BPF_HEADERS := $(THIRD_PARTY)/
# Use our own libbpf API headers and Linux UAPI headers distributed with
# libbpf to avoid dependency on system-wide headers, which could be missing or
# outdated
Expand All @@ -11,7 +13,7 @@ CFLAGS := -g -Wall
ALL_LDFLAGS := $(LDFLAGS) $(EXTRA_LDFLAGS)
CLANG := clang
LLVM_STRIP := llvm-strip
BPFTOOL_SRC := ../../third_party/bpftool/src
BPFTOOL_SRC := $(THIRD_PARTY)/bpftool/src
BPFTOOL := $(BPFTOOL_SRC)/bpftool


Expand Down Expand Up @@ -55,12 +57,13 @@ $(APP).wasm.h: $(APP).bpf.o $(BPFTOOL)

# compile for wasm with wasi-sdk
WASI_CLANG = /opt/wasi-sdk/bin/clang
WASI_CFLAGS = -O2 --sysroot=/opt/wasi-sdk/share/wasi-sysroot -Wl,--allow-undefined
WASI_CFLAGS = -O2 --sysroot=/opt/wasi-sdk/share/wasi-sysroot -Wl,--allow-undefined,--export-table

$(APP).wasm: $(APP).c $(APP).skel.h
ln -f -s ../../wasm-sdk/c/libbpf-wasm.h libbpf-wasm.h
$(WASI_CLANG) $(WASI_CFLAGS) -o $@ $<

TEST_TIME := 3
.PHONY: test
test:
sudo timeout --preserve-status -s 2 $(TEST_TIME) ../wasm-bpf $(APP).wasm
sudo timeout -s 2 $(TEST_TIME) ../wasm-bpf $(APP).wasm || if [ $$? = 124 ]; then exit 0; else exit $$?; fi
2 changes: 1 addition & 1 deletion examples/bootstrap/libbpf-wasm.h
13 changes: 8 additions & 5 deletions examples/lsm/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
.PHONY: all

ARCH ?= $(shell uname -m | sed 's/x86_64/x86/' | sed 's/aarch64/arm64/' | sed 's/ppc64le/powerpc/' | sed 's/mips.*/mips/')
VMLINUX := ../../third_party/vmlinux/$(ARCH)/vmlinux.h
BPF_HEADERS := ../../third_party/
THIRD_PARTY := ../../runtime/cpp/third_party

VMLINUX := $(THIRD_PARTY)/vmlinux/$(ARCH)/vmlinux.h
BPF_HEADERS := $(THIRD_PARTY)/
# Use our own libbpf API headers and Linux UAPI headers distributed with
# libbpf to avoid dependency on system-wide headers, which could be missing or
# outdated
Expand All @@ -11,7 +13,7 @@ CFLAGS := -g -Wall
ALL_LDFLAGS := $(LDFLAGS) $(EXTRA_LDFLAGS)
CLANG := clang
LLVM_STRIP := llvm-strip
BPFTOOL_SRC := ../../third_party/bpftool/src
BPFTOOL_SRC := $(THIRD_PARTY)/bpftool/src
BPFTOOL := $(BPFTOOL_SRC)/bpftool


Expand Down Expand Up @@ -55,12 +57,13 @@ $(APP).wasm.h: $(APP).bpf.o $(BPFTOOL)

# compile for wasm with wasi-sdk
WASI_CLANG = /opt/wasi-sdk/bin/clang
WASI_CFLAGS = -O2 --sysroot=/opt/wasi-sdk/share/wasi-sysroot -Wl,--allow-undefined
WASI_CFLAGS = -O2 --sysroot=/opt/wasi-sdk/share/wasi-sysroot -Wl,--allow-undefined,--export-table

$(APP).wasm: $(APP).c $(APP).skel.h
ln -f -s ../../wasm-sdk/c/libbpf-wasm.h libbpf-wasm.h
$(WASI_CLANG) $(WASI_CFLAGS) -o $@ $<

TEST_TIME := 3
.PHONY: test
test:
sudo timeout --preserve-status -s 2 $(TEST_TIME) ../wasm-bpf $(APP).wasm
sudo timeout -s 2 $(TEST_TIME) ../wasm-bpf $(APP).wasm || if [ $$? = 124 ]; then exit 0; else exit $$?; fi
2 changes: 1 addition & 1 deletion examples/lsm/libbpf-wasm.h
13 changes: 8 additions & 5 deletions examples/runqlat/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
.PHONY: all

ARCH ?= $(shell uname -m | sed 's/x86_64/x86/' | sed 's/aarch64/arm64/' | sed 's/ppc64le/powerpc/' | sed 's/mips.*/mips/')
VMLINUX := ../../third_party/vmlinux/$(ARCH)/vmlinux.h
BPF_HEADERS := ../../third_party/
THIRD_PARTY := ../../runtime/cpp/third_party

VMLINUX := $(THIRD_PARTY)/vmlinux/$(ARCH)/vmlinux.h
BPF_HEADERS := $(THIRD_PARTY)/
# Use our own libbpf API headers and Linux UAPI headers distributed with
# libbpf to avoid dependency on system-wide headers, which could be missing or
# outdated
Expand All @@ -11,7 +13,7 @@ CFLAGS := -g -Wall
ALL_LDFLAGS := $(LDFLAGS) $(EXTRA_LDFLAGS)
CLANG := clang
LLVM_STRIP := llvm-strip
BPFTOOL_SRC := ../../third_party/bpftool/src
BPFTOOL_SRC := $(THIRD_PARTY)/bpftool/src
BPFTOOL := $(BPFTOOL_SRC)/bpftool


Expand Down Expand Up @@ -55,9 +57,10 @@ $(APP).wasm.h: $(APP).bpf.o $(BPFTOOL)

# compile for wasm with wasi-sdk
WASI_CLANG = /opt/wasi-sdk/bin/clang
WASI_CFLAGS = -O2 --sysroot=/opt/wasi-sdk/share/wasi-sysroot -Wl,--allow-undefined
WASI_CFLAGS = -O2 --sysroot=/opt/wasi-sdk/share/wasi-sysroot -Wl,--allow-undefined,--export-table

$(APP).wasm: $(APP).c $(APP).skel.h
ln -f -s ../../wasm-sdk/c/libbpf-wasm.h libbpf-wasm.h
$(WASI_CLANG) $(WASI_CFLAGS) -o $@ $< trace_helpers.c

# install emadk
Expand All @@ -69,4 +72,4 @@ emsdk:
TEST_TIME := 3
.PHONY: test
test:
sudo timeout --preserve-status -s 2 $(TEST_TIME) ../wasm-bpf $(APP).wasm
sudo timeout -s 2 $(TEST_TIME) ../wasm-bpf $(APP).wasm || if [ $$? = 124 ]; then exit 0; else exit $$?; fi
2 changes: 1 addition & 1 deletion examples/runqlat/libbpf-wasm.h
Loading