remote-obj.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright © 2020, 2021 Oracle and/or its affiliates.
  3. *
  4. * This work is licensed under the terms of the GNU GPL-v2, version 2 or later.
  5. *
  6. * See the COPYING file in the top-level directory.
  7. *
  8. */
  9. #include "qemu/osdep.h"
  10. #include "qemu-common.h"
  11. #include "qemu/error-report.h"
  12. #include "qemu/notify.h"
  13. #include "qom/object_interfaces.h"
  14. #include "hw/qdev-core.h"
  15. #include "io/channel.h"
  16. #include "hw/qdev-core.h"
  17. #include "hw/remote/machine.h"
  18. #include "io/channel-util.h"
  19. #include "qapi/error.h"
  20. #include "sysemu/sysemu.h"
  21. #include "hw/pci/pci.h"
  22. #include "qemu/sockets.h"
  23. #include "monitor/monitor.h"
  24. #define TYPE_REMOTE_OBJECT "x-remote-object"
  25. OBJECT_DECLARE_TYPE(RemoteObject, RemoteObjectClass, REMOTE_OBJECT)
  26. struct RemoteObjectClass {
  27. ObjectClass parent_class;
  28. unsigned int nr_devs;
  29. unsigned int max_devs;
  30. };
  31. struct RemoteObject {
  32. /* private */
  33. Object parent;
  34. Notifier machine_done;
  35. int32_t fd;
  36. char *devid;
  37. QIOChannel *ioc;
  38. DeviceState *dev;
  39. DeviceListener listener;
  40. };
  41. static void remote_object_set_fd(Object *obj, const char *str, Error **errp)
  42. {
  43. RemoteObject *o = REMOTE_OBJECT(obj);
  44. int fd = -1;
  45. fd = monitor_fd_param(monitor_cur(), str, errp);
  46. if (fd == -1) {
  47. error_prepend(errp, "Could not parse remote object fd %s:", str);
  48. return;
  49. }
  50. if (!fd_is_socket(fd)) {
  51. error_setg(errp, "File descriptor '%s' is not a socket", str);
  52. close(fd);
  53. return;
  54. }
  55. o->fd = fd;
  56. }
  57. static void remote_object_set_devid(Object *obj, const char *str, Error **errp)
  58. {
  59. RemoteObject *o = REMOTE_OBJECT(obj);
  60. g_free(o->devid);
  61. o->devid = g_strdup(str);
  62. }
  63. static void remote_object_unrealize_listener(DeviceListener *listener,
  64. DeviceState *dev)
  65. {
  66. RemoteObject *o = container_of(listener, RemoteObject, listener);
  67. if (o->dev == dev) {
  68. object_unref(OBJECT(o));
  69. }
  70. }
  71. static void remote_object_machine_done(Notifier *notifier, void *data)
  72. {
  73. RemoteObject *o = container_of(notifier, RemoteObject, machine_done);
  74. DeviceState *dev = NULL;
  75. QIOChannel *ioc = NULL;
  76. Coroutine *co = NULL;
  77. RemoteCommDev *comdev = NULL;
  78. Error *err = NULL;
  79. dev = qdev_find_recursive(sysbus_get_default(), o->devid);
  80. if (!dev || !object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
  81. error_report("%s is not a PCI device", o->devid);
  82. return;
  83. }
  84. ioc = qio_channel_new_fd(o->fd, &err);
  85. if (!ioc) {
  86. error_report_err(err);
  87. return;
  88. }
  89. qio_channel_set_blocking(ioc, false, NULL);
  90. o->dev = dev;
  91. o->listener.unrealize = remote_object_unrealize_listener;
  92. device_listener_register(&o->listener);
  93. /* co-routine should free this. */
  94. comdev = g_new0(RemoteCommDev, 1);
  95. *comdev = (RemoteCommDev) {
  96. .ioc = ioc,
  97. .dev = PCI_DEVICE(dev),
  98. };
  99. co = qemu_coroutine_create(mpqemu_remote_msg_loop_co, comdev);
  100. qemu_coroutine_enter(co);
  101. }
  102. static void remote_object_init(Object *obj)
  103. {
  104. RemoteObjectClass *k = REMOTE_OBJECT_GET_CLASS(obj);
  105. RemoteObject *o = REMOTE_OBJECT(obj);
  106. if (k->nr_devs >= k->max_devs) {
  107. error_report("Reached maximum number of devices: %u", k->max_devs);
  108. return;
  109. }
  110. o->ioc = NULL;
  111. o->fd = -1;
  112. o->devid = NULL;
  113. k->nr_devs++;
  114. o->machine_done.notify = remote_object_machine_done;
  115. qemu_add_machine_init_done_notifier(&o->machine_done);
  116. }
  117. static void remote_object_finalize(Object *obj)
  118. {
  119. RemoteObjectClass *k = REMOTE_OBJECT_GET_CLASS(obj);
  120. RemoteObject *o = REMOTE_OBJECT(obj);
  121. device_listener_unregister(&o->listener);
  122. if (o->ioc) {
  123. qio_channel_shutdown(o->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
  124. qio_channel_close(o->ioc, NULL);
  125. }
  126. object_unref(OBJECT(o->ioc));
  127. k->nr_devs--;
  128. g_free(o->devid);
  129. }
  130. static void remote_object_class_init(ObjectClass *klass, void *data)
  131. {
  132. RemoteObjectClass *k = REMOTE_OBJECT_CLASS(klass);
  133. /*
  134. * Limit number of supported devices to 1. This is done to avoid devices
  135. * from one VM accessing the RAM of another VM. This is done until we
  136. * start using separate address spaces for individual devices.
  137. */
  138. k->max_devs = 1;
  139. k->nr_devs = 0;
  140. object_class_property_add_str(klass, "fd", NULL, remote_object_set_fd);
  141. object_class_property_add_str(klass, "devid", NULL,
  142. remote_object_set_devid);
  143. }
  144. static const TypeInfo remote_object_info = {
  145. .name = TYPE_REMOTE_OBJECT,
  146. .parent = TYPE_OBJECT,
  147. .instance_size = sizeof(RemoteObject),
  148. .instance_init = remote_object_init,
  149. .instance_finalize = remote_object_finalize,
  150. .class_size = sizeof(RemoteObjectClass),
  151. .class_init = remote_object_class_init,
  152. .interfaces = (InterfaceInfo[]) {
  153. { TYPE_USER_CREATABLE },
  154. { }
  155. }
  156. };
  157. static void register_types(void)
  158. {
  159. type_register_static(&remote_object_info);
  160. }
  161. type_init(register_types);