Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2009 Red Hat, Inc. |
| 2 | * Author: Michael S. Tsirkin <mst@redhat.com> |
| 3 | * |
| 4 | * This work is licensed under the terms of the GNU GPL, version 2. |
| 5 | * |
| 6 | * virtio-net server in host kernel. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/compat.h> |
| 10 | #include <linux/eventfd.h> |
| 11 | #include <linux/vhost.h> |
| 12 | #include <linux/virtio_net.h> |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 13 | #include <linux/miscdevice.h> |
| 14 | #include <linux/module.h> |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 15 | #include <linux/moduleparam.h> |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 16 | #include <linux/mutex.h> |
| 17 | #include <linux/workqueue.h> |
| 18 | #include <linux/rcupdate.h> |
| 19 | #include <linux/file.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 21 | |
| 22 | #include <linux/net.h> |
| 23 | #include <linux/if_packet.h> |
| 24 | #include <linux/if_arp.h> |
| 25 | #include <linux/if_tun.h> |
Arnd Bergmann | 501c774 | 2010-02-18 05:46:50 +0000 | [diff] [blame] | 26 | #include <linux/if_macvlan.h> |
Basil Gor | c53cff5e | 2012-05-03 22:55:23 +0000 | [diff] [blame] | 27 | #include <linux/if_vlan.h> |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 28 | |
| 29 | #include <net/sock.h> |
| 30 | |
| 31 | #include "vhost.h" |
| 32 | |
Michael S. Tsirkin | f9611c4 | 2012-12-06 14:56:00 +0200 | [diff] [blame] | 33 | static int experimental_zcopytx = 1; |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 34 | module_param(experimental_zcopytx, int, 0444); |
Michael S. Tsirkin | f9611c4 | 2012-12-06 14:56:00 +0200 | [diff] [blame] | 35 | MODULE_PARM_DESC(experimental_zcopytx, "Enable Zero Copy TX;" |
| 36 | " 1 -Enable; 0 - Disable"); |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 37 | |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 38 | /* Max number of bytes transferred before requeueing the job. |
| 39 | * Using this limit prevents one virtqueue from starving others. */ |
| 40 | #define VHOST_NET_WEIGHT 0x80000 |
| 41 | |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 42 | /* MAX number of TX used buffers for outstanding zerocopy */ |
| 43 | #define VHOST_MAX_PEND 128 |
| 44 | #define VHOST_GOODCOPY_LEN 256 |
| 45 | |
Michael S. Tsirkin | eaae813 | 2012-11-01 09:16:51 +0000 | [diff] [blame] | 46 | /* |
| 47 | * For transmit, used buffer len is unused; we override it to track buffer |
| 48 | * status internally; used for zerocopy tx only. |
| 49 | */ |
| 50 | /* Lower device DMA failed */ |
| 51 | #define VHOST_DMA_FAILED_LEN 3 |
| 52 | /* Lower device DMA done */ |
| 53 | #define VHOST_DMA_DONE_LEN 2 |
| 54 | /* Lower device DMA in progress */ |
| 55 | #define VHOST_DMA_IN_PROGRESS 1 |
| 56 | /* Buffer unused */ |
| 57 | #define VHOST_DMA_CLEAR_LEN 0 |
| 58 | |
| 59 | #define VHOST_DMA_IS_DONE(len) ((len) >= VHOST_DMA_DONE_LEN) |
| 60 | |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 61 | enum { |
| 62 | VHOST_NET_VQ_RX = 0, |
| 63 | VHOST_NET_VQ_TX = 1, |
| 64 | VHOST_NET_VQ_MAX = 2, |
| 65 | }; |
| 66 | |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 67 | struct vhost_ubuf_ref { |
| 68 | struct kref kref; |
| 69 | wait_queue_head_t wait; |
| 70 | struct vhost_virtqueue *vq; |
| 71 | }; |
| 72 | |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 73 | struct vhost_net_virtqueue { |
| 74 | struct vhost_virtqueue vq; |
Michael S. Tsirkin | 81f95a5 | 2013-04-28 15:51:40 +0300 | [diff] [blame^] | 75 | /* hdr is used to store the virtio header. |
| 76 | * Since each iovec has >= 1 byte length, we never need more than |
| 77 | * header length entries to store the header. */ |
| 78 | struct iovec hdr[sizeof(struct virtio_net_hdr_mrg_rxbuf)]; |
| 79 | size_t vhost_hlen; |
| 80 | size_t sock_hlen; |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 81 | /* vhost zerocopy support fields below: */ |
| 82 | /* last used idx for outstanding DMA zerocopy buffers */ |
| 83 | int upend_idx; |
| 84 | /* first used idx for DMA done zerocopy buffers */ |
| 85 | int done_idx; |
| 86 | /* an array of userspace buffers info */ |
| 87 | struct ubuf_info *ubuf_info; |
| 88 | /* Reference counting for outstanding ubufs. |
| 89 | * Protected by vq mutex. Writers must also take device mutex. */ |
| 90 | struct vhost_ubuf_ref *ubufs; |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 91 | }; |
| 92 | |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 93 | struct vhost_net { |
| 94 | struct vhost_dev dev; |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 95 | struct vhost_net_virtqueue vqs[VHOST_NET_VQ_MAX]; |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 96 | struct vhost_poll poll[VHOST_NET_VQ_MAX]; |
Michael S. Tsirkin | eaae813 | 2012-11-01 09:16:51 +0000 | [diff] [blame] | 97 | /* Number of TX recently submitted. |
| 98 | * Protected by tx vq lock. */ |
| 99 | unsigned tx_packets; |
| 100 | /* Number of times zerocopy TX recently failed. |
| 101 | * Protected by tx vq lock. */ |
| 102 | unsigned tx_zcopy_err; |
Michael S. Tsirkin | 1280c27 | 2012-12-04 00:17:14 +0200 | [diff] [blame] | 103 | /* Flush in progress. Protected by tx vq lock. */ |
| 104 | bool tx_flush; |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 107 | static unsigned vhost_zcopy_mask __read_mostly; |
| 108 | |
| 109 | void vhost_enable_zcopy(int vq) |
| 110 | { |
| 111 | vhost_zcopy_mask |= 0x1 << vq; |
| 112 | } |
| 113 | |
| 114 | static void vhost_zerocopy_done_signal(struct kref *kref) |
| 115 | { |
| 116 | struct vhost_ubuf_ref *ubufs = container_of(kref, struct vhost_ubuf_ref, |
| 117 | kref); |
| 118 | wake_up(&ubufs->wait); |
| 119 | } |
| 120 | |
| 121 | struct vhost_ubuf_ref *vhost_ubuf_alloc(struct vhost_virtqueue *vq, |
| 122 | bool zcopy) |
| 123 | { |
| 124 | struct vhost_ubuf_ref *ubufs; |
| 125 | /* No zero copy backend? Nothing to count. */ |
| 126 | if (!zcopy) |
| 127 | return NULL; |
| 128 | ubufs = kmalloc(sizeof(*ubufs), GFP_KERNEL); |
| 129 | if (!ubufs) |
| 130 | return ERR_PTR(-ENOMEM); |
| 131 | kref_init(&ubufs->kref); |
| 132 | init_waitqueue_head(&ubufs->wait); |
| 133 | ubufs->vq = vq; |
| 134 | return ubufs; |
| 135 | } |
| 136 | |
| 137 | void vhost_ubuf_put(struct vhost_ubuf_ref *ubufs) |
| 138 | { |
| 139 | kref_put(&ubufs->kref, vhost_zerocopy_done_signal); |
| 140 | } |
| 141 | |
| 142 | void vhost_ubuf_put_and_wait(struct vhost_ubuf_ref *ubufs) |
| 143 | { |
| 144 | kref_put(&ubufs->kref, vhost_zerocopy_done_signal); |
| 145 | wait_event(ubufs->wait, !atomic_read(&ubufs->kref.refcount)); |
| 146 | kfree(ubufs); |
| 147 | } |
| 148 | |
| 149 | int vhost_net_set_ubuf_info(struct vhost_net *n) |
| 150 | { |
| 151 | bool zcopy; |
| 152 | int i; |
| 153 | |
| 154 | for (i = 0; i < n->dev.nvqs; ++i) { |
| 155 | zcopy = vhost_zcopy_mask & (0x1 << i); |
| 156 | if (!zcopy) |
| 157 | continue; |
| 158 | n->vqs[i].ubuf_info = kmalloc(sizeof(*n->vqs[i].ubuf_info) * |
| 159 | UIO_MAXIOV, GFP_KERNEL); |
| 160 | if (!n->vqs[i].ubuf_info) |
| 161 | goto err; |
| 162 | } |
| 163 | return 0; |
| 164 | |
| 165 | err: |
| 166 | while (i--) { |
| 167 | zcopy = vhost_zcopy_mask & (0x1 << i); |
| 168 | if (!zcopy) |
| 169 | continue; |
| 170 | kfree(n->vqs[i].ubuf_info); |
| 171 | } |
| 172 | return -ENOMEM; |
| 173 | } |
| 174 | |
Michael S. Tsirkin | 81f95a5 | 2013-04-28 15:51:40 +0300 | [diff] [blame^] | 175 | void vhost_net_vq_reset(struct vhost_net *n) |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 176 | { |
| 177 | int i; |
| 178 | |
| 179 | for (i = 0; i < VHOST_NET_VQ_MAX; i++) { |
| 180 | n->vqs[i].done_idx = 0; |
| 181 | n->vqs[i].upend_idx = 0; |
| 182 | n->vqs[i].ubufs = NULL; |
| 183 | kfree(n->vqs[i].ubuf_info); |
| 184 | n->vqs[i].ubuf_info = NULL; |
Michael S. Tsirkin | 81f95a5 | 2013-04-28 15:51:40 +0300 | [diff] [blame^] | 185 | n->vqs[i].vhost_hlen = 0; |
| 186 | n->vqs[i].sock_hlen = 0; |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | } |
| 190 | |
Michael S. Tsirkin | eaae813 | 2012-11-01 09:16:51 +0000 | [diff] [blame] | 191 | static void vhost_net_tx_packet(struct vhost_net *net) |
| 192 | { |
| 193 | ++net->tx_packets; |
| 194 | if (net->tx_packets < 1024) |
| 195 | return; |
| 196 | net->tx_packets = 0; |
| 197 | net->tx_zcopy_err = 0; |
| 198 | } |
| 199 | |
| 200 | static void vhost_net_tx_err(struct vhost_net *net) |
| 201 | { |
| 202 | ++net->tx_zcopy_err; |
| 203 | } |
| 204 | |
| 205 | static bool vhost_net_tx_select_zcopy(struct vhost_net *net) |
| 206 | { |
Michael S. Tsirkin | 1280c27 | 2012-12-04 00:17:14 +0200 | [diff] [blame] | 207 | /* TX flush waits for outstanding DMAs to be done. |
| 208 | * Don't start new DMAs. |
| 209 | */ |
| 210 | return !net->tx_flush && |
| 211 | net->tx_packets / 64 >= net->tx_zcopy_err; |
Michael S. Tsirkin | eaae813 | 2012-11-01 09:16:51 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 214 | static bool vhost_sock_zcopy(struct socket *sock) |
| 215 | { |
| 216 | return unlikely(experimental_zcopytx) && |
| 217 | sock_flag(sock->sk, SOCK_ZEROCOPY); |
| 218 | } |
| 219 | |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 220 | /* Pop first len bytes from iovec. Return number of segments used. */ |
| 221 | static int move_iovec_hdr(struct iovec *from, struct iovec *to, |
| 222 | size_t len, int iov_count) |
| 223 | { |
| 224 | int seg = 0; |
| 225 | size_t size; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 226 | |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 227 | while (len && seg < iov_count) { |
| 228 | size = min(from->iov_len, len); |
| 229 | to->iov_base = from->iov_base; |
| 230 | to->iov_len = size; |
| 231 | from->iov_len -= size; |
| 232 | from->iov_base += size; |
| 233 | len -= size; |
| 234 | ++from; |
| 235 | ++to; |
| 236 | ++seg; |
| 237 | } |
| 238 | return seg; |
| 239 | } |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 240 | /* Copy iovec entries for len bytes from iovec. */ |
| 241 | static void copy_iovec_hdr(const struct iovec *from, struct iovec *to, |
| 242 | size_t len, int iovcount) |
| 243 | { |
| 244 | int seg = 0; |
| 245 | size_t size; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 246 | |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 247 | while (len && seg < iovcount) { |
| 248 | size = min(from->iov_len, len); |
| 249 | to->iov_base = from->iov_base; |
| 250 | to->iov_len = size; |
| 251 | len -= size; |
| 252 | ++from; |
| 253 | ++to; |
| 254 | ++seg; |
| 255 | } |
| 256 | } |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 257 | |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 258 | /* In case of DMA done not in order in lower device driver for some reason. |
| 259 | * upend_idx is used to track end of used idx, done_idx is used to track head |
| 260 | * of used idx. Once lower device DMA done contiguously, we will signal KVM |
| 261 | * guest used idx. |
| 262 | */ |
Michael S. Tsirkin | eaae813 | 2012-11-01 09:16:51 +0000 | [diff] [blame] | 263 | static int vhost_zerocopy_signal_used(struct vhost_net *net, |
| 264 | struct vhost_virtqueue *vq) |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 265 | { |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 266 | struct vhost_net_virtqueue *nvq = |
| 267 | container_of(vq, struct vhost_net_virtqueue, vq); |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 268 | int i; |
| 269 | int j = 0; |
| 270 | |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 271 | for (i = nvq->done_idx; i != nvq->upend_idx; i = (i + 1) % UIO_MAXIOV) { |
Michael S. Tsirkin | eaae813 | 2012-11-01 09:16:51 +0000 | [diff] [blame] | 272 | if (vq->heads[i].len == VHOST_DMA_FAILED_LEN) |
| 273 | vhost_net_tx_err(net); |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 274 | if (VHOST_DMA_IS_DONE(vq->heads[i].len)) { |
| 275 | vq->heads[i].len = VHOST_DMA_CLEAR_LEN; |
| 276 | vhost_add_used_and_signal(vq->dev, vq, |
| 277 | vq->heads[i].id, 0); |
| 278 | ++j; |
| 279 | } else |
| 280 | break; |
| 281 | } |
| 282 | if (j) |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 283 | nvq->done_idx = i; |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 284 | return j; |
| 285 | } |
| 286 | |
Michael S. Tsirkin | eaae813 | 2012-11-01 09:16:51 +0000 | [diff] [blame] | 287 | static void vhost_zerocopy_callback(struct ubuf_info *ubuf, bool success) |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 288 | { |
| 289 | struct vhost_ubuf_ref *ubufs = ubuf->ctx; |
| 290 | struct vhost_virtqueue *vq = ubufs->vq; |
Michael S. Tsirkin | 24eb21a | 2012-11-01 09:16:55 +0000 | [diff] [blame] | 291 | int cnt = atomic_read(&ubufs->kref.refcount); |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 292 | |
Michael S. Tsirkin | 24eb21a | 2012-11-01 09:16:55 +0000 | [diff] [blame] | 293 | /* |
| 294 | * Trigger polling thread if guest stopped submitting new buffers: |
| 295 | * in this case, the refcount after decrement will eventually reach 1 |
| 296 | * so here it is 2. |
| 297 | * We also trigger polling periodically after each 16 packets |
| 298 | * (the value 16 here is more or less arbitrary, it's tuned to trigger |
| 299 | * less than 10% of times). |
| 300 | */ |
| 301 | if (cnt <= 2 || !(cnt % 16)) |
| 302 | vhost_poll_queue(&vq->poll); |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 303 | /* set len to mark this desc buffers done DMA */ |
Michael S. Tsirkin | eaae813 | 2012-11-01 09:16:51 +0000 | [diff] [blame] | 304 | vq->heads[ubuf->desc].len = success ? |
| 305 | VHOST_DMA_DONE_LEN : VHOST_DMA_FAILED_LEN; |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 306 | vhost_ubuf_put(ubufs); |
| 307 | } |
| 308 | |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 309 | /* Expects to be always run from workqueue - which acts as |
| 310 | * read-size critical section for our kind of RCU. */ |
| 311 | static void handle_tx(struct vhost_net *net) |
| 312 | { |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 313 | struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX]; |
Michael S. Tsirkin | 81f95a5 | 2013-04-28 15:51:40 +0300 | [diff] [blame^] | 314 | struct vhost_virtqueue *vq = &nvq->vq; |
Michael S. Tsirkin | d5675bd | 2010-06-24 16:59:59 +0300 | [diff] [blame] | 315 | unsigned out, in, s; |
| 316 | int head; |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 317 | struct msghdr msg = { |
| 318 | .msg_name = NULL, |
| 319 | .msg_namelen = 0, |
| 320 | .msg_control = NULL, |
| 321 | .msg_controllen = 0, |
| 322 | .msg_iov = vq->iov, |
| 323 | .msg_flags = MSG_DONTWAIT, |
| 324 | }; |
| 325 | size_t len, total_len = 0; |
Jason Wang | 70181d51 | 2013-04-10 20:50:48 +0000 | [diff] [blame] | 326 | int err; |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 327 | size_t hdr_size; |
Arnd Bergmann | 28457ee | 2010-03-09 19:24:45 +0100 | [diff] [blame] | 328 | struct socket *sock; |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 329 | struct vhost_ubuf_ref *uninitialized_var(ubufs); |
Michael S. Tsirkin | cedb9bd | 2012-12-06 17:00:18 +0200 | [diff] [blame] | 330 | bool zcopy, zcopy_used; |
Arnd Bergmann | 28457ee | 2010-03-09 19:24:45 +0100 | [diff] [blame] | 331 | |
Michael S. Tsirkin | 5e18247 | 2011-01-18 13:04:43 +0200 | [diff] [blame] | 332 | /* TODO: check that we are running from vhost_worker? */ |
Michael S. Tsirkin | 11cd1a8 | 2010-11-14 17:31:52 +0200 | [diff] [blame] | 333 | sock = rcu_dereference_check(vq->private_data, 1); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 334 | if (!sock) |
| 335 | return; |
| 336 | |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 337 | mutex_lock(&vq->mutex); |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 338 | vhost_disable_notify(&net->dev, vq); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 339 | |
Michael S. Tsirkin | 81f95a5 | 2013-04-28 15:51:40 +0300 | [diff] [blame^] | 340 | hdr_size = nvq->vhost_hlen; |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 341 | zcopy = nvq->ubufs; |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 342 | |
| 343 | for (;;) { |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 344 | /* Release DMAs done buffers first */ |
| 345 | if (zcopy) |
Michael S. Tsirkin | eaae813 | 2012-11-01 09:16:51 +0000 | [diff] [blame] | 346 | vhost_zerocopy_signal_used(net, vq); |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 347 | |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 348 | head = vhost_get_vq_desc(&net->dev, vq, vq->iov, |
| 349 | ARRAY_SIZE(vq->iov), |
| 350 | &out, &in, |
| 351 | NULL, NULL); |
Michael S. Tsirkin | d5675bd | 2010-06-24 16:59:59 +0300 | [diff] [blame] | 352 | /* On error, stop handling until the next kick. */ |
Michael S. Tsirkin | 7b3384f | 2010-07-01 18:40:12 +0300 | [diff] [blame] | 353 | if (unlikely(head < 0)) |
Michael S. Tsirkin | d5675bd | 2010-06-24 16:59:59 +0300 | [diff] [blame] | 354 | break; |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 355 | /* Nothing new? Wait for eventfd to tell us they refilled. */ |
| 356 | if (head == vq->num) { |
Shirley Ma | 9e38082 | 2011-07-20 10:23:12 -0700 | [diff] [blame] | 357 | int num_pends; |
| 358 | |
Shirley Ma | 9e38082 | 2011-07-20 10:23:12 -0700 | [diff] [blame] | 359 | /* If more outstanding DMAs, queue the work. |
| 360 | * Handle upend_idx wrap around |
| 361 | */ |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 362 | num_pends = likely(nvq->upend_idx >= nvq->done_idx) ? |
| 363 | (nvq->upend_idx - nvq->done_idx) : |
| 364 | (nvq->upend_idx + UIO_MAXIOV - |
| 365 | nvq->done_idx); |
Jason Wang | 70181d51 | 2013-04-10 20:50:48 +0000 | [diff] [blame] | 366 | if (unlikely(num_pends > VHOST_MAX_PEND)) |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 367 | break; |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 368 | if (unlikely(vhost_enable_notify(&net->dev, vq))) { |
| 369 | vhost_disable_notify(&net->dev, vq); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 370 | continue; |
| 371 | } |
| 372 | break; |
| 373 | } |
| 374 | if (in) { |
| 375 | vq_err(vq, "Unexpected descriptor format for TX: " |
| 376 | "out %d, int %d\n", out, in); |
| 377 | break; |
| 378 | } |
| 379 | /* Skip header. TODO: support TSO. */ |
Michael S. Tsirkin | 81f95a5 | 2013-04-28 15:51:40 +0300 | [diff] [blame^] | 380 | s = move_iovec_hdr(vq->iov, nvq->hdr, hdr_size, out); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 381 | msg.msg_iovlen = out; |
| 382 | len = iov_length(vq->iov, out); |
| 383 | /* Sanity check */ |
| 384 | if (!len) { |
| 385 | vq_err(vq, "Unexpected header len for TX: " |
| 386 | "%zd expected %zd\n", |
Michael S. Tsirkin | 81f95a5 | 2013-04-28 15:51:40 +0300 | [diff] [blame^] | 387 | iov_length(nvq->hdr, s), hdr_size); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 388 | break; |
| 389 | } |
Michael S. Tsirkin | cedb9bd | 2012-12-06 17:00:18 +0200 | [diff] [blame] | 390 | zcopy_used = zcopy && (len >= VHOST_GOODCOPY_LEN || |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 391 | nvq->upend_idx != nvq->done_idx); |
Michael S. Tsirkin | cedb9bd | 2012-12-06 17:00:18 +0200 | [diff] [blame] | 392 | |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 393 | /* use msg_control to pass vhost zerocopy ubuf info to skb */ |
Michael S. Tsirkin | cedb9bd | 2012-12-06 17:00:18 +0200 | [diff] [blame] | 394 | if (zcopy_used) { |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 395 | vq->heads[nvq->upend_idx].id = head; |
Michael S. Tsirkin | eaae813 | 2012-11-01 09:16:51 +0000 | [diff] [blame] | 396 | if (!vhost_net_tx_select_zcopy(net) || |
| 397 | len < VHOST_GOODCOPY_LEN) { |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 398 | /* copy don't need to wait for DMA done */ |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 399 | vq->heads[nvq->upend_idx].len = |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 400 | VHOST_DMA_DONE_LEN; |
| 401 | msg.msg_control = NULL; |
| 402 | msg.msg_controllen = 0; |
| 403 | ubufs = NULL; |
| 404 | } else { |
Michael S. Tsirkin | 46aa92d | 2013-03-17 02:46:09 +0000 | [diff] [blame] | 405 | struct ubuf_info *ubuf; |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 406 | ubuf = nvq->ubuf_info + nvq->upend_idx; |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 407 | |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 408 | vq->heads[nvq->upend_idx].len = |
Michael S. Tsirkin | 70e4cb9 | 2012-11-01 09:16:37 +0000 | [diff] [blame] | 409 | VHOST_DMA_IN_PROGRESS; |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 410 | ubuf->callback = vhost_zerocopy_callback; |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 411 | ubuf->ctx = nvq->ubufs; |
| 412 | ubuf->desc = nvq->upend_idx; |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 413 | msg.msg_control = ubuf; |
| 414 | msg.msg_controllen = sizeof(ubuf); |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 415 | ubufs = nvq->ubufs; |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 416 | kref_get(&ubufs->kref); |
| 417 | } |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 418 | nvq->upend_idx = (nvq->upend_idx + 1) % UIO_MAXIOV; |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 419 | } |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 420 | /* TODO: Check specific error and bomb out unless ENOBUFS? */ |
| 421 | err = sock->ops->sendmsg(NULL, sock, &msg, len); |
| 422 | if (unlikely(err < 0)) { |
Michael S. Tsirkin | cedb9bd | 2012-12-06 17:00:18 +0200 | [diff] [blame] | 423 | if (zcopy_used) { |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 424 | if (ubufs) |
| 425 | vhost_ubuf_put(ubufs); |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 426 | nvq->upend_idx = ((unsigned)nvq->upend_idx - 1) |
| 427 | % UIO_MAXIOV; |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 428 | } |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 429 | vhost_discard_vq_desc(vq, 1); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 430 | break; |
| 431 | } |
| 432 | if (err != len) |
Michael S. Tsirkin | 95c0ec6 | 2010-06-24 17:10:25 +0300 | [diff] [blame] | 433 | pr_debug("Truncated TX packet: " |
| 434 | " len %d != %zd\n", err, len); |
Michael S. Tsirkin | cedb9bd | 2012-12-06 17:00:18 +0200 | [diff] [blame] | 435 | if (!zcopy_used) |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 436 | vhost_add_used_and_signal(&net->dev, vq, head, 0); |
Jason Wang | c8fb217 | 2012-05-02 11:42:41 +0800 | [diff] [blame] | 437 | else |
Michael S. Tsirkin | eaae813 | 2012-11-01 09:16:51 +0000 | [diff] [blame] | 438 | vhost_zerocopy_signal_used(net, vq); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 439 | total_len += len; |
Michael S. Tsirkin | eaae813 | 2012-11-01 09:16:51 +0000 | [diff] [blame] | 440 | vhost_net_tx_packet(net); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 441 | if (unlikely(total_len >= VHOST_NET_WEIGHT)) { |
| 442 | vhost_poll_queue(&vq->poll); |
| 443 | break; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | mutex_unlock(&vq->mutex); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 448 | } |
| 449 | |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 450 | static int peek_head_len(struct sock *sk) |
| 451 | { |
| 452 | struct sk_buff *head; |
| 453 | int len = 0; |
Jason Wang | 783e398 | 2011-01-17 16:11:17 +0800 | [diff] [blame] | 454 | unsigned long flags; |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 455 | |
Jason Wang | 783e398 | 2011-01-17 16:11:17 +0800 | [diff] [blame] | 456 | spin_lock_irqsave(&sk->sk_receive_queue.lock, flags); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 457 | head = skb_peek(&sk->sk_receive_queue); |
Basil Gor | c53cff5e | 2012-05-03 22:55:23 +0000 | [diff] [blame] | 458 | if (likely(head)) { |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 459 | len = head->len; |
Basil Gor | c53cff5e | 2012-05-03 22:55:23 +0000 | [diff] [blame] | 460 | if (vlan_tx_tag_present(head)) |
| 461 | len += VLAN_HLEN; |
| 462 | } |
| 463 | |
Jason Wang | 783e398 | 2011-01-17 16:11:17 +0800 | [diff] [blame] | 464 | spin_unlock_irqrestore(&sk->sk_receive_queue.lock, flags); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 465 | return len; |
| 466 | } |
| 467 | |
| 468 | /* This is a multi-buffer version of vhost_get_desc, that works if |
| 469 | * vq has read descriptors only. |
| 470 | * @vq - the relevant virtqueue |
| 471 | * @datalen - data length we'll be reading |
| 472 | * @iovcount - returned count of io vectors we fill |
| 473 | * @log - vhost log |
| 474 | * @log_num - log offset |
Jason Wang | 9424936 | 2011-01-17 16:11:08 +0800 | [diff] [blame] | 475 | * @quota - headcount quota, 1 for big buffer |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 476 | * returns number of buffer heads allocated, negative on error |
| 477 | */ |
| 478 | static int get_rx_bufs(struct vhost_virtqueue *vq, |
| 479 | struct vring_used_elem *heads, |
| 480 | int datalen, |
| 481 | unsigned *iovcount, |
| 482 | struct vhost_log *log, |
Jason Wang | 9424936 | 2011-01-17 16:11:08 +0800 | [diff] [blame] | 483 | unsigned *log_num, |
| 484 | unsigned int quota) |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 485 | { |
| 486 | unsigned int out, in; |
| 487 | int seg = 0; |
| 488 | int headcount = 0; |
| 489 | unsigned d; |
| 490 | int r, nlogs = 0; |
| 491 | |
Jason Wang | 9424936 | 2011-01-17 16:11:08 +0800 | [diff] [blame] | 492 | while (datalen > 0 && headcount < quota) { |
Jason Wang | e0e9b40 | 2010-09-14 23:53:05 +0800 | [diff] [blame] | 493 | if (unlikely(seg >= UIO_MAXIOV)) { |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 494 | r = -ENOBUFS; |
| 495 | goto err; |
| 496 | } |
| 497 | d = vhost_get_vq_desc(vq->dev, vq, vq->iov + seg, |
| 498 | ARRAY_SIZE(vq->iov) - seg, &out, |
| 499 | &in, log, log_num); |
| 500 | if (d == vq->num) { |
| 501 | r = 0; |
| 502 | goto err; |
| 503 | } |
| 504 | if (unlikely(out || in <= 0)) { |
| 505 | vq_err(vq, "unexpected descriptor format for RX: " |
| 506 | "out %d, in %d\n", out, in); |
| 507 | r = -EINVAL; |
| 508 | goto err; |
| 509 | } |
| 510 | if (unlikely(log)) { |
| 511 | nlogs += *log_num; |
| 512 | log += *log_num; |
| 513 | } |
| 514 | heads[headcount].id = d; |
| 515 | heads[headcount].len = iov_length(vq->iov + seg, in); |
| 516 | datalen -= heads[headcount].len; |
| 517 | ++headcount; |
| 518 | seg += in; |
| 519 | } |
| 520 | heads[headcount - 1].len += datalen; |
| 521 | *iovcount = seg; |
| 522 | if (unlikely(log)) |
| 523 | *log_num = nlogs; |
| 524 | return headcount; |
| 525 | err: |
| 526 | vhost_discard_vq_desc(vq, headcount); |
| 527 | return r; |
| 528 | } |
| 529 | |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 530 | /* Expects to be always run from workqueue - which acts as |
| 531 | * read-size critical section for our kind of RCU. */ |
Jason Wang | 9424936 | 2011-01-17 16:11:08 +0800 | [diff] [blame] | 532 | static void handle_rx(struct vhost_net *net) |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 533 | { |
Michael S. Tsirkin | 81f95a5 | 2013-04-28 15:51:40 +0300 | [diff] [blame^] | 534 | struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_RX]; |
| 535 | struct vhost_virtqueue *vq = &nvq->vq; |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 536 | unsigned uninitialized_var(in), log; |
| 537 | struct vhost_log *vq_log; |
| 538 | struct msghdr msg = { |
| 539 | .msg_name = NULL, |
| 540 | .msg_namelen = 0, |
| 541 | .msg_control = NULL, /* FIXME: get and handle RX aux data. */ |
| 542 | .msg_controllen = 0, |
| 543 | .msg_iov = vq->iov, |
| 544 | .msg_flags = MSG_DONTWAIT, |
| 545 | }; |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 546 | struct virtio_net_hdr_mrg_rxbuf hdr = { |
| 547 | .hdr.flags = 0, |
| 548 | .hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE |
| 549 | }; |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 550 | size_t total_len = 0; |
Michael S. Tsirkin | 910a578 | 2012-10-24 20:37:51 +0200 | [diff] [blame] | 551 | int err, mergeable; |
| 552 | s16 headcount; |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 553 | size_t vhost_hlen, sock_hlen; |
| 554 | size_t vhost_len, sock_len; |
Michael S. Tsirkin | 5e18247 | 2011-01-18 13:04:43 +0200 | [diff] [blame] | 555 | /* TODO: check that we are running from vhost_worker? */ |
| 556 | struct socket *sock = rcu_dereference_check(vq->private_data, 1); |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 557 | |
Michael S. Tsirkin | de4d768 | 2011-03-13 23:00:52 +0200 | [diff] [blame] | 558 | if (!sock) |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 559 | return; |
| 560 | |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 561 | mutex_lock(&vq->mutex); |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 562 | vhost_disable_notify(&net->dev, vq); |
Michael S. Tsirkin | 81f95a5 | 2013-04-28 15:51:40 +0300 | [diff] [blame^] | 563 | vhost_hlen = nvq->vhost_hlen; |
| 564 | sock_hlen = nvq->sock_hlen; |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 565 | |
| 566 | vq_log = unlikely(vhost_has_feature(&net->dev, VHOST_F_LOG_ALL)) ? |
| 567 | vq->log : NULL; |
Jason Wang | cfbdab9 | 2011-01-17 16:10:59 +0800 | [diff] [blame] | 568 | mergeable = vhost_has_feature(&net->dev, VIRTIO_NET_F_MRG_RXBUF); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 569 | |
| 570 | while ((sock_len = peek_head_len(sock->sk))) { |
| 571 | sock_len += sock_hlen; |
| 572 | vhost_len = sock_len + vhost_hlen; |
| 573 | headcount = get_rx_bufs(vq, vq->heads, vhost_len, |
Jason Wang | 9424936 | 2011-01-17 16:11:08 +0800 | [diff] [blame] | 574 | &in, vq_log, &log, |
| 575 | likely(mergeable) ? UIO_MAXIOV : 1); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 576 | /* On error, stop handling until the next kick. */ |
| 577 | if (unlikely(headcount < 0)) |
| 578 | break; |
| 579 | /* OK, now we need to know about added descriptors. */ |
| 580 | if (!headcount) { |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 581 | if (unlikely(vhost_enable_notify(&net->dev, vq))) { |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 582 | /* They have slipped one in as we were |
| 583 | * doing that: check again. */ |
Michael S. Tsirkin | 8ea8cf8 | 2011-05-20 02:10:54 +0300 | [diff] [blame] | 584 | vhost_disable_notify(&net->dev, vq); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 585 | continue; |
| 586 | } |
| 587 | /* Nothing new? Wait for eventfd to tell us |
| 588 | * they refilled. */ |
| 589 | break; |
| 590 | } |
| 591 | /* We don't need to be notified again. */ |
| 592 | if (unlikely((vhost_hlen))) |
| 593 | /* Skip header. TODO: support TSO. */ |
Michael S. Tsirkin | 81f95a5 | 2013-04-28 15:51:40 +0300 | [diff] [blame^] | 594 | move_iovec_hdr(vq->iov, nvq->hdr, vhost_hlen, in); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 595 | else |
| 596 | /* Copy the header for use in VIRTIO_NET_F_MRG_RXBUF: |
Jason Wang | a290aec | 2010-11-29 13:48:40 +0800 | [diff] [blame] | 597 | * needed because recvmsg can modify msg_iov. */ |
Michael S. Tsirkin | 81f95a5 | 2013-04-28 15:51:40 +0300 | [diff] [blame^] | 598 | copy_iovec_hdr(vq->iov, nvq->hdr, sock_hlen, in); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 599 | msg.msg_iovlen = in; |
| 600 | err = sock->ops->recvmsg(NULL, sock, &msg, |
| 601 | sock_len, MSG_DONTWAIT | MSG_TRUNC); |
| 602 | /* Userspace might have consumed the packet meanwhile: |
| 603 | * it's not supposed to do this usually, but might be hard |
| 604 | * to prevent. Discard data we got (if any) and keep going. */ |
| 605 | if (unlikely(err != sock_len)) { |
| 606 | pr_debug("Discarded rx packet: " |
| 607 | " len %d, expected %zd\n", err, sock_len); |
| 608 | vhost_discard_vq_desc(vq, headcount); |
| 609 | continue; |
| 610 | } |
| 611 | if (unlikely(vhost_hlen) && |
Michael S. Tsirkin | 81f95a5 | 2013-04-28 15:51:40 +0300 | [diff] [blame^] | 612 | memcpy_toiovecend(nvq->hdr, (unsigned char *)&hdr, 0, |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 613 | vhost_hlen)) { |
| 614 | vq_err(vq, "Unable to write vnet_hdr at addr %p\n", |
| 615 | vq->iov->iov_base); |
| 616 | break; |
| 617 | } |
| 618 | /* TODO: Should check and handle checksum. */ |
Jason Wang | cfbdab9 | 2011-01-17 16:10:59 +0800 | [diff] [blame] | 619 | if (likely(mergeable) && |
Michael S. Tsirkin | 81f95a5 | 2013-04-28 15:51:40 +0300 | [diff] [blame^] | 620 | memcpy_toiovecend(nvq->hdr, (unsigned char *)&headcount, |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 621 | offsetof(typeof(hdr), num_buffers), |
| 622 | sizeof hdr.num_buffers)) { |
| 623 | vq_err(vq, "Failed num_buffers write"); |
| 624 | vhost_discard_vq_desc(vq, headcount); |
| 625 | break; |
| 626 | } |
| 627 | vhost_add_used_and_signal_n(&net->dev, vq, vq->heads, |
| 628 | headcount); |
| 629 | if (unlikely(vq_log)) |
| 630 | vhost_log_write(vq, vq_log, log, vhost_len); |
| 631 | total_len += vhost_len; |
| 632 | if (unlikely(total_len >= VHOST_NET_WEIGHT)) { |
| 633 | vhost_poll_queue(&vq->poll); |
| 634 | break; |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | mutex_unlock(&vq->mutex); |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 639 | } |
| 640 | |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 641 | static void handle_tx_kick(struct vhost_work *work) |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 642 | { |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 643 | struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue, |
| 644 | poll.work); |
| 645 | struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev); |
| 646 | |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 647 | handle_tx(net); |
| 648 | } |
| 649 | |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 650 | static void handle_rx_kick(struct vhost_work *work) |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 651 | { |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 652 | struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue, |
| 653 | poll.work); |
| 654 | struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev); |
| 655 | |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 656 | handle_rx(net); |
| 657 | } |
| 658 | |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 659 | static void handle_tx_net(struct vhost_work *work) |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 660 | { |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 661 | struct vhost_net *net = container_of(work, struct vhost_net, |
| 662 | poll[VHOST_NET_VQ_TX].work); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 663 | handle_tx(net); |
| 664 | } |
| 665 | |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 666 | static void handle_rx_net(struct vhost_work *work) |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 667 | { |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 668 | struct vhost_net *net = container_of(work, struct vhost_net, |
| 669 | poll[VHOST_NET_VQ_RX].work); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 670 | handle_rx(net); |
| 671 | } |
| 672 | |
| 673 | static int vhost_net_open(struct inode *inode, struct file *f) |
| 674 | { |
| 675 | struct vhost_net *n = kmalloc(sizeof *n, GFP_KERNEL); |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 676 | struct vhost_dev *dev; |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 677 | struct vhost_virtqueue **vqs; |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 678 | int r, i; |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 679 | |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 680 | if (!n) |
| 681 | return -ENOMEM; |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 682 | vqs = kmalloc(VHOST_NET_VQ_MAX * sizeof(*vqs), GFP_KERNEL); |
| 683 | if (!vqs) { |
| 684 | kfree(n); |
| 685 | return -ENOMEM; |
| 686 | } |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 687 | |
| 688 | dev = &n->dev; |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 689 | vqs[VHOST_NET_VQ_TX] = &n->vqs[VHOST_NET_VQ_TX].vq; |
| 690 | vqs[VHOST_NET_VQ_RX] = &n->vqs[VHOST_NET_VQ_RX].vq; |
| 691 | n->vqs[VHOST_NET_VQ_TX].vq.handle_kick = handle_tx_kick; |
| 692 | n->vqs[VHOST_NET_VQ_RX].vq.handle_kick = handle_rx_kick; |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 693 | for (i = 0; i < VHOST_NET_VQ_MAX; i++) { |
| 694 | n->vqs[i].ubufs = NULL; |
| 695 | n->vqs[i].ubuf_info = NULL; |
| 696 | n->vqs[i].upend_idx = 0; |
| 697 | n->vqs[i].done_idx = 0; |
Michael S. Tsirkin | 81f95a5 | 2013-04-28 15:51:40 +0300 | [diff] [blame^] | 698 | n->vqs[i].vhost_hlen = 0; |
| 699 | n->vqs[i].sock_hlen = 0; |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 700 | } |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 701 | r = vhost_dev_init(dev, vqs, VHOST_NET_VQ_MAX); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 702 | if (r < 0) { |
| 703 | kfree(n); |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 704 | kfree(vqs); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 705 | return r; |
| 706 | } |
| 707 | |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 708 | vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, POLLOUT, dev); |
| 709 | vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, POLLIN, dev); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 710 | |
| 711 | f->private_data = n; |
| 712 | |
| 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | static void vhost_net_disable_vq(struct vhost_net *n, |
| 717 | struct vhost_virtqueue *vq) |
| 718 | { |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 719 | struct vhost_net_virtqueue *nvq = |
| 720 | container_of(vq, struct vhost_net_virtqueue, vq); |
| 721 | struct vhost_poll *poll = n->poll + (nvq - n->vqs); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 722 | if (!vq->private_data) |
| 723 | return; |
Jason Wang | 70181d51 | 2013-04-10 20:50:48 +0000 | [diff] [blame] | 724 | vhost_poll_stop(poll); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Jason Wang | 2b8b328 | 2013-01-28 01:05:18 +0000 | [diff] [blame] | 727 | static int vhost_net_enable_vq(struct vhost_net *n, |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 728 | struct vhost_virtqueue *vq) |
| 729 | { |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 730 | struct vhost_net_virtqueue *nvq = |
| 731 | container_of(vq, struct vhost_net_virtqueue, vq); |
| 732 | struct vhost_poll *poll = n->poll + (nvq - n->vqs); |
Arnd Bergmann | 28457ee | 2010-03-09 19:24:45 +0100 | [diff] [blame] | 733 | struct socket *sock; |
| 734 | |
| 735 | sock = rcu_dereference_protected(vq->private_data, |
| 736 | lockdep_is_held(&vq->mutex)); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 737 | if (!sock) |
Jason Wang | 2b8b328 | 2013-01-28 01:05:18 +0000 | [diff] [blame] | 738 | return 0; |
Jason Wang | 2b8b328 | 2013-01-28 01:05:18 +0000 | [diff] [blame] | 739 | |
Jason Wang | 70181d51 | 2013-04-10 20:50:48 +0000 | [diff] [blame] | 740 | return vhost_poll_start(poll, sock->file); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | static struct socket *vhost_net_stop_vq(struct vhost_net *n, |
| 744 | struct vhost_virtqueue *vq) |
| 745 | { |
| 746 | struct socket *sock; |
| 747 | |
| 748 | mutex_lock(&vq->mutex); |
Arnd Bergmann | 28457ee | 2010-03-09 19:24:45 +0100 | [diff] [blame] | 749 | sock = rcu_dereference_protected(vq->private_data, |
| 750 | lockdep_is_held(&vq->mutex)); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 751 | vhost_net_disable_vq(n, vq); |
| 752 | rcu_assign_pointer(vq->private_data, NULL); |
| 753 | mutex_unlock(&vq->mutex); |
| 754 | return sock; |
| 755 | } |
| 756 | |
| 757 | static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock, |
| 758 | struct socket **rx_sock) |
| 759 | { |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 760 | *tx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_TX].vq); |
| 761 | *rx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_RX].vq); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | static void vhost_net_flush_vq(struct vhost_net *n, int index) |
| 765 | { |
| 766 | vhost_poll_flush(n->poll + index); |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 767 | vhost_poll_flush(&n->vqs[index].vq.poll); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | static void vhost_net_flush(struct vhost_net *n) |
| 771 | { |
| 772 | vhost_net_flush_vq(n, VHOST_NET_VQ_TX); |
| 773 | vhost_net_flush_vq(n, VHOST_NET_VQ_RX); |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 774 | if (n->vqs[VHOST_NET_VQ_TX].ubufs) { |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 775 | mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex); |
Michael S. Tsirkin | 1280c27 | 2012-12-04 00:17:14 +0200 | [diff] [blame] | 776 | n->tx_flush = true; |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 777 | mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex); |
Michael S. Tsirkin | 1280c27 | 2012-12-04 00:17:14 +0200 | [diff] [blame] | 778 | /* Wait for all lower device DMAs done. */ |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 779 | vhost_ubuf_put_and_wait(n->vqs[VHOST_NET_VQ_TX].ubufs); |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 780 | mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex); |
Michael S. Tsirkin | 1280c27 | 2012-12-04 00:17:14 +0200 | [diff] [blame] | 781 | n->tx_flush = false; |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 782 | kref_init(&n->vqs[VHOST_NET_VQ_TX].ubufs->kref); |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 783 | mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex); |
Michael S. Tsirkin | 1280c27 | 2012-12-04 00:17:14 +0200 | [diff] [blame] | 784 | } |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | static int vhost_net_release(struct inode *inode, struct file *f) |
| 788 | { |
| 789 | struct vhost_net *n = f->private_data; |
| 790 | struct socket *tx_sock; |
| 791 | struct socket *rx_sock; |
| 792 | |
| 793 | vhost_net_stop(n, &tx_sock, &rx_sock); |
| 794 | vhost_net_flush(n); |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 795 | vhost_dev_stop(&n->dev); |
Michael S. Tsirkin | ea5d404 | 2011-11-27 19:05:58 +0200 | [diff] [blame] | 796 | vhost_dev_cleanup(&n->dev, false); |
Michael S. Tsirkin | 81f95a5 | 2013-04-28 15:51:40 +0300 | [diff] [blame^] | 797 | vhost_net_vq_reset(n); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 798 | if (tx_sock) |
| 799 | fput(tx_sock->file); |
| 800 | if (rx_sock) |
| 801 | fput(rx_sock->file); |
| 802 | /* We do an extra flush before freeing memory, |
| 803 | * since jobs can re-queue themselves. */ |
| 804 | vhost_net_flush(n); |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 805 | kfree(n->dev.vqs); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 806 | kfree(n); |
| 807 | return 0; |
| 808 | } |
| 809 | |
| 810 | static struct socket *get_raw_socket(int fd) |
| 811 | { |
| 812 | struct { |
| 813 | struct sockaddr_ll sa; |
| 814 | char buf[MAX_ADDR_LEN]; |
| 815 | } uaddr; |
| 816 | int uaddr_len = sizeof uaddr, r; |
| 817 | struct socket *sock = sockfd_lookup(fd, &r); |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 818 | |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 819 | if (!sock) |
| 820 | return ERR_PTR(-ENOTSOCK); |
| 821 | |
| 822 | /* Parameter checking */ |
| 823 | if (sock->sk->sk_type != SOCK_RAW) { |
| 824 | r = -ESOCKTNOSUPPORT; |
| 825 | goto err; |
| 826 | } |
| 827 | |
| 828 | r = sock->ops->getname(sock, (struct sockaddr *)&uaddr.sa, |
| 829 | &uaddr_len, 0); |
| 830 | if (r) |
| 831 | goto err; |
| 832 | |
| 833 | if (uaddr.sa.sll_family != AF_PACKET) { |
| 834 | r = -EPFNOSUPPORT; |
| 835 | goto err; |
| 836 | } |
| 837 | return sock; |
| 838 | err: |
| 839 | fput(sock->file); |
| 840 | return ERR_PTR(r); |
| 841 | } |
| 842 | |
Arnd Bergmann | 501c774 | 2010-02-18 05:46:50 +0000 | [diff] [blame] | 843 | static struct socket *get_tap_socket(int fd) |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 844 | { |
| 845 | struct file *file = fget(fd); |
| 846 | struct socket *sock; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 847 | |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 848 | if (!file) |
| 849 | return ERR_PTR(-EBADF); |
| 850 | sock = tun_get_socket(file); |
Arnd Bergmann | 501c774 | 2010-02-18 05:46:50 +0000 | [diff] [blame] | 851 | if (!IS_ERR(sock)) |
| 852 | return sock; |
| 853 | sock = macvtap_get_socket(file); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 854 | if (IS_ERR(sock)) |
| 855 | fput(file); |
| 856 | return sock; |
| 857 | } |
| 858 | |
| 859 | static struct socket *get_socket(int fd) |
| 860 | { |
| 861 | struct socket *sock; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 862 | |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 863 | /* special case to disable backend */ |
| 864 | if (fd == -1) |
| 865 | return NULL; |
| 866 | sock = get_raw_socket(fd); |
| 867 | if (!IS_ERR(sock)) |
| 868 | return sock; |
Arnd Bergmann | 501c774 | 2010-02-18 05:46:50 +0000 | [diff] [blame] | 869 | sock = get_tap_socket(fd); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 870 | if (!IS_ERR(sock)) |
| 871 | return sock; |
| 872 | return ERR_PTR(-ENOTSOCK); |
| 873 | } |
| 874 | |
| 875 | static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd) |
| 876 | { |
| 877 | struct socket *sock, *oldsock; |
| 878 | struct vhost_virtqueue *vq; |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 879 | struct vhost_net_virtqueue *nvq; |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 880 | struct vhost_ubuf_ref *ubufs, *oldubufs = NULL; |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 881 | int r; |
| 882 | |
| 883 | mutex_lock(&n->dev.mutex); |
| 884 | r = vhost_dev_check_owner(&n->dev); |
| 885 | if (r) |
| 886 | goto err; |
| 887 | |
| 888 | if (index >= VHOST_NET_VQ_MAX) { |
| 889 | r = -ENOBUFS; |
| 890 | goto err; |
| 891 | } |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 892 | vq = &n->vqs[index].vq; |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 893 | nvq = &n->vqs[index]; |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 894 | mutex_lock(&vq->mutex); |
| 895 | |
| 896 | /* Verify that ring has been setup correctly. */ |
| 897 | if (!vhost_vq_access_ok(vq)) { |
| 898 | r = -EFAULT; |
Jeff Dike | 1dace8c | 2010-03-04 16:10:14 -0500 | [diff] [blame] | 899 | goto err_vq; |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 900 | } |
| 901 | sock = get_socket(fd); |
| 902 | if (IS_ERR(sock)) { |
| 903 | r = PTR_ERR(sock); |
Jeff Dike | 1dace8c | 2010-03-04 16:10:14 -0500 | [diff] [blame] | 904 | goto err_vq; |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | /* start polling new socket */ |
Arnd Bergmann | 28457ee | 2010-03-09 19:24:45 +0100 | [diff] [blame] | 908 | oldsock = rcu_dereference_protected(vq->private_data, |
| 909 | lockdep_is_held(&vq->mutex)); |
David S. Miller | 11fe883 | 2010-07-20 18:25:24 -0700 | [diff] [blame] | 910 | if (sock != oldsock) { |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 911 | ubufs = vhost_ubuf_alloc(vq, sock && vhost_sock_zcopy(sock)); |
| 912 | if (IS_ERR(ubufs)) { |
| 913 | r = PTR_ERR(ubufs); |
| 914 | goto err_ubufs; |
| 915 | } |
Jason Wang | 692a998 | 2013-01-28 01:05:17 +0000 | [diff] [blame] | 916 | |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 917 | vhost_net_disable_vq(n, vq); |
| 918 | rcu_assign_pointer(vq->private_data, sock); |
Jason Wang | f59281d | 2011-06-21 18:04:27 +0800 | [diff] [blame] | 919 | r = vhost_init_used(vq); |
| 920 | if (r) |
Jason Wang | 692a998 | 2013-01-28 01:05:17 +0000 | [diff] [blame] | 921 | goto err_used; |
Jason Wang | 2b8b328 | 2013-01-28 01:05:18 +0000 | [diff] [blame] | 922 | r = vhost_net_enable_vq(n, vq); |
| 923 | if (r) |
| 924 | goto err_used; |
Jason Wang | 692a998 | 2013-01-28 01:05:17 +0000 | [diff] [blame] | 925 | |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 926 | oldubufs = nvq->ubufs; |
| 927 | nvq->ubufs = ubufs; |
Michael S. Tsirkin | 64e9a9b | 2012-12-03 07:31:51 +0000 | [diff] [blame] | 928 | |
| 929 | n->tx_packets = 0; |
| 930 | n->tx_zcopy_err = 0; |
Michael S. Tsirkin | 1280c27 | 2012-12-04 00:17:14 +0200 | [diff] [blame] | 931 | n->tx_flush = false; |
Jeff Dike | dd1f407 | 2010-03-04 16:10:14 -0500 | [diff] [blame] | 932 | } |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 933 | |
Michael S. Tsirkin | 1680e90 | 2010-07-15 15:19:12 +0300 | [diff] [blame] | 934 | mutex_unlock(&vq->mutex); |
| 935 | |
Michael S. Tsirkin | c047e5f | 2011-07-20 13:41:31 +0300 | [diff] [blame] | 936 | if (oldubufs) { |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 937 | vhost_ubuf_put_and_wait(oldubufs); |
Michael S. Tsirkin | c047e5f | 2011-07-20 13:41:31 +0300 | [diff] [blame] | 938 | mutex_lock(&vq->mutex); |
Michael S. Tsirkin | eaae813 | 2012-11-01 09:16:51 +0000 | [diff] [blame] | 939 | vhost_zerocopy_signal_used(n, vq); |
Michael S. Tsirkin | c047e5f | 2011-07-20 13:41:31 +0300 | [diff] [blame] | 940 | mutex_unlock(&vq->mutex); |
| 941 | } |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 942 | |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 943 | if (oldsock) { |
| 944 | vhost_net_flush_vq(n, index); |
| 945 | fput(oldsock->file); |
| 946 | } |
Jeff Dike | 1dace8c | 2010-03-04 16:10:14 -0500 | [diff] [blame] | 947 | |
Michael S. Tsirkin | 1680e90 | 2010-07-15 15:19:12 +0300 | [diff] [blame] | 948 | mutex_unlock(&n->dev.mutex); |
| 949 | return 0; |
| 950 | |
Jason Wang | 692a998 | 2013-01-28 01:05:17 +0000 | [diff] [blame] | 951 | err_used: |
| 952 | rcu_assign_pointer(vq->private_data, oldsock); |
| 953 | vhost_net_enable_vq(n, vq); |
| 954 | if (ubufs) |
| 955 | vhost_ubuf_put_and_wait(ubufs); |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 956 | err_ubufs: |
| 957 | fput(sock->file); |
Jeff Dike | 1dace8c | 2010-03-04 16:10:14 -0500 | [diff] [blame] | 958 | err_vq: |
| 959 | mutex_unlock(&vq->mutex); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 960 | err: |
| 961 | mutex_unlock(&n->dev.mutex); |
| 962 | return r; |
| 963 | } |
| 964 | |
| 965 | static long vhost_net_reset_owner(struct vhost_net *n) |
| 966 | { |
| 967 | struct socket *tx_sock = NULL; |
| 968 | struct socket *rx_sock = NULL; |
| 969 | long err; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 970 | |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 971 | mutex_lock(&n->dev.mutex); |
| 972 | err = vhost_dev_check_owner(&n->dev); |
| 973 | if (err) |
| 974 | goto done; |
| 975 | vhost_net_stop(n, &tx_sock, &rx_sock); |
| 976 | vhost_net_flush(n); |
| 977 | err = vhost_dev_reset_owner(&n->dev); |
Michael S. Tsirkin | 81f95a5 | 2013-04-28 15:51:40 +0300 | [diff] [blame^] | 978 | vhost_net_vq_reset(n); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 979 | done: |
| 980 | mutex_unlock(&n->dev.mutex); |
| 981 | if (tx_sock) |
| 982 | fput(tx_sock->file); |
| 983 | if (rx_sock) |
| 984 | fput(rx_sock->file); |
| 985 | return err; |
| 986 | } |
| 987 | |
| 988 | static int vhost_net_set_features(struct vhost_net *n, u64 features) |
| 989 | { |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 990 | size_t vhost_hlen, sock_hlen, hdr_len; |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 991 | int i; |
David Stevens | 8dd014a | 2010-07-27 18:52:21 +0300 | [diff] [blame] | 992 | |
| 993 | hdr_len = (features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? |
| 994 | sizeof(struct virtio_net_hdr_mrg_rxbuf) : |
| 995 | sizeof(struct virtio_net_hdr); |
| 996 | if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) { |
| 997 | /* vhost provides vnet_hdr */ |
| 998 | vhost_hlen = hdr_len; |
| 999 | sock_hlen = 0; |
| 1000 | } else { |
| 1001 | /* socket provides vnet_hdr */ |
| 1002 | vhost_hlen = 0; |
| 1003 | sock_hlen = hdr_len; |
| 1004 | } |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1005 | mutex_lock(&n->dev.mutex); |
| 1006 | if ((features & (1 << VHOST_F_LOG_ALL)) && |
| 1007 | !vhost_log_access_ok(&n->dev)) { |
| 1008 | mutex_unlock(&n->dev.mutex); |
| 1009 | return -EFAULT; |
| 1010 | } |
| 1011 | n->dev.acked_features = features; |
| 1012 | smp_wmb(); |
| 1013 | for (i = 0; i < VHOST_NET_VQ_MAX; ++i) { |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 1014 | mutex_lock(&n->vqs[i].vq.mutex); |
Michael S. Tsirkin | 81f95a5 | 2013-04-28 15:51:40 +0300 | [diff] [blame^] | 1015 | n->vqs[i].vhost_hlen = vhost_hlen; |
| 1016 | n->vqs[i].sock_hlen = sock_hlen; |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 1017 | mutex_unlock(&n->vqs[i].vq.mutex); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1018 | } |
| 1019 | vhost_net_flush(n); |
| 1020 | mutex_unlock(&n->dev.mutex); |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
| 1024 | static long vhost_net_ioctl(struct file *f, unsigned int ioctl, |
| 1025 | unsigned long arg) |
| 1026 | { |
| 1027 | struct vhost_net *n = f->private_data; |
| 1028 | void __user *argp = (void __user *)arg; |
| 1029 | u64 __user *featurep = argp; |
| 1030 | struct vhost_vring_file backend; |
| 1031 | u64 features; |
| 1032 | int r; |
Krishna Kumar | d47effe | 2011-03-01 17:06:37 +0530 | [diff] [blame] | 1033 | |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1034 | switch (ioctl) { |
| 1035 | case VHOST_NET_SET_BACKEND: |
Takuya Yoshikawa | d3553a5 | 2010-05-27 19:01:58 +0900 | [diff] [blame] | 1036 | if (copy_from_user(&backend, argp, sizeof backend)) |
| 1037 | return -EFAULT; |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1038 | return vhost_net_set_backend(n, backend.index, backend.fd); |
| 1039 | case VHOST_GET_FEATURES: |
Stefan Hajnoczi | 0dd05a3 | 2012-07-21 06:55:36 +0000 | [diff] [blame] | 1040 | features = VHOST_NET_FEATURES; |
Takuya Yoshikawa | d3553a5 | 2010-05-27 19:01:58 +0900 | [diff] [blame] | 1041 | if (copy_to_user(featurep, &features, sizeof features)) |
| 1042 | return -EFAULT; |
| 1043 | return 0; |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1044 | case VHOST_SET_FEATURES: |
Takuya Yoshikawa | d3553a5 | 2010-05-27 19:01:58 +0900 | [diff] [blame] | 1045 | if (copy_from_user(&features, featurep, sizeof features)) |
| 1046 | return -EFAULT; |
Stefan Hajnoczi | 0dd05a3 | 2012-07-21 06:55:36 +0000 | [diff] [blame] | 1047 | if (features & ~VHOST_NET_FEATURES) |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1048 | return -EOPNOTSUPP; |
| 1049 | return vhost_net_set_features(n, features); |
| 1050 | case VHOST_RESET_OWNER: |
| 1051 | return vhost_net_reset_owner(n); |
| 1052 | default: |
| 1053 | mutex_lock(&n->dev.mutex); |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 1054 | if (ioctl == VHOST_SET_OWNER) { |
| 1055 | r = vhost_net_set_ubuf_info(n); |
| 1056 | if (r) |
| 1057 | goto out; |
| 1058 | } |
Michael S. Tsirkin | 935cdee | 2012-12-06 14:03:34 +0200 | [diff] [blame] | 1059 | r = vhost_dev_ioctl(&n->dev, ioctl, argp); |
| 1060 | if (r == -ENOIOCTLCMD) |
| 1061 | r = vhost_vring_ioctl(&n->dev, ioctl, argp); |
| 1062 | else |
| 1063 | vhost_net_flush(n); |
Asias He | 2839400 | 2013-04-27 15:07:46 +0800 | [diff] [blame] | 1064 | out: |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1065 | mutex_unlock(&n->dev.mutex); |
| 1066 | return r; |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | #ifdef CONFIG_COMPAT |
| 1071 | static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl, |
| 1072 | unsigned long arg) |
| 1073 | { |
| 1074 | return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg)); |
| 1075 | } |
| 1076 | #endif |
| 1077 | |
Tobias Klauser | 373a83a | 2010-05-17 15:12:49 +0200 | [diff] [blame] | 1078 | static const struct file_operations vhost_net_fops = { |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1079 | .owner = THIS_MODULE, |
| 1080 | .release = vhost_net_release, |
| 1081 | .unlocked_ioctl = vhost_net_ioctl, |
| 1082 | #ifdef CONFIG_COMPAT |
| 1083 | .compat_ioctl = vhost_net_compat_ioctl, |
| 1084 | #endif |
| 1085 | .open = vhost_net_open, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1086 | .llseek = noop_llseek, |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1087 | }; |
| 1088 | |
| 1089 | static struct miscdevice vhost_net_misc = { |
stephen hemminger | 7c7c7f0 | 2012-01-11 19:30:38 +0000 | [diff] [blame] | 1090 | .minor = VHOST_NET_MINOR, |
| 1091 | .name = "vhost-net", |
| 1092 | .fops = &vhost_net_fops, |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1093 | }; |
| 1094 | |
Christoph Hellwig | a8d3782 | 2010-04-13 14:11:25 -0400 | [diff] [blame] | 1095 | static int vhost_net_init(void) |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1096 | { |
Michael S. Tsirkin | bab632d | 2011-07-18 03:48:46 +0000 | [diff] [blame] | 1097 | if (experimental_zcopytx) |
| 1098 | vhost_enable_zcopy(VHOST_NET_VQ_TX); |
Tejun Heo | c23f3445 | 2010-06-02 20:40:00 +0200 | [diff] [blame] | 1099 | return misc_register(&vhost_net_misc); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1100 | } |
| 1101 | module_init(vhost_net_init); |
| 1102 | |
Christoph Hellwig | a8d3782 | 2010-04-13 14:11:25 -0400 | [diff] [blame] | 1103 | static void vhost_net_exit(void) |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1104 | { |
| 1105 | misc_deregister(&vhost_net_misc); |
Michael S. Tsirkin | 3a4d5c94e | 2010-01-14 06:17:27 +0000 | [diff] [blame] | 1106 | } |
| 1107 | module_exit(vhost_net_exit); |
| 1108 | |
| 1109 | MODULE_VERSION("0.0.1"); |
| 1110 | MODULE_LICENSE("GPL v2"); |
| 1111 | MODULE_AUTHOR("Michael S. Tsirkin"); |
| 1112 | MODULE_DESCRIPTION("Host kernel accelerator for virtio net"); |
stephen hemminger | 7c7c7f0 | 2012-01-11 19:30:38 +0000 | [diff] [blame] | 1113 | MODULE_ALIAS_MISCDEV(VHOST_NET_MINOR); |
| 1114 | MODULE_ALIAS("devname:vhost-net"); |