Imported Upstream version 16.07.2
[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     Specifically, because the memzone field in an rte_ring refers to a memzone structure residing in local memory,
83     accessing the memzone field in a shared rte_ring will cause an immediate segmentation fault.
84
85 IVSHMEM Environment Configuration
86 ---------------------------------
87
88 The steps needed to successfully run IVSHMEM applications are the following:
89
90 *   Compile a special version of QEMU from sources.
91
92     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),
93     however, the source code will need to be patched to support using regular files as the IVSHMEM memory backend.
94     The patch is not included in the DPDK package,
95     but is available on the `Intel®DPDK-vswitch project webpage <https://01.org/packet-processing/intel%C2%AE-ovdk>`_
96     (either separately or in a DPDK vSwitch package).
97
98 *   Enable IVSHMEM library in the DPDK build configuration.
99
100     In the default configuration, IVSHMEM library is not compiled. To compile the IVSHMEM library,
101     one has to either use one of the provided IVSHMEM targets
102     (for example, x86_64-ivshmem-linuxapp-gcc),
103     or set CONFIG_RTE_LIBRTE_IVSHMEM to "y" in the build configuration.
104
105 *   Set up hugepage memory on the virtual machine.
106
107     The guest applications run as regular DPDK (primary) processes and thus need their own hugepage memory set up inside the VM.
108     The process is identical to the one described in the *DPDK Getting Started Guide*.
109
110 Best Practices for Writing IVSHMEM Applications
111 -----------------------------------------------
112
113 When considering the use of IVSHMEM for sharing memory, security implications need to be carefully evaluated.
114 IVSHMEM is not suitable for untrusted guests, as IVSHMEM is essentially a window into the host process memory.
115 This also has implications for the multiple VM scenarios.
116 While the IVSHMEM library tries to share as little memory as possible,
117 it is quite probable that data designated for one VM might also be present in an IVSMHMEM device designated for another VM.
118 Consequently, any shared memory corruption will affect both host and all VMs sharing that particular memory.
119
120 IVSHMEM applications essentially behave like multi-process applications,
121 so it is important to implement access serialization to data and thread safety.
122 DPDK ring structures are already thread-safe, however,
123 any custom data structures that the user might need would have to be thread-safe also.
124
125 Similar to regular DPDK multi-process applications,
126 it is not recommended to use function pointers as functions might have different memory addresses in different processes.
127
128 It is best to avoid freeing the rte_mbuf structure on a different machine from where it was allocated,
129 that is, if the mbuf was allocated on the host, the host should free it.
130 Consequently, any packet transmission and reception should also happen on the same machine (whether virtual or physical).
131 Failing to do so may lead to data corruption in the mempool cache.
132
133 Despite the IVSHMEM mechanism being zero-copy and having good performance,
134 it is still desirable to do processing in batches and follow other procedures described in
135 :ref:`Performance Optimization <Performance_Optimization>`.
136
137 Best Practices for Running IVSHMEM Applications
138 -----------------------------------------------
139
140 For performance reasons,
141 it is best to pin host processes and QEMU processes to different cores so that they do not interfere with each other.
142 If NUMA support is enabled, it is also desirable to keep host process' hugepage memory and QEMU process on the same NUMA node.
143
144 For the best performance across all NUMA nodes, each QEMU core should be pinned to host CPU core on the appropriate NUMA node.
145 QEMU's virtual NUMA nodes should also be set up to correspond to physical NUMA nodes.
146 More on how to set up DPDK and QEMU NUMA support can be found in *DPDK Getting Started Guide* and
147 `QEMU documentation <http://qemu.weilnetz.de/qemu-doc.html>`_ respectively.
148 A script called cpu_layout.py is provided with the DPDK package (in the tools directory)
149 that can be used to identify which CPU cores correspond to which NUMA node.
150
151 The QEMU IVSHMEM command line creation should be considered the last step before starting the virtual machine.
152 Currently, there is no hot plug support for QEMU IVSHMEM devices,
153 so one cannot add additional memory to an IVSHMEM device once it has been created.
154 Therefore, the correct sequence to run an IVSHMEM application is to run host application first,
155 obtain the command lines for each IVSHMEM device and then run all QEMU instances with guest applications afterwards.
156
157 It is important to note that once QEMU is started, it holds on to the hugepages it uses for IVSHMEM devices.
158 As a result, if the user wishes to shut down or restart the IVSHMEM host application,
159 it is not enough to simply shut the application down.
160 The virtual machine must also be shut down (if not, it will hold onto outdated host data).