vhost_lib.rst revision 97f17497
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
31Vhost Library
32=============
33
34The vhost library implements a user space vhost driver. It supports both vhost-cuse
35(cuse: user space character device) and vhost-user(user space socket server).
36It also creates, manages and destroys vhost devices for corresponding virtio
37devices in the guest. Vhost supported vSwitch could register callbacks to this
38library, which will be called when a vhost device is activated or deactivated
39by guest virtual machine.
40
41Vhost API Overview
42------------------
43
44*   Vhost driver registration
45
46      rte_vhost_driver_register registers the vhost driver into the system.
47      For vhost-cuse, character device file will be created under the /dev directory.
48      Character device name is specified as the parameter.
49      For vhost-user, a Unix domain socket server will be created with the parameter as
50      the local socket path.
51
52*   Vhost session start
53
54      rte_vhost_driver_session_start starts the vhost session loop.
55      Vhost session is an infinite blocking loop.
56      Put the session in a dedicate DPDK thread.
57
58*   Callback register
59
60      Vhost supported vSwitch could call rte_vhost_driver_callback_register to
61      register two callbacks, new_destory and destroy_device.
62      When virtio device is activated or deactivated by guest virtual machine,
63      the callback will be called, then vSwitch could put the device onto data
64      core or remove the device from data core by setting or unsetting
65      VIRTIO_DEV_RUNNING on the device flags.
66
67*   Read/write packets from/to guest virtual machine
68
69      rte_vhost_enqueue_burst transmit host packets to guest.
70      rte_vhost_dequeue_burst receives packets from guest.
71
72*   Feature enable/disable
73
74      Now one negotiate-able feature in vhost is merge-able.
75      vSwitch could enable/disable this feature for performance consideration.
76
77Vhost Implementation
78--------------------
79
80Vhost cuse implementation
81~~~~~~~~~~~~~~~~~~~~~~~~~
82When vSwitch registers the vhost driver, it will register a cuse device driver
83into the system and creates a character device file. This cuse driver will
84receive vhost open/release/IOCTL message from QEMU simulator.
85
86When the open call is received, vhost driver will create a vhost device for the
87virtio device in the guest.
88
89When VHOST_SET_MEM_TABLE IOCTL is received, vhost searches the memory region
90to find the starting user space virtual address that maps the memory of guest
91virtual machine. Through this virtual address and the QEMU pid, vhost could
92find the file QEMU uses to map the guest memory. Vhost maps this file into its
93address space, in this way vhost could fully access the guest physical memory,
94which means vhost could access the shared virtio ring and the guest physical
95address specified in the entry of the ring.
96
97The guest virtual machine tells the vhost whether the virtio device is ready
98for processing or is de-activated through VHOST_NET_SET_BACKEND message.
99The registered callback from vSwitch will be called.
100
101When the release call is released, vhost will destroy the device.
102
103Vhost user implementation
104~~~~~~~~~~~~~~~~~~~~~~~~~
105When vSwitch registers a vhost driver, it will create a Unix domain socket server
106into the system. This server will listen for a connection and process the vhost message from
107QEMU simulator.
108
109When there is a new socket connection, it means a new virtio device has been created in
110the guest virtual machine, and the vhost driver will create a vhost device for this virtio device.
111
112For messages with a file descriptor, the file descriptor could be directly used in the vhost
113process as it is already installed by Unix domain socket.
114
115 * VHOST_SET_MEM_TABLE
116 * VHOST_SET_VRING_KICK
117 * VHOST_SET_VRING_CALL
118 * VHOST_SET_LOG_FD
119 * VHOST_SET_VRING_ERR
120
121For VHOST_SET_MEM_TABLE message, QEMU will send us information for each memory region and its
122file descriptor in the ancillary data of the message. The fd is used to map that region.
123
124There is no VHOST_NET_SET_BACKEND message as in vhost cuse to signal us whether virtio device
125is ready or should be stopped.
126VHOST_SET_VRING_KICK is used as the signal to put the vhost device onto data plane.
127VHOST_GET_VRING_BASE is used as the signal to remove vhost device from data plane.
128
129When the socket connection is closed, vhost will destroy the device.
130
131Vhost supported vSwitch reference
132---------------------------------
133
134For more vhost details and how to support vhost in vSwitch, please refer to vhost example in the
135DPDK Sample Applications Guide.
136