IP Router in P4

In the previous post I introduced P4 and tools to implement network prototypes. Now, I would like to show a basic example how to use this tools in practice to run an experiment.

I prepared a demo of IP routing to show how simply someone can prototype a commonly used technology. The demo is based on P4 and Mininet. The source code and user guide is available at https://github.com/osinstom/p4-demos/tree/master/ip-routing.

The README file contains a description of a P4 program’s design as well as a topology of a test network. Below I summarize a design choices that I made:

  • I have used P4_16 as it is the newest version of P4.
  • As the P4 target I use BMv2 (https://github.com/p4lang/behavioral-model)
  • The P4_16 introduces the concept of architecture model for target device. I have used V1Model (https://github.com/p4lang/p4c/blob/master/p4include/v1model.p4), which is a defualt one for BMv2
  • The V1Model forces us to implement the Ingress and Egress control blocks, Parser, Deparser and methods to handle checksum (verifyChecksum and computeChecksum). For the sake of demo I don’t focus on checksum operations.
  • Design choices for the router.p4 program:
    • I defined Ethernet and IPv4 headers (only)
    • I defined additional metadata struct called routing_metadata. It is used to pass a routing decision made in the Ingress block to the Egress block. In the Egress block it will be used to determine the MAC address of the next-hop.
    • The Parser Logic handles only Ethernet and IPv4 headers (e.g. ARP, IPv6 or VLAN is not parsed)
    • For the Ingress control we defined the routing_table. This table has three actions: drop(), NoAction() and ipv4_forward(). The last action performs three operations:
    • it selects output port based on the IPv4 destination LPM
    • it sets a next-hop IP address in routing_metadata.
    • it decrements TTL
    • For the Egress control block we defined switching_table and mac_rewriting_table. The switching_table sets a destination MAC for packet based on next-hop IP from routing_metadata. The mac_rewriting_table changes the source MAC address for packet according to port, that has been selected as the output port.

If you want to know basics of how P4 works I encourage you to follow a user guide and test how IP routing written in P4 works. In the subsequent posts I will show another example - MPLS network written in P4.




Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • Implementing tunneling techniques in P4 based on the example of VXLAN
  • MPLS network based on P4
  • Network prototyping made easy with P4 and Python!
  • Initial view on VoWiFi in the 5G network
  • Configuring OVS-DPDK with VM