![]() |
P4C
The P4 Compiler
|
P4C is a reference compiler for the P4 programming language. It supports both P4-14 and P4-16; you can find more information about P4 here and the specifications for both versions of the language here. One fact attesting to the level of quality and completeness of P4C's code is that its front-end code, mid-end code, and P4C-graphs back end are used as the basis for at least one commercially supported P4 compiler.
P4C is modular; it provides a standard frontend and midend which can be combined with a target-specific backend to create a complete P4 compiler. The goal is to make adding new backends easy.
The P4C compiler is a compiler infrastructure for the P4 compiler designed with the following goals:
P4C includes seven sample backends, catering to different target architectures and use cases:
simple_switch written using the BMv2 behavioral model,Compile P4_16 or P4_14 source code. If your program successfully compiles, the command will create files with the same base name as the P4 program you supplied, and the following suffixes instead of the .p4:
.p4i, which is the output from running the preprocessor on your P4 program..json that is the JSON file format expected by BMv2 behavioral model simple_switch.By adding the option --p4runtime-files <filename>.txt as shown in the example commands below, P4C will also create a file <filename>.txt. This is a text format "P4Info" file, containing a description of the tables and other objects in your P4 program that have an auto-generated control plane API.
All of these commands take the --help argument to show documentation of supported command line options. p4c --target-help shows the supported "target, arch" pairs.
Auto-translate P4_14 source to P4_16 source:
Check syntax of P4_16 or P4_14 source code, without limitations that might be imposed by any particular compiler back end. There is no output for these commands other than error and/or warning messages.
Generate GraphViz ".dot" files for parsers and controls of a P4_16 or P4_14 source program.
Generate PDF of parser instance named "ParserImpl" generated by the p4c-graphs command above (search for graphviz below for its install instructions):
P4C has package support for several Ubuntu and Debian distributions.
A P4C package is available in the following repositories for Ubuntu 20.04 and newer.
For Debian 11 (Bullseye) it can be installed as follows:
If you cannot use a repository to install P4C, you can download the .deb file for your release and install it manually. You need to download a new file each time you want to upgrade P4C.
git clone https://github.com/p4lang/p4c.git Build. Building should also take place in a subdirectory named build. mkdir -p build cmake -B build <optional arguments> cmake --build build cmake --build build --target check The cmake command takes the following optional arguments to further customize the build (see file CMakeLists.txt for the full list):
-DCMAKE_BUILD_TYPE=Release|Debug – set CMAKE_BUILD_TYPE to Release or Debug to build with optimizations or with debug symbols to run in gdb. Default is Release.-DCMAKE_INSTALL_PREFIX=<path> – set the directory where cmake --build build --target install installs the compiler. Defaults to /usr/local.-DENABLE_BMV2=ON|OFF. Enable the bmv2 backend. Default ON.-DENABLE_EBPF=ON|OFF. Enable the ebpf backend. Default ON.-DENABLE_P4TC=ON|OFF. Enable the TC backend. Default ON.-DENABLE_UBPF=ON|OFF. Enable the ubpf backend. Default ON.-DENABLE_DPDK=ON|OFF. Enable the DPDK backend. Default ON.-DENABLE_P4C_GRAPHS=ON|OFF. Enable the p4c-graphs backend. Default ON.-DENABLE_P4FMT=ON|OFF. Enable the p4fmt backend. Default ON.-DENABLE_P4TEST=ON|OFF. Enable the p4test backend. Default ON.-DENABLE_TEST_TOOLS=ON|OFF. Enable the p4tools backend. Default OFF.-DENABLE_DOCS=ON|OFF. Build documentation. Default is OFF.-DENABLE_GC=ON|OFF. Enable the use of the garbage collection library. Default is ON.-DENABLE_GTESTS=ON|OFF. Enable building and running GTest unit tests. Default is ON.-DP4C_USE_PREINSTALLED_ABSEIL=ON|OFF. Try to find a system version of Abseil instead of a fetched one. Default is OFF.-DP4C_USE_PREINSTALLED_PROTOBUF=ON|OFF. Try to find a system version of Protobuf instead of a CMake version. Default is OFF.-DENABLE_ABSEIL_STATIC=ON|OFF. Enable the use of static abseil libraries. Default is ON. Only has an effect when P4C_USE_PREINSTALLED_ABSEIL is enabled.-DENABLE_PROTOBUF_STATIC=ON|OFF. Enable the use of static protobuf libraries. Default is ON. Only has an effect when P4C_USE_PREINSTALLED_PROTOBUF is enabled.-DENABLE_MULTITHREAD=ON|OFF. Use multithreading. Default is OFF.-DBUILD_LINK_WITH_GOLD=ON|OFF. Use Gold linker for build if available.-DBUILD_LINK_WITH_LLD=ON|OFF. Use LLD linker for build if available (overrides BUILD_LINK_WITH_GOLD).-DENABLE_LTO=ON|OFF. Use Link Time Optimization (LTO). Default is OFF.-DENABLE_WERROR=ON|OFF. Treat warnings as errors. Default is OFF.-DCMAKE_UNITY_BUILD=ON|OFF. Enable unity builds for faster compilation. Default is OFF.If adding new targets to this build system, please see instructions.
sudo cmake --build build --target install The compiler driver p4c and binaries for each of the backends are installed in /usr/local/bin by default; the P4 headers are placed in /usr/local/share/p4c. p4c -b bmv2-ss-p4org program.p4 -o program.bmv2.json If you plan to contribute to P4C, you'll find more useful information here.
P4C is supported on Ubuntu 22.04 and 24.04. macOS, including Apple Silicon (M1), is also supported.
git for version controlBackends may have additional dependencies. The dependencies for the backends included with P4C are documented here: