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