xref: /openbmc/qemu/net/vhost-user.c (revision 0c0c1fd9)
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     CharDriverState *chr;
24     VHostNetState *vhost_net;
25     guint watch;
26     uint64_t acked_features;
27     bool started;
28 } VhostUserState;
29 
30 typedef struct VhostUserChardevProps {
31     bool is_socket;
32     bool is_unix;
33 } VhostUserChardevProps;
34 
35 VHostNetState *vhost_user_get_vhost_net(NetClientState *nc)
36 {
37     VhostUserState *s = DO_UPCAST(VhostUserState, 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     VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
45     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
46     return s->acked_features;
47 }
48 
49 static void vhost_user_stop(int queues, NetClientState *ncs[])
50 {
51     VhostUserState *s;
52     int i;
53 
54     for (i = 0; i < queues; i++) {
55         assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
56 
57         s = DO_UPCAST(VhostUserState, nc, ncs[i]);
58 
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;
64             }
65             vhost_net_cleanup(s->vhost_net);
66         }
67     }
68 }
69 
70 static int vhost_user_start(int queues, NetClientState *ncs[])
71 {
72     VhostNetOptions options;
73     struct vhost_net *net = NULL;
74     VhostUserState *s;
75     int max_queues;
76     int i;
77 
78     options.backend_type = VHOST_BACKEND_TYPE_USER;
79 
80     for (i = 0; i < queues; i++) {
81         assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
82 
83         s = DO_UPCAST(VhostUserState, nc, ncs[i]);
84 
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;
92         }
93 
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;
100             }
101         }
102 
103         if (s->vhost_net) {
104             vhost_net_cleanup(s->vhost_net);
105             g_free(s->vhost_net);
106         }
107         s->vhost_net = net;
108     }
109 
110     return 0;
111 
112 err:
113     if (net) {
114         vhost_net_cleanup(net);
115     }
116     vhost_user_stop(i, ncs);
117     return -1;
118 }
119 
120 static ssize_t vhost_user_receive(NetClientState *nc, const uint8_t *buf,
121                                   size_t size)
122 {
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.
126      */
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];
132 
133         /* extract guest mac address from the RARP message */
134         memcpy(mac_addr, &buf[6], 6);
135 
136         r = vhost_net_notify_migration_done(s->vhost_net, mac_addr);
137 
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;
143         }
144     }
145 
146     return size;
147 }
148 
149 static void vhost_user_cleanup(NetClientState *nc)
150 {
151     VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
152 
153     if (s->vhost_net) {
154         vhost_net_cleanup(s->vhost_net);
155         g_free(s->vhost_net);
156         s->vhost_net = NULL;
157     }
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;
162     }
163 
164     qemu_purge_queued_packets(nc);
165 }
166 
167 static bool vhost_user_has_vnet_hdr(NetClientState *nc)
168 {
169     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
170 
171     return true;
172 }
173 
174 static bool vhost_user_has_ufo(NetClientState *nc)
175 {
176     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
177 
178     return true;
179 }
180 
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,
188 };
189 
190 static gboolean net_vhost_user_watch(GIOChannel *chan, GIOCondition cond,
191                                            void *opaque)
192 {
193     VhostUserState *s = opaque;
194 
195     qemu_chr_disconnect(s->chr);
196 
197     return FALSE;
198 }
199 
200 static void net_vhost_user_event(void *opaque, int event)
201 {
202     const char *name = opaque;
203     NetClientState *ncs[MAX_QUEUE_NUM];
204     VhostUserState *s;
205     Error *err = NULL;
206     int queues;
207 
208     queues = qemu_find_net_clients_except(name, ncs,
209                                           NET_CLIENT_DRIVER_NIC,
210                                           MAX_QUEUE_NUM);
211     assert(queues < MAX_QUEUE_NUM);
212 
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;
222         }
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;
232     }
233 
234     if (err) {
235         error_report_err(err);
236     }
237 }
238 
239 static int net_vhost_user_init(NetClientState *peer, const char *device,
240                                const char *name, CharDriverState *chr,
241                                int queues)
242 {
243     NetClientState *nc, *nc0 = NULL;
244     VhostUserState *s;
245     int i;
246 
247     assert(name);
248     assert(queues > 0);
249 
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;
254         }
255 
256         snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user%d to %s",
257                  i, chr->label);
258 
259         nc->queue_index = i;
260 
261         s = DO_UPCAST(VhostUserState, nc, nc);
262         s->chr = chr;
263     }
264 
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;
271         }
272         qemu_chr_add_handlers(chr, NULL, NULL,
273                               net_vhost_user_event, nc0->name);
274     } while (!s->started);
275 
276     assert(s->vhost_net);
277 
278     return 0;
279 }
280 
281 static int net_vhost_chardev_opts(void *opaque,
282                                   const char *name, const char *value,
283                                   Error **errp)
284 {
285     VhostUserChardevProps *props = opaque;
286 
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;
297     }
298     return 0;
299 }
300 
301 static CharDriverState *net_vhost_parse_chardev(
302     const NetdevVhostUserOptions *opts, Error **errp)
303 {
304     CharDriverState *chr = qemu_chr_find(opts->chardev);
305     VhostUserChardevProps props;
306 
307     if (chr == NULL) {
308         error_setg(errp, "chardev \"%s\" not found", opts->chardev);
309         return NULL;
310     }
311 
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;
316     }
317 
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;
322     }
323 
324     qemu_chr_fe_claim_no_fail(chr);
325 
326     return chr;
327 }
328 
329 static int net_vhost_check_net(void *opaque, QemuOpts *opts, Error **errp)
330 {
331     const char *name = opaque;
332     const char *driver, *netdev;
333 
334     driver = qemu_opt_get(opts, "driver");
335     netdev = qemu_opt_get(opts, "netdev");
336 
337     if (!driver || !netdev) {
338         return 0;
339     }
340 
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;
345     }
346 
347     return 0;
348 }
349 
350 int net_init_vhost_user(const Netdev *netdev, const char *name,
351                         NetClientState *peer, Error **errp)
352 {
353     int queues;
354     const NetdevVhostUserOptions *vhost_user_opts;
355     CharDriverState *chr;
356 
357     assert(netdev->type == NET_CLIENT_DRIVER_VHOST_USER);
358     vhost_user_opts = &netdev->u.vhost_user;
359 
360     chr = net_vhost_parse_chardev(vhost_user_opts, errp);
361     if (!chr) {
362         return -1;
363     }
364 
365     /* verify net frontend */
366     if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net,
367                           (char *)name, errp)) {
368         return -1;
369     }
370 
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;
377     }
378 
379     return net_vhost_user_init(peer, "vhost_user", name, chr, queues);
380 }
381