master
c 1,165 lines 31.6 KB
Raw
1 /*
2 * Copyright (c) 2018 Citrix Systems Inc.
3 *
4 * This work is licensed under the terms of the GNU GPL, version 2 or later.
5 * See the COPYING file in the top-level directory.
6 */
7
8 #include "qemu/osdep.h"
9 #include "qemu/main-loop.h"
10 #include "qemu/module.h"
11 #include "qemu/uuid.h"
12 #include "hw/core/qdev-properties.h"
13 #include "hw/core/sysbus.h"
14 #include "hw/xen/xen.h"
15 #include "hw/xen/xen-backend.h"
16 #include "hw/xen/xen-legacy-backend.h" /* xen_be_init() */
17 #include "hw/xen/xen-bus.h"
18 #include "hw/xen/xen-bus-helper.h"
19 #include "monitor/monitor.h"
20 #include "monitor/hmp.h"
21 #include "qapi/error.h"
22 #include "qobject/qdict.h"
23 #include "system/system.h"
24 #include "net/net.h"
25 #include "trace.h"
26
27 static char *xen_device_get_backend_path(XenDevice *xendev)
28 {
29 XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
30 XenDeviceClass *xendev_class = XEN_DEVICE_GET_CLASS(xendev);
31 const char *type = object_get_typename(OBJECT(xendev));
32 const char *backend = xendev_class->backend;
33
34 if (!backend) {
35 backend = type;
36 }
37
38 return g_strdup_printf("/local/domain/%u/backend/%s/%u/%s",
39 xenbus->backend_id, backend, xendev->frontend_id,
40 xendev->name);
41 }
42
43 static char *xen_device_get_frontend_path(XenDevice *xendev)
44 {
45 XenDeviceClass *xendev_class = XEN_DEVICE_GET_CLASS(xendev);
46 const char *type = object_get_typename(OBJECT(xendev));
47 const char *device = xendev_class->device;
48
49 if (!device) {
50 device = type;
51 }
52
53 return g_strdup_printf("/local/domain/%u/device/%s/%s",
54 xendev->frontend_id, device, xendev->name);
55 }
56
57 static void xen_device_unplug(XenDevice *xendev, Error **errp)
58 {
59 ERRP_GUARD();
60 XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
61 const char *type = object_get_typename(OBJECT(xendev));
62 xs_transaction_t tid;
63
64 trace_xen_device_unplug(type, xendev->name);
65
66 /* Mimic the way the Xen toolstack does an unplug */
67 again:
68 tid = qemu_xen_xs_transaction_start(xenbus->xsh);
69 if (tid == XBT_NULL) {
70 error_setg_errno(errp, errno, "failed xs_transaction_start");
71 return;
72 }
73
74 xs_node_printf(xenbus->xsh, tid, xendev->backend_path, "online",
75 errp, "%u", 0);
76 if (*errp) {
77 goto abort;
78 }
79
80 xs_node_printf(xenbus->xsh, tid, xendev->backend_path, "state",
81 errp, "%u", XenbusStateClosing);
82 if (*errp) {
83 goto abort;
84 }
85
86 if (!qemu_xen_xs_transaction_end(xenbus->xsh, tid, false)) {
87 if (errno == EAGAIN) {
88 goto again;
89 }
90
91 error_setg_errno(errp, errno, "failed xs_transaction_end");
92 }
93
94 return;
95
96 abort:
97 /*
98 * We only abort if there is already a failure so ignore any error
99 * from ending the transaction.
100 */
101 qemu_xen_xs_transaction_end(xenbus->xsh, tid, true);
102 }
103
104 #ifdef CONFIG_HMP
105 static void xen_bus_print_dev(MonitorHMP *hmp, DeviceState *dev, int indent)
106 {
107 XenDevice *xendev = XEN_DEVICE(dev);
108
109 monitor_hmp_printf(hmp, "%*sname = '%s' frontend_id = %u\n",
110 indent, "", xendev->name, xendev->frontend_id);
111 }
112 #endif
113
114 static char *xen_bus_get_dev_path(DeviceState *dev)
115 {
116 return xen_device_get_backend_path(XEN_DEVICE(dev));
117 }
118
119 static void xen_bus_backend_create(XenBus *xenbus, const char *type,
120 const char *name, char *path,
121 Error **errp)
122 {
123 ERRP_GUARD();
124 xs_transaction_t tid;
125 char **key;
126 QDict *opts;
127 unsigned int i, n;
128
129 trace_xen_bus_backend_create(type, path);
130
131 again:
132 tid = qemu_xen_xs_transaction_start(xenbus->xsh);
133 if (tid == XBT_NULL) {
134 error_setg(errp, "failed xs_transaction_start");
135 return;
136 }
137
138 key = qemu_xen_xs_directory(xenbus->xsh, tid, path, &n);
139 if (!key) {
140 if (!qemu_xen_xs_transaction_end(xenbus->xsh, tid, true)) {
141 error_setg_errno(errp, errno, "failed xs_transaction_end");
142 }
143 return;
144 }
145
146 opts = qdict_new();
147 for (i = 0; i < n; i++) {
148 char *val;
149
150 /*
151 * Assume anything found in the xenstore backend area, other than
152 * the keys created for a generic XenDevice, are parameters
153 * to be used to configure the backend.
154 */
155 if (!strcmp(key[i], "state") ||
156 !strcmp(key[i], "online") ||
157 !strcmp(key[i], "frontend") ||
158 !strcmp(key[i], "frontend-id") ||
159 !strcmp(key[i], "hotplug-status"))
160 continue;
161
162 val = xs_node_read(xenbus->xsh, tid, NULL, NULL, "%s/%s", path, key[i]);
163 if (val) {
164 qdict_put_str(opts, key[i], val);
165 free(val);
166 }
167 }
168
169 free(key);
170
171 if (!qemu_xen_xs_transaction_end(xenbus->xsh, tid, false)) {
172 qobject_unref(opts);
173
174 if (errno == EAGAIN) {
175 goto again;
176 }
177
178 error_setg_errno(errp, errno, "failed xs_transaction_end");
179 return;
180 }
181
182 xen_backend_device_create(xenbus, type, name, opts, errp);
183 qobject_unref(opts);
184
185 if (*errp) {
186 error_prepend(errp, "failed to create '%s' device '%s': ", type, name);
187 }
188 }
189
190 static void xen_bus_type_enumerate(XenBus *xenbus, const char *type)
191 {
192 char *domain_path = g_strdup_printf("backend/%s/%u", type, xen_domid);
193 char **backend;
194 unsigned int i, n;
195
196 trace_xen_bus_type_enumerate(type);
197
198 backend = qemu_xen_xs_directory(xenbus->xsh, XBT_NULL, domain_path, &n);
199 if (!backend) {
200 goto out;
201 }
202
203 for (i = 0; i < n; i++) {
204 char *backend_path = g_strdup_printf("%s/%s", domain_path,
205 backend[i]);
206 enum xenbus_state state;
207 unsigned int online;
208
209 if (xs_node_scanf(xenbus->xsh, XBT_NULL, backend_path, "state",
210 NULL, "%u", &state) != 1)
211 state = XenbusStateUnknown;
212
213 if (xs_node_scanf(xenbus->xsh, XBT_NULL, backend_path, "online",
214 NULL, "%u", &online) != 1)
215 online = 0;
216
217 if (online && state == XenbusStateInitialising &&
218 !xen_backend_exists(type, backend[i])) {
219 Error *local_err = NULL;
220
221 xen_bus_backend_create(xenbus, type, backend[i], backend_path,
222 &local_err);
223 if (local_err) {
224 error_report_err(local_err);
225 }
226 }
227
228 g_free(backend_path);
229 }
230
231 free(backend);
232
233 out:
234 g_free(domain_path);
235 }
236
237 static void xen_bus_enumerate(XenBus *xenbus)
238 {
239 char **type;
240 unsigned int i, n;
241
242 trace_xen_bus_enumerate();
243
244 type = qemu_xen_xs_directory(xenbus->xsh, XBT_NULL, "backend", &n);
245 if (!type) {
246 return;
247 }
248
249 for (i = 0; i < n; i++) {
250 xen_bus_type_enumerate(xenbus, type[i]);
251 }
252
253 free(type);
254 }
255
256 static void xen_bus_device_cleanup(XenDevice *xendev)
257 {
258 const char *type = object_get_typename(OBJECT(xendev));
259 Error *local_err = NULL;
260
261 trace_xen_bus_device_cleanup(type, xendev->name);
262
263 g_assert(!xendev->backend_online);
264
265 if (!xen_backend_try_device_destroy(xendev, &local_err)) {
266 object_unparent(OBJECT(xendev));
267 }
268
269 if (local_err) {
270 error_report_err(local_err);
271 }
272 }
273
274 static void xen_bus_cleanup(XenBus *xenbus)
275 {
276 XenDevice *xendev, *next;
277
278 trace_xen_bus_cleanup();
279
280 QLIST_FOREACH_SAFE(xendev, &xenbus->inactive_devices, list, next) {
281 g_assert(xendev->inactive);
282 QLIST_REMOVE(xendev, list);
283 xen_bus_device_cleanup(xendev);
284 }
285 }
286
287 static void xen_bus_backend_changed(void *opaque, const char *path)
288 {
289 XenBus *xenbus = opaque;
290
291 xen_bus_enumerate(xenbus);
292 xen_bus_cleanup(xenbus);
293 }
294
295 static void xen_bus_unrealize(BusState *bus)
296 {
297 XenBus *xenbus = XEN_BUS(bus);
298
299 trace_xen_bus_unrealize();
300
301 if (xenbus->backend_watch) {
302 unsigned int i;
303
304 for (i = 0; i < xenbus->backend_types; i++) {
305 if (xenbus->backend_watch[i]) {
306 xs_node_unwatch(xenbus->xsh, xenbus->backend_watch[i]);
307 }
308 }
309
310 g_free(xenbus->backend_watch);
311 xenbus->backend_watch = NULL;
312 }
313
314 if (xenbus->xsh) {
315 qemu_xen_xs_close(xenbus->xsh);
316 }
317 }
318
319 static void xen_bus_realize(BusState *bus, Error **errp)
320 {
321 char *key = g_strdup_printf("%u", xen_domid);
322 XenBus *xenbus = XEN_BUS(bus);
323 unsigned int domid;
324 const char **type;
325 unsigned int i;
326 Error *local_err = NULL;
327
328 trace_xen_bus_realize();
329
330 xenbus->xsh = qemu_xen_xs_open();
331 if (!xenbus->xsh) {
332 error_setg_errno(errp, errno, "failed xs_open");
333 goto fail;
334 }
335
336 /* Initialize legacy backend core & drivers */
337 xen_be_init();
338
339 if (xs_node_scanf(xenbus->xsh, XBT_NULL, "", /* domain root node */
340 "domid", NULL, "%u", &domid) == 1) {
341 xenbus->backend_id = domid;
342 } else {
343 xenbus->backend_id = 0; /* Assume lack of node means dom0 */
344 }
345
346 module_call_init(MODULE_INIT_XEN_BACKEND);
347
348 type = xen_backend_get_types(&xenbus->backend_types);
349 xenbus->backend_watch = g_new(struct qemu_xs_watch *,
350 xenbus->backend_types);
351
352 for (i = 0; i < xenbus->backend_types; i++) {
353 char *node = g_strdup_printf("backend/%s", type[i]);
354
355 xenbus->backend_watch[i] =
356 xs_node_watch(xenbus->xsh, node, key, xen_bus_backend_changed,
357 xenbus, &local_err);
358 if (local_err) {
359 warn_reportf_err(local_err,
360 "failed to set up '%s' enumeration watch: ",
361 type[i]);
362 local_err = NULL;
363 }
364
365 g_free(node);
366 }
367
368 g_free(type);
369 g_free(key);
370 return;
371
372 fail:
373 xen_bus_unrealize(bus);
374 g_free(key);
375 }
376
377 static void xen_bus_unplug_request(HotplugHandler *hotplug,
378 DeviceState *dev,
379 Error **errp)
380 {
381 XenDevice *xendev = XEN_DEVICE(dev);
382
383 xen_device_unplug(xendev, errp);
384 }
385
386 static void xen_bus_class_init(ObjectClass *class, const void *data)
387 {
388 BusClass *bus_class = BUS_CLASS(class);
389 HotplugHandlerClass *hotplug_class = HOTPLUG_HANDLER_CLASS(class);
390
391 #ifdef CONFIG_HMP
392 bus_class->print_dev = xen_bus_print_dev;
393 #endif
394 bus_class->get_dev_path = xen_bus_get_dev_path;
395 bus_class->realize = xen_bus_realize;
396 bus_class->unrealize = xen_bus_unrealize;
397
398 hotplug_class->unplug_request = xen_bus_unplug_request;
399 }
400
401 static const TypeInfo xen_bus_type_info = {
402 .name = TYPE_XEN_BUS,
403 .parent = TYPE_BUS,
404 .instance_size = sizeof(XenBus),
405 .class_size = sizeof(XenBusClass),
406 .class_init = xen_bus_class_init,
407 .interfaces = (const InterfaceInfo[]) {
408 { TYPE_HOTPLUG_HANDLER },
409 { }
410 },
411 };
412
413 void xen_device_backend_printf(XenDevice *xendev, const char *key,
414 const char *fmt, ...)
415 {
416 XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
417 Error *local_err = NULL;
418 va_list ap;
419
420 g_assert(xenbus->xsh);
421
422 va_start(ap, fmt);
423 xs_node_vprintf(xenbus->xsh, XBT_NULL, xendev->backend_path, key,
424 &local_err, fmt, ap);
425 va_end(ap);
426
427 if (local_err) {
428 error_report_err(local_err);
429 }
430 }
431
432 G_GNUC_SCANF(3, 4)
433 static int xen_device_backend_scanf(XenDevice *xendev, const char *key,
434 const char *fmt, ...)
435 {
436 XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
437 va_list ap;
438 int rc;
439
440 g_assert(xenbus->xsh);
441
442 va_start(ap, fmt);
443 rc = xs_node_vscanf(xenbus->xsh, XBT_NULL, xendev->backend_path, key,
444 NULL, fmt, ap);
445 va_end(ap);
446
447 return rc;
448 }
449
450 void xen_device_backend_set_state(XenDevice *xendev,
451 enum xenbus_state state)
452 {
453 const char *type = object_get_typename(OBJECT(xendev));
454
455 if (xendev->backend_state == state) {
456 return;
457 }
458
459 trace_xen_device_backend_state(type, xendev->name,
460 xs_strstate(state));
461
462 xendev->backend_state = state;
463 xen_device_backend_printf(xendev, "state", "%u", state);
464 }
465
466 enum xenbus_state xen_device_backend_get_state(XenDevice *xendev)
467 {
468 return xendev->backend_state;
469 }
470
471 static void xen_device_backend_set_online(XenDevice *xendev, bool online)
472 {
473 const char *type = object_get_typename(OBJECT(xendev));
474
475 if (xendev->backend_online == online) {
476 return;
477 }
478
479 trace_xen_device_backend_online(type, xendev->name, online);
480
481 xendev->backend_online = online;
482 xen_device_backend_printf(xendev, "online", "%u", online);
483 }
484
485 /*
486 * Tell from the state whether the frontend is likely alive,
487 * i.e. it will react to a change of state of the backend.
488 */
489 static bool xen_device_frontend_is_active(XenDevice *xendev)
490 {
491 switch (xendev->frontend_state) {
492 case XenbusStateInitWait:
493 case XenbusStateInitialised:
494 case XenbusStateConnected:
495 case XenbusStateClosing:
496 return true;
497 default:
498 return false;
499 }
500 }
501
502 static void xen_device_backend_changed(void *opaque, const char *path)
503 {
504 XenDevice *xendev = opaque;
505 const char *type = object_get_typename(OBJECT(xendev));
506 enum xenbus_state state;
507 unsigned int online;
508
509 trace_xen_device_backend_changed(type, xendev->name);
510
511 if (xen_device_backend_scanf(xendev, "state", "%u", &state) != 1) {
512 state = XenbusStateUnknown;
513 }
514
515 xen_device_backend_set_state(xendev, state);
516
517 if (xen_device_backend_scanf(xendev, "online", "%u", &online) != 1) {
518 online = 0;
519 }
520
521 xen_device_backend_set_online(xendev, !!online);
522
523 /*
524 * If the toolstack (or unplug request callback) has set the backend
525 * state to Closing, but there is no active frontend then set the
526 * backend state to Closed.
527 */
528 if (state == XenbusStateClosing &&
529 !xen_device_frontend_is_active(xendev)) {
530 xen_device_backend_set_state(xendev, XenbusStateClosed);
531 }
532
533 /*
534 * If a backend is still 'online' then we should leave it alone but,
535 * if a backend is not 'online', then the device is a candidate
536 * for destruction. Hence add it to the 'inactive' list to be cleaned
537 * by xen_bus_cleanup().
538 */
539 if (!online &&
540 (state == XenbusStateClosed || state == XenbusStateInitialising ||
541 state == XenbusStateInitWait || state == XenbusStateUnknown) &&
542 !xendev->inactive) {
543 XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
544
545 xendev->inactive = true;
546 QLIST_INSERT_HEAD(&xenbus->inactive_devices, xendev, list);
547
548 /*
549 * Re-write the state to cause a XenBus backend_watch notification,
550 * resulting in a call to xen_bus_cleanup().
551 */
552 xen_device_backend_printf(xendev, "state", "%u", state);
553 }
554 }
555
556 static void xen_device_backend_create(XenDevice *xendev, Error **errp)
557 {
558 ERRP_GUARD();
559 XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
560
561 xendev->backend_path = xen_device_get_backend_path(xendev);
562
563 g_assert(xenbus->xsh);
564
565 xs_node_create(xenbus->xsh, XBT_NULL, xendev->backend_path,
566 xenbus->backend_id, xendev->frontend_id, XS_PERM_READ, errp);
567 if (*errp) {
568 error_prepend(errp, "failed to create backend: ");
569 return;
570 }
571
572 xendev->backend_state_watch =
573 xs_node_watch(xendev->xsh, xendev->backend_path,
574 "state", xen_device_backend_changed, xendev,
575 errp);
576 if (*errp) {
577 error_prepend(errp, "failed to watch backend state: ");
578 return;
579 }
580
581 xendev->backend_online_watch =
582 xs_node_watch(xendev->xsh, xendev->backend_path,
583 "online", xen_device_backend_changed, xendev,
584 errp);
585 if (*errp) {
586 error_prepend(errp, "failed to watch backend online: ");
587 return;
588 }
589 }
590
591 static void xen_device_backend_destroy(XenDevice *xendev)
592 {
593 XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
594 Error *local_err = NULL;
595
596 if (xendev->backend_online_watch) {
597 xs_node_unwatch(xendev->xsh, xendev->backend_online_watch);
598 xendev->backend_online_watch = NULL;
599 }
600
601 if (xendev->backend_state_watch) {
602 xs_node_unwatch(xendev->xsh, xendev->backend_state_watch);
603 xendev->backend_state_watch = NULL;
604 }
605
606 if (!xendev->backend_path) {
607 return;
608 }
609
610 g_assert(xenbus->xsh);
611
612 xs_node_destroy(xenbus->xsh, XBT_NULL, xendev->backend_path,
613 &local_err);
614 g_free(xendev->backend_path);
615 xendev->backend_path = NULL;
616
617 if (local_err) {
618 error_report_err(local_err);
619 }
620 }
621
622 void xen_device_frontend_printf(XenDevice *xendev, const char *key,
623 const char *fmt, ...)
624 {
625 XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
626 Error *local_err = NULL;
627 va_list ap;
628
629 g_assert(xenbus->xsh);
630
631 va_start(ap, fmt);
632 xs_node_vprintf(xenbus->xsh, XBT_NULL, xendev->frontend_path, key,
633 &local_err, fmt, ap);
634 va_end(ap);
635
636 if (local_err) {
637 error_report_err(local_err);
638 }
639 }
640
641 int xen_device_frontend_scanf(XenDevice *xendev, const char *key,
642 const char *fmt, ...)
643 {
644 XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
645 va_list ap;
646 int rc;
647
648 g_assert(xenbus->xsh);
649
650 va_start(ap, fmt);
651 rc = xs_node_vscanf(xenbus->xsh, XBT_NULL, xendev->frontend_path, key,
652 NULL, fmt, ap);
653 va_end(ap);
654
655 return rc;
656 }
657
658 char *xen_device_frontend_read(XenDevice *xendev, const char *key)
659 {
660 XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
661
662 g_assert(xenbus->xsh);
663
664 return xs_node_read(xenbus->xsh, XBT_NULL, NULL, NULL, "%s/%s",
665 xendev->frontend_path, key);
666 }
667
668 static void xen_device_frontend_set_state(XenDevice *xendev,
669 enum xenbus_state state,
670 bool publish)
671 {
672 const char *type = object_get_typename(OBJECT(xendev));
673
674 if (xendev->frontend_state == state) {
675 return;
676 }
677
678 trace_xen_device_frontend_state(type, xendev->name,
679 xs_strstate(state));
680
681 xendev->frontend_state = state;
682 if (publish) {
683 xen_device_frontend_printf(xendev, "state", "%u", state);
684 }
685 }
686
687 static void xen_device_frontend_changed(void *opaque, const char *path)
688 {
689 XenDevice *xendev = opaque;
690 XenDeviceClass *xendev_class = XEN_DEVICE_GET_CLASS(xendev);
691 const char *type = object_get_typename(OBJECT(xendev));
692 enum xenbus_state state;
693
694 trace_xen_device_frontend_changed(type, xendev->name);
695
696 if (xen_device_frontend_scanf(xendev, "state", "%u", &state) != 1) {
697 state = XenbusStateUnknown;
698 }
699
700 xen_device_frontend_set_state(xendev, state, false);
701
702 if (state == XenbusStateInitialising &&
703 xendev->backend_state == XenbusStateClosed &&
704 xendev->backend_online) {
705 /*
706 * The frontend is re-initializing so switch back to
707 * InitWait.
708 */
709 xen_device_backend_set_state(xendev, XenbusStateInitWait);
710 return;
711 }
712
713 if (xendev_class->frontend_changed) {
714 Error *local_err = NULL;
715
716 xendev_class->frontend_changed(xendev, state, &local_err);
717
718 if (local_err) {
719 error_reportf_err(local_err, "frontend change error: ");
720 }
721 }
722 }
723
724 static bool xen_device_frontend_exists(XenDevice *xendev)
725 {
726 enum xenbus_state state;
727
728 return (xen_device_frontend_scanf(xendev, "state", "%u", &state) == 1);
729 }
730
731 static void xen_device_frontend_create(XenDevice *xendev, Error **errp)
732 {
733 ERRP_GUARD();
734 XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
735 XenDeviceClass *xendev_class = XEN_DEVICE_GET_CLASS(xendev);
736
737 if (xendev_class->get_frontend_path) {
738 xendev->frontend_path = xendev_class->get_frontend_path(xendev, errp);
739 if (!xendev->frontend_path) {
740 error_prepend(errp, "failed to create frontend: ");
741 return;
742 }
743 } else {
744 xendev->frontend_path = xen_device_get_frontend_path(xendev);
745 }
746
747 /*
748 * The frontend area may have already been created by a legacy
749 * toolstack.
750 */
751 if (!xen_device_frontend_exists(xendev)) {
752 g_assert(xenbus->xsh);
753
754 xs_node_create(xenbus->xsh, XBT_NULL, xendev->frontend_path,
755 xendev->frontend_id, xenbus->backend_id,
756 XS_PERM_READ | XS_PERM_WRITE, errp);
757 if (*errp) {
758 error_prepend(errp, "failed to create frontend: ");
759 return;
760 }
761 }
762
763 xendev->frontend_state_watch =
764 xs_node_watch(xendev->xsh, xendev->frontend_path, "state",
765 xen_device_frontend_changed, xendev, errp);
766 if (*errp) {
767 error_prepend(errp, "failed to watch frontend state: ");
768 }
769 }
770
771 static void xen_device_frontend_destroy(XenDevice *xendev)
772 {
773 XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
774 Error *local_err = NULL;
775
776 if (xendev->frontend_state_watch) {
777 xs_node_unwatch(xendev->xsh, xendev->frontend_state_watch);
778 xendev->frontend_state_watch = NULL;
779 }
780
781 if (!xendev->frontend_path) {
782 return;
783 }
784
785 g_assert(xenbus->xsh);
786
787 xs_node_destroy(xenbus->xsh, XBT_NULL, xendev->frontend_path,
788 &local_err);
789 g_free(xendev->frontend_path);
790 xendev->frontend_path = NULL;
791
792 if (local_err) {
793 error_report_err(local_err);
794 }
795 }
796
797 void xen_device_set_max_grant_refs(XenDevice *xendev, unsigned int nr_refs,
798 Error **errp)
799 {
800 if (qemu_xen_gnttab_set_max_grants(xendev->xgth, nr_refs)) {
801 error_setg_errno(errp, errno, "xengnttab_set_max_grants failed");
802 }
803 }
804
805 void *xen_device_map_grant_refs(XenDevice *xendev, uint32_t *refs,
806 unsigned int nr_refs, int prot,
807 Error **errp)
808 {
809 void *map = qemu_xen_gnttab_map_refs(xendev->xgth, nr_refs,
810 xendev->frontend_id, refs, prot);
811
812 if (!map) {
813 error_setg_errno(errp, errno,
814 "xengnttab_map_domain_grant_refs failed");
815 }
816
817 return map;
818 }
819
820 void xen_device_unmap_grant_refs(XenDevice *xendev, void *map, uint32_t *refs,
821 unsigned int nr_refs, Error **errp)
822 {
823 if (qemu_xen_gnttab_unmap(xendev->xgth, map, refs, nr_refs)) {
824 error_setg_errno(errp, errno, "xengnttab_unmap failed");
825 }
826 }
827
828 void xen_device_copy_grant_refs(XenDevice *xendev, bool to_domain,
829 XenDeviceGrantCopySegment segs[],
830 unsigned int nr_segs, Error **errp)
831 {
832 qemu_xen_gnttab_grant_copy(xendev->xgth, to_domain, xendev->frontend_id,
833 (XenGrantCopySegment *)segs, nr_segs, errp);
834 }
835
836 struct XenEventChannel {
837 QLIST_ENTRY(XenEventChannel) list;
838 AioContext *ctx;
839 xenevtchn_handle *xeh;
840 evtchn_port_t local_port;
841 XenEventHandler handler;
842 void *opaque;
843 };
844
845 static bool xen_device_poll(void *opaque)
846 {
847 XenEventChannel *channel = opaque;
848
849 return channel->handler(channel->opaque);
850 }
851
852 static void xen_device_event(void *opaque)
853 {
854 XenEventChannel *channel = opaque;
855 unsigned long port = qemu_xen_evtchn_pending(channel->xeh);
856
857 if (port == channel->local_port) {
858 xen_device_poll(channel);
859
860 qemu_xen_evtchn_unmask(channel->xeh, port);
861 }
862 }
863
864 void xen_device_set_event_channel_context(XenDevice *xendev,
865 XenEventChannel *channel,
866 AioContext *ctx,
867 Error **errp)
868 {
869 if (!channel) {
870 error_setg(errp, "bad channel");
871 return;
872 }
873
874 if (channel->ctx)
875 aio_set_fd_handler(channel->ctx, qemu_xen_evtchn_fd(channel->xeh),
876 NULL, NULL, NULL, NULL, NULL);
877
878 channel->ctx = ctx;
879 if (ctx) {
880 aio_set_fd_handler(channel->ctx, qemu_xen_evtchn_fd(channel->xeh),
881 xen_device_event, NULL, xen_device_poll, NULL,
882 channel);
883 }
884 }
885
886 XenEventChannel *xen_device_bind_event_channel(XenDevice *xendev,
887 unsigned int port,
888 XenEventHandler handler,
889 void *opaque, Error **errp)
890 {
891 XenEventChannel *channel = g_new0(XenEventChannel, 1);
892 xenevtchn_port_or_error_t local_port;
893
894 channel->xeh = qemu_xen_evtchn_open();
895 if (!channel->xeh) {
896 error_setg_errno(errp, errno, "failed xenevtchn_open");
897 goto fail;
898 }
899
900 local_port = qemu_xen_evtchn_bind_interdomain(channel->xeh,
901 xendev->frontend_id,
902 port);
903 if (local_port < 0) {
904 error_setg_errno(errp, errno, "xenevtchn_bind_interdomain failed");
905 goto fail;
906 }
907
908 channel->local_port = local_port;
909 channel->handler = handler;
910 channel->opaque = opaque;
911
912 /* Only reason for failure is a NULL channel */
913 xen_device_set_event_channel_context(xendev, channel,
914 qemu_get_aio_context(),
915 &error_abort);
916
917 QLIST_INSERT_HEAD(&xendev->event_channels, channel, list);
918
919 return channel;
920
921 fail:
922 if (channel->xeh) {
923 qemu_xen_evtchn_close(channel->xeh);
924 }
925
926 g_free(channel);
927
928 return NULL;
929 }
930
931 void xen_device_notify_event_channel(XenDevice *xendev,
932 XenEventChannel *channel,
933 Error **errp)
934 {
935 if (!channel) {
936 error_setg(errp, "bad channel");
937 return;
938 }
939
940 if (qemu_xen_evtchn_notify(channel->xeh, channel->local_port) < 0) {
941 error_setg_errno(errp, errno, "xenevtchn_notify failed");
942 }
943 }
944
945 unsigned int xen_event_channel_get_local_port(XenEventChannel *channel)
946 {
947 return channel->local_port;
948 }
949
950 void xen_device_unbind_event_channel(XenDevice *xendev,
951 XenEventChannel *channel,
952 Error **errp)
953 {
954 if (!channel) {
955 error_setg(errp, "bad channel");
956 return;
957 }
958
959 QLIST_REMOVE(channel, list);
960
961 if (channel->ctx) {
962 aio_set_fd_handler(channel->ctx, qemu_xen_evtchn_fd(channel->xeh),
963 NULL, NULL, NULL, NULL, NULL);
964 }
965
966 if (qemu_xen_evtchn_unbind(channel->xeh, channel->local_port) < 0) {
967 error_setg_errno(errp, errno, "xenevtchn_unbind failed");
968 }
969
970 qemu_xen_evtchn_close(channel->xeh);
971 g_free(channel);
972 }
973
974 static void xen_device_unrealize(DeviceState *dev)
975 {
976 XenDevice *xendev = XEN_DEVICE(dev);
977 XenDeviceClass *xendev_class = XEN_DEVICE_GET_CLASS(xendev);
978 const char *type = object_get_typename(OBJECT(xendev));
979 XenEventChannel *channel, *next;
980
981 if (!xendev->name) {
982 return;
983 }
984
985 trace_xen_device_unrealize(type, xendev->name);
986
987 if (xendev->exit.notify) {
988 qemu_remove_exit_notifier(&xendev->exit);
989 xendev->exit.notify = NULL;
990 }
991
992 if (xendev_class->unrealize) {
993 xendev_class->unrealize(xendev);
994 }
995
996 /* Make sure all event channels are cleaned up */
997 QLIST_FOREACH_SAFE(channel, &xendev->event_channels, list, next) {
998 xen_device_unbind_event_channel(xendev, channel, NULL);
999 }
1000
1001 xen_device_frontend_destroy(xendev);
1002 xen_device_backend_destroy(xendev);
1003
1004 if (xendev->xgth) {
1005 qemu_xen_gnttab_close(xendev->xgth);
1006 xendev->xgth = NULL;
1007 }
1008
1009 if (xendev->xsh) {
1010 qemu_xen_xs_close(xendev->xsh);
1011 xendev->xsh = NULL;
1012 }
1013
1014 g_free(xendev->name);
1015 xendev->name = NULL;
1016 }
1017
1018 static void xen_device_exit(Notifier *n, void *data)
1019 {
1020 XenDevice *xendev = container_of(n, XenDevice, exit);
1021
1022 xen_device_unrealize(DEVICE(xendev));
1023 }
1024
1025 static void xen_device_realize(DeviceState *dev, Error **errp)
1026 {
1027 ERRP_GUARD();
1028 XenDevice *xendev = XEN_DEVICE(dev);
1029 XenDeviceClass *xendev_class = XEN_DEVICE_GET_CLASS(xendev);
1030 XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
1031 const char *type = object_get_typename(OBJECT(xendev));
1032
1033 if (xendev->frontend_id == DOMID_INVALID) {
1034 xendev->frontend_id = xen_domid;
1035 }
1036
1037 if (xendev->frontend_id >= DOMID_FIRST_RESERVED) {
1038 error_setg(errp, "invalid frontend-id");
1039 goto unrealize;
1040 }
1041
1042 if (!xendev_class->get_name) {
1043 error_setg(errp, "get_name method not implemented");
1044 goto unrealize;
1045 }
1046
1047 xendev->name = xendev_class->get_name(xendev, errp);
1048 if (*errp) {
1049 error_prepend(errp, "failed to get device name: ");
1050 goto unrealize;
1051 }
1052
1053 trace_xen_device_realize(type, xendev->name);
1054
1055 xendev->xsh = qemu_xen_xs_open();
1056 if (!xendev->xsh) {
1057 error_setg_errno(errp, errno, "failed xs_open");
1058 goto unrealize;
1059 }
1060
1061 xendev->xgth = qemu_xen_gnttab_open();
1062 if (!xendev->xgth) {
1063 error_setg_errno(errp, errno, "failed xengnttab_open");
1064 goto unrealize;
1065 }
1066
1067 xen_device_backend_create(xendev, errp);
1068 if (*errp) {
1069 goto unrealize;
1070 }
1071
1072 xen_device_frontend_create(xendev, errp);
1073 if (*errp) {
1074 goto unrealize;
1075 }
1076
1077 xen_device_backend_printf(xendev, "frontend", "%s",
1078 xendev->frontend_path);
1079 xen_device_backend_printf(xendev, "frontend-id", "%u",
1080 xendev->frontend_id);
1081 xen_device_backend_printf(xendev, "hotplug-status", "connected");
1082
1083 xen_device_backend_set_online(xendev, true);
1084 xen_device_backend_set_state(xendev, XenbusStateInitWait);
1085
1086 if (!xen_device_frontend_exists(xendev)) {
1087 xen_device_frontend_printf(xendev, "backend", "%s",
1088 xendev->backend_path);
1089 xen_device_frontend_printf(xendev, "backend-id", "%u",
1090 xenbus->backend_id);
1091
1092 xen_device_frontend_set_state(xendev, XenbusStateInitialising, true);
1093 }
1094
1095 if (xendev_class->realize) {
1096 xendev_class->realize(xendev, errp);
1097 if (*errp) {
1098 goto unrealize;
1099 }
1100 }
1101
1102 xendev->exit.notify = xen_device_exit;
1103 qemu_add_exit_notifier(&xendev->exit);
1104 return;
1105
1106 unrealize:
1107 xen_device_unrealize(dev);
1108 }
1109
1110 static const Property xen_device_props[] = {
1111 DEFINE_PROP_UINT16("frontend-id", XenDevice, frontend_id,
1112 DOMID_INVALID),
1113 };
1114
1115 static void xen_device_class_init(ObjectClass *class, const void *data)
1116 {
1117 DeviceClass *dev_class = DEVICE_CLASS(class);
1118
1119 dev_class->realize = xen_device_realize;
1120 dev_class->unrealize = xen_device_unrealize;
1121 device_class_set_props(dev_class, xen_device_props);
1122 dev_class->bus_type = TYPE_XEN_BUS;
1123 }
1124
1125 static const TypeInfo xen_device_type_info = {
1126 .name = TYPE_XEN_DEVICE,
1127 .parent = TYPE_DEVICE,
1128 .instance_size = sizeof(XenDevice),
1129 .abstract = true,
1130 .class_size = sizeof(XenDeviceClass),
1131 .class_init = xen_device_class_init,
1132 };
1133
1134 typedef struct XenBridge {
1135 SysBusDevice busdev;
1136 } XenBridge;
1137
1138 #define TYPE_XEN_BRIDGE "xen-bridge"
1139
1140 static const TypeInfo xen_bridge_type_info = {
1141 .name = TYPE_XEN_BRIDGE,
1142 .parent = TYPE_SYS_BUS_DEVICE,
1143 .instance_size = sizeof(XenBridge),
1144 };
1145
1146 static void xen_register_types(void)
1147 {
1148 type_register_static(&xen_bridge_type_info);
1149 type_register_static(&xen_bus_type_info);
1150 type_register_static(&xen_device_type_info);
1151 }
1152
1153 type_init(xen_register_types)
1154
1155 void xen_bus_init(void)
1156 {
1157 DeviceState *dev = qdev_new(TYPE_XEN_BRIDGE);
1158 BusState *bus = qbus_new(TYPE_XEN_BUS, dev, NULL);
1159
1160 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
1161 qbus_set_bus_hotplug_handler(bus);
1162
1163 qemu_create_nic_bus_devices(bus, TYPE_XEN_DEVICE, "xen-net-device",
1164 "xen", "xen-net-device");
1165 }