]> git.proxmox.com Git - mirror_qemu.git/blame - net/vhost-user.c
gtk: Add show_menubar=on|off command line option.
[mirror_qemu.git] / net / vhost-user.c
CommitLineData
d314f586
NN
1/*
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
2744d920 11#include "qemu/osdep.h"
d314f586
NN
12#include "clients.h"
13#include "net/vhost_net.h"
14#include "net/vhost-user.h"
4d0cf552 15#include "hw/virtio/vhost-user.h"
4d43a603 16#include "chardev/char-fe.h"
e688df6b 17#include "qapi/error.h"
9af23989 18#include "qapi/qapi-commands-net.h"
03ce5744 19#include "qemu/config-file.h"
d314f586 20#include "qemu/error-report.h"
922a01a0 21#include "qemu/option.h"
69b32a6c 22#include "trace.h"
d314f586 23
703878e2 24typedef struct NetVhostUserState {
d314f586 25 NetClientState nc;
5d300164 26 CharBackend chr; /* only queue index 0 */
4d0cf552 27 VhostUserState *vhost_user;
d314f586 28 VHostNetState *vhost_net;
6f1de6b7 29 guint watch;
a463215b 30 uint64_t acked_features;
c89804d6 31 bool started;
703878e2 32} NetVhostUserState;
d314f586
NN
33
34VHostNetState *vhost_user_get_vhost_net(NetClientState *nc)
35{
703878e2 36 NetVhostUserState *s = DO_UPCAST(NetVhostUserState, nc, nc);
f394b2e2 37 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
d314f586
NN
38 return s->vhost_net;
39}
40
a463215b
MAL
41uint64_t vhost_user_get_acked_features(NetClientState *nc)
42{
703878e2 43 NetVhostUserState *s = DO_UPCAST(NetVhostUserState, nc, nc);
f394b2e2 44 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
a463215b
MAL
45 return s->acked_features;
46}
47
b931bfbf 48static void vhost_user_stop(int queues, NetClientState *ncs[])
d314f586 49{
703878e2 50 NetVhostUserState *s;
b931bfbf 51 int i;
d314f586 52
b931bfbf 53 for (i = 0; i < queues; i++) {
f394b2e2 54 assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
d314f586 55
703878e2 56 s = DO_UPCAST(NetVhostUserState, nc, ncs[i]);
d314f586 57
b931bfbf 58 if (s->vhost_net) {
a463215b 59 /* save acked features */
e6bcb1b6
MAL
60 uint64_t features = vhost_net_get_acked_features(s->vhost_net);
61 if (features) {
62 s->acked_features = features;
63 }
b931bfbf 64 vhost_net_cleanup(s->vhost_net);
b931bfbf
CO
65 }
66 }
d314f586
NN
67}
68
4d0cf552
TB
69static int vhost_user_start(int queues, NetClientState *ncs[],
70 VhostUserState *be)
d314f586 71{
b931bfbf 72 VhostNetOptions options;
e6bcb1b6 73 struct vhost_net *net = NULL;
703878e2 74 NetVhostUserState *s;
b931bfbf
CO
75 int max_queues;
76 int i;
77
78 options.backend_type = VHOST_BACKEND_TYPE_USER;
79
80 for (i = 0; i < queues; i++) {
f394b2e2 81 assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
b931bfbf 82
703878e2 83 s = DO_UPCAST(NetVhostUserState, nc, ncs[i]);
b931bfbf
CO
84
85 options.net_backend = ncs[i];
5d300164 86 options.opaque = be;
69e87b32 87 options.busyloop_timeout = 0;
6a756d14 88 options.nvqs = 2;
e6bcb1b6
MAL
89 net = vhost_net_init(&options);
90 if (!net) {
9af9e0fe 91 error_report("failed to init vhost_net for queue %d", i);
b931bfbf
CO
92 goto err;
93 }
94
95 if (i == 0) {
e6bcb1b6 96 max_queues = vhost_net_get_max_queues(net);
b931bfbf 97 if (queues > max_queues) {
9af9e0fe
MA
98 error_report("you are asking more queues than supported: %d",
99 max_queues);
b931bfbf
CO
100 goto err;
101 }
102 }
e6bcb1b6
MAL
103
104 if (s->vhost_net) {
105 vhost_net_cleanup(s->vhost_net);
106 g_free(s->vhost_net);
107 }
108 s->vhost_net = net;
d314f586
NN
109 }
110
b931bfbf
CO
111 return 0;
112
113err:
e6bcb1b6
MAL
114 if (net) {
115 vhost_net_cleanup(net);
a38a498d 116 g_free(net);
e6bcb1b6
MAL
117 }
118 vhost_user_stop(i, ncs);
b931bfbf 119 return -1;
d314f586
NN
120}
121
f6f56291
TC
122static ssize_t vhost_user_receive(NetClientState *nc, const uint8_t *buf,
123 size_t size)
124{
3e866365
TC
125 /* In case of RARP (message size is 60) notify backup to send a fake RARP.
126 This fake RARP will be sent by backend only for guest
127 without GUEST_ANNOUNCE capability.
f6f56291 128 */
3e866365 129 if (size == 60) {
703878e2 130 NetVhostUserState *s = DO_UPCAST(NetVhostUserState, nc, nc);
3e866365
TC
131 int r;
132 static int display_rarp_failure = 1;
133 char mac_addr[6];
134
135 /* extract guest mac address from the RARP message */
136 memcpy(mac_addr, &buf[6], 6);
137
138 r = vhost_net_notify_migration_done(s->vhost_net, mac_addr);
139
140 if ((r != 0) && (display_rarp_failure)) {
141 fprintf(stderr,
142 "Vhost user backend fails to broadcast fake RARP\n");
143 fflush(stderr);
144 display_rarp_failure = 0;
145 }
146 }
147
f6f56291
TC
148 return size;
149}
150
4d0cf552 151static void net_vhost_user_cleanup(NetClientState *nc)
d314f586 152{
703878e2 153 NetVhostUserState *s = DO_UPCAST(NetVhostUserState, nc, nc);
d314f586 154
b931bfbf
CO
155 if (s->vhost_net) {
156 vhost_net_cleanup(s->vhost_net);
e6bcb1b6 157 g_free(s->vhost_net);
b931bfbf
CO
158 s->vhost_net = NULL;
159 }
c39860e6 160 if (nc->queue_index == 0) {
41d4e5ec
YW
161 if (s->watch) {
162 g_source_remove(s->watch);
163 s->watch = 0;
164 }
1ce2610c 165 qemu_chr_fe_deinit(&s->chr, true);
4d0cf552
TB
166 if (s->vhost_user) {
167 vhost_user_cleanup(s->vhost_user);
168 g_free(s->vhost_user);
169 s->vhost_user = NULL;
170 }
25f0d2aa 171 }
b931bfbf 172
d314f586
NN
173 qemu_purge_queued_packets(nc);
174}
175
ba288898
PB
176static int vhost_user_set_vnet_endianness(NetClientState *nc,
177 bool enable)
178{
179 /* Nothing to do. If the server supports
180 * VHOST_USER_PROTOCOL_F_CROSS_ENDIAN, it will get the
181 * vnet header endianness from there. If it doesn't, negotiation
182 * fails.
183 */
184 return 0;
185}
186
d314f586
NN
187static bool vhost_user_has_vnet_hdr(NetClientState *nc)
188{
f394b2e2 189 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
d314f586
NN
190
191 return true;
192}
193
194static bool vhost_user_has_ufo(NetClientState *nc)
195{
f394b2e2 196 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
d314f586
NN
197
198 return true;
199}
200
5c485d51
KW
201static bool vhost_user_check_peer_type(NetClientState *nc, ObjectClass *oc,
202 Error **errp)
203{
204 const char *driver = object_class_get_name(oc);
205
206 if (!g_str_has_prefix(driver, "virtio-net-")) {
207 error_setg(errp, "vhost-user requires frontend driver virtio-net-*");
208 return false;
209 }
210
211 return true;
212}
213
d314f586 214static NetClientInfo net_vhost_user_info = {
f394b2e2 215 .type = NET_CLIENT_DRIVER_VHOST_USER,
703878e2 216 .size = sizeof(NetVhostUserState),
f6f56291 217 .receive = vhost_user_receive,
4d0cf552 218 .cleanup = net_vhost_user_cleanup,
d314f586
NN
219 .has_vnet_hdr = vhost_user_has_vnet_hdr,
220 .has_ufo = vhost_user_has_ufo,
ba288898
PB
221 .set_vnet_be = vhost_user_set_vnet_endianness,
222 .set_vnet_le = vhost_user_set_vnet_endianness,
5c485d51 223 .check_peer_type = vhost_user_check_peer_type,
d314f586
NN
224};
225
bf7b1eab
MAL
226static gboolean net_vhost_user_watch(void *do_not_use, GIOCondition cond,
227 void *opaque)
a6553598 228{
703878e2 229 NetVhostUserState *s = opaque;
a6553598 230
5345fdb4 231 qemu_chr_fe_disconnect(&s->chr);
a6553598 232
e7c83a88
MAL
233 return TRUE;
234}
235
083b266f 236static void net_vhost_user_event(void *opaque, QEMUChrEvent event);
e7c83a88
MAL
237
238static void chr_closed_bh(void *opaque)
239{
240 const char *name = opaque;
241 NetClientState *ncs[MAX_QUEUE_NUM];
703878e2 242 NetVhostUserState *s;
e7c83a88 243 Error *err = NULL;
f66337bd 244 int queues, i;
e7c83a88
MAL
245
246 queues = qemu_find_net_clients_except(name, ncs,
247 NET_CLIENT_DRIVER_NIC,
248 MAX_QUEUE_NUM);
249 assert(queues < MAX_QUEUE_NUM);
250
703878e2 251 s = DO_UPCAST(NetVhostUserState, nc, ncs[0]);
e7c83a88 252
f66337bd
HZ
253 for (i = queues -1; i >= 0; i--) {
254 s = DO_UPCAST(NetVhostUserState, nc, ncs[i]);
255
256 if (s->vhost_net) {
257 s->acked_features = vhost_net_get_acked_features(s->vhost_net);
258 }
c6beefd6
AM
259 }
260
e7c83a88 261 qmp_set_link(name, false, &err);
e7c83a88
MAL
262
263 qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, net_vhost_user_event,
81517ba3 264 NULL, opaque, NULL, true);
e7c83a88
MAL
265
266 if (err) {
267 error_report_err(err);
268 }
a6553598
TM
269}
270
083b266f 271static void net_vhost_user_event(void *opaque, QEMUChrEvent event)
d314f586 272{
b931bfbf
CO
273 const char *name = opaque;
274 NetClientState *ncs[MAX_QUEUE_NUM];
703878e2 275 NetVhostUserState *s;
0ec7b3e7 276 Chardev *chr;
b931bfbf
CO
277 Error *err = NULL;
278 int queues;
d314f586 279
b931bfbf 280 queues = qemu_find_net_clients_except(name, ncs,
f394b2e2 281 NET_CLIENT_DRIVER_NIC,
b931bfbf 282 MAX_QUEUE_NUM);
c1bf3531
MAL
283 assert(queues < MAX_QUEUE_NUM);
284
703878e2 285 s = DO_UPCAST(NetVhostUserState, nc, ncs[0]);
5345fdb4
MAL
286 chr = qemu_chr_fe_get_driver(&s->chr);
287 trace_vhost_user_event(chr->label, event);
d314f586
NN
288 switch (event) {
289 case CHR_EVENT_OPENED:
4d0cf552 290 if (vhost_user_start(queues, ncs, s->vhost_user) < 0) {
5345fdb4 291 qemu_chr_fe_disconnect(&s->chr);
0d572afd 292 return;
b931bfbf 293 }
e7c83a88
MAL
294 s->watch = qemu_chr_fe_add_watch(&s->chr, G_IO_HUP,
295 net_vhost_user_watch, s);
b931bfbf 296 qmp_set_link(name, true, &err);
c89804d6 297 s->started = true;
d314f586
NN
298 break;
299 case CHR_EVENT_CLOSED:
e7c83a88
MAL
300 /* a close event may happen during a read/write, but vhost
301 * code assumes the vhost_dev remains setup, so delay the
302 * stop & clear to idle.
303 * FIXME: better handle failure in vhost code, remove bh
304 */
305 if (s->watch) {
306 AioContext *ctx = qemu_get_current_aio_context();
307
308 g_source_remove(s->watch);
309 s->watch = 0;
81517ba3 310 qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, NULL, NULL,
e7c83a88
MAL
311 NULL, NULL, false);
312
313 aio_bh_schedule_oneshot(ctx, chr_closed_bh, opaque);
314 }
d314f586 315 break;
d0ab6769
PMD
316 case CHR_EVENT_BREAK:
317 case CHR_EVENT_MUX_IN:
318 case CHR_EVENT_MUX_OUT:
319 /* Ignore */
320 break;
d314f586 321 }
b931bfbf
CO
322
323 if (err) {
324 error_report_err(err);
325 }
d314f586
NN
326}
327
328static int net_vhost_user_init(NetClientState *peer, const char *device,
f9bb0c1f
JW
329 const char *name, Chardev *chr,
330 int queues)
d314f586 331{
32a6ebec 332 Error *err = NULL;
c89804d6 333 NetClientState *nc, *nc0 = NULL;
4d0cf552 334 NetVhostUserState *s = NULL;
0b99f224 335 VhostUserState *user;
b931bfbf 336 int i;
d314f586 337
c1bf3531
MAL
338 assert(name);
339 assert(queues > 0);
340
0b99f224 341 user = g_new0(struct VhostUserState, 1);
b931bfbf
CO
342 for (i = 0; i < queues; i++) {
343 nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name);
56e6f594
JW
344 snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user%d to %s",
345 i, chr->label);
b931bfbf 346 nc->queue_index = i;
5d300164
MAL
347 if (!nc0) {
348 nc0 = nc;
703878e2 349 s = DO_UPCAST(NetVhostUserState, nc, nc);
0b99f224
MAL
350 if (!qemu_chr_fe_init(&s->chr, chr, &err) ||
351 !vhost_user_init(user, &s->chr, &err)) {
5d300164 352 error_report_err(err);
4d0cf552 353 goto err;
5d300164 354 }
32a6ebec 355 }
4d0cf552
TB
356 s = DO_UPCAST(NetVhostUserState, nc, nc);
357 s->vhost_user = user;
b931bfbf 358 }
d345ed2d 359
703878e2 360 s = DO_UPCAST(NetVhostUserState, nc, nc0);
c89804d6 361 do {
5345fdb4 362 if (qemu_chr_fe_wait_connected(&s->chr, &err) < 0) {
c89804d6 363 error_report_err(err);
4d0cf552 364 goto err;
c89804d6 365 }
5345fdb4 366 qemu_chr_fe_set_handlers(&s->chr, NULL, NULL,
81517ba3
AN
367 net_vhost_user_event, NULL, nc0->name, NULL,
368 true);
c89804d6 369 } while (!s->started);
d314f586 370
1a5b68ce
MAL
371 assert(s->vhost_net);
372
d314f586 373 return 0;
4d0cf552
TB
374
375err:
376 if (user) {
377 vhost_user_cleanup(user);
378 g_free(user);
379 if (s) {
380 s->vhost_user = NULL;
381 }
382 }
c67daf4a 383 if (nc0) {
384 qemu_del_net_client(nc0);
385 }
4d0cf552
TB
386
387 return -1;
d314f586
NN
388}
389
0ec7b3e7 390static Chardev *net_vhost_claim_chardev(
81904831 391 const NetdevVhostUserOptions *opts, Error **errp)
03ce5744 392{
0ec7b3e7 393 Chardev *chr = qemu_chr_find(opts->chardev);
03ce5744
NN
394
395 if (chr == NULL) {
81904831 396 error_setg(errp, "chardev \"%s\" not found", opts->chardev);
03ce5744
NN
397 return NULL;
398 }
399
0a73336d
DB
400 if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) {
401 error_setg(errp, "chardev \"%s\" is not reconnectable",
402 opts->chardev);
03ce5744
NN
403 return NULL;
404 }
0a73336d
DB
405 if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_FD_PASS)) {
406 error_setg(errp, "chardev \"%s\" does not support FD passing",
81904831 407 opts->chardev);
03ce5744
NN
408 return NULL;
409 }
410
03ce5744
NN
411 return chr;
412}
413
cebea510 414int net_init_vhost_user(const Netdev *netdev, const char *name,
a30ecde6 415 NetClientState *peer, Error **errp)
d314f586 416{
b931bfbf 417 int queues;
03ce5744 418 const NetdevVhostUserOptions *vhost_user_opts;
0ec7b3e7 419 Chardev *chr;
03ce5744 420
f394b2e2
EB
421 assert(netdev->type == NET_CLIENT_DRIVER_VHOST_USER);
422 vhost_user_opts = &netdev->u.vhost_user;
03ce5744 423
0a73336d 424 chr = net_vhost_claim_chardev(vhost_user_opts, errp);
03ce5744 425 if (!chr) {
03ce5744
NN
426 return -1;
427 }
428
b931bfbf 429 queues = vhost_user_opts->has_queues ? vhost_user_opts->queues : 1;
fff4e48e 430 if (queues < 1 || queues > MAX_QUEUE_NUM) {
6f6f9512 431 error_setg(errp,
fff4e48e
IM
432 "vhost-user number of queues must be in range [1, %d]",
433 MAX_QUEUE_NUM);
6f6f9512
VK
434 return -1;
435 }
03ce5744 436
f9bb0c1f 437 return net_vhost_user_init(peer, "vhost_user", name, chr, queues);
d314f586 438}