remote-obj.c 4.9 KB

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