xref: /openbmc/qemu/include/hw/virtio/vhost-user.h (revision 7d87775f)
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     bool destroy;
58 } VhostUserHostNotifier;
59 
60 /**
61  * VhostUserState - shared state for all vhost-user devices
62  * @chr: the character backend for the socket
63  * @notifiers: GPtrArray of @VhostUserHostnotifier
64  * @memory_slots:
65  */
66 typedef struct VhostUserState {
67     CharBackend *chr;
68     GPtrArray *notifiers;
69     int memory_slots;
70     bool supports_config;
71 } VhostUserState;
72 
73 /**
74  * vhost_user_init() - initialise shared vhost_user state
75  * @user: allocated area for storing shared state
76  * @chr: the chardev for the vhost socket
77  * @errp: error handle
78  *
79  * User can either directly g_new() space for the state or embed
80  * VhostUserState in their larger device structure and just point to
81  * it.
82  *
83  * Return: true on success, false on error while setting errp.
84  */
85 bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp);
86 
87 /**
88  * vhost_user_cleanup() - cleanup state
89  * @user: ptr to use state
90  *
91  * Cleans up shared state and notifiers, callee is responsible for
92  * freeing the @VhostUserState memory itself.
93  */
94 void vhost_user_cleanup(VhostUserState *user);
95 
96 /**
97  * vhost_user_async_close() - cleanup vhost-user post connection drop
98  * @d: DeviceState for the associated device (passed to callback)
99  * @chardev: the CharBackend associated with the connection
100  * @vhost: the common vhost device
101  * @cb: the user callback function to complete the clean-up
102  *
103  * This function is used to handle the shutdown of a vhost-user
104  * connection to a backend. We handle this centrally to make sure we
105  * do all the steps and handle potential races due to VM shutdowns.
106  * Once the connection is disabled we call a backhalf to ensure
107  */
108 typedef void (*vu_async_close_fn)(DeviceState *cb);
109 
110 void vhost_user_async_close(DeviceState *d,
111                             CharBackend *chardev, struct vhost_dev *vhost,
112                             vu_async_close_fn cb);
113 
114 #endif
115