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"
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
,
55 VHOST_INVALID_FEATURE_BIT
58 /* Features supported by others. */
59 static const int user_feature_bits
[] = {
60 VIRTIO_F_NOTIFY_ON_EMPTY
,
61 VIRTIO_RING_F_INDIRECT_DESC
,
62 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
,
80 VIRTIO_NET_F_CTRL_VLAN
,
81 VIRTIO_NET_F_CTRL_RX_EXTRA
,
82 VIRTIO_NET_F_CTRL_MAC_ADDR
,
83 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
,
87 VHOST_INVALID_FEATURE_BIT
90 static const int *vhost_net_get_feature_bits(struct vhost_net
*net
)
92 const int *feature_bits
= 0;
94 switch (net
->nc
->info
->type
) {
95 case NET_CLIENT_OPTIONS_KIND_TAP
:
96 feature_bits
= kernel_feature_bits
;
98 case NET_CLIENT_OPTIONS_KIND_VHOST_USER
:
99 feature_bits
= user_feature_bits
;
102 error_report("Feature bits not defined for this type: %d",
103 net
->nc
->info
->type
);
110 unsigned vhost_net_get_features(struct vhost_net
*net
, unsigned features
)
112 return vhost_get_features(&net
->dev
, vhost_net_get_feature_bits(net
),
116 void vhost_net_ack_features(struct vhost_net
*net
, unsigned features
)
118 net
->dev
.acked_features
= net
->dev
.backend_features
;
119 vhost_ack_features(&net
->dev
, vhost_net_get_feature_bits(net
), features
);
122 static int vhost_net_get_fd(NetClientState
*backend
)
124 switch (backend
->info
->type
) {
125 case NET_CLIENT_OPTIONS_KIND_TAP
:
126 return tap_get_fd(backend
);
128 fprintf(stderr
, "vhost-net requires tap backend\n");
133 struct vhost_net
*vhost_net_init(VhostNetOptions
*options
)
136 bool backend_kernel
= options
->backend_type
== VHOST_BACKEND_TYPE_KERNEL
;
137 struct vhost_net
*net
= g_malloc(sizeof *net
);
139 if (!options
->net_backend
) {
140 fprintf(stderr
, "vhost-net requires net backend to be setup\n");
144 if (backend_kernel
) {
145 r
= vhost_net_get_fd(options
->net_backend
);
149 net
->dev
.backend_features
= qemu_has_vnet_hdr(options
->net_backend
)
150 ? 0 : (1 << VHOST_NET_F_VIRTIO_NET_HDR
);
153 net
->dev
.backend_features
= 0;
156 net
->nc
= options
->net_backend
;
159 net
->dev
.vqs
= net
->vqs
;
160 net
->dev
.vq_index
= net
->nc
->queue_index
;
162 r
= vhost_dev_init(&net
->dev
, options
->opaque
,
163 options
->backend_type
, options
->force
);
167 if (backend_kernel
) {
168 if (!qemu_has_vnet_hdr_len(options
->net_backend
,
169 sizeof(struct virtio_net_hdr_mrg_rxbuf
))) {
170 net
->dev
.features
&= ~(1 << VIRTIO_NET_F_MRG_RXBUF
);
172 if (~net
->dev
.features
& net
->dev
.backend_features
) {
173 fprintf(stderr
, "vhost lacks feature mask %" PRIu64
175 (uint64_t)(~net
->dev
.features
& net
->dev
.backend_features
));
176 vhost_dev_cleanup(&net
->dev
);
180 /* Set sane init value. Override when guest acks. */
181 vhost_net_ack_features(net
, 0);
188 bool vhost_net_query(VHostNetState
*net
, VirtIODevice
*dev
)
190 return vhost_dev_query(&net
->dev
, dev
);
193 static void vhost_net_set_vq_index(struct vhost_net
*net
, int vq_index
)
195 net
->dev
.vq_index
= vq_index
;
198 static int vhost_net_start_one(struct vhost_net
*net
,
201 struct vhost_vring_file file
= { };
205 net
->dev
.vqs
= net
->vqs
;
207 r
= vhost_dev_enable_notifiers(&net
->dev
, dev
);
212 r
= vhost_dev_start(&net
->dev
, dev
);
217 if (net
->nc
->info
->poll
) {
218 net
->nc
->info
->poll(net
->nc
, false);
221 if (net
->nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
) {
222 qemu_set_fd_handler(net
->backend
, NULL
, NULL
, NULL
);
223 file
.fd
= net
->backend
;
224 for (file
.index
= 0; file
.index
< net
->dev
.nvqs
; ++file
.index
) {
225 const VhostOps
*vhost_ops
= net
->dev
.vhost_ops
;
226 r
= vhost_ops
->vhost_call(&net
->dev
, VHOST_NET_SET_BACKEND
,
237 if (net
->nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
) {
238 while (file
.index
-- > 0) {
239 const VhostOps
*vhost_ops
= net
->dev
.vhost_ops
;
240 int r
= vhost_ops
->vhost_call(&net
->dev
, VHOST_NET_SET_BACKEND
,
245 if (net
->nc
->info
->poll
) {
246 net
->nc
->info
->poll(net
->nc
, true);
248 vhost_dev_stop(&net
->dev
, dev
);
250 vhost_dev_disable_notifiers(&net
->dev
, dev
);
255 static void vhost_net_stop_one(struct vhost_net
*net
,
258 struct vhost_vring_file file
= { .fd
= -1 };
260 if (net
->nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
) {
261 for (file
.index
= 0; file
.index
< net
->dev
.nvqs
; ++file
.index
) {
262 const VhostOps
*vhost_ops
= net
->dev
.vhost_ops
;
263 int r
= vhost_ops
->vhost_call(&net
->dev
, VHOST_NET_SET_BACKEND
,
267 } else if (net
->nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_VHOST_USER
) {
268 for (file
.index
= 0; file
.index
< net
->dev
.nvqs
; ++file
.index
) {
269 const VhostOps
*vhost_ops
= net
->dev
.vhost_ops
;
270 int r
= vhost_ops
->vhost_call(&net
->dev
, VHOST_RESET_OWNER
,
275 if (net
->nc
->info
->poll
) {
276 net
->nc
->info
->poll(net
->nc
, true);
278 vhost_dev_stop(&net
->dev
, dev
);
279 vhost_dev_disable_notifiers(&net
->dev
, dev
);
282 static bool vhost_net_device_endian_ok(VirtIODevice
*vdev
)
284 #ifdef TARGET_IS_BIENDIAN
285 #ifdef HOST_WORDS_BIGENDIAN
286 return virtio_is_big_endian(vdev
);
288 return !virtio_is_big_endian(vdev
);
295 int vhost_net_start(VirtIODevice
*dev
, NetClientState
*ncs
,
298 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(dev
)));
299 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
300 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(vbus
);
303 if (!vhost_net_device_endian_ok(dev
)) {
304 error_report("vhost-net does not support cross-endian");
309 if (!k
->set_guest_notifiers
) {
310 error_report("binding does not support guest notifiers");
315 for (i
= 0; i
< total_queues
; i
++) {
316 vhost_net_set_vq_index(get_vhost_net(ncs
[i
].peer
), i
* 2);
319 r
= k
->set_guest_notifiers(qbus
->parent
, total_queues
* 2, true);
321 error_report("Error binding guest notifier: %d", -r
);
325 for (i
= 0; i
< total_queues
; i
++) {
326 r
= vhost_net_start_one(get_vhost_net(ncs
[i
].peer
), dev
);
337 vhost_net_stop_one(get_vhost_net(ncs
[i
].peer
), dev
);
339 e
= k
->set_guest_notifiers(qbus
->parent
, total_queues
* 2, false);
341 fprintf(stderr
, "vhost guest notifier cleanup failed: %d\n", e
);
348 void vhost_net_stop(VirtIODevice
*dev
, NetClientState
*ncs
,
351 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(dev
)));
352 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
353 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(vbus
);
356 for (i
= 0; i
< total_queues
; i
++) {
357 vhost_net_stop_one(get_vhost_net(ncs
[i
].peer
), dev
);
360 r
= k
->set_guest_notifiers(qbus
->parent
, total_queues
* 2, false);
362 fprintf(stderr
, "vhost guest notifier cleanup failed: %d\n", r
);
368 void vhost_net_cleanup(struct vhost_net
*net
)
370 vhost_dev_cleanup(&net
->dev
);
374 bool vhost_net_virtqueue_pending(VHostNetState
*net
, int idx
)
376 return vhost_virtqueue_pending(&net
->dev
, idx
);
379 void vhost_net_virtqueue_mask(VHostNetState
*net
, VirtIODevice
*dev
,
382 vhost_virtqueue_mask(&net
->dev
, dev
, idx
, mask
);
385 VHostNetState
*get_vhost_net(NetClientState
*nc
)
387 VHostNetState
*vhost_net
= 0;
393 switch (nc
->info
->type
) {
394 case NET_CLIENT_OPTIONS_KIND_TAP
:
395 vhost_net
= tap_get_vhost_net(nc
);
397 case NET_CLIENT_OPTIONS_KIND_VHOST_USER
:
398 vhost_net
= vhost_user_get_vhost_net(nc
);
407 struct vhost_net
*vhost_net_init(VhostNetOptions
*options
)
409 error_report("vhost-net support is not compiled in");
413 bool vhost_net_query(VHostNetState
*net
, VirtIODevice
*dev
)
418 int vhost_net_start(VirtIODevice
*dev
,
424 void vhost_net_stop(VirtIODevice
*dev
,
430 void vhost_net_cleanup(struct vhost_net
*net
)
434 unsigned vhost_net_get_features(struct vhost_net
*net
, unsigned features
)
438 void vhost_net_ack_features(struct vhost_net
*net
, unsigned features
)
442 bool vhost_net_virtqueue_pending(VHostNetState
*net
, int idx
)
447 void vhost_net_virtqueue_mask(VHostNetState
*net
, VirtIODevice
*dev
,
452 VHostNetState
*get_vhost_net(NetClientState
*nc
)