An introduction to Shared Memory in DPDK

Introduction

In this article, I have explained about a basic DPDK application which reads the packets from NIC interface. In this article we will learn about the concept of shared memory in DPDK library and how to do inter-process communication using DPDK library. If you are passionate about fast packet processing using DPDK then this article is for you.

Introduction to Shared Memory

Shared memory is a memory area in the main memory (RAM) which is accessible to multiple process at the same time. A computer program or a process has access to its own private memory area and can also access shared memory area. Multiple processes can communicate with each other by reading/writing to shared memory.

Shared Memory in DPDK

DPDK library provides a mechanism to access shared memory so that multiple DPDK applications or processes can communicate with each other. DPDK library initialized the shared memory in the huge pages memory area. Huge pages are the part of main memory (RAM). To learn mode about huge pages, please read this article.

DPDK classifies the DPDK applications or process into two types i.e. Primary DPDK application and secondary DPDK application. On any system, only one DPDK primary process and multiple secondary DPDK processes are allowed to run. The DPDK primary process initializes the shared memory area and secondary DPDK process can access this memory area. Secondary DPDK processes are not allowed the initialize the shared memory area. The primary DPDK processes must be executed before secondary DPDK processes.

Shared memory ring buffers in DPDK

DPDK library provides the shared memory ring buffers for inter-process communication. Shared memory ring buffers are always created by primary DPDK process. A ring buffer is a fixed sized buffer. Any DPDK process (primary or secondary) can enqueue/dequeue the data to/from it.

Finally, we can write our DPDK application in C++ to demonstrate the concept of shared memory in DPDK. This application can be executed either as primary DPDK application or secondary DPDK application depending upon the command line arguments.

The primary DPDK application creates a packet and enqueues it in the ring buffer. The packet can received from NIC (Network Interface Card) or any other type of customized data as well. The secondary DPDK application dequeues the packet from ring buffer and process it.

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 run the primary DPDK application:

cd dpdk-tutorials/bin
sudo ./multiprocess-communication --lcores=0@0 -n 4 --proc-type=primary -- ring_buffer_1

To run the secondary DPDK application:

cd dpdk-tutorials/bin
sudo ./multiprocess-communication --lcores=0@0 -n 4 --proc-type=secondary -- ring_buffer_1

The ring buffer in shared memory is created against a specific name. In our DPDK application we created the ring buffer with name "ring_buffer_1" passed as a command line argument to the application.

There is an extensive explanations of application code showing how to initialized shared memory and create ring buffers. Don't forget to go through the code while you run the 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, we have implemented a DPDK application which shows the concept of shared memory in DPDK library. The application can be executed as primary or a secondary DPDK application. Primary application shares the data with secondary application via shared memory ring buffers.

DPDK library shared memory structures can be used to share packets or other customized data b/w multiple processes.

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

1
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