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