]> git.proxmox.com Git - mirror_qemu.git/blob - net/vhost-user.c
vhost-user: keep vhost_net after a disconnection
[mirror_qemu.git] / net / vhost-user.c
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 } VhostUserState;
28
29 typedef struct VhostUserChardevProps {
30 bool is_socket;
31 bool is_unix;
32 } VhostUserChardevProps;
33
34 VHostNetState *vhost_user_get_vhost_net(NetClientState *nc)
35 {
36 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
37 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
38 return s->vhost_net;
39 }
40
41 uint64_t vhost_user_get_acked_features(NetClientState *nc)
42 {
43 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
44 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
45 return s->acked_features;
46 }
47
48 static void vhost_user_stop(int queues, NetClientState *ncs[])
49 {
50 VhostUserState *s;
51 int i;
52
53 for (i = 0; i < queues; i++) {
54 assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
55
56 s = DO_UPCAST(VhostUserState, nc, ncs[i]);
57
58 if (s->vhost_net) {
59 /* save acked features */
60 uint64_t features = vhost_net_get_acked_features(s->vhost_net);
61 if (features) {
62 s->acked_features = features;
63 }
64 vhost_net_cleanup(s->vhost_net);
65 }
66 }
67 }
68
69 static int vhost_user_start(int queues, NetClientState *ncs[])
70 {
71 VhostNetOptions options;
72 struct vhost_net *net = NULL;
73 VhostUserState *s;
74 int max_queues;
75 int i;
76
77 options.backend_type = VHOST_BACKEND_TYPE_USER;
78
79 for (i = 0; i < queues; i++) {
80 assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
81
82 s = DO_UPCAST(VhostUserState, nc, ncs[i]);
83
84 options.net_backend = ncs[i];
85 options.opaque = s->chr;
86 options.busyloop_timeout = 0;
87 net = vhost_net_init(&options);
88 if (!net) {
89 error_report("failed to init vhost_net for queue %d", i);
90 goto err;
91 }
92
93 if (i == 0) {
94 max_queues = vhost_net_get_max_queues(net);
95 if (queues > max_queues) {
96 error_report("you are asking more queues than supported: %d",
97 max_queues);
98 goto err;
99 }
100 }
101
102 if (s->vhost_net) {
103 vhost_net_cleanup(s->vhost_net);
104 g_free(s->vhost_net);
105 }
106 s->vhost_net = net;
107 }
108
109 return 0;
110
111 err:
112 if (net) {
113 vhost_net_cleanup(net);
114 }
115 vhost_user_stop(i, ncs);
116 return -1;
117 }
118
119 static ssize_t vhost_user_receive(NetClientState *nc, const uint8_t *buf,
120 size_t size)
121 {
122 /* In case of RARP (message size is 60) notify backup to send a fake RARP.
123 This fake RARP will be sent by backend only for guest
124 without GUEST_ANNOUNCE capability.
125 */
126 if (size == 60) {
127 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
128 int r;
129 static int display_rarp_failure = 1;
130 char mac_addr[6];
131
132 /* extract guest mac address from the RARP message */
133 memcpy(mac_addr, &buf[6], 6);
134
135 r = vhost_net_notify_migration_done(s->vhost_net, mac_addr);
136
137 if ((r != 0) && (display_rarp_failure)) {
138 fprintf(stderr,
139 "Vhost user backend fails to broadcast fake RARP\n");
140 fflush(stderr);
141 display_rarp_failure = 0;
142 }
143 }
144
145 return size;
146 }
147
148 static void vhost_user_cleanup(NetClientState *nc)
149 {
150 VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
151
152 if (s->vhost_net) {
153 vhost_net_cleanup(s->vhost_net);
154 g_free(s->vhost_net);
155 s->vhost_net = NULL;
156 }
157 if (s->chr) {
158 qemu_chr_add_handlers(s->chr, NULL, NULL, NULL, NULL);
159 qemu_chr_fe_release(s->chr);
160 s->chr = NULL;
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_disconnect(s->chr);
195
196 return FALSE;
197 }
198
199 static void net_vhost_user_event(void *opaque, int event)
200 {
201 const char *name = opaque;
202 NetClientState *ncs[MAX_QUEUE_NUM];
203 VhostUserState *s;
204 Error *err = NULL;
205 int queues;
206
207 queues = qemu_find_net_clients_except(name, ncs,
208 NET_CLIENT_DRIVER_NIC,
209 MAX_QUEUE_NUM);
210 assert(queues < MAX_QUEUE_NUM);
211
212 s = DO_UPCAST(VhostUserState, nc, ncs[0]);
213 trace_vhost_user_event(s->chr->label, event);
214 switch (event) {
215 case CHR_EVENT_OPENED:
216 s->watch = qemu_chr_fe_add_watch(s->chr, G_IO_HUP,
217 net_vhost_user_watch, s);
218 if (vhost_user_start(queues, ncs) < 0) {
219 qemu_chr_disconnect(s->chr);
220 return;
221 }
222 qmp_set_link(name, true, &err);
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, CharDriverState *chr,
239 int queues)
240 {
241 NetClientState *nc;
242 VhostUserState *s;
243 int i;
244
245 assert(name);
246 assert(queues > 0);
247
248 for (i = 0; i < queues; i++) {
249 nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name);
250
251 snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user%d to %s",
252 i, chr->label);
253
254 nc->queue_index = i;
255
256 s = DO_UPCAST(VhostUserState, nc, nc);
257 s->chr = chr;
258 }
259
260 qemu_chr_add_handlers(chr, NULL, NULL, net_vhost_user_event, nc[0].name);
261
262 return 0;
263 }
264
265 static int net_vhost_chardev_opts(void *opaque,
266 const char *name, const char *value,
267 Error **errp)
268 {
269 VhostUserChardevProps *props = opaque;
270
271 if (strcmp(name, "backend") == 0 && strcmp(value, "socket") == 0) {
272 props->is_socket = true;
273 } else if (strcmp(name, "path") == 0) {
274 props->is_unix = true;
275 } else if (strcmp(name, "server") == 0) {
276 } else {
277 error_setg(errp,
278 "vhost-user does not support a chardev with option %s=%s",
279 name, value);
280 return -1;
281 }
282 return 0;
283 }
284
285 static CharDriverState *net_vhost_parse_chardev(
286 const NetdevVhostUserOptions *opts, Error **errp)
287 {
288 CharDriverState *chr = qemu_chr_find(opts->chardev);
289 VhostUserChardevProps props;
290
291 if (chr == NULL) {
292 error_setg(errp, "chardev \"%s\" not found", opts->chardev);
293 return NULL;
294 }
295
296 /* inspect chardev opts */
297 memset(&props, 0, sizeof(props));
298 if (qemu_opt_foreach(chr->opts, net_vhost_chardev_opts, &props, errp)) {
299 return NULL;
300 }
301
302 if (!props.is_socket || !props.is_unix) {
303 error_setg(errp, "chardev \"%s\" is not a unix socket",
304 opts->chardev);
305 return NULL;
306 }
307
308 qemu_chr_fe_claim_no_fail(chr);
309
310 return chr;
311 }
312
313 static int net_vhost_check_net(void *opaque, QemuOpts *opts, Error **errp)
314 {
315 const char *name = opaque;
316 const char *driver, *netdev;
317
318 driver = qemu_opt_get(opts, "driver");
319 netdev = qemu_opt_get(opts, "netdev");
320
321 if (!driver || !netdev) {
322 return 0;
323 }
324
325 if (strcmp(netdev, name) == 0 &&
326 !g_str_has_prefix(driver, "virtio-net-")) {
327 error_setg(errp, "vhost-user requires frontend driver virtio-net-*");
328 return -1;
329 }
330
331 return 0;
332 }
333
334 int net_init_vhost_user(const Netdev *netdev, const char *name,
335 NetClientState *peer, Error **errp)
336 {
337 int queues;
338 const NetdevVhostUserOptions *vhost_user_opts;
339 CharDriverState *chr;
340
341 assert(netdev->type == NET_CLIENT_DRIVER_VHOST_USER);
342 vhost_user_opts = &netdev->u.vhost_user;
343
344 chr = net_vhost_parse_chardev(vhost_user_opts, errp);
345 if (!chr) {
346 return -1;
347 }
348
349 /* verify net frontend */
350 if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net,
351 (char *)name, errp)) {
352 return -1;
353 }
354
355 queues = vhost_user_opts->has_queues ? vhost_user_opts->queues : 1;
356 if (queues < 1 || queues > MAX_QUEUE_NUM) {
357 error_setg(errp,
358 "vhost-user number of queues must be in range [1, %d]",
359 MAX_QUEUE_NUM);
360 return -1;
361 }
362
363 return net_vhost_user_init(peer, "vhost_user", name, chr, queues);
364 }