xref: /qemu/include/hw/virtio/vhost-user.h (revision 73b49878)
1 /*
2  * Copyright (c) 2017-2018 Intel Corporation
3  *
4  * This work is licensed under the terms of the GNU GPL, version 2.
5  * See the COPYING file in the top-level directory.
6  */
7 
8 #ifndef HW_VIRTIO_VHOST_USER_H
9 #define HW_VIRTIO_VHOST_USER_H
10 
11 #include "chardev/char-fe.h"
12 #include "hw/virtio/virtio.h"
13 
14 enum VhostUserProtocolFeature {
15     VHOST_USER_PROTOCOL_F_MQ = 0,
16     VHOST_USER_PROTOCOL_F_LOG_SHMFD = 1,
17     VHOST_USER_PROTOCOL_F_RARP = 2,
18     VHOST_USER_PROTOCOL_F_REPLY_ACK = 3,
19     VHOST_USER_PROTOCOL_F_NET_MTU = 4,
20     VHOST_USER_PROTOCOL_F_BACKEND_REQ = 5,
21     VHOST_USER_PROTOCOL_F_CROSS_ENDIAN = 6,
22     VHOST_USER_PROTOCOL_F_CRYPTO_SESSION = 7,
23     VHOST_USER_PROTOCOL_F_PAGEFAULT = 8,
24     VHOST_USER_PROTOCOL_F_CONFIG = 9,
25     VHOST_USER_PROTOCOL_F_BACKEND_SEND_FD = 10,
26     VHOST_USER_PROTOCOL_F_HOST_NOTIFIER = 11,
27     VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD = 12,
28     VHOST_USER_PROTOCOL_F_RESET_DEVICE = 13,
29     VHOST_USER_PROTOCOL_F_INBAND_NOTIFICATIONS = 14,
30     VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS = 15,
31     VHOST_USER_PROTOCOL_F_STATUS = 16,
32     /* Feature 17 reserved for VHOST_USER_PROTOCOL_F_XEN_MMAP. */
33     VHOST_USER_PROTOCOL_F_SHARED_OBJECT = 18,
34     VHOST_USER_PROTOCOL_F_DEVICE_STATE = 19,
35     VHOST_USER_PROTOCOL_F_MAX
36 };
37 
38 /**
39  * VhostUserHostNotifier - notifier information for one queue
40  * @rcu: rcu_head for cleanup
41  * @mr: memory region of notifier
42  * @addr: current mapped address
43  * @unmap_addr: address to be un-mapped
44  * @idx: virtioqueue index
45  *
46  * The VhostUserHostNotifier entries are re-used. When an old mapping
47  * is to be released it is moved to @unmap_addr and @addr is replaced.
48  * Once the RCU process has completed the unmap @unmap_addr is
49  * cleared.
50  */
51 typedef struct VhostUserHostNotifier {
52     struct rcu_head rcu;
53     MemoryRegion mr;
54     void *addr;
55     void *unmap_addr;
56     int idx;
57 } VhostUserHostNotifier;
58 
59 /**
60  * VhostUserState - shared state for all vhost-user devices
61  * @chr: the character backend for the socket
62  * @notifiers: GPtrArray of @VhostUserHostnotifier
63  * @memory_slots:
64  */
65 typedef struct VhostUserState {
66     CharBackend *chr;
67     GPtrArray *notifiers;
68     int memory_slots;
69     bool supports_config;
70 } VhostUserState;
71 
72 /**
73  * vhost_user_init() - initialise shared vhost_user state
74  * @user: allocated area for storing shared state
75  * @chr: the chardev for the vhost socket
76  * @errp: error handle
77  *
78  * User can either directly g_new() space for the state or embed
79  * VhostUserState in their larger device structure and just point to
80  * it.
81  *
82  * Return: true on success, false on error while setting errp.
83  */
84 bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp);
85 
86 /**
87  * vhost_user_cleanup() - cleanup state
88  * @user: ptr to use state
89  *
90  * Cleans up shared state and notifiers, callee is responsible for
91  * freeing the @VhostUserState memory itself.
92  */
93 void vhost_user_cleanup(VhostUserState *user);
94 
95 /**
96  * vhost_user_async_close() - cleanup vhost-user post connection drop
97  * @d: DeviceState for the associated device (passed to callback)
98  * @chardev: the CharBackend associated with the connection
99  * @vhost: the common vhost device
100  * @cb: the user callback function to complete the clean-up
101  *
102  * This function is used to handle the shutdown of a vhost-user
103  * connection to a backend. We handle this centrally to make sure we
104  * do all the steps and handle potential races due to VM shutdowns.
105  * Once the connection is disabled we call a backhalf to ensure
106  */
107 typedef void (*vu_async_close_fn)(DeviceState *cb);
108 
109 void vhost_user_async_close(DeviceState *d,
110                             CharBackend *chardev, struct vhost_dev *vhost,
111                             vu_async_close_fn cb,
112                             IOEventHandler *event_cb);
113 
114 #endif
115