]> git.proxmox.com Git - qemu.git/blame - hw/vhost_net.c
Merge remote-tracking branch 'stefanha/trivial-patches' into staging
[qemu.git] / hw / vhost_net.c
CommitLineData
d5970055
MT
1/*
2 * vhost-net support
3 *
4 * Copyright Red Hat, Inc. 2010
5 *
6 * Authors:
7 * Michael S. Tsirkin <mst@redhat.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 */
12
13#include "net.h"
14#include "net/tap.h"
15
16#include "virtio-net.h"
17#include "vhost_net.h"
35f75462 18#include "qemu-error.h"
d5970055
MT
19
20#include "config.h"
21
22#ifdef CONFIG_VHOST_NET
23#include <linux/vhost.h>
d5970055
MT
24#include <sys/socket.h>
25#include <linux/kvm.h>
26#include <fcntl.h>
27#include <sys/ioctl.h>
28#include <linux/virtio_ring.h>
29#include <netpacket/packet.h>
30#include <net/ethernet.h>
31#include <net/if.h>
32#include <netinet/in.h>
33
34#include <stdio.h>
35
36#include "vhost.h"
37
38struct vhost_net {
39 struct vhost_dev dev;
40 struct vhost_virtqueue vqs[2];
41 int backend;
42 VLANClientState *vc;
43};
44
45unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
46{
47 /* Clear features not supported by host kernel. */
48 if (!(net->dev.features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY))) {
49 features &= ~(1 << VIRTIO_F_NOTIFY_ON_EMPTY);
50 }
51 if (!(net->dev.features & (1 << VIRTIO_RING_F_INDIRECT_DESC))) {
52 features &= ~(1 << VIRTIO_RING_F_INDIRECT_DESC);
53 }
bcbabae8
MT
54 if (!(net->dev.features & (1 << VIRTIO_RING_F_EVENT_IDX))) {
55 features &= ~(1 << VIRTIO_RING_F_EVENT_IDX);
56 }
ca736c8e
MT
57 if (!(net->dev.features & (1 << VIRTIO_NET_F_MRG_RXBUF))) {
58 features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF);
59 }
d5970055
MT
60 return features;
61}
62
63void vhost_net_ack_features(struct vhost_net *net, unsigned features)
64{
65 net->dev.acked_features = net->dev.backend_features;
66 if (features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY)) {
67 net->dev.acked_features |= (1 << VIRTIO_F_NOTIFY_ON_EMPTY);
68 }
69 if (features & (1 << VIRTIO_RING_F_INDIRECT_DESC)) {
70 net->dev.acked_features |= (1 << VIRTIO_RING_F_INDIRECT_DESC);
71 }
bcbabae8
MT
72 if (features & (1 << VIRTIO_RING_F_EVENT_IDX)) {
73 net->dev.acked_features |= (1 << VIRTIO_RING_F_EVENT_IDX);
74 }
ca736c8e
MT
75 if (features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
76 net->dev.acked_features |= (1 << VIRTIO_NET_F_MRG_RXBUF);
77 }
d5970055
MT
78}
79
80static int vhost_net_get_fd(VLANClientState *backend)
81{
82 switch (backend->info->type) {
83 case NET_CLIENT_TYPE_TAP:
84 return tap_get_fd(backend);
85 default:
86 fprintf(stderr, "vhost-net requires tap backend\n");
87 return -EBADFD;
88 }
89}
90
5430a28f 91struct vhost_net *vhost_net_init(VLANClientState *backend, int devfd,
92 bool force)
d5970055
MT
93{
94 int r;
7267c094 95 struct vhost_net *net = g_malloc(sizeof *net);
d5970055
MT
96 if (!backend) {
97 fprintf(stderr, "vhost-net requires backend to be setup\n");
98 goto fail;
99 }
100 r = vhost_net_get_fd(backend);
101 if (r < 0) {
102 goto fail;
103 }
104 net->vc = backend;
105 net->dev.backend_features = tap_has_vnet_hdr(backend) ? 0 :
106 (1 << VHOST_NET_F_VIRTIO_NET_HDR);
107 net->backend = r;
108
5430a28f 109 r = vhost_dev_init(&net->dev, devfd, force);
d5970055
MT
110 if (r < 0) {
111 goto fail;
112 }
ca736c8e
MT
113 if (!tap_has_vnet_hdr_len(backend,
114 sizeof(struct virtio_net_hdr_mrg_rxbuf))) {
115 net->dev.features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF);
116 }
d5970055 117 if (~net->dev.features & net->dev.backend_features) {
0bfcd599 118 fprintf(stderr, "vhost lacks feature mask %" PRIu64 " for backend\n",
29f91781 119 (uint64_t)(~net->dev.features & net->dev.backend_features));
d5970055
MT
120 vhost_dev_cleanup(&net->dev);
121 goto fail;
122 }
123
124 /* Set sane init value. Override when guest acks. */
125 vhost_net_ack_features(net, 0);
126 return net;
127fail:
7267c094 128 g_free(net);
d5970055
MT
129 return NULL;
130}
131
5430a28f 132bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
133{
134 return vhost_dev_query(&net->dev, dev);
135}
136
d5970055
MT
137int vhost_net_start(struct vhost_net *net,
138 VirtIODevice *dev)
139{
140 struct vhost_vring_file file = { };
141 int r;
b0b3db79
MT
142
143 net->dev.nvqs = 2;
144 net->dev.vqs = net->vqs;
145
146 r = vhost_dev_enable_notifiers(&net->dev, dev);
147 if (r < 0) {
148 goto fail_notifiers;
149 }
ca736c8e
MT
150 if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
151 tap_set_vnet_hdr_len(net->vc,
152 sizeof(struct virtio_net_hdr_mrg_rxbuf));
153 }
d5970055 154
d5970055
MT
155 r = vhost_dev_start(&net->dev, dev);
156 if (r < 0) {
b0b3db79 157 goto fail_start;
d5970055
MT
158 }
159
160 net->vc->info->poll(net->vc, false);
161 qemu_set_fd_handler(net->backend, NULL, NULL, NULL);
162 file.fd = net->backend;
163 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
164 r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
165 if (r < 0) {
166 r = -errno;
167 goto fail;
168 }
169 }
170 return 0;
171fail:
172 file.fd = -1;
6b37c87c 173 while (file.index-- > 0) {
d5970055
MT
174 int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
175 assert(r >= 0);
176 }
177 net->vc->info->poll(net->vc, true);
178 vhost_dev_stop(&net->dev, dev);
ca736c8e
MT
179 if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
180 tap_set_vnet_hdr_len(net->vc, sizeof(struct virtio_net_hdr));
181 }
b0b3db79
MT
182fail_start:
183 vhost_dev_disable_notifiers(&net->dev, dev);
184fail_notifiers:
d5970055
MT
185 return r;
186}
187
188void vhost_net_stop(struct vhost_net *net,
189 VirtIODevice *dev)
190{
191 struct vhost_vring_file file = { .fd = -1 };
192
193 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
194 int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
195 assert(r >= 0);
196 }
197 net->vc->info->poll(net->vc, true);
198 vhost_dev_stop(&net->dev, dev);
ca736c8e
MT
199 if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
200 tap_set_vnet_hdr_len(net->vc, sizeof(struct virtio_net_hdr));
201 }
b0b3db79 202 vhost_dev_disable_notifiers(&net->dev, dev);
d5970055
MT
203}
204
205void vhost_net_cleanup(struct vhost_net *net)
206{
207 vhost_dev_cleanup(&net->dev);
ca736c8e
MT
208 if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
209 tap_set_vnet_hdr_len(net->vc, sizeof(struct virtio_net_hdr));
210 }
7267c094 211 g_free(net);
d5970055
MT
212}
213#else
5430a28f 214struct vhost_net *vhost_net_init(VLANClientState *backend, int devfd,
215 bool force)
216{
35f75462 217 error_report("vhost-net support is not compiled in");
5430a28f 218 return NULL;
219}
220
221bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
d5970055 222{
5430a28f 223 return false;
d5970055
MT
224}
225
226int vhost_net_start(struct vhost_net *net,
227 VirtIODevice *dev)
228{
5430a28f 229 return -ENOSYS;
d5970055
MT
230}
231void vhost_net_stop(struct vhost_net *net,
232 VirtIODevice *dev)
233{
234}
235
236void vhost_net_cleanup(struct vhost_net *net)
237{
238}
239
240unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
241{
5430a28f 242 return features;
d5970055
MT
243}
244void vhost_net_ack_features(struct vhost_net *net, unsigned features)
245{
246}
247#endif