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