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