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