2
0

xen-bus.c 30 KB

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