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
;
28 typedef struct VhostUserChardevProps
{
31 } VhostUserChardevProps
;
33 VHostNetState
*vhost_user_get_vhost_net(NetClientState
*nc
)
35 VhostUserState
*s
= DO_UPCAST(VhostUserState
, nc
, nc
);
36 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_VHOST_USER
);
40 static int vhost_user_running(VhostUserState
*s
)
42 return (s
->vhost_net
) ? 1 : 0;
45 static void vhost_user_stop(int queues
, NetClientState
*ncs
[])
50 for (i
= 0; i
< queues
; i
++) {
51 assert (ncs
[i
]->info
->type
== NET_CLIENT_OPTIONS_KIND_VHOST_USER
);
53 s
= DO_UPCAST(VhostUserState
, nc
, ncs
[i
]);
54 if (!vhost_user_running(s
)) {
59 vhost_net_cleanup(s
->vhost_net
);
65 static int vhost_user_start(int queues
, NetClientState
*ncs
[])
67 VhostNetOptions options
;
72 options
.backend_type
= VHOST_BACKEND_TYPE_USER
;
74 for (i
= 0; i
< queues
; i
++) {
75 assert (ncs
[i
]->info
->type
== NET_CLIENT_OPTIONS_KIND_VHOST_USER
);
77 s
= DO_UPCAST(VhostUserState
, nc
, ncs
[i
]);
78 if (vhost_user_running(s
)) {
82 options
.net_backend
= ncs
[i
];
83 options
.opaque
= s
->chr
;
84 s
->vhost_net
= vhost_net_init(&options
);
86 error_report("failed to init vhost_net for queue %d", i
);
91 max_queues
= vhost_net_get_max_queues(s
->vhost_net
);
92 if (queues
> max_queues
) {
93 error_report("you are asking more queues than supported: %d",
103 vhost_user_stop(i
+ 1, ncs
);
107 static ssize_t
vhost_user_receive(NetClientState
*nc
, const uint8_t *buf
,
110 /* In case of RARP (message size is 60) notify backup to send a fake RARP.
111 This fake RARP will be sent by backend only for guest
112 without GUEST_ANNOUNCE capability.
115 VhostUserState
*s
= DO_UPCAST(VhostUserState
, nc
, nc
);
117 static int display_rarp_failure
= 1;
120 /* extract guest mac address from the RARP message */
121 memcpy(mac_addr
, &buf
[6], 6);
123 r
= vhost_net_notify_migration_done(s
->vhost_net
, mac_addr
);
125 if ((r
!= 0) && (display_rarp_failure
)) {
127 "Vhost user backend fails to broadcast fake RARP\n");
129 display_rarp_failure
= 0;
136 static void vhost_user_cleanup(NetClientState
*nc
)
138 VhostUserState
*s
= DO_UPCAST(VhostUserState
, nc
, nc
);
141 vhost_net_cleanup(s
->vhost_net
);
145 qemu_purge_queued_packets(nc
);
148 static bool vhost_user_has_vnet_hdr(NetClientState
*nc
)
150 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_VHOST_USER
);
155 static bool vhost_user_has_ufo(NetClientState
*nc
)
157 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_VHOST_USER
);
162 static NetClientInfo net_vhost_user_info
= {
163 .type
= NET_CLIENT_OPTIONS_KIND_VHOST_USER
,
164 .size
= sizeof(VhostUserState
),
165 .receive
= vhost_user_receive
,
166 .cleanup
= vhost_user_cleanup
,
167 .has_vnet_hdr
= vhost_user_has_vnet_hdr
,
168 .has_ufo
= vhost_user_has_ufo
,
171 static gboolean
net_vhost_user_watch(GIOChannel
*chan
, GIOCondition cond
,
174 VhostUserState
*s
= opaque
;
177 /* We don't actually want to read anything, but CHR_EVENT_CLOSED will be
178 * raised as a side-effect of the read.
180 qemu_chr_fe_read_all(s
->chr
, buf
, sizeof(buf
));
185 static void net_vhost_user_event(void *opaque
, int event
)
187 const char *name
= opaque
;
188 NetClientState
*ncs
[MAX_QUEUE_NUM
];
193 queues
= qemu_find_net_clients_except(name
, ncs
,
194 NET_CLIENT_OPTIONS_KIND_NIC
,
196 assert(queues
< MAX_QUEUE_NUM
);
198 s
= DO_UPCAST(VhostUserState
, nc
, ncs
[0]);
199 trace_vhost_user_event(s
->chr
->label
, event
);
201 case CHR_EVENT_OPENED
:
202 s
->watch
= qemu_chr_fe_add_watch(s
->chr
, G_IO_HUP
,
203 net_vhost_user_watch
, s
);
204 if (vhost_user_start(queues
, ncs
) < 0) {
207 qmp_set_link(name
, true, &err
);
209 case CHR_EVENT_CLOSED
:
210 qmp_set_link(name
, false, &err
);
211 vhost_user_stop(queues
, ncs
);
212 g_source_remove(s
->watch
);
218 error_report_err(err
);
222 static int net_vhost_user_init(NetClientState
*peer
, const char *device
,
223 const char *name
, CharDriverState
*chr
,
233 for (i
= 0; i
< queues
; i
++) {
234 nc
= qemu_new_net_client(&net_vhost_user_info
, peer
, device
, name
);
236 snprintf(nc
->info_str
, sizeof(nc
->info_str
), "vhost-user%d to %s",
241 s
= DO_UPCAST(VhostUserState
, nc
, nc
);
245 qemu_chr_add_handlers(chr
, NULL
, NULL
, net_vhost_user_event
, nc
[0].name
);
250 static int net_vhost_chardev_opts(void *opaque
,
251 const char *name
, const char *value
,
254 VhostUserChardevProps
*props
= opaque
;
256 if (strcmp(name
, "backend") == 0 && strcmp(value
, "socket") == 0) {
257 props
->is_socket
= true;
258 } else if (strcmp(name
, "path") == 0) {
259 props
->is_unix
= true;
260 } else if (strcmp(name
, "server") == 0) {
263 "vhost-user does not support a chardev with option %s=%s",
270 static CharDriverState
*net_vhost_parse_chardev(
271 const NetdevVhostUserOptions
*opts
, Error
**errp
)
273 CharDriverState
*chr
= qemu_chr_find(opts
->chardev
);
274 VhostUserChardevProps props
;
277 error_setg(errp
, "chardev \"%s\" not found", opts
->chardev
);
281 /* inspect chardev opts */
282 memset(&props
, 0, sizeof(props
));
283 if (qemu_opt_foreach(chr
->opts
, net_vhost_chardev_opts
, &props
, errp
)) {
287 if (!props
.is_socket
|| !props
.is_unix
) {
288 error_setg(errp
, "chardev \"%s\" is not a unix socket",
293 qemu_chr_fe_claim_no_fail(chr
);
298 static int net_vhost_check_net(void *opaque
, QemuOpts
*opts
, Error
**errp
)
300 const char *name
= opaque
;
301 const char *driver
, *netdev
;
302 const char virtio_name
[] = "virtio-net-";
304 driver
= qemu_opt_get(opts
, "driver");
305 netdev
= qemu_opt_get(opts
, "netdev");
307 if (!driver
|| !netdev
) {
311 if (strcmp(netdev
, name
) == 0 &&
312 strncmp(driver
, virtio_name
, strlen(virtio_name
)) != 0) {
313 error_setg(errp
, "vhost-user requires frontend driver virtio-net-*");
320 int net_init_vhost_user(const NetClientOptions
*opts
, const char *name
,
321 NetClientState
*peer
, Error
**errp
)
324 const NetdevVhostUserOptions
*vhost_user_opts
;
325 CharDriverState
*chr
;
327 assert(opts
->type
== NET_CLIENT_OPTIONS_KIND_VHOST_USER
);
328 vhost_user_opts
= opts
->u
.vhost_user
.data
;
330 chr
= net_vhost_parse_chardev(vhost_user_opts
, errp
);
335 /* verify net frontend */
336 if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net
,
337 (char *)name
, errp
)) {
341 queues
= vhost_user_opts
->has_queues
? vhost_user_opts
->queues
: 1;
342 if (queues
< 1 || queues
> MAX_QUEUE_NUM
) {
344 "vhost-user number of queues must be in range [1, %d]",
349 return net_vhost_user_init(peer
, "vhost_user", name
, chr
, queues
);