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