Skip to content

Commit

Permalink
p4c-ubpf: new uBPF back-end for p4c (#2134)
Browse files Browse the repository at this point in the history
p4c-ubpf back-end
  • Loading branch information
Mihai Budiu authored Mar 4, 2020
1 parent 2633c15 commit 5a06a3c
Show file tree
Hide file tree
Showing 132 changed files with 12,634 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ install:

script:
- if [[ $TRAVIS_OS_NAME == 'linux' && $UNIFIED == ON ]] ; then docker run -w /p4c/build -e CTEST_PARALLEL_LEVEL p4c ctest --output-on-failure --schedule-random ; fi
- if [[ $TRAVIS_OS_NAME == 'osx' && $UNIFIED == ON ]] ; then ctest --output-on-failure -j 2 --schedule-random -LE "ebpf$" ; fi
- if [[ $TRAVIS_OS_NAME == 'osx' && $UNIFIED == ON ]] ; then ctest --output-on-failure -j 2 --schedule-random -LE "bpf$" ; fi
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ OPTION (ENABLE_DOCS "Build the documentation" OFF)
OPTION (ENABLE_GTESTS "Enable building and running GTest unit tests" ON)
OPTION (ENABLE_BMV2 "Build the BMV2 backend (required for the full test suite)" ON)
OPTION (ENABLE_EBPF "Build the EBPF backend (required for the full test suite)" ON)
OPTION (ENABLE_UBPF "Build the uBPF backend (required for the full test suite)" ON)
OPTION (ENABLE_P4TEST "Build the P4Test backend (required for the full test suite)" ON)
OPTION (ENABLE_P4C_GRAPHS "Build the p4c-graphs backend" ON)
OPTION (ENABLE_PROTOBUF_STATIC "Link against Protobuf statically" ON)
Expand Down Expand Up @@ -277,6 +278,9 @@ endif ()
if (ENABLE_EBPF)
add_subdirectory (backends/ebpf)
endif ()
if (ENABLE_UBPF)
add_subdirectory (backends/ubpf)
endif ()
if (ENABLE_P4TEST)
add_subdirectory (backends/p4test)
endif ()
Expand Down
2 changes: 1 addition & 1 deletion backends/ebpf/midend.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ limitations under the License.
namespace EBPF {

class MidEnd {
std::vector<DebugHook> hooks;
public:
std::vector<DebugHook> hooks;
P4::ReferenceMap refMap;
P4::TypeMap typeMap;

Expand Down
1 change: 1 addition & 0 deletions backends/ebpf/targets/ubpf_target.py
85 changes: 85 additions & 0 deletions backends/ubpf/CMakeLists.txt
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}")
82 changes: 82 additions & 0 deletions backends/ubpf/README.md
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 &lt;[email protected]&gt;

Mateusz Kossakowski &lt;[email protected]&gt;





17 changes: 17 additions & 0 deletions backends/ubpf/codeGen.h
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
Loading

0 comments on commit 5a06a3c

Please sign in to comment.