2 * Copyright © 2020, 2021 Oracle and/or its affiliates.
4 * This work is licensed under the terms of the GNU GPL-v2, version 2 or later.
6 * See the COPYING file in the top-level directory.
10 #include "qemu/osdep.h"
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/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"
25 #define TYPE_REMOTE_OBJECT "x-remote-object"
26 OBJECT_DECLARE_TYPE(RemoteObject
, RemoteObjectClass
, REMOTE_OBJECT
)
28 struct RemoteObjectClass
{
29 ObjectClass parent_class
;
32 unsigned int max_devs
;
39 Notifier machine_done
;
47 DeviceListener listener
;
50 static void remote_object_set_fd(Object
*obj
, const char *str
, Error
**errp
)
53 RemoteObject
*o
= REMOTE_OBJECT(obj
);
56 fd
= monitor_fd_param(monitor_cur(), str
, errp
);
58 error_prepend(errp
, "Could not parse remote object fd %s:", str
);
62 if (!fd_is_socket(fd
)) {
63 error_setg(errp
, "File descriptor '%s' is not a socket", str
);
71 static void remote_object_set_devid(Object
*obj
, const char *str
, Error
**errp
)
73 RemoteObject
*o
= REMOTE_OBJECT(obj
);
77 o
->devid
= g_strdup(str
);
80 static void remote_object_unrealize_listener(DeviceListener
*listener
,
83 RemoteObject
*o
= container_of(listener
, RemoteObject
, listener
);
86 object_unref(OBJECT(o
));
90 static void remote_object_machine_done(Notifier
*notifier
, void *data
)
92 RemoteObject
*o
= container_of(notifier
, RemoteObject
, machine_done
);
93 DeviceState
*dev
= NULL
;
94 QIOChannel
*ioc
= NULL
;
96 RemoteCommDev
*comdev
= NULL
;
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
);
105 ioc
= qio_channel_new_fd(o
->fd
, &err
);
107 error_report_err(err
);
110 qio_channel_set_blocking(ioc
, false, NULL
);
114 o
->listener
.unrealize
= remote_object_unrealize_listener
;
115 device_listener_register(&o
->listener
);
117 /* co-routine should free this. */
118 comdev
= g_new0(RemoteCommDev
, 1);
119 *comdev
= (RemoteCommDev
) {
121 .dev
= PCI_DEVICE(dev
),
124 co
= qemu_coroutine_create(mpqemu_remote_msg_loop_co
, comdev
);
125 qemu_coroutine_enter(co
);
128 static void remote_object_init(Object
*obj
)
130 RemoteObjectClass
*k
= REMOTE_OBJECT_GET_CLASS(obj
);
131 RemoteObject
*o
= REMOTE_OBJECT(obj
);
133 if (k
->nr_devs
>= k
->max_devs
) {
134 error_report("Reached maximum number of devices: %u", k
->max_devs
);
144 o
->machine_done
.notify
= remote_object_machine_done
;
145 qemu_add_machine_init_done_notifier(&o
->machine_done
);
148 static void remote_object_finalize(Object
*obj
)
150 RemoteObjectClass
*k
= REMOTE_OBJECT_GET_CLASS(obj
);
151 RemoteObject
*o
= REMOTE_OBJECT(obj
);
153 device_listener_unregister(&o
->listener
);
156 qio_channel_shutdown(o
->ioc
, QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
);
157 qio_channel_close(o
->ioc
, NULL
);
160 object_unref(OBJECT(o
->ioc
));
166 static void remote_object_class_init(ObjectClass
*klass
, void *data
)
168 RemoteObjectClass
*k
= REMOTE_OBJECT_CLASS(klass
);
171 * Limit number of supported devices to 1. This is done to avoid devices
172 * from one VM accessing the RAM of another VM. This is done until we
173 * start using separate address spaces for individual devices.
178 object_class_property_add_str(klass
, "fd", NULL
, remote_object_set_fd
);
179 object_class_property_add_str(klass
, "devid", NULL
,
180 remote_object_set_devid
);
183 static const TypeInfo remote_object_info
= {
184 .name
= TYPE_REMOTE_OBJECT
,
185 .parent
= TYPE_OBJECT
,
186 .instance_size
= sizeof(RemoteObject
),
187 .instance_init
= remote_object_init
,
188 .instance_finalize
= remote_object_finalize
,
189 .class_size
= sizeof(RemoteObjectClass
),
190 .class_init
= remote_object_class_init
,
191 .interfaces
= (InterfaceInfo
[]) {
192 { TYPE_USER_CREATABLE
},
197 static void register_types(void)
199 type_register_static(&remote_object_info
);
202 type_init(register_types
);