After having followed P4Lang P4 for dummies [ #002 ] article, you should have now a working P4 development environment.
Let's start writing. compiling and running our first P4 program.
This 3rd article propose to write your first P4 program based on P4Lang P4 for dummies [ #001 ] my_program.p4 specification.
[ #003 ] - Cookbook
P4 program specification
my_program.p4 packet progressing logic: "all packets arriving at port 4 are switched/forwarded to port 8"
- In this example, the switch has 8 ports
- A ingress packet arrives at port 4
- the ingress port is then checked
- If it is port 4, then the packet is switched to port 8
- my_program.p4 does not implement a default condition, so all the packets not arriving on port 4 are then dropped
- the ingress packets arrived with a header with charateristics set by the previous node
- if needed, my_program.p4 is able to set modify the egress packet header for further processing by the next network node (example of in-band network Telemetry)
Let's first create the P4 program environment:
Create veth pair before ...
Before launching our BMv2 virtual switch we need to create the veth pair that will be bound the P4 switch.
for that we will reuse bash scripts from Andy Fingerhut public GitHub Repository:
we can now launch BMv2 simple_switch and bind the 8 veth pairs we just configured
Now you need to find a way to:
- send a packet to simple_switch@PORT_4 (veth8)
- send another packet to simple_switch@PORT_1 (veth2)
We will use scapy for that:
Run scapy with sufficient privileges to send packets on specific interface
You have successfully written, compiled, load your program P4Lang P4 virtual switch ! In addition, you also checked that the logic of your program is implemented correctly by sending a packet to PORT_4 using scapy python3 tool. You then checked with tcpdump that your packet ingressed the P4 switch via PORT_4 and egressed via PORT_8 as it was expected.
What's happening to other packets arriving on a port that is different from PORT_4 ?
Let's try to find out. In that situation, let's send an ingress packet to PORT_1 (veth2) of the switch and see what's happening.
In that case we don't know what is the egress port so let's look at simple_switch console.
So in that case we see that line: "Egress port is 0", which is a special port number that designate the null0 interace. (packet dropped)
Let's now resent a packet to PORT_4 and observe simple_switch console log.
We clearly confirmed what tcpdump what putting in evidence: ingress PORT_4 leads to a packet switched to PORT_8
In this article you:
- wrote your first P4 program
- use p4c in order to compile it
- learned how to instantiate virtual ethernet pair in order to bind them with simple_switch
- launch simple_switch and load your program on it
- set up a test environment using scapy
- and verify your program using a combination a scapy and tcpdump
P4Lang P4 for dummy [ #002 ] - key take-away
- my_program.p4 is written following V1Model definition that defines:
- a parsing stage
- a checksum verification stage
- an ingress packet processing control stage
- an egress packet processing control stage
- a checksum computation stage
- deparser stages
It is described by the diagram below:
In a subsequent article we will dissect my_program.p4, but as you could observe, P4 programming is quite intuitive as it is all about switching a packet based on intrinsic ingress packet header and metadata (like packet ingress port) value.