Writing a first DPDK application (Part 2) - For Beginners
Introduction
In the last article, I have explained how to setup and install DPDK on your system and load the NIC interface with kernel modules through which our DPDK application can access the NIC interface directly. In this article we will write a simple DPDK application in C++ to read a packet from NIC. If you are passionate about learning fast packet processing using DPDK then this article is for you.
Before we write our application we need to learn about memory paging and huge pages.
Memory Paging and Huge pages
An operating system use the concept of memory paging to implement the virtual memory. Using virtual memory, an operating systems allows computer programs to exceed the size of available physical memory by storing the extra used memory on secondary storage such as HDD (hard disk drives) or SSD (solid state drives).
For example: A computer physical memory (RAM) is 8GB but an OS kernel will allow to run the computer programs using 12GB of memory. OS kernel will store the remaining memory (12 - 8 = 4GB) on secondary storage (HDD or SSD etc.). The OS kernel will store and load the memory from secondary storage to primary storage (RAM) in the form of memory pages. A memory page is a contiguous block of memory with a size of 4KB. So an OS kernel can move 4KB contiguous chunk of memory from primary storage to secondary storage and vice versa when ever required.
A huge page is a page with a bigger size. A huge page can have a size of 2MB or 1GB. A huge page will provide better performance as compared to a page because of its bigger size i.e. the OS kernel will have to do less lookups while loading/storing the pages from/to secondary storage. DPDK applications use huge pages instead of pages to achieve performance.
Allocating Huge Pages for DPDK application
DPDK provides a special python utility (dpdk-hugepages.py) to allocate huge pages for our DPDK application. This utility comes as a part of DPDK installation.
Below are steps to allocate huge pages:
Go to usertools directory in DPDK main directory. You can see the last article regarding the DPDK installation.
cd dpdk-23.11/usertools/
We will allocate huge pages of size 2MB and allocate total of 256MB of memory on NUMA node 0. NUMA (Non Uniform Memory Access) node is set or cluster of CPUs which have faster access to the node memory as compared to the CPUs which are present in the other NUMA nodes. Currently, we would be using the CPU from NUMA node 0 to run our DPDK application. That's why we are allocating the huge pages memory on NUMA node 0. You can have multiple NUMA nodes on you systems.
sudo ./dpdk-hugepages.py --pagesize 2M --setup 256M --node 0
Now we can see the status of huge pages allocation by running ./dpdk-hugepages.py utility again but with different argument.
./dpdk-hugepages.py -s
You should be able to see the below output where we have 128 huge pages allocated each of size 2MB. The total allocated memory turns out to be 2MB x 128 = 256MB. We have successfully allocated huge pages for our DPDK application.
Writing DPDK application
Finally, we can write our DPDK application in C++. The written DPDK application is present at: https://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 your first DPDK application:
cd dpdk-tutorials/bin
sudo ./reading-a-packet-from-nic --lcores=0 -n 4 --
Congratulations! You have run your first DPDK application. This is a simple application which simply reads any incoming packets on NIC interface 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 in the code of our first application. Don't forget to go through the code while you run your first 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 the incoming packets on NIC interface and display the packet size. We also discussed about the concept of huge pages and why DPDK applications use huge pages instead of simple pages.
Feel free to write me in the comments if you are unable to perform the steps successfully, mentioned in this article.
Subscribe to my newsletter
Read articles from Muhammad Awais Khalid directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Muhammad Awais Khalid
Muhammad Awais Khalid
I am a software engineer. During my career, I have developed high performance software solutions in multiple domains. I have expertise in: C/C++, Linux, Data Plane Development Kit (DPDK), VoIP (Voice Over IP) Lawful Interception Systems, Parallel Computing, GPU Computing