]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/vhost/net.c
vhost_net: try batch dequing from skb array
[mirror_ubuntu-bionic-kernel.git] / drivers / vhost / net.c
CommitLineData
3a4d5c94
MT
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>
3a4d5c94
MT
13#include <linux/miscdevice.h>
14#include <linux/module.h>
bab632d6 15#include <linux/moduleparam.h>
3a4d5c94
MT
16#include <linux/mutex.h>
17#include <linux/workqueue.h>
3a4d5c94 18#include <linux/file.h>
5a0e3ad6 19#include <linux/slab.h>
e6017571 20#include <linux/sched/clock.h>
174cd4b1 21#include <linux/sched/signal.h>
23cc5a99 22#include <linux/vmalloc.h>
3a4d5c94
MT
23
24#include <linux/net.h>
25#include <linux/if_packet.h>
26#include <linux/if_arp.h>
27#include <linux/if_tun.h>
501c774c 28#include <linux/if_macvlan.h>
635b8c8e 29#include <linux/if_tap.h>
c53cff5e 30#include <linux/if_vlan.h>
c67df11f
JW
31#include <linux/skb_array.h>
32#include <linux/skbuff.h>
3a4d5c94
MT
33
34#include <net/sock.h>
35
36#include "vhost.h"
37
f9611c43 38static int experimental_zcopytx = 1;
bab632d6 39module_param(experimental_zcopytx, int, 0444);
f9611c43
MT
40MODULE_PARM_DESC(experimental_zcopytx, "Enable Zero Copy TX;"
41 " 1 -Enable; 0 - Disable");
bab632d6 42
3a4d5c94
MT
43/* Max number of bytes transferred before requeueing the job.
44 * Using this limit prevents one virtqueue from starving others. */
45#define VHOST_NET_WEIGHT 0x80000
46
bab632d6
MT
47/* MAX number of TX used buffers for outstanding zerocopy */
48#define VHOST_MAX_PEND 128
49#define VHOST_GOODCOPY_LEN 256
50
eaae8132
MT
51/*
52 * For transmit, used buffer len is unused; we override it to track buffer
53 * status internally; used for zerocopy tx only.
54 */
55/* Lower device DMA failed */
bf995734 56#define VHOST_DMA_FAILED_LEN ((__force __virtio32)3)
eaae8132 57/* Lower device DMA done */
bf995734 58#define VHOST_DMA_DONE_LEN ((__force __virtio32)2)
eaae8132 59/* Lower device DMA in progress */
bf995734 60#define VHOST_DMA_IN_PROGRESS ((__force __virtio32)1)
eaae8132 61/* Buffer unused */
bf995734 62#define VHOST_DMA_CLEAR_LEN ((__force __virtio32)0)
eaae8132 63
bf995734 64#define VHOST_DMA_IS_DONE(len) ((__force u32)(len) >= (__force u32)VHOST_DMA_DONE_LEN)
eaae8132 65
8570a6e7
AH
66enum {
67 VHOST_NET_FEATURES = VHOST_FEATURES |
68 (1ULL << VHOST_NET_F_VIRTIO_NET_HDR) |
6b1e6cc7
JW
69 (1ULL << VIRTIO_NET_F_MRG_RXBUF) |
70 (1ULL << VIRTIO_F_IOMMU_PLATFORM)
8570a6e7
AH
71};
72
3a4d5c94
MT
73enum {
74 VHOST_NET_VQ_RX = 0,
75 VHOST_NET_VQ_TX = 1,
76 VHOST_NET_VQ_MAX = 2,
77};
78
fe729a57 79struct vhost_net_ubuf_ref {
0ad8b480
MT
80 /* refcount follows semantics similar to kref:
81 * 0: object is released
82 * 1: no outstanding ubufs
83 * >1: outstanding ubufs
84 */
85 atomic_t refcount;
2839400f
AH
86 wait_queue_head_t wait;
87 struct vhost_virtqueue *vq;
88};
89
c67df11f
JW
90#define VHOST_RX_BATCH 64
91struct vhost_net_buf {
92 struct sk_buff **queue;
93 int tail;
94 int head;
95};
96
3ab2e420
AH
97struct vhost_net_virtqueue {
98 struct vhost_virtqueue vq;
81f95a55
MT
99 size_t vhost_hlen;
100 size_t sock_hlen;
2839400f
AH
101 /* vhost zerocopy support fields below: */
102 /* last used idx for outstanding DMA zerocopy buffers */
103 int upend_idx;
104 /* first used idx for DMA done zerocopy buffers */
105 int done_idx;
106 /* an array of userspace buffers info */
107 struct ubuf_info *ubuf_info;
108 /* Reference counting for outstanding ubufs.
109 * Protected by vq mutex. Writers must also take device mutex. */
fe729a57 110 struct vhost_net_ubuf_ref *ubufs;
c67df11f
JW
111 struct skb_array *rx_array;
112 struct vhost_net_buf rxq;
3ab2e420
AH
113};
114
3a4d5c94
MT
115struct vhost_net {
116 struct vhost_dev dev;
3ab2e420 117 struct vhost_net_virtqueue vqs[VHOST_NET_VQ_MAX];
3a4d5c94 118 struct vhost_poll poll[VHOST_NET_VQ_MAX];
eaae8132
MT
119 /* Number of TX recently submitted.
120 * Protected by tx vq lock. */
121 unsigned tx_packets;
122 /* Number of times zerocopy TX recently failed.
123 * Protected by tx vq lock. */
124 unsigned tx_zcopy_err;
1280c27f
MT
125 /* Flush in progress. Protected by tx vq lock. */
126 bool tx_flush;
3a4d5c94
MT
127};
128
fe729a57 129static unsigned vhost_net_zcopy_mask __read_mostly;
2839400f 130
c67df11f
JW
131static void *vhost_net_buf_get_ptr(struct vhost_net_buf *rxq)
132{
133 if (rxq->tail != rxq->head)
134 return rxq->queue[rxq->head];
135 else
136 return NULL;
137}
138
139static int vhost_net_buf_get_size(struct vhost_net_buf *rxq)
140{
141 return rxq->tail - rxq->head;
142}
143
144static int vhost_net_buf_is_empty(struct vhost_net_buf *rxq)
145{
146 return rxq->tail == rxq->head;
147}
148
149static void *vhost_net_buf_consume(struct vhost_net_buf *rxq)
150{
151 void *ret = vhost_net_buf_get_ptr(rxq);
152 ++rxq->head;
153 return ret;
154}
155
156static int vhost_net_buf_produce(struct vhost_net_virtqueue *nvq)
157{
158 struct vhost_net_buf *rxq = &nvq->rxq;
159
160 rxq->head = 0;
161 rxq->tail = skb_array_consume_batched(nvq->rx_array, rxq->queue,
162 VHOST_RX_BATCH);
163 return rxq->tail;
164}
165
166static void vhost_net_buf_unproduce(struct vhost_net_virtqueue *nvq)
167{
168 struct vhost_net_buf *rxq = &nvq->rxq;
169
170 if (nvq->rx_array && !vhost_net_buf_is_empty(rxq)) {
171 skb_array_unconsume(nvq->rx_array, rxq->queue + rxq->head,
172 vhost_net_buf_get_size(rxq));
173 rxq->head = rxq->tail = 0;
174 }
175}
176
177static int vhost_net_buf_peek(struct vhost_net_virtqueue *nvq)
178{
179 struct vhost_net_buf *rxq = &nvq->rxq;
180
181 if (!vhost_net_buf_is_empty(rxq))
182 goto out;
183
184 if (!vhost_net_buf_produce(nvq))
185 return 0;
186
187out:
188 return __skb_array_len_with_tag(vhost_net_buf_get_ptr(rxq));
189}
190
191static void vhost_net_buf_init(struct vhost_net_buf *rxq)
192{
193 rxq->head = rxq->tail = 0;
194}
195
fe729a57 196static void vhost_net_enable_zcopy(int vq)
2839400f 197{
fe729a57 198 vhost_net_zcopy_mask |= 0x1 << vq;
2839400f
AH
199}
200
fe729a57
AH
201static struct vhost_net_ubuf_ref *
202vhost_net_ubuf_alloc(struct vhost_virtqueue *vq, bool zcopy)
2839400f 203{
fe729a57 204 struct vhost_net_ubuf_ref *ubufs;
2839400f
AH
205 /* No zero copy backend? Nothing to count. */
206 if (!zcopy)
207 return NULL;
208 ubufs = kmalloc(sizeof(*ubufs), GFP_KERNEL);
209 if (!ubufs)
210 return ERR_PTR(-ENOMEM);
0ad8b480 211 atomic_set(&ubufs->refcount, 1);
2839400f
AH
212 init_waitqueue_head(&ubufs->wait);
213 ubufs->vq = vq;
214 return ubufs;
215}
216
0ad8b480 217static int vhost_net_ubuf_put(struct vhost_net_ubuf_ref *ubufs)
2839400f 218{
0ad8b480
MT
219 int r = atomic_sub_return(1, &ubufs->refcount);
220 if (unlikely(!r))
221 wake_up(&ubufs->wait);
222 return r;
2839400f
AH
223}
224
fe729a57 225static void vhost_net_ubuf_put_and_wait(struct vhost_net_ubuf_ref *ubufs)
2839400f 226{
0ad8b480
MT
227 vhost_net_ubuf_put(ubufs);
228 wait_event(ubufs->wait, !atomic_read(&ubufs->refcount));
c38e39c3
MT
229}
230
231static void vhost_net_ubuf_put_wait_and_free(struct vhost_net_ubuf_ref *ubufs)
232{
233 vhost_net_ubuf_put_and_wait(ubufs);
2839400f
AH
234 kfree(ubufs);
235}
236
b1ad8496
AH
237static void vhost_net_clear_ubuf_info(struct vhost_net *n)
238{
b1ad8496
AH
239 int i;
240
288cfe78
MT
241 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
242 kfree(n->vqs[i].ubuf_info);
243 n->vqs[i].ubuf_info = NULL;
b1ad8496
AH
244 }
245}
246
0a1febf7 247static int vhost_net_set_ubuf_info(struct vhost_net *n)
2839400f
AH
248{
249 bool zcopy;
250 int i;
251
288cfe78 252 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
fe729a57 253 zcopy = vhost_net_zcopy_mask & (0x1 << i);
2839400f
AH
254 if (!zcopy)
255 continue;
256 n->vqs[i].ubuf_info = kmalloc(sizeof(*n->vqs[i].ubuf_info) *
257 UIO_MAXIOV, GFP_KERNEL);
258 if (!n->vqs[i].ubuf_info)
259 goto err;
260 }
261 return 0;
262
263err:
288cfe78 264 vhost_net_clear_ubuf_info(n);
2839400f
AH
265 return -ENOMEM;
266}
267
0a1febf7 268static void vhost_net_vq_reset(struct vhost_net *n)
2839400f
AH
269{
270 int i;
271
288cfe78
MT
272 vhost_net_clear_ubuf_info(n);
273
2839400f
AH
274 for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
275 n->vqs[i].done_idx = 0;
276 n->vqs[i].upend_idx = 0;
277 n->vqs[i].ubufs = NULL;
81f95a55
MT
278 n->vqs[i].vhost_hlen = 0;
279 n->vqs[i].sock_hlen = 0;
c67df11f 280 vhost_net_buf_init(&n->vqs[i].rxq);
2839400f
AH
281 }
282
283}
284
eaae8132
MT
285static void vhost_net_tx_packet(struct vhost_net *net)
286{
287 ++net->tx_packets;
288 if (net->tx_packets < 1024)
289 return;
290 net->tx_packets = 0;
291 net->tx_zcopy_err = 0;
292}
293
294static void vhost_net_tx_err(struct vhost_net *net)
295{
296 ++net->tx_zcopy_err;
297}
298
299static bool vhost_net_tx_select_zcopy(struct vhost_net *net)
300{
1280c27f
MT
301 /* TX flush waits for outstanding DMAs to be done.
302 * Don't start new DMAs.
303 */
304 return !net->tx_flush &&
305 net->tx_packets / 64 >= net->tx_zcopy_err;
eaae8132
MT
306}
307
bab632d6
MT
308static bool vhost_sock_zcopy(struct socket *sock)
309{
310 return unlikely(experimental_zcopytx) &&
311 sock_flag(sock->sk, SOCK_ZEROCOPY);
312}
313
b211616d
MT
314/* In case of DMA done not in order in lower device driver for some reason.
315 * upend_idx is used to track end of used idx, done_idx is used to track head
316 * of used idx. Once lower device DMA done contiguously, we will signal KVM
317 * guest used idx.
318 */
094afe7d
JW
319static void vhost_zerocopy_signal_used(struct vhost_net *net,
320 struct vhost_virtqueue *vq)
b211616d 321{
2839400f
AH
322 struct vhost_net_virtqueue *nvq =
323 container_of(vq, struct vhost_net_virtqueue, vq);
c92112ae 324 int i, add;
b211616d
MT
325 int j = 0;
326
2839400f 327 for (i = nvq->done_idx; i != nvq->upend_idx; i = (i + 1) % UIO_MAXIOV) {
eaae8132
MT
328 if (vq->heads[i].len == VHOST_DMA_FAILED_LEN)
329 vhost_net_tx_err(net);
b211616d
MT
330 if (VHOST_DMA_IS_DONE(vq->heads[i].len)) {
331 vq->heads[i].len = VHOST_DMA_CLEAR_LEN;
b211616d
MT
332 ++j;
333 } else
334 break;
335 }
c92112ae
JW
336 while (j) {
337 add = min(UIO_MAXIOV - nvq->done_idx, j);
338 vhost_add_used_and_signal_n(vq->dev, vq,
339 &vq->heads[nvq->done_idx], add);
340 nvq->done_idx = (nvq->done_idx + add) % UIO_MAXIOV;
341 j -= add;
342 }
b211616d
MT
343}
344
eaae8132 345static void vhost_zerocopy_callback(struct ubuf_info *ubuf, bool success)
b211616d 346{
fe729a57 347 struct vhost_net_ubuf_ref *ubufs = ubuf->ctx;
b211616d 348 struct vhost_virtqueue *vq = ubufs->vq;
0ad8b480 349 int cnt;
24eb21a1 350
b0c057ca
MT
351 rcu_read_lock_bh();
352
19c73b3e
JW
353 /* set len to mark this desc buffers done DMA */
354 vq->heads[ubuf->desc].len = success ?
355 VHOST_DMA_DONE_LEN : VHOST_DMA_FAILED_LEN;
0ad8b480 356 cnt = vhost_net_ubuf_put(ubufs);
19c73b3e 357
24eb21a1
MT
358 /*
359 * Trigger polling thread if guest stopped submitting new buffers:
0ad8b480 360 * in this case, the refcount after decrement will eventually reach 1.
24eb21a1
MT
361 * We also trigger polling periodically after each 16 packets
362 * (the value 16 here is more or less arbitrary, it's tuned to trigger
363 * less than 10% of times).
364 */
0ad8b480 365 if (cnt <= 1 || !(cnt % 16))
24eb21a1 366 vhost_poll_queue(&vq->poll);
b0c057ca
MT
367
368 rcu_read_unlock_bh();
b211616d
MT
369}
370
03088137
JW
371static inline unsigned long busy_clock(void)
372{
373 return local_clock() >> 10;
374}
375
376static bool vhost_can_busy_poll(struct vhost_dev *dev,
377 unsigned long endtime)
378{
379 return likely(!need_resched()) &&
380 likely(!time_after(busy_clock(), endtime)) &&
381 likely(!signal_pending(current)) &&
382 !vhost_has_work(dev);
383}
384
8241a1e4
JW
385static void vhost_net_disable_vq(struct vhost_net *n,
386 struct vhost_virtqueue *vq)
387{
388 struct vhost_net_virtqueue *nvq =
389 container_of(vq, struct vhost_net_virtqueue, vq);
390 struct vhost_poll *poll = n->poll + (nvq - n->vqs);
391 if (!vq->private_data)
392 return;
393 vhost_poll_stop(poll);
394}
395
396static int vhost_net_enable_vq(struct vhost_net *n,
397 struct vhost_virtqueue *vq)
398{
399 struct vhost_net_virtqueue *nvq =
400 container_of(vq, struct vhost_net_virtqueue, vq);
401 struct vhost_poll *poll = n->poll + (nvq - n->vqs);
402 struct socket *sock;
403
404 sock = vq->private_data;
405 if (!sock)
406 return 0;
407
408 return vhost_poll_start(poll, sock->file);
409}
410
03088137
JW
411static int vhost_net_tx_get_vq_desc(struct vhost_net *net,
412 struct vhost_virtqueue *vq,
413 struct iovec iov[], unsigned int iov_size,
414 unsigned int *out_num, unsigned int *in_num)
415{
416 unsigned long uninitialized_var(endtime);
417 int r = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov),
6b1e6cc7 418 out_num, in_num, NULL, NULL);
03088137
JW
419
420 if (r == vq->num && vq->busyloop_timeout) {
421 preempt_disable();
422 endtime = busy_clock() + vq->busyloop_timeout;
423 while (vhost_can_busy_poll(vq->dev, endtime) &&
424 vhost_vq_avail_empty(vq->dev, vq))
f2f09a4c 425 cpu_relax();
03088137
JW
426 preempt_enable();
427 r = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov),
6b1e6cc7 428 out_num, in_num, NULL, NULL);
03088137
JW
429 }
430
431 return r;
432}
433
0ed005ce
JW
434static bool vhost_exceeds_maxpend(struct vhost_net *net)
435{
436 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
437 struct vhost_virtqueue *vq = &nvq->vq;
438
439 return (nvq->upend_idx + vq->num - VHOST_MAX_PEND) % UIO_MAXIOV
440 == nvq->done_idx;
441}
442
3a4d5c94
MT
443/* Expects to be always run from workqueue - which acts as
444 * read-size critical section for our kind of RCU. */
445static void handle_tx(struct vhost_net *net)
446{
2839400f 447 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
81f95a55 448 struct vhost_virtqueue *vq = &nvq->vq;
98a527aa 449 unsigned out, in;
d5675bd2 450 int head;
3a4d5c94
MT
451 struct msghdr msg = {
452 .msg_name = NULL,
453 .msg_namelen = 0,
454 .msg_control = NULL,
455 .msg_controllen = 0,
3a4d5c94
MT
456 .msg_flags = MSG_DONTWAIT,
457 };
458 size_t len, total_len = 0;
70181d51 459 int err;
3a4d5c94 460 size_t hdr_size;
28457ee6 461 struct socket *sock;
fe729a57 462 struct vhost_net_ubuf_ref *uninitialized_var(ubufs);
cedb9bdc 463 bool zcopy, zcopy_used;
28457ee6 464
2e26af79
AH
465 mutex_lock(&vq->mutex);
466 sock = vq->private_data;
3a4d5c94 467 if (!sock)
2e26af79 468 goto out;
3a4d5c94 469
6b1e6cc7
JW
470 if (!vq_iotlb_prefetch(vq))
471 goto out;
472
8ea8cf89 473 vhost_disable_notify(&net->dev, vq);
3a4d5c94 474
81f95a55 475 hdr_size = nvq->vhost_hlen;
2839400f 476 zcopy = nvq->ubufs;
3a4d5c94
MT
477
478 for (;;) {
bab632d6
MT
479 /* Release DMAs done buffers first */
480 if (zcopy)
eaae8132 481 vhost_zerocopy_signal_used(net, vq);
bab632d6 482
f7c6be40
JW
483 /* If more outstanding DMAs, queue the work.
484 * Handle upend_idx wrap around
485 */
0ed005ce 486 if (unlikely(vhost_exceeds_maxpend(net)))
f7c6be40
JW
487 break;
488
03088137
JW
489 head = vhost_net_tx_get_vq_desc(net, vq, vq->iov,
490 ARRAY_SIZE(vq->iov),
491 &out, &in);
d5675bd2 492 /* On error, stop handling until the next kick. */
7b3384fc 493 if (unlikely(head < 0))
d5675bd2 494 break;
3a4d5c94
MT
495 /* Nothing new? Wait for eventfd to tell us they refilled. */
496 if (head == vq->num) {
8ea8cf89
MT
497 if (unlikely(vhost_enable_notify(&net->dev, vq))) {
498 vhost_disable_notify(&net->dev, vq);
3a4d5c94
MT
499 continue;
500 }
501 break;
502 }
503 if (in) {
504 vq_err(vq, "Unexpected descriptor format for TX: "
505 "out %d, int %d\n", out, in);
506 break;
507 }
508 /* Skip header. TODO: support TSO. */
3a4d5c94 509 len = iov_length(vq->iov, out);
c0371da6 510 iov_iter_init(&msg.msg_iter, WRITE, vq->iov, out, len);
98a527aa 511 iov_iter_advance(&msg.msg_iter, hdr_size);
3a4d5c94 512 /* Sanity check */
01e97e65 513 if (!msg_data_left(&msg)) {
3a4d5c94
MT
514 vq_err(vq, "Unexpected header len for TX: "
515 "%zd expected %zd\n",
98a527aa 516 len, hdr_size);
3a4d5c94
MT
517 break;
518 }
01e97e65 519 len = msg_data_left(&msg);
ce21a029
JW
520
521 zcopy_used = zcopy && len >= VHOST_GOODCOPY_LEN
522 && (nvq->upend_idx + 1) % UIO_MAXIOV !=
523 nvq->done_idx
524 && vhost_net_tx_select_zcopy(net);
cedb9bdc 525
bab632d6 526 /* use msg_control to pass vhost zerocopy ubuf info to skb */
cedb9bdc 527 if (zcopy_used) {
ce21a029
JW
528 struct ubuf_info *ubuf;
529 ubuf = nvq->ubuf_info + nvq->upend_idx;
530
8b38694a 531 vq->heads[nvq->upend_idx].id = cpu_to_vhost32(vq, head);
ce21a029
JW
532 vq->heads[nvq->upend_idx].len = VHOST_DMA_IN_PROGRESS;
533 ubuf->callback = vhost_zerocopy_callback;
534 ubuf->ctx = nvq->ubufs;
535 ubuf->desc = nvq->upend_idx;
536 msg.msg_control = ubuf;
537 msg.msg_controllen = sizeof(ubuf);
538 ubufs = nvq->ubufs;
0ad8b480 539 atomic_inc(&ubufs->refcount);
2839400f 540 nvq->upend_idx = (nvq->upend_idx + 1) % UIO_MAXIOV;
ce21a029 541 } else {
4364d5f9 542 msg.msg_control = NULL;
ce21a029
JW
543 ubufs = NULL;
544 }
0ed005ce
JW
545
546 total_len += len;
547 if (total_len < VHOST_NET_WEIGHT &&
548 !vhost_vq_avail_empty(&net->dev, vq) &&
549 likely(!vhost_exceeds_maxpend(net))) {
550 msg.msg_flags |= MSG_MORE;
551 } else {
552 msg.msg_flags &= ~MSG_MORE;
553 }
554
3a4d5c94 555 /* TODO: Check specific error and bomb out unless ENOBUFS? */
1b784140 556 err = sock->ops->sendmsg(sock, &msg, len);
3a4d5c94 557 if (unlikely(err < 0)) {
cedb9bdc 558 if (zcopy_used) {
ce21a029 559 vhost_net_ubuf_put(ubufs);
2839400f
AH
560 nvq->upend_idx = ((unsigned)nvq->upend_idx - 1)
561 % UIO_MAXIOV;
bab632d6 562 }
8dd014ad 563 vhost_discard_vq_desc(vq, 1);
3a4d5c94
MT
564 break;
565 }
566 if (err != len)
95c0ec6a
MT
567 pr_debug("Truncated TX packet: "
568 " len %d != %zd\n", err, len);
cedb9bdc 569 if (!zcopy_used)
bab632d6 570 vhost_add_used_and_signal(&net->dev, vq, head, 0);
c8fb217a 571 else
eaae8132 572 vhost_zerocopy_signal_used(net, vq);
eaae8132 573 vhost_net_tx_packet(net);
3a4d5c94
MT
574 if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
575 vhost_poll_queue(&vq->poll);
576 break;
577 }
578 }
2e26af79 579out:
3a4d5c94 580 mutex_unlock(&vq->mutex);
3a4d5c94
MT
581}
582
c67df11f 583static int peek_head_len(struct vhost_net_virtqueue *rvq, struct sock *sk)
8dd014ad
DS
584{
585 struct sk_buff *head;
586 int len = 0;
783e3988 587 unsigned long flags;
8dd014ad 588
c67df11f
JW
589 if (rvq->rx_array)
590 return vhost_net_buf_peek(rvq);
1576d986 591
783e3988 592 spin_lock_irqsave(&sk->sk_receive_queue.lock, flags);
8dd014ad 593 head = skb_peek(&sk->sk_receive_queue);
c53cff5e 594 if (likely(head)) {
8dd014ad 595 len = head->len;
df8a39de 596 if (skb_vlan_tag_present(head))
c53cff5e
BG
597 len += VLAN_HLEN;
598 }
599
783e3988 600 spin_unlock_irqrestore(&sk->sk_receive_queue.lock, flags);
8dd014ad
DS
601 return len;
602}
603
1576d986
JW
604static int sk_has_rx_data(struct sock *sk)
605{
606 struct socket *sock = sk->sk_socket;
607
608 if (sock->ops->peek_len)
609 return sock->ops->peek_len(sock);
610
611 return skb_queue_empty(&sk->sk_receive_queue);
612}
613
03088137
JW
614static int vhost_net_rx_peek_head_len(struct vhost_net *net, struct sock *sk)
615{
c67df11f 616 struct vhost_net_virtqueue *rvq = &net->vqs[VHOST_NET_VQ_RX];
03088137
JW
617 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
618 struct vhost_virtqueue *vq = &nvq->vq;
619 unsigned long uninitialized_var(endtime);
c67df11f 620 int len = peek_head_len(rvq, sk);
03088137
JW
621
622 if (!len && vq->busyloop_timeout) {
623 /* Both tx vq and rx socket were polled here */
624 mutex_lock(&vq->mutex);
625 vhost_disable_notify(&net->dev, vq);
626
627 preempt_disable();
628 endtime = busy_clock() + vq->busyloop_timeout;
629
630 while (vhost_can_busy_poll(&net->dev, endtime) &&
1576d986 631 !sk_has_rx_data(sk) &&
03088137 632 vhost_vq_avail_empty(&net->dev, vq))
f2f09a4c 633 cpu_relax();
03088137
JW
634
635 preempt_enable();
636
637 if (vhost_enable_notify(&net->dev, vq))
638 vhost_poll_queue(&vq->poll);
639 mutex_unlock(&vq->mutex);
640
c67df11f 641 len = peek_head_len(rvq, sk);
03088137
JW
642 }
643
644 return len;
645}
646
8dd014ad
DS
647/* This is a multi-buffer version of vhost_get_desc, that works if
648 * vq has read descriptors only.
649 * @vq - the relevant virtqueue
650 * @datalen - data length we'll be reading
651 * @iovcount - returned count of io vectors we fill
652 * @log - vhost log
653 * @log_num - log offset
94249369 654 * @quota - headcount quota, 1 for big buffer
8dd014ad
DS
655 * returns number of buffer heads allocated, negative on error
656 */
657static int get_rx_bufs(struct vhost_virtqueue *vq,
658 struct vring_used_elem *heads,
659 int datalen,
660 unsigned *iovcount,
661 struct vhost_log *log,
94249369
JW
662 unsigned *log_num,
663 unsigned int quota)
8dd014ad
DS
664{
665 unsigned int out, in;
666 int seg = 0;
667 int headcount = 0;
668 unsigned d;
669 int r, nlogs = 0;
8b38694a
MT
670 /* len is always initialized before use since we are always called with
671 * datalen > 0.
672 */
673 u32 uninitialized_var(len);
8dd014ad 674
94249369 675 while (datalen > 0 && headcount < quota) {
e0e9b406 676 if (unlikely(seg >= UIO_MAXIOV)) {
8dd014ad
DS
677 r = -ENOBUFS;
678 goto err;
679 }
47283bef 680 r = vhost_get_vq_desc(vq, vq->iov + seg,
8dd014ad
DS
681 ARRAY_SIZE(vq->iov) - seg, &out,
682 &in, log, log_num);
a39ee449
MT
683 if (unlikely(r < 0))
684 goto err;
685
686 d = r;
8dd014ad
DS
687 if (d == vq->num) {
688 r = 0;
689 goto err;
690 }
691 if (unlikely(out || in <= 0)) {
692 vq_err(vq, "unexpected descriptor format for RX: "
693 "out %d, in %d\n", out, in);
694 r = -EINVAL;
695 goto err;
696 }
697 if (unlikely(log)) {
698 nlogs += *log_num;
699 log += *log_num;
700 }
8b38694a
MT
701 heads[headcount].id = cpu_to_vhost32(vq, d);
702 len = iov_length(vq->iov + seg, in);
703 heads[headcount].len = cpu_to_vhost32(vq, len);
704 datalen -= len;
8dd014ad
DS
705 ++headcount;
706 seg += in;
707 }
99975cc6 708 heads[headcount - 1].len = cpu_to_vhost32(vq, len + datalen);
8dd014ad
DS
709 *iovcount = seg;
710 if (unlikely(log))
711 *log_num = nlogs;
d8316f39
MT
712
713 /* Detect overrun */
714 if (unlikely(datalen > 0)) {
715 r = UIO_MAXIOV + 1;
716 goto err;
717 }
8dd014ad
DS
718 return headcount;
719err:
720 vhost_discard_vq_desc(vq, headcount);
721 return r;
722}
723
3a4d5c94
MT
724/* Expects to be always run from workqueue - which acts as
725 * read-size critical section for our kind of RCU. */
94249369 726static void handle_rx(struct vhost_net *net)
3a4d5c94 727{
81f95a55
MT
728 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_RX];
729 struct vhost_virtqueue *vq = &nvq->vq;
8dd014ad
DS
730 unsigned uninitialized_var(in), log;
731 struct vhost_log *vq_log;
732 struct msghdr msg = {
733 .msg_name = NULL,
734 .msg_namelen = 0,
735 .msg_control = NULL, /* FIXME: get and handle RX aux data. */
736 .msg_controllen = 0,
8dd014ad
DS
737 .msg_flags = MSG_DONTWAIT,
738 };
0960b641
JW
739 struct virtio_net_hdr hdr = {
740 .flags = 0,
741 .gso_type = VIRTIO_NET_HDR_GSO_NONE
8dd014ad 742 };
8dd014ad 743 size_t total_len = 0;
910a578f
MT
744 int err, mergeable;
745 s16 headcount;
8dd014ad
DS
746 size_t vhost_hlen, sock_hlen;
747 size_t vhost_len, sock_len;
2e26af79 748 struct socket *sock;
ba7438ae 749 struct iov_iter fixup;
0960b641 750 __virtio16 num_buffers;
8dd014ad 751
8dd014ad 752 mutex_lock(&vq->mutex);
2e26af79
AH
753 sock = vq->private_data;
754 if (!sock)
755 goto out;
6b1e6cc7
JW
756
757 if (!vq_iotlb_prefetch(vq))
758 goto out;
759
8ea8cf89 760 vhost_disable_notify(&net->dev, vq);
8241a1e4 761 vhost_net_disable_vq(net, vq);
2e26af79 762
81f95a55
MT
763 vhost_hlen = nvq->vhost_hlen;
764 sock_hlen = nvq->sock_hlen;
8dd014ad 765
ea16c514 766 vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ?
8dd014ad 767 vq->log : NULL;
ea16c514 768 mergeable = vhost_has_feature(vq, VIRTIO_NET_F_MRG_RXBUF);
8dd014ad 769
03088137 770 while ((sock_len = vhost_net_rx_peek_head_len(net, sock->sk))) {
8dd014ad
DS
771 sock_len += sock_hlen;
772 vhost_len = sock_len + vhost_hlen;
773 headcount = get_rx_bufs(vq, vq->heads, vhost_len,
94249369
JW
774 &in, vq_log, &log,
775 likely(mergeable) ? UIO_MAXIOV : 1);
8dd014ad
DS
776 /* On error, stop handling until the next kick. */
777 if (unlikely(headcount < 0))
8241a1e4 778 goto out;
c67df11f
JW
779 if (nvq->rx_array)
780 msg.msg_control = vhost_net_buf_consume(&nvq->rxq);
d8316f39
MT
781 /* On overrun, truncate and discard */
782 if (unlikely(headcount > UIO_MAXIOV)) {
c0371da6 783 iov_iter_init(&msg.msg_iter, READ, vq->iov, 1, 1);
1b784140 784 err = sock->ops->recvmsg(sock, &msg,
d8316f39
MT
785 1, MSG_DONTWAIT | MSG_TRUNC);
786 pr_debug("Discarded rx packet: len %zd\n", sock_len);
787 continue;
788 }
8dd014ad
DS
789 /* OK, now we need to know about added descriptors. */
790 if (!headcount) {
8ea8cf89 791 if (unlikely(vhost_enable_notify(&net->dev, vq))) {
8dd014ad
DS
792 /* They have slipped one in as we were
793 * doing that: check again. */
8ea8cf89 794 vhost_disable_notify(&net->dev, vq);
8dd014ad
DS
795 continue;
796 }
797 /* Nothing new? Wait for eventfd to tell us
798 * they refilled. */
8241a1e4 799 goto out;
8dd014ad
DS
800 }
801 /* We don't need to be notified again. */
ba7438ae
AV
802 iov_iter_init(&msg.msg_iter, READ, vq->iov, in, vhost_len);
803 fixup = msg.msg_iter;
804 if (unlikely((vhost_hlen))) {
805 /* We will supply the header ourselves
806 * TODO: support TSO.
807 */
808 iov_iter_advance(&msg.msg_iter, vhost_hlen);
ba7438ae 809 }
1b784140 810 err = sock->ops->recvmsg(sock, &msg,
8dd014ad
DS
811 sock_len, MSG_DONTWAIT | MSG_TRUNC);
812 /* Userspace might have consumed the packet meanwhile:
813 * it's not supposed to do this usually, but might be hard
814 * to prevent. Discard data we got (if any) and keep going. */
815 if (unlikely(err != sock_len)) {
816 pr_debug("Discarded rx packet: "
817 " len %d, expected %zd\n", err, sock_len);
818 vhost_discard_vq_desc(vq, headcount);
819 continue;
820 }
ba7438ae 821 /* Supply virtio_net_hdr if VHOST_NET_F_VIRTIO_NET_HDR */
4c5a8442
MT
822 if (unlikely(vhost_hlen)) {
823 if (copy_to_iter(&hdr, sizeof(hdr),
824 &fixup) != sizeof(hdr)) {
825 vq_err(vq, "Unable to write vnet_hdr "
826 "at addr %p\n", vq->iov->iov_base);
8241a1e4 827 goto out;
4c5a8442
MT
828 }
829 } else {
830 /* Header came from socket; we'll need to patch
831 * ->num_buffers over if VIRTIO_NET_F_MRG_RXBUF
832 */
833 iov_iter_advance(&fixup, sizeof(hdr));
8dd014ad
DS
834 }
835 /* TODO: Should check and handle checksum. */
5201aa49 836
0960b641 837 num_buffers = cpu_to_vhost16(vq, headcount);
cfbdab95 838 if (likely(mergeable) &&
0d79a493
MT
839 copy_to_iter(&num_buffers, sizeof num_buffers,
840 &fixup) != sizeof num_buffers) {
8dd014ad
DS
841 vq_err(vq, "Failed num_buffers write");
842 vhost_discard_vq_desc(vq, headcount);
8241a1e4 843 goto out;
8dd014ad
DS
844 }
845 vhost_add_used_and_signal_n(&net->dev, vq, vq->heads,
846 headcount);
847 if (unlikely(vq_log))
848 vhost_log_write(vq, vq_log, log, vhost_len);
849 total_len += vhost_len;
850 if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
851 vhost_poll_queue(&vq->poll);
8241a1e4 852 goto out;
8dd014ad
DS
853 }
854 }
8241a1e4 855 vhost_net_enable_vq(net, vq);
2e26af79 856out:
8dd014ad 857 mutex_unlock(&vq->mutex);
8dd014ad
DS
858}
859
c23f3445 860static void handle_tx_kick(struct vhost_work *work)
3a4d5c94 861{
c23f3445
TH
862 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
863 poll.work);
864 struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
865
3a4d5c94
MT
866 handle_tx(net);
867}
868
c23f3445 869static void handle_rx_kick(struct vhost_work *work)
3a4d5c94 870{
c23f3445
TH
871 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
872 poll.work);
873 struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
874
3a4d5c94
MT
875 handle_rx(net);
876}
877
c23f3445 878static void handle_tx_net(struct vhost_work *work)
3a4d5c94 879{
c23f3445
TH
880 struct vhost_net *net = container_of(work, struct vhost_net,
881 poll[VHOST_NET_VQ_TX].work);
3a4d5c94
MT
882 handle_tx(net);
883}
884
c23f3445 885static void handle_rx_net(struct vhost_work *work)
3a4d5c94 886{
c23f3445
TH
887 struct vhost_net *net = container_of(work, struct vhost_net,
888 poll[VHOST_NET_VQ_RX].work);
3a4d5c94
MT
889 handle_rx(net);
890}
891
892static int vhost_net_open(struct inode *inode, struct file *f)
893{
23cc5a99 894 struct vhost_net *n;
c23f3445 895 struct vhost_dev *dev;
3ab2e420 896 struct vhost_virtqueue **vqs;
c67df11f 897 struct sk_buff **queue;
59566b6e 898 int i;
c23f3445 899
6c5ab651
MH
900 n = kvmalloc(sizeof *n, GFP_KERNEL | __GFP_REPEAT);
901 if (!n)
902 return -ENOMEM;
3ab2e420
AH
903 vqs = kmalloc(VHOST_NET_VQ_MAX * sizeof(*vqs), GFP_KERNEL);
904 if (!vqs) {
d04257b0 905 kvfree(n);
3ab2e420
AH
906 return -ENOMEM;
907 }
c23f3445 908
c67df11f
JW
909 queue = kmalloc_array(VHOST_RX_BATCH, sizeof(struct sk_buff *),
910 GFP_KERNEL);
911 if (!queue) {
912 kfree(vqs);
913 kvfree(n);
914 return -ENOMEM;
915 }
916 n->vqs[VHOST_NET_VQ_RX].rxq.queue = queue;
917
c23f3445 918 dev = &n->dev;
3ab2e420
AH
919 vqs[VHOST_NET_VQ_TX] = &n->vqs[VHOST_NET_VQ_TX].vq;
920 vqs[VHOST_NET_VQ_RX] = &n->vqs[VHOST_NET_VQ_RX].vq;
921 n->vqs[VHOST_NET_VQ_TX].vq.handle_kick = handle_tx_kick;
922 n->vqs[VHOST_NET_VQ_RX].vq.handle_kick = handle_rx_kick;
2839400f
AH
923 for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
924 n->vqs[i].ubufs = NULL;
925 n->vqs[i].ubuf_info = NULL;
926 n->vqs[i].upend_idx = 0;
927 n->vqs[i].done_idx = 0;
81f95a55
MT
928 n->vqs[i].vhost_hlen = 0;
929 n->vqs[i].sock_hlen = 0;
c67df11f 930 vhost_net_buf_init(&n->vqs[i].rxq);
2839400f 931 }
59566b6e 932 vhost_dev_init(dev, vqs, VHOST_NET_VQ_MAX);
3a4d5c94 933
c23f3445
TH
934 vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, POLLOUT, dev);
935 vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, POLLIN, dev);
3a4d5c94
MT
936
937 f->private_data = n;
938
939 return 0;
940}
941
3a4d5c94
MT
942static struct socket *vhost_net_stop_vq(struct vhost_net *n,
943 struct vhost_virtqueue *vq)
944{
945 struct socket *sock;
c67df11f
JW
946 struct vhost_net_virtqueue *nvq =
947 container_of(vq, struct vhost_net_virtqueue, vq);
3a4d5c94
MT
948
949 mutex_lock(&vq->mutex);
22fa90c7 950 sock = vq->private_data;
3a4d5c94 951 vhost_net_disable_vq(n, vq);
22fa90c7 952 vq->private_data = NULL;
c67df11f 953 vhost_net_buf_unproduce(nvq);
3a4d5c94
MT
954 mutex_unlock(&vq->mutex);
955 return sock;
956}
957
958static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock,
959 struct socket **rx_sock)
960{
3ab2e420
AH
961 *tx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_TX].vq);
962 *rx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_RX].vq);
3a4d5c94
MT
963}
964
965static void vhost_net_flush_vq(struct vhost_net *n, int index)
966{
967 vhost_poll_flush(n->poll + index);
3ab2e420 968 vhost_poll_flush(&n->vqs[index].vq.poll);
3a4d5c94
MT
969}
970
971static void vhost_net_flush(struct vhost_net *n)
972{
973 vhost_net_flush_vq(n, VHOST_NET_VQ_TX);
974 vhost_net_flush_vq(n, VHOST_NET_VQ_RX);
2839400f 975 if (n->vqs[VHOST_NET_VQ_TX].ubufs) {
3ab2e420 976 mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
1280c27f 977 n->tx_flush = true;
3ab2e420 978 mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
1280c27f 979 /* Wait for all lower device DMAs done. */
fe729a57 980 vhost_net_ubuf_put_and_wait(n->vqs[VHOST_NET_VQ_TX].ubufs);
3ab2e420 981 mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
1280c27f 982 n->tx_flush = false;
0ad8b480 983 atomic_set(&n->vqs[VHOST_NET_VQ_TX].ubufs->refcount, 1);
3ab2e420 984 mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
1280c27f 985 }
3a4d5c94
MT
986}
987
988static int vhost_net_release(struct inode *inode, struct file *f)
989{
990 struct vhost_net *n = f->private_data;
991 struct socket *tx_sock;
992 struct socket *rx_sock;
993
994 vhost_net_stop(n, &tx_sock, &rx_sock);
995 vhost_net_flush(n);
b211616d 996 vhost_dev_stop(&n->dev);
ea5d4046 997 vhost_dev_cleanup(&n->dev, false);
81f95a55 998 vhost_net_vq_reset(n);
3a4d5c94 999 if (tx_sock)
09aaacf0 1000 sockfd_put(tx_sock);
3a4d5c94 1001 if (rx_sock)
09aaacf0 1002 sockfd_put(rx_sock);
b0c057ca
MT
1003 /* Make sure no callbacks are outstanding */
1004 synchronize_rcu_bh();
3a4d5c94
MT
1005 /* We do an extra flush before freeing memory,
1006 * since jobs can re-queue themselves. */
1007 vhost_net_flush(n);
c67df11f 1008 kfree(n->vqs[VHOST_NET_VQ_RX].rxq.queue);
3ab2e420 1009 kfree(n->dev.vqs);
d04257b0 1010 kvfree(n);
3a4d5c94
MT
1011 return 0;
1012}
1013
1014static struct socket *get_raw_socket(int fd)
1015{
1016 struct {
1017 struct sockaddr_ll sa;
1018 char buf[MAX_ADDR_LEN];
1019 } uaddr;
1020 int uaddr_len = sizeof uaddr, r;
1021 struct socket *sock = sockfd_lookup(fd, &r);
d47effe1 1022
3a4d5c94
MT
1023 if (!sock)
1024 return ERR_PTR(-ENOTSOCK);
1025
1026 /* Parameter checking */
1027 if (sock->sk->sk_type != SOCK_RAW) {
1028 r = -ESOCKTNOSUPPORT;
1029 goto err;
1030 }
1031
1032 r = sock->ops->getname(sock, (struct sockaddr *)&uaddr.sa,
1033 &uaddr_len, 0);
1034 if (r)
1035 goto err;
1036
1037 if (uaddr.sa.sll_family != AF_PACKET) {
1038 r = -EPFNOSUPPORT;
1039 goto err;
1040 }
1041 return sock;
1042err:
09aaacf0 1043 sockfd_put(sock);
3a4d5c94
MT
1044 return ERR_PTR(r);
1045}
1046
c67df11f
JW
1047static struct skb_array *get_tap_skb_array(int fd)
1048{
1049 struct skb_array *array;
1050 struct file *file = fget(fd);
1051
1052 if (!file)
1053 return NULL;
1054 array = tun_get_skb_array(file);
1055 if (!IS_ERR(array))
1056 goto out;
1057 array = tap_get_skb_array(file);
1058 if (!IS_ERR(array))
1059 goto out;
1060 array = NULL;
1061out:
1062 fput(file);
1063 return array;
1064}
1065
501c774c 1066static struct socket *get_tap_socket(int fd)
3a4d5c94
MT
1067{
1068 struct file *file = fget(fd);
1069 struct socket *sock;
d47effe1 1070
3a4d5c94
MT
1071 if (!file)
1072 return ERR_PTR(-EBADF);
1073 sock = tun_get_socket(file);
501c774c
AB
1074 if (!IS_ERR(sock))
1075 return sock;
635b8c8e 1076 sock = tap_get_socket(file);
3a4d5c94
MT
1077 if (IS_ERR(sock))
1078 fput(file);
1079 return sock;
1080}
1081
1082static struct socket *get_socket(int fd)
1083{
1084 struct socket *sock;
d47effe1 1085
3a4d5c94
MT
1086 /* special case to disable backend */
1087 if (fd == -1)
1088 return NULL;
1089 sock = get_raw_socket(fd);
1090 if (!IS_ERR(sock))
1091 return sock;
501c774c 1092 sock = get_tap_socket(fd);
3a4d5c94
MT
1093 if (!IS_ERR(sock))
1094 return sock;
1095 return ERR_PTR(-ENOTSOCK);
1096}
1097
1098static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
1099{
1100 struct socket *sock, *oldsock;
1101 struct vhost_virtqueue *vq;
2839400f 1102 struct vhost_net_virtqueue *nvq;
fe729a57 1103 struct vhost_net_ubuf_ref *ubufs, *oldubufs = NULL;
3a4d5c94
MT
1104 int r;
1105
1106 mutex_lock(&n->dev.mutex);
1107 r = vhost_dev_check_owner(&n->dev);
1108 if (r)
1109 goto err;
1110
1111 if (index >= VHOST_NET_VQ_MAX) {
1112 r = -ENOBUFS;
1113 goto err;
1114 }
3ab2e420 1115 vq = &n->vqs[index].vq;
2839400f 1116 nvq = &n->vqs[index];
3a4d5c94
MT
1117 mutex_lock(&vq->mutex);
1118
1119 /* Verify that ring has been setup correctly. */
1120 if (!vhost_vq_access_ok(vq)) {
1121 r = -EFAULT;
1dace8c8 1122 goto err_vq;
3a4d5c94
MT
1123 }
1124 sock = get_socket(fd);
1125 if (IS_ERR(sock)) {
1126 r = PTR_ERR(sock);
1dace8c8 1127 goto err_vq;
3a4d5c94
MT
1128 }
1129
1130 /* start polling new socket */
22fa90c7 1131 oldsock = vq->private_data;
11fe8839 1132 if (sock != oldsock) {
fe729a57
AH
1133 ubufs = vhost_net_ubuf_alloc(vq,
1134 sock && vhost_sock_zcopy(sock));
bab632d6
MT
1135 if (IS_ERR(ubufs)) {
1136 r = PTR_ERR(ubufs);
1137 goto err_ubufs;
1138 }
692a998b 1139
d47effe1 1140 vhost_net_disable_vq(n, vq);
22fa90c7 1141 vq->private_data = sock;
c67df11f
JW
1142 vhost_net_buf_unproduce(nvq);
1143 if (index == VHOST_NET_VQ_RX)
1144 nvq->rx_array = get_tap_skb_array(fd);
80f7d030 1145 r = vhost_vq_init_access(vq);
f59281da 1146 if (r)
692a998b 1147 goto err_used;
2b8b328b
JW
1148 r = vhost_net_enable_vq(n, vq);
1149 if (r)
1150 goto err_used;
692a998b 1151
2839400f
AH
1152 oldubufs = nvq->ubufs;
1153 nvq->ubufs = ubufs;
64e9a9b8
MT
1154
1155 n->tx_packets = 0;
1156 n->tx_zcopy_err = 0;
1280c27f 1157 n->tx_flush = false;
dd1f4078 1158 }
3a4d5c94 1159
1680e906
MT
1160 mutex_unlock(&vq->mutex);
1161
c047e5f3 1162 if (oldubufs) {
c38e39c3 1163 vhost_net_ubuf_put_wait_and_free(oldubufs);
c047e5f3 1164 mutex_lock(&vq->mutex);
eaae8132 1165 vhost_zerocopy_signal_used(n, vq);
c047e5f3
MT
1166 mutex_unlock(&vq->mutex);
1167 }
bab632d6 1168
3a4d5c94
MT
1169 if (oldsock) {
1170 vhost_net_flush_vq(n, index);
09aaacf0 1171 sockfd_put(oldsock);
3a4d5c94 1172 }
1dace8c8 1173
1680e906
MT
1174 mutex_unlock(&n->dev.mutex);
1175 return 0;
1176
692a998b 1177err_used:
22fa90c7 1178 vq->private_data = oldsock;
692a998b
JW
1179 vhost_net_enable_vq(n, vq);
1180 if (ubufs)
c38e39c3 1181 vhost_net_ubuf_put_wait_and_free(ubufs);
bab632d6 1182err_ubufs:
09aaacf0 1183 sockfd_put(sock);
1dace8c8
JD
1184err_vq:
1185 mutex_unlock(&vq->mutex);
3a4d5c94
MT
1186err:
1187 mutex_unlock(&n->dev.mutex);
1188 return r;
1189}
1190
1191static long vhost_net_reset_owner(struct vhost_net *n)
1192{
1193 struct socket *tx_sock = NULL;
1194 struct socket *rx_sock = NULL;
1195 long err;
a9709d68 1196 struct vhost_umem *umem;
d47effe1 1197
3a4d5c94
MT
1198 mutex_lock(&n->dev.mutex);
1199 err = vhost_dev_check_owner(&n->dev);
1200 if (err)
1201 goto done;
a9709d68
JW
1202 umem = vhost_dev_reset_owner_prepare();
1203 if (!umem) {
150b9e51
MT
1204 err = -ENOMEM;
1205 goto done;
1206 }
3a4d5c94
MT
1207 vhost_net_stop(n, &tx_sock, &rx_sock);
1208 vhost_net_flush(n);
a9709d68 1209 vhost_dev_reset_owner(&n->dev, umem);
81f95a55 1210 vhost_net_vq_reset(n);
3a4d5c94
MT
1211done:
1212 mutex_unlock(&n->dev.mutex);
1213 if (tx_sock)
09aaacf0 1214 sockfd_put(tx_sock);
3a4d5c94 1215 if (rx_sock)
09aaacf0 1216 sockfd_put(rx_sock);
3a4d5c94
MT
1217 return err;
1218}
1219
1220static int vhost_net_set_features(struct vhost_net *n, u64 features)
1221{
8dd014ad 1222 size_t vhost_hlen, sock_hlen, hdr_len;
3a4d5c94 1223 int i;
8dd014ad 1224
e4fca7d6
MT
1225 hdr_len = (features & ((1ULL << VIRTIO_NET_F_MRG_RXBUF) |
1226 (1ULL << VIRTIO_F_VERSION_1))) ?
8dd014ad
DS
1227 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
1228 sizeof(struct virtio_net_hdr);
1229 if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) {
1230 /* vhost provides vnet_hdr */
1231 vhost_hlen = hdr_len;
1232 sock_hlen = 0;
1233 } else {
1234 /* socket provides vnet_hdr */
1235 vhost_hlen = 0;
1236 sock_hlen = hdr_len;
1237 }
3a4d5c94
MT
1238 mutex_lock(&n->dev.mutex);
1239 if ((features & (1 << VHOST_F_LOG_ALL)) &&
6b1e6cc7
JW
1240 !vhost_log_access_ok(&n->dev))
1241 goto out_unlock;
1242
1243 if ((features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))) {
1244 if (vhost_init_device_iotlb(&n->dev, true))
1245 goto out_unlock;
3a4d5c94 1246 }
6b1e6cc7 1247
3a4d5c94 1248 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
3ab2e420 1249 mutex_lock(&n->vqs[i].vq.mutex);
ea16c514 1250 n->vqs[i].vq.acked_features = features;
81f95a55
MT
1251 n->vqs[i].vhost_hlen = vhost_hlen;
1252 n->vqs[i].sock_hlen = sock_hlen;
3ab2e420 1253 mutex_unlock(&n->vqs[i].vq.mutex);
3a4d5c94 1254 }
3a4d5c94
MT
1255 mutex_unlock(&n->dev.mutex);
1256 return 0;
6b1e6cc7
JW
1257
1258out_unlock:
1259 mutex_unlock(&n->dev.mutex);
1260 return -EFAULT;
3a4d5c94
MT
1261}
1262
b1ad8496
AH
1263static long vhost_net_set_owner(struct vhost_net *n)
1264{
1265 int r;
1266
1267 mutex_lock(&n->dev.mutex);
05c05351
MT
1268 if (vhost_dev_has_owner(&n->dev)) {
1269 r = -EBUSY;
1270 goto out;
1271 }
b1ad8496
AH
1272 r = vhost_net_set_ubuf_info(n);
1273 if (r)
1274 goto out;
1275 r = vhost_dev_set_owner(&n->dev);
1276 if (r)
1277 vhost_net_clear_ubuf_info(n);
1278 vhost_net_flush(n);
1279out:
1280 mutex_unlock(&n->dev.mutex);
1281 return r;
1282}
1283
3a4d5c94
MT
1284static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
1285 unsigned long arg)
1286{
1287 struct vhost_net *n = f->private_data;
1288 void __user *argp = (void __user *)arg;
1289 u64 __user *featurep = argp;
1290 struct vhost_vring_file backend;
1291 u64 features;
1292 int r;
d47effe1 1293
3a4d5c94
MT
1294 switch (ioctl) {
1295 case VHOST_NET_SET_BACKEND:
d3553a52
TY
1296 if (copy_from_user(&backend, argp, sizeof backend))
1297 return -EFAULT;
3a4d5c94
MT
1298 return vhost_net_set_backend(n, backend.index, backend.fd);
1299 case VHOST_GET_FEATURES:
0dd05a3b 1300 features = VHOST_NET_FEATURES;
d3553a52
TY
1301 if (copy_to_user(featurep, &features, sizeof features))
1302 return -EFAULT;
1303 return 0;
3a4d5c94 1304 case VHOST_SET_FEATURES:
d3553a52
TY
1305 if (copy_from_user(&features, featurep, sizeof features))
1306 return -EFAULT;
0dd05a3b 1307 if (features & ~VHOST_NET_FEATURES)
3a4d5c94
MT
1308 return -EOPNOTSUPP;
1309 return vhost_net_set_features(n, features);
1310 case VHOST_RESET_OWNER:
1311 return vhost_net_reset_owner(n);
b1ad8496
AH
1312 case VHOST_SET_OWNER:
1313 return vhost_net_set_owner(n);
3a4d5c94
MT
1314 default:
1315 mutex_lock(&n->dev.mutex);
935cdee7
MT
1316 r = vhost_dev_ioctl(&n->dev, ioctl, argp);
1317 if (r == -ENOIOCTLCMD)
1318 r = vhost_vring_ioctl(&n->dev, ioctl, argp);
1319 else
1320 vhost_net_flush(n);
3a4d5c94
MT
1321 mutex_unlock(&n->dev.mutex);
1322 return r;
1323 }
1324}
1325
1326#ifdef CONFIG_COMPAT
1327static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl,
1328 unsigned long arg)
1329{
1330 return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
1331}
1332#endif
1333
6b1e6cc7
JW
1334static ssize_t vhost_net_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
1335{
1336 struct file *file = iocb->ki_filp;
1337 struct vhost_net *n = file->private_data;
1338 struct vhost_dev *dev = &n->dev;
1339 int noblock = file->f_flags & O_NONBLOCK;
1340
1341 return vhost_chr_read_iter(dev, to, noblock);
1342}
1343
1344static ssize_t vhost_net_chr_write_iter(struct kiocb *iocb,
1345 struct iov_iter *from)
1346{
1347 struct file *file = iocb->ki_filp;
1348 struct vhost_net *n = file->private_data;
1349 struct vhost_dev *dev = &n->dev;
1350
1351 return vhost_chr_write_iter(dev, from);
1352}
1353
1354static unsigned int vhost_net_chr_poll(struct file *file, poll_table *wait)
1355{
1356 struct vhost_net *n = file->private_data;
1357 struct vhost_dev *dev = &n->dev;
1358
1359 return vhost_chr_poll(file, dev, wait);
1360}
1361
373a83a6 1362static const struct file_operations vhost_net_fops = {
3a4d5c94
MT
1363 .owner = THIS_MODULE,
1364 .release = vhost_net_release,
6b1e6cc7
JW
1365 .read_iter = vhost_net_chr_read_iter,
1366 .write_iter = vhost_net_chr_write_iter,
1367 .poll = vhost_net_chr_poll,
3a4d5c94
MT
1368 .unlocked_ioctl = vhost_net_ioctl,
1369#ifdef CONFIG_COMPAT
1370 .compat_ioctl = vhost_net_compat_ioctl,
1371#endif
1372 .open = vhost_net_open,
6038f373 1373 .llseek = noop_llseek,
3a4d5c94
MT
1374};
1375
1376static struct miscdevice vhost_net_misc = {
7c7c7f01 1377 .minor = VHOST_NET_MINOR,
1378 .name = "vhost-net",
1379 .fops = &vhost_net_fops,
3a4d5c94
MT
1380};
1381
a8d3782f 1382static int vhost_net_init(void)
3a4d5c94 1383{
bab632d6 1384 if (experimental_zcopytx)
fe729a57 1385 vhost_net_enable_zcopy(VHOST_NET_VQ_TX);
c23f3445 1386 return misc_register(&vhost_net_misc);
3a4d5c94
MT
1387}
1388module_init(vhost_net_init);
1389
a8d3782f 1390static void vhost_net_exit(void)
3a4d5c94
MT
1391{
1392 misc_deregister(&vhost_net_misc);
3a4d5c94
MT
1393}
1394module_exit(vhost_net_exit);
1395
1396MODULE_VERSION("0.0.1");
1397MODULE_LICENSE("GPL v2");
1398MODULE_AUTHOR("Michael S. Tsirkin");
1399MODULE_DESCRIPTION("Host kernel accelerator for virtio net");
7c7c7f01 1400MODULE_ALIAS_MISCDEV(VHOST_NET_MINOR);
1401MODULE_ALIAS("devname:vhost-net");