]> Git Repo - qemu.git/blame - hw/net/vhost_net.c
tap: fix non-linux build
[qemu.git] / hw / net / 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 <[email protected]>
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.
6b620ca3
PB
11 *
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
d5970055
MT
14 */
15
1422e32d 16#include "net/net.h"
d5970055 17#include "net/tap.h"
03ce5744 18#include "net/vhost-user.h"
d5970055 19
0d09e41a
PB
20#include "hw/virtio/virtio-net.h"
21#include "net/vhost_net.h"
1de7afc9 22#include "qemu/error-report.h"
d5970055
MT
23
24#include "config.h"
25
26#ifdef CONFIG_VHOST_NET
27#include <linux/vhost.h>
d5970055
MT
28#include <sys/socket.h>
29#include <linux/kvm.h>
30#include <fcntl.h>
d5970055
MT
31#include <netpacket/packet.h>
32#include <net/ethernet.h>
33#include <net/if.h>
34#include <netinet/in.h>
35
36#include <stdio.h>
37
4fbe0f32 38#include "standard-headers/linux/virtio_ring.h"
0d09e41a 39#include "hw/virtio/vhost.h"
1c819449 40#include "hw/virtio/virtio-bus.h"
d5970055
MT
41
42struct vhost_net {
43 struct vhost_dev dev;
44 struct vhost_virtqueue vqs[2];
45 int backend;
35277d14 46 NetClientState *nc;
d5970055
MT
47};
48
2e6d46d7
NN
49/* Features supported by host kernel. */
50static const int kernel_feature_bits[] = {
51 VIRTIO_F_NOTIFY_ON_EMPTY,
52 VIRTIO_RING_F_INDIRECT_DESC,
53 VIRTIO_RING_F_EVENT_IDX,
54 VIRTIO_NET_F_MRG_RXBUF,
b1506132 55 VIRTIO_F_VERSION_1,
2e6d46d7
NN
56 VHOST_INVALID_FEATURE_BIT
57};
58
5f4c01ca 59/* Features supported by others. */
d122f1a2 60static const int user_feature_bits[] = {
5f4c01ca
NN
61 VIRTIO_F_NOTIFY_ON_EMPTY,
62 VIRTIO_RING_F_INDIRECT_DESC,
63 VIRTIO_RING_F_EVENT_IDX,
64
65 VIRTIO_F_ANY_LAYOUT,
b1506132 66 VIRTIO_F_VERSION_1,
5f4c01ca
NN
67 VIRTIO_NET_F_CSUM,
68 VIRTIO_NET_F_GUEST_CSUM,
69 VIRTIO_NET_F_GSO,
70 VIRTIO_NET_F_GUEST_TSO4,
71 VIRTIO_NET_F_GUEST_TSO6,
72 VIRTIO_NET_F_GUEST_ECN,
73 VIRTIO_NET_F_GUEST_UFO,
74 VIRTIO_NET_F_HOST_TSO4,
75 VIRTIO_NET_F_HOST_TSO6,
76 VIRTIO_NET_F_HOST_ECN,
77 VIRTIO_NET_F_HOST_UFO,
78 VIRTIO_NET_F_MRG_RXBUF,
79 VIRTIO_NET_F_STATUS,
80 VIRTIO_NET_F_CTRL_VQ,
81 VIRTIO_NET_F_CTRL_RX,
82 VIRTIO_NET_F_CTRL_VLAN,
83 VIRTIO_NET_F_CTRL_RX_EXTRA,
84 VIRTIO_NET_F_CTRL_MAC_ADDR,
85 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS,
86
87 VIRTIO_NET_F_MQ,
88
89 VHOST_INVALID_FEATURE_BIT
90};
91
2e6d46d7 92static const int *vhost_net_get_feature_bits(struct vhost_net *net)
d5970055 93{
2e6d46d7
NN
94 const int *feature_bits = 0;
95
96 switch (net->nc->info->type) {
97 case NET_CLIENT_OPTIONS_KIND_TAP:
98 feature_bits = kernel_feature_bits;
99 break;
5f4c01ca
NN
100 case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
101 feature_bits = user_feature_bits;
102 break;
2e6d46d7
NN
103 default:
104 error_report("Feature bits not defined for this type: %d",
105 net->nc->info->type);
106 break;
ca736c8e 107 }
2e6d46d7
NN
108
109 return feature_bits;
110}
111
9a2ba823 112uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features)
2e6d46d7
NN
113{
114 return vhost_get_features(&net->dev, vhost_net_get_feature_bits(net),
115 features);
d5970055
MT
116}
117
9a2ba823 118void vhost_net_ack_features(struct vhost_net *net, uint64_t features)
d5970055 119{
b49ae913 120 net->dev.acked_features = net->dev.backend_features;
2e6d46d7 121 vhost_ack_features(&net->dev, vhost_net_get_feature_bits(net), features);
d5970055
MT
122}
123
4e68f7a0 124static int vhost_net_get_fd(NetClientState *backend)
d5970055
MT
125{
126 switch (backend->info->type) {
2be64a68 127 case NET_CLIENT_OPTIONS_KIND_TAP:
d5970055
MT
128 return tap_get_fd(backend);
129 default:
130 fprintf(stderr, "vhost-net requires tap backend\n");
131 return -EBADFD;
132 }
133}
134
81647a65 135struct vhost_net *vhost_net_init(VhostNetOptions *options)
d5970055
MT
136{
137 int r;
1a1bfac9 138 bool backend_kernel = options->backend_type == VHOST_BACKEND_TYPE_KERNEL;
7267c094 139 struct vhost_net *net = g_malloc(sizeof *net);
81647a65
NN
140
141 if (!options->net_backend) {
142 fprintf(stderr, "vhost-net requires net backend to be setup\n");
d5970055
MT
143 goto fail;
144 }
81647a65 145
1a1bfac9
NN
146 if (backend_kernel) {
147 r = vhost_net_get_fd(options->net_backend);
148 if (r < 0) {
149 goto fail;
150 }
151 net->dev.backend_features = qemu_has_vnet_hdr(options->net_backend)
9a2ba823 152 ? 0 : (1ULL << VHOST_NET_F_VIRTIO_NET_HDR);
1a1bfac9
NN
153 net->backend = r;
154 } else {
155 net->dev.backend_features = 0;
156 net->backend = -1;
d5970055 157 }
81647a65 158 net->nc = options->net_backend;
d5970055 159
f56a1247
MT
160 net->dev.nvqs = 2;
161 net->dev.vqs = net->vqs;
830d70db 162 net->dev.vq_index = net->nc->queue_index;
f56a1247 163
81647a65 164 r = vhost_dev_init(&net->dev, options->opaque,
1a1bfac9 165 options->backend_type, options->force);
d5970055
MT
166 if (r < 0) {
167 goto fail;
168 }
1a1bfac9 169 if (backend_kernel) {
d8e80ae3
DM
170 if (!qemu_has_vnet_hdr_len(options->net_backend,
171 sizeof(struct virtio_net_hdr_mrg_rxbuf))) {
9a2ba823 172 net->dev.features &= ~(1ULL << VIRTIO_NET_F_MRG_RXBUF);
d8e80ae3 173 }
1a1bfac9
NN
174 if (~net->dev.features & net->dev.backend_features) {
175 fprintf(stderr, "vhost lacks feature mask %" PRIu64
176 " for backend\n",
177 (uint64_t)(~net->dev.features & net->dev.backend_features));
178 vhost_dev_cleanup(&net->dev);
179 goto fail;
180 }
d5970055 181 }
d5970055
MT
182 /* Set sane init value. Override when guest acks. */
183 vhost_net_ack_features(net, 0);
184 return net;
185fail:
7267c094 186 g_free(net);
d5970055
MT
187 return NULL;
188}
189
5430a28f
MT
190bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
191{
192 return vhost_dev_query(&net->dev, dev);
193}
194
cd7d1d26
JW
195static void vhost_net_set_vq_index(struct vhost_net *net, int vq_index)
196{
197 net->dev.vq_index = vq_index;
198}
199
a9f98bb5 200static int vhost_net_start_one(struct vhost_net *net,
cd7d1d26 201 VirtIODevice *dev)
d5970055
MT
202{
203 struct vhost_vring_file file = { };
204 int r;
b0b3db79 205
a9f98bb5
JW
206 net->dev.nvqs = 2;
207 net->dev.vqs = net->vqs;
a9f98bb5 208
b0b3db79
MT
209 r = vhost_dev_enable_notifiers(&net->dev, dev);
210 if (r < 0) {
211 goto fail_notifiers;
212 }
d5970055 213
d5970055
MT
214 r = vhost_dev_start(&net->dev, dev);
215 if (r < 0) {
b0b3db79 216 goto fail_start;
d5970055
MT
217 }
218
212d69f2
NN
219 if (net->nc->info->poll) {
220 net->nc->info->poll(net->nc, false);
221 }
222
1a1bfac9
NN
223 if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP) {
224 qemu_set_fd_handler(net->backend, NULL, NULL, NULL);
225 file.fd = net->backend;
226 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
227 const VhostOps *vhost_ops = net->dev.vhost_ops;
228 r = vhost_ops->vhost_call(&net->dev, VHOST_NET_SET_BACKEND,
229 &file);
230 if (r < 0) {
231 r = -errno;
232 goto fail;
233 }
d5970055
MT
234 }
235 }
236 return 0;
237fail:
238 file.fd = -1;
1a1bfac9
NN
239 if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP) {
240 while (file.index-- > 0) {
241 const VhostOps *vhost_ops = net->dev.vhost_ops;
242 int r = vhost_ops->vhost_call(&net->dev, VHOST_NET_SET_BACKEND,
243 &file);
244 assert(r >= 0);
245 }
d5970055 246 }
212d69f2
NN
247 if (net->nc->info->poll) {
248 net->nc->info->poll(net->nc, true);
249 }
d5970055 250 vhost_dev_stop(&net->dev, dev);
b0b3db79
MT
251fail_start:
252 vhost_dev_disable_notifiers(&net->dev, dev);
253fail_notifiers:
d5970055
MT
254 return r;
255}
256
a9f98bb5
JW
257static void vhost_net_stop_one(struct vhost_net *net,
258 VirtIODevice *dev)
d5970055
MT
259{
260 struct vhost_vring_file file = { .fd = -1 };
261
1a1bfac9
NN
262 if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP) {
263 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
264 const VhostOps *vhost_ops = net->dev.vhost_ops;
265 int r = vhost_ops->vhost_call(&net->dev, VHOST_NET_SET_BACKEND,
266 &file);
267 assert(r >= 0);
268 }
294ce717
LG
269 } else if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER) {
270 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
271 const VhostOps *vhost_ops = net->dev.vhost_ops;
272 int r = vhost_ops->vhost_call(&net->dev, VHOST_RESET_OWNER,
830d70db 273 &file);
294ce717
LG
274 assert(r >= 0);
275 }
d5970055 276 }
212d69f2
NN
277 if (net->nc->info->poll) {
278 net->nc->info->poll(net->nc, true);
279 }
d5970055 280 vhost_dev_stop(&net->dev, dev);
b0b3db79 281 vhost_dev_disable_notifiers(&net->dev, dev);
d5970055
MT
282}
283
371df9f5
GK
284static bool vhost_net_device_endian_ok(VirtIODevice *vdev)
285{
286#ifdef TARGET_IS_BIENDIAN
287#ifdef HOST_WORDS_BIGENDIAN
288 return virtio_is_big_endian(vdev);
289#else
290 return !virtio_is_big_endian(vdev);
291#endif
292#else
293 return true;
294#endif
295}
296
a9f98bb5
JW
297int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
298 int total_queues)
299{
1c819449
FK
300 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
301 VirtioBusState *vbus = VIRTIO_BUS(qbus);
302 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
cd7d1d26 303 int r, e, i;
a9f98bb5 304
371df9f5
GK
305 if (!vhost_net_device_endian_ok(dev)) {
306 error_report("vhost-net does not support cross-endian");
307 r = -ENOSYS;
308 goto err;
309 }
310
1c819449 311 if (!k->set_guest_notifiers) {
312fd5f2 312 error_report("binding does not support guest notifiers");
a9f98bb5
JW
313 r = -ENOSYS;
314 goto err;
315 }
316
317 for (i = 0; i < total_queues; i++) {
cd7d1d26 318 vhost_net_set_vq_index(get_vhost_net(ncs[i].peer), i * 2);
a9f98bb5
JW
319 }
320
1c819449 321 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, true);
a9f98bb5 322 if (r < 0) {
312fd5f2 323 error_report("Error binding guest notifier: %d", -r);
a9f98bb5
JW
324 goto err;
325 }
326
cd7d1d26
JW
327 for (i = 0; i < total_queues; i++) {
328 r = vhost_net_start_one(get_vhost_net(ncs[i].peer), dev);
329
330 if (r < 0) {
331 goto err_start;
332 }
333 }
334
a9f98bb5
JW
335 return 0;
336
cd7d1d26 337err_start:
a9f98bb5 338 while (--i >= 0) {
ed8b4afe 339 vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
a9f98bb5 340 }
cd7d1d26
JW
341 e = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
342 if (e < 0) {
343 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);
344 fflush(stderr);
345 }
346err:
a9f98bb5
JW
347 return r;
348}
349
350void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
351 int total_queues)
352{
1c819449
FK
353 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
354 VirtioBusState *vbus = VIRTIO_BUS(qbus);
355 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
a9f98bb5
JW
356 int i, r;
357
cd7d1d26
JW
358 for (i = 0; i < total_queues; i++) {
359 vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
360 }
361
1c819449 362 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
a9f98bb5
JW
363 if (r < 0) {
364 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r);
365 fflush(stderr);
366 }
367 assert(r >= 0);
a9f98bb5
JW
368}
369
d5970055
MT
370void vhost_net_cleanup(struct vhost_net *net)
371{
372 vhost_dev_cleanup(&net->dev);
7267c094 373 g_free(net);
d5970055 374}
f56a1247
MT
375
376bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
377{
378 return vhost_virtqueue_pending(&net->dev, idx);
379}
380
381void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
382 int idx, bool mask)
383{
384 vhost_virtqueue_mask(&net->dev, dev, idx, mask);
385}
ed8b4afe
NN
386
387VHostNetState *get_vhost_net(NetClientState *nc)
388{
389 VHostNetState *vhost_net = 0;
390
391 if (!nc) {
392 return 0;
393 }
394
395 switch (nc->info->type) {
396 case NET_CLIENT_OPTIONS_KIND_TAP:
397 vhost_net = tap_get_vhost_net(nc);
398 break;
03ce5744
NN
399 case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
400 vhost_net = vhost_user_get_vhost_net(nc);
401 break;
ed8b4afe
NN
402 default:
403 break;
404 }
405
406 return vhost_net;
407}
d5970055 408#else
81647a65 409struct vhost_net *vhost_net_init(VhostNetOptions *options)
5430a28f 410{
35f75462 411 error_report("vhost-net support is not compiled in");
5430a28f
MT
412 return NULL;
413}
414
415bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
d5970055 416{
5430a28f 417 return false;
d5970055
MT
418}
419
a9f98bb5
JW
420int vhost_net_start(VirtIODevice *dev,
421 NetClientState *ncs,
422 int total_queues)
d5970055 423{
5430a28f 424 return -ENOSYS;
d5970055 425}
a9f98bb5
JW
426void vhost_net_stop(VirtIODevice *dev,
427 NetClientState *ncs,
428 int total_queues)
d5970055
MT
429{
430}
431
432void vhost_net_cleanup(struct vhost_net *net)
433{
434}
435
9a2ba823 436uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features)
d5970055 437{
5430a28f 438 return features;
d5970055 439}
9a2ba823 440void vhost_net_ack_features(struct vhost_net *net, uint64_t features)
d5970055
MT
441{
442}
f56a1247
MT
443
444bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
445{
4dd72e04 446 return false;
f56a1247
MT
447}
448
449void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
450 int idx, bool mask)
451{
452}
ed8b4afe
NN
453
454VHostNetState *get_vhost_net(NetClientState *nc)
455{
456 return 0;
457}
d5970055 458#endif
This page took 0.55257 seconds and 4 git commands to generate.