roms: Allow passing configure options to the EDK2 build tools
[qemu/ar7.git] / net / vhost-user.c
blob5a26a24708af404ac0d8c5c383d8c572f35196d2
1 /*
2 * vhost-user.c
4 * Copyright (c) 2013 Virtual Open Systems Sarl.
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.
9 */
11 #include "qemu/osdep.h"
12 #include "clients.h"
13 #include "net/vhost_net.h"
14 #include "net/vhost-user.h"
15 #include "hw/virtio/vhost-user.h"
16 #include "chardev/char-fe.h"
17 #include "qapi/error.h"
18 #include "qapi/qapi-commands-net.h"
19 #include "qemu/config-file.h"
20 #include "qemu/error-report.h"
21 #include "qemu/option.h"
22 #include "trace.h"
24 typedef struct NetVhostUserState {
25 NetClientState nc;
26 CharBackend chr; /* only queue index 0 */
27 VhostUserState *vhost_user;
28 VHostNetState *vhost_net;
29 guint watch;
30 uint64_t acked_features;
31 bool started;
32 } NetVhostUserState;
34 VHostNetState *vhost_user_get_vhost_net(NetClientState *nc)
36 NetVhostUserState *s = DO_UPCAST(NetVhostUserState, nc, nc);
37 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
38 return s->vhost_net;
41 uint64_t vhost_user_get_acked_features(NetClientState *nc)
43 NetVhostUserState *s = DO_UPCAST(NetVhostUserState, nc, nc);
44 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
45 return s->acked_features;
48 static void vhost_user_stop(int queues, NetClientState *ncs[])
50 NetVhostUserState *s;
51 int i;
53 for (i = 0; i < queues; i++) {
54 assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
56 s = DO_UPCAST(NetVhostUserState, nc, ncs[i]);
58 if (s->vhost_net) {
59 /* save acked features */
60 uint64_t features = vhost_net_get_acked_features(s->vhost_net);
61 if (features) {
62 s->acked_features = features;
64 vhost_net_cleanup(s->vhost_net);
69 static int vhost_user_start(int queues, NetClientState *ncs[],
70 VhostUserState *be)
72 VhostNetOptions options;
73 struct vhost_net *net = NULL;
74 NetVhostUserState *s;
75 int max_queues;
76 int i;
78 options.backend_type = VHOST_BACKEND_TYPE_USER;
80 for (i = 0; i < queues; i++) {
81 assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
83 s = DO_UPCAST(NetVhostUserState, nc, ncs[i]);
85 options.net_backend = ncs[i];
86 options.opaque = be;
87 options.busyloop_timeout = 0;
88 net = vhost_net_init(&options);
89 if (!net) {
90 error_report("failed to init vhost_net for queue %d", i);
91 goto err;
94 if (i == 0) {
95 max_queues = vhost_net_get_max_queues(net);
96 if (queues > max_queues) {
97 error_report("you are asking more queues than supported: %d",
98 max_queues);
99 goto err;
103 if (s->vhost_net) {
104 vhost_net_cleanup(s->vhost_net);
105 g_free(s->vhost_net);
107 s->vhost_net = net;
110 return 0;
112 err:
113 if (net) {
114 vhost_net_cleanup(net);
115 g_free(net);
117 vhost_user_stop(i, ncs);
118 return -1;
121 static ssize_t vhost_user_receive(NetClientState *nc, const uint8_t *buf,
122 size_t size)
124 /* In case of RARP (message size is 60) notify backup to send a fake RARP.
125 This fake RARP will be sent by backend only for guest
126 without GUEST_ANNOUNCE capability.
128 if (size == 60) {
129 NetVhostUserState *s = DO_UPCAST(NetVhostUserState, nc, nc);
130 int r;
131 static int display_rarp_failure = 1;
132 char mac_addr[6];
134 /* extract guest mac address from the RARP message */
135 memcpy(mac_addr, &buf[6], 6);
137 r = vhost_net_notify_migration_done(s->vhost_net, mac_addr);
139 if ((r != 0) && (display_rarp_failure)) {
140 fprintf(stderr,
141 "Vhost user backend fails to broadcast fake RARP\n");
142 fflush(stderr);
143 display_rarp_failure = 0;
147 return size;
150 static void net_vhost_user_cleanup(NetClientState *nc)
152 NetVhostUserState *s = DO_UPCAST(NetVhostUserState, nc, nc);
154 if (s->vhost_net) {
155 vhost_net_cleanup(s->vhost_net);
156 g_free(s->vhost_net);
157 s->vhost_net = NULL;
159 if (nc->queue_index == 0) {
160 if (s->watch) {
161 g_source_remove(s->watch);
162 s->watch = 0;
164 qemu_chr_fe_deinit(&s->chr, true);
165 if (s->vhost_user) {
166 vhost_user_cleanup(s->vhost_user);
167 g_free(s->vhost_user);
168 s->vhost_user = NULL;
172 qemu_purge_queued_packets(nc);
175 static int vhost_user_set_vnet_endianness(NetClientState *nc,
176 bool enable)
178 /* Nothing to do. If the server supports
179 * VHOST_USER_PROTOCOL_F_CROSS_ENDIAN, it will get the
180 * vnet header endianness from there. If it doesn't, negotiation
181 * fails.
183 return 0;
186 static bool vhost_user_has_vnet_hdr(NetClientState *nc)
188 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
190 return true;
193 static bool vhost_user_has_ufo(NetClientState *nc)
195 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
197 return true;
200 static NetClientInfo net_vhost_user_info = {
201 .type = NET_CLIENT_DRIVER_VHOST_USER,
202 .size = sizeof(NetVhostUserState),
203 .receive = vhost_user_receive,
204 .cleanup = net_vhost_user_cleanup,
205 .has_vnet_hdr = vhost_user_has_vnet_hdr,
206 .has_ufo = vhost_user_has_ufo,
207 .set_vnet_be = vhost_user_set_vnet_endianness,
208 .set_vnet_le = vhost_user_set_vnet_endianness,
211 static gboolean net_vhost_user_watch(GIOChannel *chan, GIOCondition cond,
212 void *opaque)
214 NetVhostUserState *s = opaque;
216 qemu_chr_fe_disconnect(&s->chr);
218 return TRUE;
221 static void net_vhost_user_event(void *opaque, int event);
223 static void chr_closed_bh(void *opaque)
225 const char *name = opaque;
226 NetClientState *ncs[MAX_QUEUE_NUM];
227 NetVhostUserState *s;
228 Error *err = NULL;
229 int queues;
231 queues = qemu_find_net_clients_except(name, ncs,
232 NET_CLIENT_DRIVER_NIC,
233 MAX_QUEUE_NUM);
234 assert(queues < MAX_QUEUE_NUM);
236 s = DO_UPCAST(NetVhostUserState, nc, ncs[0]);
238 qmp_set_link(name, false, &err);
239 vhost_user_stop(queues, ncs);
241 qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, net_vhost_user_event,
242 NULL, opaque, NULL, true);
244 if (err) {
245 error_report_err(err);
249 static void net_vhost_user_event(void *opaque, int event)
251 const char *name = opaque;
252 NetClientState *ncs[MAX_QUEUE_NUM];
253 NetVhostUserState *s;
254 Chardev *chr;
255 Error *err = NULL;
256 int queues;
258 queues = qemu_find_net_clients_except(name, ncs,
259 NET_CLIENT_DRIVER_NIC,
260 MAX_QUEUE_NUM);
261 assert(queues < MAX_QUEUE_NUM);
263 s = DO_UPCAST(NetVhostUserState, nc, ncs[0]);
264 chr = qemu_chr_fe_get_driver(&s->chr);
265 trace_vhost_user_event(chr->label, event);
266 switch (event) {
267 case CHR_EVENT_OPENED:
268 if (vhost_user_start(queues, ncs, s->vhost_user) < 0) {
269 qemu_chr_fe_disconnect(&s->chr);
270 return;
272 s->watch = qemu_chr_fe_add_watch(&s->chr, G_IO_HUP,
273 net_vhost_user_watch, s);
274 qmp_set_link(name, true, &err);
275 s->started = true;
276 break;
277 case CHR_EVENT_CLOSED:
278 /* a close event may happen during a read/write, but vhost
279 * code assumes the vhost_dev remains setup, so delay the
280 * stop & clear to idle.
281 * FIXME: better handle failure in vhost code, remove bh
283 if (s->watch) {
284 AioContext *ctx = qemu_get_current_aio_context();
286 g_source_remove(s->watch);
287 s->watch = 0;
288 qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, NULL, NULL,
289 NULL, NULL, false);
291 aio_bh_schedule_oneshot(ctx, chr_closed_bh, opaque);
293 break;
296 if (err) {
297 error_report_err(err);
301 static int net_vhost_user_init(NetClientState *peer, const char *device,
302 const char *name, Chardev *chr,
303 int queues)
305 Error *err = NULL;
306 NetClientState *nc, *nc0 = NULL;
307 NetVhostUserState *s = NULL;
308 VhostUserState *user;
309 int i;
311 assert(name);
312 assert(queues > 0);
314 user = g_new0(struct VhostUserState, 1);
315 for (i = 0; i < queues; i++) {
316 nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name);
317 snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user%d to %s",
318 i, chr->label);
319 nc->queue_index = i;
320 if (!nc0) {
321 nc0 = nc;
322 s = DO_UPCAST(NetVhostUserState, nc, nc);
323 if (!qemu_chr_fe_init(&s->chr, chr, &err) ||
324 !vhost_user_init(user, &s->chr, &err)) {
325 error_report_err(err);
326 goto err;
329 s = DO_UPCAST(NetVhostUserState, nc, nc);
330 s->vhost_user = user;
333 s = DO_UPCAST(NetVhostUserState, nc, nc0);
334 do {
335 if (qemu_chr_fe_wait_connected(&s->chr, &err) < 0) {
336 error_report_err(err);
337 goto err;
339 qemu_chr_fe_set_handlers(&s->chr, NULL, NULL,
340 net_vhost_user_event, NULL, nc0->name, NULL,
341 true);
342 } while (!s->started);
344 assert(s->vhost_net);
346 return 0;
348 err:
349 if (user) {
350 vhost_user_cleanup(user);
351 g_free(user);
352 if (s) {
353 s->vhost_user = NULL;
356 if (nc0) {
357 qemu_del_net_client(nc0);
360 return -1;
363 static Chardev *net_vhost_claim_chardev(
364 const NetdevVhostUserOptions *opts, Error **errp)
366 Chardev *chr = qemu_chr_find(opts->chardev);
368 if (chr == NULL) {
369 error_setg(errp, "chardev \"%s\" not found", opts->chardev);
370 return NULL;
373 if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) {
374 error_setg(errp, "chardev \"%s\" is not reconnectable",
375 opts->chardev);
376 return NULL;
378 if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_FD_PASS)) {
379 error_setg(errp, "chardev \"%s\" does not support FD passing",
380 opts->chardev);
381 return NULL;
384 return chr;
387 static int net_vhost_check_net(void *opaque, QemuOpts *opts, Error **errp)
389 const char *name = opaque;
390 const char *driver, *netdev;
392 driver = qemu_opt_get(opts, "driver");
393 netdev = qemu_opt_get(opts, "netdev");
395 if (!driver || !netdev) {
396 return 0;
399 if (strcmp(netdev, name) == 0 &&
400 !g_str_has_prefix(driver, "virtio-net-")) {
401 error_setg(errp, "vhost-user requires frontend driver virtio-net-*");
402 return -1;
405 return 0;
408 int net_init_vhost_user(const Netdev *netdev, const char *name,
409 NetClientState *peer, Error **errp)
411 int queues;
412 const NetdevVhostUserOptions *vhost_user_opts;
413 Chardev *chr;
415 assert(netdev->type == NET_CLIENT_DRIVER_VHOST_USER);
416 vhost_user_opts = &netdev->u.vhost_user;
418 chr = net_vhost_claim_chardev(vhost_user_opts, errp);
419 if (!chr) {
420 return -1;
423 /* verify net frontend */
424 if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net,
425 (char *)name, errp)) {
426 return -1;
429 queues = vhost_user_opts->has_queues ? vhost_user_opts->queues : 1;
430 if (queues < 1 || queues > MAX_QUEUE_NUM) {
431 error_setg(errp,
432 "vhost-user number of queues must be in range [1, %d]",
433 MAX_QUEUE_NUM);
434 return -1;
437 return net_vhost_user_init(peer, "vhost_user", name, chr, queues);