2
0

remote-obj.c 4.9 KB

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