PCI: Bus number from the bridge, not the device
[qemu.git] / hw / vhost_net.c
blobc068be1f54944c26c61d0283a7553da7aae224da
1 /*
2 * vhost-net support
4 * Copyright Red Hat, Inc. 2010
6 * Authors:
7 * Michael S. Tsirkin <mst@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
13 #include "net.h"
14 #include "net/tap.h"
16 #include "virtio-net.h"
17 #include "vhost_net.h"
19 #include "config.h"
21 #ifdef CONFIG_VHOST_NET
22 #include <linux/vhost.h>
23 #include <sys/socket.h>
24 #include <linux/kvm.h>
25 #include <fcntl.h>
26 #include <sys/ioctl.h>
27 #include <linux/virtio_ring.h>
28 #include <netpacket/packet.h>
29 #include <net/ethernet.h>
30 #include <net/if.h>
31 #include <netinet/in.h>
33 #include <stdio.h>
35 #include "vhost.h"
37 struct vhost_net {
38 struct vhost_dev dev;
39 struct vhost_virtqueue vqs[2];
40 int backend;
41 VLANClientState *vc;
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);
56 return features;
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);
78 default:
79 fprintf(stderr, "vhost-net requires tap backend\n");
80 return -EBADFD;
84 struct vhost_net *vhost_net_init(VLANClientState *backend, int devfd)
86 int r;
87 struct vhost_net *net = qemu_malloc(sizeof *net);
88 if (!backend) {
89 fprintf(stderr, "vhost-net requires backend to be setup\n");
90 goto fail;
92 r = vhost_net_get_fd(backend);
93 if (r < 0) {
94 goto fail;
96 net->vc = backend;
97 net->dev.backend_features = tap_has_vnet_hdr(backend) ? 0 :
98 (1 << VHOST_NET_F_VIRTIO_NET_HDR);
99 net->backend = r;
101 r = vhost_dev_init(&net->dev, devfd);
102 if (r < 0) {
103 goto fail;
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);
113 goto fail;
116 /* Set sane init value. Override when guest acks. */
117 vhost_net_ack_features(net, 0);
118 return net;
119 fail:
120 qemu_free(net);
121 return NULL;
124 int vhost_net_start(struct vhost_net *net,
125 VirtIODevice *dev)
127 struct vhost_vring_file file = { };
128 int r;
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));
134 net->dev.nvqs = 2;
135 net->dev.vqs = net->vqs;
136 r = vhost_dev_start(&net->dev, dev);
137 if (r < 0) {
138 return r;
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);
146 if (r < 0) {
147 r = -errno;
148 goto fail;
151 return 0;
152 fail:
153 file.fd = -1;
154 while (file.index-- > 0) {
155 int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
156 assert(r >= 0);
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));
163 return r;
166 void vhost_net_stop(struct vhost_net *net,
167 VirtIODevice *dev)
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);
173 assert(r >= 0);
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));
188 qemu_free(net);
190 #else
191 struct vhost_net *vhost_net_init(VLANClientState *backend, int devfd)
193 return NULL;
196 int vhost_net_start(struct vhost_net *net,
197 VirtIODevice *dev)
199 return -ENOSYS;
201 void vhost_net_stop(struct vhost_net *net,
202 VirtIODevice *dev)
206 void vhost_net_cleanup(struct vhost_net *net)
210 unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
212 return features;
214 void vhost_net_ack_features(struct vhost_net *net, unsigned features)
217 #endif