xref: /openbmc/qemu/net/vhost-user.c (revision 8f6e5c620a5b21c070eed93721236cad48b6f9d7)
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 
35 static struct vhost_net *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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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         .get_vhost_net = vhost_user_get_vhost_net,
235 };
236 
237 static gboolean net_vhost_user_watch(void *do_not_use, GIOCondition cond,
238                                      void *opaque)
239 {
240     NetVhostUserState *s = opaque;
241 
242     qemu_chr_fe_disconnect(&s->chr);
243 
244     return G_SOURCE_CONTINUE;
245 }
246 
247 static void net_vhost_user_event(void *opaque, QEMUChrEvent event);
248 
249 static void chr_closed_bh(void *opaque)
250 {
251     const char *name = opaque;
252     NetClientState *ncs[MAX_QUEUE_NUM];
253     NetVhostUserState *s;
254     Error *err = NULL;
255     int queues, i;
256 
257     queues = qemu_find_net_clients_except(name, ncs,
258                                           NET_CLIENT_DRIVER_NIC,
259                                           MAX_QUEUE_NUM);
260     assert(queues < MAX_QUEUE_NUM);
261 
262     s = DO_UPCAST(NetVhostUserState, nc, ncs[0]);
263 
264     for (i = queues -1; i >= 0; i--) {
265         vhost_user_save_acked_features(ncs[i]);
266     }
267 
268     net_client_set_link(ncs, queues, false);
269 
270     qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, net_vhost_user_event,
271                              NULL, opaque, NULL, true);
272 
273     if (err) {
274         error_report_err(err);
275     }
276     qapi_event_send_netdev_vhost_user_disconnected(name);
277 }
278 
279 static void net_vhost_user_event(void *opaque, QEMUChrEvent event)
280 {
281     const char *name = opaque;
282     NetClientState *ncs[MAX_QUEUE_NUM];
283     NetVhostUserState *s;
284     Chardev *chr;
285     Error *err = NULL;
286     int queues;
287 
288     queues = qemu_find_net_clients_except(name, ncs,
289                                           NET_CLIENT_DRIVER_NIC,
290                                           MAX_QUEUE_NUM);
291     assert(queues < MAX_QUEUE_NUM);
292 
293     s = DO_UPCAST(NetVhostUserState, nc, ncs[0]);
294     chr = qemu_chr_fe_get_driver(&s->chr);
295     trace_vhost_user_event(chr->label, event);
296     switch (event) {
297     case CHR_EVENT_OPENED:
298         if (vhost_user_start(queues, ncs, s->vhost_user) < 0) {
299             qemu_chr_fe_disconnect(&s->chr);
300             return;
301         }
302         s->watch = qemu_chr_fe_add_watch(&s->chr, G_IO_HUP,
303                                          net_vhost_user_watch, s);
304         net_client_set_link(ncs, queues, true);
305         s->started = true;
306         qapi_event_send_netdev_vhost_user_connected(name, chr->label);
307         break;
308     case CHR_EVENT_CLOSED:
309         /* a close event may happen during a read/write, but vhost
310          * code assumes the vhost_dev remains setup, so delay the
311          * stop & clear to idle.
312          * FIXME: better handle failure in vhost code, remove bh
313          */
314         if (s->watch) {
315             AioContext *ctx = qemu_get_current_aio_context();
316 
317             g_source_remove(s->watch);
318             s->watch = 0;
319             qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, NULL, NULL,
320                                      NULL, NULL, false);
321 
322             aio_bh_schedule_oneshot(ctx, chr_closed_bh, opaque);
323         }
324         break;
325     case CHR_EVENT_BREAK:
326     case CHR_EVENT_MUX_IN:
327     case CHR_EVENT_MUX_OUT:
328         /* Ignore */
329         break;
330     }
331 
332     if (err) {
333         error_report_err(err);
334     }
335 }
336 
337 static int net_vhost_user_init(NetClientState *peer, const char *device,
338                                const char *name, Chardev *chr,
339                                int queues)
340 {
341     Error *err = NULL;
342     NetClientState *nc, *nc0 = NULL;
343     NetVhostUserState *s = NULL;
344     VhostUserState *user;
345     int i;
346 
347     assert(name);
348     assert(queues > 0);
349 
350     user = g_new0(struct VhostUserState, 1);
351     for (i = 0; i < queues; i++) {
352         nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name);
353         qemu_set_info_str(nc, "vhost-user%d to %s", i, chr->label);
354         nc->queue_index = i;
355         if (!nc0) {
356             nc0 = nc;
357             s = DO_UPCAST(NetVhostUserState, nc, nc);
358             if (!qemu_chr_fe_init(&s->chr, chr, &err) ||
359                 !vhost_user_init(user, &s->chr, &err)) {
360                 error_report_err(err);
361                 goto err;
362             }
363         }
364         s = DO_UPCAST(NetVhostUserState, nc, nc);
365         s->vhost_user = user;
366     }
367 
368     s = DO_UPCAST(NetVhostUserState, nc, nc0);
369     do {
370         if (qemu_chr_fe_wait_connected(&s->chr, &err) < 0) {
371             error_report_err(err);
372             goto err;
373         }
374         qemu_chr_fe_set_handlers(&s->chr, NULL, NULL,
375                                  net_vhost_user_event, NULL, nc0->name, NULL,
376                                  true);
377     } while (!s->started);
378 
379     assert(s->vhost_net);
380 
381     return 0;
382 
383 err:
384     if (user) {
385         vhost_user_cleanup(user);
386         g_free(user);
387         if (s) {
388             s->vhost_user = NULL;
389         }
390     }
391     if (nc0) {
392         qemu_del_net_client(nc0);
393     }
394 
395     return -1;
396 }
397 
398 static Chardev *net_vhost_claim_chardev(
399     const NetdevVhostUserOptions *opts, Error **errp)
400 {
401     Chardev *chr = qemu_chr_find(opts->chardev);
402 
403     if (chr == NULL) {
404         error_setg(errp, "chardev \"%s\" not found", opts->chardev);
405         return NULL;
406     }
407 
408     if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) {
409         error_setg(errp, "chardev \"%s\" is not reconnectable",
410                    opts->chardev);
411         return NULL;
412     }
413     if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_FD_PASS)) {
414         error_setg(errp, "chardev \"%s\" does not support FD passing",
415                    opts->chardev);
416         return NULL;
417     }
418 
419     return chr;
420 }
421 
422 int net_init_vhost_user(const Netdev *netdev, const char *name,
423                         NetClientState *peer, Error **errp)
424 {
425     int queues;
426     const NetdevVhostUserOptions *vhost_user_opts;
427     Chardev *chr;
428 
429     assert(netdev->type == NET_CLIENT_DRIVER_VHOST_USER);
430     vhost_user_opts = &netdev->u.vhost_user;
431 
432     chr = net_vhost_claim_chardev(vhost_user_opts, errp);
433     if (!chr) {
434         return -1;
435     }
436 
437     queues = vhost_user_opts->has_queues ? vhost_user_opts->queues : 1;
438     if (queues < 1 || queues > MAX_QUEUE_NUM) {
439         error_setg(errp,
440                    "vhost-user number of queues must be in range [1, %d]",
441                    MAX_QUEUE_NUM);
442         return -1;
443     }
444 
445     return net_vhost_user_init(peer, "vhost_user", name, chr, queues);
446 }
447