scsi: mptconfig: fix an assert expression
[qemu/ar7.git] / net / vhost-user.c
blobb0595f8781ceb2a7d0df2fb0248cb089cdc47992
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 bool started;
28 } VhostUserState;
30 typedef struct VhostUserChardevProps {
31 bool is_socket;
32 bool is_unix;
33 } VhostUserChardevProps;
35 VHostNetState *vhost_user_get_vhost_net(NetClientState *nc)
37 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
38 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
39 return s->vhost_net;
42 uint64_t vhost_user_get_acked_features(NetClientState *nc)
44 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
45 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
46 return s->acked_features;
49 static void vhost_user_stop(int queues, NetClientState *ncs[])
51 VhostUserState *s;
52 int i;
54 for (i = 0; i < queues; i++) {
55 assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
57 s = DO_UPCAST(VhostUserState, nc, ncs[i]);
59 if (s->vhost_net) {
60 /* save acked features */
61 uint64_t features = vhost_net_get_acked_features(s->vhost_net);
62 if (features) {
63 s->acked_features = features;
65 vhost_net_cleanup(s->vhost_net);
70 static int vhost_user_start(int queues, NetClientState *ncs[])
72 VhostNetOptions options;
73 struct vhost_net *net = NULL;
74 VhostUserState *s;
75 int max_queues;
76 int i;
78 options.backend_type = VHOST_BACKEND_TYPE_USER;
80 for (i = 0; i < queues; i++) {
81 assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
83 s = DO_UPCAST(VhostUserState, nc, ncs[i]);
85 options.net_backend = ncs[i];
86 options.opaque = s->chr;
87 options.busyloop_timeout = 0;
88 net = vhost_net_init(&options);
89 if (!net) {
90 error_report("failed to init vhost_net for queue %d", i);
91 goto err;
94 if (i == 0) {
95 max_queues = vhost_net_get_max_queues(net);
96 if (queues > max_queues) {
97 error_report("you are asking more queues than supported: %d",
98 max_queues);
99 goto err;
103 if (s->vhost_net) {
104 vhost_net_cleanup(s->vhost_net);
105 g_free(s->vhost_net);
107 s->vhost_net = net;
110 return 0;
112 err:
113 if (net) {
114 vhost_net_cleanup(net);
116 vhost_user_stop(i, ncs);
117 return -1;
120 static ssize_t vhost_user_receive(NetClientState *nc, const uint8_t *buf,
121 size_t size)
123 /* In case of RARP (message size is 60) notify backup to send a fake RARP.
124 This fake RARP will be sent by backend only for guest
125 without GUEST_ANNOUNCE capability.
127 if (size == 60) {
128 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
129 int r;
130 static int display_rarp_failure = 1;
131 char mac_addr[6];
133 /* extract guest mac address from the RARP message */
134 memcpy(mac_addr, &buf[6], 6);
136 r = vhost_net_notify_migration_done(s->vhost_net, mac_addr);
138 if ((r != 0) && (display_rarp_failure)) {
139 fprintf(stderr,
140 "Vhost user backend fails to broadcast fake RARP\n");
141 fflush(stderr);
142 display_rarp_failure = 0;
146 return size;
149 static void vhost_user_cleanup(NetClientState *nc)
151 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
153 if (s->vhost_net) {
154 vhost_net_cleanup(s->vhost_net);
155 g_free(s->vhost_net);
156 s->vhost_net = NULL;
158 if (s->chr) {
159 qemu_chr_add_handlers(s->chr, NULL, NULL, NULL, NULL);
160 qemu_chr_fe_release(s->chr);
161 s->chr = NULL;
164 qemu_purge_queued_packets(nc);
167 static bool vhost_user_has_vnet_hdr(NetClientState *nc)
169 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
171 return true;
174 static bool vhost_user_has_ufo(NetClientState *nc)
176 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
178 return true;
181 static NetClientInfo net_vhost_user_info = {
182 .type = NET_CLIENT_DRIVER_VHOST_USER,
183 .size = sizeof(VhostUserState),
184 .receive = vhost_user_receive,
185 .cleanup = vhost_user_cleanup,
186 .has_vnet_hdr = vhost_user_has_vnet_hdr,
187 .has_ufo = vhost_user_has_ufo,
190 static gboolean net_vhost_user_watch(GIOChannel *chan, GIOCondition cond,
191 void *opaque)
193 VhostUserState *s = opaque;
195 qemu_chr_disconnect(s->chr);
197 return FALSE;
200 static void net_vhost_user_event(void *opaque, int event)
202 const char *name = opaque;
203 NetClientState *ncs[MAX_QUEUE_NUM];
204 VhostUserState *s;
205 Error *err = NULL;
206 int queues;
208 queues = qemu_find_net_clients_except(name, ncs,
209 NET_CLIENT_DRIVER_NIC,
210 MAX_QUEUE_NUM);
211 assert(queues < MAX_QUEUE_NUM);
213 s = DO_UPCAST(VhostUserState, nc, ncs[0]);
214 trace_vhost_user_event(s->chr->label, event);
215 switch (event) {
216 case CHR_EVENT_OPENED:
217 s->watch = qemu_chr_fe_add_watch(s->chr, G_IO_HUP,
218 net_vhost_user_watch, s);
219 if (vhost_user_start(queues, ncs) < 0) {
220 qemu_chr_disconnect(s->chr);
221 return;
223 qmp_set_link(name, true, &err);
224 s->started = true;
225 break;
226 case CHR_EVENT_CLOSED:
227 qmp_set_link(name, false, &err);
228 vhost_user_stop(queues, ncs);
229 g_source_remove(s->watch);
230 s->watch = 0;
231 break;
234 if (err) {
235 error_report_err(err);
239 static int net_vhost_user_init(NetClientState *peer, const char *device,
240 const char *name, CharDriverState *chr,
241 int queues)
243 NetClientState *nc, *nc0 = NULL;
244 VhostUserState *s;
245 int i;
247 assert(name);
248 assert(queues > 0);
250 for (i = 0; i < queues; i++) {
251 nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name);
252 if (!nc0) {
253 nc0 = nc;
256 snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user%d to %s",
257 i, chr->label);
259 nc->queue_index = i;
261 s = DO_UPCAST(VhostUserState, nc, nc);
262 s->chr = chr;
265 s = DO_UPCAST(VhostUserState, nc, nc0);
266 do {
267 Error *err = NULL;
268 if (qemu_chr_wait_connected(chr, &err) < 0) {
269 error_report_err(err);
270 return -1;
272 qemu_chr_add_handlers(chr, NULL, NULL,
273 net_vhost_user_event, nc0->name);
274 } while (!s->started);
276 assert(s->vhost_net);
278 return 0;
281 static int net_vhost_chardev_opts(void *opaque,
282 const char *name, const char *value,
283 Error **errp)
285 VhostUserChardevProps *props = opaque;
287 if (strcmp(name, "backend") == 0 && strcmp(value, "socket") == 0) {
288 props->is_socket = true;
289 } else if (strcmp(name, "path") == 0) {
290 props->is_unix = true;
291 } else if (strcmp(name, "server") == 0) {
292 } else {
293 error_setg(errp,
294 "vhost-user does not support a chardev with option %s=%s",
295 name, value);
296 return -1;
298 return 0;
301 static CharDriverState *net_vhost_parse_chardev(
302 const NetdevVhostUserOptions *opts, Error **errp)
304 CharDriverState *chr = qemu_chr_find(opts->chardev);
305 VhostUserChardevProps props;
307 if (chr == NULL) {
308 error_setg(errp, "chardev \"%s\" not found", opts->chardev);
309 return NULL;
312 /* inspect chardev opts */
313 memset(&props, 0, sizeof(props));
314 if (qemu_opt_foreach(chr->opts, net_vhost_chardev_opts, &props, errp)) {
315 return NULL;
318 if (!props.is_socket || !props.is_unix) {
319 error_setg(errp, "chardev \"%s\" is not a unix socket",
320 opts->chardev);
321 return NULL;
324 qemu_chr_fe_claim_no_fail(chr);
326 return chr;
329 static int net_vhost_check_net(void *opaque, QemuOpts *opts, Error **errp)
331 const char *name = opaque;
332 const char *driver, *netdev;
334 driver = qemu_opt_get(opts, "driver");
335 netdev = qemu_opt_get(opts, "netdev");
337 if (!driver || !netdev) {
338 return 0;
341 if (strcmp(netdev, name) == 0 &&
342 !g_str_has_prefix(driver, "virtio-net-")) {
343 error_setg(errp, "vhost-user requires frontend driver virtio-net-*");
344 return -1;
347 return 0;
350 int net_init_vhost_user(const Netdev *netdev, const char *name,
351 NetClientState *peer, Error **errp)
353 int queues;
354 const NetdevVhostUserOptions *vhost_user_opts;
355 CharDriverState *chr;
357 assert(netdev->type == NET_CLIENT_DRIVER_VHOST_USER);
358 vhost_user_opts = &netdev->u.vhost_user;
360 chr = net_vhost_parse_chardev(vhost_user_opts, errp);
361 if (!chr) {
362 return -1;
365 /* verify net frontend */
366 if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net,
367 (char *)name, errp)) {
368 return -1;
371 queues = vhost_user_opts->has_queues ? vhost_user_opts->queues : 1;
372 if (queues < 1 || queues > MAX_QUEUE_NUM) {
373 error_setg(errp,
374 "vhost-user number of queues must be in range [1, %d]",
375 MAX_QUEUE_NUM);
376 return -1;
379 return net_vhost_user_init(peer, "vhost_user", name, chr, queues);