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.
16 #include "virtio-net.h"
17 #include "vhost_net.h"
18 #include "qemu-error.h"
22 #ifdef CONFIG_VHOST_NET
23 #include <linux/vhost.h>
24 #include <sys/socket.h>
25 #include <linux/kvm.h>
27 #include <sys/ioctl.h>
28 #include <linux/virtio_ring.h>
29 #include <netpacket/packet.h>
30 #include <net/ethernet.h>
32 #include <netinet/in.h>
40 struct vhost_virtqueue vqs
[2];
45 unsigned vhost_net_get_features(struct vhost_net
*net
, unsigned features
)
47 /* Clear features not supported by host kernel. */
48 if (!(net
->dev
.features
& (1 << VIRTIO_F_NOTIFY_ON_EMPTY
))) {
49 features
&= ~(1 << VIRTIO_F_NOTIFY_ON_EMPTY
);
51 if (!(net
->dev
.features
& (1 << VIRTIO_RING_F_INDIRECT_DESC
))) {
52 features
&= ~(1 << VIRTIO_RING_F_INDIRECT_DESC
);
54 if (!(net
->dev
.features
& (1 << VIRTIO_RING_F_EVENT_IDX
))) {
55 features
&= ~(1 << VIRTIO_RING_F_EVENT_IDX
);
57 if (!(net
->dev
.features
& (1 << VIRTIO_NET_F_MRG_RXBUF
))) {
58 features
&= ~(1 << VIRTIO_NET_F_MRG_RXBUF
);
63 void vhost_net_ack_features(struct vhost_net
*net
, unsigned features
)
65 net
->dev
.acked_features
= net
->dev
.backend_features
;
66 if (features
& (1 << VIRTIO_F_NOTIFY_ON_EMPTY
)) {
67 net
->dev
.acked_features
|= (1 << VIRTIO_F_NOTIFY_ON_EMPTY
);
69 if (features
& (1 << VIRTIO_RING_F_INDIRECT_DESC
)) {
70 net
->dev
.acked_features
|= (1 << VIRTIO_RING_F_INDIRECT_DESC
);
72 if (features
& (1 << VIRTIO_RING_F_EVENT_IDX
)) {
73 net
->dev
.acked_features
|= (1 << VIRTIO_RING_F_EVENT_IDX
);
75 if (features
& (1 << VIRTIO_NET_F_MRG_RXBUF
)) {
76 net
->dev
.acked_features
|= (1 << VIRTIO_NET_F_MRG_RXBUF
);
80 static int vhost_net_get_fd(VLANClientState
*backend
)
82 switch (backend
->info
->type
) {
83 case NET_CLIENT_TYPE_TAP
:
84 return tap_get_fd(backend
);
86 fprintf(stderr
, "vhost-net requires tap backend\n");
91 struct vhost_net
*vhost_net_init(VLANClientState
*backend
, int devfd
,
95 struct vhost_net
*net
= g_malloc(sizeof *net
);
97 fprintf(stderr
, "vhost-net requires backend to be setup\n");
100 r
= vhost_net_get_fd(backend
);
105 net
->dev
.backend_features
= tap_has_vnet_hdr(backend
) ? 0 :
106 (1 << VHOST_NET_F_VIRTIO_NET_HDR
);
109 r
= vhost_dev_init(&net
->dev
, devfd
, force
);
113 if (!tap_has_vnet_hdr_len(backend
,
114 sizeof(struct virtio_net_hdr_mrg_rxbuf
))) {
115 net
->dev
.features
&= ~(1 << VIRTIO_NET_F_MRG_RXBUF
);
117 if (~net
->dev
.features
& net
->dev
.backend_features
) {
118 fprintf(stderr
, "vhost lacks feature mask %" PRIu64
" for backend\n",
119 (uint64_t)(~net
->dev
.features
& net
->dev
.backend_features
));
120 vhost_dev_cleanup(&net
->dev
);
124 /* Set sane init value. Override when guest acks. */
125 vhost_net_ack_features(net
, 0);
132 bool vhost_net_query(VHostNetState
*net
, VirtIODevice
*dev
)
134 return vhost_dev_query(&net
->dev
, dev
);
137 int vhost_net_start(struct vhost_net
*net
,
140 struct vhost_vring_file file
= { };
144 net
->dev
.vqs
= net
->vqs
;
146 r
= vhost_dev_enable_notifiers(&net
->dev
, dev
);
150 if (net
->dev
.acked_features
& (1 << VIRTIO_NET_F_MRG_RXBUF
)) {
151 tap_set_vnet_hdr_len(net
->vc
,
152 sizeof(struct virtio_net_hdr_mrg_rxbuf
));
155 r
= vhost_dev_start(&net
->dev
, dev
);
160 net
->vc
->info
->poll(net
->vc
, false);
161 qemu_set_fd_handler(net
->backend
, NULL
, NULL
, NULL
);
162 file
.fd
= net
->backend
;
163 for (file
.index
= 0; file
.index
< net
->dev
.nvqs
; ++file
.index
) {
164 r
= ioctl(net
->dev
.control
, VHOST_NET_SET_BACKEND
, &file
);
173 while (file
.index
-- > 0) {
174 int r
= ioctl(net
->dev
.control
, VHOST_NET_SET_BACKEND
, &file
);
177 net
->vc
->info
->poll(net
->vc
, true);
178 vhost_dev_stop(&net
->dev
, dev
);
179 if (net
->dev
.acked_features
& (1 << VIRTIO_NET_F_MRG_RXBUF
)) {
180 tap_set_vnet_hdr_len(net
->vc
, sizeof(struct virtio_net_hdr
));
183 vhost_dev_disable_notifiers(&net
->dev
, dev
);
188 void vhost_net_stop(struct vhost_net
*net
,
191 struct vhost_vring_file file
= { .fd
= -1 };
193 for (file
.index
= 0; file
.index
< net
->dev
.nvqs
; ++file
.index
) {
194 int r
= ioctl(net
->dev
.control
, VHOST_NET_SET_BACKEND
, &file
);
197 net
->vc
->info
->poll(net
->vc
, true);
198 vhost_dev_stop(&net
->dev
, dev
);
199 if (net
->dev
.acked_features
& (1 << VIRTIO_NET_F_MRG_RXBUF
)) {
200 tap_set_vnet_hdr_len(net
->vc
, sizeof(struct virtio_net_hdr
));
202 vhost_dev_disable_notifiers(&net
->dev
, dev
);
205 void vhost_net_cleanup(struct vhost_net
*net
)
207 vhost_dev_cleanup(&net
->dev
);
208 if (net
->dev
.acked_features
& (1 << VIRTIO_NET_F_MRG_RXBUF
)) {
209 tap_set_vnet_hdr_len(net
->vc
, sizeof(struct virtio_net_hdr
));
214 struct vhost_net
*vhost_net_init(VLANClientState
*backend
, int devfd
,
217 error_report("vhost-net support is not compiled in");
221 bool vhost_net_query(VHostNetState
*net
, VirtIODevice
*dev
)
226 int vhost_net_start(struct vhost_net
*net
,
231 void vhost_net_stop(struct vhost_net
*net
,
236 void vhost_net_cleanup(struct vhost_net
*net
)
240 unsigned vhost_net_get_features(struct vhost_net
*net
, unsigned features
)
244 void vhost_net_ack_features(struct vhost_net
*net
, unsigned features
)