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.
12 #include "net/vhost_net.h"
13 #include "net/vhost-user.h"
14 #include "sysemu/char.h"
15 #include "qemu/config-file.h"
16 #include "qemu/error-report.h"
18 typedef struct VhostUserState
{
21 VHostNetState
*vhost_net
;
24 typedef struct VhostUserChardevProps
{
28 } VhostUserChardevProps
;
30 VHostNetState
*vhost_user_get_vhost_net(NetClientState
*nc
)
32 VhostUserState
*s
= DO_UPCAST(VhostUserState
, nc
, nc
);
33 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_VHOST_USER
);
37 static int vhost_user_running(VhostUserState
*s
)
39 return (s
->vhost_net
) ? 1 : 0;
42 static int vhost_user_start(VhostUserState
*s
)
44 VhostNetOptions options
;
46 if (vhost_user_running(s
)) {
50 options
.backend_type
= VHOST_BACKEND_TYPE_USER
;
51 options
.net_backend
= &s
->nc
;
52 options
.opaque
= s
->chr
;
55 s
->vhost_net
= vhost_net_init(&options
);
57 return vhost_user_running(s
) ? 0 : -1;
60 static void vhost_user_stop(VhostUserState
*s
)
62 if (vhost_user_running(s
)) {
63 vhost_net_cleanup(s
->vhost_net
);
69 static void vhost_user_cleanup(NetClientState
*nc
)
71 VhostUserState
*s
= DO_UPCAST(VhostUserState
, nc
, nc
);
74 qemu_purge_queued_packets(nc
);
77 static bool vhost_user_has_vnet_hdr(NetClientState
*nc
)
79 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_VHOST_USER
);
84 static bool vhost_user_has_ufo(NetClientState
*nc
)
86 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_VHOST_USER
);
91 static NetClientInfo net_vhost_user_info
= {
92 .type
= NET_CLIENT_OPTIONS_KIND_VHOST_USER
,
93 .size
= sizeof(VhostUserState
),
94 .cleanup
= vhost_user_cleanup
,
95 .has_vnet_hdr
= vhost_user_has_vnet_hdr
,
96 .has_ufo
= vhost_user_has_ufo
,
99 static void net_vhost_link_down(VhostUserState
*s
, bool link_down
)
101 s
->nc
.link_down
= link_down
;
104 s
->nc
.peer
->link_down
= link_down
;
107 if (s
->nc
.info
->link_status_changed
) {
108 s
->nc
.info
->link_status_changed(&s
->nc
);
111 if (s
->nc
.peer
&& s
->nc
.peer
->info
->link_status_changed
) {
112 s
->nc
.peer
->info
->link_status_changed(s
->nc
.peer
);
116 static void net_vhost_user_event(void *opaque
, int event
)
118 VhostUserState
*s
= opaque
;
121 case CHR_EVENT_OPENED
:
123 net_vhost_link_down(s
, false);
124 error_report("chardev \"%s\" went up", s
->chr
->label
);
126 case CHR_EVENT_CLOSED
:
127 net_vhost_link_down(s
, true);
129 error_report("chardev \"%s\" went down", s
->chr
->label
);
134 static int net_vhost_user_init(NetClientState
*peer
, const char *device
,
135 const char *name
, CharDriverState
*chr
)
140 nc
= qemu_new_net_client(&net_vhost_user_info
, peer
, device
, name
);
142 snprintf(nc
->info_str
, sizeof(nc
->info_str
), "vhost-user to %s",
145 s
= DO_UPCAST(VhostUserState
, nc
, nc
);
147 /* We don't provide a receive callback */
148 s
->nc
.receive_disabled
= 1;
151 qemu_chr_add_handlers(s
->chr
, NULL
, NULL
, net_vhost_user_event
, s
);
156 static int net_vhost_chardev_opts(const char *name
, const char *value
,
159 VhostUserChardevProps
*props
= opaque
;
161 if (strcmp(name
, "backend") == 0 && strcmp(value
, "socket") == 0) {
162 props
->is_socket
= true;
163 } else if (strcmp(name
, "path") == 0) {
164 props
->is_unix
= true;
165 } else if (strcmp(name
, "server") == 0) {
166 props
->is_server
= true;
168 error_report("vhost-user does not support a chardev"
169 " with the following option:\n %s = %s",
176 static CharDriverState
*net_vhost_parse_chardev(const NetdevVhostUserOptions
*opts
)
178 CharDriverState
*chr
= qemu_chr_find(opts
->chardev
);
179 VhostUserChardevProps props
;
182 error_report("chardev \"%s\" not found", opts
->chardev
);
186 /* inspect chardev opts */
187 memset(&props
, 0, sizeof(props
));
188 if (qemu_opt_foreach(chr
->opts
, net_vhost_chardev_opts
, &props
, true) != 0) {
192 if (!props
.is_socket
|| !props
.is_unix
) {
193 error_report("chardev \"%s\" is not a unix socket",
198 qemu_chr_fe_claim_no_fail(chr
);
203 static int net_vhost_check_net(QemuOpts
*opts
, void *opaque
)
205 const char *name
= opaque
;
206 const char *driver
, *netdev
;
207 const char virtio_name
[] = "virtio-net-";
209 driver
= qemu_opt_get(opts
, "driver");
210 netdev
= qemu_opt_get(opts
, "netdev");
212 if (!driver
|| !netdev
) {
216 if (strcmp(netdev
, name
) == 0 &&
217 strncmp(driver
, virtio_name
, strlen(virtio_name
)) != 0) {
218 error_report("vhost-user requires frontend driver virtio-net-*");
225 int net_init_vhost_user(const NetClientOptions
*opts
, const char *name
,
226 NetClientState
*peer
, Error
**errp
)
228 /* FIXME error_setg(errp, ...) on failure */
229 const NetdevVhostUserOptions
*vhost_user_opts
;
230 CharDriverState
*chr
;
232 assert(opts
->kind
== NET_CLIENT_OPTIONS_KIND_VHOST_USER
);
233 vhost_user_opts
= opts
->vhost_user
;
235 chr
= net_vhost_parse_chardev(vhost_user_opts
);
237 error_report("No suitable chardev found");
241 /* verify net frontend */
242 if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net
,
243 (char *)name
, true) == -1) {
248 return net_vhost_user_init(peer
, "vhost_user", name
, chr
);