A Simple L2 lerning switch development tutorial on P4Runtime
This tutorial uses P4Runtime's Packet-In / Out function to build a simple switch which is based on the P4Runtime Shell. It also provides an easy to use environment for you to continue to expand the capabilities of your own switch.
In this tutorial, you will try four things as follows:
- Multicast Group settings and the Flooding process using them
- Processing request to the controller using Packet-In
- Using Packet-Out to Add Entries to the Switch Table
- Adding functionalities to the P4Runtime Shell to execute such experiments
The final result is a very simple MAC learning switch.
These experiments are performed in the following environments:
- Use P4 Runtime Shell as controller
- Use Mininet corresponding to P4 Runtime as switch
- Use open source p4c for P4 compilation
If you skip the tutorial, try shortcut.
All set. Here are the steps. I recommend that you try them in order.
Tutorial 0: Preparing the Environment
Start Mininet and start the P4 Runtime Shell, which is the controller replacement. Then connect them.
Tutorial 1: NanoSwitch01
Repeat all incoming packets to all ports.
- Set a Multicast Group and output to
Tutorial 2: NanoSwitch02
Prevent repeat back to the input port.
- Drop Multicast packets if they have the same port number as Ingress port
Tutorial 3: NanoSwitch03
Create a flow table on the switch side to accommodate unknown hosts.
- Unknown is processed as Packet-In
- Packets received as Packet-In will send back as Packet-Out to repeat to all ports
Tutorial 4: NanoSwitch04
Create a host table on the controller side to correspond to known hosts.
- Add round-trip entries to the flow table for communication with known host pairs
- From now on, the switch will forward the round-trip packet without the controller
Tutorial 5: NanoSwitch05
Add the broadcast process, which has been ignored in nanosw04
- When the broadcast comes out, add the corresponding entry to the flow table
- By supporting ARP, it will behave more like a normal switch
Tutorial 6: NanoSwitch06
Corresponds to the error when the entry could not be added to the flow table in time
- Ignore the error if double registration occurs
The Switch I designed has priority on making the P4 Runtime easy to understand and try out. So it's pretty imperfect in terms of functionality, and there are several issues that come to mind. Why don't you add them yourself?
-
In actual switch operation, it is common to replace host X from port 1 to port 2 on the same switch. However, this switch does not handle this and packets cannot go back and forth.
-
There is a limit to the number of entries that can be registered, so you should remove any flow entries for which there has been no packet flow for some time.
-
In the first place, a normal L2 Learning Switch only remembers the port on which the destination host resides, and does not need to remember an entry for each flow. However, in order to do this in P4, you need two tables: one for source MAC and one for destination MAC + port. How about making a switch to match two tables in this way?
This tutorial was arranged by adding features to the P4 Runtime Shell. I have released the [Modified P4Runtime Shell] (https://github.com/yyasuda/p4runtime-shell) which I used to design this switch. If you compare Dockerfile.dev to the original version, you'll see where it's been modified. Docker Image which was created by the method shown in Tutorial 0: Preparing the Environment is also available.
It includes all the features that I used in this tutorial, as well as some features that I didn't cover here. Hit the link below to read more.
Mininet provides tools such as ping, which makes it easy to send packets without ARP, etc. However, in experiments with real devices such as the Wedge Switch, you may want to send arbitrary packets from Wedge itself (OpenNetworkLinux) or a Windows/Mac connected to the Wedge port. Here are the tools for that.
CPU port information is at the beginning of nanoswXX.p4. If necessary, update it and recompile.
#define CPU_PORT 255
If you don't know the CPU port number of the switch you are testing, P4Runtime-CPUport-finder may help.