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