2
0

xen-backend.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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/error-report.h"
  9. #include "qapi/error.h"
  10. #include "hw/xen/xen-backend.h"
  11. #include "hw/xen/xen-bus.h"
  12. typedef struct XenBackendImpl {
  13. const char *type;
  14. XenBackendDeviceCreate create;
  15. XenBackendDeviceDestroy destroy;
  16. } XenBackendImpl;
  17. struct XenBackendInstance {
  18. QLIST_ENTRY(XenBackendInstance) entry;
  19. const XenBackendImpl *impl;
  20. XenBus *xenbus;
  21. char *name;
  22. XenDevice *xendev;
  23. };
  24. static GHashTable *xen_backend_table_get(void)
  25. {
  26. static GHashTable *table;
  27. if (table == NULL) {
  28. table = g_hash_table_new(g_str_hash, g_str_equal);
  29. }
  30. return table;
  31. }
  32. static void xen_backend_table_add(XenBackendImpl *impl)
  33. {
  34. g_hash_table_insert(xen_backend_table_get(), (void *)impl->type, impl);
  35. }
  36. static const char **xen_backend_table_keys(unsigned int *count)
  37. {
  38. return (const char **)g_hash_table_get_keys_as_array(
  39. xen_backend_table_get(), count);
  40. }
  41. static const XenBackendImpl *xen_backend_table_lookup(const char *type)
  42. {
  43. return g_hash_table_lookup(xen_backend_table_get(), type);
  44. }
  45. void xen_backend_register(const XenBackendInfo *info)
  46. {
  47. XenBackendImpl *impl = g_new0(XenBackendImpl, 1);
  48. g_assert(info->type);
  49. if (xen_backend_table_lookup(info->type)) {
  50. error_report("attempt to register duplicate Xen backend type '%s'",
  51. info->type);
  52. abort();
  53. }
  54. if (!info->create) {
  55. error_report("backend type '%s' has no creator", info->type);
  56. abort();
  57. }
  58. impl->type = info->type;
  59. impl->create = info->create;
  60. impl->destroy = info->destroy;
  61. xen_backend_table_add(impl);
  62. }
  63. const char **xen_backend_get_types(unsigned int *count)
  64. {
  65. return xen_backend_table_keys(count);
  66. }
  67. static QLIST_HEAD(, XenBackendInstance) backend_list;
  68. static void xen_backend_list_add(XenBackendInstance *backend)
  69. {
  70. QLIST_INSERT_HEAD(&backend_list, backend, entry);
  71. }
  72. static XenBackendInstance *xen_backend_list_find(XenDevice *xendev)
  73. {
  74. XenBackendInstance *backend;
  75. QLIST_FOREACH(backend, &backend_list, entry) {
  76. if (backend->xendev == xendev) {
  77. return backend;
  78. }
  79. }
  80. return NULL;
  81. }
  82. bool xen_backend_exists(const char *type, const char *name)
  83. {
  84. const XenBackendImpl *impl = xen_backend_table_lookup(type);
  85. XenBackendInstance *backend;
  86. if (!impl) {
  87. return false;
  88. }
  89. QLIST_FOREACH(backend, &backend_list, entry) {
  90. if (backend->impl == impl && !strcmp(backend->name, name)) {
  91. return true;
  92. }
  93. }
  94. return false;
  95. }
  96. static void xen_backend_list_remove(XenBackendInstance *backend)
  97. {
  98. QLIST_REMOVE(backend, entry);
  99. }
  100. void xen_backend_device_create(XenBus *xenbus, const char *type,
  101. const char *name, QDict *opts, Error **errp)
  102. {
  103. ERRP_GUARD();
  104. const XenBackendImpl *impl = xen_backend_table_lookup(type);
  105. XenBackendInstance *backend;
  106. if (!impl) {
  107. return;
  108. }
  109. backend = g_new0(XenBackendInstance, 1);
  110. backend->xenbus = xenbus;
  111. backend->name = g_strdup(name);
  112. impl->create(backend, opts, errp);
  113. backend->impl = impl;
  114. xen_backend_list_add(backend);
  115. }
  116. XenBus *xen_backend_get_bus(XenBackendInstance *backend)
  117. {
  118. return backend->xenbus;
  119. }
  120. const char *xen_backend_get_name(XenBackendInstance *backend)
  121. {
  122. return backend->name;
  123. }
  124. void xen_backend_set_device(XenBackendInstance *backend,
  125. XenDevice *xendev)
  126. {
  127. g_assert(!backend->xendev);
  128. backend->xendev = xendev;
  129. }
  130. XenDevice *xen_backend_get_device(XenBackendInstance *backend)
  131. {
  132. return backend->xendev;
  133. }
  134. bool xen_backend_try_device_destroy(XenDevice *xendev, Error **errp)
  135. {
  136. XenBackendInstance *backend = xen_backend_list_find(xendev);
  137. const XenBackendImpl *impl;
  138. if (!backend) {
  139. return false;
  140. }
  141. impl = backend->impl;
  142. if (backend->xendev) {
  143. impl->destroy(backend, errp);
  144. }
  145. xen_backend_list_remove(backend);
  146. g_free(backend->name);
  147. g_free(backend);
  148. return true;
  149. }