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
{
22 VHostNetState
*vhost_net
;
25 typedef struct VhostUserChardevProps
{
29 } VhostUserChardevProps
;
31 VHostNetState
*vhost_user_get_vhost_net(NetClientState
*nc
)
33 VhostUserState
*s
= DO_UPCAST(VhostUserState
, nc
, nc
);
34 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_VHOST_USER
);
38 static int vhost_user_running(VhostUserState
*s
)
40 return (s
->vhost_net
) ? 1 : 0;
43 static int vhost_user_start(VhostUserState
*s
)
45 VhostNetOptions options
;
47 if (vhost_user_running(s
)) {
51 options
.backend_type
= VHOST_BACKEND_TYPE_USER
;
52 options
.net_backend
= &s
->nc
;
53 options
.opaque
= s
->chr
;
54 options
.force
= s
->vhostforce
;
56 s
->vhost_net
= vhost_net_init(&options
);
58 return vhost_user_running(s
) ? 0 : -1;
61 static void vhost_user_stop(VhostUserState
*s
)
63 if (vhost_user_running(s
)) {
64 vhost_net_cleanup(s
->vhost_net
);
70 static void vhost_user_cleanup(NetClientState
*nc
)
72 VhostUserState
*s
= DO_UPCAST(VhostUserState
, nc
, nc
);
75 qemu_purge_queued_packets(nc
);
78 static bool vhost_user_has_vnet_hdr(NetClientState
*nc
)
80 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_VHOST_USER
);
85 static bool vhost_user_has_ufo(NetClientState
*nc
)
87 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_VHOST_USER
);
92 static NetClientInfo net_vhost_user_info
= {
93 .type
= NET_CLIENT_OPTIONS_KIND_VHOST_USER
,
94 .size
= sizeof(VhostUserState
),
95 .cleanup
= vhost_user_cleanup
,
96 .has_vnet_hdr
= vhost_user_has_vnet_hdr
,
97 .has_ufo
= vhost_user_has_ufo
,
100 static void net_vhost_link_down(VhostUserState
*s
, bool link_down
)
102 s
->nc
.link_down
= link_down
;
105 s
->nc
.peer
->link_down
= link_down
;
108 if (s
->nc
.info
->link_status_changed
) {
109 s
->nc
.info
->link_status_changed(&s
->nc
);
112 if (s
->nc
.peer
&& s
->nc
.peer
->info
->link_status_changed
) {
113 s
->nc
.peer
->info
->link_status_changed(s
->nc
.peer
);
117 static void net_vhost_user_event(void *opaque
, int event
)
119 VhostUserState
*s
= opaque
;
122 case CHR_EVENT_OPENED
:
124 net_vhost_link_down(s
, false);
125 error_report("chardev \"%s\" went up\n", s
->chr
->label
);
127 case CHR_EVENT_CLOSED
:
128 net_vhost_link_down(s
, true);
130 error_report("chardev \"%s\" went down\n", s
->chr
->label
);
135 static int net_vhost_user_init(NetClientState
*peer
, const char *device
,
136 const char *name
, CharDriverState
*chr
,
142 nc
= qemu_new_net_client(&net_vhost_user_info
, peer
, device
, name
);
144 snprintf(nc
->info_str
, sizeof(nc
->info_str
), "vhost-user to %s",
147 s
= DO_UPCAST(VhostUserState
, nc
, nc
);
149 /* We don't provide a receive callback */
150 s
->nc
.receive_disabled
= 1;
152 s
->vhostforce
= vhostforce
;
154 qemu_chr_add_handlers(s
->chr
, NULL
, NULL
, net_vhost_user_event
, s
);
159 static int net_vhost_chardev_opts(const char *name
, const char *value
,
162 VhostUserChardevProps
*props
= opaque
;
164 if (strcmp(name
, "backend") == 0 && strcmp(value
, "socket") == 0) {
165 props
->is_socket
= true;
166 } else if (strcmp(name
, "path") == 0) {
167 props
->is_unix
= true;
168 } else if (strcmp(name
, "server") == 0) {
169 props
->is_server
= true;
171 error_report("vhost-user does not support a chardev"
172 " with the following option:\n %s = %s",
179 static CharDriverState
*net_vhost_parse_chardev(const NetdevVhostUserOptions
*opts
)
181 CharDriverState
*chr
= qemu_chr_find(opts
->chardev
);
182 VhostUserChardevProps props
;
185 error_report("chardev \"%s\" not found", opts
->chardev
);
189 /* inspect chardev opts */
190 memset(&props
, 0, sizeof(props
));
191 if (qemu_opt_foreach(chr
->opts
, net_vhost_chardev_opts
, &props
, true) != 0) {
195 if (!props
.is_socket
|| !props
.is_unix
) {
196 error_report("chardev \"%s\" is not a unix socket",
201 qemu_chr_fe_claim_no_fail(chr
);
206 static int net_vhost_check_net(QemuOpts
*opts
, void *opaque
)
208 const char *name
= opaque
;
209 const char *driver
, *netdev
;
210 const char virtio_name
[] = "virtio-net-";
212 driver
= qemu_opt_get(opts
, "driver");
213 netdev
= qemu_opt_get(opts
, "netdev");
215 if (!driver
|| !netdev
) {
219 if (strcmp(netdev
, name
) == 0 &&
220 strncmp(driver
, virtio_name
, strlen(virtio_name
)) != 0) {
221 error_report("vhost-user requires frontend driver virtio-net-*");
228 int net_init_vhost_user(const NetClientOptions
*opts
, const char *name
,
229 NetClientState
*peer
)
231 const NetdevVhostUserOptions
*vhost_user_opts
;
232 CharDriverState
*chr
;
235 assert(opts
->kind
== NET_CLIENT_OPTIONS_KIND_VHOST_USER
);
236 vhost_user_opts
= opts
->vhost_user
;
238 chr
= net_vhost_parse_chardev(vhost_user_opts
);
240 error_report("No suitable chardev found");
244 /* verify net frontend */
245 if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net
,
246 (char *)name
, true) == -1) {
250 /* vhostforce for non-MSIX */
251 if (vhost_user_opts
->has_vhostforce
) {
252 vhostforce
= vhost_user_opts
->vhostforce
;
257 return net_vhost_user_init(peer
, "vhost_user", name
, chr
, vhostforce
);