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