4 * Copyright Red Hat, Inc. 2010
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.
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"
41 #include "hw/virtio/virtio-access.h"
45 struct vhost_virtqueue vqs
[2];
50 /* Features supported by host kernel. */
51 static 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
,
57 VHOST_INVALID_FEATURE_BIT
60 /* Features supported by others. */
61 static const int user_feature_bits
[] = {
62 VIRTIO_F_NOTIFY_ON_EMPTY
,
63 VIRTIO_RING_F_INDIRECT_DESC
,
64 VIRTIO_RING_F_EVENT_IDX
,
69 VIRTIO_NET_F_GUEST_CSUM
,
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
,
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
,
88 VIRTIO_NET_F_GUEST_ANNOUNCE
,
92 VHOST_INVALID_FEATURE_BIT
95 static const int *vhost_net_get_feature_bits(struct vhost_net
*net
)
97 const int *feature_bits
= 0;
99 switch (net
->nc
->info
->type
) {
100 case NET_CLIENT_OPTIONS_KIND_TAP
:
101 feature_bits
= kernel_feature_bits
;
103 case NET_CLIENT_OPTIONS_KIND_VHOST_USER
:
104 feature_bits
= user_feature_bits
;
107 error_report("Feature bits not defined for this type: %d",
108 net
->nc
->info
->type
);
115 uint64_t vhost_net_get_features(struct vhost_net
*net
, uint64_t features
)
117 return vhost_get_features(&net
->dev
, vhost_net_get_feature_bits(net
),
121 void vhost_net_ack_features(struct vhost_net
*net
, uint64_t features
)
123 net
->dev
.acked_features
= net
->dev
.backend_features
;
124 vhost_ack_features(&net
->dev
, vhost_net_get_feature_bits(net
), features
);
127 uint64_t vhost_net_get_max_queues(VHostNetState
*net
)
129 return net
->dev
.max_queues
;
132 static int vhost_net_get_fd(NetClientState
*backend
)
134 switch (backend
->info
->type
) {
135 case NET_CLIENT_OPTIONS_KIND_TAP
:
136 return tap_get_fd(backend
);
138 fprintf(stderr
, "vhost-net requires tap backend\n");
143 struct vhost_net
*vhost_net_init(VhostNetOptions
*options
)
146 bool backend_kernel
= options
->backend_type
== VHOST_BACKEND_TYPE_KERNEL
;
147 struct vhost_net
*net
= g_malloc(sizeof *net
);
149 if (!options
->net_backend
) {
150 fprintf(stderr
, "vhost-net requires net backend to be setup\n");
153 net
->nc
= options
->net_backend
;
155 net
->dev
.max_queues
= 1;
157 net
->dev
.vqs
= net
->vqs
;
159 if (backend_kernel
) {
160 r
= vhost_net_get_fd(options
->net_backend
);
164 net
->dev
.backend_features
= qemu_has_vnet_hdr(options
->net_backend
)
165 ? 0 : (1ULL << VHOST_NET_F_VIRTIO_NET_HDR
);
167 net
->dev
.protocol_features
= 0;
169 net
->dev
.backend_features
= 0;
170 net
->dev
.protocol_features
= 0;
173 /* vhost-user needs vq_index to initiate a specific queue pair */
174 net
->dev
.vq_index
= net
->nc
->queue_index
* net
->dev
.nvqs
;
177 r
= vhost_dev_init(&net
->dev
, options
->opaque
,
178 options
->backend_type
);
182 if (backend_kernel
) {
183 if (!qemu_has_vnet_hdr_len(options
->net_backend
,
184 sizeof(struct virtio_net_hdr_mrg_rxbuf
))) {
185 net
->dev
.features
&= ~(1ULL << VIRTIO_NET_F_MRG_RXBUF
);
187 if (~net
->dev
.features
& net
->dev
.backend_features
) {
188 fprintf(stderr
, "vhost lacks feature mask %" PRIu64
190 (uint64_t)(~net
->dev
.features
& net
->dev
.backend_features
));
191 vhost_dev_cleanup(&net
->dev
);
195 /* Set sane init value. Override when guest acks. */
196 vhost_net_ack_features(net
, 0);
203 static void vhost_net_set_vq_index(struct vhost_net
*net
, int vq_index
)
205 net
->dev
.vq_index
= vq_index
;
208 static int vhost_net_set_vnet_endian(VirtIODevice
*dev
, NetClientState
*peer
,
213 if (virtio_vdev_has_feature(dev
, VIRTIO_F_VERSION_1
) ||
214 (virtio_legacy_is_cross_endian(dev
) && !virtio_is_big_endian(dev
))) {
215 r
= qemu_set_vnet_le(peer
, set
);
217 error_report("backend does not support LE vnet headers");
219 } else if (virtio_legacy_is_cross_endian(dev
)) {
220 r
= qemu_set_vnet_be(peer
, set
);
222 error_report("backend does not support BE vnet headers");
229 static int vhost_net_start_one(struct vhost_net
*net
,
232 struct vhost_vring_file file
= { };
236 net
->dev
.vqs
= net
->vqs
;
238 r
= vhost_dev_enable_notifiers(&net
->dev
, dev
);
243 r
= vhost_dev_start(&net
->dev
, dev
);
248 if (net
->nc
->info
->poll
) {
249 net
->nc
->info
->poll(net
->nc
, false);
252 if (net
->nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
) {
253 qemu_set_fd_handler(net
->backend
, NULL
, NULL
, NULL
);
254 file
.fd
= net
->backend
;
255 for (file
.index
= 0; file
.index
< net
->dev
.nvqs
; ++file
.index
) {
256 const VhostOps
*vhost_ops
= net
->dev
.vhost_ops
;
257 r
= vhost_ops
->vhost_net_set_backend(&net
->dev
, &file
);
267 if (net
->nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
) {
268 while (file
.index
-- > 0) {
269 const VhostOps
*vhost_ops
= net
->dev
.vhost_ops
;
270 int r
= vhost_ops
->vhost_net_set_backend(&net
->dev
, &file
);
274 if (net
->nc
->info
->poll
) {
275 net
->nc
->info
->poll(net
->nc
, true);
277 vhost_dev_stop(&net
->dev
, dev
);
279 vhost_dev_disable_notifiers(&net
->dev
, dev
);
284 static void vhost_net_stop_one(struct vhost_net
*net
,
287 struct vhost_vring_file file
= { .fd
= -1 };
289 if (net
->nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
) {
290 for (file
.index
= 0; file
.index
< net
->dev
.nvqs
; ++file
.index
) {
291 const VhostOps
*vhost_ops
= net
->dev
.vhost_ops
;
292 int r
= vhost_ops
->vhost_net_set_backend(&net
->dev
, &file
);
295 } else if (net
->nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_VHOST_USER
) {
296 for (file
.index
= 0; file
.index
< net
->dev
.nvqs
; ++file
.index
) {
297 const VhostOps
*vhost_ops
= net
->dev
.vhost_ops
;
298 int r
= vhost_ops
->vhost_reset_device(&net
->dev
);
302 if (net
->nc
->info
->poll
) {
303 net
->nc
->info
->poll(net
->nc
, true);
305 vhost_dev_stop(&net
->dev
, dev
);
306 vhost_dev_disable_notifiers(&net
->dev
, dev
);
309 int vhost_net_start(VirtIODevice
*dev
, NetClientState
*ncs
,
312 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(dev
)));
313 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
314 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(vbus
);
317 if (!k
->set_guest_notifiers
) {
318 error_report("binding does not support guest notifiers");
323 r
= vhost_net_set_vnet_endian(dev
, ncs
[0].peer
, true);
328 for (i
= 0; i
< total_queues
; i
++) {
329 vhost_net_set_vq_index(get_vhost_net(ncs
[i
].peer
), i
* 2);
332 r
= k
->set_guest_notifiers(qbus
->parent
, total_queues
* 2, true);
334 error_report("Error binding guest notifier: %d", -r
);
338 for (i
= 0; i
< total_queues
; i
++) {
339 r
= vhost_net_start_one(get_vhost_net(ncs
[i
].peer
), dev
);
350 vhost_net_stop_one(get_vhost_net(ncs
[i
].peer
), dev
);
352 e
= k
->set_guest_notifiers(qbus
->parent
, total_queues
* 2, false);
354 fprintf(stderr
, "vhost guest notifier cleanup failed: %d\n", e
);
358 vhost_net_set_vnet_endian(dev
, ncs
[0].peer
, false);
363 void vhost_net_stop(VirtIODevice
*dev
, NetClientState
*ncs
,
366 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(dev
)));
367 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
368 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(vbus
);
371 for (i
= 0; i
< total_queues
; i
++) {
372 vhost_net_stop_one(get_vhost_net(ncs
[i
].peer
), dev
);
375 r
= k
->set_guest_notifiers(qbus
->parent
, total_queues
* 2, false);
377 fprintf(stderr
, "vhost guest notifier cleanup failed: %d\n", r
);
382 assert(vhost_net_set_vnet_endian(dev
, ncs
[0].peer
, false) >= 0);
385 void vhost_net_cleanup(struct vhost_net
*net
)
387 vhost_dev_cleanup(&net
->dev
);
391 int vhost_net_notify_migration_done(struct vhost_net
*net
, char* mac_addr
)
393 const VhostOps
*vhost_ops
= net
->dev
.vhost_ops
;
396 if (vhost_ops
->vhost_migration_done
) {
397 r
= vhost_ops
->vhost_migration_done(&net
->dev
, mac_addr
);
403 bool vhost_net_virtqueue_pending(VHostNetState
*net
, int idx
)
405 return vhost_virtqueue_pending(&net
->dev
, idx
);
408 void vhost_net_virtqueue_mask(VHostNetState
*net
, VirtIODevice
*dev
,
411 vhost_virtqueue_mask(&net
->dev
, dev
, idx
, mask
);
414 VHostNetState
*get_vhost_net(NetClientState
*nc
)
416 VHostNetState
*vhost_net
= 0;
422 switch (nc
->info
->type
) {
423 case NET_CLIENT_OPTIONS_KIND_TAP
:
424 vhost_net
= tap_get_vhost_net(nc
);
426 case NET_CLIENT_OPTIONS_KIND_VHOST_USER
:
427 vhost_net
= vhost_user_get_vhost_net(nc
);
436 int vhost_set_vring_enable(NetClientState
*nc
, int enable
)
438 VHostNetState
*net
= get_vhost_net(nc
);
439 const VhostOps
*vhost_ops
= net
->dev
.vhost_ops
;
441 if (vhost_ops
->vhost_set_vring_enable
) {
442 return vhost_ops
->vhost_set_vring_enable(&net
->dev
, enable
);
449 uint64_t vhost_net_get_max_queues(VHostNetState
*net
)
454 struct vhost_net
*vhost_net_init(VhostNetOptions
*options
)
456 error_report("vhost-net support is not compiled in");
460 int vhost_net_start(VirtIODevice
*dev
,
466 void vhost_net_stop(VirtIODevice
*dev
,
472 void vhost_net_cleanup(struct vhost_net
*net
)
476 uint64_t vhost_net_get_features(struct vhost_net
*net
, uint64_t features
)
480 void vhost_net_ack_features(struct vhost_net
*net
, uint64_t features
)
484 bool vhost_net_virtqueue_pending(VHostNetState
*net
, int idx
)
489 void vhost_net_virtqueue_mask(VHostNetState
*net
, VirtIODevice
*dev
,
494 int vhost_net_notify_migration_done(struct vhost_net
*net
, char* mac_addr
)
499 VHostNetState
*get_vhost_net(NetClientState
*nc
)
504 int vhost_set_vring_enable(NetClientState
*nc
, int enable
)