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.
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>
29 #include <netinet/in.h>
32 #include "standard-headers/linux/virtio_ring.h"
33 #include "hw/virtio/vhost.h"
34 #include "hw/virtio/virtio-bus.h"
38 struct vhost_virtqueue vqs
[2];
43 /* Features supported by host kernel. */
44 static const int kernel_feature_bits
[] = {
45 VIRTIO_F_NOTIFY_ON_EMPTY
,
46 VIRTIO_RING_F_INDIRECT_DESC
,
47 VIRTIO_RING_F_EVENT_IDX
,
48 VIRTIO_NET_F_MRG_RXBUF
,
51 VIRTIO_F_IOMMU_PLATFORM
,
52 VHOST_INVALID_FEATURE_BIT
55 /* Features supported by others. */
56 static const int user_feature_bits
[] = {
57 VIRTIO_F_NOTIFY_ON_EMPTY
,
58 VIRTIO_RING_F_INDIRECT_DESC
,
59 VIRTIO_RING_F_EVENT_IDX
,
64 VIRTIO_NET_F_GUEST_CSUM
,
66 VIRTIO_NET_F_GUEST_TSO4
,
67 VIRTIO_NET_F_GUEST_TSO6
,
68 VIRTIO_NET_F_GUEST_ECN
,
69 VIRTIO_NET_F_GUEST_UFO
,
70 VIRTIO_NET_F_HOST_TSO4
,
71 VIRTIO_NET_F_HOST_TSO6
,
72 VIRTIO_NET_F_HOST_ECN
,
73 VIRTIO_NET_F_HOST_UFO
,
74 VIRTIO_NET_F_MRG_RXBUF
,
76 VIRTIO_F_IOMMU_PLATFORM
,
78 /* This bit implies RARP isn't sent by QEMU out of band */
79 VIRTIO_NET_F_GUEST_ANNOUNCE
,
83 VHOST_INVALID_FEATURE_BIT
86 static const int *vhost_net_get_feature_bits(struct vhost_net
*net
)
88 const int *feature_bits
= 0;
90 switch (net
->nc
->info
->type
) {
91 case NET_CLIENT_DRIVER_TAP
:
92 feature_bits
= kernel_feature_bits
;
94 case NET_CLIENT_DRIVER_VHOST_USER
:
95 feature_bits
= user_feature_bits
;
98 error_report("Feature bits not defined for this type: %d",
106 uint64_t vhost_net_get_features(struct vhost_net
*net
, uint64_t features
)
108 return vhost_get_features(&net
->dev
, vhost_net_get_feature_bits(net
),
112 void vhost_net_ack_features(struct vhost_net
*net
, uint64_t features
)
114 net
->dev
.acked_features
= net
->dev
.backend_features
;
115 vhost_ack_features(&net
->dev
, vhost_net_get_feature_bits(net
), features
);
118 uint64_t vhost_net_get_max_queues(VHostNetState
*net
)
120 return net
->dev
.max_queues
;
123 uint64_t vhost_net_get_acked_features(VHostNetState
*net
)
125 return net
->dev
.acked_features
;
128 static int vhost_net_get_fd(NetClientState
*backend
)
130 switch (backend
->info
->type
) {
131 case NET_CLIENT_DRIVER_TAP
:
132 return tap_get_fd(backend
);
134 fprintf(stderr
, "vhost-net requires tap backend\n");
139 struct vhost_net
*vhost_net_init(VhostNetOptions
*options
)
142 bool backend_kernel
= options
->backend_type
== VHOST_BACKEND_TYPE_KERNEL
;
143 struct vhost_net
*net
= g_new0(struct vhost_net
, 1);
144 uint64_t features
= 0;
146 if (!options
->net_backend
) {
147 fprintf(stderr
, "vhost-net requires net backend to be setup\n");
150 net
->nc
= options
->net_backend
;
152 net
->dev
.max_queues
= 1;
154 net
->dev
.vqs
= net
->vqs
;
156 if (backend_kernel
) {
157 r
= vhost_net_get_fd(options
->net_backend
);
161 net
->dev
.backend_features
= qemu_has_vnet_hdr(options
->net_backend
)
162 ? 0 : (1ULL << VHOST_NET_F_VIRTIO_NET_HDR
);
164 net
->dev
.protocol_features
= 0;
166 net
->dev
.backend_features
= 0;
167 net
->dev
.protocol_features
= 0;
170 /* vhost-user needs vq_index to initiate a specific queue pair */
171 net
->dev
.vq_index
= net
->nc
->queue_index
* net
->dev
.nvqs
;
174 r
= vhost_dev_init(&net
->dev
, options
->opaque
,
175 options
->backend_type
, options
->busyloop_timeout
);
179 if (backend_kernel
) {
180 if (!qemu_has_vnet_hdr_len(options
->net_backend
,
181 sizeof(struct virtio_net_hdr_mrg_rxbuf
))) {
182 net
->dev
.features
&= ~(1ULL << VIRTIO_NET_F_MRG_RXBUF
);
184 if (~net
->dev
.features
& net
->dev
.backend_features
) {
185 fprintf(stderr
, "vhost lacks feature mask %" PRIu64
187 (uint64_t)(~net
->dev
.features
& net
->dev
.backend_features
));
192 /* Set sane init value. Override when guest acks. */
193 #ifdef CONFIG_VHOST_NET_USER
194 if (net
->nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_USER
) {
195 features
= vhost_user_get_acked_features(net
->nc
);
196 if (~net
->dev
.features
& features
) {
197 fprintf(stderr
, "vhost lacks feature mask %" PRIu64
199 (uint64_t)(~net
->dev
.features
& features
));
205 vhost_net_ack_features(net
, features
);
210 vhost_dev_cleanup(&net
->dev
);
215 static void vhost_net_set_vq_index(struct vhost_net
*net
, int vq_index
)
217 net
->dev
.vq_index
= vq_index
;
220 static int vhost_net_start_one(struct vhost_net
*net
,
223 struct vhost_vring_file file
= { };
227 net
->dev
.vqs
= net
->vqs
;
229 r
= vhost_dev_enable_notifiers(&net
->dev
, dev
);
234 r
= vhost_dev_start(&net
->dev
, dev
);
239 if (net
->nc
->info
->poll
) {
240 net
->nc
->info
->poll(net
->nc
, false);
243 if (net
->nc
->info
->type
== NET_CLIENT_DRIVER_TAP
) {
244 qemu_set_fd_handler(net
->backend
, NULL
, NULL
, NULL
);
245 file
.fd
= net
->backend
;
246 for (file
.index
= 0; file
.index
< net
->dev
.nvqs
; ++file
.index
) {
247 if (!virtio_queue_enabled(dev
, net
->dev
.vq_index
+
249 /* Queue might not be ready for start */
252 r
= vhost_net_set_backend(&net
->dev
, &file
);
262 if (net
->nc
->info
->type
== NET_CLIENT_DRIVER_TAP
) {
263 while (file
.index
-- > 0) {
264 if (!virtio_queue_enabled(dev
, net
->dev
.vq_index
+
266 /* Queue might not be ready for start */
269 int r
= vhost_net_set_backend(&net
->dev
, &file
);
273 if (net
->nc
->info
->poll
) {
274 net
->nc
->info
->poll(net
->nc
, true);
276 vhost_dev_stop(&net
->dev
, dev
);
278 vhost_dev_disable_notifiers(&net
->dev
, dev
);
283 static void vhost_net_stop_one(struct vhost_net
*net
,
286 struct vhost_vring_file file
= { .fd
= -1 };
288 if (net
->nc
->info
->type
== NET_CLIENT_DRIVER_TAP
) {
289 for (file
.index
= 0; file
.index
< net
->dev
.nvqs
; ++file
.index
) {
290 int r
= vhost_net_set_backend(&net
->dev
, &file
);
294 if (net
->nc
->info
->poll
) {
295 net
->nc
->info
->poll(net
->nc
, true);
297 vhost_dev_stop(&net
->dev
, dev
);
298 vhost_dev_disable_notifiers(&net
->dev
, dev
);
301 int vhost_net_start(VirtIODevice
*dev
, NetClientState
*ncs
,
304 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(dev
)));
305 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
306 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(vbus
);
309 if (!k
->set_guest_notifiers
) {
310 error_report("binding does not support guest notifiers");
314 for (i
= 0; i
< total_queues
; i
++) {
315 struct vhost_net
*net
;
317 net
= get_vhost_net(ncs
[i
].peer
);
318 vhost_net_set_vq_index(net
, i
* 2);
320 /* Suppress the masking guest notifiers on vhost user
321 * because vhost user doesn't interrupt masking/unmasking
324 if (net
->nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_USER
) {
325 dev
->use_guest_notifier_mask
= false;
329 r
= k
->set_guest_notifiers(qbus
->parent
, total_queues
* 2, true);
331 error_report("Error binding guest notifier: %d", -r
);
335 for (i
= 0; i
< total_queues
; i
++) {
336 r
= vhost_net_start_one(get_vhost_net(ncs
[i
].peer
), dev
);
342 if (ncs
[i
].peer
->vring_enable
) {
343 /* restore vring enable state */
344 r
= vhost_set_vring_enable(ncs
[i
].peer
, ncs
[i
].peer
->vring_enable
);
356 vhost_net_stop_one(get_vhost_net(ncs
[i
].peer
), dev
);
358 e
= k
->set_guest_notifiers(qbus
->parent
, total_queues
* 2, false);
360 fprintf(stderr
, "vhost guest notifier cleanup failed: %d\n", e
);
367 void vhost_net_stop(VirtIODevice
*dev
, NetClientState
*ncs
,
370 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(dev
)));
371 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
372 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(vbus
);
375 for (i
= 0; i
< total_queues
; i
++) {
376 vhost_net_stop_one(get_vhost_net(ncs
[i
].peer
), dev
);
379 r
= k
->set_guest_notifiers(qbus
->parent
, total_queues
* 2, false);
381 fprintf(stderr
, "vhost guest notifier cleanup failed: %d\n", r
);
387 void vhost_net_cleanup(struct vhost_net
*net
)
389 vhost_dev_cleanup(&net
->dev
);
392 int vhost_net_notify_migration_done(struct vhost_net
*net
, char* mac_addr
)
394 const VhostOps
*vhost_ops
= net
->dev
.vhost_ops
;
396 assert(vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
397 assert(vhost_ops
->vhost_migration_done
);
399 return vhost_ops
->vhost_migration_done(&net
->dev
, mac_addr
);
402 bool vhost_net_virtqueue_pending(VHostNetState
*net
, int idx
)
404 return vhost_virtqueue_pending(&net
->dev
, idx
);
407 void vhost_net_virtqueue_mask(VHostNetState
*net
, VirtIODevice
*dev
,
410 vhost_virtqueue_mask(&net
->dev
, dev
, idx
, mask
);
413 VHostNetState
*get_vhost_net(NetClientState
*nc
)
415 VHostNetState
*vhost_net
= 0;
421 switch (nc
->info
->type
) {
422 case NET_CLIENT_DRIVER_TAP
:
423 vhost_net
= tap_get_vhost_net(nc
);
425 #ifdef CONFIG_VHOST_NET_USER
426 case NET_CLIENT_DRIVER_VHOST_USER
:
427 vhost_net
= vhost_user_get_vhost_net(nc
);
438 int vhost_set_vring_enable(NetClientState
*nc
, int enable
)
440 VHostNetState
*net
= get_vhost_net(nc
);
441 const VhostOps
*vhost_ops
= net
->dev
.vhost_ops
;
443 nc
->vring_enable
= enable
;
445 if (vhost_ops
&& vhost_ops
->vhost_set_vring_enable
) {
446 return vhost_ops
->vhost_set_vring_enable(&net
->dev
, enable
);
452 int vhost_net_set_mtu(struct vhost_net
*net
, uint16_t mtu
)
454 const VhostOps
*vhost_ops
= net
->dev
.vhost_ops
;
456 if (!vhost_ops
->vhost_net_set_mtu
) {
460 return vhost_ops
->vhost_net_set_mtu(&net
->dev
, mtu
);