Simple communication between two hosts

This tutorial creates two hosts connected through a P4 switch with entries passed by the control plane to the BMv2 switch using the BMv2 API.

Model topology

Download or copy the P4 code available in https://github.com/dnredson/arrow-up-rightP4Docker/blob/main/standardSucess.p4arrow-up-right

This is a basic code for a switch P4 that creates a table ipv4_lpm with the following functions:

  • ipv4_forward: that receives as parameters a port and a MAC address

  • drop: marks a packet to drop

Access the P4Compiler and paste the code in the "Code" area. For the title, input "code" and click in "Compile". At the output you should receive the message "

Code compiled

After this step, the "Shared" volume standard for all the containers created with P4Docker will be available for the switch container.

Go to the "Home" link at the top navigation menu to access the main page. We will now create the desired topology in a blank canvas.

Name your project if you want, but this step is not necessary. For this project, we will use the name "Tutorial"

Click on the button "Add Node" and select the "Host" option. Input in the form:

Name: host1

Ports: 1

Docker Image: dnredson/net

We will use for this tutorial an image that already has the tools to validate the connection between the two hosts. Click on "Sage changes"

Now, the node we just created will appear on the canvas. Click on the host to show the info on the right

Let's add another host to create the topology, click on "Add Node" and change the name to host2. Click on the "Save changes" button

If it looks like there is no new node, the two nodes may be overlapping each other, like in this example:

You can drag the node to the side to make it easier to visualize the nodes. Future versions will apply a more elegant deploy tool for the nodes, currently, the nodes are added to the center of the canvas. After dragging (if necessary), both nodes should be visible.

Click again on "Add Node" and this time select the "Switch" option. Fill the form with the following options (all fields are mandatory):

  • Name: sw1 (or any other name that you prefer)

  • P4 Code Name: code.json (or any other name that you input on the P4Compiler with the extension .json. The extension is mandatory

  • API Port: 50001 (the default port for the BMv2 API

  • Ports: 2

  • Entries: the following entries separated by ";" without spaces

To better understand the entries, we will analyze each command:

  • table_add: command to add an entry to a P4 table

  • MyIngress.ipv4_lpm : indicates that the table will be the ipv4_lpm table located at MyIngress block of the P4 code

  • ipv4_forward 10.0.1.2: indicates that when the IP matches 10.0.1.2 the ipv4_forward will be called with the parameters passed after the arrow

  • => : indicates the parameters to be passed to the ipv4_forward command

  • 00:00:00:00:01:02 1 : indicates that the destiny MAC address will be 00:00:00:00:01:02 and the packet must be sent to port 1 of the switch

To make it short, these lines will say to the switch that every packet with a destination to 10.0.1.2 must be forwarded to port 1 to the MAC address 00:00:00:00:01:02. This command will be sent to the Switch using the API running at port 50001. This is how the BMv2 switch can communicate with the control plane.

The second command represents something similar, just changing the match to 10.0.2.2, MAC address to 00:00:00:00:02:02, and port to 2. Thus, when the destiny IP is 10.0.2.2 the switch will forward the packet to port 2 with destiny 00:00:00:00:02:02.

After input all the data, click on "Save changes"

Now, we have a topology with two hosts and a switch.

The next step is to create the connections between the nodes. Click on the "Add Edge".

We will create each host in a separate network, for host 1 the network 10.0.1.0/24, and for host2 the network 10.0.2.0/24 with the switch being the first address on each network.

Beginning with the host1, select "host1" as the "Source Parent" and "host1-p1" as the source child. As the IP address inserts 10.0.1.2/24 since it's the second host in this network. For the MAC address type 00:00:00:00:01:02 as the second host in the network. Note that those addresses match the addresses used when we first create the switch in the "Entries" field. If you desire to change the addresses, remember to update the entries by clicking on the switch and using the "Edit Node" button to change the entries. We recommend that you use the same addresses for the first time running this tutorial. The first half that defines the source of the connection in the "Add node" form should look like this:

For the "Target parent" select the "sw1" and "sw1-p1" for the "target child" port. The IP Address is 10.0.1.1/24 and the MAC 00:00:00:00:01:01 . Uncheck the "Define bandwidth and delay" since we will not use these configurations on this tutorials. The target section of the form should look like the following:

Click on "Save changes" and the connection will be created

Now, we will create the connection between the port 2 of the switch and the host2. Click on the "Add Edge" button. For the source select "host2" and the Source Child host2-p1. Input the IP address 10.0.2.2/24 and the MAC address 00:00:00:00:02:02. The source should look like the following:

For the target, select the "Target parent" sw1 and for the "target Child" sw1-p2, since we want to connect to the second port of the switch. *Note: the current version doesn't validate if a port is already in use, so it is possible to create connections with the same port twice, but it will not work properly during the deployment phase. Future versions will apply validation of ports in use.

For the IP address input 10.0.2.1/24 and the MAC 00:00:00:00:02:01. Uncheck again the "Define bandwidth and delay" checkbox if necessary. The "Target" session should look like the following:

Click on "Save changes" and now our topology will be complete:

If you desire, give this project a name in the field "Topology name" above the canvas and save this topology for further studies. Here, we gave the name "Tutorial" and then "Save Topology" to download the file "TutorialTopology.json" .

With the topology concluded, click on the button "Generate Code" at the bottom menu. Click on the button "Download" to download the script to create this topology and then on the button "close" to return to the main page. We recommend that with each project you also generate the code to clean the current topology, since containers and connections with the same names will not work. Click on the button "Generate Cleander Code" and then in "Download" so you may clean the environment created by the script if necessary.

Once you download the generate and cleaning files, access the folder when the files are located. If you are using the virtual machine provided in the installation section, the path should be /home/p4d/Downloads . In this file, we will find the cleanTutorial.sh (script to clean the testbed), tutorialConfig.sh (script to create the testbed) and the tutorialTopology.json (code to load the topology)

Give permission to "cleanTutorial.sh" and "tutorialConfig.sh" to be able to execute the scripts with the command:

$ chmod +x cleanTutorial.sh $ chmod +x tutorialConfig.sh

Note that all the scripts must be executed as sudo since the docker demands this access. If you are using the Virtual machine provided by the install section, the root password is "p4d"

Now execute the config script with:

$./tutorialConfig.sh

The script will detail each step, so it is easy to locate if any step fails. If the topology was created as detailed in this tutorial, your output at the terminal should look like the following:

Now, to test the connection, we can connect to the switches and see what is happening inside each container.

To connect to the first host, execute the command:

$docker exec -it host1 /bin/bash

This command will connect your terminal to the terminal inside the container. By default, all containers are connected to the "Shared" volume, mounted in the path "/codes" inside each container

Inside the container use the command:

# ping 10.0.2.2

To test the connectivity with the host2

The topology created will be exactly the same as described at the beginning of this tutorial, but with containers docker and veth peers, bringing more realistic characteristics to the testbed:

This conclude this tutorial. You can explore the connectivity between the containers with other tools, or see other tutorials to learn how to use tools like Wireshark and explore how to debug the test bed environment.

After finishing the tests, execute the cleanTutorial script to delete all the veth and containers:

$chmod +x cleanTutorial.sh

$ ./cleanTutorial.sh

Last updated