Implementation example using P4 language and FPGA Smart NIC ~NAT edition~

Introduction

This time, in a slightly different direction than the previous articles on Open Networking, I will introduce a language called P4 that can describe the network data plane and its implementation examples.

OtherOpen NetworkingThere is an article related to the following "Click here for the list of articles, see the articles that interest you.

What is P4??

P4 (Programming Protocol-Independent Packet Processors) is a programming language for describing the network data plane. Since it does not depend on existing network protocols, it is possible for users to create their own defined protocols and protocol stacks.

In addition, since various hardware can be used, the optimum platform can be selected according to the user's requirements. In the past, users could not easily change the data plane, but with P4, they can freely change the data plane in a quick development cycle by editing the program.

P4 target hardware

P4 target hardware

P4 use case

There are many possible use cases for P4, but I will introduce some representative ones.

 

P4 use case

P4 use case

Implementing NAT on P4

This time, I tried implementing the NAT function using Intel® PAC N3000, which is equipped with an FPGA as hardware that supports P4, so I will briefly explain it.

 

 

Intel® PAC N3000

Intel® PAC N3000

NAT implementation image

This time, we implemented the NAT function as shown in the figure below on the FPGA Smart NIC using P4.

In P4, we will describe the match & action table that rewrites each according to the conditions of the IP address and port number of the incoming packet.

 

NAT function implementation image

NAT function implementation image

Writing P4 code

I can't explain all the P4 descriptions this time, but I will introduce the key points.

The first thing I will explain is the description of the match & action table. What is described in the top reads of the table description is the parameter to be read as a condition. In this description, IP address and TCP/UDP port number are used as conditions.

Next, actions at the bottom of the table description list the actions to be executed when the conditions are met. By making such a description, it becomes possible to specify which action to perform based on the condition of the header of the incoming packet.

Actions to be executed when the conditions are met must be described separately. I won't explain it in detail, but this time I'm writing to convert the address and port number by implementing NAT.

Once written, the P4 code is compiled using a compiler for FPGA Smart NIC and the binary image is written to the FPGA.

table description

table description

Action description

Action description

Configuring Match & Action Rules

In the previous description, a match & action table for NAT was implemented in the FPGA, but that alone does not work as a NAT. By default, the table is empty, so it is necessary for the user to populate the rules.

Intel® PAC N3000 provides an SDK that allows rules to be entered from the server via the command line.

If you set the rules as shown in the example below, when a packet that matches the conditions of source address 1.1.1.1, destination address 2.2.2.2, TCP source port 1000, and destination port 2000 is input, , the destination address and port will be rewritten as 10.10.10.10:1111 respectively.

By adding/modifying/deleting rules as necessary, it is possible to control the intended behavior.

# table_nat keys ( ipv4.srcAddr 1.1.1.1 ipv4.dstAddr 2.2.2.2 tcp true tcp.srcPort 1000 tcp.dstPort 2000 udp false udp.srcPort 0 udp.dstPort 0 ) action dstnat_tcp params ( ipaddr 10.10.10.10 port 1111 )

Actual operation verification

Finally, I will post the results when I actually run it.
I made the following rule settings and captured what was output after packet input.
It can also filter packets, so it can be used as a firewall.

 

# table_nat keys ( ipv4.srcAddr 10.10.10.10 ipv4.dstAddr 20.20.20.20 tcp true tcp.srcPort 1000 tcp.dstPort 2000 udp false udp.srcPort 0 udp.dstPort 0 ) action dstnat_tcp params ( ipaddr 2.2.2.2 port 2222 )
# table_nat keys ( ipv4.srcAddr 10.10.10.10 ipv4.dstAddr 20.20.20.20 tcp false tcp.srcPort 0 tcp.dstPort 0 udp true udp.srcPort 1000 udp.dstPort 2000 ) action drop_act
# table_nat keys ( ipv4.srcAddr 30.30.30.30 ipv4.dstAddr 40.40.40.40 tcp true tcp.srcPort 3000 tcp.dstPort 4000 udp false udp.srcPort 0 udp.dstPort 0 ) action drop_act
# table_nat keys ( ipv4.srcAddr 30.30.30.30 ipv4.dstAddr 40.40.40.40 tcp false tcp.srcPort 0 tcp.dstPort 0 udp true udp.srcPort 3000 udp.dstPort 4000 ) action srcnat_udp params ( ipaddr 3.3.3.3 port 3333 )

 

Below is a capture of the input and output packets.

You can see that packets are processed according to the rules.

input packet

input packet

output packet

output packet

At the end

In this article, we introduced an overview of P4 and an implementation example of NAT.

Normally, if we were to implement similar functions using an FPGA without using the P4, the amount of HDL code would be large and verification would take a considerable amount of time, but this time, by developing using the P4, the number of lines of code was reduced to 400. It took about 2 days to verify the operation. Another advantage is that even if you do not have FPGA development skills, you can develop with P4 alone.

If you have a request for a P4 code, please contact us after entering the necessary information from the inquiry button below.


Click here for list of materials

Document list

In addition to introducing products handled by Macnica,
We publish materials related to open networking, such as BGP cross network automatic construction files and network operation test evaluation reports.

Click here for details

Product Page Top

Edgecore Networks

We continue to be a pioneer in open networking by developing and selling products related to OpenNetworking/white Box switches.

DELTA ELECTRONICS

We have many achievements in supporting the business of major OEM customers in Japan and overseas, and provide high-quality/high-reliability network products.

CGS Tower Networks

We provide a network packet broker (NPB) that utilizes state-of-the-art general-purpose hardware.

Inquiry

If you have any questions regarding this article, please contact us below.

Macnica
In charge of OpenNetwork

Contact us by phone:
045-470-9831