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 14503e3554SAlex Bennée /** 15503e3554SAlex Bennée * VhostUserHostNotifier - notifier information for one queue 16503e3554SAlex Bennée * @rcu: rcu_head for cleanup 17503e3554SAlex Bennée * @mr: memory region of notifier 18503e3554SAlex Bennée * @addr: current mapped address 19503e3554SAlex Bennée * @unmap_addr: address to be un-mapped 20503e3554SAlex Bennée * @idx: virtioqueue index 21503e3554SAlex Bennée * 22503e3554SAlex Bennée * The VhostUserHostNotifier entries are re-used. When an old mapping 23503e3554SAlex Bennée * is to be released it is moved to @unmap_addr and @addr is replaced. 24503e3554SAlex Bennée * Once the RCU process has completed the unmap @unmap_addr is 25503e3554SAlex Bennée * cleared. 26503e3554SAlex Bennée */ 2744866521STiwei Bie typedef struct VhostUserHostNotifier { 280b0af4d6SXueming Li struct rcu_head rcu; 2944866521STiwei Bie MemoryRegion mr; 3044866521STiwei Bie void *addr; 310b0af4d6SXueming Li void *unmap_addr; 32503e3554SAlex Bennée int idx; 3344866521STiwei Bie } VhostUserHostNotifier; 344d0cf552STiwei Bie 35503e3554SAlex Bennée /** 36503e3554SAlex Bennée * VhostUserState - shared state for all vhost-user devices 37503e3554SAlex Bennée * @chr: the character backend for the socket 38503e3554SAlex Bennée * @notifiers: GPtrArray of @VhostUserHostnotifier 39503e3554SAlex Bennée * @memory_slots: 40503e3554SAlex Bennée */ 414d0cf552STiwei Bie typedef struct VhostUserState { 424d0cf552STiwei Bie CharBackend *chr; 43503e3554SAlex Bennée GPtrArray *notifiers; 446b0eff1aSRaphael Norwitz int memory_slots; 4556534930SAlex Bennée bool supports_config; 464d0cf552STiwei Bie } VhostUserState; 474d0cf552STiwei Bie 48503e3554SAlex Bennée /** 49503e3554SAlex Bennée * vhost_user_init() - initialise shared vhost_user state 50503e3554SAlex Bennée * @user: allocated area for storing shared state 51503e3554SAlex Bennée * @chr: the chardev for the vhost socket 52503e3554SAlex Bennée * @errp: error handle 53503e3554SAlex Bennée * 54503e3554SAlex Bennée * User can either directly g_new() space for the state or embed 55503e3554SAlex Bennée * VhostUserState in their larger device structure and just point to 56503e3554SAlex Bennée * it. 57503e3554SAlex Bennée * 58503e3554SAlex Bennée * Return: true on success, false on error while setting errp. 59503e3554SAlex Bennée */ 600b99f224SMarc-André Lureau bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp); 61503e3554SAlex Bennée 62503e3554SAlex Bennée /** 63503e3554SAlex Bennée * vhost_user_cleanup() - cleanup state 64503e3554SAlex Bennée * @user: ptr to use state 65503e3554SAlex Bennée * 66503e3554SAlex Bennée * Cleans up shared state and notifiers, callee is responsible for 67503e3554SAlex Bennée * freeing the @VhostUserState memory itself. 68503e3554SAlex Bennée */ 694d0cf552STiwei Bie void vhost_user_cleanup(VhostUserState *user); 704d0cf552STiwei Bie 7171e076a0SAlex Bennée /** 7271e076a0SAlex Bennée * vhost_user_async_close() - cleanup vhost-user post connection drop 7371e076a0SAlex Bennée * @d: DeviceState for the associated device (passed to callback) 7471e076a0SAlex Bennée * @chardev: the CharBackend associated with the connection 7571e076a0SAlex Bennée * @vhost: the common vhost device 7671e076a0SAlex Bennée * @cb: the user callback function to complete the clean-up 7771e076a0SAlex Bennée * 7871e076a0SAlex Bennée * This function is used to handle the shutdown of a vhost-user 7971e076a0SAlex Bennée * connection to a backend. We handle this centrally to make sure we 8071e076a0SAlex Bennée * do all the steps and handle potential races due to VM shutdowns. 8171e076a0SAlex Bennée * Once the connection is disabled we call a backhalf to ensure 8271e076a0SAlex Bennée */ 8371e076a0SAlex Bennée typedef void (*vu_async_close_fn)(DeviceState *cb); 8471e076a0SAlex Bennée 8571e076a0SAlex Bennée void vhost_user_async_close(DeviceState *d, 8671e076a0SAlex Bennée CharBackend *chardev, struct vhost_dev *vhost, 8771e076a0SAlex Bennée vu_async_close_fn cb); 8871e076a0SAlex Bennée 894d0cf552STiwei Bie #endif 90