vhost: use error_report() instead of fprintf(stderr,...)
[qemu.git] / net / vhost-user.c
blob2cac32e3b74e8dd0948aad869e18a5bdaf25c1f7
1 /*
2 * vhost-user.c
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.
9 */
11 #include "qemu/osdep.h"
12 #include "clients.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"
19 #include "trace.h"
21 typedef struct VhostUserState {
22 NetClientState nc;
23 CharDriverState *chr;
24 VHostNetState *vhost_net;
25 guint watch;
26 uint64_t acked_features;
27 } VhostUserState;
29 typedef struct VhostUserChardevProps {
30 bool is_socket;
31 bool is_unix;
32 } VhostUserChardevProps;
34 VHostNetState *vhost_user_get_vhost_net(NetClientState *nc)
36 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
37 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
38 return s->vhost_net;
41 uint64_t vhost_user_get_acked_features(NetClientState *nc)
43 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
44 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
45 return s->acked_features;
48 static int vhost_user_running(VhostUserState *s)
50 return (s->vhost_net) ? 1 : 0;
53 static void vhost_user_stop(int queues, NetClientState *ncs[])
55 VhostUserState *s;
56 int i;
58 for (i = 0; i < queues; i++) {
59 assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
61 s = DO_UPCAST(VhostUserState, nc, ncs[i]);
62 if (!vhost_user_running(s)) {
63 continue;
66 if (s->vhost_net) {
67 /* save acked features */
68 s->acked_features = vhost_net_get_acked_features(s->vhost_net);
69 vhost_net_cleanup(s->vhost_net);
70 s->vhost_net = NULL;
75 static int vhost_user_start(int queues, NetClientState *ncs[])
77 VhostNetOptions options;
78 VhostUserState *s;
79 int max_queues;
80 int i;
82 options.backend_type = VHOST_BACKEND_TYPE_USER;
84 for (i = 0; i < queues; i++) {
85 assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
87 s = DO_UPCAST(VhostUserState, nc, ncs[i]);
88 if (vhost_user_running(s)) {
89 continue;
92 options.net_backend = ncs[i];
93 options.opaque = s->chr;
94 options.busyloop_timeout = 0;
95 s->vhost_net = vhost_net_init(&options);
96 if (!s->vhost_net) {
97 error_report("failed to init vhost_net for queue %d", i);
98 goto err;
101 if (i == 0) {
102 max_queues = vhost_net_get_max_queues(s->vhost_net);
103 if (queues > max_queues) {
104 error_report("you are asking more queues than supported: %d",
105 max_queues);
106 goto err;
111 return 0;
113 err:
114 vhost_user_stop(i + 1, ncs);
115 return -1;
118 static ssize_t vhost_user_receive(NetClientState *nc, const uint8_t *buf,
119 size_t size)
121 /* In case of RARP (message size is 60) notify backup to send a fake RARP.
122 This fake RARP will be sent by backend only for guest
123 without GUEST_ANNOUNCE capability.
125 if (size == 60) {
126 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
127 int r;
128 static int display_rarp_failure = 1;
129 char mac_addr[6];
131 /* extract guest mac address from the RARP message */
132 memcpy(mac_addr, &buf[6], 6);
134 r = vhost_net_notify_migration_done(s->vhost_net, mac_addr);
136 if ((r != 0) && (display_rarp_failure)) {
137 fprintf(stderr,
138 "Vhost user backend fails to broadcast fake RARP\n");
139 fflush(stderr);
140 display_rarp_failure = 0;
144 return size;
147 static void vhost_user_cleanup(NetClientState *nc)
149 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
151 if (s->vhost_net) {
152 vhost_net_cleanup(s->vhost_net);
153 s->vhost_net = NULL;
155 if (s->chr) {
156 qemu_chr_add_handlers(s->chr, NULL, NULL, NULL, NULL);
157 qemu_chr_fe_release(s->chr);
158 s->chr = NULL;
161 qemu_purge_queued_packets(nc);
164 static bool vhost_user_has_vnet_hdr(NetClientState *nc)
166 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
168 return true;
171 static bool vhost_user_has_ufo(NetClientState *nc)
173 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
175 return true;
178 static NetClientInfo net_vhost_user_info = {
179 .type = NET_CLIENT_DRIVER_VHOST_USER,
180 .size = sizeof(VhostUserState),
181 .receive = vhost_user_receive,
182 .cleanup = vhost_user_cleanup,
183 .has_vnet_hdr = vhost_user_has_vnet_hdr,
184 .has_ufo = vhost_user_has_ufo,
187 static gboolean net_vhost_user_watch(GIOChannel *chan, GIOCondition cond,
188 void *opaque)
190 VhostUserState *s = opaque;
192 qemu_chr_disconnect(s->chr);
194 return FALSE;
197 static void net_vhost_user_event(void *opaque, int event)
199 const char *name = opaque;
200 NetClientState *ncs[MAX_QUEUE_NUM];
201 VhostUserState *s;
202 Error *err = NULL;
203 int queues;
205 queues = qemu_find_net_clients_except(name, ncs,
206 NET_CLIENT_DRIVER_NIC,
207 MAX_QUEUE_NUM);
208 assert(queues < MAX_QUEUE_NUM);
210 s = DO_UPCAST(VhostUserState, nc, ncs[0]);
211 trace_vhost_user_event(s->chr->label, event);
212 switch (event) {
213 case CHR_EVENT_OPENED:
214 s->watch = qemu_chr_fe_add_watch(s->chr, G_IO_HUP,
215 net_vhost_user_watch, s);
216 if (vhost_user_start(queues, ncs) < 0) {
217 qemu_chr_disconnect(s->chr);
218 return;
220 qmp_set_link(name, true, &err);
221 break;
222 case CHR_EVENT_CLOSED:
223 qmp_set_link(name, false, &err);
224 vhost_user_stop(queues, ncs);
225 g_source_remove(s->watch);
226 s->watch = 0;
227 break;
230 if (err) {
231 error_report_err(err);
235 static int net_vhost_user_init(NetClientState *peer, const char *device,
236 const char *name, CharDriverState *chr,
237 int queues)
239 NetClientState *nc;
240 VhostUserState *s;
241 int i;
243 assert(name);
244 assert(queues > 0);
246 for (i = 0; i < queues; i++) {
247 nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name);
249 snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user%d to %s",
250 i, chr->label);
252 nc->queue_index = i;
254 s = DO_UPCAST(VhostUserState, nc, nc);
255 s->chr = chr;
258 qemu_chr_add_handlers(chr, NULL, NULL, net_vhost_user_event, nc[0].name);
260 return 0;
263 static int net_vhost_chardev_opts(void *opaque,
264 const char *name, const char *value,
265 Error **errp)
267 VhostUserChardevProps *props = opaque;
269 if (strcmp(name, "backend") == 0 && strcmp(value, "socket") == 0) {
270 props->is_socket = true;
271 } else if (strcmp(name, "path") == 0) {
272 props->is_unix = true;
273 } else if (strcmp(name, "server") == 0) {
274 } else {
275 error_setg(errp,
276 "vhost-user does not support a chardev with option %s=%s",
277 name, value);
278 return -1;
280 return 0;
283 static CharDriverState *net_vhost_parse_chardev(
284 const NetdevVhostUserOptions *opts, Error **errp)
286 CharDriverState *chr = qemu_chr_find(opts->chardev);
287 VhostUserChardevProps props;
289 if (chr == NULL) {
290 error_setg(errp, "chardev \"%s\" not found", opts->chardev);
291 return NULL;
294 /* inspect chardev opts */
295 memset(&props, 0, sizeof(props));
296 if (qemu_opt_foreach(chr->opts, net_vhost_chardev_opts, &props, errp)) {
297 return NULL;
300 if (!props.is_socket || !props.is_unix) {
301 error_setg(errp, "chardev \"%s\" is not a unix socket",
302 opts->chardev);
303 return NULL;
306 qemu_chr_fe_claim_no_fail(chr);
308 return chr;
311 static int net_vhost_check_net(void *opaque, QemuOpts *opts, Error **errp)
313 const char *name = opaque;
314 const char *driver, *netdev;
316 driver = qemu_opt_get(opts, "driver");
317 netdev = qemu_opt_get(opts, "netdev");
319 if (!driver || !netdev) {
320 return 0;
323 if (strcmp(netdev, name) == 0 &&
324 !g_str_has_prefix(driver, "virtio-net-")) {
325 error_setg(errp, "vhost-user requires frontend driver virtio-net-*");
326 return -1;
329 return 0;
332 int net_init_vhost_user(const Netdev *netdev, const char *name,
333 NetClientState *peer, Error **errp)
335 int queues;
336 const NetdevVhostUserOptions *vhost_user_opts;
337 CharDriverState *chr;
339 assert(netdev->type == NET_CLIENT_DRIVER_VHOST_USER);
340 vhost_user_opts = &netdev->u.vhost_user;
342 chr = net_vhost_parse_chardev(vhost_user_opts, errp);
343 if (!chr) {
344 return -1;
347 /* verify net frontend */
348 if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net,
349 (char *)name, errp)) {
350 return -1;
353 queues = vhost_user_opts->has_queues ? vhost_user_opts->queues : 1;
354 if (queues < 1 || queues > MAX_QUEUE_NUM) {
355 error_setg(errp,
356 "vhost-user number of queues must be in range [1, %d]",
357 MAX_QUEUE_NUM);
358 return -1;
361 return net_vhost_user_init(peer, "vhost_user", name, chr, queues);