blob: f2d49ad7e7df217674fc9584be0676c4c1e9a869 [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
Peter Maydelle8d40462016-01-26 18:17:11 +000016#include "qemu/osdep.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020017#include "net/net.h"
Michael S. Tsirkind5970052010-03-17 13:08:17 +020018#include "net/tap.h"
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +030019#include "net/vhost-user.h"
Michael S. Tsirkind5970052010-03-17 13:08:17 +020020
Paolo Bonzini0d09e412013-02-05 17:06:20 +010021#include "hw/virtio/virtio-net.h"
22#include "net/vhost_net.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010023#include "qemu/error-report.h"
Michael S. Tsirkind5970052010-03-17 13:08:17 +020024
Michael S. Tsirkind5970052010-03-17 13:08:17 +020025
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>
Michael S. Tsirkind5970052010-03-17 13:08:17 +020030#include <netpacket/packet.h>
31#include <net/ethernet.h>
32#include <net/if.h>
33#include <netinet/in.h>
34
Michael S. Tsirkind5970052010-03-17 13:08:17 +020035
Michael S. Tsirkin4fbe0f32015-02-16 22:35:40 +010036#include "standard-headers/linux/virtio_ring.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010037#include "hw/virtio/vhost.h"
KONRAD Frederic1c819442013-04-24 10:21:21 +020038#include "hw/virtio/virtio-bus.h"
Michael S. Tsirkind5970052010-03-17 13:08:17 +020039
40struct vhost_net {
41 struct vhost_dev dev;
42 struct vhost_virtqueue vqs[2];
43 int backend;
Stefan Hajnoczi35277d12012-07-24 16:35:14 +010044 NetClientState *nc;
Michael S. Tsirkind5970052010-03-17 13:08:17 +020045};
46
Nikolay Nikolaev2e6d46d2014-05-27 15:04:42 +030047/* Features supported by host kernel. */
48static const int kernel_feature_bits[] = {
49 VIRTIO_F_NOTIFY_ON_EMPTY,
50 VIRTIO_RING_F_INDIRECT_DESC,
51 VIRTIO_RING_F_EVENT_IDX,
52 VIRTIO_NET_F_MRG_RXBUF,
Michael S. Tsirkinb1506132015-06-04 12:34:19 +020053 VIRTIO_F_VERSION_1,
Nikolay Nikolaev2e6d46d2014-05-27 15:04:42 +030054 VHOST_INVALID_FEATURE_BIT
55};
56
Nikolay Nikolaev5f4c01c2014-05-27 15:06:16 +030057/* Features supported by others. */
Stefan Weild122f1a2015-02-28 19:19:17 +010058static const int user_feature_bits[] = {
Nikolay Nikolaev5f4c01c2014-05-27 15:06:16 +030059 VIRTIO_F_NOTIFY_ON_EMPTY,
60 VIRTIO_RING_F_INDIRECT_DESC,
61 VIRTIO_RING_F_EVENT_IDX,
62
63 VIRTIO_F_ANY_LAYOUT,
Michael S. Tsirkinb1506132015-06-04 12:34:19 +020064 VIRTIO_F_VERSION_1,
Nikolay Nikolaev5f4c01c2014-05-27 15:06:16 +030065 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,
Nikolay Nikolaev5f4c01c2014-05-27 15:06:16 +030077
Michael S. Tsirkin72018d12015-11-17 16:55:17 +020078 /* This bit implies RARP isn't sent by QEMU out of band */
Thibaut Colletf6f56292015-10-09 17:17:31 +020079 VIRTIO_NET_F_GUEST_ANNOUNCE,
80
Nikolay Nikolaev5f4c01c2014-05-27 15:06:16 +030081 VIRTIO_NET_F_MQ,
82
83 VHOST_INVALID_FEATURE_BIT
84};
85
Nikolay Nikolaev2e6d46d2014-05-27 15:04:42 +030086static const int *vhost_net_get_feature_bits(struct vhost_net *net)
87{
88 const int *feature_bits = 0;
89
90 switch (net->nc->info->type) {
Eric Blakef394b2e2016-07-13 21:50:23 -060091 case NET_CLIENT_DRIVER_TAP:
Nikolay Nikolaev2e6d46d2014-05-27 15:04:42 +030092 feature_bits = kernel_feature_bits;
93 break;
Eric Blakef394b2e2016-07-13 21:50:23 -060094 case NET_CLIENT_DRIVER_VHOST_USER:
Nikolay Nikolaev5f4c01c2014-05-27 15:06:16 +030095 feature_bits = user_feature_bits;
96 break;
Nikolay Nikolaev2e6d46d2014-05-27 15:04:42 +030097 default:
98 error_report("Feature bits not defined for this type: %d",
99 net->nc->info->type);
100 break;
101 }
102
103 return feature_bits;
104}
105
Cornelia Huck9a2ba822015-06-04 12:34:20 +0200106uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features)
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200107{
Nikolay Nikolaev2e6d46d2014-05-27 15:04:42 +0300108 return vhost_get_features(&net->dev, vhost_net_get_feature_bits(net),
109 features);
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200110}
111
Cornelia Huck9a2ba822015-06-04 12:34:20 +0200112void vhost_net_ack_features(struct vhost_net *net, uint64_t features)
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200113{
Jason Wangb49ae912014-09-03 14:25:30 +0800114 net->dev.acked_features = net->dev.backend_features;
Nikolay Nikolaev2e6d46d2014-05-27 15:04:42 +0300115 vhost_ack_features(&net->dev, vhost_net_get_feature_bits(net), features);
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200116}
117
Yuanhan Liue2051e92015-09-23 12:19:58 +0800118uint64_t vhost_net_get_max_queues(VHostNetState *net)
119{
120 return net->dev.max_queues;
121}
122
Marc-André Lureaua4632152016-06-06 18:45:05 +0200123uint64_t vhost_net_get_acked_features(VHostNetState *net)
124{
125 return net->dev.acked_features;
126}
127
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100128static int vhost_net_get_fd(NetClientState *backend)
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200129{
130 switch (backend->info->type) {
Eric Blakef394b2e2016-07-13 21:50:23 -0600131 case NET_CLIENT_DRIVER_TAP:
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200132 return tap_get_fd(backend);
133 default:
134 fprintf(stderr, "vhost-net requires tap backend\n");
135 return -EBADFD;
136 }
137}
138
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300139struct vhost_net *vhost_net_init(VhostNetOptions *options)
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200140{
141 int r;
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300142 bool backend_kernel = options->backend_type == VHOST_BACKEND_TYPE_KERNEL;
Marc-André Lureauf1a03652016-07-27 01:15:03 +0400143 struct vhost_net *net = g_new0(struct vhost_net, 1);
Marc-André Lureaua4632152016-06-06 18:45:05 +0200144 uint64_t features = 0;
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300145
146 if (!options->net_backend) {
147 fprintf(stderr, "vhost-net requires net backend to be setup\n");
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200148 goto fail;
149 }
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800150 net->nc = options->net_backend;
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300151
Yuanhan Liue2051e92015-09-23 12:19:58 +0800152 net->dev.max_queues = 1;
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800153 net->dev.nvqs = 2;
154 net->dev.vqs = net->vqs;
Yuanhan Liue2051e92015-09-23 12:19:58 +0800155
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300156 if (backend_kernel) {
157 r = vhost_net_get_fd(options->net_backend);
158 if (r < 0) {
159 goto fail;
160 }
161 net->dev.backend_features = qemu_has_vnet_hdr(options->net_backend)
Cornelia Huck9a2ba822015-06-04 12:34:20 +0200162 ? 0 : (1ULL << VHOST_NET_F_VIRTIO_NET_HDR);
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300163 net->backend = r;
Michael S. Tsirkindcb10c02015-09-23 12:19:56 +0800164 net->dev.protocol_features = 0;
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300165 } else {
166 net->dev.backend_features = 0;
Michael S. Tsirkindcb10c02015-09-23 12:19:56 +0800167 net->dev.protocol_features = 0;
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300168 net->backend = -1;
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200169
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800170 /* vhost-user needs vq_index to initiate a specific queue pair */
171 net->dev.vq_index = net->nc->queue_index * net->dev.nvqs;
172 }
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +0200173
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300174 r = vhost_dev_init(&net->dev, options->opaque,
Jason Wang69e87b32016-07-06 09:57:55 +0800175 options->backend_type, options->busyloop_timeout);
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200176 if (r < 0) {
177 goto fail;
178 }
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300179 if (backend_kernel) {
Damjan Mariond8e80ae2014-09-11 14:55:48 -0700180 if (!qemu_has_vnet_hdr_len(options->net_backend,
181 sizeof(struct virtio_net_hdr_mrg_rxbuf))) {
Cornelia Huck9a2ba822015-06-04 12:34:20 +0200182 net->dev.features &= ~(1ULL << VIRTIO_NET_F_MRG_RXBUF);
Damjan Mariond8e80ae2014-09-11 14:55:48 -0700183 }
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300184 if (~net->dev.features & net->dev.backend_features) {
185 fprintf(stderr, "vhost lacks feature mask %" PRIu64
186 " for backend\n",
187 (uint64_t)(~net->dev.features & net->dev.backend_features));
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300188 goto fail;
189 }
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200190 }
Marc-André Lureaua4632152016-06-06 18:45:05 +0200191
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200192 /* Set sane init value. Override when guest acks. */
Eric Blakef394b2e2016-07-13 21:50:23 -0600193 if (net->nc->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
Marc-André Lureaua4632152016-06-06 18:45:05 +0200194 features = vhost_user_get_acked_features(net->nc);
195 if (~net->dev.features & features) {
196 fprintf(stderr, "vhost lacks feature mask %" PRIu64
197 " for backend\n",
198 (uint64_t)(~net->dev.features & features));
Marc-André Lureaua4632152016-06-06 18:45:05 +0200199 goto fail;
200 }
201 }
202
203 vhost_net_ack_features(net, features);
204
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200205 return net;
Marc-André Lureauf1a03652016-07-27 01:15:03 +0400206
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200207fail:
Marc-André Lureauf1a03652016-07-27 01:15:03 +0400208 vhost_dev_cleanup(&net->dev);
Anthony Liguori7267c092011-08-20 22:09:37 -0500209 g_free(net);
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200210 return NULL;
211}
212
Jason Wangcd7d1d22014-08-19 12:56:29 +0800213static void vhost_net_set_vq_index(struct vhost_net *net, int vq_index)
214{
215 net->dev.vq_index = vq_index;
216}
217
Jason Wanga9f98bb2013-01-30 19:12:35 +0800218static int vhost_net_start_one(struct vhost_net *net,
Jason Wangcd7d1d22014-08-19 12:56:29 +0800219 VirtIODevice *dev)
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200220{
221 struct vhost_vring_file file = { };
222 int r;
Michael S. Tsirkinb0b3db72011-08-11 10:21:18 +0300223
Jason Wanga9f98bb2013-01-30 19:12:35 +0800224 net->dev.nvqs = 2;
225 net->dev.vqs = net->vqs;
Jason Wanga9f98bb2013-01-30 19:12:35 +0800226
Michael S. Tsirkinb0b3db72011-08-11 10:21:18 +0300227 r = vhost_dev_enable_notifiers(&net->dev, dev);
228 if (r < 0) {
229 goto fail_notifiers;
230 }
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200231
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200232 r = vhost_dev_start(&net->dev, dev);
233 if (r < 0) {
Michael S. Tsirkinb0b3db72011-08-11 10:21:18 +0300234 goto fail_start;
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200235 }
236
Nikolay Nikolaev212d69f2014-05-27 15:04:55 +0300237 if (net->nc->info->poll) {
238 net->nc->info->poll(net->nc, false);
239 }
240
Eric Blakef394b2e2016-07-13 21:50:23 -0600241 if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) {
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300242 qemu_set_fd_handler(net->backend, NULL, NULL, NULL);
243 file.fd = net->backend;
244 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
Marc-André Lureau950d94b2016-07-27 01:15:25 +0400245 r = vhost_net_set_backend(&net->dev, &file);
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300246 if (r < 0) {
247 r = -errno;
248 goto fail;
249 }
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200250 }
251 }
252 return 0;
253fail:
254 file.fd = -1;
Eric Blakef394b2e2016-07-13 21:50:23 -0600255 if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) {
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300256 while (file.index-- > 0) {
Marc-André Lureau950d94b2016-07-27 01:15:25 +0400257 int r = vhost_net_set_backend(&net->dev, &file);
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300258 assert(r >= 0);
259 }
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200260 }
Nikolay Nikolaev212d69f2014-05-27 15:04:55 +0300261 if (net->nc->info->poll) {
262 net->nc->info->poll(net->nc, true);
263 }
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200264 vhost_dev_stop(&net->dev, dev);
Michael S. Tsirkinb0b3db72011-08-11 10:21:18 +0300265fail_start:
266 vhost_dev_disable_notifiers(&net->dev, dev);
267fail_notifiers:
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200268 return r;
269}
270
Jason Wanga9f98bb2013-01-30 19:12:35 +0800271static void vhost_net_stop_one(struct vhost_net *net,
272 VirtIODevice *dev)
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200273{
274 struct vhost_vring_file file = { .fd = -1 };
275
Eric Blakef394b2e2016-07-13 21:50:23 -0600276 if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) {
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300277 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
Marc-André Lureau950d94b2016-07-27 01:15:25 +0400278 int r = vhost_net_set_backend(&net->dev, &file);
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300279 assert(r >= 0);
280 }
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200281 }
Nikolay Nikolaev212d69f2014-05-27 15:04:55 +0300282 if (net->nc->info->poll) {
283 net->nc->info->poll(net->nc, true);
284 }
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200285 vhost_dev_stop(&net->dev, dev);
Michael S. Tsirkinb0b3db72011-08-11 10:21:18 +0300286 vhost_dev_disable_notifiers(&net->dev, dev);
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200287}
288
Jason Wanga9f98bb2013-01-30 19:12:35 +0800289int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
290 int total_queues)
291{
KONRAD Frederic1c819442013-04-24 10:21:21 +0200292 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
293 VirtioBusState *vbus = VIRTIO_BUS(qbus);
294 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
Greg Kurz3154d1e2016-02-05 11:45:26 +0100295 int r, e, i;
Jason Wanga9f98bb2013-01-30 19:12:35 +0800296
KONRAD Frederic1c819442013-04-24 10:21:21 +0200297 if (!k->set_guest_notifiers) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100298 error_report("binding does not support guest notifiers");
Laurent Viviera4076442016-01-13 20:26:25 +0100299 return -ENOSYS;
Jason Wanga9f98bb2013-01-30 19:12:35 +0800300 }
301
Greg Kurz3154d1e2016-02-05 11:45:26 +0100302 for (i = 0; i < total_queues; i++) {
Victor Kaplansky56696552016-02-18 16:12:23 +0200303 struct vhost_net *net;
304
305 net = get_vhost_net(ncs[i].peer);
306 vhost_net_set_vq_index(net, i * 2);
307
308 /* Suppress the masking guest notifiers on vhost user
309 * because vhost user doesn't interrupt masking/unmasking
310 * properly.
311 */
Eric Blakef394b2e2016-07-13 21:50:23 -0600312 if (net->nc->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
Marc-André Lureau01edc232016-07-27 01:14:55 +0400313 dev->use_guest_notifier_mask = false;
Victor Kaplansky56696552016-02-18 16:12:23 +0200314 }
315 }
Jason Wanga9f98bb2013-01-30 19:12:35 +0800316
KONRAD Frederic1c819442013-04-24 10:21:21 +0200317 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, true);
Jason Wanga9f98bb2013-01-30 19:12:35 +0800318 if (r < 0) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100319 error_report("Error binding guest notifier: %d", -r);
Greg Kurz3154d1e2016-02-05 11:45:26 +0100320 goto err;
Jason Wanga9f98bb2013-01-30 19:12:35 +0800321 }
322
Jason Wangcd7d1d22014-08-19 12:56:29 +0800323 for (i = 0; i < total_queues; i++) {
324 r = vhost_net_start_one(get_vhost_net(ncs[i].peer), dev);
325
326 if (r < 0) {
327 goto err_start;
328 }
Marc-André Lureaubfc6cf32016-06-06 18:45:06 +0200329
330 if (ncs[i].peer->vring_enable) {
331 /* restore vring enable state */
332 r = vhost_set_vring_enable(ncs[i].peer, ncs[i].peer->vring_enable);
333
334 if (r < 0) {
335 goto err_start;
336 }
337 }
Jason Wangcd7d1d22014-08-19 12:56:29 +0800338 }
339
Jason Wanga9f98bb2013-01-30 19:12:35 +0800340 return 0;
341
Jason Wangcd7d1d22014-08-19 12:56:29 +0800342err_start:
Jason Wanga9f98bb2013-01-30 19:12:35 +0800343 while (--i >= 0) {
Nikolay Nikolaeved8b4af2014-05-27 15:05:08 +0300344 vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
Jason Wanga9f98bb2013-01-30 19:12:35 +0800345 }
Jason Wangcd7d1d22014-08-19 12:56:29 +0800346 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 }
Greg Kurz3154d1e2016-02-05 11:45:26 +0100351err:
Jason Wanga9f98bb2013-01-30 19:12:35 +0800352 return r;
353}
354
355void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
356 int total_queues)
357{
KONRAD Frederic1c819442013-04-24 10:21:21 +0200358 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
359 VirtioBusState *vbus = VIRTIO_BUS(qbus);
360 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
Jason Wanga9f98bb2013-01-30 19:12:35 +0800361 int i, r;
362
Jason Wangcd7d1d22014-08-19 12:56:29 +0800363 for (i = 0; i < total_queues; i++) {
364 vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
365 }
366
KONRAD Frederic1c819442013-04-24 10:21:21 +0200367 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
Jason Wanga9f98bb2013-01-30 19:12:35 +0800368 if (r < 0) {
369 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r);
370 fflush(stderr);
371 }
372 assert(r >= 0);
Jason Wanga9f98bb2013-01-30 19:12:35 +0800373}
374
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200375void vhost_net_cleanup(struct vhost_net *net)
376{
377 vhost_dev_cleanup(&net->dev);
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200378}
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +0200379
Thibaut Collet3e866362015-10-09 17:17:32 +0200380int vhost_net_notify_migration_done(struct vhost_net *net, char* mac_addr)
381{
382 const VhostOps *vhost_ops = net->dev.vhost_ops;
Thibaut Collet3e866362015-10-09 17:17:32 +0200383
Marc-André Lureau51f7aca2016-07-27 01:15:15 +0400384 assert(vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
385 assert(vhost_ops->vhost_migration_done);
Thibaut Collet3e866362015-10-09 17:17:32 +0200386
Marc-André Lureau51f7aca2016-07-27 01:15:15 +0400387 return vhost_ops->vhost_migration_done(&net->dev, mac_addr);
Thibaut Collet3e866362015-10-09 17:17:32 +0200388}
389
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +0200390bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
391{
392 return vhost_virtqueue_pending(&net->dev, idx);
393}
394
395void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
396 int idx, bool mask)
397{
398 vhost_virtqueue_mask(&net->dev, dev, idx, mask);
399}
Nikolay Nikolaeved8b4af2014-05-27 15:05:08 +0300400
401VHostNetState *get_vhost_net(NetClientState *nc)
402{
403 VHostNetState *vhost_net = 0;
404
405 if (!nc) {
406 return 0;
407 }
408
409 switch (nc->info->type) {
Eric Blakef394b2e2016-07-13 21:50:23 -0600410 case NET_CLIENT_DRIVER_TAP:
Nikolay Nikolaeved8b4af2014-05-27 15:05:08 +0300411 vhost_net = tap_get_vhost_net(nc);
412 break;
Eric Blakef394b2e2016-07-13 21:50:23 -0600413 case NET_CLIENT_DRIVER_VHOST_USER:
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300414 vhost_net = vhost_user_get_vhost_net(nc);
Marc-André Lureau1a5b68c2016-07-27 01:15:13 +0400415 assert(vhost_net);
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300416 break;
Nikolay Nikolaeved8b4af2014-05-27 15:05:08 +0300417 default:
418 break;
419 }
420
421 return vhost_net;
422}
Changchun Ouyang7263a0a2015-09-23 12:20:01 +0800423
424int vhost_set_vring_enable(NetClientState *nc, int enable)
425{
426 VHostNetState *net = get_vhost_net(nc);
Marc-André Lureaubb12e762016-07-27 01:15:14 +0400427 const VhostOps *vhost_ops = net->dev.vhost_ops;
Changchun Ouyang7263a0a2015-09-23 12:20:01 +0800428
Marc-André Lureaubfc6cf32016-06-06 18:45:06 +0200429 nc->vring_enable = enable;
430
Ilya Maximetsca102032016-08-03 08:22:49 +0300431 if (vhost_ops && vhost_ops->vhost_set_vring_enable) {
Marc-André Lureau21e70422015-10-09 17:17:28 +0200432 return vhost_ops->vhost_set_vring_enable(&net->dev, enable);
Changchun Ouyang7263a0a2015-09-23 12:20:01 +0800433 }
434
435 return 0;
436}
437
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200438#else
Yuanhan Liue2051e92015-09-23 12:19:58 +0800439uint64_t vhost_net_get_max_queues(VHostNetState *net)
440{
441 return 1;
442}
443
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300444struct vhost_net *vhost_net_init(VhostNetOptions *options)
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200445{
Michael Tokarev35f75462011-06-10 00:55:57 +0400446 error_report("vhost-net support is not compiled in");
mst@redhat.com5430a282011-02-01 22:13:42 +0200447 return NULL;
448}
449
Jason Wanga9f98bb2013-01-30 19:12:35 +0800450int vhost_net_start(VirtIODevice *dev,
451 NetClientState *ncs,
452 int total_queues)
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200453{
mst@redhat.com5430a282011-02-01 22:13:42 +0200454 return -ENOSYS;
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200455}
Jason Wanga9f98bb2013-01-30 19:12:35 +0800456void vhost_net_stop(VirtIODevice *dev,
457 NetClientState *ncs,
458 int total_queues)
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200459{
460}
461
462void vhost_net_cleanup(struct vhost_net *net)
463{
464}
465
Cornelia Huck9a2ba822015-06-04 12:34:20 +0200466uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features)
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200467{
mst@redhat.com5430a282011-02-01 22:13:42 +0200468 return features;
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200469}
Marc-André Lureaua4632152016-06-06 18:45:05 +0200470
Cornelia Huck9a2ba822015-06-04 12:34:20 +0200471void vhost_net_ack_features(struct vhost_net *net, uint64_t features)
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200472{
473}
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +0200474
Marc-André Lureaua4632152016-06-06 18:45:05 +0200475uint64_t vhost_net_get_acked_features(VHostNetState *net)
476{
477 return 0;
478}
479
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +0200480bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
481{
Stefan Weil4dd72e02013-12-22 15:51:22 +0100482 return false;
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +0200483}
484
485void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
486 int idx, bool mask)
487{
488}
Nikolay Nikolaeved8b4af2014-05-27 15:05:08 +0300489
Thibaut Collet3e866362015-10-09 17:17:32 +0200490int vhost_net_notify_migration_done(struct vhost_net *net, char* mac_addr)
491{
492 return -1;
493}
494
Nikolay Nikolaeved8b4af2014-05-27 15:05:08 +0300495VHostNetState *get_vhost_net(NetClientState *nc)
496{
497 return 0;
498}
Changchun Ouyang7263a0a2015-09-23 12:20:01 +0800499
500int vhost_set_vring_enable(NetClientState *nc, int enable)
501{
502 return 0;
503}
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200504#endif