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/virtio-net.h"
20 #include "net/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>
39 #include "hw/virtio/vhost.h"
40 #include "hw/virtio/virtio-bus.h"
44 struct vhost_virtqueue vqs
[2];
49 unsigned vhost_net_get_features(struct vhost_net
*net
, unsigned features
)
51 /* Clear features not supported by host kernel. */
52 if (!(net
->dev
.features
& (1 << VIRTIO_F_NOTIFY_ON_EMPTY
))) {
53 features
&= ~(1 << VIRTIO_F_NOTIFY_ON_EMPTY
);
55 if (!(net
->dev
.features
& (1 << VIRTIO_RING_F_INDIRECT_DESC
))) {
56 features
&= ~(1 << VIRTIO_RING_F_INDIRECT_DESC
);
58 if (!(net
->dev
.features
& (1 << VIRTIO_RING_F_EVENT_IDX
))) {
59 features
&= ~(1 << VIRTIO_RING_F_EVENT_IDX
);
61 if (!(net
->dev
.features
& (1 << VIRTIO_NET_F_MRG_RXBUF
))) {
62 features
&= ~(1 << VIRTIO_NET_F_MRG_RXBUF
);
67 void vhost_net_ack_features(struct vhost_net
*net
, unsigned features
)
69 net
->dev
.acked_features
= net
->dev
.backend_features
;
70 if (features
& (1 << VIRTIO_F_NOTIFY_ON_EMPTY
)) {
71 net
->dev
.acked_features
|= (1 << VIRTIO_F_NOTIFY_ON_EMPTY
);
73 if (features
& (1 << VIRTIO_RING_F_INDIRECT_DESC
)) {
74 net
->dev
.acked_features
|= (1 << VIRTIO_RING_F_INDIRECT_DESC
);
76 if (features
& (1 << VIRTIO_RING_F_EVENT_IDX
)) {
77 net
->dev
.acked_features
|= (1 << VIRTIO_RING_F_EVENT_IDX
);
79 if (features
& (1 << VIRTIO_NET_F_MRG_RXBUF
)) {
80 net
->dev
.acked_features
|= (1 << VIRTIO_NET_F_MRG_RXBUF
);
84 static int vhost_net_get_fd(NetClientState
*backend
)
86 switch (backend
->info
->type
) {
87 case NET_CLIENT_OPTIONS_KIND_TAP
:
88 return tap_get_fd(backend
);
90 fprintf(stderr
, "vhost-net requires tap backend\n");
95 struct vhost_net
*vhost_net_init(NetClientState
*backend
, int devfd
,
99 struct vhost_net
*net
= g_malloc(sizeof *net
);
101 fprintf(stderr
, "vhost-net requires backend to be setup\n");
104 r
= vhost_net_get_fd(backend
);
109 net
->dev
.backend_features
= qemu_has_vnet_hdr(backend
) ? 0 :
110 (1 << VHOST_NET_F_VIRTIO_NET_HDR
);
114 net
->dev
.vqs
= net
->vqs
;
116 r
= vhost_dev_init(&net
->dev
, devfd
, "/dev/vhost-net", force
);
120 if (!qemu_has_vnet_hdr_len(backend
,
121 sizeof(struct virtio_net_hdr_mrg_rxbuf
))) {
122 net
->dev
.features
&= ~(1 << VIRTIO_NET_F_MRG_RXBUF
);
124 if (~net
->dev
.features
& net
->dev
.backend_features
) {
125 fprintf(stderr
, "vhost lacks feature mask %" PRIu64
" for backend\n",
126 (uint64_t)(~net
->dev
.features
& net
->dev
.backend_features
));
127 vhost_dev_cleanup(&net
->dev
);
131 /* Set sane init value. Override when guest acks. */
132 vhost_net_ack_features(net
, 0);
139 bool vhost_net_query(VHostNetState
*net
, VirtIODevice
*dev
)
141 return vhost_dev_query(&net
->dev
, dev
);
144 static int vhost_net_start_one(struct vhost_net
*net
,
148 struct vhost_vring_file file
= { };
151 if (net
->dev
.started
) {
156 net
->dev
.vqs
= net
->vqs
;
157 net
->dev
.vq_index
= vq_index
;
159 r
= vhost_dev_enable_notifiers(&net
->dev
, dev
);
164 r
= vhost_dev_start(&net
->dev
, dev
);
169 net
->nc
->info
->poll(net
->nc
, false);
170 qemu_set_fd_handler(net
->backend
, NULL
, NULL
, NULL
);
171 file
.fd
= net
->backend
;
172 for (file
.index
= 0; file
.index
< net
->dev
.nvqs
; ++file
.index
) {
173 r
= ioctl(net
->dev
.control
, VHOST_NET_SET_BACKEND
, &file
);
182 while (file
.index
-- > 0) {
183 int r
= ioctl(net
->dev
.control
, VHOST_NET_SET_BACKEND
, &file
);
186 net
->nc
->info
->poll(net
->nc
, true);
187 vhost_dev_stop(&net
->dev
, dev
);
189 vhost_dev_disable_notifiers(&net
->dev
, dev
);
194 static void vhost_net_stop_one(struct vhost_net
*net
,
197 struct vhost_vring_file file
= { .fd
= -1 };
199 if (!net
->dev
.started
) {
203 for (file
.index
= 0; file
.index
< net
->dev
.nvqs
; ++file
.index
) {
204 int r
= ioctl(net
->dev
.control
, VHOST_NET_SET_BACKEND
, &file
);
207 net
->nc
->info
->poll(net
->nc
, true);
208 vhost_dev_stop(&net
->dev
, dev
);
209 vhost_dev_disable_notifiers(&net
->dev
, dev
);
212 int vhost_net_start(VirtIODevice
*dev
, NetClientState
*ncs
,
215 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(dev
)));
216 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
217 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(vbus
);
220 if (!k
->set_guest_notifiers
) {
221 error_report("binding does not support guest notifiers");
226 for (i
= 0; i
< total_queues
; i
++) {
227 r
= vhost_net_start_one(tap_get_vhost_net(ncs
[i
].peer
), dev
, i
* 2);
234 r
= k
->set_guest_notifiers(qbus
->parent
, total_queues
* 2, true);
236 error_report("Error binding guest notifier: %d", -r
);
244 vhost_net_stop_one(tap_get_vhost_net(ncs
[i
].peer
), dev
);
249 void vhost_net_stop(VirtIODevice
*dev
, NetClientState
*ncs
,
252 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(dev
)));
253 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
254 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(vbus
);
257 r
= k
->set_guest_notifiers(qbus
->parent
, total_queues
* 2, false);
259 fprintf(stderr
, "vhost guest notifier cleanup failed: %d\n", r
);
264 for (i
= 0; i
< total_queues
; i
++) {
265 vhost_net_stop_one(tap_get_vhost_net(ncs
[i
].peer
), dev
);
269 void vhost_net_cleanup(struct vhost_net
*net
)
271 vhost_dev_cleanup(&net
->dev
);
275 bool vhost_net_virtqueue_pending(VHostNetState
*net
, int idx
)
277 return vhost_virtqueue_pending(&net
->dev
, idx
);
280 void vhost_net_virtqueue_mask(VHostNetState
*net
, VirtIODevice
*dev
,
283 vhost_virtqueue_mask(&net
->dev
, dev
, idx
, mask
);
286 struct vhost_net
*vhost_net_init(NetClientState
*backend
, int devfd
,
289 error_report("vhost-net support is not compiled in");
293 bool vhost_net_query(VHostNetState
*net
, VirtIODevice
*dev
)
298 int vhost_net_start(VirtIODevice
*dev
,
304 void vhost_net_stop(VirtIODevice
*dev
,
310 void vhost_net_cleanup(struct vhost_net
*net
)
314 unsigned vhost_net_get_features(struct vhost_net
*net
, unsigned features
)
318 void vhost_net_ack_features(struct vhost_net
*net
, unsigned features
)
322 bool vhost_net_virtqueue_pending(VHostNetState
*net
, int idx
)
327 void vhost_net_virtqueue_mask(VHostNetState
*net
, VirtIODevice
*dev
,