1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
/**
* QEMU vfio-user-server server object
*
* Copyright © 2022 Oracle and/or its affiliates.
*
* This work is licensed under the terms of the GNU GPL-v2, version 2 or later.
*
* See the COPYING file in the top-level directory.
*
*/
/**
* Usage: add options:
* -machine x-remote,vfio-user=on,auto-shutdown=on
* -device <PCI-device>,id=<pci-dev-id>
* -object x-vfio-user-server,id=<id>,type=unix,path=<socket-path>,
* device=<pci-dev-id>
*
* Note that x-vfio-user-server object must be used with x-remote machine only.
* This server could only support PCI devices for now.
*
* type - SocketAddress type - presently "unix" alone is supported. Required
* option
*
* path - named unix socket, it will be created by the server. It is
* a required option
*
* device - id of a device on the server, a required option. PCI devices
* alone are supported presently.
*/
#include "qemu/osdep.h"
#include "qom/object.h"
#include "qom/object_interfaces.h"
#include "qemu/error-report.h"
#include "trace.h"
#include "sysemu/runstate.h"
#include "hw/boards.h"
#include "hw/remote/machine.h"
#include "qapi/error.h"
#include "qapi/qapi-visit-sockets.h"
#define TYPE_VFU_OBJECT "x-vfio-user-server"
OBJECT_DECLARE_TYPE(VfuObject, VfuObjectClass, VFU_OBJECT)
/**
* VFU_OBJECT_ERROR - reports an error message. If auto_shutdown
* is set, it aborts the machine on error. Otherwise, it logs an
* error message without aborting.
*/
#define VFU_OBJECT_ERROR(o, fmt, ...) \
{ \
if (vfu_object_auto_shutdown()) { \
error_setg(&error_abort, (fmt), ## __VA_ARGS__); \
} else { \
error_report((fmt), ## __VA_ARGS__); \
} \
} \
struct VfuObjectClass {
ObjectClass parent_class;
unsigned int nr_devs;
};
struct VfuObject {
/* private */
Object parent;
SocketAddress *socket;
char *device;
Error *err;
};
static bool vfu_object_auto_shutdown(void)
{
bool auto_shutdown = true;
Error *local_err = NULL;
if (!current_machine) {
return auto_shutdown;
}
auto_shutdown = object_property_get_bool(OBJECT(current_machine),
"auto-shutdown",
&local_err);
/*
* local_err would be set if no such property exists - safe to ignore.
* Unlikely scenario as auto-shutdown is always defined for
* TYPE_REMOTE_MACHINE, and TYPE_VFU_OBJECT only works with
* TYPE_REMOTE_MACHINE
*/
if (local_err) {
auto_shutdown = true;
error_free(local_err);
}
return auto_shutdown;
}
static void vfu_object_set_socket(Object *obj, Visitor *v, const char *name,
void *opaque, Error **errp)
{
VfuObject *o = VFU_OBJECT(obj);
qapi_free_SocketAddress(o->socket);
o->socket = NULL;
visit_type_SocketAddress(v, name, &o->socket, errp);
if (o->socket->type != SOCKET_ADDRESS_TYPE_UNIX) {
error_setg(errp, "vfu: Unsupported socket type - %s",
SocketAddressType_str(o->socket->type));
qapi_free_SocketAddress(o->socket);
o->socket = NULL;
return;
}
trace_vfu_prop("socket", o->socket->u.q_unix.path);
}
static void vfu_object_set_device(Object *obj, const char *str, Error **errp)
{
VfuObject *o = VFU_OBJECT(obj);
g_free(o->device);
o->device = g_strdup(str);
trace_vfu_prop("device", str);
}
static void vfu_object_init(Object *obj)
{
VfuObjectClass *k = VFU_OBJECT_GET_CLASS(obj);
VfuObject *o = VFU_OBJECT(obj);
k->nr_devs++;
if (!object_dynamic_cast(OBJECT(current_machine), TYPE_REMOTE_MACHINE)) {
error_setg(&o->err, "vfu: %s only compatible with %s machine",
TYPE_VFU_OBJECT, TYPE_REMOTE_MACHINE);
return;
}
}
static void vfu_object_finalize(Object *obj)
{
VfuObjectClass *k = VFU_OBJECT_GET_CLASS(obj);
VfuObject *o = VFU_OBJECT(obj);
k->nr_devs--;
qapi_free_SocketAddress(o->socket);
o->socket = NULL;
g_free(o->device);
o->device = NULL;
if (!k->nr_devs && vfu_object_auto_shutdown()) {
qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
}
}
static void vfu_object_class_init(ObjectClass *klass, void *data)
{
VfuObjectClass *k = VFU_OBJECT_CLASS(klass);
k->nr_devs = 0;
object_class_property_add(klass, "socket", "SocketAddress", NULL,
vfu_object_set_socket, NULL, NULL);
object_class_property_set_description(klass, "socket",
"SocketAddress "
"(ex: type=unix,path=/tmp/sock). "
"Only UNIX is presently supported");
object_class_property_add_str(klass, "device", NULL,
vfu_object_set_device);
object_class_property_set_description(klass, "device",
"device ID - only PCI devices "
"are presently supported");
}
static const TypeInfo vfu_object_info = {
.name = TYPE_VFU_OBJECT,
.parent = TYPE_OBJECT,
.instance_size = sizeof(VfuObject),
.instance_init = vfu_object_init,
.instance_finalize = vfu_object_finalize,
.class_size = sizeof(VfuObjectClass),
.class_init = vfu_object_class_init,
.interfaces = (InterfaceInfo[]) {
{ TYPE_USER_CREATABLE },
{ }
}
};
static void vfu_register_types(void)
{
type_register_static(&vfu_object_info);
}
type_init(vfu_register_types);
|