1 /* Copyright (C) 2009 Red Hat, Inc.
2 * Author: Michael S. Tsirkin <mst@redhat.com>
4 * This work is licensed under the terms of the GNU GPL, version 2.
6 * virtio-net server in host kernel.
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/rcupdate.h>
19 #include <linux/file.h>
20 #include <linux/slab.h>
22 #include <linux/net.h>
23 #include <linux/if_packet.h>
24 #include <linux/if_arp.h>
25 #include <linux/if_tun.h>
26 #include <linux/if_macvlan.h>
27 #include <linux/if_vlan.h>
33 static int experimental_zcopytx = 1;
34 module_param(experimental_zcopytx, int, 0444);
35 MODULE_PARM_DESC(experimental_zcopytx, "Enable Zero Copy TX;"
36 " 1 -Enable; 0 - Disable");
38 /* Max number of bytes transferred before requeueing the job.
39 * Using this limit prevents one virtqueue from starving others. */
40 #define VHOST_NET_WEIGHT 0x80000
42 /* MAX number of TX used buffers for outstanding zerocopy */
43 #define VHOST_MAX_PEND 128
44 #define VHOST_GOODCOPY_LEN 256
47 * For transmit, used buffer len is unused; we override it to track buffer
48 * status internally; used for zerocopy tx only.
50 /* Lower device DMA failed */
51 #define VHOST_DMA_FAILED_LEN 3
52 /* Lower device DMA done */
53 #define VHOST_DMA_DONE_LEN 2
54 /* Lower device DMA in progress */
55 #define VHOST_DMA_IN_PROGRESS 1
57 #define VHOST_DMA_CLEAR_LEN 0
59 #define VHOST_DMA_IS_DONE(len) ((len) >= VHOST_DMA_DONE_LEN)
67 struct vhost_ubuf_ref {
69 wait_queue_head_t wait;
70 struct vhost_virtqueue *vq;
73 struct vhost_net_virtqueue {
74 struct vhost_virtqueue vq;
75 /* hdr is used to store the virtio header.
76 * Since each iovec has >= 1 byte length, we never need more than
77 * header length entries to store the header. */
78 struct iovec hdr[sizeof(struct virtio_net_hdr_mrg_rxbuf)];
81 /* vhost zerocopy support fields below: */
82 /* last used idx for outstanding DMA zerocopy buffers */
84 /* first used idx for DMA done zerocopy buffers */
86 /* an array of userspace buffers info */
87 struct ubuf_info *ubuf_info;
88 /* Reference counting for outstanding ubufs.
89 * Protected by vq mutex. Writers must also take device mutex. */
90 struct vhost_ubuf_ref *ubufs;
95 struct vhost_net_virtqueue vqs[VHOST_NET_VQ_MAX];
96 struct vhost_poll poll[VHOST_NET_VQ_MAX];
97 /* Number of TX recently submitted.
98 * Protected by tx vq lock. */
100 /* Number of times zerocopy TX recently failed.
101 * Protected by tx vq lock. */
102 unsigned tx_zcopy_err;
103 /* Flush in progress. Protected by tx vq lock. */
107 static unsigned vhost_zcopy_mask __read_mostly;
109 void vhost_enable_zcopy(int vq)
111 vhost_zcopy_mask |= 0x1 << vq;
114 static void vhost_zerocopy_done_signal(struct kref *kref)
116 struct vhost_ubuf_ref *ubufs = container_of(kref, struct vhost_ubuf_ref,
118 wake_up(&ubufs->wait);
121 struct vhost_ubuf_ref *vhost_ubuf_alloc(struct vhost_virtqueue *vq,
124 struct vhost_ubuf_ref *ubufs;
125 /* No zero copy backend? Nothing to count. */
128 ubufs = kmalloc(sizeof(*ubufs), GFP_KERNEL);
130 return ERR_PTR(-ENOMEM);
131 kref_init(&ubufs->kref);
132 init_waitqueue_head(&ubufs->wait);
137 void vhost_ubuf_put(struct vhost_ubuf_ref *ubufs)
139 kref_put(&ubufs->kref, vhost_zerocopy_done_signal);
142 void vhost_ubuf_put_and_wait(struct vhost_ubuf_ref *ubufs)
144 kref_put(&ubufs->kref, vhost_zerocopy_done_signal);
145 wait_event(ubufs->wait, !atomic_read(&ubufs->kref.refcount));
149 int vhost_net_set_ubuf_info(struct vhost_net *n)
154 for (i = 0; i < n->dev.nvqs; ++i) {
155 zcopy = vhost_zcopy_mask & (0x1 << i);
158 n->vqs[i].ubuf_info = kmalloc(sizeof(*n->vqs[i].ubuf_info) *
159 UIO_MAXIOV, GFP_KERNEL);
160 if (!n->vqs[i].ubuf_info)
167 zcopy = vhost_zcopy_mask & (0x1 << i);
170 kfree(n->vqs[i].ubuf_info);
175 void vhost_net_vq_reset(struct vhost_net *n)
179 for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
180 n->vqs[i].done_idx = 0;
181 n->vqs[i].upend_idx = 0;
182 n->vqs[i].ubufs = NULL;
183 kfree(n->vqs[i].ubuf_info);
184 n->vqs[i].ubuf_info = NULL;
185 n->vqs[i].vhost_hlen = 0;
186 n->vqs[i].sock_hlen = 0;
191 static void vhost_net_tx_packet(struct vhost_net *net)
194 if (net->tx_packets < 1024)
197 net->tx_zcopy_err = 0;
200 static void vhost_net_tx_err(struct vhost_net *net)
205 static bool vhost_net_tx_select_zcopy(struct vhost_net *net)
207 /* TX flush waits for outstanding DMAs to be done.
208 * Don't start new DMAs.
210 return !net->tx_flush &&
211 net->tx_packets / 64 >= net->tx_zcopy_err;
214 static bool vhost_sock_zcopy(struct socket *sock)
216 return unlikely(experimental_zcopytx) &&
217 sock_flag(sock->sk, SOCK_ZEROCOPY);
220 /* Pop first len bytes from iovec. Return number of segments used. */
221 static int move_iovec_hdr(struct iovec *from, struct iovec *to,
222 size_t len, int iov_count)
227 while (len && seg < iov_count) {
228 size = min(from->iov_len, len);
229 to->iov_base = from->iov_base;
231 from->iov_len -= size;
232 from->iov_base += size;
240 /* Copy iovec entries for len bytes from iovec. */
241 static void copy_iovec_hdr(const struct iovec *from, struct iovec *to,
242 size_t len, int iovcount)
247 while (len && seg < iovcount) {
248 size = min(from->iov_len, len);
249 to->iov_base = from->iov_base;
258 /* In case of DMA done not in order in lower device driver for some reason.
259 * upend_idx is used to track end of used idx, done_idx is used to track head
260 * of used idx. Once lower device DMA done contiguously, we will signal KVM
263 static int vhost_zerocopy_signal_used(struct vhost_net *net,
264 struct vhost_virtqueue *vq)
266 struct vhost_net_virtqueue *nvq =
267 container_of(vq, struct vhost_net_virtqueue, vq);
271 for (i = nvq->done_idx; i != nvq->upend_idx; i = (i + 1) % UIO_MAXIOV) {
272 if (vq->heads[i].len == VHOST_DMA_FAILED_LEN)
273 vhost_net_tx_err(net);
274 if (VHOST_DMA_IS_DONE(vq->heads[i].len)) {
275 vq->heads[i].len = VHOST_DMA_CLEAR_LEN;
276 vhost_add_used_and_signal(vq->dev, vq,
287 static void vhost_zerocopy_callback(struct ubuf_info *ubuf, bool success)
289 struct vhost_ubuf_ref *ubufs = ubuf->ctx;
290 struct vhost_virtqueue *vq = ubufs->vq;
291 int cnt = atomic_read(&ubufs->kref.refcount);
294 * Trigger polling thread if guest stopped submitting new buffers:
295 * in this case, the refcount after decrement will eventually reach 1
297 * We also trigger polling periodically after each 16 packets
298 * (the value 16 here is more or less arbitrary, it's tuned to trigger
299 * less than 10% of times).
301 if (cnt <= 2 || !(cnt % 16))
302 vhost_poll_queue(&vq->poll);
303 /* set len to mark this desc buffers done DMA */
304 vq->heads[ubuf->desc].len = success ?
305 VHOST_DMA_DONE_LEN : VHOST_DMA_FAILED_LEN;
306 vhost_ubuf_put(ubufs);
309 /* Expects to be always run from workqueue - which acts as
310 * read-size critical section for our kind of RCU. */
311 static void handle_tx(struct vhost_net *net)
313 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
314 struct vhost_virtqueue *vq = &nvq->vq;
317 struct msghdr msg = {
323 .msg_flags = MSG_DONTWAIT,
325 size_t len, total_len = 0;
329 struct vhost_ubuf_ref *uninitialized_var(ubufs);
330 bool zcopy, zcopy_used;
332 /* TODO: check that we are running from vhost_worker? */
333 sock = rcu_dereference_check(vq->private_data, 1);
337 mutex_lock(&vq->mutex);
338 vhost_disable_notify(&net->dev, vq);
340 hdr_size = nvq->vhost_hlen;
344 /* Release DMAs done buffers first */
346 vhost_zerocopy_signal_used(net, vq);
348 head = vhost_get_vq_desc(&net->dev, vq, vq->iov,
352 /* On error, stop handling until the next kick. */
353 if (unlikely(head < 0))
355 /* Nothing new? Wait for eventfd to tell us they refilled. */
356 if (head == vq->num) {
359 /* If more outstanding DMAs, queue the work.
360 * Handle upend_idx wrap around
362 num_pends = likely(nvq->upend_idx >= nvq->done_idx) ?
363 (nvq->upend_idx - nvq->done_idx) :
364 (nvq->upend_idx + UIO_MAXIOV -
366 if (unlikely(num_pends > VHOST_MAX_PEND))
368 if (unlikely(vhost_enable_notify(&net->dev, vq))) {
369 vhost_disable_notify(&net->dev, vq);
375 vq_err(vq, "Unexpected descriptor format for TX: "
376 "out %d, int %d\n", out, in);
379 /* Skip header. TODO: support TSO. */
380 s = move_iovec_hdr(vq->iov, nvq->hdr, hdr_size, out);
381 msg.msg_iovlen = out;
382 len = iov_length(vq->iov, out);
385 vq_err(vq, "Unexpected header len for TX: "
386 "%zd expected %zd\n",
387 iov_length(nvq->hdr, s), hdr_size);
390 zcopy_used = zcopy && (len >= VHOST_GOODCOPY_LEN ||
391 nvq->upend_idx != nvq->done_idx);
393 /* use msg_control to pass vhost zerocopy ubuf info to skb */
395 vq->heads[nvq->upend_idx].id = head;
396 if (!vhost_net_tx_select_zcopy(net) ||
397 len < VHOST_GOODCOPY_LEN) {
398 /* copy don't need to wait for DMA done */
399 vq->heads[nvq->upend_idx].len =
401 msg.msg_control = NULL;
402 msg.msg_controllen = 0;
405 struct ubuf_info *ubuf;
406 ubuf = nvq->ubuf_info + nvq->upend_idx;
408 vq->heads[nvq->upend_idx].len =
409 VHOST_DMA_IN_PROGRESS;
410 ubuf->callback = vhost_zerocopy_callback;
411 ubuf->ctx = nvq->ubufs;
412 ubuf->desc = nvq->upend_idx;
413 msg.msg_control = ubuf;
414 msg.msg_controllen = sizeof(ubuf);
416 kref_get(&ubufs->kref);
418 nvq->upend_idx = (nvq->upend_idx + 1) % UIO_MAXIOV;
420 /* TODO: Check specific error and bomb out unless ENOBUFS? */
421 err = sock->ops->sendmsg(NULL, sock, &msg, len);
422 if (unlikely(err < 0)) {
425 vhost_ubuf_put(ubufs);
426 nvq->upend_idx = ((unsigned)nvq->upend_idx - 1)
429 vhost_discard_vq_desc(vq, 1);
433 pr_debug("Truncated TX packet: "
434 " len %d != %zd\n", err, len);
436 vhost_add_used_and_signal(&net->dev, vq, head, 0);
438 vhost_zerocopy_signal_used(net, vq);
440 vhost_net_tx_packet(net);
441 if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
442 vhost_poll_queue(&vq->poll);
447 mutex_unlock(&vq->mutex);
450 static int peek_head_len(struct sock *sk)
452 struct sk_buff *head;
456 spin_lock_irqsave(&sk->sk_receive_queue.lock, flags);
457 head = skb_peek(&sk->sk_receive_queue);
460 if (vlan_tx_tag_present(head))
464 spin_unlock_irqrestore(&sk->sk_receive_queue.lock, flags);
468 /* This is a multi-buffer version of vhost_get_desc, that works if
469 * vq has read descriptors only.
470 * @vq - the relevant virtqueue
471 * @datalen - data length we'll be reading
472 * @iovcount - returned count of io vectors we fill
474 * @log_num - log offset
475 * @quota - headcount quota, 1 for big buffer
476 * returns number of buffer heads allocated, negative on error
478 static int get_rx_bufs(struct vhost_virtqueue *vq,
479 struct vring_used_elem *heads,
482 struct vhost_log *log,
486 unsigned int out, in;
492 while (datalen > 0 && headcount < quota) {
493 if (unlikely(seg >= UIO_MAXIOV)) {
497 d = vhost_get_vq_desc(vq->dev, vq, vq->iov + seg,
498 ARRAY_SIZE(vq->iov) - seg, &out,
504 if (unlikely(out || in <= 0)) {
505 vq_err(vq, "unexpected descriptor format for RX: "
506 "out %d, in %d\n", out, in);
514 heads[headcount].id = d;
515 heads[headcount].len = iov_length(vq->iov + seg, in);
516 datalen -= heads[headcount].len;
520 heads[headcount - 1].len += datalen;
526 vhost_discard_vq_desc(vq, headcount);
530 /* Expects to be always run from workqueue - which acts as
531 * read-size critical section for our kind of RCU. */
532 static void handle_rx(struct vhost_net *net)
534 struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_RX];
535 struct vhost_virtqueue *vq = &nvq->vq;
536 unsigned uninitialized_var(in), log;
537 struct vhost_log *vq_log;
538 struct msghdr msg = {
541 .msg_control = NULL, /* FIXME: get and handle RX aux data. */
544 .msg_flags = MSG_DONTWAIT,
546 struct virtio_net_hdr_mrg_rxbuf hdr = {
548 .hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE
550 size_t total_len = 0;
553 size_t vhost_hlen, sock_hlen;
554 size_t vhost_len, sock_len;
555 /* TODO: check that we are running from vhost_worker? */
556 struct socket *sock = rcu_dereference_check(vq->private_data, 1);
561 mutex_lock(&vq->mutex);
562 vhost_disable_notify(&net->dev, vq);
563 vhost_hlen = nvq->vhost_hlen;
564 sock_hlen = nvq->sock_hlen;
566 vq_log = unlikely(vhost_has_feature(&net->dev, VHOST_F_LOG_ALL)) ?
568 mergeable = vhost_has_feature(&net->dev, VIRTIO_NET_F_MRG_RXBUF);
570 while ((sock_len = peek_head_len(sock->sk))) {
571 sock_len += sock_hlen;
572 vhost_len = sock_len + vhost_hlen;
573 headcount = get_rx_bufs(vq, vq->heads, vhost_len,
575 likely(mergeable) ? UIO_MAXIOV : 1);
576 /* On error, stop handling until the next kick. */
577 if (unlikely(headcount < 0))
579 /* OK, now we need to know about added descriptors. */
581 if (unlikely(vhost_enable_notify(&net->dev, vq))) {
582 /* They have slipped one in as we were
583 * doing that: check again. */
584 vhost_disable_notify(&net->dev, vq);
587 /* Nothing new? Wait for eventfd to tell us
591 /* We don't need to be notified again. */
592 if (unlikely((vhost_hlen)))
593 /* Skip header. TODO: support TSO. */
594 move_iovec_hdr(vq->iov, nvq->hdr, vhost_hlen, in);
596 /* Copy the header for use in VIRTIO_NET_F_MRG_RXBUF:
597 * needed because recvmsg can modify msg_iov. */
598 copy_iovec_hdr(vq->iov, nvq->hdr, sock_hlen, in);
600 err = sock->ops->recvmsg(NULL, sock, &msg,
601 sock_len, MSG_DONTWAIT | MSG_TRUNC);
602 /* Userspace might have consumed the packet meanwhile:
603 * it's not supposed to do this usually, but might be hard
604 * to prevent. Discard data we got (if any) and keep going. */
605 if (unlikely(err != sock_len)) {
606 pr_debug("Discarded rx packet: "
607 " len %d, expected %zd\n", err, sock_len);
608 vhost_discard_vq_desc(vq, headcount);
611 if (unlikely(vhost_hlen) &&
612 memcpy_toiovecend(nvq->hdr, (unsigned char *)&hdr, 0,
614 vq_err(vq, "Unable to write vnet_hdr at addr %p\n",
618 /* TODO: Should check and handle checksum. */
619 if (likely(mergeable) &&
620 memcpy_toiovecend(nvq->hdr, (unsigned char *)&headcount,
621 offsetof(typeof(hdr), num_buffers),
622 sizeof hdr.num_buffers)) {
623 vq_err(vq, "Failed num_buffers write");
624 vhost_discard_vq_desc(vq, headcount);
627 vhost_add_used_and_signal_n(&net->dev, vq, vq->heads,
629 if (unlikely(vq_log))
630 vhost_log_write(vq, vq_log, log, vhost_len);
631 total_len += vhost_len;
632 if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
633 vhost_poll_queue(&vq->poll);
638 mutex_unlock(&vq->mutex);
641 static void handle_tx_kick(struct vhost_work *work)
643 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
645 struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
650 static void handle_rx_kick(struct vhost_work *work)
652 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
654 struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
659 static void handle_tx_net(struct vhost_work *work)
661 struct vhost_net *net = container_of(work, struct vhost_net,
662 poll[VHOST_NET_VQ_TX].work);
666 static void handle_rx_net(struct vhost_work *work)
668 struct vhost_net *net = container_of(work, struct vhost_net,
669 poll[VHOST_NET_VQ_RX].work);
673 static int vhost_net_open(struct inode *inode, struct file *f)
675 struct vhost_net *n = kmalloc(sizeof *n, GFP_KERNEL);
676 struct vhost_dev *dev;
677 struct vhost_virtqueue **vqs;
682 vqs = kmalloc(VHOST_NET_VQ_MAX * sizeof(*vqs), GFP_KERNEL);
689 vqs[VHOST_NET_VQ_TX] = &n->vqs[VHOST_NET_VQ_TX].vq;
690 vqs[VHOST_NET_VQ_RX] = &n->vqs[VHOST_NET_VQ_RX].vq;
691 n->vqs[VHOST_NET_VQ_TX].vq.handle_kick = handle_tx_kick;
692 n->vqs[VHOST_NET_VQ_RX].vq.handle_kick = handle_rx_kick;
693 for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
694 n->vqs[i].ubufs = NULL;
695 n->vqs[i].ubuf_info = NULL;
696 n->vqs[i].upend_idx = 0;
697 n->vqs[i].done_idx = 0;
698 n->vqs[i].vhost_hlen = 0;
699 n->vqs[i].sock_hlen = 0;
701 r = vhost_dev_init(dev, vqs, VHOST_NET_VQ_MAX);
708 vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, POLLOUT, dev);
709 vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, POLLIN, dev);
716 static void vhost_net_disable_vq(struct vhost_net *n,
717 struct vhost_virtqueue *vq)
719 struct vhost_net_virtqueue *nvq =
720 container_of(vq, struct vhost_net_virtqueue, vq);
721 struct vhost_poll *poll = n->poll + (nvq - n->vqs);
722 if (!vq->private_data)
724 vhost_poll_stop(poll);
727 static int vhost_net_enable_vq(struct vhost_net *n,
728 struct vhost_virtqueue *vq)
730 struct vhost_net_virtqueue *nvq =
731 container_of(vq, struct vhost_net_virtqueue, vq);
732 struct vhost_poll *poll = n->poll + (nvq - n->vqs);
735 sock = rcu_dereference_protected(vq->private_data,
736 lockdep_is_held(&vq->mutex));
740 return vhost_poll_start(poll, sock->file);
743 static struct socket *vhost_net_stop_vq(struct vhost_net *n,
744 struct vhost_virtqueue *vq)
748 mutex_lock(&vq->mutex);
749 sock = rcu_dereference_protected(vq->private_data,
750 lockdep_is_held(&vq->mutex));
751 vhost_net_disable_vq(n, vq);
752 rcu_assign_pointer(vq->private_data, NULL);
753 mutex_unlock(&vq->mutex);
757 static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock,
758 struct socket **rx_sock)
760 *tx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_TX].vq);
761 *rx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_RX].vq);
764 static void vhost_net_flush_vq(struct vhost_net *n, int index)
766 vhost_poll_flush(n->poll + index);
767 vhost_poll_flush(&n->vqs[index].vq.poll);
770 static void vhost_net_flush(struct vhost_net *n)
772 vhost_net_flush_vq(n, VHOST_NET_VQ_TX);
773 vhost_net_flush_vq(n, VHOST_NET_VQ_RX);
774 if (n->vqs[VHOST_NET_VQ_TX].ubufs) {
775 mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
777 mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
778 /* Wait for all lower device DMAs done. */
779 vhost_ubuf_put_and_wait(n->vqs[VHOST_NET_VQ_TX].ubufs);
780 mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
782 kref_init(&n->vqs[VHOST_NET_VQ_TX].ubufs->kref);
783 mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
787 static int vhost_net_release(struct inode *inode, struct file *f)
789 struct vhost_net *n = f->private_data;
790 struct socket *tx_sock;
791 struct socket *rx_sock;
793 vhost_net_stop(n, &tx_sock, &rx_sock);
795 vhost_dev_stop(&n->dev);
796 vhost_dev_cleanup(&n->dev, false);
797 vhost_net_vq_reset(n);
802 /* We do an extra flush before freeing memory,
803 * since jobs can re-queue themselves. */
810 static struct socket *get_raw_socket(int fd)
813 struct sockaddr_ll sa;
814 char buf[MAX_ADDR_LEN];
816 int uaddr_len = sizeof uaddr, r;
817 struct socket *sock = sockfd_lookup(fd, &r);
820 return ERR_PTR(-ENOTSOCK);
822 /* Parameter checking */
823 if (sock->sk->sk_type != SOCK_RAW) {
824 r = -ESOCKTNOSUPPORT;
828 r = sock->ops->getname(sock, (struct sockaddr *)&uaddr.sa,
833 if (uaddr.sa.sll_family != AF_PACKET) {
843 static struct socket *get_tap_socket(int fd)
845 struct file *file = fget(fd);
849 return ERR_PTR(-EBADF);
850 sock = tun_get_socket(file);
853 sock = macvtap_get_socket(file);
859 static struct socket *get_socket(int fd)
863 /* special case to disable backend */
866 sock = get_raw_socket(fd);
869 sock = get_tap_socket(fd);
872 return ERR_PTR(-ENOTSOCK);
875 static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
877 struct socket *sock, *oldsock;
878 struct vhost_virtqueue *vq;
879 struct vhost_net_virtqueue *nvq;
880 struct vhost_ubuf_ref *ubufs, *oldubufs = NULL;
883 mutex_lock(&n->dev.mutex);
884 r = vhost_dev_check_owner(&n->dev);
888 if (index >= VHOST_NET_VQ_MAX) {
892 vq = &n->vqs[index].vq;
893 nvq = &n->vqs[index];
894 mutex_lock(&vq->mutex);
896 /* Verify that ring has been setup correctly. */
897 if (!vhost_vq_access_ok(vq)) {
901 sock = get_socket(fd);
907 /* start polling new socket */
908 oldsock = rcu_dereference_protected(vq->private_data,
909 lockdep_is_held(&vq->mutex));
910 if (sock != oldsock) {
911 ubufs = vhost_ubuf_alloc(vq, sock && vhost_sock_zcopy(sock));
917 vhost_net_disable_vq(n, vq);
918 rcu_assign_pointer(vq->private_data, sock);
919 r = vhost_init_used(vq);
922 r = vhost_net_enable_vq(n, vq);
926 oldubufs = nvq->ubufs;
934 mutex_unlock(&vq->mutex);
937 vhost_ubuf_put_and_wait(oldubufs);
938 mutex_lock(&vq->mutex);
939 vhost_zerocopy_signal_used(n, vq);
940 mutex_unlock(&vq->mutex);
944 vhost_net_flush_vq(n, index);
948 mutex_unlock(&n->dev.mutex);
952 rcu_assign_pointer(vq->private_data, oldsock);
953 vhost_net_enable_vq(n, vq);
955 vhost_ubuf_put_and_wait(ubufs);
959 mutex_unlock(&vq->mutex);
961 mutex_unlock(&n->dev.mutex);
965 static long vhost_net_reset_owner(struct vhost_net *n)
967 struct socket *tx_sock = NULL;
968 struct socket *rx_sock = NULL;
971 mutex_lock(&n->dev.mutex);
972 err = vhost_dev_check_owner(&n->dev);
975 vhost_net_stop(n, &tx_sock, &rx_sock);
977 err = vhost_dev_reset_owner(&n->dev);
978 vhost_net_vq_reset(n);
980 mutex_unlock(&n->dev.mutex);
988 static int vhost_net_set_features(struct vhost_net *n, u64 features)
990 size_t vhost_hlen, sock_hlen, hdr_len;
993 hdr_len = (features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ?
994 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
995 sizeof(struct virtio_net_hdr);
996 if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) {
997 /* vhost provides vnet_hdr */
998 vhost_hlen = hdr_len;
1001 /* socket provides vnet_hdr */
1003 sock_hlen = hdr_len;
1005 mutex_lock(&n->dev.mutex);
1006 if ((features & (1 << VHOST_F_LOG_ALL)) &&
1007 !vhost_log_access_ok(&n->dev)) {
1008 mutex_unlock(&n->dev.mutex);
1011 n->dev.acked_features = features;
1013 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
1014 mutex_lock(&n->vqs[i].vq.mutex);
1015 n->vqs[i].vhost_hlen = vhost_hlen;
1016 n->vqs[i].sock_hlen = sock_hlen;
1017 mutex_unlock(&n->vqs[i].vq.mutex);
1020 mutex_unlock(&n->dev.mutex);
1024 static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
1027 struct vhost_net *n = f->private_data;
1028 void __user *argp = (void __user *)arg;
1029 u64 __user *featurep = argp;
1030 struct vhost_vring_file backend;
1035 case VHOST_NET_SET_BACKEND:
1036 if (copy_from_user(&backend, argp, sizeof backend))
1038 return vhost_net_set_backend(n, backend.index, backend.fd);
1039 case VHOST_GET_FEATURES:
1040 features = VHOST_NET_FEATURES;
1041 if (copy_to_user(featurep, &features, sizeof features))
1044 case VHOST_SET_FEATURES:
1045 if (copy_from_user(&features, featurep, sizeof features))
1047 if (features & ~VHOST_NET_FEATURES)
1049 return vhost_net_set_features(n, features);
1050 case VHOST_RESET_OWNER:
1051 return vhost_net_reset_owner(n);
1053 mutex_lock(&n->dev.mutex);
1054 if (ioctl == VHOST_SET_OWNER) {
1055 r = vhost_net_set_ubuf_info(n);
1059 r = vhost_dev_ioctl(&n->dev, ioctl, argp);
1060 if (r == -ENOIOCTLCMD)
1061 r = vhost_vring_ioctl(&n->dev, ioctl, argp);
1065 mutex_unlock(&n->dev.mutex);
1070 #ifdef CONFIG_COMPAT
1071 static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl,
1074 return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
1078 static const struct file_operations vhost_net_fops = {
1079 .owner = THIS_MODULE,
1080 .release = vhost_net_release,
1081 .unlocked_ioctl = vhost_net_ioctl,
1082 #ifdef CONFIG_COMPAT
1083 .compat_ioctl = vhost_net_compat_ioctl,
1085 .open = vhost_net_open,
1086 .llseek = noop_llseek,
1089 static struct miscdevice vhost_net_misc = {
1090 .minor = VHOST_NET_MINOR,
1091 .name = "vhost-net",
1092 .fops = &vhost_net_fops,
1095 static int vhost_net_init(void)
1097 if (experimental_zcopytx)
1098 vhost_enable_zcopy(VHOST_NET_VQ_TX);
1099 return misc_register(&vhost_net_misc);
1101 module_init(vhost_net_init);
1103 static void vhost_net_exit(void)
1105 misc_deregister(&vhost_net_misc);
1107 module_exit(vhost_net_exit);
1109 MODULE_VERSION("0.0.1");
1110 MODULE_LICENSE("GPL v2");
1111 MODULE_AUTHOR("Michael S. Tsirkin");
1112 MODULE_DESCRIPTION("Host kernel accelerator for virtio net");
1113 MODULE_ALIAS_MISCDEV(VHOST_NET_MINOR);
1114 MODULE_ALIAS("devname:vhost-net");