1 /*
2 * vhost-user.c
3 *
4 * Copyright (c) 2013 Virtual Open Systems Sarl.
5 *
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.
8 *
9 */
10
11 #include "qemu/osdep.h"
12 #include "clients.h"
13 #include "net/vhost_net.h"
14 #include "net/vhost-user.h"
15 #include "hw/virtio/vhost-user.h"
16 #include "chardev/char-fe.h"
17 #include "qapi/error.h"
18 #include "qapi/qapi-commands-net.h"
19 #include "qapi/qapi-events-net.h"
20 #include "qemu/config-file.h"
21 #include "qemu/error-report.h"
22 #include "qemu/option.h"
23 #include "trace.h"
24
25 typedef struct NetVhostUserState {
26 NetClientState nc;
27 CharBackend chr; /* only queue index 0 */
28 VhostUserState *vhost_user;
29 VHostNetState *vhost_net;
30 guint watch;
31 uint64_t acked_features;
32 bool started;
33 } NetVhostUserState;
34
vhost_user_get_vhost_net(NetClientState * nc)35 VHostNetState *vhost_user_get_vhost_net(NetClientState *nc)
36 {
37 NetVhostUserState *s = DO_UPCAST(NetVhostUserState, nc, nc);
38 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
39 return s->vhost_net;
40 }
41
vhost_user_get_acked_features(NetClientState * nc)42 uint64_t vhost_user_get_acked_features(NetClientState *nc)
43 {
44 NetVhostUserState *s = DO_UPCAST(NetVhostUserState, nc, nc);
45 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
46 return s->acked_features;
47 }
48
vhost_user_save_acked_features(NetClientState * nc)49 void vhost_user_save_acked_features(NetClientState *nc)
50 {
51 NetVhostUserState *s;
52
53 s = DO_UPCAST(NetVhostUserState, nc, nc);
54 if (s->vhost_net) {
55 uint64_t features = vhost_net_get_acked_features(s->vhost_net);
56 if (features) {
57 s->acked_features = features;
58 }
59 }
60 }
61
vhost_user_stop(int queues,NetClientState * ncs[])62 static void vhost_user_stop(int queues, NetClientState *ncs[])
63 {
64 int i;
65 NetVhostUserState *s;
66
67 for (i = 0; i < queues; i++) {
68 assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
69
70 s = DO_UPCAST(NetVhostUserState, nc, ncs[i]);
71
72 if (s->vhost_net) {
73 vhost_user_save_acked_features(ncs[i]);
74 vhost_net_cleanup(s->vhost_net);
75 }
76 }
77 }
78
vhost_user_start(int queues,NetClientState * ncs[],VhostUserState * be)79 static int vhost_user_start(int queues, NetClientState *ncs[],
80 VhostUserState *be)
81 {
82 VhostNetOptions options;
83 struct vhost_net *net = NULL;
84 NetVhostUserState *s;
85 int max_queues;
86 int i;
87
88 options.backend_type = VHOST_BACKEND_TYPE_USER;
89
90 for (i = 0; i < queues; i++) {
91 assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
92
93 s = DO_UPCAST(NetVhostUserState, nc, ncs[i]);
94
95 options.net_backend = ncs[i];
96 options.opaque = be;
97 options.busyloop_timeout = 0;
98 options.nvqs = 2;
99 net = vhost_net_init(&options);
100 if (!net) {
101 error_report("failed to init vhost_net for queue %d", i);
102 goto err;
103 }
104
105 if (i == 0) {
106 max_queues = vhost_net_get_max_queues(net);
107 if (queues > max_queues) {
108 error_report("you are asking more queues than supported: %d",
109 max_queues);
110 goto err;
111 }
112 }
113
114 if (s->vhost_net) {
115 vhost_net_cleanup(s->vhost_net);
116 g_free(s->vhost_net);
117 }
118 s->vhost_net = net;
119 }
120
121 return 0;
122
123 err:
124 if (net) {
125 vhost_net_cleanup(net);
126 g_free(net);
127 }
128 vhost_user_stop(i, ncs);
129 return -1;
130 }
131
vhost_user_receive(NetClientState * nc,const uint8_t * buf,size_t size)132 static ssize_t vhost_user_receive(NetClientState *nc, const uint8_t *buf,
133 size_t size)
134 {
135 /* In case of RARP (message size is 60) notify backup to send a fake RARP.
136 This fake RARP will be sent by backend only for guest
137 without GUEST_ANNOUNCE capability.
138 */
139 if (size == 60) {
140 NetVhostUserState *s = DO_UPCAST(NetVhostUserState, nc, nc);
141 int r;
142 static int display_rarp_failure = 1;
143 char mac_addr[6];
144
145 /* extract guest mac address from the RARP message */
146 memcpy(mac_addr, &buf[6], 6);
147
148 r = vhost_net_notify_migration_done(s->vhost_net, mac_addr);
149
150 if ((r != 0) && (display_rarp_failure)) {
151 fprintf(stderr,
152 "Vhost user backend fails to broadcast fake RARP\n");
153 fflush(stderr);
154 display_rarp_failure = 0;
155 }
156 }
157
158 return size;
159 }
160
net_vhost_user_cleanup(NetClientState * nc)161 static void net_vhost_user_cleanup(NetClientState *nc)
162 {
163 NetVhostUserState *s = DO_UPCAST(NetVhostUserState, nc, nc);
164
165 if (s->vhost_net) {
166 vhost_net_cleanup(s->vhost_net);
167 g_free(s->vhost_net);
168 s->vhost_net = NULL;
169 }
170 if (nc->queue_index == 0) {
171 if (s->watch) {
172 g_source_remove(s->watch);
173 s->watch = 0;
174 }
175 qemu_chr_fe_deinit(&s->chr, true);
176 if (s->vhost_user) {
177 vhost_user_cleanup(s->vhost_user);
178 g_free(s->vhost_user);
179 s->vhost_user = NULL;
180 }
181 }
182
183 qemu_purge_queued_packets(nc);
184 }
185
vhost_user_set_vnet_endianness(NetClientState * nc,bool enable)186 static int vhost_user_set_vnet_endianness(NetClientState *nc,
187 bool enable)
188 {
189 /* Nothing to do. If the server supports
190 * VHOST_USER_PROTOCOL_F_CROSS_ENDIAN, it will get the
191 * vnet header endianness from there. If it doesn't, negotiation
192 * fails.
193 */
194 return 0;
195 }
196
vhost_user_has_vnet_hdr(NetClientState * nc)197 static bool vhost_user_has_vnet_hdr(NetClientState *nc)
198 {
199 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
200
201 return true;
202 }
203
vhost_user_has_ufo(NetClientState * nc)204 static bool vhost_user_has_ufo(NetClientState *nc)
205 {
206 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
207
208 return true;
209 }
210
vhost_user_check_peer_type(NetClientState * nc,ObjectClass * oc,Error ** errp)211 static bool vhost_user_check_peer_type(NetClientState *nc, ObjectClass *oc,
212 Error **errp)
213 {
214 const char *driver = object_class_get_name(oc);
215
216 if (!g_str_has_prefix(driver, "virtio-net-")) {
217 error_setg(errp, "vhost-user requires frontend driver virtio-net-*");
218 return false;
219 }
220
221 return true;
222 }
223
224 static NetClientInfo net_vhost_user_info = {
225 .type = NET_CLIENT_DRIVER_VHOST_USER,
226 .size = sizeof(NetVhostUserState),
227 .receive = vhost_user_receive,
228 .cleanup = net_vhost_user_cleanup,
229 .has_vnet_hdr = vhost_user_has_vnet_hdr,
230 .has_ufo = vhost_user_has_ufo,
231 .set_vnet_be = vhost_user_set_vnet_endianness,
232 .set_vnet_le = vhost_user_set_vnet_endianness,
233 .check_peer_type = vhost_user_check_peer_type,
234 };
235
net_vhost_user_watch(void * do_not_use,GIOCondition cond,void * opaque)236 static gboolean net_vhost_user_watch(void *do_not_use, GIOCondition cond,
237 void *opaque)
238 {
239 NetVhostUserState *s = opaque;
240
241 qemu_chr_fe_disconnect(&s->chr);
242
243 return G_SOURCE_CONTINUE;
244 }
245
246 static void net_vhost_user_event(void *opaque, QEMUChrEvent event);
247
chr_closed_bh(void * opaque)248 static void chr_closed_bh(void *opaque)
249 {
250 const char *name = opaque;
251 NetClientState *ncs[MAX_QUEUE_NUM];
252 NetVhostUserState *s;
253 Error *err = NULL;
254 int queues, i;
255
256 queues = qemu_find_net_clients_except(name, ncs,
257 NET_CLIENT_DRIVER_NIC,
258 MAX_QUEUE_NUM);
259 assert(queues < MAX_QUEUE_NUM);
260
261 s = DO_UPCAST(NetVhostUserState, nc, ncs[0]);
262
263 for (i = queues -1; i >= 0; i--) {
264 vhost_user_save_acked_features(ncs[i]);
265 }
266
267 qmp_set_link(name, false, &err);
268
269 qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, net_vhost_user_event,
270 NULL, opaque, NULL, true);
271
272 if (err) {
273 error_report_err(err);
274 }
275 qapi_event_send_netdev_vhost_user_disconnected(name);
276 }
277
net_vhost_user_event(void * opaque,QEMUChrEvent event)278 static void net_vhost_user_event(void *opaque, QEMUChrEvent event)
279 {
280 const char *name = opaque;
281 NetClientState *ncs[MAX_QUEUE_NUM];
282 NetVhostUserState *s;
283 Chardev *chr;
284 Error *err = NULL;
285 int queues;
286
287 queues = qemu_find_net_clients_except(name, ncs,
288 NET_CLIENT_DRIVER_NIC,
289 MAX_QUEUE_NUM);
290 assert(queues < MAX_QUEUE_NUM);
291
292 s = DO_UPCAST(NetVhostUserState, nc, ncs[0]);
293 chr = qemu_chr_fe_get_driver(&s->chr);
294 trace_vhost_user_event(chr->label, event);
295 switch (event) {
296 case CHR_EVENT_OPENED:
297 if (vhost_user_start(queues, ncs, s->vhost_user) < 0) {
298 qemu_chr_fe_disconnect(&s->chr);
299 return;
300 }
301 s->watch = qemu_chr_fe_add_watch(&s->chr, G_IO_HUP,
302 net_vhost_user_watch, s);
303 qmp_set_link(name, true, &err);
304 s->started = true;
305 qapi_event_send_netdev_vhost_user_connected(name, chr->label);
306 break;
307 case CHR_EVENT_CLOSED:
308 /* a close event may happen during a read/write, but vhost
309 * code assumes the vhost_dev remains setup, so delay the
310 * stop & clear to idle.
311 * FIXME: better handle failure in vhost code, remove bh
312 */
313 if (s->watch) {
314 AioContext *ctx = qemu_get_current_aio_context();
315
316 g_source_remove(s->watch);
317 s->watch = 0;
318 qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, NULL, NULL,
319 NULL, NULL, false);
320
321 aio_bh_schedule_oneshot(ctx, chr_closed_bh, opaque);
322 }
323 break;
324 case CHR_EVENT_BREAK:
325 case CHR_EVENT_MUX_IN:
326 case CHR_EVENT_MUX_OUT:
327 /* Ignore */
328 break;
329 }
330
331 if (err) {
332 error_report_err(err);
333 }
334 }
335
net_vhost_user_init(NetClientState * peer,const char * device,const char * name,Chardev * chr,int queues)336 static int net_vhost_user_init(NetClientState *peer, const char *device,
337 const char *name, Chardev *chr,
338 int queues)
339 {
340 Error *err = NULL;
341 NetClientState *nc, *nc0 = NULL;
342 NetVhostUserState *s = NULL;
343 VhostUserState *user;
344 int i;
345
346 assert(name);
347 assert(queues > 0);
348
349 user = g_new0(struct VhostUserState, 1);
350 for (i = 0; i < queues; i++) {
351 nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name);
352 qemu_set_info_str(nc, "vhost-user%d to %s", i, chr->label);
353 nc->queue_index = i;
354 if (!nc0) {
355 nc0 = nc;
356 s = DO_UPCAST(NetVhostUserState, nc, nc);
357 if (!qemu_chr_fe_init(&s->chr, chr, &err) ||
358 !vhost_user_init(user, &s->chr, &err)) {
359 error_report_err(err);
360 goto err;
361 }
362 }
363 s = DO_UPCAST(NetVhostUserState, nc, nc);
364 s->vhost_user = user;
365 }
366
367 s = DO_UPCAST(NetVhostUserState, nc, nc0);
368 do {
369 if (qemu_chr_fe_wait_connected(&s->chr, &err) < 0) {
370 error_report_err(err);
371 goto err;
372 }
373 qemu_chr_fe_set_handlers(&s->chr, NULL, NULL,
374 net_vhost_user_event, NULL, nc0->name, NULL,
375 true);
376 } while (!s->started);
377
378 assert(s->vhost_net);
379
380 return 0;
381
382 err:
383 if (user) {
384 vhost_user_cleanup(user);
385 g_free(user);
386 if (s) {
387 s->vhost_user = NULL;
388 }
389 }
390 if (nc0) {
391 qemu_del_net_client(nc0);
392 }
393
394 return -1;
395 }
396
net_vhost_claim_chardev(const NetdevVhostUserOptions * opts,Error ** errp)397 static Chardev *net_vhost_claim_chardev(
398 const NetdevVhostUserOptions *opts, Error **errp)
399 {
400 Chardev *chr = qemu_chr_find(opts->chardev);
401
402 if (chr == NULL) {
403 error_setg(errp, "chardev \"%s\" not found", opts->chardev);
404 return NULL;
405 }
406
407 if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) {
408 error_setg(errp, "chardev \"%s\" is not reconnectable",
409 opts->chardev);
410 return NULL;
411 }
412 if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_FD_PASS)) {
413 error_setg(errp, "chardev \"%s\" does not support FD passing",
414 opts->chardev);
415 return NULL;
416 }
417
418 return chr;
419 }
420
net_init_vhost_user(const Netdev * netdev,const char * name,NetClientState * peer,Error ** errp)421 int net_init_vhost_user(const Netdev *netdev, const char *name,
422 NetClientState *peer, Error **errp)
423 {
424 int queues;
425 const NetdevVhostUserOptions *vhost_user_opts;
426 Chardev *chr;
427
428 assert(netdev->type == NET_CLIENT_DRIVER_VHOST_USER);
429 vhost_user_opts = &netdev->u.vhost_user;
430
431 chr = net_vhost_claim_chardev(vhost_user_opts, errp);
432 if (!chr) {
433 return -1;
434 }
435
436 queues = vhost_user_opts->has_queues ? vhost_user_opts->queues : 1;
437 if (queues < 1 || queues > MAX_QUEUE_NUM) {
438 error_setg(errp,
439 "vhost-user number of queues must be in range [1, %d]",
440 MAX_QUEUE_NUM);
441 return -1;
442 }
443
444 return net_vhost_user_init(peer, "vhost_user", name, chr, queues);
445 }
446