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)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 {
597 struct vhost_net_virtqueue *rnvq = &net->vqs[VHOST_NET_VQ_RX];
598 struct vhost_virtqueue *rvq = &rnvq->vq;
599 struct vhost_virtqueue *tvq = &tnvq->vq;
600
601 int r = vhost_get_vq_desc(tvq, tvq->iov, ARRAY_SIZE(tvq->iov),
602 out_num, in_num, NULL, NULL);
603
604 if (r == tvq->num && tvq->busyloop_timeout) {
605 /* Flush batched packets first */
606 if (!vhost_sock_zcopy(vhost_vq_get_backend(tvq)))
607 vhost_tx_batch(net, tnvq,
608 vhost_vq_get_backend(tvq),
609 msghdr);
610
611 vhost_net_busy_poll(net, rvq, tvq, busyloop_intr, false);
612
613 r = vhost_get_vq_desc(tvq, tvq->iov, ARRAY_SIZE(tvq->iov),
614 out_num, in_num, NULL, NULL);
615 }
616
617 return r;
618 }
619
vhost_exceeds_maxpend(struct vhost_net * net)620 static bool vhost_exceeds_maxpend(struct vhost_net *net)
621 {
622 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
623 struct vhost_virtqueue *vq = &nvq->vq;
624
625 return (nvq->upend_idx + UIO_MAXIOV - nvq->done_idx) % UIO_MAXIOV >
626 min_t(unsigned int, VHOST_MAX_PEND, vq->num >> 2);
627 }
628
init_iov_iter(struct vhost_virtqueue * vq,struct iov_iter * iter,size_t hdr_size,int out)629 static size_t init_iov_iter(struct vhost_virtqueue *vq, struct iov_iter *iter,
630 size_t hdr_size, int out)
631 {
632 /* Skip header. TODO: support TSO. */
633 size_t len = iov_length(vq->iov, out);
634
635 iov_iter_init(iter, ITER_SOURCE, vq->iov, out, len);
636 iov_iter_advance(iter, hdr_size);
637
638 return iov_iter_count(iter);
639 }
640
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)641 static int get_tx_bufs(struct vhost_net *net,
642 struct vhost_net_virtqueue *nvq,
643 struct msghdr *msg,
644 unsigned int *out, unsigned int *in,
645 size_t *len, bool *busyloop_intr)
646 {
647 struct vhost_virtqueue *vq = &nvq->vq;
648 int ret;
649
650 ret = vhost_net_tx_get_vq_desc(net, nvq, out, in, msg, busyloop_intr);
651
652 if (ret < 0 || ret == vq->num)
653 return ret;
654
655 if (*in) {
656 vq_err(vq, "Unexpected descriptor format for TX: out %d, int %d\n",
657 *out, *in);
658 return -EFAULT;
659 }
660
661 /* Sanity check */
662 *len = init_iov_iter(vq, &msg->msg_iter, nvq->vhost_hlen, *out);
663 if (*len == 0) {
664 vq_err(vq, "Unexpected header len for TX: %zd expected %zd\n",
665 *len, nvq->vhost_hlen);
666 return -EFAULT;
667 }
668
669 return ret;
670 }
671
tx_can_batch(struct vhost_virtqueue * vq,size_t total_len)672 static bool tx_can_batch(struct vhost_virtqueue *vq, size_t total_len)
673 {
674 return total_len < VHOST_NET_WEIGHT &&
675 !vhost_vq_avail_empty(vq->dev, vq);
676 }
677
678 #define VHOST_NET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
679
vhost_net_build_xdp(struct vhost_net_virtqueue * nvq,struct iov_iter * from)680 static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq,
681 struct iov_iter *from)
682 {
683 struct vhost_virtqueue *vq = &nvq->vq;
684 struct vhost_net *net = container_of(vq->dev, struct vhost_net,
685 dev);
686 struct socket *sock = vhost_vq_get_backend(vq);
687 struct virtio_net_hdr *gso;
688 struct xdp_buff *xdp = &nvq->xdp[nvq->batched_xdp];
689 size_t len = iov_iter_count(from);
690 int headroom = vhost_sock_xdp(sock) ? XDP_PACKET_HEADROOM : 0;
691 int buflen = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
692 int pad = SKB_DATA_ALIGN(VHOST_NET_RX_PAD + headroom + nvq->sock_hlen);
693 int sock_hlen = nvq->sock_hlen;
694 void *buf;
695 int copied;
696 int ret;
697
698 if (unlikely(len < nvq->sock_hlen))
699 return -EFAULT;
700
701 if (SKB_DATA_ALIGN(len + pad) +
702 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) > PAGE_SIZE)
703 return -ENOSPC;
704
705 buflen += SKB_DATA_ALIGN(len + pad);
706 buf = page_frag_alloc_align(&net->pf_cache, buflen, GFP_KERNEL,
707 SMP_CACHE_BYTES);
708 if (unlikely(!buf))
709 return -ENOMEM;
710
711 copied = copy_from_iter(buf + pad - sock_hlen, len, from);
712 if (copied != len) {
713 ret = -EFAULT;
714 goto err;
715 }
716
717 gso = buf + pad - sock_hlen;
718
719 if (!sock_hlen)
720 memset(buf, 0, pad);
721
722 if ((gso->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
723 vhost16_to_cpu(vq, gso->csum_start) +
724 vhost16_to_cpu(vq, gso->csum_offset) + 2 >
725 vhost16_to_cpu(vq, gso->hdr_len)) {
726 gso->hdr_len = cpu_to_vhost16(vq,
727 vhost16_to_cpu(vq, gso->csum_start) +
728 vhost16_to_cpu(vq, gso->csum_offset) + 2);
729
730 if (vhost16_to_cpu(vq, gso->hdr_len) > len) {
731 ret = -EINVAL;
732 goto err;
733 }
734 }
735
736 /* pad contains sock_hlen */
737 memcpy(buf, buf + pad - sock_hlen, sock_hlen);
738
739 xdp_init_buff(xdp, buflen, NULL);
740 xdp_prepare_buff(xdp, buf, pad, len - sock_hlen, true);
741
742 ++nvq->batched_xdp;
743
744 return 0;
745
746 err:
747 page_frag_free(buf);
748 return ret;
749 }
750
handle_tx_copy(struct vhost_net * net,struct socket * sock)751 static void handle_tx_copy(struct vhost_net *net, struct socket *sock)
752 {
753 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
754 struct vhost_virtqueue *vq = &nvq->vq;
755 unsigned out, in;
756 int head;
757 struct msghdr msg = {
758 .msg_name = NULL,
759 .msg_namelen = 0,
760 .msg_control = NULL,
761 .msg_controllen = 0,
762 .msg_flags = MSG_DONTWAIT,
763 };
764 size_t len, total_len = 0;
765 int err;
766 int sent_pkts = 0;
767 bool sock_can_batch = (sock->sk->sk_sndbuf == INT_MAX);
768 bool in_order = vhost_has_feature(vq, VIRTIO_F_IN_ORDER);
769
770 do {
771 bool busyloop_intr = false;
772
773 if (nvq->done_idx == VHOST_NET_BATCH)
774 vhost_tx_batch(net, nvq, sock, &msg);
775
776 head = get_tx_bufs(net, nvq, &msg, &out, &in, &len,
777 &busyloop_intr);
778 /* On error, stop handling until the next kick. */
779 if (unlikely(head < 0))
780 break;
781 /* Nothing new? Wait for eventfd to tell us they refilled. */
782 if (head == vq->num) {
783 /* Flush batched packets to handle pending RX
784 * work (if busyloop_intr is set) and to avoid
785 * unnecessary virtqueue kicks.
786 */
787 vhost_tx_batch(net, nvq, sock, &msg);
788 if (unlikely(busyloop_intr)) {
789 vhost_poll_queue(&vq->poll);
790 } else if (unlikely(vhost_enable_notify(&net->dev,
791 vq))) {
792 vhost_disable_notify(&net->dev, vq);
793 continue;
794 }
795 break;
796 }
797
798 total_len += len;
799
800 /* For simplicity, TX batching is only enabled if
801 * sndbuf is unlimited.
802 */
803 if (sock_can_batch) {
804 err = vhost_net_build_xdp(nvq, &msg.msg_iter);
805 if (!err) {
806 goto done;
807 } else if (unlikely(err != -ENOSPC)) {
808 vhost_tx_batch(net, nvq, sock, &msg);
809 vhost_discard_vq_desc(vq, 1);
810 vhost_net_enable_vq(net, vq);
811 break;
812 }
813
814 if (nvq->batched_xdp) {
815 /* We can't build XDP buff, go for single
816 * packet path but let's flush batched
817 * packets.
818 */
819 vhost_tx_batch(net, nvq, sock, &msg);
820 }
821 msg.msg_control = NULL;
822 } else {
823 if (tx_can_batch(vq, total_len))
824 msg.msg_flags |= MSG_MORE;
825 else
826 msg.msg_flags &= ~MSG_MORE;
827 }
828
829 err = sock->ops->sendmsg(sock, &msg, len);
830 if (unlikely(err < 0)) {
831 if (err == -EAGAIN || err == -ENOMEM || err == -ENOBUFS) {
832 vhost_discard_vq_desc(vq, 1);
833 vhost_net_enable_vq(net, vq);
834 break;
835 }
836 pr_debug("Fail to send packet: err %d", err);
837 } else if (unlikely(err != len))
838 pr_debug("Truncated TX packet: len %d != %zd\n",
839 err, len);
840 done:
841 if (in_order) {
842 vq->heads[0].id = cpu_to_vhost32(vq, head);
843 } else {
844 vq->heads[nvq->done_idx].id = cpu_to_vhost32(vq, head);
845 vq->heads[nvq->done_idx].len = 0;
846 }
847 ++nvq->done_idx;
848 } while (likely(!vhost_exceeds_weight(vq, ++sent_pkts, total_len)));
849
850 vhost_tx_batch(net, nvq, sock, &msg);
851 }
852
handle_tx_zerocopy(struct vhost_net * net,struct socket * sock)853 static void handle_tx_zerocopy(struct vhost_net *net, struct socket *sock)
854 {
855 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
856 struct vhost_virtqueue *vq = &nvq->vq;
857 unsigned out, in;
858 int head;
859 struct msghdr msg = {
860 .msg_name = NULL,
861 .msg_namelen = 0,
862 .msg_control = NULL,
863 .msg_controllen = 0,
864 .msg_flags = MSG_DONTWAIT,
865 };
866 struct tun_msg_ctl ctl;
867 size_t len, total_len = 0;
868 int err;
869 struct vhost_net_ubuf_ref *ubufs;
870 struct ubuf_info_msgzc *ubuf;
871 bool zcopy_used;
872 int sent_pkts = 0;
873
874 do {
875 bool busyloop_intr;
876
877 /* Release DMAs done buffers first */
878 vhost_zerocopy_signal_used(net, vq);
879
880 busyloop_intr = false;
881 head = get_tx_bufs(net, nvq, &msg, &out, &in, &len,
882 &busyloop_intr);
883 /* On error, stop handling until the next kick. */
884 if (unlikely(head < 0))
885 break;
886 /* Nothing new? Wait for eventfd to tell us they refilled. */
887 if (head == vq->num) {
888 if (unlikely(busyloop_intr)) {
889 vhost_poll_queue(&vq->poll);
890 } else if (unlikely(vhost_enable_notify(&net->dev, vq))) {
891 vhost_disable_notify(&net->dev, vq);
892 continue;
893 }
894 break;
895 }
896
897 zcopy_used = len >= VHOST_GOODCOPY_LEN
898 && !vhost_exceeds_maxpend(net)
899 && vhost_net_tx_select_zcopy(net);
900
901 /* use msg_control to pass vhost zerocopy ubuf info to skb */
902 if (zcopy_used) {
903 ubuf = nvq->ubuf_info + nvq->upend_idx;
904 vq->heads[nvq->upend_idx].id = cpu_to_vhost32(vq, head);
905 vq->heads[nvq->upend_idx].len = VHOST_DMA_IN_PROGRESS;
906 ubuf->ctx = nvq->ubufs;
907 ubuf->desc = nvq->upend_idx;
908 ubuf->ubuf.ops = &vhost_ubuf_ops;
909 ubuf->ubuf.flags = SKBFL_ZEROCOPY_FRAG;
910 refcount_set(&ubuf->ubuf.refcnt, 1);
911 msg.msg_control = &ctl;
912 ctl.type = TUN_MSG_UBUF;
913 ctl.ptr = &ubuf->ubuf;
914 msg.msg_controllen = sizeof(ctl);
915 ubufs = nvq->ubufs;
916 atomic_inc(&ubufs->refcount);
917 nvq->upend_idx = (nvq->upend_idx + 1) % UIO_MAXIOV;
918 } else {
919 msg.msg_control = NULL;
920 ubufs = NULL;
921 }
922 total_len += len;
923 if (tx_can_batch(vq, total_len) &&
924 likely(!vhost_exceeds_maxpend(net))) {
925 msg.msg_flags |= MSG_MORE;
926 } else {
927 msg.msg_flags &= ~MSG_MORE;
928 }
929
930 err = sock->ops->sendmsg(sock, &msg, len);
931 if (unlikely(err < 0)) {
932 bool retry = err == -EAGAIN || err == -ENOMEM || err == -ENOBUFS;
933
934 if (zcopy_used) {
935 if (vq->heads[ubuf->desc].len == VHOST_DMA_IN_PROGRESS)
936 vhost_net_ubuf_put(ubufs);
937 if (retry)
938 nvq->upend_idx = ((unsigned)nvq->upend_idx - 1)
939 % UIO_MAXIOV;
940 else
941 vq->heads[ubuf->desc].len = VHOST_DMA_DONE_LEN;
942 }
943 if (retry) {
944 vhost_discard_vq_desc(vq, 1);
945 vhost_net_enable_vq(net, vq);
946 break;
947 }
948 pr_debug("Fail to send packet: err %d", err);
949 } else if (unlikely(err != len))
950 pr_debug("Truncated TX packet: "
951 " len %d != %zd\n", err, len);
952 if (!zcopy_used)
953 vhost_add_used_and_signal(&net->dev, vq, head, 0);
954 else
955 vhost_zerocopy_signal_used(net, vq);
956 vhost_net_tx_packet(net);
957 } while (likely(!vhost_exceeds_weight(vq, ++sent_pkts, total_len)));
958 }
959
960 /* Expects to be always run from workqueue - which acts as
961 * read-size critical section for our kind of RCU. */
handle_tx(struct vhost_net * net)962 static void handle_tx(struct vhost_net *net)
963 {
964 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
965 struct vhost_virtqueue *vq = &nvq->vq;
966 struct socket *sock;
967
968 mutex_lock_nested(&vq->mutex, VHOST_NET_VQ_TX);
969 sock = vhost_vq_get_backend(vq);
970 if (!sock)
971 goto out;
972
973 if (!vq_meta_prefetch(vq))
974 goto out;
975
976 vhost_disable_notify(&net->dev, vq);
977 vhost_net_disable_vq(net, vq);
978
979 if (vhost_sock_zcopy(sock))
980 handle_tx_zerocopy(net, sock);
981 else
982 handle_tx_copy(net, sock);
983
984 out:
985 mutex_unlock(&vq->mutex);
986 }
987
peek_head_len(struct vhost_net_virtqueue * rvq,struct sock * sk)988 static int peek_head_len(struct vhost_net_virtqueue *rvq, struct sock *sk)
989 {
990 struct sk_buff *head;
991 int len = 0;
992 unsigned long flags;
993
994 if (rvq->rx_ring)
995 return vhost_net_buf_peek(rvq);
996
997 spin_lock_irqsave(&sk->sk_receive_queue.lock, flags);
998 head = skb_peek(&sk->sk_receive_queue);
999 if (likely(head)) {
1000 len = head->len;
1001 if (skb_vlan_tag_present(head))
1002 len += VLAN_HLEN;
1003 }
1004
1005 spin_unlock_irqrestore(&sk->sk_receive_queue.lock, flags);
1006 return len;
1007 }
1008
vhost_net_rx_peek_head_len(struct vhost_net * net,struct sock * sk,bool * busyloop_intr,unsigned int * count)1009 static int vhost_net_rx_peek_head_len(struct vhost_net *net, struct sock *sk,
1010 bool *busyloop_intr, unsigned int *count)
1011 {
1012 struct vhost_net_virtqueue *rnvq = &net->vqs[VHOST_NET_VQ_RX];
1013 struct vhost_net_virtqueue *tnvq = &net->vqs[VHOST_NET_VQ_TX];
1014 struct vhost_virtqueue *rvq = &rnvq->vq;
1015 struct vhost_virtqueue *tvq = &tnvq->vq;
1016 int len = peek_head_len(rnvq, sk);
1017
1018 if (!len && rvq->busyloop_timeout) {
1019 /* Flush batched heads first */
1020 vhost_net_signal_used(rnvq, *count);
1021 *count = 0;
1022 /* Both tx vq and rx socket were polled here */
1023 vhost_net_busy_poll(net, rvq, tvq, busyloop_intr, true);
1024
1025 len = peek_head_len(rnvq, sk);
1026 }
1027
1028 return len;
1029 }
1030
1031 /* This is a multi-buffer version of vhost_get_desc, that works if
1032 * vq has read descriptors only.
1033 * @nvq - the relevant vhost_net virtqueue
1034 * @datalen - data length we'll be reading
1035 * @iovcount - returned count of io vectors we fill
1036 * @log - vhost log
1037 * @log_num - log offset
1038 * @quota - headcount quota, 1 for big buffer
1039 * returns number of buffer heads allocated, negative on error
1040 */
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)1041 static int get_rx_bufs(struct vhost_net_virtqueue *nvq,
1042 struct vring_used_elem *heads,
1043 u16 *nheads,
1044 int datalen,
1045 unsigned *iovcount,
1046 struct vhost_log *log,
1047 unsigned *log_num,
1048 unsigned int quota)
1049 {
1050 struct vhost_virtqueue *vq = &nvq->vq;
1051 bool in_order = vhost_has_feature(vq, VIRTIO_F_IN_ORDER);
1052 unsigned int out, in;
1053 int seg = 0;
1054 int headcount = 0;
1055 unsigned d;
1056 int r, nlogs = 0;
1057 /* len is always initialized before use since we are always called with
1058 * datalen > 0.
1059 */
1060 u32 len;
1061
1062 while (datalen > 0 && headcount < quota) {
1063 if (unlikely(seg >= UIO_MAXIOV)) {
1064 r = -ENOBUFS;
1065 goto err;
1066 }
1067 r = vhost_get_vq_desc(vq, vq->iov + seg,
1068 ARRAY_SIZE(vq->iov) - seg, &out,
1069 &in, log, log_num);
1070 if (unlikely(r < 0))
1071 goto err;
1072
1073 d = r;
1074 if (d == vq->num) {
1075 r = 0;
1076 goto err;
1077 }
1078 if (unlikely(out || in <= 0)) {
1079 vq_err(vq, "unexpected descriptor format for RX: "
1080 "out %d, in %d\n", out, in);
1081 r = -EINVAL;
1082 goto err;
1083 }
1084 if (unlikely(log)) {
1085 nlogs += *log_num;
1086 log += *log_num;
1087 }
1088 len = iov_length(vq->iov + seg, in);
1089 if (!in_order) {
1090 heads[headcount].id = cpu_to_vhost32(vq, d);
1091 heads[headcount].len = cpu_to_vhost32(vq, len);
1092 }
1093 ++headcount;
1094 datalen -= len;
1095 seg += in;
1096 }
1097
1098 *iovcount = seg;
1099 if (unlikely(log))
1100 *log_num = nlogs;
1101
1102 /* Detect overrun */
1103 if (unlikely(datalen > 0)) {
1104 r = UIO_MAXIOV + 1;
1105 goto err;
1106 }
1107
1108 if (!in_order)
1109 heads[headcount - 1].len = cpu_to_vhost32(vq, len + datalen);
1110 else {
1111 heads[0].len = cpu_to_vhost32(vq, len + datalen);
1112 heads[0].id = cpu_to_vhost32(vq, d);
1113 nheads[0] = headcount;
1114 }
1115
1116 return headcount;
1117 err:
1118 vhost_discard_vq_desc(vq, headcount);
1119 return r;
1120 }
1121
1122 /* Expects to be always run from workqueue - which acts as
1123 * read-size critical section for our kind of RCU. */
handle_rx(struct vhost_net * net)1124 static void handle_rx(struct vhost_net *net)
1125 {
1126 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_RX];
1127 struct vhost_virtqueue *vq = &nvq->vq;
1128 bool in_order = vhost_has_feature(vq, VIRTIO_F_IN_ORDER);
1129 unsigned int count = 0;
1130 unsigned in, log;
1131 struct vhost_log *vq_log;
1132 struct msghdr msg = {
1133 .msg_name = NULL,
1134 .msg_namelen = 0,
1135 .msg_control = NULL, /* FIXME: get and handle RX aux data. */
1136 .msg_controllen = 0,
1137 .msg_flags = MSG_DONTWAIT,
1138 };
1139 struct virtio_net_hdr hdr = {
1140 .flags = 0,
1141 .gso_type = VIRTIO_NET_HDR_GSO_NONE
1142 };
1143 size_t total_len = 0;
1144 int err, mergeable;
1145 s16 headcount;
1146 size_t vhost_hlen, sock_hlen;
1147 size_t vhost_len, sock_len;
1148 bool busyloop_intr = false;
1149 bool set_num_buffers;
1150 struct socket *sock;
1151 struct iov_iter fixup;
1152 __virtio16 num_buffers;
1153 int recv_pkts = 0;
1154
1155 mutex_lock_nested(&vq->mutex, VHOST_NET_VQ_RX);
1156 sock = vhost_vq_get_backend(vq);
1157 if (!sock)
1158 goto out;
1159
1160 if (!vq_meta_prefetch(vq))
1161 goto out;
1162
1163 vhost_disable_notify(&net->dev, vq);
1164 vhost_net_disable_vq(net, vq);
1165
1166 vhost_hlen = nvq->vhost_hlen;
1167 sock_hlen = nvq->sock_hlen;
1168
1169 vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ?
1170 vq->log : NULL;
1171 mergeable = vhost_has_feature(vq, VIRTIO_NET_F_MRG_RXBUF);
1172 set_num_buffers = mergeable ||
1173 vhost_has_feature(vq, VIRTIO_F_VERSION_1);
1174
1175 do {
1176 sock_len = vhost_net_rx_peek_head_len(net, sock->sk,
1177 &busyloop_intr, &count);
1178 if (!sock_len)
1179 break;
1180 sock_len += sock_hlen;
1181 vhost_len = sock_len + vhost_hlen;
1182 headcount = get_rx_bufs(nvq, vq->heads + count,
1183 vq->nheads + count,
1184 vhost_len, &in, vq_log, &log,
1185 likely(mergeable) ? UIO_MAXIOV : 1);
1186 /* On error, stop handling until the next kick. */
1187 if (unlikely(headcount < 0))
1188 goto out;
1189 /* OK, now we need to know about added descriptors. */
1190 if (!headcount) {
1191 if (unlikely(busyloop_intr)) {
1192 vhost_poll_queue(&vq->poll);
1193 } else if (unlikely(vhost_enable_notify(&net->dev, vq))) {
1194 /* They have slipped one in as we were
1195 * doing that: check again. */
1196 vhost_disable_notify(&net->dev, vq);
1197 continue;
1198 }
1199 /* Nothing new? Wait for eventfd to tell us
1200 * they refilled. */
1201 goto out;
1202 }
1203 busyloop_intr = false;
1204 if (nvq->rx_ring)
1205 msg.msg_control = vhost_net_buf_consume(&nvq->rxq);
1206 /* On overrun, truncate and discard */
1207 if (unlikely(headcount > UIO_MAXIOV)) {
1208 iov_iter_init(&msg.msg_iter, ITER_DEST, vq->iov, 1, 1);
1209 err = sock->ops->recvmsg(sock, &msg,
1210 1, MSG_DONTWAIT | MSG_TRUNC);
1211 pr_debug("Discarded rx packet: len %zd\n", sock_len);
1212 continue;
1213 }
1214 /* We don't need to be notified again. */
1215 iov_iter_init(&msg.msg_iter, ITER_DEST, vq->iov, in, vhost_len);
1216 fixup = msg.msg_iter;
1217 if (unlikely((vhost_hlen))) {
1218 /* We will supply the header ourselves
1219 * TODO: support TSO.
1220 */
1221 iov_iter_advance(&msg.msg_iter, vhost_hlen);
1222 }
1223 err = sock->ops->recvmsg(sock, &msg,
1224 sock_len, MSG_DONTWAIT | MSG_TRUNC);
1225 /* Userspace might have consumed the packet meanwhile:
1226 * it's not supposed to do this usually, but might be hard
1227 * to prevent. Discard data we got (if any) and keep going. */
1228 if (unlikely(err != sock_len)) {
1229 pr_debug("Discarded rx packet: "
1230 " len %d, expected %zd\n", err, sock_len);
1231 vhost_discard_vq_desc(vq, headcount);
1232 continue;
1233 }
1234 /* Supply virtio_net_hdr if VHOST_NET_F_VIRTIO_NET_HDR */
1235 if (unlikely(vhost_hlen)) {
1236 if (copy_to_iter(&hdr, sizeof(hdr),
1237 &fixup) != sizeof(hdr)) {
1238 vq_err(vq, "Unable to write vnet_hdr "
1239 "at addr %p\n", vq->iov->iov_base);
1240 goto out;
1241 }
1242 } else {
1243 /* Header came from socket; we'll need to patch
1244 * ->num_buffers over if VIRTIO_NET_F_MRG_RXBUF
1245 */
1246 iov_iter_advance(&fixup, sizeof(hdr));
1247 }
1248 /* TODO: Should check and handle checksum. */
1249
1250 num_buffers = cpu_to_vhost16(vq, headcount);
1251 if (likely(set_num_buffers) &&
1252 copy_to_iter(&num_buffers, sizeof num_buffers,
1253 &fixup) != sizeof num_buffers) {
1254 vq_err(vq, "Failed num_buffers write");
1255 vhost_discard_vq_desc(vq, headcount);
1256 goto out;
1257 }
1258 nvq->done_idx += headcount;
1259 count += in_order ? 1 : headcount;
1260 if (nvq->done_idx > VHOST_NET_BATCH) {
1261 vhost_net_signal_used(nvq, count);
1262 count = 0;
1263 }
1264 if (unlikely(vq_log))
1265 vhost_log_write(vq, vq_log, log, vhost_len,
1266 vq->iov, in);
1267 total_len += vhost_len;
1268 } while (likely(!vhost_exceeds_weight(vq, ++recv_pkts, total_len)));
1269
1270 if (unlikely(busyloop_intr))
1271 vhost_poll_queue(&vq->poll);
1272 else if (!sock_len)
1273 vhost_net_enable_vq(net, vq);
1274 out:
1275 vhost_net_signal_used(nvq, count);
1276 mutex_unlock(&vq->mutex);
1277 }
1278
handle_tx_kick(struct vhost_work * work)1279 static void handle_tx_kick(struct vhost_work *work)
1280 {
1281 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1282 poll.work);
1283 struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
1284
1285 handle_tx(net);
1286 }
1287
handle_rx_kick(struct vhost_work * work)1288 static void handle_rx_kick(struct vhost_work *work)
1289 {
1290 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1291 poll.work);
1292 struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
1293
1294 handle_rx(net);
1295 }
1296
handle_tx_net(struct vhost_work * work)1297 static void handle_tx_net(struct vhost_work *work)
1298 {
1299 struct vhost_net *net = container_of(work, struct vhost_net,
1300 poll[VHOST_NET_VQ_TX].work);
1301 handle_tx(net);
1302 }
1303
handle_rx_net(struct vhost_work * work)1304 static void handle_rx_net(struct vhost_work *work)
1305 {
1306 struct vhost_net *net = container_of(work, struct vhost_net,
1307 poll[VHOST_NET_VQ_RX].work);
1308 handle_rx(net);
1309 }
1310
vhost_net_open(struct inode * inode,struct file * f)1311 static int vhost_net_open(struct inode *inode, struct file *f)
1312 {
1313 struct vhost_net *n;
1314 struct vhost_dev *dev;
1315 struct vhost_virtqueue **vqs;
1316 void **queue;
1317 struct xdp_buff *xdp;
1318 int i;
1319
1320 n = kvmalloc(sizeof *n, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
1321 if (!n)
1322 return -ENOMEM;
1323 vqs = kmalloc_array(VHOST_NET_VQ_MAX, sizeof(*vqs), GFP_KERNEL);
1324 if (!vqs) {
1325 kvfree(n);
1326 return -ENOMEM;
1327 }
1328
1329 queue = kmalloc_array(VHOST_NET_BATCH, sizeof(void *),
1330 GFP_KERNEL);
1331 if (!queue) {
1332 kfree(vqs);
1333 kvfree(n);
1334 return -ENOMEM;
1335 }
1336 n->vqs[VHOST_NET_VQ_RX].rxq.queue = queue;
1337
1338 xdp = kmalloc_array(VHOST_NET_BATCH, sizeof(*xdp), GFP_KERNEL);
1339 if (!xdp) {
1340 kfree(vqs);
1341 kvfree(n);
1342 kfree(queue);
1343 return -ENOMEM;
1344 }
1345 n->vqs[VHOST_NET_VQ_TX].xdp = xdp;
1346
1347 dev = &n->dev;
1348 vqs[VHOST_NET_VQ_TX] = &n->vqs[VHOST_NET_VQ_TX].vq;
1349 vqs[VHOST_NET_VQ_RX] = &n->vqs[VHOST_NET_VQ_RX].vq;
1350 n->vqs[VHOST_NET_VQ_TX].vq.handle_kick = handle_tx_kick;
1351 n->vqs[VHOST_NET_VQ_RX].vq.handle_kick = handle_rx_kick;
1352 for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
1353 n->vqs[i].ubufs = NULL;
1354 n->vqs[i].ubuf_info = NULL;
1355 n->vqs[i].upend_idx = 0;
1356 n->vqs[i].done_idx = 0;
1357 n->vqs[i].batched_xdp = 0;
1358 n->vqs[i].vhost_hlen = 0;
1359 n->vqs[i].sock_hlen = 0;
1360 n->vqs[i].rx_ring = NULL;
1361 vhost_net_buf_init(&n->vqs[i].rxq);
1362 }
1363 vhost_dev_init(dev, vqs, VHOST_NET_VQ_MAX,
1364 UIO_MAXIOV + VHOST_NET_BATCH,
1365 VHOST_NET_PKT_WEIGHT, VHOST_NET_WEIGHT, true,
1366 NULL);
1367
1368 vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, EPOLLOUT, dev,
1369 vqs[VHOST_NET_VQ_TX]);
1370 vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, EPOLLIN, dev,
1371 vqs[VHOST_NET_VQ_RX]);
1372
1373 f->private_data = n;
1374 page_frag_cache_init(&n->pf_cache);
1375
1376 return 0;
1377 }
1378
vhost_net_stop_vq(struct vhost_net * n,struct vhost_virtqueue * vq)1379 static struct socket *vhost_net_stop_vq(struct vhost_net *n,
1380 struct vhost_virtqueue *vq)
1381 {
1382 struct socket *sock;
1383 struct vhost_net_virtqueue *nvq =
1384 container_of(vq, struct vhost_net_virtqueue, vq);
1385
1386 mutex_lock(&vq->mutex);
1387 sock = vhost_vq_get_backend(vq);
1388 vhost_net_disable_vq(n, vq);
1389 vhost_vq_set_backend(vq, NULL);
1390 vhost_net_buf_unproduce(nvq);
1391 nvq->rx_ring = NULL;
1392 mutex_unlock(&vq->mutex);
1393 return sock;
1394 }
1395
vhost_net_stop(struct vhost_net * n,struct socket ** tx_sock,struct socket ** rx_sock)1396 static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock,
1397 struct socket **rx_sock)
1398 {
1399 *tx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_TX].vq);
1400 *rx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_RX].vq);
1401 }
1402
vhost_net_flush(struct vhost_net * n)1403 static void vhost_net_flush(struct vhost_net *n)
1404 {
1405 vhost_dev_flush(&n->dev);
1406 if (n->vqs[VHOST_NET_VQ_TX].ubufs) {
1407 mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
1408 n->tx_flush = true;
1409 mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
1410 /* Wait for all lower device DMAs done. */
1411 vhost_net_ubuf_put_and_wait(n->vqs[VHOST_NET_VQ_TX].ubufs);
1412 mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
1413 n->tx_flush = false;
1414 atomic_set(&n->vqs[VHOST_NET_VQ_TX].ubufs->refcount, 1);
1415 mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
1416 }
1417 }
1418
vhost_net_release(struct inode * inode,struct file * f)1419 static int vhost_net_release(struct inode *inode, struct file *f)
1420 {
1421 struct vhost_net *n = f->private_data;
1422 struct socket *tx_sock;
1423 struct socket *rx_sock;
1424
1425 vhost_net_stop(n, &tx_sock, &rx_sock);
1426 vhost_net_flush(n);
1427 vhost_dev_stop(&n->dev);
1428 vhost_dev_cleanup(&n->dev);
1429 vhost_net_vq_reset(n);
1430 if (tx_sock)
1431 sockfd_put(tx_sock);
1432 if (rx_sock)
1433 sockfd_put(rx_sock);
1434 /* Make sure no callbacks are outstanding */
1435 synchronize_rcu();
1436 /* We do an extra flush before freeing memory,
1437 * since jobs can re-queue themselves. */
1438 vhost_net_flush(n);
1439 kfree(n->vqs[VHOST_NET_VQ_RX].rxq.queue);
1440 kfree(n->vqs[VHOST_NET_VQ_TX].xdp);
1441 kfree(n->dev.vqs);
1442 page_frag_cache_drain(&n->pf_cache);
1443 kvfree(n);
1444 return 0;
1445 }
1446
get_raw_socket(int fd)1447 static struct socket *get_raw_socket(int fd)
1448 {
1449 int r;
1450 struct socket *sock = sockfd_lookup(fd, &r);
1451
1452 if (!sock)
1453 return ERR_PTR(-ENOTSOCK);
1454
1455 /* Parameter checking */
1456 if (sock->sk->sk_type != SOCK_RAW) {
1457 r = -ESOCKTNOSUPPORT;
1458 goto err;
1459 }
1460
1461 if (sock->sk->sk_family != AF_PACKET) {
1462 r = -EPFNOSUPPORT;
1463 goto err;
1464 }
1465 return sock;
1466 err:
1467 sockfd_put(sock);
1468 return ERR_PTR(r);
1469 }
1470
get_tap_ptr_ring(struct file * file)1471 static struct ptr_ring *get_tap_ptr_ring(struct file *file)
1472 {
1473 struct ptr_ring *ring;
1474 ring = tun_get_tx_ring(file);
1475 if (!IS_ERR(ring))
1476 goto out;
1477 ring = tap_get_ptr_ring(file);
1478 if (!IS_ERR(ring))
1479 goto out;
1480 ring = NULL;
1481 out:
1482 return ring;
1483 }
1484
get_tap_socket(int fd)1485 static struct socket *get_tap_socket(int fd)
1486 {
1487 struct file *file = fget(fd);
1488 struct socket *sock;
1489
1490 if (!file)
1491 return ERR_PTR(-EBADF);
1492 sock = tun_get_socket(file);
1493 if (!IS_ERR(sock))
1494 return sock;
1495 sock = tap_get_socket(file);
1496 if (IS_ERR(sock))
1497 fput(file);
1498 return sock;
1499 }
1500
get_socket(int fd)1501 static struct socket *get_socket(int fd)
1502 {
1503 struct socket *sock;
1504
1505 /* special case to disable backend */
1506 if (fd == -1)
1507 return NULL;
1508 sock = get_raw_socket(fd);
1509 if (!IS_ERR(sock))
1510 return sock;
1511 sock = get_tap_socket(fd);
1512 if (!IS_ERR(sock))
1513 return sock;
1514 return ERR_PTR(-ENOTSOCK);
1515 }
1516
vhost_net_set_backend(struct vhost_net * n,unsigned index,int fd)1517 static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
1518 {
1519 struct socket *sock, *oldsock;
1520 struct vhost_virtqueue *vq;
1521 struct vhost_net_virtqueue *nvq;
1522 struct vhost_net_ubuf_ref *ubufs, *oldubufs = NULL;
1523 int r;
1524
1525 mutex_lock(&n->dev.mutex);
1526 r = vhost_dev_check_owner(&n->dev);
1527 if (r)
1528 goto err;
1529
1530 if (index >= VHOST_NET_VQ_MAX) {
1531 r = -ENOBUFS;
1532 goto err;
1533 }
1534 vq = &n->vqs[index].vq;
1535 nvq = &n->vqs[index];
1536 mutex_lock(&vq->mutex);
1537
1538 if (fd == -1)
1539 vhost_clear_msg(&n->dev);
1540
1541 /* Verify that ring has been setup correctly. */
1542 if (!vhost_vq_access_ok(vq)) {
1543 r = -EFAULT;
1544 goto err_vq;
1545 }
1546 sock = get_socket(fd);
1547 if (IS_ERR(sock)) {
1548 r = PTR_ERR(sock);
1549 goto err_vq;
1550 }
1551
1552 /* start polling new socket */
1553 oldsock = vhost_vq_get_backend(vq);
1554 if (sock != oldsock) {
1555 ubufs = vhost_net_ubuf_alloc(vq,
1556 sock && vhost_sock_zcopy(sock));
1557 if (IS_ERR(ubufs)) {
1558 r = PTR_ERR(ubufs);
1559 goto err_ubufs;
1560 }
1561
1562 vhost_net_disable_vq(n, vq);
1563 vhost_vq_set_backend(vq, sock);
1564 vhost_net_buf_unproduce(nvq);
1565 r = vhost_vq_init_access(vq);
1566 if (r)
1567 goto err_used;
1568 r = vhost_net_enable_vq(n, vq);
1569 if (r)
1570 goto err_used;
1571 if (index == VHOST_NET_VQ_RX) {
1572 if (sock)
1573 nvq->rx_ring = get_tap_ptr_ring(sock->file);
1574 else
1575 nvq->rx_ring = NULL;
1576 }
1577
1578 oldubufs = nvq->ubufs;
1579 nvq->ubufs = ubufs;
1580
1581 n->tx_packets = 0;
1582 n->tx_zcopy_err = 0;
1583 n->tx_flush = false;
1584 }
1585
1586 mutex_unlock(&vq->mutex);
1587
1588 if (oldubufs) {
1589 vhost_net_ubuf_put_wait_and_free(oldubufs);
1590 mutex_lock(&vq->mutex);
1591 vhost_zerocopy_signal_used(n, vq);
1592 mutex_unlock(&vq->mutex);
1593 }
1594
1595 if (oldsock) {
1596 vhost_dev_flush(&n->dev);
1597 sockfd_put(oldsock);
1598 }
1599
1600 mutex_unlock(&n->dev.mutex);
1601 return 0;
1602
1603 err_used:
1604 vhost_vq_set_backend(vq, oldsock);
1605 vhost_net_enable_vq(n, vq);
1606 if (ubufs)
1607 vhost_net_ubuf_put_wait_and_free(ubufs);
1608 err_ubufs:
1609 if (sock)
1610 sockfd_put(sock);
1611 err_vq:
1612 mutex_unlock(&vq->mutex);
1613 err:
1614 mutex_unlock(&n->dev.mutex);
1615 return r;
1616 }
1617
vhost_net_reset_owner(struct vhost_net * n)1618 static long vhost_net_reset_owner(struct vhost_net *n)
1619 {
1620 struct socket *tx_sock = NULL;
1621 struct socket *rx_sock = NULL;
1622 long err;
1623 struct vhost_iotlb *umem;
1624
1625 mutex_lock(&n->dev.mutex);
1626 err = vhost_dev_check_owner(&n->dev);
1627 if (err)
1628 goto done;
1629 umem = vhost_dev_reset_owner_prepare();
1630 if (!umem) {
1631 err = -ENOMEM;
1632 goto done;
1633 }
1634 vhost_net_stop(n, &tx_sock, &rx_sock);
1635 vhost_net_flush(n);
1636 vhost_dev_stop(&n->dev);
1637 vhost_dev_reset_owner(&n->dev, umem);
1638 vhost_net_vq_reset(n);
1639 done:
1640 mutex_unlock(&n->dev.mutex);
1641 if (tx_sock)
1642 sockfd_put(tx_sock);
1643 if (rx_sock)
1644 sockfd_put(rx_sock);
1645 return err;
1646 }
1647
vhost_net_set_features(struct vhost_net * n,const u64 * features)1648 static int vhost_net_set_features(struct vhost_net *n, const u64 *features)
1649 {
1650 size_t vhost_hlen, sock_hlen, hdr_len;
1651 int i;
1652
1653 hdr_len = virtio_features_test_bit(features, VIRTIO_NET_F_MRG_RXBUF) ||
1654 virtio_features_test_bit(features, VIRTIO_F_VERSION_1) ?
1655 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
1656 sizeof(struct virtio_net_hdr);
1657
1658 if (virtio_features_test_bit(features,
1659 VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO) ||
1660 virtio_features_test_bit(features,
1661 VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO))
1662 hdr_len = sizeof(struct virtio_net_hdr_v1_hash_tunnel);
1663
1664 if (virtio_features_test_bit(features, VHOST_NET_F_VIRTIO_NET_HDR)) {
1665 /* vhost provides vnet_hdr */
1666 vhost_hlen = hdr_len;
1667 sock_hlen = 0;
1668 } else {
1669 /* socket provides vnet_hdr */
1670 vhost_hlen = 0;
1671 sock_hlen = hdr_len;
1672 }
1673 mutex_lock(&n->dev.mutex);
1674 if (virtio_features_test_bit(features, VHOST_F_LOG_ALL) &&
1675 !vhost_log_access_ok(&n->dev))
1676 goto out_unlock;
1677
1678 if (virtio_features_test_bit(features, VIRTIO_F_ACCESS_PLATFORM)) {
1679 if (vhost_init_device_iotlb(&n->dev))
1680 goto out_unlock;
1681 }
1682
1683 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
1684 mutex_lock(&n->vqs[i].vq.mutex);
1685 virtio_features_copy(n->vqs[i].vq.acked_features_array,
1686 features);
1687 n->vqs[i].vhost_hlen = vhost_hlen;
1688 n->vqs[i].sock_hlen = sock_hlen;
1689 mutex_unlock(&n->vqs[i].vq.mutex);
1690 }
1691 mutex_unlock(&n->dev.mutex);
1692 return 0;
1693
1694 out_unlock:
1695 mutex_unlock(&n->dev.mutex);
1696 return -EFAULT;
1697 }
1698
vhost_net_set_owner(struct vhost_net * n)1699 static long vhost_net_set_owner(struct vhost_net *n)
1700 {
1701 int r;
1702
1703 mutex_lock(&n->dev.mutex);
1704 if (vhost_dev_has_owner(&n->dev)) {
1705 r = -EBUSY;
1706 goto out;
1707 }
1708 r = vhost_net_set_ubuf_info(n);
1709 if (r)
1710 goto out;
1711 r = vhost_dev_set_owner(&n->dev);
1712 if (r)
1713 vhost_net_clear_ubuf_info(n);
1714 vhost_net_flush(n);
1715 out:
1716 mutex_unlock(&n->dev.mutex);
1717 return r;
1718 }
1719
vhost_net_ioctl(struct file * f,unsigned int ioctl,unsigned long arg)1720 static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
1721 unsigned long arg)
1722 {
1723 u64 all_features[VIRTIO_FEATURES_DWORDS];
1724 struct vhost_net *n = f->private_data;
1725 void __user *argp = (void __user *)arg;
1726 u64 __user *featurep = argp;
1727 struct vhost_vring_file backend;
1728 u64 features, count, copied;
1729 int r, i;
1730
1731 switch (ioctl) {
1732 case VHOST_NET_SET_BACKEND:
1733 if (copy_from_user(&backend, argp, sizeof backend))
1734 return -EFAULT;
1735 return vhost_net_set_backend(n, backend.index, backend.fd);
1736 case VHOST_GET_FEATURES:
1737 features = vhost_net_features[0];
1738 if (copy_to_user(featurep, &features, sizeof features))
1739 return -EFAULT;
1740 return 0;
1741 case VHOST_SET_FEATURES:
1742 if (copy_from_user(&features, featurep, sizeof features))
1743 return -EFAULT;
1744 if (features & ~vhost_net_features[0])
1745 return -EOPNOTSUPP;
1746
1747 virtio_features_from_u64(all_features, features);
1748 return vhost_net_set_features(n, all_features);
1749 case VHOST_GET_FEATURES_ARRAY:
1750 if (copy_from_user(&count, featurep, sizeof(count)))
1751 return -EFAULT;
1752
1753 /* Copy the net features, up to the user-provided buffer size */
1754 argp += sizeof(u64);
1755 copied = min(count, VIRTIO_FEATURES_DWORDS);
1756 if (copy_to_user(argp, vhost_net_features,
1757 copied * sizeof(u64)))
1758 return -EFAULT;
1759
1760 /* Zero the trailing space provided by user-space, if any */
1761 if (clear_user(argp, size_mul(count - copied, sizeof(u64))))
1762 return -EFAULT;
1763 return 0;
1764 case VHOST_SET_FEATURES_ARRAY:
1765 if (copy_from_user(&count, featurep, sizeof(count)))
1766 return -EFAULT;
1767
1768 virtio_features_zero(all_features);
1769 argp += sizeof(u64);
1770 copied = min(count, VIRTIO_FEATURES_DWORDS);
1771 if (copy_from_user(all_features, argp, copied * sizeof(u64)))
1772 return -EFAULT;
1773
1774 /*
1775 * Any feature specified by user-space above
1776 * VIRTIO_FEATURES_MAX is not supported by definition.
1777 */
1778 for (i = copied; i < count; ++i) {
1779 if (copy_from_user(&features, featurep + 1 + i,
1780 sizeof(features)))
1781 return -EFAULT;
1782 if (features)
1783 return -EOPNOTSUPP;
1784 }
1785
1786 for (i = 0; i < VIRTIO_FEATURES_DWORDS; i++)
1787 if (all_features[i] & ~vhost_net_features[i])
1788 return -EOPNOTSUPP;
1789
1790 return vhost_net_set_features(n, all_features);
1791 case VHOST_GET_BACKEND_FEATURES:
1792 features = VHOST_NET_BACKEND_FEATURES;
1793 if (copy_to_user(featurep, &features, sizeof(features)))
1794 return -EFAULT;
1795 return 0;
1796 case VHOST_SET_BACKEND_FEATURES:
1797 if (copy_from_user(&features, featurep, sizeof(features)))
1798 return -EFAULT;
1799 if (features & ~VHOST_NET_BACKEND_FEATURES)
1800 return -EOPNOTSUPP;
1801 vhost_set_backend_features(&n->dev, features);
1802 return 0;
1803 case VHOST_RESET_OWNER:
1804 return vhost_net_reset_owner(n);
1805 case VHOST_SET_OWNER:
1806 return vhost_net_set_owner(n);
1807 default:
1808 mutex_lock(&n->dev.mutex);
1809 r = vhost_dev_ioctl(&n->dev, ioctl, argp);
1810 if (r == -ENOIOCTLCMD)
1811 r = vhost_vring_ioctl(&n->dev, ioctl, argp);
1812 else
1813 vhost_net_flush(n);
1814 mutex_unlock(&n->dev.mutex);
1815 return r;
1816 }
1817 }
1818
vhost_net_chr_read_iter(struct kiocb * iocb,struct iov_iter * to)1819 static ssize_t vhost_net_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
1820 {
1821 struct file *file = iocb->ki_filp;
1822 struct vhost_net *n = file->private_data;
1823 struct vhost_dev *dev = &n->dev;
1824 int noblock = file->f_flags & O_NONBLOCK;
1825
1826 return vhost_chr_read_iter(dev, to, noblock);
1827 }
1828
vhost_net_chr_write_iter(struct kiocb * iocb,struct iov_iter * from)1829 static ssize_t vhost_net_chr_write_iter(struct kiocb *iocb,
1830 struct iov_iter *from)
1831 {
1832 struct file *file = iocb->ki_filp;
1833 struct vhost_net *n = file->private_data;
1834 struct vhost_dev *dev = &n->dev;
1835
1836 return vhost_chr_write_iter(dev, from);
1837 }
1838
vhost_net_chr_poll(struct file * file,poll_table * wait)1839 static __poll_t vhost_net_chr_poll(struct file *file, poll_table *wait)
1840 {
1841 struct vhost_net *n = file->private_data;
1842 struct vhost_dev *dev = &n->dev;
1843
1844 return vhost_chr_poll(file, dev, wait);
1845 }
1846
1847 static const struct file_operations vhost_net_fops = {
1848 .owner = THIS_MODULE,
1849 .release = vhost_net_release,
1850 .read_iter = vhost_net_chr_read_iter,
1851 .write_iter = vhost_net_chr_write_iter,
1852 .poll = vhost_net_chr_poll,
1853 .unlocked_ioctl = vhost_net_ioctl,
1854 .compat_ioctl = compat_ptr_ioctl,
1855 .open = vhost_net_open,
1856 .llseek = noop_llseek,
1857 };
1858
1859 static struct miscdevice vhost_net_misc = {
1860 .minor = VHOST_NET_MINOR,
1861 .name = "vhost-net",
1862 .fops = &vhost_net_fops,
1863 };
1864
vhost_net_init(void)1865 static int __init vhost_net_init(void)
1866 {
1867 if (experimental_zcopytx)
1868 vhost_net_enable_zcopy(VHOST_NET_VQ_TX);
1869 return misc_register(&vhost_net_misc);
1870 }
1871 module_init(vhost_net_init);
1872
vhost_net_exit(void)1873 static void __exit vhost_net_exit(void)
1874 {
1875 misc_deregister(&vhost_net_misc);
1876 }
1877 module_exit(vhost_net_exit);
1878
1879 MODULE_VERSION("0.0.1");
1880 MODULE_LICENSE("GPL v2");
1881 MODULE_AUTHOR("Michael S. Tsirkin");
1882 MODULE_DESCRIPTION("Host kernel accelerator for virtio net");
1883 MODULE_ALIAS_MISCDEV(VHOST_NET_MINOR);
1884 MODULE_ALIAS("devname:vhost-net");
1885