4 * Copyright Red Hat, Inc. 2010
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"
19 #include "net/vhost-user.h"
21 #include "standard-headers/linux/vhost_types.h"
22 #include "hw/virtio/virtio-net.h"
23 #include "net/vhost_net.h"
24 #include "qemu/error-report.h"
27 #include <sys/socket.h>
28 #include <netpacket/packet.h>
29 #include <net/ethernet.h>
31 #include <netinet/in.h>
34 #include "standard-headers/linux/virtio_ring.h"
35 #include "hw/virtio/vhost.h"
36 #include "hw/virtio/virtio-bus.h"
40 struct vhost_virtqueue vqs[2];
45 /* Features supported by host kernel. */
46 static const int kernel_feature_bits[] = {
47 VIRTIO_F_NOTIFY_ON_EMPTY,
48 VIRTIO_RING_F_INDIRECT_DESC,
49 VIRTIO_RING_F_EVENT_IDX,
50 VIRTIO_NET_F_MRG_RXBUF,
53 VIRTIO_F_IOMMU_PLATFORM,
54 VHOST_INVALID_FEATURE_BIT
57 /* Features supported by others. */
58 static const int user_feature_bits[] = {
59 VIRTIO_F_NOTIFY_ON_EMPTY,
60 VIRTIO_RING_F_INDIRECT_DESC,
61 VIRTIO_RING_F_EVENT_IDX,
66 VIRTIO_NET_F_GUEST_CSUM,
68 VIRTIO_NET_F_GUEST_TSO4,
69 VIRTIO_NET_F_GUEST_TSO6,
70 VIRTIO_NET_F_GUEST_ECN,
71 VIRTIO_NET_F_GUEST_UFO,
72 VIRTIO_NET_F_HOST_TSO4,
73 VIRTIO_NET_F_HOST_TSO6,
74 VIRTIO_NET_F_HOST_ECN,
75 VIRTIO_NET_F_HOST_UFO,
76 VIRTIO_NET_F_MRG_RXBUF,
78 VIRTIO_F_IOMMU_PLATFORM,
80 /* This bit implies RARP isn't sent by QEMU out of band */
81 VIRTIO_NET_F_GUEST_ANNOUNCE,
85 VHOST_INVALID_FEATURE_BIT
88 static const int *vhost_net_get_feature_bits(struct vhost_net *net)
90 const int *feature_bits = 0;
92 switch (net->nc->info->type) {
93 case NET_CLIENT_DRIVER_TAP:
94 feature_bits = kernel_feature_bits;
96 case NET_CLIENT_DRIVER_VHOST_USER:
97 feature_bits = user_feature_bits;
100 error_report("Feature bits not defined for this type: %d",
101 net->nc->info->type);
108 uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features)
110 return vhost_get_features(&net->dev, vhost_net_get_feature_bits(net),
114 void vhost_net_ack_features(struct vhost_net *net, uint64_t features)
116 net->dev.acked_features = net->dev.backend_features;
117 vhost_ack_features(&net->dev, vhost_net_get_feature_bits(net), features);
120 uint64_t vhost_net_get_max_queues(VHostNetState *net)
122 return net->dev.max_queues;
125 uint64_t vhost_net_get_acked_features(VHostNetState *net)
127 return net->dev.acked_features;
130 static int vhost_net_get_fd(NetClientState *backend)
132 switch (backend->info->type) {
133 case NET_CLIENT_DRIVER_TAP:
134 return tap_get_fd(backend);
136 fprintf(stderr, "vhost-net requires tap backend\n");
141 struct vhost_net *vhost_net_init(VhostNetOptions *options)
144 bool backend_kernel = options->backend_type == VHOST_BACKEND_TYPE_KERNEL;
145 struct vhost_net *net = g_new0(struct vhost_net, 1);
146 uint64_t features = 0;
148 if (!options->net_backend) {
149 fprintf(stderr, "vhost-net requires net backend to be setup\n");
152 net->nc = options->net_backend;
154 net->dev.max_queues = 1;
156 net->dev.vqs = net->vqs;
158 if (backend_kernel) {
159 r = vhost_net_get_fd(options->net_backend);
163 net->dev.backend_features = qemu_has_vnet_hdr(options->net_backend)
164 ? 0 : (1ULL << VHOST_NET_F_VIRTIO_NET_HDR);
166 net->dev.protocol_features = 0;
168 net->dev.backend_features = 0;
169 net->dev.protocol_features = 0;
172 /* vhost-user needs vq_index to initiate a specific queue pair */
173 net->dev.vq_index = net->nc->queue_index * net->dev.nvqs;
176 r = vhost_dev_init(&net->dev, options->opaque,
177 options->backend_type, options->busyloop_timeout);
181 if (backend_kernel) {
182 if (!qemu_has_vnet_hdr_len(options->net_backend,
183 sizeof(struct virtio_net_hdr_mrg_rxbuf))) {
184 net->dev.features &= ~(1ULL << VIRTIO_NET_F_MRG_RXBUF);
186 if (~net->dev.features & net->dev.backend_features) {
187 fprintf(stderr, "vhost lacks feature mask %" PRIu64
189 (uint64_t)(~net->dev.features & net->dev.backend_features));
194 /* Set sane init value. Override when guest acks. */
195 #ifdef CONFIG_VHOST_NET_USER
196 if (net->nc->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
197 features = vhost_user_get_acked_features(net->nc);
198 if (~net->dev.features & features) {
199 fprintf(stderr, "vhost lacks feature mask %" PRIu64
201 (uint64_t)(~net->dev.features & features));
207 vhost_net_ack_features(net, features);
212 vhost_dev_cleanup(&net->dev);
217 static void vhost_net_set_vq_index(struct vhost_net *net, int vq_index)
219 net->dev.vq_index = vq_index;
222 static int vhost_net_start_one(struct vhost_net *net,
225 struct vhost_vring_file file = { };
229 net->dev.vqs = net->vqs;
231 r = vhost_dev_enable_notifiers(&net->dev, dev);
236 r = vhost_dev_start(&net->dev, dev);
241 if (net->nc->info->poll) {
242 net->nc->info->poll(net->nc, false);
245 if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) {
246 qemu_set_fd_handler(net->backend, NULL, NULL, NULL);
247 file.fd = net->backend;
248 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
249 r = vhost_net_set_backend(&net->dev, &file);
259 if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) {
260 while (file.index-- > 0) {
261 int r = vhost_net_set_backend(&net->dev, &file);
265 if (net->nc->info->poll) {
266 net->nc->info->poll(net->nc, true);
268 vhost_dev_stop(&net->dev, dev);
270 vhost_dev_disable_notifiers(&net->dev, dev);
275 static void vhost_net_stop_one(struct vhost_net *net,
278 struct vhost_vring_file file = { .fd = -1 };
280 if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) {
281 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
282 int r = vhost_net_set_backend(&net->dev, &file);
286 if (net->nc->info->poll) {
287 net->nc->info->poll(net->nc, true);
289 vhost_dev_stop(&net->dev, dev);
290 vhost_dev_disable_notifiers(&net->dev, dev);
293 int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
296 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
297 VirtioBusState *vbus = VIRTIO_BUS(qbus);
298 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
301 if (!k->set_guest_notifiers) {
302 error_report("binding does not support guest notifiers");
306 for (i = 0; i < total_queues; i++) {
307 struct vhost_net *net;
309 net = get_vhost_net(ncs[i].peer);
310 vhost_net_set_vq_index(net, i * 2);
312 /* Suppress the masking guest notifiers on vhost user
313 * because vhost user doesn't interrupt masking/unmasking
316 if (net->nc->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
317 dev->use_guest_notifier_mask = false;
321 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, true);
323 error_report("Error binding guest notifier: %d", -r);
327 for (i = 0; i < total_queues; i++) {
328 r = vhost_net_start_one(get_vhost_net(ncs[i].peer), dev);
334 if (ncs[i].peer->vring_enable) {
335 /* restore vring enable state */
336 r = vhost_set_vring_enable(ncs[i].peer, ncs[i].peer->vring_enable);
348 vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
350 e = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
352 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);
359 void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
362 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
363 VirtioBusState *vbus = VIRTIO_BUS(qbus);
364 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
367 for (i = 0; i < total_queues; i++) {
368 vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
371 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
373 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r);
379 void vhost_net_cleanup(struct vhost_net *net)
381 vhost_dev_cleanup(&net->dev);
384 int vhost_net_notify_migration_done(struct vhost_net *net, char* mac_addr)
386 const VhostOps *vhost_ops = net->dev.vhost_ops;
388 assert(vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
389 assert(vhost_ops->vhost_migration_done);
391 return vhost_ops->vhost_migration_done(&net->dev, mac_addr);
394 bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
396 return vhost_virtqueue_pending(&net->dev, idx);
399 void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
402 vhost_virtqueue_mask(&net->dev, dev, idx, mask);
405 VHostNetState *get_vhost_net(NetClientState *nc)
407 VHostNetState *vhost_net = 0;
413 switch (nc->info->type) {
414 case NET_CLIENT_DRIVER_TAP:
415 vhost_net = tap_get_vhost_net(nc);
417 #ifdef CONFIG_VHOST_NET_USER
418 case NET_CLIENT_DRIVER_VHOST_USER:
419 vhost_net = vhost_user_get_vhost_net(nc);
430 int vhost_set_vring_enable(NetClientState *nc, int enable)
432 VHostNetState *net = get_vhost_net(nc);
433 const VhostOps *vhost_ops = net->dev.vhost_ops;
435 nc->vring_enable = enable;
437 if (vhost_ops && vhost_ops->vhost_set_vring_enable) {
438 return vhost_ops->vhost_set_vring_enable(&net->dev, enable);
444 int vhost_net_set_mtu(struct vhost_net *net, uint16_t mtu)
446 const VhostOps *vhost_ops = net->dev.vhost_ops;
448 if (!vhost_ops->vhost_net_set_mtu) {
452 return vhost_ops->vhost_net_set_mtu(&net->dev, mtu);