blob: 5b94c84541b580d26c45c7cb27681d63c465d69d [file] [log] [blame]
Nikolay Nikolaevd314f582014-05-27 15:06:29 +03001/*
2 * vhost-user.c
3 *
4 * Copyright (c) 2013 Virtual Open Systems Sarl.
5 *
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
8 *
9 */
10
Peter Maydell2744d922016-01-29 17:50:00 +000011#include "qemu/osdep.h"
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030012#include "clients.h"
13#include "net/vhost_net.h"
14#include "net/vhost-user.h"
15#include "sysemu/char.h"
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +030016#include "qemu/config-file.h"
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030017#include "qemu/error-report.h"
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080018#include "qmp-commands.h"
Marc-André Lureau69b32a62015-10-09 17:17:30 +020019#include "trace.h"
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030020
21typedef struct VhostUserState {
22 NetClientState nc;
23 CharDriverState *chr;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030024 VHostNetState *vhost_net;
Paolo Bonzini6f1de6b2016-06-20 15:02:40 +020025 guint watch;
Marc-André Lureaua4632152016-06-06 18:45:05 +020026 uint64_t acked_features;
Marc-André Lureauc89804d2016-07-27 01:15:19 +040027 bool started;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030028} VhostUserState;
29
30VHostNetState *vhost_user_get_vhost_net(NetClientState *nc)
31{
32 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
Eric Blakef394b2e2016-07-13 21:50:23 -060033 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030034 return s->vhost_net;
35}
36
Marc-André Lureaua4632152016-06-06 18:45:05 +020037uint64_t vhost_user_get_acked_features(NetClientState *nc)
38{
39 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
Eric Blakef394b2e2016-07-13 21:50:23 -060040 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
Marc-André Lureaua4632152016-06-06 18:45:05 +020041 return s->acked_features;
42}
43
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080044static void vhost_user_stop(int queues, NetClientState *ncs[])
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030045{
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080046 VhostUserState *s;
47 int i;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030048
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080049 for (i = 0; i < queues; i++) {
Eric Blakef394b2e2016-07-13 21:50:23 -060050 assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080051
52 s = DO_UPCAST(VhostUserState, nc, ncs[i]);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080053
54 if (s->vhost_net) {
Marc-André Lureaua4632152016-06-06 18:45:05 +020055 /* save acked features */
Marc-André Lureaue6bcb1b2016-07-27 01:15:12 +040056 uint64_t features = vhost_net_get_acked_features(s->vhost_net);
57 if (features) {
58 s->acked_features = features;
59 }
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080060 vhost_net_cleanup(s->vhost_net);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080061 }
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030062 }
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030063}
64
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080065static int vhost_user_start(int queues, NetClientState *ncs[])
Nikolay Nikolaevd314f582014-05-27 15:06:29 +030066{
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080067 VhostNetOptions options;
Marc-André Lureaue6bcb1b2016-07-27 01:15:12 +040068 struct vhost_net *net = NULL;
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080069 VhostUserState *s;
70 int max_queues;
71 int i;
72
73 options.backend_type = VHOST_BACKEND_TYPE_USER;
74
75 for (i = 0; i < queues; i++) {
Eric Blakef394b2e2016-07-13 21:50:23 -060076 assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080077
78 s = DO_UPCAST(VhostUserState, nc, ncs[i]);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080079
80 options.net_backend = ncs[i];
81 options.opaque = s->chr;
Jason Wang69e87b32016-07-06 09:57:55 +080082 options.busyloop_timeout = 0;
Marc-André Lureaue6bcb1b2016-07-27 01:15:12 +040083 net = vhost_net_init(&options);
84 if (!net) {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +010085 error_report("failed to init vhost_net for queue %d", i);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080086 goto err;
87 }
88
89 if (i == 0) {
Marc-André Lureaue6bcb1b2016-07-27 01:15:12 +040090 max_queues = vhost_net_get_max_queues(net);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080091 if (queues > max_queues) {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +010092 error_report("you are asking more queues than supported: %d",
93 max_queues);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +080094 goto err;
95 }
96 }
Marc-André Lureaue6bcb1b2016-07-27 01:15:12 +040097
98 if (s->vhost_net) {
99 vhost_net_cleanup(s->vhost_net);
100 g_free(s->vhost_net);
101 }
102 s->vhost_net = net;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300103 }
104
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800105 return 0;
106
107err:
Marc-André Lureaue6bcb1b2016-07-27 01:15:12 +0400108 if (net) {
109 vhost_net_cleanup(net);
110 }
111 vhost_user_stop(i, ncs);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800112 return -1;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300113}
114
Thibaut Colletf6f56292015-10-09 17:17:31 +0200115static ssize_t vhost_user_receive(NetClientState *nc, const uint8_t *buf,
116 size_t size)
117{
Thibaut Collet3e866362015-10-09 17:17:32 +0200118 /* In case of RARP (message size is 60) notify backup to send a fake RARP.
119 This fake RARP will be sent by backend only for guest
120 without GUEST_ANNOUNCE capability.
Thibaut Colletf6f56292015-10-09 17:17:31 +0200121 */
Thibaut Collet3e866362015-10-09 17:17:32 +0200122 if (size == 60) {
123 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
124 int r;
125 static int display_rarp_failure = 1;
126 char mac_addr[6];
127
128 /* extract guest mac address from the RARP message */
129 memcpy(mac_addr, &buf[6], 6);
130
131 r = vhost_net_notify_migration_done(s->vhost_net, mac_addr);
132
133 if ((r != 0) && (display_rarp_failure)) {
134 fprintf(stderr,
135 "Vhost user backend fails to broadcast fake RARP\n");
136 fflush(stderr);
137 display_rarp_failure = 0;
138 }
139 }
140
Thibaut Colletf6f56292015-10-09 17:17:31 +0200141 return size;
142}
143
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300144static void vhost_user_cleanup(NetClientState *nc)
145{
146 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
147
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800148 if (s->vhost_net) {
149 vhost_net_cleanup(s->vhost_net);
Marc-André Lureaue6bcb1b2016-07-27 01:15:12 +0400150 g_free(s->vhost_net);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800151 s->vhost_net = NULL;
152 }
Paolo Bonzini25f0d2a2016-06-29 15:15:33 +0200153 if (s->chr) {
154 qemu_chr_add_handlers(s->chr, NULL, NULL, NULL, NULL);
155 qemu_chr_fe_release(s->chr);
156 s->chr = NULL;
157 }
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800158
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300159 qemu_purge_queued_packets(nc);
160}
161
162static bool vhost_user_has_vnet_hdr(NetClientState *nc)
163{
Eric Blakef394b2e2016-07-13 21:50:23 -0600164 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300165
166 return true;
167}
168
169static bool vhost_user_has_ufo(NetClientState *nc)
170{
Eric Blakef394b2e2016-07-13 21:50:23 -0600171 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300172
173 return true;
174}
175
176static NetClientInfo net_vhost_user_info = {
Eric Blakef394b2e2016-07-13 21:50:23 -0600177 .type = NET_CLIENT_DRIVER_VHOST_USER,
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300178 .size = sizeof(VhostUserState),
Thibaut Colletf6f56292015-10-09 17:17:31 +0200179 .receive = vhost_user_receive,
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300180 .cleanup = vhost_user_cleanup,
181 .has_vnet_hdr = vhost_user_has_vnet_hdr,
182 .has_ufo = vhost_user_has_ufo,
183};
184
Tetsuya Mukawaa6553592016-06-06 18:44:59 +0200185static gboolean net_vhost_user_watch(GIOChannel *chan, GIOCondition cond,
186 void *opaque)
187{
188 VhostUserState *s = opaque;
Tetsuya Mukawaa6553592016-06-06 18:44:59 +0200189
Marc-André Lureau9c7d18b2016-07-27 01:14:57 +0400190 qemu_chr_disconnect(s->chr);
Tetsuya Mukawaa6553592016-06-06 18:44:59 +0200191
192 return FALSE;
193}
194
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300195static void net_vhost_user_event(void *opaque, int event)
196{
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800197 const char *name = opaque;
198 NetClientState *ncs[MAX_QUEUE_NUM];
199 VhostUserState *s;
200 Error *err = NULL;
201 int queues;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300202
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800203 queues = qemu_find_net_clients_except(name, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -0600204 NET_CLIENT_DRIVER_NIC,
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800205 MAX_QUEUE_NUM);
Marc-André Lureauc1bf3532016-02-23 19:10:49 +0100206 assert(queues < MAX_QUEUE_NUM);
207
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800208 s = DO_UPCAST(VhostUserState, nc, ncs[0]);
Marc-André Lureau69b32a62015-10-09 17:17:30 +0200209 trace_vhost_user_event(s->chr->label, event);
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300210 switch (event) {
211 case CHR_EVENT_OPENED:
Tetsuya Mukawaa6553592016-06-06 18:44:59 +0200212 s->watch = qemu_chr_fe_add_watch(s->chr, G_IO_HUP,
213 net_vhost_user_watch, s);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800214 if (vhost_user_start(queues, ncs) < 0) {
Marc-André Lureau0d572af2016-06-06 18:45:03 +0200215 qemu_chr_disconnect(s->chr);
216 return;
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800217 }
218 qmp_set_link(name, true, &err);
Marc-André Lureauc89804d2016-07-27 01:15:19 +0400219 s->started = true;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300220 break;
221 case CHR_EVENT_CLOSED:
Wen Congyangd39c87d2015-11-11 14:53:29 +0800222 qmp_set_link(name, false, &err);
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800223 vhost_user_stop(queues, ncs);
Tetsuya Mukawaa6553592016-06-06 18:44:59 +0200224 g_source_remove(s->watch);
225 s->watch = 0;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300226 break;
227 }
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800228
229 if (err) {
230 error_report_err(err);
231 }
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300232}
233
234static int net_vhost_user_init(NetClientState *peer, const char *device,
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800235 const char *name, CharDriverState *chr,
236 int queues)
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300237{
Marc-André Lureauc89804d2016-07-27 01:15:19 +0400238 NetClientState *nc, *nc0 = NULL;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300239 VhostUserState *s;
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800240 int i;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300241
Marc-André Lureauc1bf3532016-02-23 19:10:49 +0100242 assert(name);
243 assert(queues > 0);
244
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800245 for (i = 0; i < queues; i++) {
246 nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name);
Marc-André Lureauc89804d2016-07-27 01:15:19 +0400247 if (!nc0) {
248 nc0 = nc;
249 }
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300250
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800251 snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user%d to %s",
252 i, chr->label);
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300253
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800254 nc->queue_index = i;
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300255
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800256 s = DO_UPCAST(VhostUserState, nc, nc);
257 s->chr = chr;
258 }
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300259
Marc-André Lureauc89804d2016-07-27 01:15:19 +0400260 s = DO_UPCAST(VhostUserState, nc, nc0);
261 do {
262 Error *err = NULL;
263 if (qemu_chr_wait_connected(chr, &err) < 0) {
264 error_report_err(err);
265 return -1;
266 }
267 qemu_chr_add_handlers(chr, NULL, NULL,
268 net_vhost_user_event, nc0->name);
269 } while (!s->started);
Michael S. Tsirkind345ed22015-07-15 13:47:31 +0300270
Marc-André Lureau1a5b68c2016-07-27 01:15:13 +0400271 assert(s->vhost_net);
272
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300273 return 0;
274}
275
Daniel P. Berrange0a733362016-10-07 13:18:34 +0100276static CharDriverState *net_vhost_claim_chardev(
Markus Armbruster81904832015-03-13 14:17:16 +0100277 const NetdevVhostUserOptions *opts, Error **errp)
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300278{
279 CharDriverState *chr = qemu_chr_find(opts->chardev);
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300280
281 if (chr == NULL) {
Markus Armbruster81904832015-03-13 14:17:16 +0100282 error_setg(errp, "chardev \"%s\" not found", opts->chardev);
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300283 return NULL;
284 }
285
Daniel P. Berrange0a733362016-10-07 13:18:34 +0100286 if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) {
287 error_setg(errp, "chardev \"%s\" is not reconnectable",
288 opts->chardev);
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300289 return NULL;
290 }
Daniel P. Berrange0a733362016-10-07 13:18:34 +0100291 if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_FD_PASS)) {
292 error_setg(errp, "chardev \"%s\" does not support FD passing",
Markus Armbruster81904832015-03-13 14:17:16 +0100293 opts->chardev);
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300294 return NULL;
295 }
296
297 qemu_chr_fe_claim_no_fail(chr);
298
299 return chr;
300}
301
Markus Armbruster28d0de72015-03-13 13:35:14 +0100302static int net_vhost_check_net(void *opaque, QemuOpts *opts, Error **errp)
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300303{
304 const char *name = opaque;
305 const char *driver, *netdev;
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300306
307 driver = qemu_opt_get(opts, "driver");
308 netdev = qemu_opt_get(opts, "netdev");
309
310 if (!driver || !netdev) {
311 return 0;
312 }
313
314 if (strcmp(netdev, name) == 0 &&
Marc-André Lureaud9d26112016-07-27 01:14:56 +0400315 !g_str_has_prefix(driver, "virtio-net-")) {
Markus Armbruster81904832015-03-13 14:17:16 +0100316 error_setg(errp, "vhost-user requires frontend driver virtio-net-*");
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300317 return -1;
318 }
319
320 return 0;
321}
322
Kővágó, Zoltáncebea512016-07-13 21:50:12 -0600323int net_init_vhost_user(const Netdev *netdev, const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200324 NetClientState *peer, Error **errp)
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300325{
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800326 int queues;
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300327 const NetdevVhostUserOptions *vhost_user_opts;
328 CharDriverState *chr;
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300329
Eric Blakef394b2e2016-07-13 21:50:23 -0600330 assert(netdev->type == NET_CLIENT_DRIVER_VHOST_USER);
331 vhost_user_opts = &netdev->u.vhost_user;
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300332
Daniel P. Berrange0a733362016-10-07 13:18:34 +0100333 chr = net_vhost_claim_chardev(vhost_user_opts, errp);
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300334 if (!chr) {
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300335 return -1;
336 }
337
338 /* verify net frontend */
339 if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net,
Markus Armbruster81904832015-03-13 14:17:16 +0100340 (char *)name, errp)) {
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300341 return -1;
342 }
343
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800344 queues = vhost_user_opts->has_queues ? vhost_user_opts->queues : 1;
Ilya Maximetsfff4e482016-02-24 13:44:34 +0300345 if (queues < 1 || queues > MAX_QUEUE_NUM) {
Victor Kaplansky6f6f9512015-12-01 15:32:26 +0200346 error_setg(errp,
Ilya Maximetsfff4e482016-02-24 13:44:34 +0300347 "vhost-user number of queues must be in range [1, %d]",
348 MAX_QUEUE_NUM);
Victor Kaplansky6f6f9512015-12-01 15:32:26 +0200349 return -1;
350 }
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300351
Changchun Ouyangb931bfb2015-09-23 12:20:00 +0800352 return net_vhost_user_init(peer, "vhost_user", name, chr, queues);
Nikolay Nikolaevd314f582014-05-27 15:06:29 +0300353}