2
0

xen-bus.c 32 KB

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