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 "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"
24 typedef struct NetVhostUserState
{
26 CharBackend chr
; /* only queue index 0 */
27 VhostUserState
*vhost_user
;
28 VHostNetState
*vhost_net
;
30 uint64_t acked_features
;
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
);
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
[])
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
]);
59 /* save acked features */
60 uint64_t features
= vhost_net_get_acked_features(s
->vhost_net
);
62 s
->acked_features
= features
;
64 vhost_net_cleanup(s
->vhost_net
);
69 static int vhost_user_start(int queues
, NetClientState
*ncs
[],
72 VhostNetOptions options
;
73 struct vhost_net
*net
= NULL
;
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
];
87 options
.busyloop_timeout
= 0;
88 net
= vhost_net_init(&options
);
90 error_report("failed to init vhost_net for queue %d", i
);
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",
104 vhost_net_cleanup(s
->vhost_net
);
105 g_free(s
->vhost_net
);
114 vhost_net_cleanup(net
);
117 vhost_user_stop(i
, ncs
);
121 static ssize_t
vhost_user_receive(NetClientState
*nc
, const uint8_t *buf
,
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.
129 NetVhostUserState
*s
= DO_UPCAST(NetVhostUserState
, nc
, nc
);
131 static int display_rarp_failure
= 1;
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
)) {
141 "Vhost user backend fails to broadcast fake RARP\n");
143 display_rarp_failure
= 0;
150 static void net_vhost_user_cleanup(NetClientState
*nc
)
152 NetVhostUserState
*s
= DO_UPCAST(NetVhostUserState
, nc
, nc
);
155 vhost_net_cleanup(s
->vhost_net
);
156 g_free(s
->vhost_net
);
159 if (nc
->queue_index
== 0) {
161 g_source_remove(s
->watch
);
164 qemu_chr_fe_deinit(&s
->chr
, true);
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 bool vhost_user_has_vnet_hdr(NetClientState
*nc
)
177 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_USER
);
182 static bool vhost_user_has_ufo(NetClientState
*nc
)
184 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_USER
);
189 static NetClientInfo net_vhost_user_info
= {
190 .type
= NET_CLIENT_DRIVER_VHOST_USER
,
191 .size
= sizeof(NetVhostUserState
),
192 .receive
= vhost_user_receive
,
193 .cleanup
= net_vhost_user_cleanup
,
194 .has_vnet_hdr
= vhost_user_has_vnet_hdr
,
195 .has_ufo
= vhost_user_has_ufo
,
198 static gboolean
net_vhost_user_watch(GIOChannel
*chan
, GIOCondition cond
,
201 NetVhostUserState
*s
= opaque
;
203 qemu_chr_fe_disconnect(&s
->chr
);
208 static void net_vhost_user_event(void *opaque
, int event
);
210 static void chr_closed_bh(void *opaque
)
212 const char *name
= opaque
;
213 NetClientState
*ncs
[MAX_QUEUE_NUM
];
214 NetVhostUserState
*s
;
218 queues
= qemu_find_net_clients_except(name
, ncs
,
219 NET_CLIENT_DRIVER_NIC
,
221 assert(queues
< MAX_QUEUE_NUM
);
223 s
= DO_UPCAST(NetVhostUserState
, nc
, ncs
[0]);
225 qmp_set_link(name
, false, &err
);
226 vhost_user_stop(queues
, ncs
);
228 qemu_chr_fe_set_handlers(&s
->chr
, NULL
, NULL
, net_vhost_user_event
,
229 NULL
, opaque
, NULL
, true);
232 error_report_err(err
);
236 static void net_vhost_user_event(void *opaque
, int event
)
238 const char *name
= opaque
;
239 NetClientState
*ncs
[MAX_QUEUE_NUM
];
240 NetVhostUserState
*s
;
245 queues
= qemu_find_net_clients_except(name
, ncs
,
246 NET_CLIENT_DRIVER_NIC
,
248 assert(queues
< MAX_QUEUE_NUM
);
250 s
= DO_UPCAST(NetVhostUserState
, nc
, ncs
[0]);
251 chr
= qemu_chr_fe_get_driver(&s
->chr
);
252 trace_vhost_user_event(chr
->label
, event
);
254 case CHR_EVENT_OPENED
:
255 if (vhost_user_start(queues
, ncs
, s
->vhost_user
) < 0) {
256 qemu_chr_fe_disconnect(&s
->chr
);
259 s
->watch
= qemu_chr_fe_add_watch(&s
->chr
, G_IO_HUP
,
260 net_vhost_user_watch
, s
);
261 qmp_set_link(name
, true, &err
);
264 case CHR_EVENT_CLOSED
:
265 /* a close event may happen during a read/write, but vhost
266 * code assumes the vhost_dev remains setup, so delay the
267 * stop & clear to idle.
268 * FIXME: better handle failure in vhost code, remove bh
271 AioContext
*ctx
= qemu_get_current_aio_context();
273 g_source_remove(s
->watch
);
275 qemu_chr_fe_set_handlers(&s
->chr
, NULL
, NULL
, NULL
, NULL
,
278 aio_bh_schedule_oneshot(ctx
, chr_closed_bh
, opaque
);
284 error_report_err(err
);
288 static int net_vhost_user_init(NetClientState
*peer
, const char *device
,
289 const char *name
, Chardev
*chr
,
293 NetClientState
*nc
, *nc0
= NULL
;
294 VhostUserState
*user
= NULL
;
295 NetVhostUserState
*s
= NULL
;
301 user
= vhost_user_init();
303 error_report("failed to init vhost_user");
307 for (i
= 0; i
< queues
; i
++) {
308 nc
= qemu_new_net_client(&net_vhost_user_info
, peer
, device
, name
);
309 snprintf(nc
->info_str
, sizeof(nc
->info_str
), "vhost-user%d to %s",
314 s
= DO_UPCAST(NetVhostUserState
, nc
, nc
);
315 if (!qemu_chr_fe_init(&s
->chr
, chr
, &err
)) {
316 error_report_err(err
);
321 s
= DO_UPCAST(NetVhostUserState
, nc
, nc
);
322 s
->vhost_user
= user
;
325 s
= DO_UPCAST(NetVhostUserState
, nc
, nc0
);
327 if (qemu_chr_fe_wait_connected(&s
->chr
, &err
) < 0) {
328 error_report_err(err
);
331 qemu_chr_fe_set_handlers(&s
->chr
, NULL
, NULL
,
332 net_vhost_user_event
, NULL
, nc0
->name
, NULL
,
334 } while (!s
->started
);
336 assert(s
->vhost_net
);
342 vhost_user_cleanup(user
);
345 s
->vhost_user
= NULL
;
349 qemu_del_net_client(nc0
);
355 static Chardev
*net_vhost_claim_chardev(
356 const NetdevVhostUserOptions
*opts
, Error
**errp
)
358 Chardev
*chr
= qemu_chr_find(opts
->chardev
);
361 error_setg(errp
, "chardev \"%s\" not found", opts
->chardev
);
365 if (!qemu_chr_has_feature(chr
, QEMU_CHAR_FEATURE_RECONNECTABLE
)) {
366 error_setg(errp
, "chardev \"%s\" is not reconnectable",
370 if (!qemu_chr_has_feature(chr
, QEMU_CHAR_FEATURE_FD_PASS
)) {
371 error_setg(errp
, "chardev \"%s\" does not support FD passing",
379 static int net_vhost_check_net(void *opaque
, QemuOpts
*opts
, Error
**errp
)
381 const char *name
= opaque
;
382 const char *driver
, *netdev
;
384 driver
= qemu_opt_get(opts
, "driver");
385 netdev
= qemu_opt_get(opts
, "netdev");
387 if (!driver
|| !netdev
) {
391 if (strcmp(netdev
, name
) == 0 &&
392 !g_str_has_prefix(driver
, "virtio-net-")) {
393 error_setg(errp
, "vhost-user requires frontend driver virtio-net-*");
400 int net_init_vhost_user(const Netdev
*netdev
, const char *name
,
401 NetClientState
*peer
, Error
**errp
)
404 const NetdevVhostUserOptions
*vhost_user_opts
;
407 assert(netdev
->type
== NET_CLIENT_DRIVER_VHOST_USER
);
408 vhost_user_opts
= &netdev
->u
.vhost_user
;
410 chr
= net_vhost_claim_chardev(vhost_user_opts
, errp
);
415 /* verify net frontend */
416 if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net
,
417 (char *)name
, errp
)) {
421 queues
= vhost_user_opts
->has_queues
? vhost_user_opts
->queues
: 1;
422 if (queues
< 1 || queues
> MAX_QUEUE_NUM
) {
424 "vhost-user number of queues must be in range [1, %d]",
429 return net_vhost_user_init(peer
, "vhost_user", name
, chr
, queues
);