xref: /openbmc/qemu/include/hw/virtio/vhost-user.h (revision 806f71ee)
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_MAX
35 };
36 
37 /**
38  * VhostUserHostNotifier - notifier information for one queue
39  * @rcu: rcu_head for cleanup
40  * @mr: memory region of notifier
41  * @addr: current mapped address
42  * @unmap_addr: address to be un-mapped
43  * @idx: virtioqueue index
44  *
45  * The VhostUserHostNotifier entries are re-used. When an old mapping
46  * is to be released it is moved to @unmap_addr and @addr is replaced.
47  * Once the RCU process has completed the unmap @unmap_addr is
48  * cleared.
49  */
50 typedef struct VhostUserHostNotifier {
51     struct rcu_head rcu;
52     MemoryRegion mr;
53     void *addr;
54     void *unmap_addr;
55     int idx;
56 } VhostUserHostNotifier;
57 
58 /**
59  * VhostUserState - shared state for all vhost-user devices
60  * @chr: the character backend for the socket
61  * @notifiers: GPtrArray of @VhostUserHostnotifier
62  * @memory_slots:
63  */
64 typedef struct VhostUserState {
65     CharBackend *chr;
66     GPtrArray *notifiers;
67     int memory_slots;
68     bool supports_config;
69 } VhostUserState;
70 
71 /**
72  * vhost_user_init() - initialise shared vhost_user state
73  * @user: allocated area for storing shared state
74  * @chr: the chardev for the vhost socket
75  * @errp: error handle
76  *
77  * User can either directly g_new() space for the state or embed
78  * VhostUserState in their larger device structure and just point to
79  * it.
80  *
81  * Return: true on success, false on error while setting errp.
82  */
83 bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp);
84 
85 /**
86  * vhost_user_cleanup() - cleanup state
87  * @user: ptr to use state
88  *
89  * Cleans up shared state and notifiers, callee is responsible for
90  * freeing the @VhostUserState memory itself.
91  */
92 void vhost_user_cleanup(VhostUserState *user);
93 
94 /**
95  * vhost_user_async_close() - cleanup vhost-user post connection drop
96  * @d: DeviceState for the associated device (passed to callback)
97  * @chardev: the CharBackend associated with the connection
98  * @vhost: the common vhost device
99  * @cb: the user callback function to complete the clean-up
100  *
101  * This function is used to handle the shutdown of a vhost-user
102  * connection to a backend. We handle this centrally to make sure we
103  * do all the steps and handle potential races due to VM shutdowns.
104  * Once the connection is disabled we call a backhalf to ensure
105  */
106 typedef void (*vu_async_close_fn)(DeviceState *cb);
107 
108 void vhost_user_async_close(DeviceState *d,
109                             CharBackend *chardev, struct vhost_dev *vhost,
110                             vu_async_close_fn cb,
111                             IOEventHandler *event_cb);
112 
113 #endif
114