]> Git Repo - qemu.git/blob - hw/net/vhost_net.c
vhost: don't send RESET_OWNER at stop
[qemu.git] / hw / net / 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  * 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 #include "net/vhost-user.h"
19
20 #include "hw/virtio/virtio-net.h"
21 #include "net/vhost_net.h"
22 #include "qemu/error-report.h"
23
24 #include "config.h"
25
26 #ifdef CONFIG_VHOST_NET
27 #include <linux/vhost.h>
28 #include <sys/socket.h>
29 #include <linux/kvm.h>
30 #include <fcntl.h>
31 #include <netpacket/packet.h>
32 #include <net/ethernet.h>
33 #include <net/if.h>
34 #include <netinet/in.h>
35
36 #include <stdio.h>
37
38 #include "standard-headers/linux/virtio_ring.h"
39 #include "hw/virtio/vhost.h"
40 #include "hw/virtio/virtio-bus.h"
41 #include "hw/virtio/virtio-access.h"
42
43 struct vhost_net {
44     struct vhost_dev dev;
45     struct vhost_virtqueue vqs[2];
46     int backend;
47     NetClientState *nc;
48 };
49
50 /* Features supported by host kernel. */
51 static const int kernel_feature_bits[] = {
52     VIRTIO_F_NOTIFY_ON_EMPTY,
53     VIRTIO_RING_F_INDIRECT_DESC,
54     VIRTIO_RING_F_EVENT_IDX,
55     VIRTIO_NET_F_MRG_RXBUF,
56     VIRTIO_F_VERSION_1,
57     VHOST_INVALID_FEATURE_BIT
58 };
59
60 /* Features supported by others. */
61 static const int user_feature_bits[] = {
62     VIRTIO_F_NOTIFY_ON_EMPTY,
63     VIRTIO_RING_F_INDIRECT_DESC,
64     VIRTIO_RING_F_EVENT_IDX,
65
66     VIRTIO_F_ANY_LAYOUT,
67     VIRTIO_F_VERSION_1,
68     VIRTIO_NET_F_CSUM,
69     VIRTIO_NET_F_GUEST_CSUM,
70     VIRTIO_NET_F_GSO,
71     VIRTIO_NET_F_GUEST_TSO4,
72     VIRTIO_NET_F_GUEST_TSO6,
73     VIRTIO_NET_F_GUEST_ECN,
74     VIRTIO_NET_F_GUEST_UFO,
75     VIRTIO_NET_F_HOST_TSO4,
76     VIRTIO_NET_F_HOST_TSO6,
77     VIRTIO_NET_F_HOST_ECN,
78     VIRTIO_NET_F_HOST_UFO,
79     VIRTIO_NET_F_MRG_RXBUF,
80     VIRTIO_NET_F_STATUS,
81     VIRTIO_NET_F_CTRL_VQ,
82     VIRTIO_NET_F_CTRL_RX,
83     VIRTIO_NET_F_CTRL_VLAN,
84     VIRTIO_NET_F_CTRL_RX_EXTRA,
85     VIRTIO_NET_F_CTRL_MAC_ADDR,
86     VIRTIO_NET_F_CTRL_GUEST_OFFLOADS,
87
88     VIRTIO_NET_F_GUEST_ANNOUNCE,
89
90     VIRTIO_NET_F_MQ,
91
92     VHOST_INVALID_FEATURE_BIT
93 };
94
95 static const int *vhost_net_get_feature_bits(struct vhost_net *net)
96 {
97     const int *feature_bits = 0;
98
99     switch (net->nc->info->type) {
100     case NET_CLIENT_OPTIONS_KIND_TAP:
101         feature_bits = kernel_feature_bits;
102         break;
103     case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
104         feature_bits = user_feature_bits;
105         break;
106     default:
107         error_report("Feature bits not defined for this type: %d",
108                 net->nc->info->type);
109         break;
110     }
111
112     return feature_bits;
113 }
114
115 uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features)
116 {
117     return vhost_get_features(&net->dev, vhost_net_get_feature_bits(net),
118             features);
119 }
120
121 void vhost_net_ack_features(struct vhost_net *net, uint64_t features)
122 {
123     net->dev.acked_features = net->dev.backend_features;
124     vhost_ack_features(&net->dev, vhost_net_get_feature_bits(net), features);
125 }
126
127 uint64_t vhost_net_get_max_queues(VHostNetState *net)
128 {
129     return net->dev.max_queues;
130 }
131
132 static int vhost_net_get_fd(NetClientState *backend)
133 {
134     switch (backend->info->type) {
135     case NET_CLIENT_OPTIONS_KIND_TAP:
136         return tap_get_fd(backend);
137     default:
138         fprintf(stderr, "vhost-net requires tap backend\n");
139         return -EBADFD;
140     }
141 }
142
143 struct vhost_net *vhost_net_init(VhostNetOptions *options)
144 {
145     int r;
146     bool backend_kernel = options->backend_type == VHOST_BACKEND_TYPE_KERNEL;
147     struct vhost_net *net = g_malloc(sizeof *net);
148
149     if (!options->net_backend) {
150         fprintf(stderr, "vhost-net requires net backend to be setup\n");
151         goto fail;
152     }
153     net->nc = options->net_backend;
154
155     net->dev.max_queues = 1;
156     net->dev.nvqs = 2;
157     net->dev.vqs = net->vqs;
158
159     if (backend_kernel) {
160         r = vhost_net_get_fd(options->net_backend);
161         if (r < 0) {
162             goto fail;
163         }
164         net->dev.backend_features = qemu_has_vnet_hdr(options->net_backend)
165             ? 0 : (1ULL << VHOST_NET_F_VIRTIO_NET_HDR);
166         net->backend = r;
167         net->dev.protocol_features = 0;
168     } else {
169         net->dev.backend_features = 0;
170         net->dev.protocol_features = 0;
171         net->backend = -1;
172
173         /* vhost-user needs vq_index to initiate a specific queue pair */
174         net->dev.vq_index = net->nc->queue_index * net->dev.nvqs;
175     }
176
177     r = vhost_dev_init(&net->dev, options->opaque,
178                        options->backend_type);
179     if (r < 0) {
180         goto fail;
181     }
182     if (backend_kernel) {
183         if (!qemu_has_vnet_hdr_len(options->net_backend,
184                                sizeof(struct virtio_net_hdr_mrg_rxbuf))) {
185             net->dev.features &= ~(1ULL << VIRTIO_NET_F_MRG_RXBUF);
186         }
187         if (~net->dev.features & net->dev.backend_features) {
188             fprintf(stderr, "vhost lacks feature mask %" PRIu64
189                    " for backend\n",
190                    (uint64_t)(~net->dev.features & net->dev.backend_features));
191             vhost_dev_cleanup(&net->dev);
192             goto fail;
193         }
194     }
195     /* Set sane init value. Override when guest acks. */
196     vhost_net_ack_features(net, 0);
197     return net;
198 fail:
199     g_free(net);
200     return NULL;
201 }
202
203 static void vhost_net_set_vq_index(struct vhost_net *net, int vq_index)
204 {
205     net->dev.vq_index = vq_index;
206 }
207
208 static int vhost_net_set_vnet_endian(VirtIODevice *dev, NetClientState *peer,
209                                      bool set)
210 {
211     int r = 0;
212
213     if (virtio_vdev_has_feature(dev, VIRTIO_F_VERSION_1) ||
214         (virtio_legacy_is_cross_endian(dev) && !virtio_is_big_endian(dev))) {
215         r = qemu_set_vnet_le(peer, set);
216         if (r) {
217             error_report("backend does not support LE vnet headers");
218         }
219     } else if (virtio_legacy_is_cross_endian(dev)) {
220         r = qemu_set_vnet_be(peer, set);
221         if (r) {
222             error_report("backend does not support BE vnet headers");
223         }
224     }
225
226     return r;
227 }
228
229 static int vhost_net_start_one(struct vhost_net *net,
230                                VirtIODevice *dev)
231 {
232     struct vhost_vring_file file = { };
233     int r;
234
235     net->dev.nvqs = 2;
236     net->dev.vqs = net->vqs;
237
238     r = vhost_dev_enable_notifiers(&net->dev, dev);
239     if (r < 0) {
240         goto fail_notifiers;
241     }
242
243     r = vhost_dev_start(&net->dev, dev);
244     if (r < 0) {
245         goto fail_start;
246     }
247
248     if (net->nc->info->poll) {
249         net->nc->info->poll(net->nc, false);
250     }
251
252     if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP) {
253         qemu_set_fd_handler(net->backend, NULL, NULL, NULL);
254         file.fd = net->backend;
255         for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
256             const VhostOps *vhost_ops = net->dev.vhost_ops;
257             r = vhost_ops->vhost_net_set_backend(&net->dev, &file);
258             if (r < 0) {
259                 r = -errno;
260                 goto fail;
261             }
262         }
263     }
264     return 0;
265 fail:
266     file.fd = -1;
267     if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP) {
268         while (file.index-- > 0) {
269             const VhostOps *vhost_ops = net->dev.vhost_ops;
270             int r = vhost_ops->vhost_net_set_backend(&net->dev, &file);
271             assert(r >= 0);
272         }
273     }
274     if (net->nc->info->poll) {
275         net->nc->info->poll(net->nc, true);
276     }
277     vhost_dev_stop(&net->dev, dev);
278 fail_start:
279     vhost_dev_disable_notifiers(&net->dev, dev);
280 fail_notifiers:
281     return r;
282 }
283
284 static void vhost_net_stop_one(struct vhost_net *net,
285                                VirtIODevice *dev)
286 {
287     struct vhost_vring_file file = { .fd = -1 };
288
289     if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP) {
290         for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
291             const VhostOps *vhost_ops = net->dev.vhost_ops;
292             int r = vhost_ops->vhost_net_set_backend(&net->dev, &file);
293             assert(r >= 0);
294         }
295     }
296     if (net->nc->info->poll) {
297         net->nc->info->poll(net->nc, true);
298     }
299     vhost_dev_stop(&net->dev, dev);
300     vhost_dev_disable_notifiers(&net->dev, dev);
301 }
302
303 int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
304                     int total_queues)
305 {
306     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
307     VirtioBusState *vbus = VIRTIO_BUS(qbus);
308     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
309     int r, e, i;
310
311     if (!k->set_guest_notifiers) {
312         error_report("binding does not support guest notifiers");
313         r = -ENOSYS;
314         goto err;
315     }
316
317     r = vhost_net_set_vnet_endian(dev, ncs[0].peer, true);
318     if (r < 0) {
319         goto err;
320     }
321
322     for (i = 0; i < total_queues; i++) {
323         vhost_net_set_vq_index(get_vhost_net(ncs[i].peer), i * 2);
324     }
325
326     r = k->set_guest_notifiers(qbus->parent, total_queues * 2, true);
327     if (r < 0) {
328         error_report("Error binding guest notifier: %d", -r);
329         goto err_endian;
330     }
331
332     for (i = 0; i < total_queues; i++) {
333         r = vhost_net_start_one(get_vhost_net(ncs[i].peer), dev);
334
335         if (r < 0) {
336             goto err_start;
337         }
338     }
339
340     return 0;
341
342 err_start:
343     while (--i >= 0) {
344         vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
345     }
346     e = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
347     if (e < 0) {
348         fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);
349         fflush(stderr);
350     }
351 err_endian:
352     vhost_net_set_vnet_endian(dev, ncs[0].peer, false);
353 err:
354     return r;
355 }
356
357 void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
358                     int total_queues)
359 {
360     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
361     VirtioBusState *vbus = VIRTIO_BUS(qbus);
362     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
363     int i, r;
364
365     for (i = 0; i < total_queues; i++) {
366         vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
367     }
368
369     r = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
370     if (r < 0) {
371         fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r);
372         fflush(stderr);
373     }
374     assert(r >= 0);
375
376     assert(vhost_net_set_vnet_endian(dev, ncs[0].peer, false) >= 0);
377 }
378
379 void vhost_net_cleanup(struct vhost_net *net)
380 {
381     vhost_dev_cleanup(&net->dev);
382     g_free(net);
383 }
384
385 int vhost_net_notify_migration_done(struct vhost_net *net, char* mac_addr)
386 {
387     const VhostOps *vhost_ops = net->dev.vhost_ops;
388     int r = -1;
389
390     if (vhost_ops->vhost_migration_done) {
391         r = vhost_ops->vhost_migration_done(&net->dev, mac_addr);
392     }
393
394     return r;
395 }
396
397 bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
398 {
399     return vhost_virtqueue_pending(&net->dev, idx);
400 }
401
402 void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
403                               int idx, bool mask)
404 {
405     vhost_virtqueue_mask(&net->dev, dev, idx, mask);
406 }
407
408 VHostNetState *get_vhost_net(NetClientState *nc)
409 {
410     VHostNetState *vhost_net = 0;
411
412     if (!nc) {
413         return 0;
414     }
415
416     switch (nc->info->type) {
417     case NET_CLIENT_OPTIONS_KIND_TAP:
418         vhost_net = tap_get_vhost_net(nc);
419         break;
420     case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
421         vhost_net = vhost_user_get_vhost_net(nc);
422         break;
423     default:
424         break;
425     }
426
427     return vhost_net;
428 }
429
430 int vhost_set_vring_enable(NetClientState *nc, int enable)
431 {
432     VHostNetState *net = get_vhost_net(nc);
433     const VhostOps *vhost_ops = net->dev.vhost_ops;
434
435     if (vhost_ops->vhost_set_vring_enable) {
436         return vhost_ops->vhost_set_vring_enable(&net->dev, enable);
437     }
438
439     return 0;
440 }
441
442 #else
443 uint64_t vhost_net_get_max_queues(VHostNetState *net)
444 {
445     return 1;
446 }
447
448 struct vhost_net *vhost_net_init(VhostNetOptions *options)
449 {
450     error_report("vhost-net support is not compiled in");
451     return NULL;
452 }
453
454 int vhost_net_start(VirtIODevice *dev,
455                     NetClientState *ncs,
456                     int total_queues)
457 {
458     return -ENOSYS;
459 }
460 void vhost_net_stop(VirtIODevice *dev,
461                     NetClientState *ncs,
462                     int total_queues)
463 {
464 }
465
466 void vhost_net_cleanup(struct vhost_net *net)
467 {
468 }
469
470 uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features)
471 {
472     return features;
473 }
474 void vhost_net_ack_features(struct vhost_net *net, uint64_t features)
475 {
476 }
477
478 bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
479 {
480     return false;
481 }
482
483 void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
484                               int idx, bool mask)
485 {
486 }
487
488 int vhost_net_notify_migration_done(struct vhost_net *net, char* mac_addr)
489 {
490     return -1;
491 }
492
493 VHostNetState *get_vhost_net(NetClientState *nc)
494 {
495     return 0;
496 }
497
498 int vhost_set_vring_enable(NetClientState *nc, int enable)
499 {
500     return 0;
501 }
502 #endif
This page took 0.053457 seconds and 4 git commands to generate.