Creating a new Bmv2 project on P4Pi - p4lang/p4pi GitHub Wiki
Creating a new Bmv2 project on P4Pi
P4Pi can use p4c
as a P4 compiler and simple_switch
or 'simple_switch_grpc' to execute compiled binary program. There are many options to compile and run a new P4 project with Bmv2. In this page, we only describe the easiest way to start a new P4 project. Note that Bmv2 has already been enabled in our provided image, which means that the Bmv2 commands (e.g. p4c
, simple_switch
, simple_switch_grpc
, simple_switch_CLI
) can direcly be used in P4Pi.
Step 1 - Create a new P4 program / Copy your existing P4 program to P4Pi
Using scp
you can copy your P4 source files to the P4Pi platform or alternatively you can create new P4 files locally on the node.
The bmv2 service and other helper scripts expect that your code is available in a project folder under '/root/bmv2/examples'. In the following example, we simply create a new P4 project/program with the name myprog.p4
. Note that first you should open an SSH connection to the P4Pi node and then you can use the following commands:
sudo su
cd /root/bmv2/examples
mkdir myprog
scp ..@..:myprog.p4 myprog/myprog.p4
Note that the main P4 program should be named as the containing project folder. In this case, the project folder is 'myprog' and the main P4 program file is named as 'myprog.p4'.
Step 2 - Develop your P4 program!
You can use any editor that you prefer to edit your P4 program in P4Pi, for instance:
vim /root/bmv2/examples/myprog/myprog.p4
Step 3 - Launch your new program
Assuming that Bmv2 is the enabled backend, if not, please check how to enable Bmv2 on P4Pi. You can execute your program either through the web terminal or manually with the following commands (in the SSH terminal):
sudo su
echo 'myprog' > /root/t4p4s-switch
systemctl restart bmv2.service
Note that file name 't4p4s-switch' in the above commands is a not a typo. You can set the name of P4 program to be compiled in the same configuration file.
Step 4 - Running P4Runtime shell for your program
Similarly to other examples, you can use the same script to launch the P4 Runtime shell:
sudo bmv2-p4rtshell myprog
The command will generate p4info files needed for P4Runtime Shell and provide the appropriate arguments to the shell.
Step 5 - Running switch api to configure non-programmable elements
You can create/modify multicast groups and setup queues with command 'simple_switch_CLI' via a web or ssh terminal.
simple_switch_CLI
p4c
:
Manual Settings - Compile your new program with cd /root/bmv2/examples/myprog
p4c-bm2-ss -I /usr/share/p4c/p4include --std p4-16 --p4runtime-files /root/bmv2/bin/myprog.p4info.txt -o /root/bmv2/bin/myprog.json myprog.p4
After this step, you will see a generated binary program called myprog.json
and the p4info file.
simple_switch_grpc
on specific network interface:
Manual Settings - Run your new program with sudo simple_switch_grpc -i 0@veth0 -i 1@veth1 /root/bmv2/bin/myprog.json -- --grpc-server-addr 127.0.0.1:50051
Manual execution of your program with verbose output
sudo simple_switch_grpc -i 0@veth0 -i 1@veth1 /root/bmv2/bin/myprog.json --log-console -- --grpc-server-addr 127.0.0.1:50051
Debugging P4 switch program with gdb
sudo simple_switch_grpc -i 0@veth0 -i 1@veth1 /root/bmv2/bin/myprog.json --debugger -- --grpc-server-addr 127.0.0.1:50051
or alternatively you can add the argument '--debugger' to the appropriate line in script '/usr/bin/bmv2-start'. Then the switch will be launched in debug mode by restarting bmv2.service.
Connect the debugger to the switch:
sudo python /usr/lib/python3/dist-packages/p4dbg.py