target/arm: Implement MVE WLSTP insn
[qemu/ar7.git] / hw / net / vhost_net.c
blob44c1ed92dcd10f5f24fff30fd70d66619801e09e
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.
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.
16 #include "qemu/osdep.h"
17 #include "net/net.h"
18 #include "net/tap.h"
19 #include "net/vhost-user.h"
20 #include "net/vhost-vdpa.h"
22 #include "standard-headers/linux/vhost_types.h"
23 #include "hw/virtio/virtio-net.h"
24 #include "net/vhost_net.h"
25 #include "qemu/error-report.h"
26 #include "qemu/main-loop.h"
28 #include <sys/socket.h>
29 #include <net/if.h>
30 #include <netinet/in.h>
33 #include "standard-headers/linux/virtio_ring.h"
34 #include "hw/virtio/vhost.h"
35 #include "hw/virtio/virtio-bus.h"
38 /* Features supported by host kernel. */
39 static const int kernel_feature_bits[] = {
40 VIRTIO_F_NOTIFY_ON_EMPTY,
41 VIRTIO_RING_F_INDIRECT_DESC,
42 VIRTIO_RING_F_EVENT_IDX,
43 VIRTIO_NET_F_MRG_RXBUF,
44 VIRTIO_F_VERSION_1,
45 VIRTIO_NET_F_MTU,
46 VIRTIO_F_IOMMU_PLATFORM,
47 VIRTIO_F_RING_PACKED,
48 VIRTIO_NET_F_HASH_REPORT,
49 VHOST_INVALID_FEATURE_BIT
52 /* Features supported by others. */
53 static const int user_feature_bits[] = {
54 VIRTIO_F_NOTIFY_ON_EMPTY,
55 VIRTIO_RING_F_INDIRECT_DESC,
56 VIRTIO_RING_F_EVENT_IDX,
58 VIRTIO_F_ANY_LAYOUT,
59 VIRTIO_F_VERSION_1,
60 VIRTIO_NET_F_CSUM,
61 VIRTIO_NET_F_GUEST_CSUM,
62 VIRTIO_NET_F_GSO,
63 VIRTIO_NET_F_GUEST_TSO4,
64 VIRTIO_NET_F_GUEST_TSO6,
65 VIRTIO_NET_F_GUEST_ECN,
66 VIRTIO_NET_F_GUEST_UFO,
67 VIRTIO_NET_F_HOST_TSO4,
68 VIRTIO_NET_F_HOST_TSO6,
69 VIRTIO_NET_F_HOST_ECN,
70 VIRTIO_NET_F_HOST_UFO,
71 VIRTIO_NET_F_MRG_RXBUF,
72 VIRTIO_NET_F_MTU,
73 VIRTIO_F_IOMMU_PLATFORM,
74 VIRTIO_F_RING_PACKED,
75 VIRTIO_NET_F_RSS,
76 VIRTIO_NET_F_HASH_REPORT,
78 /* This bit implies RARP isn't sent by QEMU out of band */
79 VIRTIO_NET_F_GUEST_ANNOUNCE,
81 VIRTIO_NET_F_MQ,
83 VHOST_INVALID_FEATURE_BIT
86 static const int *vhost_net_get_feature_bits(struct vhost_net *net)
88 const int *feature_bits = 0;
90 switch (net->nc->info->type) {
91 case NET_CLIENT_DRIVER_TAP:
92 feature_bits = kernel_feature_bits;
93 break;
94 case NET_CLIENT_DRIVER_VHOST_USER:
95 feature_bits = user_feature_bits;
96 break;
97 #ifdef CONFIG_VHOST_NET_VDPA
98 case NET_CLIENT_DRIVER_VHOST_VDPA:
99 feature_bits = vdpa_feature_bits;
100 break;
101 #endif
102 default:
103 error_report("Feature bits not defined for this type: %d",
104 net->nc->info->type);
105 break;
108 return feature_bits;
111 uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features)
113 return vhost_get_features(&net->dev, vhost_net_get_feature_bits(net),
114 features);
116 int vhost_net_get_config(struct vhost_net *net, uint8_t *config,
117 uint32_t config_len)
119 return vhost_dev_get_config(&net->dev, config, config_len);
121 int vhost_net_set_config(struct vhost_net *net, const uint8_t *data,
122 uint32_t offset, uint32_t size, uint32_t flags)
124 return vhost_dev_set_config(&net->dev, data, offset, size, flags);
127 void vhost_net_ack_features(struct vhost_net *net, uint64_t features)
129 net->dev.acked_features = net->dev.backend_features;
130 vhost_ack_features(&net->dev, vhost_net_get_feature_bits(net), features);
133 uint64_t vhost_net_get_max_queues(VHostNetState *net)
135 return net->dev.max_queues;
138 uint64_t vhost_net_get_acked_features(VHostNetState *net)
140 return net->dev.acked_features;
143 static int vhost_net_get_fd(NetClientState *backend)
145 switch (backend->info->type) {
146 case NET_CLIENT_DRIVER_TAP:
147 return tap_get_fd(backend);
148 default:
149 fprintf(stderr, "vhost-net requires tap backend\n");
150 return -ENOSYS;
154 struct vhost_net *vhost_net_init(VhostNetOptions *options)
156 int r;
157 bool backend_kernel = options->backend_type == VHOST_BACKEND_TYPE_KERNEL;
158 struct vhost_net *net = g_new0(struct vhost_net, 1);
159 uint64_t features = 0;
161 if (!options->net_backend) {
162 fprintf(stderr, "vhost-net requires net backend to be setup\n");
163 goto fail;
165 net->nc = options->net_backend;
167 net->dev.max_queues = 1;
168 net->dev.nvqs = 2;
169 net->dev.vqs = net->vqs;
171 if (backend_kernel) {
172 r = vhost_net_get_fd(options->net_backend);
173 if (r < 0) {
174 goto fail;
176 net->dev.backend_features = qemu_has_vnet_hdr(options->net_backend)
177 ? 0 : (1ULL << VHOST_NET_F_VIRTIO_NET_HDR);
178 net->backend = r;
179 net->dev.protocol_features = 0;
180 } else {
181 net->dev.backend_features = 0;
182 net->dev.protocol_features = 0;
183 net->backend = -1;
185 /* vhost-user needs vq_index to initiate a specific queue pair */
186 net->dev.vq_index = net->nc->queue_index * net->dev.nvqs;
189 r = vhost_dev_init(&net->dev, options->opaque,
190 options->backend_type, options->busyloop_timeout);
191 if (r < 0) {
192 goto fail;
194 if (backend_kernel) {
195 if (!qemu_has_vnet_hdr_len(options->net_backend,
196 sizeof(struct virtio_net_hdr_mrg_rxbuf))) {
197 net->dev.features &= ~(1ULL << VIRTIO_NET_F_MRG_RXBUF);
199 if (~net->dev.features & net->dev.backend_features) {
200 fprintf(stderr, "vhost lacks feature mask %" PRIu64
201 " for backend\n",
202 (uint64_t)(~net->dev.features & net->dev.backend_features));
203 goto fail;
207 /* Set sane init value. Override when guest acks. */
208 #ifdef CONFIG_VHOST_NET_USER
209 if (net->nc->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
210 features = vhost_user_get_acked_features(net->nc);
211 if (~net->dev.features & features) {
212 fprintf(stderr, "vhost lacks feature mask %" PRIu64
213 " for backend\n",
214 (uint64_t)(~net->dev.features & features));
215 goto fail;
218 #endif
220 vhost_net_ack_features(net, features);
222 return net;
224 fail:
225 vhost_dev_cleanup(&net->dev);
226 g_free(net);
227 return NULL;
230 static void vhost_net_set_vq_index(struct vhost_net *net, int vq_index)
232 net->dev.vq_index = vq_index;
235 static int vhost_net_start_one(struct vhost_net *net,
236 VirtIODevice *dev)
238 struct vhost_vring_file file = { };
239 int r;
241 net->dev.nvqs = 2;
242 net->dev.vqs = net->vqs;
244 r = vhost_dev_enable_notifiers(&net->dev, dev);
245 if (r < 0) {
246 goto fail_notifiers;
249 r = vhost_dev_start(&net->dev, dev);
250 if (r < 0) {
251 goto fail_start;
254 if (net->nc->info->poll) {
255 net->nc->info->poll(net->nc, false);
258 if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) {
259 qemu_set_fd_handler(net->backend, NULL, NULL, NULL);
260 file.fd = net->backend;
261 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
262 if (!virtio_queue_enabled(dev, net->dev.vq_index +
263 file.index)) {
264 /* Queue might not be ready for start */
265 continue;
267 r = vhost_net_set_backend(&net->dev, &file);
268 if (r < 0) {
269 r = -errno;
270 goto fail;
274 return 0;
275 fail:
276 file.fd = -1;
277 if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) {
278 while (file.index-- > 0) {
279 if (!virtio_queue_enabled(dev, net->dev.vq_index +
280 file.index)) {
281 /* Queue might not be ready for start */
282 continue;
284 int r = vhost_net_set_backend(&net->dev, &file);
285 assert(r >= 0);
288 if (net->nc->info->poll) {
289 net->nc->info->poll(net->nc, true);
291 vhost_dev_stop(&net->dev, dev);
292 fail_start:
293 vhost_dev_disable_notifiers(&net->dev, dev);
294 fail_notifiers:
295 return r;
298 static void vhost_net_stop_one(struct vhost_net *net,
299 VirtIODevice *dev)
301 struct vhost_vring_file file = { .fd = -1 };
303 if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) {
304 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
305 int r = vhost_net_set_backend(&net->dev, &file);
306 assert(r >= 0);
309 if (net->nc->info->poll) {
310 net->nc->info->poll(net->nc, true);
312 vhost_dev_stop(&net->dev, dev);
313 vhost_dev_disable_notifiers(&net->dev, dev);
316 int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
317 int total_queues)
319 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
320 VirtioBusState *vbus = VIRTIO_BUS(qbus);
321 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
322 struct vhost_net *net;
323 int r, e, i;
324 NetClientState *peer;
326 if (!k->set_guest_notifiers) {
327 error_report("binding does not support guest notifiers");
328 return -ENOSYS;
331 for (i = 0; i < total_queues; i++) {
333 peer = qemu_get_peer(ncs, i);
334 net = get_vhost_net(peer);
335 vhost_net_set_vq_index(net, i * 2);
337 /* Suppress the masking guest notifiers on vhost user
338 * because vhost user doesn't interrupt masking/unmasking
339 * properly.
341 if (net->nc->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
342 dev->use_guest_notifier_mask = false;
346 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, true);
347 if (r < 0) {
348 error_report("Error binding guest notifier: %d", -r);
349 goto err;
352 for (i = 0; i < total_queues; i++) {
353 peer = qemu_get_peer(ncs, i);
354 r = vhost_net_start_one(get_vhost_net(peer), dev);
356 if (r < 0) {
357 goto err_start;
360 if (peer->vring_enable) {
361 /* restore vring enable state */
362 r = vhost_set_vring_enable(peer, peer->vring_enable);
364 if (r < 0) {
365 goto err_start;
370 return 0;
372 err_start:
373 while (--i >= 0) {
374 peer = qemu_get_peer(ncs , i);
375 vhost_net_stop_one(get_vhost_net(peer), dev);
377 e = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
378 if (e < 0) {
379 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);
380 fflush(stderr);
382 err:
383 return r;
386 void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
387 int total_queues)
389 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
390 VirtioBusState *vbus = VIRTIO_BUS(qbus);
391 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
392 int i, r;
394 for (i = 0; i < total_queues; i++) {
395 vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
398 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
399 if (r < 0) {
400 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r);
401 fflush(stderr);
403 assert(r >= 0);
406 void vhost_net_cleanup(struct vhost_net *net)
408 vhost_dev_cleanup(&net->dev);
411 int vhost_net_notify_migration_done(struct vhost_net *net, char* mac_addr)
413 const VhostOps *vhost_ops = net->dev.vhost_ops;
415 assert(vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
416 assert(vhost_ops->vhost_migration_done);
418 return vhost_ops->vhost_migration_done(&net->dev, mac_addr);
421 bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
423 return vhost_virtqueue_pending(&net->dev, idx);
426 void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
427 int idx, bool mask)
429 vhost_virtqueue_mask(&net->dev, dev, idx, mask);
432 VHostNetState *get_vhost_net(NetClientState *nc)
434 VHostNetState *vhost_net = 0;
436 if (!nc) {
437 return 0;
440 switch (nc->info->type) {
441 case NET_CLIENT_DRIVER_TAP:
442 vhost_net = tap_get_vhost_net(nc);
443 break;
444 #ifdef CONFIG_VHOST_NET_USER
445 case NET_CLIENT_DRIVER_VHOST_USER:
446 vhost_net = vhost_user_get_vhost_net(nc);
447 assert(vhost_net);
448 break;
449 #endif
450 #ifdef CONFIG_VHOST_NET_VDPA
451 case NET_CLIENT_DRIVER_VHOST_VDPA:
452 vhost_net = vhost_vdpa_get_vhost_net(nc);
453 assert(vhost_net);
454 break;
455 #endif
456 default:
457 break;
460 return vhost_net;
463 int vhost_set_vring_enable(NetClientState *nc, int enable)
465 VHostNetState *net = get_vhost_net(nc);
466 const VhostOps *vhost_ops = net->dev.vhost_ops;
468 nc->vring_enable = enable;
470 if (vhost_ops && vhost_ops->vhost_set_vring_enable) {
471 return vhost_ops->vhost_set_vring_enable(&net->dev, enable);
474 return 0;
477 int vhost_net_set_mtu(struct vhost_net *net, uint16_t mtu)
479 const VhostOps *vhost_ops = net->dev.vhost_ops;
481 if (!vhost_ops->vhost_net_set_mtu) {
482 return 0;
485 return vhost_ops->vhost_net_set_mtu(&net->dev, mtu);