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 /* Features supported by host kernel. */
50 static const int kernel_feature_bits
[] = {
51 VIRTIO_F_NOTIFY_ON_EMPTY
,
52 VIRTIO_RING_F_INDIRECT_DESC
,
53 VIRTIO_RING_F_EVENT_IDX
,
54 VIRTIO_NET_F_MRG_RXBUF
,
55 VHOST_INVALID_FEATURE_BIT
58 static const int *vhost_net_get_feature_bits(struct vhost_net
*net
)
60 const int *feature_bits
= 0;
62 switch (net
->nc
->info
->type
) {
63 case NET_CLIENT_OPTIONS_KIND_TAP
:
64 feature_bits
= kernel_feature_bits
;
67 error_report("Feature bits not defined for this type: %d",
75 unsigned vhost_net_get_features(struct vhost_net
*net
, unsigned features
)
77 return vhost_get_features(&net
->dev
, vhost_net_get_feature_bits(net
),
81 void vhost_net_ack_features(struct vhost_net
*net
, unsigned features
)
83 vhost_ack_features(&net
->dev
, vhost_net_get_feature_bits(net
), features
);
86 static int vhost_net_get_fd(NetClientState
*backend
)
88 switch (backend
->info
->type
) {
89 case NET_CLIENT_OPTIONS_KIND_TAP
:
90 return tap_get_fd(backend
);
92 fprintf(stderr
, "vhost-net requires tap backend\n");
97 struct vhost_net
*vhost_net_init(VhostNetOptions
*options
)
100 struct vhost_net
*net
= g_malloc(sizeof *net
);
102 if (!options
->net_backend
) {
103 fprintf(stderr
, "vhost-net requires net backend to be setup\n");
107 r
= vhost_net_get_fd(options
->net_backend
);
111 net
->nc
= options
->net_backend
;
112 net
->dev
.backend_features
= qemu_has_vnet_hdr(options
->net_backend
) ? 0 :
113 (1 << VHOST_NET_F_VIRTIO_NET_HDR
);
117 net
->dev
.vqs
= net
->vqs
;
119 r
= vhost_dev_init(&net
->dev
, options
->opaque
,
124 if (!qemu_has_vnet_hdr_len(options
->net_backend
,
125 sizeof(struct virtio_net_hdr_mrg_rxbuf
))) {
126 net
->dev
.features
&= ~(1 << VIRTIO_NET_F_MRG_RXBUF
);
128 if (~net
->dev
.features
& net
->dev
.backend_features
) {
129 fprintf(stderr
, "vhost lacks feature mask %" PRIu64
" for backend\n",
130 (uint64_t)(~net
->dev
.features
& net
->dev
.backend_features
));
131 vhost_dev_cleanup(&net
->dev
);
135 /* Set sane init value. Override when guest acks. */
136 vhost_net_ack_features(net
, 0);
143 bool vhost_net_query(VHostNetState
*net
, VirtIODevice
*dev
)
145 return vhost_dev_query(&net
->dev
, dev
);
148 static int vhost_net_start_one(struct vhost_net
*net
,
152 struct vhost_vring_file file
= { };
155 if (net
->dev
.started
) {
160 net
->dev
.vqs
= net
->vqs
;
161 net
->dev
.vq_index
= vq_index
;
163 r
= vhost_dev_enable_notifiers(&net
->dev
, dev
);
168 r
= vhost_dev_start(&net
->dev
, dev
);
173 if (net
->nc
->info
->poll
) {
174 net
->nc
->info
->poll(net
->nc
, false);
177 qemu_set_fd_handler(net
->backend
, NULL
, NULL
, NULL
);
178 file
.fd
= net
->backend
;
179 for (file
.index
= 0; file
.index
< net
->dev
.nvqs
; ++file
.index
) {
180 r
= ioctl(net
->dev
.control
, VHOST_NET_SET_BACKEND
, &file
);
189 while (file
.index
-- > 0) {
190 int r
= ioctl(net
->dev
.control
, VHOST_NET_SET_BACKEND
, &file
);
193 if (net
->nc
->info
->poll
) {
194 net
->nc
->info
->poll(net
->nc
, true);
196 vhost_dev_stop(&net
->dev
, dev
);
198 vhost_dev_disable_notifiers(&net
->dev
, dev
);
203 static void vhost_net_stop_one(struct vhost_net
*net
,
206 struct vhost_vring_file file
= { .fd
= -1 };
208 if (!net
->dev
.started
) {
212 for (file
.index
= 0; file
.index
< net
->dev
.nvqs
; ++file
.index
) {
213 int r
= ioctl(net
->dev
.control
, VHOST_NET_SET_BACKEND
, &file
);
216 if (net
->nc
->info
->poll
) {
217 net
->nc
->info
->poll(net
->nc
, true);
219 vhost_dev_stop(&net
->dev
, dev
);
220 vhost_dev_disable_notifiers(&net
->dev
, dev
);
223 int vhost_net_start(VirtIODevice
*dev
, NetClientState
*ncs
,
226 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(dev
)));
227 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
228 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(vbus
);
231 if (!k
->set_guest_notifiers
) {
232 error_report("binding does not support guest notifiers");
237 for (i
= 0; i
< total_queues
; i
++) {
238 r
= vhost_net_start_one(get_vhost_net(ncs
[i
].peer
), dev
, i
* 2);
245 r
= k
->set_guest_notifiers(qbus
->parent
, total_queues
* 2, true);
247 error_report("Error binding guest notifier: %d", -r
);
255 vhost_net_stop_one(get_vhost_net(ncs
[i
].peer
), dev
);
260 void vhost_net_stop(VirtIODevice
*dev
, NetClientState
*ncs
,
263 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(dev
)));
264 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
265 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(vbus
);
268 r
= k
->set_guest_notifiers(qbus
->parent
, total_queues
* 2, false);
270 fprintf(stderr
, "vhost guest notifier cleanup failed: %d\n", r
);
275 for (i
= 0; i
< total_queues
; i
++) {
276 vhost_net_stop_one(get_vhost_net(ncs
[i
].peer
), dev
);
280 void vhost_net_cleanup(struct vhost_net
*net
)
282 vhost_dev_cleanup(&net
->dev
);
286 bool vhost_net_virtqueue_pending(VHostNetState
*net
, int idx
)
288 return vhost_virtqueue_pending(&net
->dev
, idx
);
291 void vhost_net_virtqueue_mask(VHostNetState
*net
, VirtIODevice
*dev
,
294 vhost_virtqueue_mask(&net
->dev
, dev
, idx
, mask
);
297 VHostNetState
*get_vhost_net(NetClientState
*nc
)
299 VHostNetState
*vhost_net
= 0;
305 switch (nc
->info
->type
) {
306 case NET_CLIENT_OPTIONS_KIND_TAP
:
307 vhost_net
= tap_get_vhost_net(nc
);
316 struct vhost_net
*vhost_net_init(VhostNetOptions
*options
)
318 error_report("vhost-net support is not compiled in");
322 bool vhost_net_query(VHostNetState
*net
, VirtIODevice
*dev
)
327 int vhost_net_start(VirtIODevice
*dev
,
333 void vhost_net_stop(VirtIODevice
*dev
,
339 void vhost_net_cleanup(struct vhost_net
*net
)
343 unsigned vhost_net_get_features(struct vhost_net
*net
, unsigned features
)
347 void vhost_net_ack_features(struct vhost_net
*net
, unsigned features
)
351 bool vhost_net_virtqueue_pending(VHostNetState
*net
, int idx
)
356 void vhost_net_virtqueue_mask(VHostNetState
*net
, VirtIODevice
*dev
,
361 VHostNetState
*get_vhost_net(NetClientState
*nc
)