blob: d91b7b155ea7a8cfc7d777704d9c6842edc60256 [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 <netpacket/packet.h>
32#include <net/ethernet.h>
33#include <net/if.h>
34#include <netinet/in.h>
35
36#include <stdio.h>
37
Michael S. Tsirkin4fbe0f32015-02-16 22:35:40 +010038#include "standard-headers/linux/virtio_ring.h"
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"
Greg Kurz5be7d9f2015-06-17 15:23:49 +020041#include "hw/virtio/virtio-access.h"
Michael S. Tsirkind5970052010-03-17 13:08:17 +020042
43struct vhost_net {
44 struct vhost_dev dev;
45 struct vhost_virtqueue vqs[2];
46 int backend;
Stefan Hajnoczi35277d12012-07-24 16:35:14 +010047 NetClientState *nc;
Michael S. Tsirkind5970052010-03-17 13:08:17 +020048};
49
Nikolay Nikolaev2e6d46d2014-05-27 15:04:42 +030050/* Features supported by host kernel. */
51static 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,
Michael S. Tsirkinb1506132015-06-04 12:34:19 +020056 VIRTIO_F_VERSION_1,
Nikolay Nikolaev2e6d46d2014-05-27 15:04:42 +030057 VHOST_INVALID_FEATURE_BIT
58};
59
Nikolay Nikolaev5f4c01c2014-05-27 15:06:16 +030060/* Features supported by others. */
Stefan Weild122f1a2015-02-28 19:19:17 +010061static const int user_feature_bits[] = {
Nikolay Nikolaev5f4c01c2014-05-27 15:06:16 +030062 VIRTIO_F_NOTIFY_ON_EMPTY,
63 VIRTIO_RING_F_INDIRECT_DESC,
64 VIRTIO_RING_F_EVENT_IDX,
65
66 VIRTIO_F_ANY_LAYOUT,
Michael S. Tsirkinb1506132015-06-04 12:34:19 +020067 VIRTIO_F_VERSION_1,
Nikolay Nikolaev5f4c01c2014-05-27 15:06:16 +030068 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
Thibaut Colletf6f56292015-10-09 17:17:31 +020088 VIRTIO_NET_F_GUEST_ANNOUNCE,
89
Nikolay Nikolaev5f4c01c2014-05-27 15:06:16 +030090 VIRTIO_NET_F_MQ,
91
92 VHOST_INVALID_FEATURE_BIT
93};
94
Nikolay Nikolaev2e6d46d2014-05-27 15:04:42 +030095static 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;
Nikolay Nikolaev5f4c01c2014-05-27 15:06:16 +0300103 case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
104 feature_bits = user_feature_bits;
105 break;
Nikolay Nikolaev2e6d46d2014-05-27 15:04:42 +0300106 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
Cornelia Huck9a2ba822015-06-04 12:34:20 +0200115uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features)
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200116{
Nikolay Nikolaev2e6d46d2014-05-27 15:04:42 +0300117 return vhost_get_features(&net->dev, vhost_net_get_feature_bits(net),
118 features);
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200119}
120
Cornelia Huck9a2ba822015-06-04 12:34:20 +0200121void vhost_net_ack_features(struct vhost_net *net, uint64_t features)
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200122{
Jason Wangb49ae912014-09-03 14:25:30 +0800123 net->dev.acked_features = net->dev.backend_features;
Nikolay Nikolaev2e6d46d2014-05-27 15:04:42 +0300124 vhost_ack_features(&net->dev, vhost_net_get_feature_bits(net), features);
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200125}
126
Yuanhan Liue2051e92015-09-23 12:19:58 +0800127uint64_t vhost_net_get_max_queues(VHostNetState *net)
128{
129 return net->dev.max_queues;
130}
131
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100132static int vhost_net_get_fd(NetClientState *backend)
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200133{
134 switch (backend->info->type) {
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200135 case NET_CLIENT_OPTIONS_KIND_TAP:
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200136 return tap_get_fd(backend);
137 default:
138 fprintf(stderr, "vhost-net requires tap backend\n");
139 return -EBADFD;
140 }
141}
142
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300143struct vhost_net *vhost_net_init(VhostNetOptions *options)
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200144{
145 int r;
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300146 bool backend_kernel = options->backend_type == VHOST_BACKEND_TYPE_KERNEL;
Anthony Liguori7267c092011-08-20 22:09:37 -0500147 struct vhost_net *net = g_malloc(sizeof *net);
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300148
149 if (!options->net_backend) {
150 fprintf(stderr, "vhost-net requires net backend to be setup\n");
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200151 goto fail;
152 }
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800153 net->nc = options->net_backend;
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300154
Yuanhan Liue2051e92015-09-23 12:19:58 +0800155 net->dev.max_queues = 1;
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800156 net->dev.nvqs = 2;
157 net->dev.vqs = net->vqs;
Yuanhan Liue2051e92015-09-23 12:19:58 +0800158
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300159 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)
Cornelia Huck9a2ba822015-06-04 12:34:20 +0200165 ? 0 : (1ULL << VHOST_NET_F_VIRTIO_NET_HDR);
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300166 net->backend = r;
Michael S. Tsirkindcb10c02015-09-23 12:19:56 +0800167 net->dev.protocol_features = 0;
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300168 } else {
169 net->dev.backend_features = 0;
Michael S. Tsirkindcb10c02015-09-23 12:19:56 +0800170 net->dev.protocol_features = 0;
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300171 net->backend = -1;
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200172
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800173 /* vhost-user needs vq_index to initiate a specific queue pair */
174 net->dev.vq_index = net->nc->queue_index * net->dev.nvqs;
175 }
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +0200176
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300177 r = vhost_dev_init(&net->dev, options->opaque,
Pankaj Gupta1e7398a2015-06-16 13:48:59 +0530178 options->backend_type);
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200179 if (r < 0) {
180 goto fail;
181 }
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300182 if (backend_kernel) {
Damjan Mariond8e80ae2014-09-11 14:55:48 -0700183 if (!qemu_has_vnet_hdr_len(options->net_backend,
184 sizeof(struct virtio_net_hdr_mrg_rxbuf))) {
Cornelia Huck9a2ba822015-06-04 12:34:20 +0200185 net->dev.features &= ~(1ULL << VIRTIO_NET_F_MRG_RXBUF);
Damjan Mariond8e80ae2014-09-11 14:55:48 -0700186 }
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300187 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 }
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200194 }
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200195 /* Set sane init value. Override when guest acks. */
196 vhost_net_ack_features(net, 0);
197 return net;
198fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500199 g_free(net);
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200200 return NULL;
201}
202
Jason Wangcd7d1d22014-08-19 12:56:29 +0800203static void vhost_net_set_vq_index(struct vhost_net *net, int vq_index)
204{
205 net->dev.vq_index = vq_index;
206}
207
Greg Kurz5be7d9f2015-06-17 15:23:49 +0200208static int vhost_net_set_vnet_endian(VirtIODevice *dev, NetClientState *peer,
209 bool set)
210{
211 int r = 0;
212
Cornelia Huck95129d62015-08-17 11:48:29 +0200213 if (virtio_vdev_has_feature(dev, VIRTIO_F_VERSION_1) ||
Greg Kurz5be7d9f2015-06-17 15:23:49 +0200214 (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
Jason Wanga9f98bb2013-01-30 19:12:35 +0800229static int vhost_net_start_one(struct vhost_net *net,
Jason Wangcd7d1d22014-08-19 12:56:29 +0800230 VirtIODevice *dev)
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200231{
232 struct vhost_vring_file file = { };
233 int r;
Michael S. Tsirkinb0b3db72011-08-11 10:21:18 +0300234
Jason Wanga9f98bb2013-01-30 19:12:35 +0800235 net->dev.nvqs = 2;
236 net->dev.vqs = net->vqs;
Jason Wanga9f98bb2013-01-30 19:12:35 +0800237
Michael S. Tsirkinb0b3db72011-08-11 10:21:18 +0300238 r = vhost_dev_enable_notifiers(&net->dev, dev);
239 if (r < 0) {
240 goto fail_notifiers;
241 }
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200242
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200243 r = vhost_dev_start(&net->dev, dev);
244 if (r < 0) {
Michael S. Tsirkinb0b3db72011-08-11 10:21:18 +0300245 goto fail_start;
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200246 }
247
Nikolay Nikolaev212d69f2014-05-27 15:04:55 +0300248 if (net->nc->info->poll) {
249 net->nc->info->poll(net->nc, false);
250 }
251
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300252 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;
Marc-André Lureau21e70422015-10-09 17:17:28 +0200257 r = vhost_ops->vhost_net_set_backend(&net->dev, &file);
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300258 if (r < 0) {
259 r = -errno;
260 goto fail;
261 }
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200262 }
263 }
264 return 0;
265fail:
266 file.fd = -1;
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300267 if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP) {
268 while (file.index-- > 0) {
269 const VhostOps *vhost_ops = net->dev.vhost_ops;
Marc-André Lureau21e70422015-10-09 17:17:28 +0200270 int r = vhost_ops->vhost_net_set_backend(&net->dev, &file);
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300271 assert(r >= 0);
272 }
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200273 }
Nikolay Nikolaev212d69f2014-05-27 15:04:55 +0300274 if (net->nc->info->poll) {
275 net->nc->info->poll(net->nc, true);
276 }
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200277 vhost_dev_stop(&net->dev, dev);
Michael S. Tsirkinb0b3db72011-08-11 10:21:18 +0300278fail_start:
279 vhost_dev_disable_notifiers(&net->dev, dev);
280fail_notifiers:
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200281 return r;
282}
283
Jason Wanga9f98bb2013-01-30 19:12:35 +0800284static void vhost_net_stop_one(struct vhost_net *net,
285 VirtIODevice *dev)
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200286{
287 struct vhost_vring_file file = { .fd = -1 };
288
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300289 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;
Marc-André Lureau21e70422015-10-09 17:17:28 +0200292 int r = vhost_ops->vhost_net_set_backend(&net->dev, &file);
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300293 assert(r >= 0);
294 }
Luke Gorrie294ce712015-04-26 15:00:49 +0200295 } else if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER) {
296 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
297 const VhostOps *vhost_ops = net->dev.vhost_ops;
Marc-André Lureau21e70422015-10-09 17:17:28 +0200298 int r = vhost_ops->vhost_reset_device(&net->dev);
Luke Gorrie294ce712015-04-26 15:00:49 +0200299 assert(r >= 0);
300 }
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200301 }
Nikolay Nikolaev212d69f2014-05-27 15:04:55 +0300302 if (net->nc->info->poll) {
303 net->nc->info->poll(net->nc, true);
304 }
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200305 vhost_dev_stop(&net->dev, dev);
Michael S. Tsirkinb0b3db72011-08-11 10:21:18 +0300306 vhost_dev_disable_notifiers(&net->dev, dev);
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200307}
308
Jason Wanga9f98bb2013-01-30 19:12:35 +0800309int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
310 int total_queues)
311{
KONRAD Frederic1c819442013-04-24 10:21:21 +0200312 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
313 VirtioBusState *vbus = VIRTIO_BUS(qbus);
314 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
Jason Wangcd7d1d22014-08-19 12:56:29 +0800315 int r, e, i;
Jason Wanga9f98bb2013-01-30 19:12:35 +0800316
KONRAD Frederic1c819442013-04-24 10:21:21 +0200317 if (!k->set_guest_notifiers) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100318 error_report("binding does not support guest notifiers");
Jason Wanga9f98bb2013-01-30 19:12:35 +0800319 r = -ENOSYS;
320 goto err;
321 }
322
Greg Kurz5be7d9f2015-06-17 15:23:49 +0200323 r = vhost_net_set_vnet_endian(dev, ncs[0].peer, true);
324 if (r < 0) {
325 goto err;
326 }
327
Jason Wanga9f98bb2013-01-30 19:12:35 +0800328 for (i = 0; i < total_queues; i++) {
Jason Wangcd7d1d22014-08-19 12:56:29 +0800329 vhost_net_set_vq_index(get_vhost_net(ncs[i].peer), i * 2);
Jason Wanga9f98bb2013-01-30 19:12:35 +0800330 }
331
KONRAD Frederic1c819442013-04-24 10:21:21 +0200332 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, true);
Jason Wanga9f98bb2013-01-30 19:12:35 +0800333 if (r < 0) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100334 error_report("Error binding guest notifier: %d", -r);
Greg Kurz5be7d9f2015-06-17 15:23:49 +0200335 goto err_endian;
Jason Wanga9f98bb2013-01-30 19:12:35 +0800336 }
337
Jason Wangcd7d1d22014-08-19 12:56:29 +0800338 for (i = 0; i < total_queues; i++) {
339 r = vhost_net_start_one(get_vhost_net(ncs[i].peer), dev);
340
341 if (r < 0) {
342 goto err_start;
343 }
344 }
345
Jason Wanga9f98bb2013-01-30 19:12:35 +0800346 return 0;
347
Jason Wangcd7d1d22014-08-19 12:56:29 +0800348err_start:
Jason Wanga9f98bb2013-01-30 19:12:35 +0800349 while (--i >= 0) {
Nikolay Nikolaeved8b4af2014-05-27 15:05:08 +0300350 vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
Jason Wanga9f98bb2013-01-30 19:12:35 +0800351 }
Jason Wangcd7d1d22014-08-19 12:56:29 +0800352 e = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
353 if (e < 0) {
354 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);
355 fflush(stderr);
356 }
Greg Kurz5be7d9f2015-06-17 15:23:49 +0200357err_endian:
358 vhost_net_set_vnet_endian(dev, ncs[0].peer, false);
Jason Wangcd7d1d22014-08-19 12:56:29 +0800359err:
Jason Wanga9f98bb2013-01-30 19:12:35 +0800360 return r;
361}
362
363void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
364 int total_queues)
365{
KONRAD Frederic1c819442013-04-24 10:21:21 +0200366 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
367 VirtioBusState *vbus = VIRTIO_BUS(qbus);
368 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
Jason Wanga9f98bb2013-01-30 19:12:35 +0800369 int i, r;
370
Jason Wangcd7d1d22014-08-19 12:56:29 +0800371 for (i = 0; i < total_queues; i++) {
372 vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
373 }
374
KONRAD Frederic1c819442013-04-24 10:21:21 +0200375 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
Jason Wanga9f98bb2013-01-30 19:12:35 +0800376 if (r < 0) {
377 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r);
378 fflush(stderr);
379 }
380 assert(r >= 0);
Greg Kurz5be7d9f2015-06-17 15:23:49 +0200381
382 assert(vhost_net_set_vnet_endian(dev, ncs[0].peer, false) >= 0);
Jason Wanga9f98bb2013-01-30 19:12:35 +0800383}
384
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200385void vhost_net_cleanup(struct vhost_net *net)
386{
387 vhost_dev_cleanup(&net->dev);
Anthony Liguori7267c092011-08-20 22:09:37 -0500388 g_free(net);
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200389}
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +0200390
Thibaut Collet3e866362015-10-09 17:17:32 +0200391int vhost_net_notify_migration_done(struct vhost_net *net, char* mac_addr)
392{
393 const VhostOps *vhost_ops = net->dev.vhost_ops;
394 int r = -1;
395
396 if (vhost_ops->vhost_migration_done) {
397 r = vhost_ops->vhost_migration_done(&net->dev, mac_addr);
398 }
399
400 return r;
401}
402
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +0200403bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
404{
405 return vhost_virtqueue_pending(&net->dev, idx);
406}
407
408void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
409 int idx, bool mask)
410{
411 vhost_virtqueue_mask(&net->dev, dev, idx, mask);
412}
Nikolay Nikolaeved8b4af2014-05-27 15:05:08 +0300413
414VHostNetState *get_vhost_net(NetClientState *nc)
415{
416 VHostNetState *vhost_net = 0;
417
418 if (!nc) {
419 return 0;
420 }
421
422 switch (nc->info->type) {
423 case NET_CLIENT_OPTIONS_KIND_TAP:
424 vhost_net = tap_get_vhost_net(nc);
425 break;
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300426 case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
427 vhost_net = vhost_user_get_vhost_net(nc);
428 break;
Nikolay Nikolaeved8b4af2014-05-27 15:05:08 +0300429 default:
430 break;
431 }
432
433 return vhost_net;
434}
Changchun Ouyang7263a0a2015-09-23 12:20:01 +0800435
436int vhost_set_vring_enable(NetClientState *nc, int enable)
437{
438 VHostNetState *net = get_vhost_net(nc);
439 const VhostOps *vhost_ops = net->dev.vhost_ops;
440
Marc-André Lureau21e70422015-10-09 17:17:28 +0200441 if (vhost_ops->vhost_set_vring_enable) {
442 return vhost_ops->vhost_set_vring_enable(&net->dev, enable);
Changchun Ouyang7263a0a2015-09-23 12:20:01 +0800443 }
444
445 return 0;
446}
447
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200448#else
Yuanhan Liue2051e92015-09-23 12:19:58 +0800449uint64_t vhost_net_get_max_queues(VHostNetState *net)
450{
451 return 1;
452}
453
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300454struct vhost_net *vhost_net_init(VhostNetOptions *options)
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200455{
Michael Tokarev35f75462011-06-10 00:55:57 +0400456 error_report("vhost-net support is not compiled in");
mst@redhat.com5430a282011-02-01 22:13:42 +0200457 return NULL;
458}
459
Jason Wanga9f98bb2013-01-30 19:12:35 +0800460int vhost_net_start(VirtIODevice *dev,
461 NetClientState *ncs,
462 int total_queues)
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200463{
mst@redhat.com5430a282011-02-01 22:13:42 +0200464 return -ENOSYS;
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200465}
Jason Wanga9f98bb2013-01-30 19:12:35 +0800466void vhost_net_stop(VirtIODevice *dev,
467 NetClientState *ncs,
468 int total_queues)
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200469{
470}
471
472void vhost_net_cleanup(struct vhost_net *net)
473{
474}
475
Cornelia Huck9a2ba822015-06-04 12:34:20 +0200476uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features)
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200477{
mst@redhat.com5430a282011-02-01 22:13:42 +0200478 return features;
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200479}
Cornelia Huck9a2ba822015-06-04 12:34:20 +0200480void vhost_net_ack_features(struct vhost_net *net, uint64_t features)
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200481{
482}
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +0200483
484bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
485{
Stefan Weil4dd72e02013-12-22 15:51:22 +0100486 return false;
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +0200487}
488
489void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
490 int idx, bool mask)
491{
492}
Nikolay Nikolaeved8b4af2014-05-27 15:05:08 +0300493
Thibaut Collet3e866362015-10-09 17:17:32 +0200494int vhost_net_notify_migration_done(struct vhost_net *net, char* mac_addr)
495{
496 return -1;
497}
498
Nikolay Nikolaeved8b4af2014-05-27 15:05:08 +0300499VHostNetState *get_vhost_net(NetClientState *nc)
500{
501 return 0;
502}
Changchun Ouyang7263a0a2015-09-23 12:20:01 +0800503
504int vhost_set_vring_enable(NetClientState *nc, int enable)
505{
506 return 0;
507}
Michael S. Tsirkind5970052010-03-17 13:08:17 +0200508#endif