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,
88 struct vhost_net *net = qemu_malloc(sizeof *net);
90 fprintf(stderr, "vhost-net requires backend to be setup\n");
93 r = vhost_net_get_fd(backend);
98 net->dev.backend_features = tap_has_vnet_hdr(backend) ? 0 :
99 (1 << VHOST_NET_F_VIRTIO_NET_HDR);
102 r = vhost_dev_init(&net->dev, devfd, force);
106 if (!tap_has_vnet_hdr_len(backend,
107 sizeof(struct virtio_net_hdr_mrg_rxbuf))) {
108 net->dev.features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF);
110 if (~net->dev.features & net->dev.backend_features) {
111 fprintf(stderr, "vhost lacks feature mask %" PRIu64 " for backend\n",
112 (uint64_t)(~net->dev.features & net->dev.backend_features));
113 vhost_dev_cleanup(&net->dev);
117 /* Set sane init value. Override when guest acks. */
118 vhost_net_ack_features(net, 0);
125 bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
127 return vhost_dev_query(&net->dev, dev);
130 int vhost_net_start(struct vhost_net *net,
133 struct vhost_vring_file file = { };
135 if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
136 tap_set_vnet_hdr_len(net->vc,
137 sizeof(struct virtio_net_hdr_mrg_rxbuf));
141 net->dev.vqs = net->vqs;
142 r = vhost_dev_start(&net->dev, dev);
147 net->vc->info->poll(net->vc, false);
148 qemu_set_fd_handler(net->backend, NULL, NULL, NULL);
149 file.fd = net->backend;
150 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
151 r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
160 while (file.index-- > 0) {
161 int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
164 net->vc->info->poll(net->vc, true);
165 vhost_dev_stop(&net->dev, dev);
166 if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
167 tap_set_vnet_hdr_len(net->vc, sizeof(struct virtio_net_hdr));
172 void vhost_net_stop(struct vhost_net *net,
175 struct vhost_vring_file file = { .fd = -1 };
177 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
178 int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
181 net->vc->info->poll(net->vc, true);
182 vhost_dev_stop(&net->dev, dev);
183 if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
184 tap_set_vnet_hdr_len(net->vc, sizeof(struct virtio_net_hdr));
188 void vhost_net_cleanup(struct vhost_net *net)
190 vhost_dev_cleanup(&net->dev);
191 if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
192 tap_set_vnet_hdr_len(net->vc, sizeof(struct virtio_net_hdr));
197 struct vhost_net *vhost_net_init(VLANClientState *backend, int devfd,
203 bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
208 int vhost_net_start(struct vhost_net *net,
213 void vhost_net_stop(struct vhost_net *net,
218 void vhost_net_cleanup(struct vhost_net *net)
222 unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
226 void vhost_net_ack_features(struct vhost_net *net, unsigned features)