xref: /qemu/include/hw/virtio/vhost-user.h (revision f02a4b8e6431598612466f76aac64ab492849abf)
14d0cf552STiwei Bie /*
24d0cf552STiwei Bie  * Copyright (c) 2017-2018 Intel Corporation
34d0cf552STiwei Bie  *
44d0cf552STiwei Bie  * This work is licensed under the terms of the GNU GPL, version 2.
54d0cf552STiwei Bie  * See the COPYING file in the top-level directory.
64d0cf552STiwei Bie  */
74d0cf552STiwei Bie 
84d0cf552STiwei Bie #ifndef HW_VIRTIO_VHOST_USER_H
94d0cf552STiwei Bie #define HW_VIRTIO_VHOST_USER_H
104d0cf552STiwei Bie 
114d0cf552STiwei Bie #include "chardev/char-fe.h"
1244866521STiwei Bie #include "hw/virtio/virtio.h"
1344866521STiwei Bie 
143d123a8bSJonah Palmer enum VhostUserProtocolFeature {
153d123a8bSJonah Palmer     VHOST_USER_PROTOCOL_F_MQ = 0,
163d123a8bSJonah Palmer     VHOST_USER_PROTOCOL_F_LOG_SHMFD = 1,
173d123a8bSJonah Palmer     VHOST_USER_PROTOCOL_F_RARP = 2,
183d123a8bSJonah Palmer     VHOST_USER_PROTOCOL_F_REPLY_ACK = 3,
193d123a8bSJonah Palmer     VHOST_USER_PROTOCOL_F_NET_MTU = 4,
203d123a8bSJonah Palmer     VHOST_USER_PROTOCOL_F_BACKEND_REQ = 5,
213d123a8bSJonah Palmer     VHOST_USER_PROTOCOL_F_CROSS_ENDIAN = 6,
223d123a8bSJonah Palmer     VHOST_USER_PROTOCOL_F_CRYPTO_SESSION = 7,
233d123a8bSJonah Palmer     VHOST_USER_PROTOCOL_F_PAGEFAULT = 8,
243d123a8bSJonah Palmer     VHOST_USER_PROTOCOL_F_CONFIG = 9,
253d123a8bSJonah Palmer     VHOST_USER_PROTOCOL_F_BACKEND_SEND_FD = 10,
263d123a8bSJonah Palmer     VHOST_USER_PROTOCOL_F_HOST_NOTIFIER = 11,
273d123a8bSJonah Palmer     VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD = 12,
283d123a8bSJonah Palmer     VHOST_USER_PROTOCOL_F_RESET_DEVICE = 13,
293d123a8bSJonah Palmer     VHOST_USER_PROTOCOL_F_INBAND_NOTIFICATIONS = 14,
303d123a8bSJonah Palmer     VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS = 15,
313d123a8bSJonah Palmer     VHOST_USER_PROTOCOL_F_STATUS = 16,
3216094766SAlbert Esteve     VHOST_USER_PROTOCOL_F_SHARED_OBJECT = 17,
333d123a8bSJonah Palmer     VHOST_USER_PROTOCOL_F_MAX
343d123a8bSJonah Palmer };
353d123a8bSJonah Palmer 
36503e3554SAlex Bennée /**
37503e3554SAlex Bennée  * VhostUserHostNotifier - notifier information for one queue
38503e3554SAlex Bennée  * @rcu: rcu_head for cleanup
39503e3554SAlex Bennée  * @mr: memory region of notifier
40503e3554SAlex Bennée  * @addr: current mapped address
41503e3554SAlex Bennée  * @unmap_addr: address to be un-mapped
42503e3554SAlex Bennée  * @idx: virtioqueue index
43503e3554SAlex Bennée  *
44503e3554SAlex Bennée  * The VhostUserHostNotifier entries are re-used. When an old mapping
45503e3554SAlex Bennée  * is to be released it is moved to @unmap_addr and @addr is replaced.
46503e3554SAlex Bennée  * Once the RCU process has completed the unmap @unmap_addr is
47503e3554SAlex Bennée  * cleared.
48503e3554SAlex Bennée  */
4944866521STiwei Bie typedef struct VhostUserHostNotifier {
500b0af4d6SXueming Li     struct rcu_head rcu;
5144866521STiwei Bie     MemoryRegion mr;
5244866521STiwei Bie     void *addr;
530b0af4d6SXueming Li     void *unmap_addr;
54503e3554SAlex Bennée     int idx;
5544866521STiwei Bie } VhostUserHostNotifier;
564d0cf552STiwei Bie 
57503e3554SAlex Bennée /**
58503e3554SAlex Bennée  * VhostUserState - shared state for all vhost-user devices
59503e3554SAlex Bennée  * @chr: the character backend for the socket
60503e3554SAlex Bennée  * @notifiers: GPtrArray of @VhostUserHostnotifier
61503e3554SAlex Bennée  * @memory_slots:
62503e3554SAlex Bennée  */
634d0cf552STiwei Bie typedef struct VhostUserState {
644d0cf552STiwei Bie     CharBackend *chr;
65503e3554SAlex Bennée     GPtrArray *notifiers;
666b0eff1aSRaphael Norwitz     int memory_slots;
6756534930SAlex Bennée     bool supports_config;
684d0cf552STiwei Bie } VhostUserState;
694d0cf552STiwei Bie 
70503e3554SAlex Bennée /**
71503e3554SAlex Bennée  * vhost_user_init() - initialise shared vhost_user state
72503e3554SAlex Bennée  * @user: allocated area for storing shared state
73503e3554SAlex Bennée  * @chr: the chardev for the vhost socket
74503e3554SAlex Bennée  * @errp: error handle
75503e3554SAlex Bennée  *
76503e3554SAlex Bennée  * User can either directly g_new() space for the state or embed
77503e3554SAlex Bennée  * VhostUserState in their larger device structure and just point to
78503e3554SAlex Bennée  * it.
79503e3554SAlex Bennée  *
80503e3554SAlex Bennée  * Return: true on success, false on error while setting errp.
81503e3554SAlex Bennée  */
820b99f224SMarc-André Lureau bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp);
83503e3554SAlex Bennée 
84503e3554SAlex Bennée /**
85503e3554SAlex Bennée  * vhost_user_cleanup() - cleanup state
86503e3554SAlex Bennée  * @user: ptr to use state
87503e3554SAlex Bennée  *
88503e3554SAlex Bennée  * Cleans up shared state and notifiers, callee is responsible for
89503e3554SAlex Bennée  * freeing the @VhostUserState memory itself.
90503e3554SAlex Bennée  */
914d0cf552STiwei Bie void vhost_user_cleanup(VhostUserState *user);
924d0cf552STiwei Bie 
9371e076a0SAlex Bennée /**
9471e076a0SAlex Bennée  * vhost_user_async_close() - cleanup vhost-user post connection drop
9571e076a0SAlex Bennée  * @d: DeviceState for the associated device (passed to callback)
9671e076a0SAlex Bennée  * @chardev: the CharBackend associated with the connection
9771e076a0SAlex Bennée  * @vhost: the common vhost device
9871e076a0SAlex Bennée  * @cb: the user callback function to complete the clean-up
9971e076a0SAlex Bennée  *
10071e076a0SAlex Bennée  * This function is used to handle the shutdown of a vhost-user
10171e076a0SAlex Bennée  * connection to a backend. We handle this centrally to make sure we
10271e076a0SAlex Bennée  * do all the steps and handle potential races due to VM shutdowns.
10371e076a0SAlex Bennée  * Once the connection is disabled we call a backhalf to ensure
10471e076a0SAlex Bennée  */
10571e076a0SAlex Bennée typedef void (*vu_async_close_fn)(DeviceState *cb);
10671e076a0SAlex Bennée 
10771e076a0SAlex Bennée void vhost_user_async_close(DeviceState *d,
10871e076a0SAlex Bennée                             CharBackend *chardev, struct vhost_dev *vhost,
109*f02a4b8eSLi Feng                             vu_async_close_fn cb,
110*f02a4b8eSLi Feng                             IOEventHandler *event_cb);
11171e076a0SAlex Bennée 
1124d0cf552STiwei Bie #endif
113