1dd873966SEric Auger /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ 251b24e34SJan Kiszka #ifndef _LINUX_VHOST_H 351b24e34SJan Kiszka #define _LINUX_VHOST_H 451b24e34SJan Kiszka /* Userspace interface for in-kernel virtio accelerators. */ 551b24e34SJan Kiszka 651b24e34SJan Kiszka /* vhost is used to reduce the number of system calls involved in virtio. 751b24e34SJan Kiszka * 851b24e34SJan Kiszka * Existing virtio net code is used in the guest without modification. 951b24e34SJan Kiszka * 1051b24e34SJan Kiszka * This header includes interface used by userspace hypervisor for 1151b24e34SJan Kiszka * device configuration. 1251b24e34SJan Kiszka */ 1351b24e34SJan Kiszka 1451b24e34SJan Kiszka #include <linux/types.h> 1551b24e34SJan Kiszka 1651b24e34SJan Kiszka #include <linux/ioctl.h> 1751b24e34SJan Kiszka #include <linux/virtio_config.h> 18a9fd1654SJens Freimann #include <linux/virtio_ring.h> 1951b24e34SJan Kiszka 2051b24e34SJan Kiszka struct vhost_vring_state { 2151b24e34SJan Kiszka unsigned int index; 2251b24e34SJan Kiszka unsigned int num; 2351b24e34SJan Kiszka }; 2451b24e34SJan Kiszka 2551b24e34SJan Kiszka struct vhost_vring_file { 2651b24e34SJan Kiszka unsigned int index; 2751b24e34SJan Kiszka int fd; /* Pass -1 to unbind from file. */ 2851b24e34SJan Kiszka 2951b24e34SJan Kiszka }; 3051b24e34SJan Kiszka 3151b24e34SJan Kiszka struct vhost_vring_addr { 3251b24e34SJan Kiszka unsigned int index; 3351b24e34SJan Kiszka /* Option flags. */ 3451b24e34SJan Kiszka unsigned int flags; 3551b24e34SJan Kiszka /* Flag values: */ 3651b24e34SJan Kiszka /* Whether log address is valid. If set enables logging. */ 3751b24e34SJan Kiszka #define VHOST_VRING_F_LOG 0 3851b24e34SJan Kiszka 3951b24e34SJan Kiszka /* Start of array of descriptors (virtually contiguous) */ 4051b24e34SJan Kiszka __u64 desc_user_addr; 4151b24e34SJan Kiszka /* Used structure address. Must be 32 bit aligned */ 4251b24e34SJan Kiszka __u64 used_user_addr; 4351b24e34SJan Kiszka /* Available structure address. Must be 16 bit aligned */ 4451b24e34SJan Kiszka __u64 avail_user_addr; 4551b24e34SJan Kiszka /* Logging support. */ 4651b24e34SJan Kiszka /* Log writes to used structure, at offset calculated from specified 4751b24e34SJan Kiszka * address. Address must be 32 bit aligned. */ 4851b24e34SJan Kiszka __u64 log_guest_addr; 4951b24e34SJan Kiszka }; 5051b24e34SJan Kiszka 51dbdfea92SCornelia Huck /* no alignment requirement */ 52dbdfea92SCornelia Huck struct vhost_iotlb_msg { 53dbdfea92SCornelia Huck __u64 iova; 54dbdfea92SCornelia Huck __u64 size; 55dbdfea92SCornelia Huck __u64 uaddr; 56dbdfea92SCornelia Huck #define VHOST_ACCESS_RO 0x1 57dbdfea92SCornelia Huck #define VHOST_ACCESS_WO 0x2 58dbdfea92SCornelia Huck #define VHOST_ACCESS_RW 0x3 59dbdfea92SCornelia Huck __u8 perm; 60dbdfea92SCornelia Huck #define VHOST_IOTLB_MISS 1 61dbdfea92SCornelia Huck #define VHOST_IOTLB_UPDATE 2 62dbdfea92SCornelia Huck #define VHOST_IOTLB_INVALIDATE 3 63dbdfea92SCornelia Huck #define VHOST_IOTLB_ACCESS_FAIL 4 64dbdfea92SCornelia Huck __u8 type; 65dbdfea92SCornelia Huck }; 66dbdfea92SCornelia Huck 67dbdfea92SCornelia Huck #define VHOST_IOTLB_MSG 0x1 68*d36f7de8SCornelia Huck #define VHOST_IOTLB_MSG_V2 0x2 69dbdfea92SCornelia Huck 70dbdfea92SCornelia Huck struct vhost_msg { 71dbdfea92SCornelia Huck int type; 72dbdfea92SCornelia Huck union { 73dbdfea92SCornelia Huck struct vhost_iotlb_msg iotlb; 74dbdfea92SCornelia Huck __u8 padding[64]; 75dbdfea92SCornelia Huck }; 76dbdfea92SCornelia Huck }; 77dbdfea92SCornelia Huck 78*d36f7de8SCornelia Huck struct vhost_msg_v2 { 79*d36f7de8SCornelia Huck __u32 type; 80*d36f7de8SCornelia Huck __u32 reserved; 81*d36f7de8SCornelia Huck union { 82*d36f7de8SCornelia Huck struct vhost_iotlb_msg iotlb; 83*d36f7de8SCornelia Huck __u8 padding[64]; 84*d36f7de8SCornelia Huck }; 85*d36f7de8SCornelia Huck }; 86*d36f7de8SCornelia Huck 8751b24e34SJan Kiszka struct vhost_memory_region { 8851b24e34SJan Kiszka __u64 guest_phys_addr; 8951b24e34SJan Kiszka __u64 memory_size; /* bytes */ 9051b24e34SJan Kiszka __u64 userspace_addr; 9151b24e34SJan Kiszka __u64 flags_padding; /* No flags are currently specified. */ 9251b24e34SJan Kiszka }; 9351b24e34SJan Kiszka 9451b24e34SJan Kiszka /* All region addresses and sizes must be 4K aligned. */ 9551b24e34SJan Kiszka #define VHOST_PAGE_SIZE 0x1000 9651b24e34SJan Kiszka 9751b24e34SJan Kiszka struct vhost_memory { 9851b24e34SJan Kiszka __u32 nregions; 9951b24e34SJan Kiszka __u32 padding; 10051b24e34SJan Kiszka struct vhost_memory_region regions[0]; 10151b24e34SJan Kiszka }; 10251b24e34SJan Kiszka 10351b24e34SJan Kiszka /* ioctls */ 10451b24e34SJan Kiszka 10551b24e34SJan Kiszka #define VHOST_VIRTIO 0xAF 10651b24e34SJan Kiszka 10751b24e34SJan Kiszka /* Features bitmask for forward compatibility. Transport bits are used for 10851b24e34SJan Kiszka * vhost specific features. */ 10951b24e34SJan Kiszka #define VHOST_GET_FEATURES _IOR(VHOST_VIRTIO, 0x00, __u64) 11051b24e34SJan Kiszka #define VHOST_SET_FEATURES _IOW(VHOST_VIRTIO, 0x00, __u64) 11151b24e34SJan Kiszka 11251b24e34SJan Kiszka /* Set current process as the (exclusive) owner of this file descriptor. This 11351b24e34SJan Kiszka * must be called before any other vhost command. Further calls to 11451b24e34SJan Kiszka * VHOST_OWNER_SET fail until VHOST_OWNER_RESET is called. */ 11551b24e34SJan Kiszka #define VHOST_SET_OWNER _IO(VHOST_VIRTIO, 0x01) 11651b24e34SJan Kiszka /* Give up ownership, and reset the device to default values. 11751b24e34SJan Kiszka * Allows subsequent call to VHOST_OWNER_SET to succeed. */ 11860915dc4SYuanhan Liu #define VHOST_RESET_OWNER _IO(VHOST_VIRTIO, 0x02) 11951b24e34SJan Kiszka 12051b24e34SJan Kiszka /* Set up/modify memory layout */ 12151b24e34SJan Kiszka #define VHOST_SET_MEM_TABLE _IOW(VHOST_VIRTIO, 0x03, struct vhost_memory) 12251b24e34SJan Kiszka 12351b24e34SJan Kiszka /* Write logging setup. */ 12451b24e34SJan Kiszka /* Memory writes can optionally be logged by setting bit at an offset 12551b24e34SJan Kiszka * (calculated from the physical address) from specified log base. 12651b24e34SJan Kiszka * The bit is set using an atomic 32 bit operation. */ 12751b24e34SJan Kiszka /* Set base address for logging. */ 12851b24e34SJan Kiszka #define VHOST_SET_LOG_BASE _IOW(VHOST_VIRTIO, 0x04, __u64) 12951b24e34SJan Kiszka /* Specify an eventfd file descriptor to signal on log write. */ 13051b24e34SJan Kiszka #define VHOST_SET_LOG_FD _IOW(VHOST_VIRTIO, 0x07, int) 13151b24e34SJan Kiszka 13251b24e34SJan Kiszka /* Ring setup. */ 13351b24e34SJan Kiszka /* Set number of descriptors in ring. This parameter can not 13451b24e34SJan Kiszka * be modified while ring is running (bound to a device). */ 13551b24e34SJan Kiszka #define VHOST_SET_VRING_NUM _IOW(VHOST_VIRTIO, 0x10, struct vhost_vring_state) 13651b24e34SJan Kiszka /* Set addresses for the ring. */ 13751b24e34SJan Kiszka #define VHOST_SET_VRING_ADDR _IOW(VHOST_VIRTIO, 0x11, struct vhost_vring_addr) 13851b24e34SJan Kiszka /* Base value where queue looks for available descriptors */ 13951b24e34SJan Kiszka #define VHOST_SET_VRING_BASE _IOW(VHOST_VIRTIO, 0x12, struct vhost_vring_state) 14051b24e34SJan Kiszka /* Get accessor: reads index, writes value in num */ 14151b24e34SJan Kiszka #define VHOST_GET_VRING_BASE _IOWR(VHOST_VIRTIO, 0x12, struct vhost_vring_state) 14251b24e34SJan Kiszka 143332f6407SGreg Kurz /* Set the vring byte order in num. Valid values are VHOST_VRING_LITTLE_ENDIAN 144332f6407SGreg Kurz * or VHOST_VRING_BIG_ENDIAN (other values return -EINVAL). 145332f6407SGreg Kurz * The byte order cannot be changed while the device is active: trying to do so 146332f6407SGreg Kurz * returns -EBUSY. 147332f6407SGreg Kurz * This is a legacy only API that is simply ignored when VIRTIO_F_VERSION_1 is 148332f6407SGreg Kurz * set. 149332f6407SGreg Kurz * Not all kernel configurations support this ioctl, but all configurations that 150332f6407SGreg Kurz * support SET also support GET. 151332f6407SGreg Kurz */ 152332f6407SGreg Kurz #define VHOST_VRING_LITTLE_ENDIAN 0 153332f6407SGreg Kurz #define VHOST_VRING_BIG_ENDIAN 1 154332f6407SGreg Kurz #define VHOST_SET_VRING_ENDIAN _IOW(VHOST_VIRTIO, 0x13, struct vhost_vring_state) 155332f6407SGreg Kurz #define VHOST_GET_VRING_ENDIAN _IOW(VHOST_VIRTIO, 0x14, struct vhost_vring_state) 156332f6407SGreg Kurz 15751b24e34SJan Kiszka /* The following ioctls use eventfd file descriptors to signal and poll 15851b24e34SJan Kiszka * for events. */ 15951b24e34SJan Kiszka 16051b24e34SJan Kiszka /* Set eventfd to poll for added buffers */ 16151b24e34SJan Kiszka #define VHOST_SET_VRING_KICK _IOW(VHOST_VIRTIO, 0x20, struct vhost_vring_file) 16251b24e34SJan Kiszka /* Set eventfd to signal when buffers have beed used */ 16351b24e34SJan Kiszka #define VHOST_SET_VRING_CALL _IOW(VHOST_VIRTIO, 0x21, struct vhost_vring_file) 16451b24e34SJan Kiszka /* Set eventfd to signal an error */ 16551b24e34SJan Kiszka #define VHOST_SET_VRING_ERR _IOW(VHOST_VIRTIO, 0x22, struct vhost_vring_file) 166b89485a5SPaolo Bonzini /* Set busy loop timeout (in us) */ 167b89485a5SPaolo Bonzini #define VHOST_SET_VRING_BUSYLOOP_TIMEOUT _IOW(VHOST_VIRTIO, 0x23, \ 168b89485a5SPaolo Bonzini struct vhost_vring_state) 169b89485a5SPaolo Bonzini /* Get busy loop timeout (in us) */ 170b89485a5SPaolo Bonzini #define VHOST_GET_VRING_BUSYLOOP_TIMEOUT _IOW(VHOST_VIRTIO, 0x24, \ 171b89485a5SPaolo Bonzini struct vhost_vring_state) 17251b24e34SJan Kiszka 173*d36f7de8SCornelia Huck /* Set or get vhost backend capability */ 174*d36f7de8SCornelia Huck 175*d36f7de8SCornelia Huck /* Use message type V2 */ 176*d36f7de8SCornelia Huck #define VHOST_BACKEND_F_IOTLB_MSG_V2 0x1 177*d36f7de8SCornelia Huck 178*d36f7de8SCornelia Huck #define VHOST_SET_BACKEND_FEATURES _IOW(VHOST_VIRTIO, 0x25, __u64) 179*d36f7de8SCornelia Huck #define VHOST_GET_BACKEND_FEATURES _IOW(VHOST_VIRTIO, 0x26, __u64) 180*d36f7de8SCornelia Huck 18151b24e34SJan Kiszka /* VHOST_NET specific defines */ 18251b24e34SJan Kiszka 18351b24e34SJan Kiszka /* Attach virtio net ring to a raw socket, or tap device. 18451b24e34SJan Kiszka * The socket must be already bound to an ethernet device, this device will be 18551b24e34SJan Kiszka * used for transmit. Pass fd -1 to unbind from the socket and the transmit 18651b24e34SJan Kiszka * device. This can be used to stop the ring (e.g. for migration). */ 18751b24e34SJan Kiszka #define VHOST_NET_SET_BACKEND _IOW(VHOST_VIRTIO, 0x30, struct vhost_vring_file) 18851b24e34SJan Kiszka 18951b24e34SJan Kiszka /* Feature bits */ 19051b24e34SJan Kiszka /* Log all write descriptors. Can be changed while device is active. */ 19151b24e34SJan Kiszka #define VHOST_F_LOG_ALL 26 19251b24e34SJan Kiszka /* vhost-net should add virtio_net_hdr for RX, and strip for TX packets. */ 19351b24e34SJan Kiszka #define VHOST_NET_F_VIRTIO_NET_HDR 27 19451b24e34SJan Kiszka 195e098b453SAlexey Kardashevskiy /* VHOST_SCSI specific definitions */ 196e098b453SAlexey Kardashevskiy 197e098b453SAlexey Kardashevskiy /* 198e098b453SAlexey Kardashevskiy * Used by QEMU userspace to ensure a consistent vhost-scsi ABI. 199e098b453SAlexey Kardashevskiy * 200e098b453SAlexey Kardashevskiy * ABI Rev 0: July 2012 version starting point for v3.6-rc merge candidate + 201e098b453SAlexey Kardashevskiy * RFC-v2 vhost-scsi userspace. Add GET_ABI_VERSION ioctl usage 202e098b453SAlexey Kardashevskiy * ABI Rev 1: January 2013. Ignore vhost_tpgt filed in struct vhost_scsi_target. 203e098b453SAlexey Kardashevskiy * All the targets under vhost_wwpn can be seen and used by guset. 204e098b453SAlexey Kardashevskiy */ 205e098b453SAlexey Kardashevskiy 206e098b453SAlexey Kardashevskiy #define VHOST_SCSI_ABI_VERSION 1 207e098b453SAlexey Kardashevskiy 208e098b453SAlexey Kardashevskiy struct vhost_scsi_target { 209e098b453SAlexey Kardashevskiy int abi_version; 210e098b453SAlexey Kardashevskiy char vhost_wwpn[224]; /* TRANSPORT_IQN_LEN */ 211e098b453SAlexey Kardashevskiy unsigned short vhost_tpgt; 212e098b453SAlexey Kardashevskiy unsigned short reserved; 213e098b453SAlexey Kardashevskiy }; 214e098b453SAlexey Kardashevskiy 215e098b453SAlexey Kardashevskiy #define VHOST_SCSI_SET_ENDPOINT _IOW(VHOST_VIRTIO, 0x40, struct vhost_scsi_target) 216e098b453SAlexey Kardashevskiy #define VHOST_SCSI_CLEAR_ENDPOINT _IOW(VHOST_VIRTIO, 0x41, struct vhost_scsi_target) 217e098b453SAlexey Kardashevskiy /* Changing this breaks userspace. */ 218e098b453SAlexey Kardashevskiy #define VHOST_SCSI_GET_ABI_VERSION _IOW(VHOST_VIRTIO, 0x42, int) 219e098b453SAlexey Kardashevskiy /* Set and get the events missed flag */ 220e098b453SAlexey Kardashevskiy #define VHOST_SCSI_SET_EVENTS_MISSED _IOW(VHOST_VIRTIO, 0x43, __u32) 221e098b453SAlexey Kardashevskiy #define VHOST_SCSI_GET_EVENTS_MISSED _IOW(VHOST_VIRTIO, 0x44, __u32) 222e098b453SAlexey Kardashevskiy 223dbdfea92SCornelia Huck /* VHOST_VSOCK specific defines */ 224dbdfea92SCornelia Huck 225dbdfea92SCornelia Huck #define VHOST_VSOCK_SET_GUEST_CID _IOW(VHOST_VIRTIO, 0x60, __u64) 226dbdfea92SCornelia Huck #define VHOST_VSOCK_SET_RUNNING _IOW(VHOST_VIRTIO, 0x61, int) 227dbdfea92SCornelia Huck 22851b24e34SJan Kiszka #endif 229