P4C
The P4 Compiler
Loading...
Searching...
No Matches
Getting Started

Main Build Main Build Main Build Bazel Build Validation Docker Container

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.

Overview

The P4C compiler is a compiler infrastructure for the P4 compiler designed with the following goals:

  • Support current and future versions of P4
  • Support multiple back-ends
    • Generate code for ASICs, NICs, FPGAs, software switches and other targets
  • Provide support for other tools (debuggers, IDEs, control-plane, etc.)
  • Open-source front-end
  • Extensible architecture (easy to add new passes and optimizations)
  • Use modern compiler techniques (immutable IR, visitor patterns, strong type checking, etc.)
  • Comprehensive testing

Additional documentation

  • The documentation for P4_16 and P4Runtime is available here
  • the core design of the compiler intermediate representation (IR) and the visitor patterns are briefly described in IR
  • The migration guide describes how P4_14 (v1.0) programs are translated into P4_16 programs

Sample Backends in P4C

P4C includes seven sample backends, catering to different target architectures and use cases:

  • p4c-bm2-ss: can be used to target the P4 simple_switch written using the BMv2 behavioral model,
  • p4c-dpdk: can be used to target the DPDK software switch (SWX) pipeline,
  • p4c-ebpf: can be used to generate C code which can be compiled to eBPF and then loaded in the Linux kernel. The eBPF backend currently implements three architecture models: ebpf_model.p4 for packet filtering, xdp_model.p4 for XDP and the fully-featured PSA (Portable Switch Architecture) model.
  • p4test: a source-to-source P4 translator which can be used for testing, learning compiler internals and debugging,
  • p4c-graphs: can be used to generate visual representations of a P4 program; for now it only supports generating graphs of top-level control flows, and
  • p4c-ubpf: can be used to generate eBPF code that runs in user-space.
  • p4tools: a platform for P4 test utilities, including a test-case generator for P4 programs. Sample command lines:

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:

  • a file with suffix .p4i, which is the output from running the preprocessor on your P4 program.
  • a file with suffix .json that is the JSON file format expected by BMv2 behavioral model simple_switch.
p4c --target bmv2 --arch v1model my-p4-16-prog.p4
p4c --target bmv2 --arch v1model --std p4-14 my-p4-14-prog.p4

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.

p4c --target bmv2 --arch v1model --p4runtime-files my-p4-16-prog.p4info.txt my-p4-16-prog.p4
p4c --target bmv2 --arch v1model --p4runtime-files my-p4-14-prog.p4info.txt --std p4-14 my-p4-14-prog.p4

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.

p4c --help
p4c --target-help

Auto-translate P4_14 source to P4_16 source:

p4test --std p4-14 my-p4-14-prog.p4 --pp auto-translated-p4-16-prog.p4

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.

p4test my-p4-16-prog.p4
p4test --std p4-14 my-p4-14-prog.p4

Generate GraphViz ".dot" files for parsers and controls of a P4_16 or P4_14 source program.

p4c-graphs my-p4-16-prog.p4
p4c-graphs --std p4-14 my-p4-14-prog.p4

Generate PDF of parser instance named "ParserImpl" generated by the p4c-graphs command above (search for graphviz below for its install instructions):

dot -Tpdf ParserImpl.dot > ParserImpl.pdf

Getting started

Installing packaged versions of P4C

P4C has package support for several Ubuntu and Debian distributions.

Ubuntu

A P4C package is available in the following repositories for Ubuntu 20.04 and newer.

source /etc/lsb-release
echo "deb http://download.opensuse.org/repositories/home:/p4lang/xUbuntu_${DISTRIB_RELEASE}/ /" | sudo tee /etc/apt/sources.list.d/home:p4lang.list
curl -fsSL https://download.opensuse.org/repositories/home:p4lang/xUbuntu_${DISTRIB_RELEASE}/Release.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/home_p4lang.gpg > /dev/null
sudo apt-get update
sudo apt install p4lang-p4c

Debian

For Debian 11 (Bullseye) it can be installed as follows:

echo 'deb https://download.opensuse.org/repositories/home:/p4lang/Debian_11/ /' | sudo tee /etc/apt/sources.list.d/home:p4lang.list
curl -fsSL https://download.opensuse.org/repositories/home:p4lang/Debian_11/Release.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/home_p4lang.gpg > /dev/null
sudo apt update
sudo apt install p4lang-p4c

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.

  1. Go to p4lang-p4c package page on OpenSUSE Build Service, click on "Download package" and choose your operating system version.
  2. Install P4C, changing the path below to the path where you downloaded the package.
sudo dpkg -i /path/to/package.deb

Installing P4C from source

  1. Clone the repository. git clone https://github.com/p4lang/p4c.git
  2. Install dependencies. You can find specific instructions for Ubuntu 22.04 here and for macOS 11 here. You can also look at the CI installation script.
  3. 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.

  4. (Optional) Install the compiler and the P4 shared headers globally. 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.
  5. You're ready to go! You should be able to compile a P4-16 program for BMV2 using: 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.

Dependencies

P4C is supported on Ubuntu 22.04 and 24.04. macOS, including Apple Silicon (M1), is also supported.

  • A C++17 compiler. GCC 9.4 or later or Clang 10.0 or later is required.
  • git for version control
  • CMake 3.16.3 or higher
  • Boehm-Weiser garbage-collector C++ library
  • GNU Bison and Flex for the parser and lexical analyzer generators.
  • Google Protocol Buffers (handled automatically via CMake FetchContent)
  • C++ boost library
  • Python 3 and uv for scripting and running tests
  • Optional: Documentation generation requires Doxygen (1.13.2) and Graphviz (2.38.0 or higher).

Backends may have additional dependencies. The dependencies for the backends included with P4C are documented here:

  • BMv2
  • eBPF
  • graphs