Open yunwei37 opened 2 years ago
Would you please give me some feedback or suggestions? If you think it's worth trying, I can continue to do more implements and researches on this issue.
What are expected changes in bcc/libbpf-tools? Just makefile options or your have something more. If just makefile options you probably don't need to modify bcc/libbpf-tools makefile as you can just compile libbpf tool with different make options. Due to long-time maintenance, I would like to see what changes are needed in bcc/libbpf-tools. If there are no changes needed, I think you have total freedom to create a webassembly-libbpf repo...
Thanks for the suggestions!
What are expected changes in bcc/libbpf-tools?
I think I may need to change in bcc/libbpf-tools:
clang
and bpftool
, with different options. The original clang cannot support compile to wasm target. (In fact this may not need to modified the makefile greatly, but I have something more than this...)And I would create an libbpf-wasm
repo outside of bcc/libbpf-tools
, which may content the libbpf C headers modified for porting libbpf to wasm, so this would have nothing to do with bcc repo.
The main difficulty in combining wasm and ebpf may be that the memory layout of WASM is different from that of eBPF programs, and the structure of C language cannot be directly mapped, so any transfer structures must be serialized.
For example, the event submitted in the kernel:
bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, &data, sizeof(data));
The event cannot be directly read in user space wasm vm like this, event both the wasm code and eBPF code is compiled from C:
static void handle_event(void *ctx, int cpu, void *data, __u32 data_size)
{
struct str_t *e = data;
struct tm *tm;
char ts[16];
printf("%-9s %-7d %s\n", ts, e->pid, e->str);
}
I will go and prepare for more detail designs and some draft implements later. Thanks for you time!
Hey, @yunwei37, I am the author of that draft PR (there's another one that's already merged) and a BumbleBee contributor. Let me know if there's any update on this idea, I like it!
This is a minimal working runtime and toolchain example based on libbpf and WAMR, with only 300+ lines in runtime host implementation:
https://github.com/eunomia-bpf/wasm-bpf
A WebAssembly eBPF library and runtime powered by CO-RE(Compile Once – Run Everywhere) libbpf and WAMR.
General purpose
: provide most abilities from eBPF to Wasm, polling
from the ring buffer or perf buffer, bidirectional communications between kernel
eBPF and userspace
Wasm using maps
, dynamically loading
, attaching
or detaching
, etc. Supports a large number of eBPF program types and map types, covering the use cases from tracing
, networking
, security
.High performance
: No serialization
overhead for complex data types, using shared memory
to avoid copy overhead between host and Wasm.Easy to use
: provide a similar developing experience as the libbpf-bootstrap, auto generate
the Wasm-eBPF skeleton
headers and type
definitions for bindings.Ultralightweight
: the runtime has only 300+
lines of code, binary only 1.5 MB
in size. Compiled Wasm module would be only ~90K
.
Hi, community!
I propose that maybe we can try to compile the
libbpf-tools
C code to OCI-compatible WebAssembly module, and then use a launcher to get eBPF programs running from the cloud to the kernel in 1 line of bash?Just like what bumblebee has done, but with the help of WebAssembly:
rewrite
the existing libbpf-tools or do a lot ofcode modification
, we can use the existing C code and compile it to WasmCompile once, run everywhere
). We have tested some tools onx86
andarm
to prove it.plugin
in other systems, like what Envoy has done: WebAssembly-in-Envoyother languages
(Go, Rust, Java, TypeScript, etc.), Over 30 programming languages can be compiled to WebAssembly modules.As far as I know, Bumblebee seems cannot support running on Arm: Fail installation on ARM and document supported architecture.
We have created a
prove-of-concept
library and a demo tool to show how to compile libbpf base tools to Wasm and run it:Background
What is Wasm?
What is WASI?
The compile workflow
The workflow to compile a
libbpf-tool
toWasm
maybe like this:bpftool
WASI
supportlauncher
to load the Wasm module in user space: export some helper functions to the WebAssembly runtime, and then load the bpf object into the kernel from the wasm module with the helper functions.What needs to be done?
What maybe need to be done in libbpf-tools next if we want to support wasm target:
WASI
does not have any modules for executing eBPF programs. In other words, we need to create an abstraction layer between the WebAssembly Runtime and the libbpf library.argp
to wasm, becauseWASI
only has standard c library supported.libbpf
APIs to our ABI interfaces between libbpf and WebAssembly runtime.Some small problems may need to be fixed, but I think we may no need to do many code modifications.
some usage examples
Take the
sigsnoop
tool as an example, we can use the following command to run it from the cloud:Or you can compile the
sigsnoop
tool locally to Wasm and run it: