Skip to content

SadeekFarhan21/p4c

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Main Build Main Build Main Build

Bazel Build Validation Docker Container

p4c

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 code contains seven sample backends:

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 https://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 https://build.opensuse.org/package/show/home:p4lang/p4lang-p4c, 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. It includes submodules, so be sure to use --recursive to pull them in:

    git clone --recursive https://github.com/p4lang/p4c.git
    

    If you forgot --recursive, you can update the submodules at any time using:

    git submodule update --init --recursive
    
  2. Install dependencies. You can find specific instructions for Ubuntu 20.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 build
    cd build
    cmake .. <optional arguments>
    make -j4
    make -j4 check
    

    The cmake command takes the following optional arguments to further customize the build:

    • -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 make 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_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_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 make 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

Ubuntu 20.04 is the officially supported platform for p4c. There's also unofficial support for macOS 11. Other platforms are untested; you can try to use them, but YMMV.

  • A C++17 compiler. GCC 9.1 or later or Clang 6.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 v3.25.3 or higher for control plane API generation

  • C++ boost library

  • Python 3 for scripting and running tests

  • Optional: Documentation generation (enabled when configuring with --enable-doxygen-doc) requires Doxygen (1.8.10 or higher) and Graphviz (2.38.0 or higher).

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