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