Add a eBPF-capable PCIe device
RFC PATCH 1/1 Add a eBPF-capable PCIe device
This RFC adds a PCIe device model that is capable of running machine code complaint with the extended Berkeley Packet Filter (eBPF) virtual machine [1]. This machine can then be used to run offloaded code in the PCIe device against data passed to the PCIe device via DMA. eBPF is a very well supported “virtual” ISA and has an increasingly level of support within the Linux kernel. As such, it makes a good choice as a vendor-agnostic ISA for application acceleration via PCIe offload engines [2]. This RFC is useful for exploring this idea in an emulated environment without needing physical PCIe attached eBPF offload engines.
The eBPF device model leverages a userspace eBPF C library (uBPF) [3] in order to implement the model and this is therefore a dependency when building QEMU. If this uBPF library is detected at configuration time and --disable_libubpf is not set then this model will be enabled when QEMU is built.
Documentation on how to program the eBPF PCIe device is included in docs/specs/ebpf.txt.
A sample Linux kernel driver can be found in [4]. Note that this driver also exposes a section of one of the BARs on the eBPF PCIe device as a p2pdma capable memory [5] and as such requires a 4.20.x or more recent kernel. This is done to explore the ability of combining the eBPF offload engine with p2pdma to build more efficient systems.
Note that this RFC is just a starting point for a PCIe eBPF model. The programming model for the device is not as efficient as it could be right now. In the future we plan to add more parallelism and the ability to handle multiple DMA requests. Long term we may add a NVMe controller interface to this PCIe device to align with work being by SNIA and NVM Express around NVMe Computational Storage [6]. Suggestions for other enhancements are very welcome!
A simple “smoke-test” userspace application that utilizes both the kernel driver and the PCIe model is available [7]. It shows how a C program can be compiled to eBPF in user-space, pushed to the PCIe device and be used to operate on data DMA’ed in from an NVMe SSD.
This patch applies to release v4.0.0 of QEMU.
[1]: https://www.kernel.org/doc/Documentation/networking/filter.txt
[2]: https://lkml.org/lkml/2019/2/7/647
[3]: https://github.com/iovisor/ubpf
[4]: https://github.com/iomartin/pci_ubpf_driver
[5]: https://www.kernel.org/doc/Documentation/driver-api/pci/p2pdma.rst
[6]: https://www.snia.org/computational
[7]: https://github.com/iomartin/ebpf-test
--- configure | 27 +++ docs/specs/ebpf.txt | 182 +++++++++++++++ hw/misc/Makefile.objs | 3 + hw/misc/pcie-ebpf.c | 513 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 725 insertions(+) create mode 100644 docs/specs/ebpf.txt create mode 100644 hw/misc/pcie-ebpf.c
configure +27 -0
@@ -491,6 +491,7 @@ docker="no" debug_mutex="no" libpmem="" default_devices="yes" +libubpf="" # cross compilers defaults, can be overridden with --cross-cc-ARCH cross_cc_aarch64="aarch64-linux-gnu-gcc"
@@ -1518,6 +1519,10 @@ for opt do ;; --disable-libpmem) libpmem=no ;; + --disable-libubpf) libubpf="no" + ;; + --enable-libubpf) libubpf="yes" + ;; *) echo "ERROR: unknown option $opt" echo "Try '$0 --help' for more information"
@@ -3974,6 +3979,24 @@ elif test "$tpm" = "yes"; then fi fi +########################################## +# libubpf probe + +if test "$libubpf" != "no" ; then + cat > $TMPC <<EOF +#include <ubpf.h> +int main(void) { ubpf_create(); return 0; } +EOF + if compile_prog "" "-lubpf" ; then + libubpf=yes + else + if test "$libubpf" = "yes" ; then + feature_not_found "libubpf" "Install libubpf" + fi + libubpf=no + fi +fi + ########################################## # attr probe
@@ -6438,6 +6461,7 @@ echo "docker $docker" echo "libpmem support $libpmem" echo "libudev $libudev" echo "default devices $default_devices" +echo "libubpf $libubpf" if test "$supported_cpu" = "no"; then echo
@@ -6833,6 +6857,9 @@ fi if test "$linux_aio" = "yes" ; then echo "CONFIG_LINUX_AIO=y" >> $config_host_mak fi +if test "$libubpf" = "yes" ; then + echo "CONFIG_LIBUBPF=y" >> $config_host_mak +fi if test "$attr" = "yes" ; then echo "CONFIG_ATTR=y" >> $config_host_mak fi
docs/specs/ebpf.txt new file +182 -0
182 lines not on this page · raw message
hw/misc/Makefile.objs +3 -0
@@ -8,6 +8,7 @@ common-obj-$(CONFIG_ISA_TESTDEV) += pc-testdev.o common-obj-$(CONFIG_PCI_TESTDEV) += pci-testdev.o common-obj-$(CONFIG_EDU) += edu.o common-obj-$(CONFIG_PCA9552) += pca9552.o +common-obj-$(CONFIG_LIBUBPF) += pcie-ebpf.o common-obj-y += unimp.o common-obj-$(CONFIG_FW_CFG_DMA) += vmcoreinfo.o
@@ -77,3 +78,5 @@ obj-$(CONFIG_AUX) += auxbus.o obj-$(CONFIG_ASPEED_SOC) += aspeed_scu.o aspeed_sdmc.o obj-$(CONFIG_MSF2) += msf2-sysreg.o obj-$(CONFIG_NRF51_SOC) += nrf51_rng.o + +pcie-ebpf.o-libs := -lubpf
hw/misc/pcie-ebpf.c new file +513 -0
513 lines not on this page · raw message
Signature · 1 line
2.21.0
On Mon, Jun 03, 2019 at 02:58:26PM -0600, Martin Ichilevici de Oliveira wrote:
Thanks for sharing!
The bpf_ram accesses are unsafe. The guest can modify bpf_ram while the device is accessing it. This is likely to cause security problems.
I think a model is required where the device copies in the program and additional data before processing. This way the guest cannot modify it while the device is executing the program.
Also, please validate inputs. The guest is untrusted. Offsets, sizes, etc cannot be trusted and must be bounds-checked.