cd9b2322398a8d348fdbdd866890959bc8f09453
[dpdk.git] / doc / guides / sample_app_ug / vhost.rst
1
2 ..  BSD LICENSE
3     Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
4     All rights reserved.
5
6     Redistribution and use in source and binary forms, with or without
7     modification, are permitted provided that the following conditions
8     are met:
9
10     * Redistributions of source code must retain the above copyright
11     notice, this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright
13     notice, this list of conditions and the following disclaimer in
14     the documentation and/or other materials provided with the
15     distribution.
16     * Neither the name of Intel Corporation nor the names of its
17     contributors may be used to endorse or promote products derived
18     from this software without specific prior written permission.
19
20     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23     A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24     OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26     LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32
33 Vhost Sample Application
34 ========================
35
36 The vhost sample application demonstrates integration of the Data Plane Development Kit (DPDK)
37 with the Linux* KVM hypervisor by implementing the vhost-net offload API.
38 The sample application performs simple packet switching between virtual machines based on Media Access Control
39 (MAC) address or Virtual Local Area Network (VLAN) tag.
40 The splitting of ethernet traffic from an external switch is performed in hardware by the Virtual Machine Device Queues
41 (VMDQ) and Data Center Bridging (DCB) features of the IntelĀ® 82599 10 Gigabit Ethernet Controller.
42
43 Background
44 ----------
45
46 Virtio networking (virtio-net) was developed as the Linux* KVM para-virtualized method for communicating network packets
47 between host and guest.
48 It was found that virtio-net performance was poor due to context switching and packet copying between host, guest, and QEMU.
49 The following figure shows the system architecture for a virtio-based networking (virtio-net).
50
51 .. _figure_16:
52
53 **Figure16. QEMU Virtio-net (prior to vhost-net)**
54
55 .. image19_png has been renamed
56
57 |qemu_virtio_net|
58
59 The Linux* Kernel vhost-net module was developed as an offload mechanism for virtio-net.
60 The vhost-net module enables KVM (QEMU) to offload the servicing of virtio-net devices to the vhost-net kernel module,
61 reducing the context switching and packet copies in the virtual dataplane.
62
63 This is achieved by QEMU sharing the following information with the vhost-net module through the vhost-net API:
64
65 *   The layout of the guest memory space, to enable the vhost-net module to translate addresses.
66
67 *   The locations of virtual queues in QEMU virtual address space,
68     to enable the vhost module to read/write directly to and from the virtqueues.
69
70 *   An event file descriptor (eventfd) configured in KVM to send interrupts to the virtio- net device driver in the guest.
71     This enables the vhost-net module to notify (call) the guest.
72
73 *   An eventfd configured in KVM to be triggered on writes to the virtio-net device's
74     Peripheral Component Interconnect (PCI) config space.
75     This enables the vhost-net module to receive notifications (kicks) from the guest.
76
77 The following figure shows the system architecture for virtio-net networking with vhost-net offload.
78
79 .. _figure_17:
80
81 **Figure 17. Virtio with Linux* Kernel Vhost**
82
83 .. image20_png has been renamed
84
85 |virtio_linux_vhost|
86
87 Sample Code Overview
88 --------------------
89
90 The DPDK vhost-net sample code demonstrates KVM (QEMU) offloading the servicing of a Virtual Machine's (VM's)
91 virtio-net devices to a DPDK-based application in place of the kernel's vhost-net module.
92
93 The DPDK vhost-net sample code is based on vhost library. Vhost library is developed for user space ethernet switch to
94 easily integrate with vhost functionality.
95
96 The vhost library implements the following features:
97
98 *   Management of virtio-net device creation/destruction events.
99
100 *   Mapping of the VM's physical memory into the DPDK vhost-net's address space.
101
102 *   Triggering/receiving notifications to/from VMs via eventfds.
103
104 *   A virtio-net back-end implementation providing a subset of virtio-net features.
105
106 There are two vhost implementations in vhost library, vhost cuse and vhost user. In vhost cuse, a character device driver is implemented to
107 receive and process vhost requests through ioctl messages. In vhost user, a socket server is created to received vhost requests through
108 socket messages. Most of the messages share the same handler routine.
109
110 .. note::
111     **Any vhost cuse specific requirement in the following sections will be emphasized**.
112
113 Two impelmentations are turned on and off statically through configure file. Only one implementation could be turned on. They don't co-exist in current implementation.
114
115 The vhost sample code application is a simple packet switching application with the following feature:
116
117 *   Packet switching between virtio-net devices and the network interface card,
118     including using VMDQs to reduce the switching that needs to be performed in software.
119
120 The following figure shows the architecture of the Vhost sample application based on vhost-cuse.
121
122 .. _figure_18:
123
124 **Figure 18. Vhost-net Architectural Overview**
125
126 .. image21_png has been renamed
127
128 |vhost_net_arch|
129
130 The following figure shows the flow of packets through the vhost-net sample application.
131
132 .. _figure_19:
133
134 **Figure 19. Packet Flow Through the vhost-net Sample Application**
135
136 .. image22_png  has been renamed
137
138 |vhost_net_sample_app|
139
140 Supported Distributions
141 -----------------------
142
143 The example in this section have been validated with the following distributions:
144
145 *   Fedora* 18
146
147 *   Fedora* 19
148
149 *   Fedora* 20
150
151 Prerequisites
152 -------------
153
154 This section lists prerequisite packages that must be installed.
155
156 Installing Packages on the Host(vhost cuse required)
157 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
158
159 The vhost cuse code uses the following packages; fuse, fuse-devel, and kernel-modules-extra.
160 The vhost user code don't rely on those modules as eventfds are already installed into vhost process through
161 unix domain socket.
162
163 #.  Install Fuse Development Libraries and headers:
164
165     .. code-block:: console
166
167         yum -y install fuse fuse-devel
168
169 #.  Install the Cuse Kernel Module:
170
171     .. code-block:: console
172
173         yum -y install kernel-modules-extra
174
175 QEMU simulator
176 ~~~~~~~~~~~~~~
177
178 For vhost user, qemu 2.2 is required.
179
180 Setting up the Execution Environment
181 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
182
183 The vhost sample code requires that QEMU allocates a VM's memory on the hugetlbfs file system.
184 As the vhost sample code requires hugepages,
185 the best practice is to partition the system into separate hugepage mount points for the VMs and the vhost sample code.
186
187 .. note::
188
189     This is best-practice only and is not mandatory.
190     For systems that only support 2 MB page sizes,
191     both QEMU and vhost sample code can use the same hugetlbfs mount point without issue.
192
193 **QEMU**
194
195 VMs with gigabytes of memory can benefit from having QEMU allocate their memory from 1 GB huge pages.
196 1 GB huge pages must be allocated at boot time by passing kernel parameters through the grub boot loader.
197
198 #.  Calculate the maximum memory usage of all VMs to be run on the system.
199     Then, round this value up to the nearest Gigabyte the execution environment will require.
200
201 #.  Edit the /etc/default/grub file, and add the following to the GRUB_CMDLINE_LINUX entry:
202
203     .. code-block:: console
204
205         GRUB_CMDLINE_LINUX="... hugepagesz=1G hugepages=<Number of hugepages required> default_hugepagesz=1G"
206
207 #.  Update the grub boot loader:
208
209     .. code-block:: console
210
211         grub2-mkconfig -o /boot/grub2/grub.cfg
212
213 #.  Reboot the system.
214
215 #.  The hugetlbfs mount point (/dev/hugepages) should now default to allocating gigabyte pages.
216
217 .. note::
218
219     Making the above modification will change the system default hugepage size to 1 GB for all applications.
220
221 **Vhost Sample Code**
222
223 In this section, we create a second hugetlbs mount point to allocate hugepages for the DPDK vhost sample code.
224
225 #.  Allocate sufficient 2 MB pages for the DPDK vhost sample code:
226
227     .. code-block:: console
228
229         echo 256 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages
230
231 #.  Mount hugetlbs at a separate mount point for 2 MB pages:
232
233     .. code-block:: console
234
235         mount -t hugetlbfs nodev /mnt/huge -o pagesize=2M
236
237 The above steps can be automated by doing the following:
238
239 #.  Edit /etc/fstab to add an entry to automatically mount the second hugetlbfs mount point:
240
241     ::
242
243         hugetlbfs <tab> /mnt/huge <tab> hugetlbfs defaults,pagesize=1G 0 0
244
245 #.  Edit the /etc/default/grub file, and add the following to the GRUB_CMDLINE_LINUX entry:
246
247     ::
248
249         GRUB_CMDLINE_LINUX="... hugepagesz=2M hugepages=256 ... default_hugepagesz=1G"
250
251 #.  Update the grub bootloader:
252
253     .. code-block:: console
254
255         grub2-mkconfig -o /boot/grub2/grub.cfg
256
257 #.  Reboot the system.
258
259 .. note::
260
261     Ensure that the default hugepage size after this setup is 1 GB.
262
263 Setting up the Guest Execution Environment
264 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
265
266 It is recommended for testing purposes that the DPDK testpmd sample application is used in the guest to forward packets,
267 the reasons for this are discussed in Section 22.7, "Running the Virtual Machine (QEMU)".
268
269 The testpmd application forwards packets between pairs of Ethernet devices,
270 it requires an even number of Ethernet devices (virtio or otherwise) to execute.
271 It is therefore recommended to create multiples of two virtio-net devices for each Virtual Machine either through libvirt or
272 at the command line as follows.
273
274 .. note::
275
276     Observe that in the example, "-device" and "-netdev" are repeated for two virtio-net devices.
277
278 For vhost cuse:
279
280 .. code-block:: console
281
282     user@target:~$ qemu-system-x86_64 ... \
283     -netdev tap,id=hostnet1,vhost=on,vhostfd=<open fd> \
284     -device virtio-net-pci, netdev=hostnet1,id=net1 \
285     -netdev tap,id=hostnet2,vhost=on,vhostfd=<open fd> \
286     -device virtio-net-pci, netdev=hostnet2,id=net1
287
288 For vhost user:
289
290 .. code-block:: console
291
292     user@target:~$ qemu-system-x86_64 ... \
293     -chardev socket,id=char1,path=<sock_path> \
294     -netdev type=vhost-user,id=hostnet1,chardev=char1 \
295     -device virtio-net-pci,netdev=hostnet1,id=net1 \
296     -chardev socket,id=char2,path=<sock_path> \
297     -netdev type=vhost-user,id=hostnet2,chardev=char2 \
298     -device virtio-net-pci,netdev=hostnet2,id=net2
299
300 sock_path is the path for the socket file created by vhost.
301
302 Compiling the Sample Code
303 -------------------------
304 #.  Compile vhost lib:
305
306     To enable vhost, turn on vhost library in the configure file config/common_linuxapp.
307
308     .. code-block:: console
309
310         CONFIG_RTE_LIBRTE_VHOST=n
311
312     vhost user is turned on by default in the lib/librte_vhost/Makefile.
313     To enable vhost cuse, uncomment vhost cuse and comment vhost user manually. In future, a configure will be created for switch between two implementations.
314
315     .. code-block:: console
316
317         SRCS-$(CONFIG_RTE_LIBRTE_VHOST) += vhost_cuse/vhost-net-cdev.c vhost_cuse/virtio-net-cdev.c vhost_cuse/eventfd_copy.c
318         #SRCS-$(CONFIG_RTE_LIBRTE_VHOST) += vhost_user/vhost-net-user.c vhost_user/virtio-net-user.c vhost_user/fd_man.c
319
320      After vhost is enabled and the implementation is selected, build the vhost library.
321
322 #.  Go to the examples directory:
323
324     .. code-block:: console
325
326         export RTE_SDK=/path/to/rte_sdk
327         cd ${RTE_SDK}/examples/vhost
328
329 #.  Set the target (a default target is used if not specified). For example:
330
331     .. code-block:: console
332
333         export RTE_TARGET=x86_64-native-linuxapp-gcc
334
335     See the DPDK Getting Started Guide for possible RTE_TARGET values.
336
337 #.  Build the application:
338
339     .. code-block:: console
340
341         make
342
343     .. note::
344
345         Note For zero copy, need firstly disable CONFIG_RTE_MBUF_SCATTER_GATHER,
346         CONFIG_RTE_LIBRTE_IP_FRAG and CONFIG_RTE_LIBRTE_DISTRIBUTOR
347         in the config file and then re-configure and compile the core lib, and then build the application:
348
349     .. code-block:: console
350
351         vi ${RTE_SDK}/config/common_linuxapp
352
353     change it as follows:
354
355     ::
356
357         CONFIG_RTE_MBUF_SCATTER_GATHER=n
358         CONFIG_RTE_LIBRTE_IP_FRAG=n
359         CONFIG_RTE_LIBRTE_DISTRIBUTOR=n
360
361     .. code-block:: console
362
363         cd ${RTE_SDK}
364         make config ${RTE_TARGET}
365         make install ${RTE_TARGET}
366         cd ${RTE_SDK}/examples/vhost
367         make
368
369 #.  Go to the eventfd_link directory(vhost cuse required):
370
371     .. code-block:: console
372
373         cd ${RTE_SDK}/lib/librte_vhost/eventfd_link
374
375 #.  Build the eventfd_link kernel module(vhost cuse required):
376
377     .. code-block:: console
378
379         make
380
381 Running the Sample Code
382 -----------------------
383
384 #.  Install the cuse kernel module(vhost cuse required):
385
386     .. code-block:: console
387
388         modprobe cuse
389
390 #.  Go to the eventfd_link directory(vhost cuse required):
391
392     .. code-block:: console
393
394         export RTE_SDK=/path/to/rte_sdk
395         cd ${RTE_SDK}/lib/librte_vhost/eventfd_link
396
397 #.  Install the eventfd_link module(vhost cuse required):
398
399     .. code-block:: console
400
401         insmod ./eventfd_link.ko
402
403 #.  Go to the examples directory:
404
405     .. code-block:: console
406
407         export RTE_SDK=/path/to/rte_sdk
408         cd ${RTE_SDK}/examples/vhost
409
410 #.  Run the vhost-switch sample code:
411
412     vhost cuse:
413
414     .. code-block:: console
415
416         user@target:~$ ./build/app/vhost-switch -c f -n 4 --huge-dir / mnt/huge -- -p 0x1 --dev-basename usvhost --dev-index 1
417
418     vhost user: a socket file named usvhost will be created under current directory. Use its path as the socket path in guest's qemu commandline.
419
420     .. code-block:: console
421
422         user@target:~$ ./build/app/vhost-switch -c f -n 4 --huge-dir / mnt/huge -- -p 0x1 --dev-basename usvhost
423
424 .. note::
425
426     Please note the huge-dir parameter instructs the DPDK to allocate its memory from the 2 MB page hugetlbfs.
427
428 Parameters
429 ~~~~~~~~~~
430
431 **Basename and Index.**
432 vhost cuse uses a Linux* character device to communicate with QEMU.
433 The basename and the index are used to generate the character devices name.
434
435     /dev/<basename>-<index>
436
437 The index parameter is provided for a situation where multiple instances of the virtual switch is required.
438
439 For compatibility with the QEMU wrapper script, a base name of "usvhost" and an index of "1" should be used:
440
441 .. code-block:: console
442
443     user@target:~$ ./build/app/vhost-switch -c f -n 4 --huge-dir / mnt/huge -- -p 0x1 --dev-basename usvhost --dev-index 1
444
445 **vm2vm.**
446 The vm2vm parameter disable/set mode of packet switching between guests in the host.
447 Value of "0" means disabling vm2vm implies that on virtual machine packet transmission will always go to the Ethernet port;
448 Value of "1" means software mode packet forwarding between guests, it needs packets copy in vHOST,
449 so valid only in one-copy implementation, and invalid for zero copy implementation;
450 value of "2" means hardware mode packet forwarding between guests, it allows packets go to the Ethernet port,
451 hardware L2 switch will determine which guest the packet should forward to or need send to external,
452 which bases on the packet destination MAC address and VLAN tag.
453
454 .. code-block:: console
455
456     user@target:~$ ./build/app/vhost-switch -c f -n 4 --huge-dir /mnt/huge -- --vm2vm [0,1,2]
457
458 **Mergeable Buffers.**
459 The mergeable buffers parameter controls how virtio-net descriptors are used for virtio-net headers.
460 In a disabled state, one virtio-net header is used per packet buffer;
461 in an enabled state one virtio-net header is used for multiple packets.
462 The default value is 0 or disabled since recent kernels virtio-net drivers show performance degradation with this feature is enabled.
463
464 .. code-block:: console
465
466     user@target:~$ ./build/app/vhost-switch -c f -n 4 --huge-dir / mnt/huge -- --mergeable [0,1]
467
468 **Stats.**
469 The stats parameter controls the printing of virtio-net device statistics.
470 The parameter specifies an interval second to print statistics, with an interval of 0 seconds disabling statistics.
471
472 .. code-block:: console
473
474     user@target:~$ ./build/app/vhost-switch -c f -n 4 --huge-dir / mnt/huge -- --stats [0,n]
475
476 **RX Retry.**
477 The rx-retry option enables/disables enqueue retries when the guests RX queue is full.
478 This feature resolves a packet loss that is observed at high data-rates,
479 by allowing it to delay and retry in the receive path.
480 This option is enabled by default.
481
482 .. code-block:: console
483
484     user@target:~$ ./build/app/vhost-switch -c f -n 4 --huge-dir / mnt/huge -- --rx-retry [0,1]
485
486 **RX Retry Number.**
487 The rx-retry-num option specifies the number of retries on an RX burst,
488 it takes effect only when rx retry is enabled.
489 The default value is 4.
490
491 .. code-block:: console
492
493     user@target:~$ ./build/app/vhost-switch -c f -n 4 --huge-dir / mnt/huge -- --rx-retry 1 --rx-retry-num 5
494
495 **RX Retry Delay Time.**
496 The rx-retry-delay option specifies the timeout (in micro seconds) between retries on an RX burst,
497 it takes effect only when rx retry is enabled.
498 The default value is 15.
499
500 .. code-block:: console
501
502     user@target:~$ ./build/app/vhost-switch -c f -n 4 --huge-dir / mnt/huge -- --rx-retry 1 --rx-retry-delay 20
503
504 **Zero copy.**
505 The zero copy option enables/disables the zero copy mode for RX/TX packet,
506 in the zero copy mode the packet buffer address from guest translate into host physical address
507 and then set directly as DMA address.
508 If the zero copy mode is disabled, then one copy mode is utilized in the sample.
509 This option is disabled by default.
510
511 .. code-block:: console
512
513     user@target:~$ ./build/app/vhost-switch -c f -n 4 --huge-dir /mnt/huge -- --zero-copy [0,1]
514
515 **RX descriptor number.**
516 The RX descriptor number option specify the Ethernet RX descriptor number,
517 Linux legacy virtio-net has different behaviour in how to use the vring descriptor from DPDK based virtio-net PMD,
518 the former likely allocate half for virtio header, another half for frame buffer,
519 while the latter allocate all for frame buffer,
520 this lead to different number for available frame buffer in vring,
521 and then lead to different Ethernet RX descriptor number could be used in zero copy mode.
522 So it is valid only in zero copy mode is enabled. The value is 32 by default.
523
524 .. code-block:: console
525
526     user@target:~$ ./build/app/vhost-switch -c f -n 4 --huge-dir /mnt/huge -- --zero-copy 1 --rx-desc-num [0, n]
527
528 **TX descriptornumber.**
529 The TX descriptor number option specify the Ethernet TX descriptor number, it is valid only in zero copy mode is enabled.
530 The value is 64 by default.
531
532 .. code-block:: console
533
534     user@target:~$ ./build/app/vhost-switch -c f -n 4 --huge-dir /mnt/huge -- --zero-copy 1 --tx-desc-num [0, n]
535
536 **VLAN strip.**
537 The VLAN strip option enable/disable the VLAN strip on host, if disabled, the guest will receive the packets with VLAN tag.
538 It is enabled by default.
539
540 .. code-block:: console
541
542     user@target:~$ ./build/app/vhost-switch -c f -n 4 --huge-dir /mnt/huge -- --vlan-strip [0, 1]
543
544 Running the Virtual Machine (QEMU)
545 ----------------------------------
546
547 QEMU must be executed with specific parameters to:
548
549 *   Ensure the guest is configured to use virtio-net network adapters.
550
551     .. code-block:: console
552
553         user@target:~$ qemu-system-x86_64 ... -device virtio-net-pci,netdev=hostnet1,id=net1 ...
554
555 *   Ensure the guest's virtio-net network adapter is configured with offloads disabled.
556
557     .. code-block:: console
558
559         user@target:~$ qemu-system-x86_64 ... -device virtio-net-pci,netdev=hostnet1,id=net1,csum=off,gso=off,guest_tso4=off,guest_tso6=off,guest_ecn=off
560
561 *   Redirect QEMU to communicate with the DPDK vhost-net sample code in place of the vhost-net kernel module(vhost cuse).
562
563     .. code-block:: console
564
565         user@target:~$ qemu-system-x86_64 ... -netdev tap,id=hostnet1,vhost=on,vhostfd=<open fd> ...
566
567 *   Enable the vhost-net sample code to map the VM's memory into its own process address space.
568
569     .. code-block:: console
570
571         user@target:~$ qemu-system-x86_64 ... -mem-prealloc -mem-path / dev/hugepages ...
572
573 .. note::
574
575     The QEMU wrapper (qemu-wrap.py) is a Python script designed to automate the QEMU configuration described above.
576     It also facilitates integration with libvirt, although the script may also be used standalone without libvirt.
577
578 Redirecting QEMU to vhost-net Sample Code(vhost cuse)
579 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
580
581 To redirect QEMU to the vhost-net sample code implementation of the vhost-net API,
582 an open file descriptor must be passed to QEMU running as a child process.
583
584 .. code-block:: python
585
586     #!/usr/bin/python
587     fd = os.open("/dev/usvhost-1", os.O_RDWR)
588     subprocess.call("qemu-system-x86_64 ... . -netdev tap,id=vhostnet0,vhost=on,vhostfd=" + fd +"...", shell=True)
589
590 .. note::
591
592     This process is automated in the QEMU wrapper script discussed in Section 24.7.3.
593
594 Mapping the Virtual Machine's Memory
595 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
596
597 For the DPDK vhost-net sample code to be run correctly, QEMU must allocate the VM's memory on hugetlbfs.
598 This is done by specifying mem-prealloc and mem-path when executing QEMU.
599 The vhost-net sample code accesses the virtio-net device's virtual rings and packet buffers
600 by finding and mapping the VM's physical memory on hugetlbfs.
601 In this case, the path passed to the guest should be that of the 1 GB page hugetlbfs:
602
603 .. code-block:: console
604
605     user@target:~$ qemu-system-x86_64 ... -mem-prealloc -mem-path / dev/hugepages ...
606
607 .. note::
608
609     This process is automated in the QEMU wrapper script discussed in Section 24.7.3.
610     The following two sections only applies to vhost cuse. For vhost-user, please make corresponding changes to qemu-wrapper script and guest XML file.
611
612 QEMU Wrapper Script
613 ~~~~~~~~~~~~~~~~~~~
614
615 The QEMU wrapper script automatically detects and calls QEMU with the necessary parameters required
616 to integrate with the vhost sample code.
617 It performs the following actions:
618
619 *   Automatically detects the location of the hugetlbfs and inserts this into the command line parameters.
620
621 *   Automatically open file descriptors for each virtio-net device and inserts this into the command line parameters.
622
623 *   Disables offloads on each virtio-net device.
624
625 *   Calls Qemu passing both the command line parameters passed to the script itself and those it has auto-detected.
626
627 The QEMU wrapper script will automatically configure calls to QEMU:
628
629 .. code-block:: console
630
631     user@target:~$ qemu-wrap.py -machine pc-i440fx-1.4,accel=kvm,usb=off -cpu SandyBridge -smp 4,sockets=4,cores=1,threads=1
632     -netdev tap,id=hostnet1,vhost=on -device virtio-net-pci,netdev=hostnet1,id=net1 -hda <disk img> -m 4096
633
634 which will become the following call to QEMU:
635
636 .. code-block:: console
637
638     /usr/local/bin/qemu-system-x86_64 -machine pc-i440fx-1.4,accel=kvm,usb=off -cpu SandyBridge -smp 4,sockets=4,cores=1,threads=1
639     -netdev tap,id=hostnet1,vhost=on,vhostfd=<open fd> -device virtio-net-pci,netdev=hostnet1,id=net1,
640     csum=off,gso=off,guest_tso4=off,guest_tso6=off,guest_ecn=off -hda <disk img> -m 4096 -mem-path /dev/hugepages -mem-prealloc
641
642 Libvirt Integration
643 ~~~~~~~~~~~~~~~~~~~
644
645 The QEMU wrapper script (qemu-wrap.py) "wraps" libvirt calls to QEMU,
646 such that QEMU is called with the correct parameters described above.
647 To call the QEMU wrapper automatically from libvirt, the following configuration changes must be made:
648
649 *   Place the QEMU wrapper script in libvirt's binary search PATH ($PATH).
650     A good location is in the directory that contains the QEMU binary.
651
652 *   Ensure that the script has the same owner/group and file permissions as the QEMU binary.
653
654 *   Update the VM xml file using virsh edit <vm name>:
655
656     *   Set the VM to use the launch script
657
658     *   Set the emulator path contained in the #<emulator><emulator/> tags For example,
659         replace <emulator>/usr/bin/qemu-kvm<emulator/> with  <emulator>/usr/bin/qemu-wrap.py<emulator/>
660
661     *   Set the VM's virtio-net device's to use vhost-net offload:
662
663         .. code-block:: xml
664
665             <interface type="network">
666             <model type="virtio"/>
667             <driver name="vhost"/>
668             <interface/>
669
670     *   Enable libvirt to access the DPDK Vhost sample code's character device file by adding it
671         to controllers cgroup for libvirtd using the following steps:
672
673         .. code-block:: xml
674
675             cgroup_controllers = [ ... "devices", ... ] clear_emulator_capabilities = 0
676             user = "root" group = "root"
677             cgroup_device_acl = [
678                 "/dev/null", "/dev/full", "/dev/zero",
679                 "/dev/random", "/dev/urandom",
680                 "/dev/ptmx", "/dev/kvm", "/dev/kqemu",
681                 "/dev/rtc", "/dev/hpet", "/dev/net/tun",
682                 "/dev/<devbase-name>-<index>",
683             ]
684
685 *   Disable SELinux  or set to permissive mode.
686
687
688 *   Mount cgroup device controller:
689
690     .. code-block:: console
691
692         user@target:~$ mkdir /dev/cgroup
693         user@target:~$ mount -t cgroup none /dev/cgroup -o devices
694
695 *   Restart the libvirtd system process
696
697     For example, on Fedora* "systemctl restart libvirtd.service"
698
699 *   Edit the configuration parameters section of the script:
700
701     *   Configure the "emul_path" variable to point to the QEMU emulator.
702
703         .. code-block:: xml
704
705             emul_path = "/usr/local/bin/qemu-system-x86_64"
706
707     *   Configure the "us_vhost_path" variable to point to the DPDK vhost-net sample code's character devices name.
708         DPDK vhost-net sample code's character device will be in the format "/dev/<basename>-<index>".
709
710         .. code-block:: xml
711
712             us_vhost_path = "/dev/usvhost-1"
713
714 Common Issues
715 ~~~~~~~~~~~~~
716
717 *   QEMU failing to allocate memory on hugetlbfs, with an error like the following::
718
719        file_ram_alloc: can't mmap RAM pages: Cannot allocate memory
720
721     When running QEMU the above error indicates that it has failed to allocate memory for the Virtual Machine on
722     the hugetlbfs. This is typically due to insufficient hugepages being free to support the allocation request.
723     The number of free hugepages can be checked as follows:
724
725     .. code-block:: console
726
727         cat /sys/kernel/mm/hugepages/hugepages-<pagesize>/nr_hugepages
728
729     The command above indicates how many hugepages are free to support QEMU's allocation request.
730
731 *   User space VHOST when the guest has 2MB sized huge pages:
732
733     The guest may have 2MB or 1GB sized huge pages. The user space VHOST should work properly in both cases.
734
735 *   User space VHOST will not work with QEMU without the ``-mem-prealloc`` option:
736
737     The current implementation works properly only when the guest memory is pre-allocated, so it is required to
738     use a QEMU version (e.g. 1.6) which supports ``-mem-prealloc``. The ``-mem-prealloc`` option must be
739     specified explicitly in the QEMU command line.
740
741 *   User space VHOST will not work with a QEMU version without shared memory mapping:
742
743     As shared memory mapping is mandatory for user space VHOST to work properly with the guest, user space VHOST
744     needs access to the shared memory from the guest to receive and transmit packets. It is important to make sure
745     the QEMU version supports shared memory mapping.
746
747 *   Issues with ``virsh destroy`` not destroying the VM:
748
749     Using libvirt ``virsh create`` the ``qemu-wrap.py`` spawns a new process to run ``qemu-kvm``. This impacts the behavior
750     of ``virsh destroy`` which kills the process running ``qemu-wrap.py`` without actually destroying the VM (it leaves
751     the ``qemu-kvm`` process running):
752
753     This following patch should fix this issue:
754         http://dpdk.org/ml/archives/dev/2014-June/003607.html
755
756 *   In an Ubuntu environment, QEMU fails to start a new guest normally with user space VHOST due to not being able
757     to allocate huge pages for the new guest:
758
759     The solution for this issue is to add ``-boot c`` into the QEMU command line to make sure the huge pages are
760     allocated properly and then the guest should start normally.
761
762     Use ``cat /proc/meminfo`` to check if there is any changes in the value of ``HugePages_Total`` and ``HugePages_Free``
763     after the guest startup.
764
765 *   Log message: ``eventfd_link: module verification failed: signature and/or required key missing - tainting kernel``:
766
767     This log message may be ignored. The message occurs due to the kernel module ``eventfd_link``, which is not a standard
768     Linux module but which is necessary for the user space VHOST current implementation (CUSE-based) to communicate with
769     the guest.
770
771
772 Running DPDK in the Virtual Machine
773 -----------------------------------
774
775 For the DPDK vhost-net sample code to switch packets into the VM,
776 the sample code must first learn the MAC address of the VM's virtio-net device.
777 The sample code detects the address from packets being transmitted from the VM, similar to a learning switch.
778
779 This behavior requires no special action or configuration with the Linux* virtio-net driver in the VM
780 as the Linux* Kernel will automatically transmit packets during device initialization.
781 However, DPDK-based applications must be modified to automatically transmit packets during initialization
782 to facilitate the DPDK vhost- net sample code's MAC learning.
783
784 The DPDK testpmd application can be configured to automatically transmit packets during initialization
785 and to act as an L2 forwarding switch.
786
787 Testpmd MAC Forwarding
788 ~~~~~~~~~~~~~~~~~~~~~~
789
790 At high packet rates, a minor packet loss may be observed.
791 To resolve this issue, a "wait and retry" mode is implemented in the testpmd and vhost sample code.
792 In the "wait and retry" mode if the virtqueue is found to be full, then testpmd waits for a period of time before retrying to enqueue packets.
793
794 The "wait and retry" algorithm is implemented in DPDK testpmd as a forwarding method call "mac_retry".
795 The following sequence diagram describes the algorithm in detail.
796
797 .. _figure_20:
798
799 **Figure 20. Packet Flow on TX in DPDK-testpmd**
800
801 .. image23_png has been renamed
802
803 |tx_dpdk_testpmd|
804
805 Running Testpmd
806 ~~~~~~~~~~~~~~~
807
808 The testpmd application is automatically built when DPDK is installed.
809 Run the testpmd application as follows:
810
811 .. code-block:: console
812
813     user@target:~$ x86_64-native-linuxapp-gcc/app/testpmd -c 0x3 -- n 4 -socket-mem 128 -- --burst=64 -i
814
815 The destination MAC address for packets transmitted on each port can be set at the command line:
816
817 .. code-block:: console
818
819     user@target:~$ x86_64-native-linuxapp-gcc/app/testpmd -c 0x3 -- n 4 -socket-mem 128 -- --burst=64 -i --eth- peer=0,aa:bb:cc:dd:ee:ff --eth-peer=1,ff,ee,dd,cc,bb,aa
820
821 *   Packets received on port 1 will be forwarded on port 0 to MAC address
822
823     aa:bb:cc:dd:ee:ff.
824
825 *   Packets received on port 0 will be forwarded on port 1 to MAC address
826
827     ff,ee,dd,cc,bb,aa.
828
829 The testpmd application can then be configured to act as an L2 forwarding application:
830
831 .. code-block:: console
832
833     testpmd> set fwd mac_retry
834
835 The testpmd can then be configured to start processing packets,
836 transmitting packets first so the DPDK vhost sample code on the host can learn the MAC address:
837
838 .. code-block:: console
839
840     testpmd> start tx_first
841
842 .. note::
843
844     Please note "set fwd mac_retry" is used in place of "set fwd mac_fwd" to ensure the retry feature is activated.
845
846 Passing Traffic to the Virtual Machine Device
847 ---------------------------------------------
848
849 For a virtio-net device to receive traffic,
850 the traffic's Layer 2 header must include both the virtio-net device's MAC address and VLAN tag.
851 The DPDK sample code behaves in a similar manner to a learning switch in that
852 it learns the MAC address of the virtio-net devices from the first transmitted packet.
853 On learning the MAC address,
854 the DPDK vhost sample code prints a message with the MAC address and VLAN tag virtio-net device.
855 For example:
856
857 .. code-block:: console
858
859     DATA: (0) MAC_ADDRESS cc:bb:bb:bb:bb:bb and VLAN_TAG 1000 registered
860
861 The above message indicates that device 0 has been registered with MAC address cc:bb:bb:bb:bb:bb and VLAN tag 1000.
862 Any packets received on the NIC with these values is placed on the devices receive queue.
863 When a virtio-net device transmits packets, the VLAN tag is added to the packet by the DPDK vhost sample code.
864
865 .. |vhost_net_arch| image:: img/vhost_net_arch.*
866
867 .. |qemu_virtio_net| image:: img/qemu_virtio_net.*
868
869 .. |tx_dpdk_testpmd| image:: img/tx_dpdk_testpmd.*
870
871 .. |vhost_net_sample_app| image:: img/vhost_net_sample_app.*
872
873 .. |virtio_linux_vhost| image:: img/virtio_linux_vhost.*