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
,
90 VHOST_INVALID_FEATURE_BIT
93 static const int *vhost_net_get_feature_bits(struct vhost_net
*net
)
95 const int *feature_bits
= 0;
97 switch (net
->nc
->info
->type
) {
98 case NET_CLIENT_OPTIONS_KIND_TAP
:
99 feature_bits
= kernel_feature_bits
;
101 case NET_CLIENT_OPTIONS_KIND_VHOST_USER
:
102 feature_bits
= user_feature_bits
;
105 error_report("Feature bits not defined for this type: %d",
106 net
->nc
->info
->type
);
113 uint64_t vhost_net_get_features(struct vhost_net
*net
, uint64_t features
)
115 return vhost_get_features(&net
->dev
, vhost_net_get_feature_bits(net
),
119 void vhost_net_ack_features(struct vhost_net
*net
, uint64_t features
)
121 net
->dev
.acked_features
= net
->dev
.backend_features
;
122 vhost_ack_features(&net
->dev
, vhost_net_get_feature_bits(net
), features
);
125 static int vhost_net_get_fd(NetClientState
*backend
)
127 switch (backend
->info
->type
) {
128 case NET_CLIENT_OPTIONS_KIND_TAP
:
129 return tap_get_fd(backend
);
131 fprintf(stderr
, "vhost-net requires tap backend\n");
136 struct vhost_net
*vhost_net_init(VhostNetOptions
*options
)
139 bool backend_kernel
= options
->backend_type
== VHOST_BACKEND_TYPE_KERNEL
;
140 struct vhost_net
*net
= g_malloc(sizeof *net
);
142 if (!options
->net_backend
) {
143 fprintf(stderr
, "vhost-net requires net backend to be setup\n");
147 if (backend_kernel
) {
148 r
= vhost_net_get_fd(options
->net_backend
);
152 net
->dev
.backend_features
= qemu_has_vnet_hdr(options
->net_backend
)
153 ? 0 : (1ULL << VHOST_NET_F_VIRTIO_NET_HDR
);
156 net
->dev
.backend_features
= 0;
159 net
->nc
= options
->net_backend
;
162 net
->dev
.vqs
= net
->vqs
;
164 r
= vhost_dev_init(&net
->dev
, options
->opaque
,
165 options
->backend_type
);
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 static void vhost_net_set_vq_index(struct vhost_net
*net
, int vq_index
)
192 net
->dev
.vq_index
= vq_index
;
195 static int vhost_net_set_vnet_endian(VirtIODevice
*dev
, NetClientState
*peer
,
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
);
204 error_report("backend does not support LE vnet headers");
206 } else if (virtio_legacy_is_cross_endian(dev
)) {
207 r
= qemu_set_vnet_be(peer
, set
);
209 error_report("backend does not support BE vnet headers");
216 static int vhost_net_start_one(struct vhost_net
*net
,
219 struct vhost_vring_file file
= { };
223 net
->dev
.vqs
= net
->vqs
;
225 r
= vhost_dev_enable_notifiers(&net
->dev
, dev
);
230 r
= vhost_dev_start(&net
->dev
, dev
);
235 if (net
->nc
->info
->poll
) {
236 net
->nc
->info
->poll(net
->nc
, false);
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
,
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
,
263 if (net
->nc
->info
->poll
) {
264 net
->nc
->info
->poll(net
->nc
, true);
266 vhost_dev_stop(&net
->dev
, dev
);
268 vhost_dev_disable_notifiers(&net
->dev
, dev
);
273 static void vhost_net_stop_one(struct vhost_net
*net
,
276 struct vhost_vring_file file
= { .fd
= -1 };
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
,
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
,
293 if (net
->nc
->info
->poll
) {
294 net
->nc
->info
->poll(net
->nc
, true);
296 vhost_dev_stop(&net
->dev
, dev
);
297 vhost_dev_disable_notifiers(&net
->dev
, dev
);
300 int vhost_net_start(VirtIODevice
*dev
, NetClientState
*ncs
,
303 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(dev
)));
304 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
305 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(vbus
);
308 if (!k
->set_guest_notifiers
) {
309 error_report("binding does not support guest notifiers");
314 r
= vhost_net_set_vnet_endian(dev
, ncs
[0].peer
, true);
319 for (i
= 0; i
< total_queues
; i
++) {
320 vhost_net_set_vq_index(get_vhost_net(ncs
[i
].peer
), i
* 2);
323 r
= k
->set_guest_notifiers(qbus
->parent
, total_queues
* 2, true);
325 error_report("Error binding guest notifier: %d", -r
);
329 for (i
= 0; i
< total_queues
; i
++) {
330 r
= vhost_net_start_one(get_vhost_net(ncs
[i
].peer
), dev
);
341 vhost_net_stop_one(get_vhost_net(ncs
[i
].peer
), dev
);
343 e
= k
->set_guest_notifiers(qbus
->parent
, total_queues
* 2, false);
345 fprintf(stderr
, "vhost guest notifier cleanup failed: %d\n", e
);
349 vhost_net_set_vnet_endian(dev
, ncs
[0].peer
, false);
354 void vhost_net_stop(VirtIODevice
*dev
, NetClientState
*ncs
,
357 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(dev
)));
358 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
359 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(vbus
);
362 for (i
= 0; i
< total_queues
; i
++) {
363 vhost_net_stop_one(get_vhost_net(ncs
[i
].peer
), dev
);
366 r
= k
->set_guest_notifiers(qbus
->parent
, total_queues
* 2, false);
368 fprintf(stderr
, "vhost guest notifier cleanup failed: %d\n", r
);
373 assert(vhost_net_set_vnet_endian(dev
, ncs
[0].peer
, false) >= 0);
376 void vhost_net_cleanup(struct vhost_net
*net
)
378 vhost_dev_cleanup(&net
->dev
);
382 bool vhost_net_virtqueue_pending(VHostNetState
*net
, int idx
)
384 return vhost_virtqueue_pending(&net
->dev
, idx
);
387 void vhost_net_virtqueue_mask(VHostNetState
*net
, VirtIODevice
*dev
,
390 vhost_virtqueue_mask(&net
->dev
, dev
, idx
, mask
);
393 VHostNetState
*get_vhost_net(NetClientState
*nc
)
395 VHostNetState
*vhost_net
= 0;
401 switch (nc
->info
->type
) {
402 case NET_CLIENT_OPTIONS_KIND_TAP
:
403 vhost_net
= tap_get_vhost_net(nc
);
405 case NET_CLIENT_OPTIONS_KIND_VHOST_USER
:
406 vhost_net
= vhost_user_get_vhost_net(nc
);
415 struct vhost_net
*vhost_net_init(VhostNetOptions
*options
)
417 error_report("vhost-net support is not compiled in");
421 int vhost_net_start(VirtIODevice
*dev
,
427 void vhost_net_stop(VirtIODevice
*dev
,
433 void vhost_net_cleanup(struct vhost_net
*net
)
437 uint64_t vhost_net_get_features(struct vhost_net
*net
, uint64_t features
)
441 void vhost_net_ack_features(struct vhost_net
*net
, uint64_t features
)
445 bool vhost_net_virtqueue_pending(VHostNetState
*net
, int idx
)
450 void vhost_net_virtqueue_mask(VHostNetState
*net
, VirtIODevice
*dev
,
455 VHostNetState
*get_vhost_net(NetClientState
*nc
)