Imported Upstream version 16.04
[deb_dpdk.git] / doc / guides / prog_guide / ivshmem_lib.rst
1 ..  BSD LICENSE
2     Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
3     All rights reserved.
4
5     Redistribution and use in source and binary forms, with or without
6     modification, are permitted provided that the following conditions
7     are met:
8
9     * Redistributions of source code must retain the above copyright
10     notice, this list of conditions and the following disclaimer.
11     * Redistributions in binary form must reproduce the above copyright
12     notice, this list of conditions and the following disclaimer in
13     the documentation and/or other materials provided with the
14     distribution.
15     * Neither the name of Intel Corporation nor the names of its
16     contributors may be used to endorse or promote products derived
17     from this software without specific prior written permission.
18
19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22     A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23     OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25     LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 IVSHMEM Library
32 ===============
33
34 The DPDK IVSHMEM library facilitates fast zero-copy data sharing among virtual machines
35 (host-to-guest or guest-to-guest) by means of QEMU's IVSHMEM mechanism.
36
37 The library works by providing a command line for QEMU to map several hugepages into a single IVSHMEM device.
38 For the guest to know what is inside any given IVSHMEM device
39 (and to distinguish between DPDK and non-DPDK IVSHMEM devices),
40 a metadata file is also mapped into the IVSHMEM segment.
41 No work needs to be done by the guest application to map IVSHMEM devices into memory;
42 they are automatically recognized by the DPDK Environment Abstraction Layer (EAL).
43
44 A typical DPDK IVSHMEM use case looks like the following.
45
46
47 .. figure:: img/ivshmem.*
48
49    Typical Ivshmem use case
50
51
52 The same could work with several virtual machines, providing host-to-VM or VM-to-VM communication.
53 The maximum number of metadata files is 32 (by default) and each metadata file can contain different (or even the same) hugepages.
54 The only constraint is that each VM has to have access to the memory it is sharing with other entities (be it host or another VM).
55 For example, if the user wants to share the same memzone across two VMs, each VM must have that memzone in its metadata file.
56
57 IVHSHMEM Library API Overview
58 -----------------------------
59
60 The following is a simple guide to using the IVSHMEM Library API:
61
62 *   Call rte_ivshmem_metadata_create() to create a new metadata file.
63     The metadata name is used to distinguish between multiple metadata files.
64
65 *   Populate each metadata file with DPDK data structures.
66     This can be done using the following API calls:
67
68     *   rte_ivhshmem_metadata_add_memzone() to add rte_memzone to metadata file
69
70     *   rte_ivshmem_metadata_add_ring() to add rte_ring to metadata file
71
72     *   rte_ivshmem_metadata_add_mempool() to add rte_mempool to metadata file
73
74 *   Finally, call rte_ivshmem_metadata_cmdline_generate() to generate the command line for QEMU.
75     Multiple metadata files (and thus multiple command lines) can be supplied to a single VM.
76
77 .. note::
78
79     Only data structures fully residing in DPDK hugepage memory work correctly.
80     Supported data structures created by malloc(), mmap()
81     or otherwise using non-DPDK memory cause undefined behavior and even a segmentation fault.
82
83 IVSHMEM Environment Configuration
84 ---------------------------------
85
86 The steps needed to successfully run IVSHMEM applications are the following:
87
88 *   Compile a special version of QEMU from sources.
89
90     The source code can be found on the QEMU website (currently, version 1.4.x is supported, but version 1.5.x is known to work also),
91     however, the source code will need to be patched to support using regular files as the IVSHMEM memory backend.
92     The patch is not included in the DPDK package,
93     but is available on the `Intel®DPDK-vswitch project webpage <https://01.org/packet-processing/intel%C2%AE-ovdk>`_
94     (either separately or in a DPDK vSwitch package).
95
96 *   Enable IVSHMEM library in the DPDK build configuration.
97
98     In the default configuration, IVSHMEM library is not compiled. To compile the IVSHMEM library,
99     one has to either use one of the provided IVSHMEM targets
100     (for example, x86_64-ivshmem-linuxapp-gcc),
101     or set CONFIG_RTE_LIBRTE_IVSHMEM to "y" in the build configuration.
102
103 *   Set up hugepage memory on the virtual machine.
104
105     The guest applications run as regular DPDK (primary) processes and thus need their own hugepage memory set up inside the VM.
106     The process is identical to the one described in the *DPDK Getting Started Guide*.
107
108 Best Practices for Writing IVSHMEM Applications
109 -----------------------------------------------
110
111 When considering the use of IVSHMEM for sharing memory, security implications need to be carefully evaluated.
112 IVSHMEM is not suitable for untrusted guests, as IVSHMEM is essentially a window into the host process memory.
113 This also has implications for the multiple VM scenarios.
114 While the IVSHMEM library tries to share as little memory as possible,
115 it is quite probable that data designated for one VM might also be present in an IVSMHMEM device designated for another VM.
116 Consequently, any shared memory corruption will affect both host and all VMs sharing that particular memory.
117
118 IVSHMEM applications essentially behave like multi-process applications,
119 so it is important to implement access serialization to data and thread safety.
120 DPDK ring structures are already thread-safe, however,
121 any custom data structures that the user might need would have to be thread-safe also.
122
123 Similar to regular DPDK multi-process applications,
124 it is not recommended to use function pointers as functions might have different memory addresses in different processes.
125
126 It is best to avoid freeing the rte_mbuf structure on a different machine from where it was allocated,
127 that is, if the mbuf was allocated on the host, the host should free it.
128 Consequently, any packet transmission and reception should also happen on the same machine (whether virtual or physical).
129 Failing to do so may lead to data corruption in the mempool cache.
130
131 Despite the IVSHMEM mechanism being zero-copy and having good performance,
132 it is still desirable to do processing in batches and follow other procedures described in
133 :ref:`Performance Optimization <Performance_Optimization>`.
134
135 Best Practices for Running IVSHMEM Applications
136 -----------------------------------------------
137
138 For performance reasons,
139 it is best to pin host processes and QEMU processes to different cores so that they do not interfere with each other.
140 If NUMA support is enabled, it is also desirable to keep host process' hugepage memory and QEMU process on the same NUMA node.
141
142 For the best performance across all NUMA nodes, each QEMU core should be pinned to host CPU core on the appropriate NUMA node.
143 QEMU's virtual NUMA nodes should also be set up to correspond to physical NUMA nodes.
144 More on how to set up DPDK and QEMU NUMA support can be found in *DPDK Getting Started Guide* and
145 `QEMU documentation <http://qemu.weilnetz.de/qemu-doc.html>`_ respectively.
146 A script called cpu_layout.py is provided with the DPDK package (in the tools directory)
147 that can be used to identify which CPU cores correspond to which NUMA node.
148
149 The QEMU IVSHMEM command line creation should be considered the last step before starting the virtual machine.
150 Currently, there is no hot plug support for QEMU IVSHMEM devices,
151 so one cannot add additional memory to an IVSHMEM device once it has been created.
152 Therefore, the correct sequence to run an IVSHMEM application is to run host application first,
153 obtain the command lines for each IVSHMEM device and then run all QEMU instances with guest applications afterwards.
154
155 It is important to note that once QEMU is started, it holds on to the hugepages it uses for IVSHMEM devices.
156 As a result, if the user wishes to shut down or restart the IVSHMEM host application,
157 it is not enough to simply shut the application down.
158 The virtual machine must also be shut down (if not, it will hold onto outdated host data).