xref: /dpdk/doc/guides/prog_guide/thread_safety.rst (revision 41dd9a6bc2d9c6e20e139ad713cc9d172572dd43)
1*41dd9a6bSDavid Young..  SPDX-License-Identifier: BSD-3-Clause
2*41dd9a6bSDavid Young    Copyright(c) 2010-2014 Intel Corporation.
3*41dd9a6bSDavid Young
4*41dd9a6bSDavid YoungThread Safety
5*41dd9a6bSDavid Young=============
6*41dd9a6bSDavid Young
7*41dd9a6bSDavid YoungThe DPDK is comprised of several libraries.
8*41dd9a6bSDavid YoungSome of the functions in these libraries can be safely called from multiple threads simultaneously, while others cannot.
9*41dd9a6bSDavid YoungThis section allows the developer to take these issues into account when building their own application.
10*41dd9a6bSDavid Young
11*41dd9a6bSDavid YoungThe run-time environment of the DPDK is typically a single thread per logical core.
12*41dd9a6bSDavid YoungIn some cases, it is not only multi-threaded, but multi-process.
13*41dd9a6bSDavid YoungTypically, it is best to avoid sharing data structures between threads and/or processes where possible.
14*41dd9a6bSDavid YoungWhere this is not possible, then the execution blocks must access the data in a thread- safe manner.
15*41dd9a6bSDavid YoungMechanisms such as atomics or locking can be used that will allow execution blocks to operate serially.
16*41dd9a6bSDavid YoungHowever, this can have an effect on the performance of the application.
17*41dd9a6bSDavid Young
18*41dd9a6bSDavid YoungFast-Path APIs
19*41dd9a6bSDavid Young--------------
20*41dd9a6bSDavid Young
21*41dd9a6bSDavid YoungApplications operating in the data plane are performance sensitive but
22*41dd9a6bSDavid Youngcertain functions within those libraries may not be safe to call from multiple threads simultaneously.
23*41dd9a6bSDavid YoungThe hash, LPM and mempool libraries and RX/TX in the PMD are examples of this.
24*41dd9a6bSDavid Young
25*41dd9a6bSDavid YoungThe hash and LPM libraries are, by design, thread unsafe in order to maintain performance.
26*41dd9a6bSDavid YoungHowever, if required the developer can add layers on top of these libraries to provide thread safety.
27*41dd9a6bSDavid YoungLocking is not needed in all situations, and in both the hash and LPM libraries,
28*41dd9a6bSDavid Younglookups of values can be performed in parallel in multiple threads.
29*41dd9a6bSDavid YoungAdding, removing or modifying values, however,
30*41dd9a6bSDavid Youngcannot be done in multiple threads without using locking when a single hash or LPM table is accessed.
31*41dd9a6bSDavid YoungAnother alternative to locking would be to create multiple instances of these tables allowing each thread its own copy.
32*41dd9a6bSDavid Young
33*41dd9a6bSDavid YoungThe RX and TX of the PMD are the most critical aspects of a DPDK application
34*41dd9a6bSDavid Youngand it is recommended that no locking be used as it will impact performance.
35*41dd9a6bSDavid YoungNote, however, that these functions can safely be used from multiple threads
36*41dd9a6bSDavid Youngwhen each thread is performing I/O on a different NIC queue.
37*41dd9a6bSDavid YoungIf multiple threads are to use the same hardware queue on the same NIC port,
38*41dd9a6bSDavid Youngthen locking, or some other form of mutual exclusion, is necessary.
39*41dd9a6bSDavid Young
40*41dd9a6bSDavid YoungThe ring library is based on a lockless ring-buffer algorithm that maintains its original design for thread safety.
41*41dd9a6bSDavid YoungMoreover, it provides high performance for either multi- or single-consumer/producer enqueue/dequeue operations.
42*41dd9a6bSDavid YoungThe mempool library is based on the DPDK lockless ring library and therefore is also multi-thread safe.
43*41dd9a6bSDavid Young
44*41dd9a6bSDavid YoungPerformance Insensitive API
45*41dd9a6bSDavid Young---------------------------
46*41dd9a6bSDavid Young
47*41dd9a6bSDavid YoungOutside of the performance sensitive areas described in Section 25.1,
48*41dd9a6bSDavid Youngthe DPDK provides a thread-safe API for most other libraries.
49*41dd9a6bSDavid YoungFor example, malloc and memzone functions are safe for use in multi-threaded and multi-process environments.
50*41dd9a6bSDavid Young
51*41dd9a6bSDavid YoungThe setup and configuration of the PMD is not performance sensitive, but is not thread safe either.
52*41dd9a6bSDavid YoungIt is possible that the multiple read/writes during PMD setup and configuration could be corrupted in a multi-thread environment.
53*41dd9a6bSDavid YoungSince this is not performance sensitive, the developer can choose to add their own layer to provide thread-safe setup and configuration.
54*41dd9a6bSDavid YoungIt is expected that, in most applications, the initial configuration of the network ports would be done by a single thread at startup.
55*41dd9a6bSDavid Young
56*41dd9a6bSDavid YoungLibrary Initialization
57*41dd9a6bSDavid Young----------------------
58*41dd9a6bSDavid Young
59*41dd9a6bSDavid YoungIt is recommended that DPDK libraries are initialized in the main thread at application startup
60*41dd9a6bSDavid Youngrather than subsequently in the forwarding threads.
61*41dd9a6bSDavid YoungHowever, the DPDK performs checks to ensure that libraries are only initialized once.
62*41dd9a6bSDavid YoungIf initialization is attempted more than once, an error is returned.
63*41dd9a6bSDavid Young
64*41dd9a6bSDavid YoungIn the multi-process case, the configuration information of shared memory will only be initialized by the primary process.
65*41dd9a6bSDavid YoungThereafter, both primary and secondary processes can allocate/release any objects of memory that finally rely on rte_malloc or memzones.
66*41dd9a6bSDavid Young
67*41dd9a6bSDavid YoungInterrupt Thread
68*41dd9a6bSDavid Young----------------
69*41dd9a6bSDavid Young
70*41dd9a6bSDavid YoungThe DPDK works almost entirely in Linux user space in polling mode.
71*41dd9a6bSDavid YoungFor certain infrequent operations, such as receiving a PMD link status change notification,
72*41dd9a6bSDavid Youngcallbacks may be called in an additional thread outside the main DPDK processing threads.
73*41dd9a6bSDavid YoungThese function callbacks should avoid manipulating DPDK objects that are also managed by the normal DPDK threads,
74*41dd9a6bSDavid Youngand if they need to do so,
75*41dd9a6bSDavid Youngit is up to the application to provide the appropriate locking or mutual exclusion restrictions around those objects.
76