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