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