P4 Learning——Basic Forwarding

Article directory


https://github.com/p4lang/tutorials/tree/master/exercises/basic

1. Realize basic forwarding

For simplicity, only IPv4 forwarding is implemented. For IPv4 forwarding, the switch must perform the following operations on each packet:

  • Update source and destination MAC addresses
  • Reduce the time-to-live TTL in the IP header
  • Forward the packet out the appropriate port

The switch will have a table that the control plane populates with static rules. Each rule maps an IP address to a MAC address and next-hop output port. We have already defined the control plane rules, so only the data plane logic of the P4 program needs to be implemented.

We will use the following topology:
insert image description here

Step 1: Run the incomplete starter code
/tutorials/exercises/basic also includes a basic P4 program skeleton, basic.p4, which initially drops all packets. Our job is to extend this framework to correctly forward IPv4 packets.
Before that, let's compile basic.p4 and enable a switch in Mininet to test its behavior

1. Execute sudo make run in the shell

  • compile basic.p4
  • Start pod-topo in Mininet (start the topology above) and configure all switches with appropriate and P4 programs and entries
  • After using the commands listed in pod-topo/topology.json to configure all hosts (including the host's ip, mac, and arp settings)
    insert image description here
    successfully, it will display:
    insert image description here

2. Go to ping the hosts in the topology
insert image description here
and find that the ping fails, because each switch is programmed according to basic.p4, so all the packets will be discarded when reaching the end point. Then we need to expand this file so that it forwards packets

Points to note for the control plane:

  • The P4 program defines a packet processing pipeline, but the rules in each table are inserted by the control plane. When a rule matches a packet, its action is invoked with the parameters provided by the control plane as part of the rule
  • In this connection, the control plane logic as part of starting the Mininet instance, the make run command will install the packet processing rules in the tables of each switch
  • We use P4Runtime to install control plane rules. The sX-runtime.json file contains the names of table, keys, and actions, as defined in the P4Info file generated by the compiler (find the build/basic.P4Info file after executing make run). Any changes in the P4 program that add or rename tables, keys, or operations need to be reflected in these sX-runtime.json files.

Step 2: Implement L3 forwarding.
The basic.p4 file contains a framework of a P4 program, in which key logic parts are replaced by TODO comments. Our implementation finally completes a complete L3 forwarding by replacing the TODO part in the P4 program with our own logic
. The ground basic.p4 will contain the following components:

  • The definition of Ethernet (ethernet_t) and IPv4 (ipv4_t) header types, the format has been defined, no need to modify
    insert image description here

  • The Ethernet and IPv4 parser that fills the ethernet_t and ipv4_t fields, for example, first parse the Ethernet frame header, such as etherType is 0x800, and then continue to do ipv4 parsing
    insert image description here

  • Ingress processing: There are two actions, one is drop and the other is ipv4_forward, which requires us to add logic rules, according to IPv4 forwarding logic: there is a next-hop exit, and the destination MAC of the Ethernet frame header is replaced with the next hop MAC, replace the source MAC of the Ethernet frame header with the MAC of the switch, and subtract 1 from the TTL
    table ipv4_Ipm is the table of the control plane, its content: 1. Do action based on the longest match rule of the destination IP mask, and the match is ipv4_forward , otherwise drop, 2, ipv4_Ipm.apply application table
    insert image description here

  • The deparser deparser assembles the previously dismantled package back
    insert image description here

Step 3: Solution implementation
insert image description here
insert image description here
insert image description here
After modification, re-execute sudo make run and pingall
insert image description here
sudo make stop to stop running in the background
sudo make clean to clean up the remaining build and log files of the last program
before running sudo make run each time, it is recommended to execute the above two This command cleans up the environment.

Guess you like

Origin blog.csdn.net/weixin_46025531/article/details/124412648