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.
18 #include "net/vhost-user.h"
20 #include "hw/virtio/virtio-net.h"
21 #include "net/vhost_net.h"
22 #include "qemu/error-report.h"
26 #ifdef CONFIG_VHOST_NET
27 #include <linux/vhost.h>
28 #include <sys/socket.h>
29 #include <linux/kvm.h>
31 #include <netpacket/packet.h>
32 #include <net/ethernet.h>
34 #include <netinet/in.h>
38 #include "standard-headers/linux/virtio_ring.h"
39 #include "hw/virtio/vhost.h"
40 #include "hw/virtio/virtio-bus.h"
44 struct vhost_virtqueue vqs[2];
49 /* Features supported by host kernel. */
50 static 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,
56 VHOST_INVALID_FEATURE_BIT
59 /* Features supported by others. */
60 static const int user_feature_bits[] = {
61 VIRTIO_F_NOTIFY_ON_EMPTY,
62 VIRTIO_RING_F_INDIRECT_DESC,
63 VIRTIO_RING_F_EVENT_IDX,
68 VIRTIO_NET_F_GUEST_CSUM,
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,
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,
89 VHOST_INVALID_FEATURE_BIT
92 static const int *vhost_net_get_feature_bits(struct vhost_net *net)
94 const int *feature_bits = 0;
96 switch (net->nc->info->type) {
97 case NET_CLIENT_OPTIONS_KIND_TAP:
98 feature_bits = kernel_feature_bits;
100 case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
101 feature_bits = user_feature_bits;
104 error_report("Feature bits not defined for this type: %d",
105 net->nc->info->type);
112 uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features)
114 return vhost_get_features(&net->dev, vhost_net_get_feature_bits(net),
118 void vhost_net_ack_features(struct vhost_net *net, uint64_t features)
120 net->dev.acked_features = net->dev.backend_features;
121 vhost_ack_features(&net->dev, vhost_net_get_feature_bits(net), features);
124 static int vhost_net_get_fd(NetClientState *backend)
126 switch (backend->info->type) {
127 case NET_CLIENT_OPTIONS_KIND_TAP:
128 return tap_get_fd(backend);
130 fprintf(stderr, "vhost-net requires tap backend\n");
135 struct vhost_net *vhost_net_init(VhostNetOptions *options)
138 bool backend_kernel = options->backend_type == VHOST_BACKEND_TYPE_KERNEL;
139 struct vhost_net *net = g_malloc(sizeof *net);
141 if (!options->net_backend) {
142 fprintf(stderr, "vhost-net requires net backend to be setup\n");
146 if (backend_kernel) {
147 r = vhost_net_get_fd(options->net_backend);
151 net->dev.backend_features = qemu_has_vnet_hdr(options->net_backend)
152 ? 0 : (1ULL << VHOST_NET_F_VIRTIO_NET_HDR);
155 net->dev.backend_features = 0;
158 net->nc = options->net_backend;
161 net->dev.vqs = net->vqs;
162 net->dev.vq_index = net->nc->queue_index;
164 r = vhost_dev_init(&net->dev, options->opaque,
165 options->backend_type, options->force);
169 if (backend_kernel) {
170 if (!qemu_has_vnet_hdr_len(options->net_backend,
171 sizeof(struct virtio_net_hdr_mrg_rxbuf))) {
172 net->dev.features &= ~(1ULL << VIRTIO_NET_F_MRG_RXBUF);
174 if (~net->dev.features & net->dev.backend_features) {
175 fprintf(stderr, "vhost lacks feature mask %" PRIu64
177 (uint64_t)(~net->dev.features & net->dev.backend_features));
178 vhost_dev_cleanup(&net->dev);
182 /* Set sane init value. Override when guest acks. */
183 vhost_net_ack_features(net, 0);
190 bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
192 return vhost_dev_query(&net->dev, dev);
195 static void vhost_net_set_vq_index(struct vhost_net *net, int vq_index)
197 net->dev.vq_index = vq_index;
200 static int vhost_net_start_one(struct vhost_net *net,
203 struct vhost_vring_file file = { };
207 net->dev.vqs = net->vqs;
209 r = vhost_dev_enable_notifiers(&net->dev, dev);
214 r = vhost_dev_start(&net->dev, dev);
219 if (net->nc->info->poll) {
220 net->nc->info->poll(net->nc, false);
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,
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,
247 if (net->nc->info->poll) {
248 net->nc->info->poll(net->nc, true);
250 vhost_dev_stop(&net->dev, dev);
252 vhost_dev_disable_notifiers(&net->dev, dev);
257 static void vhost_net_stop_one(struct vhost_net *net,
260 struct vhost_vring_file file = { .fd = -1 };
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,
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,
277 if (net->nc->info->poll) {
278 net->nc->info->poll(net->nc, true);
280 vhost_dev_stop(&net->dev, dev);
281 vhost_dev_disable_notifiers(&net->dev, dev);
284 static bool vhost_net_device_endian_ok(VirtIODevice *vdev)
286 #ifdef TARGET_IS_BIENDIAN
287 #ifdef HOST_WORDS_BIGENDIAN
288 return virtio_is_big_endian(vdev);
290 return !virtio_is_big_endian(vdev);
297 int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
300 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
301 VirtioBusState *vbus = VIRTIO_BUS(qbus);
302 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
305 if (!vhost_net_device_endian_ok(dev)) {
306 error_report("vhost-net does not support cross-endian");
311 if (!k->set_guest_notifiers) {
312 error_report("binding does not support guest notifiers");
317 for (i = 0; i < total_queues; i++) {
318 vhost_net_set_vq_index(get_vhost_net(ncs[i].peer), i * 2);
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);
339 vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
341 e = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
343 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);
350 void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
353 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
354 VirtioBusState *vbus = VIRTIO_BUS(qbus);
355 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
358 for (i = 0; i < total_queues; i++) {
359 vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
362 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
364 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r);
370 void vhost_net_cleanup(struct vhost_net *net)
372 vhost_dev_cleanup(&net->dev);
376 bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
378 return vhost_virtqueue_pending(&net->dev, idx);
381 void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
384 vhost_virtqueue_mask(&net->dev, dev, idx, mask);
387 VHostNetState *get_vhost_net(NetClientState *nc)
389 VHostNetState *vhost_net = 0;
395 switch (nc->info->type) {
396 case NET_CLIENT_OPTIONS_KIND_TAP:
397 vhost_net = tap_get_vhost_net(nc);
399 case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
400 vhost_net = vhost_user_get_vhost_net(nc);
409 struct vhost_net *vhost_net_init(VhostNetOptions *options)
411 error_report("vhost-net support is not compiled in");
415 bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
420 int vhost_net_start(VirtIODevice *dev,
426 void vhost_net_stop(VirtIODevice *dev,
432 void vhost_net_cleanup(struct vhost_net *net)
436 uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features)
440 void vhost_net_ack_features(struct vhost_net *net, uint64_t features)
444 bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
449 void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
454 VHostNetState *get_vhost_net(NetClientState *nc)