xref: /qemu/hw/net/vhost_net.c (revision 81647a655fa4ff99fd5748363a174edd87a40950)
1 /*
2  * vhost-net support
3  *
4  * Copyright Red Hat, Inc. 2010
5  *
6  * Authors:
7  *  Michael S. Tsirkin <mst@redhat.com>
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  * Contributions after 2012-01-13 are licensed under the terms of the
13  * GNU GPL, version 2 or (at your option) any later version.
14  */
15 
16 #include "net/net.h"
17 #include "net/tap.h"
18 
19 #include "hw/virtio/virtio-net.h"
20 #include "net/vhost_net.h"
21 #include "qemu/error-report.h"
22 
23 #include "config.h"
24 
25 #ifdef CONFIG_VHOST_NET
26 #include <linux/vhost.h>
27 #include <sys/socket.h>
28 #include <linux/kvm.h>
29 #include <fcntl.h>
30 #include <sys/ioctl.h>
31 #include <linux/virtio_ring.h>
32 #include <netpacket/packet.h>
33 #include <net/ethernet.h>
34 #include <net/if.h>
35 #include <netinet/in.h>
36 
37 #include <stdio.h>
38 
39 #include "hw/virtio/vhost.h"
40 #include "hw/virtio/virtio-bus.h"
41 
42 struct vhost_net {
43     struct vhost_dev dev;
44     struct vhost_virtqueue vqs[2];
45     int backend;
46     NetClientState *nc;
47 };
48 
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
56 };
57 
58 static const int *vhost_net_get_feature_bits(struct vhost_net *net)
59 {
60     const int *feature_bits = 0;
61 
62     switch (net->nc->info->type) {
63     case NET_CLIENT_OPTIONS_KIND_TAP:
64         feature_bits = kernel_feature_bits;
65         break;
66     default:
67         error_report("Feature bits not defined for this type: %d",
68                 net->nc->info->type);
69         break;
70     }
71 
72     return feature_bits;
73 }
74 
75 unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
76 {
77     return vhost_get_features(&net->dev, vhost_net_get_feature_bits(net),
78             features);
79 }
80 
81 void vhost_net_ack_features(struct vhost_net *net, unsigned features)
82 {
83     vhost_ack_features(&net->dev, vhost_net_get_feature_bits(net), features);
84 }
85 
86 static int vhost_net_get_fd(NetClientState *backend)
87 {
88     switch (backend->info->type) {
89     case NET_CLIENT_OPTIONS_KIND_TAP:
90         return tap_get_fd(backend);
91     default:
92         fprintf(stderr, "vhost-net requires tap backend\n");
93         return -EBADFD;
94     }
95 }
96 
97 struct vhost_net *vhost_net_init(VhostNetOptions *options)
98 {
99     int r;
100     struct vhost_net *net = g_malloc(sizeof *net);
101 
102     if (!options->net_backend) {
103         fprintf(stderr, "vhost-net requires net backend to be setup\n");
104         goto fail;
105     }
106 
107     r = vhost_net_get_fd(options->net_backend);
108     if (r < 0) {
109         goto fail;
110     }
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);
114     net->backend = r;
115 
116     net->dev.nvqs = 2;
117     net->dev.vqs = net->vqs;
118 
119     r = vhost_dev_init(&net->dev, options->opaque,
120                        options->force);
121     if (r < 0) {
122         goto fail;
123     }
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);
127     }
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);
132         goto fail;
133     }
134 
135     /* Set sane init value. Override when guest acks. */
136     vhost_net_ack_features(net, 0);
137     return net;
138 fail:
139     g_free(net);
140     return NULL;
141 }
142 
143 bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
144 {
145     return vhost_dev_query(&net->dev, dev);
146 }
147 
148 static int vhost_net_start_one(struct vhost_net *net,
149                                VirtIODevice *dev,
150                                int vq_index)
151 {
152     struct vhost_vring_file file = { };
153     int r;
154 
155     if (net->dev.started) {
156         return 0;
157     }
158 
159     net->dev.nvqs = 2;
160     net->dev.vqs = net->vqs;
161     net->dev.vq_index = vq_index;
162 
163     r = vhost_dev_enable_notifiers(&net->dev, dev);
164     if (r < 0) {
165         goto fail_notifiers;
166     }
167 
168     r = vhost_dev_start(&net->dev, dev);
169     if (r < 0) {
170         goto fail_start;
171     }
172 
173     if (net->nc->info->poll) {
174         net->nc->info->poll(net->nc, false);
175     }
176 
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);
181         if (r < 0) {
182             r = -errno;
183             goto fail;
184         }
185     }
186     return 0;
187 fail:
188     file.fd = -1;
189     while (file.index-- > 0) {
190         int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
191         assert(r >= 0);
192     }
193     if (net->nc->info->poll) {
194         net->nc->info->poll(net->nc, true);
195     }
196     vhost_dev_stop(&net->dev, dev);
197 fail_start:
198     vhost_dev_disable_notifiers(&net->dev, dev);
199 fail_notifiers:
200     return r;
201 }
202 
203 static void vhost_net_stop_one(struct vhost_net *net,
204                                VirtIODevice *dev)
205 {
206     struct vhost_vring_file file = { .fd = -1 };
207 
208     if (!net->dev.started) {
209         return;
210     }
211 
212     for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
213         int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
214         assert(r >= 0);
215     }
216     if (net->nc->info->poll) {
217         net->nc->info->poll(net->nc, true);
218     }
219     vhost_dev_stop(&net->dev, dev);
220     vhost_dev_disable_notifiers(&net->dev, dev);
221 }
222 
223 int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
224                     int total_queues)
225 {
226     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
227     VirtioBusState *vbus = VIRTIO_BUS(qbus);
228     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
229     int r, i = 0;
230 
231     if (!k->set_guest_notifiers) {
232         error_report("binding does not support guest notifiers");
233         r = -ENOSYS;
234         goto err;
235     }
236 
237     for (i = 0; i < total_queues; i++) {
238         r = vhost_net_start_one(get_vhost_net(ncs[i].peer), dev, i * 2);
239 
240         if (r < 0) {
241             goto err;
242         }
243     }
244 
245     r = k->set_guest_notifiers(qbus->parent, total_queues * 2, true);
246     if (r < 0) {
247         error_report("Error binding guest notifier: %d", -r);
248         goto err;
249     }
250 
251     return 0;
252 
253 err:
254     while (--i >= 0) {
255         vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
256     }
257     return r;
258 }
259 
260 void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
261                     int total_queues)
262 {
263     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
264     VirtioBusState *vbus = VIRTIO_BUS(qbus);
265     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
266     int i, r;
267 
268     r = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
269     if (r < 0) {
270         fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r);
271         fflush(stderr);
272     }
273     assert(r >= 0);
274 
275     for (i = 0; i < total_queues; i++) {
276         vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
277     }
278 }
279 
280 void vhost_net_cleanup(struct vhost_net *net)
281 {
282     vhost_dev_cleanup(&net->dev);
283     g_free(net);
284 }
285 
286 bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
287 {
288     return vhost_virtqueue_pending(&net->dev, idx);
289 }
290 
291 void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
292                               int idx, bool mask)
293 {
294     vhost_virtqueue_mask(&net->dev, dev, idx, mask);
295 }
296 
297 VHostNetState *get_vhost_net(NetClientState *nc)
298 {
299     VHostNetState *vhost_net = 0;
300 
301     if (!nc) {
302         return 0;
303     }
304 
305     switch (nc->info->type) {
306     case NET_CLIENT_OPTIONS_KIND_TAP:
307         vhost_net = tap_get_vhost_net(nc);
308         break;
309     default:
310         break;
311     }
312 
313     return vhost_net;
314 }
315 #else
316 struct vhost_net *vhost_net_init(VhostNetOptions *options)
317 {
318     error_report("vhost-net support is not compiled in");
319     return NULL;
320 }
321 
322 bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
323 {
324     return false;
325 }
326 
327 int vhost_net_start(VirtIODevice *dev,
328                     NetClientState *ncs,
329                     int total_queues)
330 {
331     return -ENOSYS;
332 }
333 void vhost_net_stop(VirtIODevice *dev,
334                     NetClientState *ncs,
335                     int total_queues)
336 {
337 }
338 
339 void vhost_net_cleanup(struct vhost_net *net)
340 {
341 }
342 
343 unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
344 {
345     return features;
346 }
347 void vhost_net_ack_features(struct vhost_net *net, unsigned features)
348 {
349 }
350 
351 bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
352 {
353     return false;
354 }
355 
356 void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
357                               int idx, bool mask)
358 {
359 }
360 
361 VHostNetState *get_vhost_net(NetClientState *nc)
362 {
363     return 0;
364 }
365 #endif
366