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, 32d4eb5038SHanna Czenczek /* Feature 17 reserved for VHOST_USER_PROTOCOL_F_XEN_MMAP. */ 33d4eb5038SHanna Czenczek VHOST_USER_PROTOCOL_F_SHARED_OBJECT = 18, 34cda83adcSHanna Czenczek VHOST_USER_PROTOCOL_F_DEVICE_STATE = 19, 353d123a8bSJonah Palmer VHOST_USER_PROTOCOL_F_MAX 363d123a8bSJonah Palmer }; 373d123a8bSJonah Palmer 38503e3554SAlex Bennée /** 39503e3554SAlex Bennée * VhostUserHostNotifier - notifier information for one queue 40503e3554SAlex Bennée * @rcu: rcu_head for cleanup 41503e3554SAlex Bennée * @mr: memory region of notifier 42503e3554SAlex Bennée * @addr: current mapped address 43503e3554SAlex Bennée * @unmap_addr: address to be un-mapped 44503e3554SAlex Bennée * @idx: virtioqueue index 45503e3554SAlex Bennée * 46503e3554SAlex Bennée * The VhostUserHostNotifier entries are re-used. When an old mapping 47503e3554SAlex Bennée * is to be released it is moved to @unmap_addr and @addr is replaced. 48503e3554SAlex Bennée * Once the RCU process has completed the unmap @unmap_addr is 49503e3554SAlex Bennée * cleared. 50503e3554SAlex Bennée */ 5144866521STiwei Bie typedef struct VhostUserHostNotifier { 520b0af4d6SXueming Li struct rcu_head rcu; 5344866521STiwei Bie MemoryRegion mr; 5444866521STiwei Bie void *addr; 550b0af4d6SXueming Li void *unmap_addr; 56503e3554SAlex Bennée int idx; 57*963b0276Syaozhenguo bool destroy; 5844866521STiwei Bie } VhostUserHostNotifier; 594d0cf552STiwei Bie 60503e3554SAlex Bennée /** 61503e3554SAlex Bennée * VhostUserState - shared state for all vhost-user devices 62503e3554SAlex Bennée * @chr: the character backend for the socket 63503e3554SAlex Bennée * @notifiers: GPtrArray of @VhostUserHostnotifier 64503e3554SAlex Bennée * @memory_slots: 65503e3554SAlex Bennée */ 664d0cf552STiwei Bie typedef struct VhostUserState { 674d0cf552STiwei Bie CharBackend *chr; 68503e3554SAlex Bennée GPtrArray *notifiers; 696b0eff1aSRaphael Norwitz int memory_slots; 7056534930SAlex Bennée bool supports_config; 714d0cf552STiwei Bie } VhostUserState; 724d0cf552STiwei Bie 73503e3554SAlex Bennée /** 74503e3554SAlex Bennée * vhost_user_init() - initialise shared vhost_user state 75503e3554SAlex Bennée * @user: allocated area for storing shared state 76503e3554SAlex Bennée * @chr: the chardev for the vhost socket 77503e3554SAlex Bennée * @errp: error handle 78503e3554SAlex Bennée * 79503e3554SAlex Bennée * User can either directly g_new() space for the state or embed 80503e3554SAlex Bennée * VhostUserState in their larger device structure and just point to 81503e3554SAlex Bennée * it. 82503e3554SAlex Bennée * 83503e3554SAlex Bennée * Return: true on success, false on error while setting errp. 84503e3554SAlex Bennée */ 850b99f224SMarc-André Lureau bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp); 86503e3554SAlex Bennée 87503e3554SAlex Bennée /** 88503e3554SAlex Bennée * vhost_user_cleanup() - cleanup state 89503e3554SAlex Bennée * @user: ptr to use state 90503e3554SAlex Bennée * 91503e3554SAlex Bennée * Cleans up shared state and notifiers, callee is responsible for 92503e3554SAlex Bennée * freeing the @VhostUserState memory itself. 93503e3554SAlex Bennée */ 944d0cf552STiwei Bie void vhost_user_cleanup(VhostUserState *user); 954d0cf552STiwei Bie 9671e076a0SAlex Bennée /** 9771e076a0SAlex Bennée * vhost_user_async_close() - cleanup vhost-user post connection drop 9871e076a0SAlex Bennée * @d: DeviceState for the associated device (passed to callback) 9971e076a0SAlex Bennée * @chardev: the CharBackend associated with the connection 10071e076a0SAlex Bennée * @vhost: the common vhost device 10171e076a0SAlex Bennée * @cb: the user callback function to complete the clean-up 10271e076a0SAlex Bennée * 10371e076a0SAlex Bennée * This function is used to handle the shutdown of a vhost-user 10471e076a0SAlex Bennée * connection to a backend. We handle this centrally to make sure we 10571e076a0SAlex Bennée * do all the steps and handle potential races due to VM shutdowns. 10671e076a0SAlex Bennée * Once the connection is disabled we call a backhalf to ensure 10771e076a0SAlex Bennée */ 10871e076a0SAlex Bennée typedef void (*vu_async_close_fn)(DeviceState *cb); 10971e076a0SAlex Bennée 11071e076a0SAlex Bennée void vhost_user_async_close(DeviceState *d, 11171e076a0SAlex Bennée CharBackend *chardev, struct vhost_dev *vhost, 1129569fe0aSLi Feng vu_async_close_fn cb); 11371e076a0SAlex Bennée 1144d0cf552STiwei Bie #endif 115