1.. SPDX-License-Identifier: BSD-3-Clause 2 Copyright(c) 2010-2014 Intel Corporation. 3 4Multi-process Sample Application 5================================ 6 7This chapter describes example multi-processing applications that are included in the DPDK. 8 9Example Applications 10-------------------- 11 12Building the Sample Applications 13~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 14 15The multi-process example applications are built the same way as other sample applications, 16and as documented in the *DPDK Getting Started Guide*. 17 18 19To compile the sample application see :doc:`compiling`. 20 21The applications are located in the ``multi_process`` sub-directory. 22 23.. note:: 24 25 If only a specific multi-process application needs to be built, 26 the final make command can be run just in that application's directory. 27 28Basic Multi-process Example 29~~~~~~~~~~~~~~~~~~~~~~~~~~~ 30 31The ``examples/simple_mp`` folder contains a basic example application 32that demonstrates how two DPDK processes can work together 33to share information using queues and memory pools. 34 35Running the Application 36^^^^^^^^^^^^^^^^^^^^^^^ 37 38To run the application, start ``simple_mp`` binary in one terminal, 39passing at least two cores in the coremask/corelist: 40 41.. code-block:: console 42 43 ./<build_dir>/examples/dpdk-simple_mp -l 0-1 -n 4 --proc-type=primary 44 45For the first DPDK process run, the proc-type flag can be omitted or set to auto, 46since all DPDK processes will default to being a primary instance, 47meaning they have control over the hugepage shared memory regions. 48The process should start successfully and display a command prompt as follows: 49 50.. code-block:: console 51 52 $ ./<build_dir>/examples/dpdk-simple_mp -l 0-1 -n 4 --proc-type=primary 53 EAL: coremask set to 3 54 EAL: Detected lcore 0 on socket 0 55 EAL: Detected lcore 1 on socket 0 56 EAL: Detected lcore 2 on socket 0 57 EAL: Detected lcore 3 on socket 0 58 ... 59 60 EAL: Requesting 2 pages of size 1073741824 61 EAL: Requesting 768 pages of size 2097152 62 EAL: Ask a virtual area of 0x40000000 bytes 63 EAL: Virtual area found at 0x7ff200000000 (size = 0x40000000) 64 ... 65 66 EAL: check module finished 67 EAL: Main core 0 is ready (tid=54e41820) 68 EAL: Core 1 is ready (tid=53b32700) 69 70 Starting core 1 71 72 simple_mp > 73 74To run the secondary process to communicate with the primary process, 75again run the same binary setting at least two cores in the coremask/corelist: 76 77.. code-block:: console 78 79 ./<build_dir>/examples/dpdk-simple_mp -l 2-3 -n 4 --proc-type=secondary 80 81When running a secondary process such as above, 82the ``proc-type`` parameter can be specified as auto. 83Omitting the parameter will cause the process to try and start as a primary 84rather than secondary process. 85 86The process starts up, displaying similar status messages to the primary instance 87as it initializes then prints a command prompt. 88 89Once both processes are running, messages can be sent between them using the send command. 90At any stage, either process can be terminated using the quit command. 91 92.. code-block:: console 93 94 EAL: Main core 10 is ready (tid=b5f89820) EAL: Main core 8 is ready (tid=864a3820) 95 EAL: Core 11 is ready (tid=84ffe700) EAL: Core 9 is ready (tid=85995700) 96 Starting core 11 Starting core 9 97 simple_mp > send hello_secondary simple_mp > core 9: Received 'hello_secondary' 98 simple_mp > core 11: Received 'hello_primary' simple_mp > send hello_primary 99 simple_mp > quit simple_mp > quit 100 101.. note:: 102 103 If the primary instance is terminated, the secondary instance must also be shut-down and restarted after the primary. 104 This is necessary because the primary instance will clear and reset the shared memory regions on startup, 105 invalidating the secondary process's pointers. 106 The secondary process can be stopped and restarted without affecting the primary process. 107 108How the Application Works 109^^^^^^^^^^^^^^^^^^^^^^^^^ 110 111This application uses two queues and a single memory pool created in the primary process. 112The secondary process then uses lookup functions to attach to these objects. 113 114.. literalinclude:: ../../../examples/multi_process/simple_mp/main.c 115 :language: c 116 :start-after: Start of ring structure. 8< 117 :end-before: >8 End of ring structure. 118 :dedent: 1 119 120Note, however, that the named ring structure used as send_ring in the primary process is the recv_ring in the secondary process. 121 122The application has two threads: 123 124sender 125 Reads from stdin, converts them to messages, and enqueues them to the ring. 126 127receiver 128 Dequeues any messages on the ring, prints them, then frees the buffer. 129 130Symmetric Multi-process Example 131~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 132 133The symmetric multi process example demonstrates how a set of processes can run in parallel, 134with each process performing the same set of packet- processing operations. 135The following diagram shows the data-flow through the application, using two processes. 136 137.. _figure_sym_multi_proc_app: 138 139.. figure:: img/sym_multi_proc_app.* 140 141 Example Data Flow in a Symmetric Multi-process Application 142 143Each process reads packets from each of the network ports in use. 144RSS distributes incoming packets on each port to different hardware Rx queues. 145Each process reads a different RX queue on each port and so does not contend with any other process for that queue access. 146Each process writes outgoing packets to a different Tx queue on each port. 147 148Running the Application 149^^^^^^^^^^^^^^^^^^^^^^^ 150 151The first instance of the ``symmetric_mp`` process is the primary instance, with the EAL arguments: 152 153``-p <portmask>`` 154 The ``portmask`` is a hexadecimal bitmask of what ports on the system are to be used. 155 For example: ``-p 3`` to use ports 0 and 1 only. 156 157``--num-procs <N>`` 158 The ``N`` is the total number of ``symmetric_mp`` instances 159 that will be run side-by-side to perform packet processing. 160 This parameter is used to configure the appropriate number of receive queues on each network port. 161 162``--proc-id <n>`` 163 Where ``n`` is a numeric value in the range ``0 <= n < N`` 164 (number of processes, specified above). 165 This identifies which ``symmetric_mp`` instance is being run, 166 so that each process can read a unique receive queue on each network port. 167 168The secondary instance must be started with similar EAL parameters. 169Example: 170 171.. code-block:: console 172 173 # ./<build_dir>/examples/dpdk-symmetric_mp -l 1 -n 4 --proc-type=auto -- -p 3 --num-procs=4 --proc-id=0 174 # ./<build_dir>/examples/dpdk-symmetric_mp -l 2 -n 4 --proc-type=auto -- -p 3 --num-procs=4 --proc-id=1 175 # ./<build_dir>/examples/dpdk-symmetric_mp -l 3 -n 4 --proc-type=auto -- -p 3 --num-procs=4 --proc-id=2 176 # ./<build_dir>/examples/dpdk-symmetric_mp -l 4 -n 4 --proc-type=auto -- -p 3 --num-procs=4 --proc-id=3 177 178.. note:: 179 180 In the above example, ``auto`` is used so the first instance becomes the primary process. 181 182How the Application Works 183^^^^^^^^^^^^^^^^^^^^^^^^^ 184 185The primary instance creates the memory pool and initializes the network ports. 186 187.. literalinclude:: ../../../examples/multi_process/symmetric_mp/main.c 188 :language: c 189 :start-after: Primary instance initialized. 8< 190 :end-before: >8 End of primary instance initialization. 191 :dedent: 1 192 193The secondary instance gets the port information and exported by the primary process. 194The memory pool is accessed by doing a lookup for it by name: 195 196.. code-block:: c 197 198 if (proc_type == RTE_PROC_SECONDARY) 199 mp = rte_mempool_lookup(_SMP_MBUF_POOL); 200 else 201 mp = rte_mempool_create(_SMP_MBUF_POOL, NB_MBUFS, MBUF_SIZE, ... ) 202 203The main loop of each process, both primary and secondary, is the same. 204Each process reads from each port using the queue corresponding to its proc-id parameter, 205and writes to the corresponding transmit queue on the output port. 206 207Client-Server Multi-process Example 208~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 209 210The example multi-process application demonstrates a client-server type multi-process design. 211A single server process receives a set of packets from the ports 212and distributes these packets using round-robin ordering to the client processes, 213Each client processes packets and does level-2 forwarding 214by sending each packet out on a different network port. 215 216The following diagram shows the data-flow through the application, using two client processes. 217 218.. _figure_client_svr_sym_multi_proc_app: 219 220.. figure:: img/client_svr_sym_multi_proc_app.* 221 222 Example Data Flow in a Client-Server Symmetric Multi-process Application 223 224 225Running the Application 226^^^^^^^^^^^^^^^^^^^^^^^ 227 228The server process must be run as the primary process to set up all memory structures. 229In addition to the EAL parameters, the application-specific parameters are: 230 231* -p <portmask >, where portmask is a hexadecimal bitmask of what ports on the system are to be used. 232 For example: -p 3 to use ports 0 and 1 only. 233 234* -n <num-clients>, where the num-clients parameter is the number of client processes that will process the packets received 235 by the server application. 236 237.. note:: 238 239 In the server process, has a single thread using the lowest numbered lcore 240 in the coremask/corelist, performs all packet I/O. 241 If coremask/corelist parameter specifies with more than a single lcore bit set, 242 an additional lcore will be used for a thread to print packet count periodically. 243 244The server application stores configuration data in shared memory, 245including the network ports used. 246The only application parameter needed by a client process is its client instance ID. 247To run a server application on lcore 1 (with lcore 2 printing statistics) 248along with two client processes running on lcores 3 and 4, 249the commands are: 250 251.. code-block:: console 252 253 # ./<build_dir>/examples/dpdk-mp_server -l 1-2 -n 4 -- -p 3 -n 2 254 # ./<build_dir>/examples/dpdk-mp_client -l 3 -n 4 --proc-type=auto -- -n 0 255 # ./<build_dir>/examples/dpdk-mp_client -l 4 -n 4 --proc-type=auto -- -n 1 256 257.. note:: 258 259 If the server application dies and needs to be restarted, all client applications also need to be restarted, 260 as there is no support in the server application for it to run as a secondary process. 261 Any client processes that need restarting can be restarted without affecting the server process. 262 263How the Application Works 264^^^^^^^^^^^^^^^^^^^^^^^^^ 265 266The server (primary) process performs the initialization of network port and data structure 267and stores its port configuration data in a memory zone in hugepage shared memory. 268The client process does not need the portmask parameter passed in via the command line. 269The server process is the primary process, and the client processes are secondary processes. 270 271The server operates by reading packets from each network port 272and distributing those packets to the client queues. 273The client reads from the ring and routes the packet to a different network port. 274The routing used is very simple: all packets received on the first NIC port 275are transmitted back out on the second port and vice versa. 276Similarly, packets are routed between the 3rd and 4th network ports and so on. 277