xref: /qemu/include/hw/virtio/vhost-user.h (revision d4eb50380807f5e881cbb66630f210e3e6eb9750)
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,
32*d4eb5038SHanna Czenczek     /* Feature 17 reserved for VHOST_USER_PROTOCOL_F_XEN_MMAP. */
33*d4eb5038SHanna Czenczek     VHOST_USER_PROTOCOL_F_SHARED_OBJECT = 18,
343d123a8bSJonah Palmer     VHOST_USER_PROTOCOL_F_MAX
353d123a8bSJonah Palmer };
363d123a8bSJonah Palmer 
37503e3554SAlex Bennée /**
38503e3554SAlex Bennée  * VhostUserHostNotifier - notifier information for one queue
39503e3554SAlex Bennée  * @rcu: rcu_head for cleanup
40503e3554SAlex Bennée  * @mr: memory region of notifier
41503e3554SAlex Bennée  * @addr: current mapped address
42503e3554SAlex Bennée  * @unmap_addr: address to be un-mapped
43503e3554SAlex Bennée  * @idx: virtioqueue index
44503e3554SAlex Bennée  *
45503e3554SAlex Bennée  * The VhostUserHostNotifier entries are re-used. When an old mapping
46503e3554SAlex Bennée  * is to be released it is moved to @unmap_addr and @addr is replaced.
47503e3554SAlex Bennée  * Once the RCU process has completed the unmap @unmap_addr is
48503e3554SAlex Bennée  * cleared.
49503e3554SAlex Bennée  */
5044866521STiwei Bie typedef struct VhostUserHostNotifier {
510b0af4d6SXueming Li     struct rcu_head rcu;
5244866521STiwei Bie     MemoryRegion mr;
5344866521STiwei Bie     void *addr;
540b0af4d6SXueming Li     void *unmap_addr;
55503e3554SAlex Bennée     int idx;
5644866521STiwei Bie } VhostUserHostNotifier;
574d0cf552STiwei Bie 
58503e3554SAlex Bennée /**
59503e3554SAlex Bennée  * VhostUserState - shared state for all vhost-user devices
60503e3554SAlex Bennée  * @chr: the character backend for the socket
61503e3554SAlex Bennée  * @notifiers: GPtrArray of @VhostUserHostnotifier
62503e3554SAlex Bennée  * @memory_slots:
63503e3554SAlex Bennée  */
644d0cf552STiwei Bie typedef struct VhostUserState {
654d0cf552STiwei Bie     CharBackend *chr;
66503e3554SAlex Bennée     GPtrArray *notifiers;
676b0eff1aSRaphael Norwitz     int memory_slots;
6856534930SAlex Bennée     bool supports_config;
694d0cf552STiwei Bie } VhostUserState;
704d0cf552STiwei Bie 
71503e3554SAlex Bennée /**
72503e3554SAlex Bennée  * vhost_user_init() - initialise shared vhost_user state
73503e3554SAlex Bennée  * @user: allocated area for storing shared state
74503e3554SAlex Bennée  * @chr: the chardev for the vhost socket
75503e3554SAlex Bennée  * @errp: error handle
76503e3554SAlex Bennée  *
77503e3554SAlex Bennée  * User can either directly g_new() space for the state or embed
78503e3554SAlex Bennée  * VhostUserState in their larger device structure and just point to
79503e3554SAlex Bennée  * it.
80503e3554SAlex Bennée  *
81503e3554SAlex Bennée  * Return: true on success, false on error while setting errp.
82503e3554SAlex Bennée  */
830b99f224SMarc-André Lureau bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp);
84503e3554SAlex Bennée 
85503e3554SAlex Bennée /**
86503e3554SAlex Bennée  * vhost_user_cleanup() - cleanup state
87503e3554SAlex Bennée  * @user: ptr to use state
88503e3554SAlex Bennée  *
89503e3554SAlex Bennée  * Cleans up shared state and notifiers, callee is responsible for
90503e3554SAlex Bennée  * freeing the @VhostUserState memory itself.
91503e3554SAlex Bennée  */
924d0cf552STiwei Bie void vhost_user_cleanup(VhostUserState *user);
934d0cf552STiwei Bie 
9471e076a0SAlex Bennée /**
9571e076a0SAlex Bennée  * vhost_user_async_close() - cleanup vhost-user post connection drop
9671e076a0SAlex Bennée  * @d: DeviceState for the associated device (passed to callback)
9771e076a0SAlex Bennée  * @chardev: the CharBackend associated with the connection
9871e076a0SAlex Bennée  * @vhost: the common vhost device
9971e076a0SAlex Bennée  * @cb: the user callback function to complete the clean-up
10071e076a0SAlex Bennée  *
10171e076a0SAlex Bennée  * This function is used to handle the shutdown of a vhost-user
10271e076a0SAlex Bennée  * connection to a backend. We handle this centrally to make sure we
10371e076a0SAlex Bennée  * do all the steps and handle potential races due to VM shutdowns.
10471e076a0SAlex Bennée  * Once the connection is disabled we call a backhalf to ensure
10571e076a0SAlex Bennée  */
10671e076a0SAlex Bennée typedef void (*vu_async_close_fn)(DeviceState *cb);
10771e076a0SAlex Bennée 
10871e076a0SAlex Bennée void vhost_user_async_close(DeviceState *d,
10971e076a0SAlex Bennée                             CharBackend *chardev, struct vhost_dev *vhost,
110f02a4b8eSLi Feng                             vu_async_close_fn cb,
111f02a4b8eSLi Feng                             IOEventHandler *event_cb);
11271e076a0SAlex Bennée 
1134d0cf552STiwei Bie #endif
114