P4 Language: First Impression

Posted on Categories networking

What is P4

P4 language is a target-independent networking programming language, I think the image below best describes the ambitions of P4:

Components of P4:

to be able to use P4 in our example, you need the following tools and items:

  • p4 source file written in P4 language: the dataplane logic of the target switch, you can find one in the bmv2 GitHub repository
  • P4 compiler: to compile the P4 source file to one that target switch can load, the current one is p4c
  • An implementation of software switch using bmv2: bmv2 is a framework for you to implement P4 software switches, currently there are 5 implementations: l2_switch / psa_switch / simple_router / simple_switch / simple_switch_grpc

Hello World with P4

let’s get started using one of the examples of bmv2.

1. first let’s build a simple siwtch: simpe_switch

git clone https://github.com/p4lang/behavioral-model
cd behavioral-model
sudo ./install_deps.sh //install all the dependencies, this will involve some serious compiling, so take your time
./configure --enable-debugger
sudo make install
sudo ldconfig

after this, the simple_switch will be built and installed to your server, you can try to run simple_switch and get some outputs:

danny@ubuntu:~/programming/behavioral-model$ simple_switch
Error: please specify an input JSON configuration file
Usage: SWITCH_NAME [options]
-h [ --help ] Display this help message
-i [ --interface ] arg @: Attach network
interface as port at
startup. Can appear multiple times
--pcap [=arg(=.)] Generate pcap files for interfaces. Argument is
optional and is the directory where pcap files
should be written. If omitted, files w

2 let’s install the p4 compiler

sudo apt-get install 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 //install p4c dependencies

sudo apt-get install autoconf automake libtool curl make g++ unzip // install protobuf dependencies

install protobuf

git clone https://github.com/google/protobuf.git
cd protobuf/
git checkout v3.2.0
sudo make install
sudo ldconfig

install p4c
git clone --recursive https://github.com/p4lang/p4c
cd p4c
mkdir build
cd build
cmake ..

sudo make install
sudo ldconfig

3 compile an P4 program

for this test, we will not write an p4 programm from sratch, we will use one comes with the bmv2:
wget https://raw.githubusercontent.com/p4lang/behavioral-model/master/mininet/simple_router.p4
p4c --target bmv2 --arch v1model --std p4-14 simple_router.p4

4 runnig the p4 programm

sudo simple_switch -i 1@s1-eth1 -i 2@s1-eth2 -i 3@s1-eth3 -i 4@s1-eth4 --device-id 0 simple_router.json --log-console --thrift-port 9090
to connect to the switch for routing table manipulation:
tools/./runtime_CLI.py --thrift-port 9090

to be able to test the switch, we need mininet, we will cover this in a later article

Leave a Reply

Your email address will not be published. Required fields are marked *