Processing packets in DPDK (Data Plane Development Kit)

Introduction

In the last article, I have explained about a basic DPDK application which reads the packets from NIC interface. In this article we will learn about how to actually process a packet after receiving it from NIC. If you are passionate about fast packet processing using DPDK then this article is for you.

Setting up DPDK environment

The DPDK environment setup includes binding the NIC to the specific kernel modules to access them directly and setting up huge pages. All of this is explained in detail in the last article. Kindly go through it in case you need to setup your DPDK environment.

Before we write our application we need to learn about ring buffers in DPDK.

Ring Buffers in DPDK

Ring buffers are finite spaced buffers used to store the packets. Their main purpose is to share the data between two threads. DPDK provides APIs to create, maintain and destroy ring buffers of specific sizes.

Consider a situation where one thread reads a packet from NIC and need to transfer this packet to another thread which process it. The packet reading thread will place the packet in the ring buffer and packet processing thread will read it from the ring buffer.

Writing DPDK application

Finally, we can write our packet processing DPDK application in C++. The written DPDK application is present at: github.com/awaiskhalidawan/dpdk-tutorials

Below are the steps to build and run the application:

git clone https://github.com/awaiskhalidawan/dpdk-tutorials.git
cd dpdk-tutorials/
mkdir build
cd build
cmake ..
make

The application binaries will be generated in dpdk-tutorials/bin folder after the successful execution of above steps.

To finally run the DPDK application:

cd dpdk-tutorials/bin
sudo ./processing-a-packet --lcores=0-1 -n 4 --

Congratulations! You have run a DPDK application which reads and processes the packets. This is a simple application which spawns two threads. The threads are running on a specific core i.e. in our case packet reading routine is running on CPU0 and packet processing routine is running on CPU1.

Packet reading thread reads any incoming packet(s) on NIC interface and share them with a packet processing thread using ring buffers. The packet processing thread reads the packet(s) from ring buffer and prints the packet size. All of this is being done without the involvement of Linux Kernel.

There is an extensive explanations of DPDK APIs as well as the code flow in the code of this application. Don't forget to go through the code while you run this DPDK application.

Note: A DPDK application must be run with sudo for now as DPDK abstraction layer requires sudo privileges. I will write a detailed article later on about how to run DPDK applications without sudo privileges.

Summary

In this article, a simple DPDK application is written and executed. This application reads and process the incoming packets on the NIC interface using two separate threads running on specific CPU(s). The packets are shared b/w two threads using the ring buffers. We also discussed about the concept of ring buffers in DPDK.

Feel free to write me in the comments if you are unable to perform the steps successfully, mentioned in this article.