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.
11 #include "qemu/osdep.h"
13 #include "net/vhost_net.h"
14 #include "net/vhost-user.h"
15 #include "chardev/char-fe.h"
16 #include "qapi/error.h"
17 #include "qapi/qapi-commands-net.h"
18 #include "qemu/config-file.h"
19 #include "qemu/error-report.h"
20 #include "qemu/option.h"
23 typedef struct NetVhostUserState {
25 CharBackend chr; /* only queue index 0 */
26 VHostNetState *vhost_net;
28 uint64_t acked_features;
32 VHostNetState *vhost_user_get_vhost_net(NetClientState *nc)
34 NetVhostUserState *s = DO_UPCAST(NetVhostUserState, nc, nc);
35 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
39 uint64_t vhost_user_get_acked_features(NetClientState *nc)
41 NetVhostUserState *s = DO_UPCAST(NetVhostUserState, nc, nc);
42 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
43 return s->acked_features;
46 static void vhost_user_stop(int queues, NetClientState *ncs[])
51 for (i = 0; i < queues; i++) {
52 assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
54 s = DO_UPCAST(NetVhostUserState, nc, ncs[i]);
57 /* save acked features */
58 uint64_t features = vhost_net_get_acked_features(s->vhost_net);
60 s->acked_features = features;
62 vhost_net_cleanup(s->vhost_net);
67 static int vhost_user_start(int queues, NetClientState *ncs[], CharBackend *be)
69 VhostNetOptions options;
70 struct vhost_net *net = NULL;
75 options.backend_type = VHOST_BACKEND_TYPE_USER;
77 for (i = 0; i < queues; i++) {
78 assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
80 s = DO_UPCAST(NetVhostUserState, nc, ncs[i]);
82 options.net_backend = ncs[i];
84 options.busyloop_timeout = 0;
85 net = vhost_net_init(&options);
87 error_report("failed to init vhost_net for queue %d", i);
92 max_queues = vhost_net_get_max_queues(net);
93 if (queues > max_queues) {
94 error_report("you are asking more queues than supported: %d",
101 vhost_net_cleanup(s->vhost_net);
102 g_free(s->vhost_net);
111 vhost_net_cleanup(net);
114 vhost_user_stop(i, ncs);
118 static ssize_t vhost_user_receive(NetClientState *nc, const uint8_t *buf,
121 /* In case of RARP (message size is 60) notify backup to send a fake RARP.
122 This fake RARP will be sent by backend only for guest
123 without GUEST_ANNOUNCE capability.
126 NetVhostUserState *s = DO_UPCAST(NetVhostUserState, nc, nc);
128 static int display_rarp_failure = 1;
131 /* extract guest mac address from the RARP message */
132 memcpy(mac_addr, &buf[6], 6);
134 r = vhost_net_notify_migration_done(s->vhost_net, mac_addr);
136 if ((r != 0) && (display_rarp_failure)) {
138 "Vhost user backend fails to broadcast fake RARP\n");
140 display_rarp_failure = 0;
147 static void vhost_user_cleanup(NetClientState *nc)
149 NetVhostUserState *s = DO_UPCAST(NetVhostUserState, nc, nc);
152 vhost_net_cleanup(s->vhost_net);
153 g_free(s->vhost_net);
156 if (nc->queue_index == 0) {
158 g_source_remove(s->watch);
161 qemu_chr_fe_deinit(&s->chr, true);
164 qemu_purge_queued_packets(nc);
167 static bool vhost_user_has_vnet_hdr(NetClientState *nc)
169 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
174 static bool vhost_user_has_ufo(NetClientState *nc)
176 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
181 static NetClientInfo net_vhost_user_info = {
182 .type = NET_CLIENT_DRIVER_VHOST_USER,
183 .size = sizeof(NetVhostUserState),
184 .receive = vhost_user_receive,
185 .cleanup = vhost_user_cleanup,
186 .has_vnet_hdr = vhost_user_has_vnet_hdr,
187 .has_ufo = vhost_user_has_ufo,
190 static gboolean net_vhost_user_watch(GIOChannel *chan, GIOCondition cond,
193 NetVhostUserState *s = opaque;
195 qemu_chr_fe_disconnect(&s->chr);
200 static void net_vhost_user_event(void *opaque, int event);
202 static void chr_closed_bh(void *opaque)
204 const char *name = opaque;
205 NetClientState *ncs[MAX_QUEUE_NUM];
206 NetVhostUserState *s;
210 queues = qemu_find_net_clients_except(name, ncs,
211 NET_CLIENT_DRIVER_NIC,
213 assert(queues < MAX_QUEUE_NUM);
215 s = DO_UPCAST(NetVhostUserState, nc, ncs[0]);
217 qmp_set_link(name, false, &err);
218 vhost_user_stop(queues, ncs);
220 qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, net_vhost_user_event,
221 NULL, opaque, NULL, true);
224 error_report_err(err);
228 static void net_vhost_user_event(void *opaque, int event)
230 const char *name = opaque;
231 NetClientState *ncs[MAX_QUEUE_NUM];
232 NetVhostUserState *s;
237 queues = qemu_find_net_clients_except(name, ncs,
238 NET_CLIENT_DRIVER_NIC,
240 assert(queues < MAX_QUEUE_NUM);
242 s = DO_UPCAST(NetVhostUserState, nc, ncs[0]);
243 chr = qemu_chr_fe_get_driver(&s->chr);
244 trace_vhost_user_event(chr->label, event);
246 case CHR_EVENT_OPENED:
247 if (vhost_user_start(queues, ncs, &s->chr) < 0) {
248 qemu_chr_fe_disconnect(&s->chr);
251 s->watch = qemu_chr_fe_add_watch(&s->chr, G_IO_HUP,
252 net_vhost_user_watch, s);
253 qmp_set_link(name, true, &err);
256 case CHR_EVENT_CLOSED:
257 /* a close event may happen during a read/write, but vhost
258 * code assumes the vhost_dev remains setup, so delay the
259 * stop & clear to idle.
260 * FIXME: better handle failure in vhost code, remove bh
263 AioContext *ctx = qemu_get_current_aio_context();
265 g_source_remove(s->watch);
267 qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, NULL, NULL,
270 aio_bh_schedule_oneshot(ctx, chr_closed_bh, opaque);
276 error_report_err(err);
280 static int net_vhost_user_init(NetClientState *peer, const char *device,
281 const char *name, Chardev *chr,
285 NetClientState *nc, *nc0 = NULL;
286 NetVhostUserState *s;
292 for (i = 0; i < queues; i++) {
293 nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name);
294 snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user%d to %s",
299 s = DO_UPCAST(NetVhostUserState, nc, nc);
300 if (!qemu_chr_fe_init(&s->chr, chr, &err)) {
301 error_report_err(err);
308 s = DO_UPCAST(NetVhostUserState, nc, nc0);
310 if (qemu_chr_fe_wait_connected(&s->chr, &err) < 0) {
311 error_report_err(err);
314 qemu_chr_fe_set_handlers(&s->chr, NULL, NULL,
315 net_vhost_user_event, NULL, nc0->name, NULL,
317 } while (!s->started);
319 assert(s->vhost_net);
324 static Chardev *net_vhost_claim_chardev(
325 const NetdevVhostUserOptions *opts, Error **errp)
327 Chardev *chr = qemu_chr_find(opts->chardev);
330 error_setg(errp, "chardev \"%s\" not found", opts->chardev);
334 if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) {
335 error_setg(errp, "chardev \"%s\" is not reconnectable",
339 if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_FD_PASS)) {
340 error_setg(errp, "chardev \"%s\" does not support FD passing",
348 static int net_vhost_check_net(void *opaque, QemuOpts *opts, Error **errp)
350 const char *name = opaque;
351 const char *driver, *netdev;
353 driver = qemu_opt_get(opts, "driver");
354 netdev = qemu_opt_get(opts, "netdev");
356 if (!driver || !netdev) {
360 if (strcmp(netdev, name) == 0 &&
361 !g_str_has_prefix(driver, "virtio-net-")) {
362 error_setg(errp, "vhost-user requires frontend driver virtio-net-*");
369 int net_init_vhost_user(const Netdev *netdev, const char *name,
370 NetClientState *peer, Error **errp)
373 const NetdevVhostUserOptions *vhost_user_opts;
376 assert(netdev->type == NET_CLIENT_DRIVER_VHOST_USER);
377 vhost_user_opts = &netdev->u.vhost_user;
379 chr = net_vhost_claim_chardev(vhost_user_opts, errp);
384 /* verify net frontend */
385 if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net,
386 (char *)name, errp)) {
390 queues = vhost_user_opts->has_queues ? vhost_user_opts->queues : 1;
391 if (queues < 1 || queues > MAX_QUEUE_NUM) {
393 "vhost-user number of queues must be in range [1, %d]",
398 return net_vhost_user_init(peer, "vhost_user", name, chr, queues);