P4 Installation Tutorial
Last update: 2021.4.16
This tutorial shows how to install P4 and its dependencies from scratch.
PI(An implementation framework for a P4Runtime server),p4c(P4_16 reference compiler) andbehavior-model(The reference P4 software switch), which are all the components you need to run p4 programs, will be installed. When finishing installation, you can play with p4 tutorial.The installation is tested on Ubuntu 18.04.5 LTS. The whole installation may take 2 hours depending on network condition. At least 25 Gbytes of free disk space is needed.
It is recommended to install the parts following the order listed below. The version of the packages and repos must follow the instructions. Any mismatch version may cause unsuccessful installation.
To check latest package and repo versions, visit https://github.com/p4lang/tutorials/blob/master/vm/user-bootstrap.sh and get new versions from the bash file.
Step1: Install dependencies
-
install basic dependencies needed for P4.
sudo apt-get install -y cmake g++ git automake libtool libgc-dev bison flex libfl-dev libgmp-dev libboost-dev libboost-iostreams-dev libboost-graph-dev llvm pkg-config python python-scapy python-ipaddr python-ply tcpdump graphviz golang libpcre3-dev libpcre3 curl mininet lsb-releaseinstall doxygen (optional)
sudo apt-get install -y texlive-full doxygen -
install dependencies needed for P4 behavior model version 2
git clone https://github.com/p4lang/behavioral-model.git cd behavioral-model ./install_deps.sh cd .. -
install
protobufsudo pip install protobuf==3.2.0git clone https://github.com/protocolbuffers/protobuf.git cd protobuf git checkout v3.2.0 export CFLAGS="-Os" export CXXFLAGS="-Os" export LDFLAGS="-Wl,-s" ./autogen.sh ./configure --prefix=/usr make sudo make install sudo ldconfig unset CFLAGS CXXFLAGS LDFLAGS cd ..Note: when
./configureruns with--prefix=/usr,protobuf v3.2will be installed under/usrdirectory. Otherwiseprotobuf v3.2will be installed under/usr/localdirectory. Programs under/usr/localhave higher priority than those under/usrby default.If there are multiple
protobufs of different versions installed, please ensure that theprotobuf v3.2acquires highest priority to meet the requirement from PI,p4c,etc.protoc --version > libprotoc 3.2.0To delete different version of
protobuf, deletelibprotoc*,protocinbin,include,share,libfolder. -
install
grpcsudo pip install grpcio sudo pip install psutilgit clone https://github.com/google/grpc.git cd grpc git checkout v1.3.2 git submodule update --init --recursive export LDFLAGS="-Wl,-s" makesubmodule
boringsslandboringssl-with-bazelare different branches of the same repoboringssl. If the compiling process stops and the following error occurs:cc1: warnings being treated as errorsOpen and edit
Makefile. In the line that starts withCPPFLAGS, remove-Werrorflag. Save and closeMakefile. Then runmake cleanandmake. This time the compiling should work well.sudo make install sudo ldconfig unset LDFLAGS cd .. -
install
sysrepoand its dependencieslibyanggit clone https://github.com/CESNET/libyang.git cd libyang git checkout v1.0.184 mkdir build cd build cmake .. make sudo make install sudo ldconfig cd ../..git clone https://github.com/sysrepo/sysrepo.git cd sysrepo git checkout v1.4.70 mkdir build cd build cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_EXAMPLES=Off -DCALL_TARGET_BINS_DIRECTLY=Off .. make sudo make install sudo ldconfig cd ../..
Step2: Install P4
-
PIgit clone https://github.com/p4lang/PI cd PI git checkout 41358da0ff32c94fa13179b9cee0ab597c9ccbcc git submodule update --init --recursive ./autogen.sh ./configure --with-proto make make check sudo make install sudo ldconfig cd .. -
behavioral-modelcd behavioral-model git checkout b447ac4c0cfd83e5e72a3cc6120251c1e91128ab ./autogen.shTo enable switch logging:
./configure --enable-debugger --with-piFor higher performance:
./configure --with-pi 'CXXFLAGS=-g -O3' 'CFLAGS=-g -O3' --disable-logging-macros --disable-eloggermake make check sudo make install sudo ldconfigcd targets/simple_switch_grpc ./autogen.sh ./configure --with-thrift make sudo make install sudo ldconfig cd ../../..run
simple_switchto check whether successfully installedsimple_switch. -
p4cgit clone https://github.com/p4lang/p4c.git cd p4c git checkout 69e132d0d663e3408d740aaf8ed534ecefc88810 git submodule update --init --recursive mkdir build cd build cmake .. make make check sudo make install sudo ldconfig cd ../..run
p4c --helpto check whether successfully installedp4c.
No error? Congratulations! You have successfully installed everything you need for P4.
Have fun with P4!
Step3 (optional): run a p4 program
You can now follow p4 tutorial to learn P4. To run Basic Forwarding exercise, execute:
git clone https://github.com/p4lang/tutorials.git
cd tutorials/exercises/basic
make clean
make run
You should now see a Mininet command prompt. Try to ping between hosts in the topology:
h1 ping h2
Type exit to leave each xterm and the Mininet command line. Then, to stop mininet:
make stop
And to delete all pcaps, build files, and logs:
make clean
The ping failed because each switch is programmed according to basic.p4, which drops all packets on arrival.