]> git.proxmox.com Git - mirror_qemu.git/blame - hw/net/vhost_net.c
s390x: Migrate guest storage keys (initial memory only)
[mirror_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 <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.
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"
5be7d9f1 41#include "hw/virtio/virtio-access.h"
d5970055
MT
42
43struct vhost_net {
44 struct vhost_dev dev;
45 struct vhost_virtqueue vqs[2];
46 int backend;
35277d14 47 NetClientState *nc;
d5970055
MT
48};
49
2e6d46d7
NN
50/* Features supported by host kernel. */
51static const int kernel_feature_bits[] = {
52 VIRTIO_F_NOTIFY_ON_EMPTY,
53 VIRTIO_RING_F_INDIRECT_DESC,
54 VIRTIO_RING_F_EVENT_IDX,
55 VIRTIO_NET_F_MRG_RXBUF,
b1506132 56 VIRTIO_F_VERSION_1,
2e6d46d7
NN
57 VHOST_INVALID_FEATURE_BIT
58};
59
5f4c01ca 60/* Features supported by others. */
d122f1a2 61static const int user_feature_bits[] = {
5f4c01ca
NN
62 VIRTIO_F_NOTIFY_ON_EMPTY,
63 VIRTIO_RING_F_INDIRECT_DESC,
64 VIRTIO_RING_F_EVENT_IDX,
65
66 VIRTIO_F_ANY_LAYOUT,
b1506132 67 VIRTIO_F_VERSION_1,
5f4c01ca
NN
68 VIRTIO_NET_F_CSUM,
69 VIRTIO_NET_F_GUEST_CSUM,
70 VIRTIO_NET_F_GSO,
71 VIRTIO_NET_F_GUEST_TSO4,
72 VIRTIO_NET_F_GUEST_TSO6,
73 VIRTIO_NET_F_GUEST_ECN,
74 VIRTIO_NET_F_GUEST_UFO,
75 VIRTIO_NET_F_HOST_TSO4,
76 VIRTIO_NET_F_HOST_TSO6,
77 VIRTIO_NET_F_HOST_ECN,
78 VIRTIO_NET_F_HOST_UFO,
79 VIRTIO_NET_F_MRG_RXBUF,
80 VIRTIO_NET_F_STATUS,
81 VIRTIO_NET_F_CTRL_VQ,
82 VIRTIO_NET_F_CTRL_RX,
83 VIRTIO_NET_F_CTRL_VLAN,
84 VIRTIO_NET_F_CTRL_RX_EXTRA,
85 VIRTIO_NET_F_CTRL_MAC_ADDR,
86 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS,
87
88 VIRTIO_NET_F_MQ,
89
90 VHOST_INVALID_FEATURE_BIT
91};
92
2e6d46d7 93static const int *vhost_net_get_feature_bits(struct vhost_net *net)
d5970055 94{
2e6d46d7
NN
95 const int *feature_bits = 0;
96
97 switch (net->nc->info->type) {
98 case NET_CLIENT_OPTIONS_KIND_TAP:
99 feature_bits = kernel_feature_bits;
100 break;
5f4c01ca
NN
101 case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
102 feature_bits = user_feature_bits;
103 break;
2e6d46d7
NN
104 default:
105 error_report("Feature bits not defined for this type: %d",
106 net->nc->info->type);
107 break;
ca736c8e 108 }
2e6d46d7
NN
109
110 return feature_bits;
111}
112
9a2ba823 113uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features)
2e6d46d7
NN
114{
115 return vhost_get_features(&net->dev, vhost_net_get_feature_bits(net),
116 features);
d5970055
MT
117}
118
9a2ba823 119void vhost_net_ack_features(struct vhost_net *net, uint64_t features)
d5970055 120{
b49ae913 121 net->dev.acked_features = net->dev.backend_features;
2e6d46d7 122 vhost_ack_features(&net->dev, vhost_net_get_feature_bits(net), features);
d5970055
MT
123}
124
4e68f7a0 125static int vhost_net_get_fd(NetClientState *backend)
d5970055
MT
126{
127 switch (backend->info->type) {
2be64a68 128 case NET_CLIENT_OPTIONS_KIND_TAP:
d5970055
MT
129 return tap_get_fd(backend);
130 default:
131 fprintf(stderr, "vhost-net requires tap backend\n");
132 return -EBADFD;
133 }
134}
135
81647a65 136struct vhost_net *vhost_net_init(VhostNetOptions *options)
d5970055
MT
137{
138 int r;
1a1bfac9 139 bool backend_kernel = options->backend_type == VHOST_BACKEND_TYPE_KERNEL;
7267c094 140 struct vhost_net *net = g_malloc(sizeof *net);
81647a65
NN
141
142 if (!options->net_backend) {
143 fprintf(stderr, "vhost-net requires net backend to be setup\n");
d5970055
MT
144 goto fail;
145 }
81647a65 146
1a1bfac9
NN
147 if (backend_kernel) {
148 r = vhost_net_get_fd(options->net_backend);
149 if (r < 0) {
150 goto fail;
151 }
152 net->dev.backend_features = qemu_has_vnet_hdr(options->net_backend)
9a2ba823 153 ? 0 : (1ULL << VHOST_NET_F_VIRTIO_NET_HDR);
1a1bfac9
NN
154 net->backend = r;
155 } else {
156 net->dev.backend_features = 0;
157 net->backend = -1;
d5970055 158 }
81647a65 159 net->nc = options->net_backend;
d5970055 160
f56a1247
MT
161 net->dev.nvqs = 2;
162 net->dev.vqs = net->vqs;
163
81647a65 164 r = vhost_dev_init(&net->dev, options->opaque,
1e7398a1 165 options->backend_type);
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
cd7d1d26
JW
190static void vhost_net_set_vq_index(struct vhost_net *net, int vq_index)
191{
192 net->dev.vq_index = vq_index;
193}
194
5be7d9f1
GK
195static int vhost_net_set_vnet_endian(VirtIODevice *dev, NetClientState *peer,
196 bool set)
197{
198 int r = 0;
199
200 if (virtio_has_feature(dev, VIRTIO_F_VERSION_1) ||
201 (virtio_legacy_is_cross_endian(dev) && !virtio_is_big_endian(dev))) {
202 r = qemu_set_vnet_le(peer, set);
203 if (r) {
204 error_report("backend does not support LE vnet headers");
205 }
206 } else if (virtio_legacy_is_cross_endian(dev)) {
207 r = qemu_set_vnet_be(peer, set);
208 if (r) {
209 error_report("backend does not support BE vnet headers");
210 }
211 }
212
213 return r;
214}
215
a9f98bb5 216static int vhost_net_start_one(struct vhost_net *net,
cd7d1d26 217 VirtIODevice *dev)
d5970055
MT
218{
219 struct vhost_vring_file file = { };
220 int r;
b0b3db79 221
a9f98bb5
JW
222 net->dev.nvqs = 2;
223 net->dev.vqs = net->vqs;
a9f98bb5 224
b0b3db79
MT
225 r = vhost_dev_enable_notifiers(&net->dev, dev);
226 if (r < 0) {
227 goto fail_notifiers;
228 }
d5970055 229
d5970055
MT
230 r = vhost_dev_start(&net->dev, dev);
231 if (r < 0) {
b0b3db79 232 goto fail_start;
d5970055
MT
233 }
234
212d69f2
NN
235 if (net->nc->info->poll) {
236 net->nc->info->poll(net->nc, false);
237 }
238
1a1bfac9
NN
239 if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP) {
240 qemu_set_fd_handler(net->backend, NULL, NULL, NULL);
241 file.fd = net->backend;
242 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
243 const VhostOps *vhost_ops = net->dev.vhost_ops;
244 r = vhost_ops->vhost_call(&net->dev, VHOST_NET_SET_BACKEND,
245 &file);
246 if (r < 0) {
247 r = -errno;
248 goto fail;
249 }
d5970055
MT
250 }
251 }
252 return 0;
253fail:
254 file.fd = -1;
1a1bfac9
NN
255 if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP) {
256 while (file.index-- > 0) {
257 const VhostOps *vhost_ops = net->dev.vhost_ops;
258 int r = vhost_ops->vhost_call(&net->dev, VHOST_NET_SET_BACKEND,
259 &file);
260 assert(r >= 0);
261 }
d5970055 262 }
212d69f2
NN
263 if (net->nc->info->poll) {
264 net->nc->info->poll(net->nc, true);
265 }
d5970055 266 vhost_dev_stop(&net->dev, dev);
b0b3db79
MT
267fail_start:
268 vhost_dev_disable_notifiers(&net->dev, dev);
269fail_notifiers:
d5970055
MT
270 return r;
271}
272
a9f98bb5
JW
273static void vhost_net_stop_one(struct vhost_net *net,
274 VirtIODevice *dev)
d5970055
MT
275{
276 struct vhost_vring_file file = { .fd = -1 };
277
1a1bfac9
NN
278 if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP) {
279 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
280 const VhostOps *vhost_ops = net->dev.vhost_ops;
281 int r = vhost_ops->vhost_call(&net->dev, VHOST_NET_SET_BACKEND,
282 &file);
283 assert(r >= 0);
284 }
294ce717
LG
285 } else if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER) {
286 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
287 const VhostOps *vhost_ops = net->dev.vhost_ops;
288 int r = vhost_ops->vhost_call(&net->dev, VHOST_RESET_OWNER,
d345ed2d 289 NULL);
294ce717
LG
290 assert(r >= 0);
291 }
d5970055 292 }
212d69f2
NN
293 if (net->nc->info->poll) {
294 net->nc->info->poll(net->nc, true);
295 }
d5970055 296 vhost_dev_stop(&net->dev, dev);
b0b3db79 297 vhost_dev_disable_notifiers(&net->dev, dev);
d5970055
MT
298}
299
a9f98bb5
JW
300int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
301 int total_queues)
302{
1c819449
FK
303 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
304 VirtioBusState *vbus = VIRTIO_BUS(qbus);
305 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
cd7d1d26 306 int r, e, i;
a9f98bb5 307
1c819449 308 if (!k->set_guest_notifiers) {
312fd5f2 309 error_report("binding does not support guest notifiers");
a9f98bb5
JW
310 r = -ENOSYS;
311 goto err;
312 }
313
5be7d9f1
GK
314 r = vhost_net_set_vnet_endian(dev, ncs[0].peer, true);
315 if (r < 0) {
316 goto err;
317 }
318
a9f98bb5 319 for (i = 0; i < total_queues; i++) {
cd7d1d26 320 vhost_net_set_vq_index(get_vhost_net(ncs[i].peer), i * 2);
a9f98bb5
JW
321 }
322
1c819449 323 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, true);
a9f98bb5 324 if (r < 0) {
312fd5f2 325 error_report("Error binding guest notifier: %d", -r);
5be7d9f1 326 goto err_endian;
a9f98bb5
JW
327 }
328
cd7d1d26
JW
329 for (i = 0; i < total_queues; i++) {
330 r = vhost_net_start_one(get_vhost_net(ncs[i].peer), dev);
331
332 if (r < 0) {
333 goto err_start;
334 }
335 }
336
a9f98bb5
JW
337 return 0;
338
cd7d1d26 339err_start:
a9f98bb5 340 while (--i >= 0) {
ed8b4afe 341 vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
a9f98bb5 342 }
cd7d1d26
JW
343 e = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
344 if (e < 0) {
345 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);
346 fflush(stderr);
347 }
5be7d9f1
GK
348err_endian:
349 vhost_net_set_vnet_endian(dev, ncs[0].peer, false);
cd7d1d26 350err:
a9f98bb5
JW
351 return r;
352}
353
354void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
355 int total_queues)
356{
1c819449
FK
357 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
358 VirtioBusState *vbus = VIRTIO_BUS(qbus);
359 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
a9f98bb5
JW
360 int i, r;
361
cd7d1d26
JW
362 for (i = 0; i < total_queues; i++) {
363 vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
364 }
365
1c819449 366 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
a9f98bb5
JW
367 if (r < 0) {
368 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r);
369 fflush(stderr);
370 }
371 assert(r >= 0);
5be7d9f1
GK
372
373 assert(vhost_net_set_vnet_endian(dev, ncs[0].peer, false) >= 0);
a9f98bb5
JW
374}
375
d5970055
MT
376void vhost_net_cleanup(struct vhost_net *net)
377{
378 vhost_dev_cleanup(&net->dev);
7267c094 379 g_free(net);
d5970055 380}
f56a1247
MT
381
382bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
383{
384 return vhost_virtqueue_pending(&net->dev, idx);
385}
386
387void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
388 int idx, bool mask)
389{
390 vhost_virtqueue_mask(&net->dev, dev, idx, mask);
391}
ed8b4afe
NN
392
393VHostNetState *get_vhost_net(NetClientState *nc)
394{
395 VHostNetState *vhost_net = 0;
396
397 if (!nc) {
398 return 0;
399 }
400
401 switch (nc->info->type) {
402 case NET_CLIENT_OPTIONS_KIND_TAP:
403 vhost_net = tap_get_vhost_net(nc);
404 break;
03ce5744
NN
405 case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
406 vhost_net = vhost_user_get_vhost_net(nc);
407 break;
ed8b4afe
NN
408 default:
409 break;
410 }
411
412 return vhost_net;
413}
d5970055 414#else
81647a65 415struct vhost_net *vhost_net_init(VhostNetOptions *options)
5430a28f 416{
35f75462 417 error_report("vhost-net support is not compiled in");
5430a28f
MT
418 return NULL;
419}
420
a9f98bb5
JW
421int vhost_net_start(VirtIODevice *dev,
422 NetClientState *ncs,
423 int total_queues)
d5970055 424{
5430a28f 425 return -ENOSYS;
d5970055 426}
a9f98bb5
JW
427void vhost_net_stop(VirtIODevice *dev,
428 NetClientState *ncs,
429 int total_queues)
d5970055
MT
430{
431}
432
433void vhost_net_cleanup(struct vhost_net *net)
434{
435}
436
9a2ba823 437uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features)
d5970055 438{
5430a28f 439 return features;
d5970055 440}
9a2ba823 441void vhost_net_ack_features(struct vhost_net *net, uint64_t features)
d5970055
MT
442{
443}
f56a1247
MT
444
445bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
446{
4dd72e04 447 return false;
f56a1247
MT
448}
449
450void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
451 int idx, bool mask)
452{
453}
ed8b4afe
NN
454
455VHostNetState *get_vhost_net(NetClientState *nc)
456{
457 return 0;
458}
d5970055 459#endif