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 VIRTIO_NET_F_HASH_REPORT
,
49 VHOST_INVALID_FEATURE_BIT
52 /* Features supported by others. */
53 static const int user_feature_bits
[] = {
54 VIRTIO_F_NOTIFY_ON_EMPTY
,
55 VIRTIO_RING_F_INDIRECT_DESC
,
56 VIRTIO_RING_F_EVENT_IDX
,
61 VIRTIO_NET_F_GUEST_CSUM
,
63 VIRTIO_NET_F_GUEST_TSO4
,
64 VIRTIO_NET_F_GUEST_TSO6
,
65 VIRTIO_NET_F_GUEST_ECN
,
66 VIRTIO_NET_F_GUEST_UFO
,
67 VIRTIO_NET_F_HOST_TSO4
,
68 VIRTIO_NET_F_HOST_TSO6
,
69 VIRTIO_NET_F_HOST_ECN
,
70 VIRTIO_NET_F_HOST_UFO
,
71 VIRTIO_NET_F_MRG_RXBUF
,
73 VIRTIO_F_IOMMU_PLATFORM
,
76 VIRTIO_NET_F_HASH_REPORT
,
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
;
97 #ifdef CONFIG_VHOST_NET_VDPA
98 case NET_CLIENT_DRIVER_VHOST_VDPA
:
99 feature_bits
= vdpa_feature_bits
;
103 error_report("Feature bits not defined for this type: %d",
104 net
->nc
->info
->type
);
111 uint64_t vhost_net_get_features(struct vhost_net
*net
, uint64_t features
)
113 return vhost_get_features(&net
->dev
, vhost_net_get_feature_bits(net
),
116 int vhost_net_get_config(struct vhost_net
*net
, uint8_t *config
,
119 return vhost_dev_get_config(&net
->dev
, config
, config_len
);
121 int vhost_net_set_config(struct vhost_net
*net
, const uint8_t *data
,
122 uint32_t offset
, uint32_t size
, uint32_t flags
)
124 return vhost_dev_set_config(&net
->dev
, data
, offset
, size
, flags
);
127 void vhost_net_ack_features(struct vhost_net
*net
, uint64_t features
)
129 net
->dev
.acked_features
= net
->dev
.backend_features
;
130 vhost_ack_features(&net
->dev
, vhost_net_get_feature_bits(net
), features
);
133 uint64_t vhost_net_get_max_queues(VHostNetState
*net
)
135 return net
->dev
.max_queues
;
138 uint64_t vhost_net_get_acked_features(VHostNetState
*net
)
140 return net
->dev
.acked_features
;
143 static int vhost_net_get_fd(NetClientState
*backend
)
145 switch (backend
->info
->type
) {
146 case NET_CLIENT_DRIVER_TAP
:
147 return tap_get_fd(backend
);
149 fprintf(stderr
, "vhost-net requires tap backend\n");
154 struct vhost_net
*vhost_net_init(VhostNetOptions
*options
)
157 bool backend_kernel
= options
->backend_type
== VHOST_BACKEND_TYPE_KERNEL
;
158 struct vhost_net
*net
= g_new0(struct vhost_net
, 1);
159 uint64_t features
= 0;
161 if (!options
->net_backend
) {
162 fprintf(stderr
, "vhost-net requires net backend to be setup\n");
165 net
->nc
= options
->net_backend
;
167 net
->dev
.max_queues
= 1;
169 net
->dev
.vqs
= net
->vqs
;
171 if (backend_kernel
) {
172 r
= vhost_net_get_fd(options
->net_backend
);
176 net
->dev
.backend_features
= qemu_has_vnet_hdr(options
->net_backend
)
177 ? 0 : (1ULL << VHOST_NET_F_VIRTIO_NET_HDR
);
179 net
->dev
.protocol_features
= 0;
181 net
->dev
.backend_features
= 0;
182 net
->dev
.protocol_features
= 0;
185 /* vhost-user needs vq_index to initiate a specific queue pair */
186 net
->dev
.vq_index
= net
->nc
->queue_index
* net
->dev
.nvqs
;
189 r
= vhost_dev_init(&net
->dev
, options
->opaque
,
190 options
->backend_type
, options
->busyloop_timeout
);
194 if (backend_kernel
) {
195 if (!qemu_has_vnet_hdr_len(options
->net_backend
,
196 sizeof(struct virtio_net_hdr_mrg_rxbuf
))) {
197 net
->dev
.features
&= ~(1ULL << VIRTIO_NET_F_MRG_RXBUF
);
199 if (~net
->dev
.features
& net
->dev
.backend_features
) {
200 fprintf(stderr
, "vhost lacks feature mask %" PRIu64
202 (uint64_t)(~net
->dev
.features
& net
->dev
.backend_features
));
207 /* Set sane init value. Override when guest acks. */
208 #ifdef CONFIG_VHOST_NET_USER
209 if (net
->nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_USER
) {
210 features
= vhost_user_get_acked_features(net
->nc
);
211 if (~net
->dev
.features
& features
) {
212 fprintf(stderr
, "vhost lacks feature mask %" PRIu64
214 (uint64_t)(~net
->dev
.features
& features
));
220 vhost_net_ack_features(net
, features
);
225 vhost_dev_cleanup(&net
->dev
);
230 static void vhost_net_set_vq_index(struct vhost_net
*net
, int vq_index
)
232 net
->dev
.vq_index
= vq_index
;
235 static int vhost_net_start_one(struct vhost_net
*net
,
238 struct vhost_vring_file file
= { };
242 net
->dev
.vqs
= net
->vqs
;
244 r
= vhost_dev_enable_notifiers(&net
->dev
, dev
);
249 r
= vhost_dev_start(&net
->dev
, dev
);
254 if (net
->nc
->info
->poll
) {
255 net
->nc
->info
->poll(net
->nc
, false);
258 if (net
->nc
->info
->type
== NET_CLIENT_DRIVER_TAP
) {
259 qemu_set_fd_handler(net
->backend
, NULL
, NULL
, NULL
);
260 file
.fd
= net
->backend
;
261 for (file
.index
= 0; file
.index
< net
->dev
.nvqs
; ++file
.index
) {
262 if (!virtio_queue_enabled(dev
, net
->dev
.vq_index
+
264 /* Queue might not be ready for start */
267 r
= vhost_net_set_backend(&net
->dev
, &file
);
277 if (net
->nc
->info
->type
== NET_CLIENT_DRIVER_TAP
) {
278 while (file
.index
-- > 0) {
279 if (!virtio_queue_enabled(dev
, net
->dev
.vq_index
+
281 /* Queue might not be ready for start */
284 int r
= vhost_net_set_backend(&net
->dev
, &file
);
288 if (net
->nc
->info
->poll
) {
289 net
->nc
->info
->poll(net
->nc
, true);
291 vhost_dev_stop(&net
->dev
, dev
);
293 vhost_dev_disable_notifiers(&net
->dev
, dev
);
298 static void vhost_net_stop_one(struct vhost_net
*net
,
301 struct vhost_vring_file file
= { .fd
= -1 };
303 if (net
->nc
->info
->type
== NET_CLIENT_DRIVER_TAP
) {
304 for (file
.index
= 0; file
.index
< net
->dev
.nvqs
; ++file
.index
) {
305 int r
= vhost_net_set_backend(&net
->dev
, &file
);
309 if (net
->nc
->info
->poll
) {
310 net
->nc
->info
->poll(net
->nc
, true);
312 vhost_dev_stop(&net
->dev
, dev
);
313 vhost_dev_disable_notifiers(&net
->dev
, dev
);
316 int vhost_net_start(VirtIODevice
*dev
, NetClientState
*ncs
,
319 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(dev
)));
320 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
321 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(vbus
);
322 struct vhost_net
*net
;
324 NetClientState
*peer
;
326 if (!k
->set_guest_notifiers
) {
327 error_report("binding does not support guest notifiers");
331 for (i
= 0; i
< total_queues
; i
++) {
333 peer
= qemu_get_peer(ncs
, i
);
334 net
= get_vhost_net(peer
);
335 vhost_net_set_vq_index(net
, i
* 2);
337 /* Suppress the masking guest notifiers on vhost user
338 * because vhost user doesn't interrupt masking/unmasking
341 if (net
->nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_USER
) {
342 dev
->use_guest_notifier_mask
= false;
346 r
= k
->set_guest_notifiers(qbus
->parent
, total_queues
* 2, true);
348 error_report("Error binding guest notifier: %d", -r
);
352 for (i
= 0; i
< total_queues
; i
++) {
353 peer
= qemu_get_peer(ncs
, i
);
354 r
= vhost_net_start_one(get_vhost_net(peer
), dev
);
360 if (peer
->vring_enable
) {
361 /* restore vring enable state */
362 r
= vhost_set_vring_enable(peer
, peer
->vring_enable
);
374 peer
= qemu_get_peer(ncs
, i
);
375 vhost_net_stop_one(get_vhost_net(peer
), dev
);
377 e
= k
->set_guest_notifiers(qbus
->parent
, total_queues
* 2, false);
379 fprintf(stderr
, "vhost guest notifier cleanup failed: %d\n", e
);
386 void vhost_net_stop(VirtIODevice
*dev
, NetClientState
*ncs
,
389 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(dev
)));
390 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
391 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(vbus
);
394 for (i
= 0; i
< total_queues
; i
++) {
395 vhost_net_stop_one(get_vhost_net(ncs
[i
].peer
), dev
);
398 r
= k
->set_guest_notifiers(qbus
->parent
, total_queues
* 2, false);
400 fprintf(stderr
, "vhost guest notifier cleanup failed: %d\n", r
);
406 void vhost_net_cleanup(struct vhost_net
*net
)
408 vhost_dev_cleanup(&net
->dev
);
411 int vhost_net_notify_migration_done(struct vhost_net
*net
, char* mac_addr
)
413 const VhostOps
*vhost_ops
= net
->dev
.vhost_ops
;
415 assert(vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
416 assert(vhost_ops
->vhost_migration_done
);
418 return vhost_ops
->vhost_migration_done(&net
->dev
, mac_addr
);
421 bool vhost_net_virtqueue_pending(VHostNetState
*net
, int idx
)
423 return vhost_virtqueue_pending(&net
->dev
, idx
);
426 void vhost_net_virtqueue_mask(VHostNetState
*net
, VirtIODevice
*dev
,
429 vhost_virtqueue_mask(&net
->dev
, dev
, idx
, mask
);
432 VHostNetState
*get_vhost_net(NetClientState
*nc
)
434 VHostNetState
*vhost_net
= 0;
440 switch (nc
->info
->type
) {
441 case NET_CLIENT_DRIVER_TAP
:
442 vhost_net
= tap_get_vhost_net(nc
);
444 #ifdef CONFIG_VHOST_NET_USER
445 case NET_CLIENT_DRIVER_VHOST_USER
:
446 vhost_net
= vhost_user_get_vhost_net(nc
);
450 #ifdef CONFIG_VHOST_NET_VDPA
451 case NET_CLIENT_DRIVER_VHOST_VDPA
:
452 vhost_net
= vhost_vdpa_get_vhost_net(nc
);
463 int vhost_set_vring_enable(NetClientState
*nc
, int enable
)
465 VHostNetState
*net
= get_vhost_net(nc
);
466 const VhostOps
*vhost_ops
= net
->dev
.vhost_ops
;
468 nc
->vring_enable
= enable
;
470 if (vhost_ops
&& vhost_ops
->vhost_set_vring_enable
) {
471 return vhost_ops
->vhost_set_vring_enable(&net
->dev
, enable
);
477 int vhost_net_set_mtu(struct vhost_net
*net
, uint16_t mtu
)
479 const VhostOps
*vhost_ops
= net
->dev
.vhost_ops
;
481 if (!vhost_ops
->vhost_net_set_mtu
) {
485 return vhost_ops
->vhost_net_set_mtu(&net
->dev
, mtu
);