vhost: introduce vhost_exceeds_weight()
[linux-2.6-block.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>
1ffcbc85 35#include <net/xdp.h>
3a4d5c94
MT
36
37#include "vhost.h"
38
f9611c43 39static int experimental_zcopytx = 1;
bab632d6 40module_param(experimental_zcopytx, int, 0444);
f9611c43
MT
41MODULE_PARM_DESC(experimental_zcopytx, "Enable Zero Copy TX;"
42 " 1 -Enable; 0 - Disable");
bab632d6 43
3a4d5c94
MT
44/* Max number of bytes transferred before requeueing the job.
45 * Using this limit prevents one virtqueue from starving others. */
46#define VHOST_NET_WEIGHT 0x80000
47
a2ac9990 48/* Max number of packets transferred before requeueing the job.
db688c24
PA
49 * Using this limit prevents one virtqueue from starving others with small
50 * pkts.
51 */
52#define VHOST_NET_PKT_WEIGHT 256
a2ac9990 53
bab632d6
MT
54/* MAX number of TX used buffers for outstanding zerocopy */
55#define VHOST_MAX_PEND 128
56#define VHOST_GOODCOPY_LEN 256
57
eaae8132
MT
58/*
59 * For transmit, used buffer len is unused; we override it to track buffer
60 * status internally; used for zerocopy tx only.
61 */
62/* Lower device DMA failed */
bf995734 63#define VHOST_DMA_FAILED_LEN ((__force __virtio32)3)
eaae8132 64/* Lower device DMA done */
bf995734 65#define VHOST_DMA_DONE_LEN ((__force __virtio32)2)
eaae8132 66/* Lower device DMA in progress */
bf995734 67#define VHOST_DMA_IN_PROGRESS ((__force __virtio32)1)
eaae8132 68/* Buffer unused */
bf995734 69#define VHOST_DMA_CLEAR_LEN ((__force __virtio32)0)
eaae8132 70
bf995734 71#define VHOST_DMA_IS_DONE(len) ((__force u32)(len) >= (__force u32)VHOST_DMA_DONE_LEN)
eaae8132 72
8570a6e7
AH
73enum {
74 VHOST_NET_FEATURES = VHOST_FEATURES |
75 (1ULL << VHOST_NET_F_VIRTIO_NET_HDR) |
6b1e6cc7
JW
76 (1ULL << VIRTIO_NET_F_MRG_RXBUF) |
77 (1ULL << VIRTIO_F_IOMMU_PLATFORM)
8570a6e7
AH
78};
79
429711ae
JW
80enum {
81 VHOST_NET_BACKEND_FEATURES = (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2)
82};
83
3a4d5c94
MT
84enum {
85 VHOST_NET_VQ_RX = 0,
86 VHOST_NET_VQ_TX = 1,
87 VHOST_NET_VQ_MAX = 2,
88};
89
fe729a57 90struct vhost_net_ubuf_ref {
0ad8b480
MT
91 /* refcount follows semantics similar to kref:
92 * 0: object is released
93 * 1: no outstanding ubufs
94 * >1: outstanding ubufs
95 */
96 atomic_t refcount;
2839400f
AH
97 wait_queue_head_t wait;
98 struct vhost_virtqueue *vq;
99};
100
d0d86971 101#define VHOST_NET_BATCH 64
c67df11f 102struct vhost_net_buf {
5990a305 103 void **queue;
c67df11f
JW
104 int tail;
105 int head;
106};
107
3ab2e420
AH
108struct vhost_net_virtqueue {
109 struct vhost_virtqueue vq;
81f95a55
MT
110 size_t vhost_hlen;
111 size_t sock_hlen;
2839400f
AH
112 /* vhost zerocopy support fields below: */
113 /* last used idx for outstanding DMA zerocopy buffers */
114 int upend_idx;
f5a4941a
JW
115 /* For TX, first used idx for DMA done zerocopy buffers
116 * For RX, number of batched heads
117 */
2839400f 118 int done_idx;
0a0be13b
JW
119 /* Number of XDP frames batched */
120 int batched_xdp;
2839400f
AH
121 /* an array of userspace buffers info */
122 struct ubuf_info *ubuf_info;
123 /* Reference counting for outstanding ubufs.
124 * Protected by vq mutex. Writers must also take device mutex. */
fe729a57 125 struct vhost_net_ubuf_ref *ubufs;
5990a305 126 struct ptr_ring *rx_ring;
c67df11f 127 struct vhost_net_buf rxq;
0a0be13b
JW
128 /* Batched XDP buffs */
129 struct xdp_buff *xdp;
3ab2e420
AH
130};
131
3a4d5c94
MT
132struct vhost_net {
133 struct vhost_dev dev;
3ab2e420 134 struct vhost_net_virtqueue vqs[VHOST_NET_VQ_MAX];
3a4d5c94 135 struct vhost_poll poll[VHOST_NET_VQ_MAX];
eaae8132
MT
136 /* Number of TX recently submitted.
137 * Protected by tx vq lock. */
138 unsigned tx_packets;
139 /* Number of times zerocopy TX recently failed.
140 * Protected by tx vq lock. */
141 unsigned tx_zcopy_err;
1280c27f
MT
142 /* Flush in progress. Protected by tx vq lock. */
143 bool tx_flush;
e4dab1e6
JW
144 /* Private page frag */
145 struct page_frag page_frag;
146 /* Refcount bias of page frag */
147 int refcnt_bias;
3a4d5c94
MT
148};
149
fe729a57 150static unsigned vhost_net_zcopy_mask __read_mostly;
2839400f 151
c67df11f
JW
152static void *vhost_net_buf_get_ptr(struct vhost_net_buf *rxq)
153{
154 if (rxq->tail != rxq->head)
155 return rxq->queue[rxq->head];
156 else
157 return NULL;
158}
159
160static int vhost_net_buf_get_size(struct vhost_net_buf *rxq)
161{
162 return rxq->tail - rxq->head;
163}
164
165static int vhost_net_buf_is_empty(struct vhost_net_buf *rxq)
166{
167 return rxq->tail == rxq->head;
168}
169
170static void *vhost_net_buf_consume(struct vhost_net_buf *rxq)
171{
172 void *ret = vhost_net_buf_get_ptr(rxq);
173 ++rxq->head;
174 return ret;
175}
176
177static int vhost_net_buf_produce(struct vhost_net_virtqueue *nvq)
178{
179 struct vhost_net_buf *rxq = &nvq->rxq;
180
181 rxq->head = 0;
5990a305 182 rxq->tail = ptr_ring_consume_batched(nvq->rx_ring, rxq->queue,
d0d86971 183 VHOST_NET_BATCH);
c67df11f
JW
184 return rxq->tail;
185}
186
187static void vhost_net_buf_unproduce(struct vhost_net_virtqueue *nvq)
188{
189 struct vhost_net_buf *rxq = &nvq->rxq;
190
5990a305
JW
191 if (nvq->rx_ring && !vhost_net_buf_is_empty(rxq)) {
192 ptr_ring_unconsume(nvq->rx_ring, rxq->queue + rxq->head,
193 vhost_net_buf_get_size(rxq),
3a403076 194 tun_ptr_free);
c67df11f
JW
195 rxq->head = rxq->tail = 0;
196 }
197}
198
fc72d1d5
JW
199static int vhost_net_buf_peek_len(void *ptr)
200{
1ffcbc85
JDB
201 if (tun_is_xdp_frame(ptr)) {
202 struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr);
fc72d1d5 203
1ffcbc85 204 return xdpf->len;
fc72d1d5
JW
205 }
206
207 return __skb_array_len_with_tag(ptr);
208}
209
c67df11f
JW
210static int vhost_net_buf_peek(struct vhost_net_virtqueue *nvq)
211{
212 struct vhost_net_buf *rxq = &nvq->rxq;
213
214 if (!vhost_net_buf_is_empty(rxq))
215 goto out;
216
217 if (!vhost_net_buf_produce(nvq))
218 return 0;
219
220out:
fc72d1d5 221 return vhost_net_buf_peek_len(vhost_net_buf_get_ptr(rxq));
c67df11f
JW
222}
223
224static void vhost_net_buf_init(struct vhost_net_buf *rxq)
225{
226 rxq->head = rxq->tail = 0;
227}
228
fe729a57 229static void vhost_net_enable_zcopy(int vq)
2839400f 230{
fe729a57 231 vhost_net_zcopy_mask |= 0x1 << vq;
2839400f
AH
232}
233
fe729a57
AH
234static struct vhost_net_ubuf_ref *
235vhost_net_ubuf_alloc(struct vhost_virtqueue *vq, bool zcopy)
2839400f 236{
fe729a57 237 struct vhost_net_ubuf_ref *ubufs;
2839400f
AH
238 /* No zero copy backend? Nothing to count. */
239 if (!zcopy)
240 return NULL;
241 ubufs = kmalloc(sizeof(*ubufs), GFP_KERNEL);
242 if (!ubufs)
243 return ERR_PTR(-ENOMEM);
0ad8b480 244 atomic_set(&ubufs->refcount, 1);
2839400f
AH
245 init_waitqueue_head(&ubufs->wait);
246 ubufs->vq = vq;
247 return ubufs;
248}
249
0ad8b480 250static int vhost_net_ubuf_put(struct vhost_net_ubuf_ref *ubufs)
2839400f 251{
0ad8b480
MT
252 int r = atomic_sub_return(1, &ubufs->refcount);
253 if (unlikely(!r))
254 wake_up(&ubufs->wait);
255 return r;
2839400f
AH
256}
257
fe729a57 258static void vhost_net_ubuf_put_and_wait(struct vhost_net_ubuf_ref *ubufs)
2839400f 259{
0ad8b480
MT
260 vhost_net_ubuf_put(ubufs);
261 wait_event(ubufs->wait, !atomic_read(&ubufs->refcount));
c38e39c3
MT
262}
263
264static void vhost_net_ubuf_put_wait_and_free(struct vhost_net_ubuf_ref *ubufs)
265{
266 vhost_net_ubuf_put_and_wait(ubufs);
2839400f
AH
267 kfree(ubufs);
268}
269
b1ad8496
AH
270static void vhost_net_clear_ubuf_info(struct vhost_net *n)
271{
b1ad8496
AH
272 int i;
273
288cfe78
MT
274 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
275 kfree(n->vqs[i].ubuf_info);
276 n->vqs[i].ubuf_info = NULL;
b1ad8496
AH
277 }
278}
279
0a1febf7 280static int vhost_net_set_ubuf_info(struct vhost_net *n)
2839400f
AH
281{
282 bool zcopy;
283 int i;
284
288cfe78 285 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
fe729a57 286 zcopy = vhost_net_zcopy_mask & (0x1 << i);
2839400f
AH
287 if (!zcopy)
288 continue;
6da2ec56
KC
289 n->vqs[i].ubuf_info =
290 kmalloc_array(UIO_MAXIOV,
291 sizeof(*n->vqs[i].ubuf_info),
292 GFP_KERNEL);
2839400f
AH
293 if (!n->vqs[i].ubuf_info)
294 goto err;
295 }
296 return 0;
297
298err:
288cfe78 299 vhost_net_clear_ubuf_info(n);
2839400f
AH
300 return -ENOMEM;
301}
302
0a1febf7 303static void vhost_net_vq_reset(struct vhost_net *n)
2839400f
AH
304{
305 int i;
306
288cfe78
MT
307 vhost_net_clear_ubuf_info(n);
308
2839400f
AH
309 for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
310 n->vqs[i].done_idx = 0;
311 n->vqs[i].upend_idx = 0;
312 n->vqs[i].ubufs = NULL;
81f95a55
MT
313 n->vqs[i].vhost_hlen = 0;
314 n->vqs[i].sock_hlen = 0;
c67df11f 315 vhost_net_buf_init(&n->vqs[i].rxq);
2839400f
AH
316 }
317
318}
319
eaae8132
MT
320static void vhost_net_tx_packet(struct vhost_net *net)
321{
322 ++net->tx_packets;
323 if (net->tx_packets < 1024)
324 return;
325 net->tx_packets = 0;
326 net->tx_zcopy_err = 0;
327}
328
329static void vhost_net_tx_err(struct vhost_net *net)
330{
331 ++net->tx_zcopy_err;
332}
333
334static bool vhost_net_tx_select_zcopy(struct vhost_net *net)
335{
1280c27f
MT
336 /* TX flush waits for outstanding DMAs to be done.
337 * Don't start new DMAs.
338 */
339 return !net->tx_flush &&
340 net->tx_packets / 64 >= net->tx_zcopy_err;
eaae8132
MT
341}
342
bab632d6
MT
343static bool vhost_sock_zcopy(struct socket *sock)
344{
345 return unlikely(experimental_zcopytx) &&
346 sock_flag(sock->sk, SOCK_ZEROCOPY);
347}
348
0a0be13b
JW
349static bool vhost_sock_xdp(struct socket *sock)
350{
351 return sock_flag(sock->sk, SOCK_XDP);
352}
353
b211616d
MT
354/* In case of DMA done not in order in lower device driver for some reason.
355 * upend_idx is used to track end of used idx, done_idx is used to track head
356 * of used idx. Once lower device DMA done contiguously, we will signal KVM
357 * guest used idx.
358 */
094afe7d
JW
359static void vhost_zerocopy_signal_used(struct vhost_net *net,
360 struct vhost_virtqueue *vq)
b211616d 361{
2839400f
AH
362 struct vhost_net_virtqueue *nvq =
363 container_of(vq, struct vhost_net_virtqueue, vq);
c92112ae 364 int i, add;
b211616d
MT
365 int j = 0;
366
2839400f 367 for (i = nvq->done_idx; i != nvq->upend_idx; i = (i + 1) % UIO_MAXIOV) {
eaae8132
MT
368 if (vq->heads[i].len == VHOST_DMA_FAILED_LEN)
369 vhost_net_tx_err(net);
b211616d
MT
370 if (VHOST_DMA_IS_DONE(vq->heads[i].len)) {
371 vq->heads[i].len = VHOST_DMA_CLEAR_LEN;
b211616d
MT
372 ++j;
373 } else
374 break;
375 }
c92112ae
JW
376 while (j) {
377 add = min(UIO_MAXIOV - nvq->done_idx, j);
378 vhost_add_used_and_signal_n(vq->dev, vq,
379 &vq->heads[nvq->done_idx], add);
380 nvq->done_idx = (nvq->done_idx + add) % UIO_MAXIOV;
381 j -= add;
382 }
b211616d
MT
383}
384
eaae8132 385static void vhost_zerocopy_callback(struct ubuf_info *ubuf, bool success)
b211616d 386{
fe729a57 387 struct vhost_net_ubuf_ref *ubufs = ubuf->ctx;
b211616d 388 struct vhost_virtqueue *vq = ubufs->vq;
0ad8b480 389 int cnt;
24eb21a1 390
b0c057ca
MT
391 rcu_read_lock_bh();
392
19c73b3e
JW
393 /* set len to mark this desc buffers done DMA */
394 vq->heads[ubuf->desc].len = success ?
395 VHOST_DMA_DONE_LEN : VHOST_DMA_FAILED_LEN;
0ad8b480 396 cnt = vhost_net_ubuf_put(ubufs);
19c73b3e 397
24eb21a1
MT
398 /*
399 * Trigger polling thread if guest stopped submitting new buffers:
0ad8b480 400 * in this case, the refcount after decrement will eventually reach 1.
24eb21a1
MT
401 * We also trigger polling periodically after each 16 packets
402 * (the value 16 here is more or less arbitrary, it's tuned to trigger
403 * less than 10% of times).
404 */
0ad8b480 405 if (cnt <= 1 || !(cnt % 16))
24eb21a1 406 vhost_poll_queue(&vq->poll);
b0c057ca
MT
407
408 rcu_read_unlock_bh();
b211616d
MT
409}
410
03088137
JW
411static inline unsigned long busy_clock(void)
412{
413 return local_clock() >> 10;
414}
415
027b1760 416static bool vhost_can_busy_poll(unsigned long endtime)
03088137 417{
027b1760
TM
418 return likely(!need_resched() && !time_after(busy_clock(), endtime) &&
419 !signal_pending(current));
03088137
JW
420}
421
8241a1e4
JW
422static void vhost_net_disable_vq(struct vhost_net *n,
423 struct vhost_virtqueue *vq)
424{
425 struct vhost_net_virtqueue *nvq =
426 container_of(vq, struct vhost_net_virtqueue, vq);
427 struct vhost_poll *poll = n->poll + (nvq - n->vqs);
428 if (!vq->private_data)
429 return;
430 vhost_poll_stop(poll);
431}
432
433static int vhost_net_enable_vq(struct vhost_net *n,
434 struct vhost_virtqueue *vq)
435{
436 struct vhost_net_virtqueue *nvq =
437 container_of(vq, struct vhost_net_virtqueue, vq);
438 struct vhost_poll *poll = n->poll + (nvq - n->vqs);
439 struct socket *sock;
440
441 sock = vq->private_data;
442 if (!sock)
443 return 0;
444
445 return vhost_poll_start(poll, sock->file);
446}
447
4afb52c2
JW
448static void vhost_net_signal_used(struct vhost_net_virtqueue *nvq)
449{
450 struct vhost_virtqueue *vq = &nvq->vq;
451 struct vhost_dev *dev = vq->dev;
452
453 if (!nvq->done_idx)
454 return;
455
456 vhost_add_used_and_signal_n(dev, vq, vq->heads, nvq->done_idx);
457 nvq->done_idx = 0;
458}
459
0a0be13b
JW
460static void vhost_tx_batch(struct vhost_net *net,
461 struct vhost_net_virtqueue *nvq,
462 struct socket *sock,
463 struct msghdr *msghdr)
464{
465 struct tun_msg_ctl ctl = {
466 .type = TUN_MSG_PTR,
467 .num = nvq->batched_xdp,
468 .ptr = nvq->xdp,
469 };
470 int err;
471
472 if (nvq->batched_xdp == 0)
473 goto signal_used;
474
475 msghdr->msg_control = &ctl;
476 err = sock->ops->sendmsg(sock, msghdr, 0);
477 if (unlikely(err < 0)) {
478 vq_err(&nvq->vq, "Fail to batch sending packets\n");
479 return;
480 }
481
482signal_used:
483 vhost_net_signal_used(nvq);
484 nvq->batched_xdp = 0;
485}
486
dc151282
TZ
487static int sock_has_rx_data(struct socket *sock)
488{
489 if (unlikely(!sock))
490 return 0;
491
492 if (sock->ops->peek_len)
493 return sock->ops->peek_len(sock);
494
495 return skb_queue_empty(&sock->sk->sk_receive_queue);
496}
497
498static void vhost_net_busy_poll_try_queue(struct vhost_net *net,
499 struct vhost_virtqueue *vq)
500{
501 if (!vhost_vq_avail_empty(&net->dev, vq)) {
502 vhost_poll_queue(&vq->poll);
503 } else if (unlikely(vhost_enable_notify(&net->dev, vq))) {
504 vhost_disable_notify(&net->dev, vq);
505 vhost_poll_queue(&vq->poll);
506 }
507}
508
509static void vhost_net_busy_poll(struct vhost_net *net,
510 struct vhost_virtqueue *rvq,
511 struct vhost_virtqueue *tvq,
512 bool *busyloop_intr,
513 bool poll_rx)
514{
515 unsigned long busyloop_timeout;
516 unsigned long endtime;
517 struct socket *sock;
518 struct vhost_virtqueue *vq = poll_rx ? tvq : rvq;
519
476e8ba7
JW
520 /* Try to hold the vq mutex of the paired virtqueue. We can't
521 * use mutex_lock() here since we could not guarantee a
522 * consistenet lock ordering.
523 */
524 if (!mutex_trylock(&vq->mutex))
525 return;
526
dc151282
TZ
527 vhost_disable_notify(&net->dev, vq);
528 sock = rvq->private_data;
529
530 busyloop_timeout = poll_rx ? rvq->busyloop_timeout:
531 tvq->busyloop_timeout;
532
533 preempt_disable();
534 endtime = busy_clock() + busyloop_timeout;
535
536 while (vhost_can_busy_poll(endtime)) {
537 if (vhost_has_work(&net->dev)) {
538 *busyloop_intr = true;
539 break;
540 }
541
542 if ((sock_has_rx_data(sock) &&
543 !vhost_vq_avail_empty(&net->dev, rvq)) ||
544 !vhost_vq_avail_empty(&net->dev, tvq))
545 break;
546
547 cpu_relax();
548 }
549
550 preempt_enable();
551
552 if (poll_rx || sock_has_rx_data(sock))
553 vhost_net_busy_poll_try_queue(net, vq);
554 else if (!poll_rx) /* On tx here, sock has no rx data. */
555 vhost_enable_notify(&net->dev, rvq);
556
557 mutex_unlock(&vq->mutex);
558}
559
03088137 560static int vhost_net_tx_get_vq_desc(struct vhost_net *net,
441abde4 561 struct vhost_net_virtqueue *tnvq,
027b1760 562 unsigned int *out_num, unsigned int *in_num,
0a0be13b 563 struct msghdr *msghdr, bool *busyloop_intr)
03088137 564{
441abde4
TZ
565 struct vhost_net_virtqueue *rnvq = &net->vqs[VHOST_NET_VQ_RX];
566 struct vhost_virtqueue *rvq = &rnvq->vq;
567 struct vhost_virtqueue *tvq = &tnvq->vq;
568
569 int r = vhost_get_vq_desc(tvq, tvq->iov, ARRAY_SIZE(tvq->iov),
6b1e6cc7 570 out_num, in_num, NULL, NULL);
03088137 571
441abde4 572 if (r == tvq->num && tvq->busyloop_timeout) {
0a0be13b 573 /* Flush batched packets first */
441abde4 574 if (!vhost_sock_zcopy(tvq->private_data))
441abde4
TZ
575 vhost_tx_batch(net, tnvq, tvq->private_data, msghdr);
576
577 vhost_net_busy_poll(net, rvq, tvq, busyloop_intr, false);
578
579 r = vhost_get_vq_desc(tvq, tvq->iov, ARRAY_SIZE(tvq->iov),
6b1e6cc7 580 out_num, in_num, NULL, NULL);
03088137
JW
581 }
582
583 return r;
584}
585
0ed005ce
JW
586static bool vhost_exceeds_maxpend(struct vhost_net *net)
587{
588 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
589 struct vhost_virtqueue *vq = &nvq->vq;
590
1e6f7453
WB
591 return (nvq->upend_idx + UIO_MAXIOV - nvq->done_idx) % UIO_MAXIOV >
592 min_t(unsigned int, VHOST_MAX_PEND, vq->num >> 2);
0ed005ce
JW
593}
594
b0d0ea50
JW
595static size_t init_iov_iter(struct vhost_virtqueue *vq, struct iov_iter *iter,
596 size_t hdr_size, int out)
597{
598 /* Skip header. TODO: support TSO. */
599 size_t len = iov_length(vq->iov, out);
600
601 iov_iter_init(iter, WRITE, vq->iov, out, len);
602 iov_iter_advance(iter, hdr_size);
603
604 return iov_iter_count(iter);
605}
606
a2a91a13
JW
607static int get_tx_bufs(struct vhost_net *net,
608 struct vhost_net_virtqueue *nvq,
609 struct msghdr *msg,
610 unsigned int *out, unsigned int *in,
611 size_t *len, bool *busyloop_intr)
612{
613 struct vhost_virtqueue *vq = &nvq->vq;
614 int ret;
615
0a0be13b 616 ret = vhost_net_tx_get_vq_desc(net, nvq, out, in, msg, busyloop_intr);
4afb52c2 617
a2a91a13
JW
618 if (ret < 0 || ret == vq->num)
619 return ret;
620
621 if (*in) {
622 vq_err(vq, "Unexpected descriptor format for TX: out %d, int %d\n",
623 *out, *in);
624 return -EFAULT;
625 }
626
627 /* Sanity check */
628 *len = init_iov_iter(vq, &msg->msg_iter, nvq->vhost_hlen, *out);
629 if (*len == 0) {
630 vq_err(vq, "Unexpected header len for TX: %zd expected %zd\n",
631 *len, nvq->vhost_hlen);
632 return -EFAULT;
633 }
634
635 return ret;
636}
637
c92a8a8c
JW
638static bool tx_can_batch(struct vhost_virtqueue *vq, size_t total_len)
639{
640 return total_len < VHOST_NET_WEIGHT &&
641 !vhost_vq_avail_empty(vq->dev, vq);
642}
643
e4dab1e6
JW
644#define SKB_FRAG_PAGE_ORDER get_order(32768)
645
646static bool vhost_net_page_frag_refill(struct vhost_net *net, unsigned int sz,
647 struct page_frag *pfrag, gfp_t gfp)
648{
649 if (pfrag->page) {
650 if (pfrag->offset + sz <= pfrag->size)
651 return true;
652 __page_frag_cache_drain(pfrag->page, net->refcnt_bias);
653 }
654
655 pfrag->offset = 0;
656 net->refcnt_bias = 0;
657 if (SKB_FRAG_PAGE_ORDER) {
658 /* Avoid direct reclaim but allow kswapd to wake */
659 pfrag->page = alloc_pages((gfp & ~__GFP_DIRECT_RECLAIM) |
660 __GFP_COMP | __GFP_NOWARN |
661 __GFP_NORETRY,
662 SKB_FRAG_PAGE_ORDER);
663 if (likely(pfrag->page)) {
664 pfrag->size = PAGE_SIZE << SKB_FRAG_PAGE_ORDER;
665 goto done;
666 }
667 }
668 pfrag->page = alloc_page(gfp);
669 if (likely(pfrag->page)) {
670 pfrag->size = PAGE_SIZE;
671 goto done;
672 }
673 return false;
674
675done:
676 net->refcnt_bias = USHRT_MAX;
677 page_ref_add(pfrag->page, USHRT_MAX - 1);
678 return true;
679}
680
0a0be13b
JW
681#define VHOST_NET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
682
683static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq,
684 struct iov_iter *from)
685{
686 struct vhost_virtqueue *vq = &nvq->vq;
e4dab1e6
JW
687 struct vhost_net *net = container_of(vq->dev, struct vhost_net,
688 dev);
0a0be13b 689 struct socket *sock = vq->private_data;
e4dab1e6 690 struct page_frag *alloc_frag = &net->page_frag;
0a0be13b
JW
691 struct virtio_net_hdr *gso;
692 struct xdp_buff *xdp = &nvq->xdp[nvq->batched_xdp];
693 struct tun_xdp_hdr *hdr;
694 size_t len = iov_iter_count(from);
695 int headroom = vhost_sock_xdp(sock) ? XDP_PACKET_HEADROOM : 0;
696 int buflen = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
697 int pad = SKB_DATA_ALIGN(VHOST_NET_RX_PAD + headroom + nvq->sock_hlen);
698 int sock_hlen = nvq->sock_hlen;
699 void *buf;
700 int copied;
701
702 if (unlikely(len < nvq->sock_hlen))
703 return -EFAULT;
704
705 if (SKB_DATA_ALIGN(len + pad) +
706 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) > PAGE_SIZE)
707 return -ENOSPC;
708
709 buflen += SKB_DATA_ALIGN(len + pad);
710 alloc_frag->offset = ALIGN((u64)alloc_frag->offset, SMP_CACHE_BYTES);
e4dab1e6
JW
711 if (unlikely(!vhost_net_page_frag_refill(net, buflen,
712 alloc_frag, GFP_KERNEL)))
0a0be13b
JW
713 return -ENOMEM;
714
715 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
716 copied = copy_page_from_iter(alloc_frag->page,
717 alloc_frag->offset +
718 offsetof(struct tun_xdp_hdr, gso),
719 sock_hlen, from);
720 if (copied != sock_hlen)
721 return -EFAULT;
722
723 hdr = buf;
724 gso = &hdr->gso;
725
726 if ((gso->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
727 vhost16_to_cpu(vq, gso->csum_start) +
728 vhost16_to_cpu(vq, gso->csum_offset) + 2 >
729 vhost16_to_cpu(vq, gso->hdr_len)) {
730 gso->hdr_len = cpu_to_vhost16(vq,
731 vhost16_to_cpu(vq, gso->csum_start) +
732 vhost16_to_cpu(vq, gso->csum_offset) + 2);
733
734 if (vhost16_to_cpu(vq, gso->hdr_len) > len)
735 return -EINVAL;
736 }
737
738 len -= sock_hlen;
739 copied = copy_page_from_iter(alloc_frag->page,
740 alloc_frag->offset + pad,
741 len, from);
742 if (copied != len)
743 return -EFAULT;
744
745 xdp->data_hard_start = buf;
746 xdp->data = buf + pad;
747 xdp->data_end = xdp->data + len;
748 hdr->buflen = buflen;
749
e4dab1e6 750 --net->refcnt_bias;
0a0be13b
JW
751 alloc_frag->offset += buflen;
752
753 ++nvq->batched_xdp;
754
755 return 0;
756}
757
0d20bdf3 758static void handle_tx_copy(struct vhost_net *net, struct socket *sock)
3a4d5c94 759{
2839400f 760 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
81f95a55 761 struct vhost_virtqueue *vq = &nvq->vq;
98a527aa 762 unsigned out, in;
d5675bd2 763 int head;
3a4d5c94
MT
764 struct msghdr msg = {
765 .msg_name = NULL,
766 .msg_namelen = 0,
767 .msg_control = NULL,
768 .msg_controllen = 0,
3a4d5c94
MT
769 .msg_flags = MSG_DONTWAIT,
770 };
771 size_t len, total_len = 0;
70181d51 772 int err;
a2ac9990 773 int sent_pkts = 0;
0a0be13b 774 bool sock_can_batch = (sock->sk->sk_sndbuf == INT_MAX);
28457ee6 775
0d20bdf3
JW
776 for (;;) {
777 bool busyloop_intr = false;
3a4d5c94 778
0a0be13b
JW
779 if (nvq->done_idx == VHOST_NET_BATCH)
780 vhost_tx_batch(net, nvq, sock, &msg);
781
0d20bdf3
JW
782 head = get_tx_bufs(net, nvq, &msg, &out, &in, &len,
783 &busyloop_intr);
784 /* On error, stop handling until the next kick. */
785 if (unlikely(head < 0))
786 break;
787 /* Nothing new? Wait for eventfd to tell us they refilled. */
788 if (head == vq->num) {
789 if (unlikely(busyloop_intr)) {
790 vhost_poll_queue(&vq->poll);
791 } else if (unlikely(vhost_enable_notify(&net->dev,
792 vq))) {
793 vhost_disable_notify(&net->dev, vq);
794 continue;
795 }
796 break;
797 }
6b1e6cc7 798
0d20bdf3 799 total_len += len;
0a0be13b
JW
800
801 /* For simplicity, TX batching is only enabled if
802 * sndbuf is unlimited.
803 */
804 if (sock_can_batch) {
805 err = vhost_net_build_xdp(nvq, &msg.msg_iter);
806 if (!err) {
807 goto done;
808 } else if (unlikely(err != -ENOSPC)) {
809 vhost_tx_batch(net, nvq, sock, &msg);
810 vhost_discard_vq_desc(vq, 1);
811 vhost_net_enable_vq(net, vq);
812 break;
813 }
814
815 /* We can't build XDP buff, go for single
816 * packet path but let's flush batched
817 * packets.
818 */
819 vhost_tx_batch(net, nvq, sock, &msg);
820 msg.msg_control = NULL;
821 } else {
822 if (tx_can_batch(vq, total_len))
823 msg.msg_flags |= MSG_MORE;
824 else
825 msg.msg_flags &= ~MSG_MORE;
826 }
0d20bdf3
JW
827
828 /* TODO: Check specific error and bomb out unless ENOBUFS? */
829 err = sock->ops->sendmsg(sock, &msg, len);
830 if (unlikely(err < 0)) {
831 vhost_discard_vq_desc(vq, 1);
832 vhost_net_enable_vq(net, vq);
833 break;
834 }
835 if (err != len)
836 pr_debug("Truncated TX packet: len %d != %zd\n",
837 err, len);
0a0be13b
JW
838done:
839 vq->heads[nvq->done_idx].id = cpu_to_vhost32(vq, head);
840 vq->heads[nvq->done_idx].len = 0;
841 ++nvq->done_idx;
e82b9b07 842 if (vhost_exceeds_weight(vq, ++sent_pkts, total_len))
0d20bdf3 843 break;
0d20bdf3 844 }
4afb52c2 845
0a0be13b 846 vhost_tx_batch(net, nvq, sock, &msg);
0d20bdf3 847}
3a4d5c94 848
0d20bdf3
JW
849static void handle_tx_zerocopy(struct vhost_net *net, struct socket *sock)
850{
851 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
852 struct vhost_virtqueue *vq = &nvq->vq;
853 unsigned out, in;
854 int head;
855 struct msghdr msg = {
856 .msg_name = NULL,
857 .msg_namelen = 0,
858 .msg_control = NULL,
859 .msg_controllen = 0,
860 .msg_flags = MSG_DONTWAIT,
861 };
fe8dd45b 862 struct tun_msg_ctl ctl;
0d20bdf3
JW
863 size_t len, total_len = 0;
864 int err;
865 struct vhost_net_ubuf_ref *uninitialized_var(ubufs);
866 bool zcopy_used;
867 int sent_pkts = 0;
3a4d5c94
MT
868
869 for (;;) {
027b1760
TM
870 bool busyloop_intr;
871
bab632d6 872 /* Release DMAs done buffers first */
0d20bdf3 873 vhost_zerocopy_signal_used(net, vq);
bab632d6 874
027b1760 875 busyloop_intr = false;
a2a91a13
JW
876 head = get_tx_bufs(net, nvq, &msg, &out, &in, &len,
877 &busyloop_intr);
d5675bd2 878 /* On error, stop handling until the next kick. */
7b3384fc 879 if (unlikely(head < 0))
d5675bd2 880 break;
3a4d5c94
MT
881 /* Nothing new? Wait for eventfd to tell us they refilled. */
882 if (head == vq->num) {
027b1760
TM
883 if (unlikely(busyloop_intr)) {
884 vhost_poll_queue(&vq->poll);
885 } else if (unlikely(vhost_enable_notify(&net->dev, vq))) {
8ea8cf89 886 vhost_disable_notify(&net->dev, vq);
3a4d5c94
MT
887 continue;
888 }
889 break;
890 }
ce21a029 891
0d20bdf3
JW
892 zcopy_used = len >= VHOST_GOODCOPY_LEN
893 && !vhost_exceeds_maxpend(net)
894 && vhost_net_tx_select_zcopy(net);
cedb9bdc 895
bab632d6 896 /* use msg_control to pass vhost zerocopy ubuf info to skb */
cedb9bdc 897 if (zcopy_used) {
ce21a029
JW
898 struct ubuf_info *ubuf;
899 ubuf = nvq->ubuf_info + nvq->upend_idx;
900
8b38694a 901 vq->heads[nvq->upend_idx].id = cpu_to_vhost32(vq, head);
ce21a029
JW
902 vq->heads[nvq->upend_idx].len = VHOST_DMA_IN_PROGRESS;
903 ubuf->callback = vhost_zerocopy_callback;
904 ubuf->ctx = nvq->ubufs;
905 ubuf->desc = nvq->upend_idx;
c1d1b437 906 refcount_set(&ubuf->refcnt, 1);
fe8dd45b
JW
907 msg.msg_control = &ctl;
908 ctl.type = TUN_MSG_UBUF;
909 ctl.ptr = ubuf;
910 msg.msg_controllen = sizeof(ctl);
ce21a029 911 ubufs = nvq->ubufs;
0ad8b480 912 atomic_inc(&ubufs->refcount);
2839400f 913 nvq->upend_idx = (nvq->upend_idx + 1) % UIO_MAXIOV;
ce21a029 914 } else {
4364d5f9 915 msg.msg_control = NULL;
ce21a029
JW
916 ubufs = NULL;
917 }
0ed005ce 918 total_len += len;
c92a8a8c 919 if (tx_can_batch(vq, total_len) &&
0ed005ce
JW
920 likely(!vhost_exceeds_maxpend(net))) {
921 msg.msg_flags |= MSG_MORE;
922 } else {
923 msg.msg_flags &= ~MSG_MORE;
924 }
925
3a4d5c94 926 /* TODO: Check specific error and bomb out unless ENOBUFS? */
1b784140 927 err = sock->ops->sendmsg(sock, &msg, len);
3a4d5c94 928 if (unlikely(err < 0)) {
cedb9bdc 929 if (zcopy_used) {
ce21a029 930 vhost_net_ubuf_put(ubufs);
2839400f
AH
931 nvq->upend_idx = ((unsigned)nvq->upend_idx - 1)
932 % UIO_MAXIOV;
bab632d6 933 }
8dd014ad 934 vhost_discard_vq_desc(vq, 1);
feb8892c 935 vhost_net_enable_vq(net, vq);
3a4d5c94
MT
936 break;
937 }
938 if (err != len)
95c0ec6a
MT
939 pr_debug("Truncated TX packet: "
940 " len %d != %zd\n", err, len);
cedb9bdc 941 if (!zcopy_used)
bab632d6 942 vhost_add_used_and_signal(&net->dev, vq, head, 0);
c8fb217a 943 else
eaae8132 944 vhost_zerocopy_signal_used(net, vq);
eaae8132 945 vhost_net_tx_packet(net);
e82b9b07
JW
946 if (unlikely(vhost_exceeds_weight(vq, ++sent_pkts,
947 total_len)))
3a4d5c94 948 break;
3a4d5c94 949 }
0d20bdf3
JW
950}
951
952/* Expects to be always run from workqueue - which acts as
953 * read-size critical section for our kind of RCU. */
954static void handle_tx(struct vhost_net *net)
955{
956 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
957 struct vhost_virtqueue *vq = &nvq->vq;
958 struct socket *sock;
959
a6a67a2f 960 mutex_lock_nested(&vq->mutex, VHOST_NET_VQ_TX);
0d20bdf3
JW
961 sock = vq->private_data;
962 if (!sock)
963 goto out;
964
965 if (!vq_iotlb_prefetch(vq))
966 goto out;
967
968 vhost_disable_notify(&net->dev, vq);
969 vhost_net_disable_vq(net, vq);
970
971 if (vhost_sock_zcopy(sock))
972 handle_tx_zerocopy(net, sock);
973 else
974 handle_tx_copy(net, sock);
975
2e26af79 976out:
3a4d5c94 977 mutex_unlock(&vq->mutex);
3a4d5c94
MT
978}
979
c67df11f 980static int peek_head_len(struct vhost_net_virtqueue *rvq, struct sock *sk)
8dd014ad
DS
981{
982 struct sk_buff *head;
983 int len = 0;
783e3988 984 unsigned long flags;
8dd014ad 985
5990a305 986 if (rvq->rx_ring)
c67df11f 987 return vhost_net_buf_peek(rvq);
1576d986 988
783e3988 989 spin_lock_irqsave(&sk->sk_receive_queue.lock, flags);
8dd014ad 990 head = skb_peek(&sk->sk_receive_queue);
c53cff5e 991 if (likely(head)) {
8dd014ad 992 len = head->len;
df8a39de 993 if (skb_vlan_tag_present(head))
c53cff5e
BG
994 len += VLAN_HLEN;
995 }
996
783e3988 997 spin_unlock_irqrestore(&sk->sk_receive_queue.lock, flags);
8dd014ad
DS
998 return len;
999}
1000
be294a51
TM
1001static int vhost_net_rx_peek_head_len(struct vhost_net *net, struct sock *sk,
1002 bool *busyloop_intr)
03088137 1003{
28b9b33b
TM
1004 struct vhost_net_virtqueue *rnvq = &net->vqs[VHOST_NET_VQ_RX];
1005 struct vhost_net_virtqueue *tnvq = &net->vqs[VHOST_NET_VQ_TX];
6369fec5 1006 struct vhost_virtqueue *rvq = &rnvq->vq;
28b9b33b 1007 struct vhost_virtqueue *tvq = &tnvq->vq;
28b9b33b 1008 int len = peek_head_len(rnvq, sk);
03088137 1009
dc151282 1010 if (!len && rvq->busyloop_timeout) {
f5a4941a 1011 /* Flush batched heads first */
09c32489 1012 vhost_net_signal_used(rnvq);
03088137 1013 /* Both tx vq and rx socket were polled here */
dc151282 1014 vhost_net_busy_poll(net, rvq, tvq, busyloop_intr, true);
03088137 1015
28b9b33b 1016 len = peek_head_len(rnvq, sk);
03088137
JW
1017 }
1018
1019 return len;
1020}
1021
8dd014ad
DS
1022/* This is a multi-buffer version of vhost_get_desc, that works if
1023 * vq has read descriptors only.
1024 * @vq - the relevant virtqueue
1025 * @datalen - data length we'll be reading
1026 * @iovcount - returned count of io vectors we fill
1027 * @log - vhost log
1028 * @log_num - log offset
94249369 1029 * @quota - headcount quota, 1 for big buffer
8dd014ad
DS
1030 * returns number of buffer heads allocated, negative on error
1031 */
1032static int get_rx_bufs(struct vhost_virtqueue *vq,
1033 struct vring_used_elem *heads,
1034 int datalen,
1035 unsigned *iovcount,
1036 struct vhost_log *log,
94249369
JW
1037 unsigned *log_num,
1038 unsigned int quota)
8dd014ad
DS
1039{
1040 unsigned int out, in;
1041 int seg = 0;
1042 int headcount = 0;
1043 unsigned d;
1044 int r, nlogs = 0;
8b38694a
MT
1045 /* len is always initialized before use since we are always called with
1046 * datalen > 0.
1047 */
1048 u32 uninitialized_var(len);
8dd014ad 1049
94249369 1050 while (datalen > 0 && headcount < quota) {
e0e9b406 1051 if (unlikely(seg >= UIO_MAXIOV)) {
8dd014ad
DS
1052 r = -ENOBUFS;
1053 goto err;
1054 }
47283bef 1055 r = vhost_get_vq_desc(vq, vq->iov + seg,
8dd014ad
DS
1056 ARRAY_SIZE(vq->iov) - seg, &out,
1057 &in, log, log_num);
a39ee449
MT
1058 if (unlikely(r < 0))
1059 goto err;
1060
1061 d = r;
8dd014ad
DS
1062 if (d == vq->num) {
1063 r = 0;
1064 goto err;
1065 }
1066 if (unlikely(out || in <= 0)) {
1067 vq_err(vq, "unexpected descriptor format for RX: "
1068 "out %d, in %d\n", out, in);
1069 r = -EINVAL;
1070 goto err;
1071 }
1072 if (unlikely(log)) {
1073 nlogs += *log_num;
1074 log += *log_num;
1075 }
8b38694a
MT
1076 heads[headcount].id = cpu_to_vhost32(vq, d);
1077 len = iov_length(vq->iov + seg, in);
1078 heads[headcount].len = cpu_to_vhost32(vq, len);
1079 datalen -= len;
8dd014ad
DS
1080 ++headcount;
1081 seg += in;
1082 }
99975cc6 1083 heads[headcount - 1].len = cpu_to_vhost32(vq, len + datalen);
8dd014ad
DS
1084 *iovcount = seg;
1085 if (unlikely(log))
1086 *log_num = nlogs;
d8316f39
MT
1087
1088 /* Detect overrun */
1089 if (unlikely(datalen > 0)) {
1090 r = UIO_MAXIOV + 1;
1091 goto err;
1092 }
8dd014ad
DS
1093 return headcount;
1094err:
1095 vhost_discard_vq_desc(vq, headcount);
1096 return r;
1097}
1098
3a4d5c94
MT
1099/* Expects to be always run from workqueue - which acts as
1100 * read-size critical section for our kind of RCU. */
94249369 1101static void handle_rx(struct vhost_net *net)
3a4d5c94 1102{
81f95a55
MT
1103 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_RX];
1104 struct vhost_virtqueue *vq = &nvq->vq;
8dd014ad
DS
1105 unsigned uninitialized_var(in), log;
1106 struct vhost_log *vq_log;
1107 struct msghdr msg = {
1108 .msg_name = NULL,
1109 .msg_namelen = 0,
1110 .msg_control = NULL, /* FIXME: get and handle RX aux data. */
1111 .msg_controllen = 0,
8dd014ad
DS
1112 .msg_flags = MSG_DONTWAIT,
1113 };
0960b641
JW
1114 struct virtio_net_hdr hdr = {
1115 .flags = 0,
1116 .gso_type = VIRTIO_NET_HDR_GSO_NONE
8dd014ad 1117 };
8dd014ad 1118 size_t total_len = 0;
910a578f 1119 int err, mergeable;
f5a4941a 1120 s16 headcount;
8dd014ad
DS
1121 size_t vhost_hlen, sock_hlen;
1122 size_t vhost_len, sock_len;
be294a51 1123 bool busyloop_intr = false;
2e26af79 1124 struct socket *sock;
ba7438ae 1125 struct iov_iter fixup;
0960b641 1126 __virtio16 num_buffers;
db688c24 1127 int recv_pkts = 0;
8dd014ad 1128
a6a67a2f 1129 mutex_lock_nested(&vq->mutex, VHOST_NET_VQ_RX);
2e26af79
AH
1130 sock = vq->private_data;
1131 if (!sock)
1132 goto out;
6b1e6cc7
JW
1133
1134 if (!vq_iotlb_prefetch(vq))
1135 goto out;
1136
8ea8cf89 1137 vhost_disable_notify(&net->dev, vq);
8241a1e4 1138 vhost_net_disable_vq(net, vq);
2e26af79 1139
81f95a55
MT
1140 vhost_hlen = nvq->vhost_hlen;
1141 sock_hlen = nvq->sock_hlen;
8dd014ad 1142
ea16c514 1143 vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ?
8dd014ad 1144 vq->log : NULL;
ea16c514 1145 mergeable = vhost_has_feature(vq, VIRTIO_NET_F_MRG_RXBUF);
8dd014ad 1146
be294a51
TM
1147 while ((sock_len = vhost_net_rx_peek_head_len(net, sock->sk,
1148 &busyloop_intr))) {
8dd014ad
DS
1149 sock_len += sock_hlen;
1150 vhost_len = sock_len + vhost_hlen;
f5a4941a
JW
1151 headcount = get_rx_bufs(vq, vq->heads + nvq->done_idx,
1152 vhost_len, &in, vq_log, &log,
94249369 1153 likely(mergeable) ? UIO_MAXIOV : 1);
8dd014ad
DS
1154 /* On error, stop handling until the next kick. */
1155 if (unlikely(headcount < 0))
8241a1e4 1156 goto out;
8dd014ad
DS
1157 /* OK, now we need to know about added descriptors. */
1158 if (!headcount) {
6369fec5
TM
1159 if (unlikely(busyloop_intr)) {
1160 vhost_poll_queue(&vq->poll);
1161 } else if (unlikely(vhost_enable_notify(&net->dev, vq))) {
8dd014ad
DS
1162 /* They have slipped one in as we were
1163 * doing that: check again. */
8ea8cf89 1164 vhost_disable_notify(&net->dev, vq);
8dd014ad
DS
1165 continue;
1166 }
1167 /* Nothing new? Wait for eventfd to tell us
1168 * they refilled. */
8241a1e4 1169 goto out;
8dd014ad 1170 }
6369fec5 1171 busyloop_intr = false;
5990a305 1172 if (nvq->rx_ring)
6e474083
WX
1173 msg.msg_control = vhost_net_buf_consume(&nvq->rxq);
1174 /* On overrun, truncate and discard */
1175 if (unlikely(headcount > UIO_MAXIOV)) {
1176 iov_iter_init(&msg.msg_iter, READ, vq->iov, 1, 1);
1177 err = sock->ops->recvmsg(sock, &msg,
1178 1, MSG_DONTWAIT | MSG_TRUNC);
1179 pr_debug("Discarded rx packet: len %zd\n", sock_len);
1180 continue;
1181 }
8dd014ad 1182 /* We don't need to be notified again. */
ba7438ae
AV
1183 iov_iter_init(&msg.msg_iter, READ, vq->iov, in, vhost_len);
1184 fixup = msg.msg_iter;
1185 if (unlikely((vhost_hlen))) {
1186 /* We will supply the header ourselves
1187 * TODO: support TSO.
1188 */
1189 iov_iter_advance(&msg.msg_iter, vhost_hlen);
ba7438ae 1190 }
1b784140 1191 err = sock->ops->recvmsg(sock, &msg,
8dd014ad
DS
1192 sock_len, MSG_DONTWAIT | MSG_TRUNC);
1193 /* Userspace might have consumed the packet meanwhile:
1194 * it's not supposed to do this usually, but might be hard
1195 * to prevent. Discard data we got (if any) and keep going. */
1196 if (unlikely(err != sock_len)) {
1197 pr_debug("Discarded rx packet: "
1198 " len %d, expected %zd\n", err, sock_len);
1199 vhost_discard_vq_desc(vq, headcount);
1200 continue;
1201 }
ba7438ae 1202 /* Supply virtio_net_hdr if VHOST_NET_F_VIRTIO_NET_HDR */
4c5a8442
MT
1203 if (unlikely(vhost_hlen)) {
1204 if (copy_to_iter(&hdr, sizeof(hdr),
1205 &fixup) != sizeof(hdr)) {
1206 vq_err(vq, "Unable to write vnet_hdr "
1207 "at addr %p\n", vq->iov->iov_base);
8241a1e4 1208 goto out;
4c5a8442
MT
1209 }
1210 } else {
1211 /* Header came from socket; we'll need to patch
1212 * ->num_buffers over if VIRTIO_NET_F_MRG_RXBUF
1213 */
1214 iov_iter_advance(&fixup, sizeof(hdr));
8dd014ad
DS
1215 }
1216 /* TODO: Should check and handle checksum. */
5201aa49 1217
0960b641 1218 num_buffers = cpu_to_vhost16(vq, headcount);
cfbdab95 1219 if (likely(mergeable) &&
0d79a493
MT
1220 copy_to_iter(&num_buffers, sizeof num_buffers,
1221 &fixup) != sizeof num_buffers) {
8dd014ad
DS
1222 vq_err(vq, "Failed num_buffers write");
1223 vhost_discard_vq_desc(vq, headcount);
8241a1e4 1224 goto out;
8dd014ad 1225 }
f5a4941a 1226 nvq->done_idx += headcount;
d0d86971 1227 if (nvq->done_idx > VHOST_NET_BATCH)
09c32489 1228 vhost_net_signal_used(nvq);
8dd014ad 1229 if (unlikely(vq_log))
cc5e7107
JW
1230 vhost_log_write(vq, vq_log, log, vhost_len,
1231 vq->iov, in);
8dd014ad 1232 total_len += vhost_len;
e82b9b07 1233 if (unlikely(vhost_exceeds_weight(vq, ++recv_pkts, total_len)))
8241a1e4 1234 goto out;
8dd014ad 1235 }
be294a51
TM
1236 if (unlikely(busyloop_intr))
1237 vhost_poll_queue(&vq->poll);
1238 else
1239 vhost_net_enable_vq(net, vq);
2e26af79 1240out:
09c32489 1241 vhost_net_signal_used(nvq);
8dd014ad 1242 mutex_unlock(&vq->mutex);
8dd014ad
DS
1243}
1244
c23f3445 1245static void handle_tx_kick(struct vhost_work *work)
3a4d5c94 1246{
c23f3445
TH
1247 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1248 poll.work);
1249 struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
1250
3a4d5c94
MT
1251 handle_tx(net);
1252}
1253
c23f3445 1254static void handle_rx_kick(struct vhost_work *work)
3a4d5c94 1255{
c23f3445
TH
1256 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1257 poll.work);
1258 struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
1259
3a4d5c94
MT
1260 handle_rx(net);
1261}
1262
c23f3445 1263static void handle_tx_net(struct vhost_work *work)
3a4d5c94 1264{
c23f3445
TH
1265 struct vhost_net *net = container_of(work, struct vhost_net,
1266 poll[VHOST_NET_VQ_TX].work);
3a4d5c94
MT
1267 handle_tx(net);
1268}
1269
c23f3445 1270static void handle_rx_net(struct vhost_work *work)
3a4d5c94 1271{
c23f3445
TH
1272 struct vhost_net *net = container_of(work, struct vhost_net,
1273 poll[VHOST_NET_VQ_RX].work);
3a4d5c94
MT
1274 handle_rx(net);
1275}
1276
1277static int vhost_net_open(struct inode *inode, struct file *f)
1278{
23cc5a99 1279 struct vhost_net *n;
c23f3445 1280 struct vhost_dev *dev;
3ab2e420 1281 struct vhost_virtqueue **vqs;
5990a305 1282 void **queue;
0a0be13b 1283 struct xdp_buff *xdp;
59566b6e 1284 int i;
c23f3445 1285
dcda9b04 1286 n = kvmalloc(sizeof *n, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
6c5ab651
MH
1287 if (!n)
1288 return -ENOMEM;
6da2ec56 1289 vqs = kmalloc_array(VHOST_NET_VQ_MAX, sizeof(*vqs), GFP_KERNEL);
3ab2e420 1290 if (!vqs) {
d04257b0 1291 kvfree(n);
3ab2e420
AH
1292 return -ENOMEM;
1293 }
c23f3445 1294
d0d86971 1295 queue = kmalloc_array(VHOST_NET_BATCH, sizeof(void *),
c67df11f
JW
1296 GFP_KERNEL);
1297 if (!queue) {
1298 kfree(vqs);
1299 kvfree(n);
1300 return -ENOMEM;
1301 }
1302 n->vqs[VHOST_NET_VQ_RX].rxq.queue = queue;
1303
0a0be13b
JW
1304 xdp = kmalloc_array(VHOST_NET_BATCH, sizeof(*xdp), GFP_KERNEL);
1305 if (!xdp) {
1306 kfree(vqs);
1307 kvfree(n);
1308 kfree(queue);
8a1aff14 1309 return -ENOMEM;
0a0be13b
JW
1310 }
1311 n->vqs[VHOST_NET_VQ_TX].xdp = xdp;
1312
c23f3445 1313 dev = &n->dev;
3ab2e420
AH
1314 vqs[VHOST_NET_VQ_TX] = &n->vqs[VHOST_NET_VQ_TX].vq;
1315 vqs[VHOST_NET_VQ_RX] = &n->vqs[VHOST_NET_VQ_RX].vq;
1316 n->vqs[VHOST_NET_VQ_TX].vq.handle_kick = handle_tx_kick;
1317 n->vqs[VHOST_NET_VQ_RX].vq.handle_kick = handle_rx_kick;
2839400f
AH
1318 for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
1319 n->vqs[i].ubufs = NULL;
1320 n->vqs[i].ubuf_info = NULL;
1321 n->vqs[i].upend_idx = 0;
1322 n->vqs[i].done_idx = 0;
0a0be13b 1323 n->vqs[i].batched_xdp = 0;
81f95a55
MT
1324 n->vqs[i].vhost_hlen = 0;
1325 n->vqs[i].sock_hlen = 0;
ab7e34b3 1326 n->vqs[i].rx_ring = NULL;
c67df11f 1327 vhost_net_buf_init(&n->vqs[i].rxq);
2839400f 1328 }
b46a0bf7 1329 vhost_dev_init(dev, vqs, VHOST_NET_VQ_MAX,
e82b9b07
JW
1330 UIO_MAXIOV + VHOST_NET_BATCH,
1331 VHOST_NET_WEIGHT, VHOST_NET_PKT_WEIGHT);
3a4d5c94 1332
a9a08845
LT
1333 vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, EPOLLOUT, dev);
1334 vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, EPOLLIN, dev);
3a4d5c94
MT
1335
1336 f->private_data = n;
e4dab1e6
JW
1337 n->page_frag.page = NULL;
1338 n->refcnt_bias = 0;
3a4d5c94
MT
1339
1340 return 0;
1341}
1342
3a4d5c94
MT
1343static struct socket *vhost_net_stop_vq(struct vhost_net *n,
1344 struct vhost_virtqueue *vq)
1345{
1346 struct socket *sock;
c67df11f
JW
1347 struct vhost_net_virtqueue *nvq =
1348 container_of(vq, struct vhost_net_virtqueue, vq);
3a4d5c94
MT
1349
1350 mutex_lock(&vq->mutex);
22fa90c7 1351 sock = vq->private_data;
3a4d5c94 1352 vhost_net_disable_vq(n, vq);
22fa90c7 1353 vq->private_data = NULL;
c67df11f 1354 vhost_net_buf_unproduce(nvq);
303fd71b 1355 nvq->rx_ring = NULL;
3a4d5c94
MT
1356 mutex_unlock(&vq->mutex);
1357 return sock;
1358}
1359
1360static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock,
1361 struct socket **rx_sock)
1362{
3ab2e420
AH
1363 *tx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_TX].vq);
1364 *rx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_RX].vq);
3a4d5c94
MT
1365}
1366
1367static void vhost_net_flush_vq(struct vhost_net *n, int index)
1368{
1369 vhost_poll_flush(n->poll + index);
3ab2e420 1370 vhost_poll_flush(&n->vqs[index].vq.poll);
3a4d5c94
MT
1371}
1372
1373static void vhost_net_flush(struct vhost_net *n)
1374{
1375 vhost_net_flush_vq(n, VHOST_NET_VQ_TX);
1376 vhost_net_flush_vq(n, VHOST_NET_VQ_RX);
2839400f 1377 if (n->vqs[VHOST_NET_VQ_TX].ubufs) {
3ab2e420 1378 mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
1280c27f 1379 n->tx_flush = true;
3ab2e420 1380 mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
1280c27f 1381 /* Wait for all lower device DMAs done. */
fe729a57 1382 vhost_net_ubuf_put_and_wait(n->vqs[VHOST_NET_VQ_TX].ubufs);
3ab2e420 1383 mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
1280c27f 1384 n->tx_flush = false;
0ad8b480 1385 atomic_set(&n->vqs[VHOST_NET_VQ_TX].ubufs->refcount, 1);
3ab2e420 1386 mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
1280c27f 1387 }
3a4d5c94
MT
1388}
1389
1390static int vhost_net_release(struct inode *inode, struct file *f)
1391{
1392 struct vhost_net *n = f->private_data;
1393 struct socket *tx_sock;
1394 struct socket *rx_sock;
1395
1396 vhost_net_stop(n, &tx_sock, &rx_sock);
1397 vhost_net_flush(n);
b211616d 1398 vhost_dev_stop(&n->dev);
f6f93f75 1399 vhost_dev_cleanup(&n->dev);
81f95a55 1400 vhost_net_vq_reset(n);
3a4d5c94 1401 if (tx_sock)
09aaacf0 1402 sockfd_put(tx_sock);
3a4d5c94 1403 if (rx_sock)
09aaacf0 1404 sockfd_put(rx_sock);
b0c057ca 1405 /* Make sure no callbacks are outstanding */
d05faa5f 1406 synchronize_rcu();
3a4d5c94
MT
1407 /* We do an extra flush before freeing memory,
1408 * since jobs can re-queue themselves. */
1409 vhost_net_flush(n);
c67df11f 1410 kfree(n->vqs[VHOST_NET_VQ_RX].rxq.queue);
0a0be13b 1411 kfree(n->vqs[VHOST_NET_VQ_TX].xdp);
3ab2e420 1412 kfree(n->dev.vqs);
e4dab1e6
JW
1413 if (n->page_frag.page)
1414 __page_frag_cache_drain(n->page_frag.page, n->refcnt_bias);
d04257b0 1415 kvfree(n);
3a4d5c94
MT
1416 return 0;
1417}
1418
1419static struct socket *get_raw_socket(int fd)
1420{
1421 struct {
1422 struct sockaddr_ll sa;
1423 char buf[MAX_ADDR_LEN];
1424 } uaddr;
9b2c45d4 1425 int r;
3a4d5c94 1426 struct socket *sock = sockfd_lookup(fd, &r);
d47effe1 1427
3a4d5c94
MT
1428 if (!sock)
1429 return ERR_PTR(-ENOTSOCK);
1430
1431 /* Parameter checking */
1432 if (sock->sk->sk_type != SOCK_RAW) {
1433 r = -ESOCKTNOSUPPORT;
1434 goto err;
1435 }
1436
9b2c45d4
DV
1437 r = sock->ops->getname(sock, (struct sockaddr *)&uaddr.sa, 0);
1438 if (r < 0)
3a4d5c94
MT
1439 goto err;
1440
1441 if (uaddr.sa.sll_family != AF_PACKET) {
1442 r = -EPFNOSUPPORT;
1443 goto err;
1444 }
1445 return sock;
1446err:
09aaacf0 1447 sockfd_put(sock);
3a4d5c94
MT
1448 return ERR_PTR(r);
1449}
1450
5990a305 1451static struct ptr_ring *get_tap_ptr_ring(int fd)
c67df11f 1452{
5990a305 1453 struct ptr_ring *ring;
c67df11f
JW
1454 struct file *file = fget(fd);
1455
1456 if (!file)
1457 return NULL;
5990a305
JW
1458 ring = tun_get_tx_ring(file);
1459 if (!IS_ERR(ring))
c67df11f 1460 goto out;
5990a305
JW
1461 ring = tap_get_ptr_ring(file);
1462 if (!IS_ERR(ring))
c67df11f 1463 goto out;
5990a305 1464 ring = NULL;
c67df11f
JW
1465out:
1466 fput(file);
5990a305 1467 return ring;
c67df11f
JW
1468}
1469
501c774c 1470static struct socket *get_tap_socket(int fd)
3a4d5c94
MT
1471{
1472 struct file *file = fget(fd);
1473 struct socket *sock;
d47effe1 1474
3a4d5c94
MT
1475 if (!file)
1476 return ERR_PTR(-EBADF);
1477 sock = tun_get_socket(file);
501c774c
AB
1478 if (!IS_ERR(sock))
1479 return sock;
635b8c8e 1480 sock = tap_get_socket(file);
3a4d5c94
MT
1481 if (IS_ERR(sock))
1482 fput(file);
1483 return sock;
1484}
1485
1486static struct socket *get_socket(int fd)
1487{
1488 struct socket *sock;
d47effe1 1489
3a4d5c94
MT
1490 /* special case to disable backend */
1491 if (fd == -1)
1492 return NULL;
1493 sock = get_raw_socket(fd);
1494 if (!IS_ERR(sock))
1495 return sock;
501c774c 1496 sock = get_tap_socket(fd);
3a4d5c94
MT
1497 if (!IS_ERR(sock))
1498 return sock;
1499 return ERR_PTR(-ENOTSOCK);
1500}
1501
1502static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
1503{
1504 struct socket *sock, *oldsock;
1505 struct vhost_virtqueue *vq;
2839400f 1506 struct vhost_net_virtqueue *nvq;
fe729a57 1507 struct vhost_net_ubuf_ref *ubufs, *oldubufs = NULL;
3a4d5c94
MT
1508 int r;
1509
1510 mutex_lock(&n->dev.mutex);
1511 r = vhost_dev_check_owner(&n->dev);
1512 if (r)
1513 goto err;
1514
1515 if (index >= VHOST_NET_VQ_MAX) {
1516 r = -ENOBUFS;
1517 goto err;
1518 }
3ab2e420 1519 vq = &n->vqs[index].vq;
2839400f 1520 nvq = &n->vqs[index];
3a4d5c94
MT
1521 mutex_lock(&vq->mutex);
1522
1523 /* Verify that ring has been setup correctly. */
1524 if (!vhost_vq_access_ok(vq)) {
1525 r = -EFAULT;
1dace8c8 1526 goto err_vq;
3a4d5c94
MT
1527 }
1528 sock = get_socket(fd);
1529 if (IS_ERR(sock)) {
1530 r = PTR_ERR(sock);
1dace8c8 1531 goto err_vq;
3a4d5c94
MT
1532 }
1533
1534 /* start polling new socket */
22fa90c7 1535 oldsock = vq->private_data;
11fe8839 1536 if (sock != oldsock) {
fe729a57
AH
1537 ubufs = vhost_net_ubuf_alloc(vq,
1538 sock && vhost_sock_zcopy(sock));
bab632d6
MT
1539 if (IS_ERR(ubufs)) {
1540 r = PTR_ERR(ubufs);
1541 goto err_ubufs;
1542 }
692a998b 1543
d47effe1 1544 vhost_net_disable_vq(n, vq);
22fa90c7 1545 vq->private_data = sock;
c67df11f 1546 vhost_net_buf_unproduce(nvq);
80f7d030 1547 r = vhost_vq_init_access(vq);
f59281da 1548 if (r)
692a998b 1549 goto err_used;
2b8b328b
JW
1550 r = vhost_net_enable_vq(n, vq);
1551 if (r)
1552 goto err_used;
303fd71b
JW
1553 if (index == VHOST_NET_VQ_RX)
1554 nvq->rx_ring = get_tap_ptr_ring(fd);
692a998b 1555
2839400f
AH
1556 oldubufs = nvq->ubufs;
1557 nvq->ubufs = ubufs;
64e9a9b8
MT
1558
1559 n->tx_packets = 0;
1560 n->tx_zcopy_err = 0;
1280c27f 1561 n->tx_flush = false;
dd1f4078 1562 }
3a4d5c94 1563
1680e906
MT
1564 mutex_unlock(&vq->mutex);
1565
c047e5f3 1566 if (oldubufs) {
c38e39c3 1567 vhost_net_ubuf_put_wait_and_free(oldubufs);
c047e5f3 1568 mutex_lock(&vq->mutex);
eaae8132 1569 vhost_zerocopy_signal_used(n, vq);
c047e5f3
MT
1570 mutex_unlock(&vq->mutex);
1571 }
bab632d6 1572
3a4d5c94
MT
1573 if (oldsock) {
1574 vhost_net_flush_vq(n, index);
09aaacf0 1575 sockfd_put(oldsock);
3a4d5c94 1576 }
1dace8c8 1577
1680e906
MT
1578 mutex_unlock(&n->dev.mutex);
1579 return 0;
1580
692a998b 1581err_used:
22fa90c7 1582 vq->private_data = oldsock;
692a998b
JW
1583 vhost_net_enable_vq(n, vq);
1584 if (ubufs)
c38e39c3 1585 vhost_net_ubuf_put_wait_and_free(ubufs);
bab632d6 1586err_ubufs:
b8f1f658
JW
1587 if (sock)
1588 sockfd_put(sock);
1dace8c8
JD
1589err_vq:
1590 mutex_unlock(&vq->mutex);
3a4d5c94
MT
1591err:
1592 mutex_unlock(&n->dev.mutex);
1593 return r;
1594}
1595
1596static long vhost_net_reset_owner(struct vhost_net *n)
1597{
1598 struct socket *tx_sock = NULL;
1599 struct socket *rx_sock = NULL;
1600 long err;
a9709d68 1601 struct vhost_umem *umem;
d47effe1 1602
3a4d5c94
MT
1603 mutex_lock(&n->dev.mutex);
1604 err = vhost_dev_check_owner(&n->dev);
1605 if (err)
1606 goto done;
a9709d68
JW
1607 umem = vhost_dev_reset_owner_prepare();
1608 if (!umem) {
150b9e51
MT
1609 err = -ENOMEM;
1610 goto done;
1611 }
3a4d5c94
MT
1612 vhost_net_stop(n, &tx_sock, &rx_sock);
1613 vhost_net_flush(n);
4cd87951 1614 vhost_dev_stop(&n->dev);
a9709d68 1615 vhost_dev_reset_owner(&n->dev, umem);
81f95a55 1616 vhost_net_vq_reset(n);
3a4d5c94
MT
1617done:
1618 mutex_unlock(&n->dev.mutex);
1619 if (tx_sock)
09aaacf0 1620 sockfd_put(tx_sock);
3a4d5c94 1621 if (rx_sock)
09aaacf0 1622 sockfd_put(rx_sock);
3a4d5c94
MT
1623 return err;
1624}
1625
429711ae
JW
1626static int vhost_net_set_backend_features(struct vhost_net *n, u64 features)
1627{
1628 int i;
1629
1630 mutex_lock(&n->dev.mutex);
1631 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
1632 mutex_lock(&n->vqs[i].vq.mutex);
1633 n->vqs[i].vq.acked_backend_features = features;
1634 mutex_unlock(&n->vqs[i].vq.mutex);
1635 }
1636 mutex_unlock(&n->dev.mutex);
1637
1638 return 0;
1639}
1640
3a4d5c94
MT
1641static int vhost_net_set_features(struct vhost_net *n, u64 features)
1642{
8dd014ad 1643 size_t vhost_hlen, sock_hlen, hdr_len;
3a4d5c94 1644 int i;
8dd014ad 1645
e4fca7d6
MT
1646 hdr_len = (features & ((1ULL << VIRTIO_NET_F_MRG_RXBUF) |
1647 (1ULL << VIRTIO_F_VERSION_1))) ?
8dd014ad
DS
1648 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
1649 sizeof(struct virtio_net_hdr);
1650 if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) {
1651 /* vhost provides vnet_hdr */
1652 vhost_hlen = hdr_len;
1653 sock_hlen = 0;
1654 } else {
1655 /* socket provides vnet_hdr */
1656 vhost_hlen = 0;
1657 sock_hlen = hdr_len;
1658 }
3a4d5c94
MT
1659 mutex_lock(&n->dev.mutex);
1660 if ((features & (1 << VHOST_F_LOG_ALL)) &&
6b1e6cc7
JW
1661 !vhost_log_access_ok(&n->dev))
1662 goto out_unlock;
1663
1664 if ((features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))) {
1665 if (vhost_init_device_iotlb(&n->dev, true))
1666 goto out_unlock;
3a4d5c94 1667 }
6b1e6cc7 1668
3a4d5c94 1669 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
3ab2e420 1670 mutex_lock(&n->vqs[i].vq.mutex);
ea16c514 1671 n->vqs[i].vq.acked_features = features;
81f95a55
MT
1672 n->vqs[i].vhost_hlen = vhost_hlen;
1673 n->vqs[i].sock_hlen = sock_hlen;
3ab2e420 1674 mutex_unlock(&n->vqs[i].vq.mutex);
3a4d5c94 1675 }
3a4d5c94
MT
1676 mutex_unlock(&n->dev.mutex);
1677 return 0;
6b1e6cc7
JW
1678
1679out_unlock:
1680 mutex_unlock(&n->dev.mutex);
1681 return -EFAULT;
3a4d5c94
MT
1682}
1683
b1ad8496
AH
1684static long vhost_net_set_owner(struct vhost_net *n)
1685{
1686 int r;
1687
1688 mutex_lock(&n->dev.mutex);
05c05351
MT
1689 if (vhost_dev_has_owner(&n->dev)) {
1690 r = -EBUSY;
1691 goto out;
1692 }
b1ad8496
AH
1693 r = vhost_net_set_ubuf_info(n);
1694 if (r)
1695 goto out;
1696 r = vhost_dev_set_owner(&n->dev);
1697 if (r)
1698 vhost_net_clear_ubuf_info(n);
1699 vhost_net_flush(n);
1700out:
1701 mutex_unlock(&n->dev.mutex);
1702 return r;
1703}
1704
3a4d5c94
MT
1705static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
1706 unsigned long arg)
1707{
1708 struct vhost_net *n = f->private_data;
1709 void __user *argp = (void __user *)arg;
1710 u64 __user *featurep = argp;
1711 struct vhost_vring_file backend;
1712 u64 features;
1713 int r;
d47effe1 1714
3a4d5c94
MT
1715 switch (ioctl) {
1716 case VHOST_NET_SET_BACKEND:
d3553a52
TY
1717 if (copy_from_user(&backend, argp, sizeof backend))
1718 return -EFAULT;
3a4d5c94
MT
1719 return vhost_net_set_backend(n, backend.index, backend.fd);
1720 case VHOST_GET_FEATURES:
0dd05a3b 1721 features = VHOST_NET_FEATURES;
d3553a52
TY
1722 if (copy_to_user(featurep, &features, sizeof features))
1723 return -EFAULT;
1724 return 0;
3a4d5c94 1725 case VHOST_SET_FEATURES:
d3553a52
TY
1726 if (copy_from_user(&features, featurep, sizeof features))
1727 return -EFAULT;
0dd05a3b 1728 if (features & ~VHOST_NET_FEATURES)
3a4d5c94
MT
1729 return -EOPNOTSUPP;
1730 return vhost_net_set_features(n, features);
429711ae
JW
1731 case VHOST_GET_BACKEND_FEATURES:
1732 features = VHOST_NET_BACKEND_FEATURES;
1733 if (copy_to_user(featurep, &features, sizeof(features)))
1734 return -EFAULT;
1735 return 0;
1736 case VHOST_SET_BACKEND_FEATURES:
1737 if (copy_from_user(&features, featurep, sizeof(features)))
1738 return -EFAULT;
1739 if (features & ~VHOST_NET_BACKEND_FEATURES)
1740 return -EOPNOTSUPP;
1741 return vhost_net_set_backend_features(n, features);
3a4d5c94
MT
1742 case VHOST_RESET_OWNER:
1743 return vhost_net_reset_owner(n);
b1ad8496
AH
1744 case VHOST_SET_OWNER:
1745 return vhost_net_set_owner(n);
3a4d5c94
MT
1746 default:
1747 mutex_lock(&n->dev.mutex);
935cdee7
MT
1748 r = vhost_dev_ioctl(&n->dev, ioctl, argp);
1749 if (r == -ENOIOCTLCMD)
1750 r = vhost_vring_ioctl(&n->dev, ioctl, argp);
1751 else
1752 vhost_net_flush(n);
3a4d5c94
MT
1753 mutex_unlock(&n->dev.mutex);
1754 return r;
1755 }
1756}
1757
1758#ifdef CONFIG_COMPAT
1759static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl,
1760 unsigned long arg)
1761{
1762 return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
1763}
1764#endif
1765
6b1e6cc7
JW
1766static ssize_t vhost_net_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
1767{
1768 struct file *file = iocb->ki_filp;
1769 struct vhost_net *n = file->private_data;
1770 struct vhost_dev *dev = &n->dev;
1771 int noblock = file->f_flags & O_NONBLOCK;
1772
1773 return vhost_chr_read_iter(dev, to, noblock);
1774}
1775
1776static ssize_t vhost_net_chr_write_iter(struct kiocb *iocb,
1777 struct iov_iter *from)
1778{
1779 struct file *file = iocb->ki_filp;
1780 struct vhost_net *n = file->private_data;
1781 struct vhost_dev *dev = &n->dev;
1782
1783 return vhost_chr_write_iter(dev, from);
1784}
1785
afc9a42b 1786static __poll_t vhost_net_chr_poll(struct file *file, poll_table *wait)
6b1e6cc7
JW
1787{
1788 struct vhost_net *n = file->private_data;
1789 struct vhost_dev *dev = &n->dev;
1790
1791 return vhost_chr_poll(file, dev, wait);
1792}
1793
373a83a6 1794static const struct file_operations vhost_net_fops = {
3a4d5c94
MT
1795 .owner = THIS_MODULE,
1796 .release = vhost_net_release,
6b1e6cc7
JW
1797 .read_iter = vhost_net_chr_read_iter,
1798 .write_iter = vhost_net_chr_write_iter,
1799 .poll = vhost_net_chr_poll,
3a4d5c94
MT
1800 .unlocked_ioctl = vhost_net_ioctl,
1801#ifdef CONFIG_COMPAT
1802 .compat_ioctl = vhost_net_compat_ioctl,
1803#endif
1804 .open = vhost_net_open,
6038f373 1805 .llseek = noop_llseek,
3a4d5c94
MT
1806};
1807
1808static struct miscdevice vhost_net_misc = {
7c7c7f01 1809 .minor = VHOST_NET_MINOR,
1810 .name = "vhost-net",
1811 .fops = &vhost_net_fops,
3a4d5c94
MT
1812};
1813
a8d3782f 1814static int vhost_net_init(void)
3a4d5c94 1815{
bab632d6 1816 if (experimental_zcopytx)
fe729a57 1817 vhost_net_enable_zcopy(VHOST_NET_VQ_TX);
c23f3445 1818 return misc_register(&vhost_net_misc);
3a4d5c94
MT
1819}
1820module_init(vhost_net_init);
1821
a8d3782f 1822static void vhost_net_exit(void)
3a4d5c94
MT
1823{
1824 misc_deregister(&vhost_net_misc);
3a4d5c94
MT
1825}
1826module_exit(vhost_net_exit);
1827
1828MODULE_VERSION("0.0.1");
1829MODULE_LICENSE("GPL v2");
1830MODULE_AUTHOR("Michael S. Tsirkin");
1831MODULE_DESCRIPTION("Host kernel accelerator for virtio net");
7c7c7f01 1832MODULE_ALIAS_MISCDEV(VHOST_NET_MINOR);
1833MODULE_ALIAS("devname:vhost-net");