First of all create a new .p4 file in a directory called p4projects
mkdir p4projects
cd ./p4projects
touch hello.p4You can take the P4 code to test from hello.p4
- Pull the official P4 compiler image
docker pull p4lang/p4c:latest- Direct to your directory that holds your hello.p4 program and run the Docker p4c container
cd p4projects
docker run -it --rm --privileged --network host -v $(pwd):/work p4lang/p4c:latest bash- Inside the container, redirect to your pwd mounted on /work
cd /work- Next, compile your P4 code
p4c -b bmv2 --p4runtime-files build/hello.p4info.txtpb hello.p4 -o buildAt this point, you might get the error while loading shared libraries: libboost_iostreams.so.1.71.0: cannot open shared object file: No such file or directory. To fix, install the libboost-iostreams1.71.0
apt-get update && apt-get install -y libboost-iostreams1.71.0Then, try to compile your P4 program again.
Now, you should see hello.p4 and a directory named build if your code doesn't have bugs.
Inside the build directory, you should see these files as follows:
hello.json hello.p4i hello.p4info.txt hello.p4info.txtpb- Pull the official behavioral-model image
docker pull p4lang/behavioral-model- Run the Docker container for behavioral-model
docker run -it --rm --privileged --network host -v $(pwd):/work p4lang/behavioral-model:latest bash- Inside the container, redirect to your pwd mounted on /work
cd /work- Check your simple_switch_grpc version, and run the simple_switch_grpc
simple_switch_grpc --version
simple_switch_grpc --device-id 0 --log-console build/hello.jsonAfter that, you might get the result as shown below:
Calling target program-options parser
Server listening on 0.0.0.0:9559You must let the switch on when you upload rules from P4Runtime Controller!
Install P4Runtime shell via pip3
# Install p4runtime-shell package and run it
pip3 install p4runtime-shell --break-system-packages
python3 -m p4runtime_sh --grpc-addr <server IP>:<server port> \
--device-id 0 --election-id 0,1 --config <p4info.txt>,<pipeline config>Now you can check if there is a line like
*** Welcome to the IPython shell for P4Runtime ***then you are good to go. If no error shows up, you have successfully installed P4Runtime!
Make sure that the simple_switch_grpc is ON as stated in section II.
Create a Python file, and run the test code from test_P4.py in another terminal.
If the result shows the connection successfully, that means the connection is good.