blob: b21e7a434f90b788f408cf9de9e1bd81f3c821e8 [file] [log] [blame]
Michael S. Tsirkind5970052010-03-17 13:08:17 +02001/*
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.
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010011 *
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.
Michael S. Tsirkind5970052010-03-17 13:08:17 +020014 */
15
Paolo Bonzini1422e322012-10-24 08:43:34 +020016#include "net/net.h"
Michael S. Tsirkind5970052010-03-17 13:08:17 +020017#include "net/tap.h"
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +030018#include "net/vhost-user.h"
Michael S. Tsirkind5970052010-03-17 13:08:17 +020019
Paolo Bonzini0d09e412013-02-05 17:06:20 +010020#include "hw/virtio/virtio-net.h"
21#include "net/vhost_net.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010022#include "qemu/error-report.h"
Michael S. Tsirkind5970052010-03-17 13:08:17 +020023
24#include "config.h"
25
26#ifdef CONFIG_VHOST_NET
27#include <linux/vhost.h>
Michael S. Tsirkind5970052010-03-17 13:08:17 +020028#include <sys/socket.h>
29#include <linux/kvm.h>
30#include <fcntl.h>
Michael S. Tsirkind5970052010-03-17 13:08:17 +020031#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
Paolo Bonzini0d09e412013-02-05 17:06:20 +010039#include "hw/virtio/vhost.h"
KONRAD Frederic1c819442013-04-24 10:21:21 +020040#include "hw/virtio/virtio-bus.h"
Michael S. Tsirkind5970052010-03-17 13:08:17 +020041
42struct vhost_net {
43 struct vhost_dev dev;
44 struct vhost_virtqueue vqs[2];
45 int backend;
Stefan Hajnoczi35277d12012-07-24 16:35:14 +010046 NetClientState *nc;
Michael S. Tsirkind5970052010-03-17 13:08:17 +020047};
48
Nikolay Nikolaev2e6d46d2014-05-27 15:04:42 +030049/* Features supported by host kernel. */
50static 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
Nikolay Nikolaev5f4c01c2014-05-27 15:06:16 +030058/* Features supported by others. */
59const int user_feature_bits[] = {
60 VIRTIO_F_NOTIFY_ON_EMPTY,
61 VIRTIO_RING_F_INDIRECT_DESC,
62 VIRTIO_RING_F_EVENT_IDX,
63
64 VIRTIO_F_ANY_LAYOUT,
65 VIRTIO_NET_F_CSUM,
66 VIRTIO_NET_F_GUEST_CSUM,
67 VIRTIO_NET_F_GSO,
68 VIRTIO_NET_F_GUEST_TSO4,
69 VIRTIO_NET_F_GUEST_TSO6,
70 VIRTIO_NET_F_GUEST_ECN,
71 VIRTIO_NET_F_GUEST_UFO,
72 VIRTIO_NET_F_HOST_TSO4,
73 VIRTIO_NET_F_HOST_TSO6,
74 VIRTIO_NET_F_HOST_ECN,
75 VIRTIO_NET_F_HOST_UFO,
76 VIRTIO_NET_F_MRG_RXBUF,
77 VIRTIO_NET_F_STATUS,
78 VIRTIO_NET_F_CTRL_VQ,
79 VIRTIO_NET_F_CTRL_RX,
80 VIRTIO_NET_F_CTRL_VLAN,
81 VIRTIO_NET_F_CTRL_RX_EXTRA,
82 VIRTIO_NET_F_CTRL_MAC_ADDR,
83 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS,
84
85 VIRTIO_NET_F_MQ,
86
87 VHOST_INVALID_FEATURE_BIT
88};
89
Nikolay Nikolaev2e6d46d2014-05-27 15:04:42 +030090static const int *vhost_net_get_feature_bits(struct vhost_net *net)
91{
92 const int *feature_bits = 0;
93
94 switch (net->nc->info->type) {
95 case NET_CLIENT_OPTIONS_KIND_TAP:
96 feature_bits = kernel_feature_bits;
97 break;
Nikolay Nikolaev5f4c01c2014-05-27 15:06:16 +030098 case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
99 feature_bits = user_feature_bits;
100 break;
Nikolay Nikolaev2e6d46d2014-05-27 15:04:42 +0300101 default:
102 error_report("Feature bits not defined for this type: %d",
103 net->nc->info->type);
104 break;
105 }
106
107 return feature_bits;
108}
109
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200110unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
111{
Nikolay Nikolaev2e6d46d2014-05-27 15:04:42 +0300112 return vhost_get_features(&net->dev, vhost_net_get_feature_bits(net),
113 features);
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200114}
115
116void vhost_net_ack_features(struct vhost_net *net, unsigned features)
117{
Jason Wangb49ae912014-09-03 14:25:30 +0800118 net->dev.acked_features = net->dev.backend_features;
Nikolay Nikolaev2e6d46d2014-05-27 15:04:42 +0300119 vhost_ack_features(&net->dev, vhost_net_get_feature_bits(net), features);
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200120}
121
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100122static int vhost_net_get_fd(NetClientState *backend)
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200123{
124 switch (backend->info->type) {
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200125 case NET_CLIENT_OPTIONS_KIND_TAP:
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200126 return tap_get_fd(backend);
127 default:
128 fprintf(stderr, "vhost-net requires tap backend\n");
129 return -EBADFD;
130 }
131}
132
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300133struct vhost_net *vhost_net_init(VhostNetOptions *options)
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200134{
135 int r;
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300136 bool backend_kernel = options->backend_type == VHOST_BACKEND_TYPE_KERNEL;
Anthony Liguori7267c092011-08-20 22:09:37 -0500137 struct vhost_net *net = g_malloc(sizeof *net);
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300138
139 if (!options->net_backend) {
140 fprintf(stderr, "vhost-net requires net backend to be setup\n");
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200141 goto fail;
142 }
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300143
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300144 if (backend_kernel) {
145 r = vhost_net_get_fd(options->net_backend);
146 if (r < 0) {
147 goto fail;
148 }
149 net->dev.backend_features = qemu_has_vnet_hdr(options->net_backend)
150 ? 0 : (1 << VHOST_NET_F_VIRTIO_NET_HDR);
151 net->backend = r;
152 } else {
153 net->dev.backend_features = 0;
154 net->backend = -1;
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200155 }
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300156 net->nc = options->net_backend;
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200157
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +0200158 net->dev.nvqs = 2;
159 net->dev.vqs = net->vqs;
160
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300161 r = vhost_dev_init(&net->dev, options->opaque,
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300162 options->backend_type, options->force);
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200163 if (r < 0) {
164 goto fail;
165 }
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300166 if (!qemu_has_vnet_hdr_len(options->net_backend,
Stefan Hajnoczie3e48562014-02-20 12:14:08 +0100167 sizeof(struct virtio_net_hdr_mrg_rxbuf))) {
Michael S. Tsirkinca736c82010-07-16 11:43:43 +0300168 net->dev.features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF);
169 }
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300170 if (backend_kernel) {
171 if (~net->dev.features & net->dev.backend_features) {
172 fprintf(stderr, "vhost lacks feature mask %" PRIu64
173 " for backend\n",
174 (uint64_t)(~net->dev.features & net->dev.backend_features));
175 vhost_dev_cleanup(&net->dev);
176 goto fail;
177 }
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200178 }
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200179 /* Set sane init value. Override when guest acks. */
180 vhost_net_ack_features(net, 0);
181 return net;
182fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500183 g_free(net);
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200184 return NULL;
185}
186
mst@redhat.com5430a282011-02-01 22:13:42 +0200187bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
188{
189 return vhost_dev_query(&net->dev, dev);
190}
191
Jason Wangcd7d1d22014-08-19 12:56:29 +0800192static void vhost_net_set_vq_index(struct vhost_net *net, int vq_index)
193{
194 net->dev.vq_index = vq_index;
195}
196
Jason Wanga9f98bb2013-01-30 19:12:35 +0800197static int vhost_net_start_one(struct vhost_net *net,
Jason Wangcd7d1d22014-08-19 12:56:29 +0800198 VirtIODevice *dev)
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200199{
200 struct vhost_vring_file file = { };
201 int r;
Michael S. Tsirkinb0b3db72011-08-11 10:21:18 +0300202
Jason Wanga9f98bb2013-01-30 19:12:35 +0800203 net->dev.nvqs = 2;
204 net->dev.vqs = net->vqs;
Jason Wanga9f98bb2013-01-30 19:12:35 +0800205
Michael S. Tsirkinb0b3db72011-08-11 10:21:18 +0300206 r = vhost_dev_enable_notifiers(&net->dev, dev);
207 if (r < 0) {
208 goto fail_notifiers;
209 }
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200210
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200211 r = vhost_dev_start(&net->dev, dev);
212 if (r < 0) {
Michael S. Tsirkinb0b3db72011-08-11 10:21:18 +0300213 goto fail_start;
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200214 }
215
Nikolay Nikolaev212d69f2014-05-27 15:04:55 +0300216 if (net->nc->info->poll) {
217 net->nc->info->poll(net->nc, false);
218 }
219
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300220 if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP) {
221 qemu_set_fd_handler(net->backend, NULL, NULL, NULL);
222 file.fd = net->backend;
223 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
224 const VhostOps *vhost_ops = net->dev.vhost_ops;
225 r = vhost_ops->vhost_call(&net->dev, VHOST_NET_SET_BACKEND,
226 &file);
227 if (r < 0) {
228 r = -errno;
229 goto fail;
230 }
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200231 }
232 }
233 return 0;
234fail:
235 file.fd = -1;
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300236 if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP) {
237 while (file.index-- > 0) {
238 const VhostOps *vhost_ops = net->dev.vhost_ops;
239 int r = vhost_ops->vhost_call(&net->dev, VHOST_NET_SET_BACKEND,
240 &file);
241 assert(r >= 0);
242 }
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200243 }
Nikolay Nikolaev212d69f2014-05-27 15:04:55 +0300244 if (net->nc->info->poll) {
245 net->nc->info->poll(net->nc, true);
246 }
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200247 vhost_dev_stop(&net->dev, dev);
Michael S. Tsirkinb0b3db72011-08-11 10:21:18 +0300248fail_start:
249 vhost_dev_disable_notifiers(&net->dev, dev);
250fail_notifiers:
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200251 return r;
252}
253
Jason Wanga9f98bb2013-01-30 19:12:35 +0800254static void vhost_net_stop_one(struct vhost_net *net,
255 VirtIODevice *dev)
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200256{
257 struct vhost_vring_file file = { .fd = -1 };
258
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300259 if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP) {
260 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
261 const VhostOps *vhost_ops = net->dev.vhost_ops;
262 int r = vhost_ops->vhost_call(&net->dev, VHOST_NET_SET_BACKEND,
263 &file);
264 assert(r >= 0);
265 }
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200266 }
Nikolay Nikolaev212d69f2014-05-27 15:04:55 +0300267 if (net->nc->info->poll) {
268 net->nc->info->poll(net->nc, true);
269 }
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200270 vhost_dev_stop(&net->dev, dev);
Michael S. Tsirkinb0b3db72011-08-11 10:21:18 +0300271 vhost_dev_disable_notifiers(&net->dev, dev);
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200272}
273
Greg Kurz371df9f2014-06-24 19:55:03 +0200274static bool vhost_net_device_endian_ok(VirtIODevice *vdev)
275{
276#ifdef TARGET_IS_BIENDIAN
277#ifdef HOST_WORDS_BIGENDIAN
278 return virtio_is_big_endian(vdev);
279#else
280 return !virtio_is_big_endian(vdev);
281#endif
282#else
283 return true;
284#endif
285}
286
Jason Wanga9f98bb2013-01-30 19:12:35 +0800287int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
288 int total_queues)
289{
KONRAD Frederic1c819442013-04-24 10:21:21 +0200290 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
291 VirtioBusState *vbus = VIRTIO_BUS(qbus);
292 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
Jason Wangcd7d1d22014-08-19 12:56:29 +0800293 int r, e, i;
Jason Wanga9f98bb2013-01-30 19:12:35 +0800294
Greg Kurz371df9f2014-06-24 19:55:03 +0200295 if (!vhost_net_device_endian_ok(dev)) {
296 error_report("vhost-net does not support cross-endian");
297 r = -ENOSYS;
298 goto err;
299 }
300
KONRAD Frederic1c819442013-04-24 10:21:21 +0200301 if (!k->set_guest_notifiers) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100302 error_report("binding does not support guest notifiers");
Jason Wanga9f98bb2013-01-30 19:12:35 +0800303 r = -ENOSYS;
304 goto err;
305 }
306
307 for (i = 0; i < total_queues; i++) {
Jason Wangcd7d1d22014-08-19 12:56:29 +0800308 vhost_net_set_vq_index(get_vhost_net(ncs[i].peer), i * 2);
Jason Wanga9f98bb2013-01-30 19:12:35 +0800309 }
310
KONRAD Frederic1c819442013-04-24 10:21:21 +0200311 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, true);
Jason Wanga9f98bb2013-01-30 19:12:35 +0800312 if (r < 0) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100313 error_report("Error binding guest notifier: %d", -r);
Jason Wanga9f98bb2013-01-30 19:12:35 +0800314 goto err;
315 }
316
Jason Wangcd7d1d22014-08-19 12:56:29 +0800317 for (i = 0; i < total_queues; i++) {
318 r = vhost_net_start_one(get_vhost_net(ncs[i].peer), dev);
319
320 if (r < 0) {
321 goto err_start;
322 }
323 }
324
Jason Wanga9f98bb2013-01-30 19:12:35 +0800325 return 0;
326
Jason Wangcd7d1d22014-08-19 12:56:29 +0800327err_start:
Jason Wanga9f98bb2013-01-30 19:12:35 +0800328 while (--i >= 0) {
Nikolay Nikolaeved8b4af2014-05-27 15:05:08 +0300329 vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
Jason Wanga9f98bb2013-01-30 19:12:35 +0800330 }
Jason Wangcd7d1d22014-08-19 12:56:29 +0800331 e = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
332 if (e < 0) {
333 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);
334 fflush(stderr);
335 }
336err:
Jason Wanga9f98bb2013-01-30 19:12:35 +0800337 return r;
338}
339
340void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
341 int total_queues)
342{
KONRAD Frederic1c819442013-04-24 10:21:21 +0200343 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
344 VirtioBusState *vbus = VIRTIO_BUS(qbus);
345 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
Jason Wanga9f98bb2013-01-30 19:12:35 +0800346 int i, r;
347
Jason Wangcd7d1d22014-08-19 12:56:29 +0800348 for (i = 0; i < total_queues; i++) {
349 vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
350 }
351
KONRAD Frederic1c819442013-04-24 10:21:21 +0200352 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
Jason Wanga9f98bb2013-01-30 19:12:35 +0800353 if (r < 0) {
354 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r);
355 fflush(stderr);
356 }
357 assert(r >= 0);
Jason Wanga9f98bb2013-01-30 19:12:35 +0800358}
359
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200360void vhost_net_cleanup(struct vhost_net *net)
361{
362 vhost_dev_cleanup(&net->dev);
Anthony Liguori7267c092011-08-20 22:09:37 -0500363 g_free(net);
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200364}
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +0200365
366bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
367{
368 return vhost_virtqueue_pending(&net->dev, idx);
369}
370
371void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
372 int idx, bool mask)
373{
374 vhost_virtqueue_mask(&net->dev, dev, idx, mask);
375}
Nikolay Nikolaeved8b4af2014-05-27 15:05:08 +0300376
377VHostNetState *get_vhost_net(NetClientState *nc)
378{
379 VHostNetState *vhost_net = 0;
380
381 if (!nc) {
382 return 0;
383 }
384
385 switch (nc->info->type) {
386 case NET_CLIENT_OPTIONS_KIND_TAP:
387 vhost_net = tap_get_vhost_net(nc);
388 break;
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300389 case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
390 vhost_net = vhost_user_get_vhost_net(nc);
391 break;
Nikolay Nikolaeved8b4af2014-05-27 15:05:08 +0300392 default:
393 break;
394 }
395
396 return vhost_net;
397}
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200398#else
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300399struct vhost_net *vhost_net_init(VhostNetOptions *options)
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200400{
Michael Tokarev35f75462011-06-10 00:55:57 +0400401 error_report("vhost-net support is not compiled in");
mst@redhat.com5430a282011-02-01 22:13:42 +0200402 return NULL;
403}
404
405bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
406{
407 return false;
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200408}
409
Jason Wanga9f98bb2013-01-30 19:12:35 +0800410int vhost_net_start(VirtIODevice *dev,
411 NetClientState *ncs,
412 int total_queues)
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200413{
mst@redhat.com5430a282011-02-01 22:13:42 +0200414 return -ENOSYS;
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200415}
Jason Wanga9f98bb2013-01-30 19:12:35 +0800416void vhost_net_stop(VirtIODevice *dev,
417 NetClientState *ncs,
418 int total_queues)
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200419{
420}
421
422void vhost_net_cleanup(struct vhost_net *net)
423{
424}
425
426unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
427{
mst@redhat.com5430a282011-02-01 22:13:42 +0200428 return features;
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200429}
430void vhost_net_ack_features(struct vhost_net *net, unsigned features)
431{
432}
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +0200433
434bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
435{
Stefan Weil4dd72e02013-12-22 15:51:22 +0100436 return false;
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +0200437}
438
439void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
440 int idx, bool mask)
441{
442}
Nikolay Nikolaeved8b4af2014-05-27 15:05:08 +0300443
444VHostNetState *get_vhost_net(NetClientState *nc)
445{
446 return 0;
447}
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200448#endif