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"
20 #include "net/vhost-vdpa.h"
22 #include "standard-headers/linux/vhost_types.h"
23 #include "hw/virtio/virtio-net.h"
24 #include "net/vhost_net.h"
25 #include "qemu/error-report.h"
26 #include "qemu/main-loop.h"
28 #include <sys/socket.h>
30 #include <netinet/in.h>
33 #include "standard-headers/linux/virtio_ring.h"
34 #include "hw/virtio/vhost.h"
35 #include "hw/virtio/virtio-bus.h"
38 /* Features supported by host kernel. */
39 static const int kernel_feature_bits
[] = {
40 VIRTIO_F_NOTIFY_ON_EMPTY
,
41 VIRTIO_RING_F_INDIRECT_DESC
,
42 VIRTIO_RING_F_EVENT_IDX
,
43 VIRTIO_NET_F_MRG_RXBUF
,
46 VIRTIO_F_IOMMU_PLATFORM
,
48 VHOST_INVALID_FEATURE_BIT
51 /* Features supported by others. */
52 static const int user_feature_bits
[] = {
53 VIRTIO_F_NOTIFY_ON_EMPTY
,
54 VIRTIO_RING_F_INDIRECT_DESC
,
55 VIRTIO_RING_F_EVENT_IDX
,
60 VIRTIO_NET_F_GUEST_CSUM
,
62 VIRTIO_NET_F_GUEST_TSO4
,
63 VIRTIO_NET_F_GUEST_TSO6
,
64 VIRTIO_NET_F_GUEST_ECN
,
65 VIRTIO_NET_F_GUEST_UFO
,
66 VIRTIO_NET_F_HOST_TSO4
,
67 VIRTIO_NET_F_HOST_TSO6
,
68 VIRTIO_NET_F_HOST_ECN
,
69 VIRTIO_NET_F_HOST_UFO
,
70 VIRTIO_NET_F_MRG_RXBUF
,
72 VIRTIO_F_IOMMU_PLATFORM
,
75 /* This bit implies RARP isn't sent by QEMU out of band */
76 VIRTIO_NET_F_GUEST_ANNOUNCE
,
80 VHOST_INVALID_FEATURE_BIT
83 static const int *vhost_net_get_feature_bits(struct vhost_net
*net
)
85 const int *feature_bits
= 0;
87 switch (net
->nc
->info
->type
) {
88 case NET_CLIENT_DRIVER_TAP
:
89 feature_bits
= kernel_feature_bits
;
91 case NET_CLIENT_DRIVER_VHOST_USER
:
92 feature_bits
= user_feature_bits
;
94 #ifdef CONFIG_VHOST_NET_VDPA
95 case NET_CLIENT_DRIVER_VHOST_VDPA
:
96 feature_bits
= vdpa_feature_bits
;
100 error_report("Feature bits not defined for this type: %d",
101 net
->nc
->info
->type
);
108 uint64_t vhost_net_get_features(struct vhost_net
*net
, uint64_t features
)
110 return vhost_get_features(&net
->dev
, vhost_net_get_feature_bits(net
),
113 int vhost_net_get_config(struct vhost_net
*net
, uint8_t *config
,
116 return vhost_dev_get_config(&net
->dev
, config
, config_len
);
118 int vhost_net_set_config(struct vhost_net
*net
, const uint8_t *data
,
119 uint32_t offset
, uint32_t size
, uint32_t flags
)
121 return vhost_dev_set_config(&net
->dev
, data
, offset
, size
, flags
);
124 void vhost_net_ack_features(struct vhost_net
*net
, uint64_t features
)
126 net
->dev
.acked_features
= net
->dev
.backend_features
;
127 vhost_ack_features(&net
->dev
, vhost_net_get_feature_bits(net
), features
);
130 uint64_t vhost_net_get_max_queues(VHostNetState
*net
)
132 return net
->dev
.max_queues
;
135 uint64_t vhost_net_get_acked_features(VHostNetState
*net
)
137 return net
->dev
.acked_features
;
140 static int vhost_net_get_fd(NetClientState
*backend
)
142 switch (backend
->info
->type
) {
143 case NET_CLIENT_DRIVER_TAP
:
144 return tap_get_fd(backend
);
146 fprintf(stderr
, "vhost-net requires tap backend\n");
151 struct vhost_net
*vhost_net_init(VhostNetOptions
*options
)
154 bool backend_kernel
= options
->backend_type
== VHOST_BACKEND_TYPE_KERNEL
;
155 struct vhost_net
*net
= g_new0(struct vhost_net
, 1);
156 uint64_t features
= 0;
158 if (!options
->net_backend
) {
159 fprintf(stderr
, "vhost-net requires net backend to be setup\n");
162 net
->nc
= options
->net_backend
;
164 net
->dev
.max_queues
= 1;
166 net
->dev
.vqs
= net
->vqs
;
168 if (backend_kernel
) {
169 r
= vhost_net_get_fd(options
->net_backend
);
173 net
->dev
.backend_features
= qemu_has_vnet_hdr(options
->net_backend
)
174 ? 0 : (1ULL << VHOST_NET_F_VIRTIO_NET_HDR
);
176 net
->dev
.protocol_features
= 0;
178 net
->dev
.backend_features
= 0;
179 net
->dev
.protocol_features
= 0;
182 /* vhost-user needs vq_index to initiate a specific queue pair */
183 net
->dev
.vq_index
= net
->nc
->queue_index
* net
->dev
.nvqs
;
186 r
= vhost_dev_init(&net
->dev
, options
->opaque
,
187 options
->backend_type
, options
->busyloop_timeout
);
191 if (backend_kernel
) {
192 if (!qemu_has_vnet_hdr_len(options
->net_backend
,
193 sizeof(struct virtio_net_hdr_mrg_rxbuf
))) {
194 net
->dev
.features
&= ~(1ULL << VIRTIO_NET_F_MRG_RXBUF
);
196 if (~net
->dev
.features
& net
->dev
.backend_features
) {
197 fprintf(stderr
, "vhost lacks feature mask %" PRIu64
199 (uint64_t)(~net
->dev
.features
& net
->dev
.backend_features
));
204 /* Set sane init value. Override when guest acks. */
205 #ifdef CONFIG_VHOST_NET_USER
206 if (net
->nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_USER
) {
207 features
= vhost_user_get_acked_features(net
->nc
);
208 if (~net
->dev
.features
& features
) {
209 fprintf(stderr
, "vhost lacks feature mask %" PRIu64
211 (uint64_t)(~net
->dev
.features
& features
));
217 vhost_net_ack_features(net
, features
);
222 vhost_dev_cleanup(&net
->dev
);
227 static void vhost_net_set_vq_index(struct vhost_net
*net
, int vq_index
)
229 net
->dev
.vq_index
= vq_index
;
232 static int vhost_net_start_one(struct vhost_net
*net
,
235 struct vhost_vring_file file
= { };
239 net
->dev
.vqs
= net
->vqs
;
241 r
= vhost_dev_enable_notifiers(&net
->dev
, dev
);
246 r
= vhost_dev_start(&net
->dev
, dev
);
251 if (net
->nc
->info
->poll
) {
252 net
->nc
->info
->poll(net
->nc
, false);
255 if (net
->nc
->info
->type
== NET_CLIENT_DRIVER_TAP
) {
256 qemu_set_fd_handler(net
->backend
, NULL
, NULL
, NULL
);
257 file
.fd
= net
->backend
;
258 for (file
.index
= 0; file
.index
< net
->dev
.nvqs
; ++file
.index
) {
259 if (!virtio_queue_enabled(dev
, net
->dev
.vq_index
+
261 /* Queue might not be ready for start */
264 r
= vhost_net_set_backend(&net
->dev
, &file
);
274 if (net
->nc
->info
->type
== NET_CLIENT_DRIVER_TAP
) {
275 while (file
.index
-- > 0) {
276 if (!virtio_queue_enabled(dev
, net
->dev
.vq_index
+
278 /* Queue might not be ready for start */
281 int r
= vhost_net_set_backend(&net
->dev
, &file
);
285 if (net
->nc
->info
->poll
) {
286 net
->nc
->info
->poll(net
->nc
, true);
288 vhost_dev_stop(&net
->dev
, dev
);
290 vhost_dev_disable_notifiers(&net
->dev
, dev
);
295 static void vhost_net_stop_one(struct vhost_net
*net
,
298 struct vhost_vring_file file
= { .fd
= -1 };
300 if (net
->nc
->info
->type
== NET_CLIENT_DRIVER_TAP
) {
301 for (file
.index
= 0; file
.index
< net
->dev
.nvqs
; ++file
.index
) {
302 int r
= vhost_net_set_backend(&net
->dev
, &file
);
306 if (net
->nc
->info
->poll
) {
307 net
->nc
->info
->poll(net
->nc
, true);
309 vhost_dev_stop(&net
->dev
, dev
);
310 vhost_dev_disable_notifiers(&net
->dev
, dev
);
313 int vhost_net_start(VirtIODevice
*dev
, NetClientState
*ncs
,
316 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(dev
)));
317 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
318 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(vbus
);
319 struct vhost_net
*net
;
321 NetClientState
*peer
;
323 if (!k
->set_guest_notifiers
) {
324 error_report("binding does not support guest notifiers");
328 for (i
= 0; i
< total_queues
; i
++) {
330 peer
= qemu_get_peer(ncs
, i
);
331 net
= get_vhost_net(peer
);
332 vhost_net_set_vq_index(net
, i
* 2);
334 /* Suppress the masking guest notifiers on vhost user
335 * because vhost user doesn't interrupt masking/unmasking
338 if (net
->nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_USER
) {
339 dev
->use_guest_notifier_mask
= false;
343 r
= k
->set_guest_notifiers(qbus
->parent
, total_queues
* 2, true);
345 error_report("Error binding guest notifier: %d", -r
);
349 for (i
= 0; i
< total_queues
; i
++) {
350 peer
= qemu_get_peer(ncs
, i
);
351 r
= vhost_net_start_one(get_vhost_net(peer
), dev
);
357 if (peer
->vring_enable
) {
358 /* restore vring enable state */
359 r
= vhost_set_vring_enable(peer
, peer
->vring_enable
);
371 peer
= qemu_get_peer(ncs
, i
);
372 vhost_net_stop_one(get_vhost_net(peer
), dev
);
374 e
= k
->set_guest_notifiers(qbus
->parent
, total_queues
* 2, false);
376 fprintf(stderr
, "vhost guest notifier cleanup failed: %d\n", e
);
383 void vhost_net_stop(VirtIODevice
*dev
, NetClientState
*ncs
,
386 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(dev
)));
387 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
388 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(vbus
);
391 for (i
= 0; i
< total_queues
; i
++) {
392 vhost_net_stop_one(get_vhost_net(ncs
[i
].peer
), dev
);
395 r
= k
->set_guest_notifiers(qbus
->parent
, total_queues
* 2, false);
397 fprintf(stderr
, "vhost guest notifier cleanup failed: %d\n", r
);
403 void vhost_net_cleanup(struct vhost_net
*net
)
405 vhost_dev_cleanup(&net
->dev
);
408 int vhost_net_notify_migration_done(struct vhost_net
*net
, char* mac_addr
)
410 const VhostOps
*vhost_ops
= net
->dev
.vhost_ops
;
412 assert(vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
413 assert(vhost_ops
->vhost_migration_done
);
415 return vhost_ops
->vhost_migration_done(&net
->dev
, mac_addr
);
418 bool vhost_net_virtqueue_pending(VHostNetState
*net
, int idx
)
420 return vhost_virtqueue_pending(&net
->dev
, idx
);
423 void vhost_net_virtqueue_mask(VHostNetState
*net
, VirtIODevice
*dev
,
426 vhost_virtqueue_mask(&net
->dev
, dev
, idx
, mask
);
429 VHostNetState
*get_vhost_net(NetClientState
*nc
)
431 VHostNetState
*vhost_net
= 0;
437 switch (nc
->info
->type
) {
438 case NET_CLIENT_DRIVER_TAP
:
439 vhost_net
= tap_get_vhost_net(nc
);
441 #ifdef CONFIG_VHOST_NET_USER
442 case NET_CLIENT_DRIVER_VHOST_USER
:
443 vhost_net
= vhost_user_get_vhost_net(nc
);
447 #ifdef CONFIG_VHOST_NET_VDPA
448 case NET_CLIENT_DRIVER_VHOST_VDPA
:
449 vhost_net
= vhost_vdpa_get_vhost_net(nc
);
460 int vhost_set_vring_enable(NetClientState
*nc
, int enable
)
462 VHostNetState
*net
= get_vhost_net(nc
);
463 const VhostOps
*vhost_ops
= net
->dev
.vhost_ops
;
465 nc
->vring_enable
= enable
;
467 if (vhost_ops
&& vhost_ops
->vhost_set_vring_enable
) {
468 return vhost_ops
->vhost_set_vring_enable(&net
->dev
, enable
);
474 int vhost_net_set_mtu(struct vhost_net
*net
, uint16_t mtu
)
476 const VhostOps
*vhost_ops
= net
->dev
.vhost_ops
;
478 if (!vhost_ops
->vhost_net_set_mtu
) {
482 return vhost_ops
->vhost_net_set_mtu(&net
->dev
, mtu
);