xref: /dpdk/doc/guides/sample_app_ug/multi_process.rst (revision d0dff9ba445e47199a420dd4b5451ec2860d3d3b)
1*d0dff9baSBernard Iremonger..  BSD LICENSE
2*d0dff9baSBernard Iremonger    Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
3*d0dff9baSBernard Iremonger    All rights reserved.
4*d0dff9baSBernard Iremonger
5*d0dff9baSBernard Iremonger    Redistribution and use in source and binary forms, with or without
6*d0dff9baSBernard Iremonger    modification, are permitted provided that the following conditions
7*d0dff9baSBernard Iremonger    are met:
8*d0dff9baSBernard Iremonger
9*d0dff9baSBernard Iremonger    * Redistributions of source code must retain the above copyright
10*d0dff9baSBernard Iremonger    notice, this list of conditions and the following disclaimer.
11*d0dff9baSBernard Iremonger    * Redistributions in binary form must reproduce the above copyright
12*d0dff9baSBernard Iremonger    notice, this list of conditions and the following disclaimer in
13*d0dff9baSBernard Iremonger    the documentation and/or other materials provided with the
14*d0dff9baSBernard Iremonger    distribution.
15*d0dff9baSBernard Iremonger    * Neither the name of Intel Corporation nor the names of its
16*d0dff9baSBernard Iremonger    contributors may be used to endorse or promote products derived
17*d0dff9baSBernard Iremonger    from this software without specific prior written permission.
18*d0dff9baSBernard Iremonger
19*d0dff9baSBernard Iremonger    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20*d0dff9baSBernard Iremonger    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21*d0dff9baSBernard Iremonger    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22*d0dff9baSBernard Iremonger    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23*d0dff9baSBernard Iremonger    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24*d0dff9baSBernard Iremonger    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25*d0dff9baSBernard Iremonger    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26*d0dff9baSBernard Iremonger    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27*d0dff9baSBernard Iremonger    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28*d0dff9baSBernard Iremonger    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29*d0dff9baSBernard Iremonger    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30*d0dff9baSBernard Iremonger
31*d0dff9baSBernard IremongerMulti-process Sample Application
32*d0dff9baSBernard Iremonger================================
33*d0dff9baSBernard Iremonger
34*d0dff9baSBernard IremongerThis chapter describes the example applications for multi-processing that are included in the Intel® DPDK.
35*d0dff9baSBernard Iremonger
36*d0dff9baSBernard IremongerExample Applications
37*d0dff9baSBernard Iremonger--------------------
38*d0dff9baSBernard Iremonger
39*d0dff9baSBernard IremongerBuilding the Sample Applications
40*d0dff9baSBernard Iremonger~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
41*d0dff9baSBernard Iremonger
42*d0dff9baSBernard IremongerThe multi-process example applications are built in the same way as other sample applications,
43*d0dff9baSBernard Iremongerand as documented in the *Intel® DPDK Getting Started Guide*.
44*d0dff9baSBernard IremongerTo build all the example applications:
45*d0dff9baSBernard Iremonger
46*d0dff9baSBernard Iremonger#.  Set RTE_SDK and go to the example directory:
47*d0dff9baSBernard Iremonger
48*d0dff9baSBernard Iremonger    .. code-block:: console
49*d0dff9baSBernard Iremonger
50*d0dff9baSBernard Iremonger        export RTE_SDK=/path/to/rte_sdk
51*d0dff9baSBernard Iremonger        cd ${RTE_SDK}/examples/multi_process
52*d0dff9baSBernard Iremonger
53*d0dff9baSBernard Iremonger#.  Set the target (a default target will be used if not specified). For example:
54*d0dff9baSBernard Iremonger
55*d0dff9baSBernard Iremonger    .. code-block:: console
56*d0dff9baSBernard Iremonger
57*d0dff9baSBernard Iremonger        export RTE_TARGET=x86_64-native-linuxapp-gcc
58*d0dff9baSBernard Iremonger
59*d0dff9baSBernard Iremonger    See the *Intel® DPDK Getting Started Guide* for possible RTE_TARGET values.
60*d0dff9baSBernard Iremonger
61*d0dff9baSBernard Iremonger#.  Build the applications:
62*d0dff9baSBernard Iremonger
63*d0dff9baSBernard Iremonger    .. code-block:: console
64*d0dff9baSBernard Iremonger
65*d0dff9baSBernard Iremonger        make
66*d0dff9baSBernard Iremonger
67*d0dff9baSBernard Iremonger.. note::
68*d0dff9baSBernard Iremonger
69*d0dff9baSBernard Iremonger    If just a specific multi-process application needs to be built,
70*d0dff9baSBernard Iremonger    the final make command can be run just in that application's directory,
71*d0dff9baSBernard Iremonger    rather than at the top-level multi-process directory.
72*d0dff9baSBernard Iremonger
73*d0dff9baSBernard IremongerBasic Multi-process Example
74*d0dff9baSBernard Iremonger~~~~~~~~~~~~~~~~~~~~~~~~~~~
75*d0dff9baSBernard Iremonger
76*d0dff9baSBernard IremongerThe examples/simple_mp folder in the Intel® DPDK release contains a basic example application to demonstrate how
77*d0dff9baSBernard Iremongertwo Intel® DPDK processes can work together using queues and memory pools to share information.
78*d0dff9baSBernard Iremonger
79*d0dff9baSBernard IremongerRunning the Application
80*d0dff9baSBernard Iremonger^^^^^^^^^^^^^^^^^^^^^^^
81*d0dff9baSBernard Iremonger
82*d0dff9baSBernard IremongerTo run the application, start one copy of the simple_mp binary in one terminal,
83*d0dff9baSBernard Iremongerpassing at least two cores in the coremask, as follows:
84*d0dff9baSBernard Iremonger
85*d0dff9baSBernard Iremonger.. code-block:: console
86*d0dff9baSBernard Iremonger
87*d0dff9baSBernard Iremonger    ./build/simple_mp -c 3 -n 4 --proc-type=primary
88*d0dff9baSBernard Iremonger
89*d0dff9baSBernard IremongerFor the first Intel® DPDK process run, the proc-type flag can be omitted or set to auto,
90*d0dff9baSBernard Iremongersince all Intel® DPDK processes will default to being a primary instance,
91*d0dff9baSBernard Iremongermeaning they have control over the hugepage shared memory regions.
92*d0dff9baSBernard IremongerThe process should start successfully and display a command prompt as follows:
93*d0dff9baSBernard Iremonger
94*d0dff9baSBernard Iremonger.. code-block:: console
95*d0dff9baSBernard Iremonger
96*d0dff9baSBernard Iremonger    $ ./build/simple_mp -c 3 -n 4 --proc-type=primary
97*d0dff9baSBernard Iremonger    EAL: coremask set to 3
98*d0dff9baSBernard Iremonger    EAL: Detected lcore 0 on socket 0
99*d0dff9baSBernard Iremonger    EAL: Detected lcore 1 on socket 0
100*d0dff9baSBernard Iremonger    EAL: Detected lcore 2 on socket 0
101*d0dff9baSBernard Iremonger    EAL: Detected lcore 3 on socket 0
102*d0dff9baSBernard Iremonger    ...
103*d0dff9baSBernard Iremonger
104*d0dff9baSBernard Iremonger    EAL: Requesting 2 pages of size 1073741824
105*d0dff9baSBernard Iremonger    EAL: Requesting 768 pages of size 2097152
106*d0dff9baSBernard Iremonger    EAL: Ask a virtual area of 0x40000000 bytes
107*d0dff9baSBernard Iremonger    EAL: Virtual area found at 0x7ff200000000 (size = 0x40000000)
108*d0dff9baSBernard Iremonger    ...
109*d0dff9baSBernard Iremonger
110*d0dff9baSBernard Iremonger    EAL: check igb_uio module
111*d0dff9baSBernard Iremonger    EAL: check module finished
112*d0dff9baSBernard Iremonger    EAL: Master core 0 is ready (tid=54e41820)
113*d0dff9baSBernard Iremonger    EAL: Core 1 is ready (tid=53b32700)
114*d0dff9baSBernard Iremonger
115*d0dff9baSBernard Iremonger    Starting core 1
116*d0dff9baSBernard Iremonger
117*d0dff9baSBernard Iremonger    simple_mp >
118*d0dff9baSBernard Iremonger
119*d0dff9baSBernard IremongerTo run the secondary process to communicate with the primary process,
120*d0dff9baSBernard Iremongeragain run the same binary setting at least two cores in the coremask:
121*d0dff9baSBernard Iremonger
122*d0dff9baSBernard Iremonger.. code-block:: console
123*d0dff9baSBernard Iremonger
124*d0dff9baSBernard Iremonger    ./build/simple_mp -c C -n 4 --proc-type=secondary
125*d0dff9baSBernard Iremonger
126*d0dff9baSBernard IremongerWhen running a secondary process such as that shown above, the proc-type parameter can again be specified as auto.
127*d0dff9baSBernard IremongerHowever, omitting the parameter altogether will cause the process to try and start as a primary rather than secondary process.
128*d0dff9baSBernard Iremonger
129*d0dff9baSBernard IremongerOnce the process type is specified correctly,
130*d0dff9baSBernard Iremongerthe process starts up, displaying largely similar status messages to the primary instance as it initializes.
131*d0dff9baSBernard IremongerOnce again, you will be presented with a command prompt.
132*d0dff9baSBernard Iremonger
133*d0dff9baSBernard IremongerOnce both processes are running, messages can be sent between them using the send command.
134*d0dff9baSBernard IremongerAt any stage, either process can be terminated using the quit command.
135*d0dff9baSBernard Iremonger
136*d0dff9baSBernard Iremonger.. code-block:: console
137*d0dff9baSBernard Iremonger
138*d0dff9baSBernard Iremonger   EAL: Master core 10 is ready (tid=b5f89820)           EAL: Master core 8 is ready (tid=864a3820)
139*d0dff9baSBernard Iremonger   EAL: Core 11 is ready (tid=84ffe700)                  EAL: Core 9 is ready (tid=85995700)
140*d0dff9baSBernard Iremonger   Starting core 11                                      Starting core 9
141*d0dff9baSBernard Iremonger   simple_mp > send hello_secondary                      simple_mp > core 9: Received 'hello_secondary'
142*d0dff9baSBernard Iremonger   simple_mp > core 11: Received 'hello_primary'         simple_mp > send hello_primary
143*d0dff9baSBernard Iremonger   simple_mp > quit                                      simple_mp > quit
144*d0dff9baSBernard Iremonger
145*d0dff9baSBernard Iremonger.. note::
146*d0dff9baSBernard Iremonger
147*d0dff9baSBernard Iremonger    If the primary instance is terminated, the secondary instance must also be shut-down and restarted after the primary.
148*d0dff9baSBernard Iremonger    This is necessary because the primary instance will clear and reset the shared memory regions on startup,
149*d0dff9baSBernard Iremonger    invalidating the secondary process's pointers.
150*d0dff9baSBernard Iremonger    The secondary process can be stopped and restarted without affecting the primary process.
151*d0dff9baSBernard Iremonger
152*d0dff9baSBernard IremongerHow the Application Works
153*d0dff9baSBernard Iremonger^^^^^^^^^^^^^^^^^^^^^^^^^
154*d0dff9baSBernard Iremonger
155*d0dff9baSBernard IremongerThe core of this example application is based on using two queues and a single memory pool in shared memory.
156*d0dff9baSBernard IremongerThese three objects are created at startup by the primary process,
157*d0dff9baSBernard Iremongersince the secondary process cannot create objects in memory as it cannot reserve memory zones,
158*d0dff9baSBernard Iremongerand the secondary process then uses lookup functions to attach to these objects as it starts up.
159*d0dff9baSBernard Iremonger
160*d0dff9baSBernard Iremonger.. code-block:: c
161*d0dff9baSBernard Iremonger
162*d0dff9baSBernard Iremonger    if (rte_eal_process_type() == RTE_PROC_PRIMARY){
163*d0dff9baSBernard Iremonger        send_ring = rte_ring_create(_PRI_2_SEC, ring_size, SOCKET0, flags);
164*d0dff9baSBernard Iremonger        recv_ring = rte_ring_create(_SEC_2_PRI, ring_size, SOCKET0, flags);
165*d0dff9baSBernard Iremonger        message_pool = rte_mempool_create(_MSG_POOL, pool_size, string_size, pool_cache, priv_data_sz, NULL, NULL, NULL, NULL, SOCKET0, flags);
166*d0dff9baSBernard Iremonger    } else {
167*d0dff9baSBernard Iremonger        recv_ring = rte_ring_lookup(_PRI_2_SEC);
168*d0dff9baSBernard Iremonger        send_ring = rte_ring_lookup(_SEC_2_PRI);
169*d0dff9baSBernard Iremonger        message_pool = rte_mempool_lookup(_MSG_POOL);
170*d0dff9baSBernard Iremonger    }
171*d0dff9baSBernard Iremonger
172*d0dff9baSBernard IremongerNote, however, that the named ring structure used as send_ring in the primary process is the recv_ring in the secondary process.
173*d0dff9baSBernard Iremonger
174*d0dff9baSBernard IremongerOnce the rings and memory pools are all available in both the primary and secondary processes,
175*d0dff9baSBernard Iremongerthe application simply dedicates two threads to sending and receiving messages respectively.
176*d0dff9baSBernard IremongerThe receive thread simply dequeues any messages on the receive ring, prints them,
177*d0dff9baSBernard Iremongerand frees the buffer space used by the messages back to the memory pool.
178*d0dff9baSBernard IremongerThe send thread makes use of the command-prompt library to interactively request user input for messages to send.
179*d0dff9baSBernard IremongerOnce a send command is issued by the user, a buffer is allocated from the memory pool, filled in with the message contents,
180*d0dff9baSBernard Iremongerthen enqueued on the appropriate rte_ring.
181*d0dff9baSBernard Iremonger
182*d0dff9baSBernard IremongerSymmetric Multi-process Example
183*d0dff9baSBernard Iremonger~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
184*d0dff9baSBernard Iremonger
185*d0dff9baSBernard IremongerThe second example of Intel® DPDK multi-process support demonstrates how a set of processes can run in parallel,
186*d0dff9baSBernard Iremongerwith each process performing the same set of packet- processing operations.
187*d0dff9baSBernard Iremonger(Since each process is identical in functionality to the others,
188*d0dff9baSBernard Iremongerwe refer to this as symmetric multi-processing, to differentiate it from asymmetric multi- processing -
189*d0dff9baSBernard Iremongersuch as a client-server mode of operation seen in the next example,
190*d0dff9baSBernard Iremongerwhere different processes perform different tasks, yet co-operate to form a packet-processing system.)
191*d0dff9baSBernard IremongerThe following diagram shows the data-flow through the application, using two processes.
192*d0dff9baSBernard Iremonger
193*d0dff9baSBernard Iremonger.. _figure_6:
194*d0dff9baSBernard Iremonger
195*d0dff9baSBernard Iremonger**Figure 6. Example Data Flow in a Symmetric Multi-process Application**
196*d0dff9baSBernard Iremonger
197*d0dff9baSBernard Iremonger.. image9_png has been renamed
198*d0dff9baSBernard Iremonger
199*d0dff9baSBernard Iremonger|sym_multi_proc_app|
200*d0dff9baSBernard Iremonger
201*d0dff9baSBernard IremongerAs the diagram shows, each process reads packets from each of the network ports in use.
202*d0dff9baSBernard IremongerRSS is used to distribute incoming packets on each port to different hardware RX queues.
203*d0dff9baSBernard IremongerEach process reads a different RX queue on each port and so does not contend with any other process for that queue access.
204*d0dff9baSBernard IremongerSimilarly, each process writes outgoing packets to a different TX queue on each port.
205*d0dff9baSBernard Iremonger
206*d0dff9baSBernard IremongerRunning the Application
207*d0dff9baSBernard Iremonger^^^^^^^^^^^^^^^^^^^^^^^
208*d0dff9baSBernard Iremonger
209*d0dff9baSBernard IremongerAs with the simple_mp example, the first instance of the symmetric_mp process must be run as the primary instance,
210*d0dff9baSBernard Iremongerthough with a number of other application- specific parameters also provided after the EAL arguments.
211*d0dff9baSBernard IremongerThese additional parameters are:
212*d0dff9baSBernard Iremonger
213*d0dff9baSBernard Iremonger*   -p <portmask>, where portmask is a hexadecimal bitmask of what ports on the system are to be used.
214*d0dff9baSBernard Iremonger    For example: -p 3 to use ports 0 and 1 only.
215*d0dff9baSBernard Iremonger
216*d0dff9baSBernard Iremonger*   --num-procs <N>, where N is the total number of symmetric_mp instances that will be run side-by-side to perform packet processing.
217*d0dff9baSBernard Iremonger    This parameter is used to configure the appropriate number of receive queues on each network port.
218*d0dff9baSBernard Iremonger
219*d0dff9baSBernard Iremonger*   --proc-id <n>, where n is a numeric value in the range 0 <= n < N (number of processes, specified above).
220*d0dff9baSBernard Iremonger    This identifies which symmetric_mp instance is being run, so that each process can read a unique receive queue on each network port.
221*d0dff9baSBernard Iremonger
222*d0dff9baSBernard IremongerThe secondary symmetric_mp instances must also have these parameters specified,
223*d0dff9baSBernard Iremongerand the first two must be the same as those passed to the primary instance, or errors result.
224*d0dff9baSBernard Iremonger
225*d0dff9baSBernard IremongerFor example, to run a set of four symmetric_mp instances, running on lcores 1-4,
226*d0dff9baSBernard Iremongerall performing level-2 forwarding of packets between ports 0 and 1,
227*d0dff9baSBernard Iremongerthe following commands can be used (assuming run as root):
228*d0dff9baSBernard Iremonger
229*d0dff9baSBernard Iremonger.. code-block:: console
230*d0dff9baSBernard Iremonger
231*d0dff9baSBernard Iremonger    # ./build/symmetric_mp -c 2 -n 4 --proc-type=auto -- -p 3 --num-procs=4 --proc-id=0
232*d0dff9baSBernard Iremonger    # ./build/symmetric_mp -c 4 -n 4 --proc-type=auto -- -p 3 --num-procs=4 --proc-id=1
233*d0dff9baSBernard Iremonger    # ./build/symmetric_mp -c 8 -n 4 --proc-type=auto -- -p 3 --num-procs=4 --proc-id=2
234*d0dff9baSBernard Iremonger    # ./build/symmetric_mp -c 10 -n 4 --proc-type=auto -- -p 3 --num-procs=4 --proc-id=3
235*d0dff9baSBernard Iremonger
236*d0dff9baSBernard Iremonger.. note::
237*d0dff9baSBernard Iremonger
238*d0dff9baSBernard Iremonger    In the above example, the process type can be explicitly specified as primary or secondary, rather than auto.
239*d0dff9baSBernard Iremonger    When using auto, the first process run creates all the memory structures needed for all processes -
240*d0dff9baSBernard Iremonger    irrespective of whether it has a proc-id of 0, 1, 2 or 3.
241*d0dff9baSBernard Iremonger
242*d0dff9baSBernard Iremonger.. note::
243*d0dff9baSBernard Iremonger
244*d0dff9baSBernard Iremonger    For the symmetric multi-process example, since all processes work in the same manner,
245*d0dff9baSBernard Iremonger    once the hugepage shared memory and the network ports are initialized,
246*d0dff9baSBernard Iremonger    it is not necessary to restart all processes if the primary instance dies.
247*d0dff9baSBernard Iremonger    Instead, that process can be restarted as a secondary,
248*d0dff9baSBernard Iremonger    by explicitly setting the proc-type to secondary on the command line.
249*d0dff9baSBernard Iremonger    (All subsequent instances launched will also need this explicitly specified,
250*d0dff9baSBernard Iremonger    as auto-detection will detect no primary processes running and therefore attempt to re-initialize shared memory.)
251*d0dff9baSBernard Iremonger
252*d0dff9baSBernard IremongerHow the Application Works
253*d0dff9baSBernard Iremonger^^^^^^^^^^^^^^^^^^^^^^^^^
254*d0dff9baSBernard Iremonger
255*d0dff9baSBernard IremongerThe initialization calls in both the primary and secondary instances are the same for the most part,
256*d0dff9baSBernard Iremongercalling the rte_eal_init(), 1 G and 10 G driver initialization and then rte_eal_pci_probe() functions.
257*d0dff9baSBernard IremongerThereafter, the initialization done depends on whether the process is configured as a primary or secondary instance.
258*d0dff9baSBernard Iremonger
259*d0dff9baSBernard IremongerIn the primary instance, a memory pool is created for the packet mbufs and the network ports to be used are initialized -
260*d0dff9baSBernard Iremongerthe number of RX and TX queues per port being determined by the num-procs parameter passed on the command-line.
261*d0dff9baSBernard IremongerThe structures for the initialized network ports are stored in shared memory and
262*d0dff9baSBernard Iremongertherefore will be accessible by the secondary process as it initializes.
263*d0dff9baSBernard Iremonger
264*d0dff9baSBernard Iremonger.. code-block:: c
265*d0dff9baSBernard Iremonger
266*d0dff9baSBernard Iremonger    if (num_ports & 1)
267*d0dff9baSBernard Iremonger       rte_exit(EXIT_FAILURE, "Application must use an even number of ports\n");
268*d0dff9baSBernard Iremonger
269*d0dff9baSBernard Iremonger    for(i = 0; i < num_ports; i++){
270*d0dff9baSBernard Iremonger        if(proc_type == RTE_PROC_PRIMARY)
271*d0dff9baSBernard Iremonger            if (smp_port_init(ports[i], mp, (uint16_t)num_procs) < 0)
272*d0dff9baSBernard Iremonger                rte_exit(EXIT_FAILURE, "Error initialising ports\n");
273*d0dff9baSBernard Iremonger    }
274*d0dff9baSBernard Iremonger
275*d0dff9baSBernard IremongerIn the secondary instance, rather than initializing the network ports, the port information exported by the primary process is used,
276*d0dff9baSBernard Iremongergiving the secondary process access to the hardware and software rings for each network port.
277*d0dff9baSBernard IremongerSimilarly, the memory pool of mbufs is accessed by doing a lookup for it by name:
278*d0dff9baSBernard Iremonger
279*d0dff9baSBernard Iremonger.. code-block:: c
280*d0dff9baSBernard Iremonger
281*d0dff9baSBernard Iremonger    mp = (proc_type == RTE_PROC_SECONDARY) ? rte_mempool_lookup(_SMP_MBUF_POOL) : rte_mempool_create(_SMP_MBUF_POOL, NB_MBUFS, MBUF_SIZE, ... )
282*d0dff9baSBernard Iremonger
283*d0dff9baSBernard IremongerOnce this initialization is complete, the main loop of each process, both primary and secondary,
284*d0dff9baSBernard Iremongeris exactly the same - each process reads from each port using the queue corresponding to its proc-id parameter,
285*d0dff9baSBernard Iremongerand writes to the corresponding transmit queue on the output port.
286*d0dff9baSBernard Iremonger
287*d0dff9baSBernard IremongerClient-Server Multi-process Example
288*d0dff9baSBernard Iremonger~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
289*d0dff9baSBernard Iremonger
290*d0dff9baSBernard IremongerThe third example multi-process application included with the Intel® DPDK shows how one can
291*d0dff9baSBernard Iremongeruse a client-server type multi-process design to do packet processing.
292*d0dff9baSBernard IremongerIn this example, a single server process performs the packet reception from the ports being used and
293*d0dff9baSBernard Iremongerdistributes these packets using round-robin ordering among a set of client  processes,
294*d0dff9baSBernard Iremongerwhich perform the actual packet processing.
295*d0dff9baSBernard IremongerIn this case, the client applications just perform level-2 forwarding of packets by sending each packet out on a different network port.
296*d0dff9baSBernard Iremonger
297*d0dff9baSBernard IremongerThe following diagram shows the data-flow through the application, using two client processes.
298*d0dff9baSBernard Iremonger
299*d0dff9baSBernard Iremonger.. _figure_7:
300*d0dff9baSBernard Iremonger
301*d0dff9baSBernard Iremonger**Figure 7. Example Data Flow in a Client-Server Symmetric Multi-process Application**
302*d0dff9baSBernard Iremonger
303*d0dff9baSBernard Iremonger.. image10_png has been renamed
304*d0dff9baSBernard Iremonger
305*d0dff9baSBernard Iremonger|client_svr_sym_multi_proc_app|
306*d0dff9baSBernard Iremonger
307*d0dff9baSBernard IremongerRunning the Application
308*d0dff9baSBernard Iremonger^^^^^^^^^^^^^^^^^^^^^^^
309*d0dff9baSBernard Iremonger
310*d0dff9baSBernard IremongerThe server process must be run initially as the primary process to set up all memory structures for use by the clients.
311*d0dff9baSBernard IremongerIn addition to the EAL parameters, the application- specific parameters are:
312*d0dff9baSBernard Iremonger
313*d0dff9baSBernard Iremonger*   -p <portmask >, where portmask is a hexadecimal bitmask of what ports on the system are to be used.
314*d0dff9baSBernard Iremonger    For example: -p 3 to use ports 0 and 1 only.
315*d0dff9baSBernard Iremonger
316*d0dff9baSBernard Iremonger*   -n <num-clients>, where the num-clients parameter is the number of client processes that will process the packets received
317*d0dff9baSBernard Iremonger    by the server application.
318*d0dff9baSBernard Iremonger
319*d0dff9baSBernard Iremonger.. note::
320*d0dff9baSBernard Iremonger
321*d0dff9baSBernard Iremonger    In the server process, a single thread, the master thread, that is, the lowest numbered lcore in the coremask, performs all packet I/O.
322*d0dff9baSBernard Iremonger    If a coremask is specified with more than a single lcore bit set in it,
323*d0dff9baSBernard Iremonger    an additional lcore will be used for a thread to periodically print packet count statistics.
324*d0dff9baSBernard Iremonger
325*d0dff9baSBernard IremongerSince the server application stores configuration data in shared memory, including the network ports to be used,
326*d0dff9baSBernard Iremongerthe only application parameter needed by a client process is its client instance ID.
327*d0dff9baSBernard IremongerTherefore, to run a server application on lcore 1 (with lcore 2 printing statistics) along with two client processes running on lcores 3 and 4,
328*d0dff9baSBernard Iremongerthe following commands could be used:
329*d0dff9baSBernard Iremonger
330*d0dff9baSBernard Iremonger.. code-block:: console
331*d0dff9baSBernard Iremonger
332*d0dff9baSBernard Iremonger    # ./mp_server/build/mp_server -c 6 -n 4 -- -p 3 -n 2
333*d0dff9baSBernard Iremonger    # ./mp_client/build/mp_client -c 8 -n 4 --proc-type=auto -- -n 0
334*d0dff9baSBernard Iremonger    # ./mp_client/build/mp_client -c 10 -n 4 --proc-type=auto -- -n 1
335*d0dff9baSBernard Iremonger
336*d0dff9baSBernard Iremonger.. note::
337*d0dff9baSBernard Iremonger
338*d0dff9baSBernard Iremonger    If the server application dies and needs to be restarted, all client applications also need to be restarted,
339*d0dff9baSBernard Iremonger    as there is no support in the server application for it to run as a secondary process.
340*d0dff9baSBernard Iremonger    Any client processes that need restarting can be restarted without affecting the server process.
341*d0dff9baSBernard Iremonger
342*d0dff9baSBernard IremongerHow the Application Works
343*d0dff9baSBernard Iremonger^^^^^^^^^^^^^^^^^^^^^^^^^
344*d0dff9baSBernard Iremonger
345*d0dff9baSBernard IremongerThe server process performs the network port and data structure initialization much as the symmetric multi-process application does when run as primary.
346*d0dff9baSBernard IremongerOne additional enhancement in this sample application is that the server process stores its port configuration data in a memory zone in hugepage shared memory.
347*d0dff9baSBernard IremongerThis eliminates the need for the client processes to have the portmask parameter passed into them on the command line,
348*d0dff9baSBernard Iremongeras is done for the symmetric multi-process application, and therefore eliminates mismatched parameters as a potential source of errors.
349*d0dff9baSBernard Iremonger
350*d0dff9baSBernard IremongerIn the same way that the server process is designed to be run as a primary process instance only,
351*d0dff9baSBernard Iremongerthe client processes are designed to be run as secondary instances only.
352*d0dff9baSBernard IremongerThey have no code to attempt to create shared memory objects.
353*d0dff9baSBernard IremongerInstead, handles to all needed rings and memory pools are obtained via calls to rte_ring_lookup() and rte_mempool_lookup().
354*d0dff9baSBernard IremongerThe network ports for use by the processes are obtained by loading the network port drivers and probing the PCI bus,
355*d0dff9baSBernard Iremongerwhich will, as in the symmetric multi-process example,
356*d0dff9baSBernard Iremongerautomatically get access to the network ports using the settings already configured by the primary/server process.
357*d0dff9baSBernard Iremonger
358*d0dff9baSBernard IremongerOnce all applications are initialized, the server operates by reading packets from each network port in turn and
359*d0dff9baSBernard Iremongerdistributing those packets to the client queues (software rings, one for each client process) in round-robin order.
360*d0dff9baSBernard IremongerOn the client side, the packets are read from the rings in as big of bursts as possible, then routed out to a different network port.
361*d0dff9baSBernard IremongerThe routing used is very simple. All packets received on the first NIC port are transmitted back out on the second port and vice versa.
362*d0dff9baSBernard IremongerSimilarly, packets are routed between the 3rd and 4th network ports and so on.
363*d0dff9baSBernard IremongerThe sending of packets is done by writing the packets directly to the network ports; they are not transferred back via the server process.
364*d0dff9baSBernard Iremonger
365*d0dff9baSBernard IremongerIn both the server and the client processes, outgoing packets are buffered before being sent,
366*d0dff9baSBernard Iremongerso as to allow the sending of multiple packets in a single burst to improve efficiency.
367*d0dff9baSBernard IremongerFor example, the client process will buffer packets to send,
368*d0dff9baSBernard Iremongeruntil either the buffer is full or until we receive no further packets from the server.
369*d0dff9baSBernard Iremonger
370*d0dff9baSBernard IremongerMaster-slave Multi-process Example
371*d0dff9baSBernard Iremonger~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
372*d0dff9baSBernard Iremonger
373*d0dff9baSBernard IremongerThe fourth example of Intel® DPDK multi-process support demonstrates a master-slave model that
374*d0dff9baSBernard Iremongerprovide the capability of application recovery if a slave process crashes or  meets unexpected conditions.
375*d0dff9baSBernard IremongerIn addition, it also demonstrates the floating process,
376*d0dff9baSBernard Iremongerwhich can run among different cores in contrast to the traditional way of binding a process/thread to a specific CPU core,
377*d0dff9baSBernard Iremongerusing the local cache mechanism of mempool structures.
378*d0dff9baSBernard Iremonger
379*d0dff9baSBernard IremongerThis application performs the same functionality as the L2 Forwarding sample application,
380*d0dff9baSBernard Iremongertherefore this chapter does not cover that part but describes functionality that is introduced in this multi-process example only.
381*d0dff9baSBernard IremongerPlease refer to Chapter 9, "L2 Forwarding Sample Application (in Real and Virtualized Environments)" for more information.
382*d0dff9baSBernard Iremonger
383*d0dff9baSBernard IremongerUnlike previous examples where all processes are started from the command line with input arguments, in this example,
384*d0dff9baSBernard Iremongeronly one process is spawned from the command line and that process creates other processes.
385*d0dff9baSBernard IremongerThe following section describes this in more detail.
386*d0dff9baSBernard Iremonger
387*d0dff9baSBernard IremongerMaster-slave Process Models
388*d0dff9baSBernard Iremonger^^^^^^^^^^^^^^^^^^^^^^^^^^^
389*d0dff9baSBernard Iremonger
390*d0dff9baSBernard IremongerThe process spawned from the command line is called the *master process* in this document.
391*d0dff9baSBernard IremongerA process created by the master is called a *slave process*.
392*d0dff9baSBernard IremongerThe application has only one master process, but could have multiple slave processes.
393*d0dff9baSBernard Iremonger
394*d0dff9baSBernard IremongerOnce the master process begins to run, it tries to initialize all the resources such as
395*d0dff9baSBernard Iremongermemory, CPU cores, driver, ports, and so on, as the other examples do.
396*d0dff9baSBernard IremongerThereafter, it creates slave processes, as shown in the following figure.
397*d0dff9baSBernard Iremonger
398*d0dff9baSBernard Iremonger.. _figure_8:
399*d0dff9baSBernard Iremonger
400*d0dff9baSBernard Iremonger**Figure 8. Master-slave Process Workflow**
401*d0dff9baSBernard Iremonger
402*d0dff9baSBernard Iremonger.. image11_png has been renamed
403*d0dff9baSBernard Iremonger
404*d0dff9baSBernard Iremonger|master_slave_proc|
405*d0dff9baSBernard Iremonger
406*d0dff9baSBernard IremongerThe master process calls the rte_eal_mp_remote_launch() EAL function to launch an application function for each pinned thread through the pipe.
407*d0dff9baSBernard IremongerThen, it waits to check if any slave processes have exited.
408*d0dff9baSBernard IremongerIf so, the process tries to re-initialize the resources that belong to that slave and launch them in the pinned thread entry again.
409*d0dff9baSBernard IremongerThe following section describes the recovery procedures in more detail.
410*d0dff9baSBernard Iremonger
411*d0dff9baSBernard IremongerFor each pinned thread in EAL, after reading any data from the pipe, it tries to call the function that the application specified.
412*d0dff9baSBernard IremongerIn this master specified function, a fork() call creates a slave process that performs the L2 forwarding task.
413*d0dff9baSBernard IremongerThen, the function waits until the slave exits, is killed or crashes. Thereafter, it notifies the master of this event and returns.
414*d0dff9baSBernard IremongerFinally, the EAL pinned thread waits until the new function is launched.
415*d0dff9baSBernard Iremonger
416*d0dff9baSBernard IremongerAfter discussing the master-slave model, it is necessary to mention another issue, global and static variables.
417*d0dff9baSBernard Iremonger
418*d0dff9baSBernard IremongerFor multiple-thread cases, all global and static variables have only one copy and they can be accessed by any thread if applicable.
419*d0dff9baSBernard IremongerSo, they can be used to sync or share data among threads.
420*d0dff9baSBernard Iremonger
421*d0dff9baSBernard IremongerIn the previous examples, each process has separate global and static variables in memory and are independent of each other.
422*d0dff9baSBernard IremongerIf it is necessary to share the knowledge, some communication mechanism should be deployed, such as, memzone, ring, shared memory, and so on.
423*d0dff9baSBernard IremongerThe global or static variables are not a valid approach to share data among processes.
424*d0dff9baSBernard IremongerFor variables in this example, on the one hand, the slave process inherits all the knowledge of these variables after being created by the master.
425*d0dff9baSBernard IremongerOn the other hand, other processes cannot know if one or more processes modifies them after slave creation since that
426*d0dff9baSBernard Iremongeris the nature of a multiple process address space.
427*d0dff9baSBernard IremongerBut this does not mean that these variables cannot be used to share or sync data; it depends on the use case.
428*d0dff9baSBernard IremongerThe following are the possible use cases:
429*d0dff9baSBernard Iremonger
430*d0dff9baSBernard Iremonger#.  The master process starts and initializes a variable and it will never be changed after slave processes created. This case is OK.
431*d0dff9baSBernard Iremonger
432*d0dff9baSBernard Iremonger#.  After the slave processes are created, the master or slave cores need to change a variable, but other processes do not need to know the change.
433*d0dff9baSBernard Iremonger    This case is also OK.
434*d0dff9baSBernard Iremonger
435*d0dff9baSBernard Iremonger#.  After the slave processes are created, the master or a slave needs to change a variable.
436*d0dff9baSBernard Iremonger    In the meantime, one or more other process needs to be aware of the change.
437*d0dff9baSBernard Iremonger    In this case, global and static variables cannot be used to share knowledge. Another communication mechanism is needed.
438*d0dff9baSBernard Iremonger    A simple approach without lock protection can be a heap buffer allocated by rte_malloc or mem zone.
439*d0dff9baSBernard Iremonger
440*d0dff9baSBernard IremongerSlave Process Recovery Mechanism
441*d0dff9baSBernard Iremonger^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
442*d0dff9baSBernard Iremonger
443*d0dff9baSBernard IremongerBefore talking about the recovery mechanism, it is necessary to know what is needed before a new slave instance can run if a previous one exited.
444*d0dff9baSBernard Iremonger
445*d0dff9baSBernard IremongerWhen a slave process exits, the system returns all the resources allocated for this process automatically.
446*d0dff9baSBernard IremongerHowever, this does not include the resources that were allocated by the Intel® DPDK. All the hardware resources are shared among the processes,
447*d0dff9baSBernard Iremongerwhich include memzone, mempool, ring, a heap buffer allocated by the rte_malloc library, and so on.
448*d0dff9baSBernard IremongerIf the new instance runs and the allocated resource is not returned, either resource allocation failed or the hardware resource is lost forever.
449*d0dff9baSBernard Iremonger
450*d0dff9baSBernard IremongerWhen a slave process runs, it may have dependencies on other processes.
451*d0dff9baSBernard IremongerThey could have execution sequence orders; they could share the ring to communicate; they could share the same port for reception and forwarding;
452*d0dff9baSBernard Iremongerthey could use lock structures to do exclusive access in some critical path.
453*d0dff9baSBernard IremongerWhat happens to the dependent process(es) if the peer leaves?
454*d0dff9baSBernard IremongerThe consequence are varied since the dependency cases are complex.
455*d0dff9baSBernard IremongerIt depends on what the processed had shared.
456*d0dff9baSBernard IremongerHowever, it is necessary to notify the peer(s) if one slave exited.
457*d0dff9baSBernard IremongerThen, the peer(s) will be aware of that and wait until the new instance begins to run.
458*d0dff9baSBernard Iremonger
459*d0dff9baSBernard IremongerTherefore, to provide the capability to resume the new slave instance if the previous one exited, it is necessary to provide several mechanisms:
460*d0dff9baSBernard Iremonger
461*d0dff9baSBernard Iremonger#.  Keep a resource list for each slave process.
462*d0dff9baSBernard Iremonger    Before a slave process run, the master should prepare a resource list.
463*d0dff9baSBernard Iremonger    After it exits, the master could either delete the allocated resources and create new ones,
464*d0dff9baSBernard Iremonger    or re-initialize those for use by the new instance.
465*d0dff9baSBernard Iremonger
466*d0dff9baSBernard Iremonger#.  Set up a notification mechanism for slave process exit cases. After the specific slave leaves,
467*d0dff9baSBernard Iremonger    the master should be notified and then help to create a new instance.
468*d0dff9baSBernard Iremonger    This mechanism is provided in Section 15.1.5.1, "Master-slave Process Models".
469*d0dff9baSBernard Iremonger
470*d0dff9baSBernard Iremonger#.  Use a synchronization mechanism among dependent processes.
471*d0dff9baSBernard Iremonger    The master should have the capability to stop or kill slave processes that have a dependency on the one that has exited.
472*d0dff9baSBernard Iremonger    Then, after the new instance of exited slave process begins to run, the dependency ones could resume or run from the start.
473*d0dff9baSBernard Iremonger    The example sends a STOP command to slave processes dependent on the exited one, then they will exit.
474*d0dff9baSBernard Iremonger    Thereafter, the master creates new instances for the exited slave processes.
475*d0dff9baSBernard Iremonger
476*d0dff9baSBernard IremongerThe following diagram describes slave process recovery.
477*d0dff9baSBernard Iremonger
478*d0dff9baSBernard Iremonger.. _figure_9:
479*d0dff9baSBernard Iremonger
480*d0dff9baSBernard Iremonger**Figure 9. Slave Process Recovery Process Flow**
481*d0dff9baSBernard Iremonger
482*d0dff9baSBernard Iremonger.. image12_png has been renamed
483*d0dff9baSBernard Iremonger
484*d0dff9baSBernard Iremonger|slave_proc_recov|
485*d0dff9baSBernard Iremonger
486*d0dff9baSBernard IremongerFloating Process Support
487*d0dff9baSBernard Iremonger^^^^^^^^^^^^^^^^^^^^^^^^
488*d0dff9baSBernard Iremonger
489*d0dff9baSBernard IremongerWhen the Intel® DPDK application runs, there is always a -c option passed in to indicate the cores that are enabled.
490*d0dff9baSBernard IremongerThen, the Intel® DPDK creates a thread for each enabled core.
491*d0dff9baSBernard IremongerBy doing so, it creates a 1:1 mapping between the enabled core and each thread.
492*d0dff9baSBernard IremongerThe enabled core always has an ID, therefore, each thread has a unique core ID in the Intel® DPDK execution environment.
493*d0dff9baSBernard IremongerWith the ID, each thread can easily access the structures or resources exclusively belonging to it without using function parameter passing.
494*d0dff9baSBernard IremongerIt can easily use the rte_lcore_id() function to get the value in every function that is called.
495*d0dff9baSBernard Iremonger
496*d0dff9baSBernard IremongerFor threads/processes not created in that way, either pinned to a core or not, they will not own a unique ID and the
497*d0dff9baSBernard Iremongerrte_lcore_id() function will not work in the correct way.
498*d0dff9baSBernard IremongerHowever, sometimes these threads/processes still need the unique ID mechanism to do easy access on structures or resources.
499*d0dff9baSBernard IremongerFor example, the Intel® DPDK mempool library provides a local cache mechanism
500*d0dff9baSBernard Iremonger(refer to *Intel® DPDK Programmer's Guide* , Section 6.4, "Local Cache")
501*d0dff9baSBernard Iremongerfor fast element allocation and freeing.
502*d0dff9baSBernard IremongerIf using a non-unique ID or a fake one,
503*d0dff9baSBernard Iremongera race condition occurs if two or more threads/ processes with the same core ID try to use the local cache.
504*d0dff9baSBernard Iremonger
505*d0dff9baSBernard IremongerTherefore, unused core IDs from the passing of parameters with the -c option are used to organize the core ID allocation array.
506*d0dff9baSBernard IremongerOnce the floating process is spawned, it tries to allocate a unique core ID from the array and release it on exit.
507*d0dff9baSBernard Iremonger
508*d0dff9baSBernard IremongerA natural way to spawn a floating process is to use the fork() function and allocate a unique core ID from the unused core ID array.
509*d0dff9baSBernard IremongerHowever, it is necessary to write new code to provide a notification mechanism for slave exit
510*d0dff9baSBernard Iremongerand make sure the process recovery mechanism can work with it.
511*d0dff9baSBernard Iremonger
512*d0dff9baSBernard IremongerTo avoid producing redundant code, the Master-Slave process model is still used to spawn floating processes,
513*d0dff9baSBernard Iremongerthen cancel the affinity to specific cores.
514*d0dff9baSBernard IremongerBesides that, clear the core ID assigned to the Intel® DPDK spawning a thread that has a 1:1 mapping with the core mask.
515*d0dff9baSBernard IremongerThereafter, get a new core ID from the unused core ID allocation array.
516*d0dff9baSBernard Iremonger
517*d0dff9baSBernard IremongerRun the Application
518*d0dff9baSBernard Iremonger^^^^^^^^^^^^^^^^^^^
519*d0dff9baSBernard Iremonger
520*d0dff9baSBernard IremongerThis example has a command line similar to the L2 Forwarding sample application with a few differences.
521*d0dff9baSBernard Iremonger
522*d0dff9baSBernard IremongerTo run the application, start one copy of the l2fwd_fork binary in one terminal.
523*d0dff9baSBernard IremongerUnlike the L2 Forwarding example,
524*d0dff9baSBernard Iremongerthis example requires at least three cores since the master process will wait and be accountable for slave process recovery.
525*d0dff9baSBernard IremongerThe command is as follows:
526*d0dff9baSBernard Iremonger
527*d0dff9baSBernard Iremonger.. code-block:: console
528*d0dff9baSBernard Iremonger
529*d0dff9baSBernard Iremonger    #./build/l2fwd_fork -c 1c -n 4 -- -p 3 -f
530*d0dff9baSBernard Iremonger
531*d0dff9baSBernard IremongerThis example provides another -f option to specify the use of floating process.
532*d0dff9baSBernard IremongerIf not specified, the example will use a pinned process to perform the L2 forwarding task.
533*d0dff9baSBernard Iremonger
534*d0dff9baSBernard IremongerTo verify the recovery mechanism, proceed as follows: First, check the PID of the slave processes:
535*d0dff9baSBernard Iremonger
536*d0dff9baSBernard Iremonger.. code-block:: console
537*d0dff9baSBernard Iremonger
538*d0dff9baSBernard Iremonger    #ps -fe | grep l2fwd_fork
539*d0dff9baSBernard Iremonger    root 5136 4843 29 11:11 pts/1 00:00:05 ./build/l2fwd_fork
540*d0dff9baSBernard Iremonger    root 5145 5136 98 11:11 pts/1 00:00:11 ./build/l2fwd_fork
541*d0dff9baSBernard Iremonger    root 5146 5136 98 11:11 pts/1 00:00:11 ./build/l2fwd_fork
542*d0dff9baSBernard Iremonger
543*d0dff9baSBernard IremongerThen, kill one of the slaves:
544*d0dff9baSBernard Iremonger
545*d0dff9baSBernard Iremonger.. code-block:: console
546*d0dff9baSBernard Iremonger
547*d0dff9baSBernard Iremonger    #kill -9 5145
548*d0dff9baSBernard Iremonger
549*d0dff9baSBernard IremongerAfter 1 or 2 seconds, check whether the slave has resumed:
550*d0dff9baSBernard Iremonger
551*d0dff9baSBernard Iremonger.. code-block:: console
552*d0dff9baSBernard Iremonger
553*d0dff9baSBernard Iremonger    #ps -fe | grep l2fwd_fork
554*d0dff9baSBernard Iremonger    root 5136 4843 3 11:11 pts/1 00:00:06 ./build/l2fwd_fork
555*d0dff9baSBernard Iremonger    root 5247 5136 99 11:14 pts/1 00:00:01 ./build/l2fwd_fork
556*d0dff9baSBernard Iremonger    root 5248 5136 99 11:14 pts/1 00:00:01 ./build/l2fwd_fork
557*d0dff9baSBernard Iremonger
558*d0dff9baSBernard IremongerIt can also monitor the traffic generator statics to see whether slave processes have resumed.
559*d0dff9baSBernard Iremonger
560*d0dff9baSBernard IremongerExplanation
561*d0dff9baSBernard Iremonger^^^^^^^^^^^
562*d0dff9baSBernard Iremonger
563*d0dff9baSBernard IremongerAs described in previous sections,
564*d0dff9baSBernard Iremongernot all global and static variables need to change to be accessible in multiple processes;
565*d0dff9baSBernard Iremongerit depends on how they are used.
566*d0dff9baSBernard IremongerIn this example,
567*d0dff9baSBernard Iremongerthe statics info on packets dropped/forwarded/received count needs to be updated by the slave process,
568*d0dff9baSBernard Iremongerand the master needs to see the update and print them out.
569*d0dff9baSBernard IremongerSo, it needs to allocate a heap buffer using rte_zmalloc.
570*d0dff9baSBernard IremongerIn addition, if the -f option is specified,
571*d0dff9baSBernard Iremongeran array is needed to store the allocated core ID for the floating process so that the master can return it
572*d0dff9baSBernard Iremongerafter a slave has exited accidently.
573*d0dff9baSBernard Iremonger
574*d0dff9baSBernard Iremonger.. code-block:: c
575*d0dff9baSBernard Iremonger
576*d0dff9baSBernard Iremonger    static int
577*d0dff9baSBernard Iremonger    l2fwd_malloc_shared_struct(void)
578*d0dff9baSBernard Iremonger    {
579*d0dff9baSBernard Iremonger        port_statistics = rte_zmalloc("port_stat", sizeof(struct l2fwd_port_statistics) * RTE_MAX_ETHPORTS, 0);
580*d0dff9baSBernard Iremonger
581*d0dff9baSBernard Iremonger        if (port_statistics == NULL)
582*d0dff9baSBernard Iremonger            return -1;
583*d0dff9baSBernard Iremonger
584*d0dff9baSBernard Iremonger        /* allocate mapping_id array */
585*d0dff9baSBernard Iremonger
586*d0dff9baSBernard Iremonger        if (float_proc) {
587*d0dff9baSBernard Iremonger            int i;
588*d0dff9baSBernard Iremonger
589*d0dff9baSBernard Iremonger            mapping_id = rte_malloc("mapping_id", sizeof(unsigned) * RTE_MAX_LCORE, 0);
590*d0dff9baSBernard Iremonger            if (mapping_id == NULL)
591*d0dff9baSBernard Iremonger                return -1;
592*d0dff9baSBernard Iremonger
593*d0dff9baSBernard Iremonger            for (i = 0 ;i < RTE_MAX_LCORE; i++)
594*d0dff9baSBernard Iremonger                mapping_id[i] = INVALID_MAPPING_ID;
595*d0dff9baSBernard Iremonger
596*d0dff9baSBernard Iremonger        }
597*d0dff9baSBernard Iremonger        return 0;
598*d0dff9baSBernard Iremonger    }
599*d0dff9baSBernard Iremonger
600*d0dff9baSBernard IremongerFor each slave process, packets are received from one port and forwarded to another port that another slave is operating on.
601*d0dff9baSBernard IremongerIf the other slave exits accidentally, the port it is operating on may not work normally,
602*d0dff9baSBernard Iremongerso the first slave cannot forward packets to that port.
603*d0dff9baSBernard IremongerThere is a dependency on the port in this case. So, the master should recognize the dependency.
604*d0dff9baSBernard IremongerThe following is the code to detect this dependency:
605*d0dff9baSBernard Iremonger
606*d0dff9baSBernard Iremonger.. code-block:: c
607*d0dff9baSBernard Iremonger
608*d0dff9baSBernard Iremonger    for (portid = 0; portid < nb_ports; portid++) {
609*d0dff9baSBernard Iremonger        /* skip ports that are not enabled */
610*d0dff9baSBernard Iremonger
611*d0dff9baSBernard Iremonger        if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
612*d0dff9baSBernard Iremonger            continue;
613*d0dff9baSBernard Iremonger
614*d0dff9baSBernard Iremonger        /* Find pair ports' lcores */
615*d0dff9baSBernard Iremonger
616*d0dff9baSBernard Iremonger        find_lcore = find_pair_lcore = 0;
617*d0dff9baSBernard Iremonger        pair_port = l2fwd_dst_ports[portid];
618*d0dff9baSBernard Iremonger
619*d0dff9baSBernard Iremonger        for (i = 0; i < RTE_MAX_LCORE; i++) {
620*d0dff9baSBernard Iremonger            if (!rte_lcore_is_enabled(i))
621*d0dff9baSBernard Iremonger                continue;
622*d0dff9baSBernard Iremonger
623*d0dff9baSBernard Iremonger            for (j = 0; j < lcore_queue_conf[i].n_rx_port;j++) {
624*d0dff9baSBernard Iremonger                if (lcore_queue_conf[i].rx_port_list[j] == portid) {
625*d0dff9baSBernard Iremonger                    lcore = i;
626*d0dff9baSBernard Iremonger                    find_lcore = 1;
627*d0dff9baSBernard Iremonger                    break;
628*d0dff9baSBernard Iremonger                }
629*d0dff9baSBernard Iremonger
630*d0dff9baSBernard Iremonger                if (lcore_queue_conf[i].rx_port_list[j] == pair_port) {
631*d0dff9baSBernard Iremonger                    pair_lcore = i;
632*d0dff9baSBernard Iremonger                    find_pair_lcore = 1;
633*d0dff9baSBernard Iremonger                    break;
634*d0dff9baSBernard Iremonger                }
635*d0dff9baSBernard Iremonger            }
636*d0dff9baSBernard Iremonger
637*d0dff9baSBernard Iremonger            if (find_lcore && find_pair_lcore)
638*d0dff9baSBernard Iremonger                break;
639*d0dff9baSBernard Iremonger        }
640*d0dff9baSBernard Iremonger
641*d0dff9baSBernard Iremonger        if (!find_lcore || !find_pair_lcore)
642*d0dff9baSBernard Iremonger            rte_exit(EXIT_FAILURE, "Not find port=%d pair\\n", portid);
643*d0dff9baSBernard Iremonger
644*d0dff9baSBernard Iremonger        printf("lcore %u and %u paired\\n", lcore, pair_lcore);
645*d0dff9baSBernard Iremonger
646*d0dff9baSBernard Iremonger        lcore_resource[lcore].pair_id = pair_lcore;
647*d0dff9baSBernard Iremonger        lcore_resource[pair_lcore].pair_id = lcore;
648*d0dff9baSBernard Iremonger    }
649*d0dff9baSBernard Iremonger
650*d0dff9baSBernard IremongerBefore launching the slave process,
651*d0dff9baSBernard Iremongerit is necessary to set up the communication channel between the master and slave so that
652*d0dff9baSBernard Iremongerthe master can notify the slave if its peer process with the dependency exited.
653*d0dff9baSBernard IremongerIn addition, the master needs to register a callback function in the case where a specific slave exited.
654*d0dff9baSBernard Iremonger
655*d0dff9baSBernard Iremonger.. code-block:: c
656*d0dff9baSBernard Iremonger
657*d0dff9baSBernard Iremonger    for (i = 0; i < RTE_MAX_LCORE; i++) {
658*d0dff9baSBernard Iremonger        if (lcore_resource[i].enabled) {
659*d0dff9baSBernard Iremonger            /* Create ring for master and slave communication */
660*d0dff9baSBernard Iremonger
661*d0dff9baSBernard Iremonger            ret = create_ms_ring(i);
662*d0dff9baSBernard Iremonger            if (ret != 0)
663*d0dff9baSBernard Iremonger                rte_exit(EXIT_FAILURE, "Create ring for lcore=%u failed",i);
664*d0dff9baSBernard Iremonger
665*d0dff9baSBernard Iremonger            if (flib_register_slave_exit_notify(i,slave_exit_cb) != 0)
666*d0dff9baSBernard Iremonger                rte_exit(EXIT_FAILURE, "Register master_trace_slave_exit failed");
667*d0dff9baSBernard Iremonger        }
668*d0dff9baSBernard Iremonger    }
669*d0dff9baSBernard Iremonger
670*d0dff9baSBernard IremongerAfter launching the slave process, the master waits and prints out the port statics periodically.
671*d0dff9baSBernard IremongerIf an event indicating that a slave process exited is detected,
672*d0dff9baSBernard Iremongerit sends the STOP command to the peer and waits until it has also exited.
673*d0dff9baSBernard IremongerThen, it tries to clean up the execution environment and prepare new resources.
674*d0dff9baSBernard IremongerFinally, the new slave instance is launched.
675*d0dff9baSBernard Iremonger
676*d0dff9baSBernard Iremonger.. code-block:: c
677*d0dff9baSBernard Iremonger
678*d0dff9baSBernard Iremonger    while (1) {
679*d0dff9baSBernard Iremonger        sleep(1);
680*d0dff9baSBernard Iremonger        cur_tsc = rte_rdtsc();
681*d0dff9baSBernard Iremonger        diff_tsc = cur_tsc - prev_tsc;
682*d0dff9baSBernard Iremonger
683*d0dff9baSBernard Iremonger        /* if timer is enabled */
684*d0dff9baSBernard Iremonger
685*d0dff9baSBernard Iremonger        if (timer_period > 0) {
686*d0dff9baSBernard Iremonger            /* advance the timer */
687*d0dff9baSBernard Iremonger            timer_tsc += diff_tsc;
688*d0dff9baSBernard Iremonger
689*d0dff9baSBernard Iremonger            /* if timer has reached its timeout */
690*d0dff9baSBernard Iremonger            if (unlikely(timer_tsc >= (uint64_t) timer_period)) {
691*d0dff9baSBernard Iremonger                print_stats();
692*d0dff9baSBernard Iremonger
693*d0dff9baSBernard Iremonger                /* reset the timer */
694*d0dff9baSBernard Iremonger                timer_tsc = 0;
695*d0dff9baSBernard Iremonger            }
696*d0dff9baSBernard Iremonger        }
697*d0dff9baSBernard Iremonger
698*d0dff9baSBernard Iremonger        prev_tsc = cur_tsc;
699*d0dff9baSBernard Iremonger
700*d0dff9baSBernard Iremonger        /* Check any slave need restart or recreate */
701*d0dff9baSBernard Iremonger
702*d0dff9baSBernard Iremonger        rte_spinlock_lock(&res_lock);
703*d0dff9baSBernard Iremonger
704*d0dff9baSBernard Iremonger        for (i = 0; i < RTE_MAX_LCORE; i++) {
705*d0dff9baSBernard Iremonger            struct lcore_resource_struct *res = &lcore_resource[i];
706*d0dff9baSBernard Iremonger            struct lcore_resource_struct *pair = &lcore_resource[res->pair_id];
707*d0dff9baSBernard Iremonger
708*d0dff9baSBernard Iremonger            /* If find slave exited, try to reset pair */
709*d0dff9baSBernard Iremonger
710*d0dff9baSBernard Iremonger            if (res->enabled && res->flags && pair->enabled) {
711*d0dff9baSBernard Iremonger                if (!pair->flags) {
712*d0dff9baSBernard Iremonger                    master_sendcmd_with_ack(pair->lcore_id, CMD_STOP);
713*d0dff9baSBernard Iremonger                    rte_spinlock_unlock(&res_lock);
714*d0dff9baSBernard Iremonger                    sleep(1);
715*d0dff9baSBernard Iremonger                    rte_spinlock_lock(&res_lock);
716*d0dff9baSBernard Iremonger                    if (pair->flags)
717*d0dff9baSBernard Iremonger                        continue;
718*d0dff9baSBernard Iremonger                }
719*d0dff9baSBernard Iremonger
720*d0dff9baSBernard Iremonger                if (reset_pair(res->lcore_id, pair->lcore_id) != 0)
721*d0dff9baSBernard Iremonger                    rte_exit(EXIT_FAILURE, "failed to reset slave");
722*d0dff9baSBernard Iremonger
723*d0dff9baSBernard Iremonger                res->flags = 0;
724*d0dff9baSBernard Iremonger                pair->flags = 0;
725*d0dff9baSBernard Iremonger            }
726*d0dff9baSBernard Iremonger        }
727*d0dff9baSBernard Iremonger        rte_spinlock_unlock(&res_lock);
728*d0dff9baSBernard Iremonger    }
729*d0dff9baSBernard Iremonger
730*d0dff9baSBernard IremongerWhen the slave process is spawned and starts to run, it checks whether the floating process option is applied.
731*d0dff9baSBernard IremongerIf so, it clears the affinity to a specific core and also sets the unique core ID to 0.
732*d0dff9baSBernard IremongerThen, it tries to allocate a new core ID.
733*d0dff9baSBernard IremongerSince the core ID has changed, the resource allocated by the master cannot work,
734*d0dff9baSBernard Iremongerso it remaps the resource to the new core ID slot.
735*d0dff9baSBernard Iremonger
736*d0dff9baSBernard Iremonger.. code-block:: c
737*d0dff9baSBernard Iremonger
738*d0dff9baSBernard Iremonger    static int
739*d0dff9baSBernard Iremonger    l2fwd_launch_one_lcore( attribute ((unused)) void *dummy)
740*d0dff9baSBernard Iremonger    {
741*d0dff9baSBernard Iremonger        unsigned lcore_id = rte_lcore_id();
742*d0dff9baSBernard Iremonger
743*d0dff9baSBernard Iremonger        if (float_proc) {
744*d0dff9baSBernard Iremonger            unsigned flcore_id;
745*d0dff9baSBernard Iremonger
746*d0dff9baSBernard Iremonger            /* Change it to floating process, also change it's lcore_id */
747*d0dff9baSBernard Iremonger
748*d0dff9baSBernard Iremonger            clear_cpu_affinity();
749*d0dff9baSBernard Iremonger
750*d0dff9baSBernard Iremonger            RTE_PER_LCORE(_lcore_id) = 0;
751*d0dff9baSBernard Iremonger
752*d0dff9baSBernard Iremonger            /* Get a lcore_id */
753*d0dff9baSBernard Iremonger
754*d0dff9baSBernard Iremonger            if (flib_assign_lcore_id() < 0 ) {
755*d0dff9baSBernard Iremonger                printf("flib_assign_lcore_id failed\n");
756*d0dff9baSBernard Iremonger                return -1;
757*d0dff9baSBernard Iremonger            }
758*d0dff9baSBernard Iremonger
759*d0dff9baSBernard Iremonger            flcore_id = rte_lcore_id();
760*d0dff9baSBernard Iremonger
761*d0dff9baSBernard Iremonger            /* Set mapping id, so master can return it after slave exited */
762*d0dff9baSBernard Iremonger
763*d0dff9baSBernard Iremonger            mapping_id[lcore_id] = flcore_id;
764*d0dff9baSBernard Iremonger            printf("Org lcore_id = %u, cur lcore_id = %u\n",lcore_id, flcore_id);
765*d0dff9baSBernard Iremonger            remapping_slave_resource(lcore_id, flcore_id);
766*d0dff9baSBernard Iremonger        }
767*d0dff9baSBernard Iremonger
768*d0dff9baSBernard Iremonger        l2fwd_main_loop();
769*d0dff9baSBernard Iremonger
770*d0dff9baSBernard Iremonger        /* return lcore_id before return */
771*d0dff9baSBernard Iremonger        if (float_proc) {
772*d0dff9baSBernard Iremonger            flib_free_lcore_id(rte_lcore_id());
773*d0dff9baSBernard Iremonger            mapping_id[lcore_id] = INVALID_MAPPING_ID;
774*d0dff9baSBernard Iremonger        }
775*d0dff9baSBernard Iremonger        return 0;
776*d0dff9baSBernard Iremonger    }
777*d0dff9baSBernard Iremonger
778*d0dff9baSBernard Iremonger.. |sym_multi_proc_app| image:: img/sym_multi_proc_app.png
779*d0dff9baSBernard Iremonger
780*d0dff9baSBernard Iremonger.. |client_svr_sym_multi_proc_app| image:: img/client_svr_sym_multi_proc_app.png
781*d0dff9baSBernard Iremonger
782*d0dff9baSBernard Iremonger.. |master_slave_proc| image:: img/master_slave_proc.png
783*d0dff9baSBernard Iremonger
784*d0dff9baSBernard Iremonger.. |slave_proc_recov| image:: img/slave_proc_recov.png
785