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 14*3d123a8bSJonah Palmer enum VhostUserProtocolFeature { 15*3d123a8bSJonah Palmer VHOST_USER_PROTOCOL_F_MQ = 0, 16*3d123a8bSJonah Palmer VHOST_USER_PROTOCOL_F_LOG_SHMFD = 1, 17*3d123a8bSJonah Palmer VHOST_USER_PROTOCOL_F_RARP = 2, 18*3d123a8bSJonah Palmer VHOST_USER_PROTOCOL_F_REPLY_ACK = 3, 19*3d123a8bSJonah Palmer VHOST_USER_PROTOCOL_F_NET_MTU = 4, 20*3d123a8bSJonah Palmer VHOST_USER_PROTOCOL_F_BACKEND_REQ = 5, 21*3d123a8bSJonah Palmer VHOST_USER_PROTOCOL_F_CROSS_ENDIAN = 6, 22*3d123a8bSJonah Palmer VHOST_USER_PROTOCOL_F_CRYPTO_SESSION = 7, 23*3d123a8bSJonah Palmer VHOST_USER_PROTOCOL_F_PAGEFAULT = 8, 24*3d123a8bSJonah Palmer VHOST_USER_PROTOCOL_F_CONFIG = 9, 25*3d123a8bSJonah Palmer VHOST_USER_PROTOCOL_F_BACKEND_SEND_FD = 10, 26*3d123a8bSJonah Palmer VHOST_USER_PROTOCOL_F_HOST_NOTIFIER = 11, 27*3d123a8bSJonah Palmer VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD = 12, 28*3d123a8bSJonah Palmer VHOST_USER_PROTOCOL_F_RESET_DEVICE = 13, 29*3d123a8bSJonah Palmer VHOST_USER_PROTOCOL_F_INBAND_NOTIFICATIONS = 14, 30*3d123a8bSJonah Palmer VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS = 15, 31*3d123a8bSJonah Palmer VHOST_USER_PROTOCOL_F_STATUS = 16, 32*3d123a8bSJonah Palmer VHOST_USER_PROTOCOL_F_MAX 33*3d123a8bSJonah Palmer }; 34*3d123a8bSJonah Palmer 35503e3554SAlex Bennée /** 36503e3554SAlex Bennée * VhostUserHostNotifier - notifier information for one queue 37503e3554SAlex Bennée * @rcu: rcu_head for cleanup 38503e3554SAlex Bennée * @mr: memory region of notifier 39503e3554SAlex Bennée * @addr: current mapped address 40503e3554SAlex Bennée * @unmap_addr: address to be un-mapped 41503e3554SAlex Bennée * @idx: virtioqueue index 42503e3554SAlex Bennée * 43503e3554SAlex Bennée * The VhostUserHostNotifier entries are re-used. When an old mapping 44503e3554SAlex Bennée * is to be released it is moved to @unmap_addr and @addr is replaced. 45503e3554SAlex Bennée * Once the RCU process has completed the unmap @unmap_addr is 46503e3554SAlex Bennée * cleared. 47503e3554SAlex Bennée */ 4844866521STiwei Bie typedef struct VhostUserHostNotifier { 490b0af4d6SXueming Li struct rcu_head rcu; 5044866521STiwei Bie MemoryRegion mr; 5144866521STiwei Bie void *addr; 520b0af4d6SXueming Li void *unmap_addr; 53503e3554SAlex Bennée int idx; 5444866521STiwei Bie } VhostUserHostNotifier; 554d0cf552STiwei Bie 56503e3554SAlex Bennée /** 57503e3554SAlex Bennée * VhostUserState - shared state for all vhost-user devices 58503e3554SAlex Bennée * @chr: the character backend for the socket 59503e3554SAlex Bennée * @notifiers: GPtrArray of @VhostUserHostnotifier 60503e3554SAlex Bennée * @memory_slots: 61503e3554SAlex Bennée */ 624d0cf552STiwei Bie typedef struct VhostUserState { 634d0cf552STiwei Bie CharBackend *chr; 64503e3554SAlex Bennée GPtrArray *notifiers; 656b0eff1aSRaphael Norwitz int memory_slots; 6656534930SAlex Bennée bool supports_config; 674d0cf552STiwei Bie } VhostUserState; 684d0cf552STiwei Bie 69503e3554SAlex Bennée /** 70503e3554SAlex Bennée * vhost_user_init() - initialise shared vhost_user state 71503e3554SAlex Bennée * @user: allocated area for storing shared state 72503e3554SAlex Bennée * @chr: the chardev for the vhost socket 73503e3554SAlex Bennée * @errp: error handle 74503e3554SAlex Bennée * 75503e3554SAlex Bennée * User can either directly g_new() space for the state or embed 76503e3554SAlex Bennée * VhostUserState in their larger device structure and just point to 77503e3554SAlex Bennée * it. 78503e3554SAlex Bennée * 79503e3554SAlex Bennée * Return: true on success, false on error while setting errp. 80503e3554SAlex Bennée */ 810b99f224SMarc-André Lureau bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp); 82503e3554SAlex Bennée 83503e3554SAlex Bennée /** 84503e3554SAlex Bennée * vhost_user_cleanup() - cleanup state 85503e3554SAlex Bennée * @user: ptr to use state 86503e3554SAlex Bennée * 87503e3554SAlex Bennée * Cleans up shared state and notifiers, callee is responsible for 88503e3554SAlex Bennée * freeing the @VhostUserState memory itself. 89503e3554SAlex Bennée */ 904d0cf552STiwei Bie void vhost_user_cleanup(VhostUserState *user); 914d0cf552STiwei Bie 9271e076a0SAlex Bennée /** 9371e076a0SAlex Bennée * vhost_user_async_close() - cleanup vhost-user post connection drop 9471e076a0SAlex Bennée * @d: DeviceState for the associated device (passed to callback) 9571e076a0SAlex Bennée * @chardev: the CharBackend associated with the connection 9671e076a0SAlex Bennée * @vhost: the common vhost device 9771e076a0SAlex Bennée * @cb: the user callback function to complete the clean-up 9871e076a0SAlex Bennée * 9971e076a0SAlex Bennée * This function is used to handle the shutdown of a vhost-user 10071e076a0SAlex Bennée * connection to a backend. We handle this centrally to make sure we 10171e076a0SAlex Bennée * do all the steps and handle potential races due to VM shutdowns. 10271e076a0SAlex Bennée * Once the connection is disabled we call a backhalf to ensure 10371e076a0SAlex Bennée */ 10471e076a0SAlex Bennée typedef void (*vu_async_close_fn)(DeviceState *cb); 10571e076a0SAlex Bennée 10671e076a0SAlex Bennée void vhost_user_async_close(DeviceState *d, 10771e076a0SAlex Bennée CharBackend *chardev, struct vhost_dev *vhost, 10871e076a0SAlex Bennée vu_async_close_fn cb); 10971e076a0SAlex Bennée 1104d0cf552STiwei Bie #endif 111