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