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 "sysemu/char.h"
16 #include "qemu/config-file.h"
17 #include "qemu/error-report.h"
18 #include "qmp-commands.h"
21 typedef struct VhostUserState
{
24 VHostNetState
*vhost_net
;
26 uint64_t acked_features
;
30 typedef struct VhostUserChardevProps
{
33 } VhostUserChardevProps
;
35 VHostNetState
*vhost_user_get_vhost_net(NetClientState
*nc
)
37 VhostUserState
*s
= DO_UPCAST(VhostUserState
, nc
, nc
);
38 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_USER
);
42 uint64_t vhost_user_get_acked_features(NetClientState
*nc
)
44 VhostUserState
*s
= DO_UPCAST(VhostUserState
, nc
, nc
);
45 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_USER
);
46 return s
->acked_features
;
49 static void vhost_user_stop(int queues
, NetClientState
*ncs
[])
54 for (i
= 0; i
< queues
; i
++) {
55 assert(ncs
[i
]->info
->type
== NET_CLIENT_DRIVER_VHOST_USER
);
57 s
= DO_UPCAST(VhostUserState
, nc
, ncs
[i
]);
60 /* save acked features */
61 uint64_t features
= vhost_net_get_acked_features(s
->vhost_net
);
63 s
->acked_features
= features
;
65 vhost_net_cleanup(s
->vhost_net
);
70 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(VhostUserState
, nc
, ncs
[i
]);
85 options
.net_backend
= ncs
[i
];
86 options
.opaque
= s
->chr
;
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
);
116 vhost_user_stop(i
, ncs
);
120 static ssize_t
vhost_user_receive(NetClientState
*nc
, const uint8_t *buf
,
123 /* In case of RARP (message size is 60) notify backup to send a fake RARP.
124 This fake RARP will be sent by backend only for guest
125 without GUEST_ANNOUNCE capability.
128 VhostUserState
*s
= DO_UPCAST(VhostUserState
, nc
, nc
);
130 static int display_rarp_failure
= 1;
133 /* extract guest mac address from the RARP message */
134 memcpy(mac_addr
, &buf
[6], 6);
136 r
= vhost_net_notify_migration_done(s
->vhost_net
, mac_addr
);
138 if ((r
!= 0) && (display_rarp_failure
)) {
140 "Vhost user backend fails to broadcast fake RARP\n");
142 display_rarp_failure
= 0;
149 static void vhost_user_cleanup(NetClientState
*nc
)
151 VhostUserState
*s
= DO_UPCAST(VhostUserState
, nc
, nc
);
154 vhost_net_cleanup(s
->vhost_net
);
155 g_free(s
->vhost_net
);
159 qemu_chr_add_handlers(s
->chr
, NULL
, NULL
, NULL
, NULL
);
160 qemu_chr_fe_release(s
->chr
);
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(VhostUserState
),
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 VhostUserState
*s
= opaque
;
195 qemu_chr_disconnect(s
->chr
);
200 static void net_vhost_user_event(void *opaque
, int event
)
202 const char *name
= opaque
;
203 NetClientState
*ncs
[MAX_QUEUE_NUM
];
208 queues
= qemu_find_net_clients_except(name
, ncs
,
209 NET_CLIENT_DRIVER_NIC
,
211 assert(queues
< MAX_QUEUE_NUM
);
213 s
= DO_UPCAST(VhostUserState
, nc
, ncs
[0]);
214 trace_vhost_user_event(s
->chr
->label
, event
);
216 case CHR_EVENT_OPENED
:
217 s
->watch
= qemu_chr_fe_add_watch(s
->chr
, G_IO_HUP
,
218 net_vhost_user_watch
, s
);
219 if (vhost_user_start(queues
, ncs
) < 0) {
220 qemu_chr_disconnect(s
->chr
);
223 qmp_set_link(name
, true, &err
);
226 case CHR_EVENT_CLOSED
:
227 qmp_set_link(name
, false, &err
);
228 vhost_user_stop(queues
, ncs
);
229 g_source_remove(s
->watch
);
235 error_report_err(err
);
239 static int net_vhost_user_init(NetClientState
*peer
, const char *device
,
240 const char *name
, CharDriverState
*chr
,
243 NetClientState
*nc
, *nc0
= NULL
;
250 for (i
= 0; i
< queues
; i
++) {
251 nc
= qemu_new_net_client(&net_vhost_user_info
, peer
, device
, name
);
256 snprintf(nc
->info_str
, sizeof(nc
->info_str
), "vhost-user%d to %s",
261 s
= DO_UPCAST(VhostUserState
, nc
, nc
);
265 s
= DO_UPCAST(VhostUserState
, nc
, nc0
);
268 if (qemu_chr_wait_connected(chr
, &err
) < 0) {
269 error_report_err(err
);
272 qemu_chr_add_handlers(chr
, NULL
, NULL
,
273 net_vhost_user_event
, nc0
->name
);
274 } while (!s
->started
);
276 assert(s
->vhost_net
);
281 static int net_vhost_chardev_opts(void *opaque
,
282 const char *name
, const char *value
,
285 VhostUserChardevProps
*props
= opaque
;
287 if (strcmp(name
, "backend") == 0 && strcmp(value
, "socket") == 0) {
288 props
->is_socket
= true;
289 } else if (strcmp(name
, "path") == 0) {
290 props
->is_unix
= true;
291 } else if (strcmp(name
, "server") == 0) {
294 "vhost-user does not support a chardev with option %s=%s",
301 static CharDriverState
*net_vhost_parse_chardev(
302 const NetdevVhostUserOptions
*opts
, Error
**errp
)
304 CharDriverState
*chr
= qemu_chr_find(opts
->chardev
);
305 VhostUserChardevProps props
;
308 error_setg(errp
, "chardev \"%s\" not found", opts
->chardev
);
312 /* inspect chardev opts */
313 memset(&props
, 0, sizeof(props
));
314 if (qemu_opt_foreach(chr
->opts
, net_vhost_chardev_opts
, &props
, errp
)) {
318 if (!props
.is_socket
|| !props
.is_unix
) {
319 error_setg(errp
, "chardev \"%s\" is not a unix socket",
324 qemu_chr_fe_claim_no_fail(chr
);
329 static int net_vhost_check_net(void *opaque
, QemuOpts
*opts
, Error
**errp
)
331 const char *name
= opaque
;
332 const char *driver
, *netdev
;
334 driver
= qemu_opt_get(opts
, "driver");
335 netdev
= qemu_opt_get(opts
, "netdev");
337 if (!driver
|| !netdev
) {
341 if (strcmp(netdev
, name
) == 0 &&
342 !g_str_has_prefix(driver
, "virtio-net-")) {
343 error_setg(errp
, "vhost-user requires frontend driver virtio-net-*");
350 int net_init_vhost_user(const Netdev
*netdev
, const char *name
,
351 NetClientState
*peer
, Error
**errp
)
354 const NetdevVhostUserOptions
*vhost_user_opts
;
355 CharDriverState
*chr
;
357 assert(netdev
->type
== NET_CLIENT_DRIVER_VHOST_USER
);
358 vhost_user_opts
= &netdev
->u
.vhost_user
;
360 chr
= net_vhost_parse_chardev(vhost_user_opts
, errp
);
365 /* verify net frontend */
366 if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net
,
367 (char *)name
, errp
)) {
371 queues
= vhost_user_opts
->has_queues
? vhost_user_opts
->queues
: 1;
372 if (queues
< 1 || queues
> MAX_QUEUE_NUM
) {
374 "vhost-user number of queues must be in range [1, %d]",
379 return net_vhost_user_init(peer
, "vhost_user", name
, chr
, queues
);