| 1 | /* |
| 2 | This is the exact copy of linux-2.6.32-220.7.1.el6.x86_64/drivers/vhost/vhost.h |
| 3 | except for this comment. |
| 4 | */ |
| 5 | #ifndef _VHOST_H |
| 6 | #define _VHOST_H |
| 7 | |
| 8 | #include <linux/eventfd.h> |
| 9 | #ifdef RHEL_RELEASE_CODE |
| 10 | #include <linux/vhost.h> |
| 11 | #else |
| 12 | #include "./linux/vhost.h" |
| 13 | #endif |
| 14 | #include <linux/mm.h> |
| 15 | #include <linux/mutex.h> |
| 16 | #include <linux/poll.h> |
| 17 | #include <linux/file.h> |
| 18 | #include <linux/skbuff.h> |
| 19 | #include <linux/uio.h> |
| 20 | #include <linux/virtio_config.h> |
| 21 | #include <linux/virtio_ring.h> |
| 22 | #include <asm/atomic.h> |
| 23 | |
| 24 | /* This is for zerocopy, used buffer len is set to 1 when lower device DMA |
| 25 | * done */ |
| 26 | #define VHOST_DMA_DONE_LEN 1 |
| 27 | #define VHOST_DMA_CLEAR_LEN 0 |
| 28 | |
| 29 | struct vhost_device; |
| 30 | |
| 31 | struct vhost_work; |
| 32 | typedef void (*vhost_work_fn_t)(struct vhost_work *work); |
| 33 | |
| 34 | struct vhost_work { |
| 35 | struct list_head node; |
| 36 | vhost_work_fn_t fn; |
| 37 | wait_queue_head_t done; |
| 38 | int flushing; |
| 39 | unsigned queue_seq; |
| 40 | unsigned done_seq; |
| 41 | }; |
| 42 | |
| 43 | /* Poll a file (eventfd or socket) */ |
| 44 | /* Note: there's nothing vhost specific about this structure. */ |
| 45 | struct vhost_poll { |
| 46 | poll_table table; |
| 47 | wait_queue_head_t *wqh; |
| 48 | wait_queue_entry_t wait; |
| 49 | struct vhost_work work; |
| 50 | unsigned long mask; |
| 51 | struct vhost_dev *dev; |
| 52 | }; |
| 53 | |
| 54 | void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn, |
| 55 | unsigned long mask, struct vhost_dev *dev); |
| 56 | void vhost_poll_start(struct vhost_poll *poll, struct file *file); |
| 57 | void vhost_poll_stop(struct vhost_poll *poll); |
| 58 | void vhost_poll_flush(struct vhost_poll *poll); |
| 59 | void vhost_poll_queue(struct vhost_poll *poll); |
| 60 | |
| 61 | struct vhost_log { |
| 62 | u64 addr; |
| 63 | u64 len; |
| 64 | }; |
| 65 | |
| 66 | struct vhost_virtqueue; |
| 67 | |
| 68 | struct vhost_ubuf_ref { |
| 69 | struct kref kref; |
| 70 | wait_queue_head_t wait; |
| 71 | struct vhost_virtqueue *vq; |
| 72 | }; |
| 73 | |
| 74 | struct vhost_ubuf_ref *vhost_ubuf_alloc(struct vhost_virtqueue *, bool zcopy); |
| 75 | void vhost_ubuf_put(struct vhost_ubuf_ref *); |
| 76 | void vhost_ubuf_put_and_wait(struct vhost_ubuf_ref *); |
| 77 | |
| 78 | /* The virtqueue structure describes a queue attached to a device. */ |
| 79 | struct vhost_virtqueue { |
| 80 | struct vhost_dev *dev; |
| 81 | |
| 82 | /* The actual ring of buffers. */ |
| 83 | struct mutex mutex; |
| 84 | unsigned int num; |
| 85 | struct vring_desc __user *desc; |
| 86 | struct vring_avail __user *avail; |
| 87 | struct vring_used __user *used; |
| 88 | struct file *kick; |
| 89 | struct file *call; |
| 90 | struct file *error; |
| 91 | struct eventfd_ctx *call_ctx; |
| 92 | struct eventfd_ctx *error_ctx; |
| 93 | struct eventfd_ctx *log_ctx; |
| 94 | |
| 95 | struct vhost_poll poll; |
| 96 | |
| 97 | /* The routine to call when the Guest pings us, or timeout. */ |
| 98 | vhost_work_fn_t handle_kick; |
| 99 | |
| 100 | /* Last available index we saw. */ |
| 101 | u16 last_avail_idx; |
| 102 | |
| 103 | /* Caches available index value from user. */ |
| 104 | u16 avail_idx; |
| 105 | |
| 106 | /* Last index we used. */ |
| 107 | u16 last_used_idx; |
| 108 | |
| 109 | /* Used flags */ |
| 110 | u16 used_flags; |
| 111 | |
| 112 | /* Last used index value we have signalled on */ |
| 113 | u16 signalled_used; |
| 114 | |
| 115 | /* Last used index value we have signalled on */ |
| 116 | bool signalled_used_valid; |
| 117 | |
| 118 | /* Log writes to used structure. */ |
| 119 | bool log_used; |
| 120 | u64 log_addr; |
| 121 | |
| 122 | struct iovec iov[UIO_MAXIOV]; |
| 123 | /* hdr is used to store the virtio header. |
| 124 | * Since each iovec has >= 1 byte length, we never need more than |
| 125 | * header length entries to store the header. */ |
| 126 | struct iovec hdr[sizeof(struct virtio_net_hdr_mrg_rxbuf)]; |
| 127 | struct iovec *indirect; |
| 128 | size_t vhost_hlen; |
| 129 | size_t sock_hlen; |
| 130 | struct vring_used_elem *heads; |
| 131 | /* We use a kind of RCU to access private pointer. |
| 132 | * All readers access it from worker, which makes it possible to |
| 133 | * flush the vhost_work instead of synchronize_rcu. Therefore readers do |
| 134 | * not need to call rcu_read_lock/rcu_read_unlock: the beginning of |
| 135 | * vhost_work execution acts instead of rcu_read_lock() and the end of |
| 136 | * vhost_work execution acts instead of rcu_read_lock(). |
| 137 | * Writers use virtqueue mutex. */ |
| 138 | void *private_data; |
| 139 | /* Log write descriptors */ |
| 140 | void __user *log_base; |
| 141 | struct vhost_log *log; |
| 142 | /* vhost zerocopy support fields below: */ |
| 143 | /* last used idx for outstanding DMA zerocopy buffers */ |
| 144 | int upend_idx; |
| 145 | /* first used idx for DMA done zerocopy buffers */ |
| 146 | int done_idx; |
| 147 | /* an array of userspace buffers info */ |
| 148 | struct ubuf_info *ubuf_info; |
| 149 | /* Reference counting for outstanding ubufs. |
| 150 | * Protected by vq mutex. Writers must also take device mutex. */ |
| 151 | struct vhost_ubuf_ref *ubufs; |
| 152 | }; |
| 153 | |
| 154 | struct vhost_dev { |
| 155 | /* Readers use RCU to access memory table pointer |
| 156 | * log base pointer and features. |
| 157 | * Writers use mutex below.*/ |
| 158 | struct vhost_memory *memory; |
| 159 | struct mm_struct *mm; |
| 160 | struct mutex mutex; |
| 161 | unsigned acked_features; |
| 162 | struct vhost_virtqueue *vqs; |
| 163 | int nvqs; |
| 164 | struct file *log_file; |
| 165 | struct eventfd_ctx *log_ctx; |
| 166 | spinlock_t work_lock; |
| 167 | struct list_head work_list; |
| 168 | struct task_struct *worker; |
| 169 | }; |
| 170 | |
| 171 | long vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue *vqs, int nvqs); |
| 172 | long vhost_dev_check_owner(struct vhost_dev *); |
| 173 | long vhost_dev_reset_owner(struct vhost_dev *); |
| 174 | void vhost_dev_cleanup(struct vhost_dev *); |
| 175 | long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, unsigned long arg); |
| 176 | int vhost_vq_access_ok(struct vhost_virtqueue *vq); |
| 177 | int vhost_log_access_ok(struct vhost_dev *); |
| 178 | |
| 179 | int vhost_get_vq_desc(struct vhost_dev *, struct vhost_virtqueue *, |
| 180 | struct iovec iov[], unsigned int iov_count, |
| 181 | unsigned int *out_num, unsigned int *in_num, |
| 182 | struct vhost_log *log, unsigned int *log_num); |
| 183 | void vhost_discard_vq_desc(struct vhost_virtqueue *, int n); |
| 184 | |
| 185 | int vhost_init_used(struct vhost_virtqueue *); |
| 186 | int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len); |
| 187 | int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads, |
| 188 | unsigned count); |
| 189 | void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *, |
| 190 | unsigned int id, int len); |
| 191 | void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *, |
| 192 | struct vring_used_elem *heads, unsigned count); |
| 193 | void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *); |
| 194 | void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *); |
| 195 | bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *); |
| 196 | |
| 197 | int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log, |
| 198 | unsigned int log_num, u64 len); |
| 199 | void vhost_zerocopy_callback(void *arg); |
| 200 | int vhost_zerocopy_signal_used(struct vhost_virtqueue *vq); |
| 201 | |
| 202 | #define vq_err(vq, fmt, ...) do { \ |
| 203 | pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \ |
| 204 | if ((vq)->error_ctx) \ |
| 205 | eventfd_signal((vq)->error_ctx, 1);\ |
| 206 | } while (0) |
| 207 | |
| 208 | #ifndef __rcu_dereference_index_check |
| 209 | #define __rcu_dereference_index_check(p, c) \ |
| 210 | ({ \ |
| 211 | typeof(p) _________p1 = READ_ONCE(p); \ |
| 212 | RCU_LOCKDEP_WARN(c, \ |
| 213 | "suspicious rcu_dereference_index_check()" \ |
| 214 | " usage"); \ |
| 215 | smp_read_barrier_depends(); \ |
| 216 | (_________p1); \ |
| 217 | }) |
| 218 | #endif |
| 219 | |
| 220 | enum { |
| 221 | VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) | |
| 222 | (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | |
| 223 | (1ULL << VIRTIO_RING_F_EVENT_IDX) | |
| 224 | (1ULL << VHOST_F_LOG_ALL) | |
| 225 | (1ULL << VHOST_NET_F_VIRTIO_NET_HDR) | |
| 226 | (1ULL << VIRTIO_NET_F_MRG_RXBUF), |
| 227 | }; |
| 228 | |
| 229 | static inline int vhost_has_feature(struct vhost_dev *dev, int bit) |
| 230 | { |
| 231 | #ifdef RHEL_RELEASE_CODE |
| 232 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0)) |
| 233 | unsigned acked_features = rcu_dereference_index_check(dev->acked_features, rcu_read_lock_held()); |
| 234 | #else |
| 235 | unsigned acked_features = rcu_dereference(dev->acked_features); |
| 236 | #endif |
| 237 | #else |
| 238 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(4,2,0)) |
| 239 | unsigned acked_features = rcu_dereference_index_check(dev->acked_features, rcu_read_lock_held()); |
| 240 | #else |
| 241 | unsigned acked_features = __rcu_dereference_index_check(dev->acked_features, rcu_read_lock_held()); |
| 242 | #endif |
| 243 | #endif |
| 244 | return acked_features & (1 << bit); |
| 245 | } |
| 246 | |
| 247 | void vhost_enable_zcopy(int vq); |
| 248 | |
| 249 | #endif |