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