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