]> Git Repo - qemu.git/blob - hw/vhost_net.c
SPARC: Emulation of GRLIB APB UART
[qemu.git] / hw / vhost_net.c
1 /*
2  * vhost-net support
3  *
4  * Copyright Red Hat, Inc. 2010
5  *
6  * Authors:
7  *  Michael S. Tsirkin <[email protected]>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  */
12
13 #include "net.h"
14 #include "net/tap.h"
15
16 #include "virtio-net.h"
17 #include "vhost_net.h"
18
19 #include "config.h"
20
21 #ifdef CONFIG_VHOST_NET
22 #include <linux/vhost.h>
23 #include <sys/socket.h>
24 #include <linux/kvm.h>
25 #include <fcntl.h>
26 #include <sys/ioctl.h>
27 #include <linux/virtio_ring.h>
28 #include <netpacket/packet.h>
29 #include <net/ethernet.h>
30 #include <net/if.h>
31 #include <netinet/in.h>
32
33 #include <stdio.h>
34
35 #include "vhost.h"
36
37 struct vhost_net {
38     struct vhost_dev dev;
39     struct vhost_virtqueue vqs[2];
40     int backend;
41     VLANClientState *vc;
42 };
43
44 unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
45 {
46     /* Clear features not supported by host kernel. */
47     if (!(net->dev.features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY))) {
48         features &= ~(1 << VIRTIO_F_NOTIFY_ON_EMPTY);
49     }
50     if (!(net->dev.features & (1 << VIRTIO_RING_F_INDIRECT_DESC))) {
51         features &= ~(1 << VIRTIO_RING_F_INDIRECT_DESC);
52     }
53     if (!(net->dev.features & (1 << VIRTIO_NET_F_MRG_RXBUF))) {
54         features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF);
55     }
56     return features;
57 }
58
59 void vhost_net_ack_features(struct vhost_net *net, unsigned features)
60 {
61     net->dev.acked_features = net->dev.backend_features;
62     if (features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY)) {
63         net->dev.acked_features |= (1 << VIRTIO_F_NOTIFY_ON_EMPTY);
64     }
65     if (features & (1 << VIRTIO_RING_F_INDIRECT_DESC)) {
66         net->dev.acked_features |= (1 << VIRTIO_RING_F_INDIRECT_DESC);
67     }
68     if (features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
69         net->dev.acked_features |= (1 << VIRTIO_NET_F_MRG_RXBUF);
70     }
71 }
72
73 static int vhost_net_get_fd(VLANClientState *backend)
74 {
75     switch (backend->info->type) {
76     case NET_CLIENT_TYPE_TAP:
77         return tap_get_fd(backend);
78     default:
79         fprintf(stderr, "vhost-net requires tap backend\n");
80         return -EBADFD;
81     }
82 }
83
84 struct vhost_net *vhost_net_init(VLANClientState *backend, int devfd)
85 {
86     int r;
87     struct vhost_net *net = qemu_malloc(sizeof *net);
88     if (!backend) {
89         fprintf(stderr, "vhost-net requires backend to be setup\n");
90         goto fail;
91     }
92     r = vhost_net_get_fd(backend);
93     if (r < 0) {
94         goto fail;
95     }
96     net->vc = backend;
97     net->dev.backend_features = tap_has_vnet_hdr(backend) ? 0 :
98         (1 << VHOST_NET_F_VIRTIO_NET_HDR);
99     net->backend = r;
100
101     r = vhost_dev_init(&net->dev, devfd);
102     if (r < 0) {
103         goto fail;
104     }
105     if (!tap_has_vnet_hdr_len(backend,
106                               sizeof(struct virtio_net_hdr_mrg_rxbuf))) {
107         net->dev.features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF);
108     }
109     if (~net->dev.features & net->dev.backend_features) {
110         fprintf(stderr, "vhost lacks feature mask %" PRIu64 " for backend\n",
111                 (uint64_t)(~net->dev.features & net->dev.backend_features));
112         vhost_dev_cleanup(&net->dev);
113         goto fail;
114     }
115
116     /* Set sane init value. Override when guest acks. */
117     vhost_net_ack_features(net, 0);
118     return net;
119 fail:
120     qemu_free(net);
121     return NULL;
122 }
123
124 int vhost_net_start(struct vhost_net *net,
125                     VirtIODevice *dev)
126 {
127     struct vhost_vring_file file = { };
128     int r;
129     if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
130         tap_set_vnet_hdr_len(net->vc,
131                              sizeof(struct virtio_net_hdr_mrg_rxbuf));
132     }
133
134     net->dev.nvqs = 2;
135     net->dev.vqs = net->vqs;
136     r = vhost_dev_start(&net->dev, dev);
137     if (r < 0) {
138         return r;
139     }
140
141     net->vc->info->poll(net->vc, false);
142     qemu_set_fd_handler(net->backend, NULL, NULL, NULL);
143     file.fd = net->backend;
144     for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
145         r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
146         if (r < 0) {
147             r = -errno;
148             goto fail;
149         }
150     }
151     return 0;
152 fail:
153     file.fd = -1;
154     while (file.index-- > 0) {
155         int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
156         assert(r >= 0);
157     }
158     net->vc->info->poll(net->vc, true);
159     vhost_dev_stop(&net->dev, dev);
160     if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
161         tap_set_vnet_hdr_len(net->vc, sizeof(struct virtio_net_hdr));
162     }
163     return r;
164 }
165
166 void vhost_net_stop(struct vhost_net *net,
167                     VirtIODevice *dev)
168 {
169     struct vhost_vring_file file = { .fd = -1 };
170
171     for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
172         int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
173         assert(r >= 0);
174     }
175     net->vc->info->poll(net->vc, true);
176     vhost_dev_stop(&net->dev, dev);
177     if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
178         tap_set_vnet_hdr_len(net->vc, sizeof(struct virtio_net_hdr));
179     }
180 }
181
182 void vhost_net_cleanup(struct vhost_net *net)
183 {
184     vhost_dev_cleanup(&net->dev);
185     if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
186         tap_set_vnet_hdr_len(net->vc, sizeof(struct virtio_net_hdr));
187     }
188     qemu_free(net);
189 }
190 #else
191 struct vhost_net *vhost_net_init(VLANClientState *backend, int devfd)
192 {
193         return NULL;
194 }
195
196 int vhost_net_start(struct vhost_net *net,
197                     VirtIODevice *dev)
198 {
199         return -ENOSYS;
200 }
201 void vhost_net_stop(struct vhost_net *net,
202                     VirtIODevice *dev)
203 {
204 }
205
206 void vhost_net_cleanup(struct vhost_net *net)
207 {
208 }
209
210 unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
211 {
212         return features;
213 }
214 void vhost_net_ack_features(struct vhost_net *net, unsigned features)
215 {
216 }
217 #endif
This page took 0.03631 seconds and 4 git commands to generate.