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"
17 #include "qmp-commands.h"
19 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 void vhost_user_stop(int queues
, NetClientState
*ncs
[])
48 for (i
= 0; i
< queues
; i
++) {
49 assert (ncs
[i
]->info
->type
== NET_CLIENT_OPTIONS_KIND_VHOST_USER
);
51 s
= DO_UPCAST(VhostUserState
, nc
, ncs
[i
]);
52 if (!vhost_user_running(s
)) {
57 vhost_net_cleanup(s
->vhost_net
);
63 static int vhost_user_start(int queues
, NetClientState
*ncs
[])
65 VhostNetOptions options
;
70 options
.backend_type
= VHOST_BACKEND_TYPE_USER
;
72 for (i
= 0; i
< queues
; i
++) {
73 assert (ncs
[i
]->info
->type
== NET_CLIENT_OPTIONS_KIND_VHOST_USER
);
75 s
= DO_UPCAST(VhostUserState
, nc
, ncs
[i
]);
76 if (vhost_user_running(s
)) {
80 options
.net_backend
= ncs
[i
];
81 options
.opaque
= s
->chr
;
82 s
->vhost_net
= vhost_net_init(&options
);
84 error_report("failed to init vhost_net for queue %d\n", i
);
89 max_queues
= vhost_net_get_max_queues(s
->vhost_net
);
90 if (queues
> max_queues
) {
91 error_report("you are asking more queues than "
92 "supported: %d\n", max_queues
);
101 vhost_user_stop(i
+ 1, ncs
);
105 static void vhost_user_cleanup(NetClientState
*nc
)
107 VhostUserState
*s
= DO_UPCAST(VhostUserState
, nc
, nc
);
110 vhost_net_cleanup(s
->vhost_net
);
114 qemu_purge_queued_packets(nc
);
117 static bool vhost_user_has_vnet_hdr(NetClientState
*nc
)
119 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_VHOST_USER
);
124 static bool vhost_user_has_ufo(NetClientState
*nc
)
126 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_VHOST_USER
);
131 static NetClientInfo net_vhost_user_info
= {
132 .type
= NET_CLIENT_OPTIONS_KIND_VHOST_USER
,
133 .size
= sizeof(VhostUserState
),
134 .cleanup
= vhost_user_cleanup
,
135 .has_vnet_hdr
= vhost_user_has_vnet_hdr
,
136 .has_ufo
= vhost_user_has_ufo
,
139 static void net_vhost_user_event(void *opaque
, int event
)
141 const char *name
= opaque
;
142 NetClientState
*ncs
[MAX_QUEUE_NUM
];
147 queues
= qemu_find_net_clients_except(name
, ncs
,
148 NET_CLIENT_OPTIONS_KIND_NIC
,
150 s
= DO_UPCAST(VhostUserState
, nc
, ncs
[0]);
152 case CHR_EVENT_OPENED
:
153 if (vhost_user_start(queues
, ncs
) < 0) {
156 qmp_set_link(name
, true, &err
);
157 error_report("chardev \"%s\" went up", s
->chr
->label
);
159 case CHR_EVENT_CLOSED
:
160 qmp_set_link(name
, true, &err
);
161 vhost_user_stop(queues
, ncs
);
162 error_report("chardev \"%s\" went down", s
->chr
->label
);
167 error_report_err(err
);
171 static int net_vhost_user_init(NetClientState
*peer
, const char *device
,
172 const char *name
, CharDriverState
*chr
,
179 for (i
= 0; i
< queues
; i
++) {
180 nc
= qemu_new_net_client(&net_vhost_user_info
, peer
, device
, name
);
182 snprintf(nc
->info_str
, sizeof(nc
->info_str
), "vhost-user%d to %s",
185 /* We don't provide a receive callback */
186 nc
->receive_disabled
= 1;
189 s
= DO_UPCAST(VhostUserState
, nc
, nc
);
193 qemu_chr_add_handlers(chr
, NULL
, NULL
, net_vhost_user_event
, (void*)name
);
198 static int net_vhost_chardev_opts(void *opaque
,
199 const char *name
, const char *value
,
202 VhostUserChardevProps
*props
= opaque
;
204 if (strcmp(name
, "backend") == 0 && strcmp(value
, "socket") == 0) {
205 props
->is_socket
= true;
206 } else if (strcmp(name
, "path") == 0) {
207 props
->is_unix
= true;
208 } else if (strcmp(name
, "server") == 0) {
209 props
->is_server
= true;
212 "vhost-user does not support a chardev with option %s=%s",
219 static CharDriverState
*net_vhost_parse_chardev(
220 const NetdevVhostUserOptions
*opts
, Error
**errp
)
222 CharDriverState
*chr
= qemu_chr_find(opts
->chardev
);
223 VhostUserChardevProps props
;
226 error_setg(errp
, "chardev \"%s\" not found", opts
->chardev
);
230 /* inspect chardev opts */
231 memset(&props
, 0, sizeof(props
));
232 if (qemu_opt_foreach(chr
->opts
, net_vhost_chardev_opts
, &props
, errp
)) {
236 if (!props
.is_socket
|| !props
.is_unix
) {
237 error_setg(errp
, "chardev \"%s\" is not a unix socket",
242 qemu_chr_fe_claim_no_fail(chr
);
247 static int net_vhost_check_net(void *opaque
, QemuOpts
*opts
, Error
**errp
)
249 const char *name
= opaque
;
250 const char *driver
, *netdev
;
251 const char virtio_name
[] = "virtio-net-";
253 driver
= qemu_opt_get(opts
, "driver");
254 netdev
= qemu_opt_get(opts
, "netdev");
256 if (!driver
|| !netdev
) {
260 if (strcmp(netdev
, name
) == 0 &&
261 strncmp(driver
, virtio_name
, strlen(virtio_name
)) != 0) {
262 error_setg(errp
, "vhost-user requires frontend driver virtio-net-*");
269 int net_init_vhost_user(const NetClientOptions
*opts
, const char *name
,
270 NetClientState
*peer
, Error
**errp
)
273 const NetdevVhostUserOptions
*vhost_user_opts
;
274 CharDriverState
*chr
;
276 assert(opts
->kind
== NET_CLIENT_OPTIONS_KIND_VHOST_USER
);
277 vhost_user_opts
= opts
->vhost_user
;
279 chr
= net_vhost_parse_chardev(vhost_user_opts
, errp
);
284 /* verify net frontend */
285 if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net
,
286 (char *)name
, errp
)) {
290 queues
= vhost_user_opts
->has_queues
? vhost_user_opts
->queues
: 1;
292 return net_vhost_user_init(peer
, "vhost_user", name
, chr
, queues
);