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 "qapi/error.h"
26 #include "qemu/error-report.h"
27 #include "qemu/main-loop.h"
29 #include <sys/socket.h>
31 #include <netinet/in.h>
34 #include "standard-headers/linux/virtio_ring.h"
35 #include "hw/virtio/vhost.h"
36 #include "hw/virtio/virtio-bus.h"
39 /* Features supported by host kernel. */
40 static const int kernel_feature_bits
[] = {
41 VIRTIO_F_NOTIFY_ON_EMPTY
,
42 VIRTIO_RING_F_INDIRECT_DESC
,
43 VIRTIO_RING_F_EVENT_IDX
,
44 VIRTIO_NET_F_MRG_RXBUF
,
47 VIRTIO_F_IOMMU_PLATFORM
,
49 VIRTIO_NET_F_HASH_REPORT
,
50 VHOST_INVALID_FEATURE_BIT
53 /* Features supported by others. */
54 static const int user_feature_bits
[] = {
55 VIRTIO_F_NOTIFY_ON_EMPTY
,
56 VIRTIO_RING_F_INDIRECT_DESC
,
57 VIRTIO_RING_F_EVENT_IDX
,
62 VIRTIO_NET_F_GUEST_CSUM
,
64 VIRTIO_NET_F_GUEST_TSO4
,
65 VIRTIO_NET_F_GUEST_TSO6
,
66 VIRTIO_NET_F_GUEST_ECN
,
67 VIRTIO_NET_F_GUEST_UFO
,
68 VIRTIO_NET_F_HOST_TSO4
,
69 VIRTIO_NET_F_HOST_TSO6
,
70 VIRTIO_NET_F_HOST_ECN
,
71 VIRTIO_NET_F_HOST_UFO
,
72 VIRTIO_NET_F_MRG_RXBUF
,
74 VIRTIO_F_IOMMU_PLATFORM
,
77 VIRTIO_NET_F_HASH_REPORT
,
79 /* This bit implies RARP isn't sent by QEMU out of band */
80 VIRTIO_NET_F_GUEST_ANNOUNCE
,
84 VHOST_INVALID_FEATURE_BIT
87 static const int *vhost_net_get_feature_bits(struct vhost_net
*net
)
89 const int *feature_bits
= 0;
91 switch (net
->nc
->info
->type
) {
92 case NET_CLIENT_DRIVER_TAP
:
93 feature_bits
= kernel_feature_bits
;
95 case NET_CLIENT_DRIVER_VHOST_USER
:
96 feature_bits
= user_feature_bits
;
98 #ifdef CONFIG_VHOST_NET_VDPA
99 case NET_CLIENT_DRIVER_VHOST_VDPA
:
100 feature_bits
= vdpa_feature_bits
;
104 error_report("Feature bits not defined for this type: %d",
105 net
->nc
->info
->type
);
112 uint64_t vhost_net_get_features(struct vhost_net
*net
, uint64_t features
)
114 return vhost_get_features(&net
->dev
, vhost_net_get_feature_bits(net
),
117 int vhost_net_get_config(struct vhost_net
*net
, uint8_t *config
,
120 return vhost_dev_get_config(&net
->dev
, config
, config_len
, NULL
);
122 int vhost_net_set_config(struct vhost_net
*net
, const uint8_t *data
,
123 uint32_t offset
, uint32_t size
, uint32_t flags
)
125 return vhost_dev_set_config(&net
->dev
, data
, offset
, size
, flags
);
128 void vhost_net_ack_features(struct vhost_net
*net
, uint64_t features
)
130 net
->dev
.acked_features
= net
->dev
.backend_features
;
131 vhost_ack_features(&net
->dev
, vhost_net_get_feature_bits(net
), features
);
134 uint64_t vhost_net_get_max_queues(VHostNetState
*net
)
136 return net
->dev
.max_queues
;
139 uint64_t vhost_net_get_acked_features(VHostNetState
*net
)
141 return net
->dev
.acked_features
;
144 static int vhost_net_get_fd(NetClientState
*backend
)
146 switch (backend
->info
->type
) {
147 case NET_CLIENT_DRIVER_TAP
:
148 return tap_get_fd(backend
);
150 fprintf(stderr
, "vhost-net requires tap backend\n");
155 struct vhost_net
*vhost_net_init(VhostNetOptions
*options
)
158 bool backend_kernel
= options
->backend_type
== VHOST_BACKEND_TYPE_KERNEL
;
159 struct vhost_net
*net
= g_new0(struct vhost_net
, 1);
160 uint64_t features
= 0;
161 Error
*local_err
= NULL
;
163 if (!options
->net_backend
) {
164 fprintf(stderr
, "vhost-net requires net backend to be setup\n");
167 net
->nc
= options
->net_backend
;
168 net
->dev
.nvqs
= options
->nvqs
;
170 net
->dev
.max_queues
= 1;
171 net
->dev
.vqs
= net
->vqs
;
173 if (backend_kernel
) {
174 r
= vhost_net_get_fd(options
->net_backend
);
178 net
->dev
.backend_features
= qemu_has_vnet_hdr(options
->net_backend
)
179 ? 0 : (1ULL << VHOST_NET_F_VIRTIO_NET_HDR
);
181 net
->dev
.protocol_features
= 0;
183 net
->dev
.backend_features
= 0;
184 net
->dev
.protocol_features
= 0;
187 /* vhost-user needs vq_index to initiate a specific queue pair */
188 net
->dev
.vq_index
= net
->nc
->queue_index
* net
->dev
.nvqs
;
191 r
= vhost_dev_init(&net
->dev
, options
->opaque
,
192 options
->backend_type
, options
->busyloop_timeout
,
195 error_report_err(local_err
);
198 if (backend_kernel
) {
199 if (!qemu_has_vnet_hdr_len(options
->net_backend
,
200 sizeof(struct virtio_net_hdr_mrg_rxbuf
))) {
201 net
->dev
.features
&= ~(1ULL << VIRTIO_NET_F_MRG_RXBUF
);
203 if (~net
->dev
.features
& net
->dev
.backend_features
) {
204 fprintf(stderr
, "vhost lacks feature mask %" PRIu64
206 (uint64_t)(~net
->dev
.features
& net
->dev
.backend_features
));
211 /* Set sane init value. Override when guest acks. */
212 #ifdef CONFIG_VHOST_NET_USER
213 if (net
->nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_USER
) {
214 features
= vhost_user_get_acked_features(net
->nc
);
215 if (~net
->dev
.features
& features
) {
216 fprintf(stderr
, "vhost lacks feature mask %" PRIu64
218 (uint64_t)(~net
->dev
.features
& features
));
224 vhost_net_ack_features(net
, features
);
229 vhost_dev_cleanup(&net
->dev
);
234 static void vhost_net_set_vq_index(struct vhost_net
*net
, int vq_index
)
236 net
->dev
.vq_index
= vq_index
;
239 static int vhost_net_start_one(struct vhost_net
*net
,
242 struct vhost_vring_file file
= { };
245 r
= vhost_dev_enable_notifiers(&net
->dev
, dev
);
250 r
= vhost_dev_start(&net
->dev
, dev
);
255 if (net
->nc
->info
->poll
) {
256 net
->nc
->info
->poll(net
->nc
, false);
259 if (net
->nc
->info
->type
== NET_CLIENT_DRIVER_TAP
) {
260 qemu_set_fd_handler(net
->backend
, NULL
, NULL
, NULL
);
261 file
.fd
= net
->backend
;
262 for (file
.index
= 0; file
.index
< net
->dev
.nvqs
; ++file
.index
) {
263 if (!virtio_queue_enabled(dev
, net
->dev
.vq_index
+
265 /* Queue might not be ready for start */
268 r
= vhost_net_set_backend(&net
->dev
, &file
);
278 if (net
->nc
->info
->type
== NET_CLIENT_DRIVER_TAP
) {
279 while (file
.index
-- > 0) {
280 if (!virtio_queue_enabled(dev
, net
->dev
.vq_index
+
282 /* Queue might not be ready for start */
285 int r
= vhost_net_set_backend(&net
->dev
, &file
);
289 if (net
->nc
->info
->poll
) {
290 net
->nc
->info
->poll(net
->nc
, true);
292 vhost_dev_stop(&net
->dev
, dev
);
294 vhost_dev_disable_notifiers(&net
->dev
, dev
);
299 static void vhost_net_stop_one(struct vhost_net
*net
,
302 struct vhost_vring_file file
= { .fd
= -1 };
304 if (net
->nc
->info
->type
== NET_CLIENT_DRIVER_TAP
) {
305 for (file
.index
= 0; file
.index
< net
->dev
.nvqs
; ++file
.index
) {
306 int r
= vhost_net_set_backend(&net
->dev
, &file
);
310 if (net
->nc
->info
->poll
) {
311 net
->nc
->info
->poll(net
->nc
, true);
313 vhost_dev_stop(&net
->dev
, dev
);
314 vhost_dev_disable_notifiers(&net
->dev
, dev
);
317 int vhost_net_start(VirtIODevice
*dev
, NetClientState
*ncs
,
320 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(dev
)));
321 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
322 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(vbus
);
323 struct vhost_net
*net
;
325 NetClientState
*peer
;
327 if (!k
->set_guest_notifiers
) {
328 error_report("binding does not support guest notifiers");
332 for (i
= 0; i
< total_queues
; i
++) {
334 peer
= qemu_get_peer(ncs
, i
);
335 net
= get_vhost_net(peer
);
336 vhost_net_set_vq_index(net
, i
* 2);
338 /* Suppress the masking guest notifiers on vhost user
339 * because vhost user doesn't interrupt masking/unmasking
342 if (net
->nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_USER
) {
343 dev
->use_guest_notifier_mask
= false;
347 r
= k
->set_guest_notifiers(qbus
->parent
, total_queues
* 2, true);
349 error_report("Error binding guest notifier: %d", -r
);
353 for (i
= 0; i
< total_queues
; i
++) {
354 peer
= qemu_get_peer(ncs
, i
);
355 r
= vhost_net_start_one(get_vhost_net(peer
), dev
);
361 if (peer
->vring_enable
) {
362 /* restore vring enable state */
363 r
= vhost_set_vring_enable(peer
, peer
->vring_enable
);
375 peer
= qemu_get_peer(ncs
, i
);
376 vhost_net_stop_one(get_vhost_net(peer
), dev
);
378 e
= k
->set_guest_notifiers(qbus
->parent
, total_queues
* 2, false);
380 fprintf(stderr
, "vhost guest notifier cleanup failed: %d\n", e
);
387 void vhost_net_stop(VirtIODevice
*dev
, NetClientState
*ncs
,
390 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(dev
)));
391 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
392 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(vbus
);
395 for (i
= 0; i
< total_queues
; i
++) {
396 vhost_net_stop_one(get_vhost_net(ncs
[i
].peer
), dev
);
399 r
= k
->set_guest_notifiers(qbus
->parent
, total_queues
* 2, false);
401 fprintf(stderr
, "vhost guest notifier cleanup failed: %d\n", r
);
407 void vhost_net_cleanup(struct vhost_net
*net
)
409 vhost_dev_cleanup(&net
->dev
);
412 int vhost_net_notify_migration_done(struct vhost_net
*net
, char* mac_addr
)
414 const VhostOps
*vhost_ops
= net
->dev
.vhost_ops
;
416 assert(vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
417 assert(vhost_ops
->vhost_migration_done
);
419 return vhost_ops
->vhost_migration_done(&net
->dev
, mac_addr
);
422 bool vhost_net_virtqueue_pending(VHostNetState
*net
, int idx
)
424 return vhost_virtqueue_pending(&net
->dev
, idx
);
427 void vhost_net_virtqueue_mask(VHostNetState
*net
, VirtIODevice
*dev
,
430 vhost_virtqueue_mask(&net
->dev
, dev
, idx
, mask
);
433 VHostNetState
*get_vhost_net(NetClientState
*nc
)
435 VHostNetState
*vhost_net
= 0;
441 switch (nc
->info
->type
) {
442 case NET_CLIENT_DRIVER_TAP
:
443 vhost_net
= tap_get_vhost_net(nc
);
445 #ifdef CONFIG_VHOST_NET_USER
446 case NET_CLIENT_DRIVER_VHOST_USER
:
447 vhost_net
= vhost_user_get_vhost_net(nc
);
451 #ifdef CONFIG_VHOST_NET_VDPA
452 case NET_CLIENT_DRIVER_VHOST_VDPA
:
453 vhost_net
= vhost_vdpa_get_vhost_net(nc
);
464 int vhost_set_vring_enable(NetClientState
*nc
, int enable
)
466 VHostNetState
*net
= get_vhost_net(nc
);
467 const VhostOps
*vhost_ops
= net
->dev
.vhost_ops
;
469 nc
->vring_enable
= enable
;
471 if (vhost_ops
&& vhost_ops
->vhost_set_vring_enable
) {
472 return vhost_ops
->vhost_set_vring_enable(&net
->dev
, enable
);
478 int vhost_net_set_mtu(struct vhost_net
*net
, uint16_t mtu
)
480 const VhostOps
*vhost_ops
= net
->dev
.vhost_ops
;
482 if (!vhost_ops
->vhost_net_set_mtu
) {
486 return vhost_ops
->vhost_net_set_mtu(&net
->dev
, mtu
);