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.
19 #include "hw/virtio-net.h"
20 #include "hw/vhost_net.h"
21 #include "qemu/error-report.h"
25 #ifdef CONFIG_VHOST_NET
26 #include <linux/vhost.h>
27 #include <sys/socket.h>
28 #include <linux/kvm.h>
30 #include <sys/ioctl.h>
31 #include <linux/virtio_ring.h>
32 #include <netpacket/packet.h>
33 #include <net/ethernet.h>
35 #include <netinet/in.h>
43 struct vhost_virtqueue vqs
[2];
48 unsigned vhost_net_get_features(struct vhost_net
*net
, unsigned features
)
50 /* Clear features not supported by host kernel. */
51 if (!(net
->dev
.features
& (1 << VIRTIO_F_NOTIFY_ON_EMPTY
))) {
52 features
&= ~(1 << VIRTIO_F_NOTIFY_ON_EMPTY
);
54 if (!(net
->dev
.features
& (1 << VIRTIO_RING_F_INDIRECT_DESC
))) {
55 features
&= ~(1 << VIRTIO_RING_F_INDIRECT_DESC
);
57 if (!(net
->dev
.features
& (1 << VIRTIO_RING_F_EVENT_IDX
))) {
58 features
&= ~(1 << VIRTIO_RING_F_EVENT_IDX
);
60 if (!(net
->dev
.features
& (1 << VIRTIO_NET_F_MRG_RXBUF
))) {
61 features
&= ~(1 << VIRTIO_NET_F_MRG_RXBUF
);
66 void vhost_net_ack_features(struct vhost_net
*net
, unsigned features
)
68 net
->dev
.acked_features
= net
->dev
.backend_features
;
69 if (features
& (1 << VIRTIO_F_NOTIFY_ON_EMPTY
)) {
70 net
->dev
.acked_features
|= (1 << VIRTIO_F_NOTIFY_ON_EMPTY
);
72 if (features
& (1 << VIRTIO_RING_F_INDIRECT_DESC
)) {
73 net
->dev
.acked_features
|= (1 << VIRTIO_RING_F_INDIRECT_DESC
);
75 if (features
& (1 << VIRTIO_RING_F_EVENT_IDX
)) {
76 net
->dev
.acked_features
|= (1 << VIRTIO_RING_F_EVENT_IDX
);
78 if (features
& (1 << VIRTIO_NET_F_MRG_RXBUF
)) {
79 net
->dev
.acked_features
|= (1 << VIRTIO_NET_F_MRG_RXBUF
);
83 static int vhost_net_get_fd(NetClientState
*backend
)
85 switch (backend
->info
->type
) {
86 case NET_CLIENT_OPTIONS_KIND_TAP
:
87 return tap_get_fd(backend
);
89 fprintf(stderr
, "vhost-net requires tap backend\n");
94 struct vhost_net
*vhost_net_init(NetClientState
*backend
, int devfd
,
98 struct vhost_net
*net
= g_malloc(sizeof *net
);
100 fprintf(stderr
, "vhost-net requires backend to be setup\n");
103 r
= vhost_net_get_fd(backend
);
108 net
->dev
.backend_features
= tap_has_vnet_hdr(backend
) ? 0 :
109 (1 << VHOST_NET_F_VIRTIO_NET_HDR
);
113 net
->dev
.vqs
= net
->vqs
;
115 r
= vhost_dev_init(&net
->dev
, devfd
, "/dev/vhost-net", force
);
119 if (!tap_has_vnet_hdr_len(backend
,
120 sizeof(struct virtio_net_hdr_mrg_rxbuf
))) {
121 net
->dev
.features
&= ~(1 << VIRTIO_NET_F_MRG_RXBUF
);
123 if (~net
->dev
.features
& net
->dev
.backend_features
) {
124 fprintf(stderr
, "vhost lacks feature mask %" PRIu64
" for backend\n",
125 (uint64_t)(~net
->dev
.features
& net
->dev
.backend_features
));
126 vhost_dev_cleanup(&net
->dev
);
130 /* Set sane init value. Override when guest acks. */
131 vhost_net_ack_features(net
, 0);
138 bool vhost_net_query(VHostNetState
*net
, VirtIODevice
*dev
)
140 return vhost_dev_query(&net
->dev
, dev
);
143 static int vhost_net_start_one(struct vhost_net
*net
,
147 struct vhost_vring_file file
= { };
150 if (net
->dev
.started
) {
155 net
->dev
.vqs
= net
->vqs
;
156 net
->dev
.vq_index
= vq_index
;
158 r
= vhost_dev_enable_notifiers(&net
->dev
, dev
);
163 r
= vhost_dev_start(&net
->dev
, dev
);
168 net
->nc
->info
->poll(net
->nc
, false);
169 qemu_set_fd_handler(net
->backend
, NULL
, NULL
, NULL
);
170 file
.fd
= net
->backend
;
171 for (file
.index
= 0; file
.index
< net
->dev
.nvqs
; ++file
.index
) {
172 r
= ioctl(net
->dev
.control
, VHOST_NET_SET_BACKEND
, &file
);
181 while (file
.index
-- > 0) {
182 int r
= ioctl(net
->dev
.control
, VHOST_NET_SET_BACKEND
, &file
);
185 net
->nc
->info
->poll(net
->nc
, true);
186 vhost_dev_stop(&net
->dev
, dev
);
188 vhost_dev_disable_notifiers(&net
->dev
, dev
);
193 static void vhost_net_stop_one(struct vhost_net
*net
,
196 struct vhost_vring_file file
= { .fd
= -1 };
198 if (!net
->dev
.started
) {
202 for (file
.index
= 0; file
.index
< net
->dev
.nvqs
; ++file
.index
) {
203 int r
= ioctl(net
->dev
.control
, VHOST_NET_SET_BACKEND
, &file
);
206 net
->nc
->info
->poll(net
->nc
, true);
207 vhost_dev_stop(&net
->dev
, dev
);
208 vhost_dev_disable_notifiers(&net
->dev
, dev
);
211 int vhost_net_start(VirtIODevice
*dev
, NetClientState
*ncs
,
216 if (!dev
->binding
->set_guest_notifiers
) {
217 error_report("binding does not support guest notifiers");
222 for (i
= 0; i
< total_queues
; i
++) {
223 r
= vhost_net_start_one(tap_get_vhost_net(ncs
[i
].peer
), dev
, i
* 2);
230 r
= dev
->binding
->set_guest_notifiers(dev
->binding_opaque
,
234 error_report("Error binding guest notifier: %d", -r
);
242 vhost_net_stop_one(tap_get_vhost_net(ncs
[i
].peer
), dev
);
247 void vhost_net_stop(VirtIODevice
*dev
, NetClientState
*ncs
,
252 r
= dev
->binding
->set_guest_notifiers(dev
->binding_opaque
,
256 fprintf(stderr
, "vhost guest notifier cleanup failed: %d\n", r
);
261 for (i
= 0; i
< total_queues
; i
++) {
262 vhost_net_stop_one(tap_get_vhost_net(ncs
[i
].peer
), dev
);
266 void vhost_net_cleanup(struct vhost_net
*net
)
268 vhost_dev_cleanup(&net
->dev
);
272 bool vhost_net_virtqueue_pending(VHostNetState
*net
, int idx
)
274 return vhost_virtqueue_pending(&net
->dev
, idx
);
277 void vhost_net_virtqueue_mask(VHostNetState
*net
, VirtIODevice
*dev
,
280 vhost_virtqueue_mask(&net
->dev
, dev
, idx
, mask
);
283 struct vhost_net
*vhost_net_init(NetClientState
*backend
, int devfd
,
286 error_report("vhost-net support is not compiled in");
290 bool vhost_net_query(VHostNetState
*net
, VirtIODevice
*dev
)
295 int vhost_net_start(VirtIODevice
*dev
,
301 void vhost_net_stop(VirtIODevice
*dev
,
307 void vhost_net_cleanup(struct vhost_net
*net
)
311 unsigned vhost_net_get_features(struct vhost_net
*net
, unsigned features
)
315 void vhost_net_ack_features(struct vhost_net
*net
, unsigned features
)
319 bool vhost_net_virtqueue_pending(VHostNetState
*net
, int idx
)
324 void vhost_net_virtqueue_mask(VHostNetState
*net
, VirtIODevice
*dev
,