4 * Copyright Red Hat, Inc. 2010
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
16 #include "virtio-net.h"
17 #include "vhost_net.h"
21 #ifdef CONFIG_VHOST_NET
22 #include <linux/vhost.h>
23 #include <sys/socket.h>
24 #include <linux/kvm.h>
26 #include <sys/ioctl.h>
27 #include <linux/virtio_ring.h>
28 #include <netpacket/packet.h>
29 #include <net/ethernet.h>
31 #include <netinet/in.h>
39 struct vhost_virtqueue vqs[2];
44 unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
46 /* Clear features not supported by host kernel. */
47 if (!(net->dev.features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY))) {
48 features &= ~(1 << VIRTIO_F_NOTIFY_ON_EMPTY);
50 if (!(net->dev.features & (1 << VIRTIO_RING_F_INDIRECT_DESC))) {
51 features &= ~(1 << VIRTIO_RING_F_INDIRECT_DESC);
53 if (!(net->dev.features & (1 << VIRTIO_NET_F_MRG_RXBUF))) {
54 features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF);
59 void vhost_net_ack_features(struct vhost_net *net, unsigned features)
61 net->dev.acked_features = net->dev.backend_features;
62 if (features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY)) {
63 net->dev.acked_features |= (1 << VIRTIO_F_NOTIFY_ON_EMPTY);
65 if (features & (1 << VIRTIO_RING_F_INDIRECT_DESC)) {
66 net->dev.acked_features |= (1 << VIRTIO_RING_F_INDIRECT_DESC);
68 if (features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
69 net->dev.acked_features |= (1 << VIRTIO_NET_F_MRG_RXBUF);
73 static int vhost_net_get_fd(VLANClientState *backend)
75 switch (backend->info->type) {
76 case NET_CLIENT_TYPE_TAP:
77 return tap_get_fd(backend);
79 fprintf(stderr, "vhost-net requires tap backend\n");
84 struct vhost_net *vhost_net_init(VLANClientState *backend, int devfd)
87 struct vhost_net *net = qemu_malloc(sizeof *net);
89 fprintf(stderr, "vhost-net requires backend to be setup\n");
92 r = vhost_net_get_fd(backend);
97 net->dev.backend_features = tap_has_vnet_hdr(backend) ? 0 :
98 (1 << VHOST_NET_F_VIRTIO_NET_HDR);
101 r = vhost_dev_init(&net->dev, devfd);
105 if (!tap_has_vnet_hdr_len(backend,
106 sizeof(struct virtio_net_hdr_mrg_rxbuf))) {
107 net->dev.features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF);
109 if (~net->dev.features & net->dev.backend_features) {
110 fprintf(stderr, "vhost lacks feature mask %" PRIu64 " for backend\n",
111 (uint64_t)(~net->dev.features & net->dev.backend_features));
112 vhost_dev_cleanup(&net->dev);
116 /* Set sane init value. Override when guest acks. */
117 vhost_net_ack_features(net, 0);
124 int vhost_net_start(struct vhost_net *net,
127 struct vhost_vring_file file = { };
129 if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
130 tap_set_vnet_hdr_len(net->vc,
131 sizeof(struct virtio_net_hdr_mrg_rxbuf));
135 net->dev.vqs = net->vqs;
136 r = vhost_dev_start(&net->dev, dev);
141 net->vc->info->poll(net->vc, false);
142 qemu_set_fd_handler(net->backend, NULL, NULL, NULL);
143 file.fd = net->backend;
144 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
145 r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
154 while (file.index-- > 0) {
155 int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
158 net->vc->info->poll(net->vc, true);
159 vhost_dev_stop(&net->dev, dev);
160 if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
161 tap_set_vnet_hdr_len(net->vc, sizeof(struct virtio_net_hdr));
166 void vhost_net_stop(struct vhost_net *net,
169 struct vhost_vring_file file = { .fd = -1 };
171 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
172 int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
175 net->vc->info->poll(net->vc, true);
176 vhost_dev_stop(&net->dev, dev);
177 if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
178 tap_set_vnet_hdr_len(net->vc, sizeof(struct virtio_net_hdr));
182 void vhost_net_cleanup(struct vhost_net *net)
184 vhost_dev_cleanup(&net->dev);
185 if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
186 tap_set_vnet_hdr_len(net->vc, sizeof(struct virtio_net_hdr));
191 struct vhost_net *vhost_net_init(VLANClientState *backend, int devfd)
196 int vhost_net_start(struct vhost_net *net,
201 void vhost_net_stop(struct vhost_net *net,
206 void vhost_net_cleanup(struct vhost_net *net)
210 unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
214 void vhost_net_ack_features(struct vhost_net *net, unsigned features)