-
Notifications
You must be signed in to change notification settings - Fork 449
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
p4c-ubpf: new uBPF back-end for p4c (#2134)
p4c-ubpf back-end
- Loading branch information
Mihai Budiu
authored
Mar 4, 2020
1 parent
2633c15
commit 5a06a3c
Showing
132 changed files
with
12,634 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../ubpf/targets/ubpf_target.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# Copyright 2019 Orange | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# CMakefile for the uBPF P4-16 backend. | ||
|
||
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/version.h.cmake" | ||
"${CMAKE_CURRENT_BINARY_DIR}/version.h" @ONLY) | ||
|
||
set(P4C_UBPF_SOURCES | ||
p4c-ubpf.cpp | ||
ubpfBackend.cpp | ||
ubpfProgram.cpp | ||
ubpfParser.cpp | ||
ubpfDeparser.cpp | ||
ubpfControl.cpp | ||
ubpfType.cpp | ||
ubpfTable.cpp | ||
ubpfRegister.cpp | ||
ubpfModel.cpp | ||
target.cpp | ||
midend.cpp | ||
../../backends/ebpf/ebpfProgram.cpp | ||
../../backends/ebpf/ebpfTable.cpp | ||
../../backends/ebpf/ebpfParser.cpp | ||
../../backends/ebpf/ebpfControl.cpp | ||
../../backends/ebpf/ebpfOptions.cpp | ||
../../backends/ebpf/target.cpp | ||
../../backends/ebpf/codeGen.cpp | ||
../../backends/ebpf/ebpfType.cpp | ||
../../backends/ebpf/ebpfModel.cpp | ||
../../backends/ebpf/midend.cpp | ||
../../backends/ebpf/lower.cpp) | ||
|
||
set(P4C_UBPF_HEADERS | ||
codeGen.h | ||
ubpfProgram.h | ||
ubpfType.h | ||
ubpfParser.h | ||
ubpfDeparser.h | ||
ubpfControl.h | ||
ubpfRegister.h | ||
ubpfModel.h | ||
target.h | ||
midend.h | ||
ubpfBackend.h) | ||
|
||
set (P4C_UBPF_DIST_HEADERS p4include/ubpf_model.p4) | ||
|
||
add_cpplint_files(${CMAKE_CURRENT_SOURCE_DIR} "$(P4C_UBPF_SOURCES)") | ||
|
||
build_unified(P4C_UBPF_SOURCES ALL) | ||
add_executable(p4c-ubpf ${P4C_UBPF_SOURCES}) | ||
target_link_libraries (p4c-ubpf ${P4C_LIBRARIES} ${P4C_LIB_DEPS}) | ||
add_dependencies(p4c-ubpf genIR frontend) | ||
|
||
install (TARGETS p4c-ubpf | ||
RUNTIME DESTINATION ${P4C_RUNTIME_OUTPUT_DIRECTORY}) | ||
install (DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/p4include | ||
DESTINATION ${P4C_ARTIFACTS_OUTPUT_DIRECTORY}) | ||
|
||
|
||
add_custom_target(linkp4cubpf | ||
COMMAND ${CMAKE_COMMAND} -E create_symlink ${CMAKE_CURRENT_BINARY_DIR}/p4c-ubpf ${P4C_BINARY_DIR}/p4c-ubpf | ||
COMMAND ${CMAKE_COMMAND} -E make_directory ${P4C_BINARY_DIR}/p4include && | ||
${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/${P4C_UBPF_DIST_HEADERS} ${P4C_BINARY_DIR}/p4include | ||
COMMAND ${CMAKE_COMMAND} -E create_symlink ${P4C_BINARY_DIR}/p4include ${CMAKE_CURRENT_BINARY_DIR}/p4include | ||
) | ||
|
||
add_dependencies(p4c_driver linkp4cubpf) | ||
|
||
set(UBPF_DRIVER "${CMAKE_CURRENT_SOURCE_DIR}/run-ubpf-test.py -t ubpf -c \"${P4C_BINARY_DIR}/p4c-ubpf\"") | ||
set (UBPF_TEST_SUITES ${P4C_SOURCE_DIR}/testdata/p4_16_samples/*_ubpf.p4) | ||
set (UBPF_XFAIL_TESTS) | ||
p4c_add_tests("ubpf" ${UBPF_DRIVER} "${UBPF_TEST_SUITES}" "${UBPF_XFAIL_TESTS}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# Introduction to uBPF Backend | ||
|
||
The **p4c-ubpf** compiler allows to translate P4 programs into the uBPF programs. We use the uBPF implementation provided | ||
by [the P4rt-OVS switch](https://github.com/Orange-OpenSource/p4rt-ovs). The uBPF VM is based on the | ||
open-source implementation provided by [IOVisor](https://github.com/iovisor/ubpf). | ||
|
||
The P4-to-uBPF compiler accepts only the P4_16 programs written for the [ubpf_model.p4](../ubpf/p4include/ubpf_model.p4) architecture model. | ||
|
||
The backend for uBPF is mostly based on [P4-to-eBPF compiler](../ebpf/README.md). In fact, it implements the same concepts, but | ||
generates C code, which is compatible with the user space BPF implementation. | ||
|
||
## Background | ||
|
||
### P4 | ||
|
||
Please, refer to [the overview of P4 written in eBPF Backend](../ebpf#p4). | ||
|
||
### uBPF | ||
|
||
**Why uBPF?** The uBPF Virtual Machine can be used in any solution implementing the kernel bypass (e.g. DPDK apps). | ||
|
||
The uBPF project re-implements the [eBPF](../ebpf#ebpf) kernel-based Virtual Machine. While the BPF programs are | ||
intented to run in the kernel, the uBPF project enables running the BPF programs in user-space applications. It contains | ||
eBPF assembler, disassembler, interpreter, and JIT compiler for x86-64. | ||
|
||
Moreover, contrary to the eBPF implementation, uBPF is not licensed under GPL. The uBPF implementation is licensed under | ||
Apache License, version 2.0. | ||
|
||
## Compiling P4 to uBPF | ||
|
||
The scope of the uBPF backend is wider than the scope of the eBPF backend. Except for simple packet filtering the | ||
P4-to-uBPF compiler supports also P4 registers and programmable actions including packet's modifications and tunneling. For further details | ||
refer to [uBPF architecture model](p4include/ubpf_model.p4). | ||
|
||
The current version of the P4-to-uBPF compiler translates P4_16 programs to programs written in the C language. This | ||
program is compatible with the uBPF VM and the `clang` compiler can be used to generate uBPF bytecode. | ||
|
||
### Translation between P4 and C | ||
|
||
We follow the convention of the P4-to-eBPF compiler so the parser translation is presented in the table | ||
[Translating parsers](../ebpf#translating-parsers) from P4-to-eBPF. The translation of match-action pipelines is presented | ||
in the [Translating match-action pipelines](../ebpf#translating-match-action-pipelines) table from P4-to-eBPF. | ||
|
||
However, we introduced some modifications, which are listed below: | ||
|
||
* The generated code uses user-level data types (e.g. uint8_t, etc.). | ||
* Methods to extract packet fields (e.g. load_dword, etc.) have been re-implemented to use user-space data types. | ||
* The uBPF helpers are imported into the C programs. | ||
* We have added `mark_to_drop()` extern to the `ubpf` model, so that packets to drop are marked in the P4-native way. | ||
* We have added support for P4 registers implemented as BPF maps | ||
|
||
### How to use? | ||
|
||
The sample P4 programs are located in `examples/` directory. We have tested them with the [P4rt-OVS](https://github.com/Orange-OpenSource/p4rt-ovs) switch - | ||
the Open vSwitch that can be extended with BPF programs at runtime. See [the detailed tutorial](./docs/EXAMPLES.md) on how to run and test those examples. | ||
|
||
In order to generate the C code use the following command: | ||
|
||
`p4c-ubpf PROGRAM.p4 -o out.c` | ||
|
||
This command will generate out.c and the corresponding out.h file containing definitions of the packet structures and BPF maps. | ||
|
||
Once the C program is generated it can be compiled using: | ||
|
||
`clang -O2 -target bpf -c out.c -o /tmp/out.o` | ||
|
||
The output file (`out.o`) can be injected to the uBPF VM. | ||
|
||
### Known limitations | ||
|
||
* No support for some P4 constructs (meters, counters, etc.) | ||
|
||
### Contact | ||
|
||
Tomasz Osiński <[email protected]> | ||
|
||
Mateusz Kossakowski <[email protected]> | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef P4C_CODEGEN_H | ||
#define P4C_CODEGEN_H | ||
|
||
#include "lib/sourceCodeBuilder.h" | ||
#include "ebpf/codeGen.h" | ||
#include "target.h" | ||
|
||
namespace UBPF { | ||
class UbpfCodeBuilder : public EBPF::CodeBuilder { | ||
public: | ||
const UbpfTarget *target; | ||
explicit UbpfCodeBuilder(const UbpfTarget *target) : EBPF::CodeBuilder(target), target(target) {} | ||
}; | ||
} | ||
|
||
|
||
#endif //P4C_CODEGEN_H |
Oops, something went wrong.