c76d2b38ece08dae4ae224dfa19dfe3c113ffe1d
[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 QEUMU'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 .. image28_png has been renamed
47
48 |ivshmem|
49
50 The same could work with several virtual machines, providing host-to-VM or VM-to-VM communication.
51 The maximum number of metadata files is 32 (by default) and each metadata file can contain different (or even the same) hugepages.
52 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).
53 For example, if the user wants to share the same memzone across two VMs, each VM must have that memzone in its metadata file.
54
55 IVHSHMEM Library API Overview
56 -----------------------------
57
58 The following is a simple guide to using the IVSHMEM Library API:
59
60 *   Call rte_ivshmem_metadata_create() to create a new metadata file.
61     The metadata name is used to distinguish between multiple metadata files.
62
63 *   Populate each metadata file with DPDK data structures.
64     This can be done using the following API calls:
65
66     *   rte_ivhshmem_metadata_add_memzone() to add rte_memzone to metadata file
67
68     *   rte_ivshmem_metadata_add_ring() to add rte_ring to metadata file
69
70     *   rte_ivshmem_metadata_add_mempool() to add rte_mempool to metadata file
71
72 *   Finally, call rte_ivshmem_metadata_cmdline_generate() to generate the command line for QEMU.
73     Multiple metadata files (and thus multiple command lines) can be supplied to a single VM.
74
75 .. note::
76
77     Only data structures fully residing in DPDK hugepage memory work correctly.
78     Supported data structures created by malloc(), mmap()
79     or otherwise using non-DPDK memory cause undefined behavior and even a segmentation fault.
80
81 IVSHMEM Environment Configuration
82 ---------------------------------
83
84 The steps needed to successfully run IVSHMEM applications are the following:
85
86 *   Compile a special version of QEMU from sources.
87
88     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),
89     however, the source code will need to be patched to support using regular files as the IVSHMEM memory backend.
90     The patch is not included in the DPDK package,
91     but is available on the `Intel®DPDK-vswitch project webpage <https://01.org/packet-processing/intel%C2%AE-ovdk>`_
92     (either separately or in a DPDK vSwitch package).
93
94 *   Enable IVSHMEM library in the DPDK build configuration.
95
96     In the default configuration, IVSHMEM library is not compiled. To compile the IVSHMEM library,
97     one has to either use one of the provided IVSHMEM targets
98     (for example, x86_64-ivshmem-linuxapp-gcc),
99     or set CONFIG_RTE_LIBRTE_IVSHMEM to "y" in the build configuration.
100
101 *   Set up hugepage memory on the virtual machine.
102
103     The guest applications run as regular DPDK (primary) processes and thus need their own hugepage memory set up inside the VM.
104     The process is identical to the one described in the *DPDK Getting Started Guide*.
105
106 Best Practices for Writing IVSHMEM Applications
107 -----------------------------------------------
108
109 When considering the use of IVSHMEM for sharing memory, security implications need to be carefully evaluated.
110 IVSHMEM is not suitable for untrusted guests, as IVSHMEM is essentially a window into the host processs memory.
111 This also has implications for the multiple VM scenarios.
112 While the IVSHMEM library tries to share as little memory as possible,
113 it is quite probable that data designated for one VM might also be present in an IVSMHMEM device designated for another VM.
114 Consequently, any shared memory corruption will affect both host and all VMs sharing that particular memory.
115
116 IVSHMEM applications essentially behave like multi-process applications,
117 so it is important to implement access serialization to data and thread safety.
118 DPDK ring structures are already thread-safe, however,
119 any custom data structures that the user might need would have to be thread-safe also.
120
121 Similar to regular DPDK multi-process applications,
122 it is not recommended to use function pointers as functions might have different memory addresses in different processes.
123
124 It is best to avoid freeing the rte_mbuf structure on a different machine from where it was allocated,
125 that is, if the mbuf was allocated on the host, the host should free it.
126 Consequently, any packet transmission and reception should also happen on the same machine (whether virtual or physical).
127 Failing to do so may lead to data corruption in the mempool cache.
128
129 Despite the IVSHMEM mechanism being zero-copy and having good performance,
130 it is still desirable to do processing in batches and follow other procedures described in
131 :ref:`Performance Optimization <Performance_Optimization>`.
132
133 Best Practices for Running IVSHMEM Applications
134 -----------------------------------------------
135
136 For performance reasons,
137 it is best to pin host processes and QEMU processes to different cores so that they do not interfere with each other.
138 If NUMA support is enabled, it is also desirable to keep host process' hugepage memory and QEMU process on the same NUMA node.
139
140 For the best performance across all NUMA nodes, each QUEMU core should be pinned to host CPU core on the appropriate NUMA node.
141 QEMU's virtual NUMA nodes should also be set up to correspond to physical NUMA nodes.
142 More on how to set up DPDK and QEMU NUMA support can be found in *DPDK Getting Started Guide* and
143 `QEMU documentation <http://qemu.weilnetz.de/qemu-doc.html>`_ respectively.
144 A script called cpu_layout.py is provided with the DPDK package (in the tools directory)
145 that can be used to identify which CPU cores correspond to which NUMA node.
146
147 The QEMU IVSHMEM command line creation should be considered the last step before starting the virtual machine.
148 Currently, there is no hot plug support for QEMU IVSHMEM devices,
149 so one cannot add additional memory to an IVSHMEM device once it has been created.
150 Therefore, the correct sequence to run an IVSHMEM application is to run host application first,
151 obtain the command lines for each IVSHMEM device and then run all QEMU instances with guest applications afterwards.
152
153 It is important to note that once QEMU is started, it holds on to the hugepages it uses for IVSHMEM devices.
154 As a result, if the user wishes to shut down or restart the IVSHMEM host application,
155 it is not enough to simply shut the application down.
156 The virtual machine must also be shut down (if not, it will hold onto outdated host data).
157
158 .. |ivshmem| image:: img/ivshmem.*