1 /* 2 * qdev property parsing and global properties 3 * (parts specific for qemu-system-*) 4 * 5 * This file is based on code from hw/qdev-properties.c from 6 * commit 074a86fccd185616469dfcdc0e157f438aebba18, 7 * Copyright (c) Gerd Hoffmann <kraxel@redhat.com> and other contributors. 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2 or later. 10 * See the COPYING file in the top-level directory. 11 */ 12 13 #include "net/net.h" 14 #include "hw/qdev.h" 15 #include "qapi/qmp/qerror.h" 16 #include "sysemu/blockdev.h" 17 #include "hw/block/block.h" 18 #include "net/hub.h" 19 #include "qapi/visitor.h" 20 #include "sysemu/char.h" 21 22 static void get_pointer(Object *obj, Visitor *v, Property *prop, 23 const char *(*print)(void *ptr), 24 const char *name, Error **errp) 25 { 26 DeviceState *dev = DEVICE(obj); 27 void **ptr = qdev_get_prop_ptr(dev, prop); 28 char *p; 29 30 p = (char *) (*ptr ? print(*ptr) : ""); 31 visit_type_str(v, &p, name, errp); 32 } 33 34 static void set_pointer(Object *obj, Visitor *v, Property *prop, 35 int (*parse)(DeviceState *dev, const char *str, 36 void **ptr), 37 const char *name, Error **errp) 38 { 39 DeviceState *dev = DEVICE(obj); 40 Error *local_err = NULL; 41 void **ptr = qdev_get_prop_ptr(dev, prop); 42 char *str; 43 int ret; 44 45 if (dev->realized) { 46 qdev_prop_set_after_realize(dev, name, errp); 47 return; 48 } 49 50 visit_type_str(v, &str, name, &local_err); 51 if (local_err) { 52 error_propagate(errp, local_err); 53 return; 54 } 55 if (!*str) { 56 g_free(str); 57 *ptr = NULL; 58 return; 59 } 60 ret = parse(dev, str, ptr); 61 error_set_from_qdev_prop_error(errp, ret, dev, prop, str); 62 g_free(str); 63 } 64 65 /* --- drive --- */ 66 67 static int parse_drive(DeviceState *dev, const char *str, void **ptr) 68 { 69 BlockDriverState *bs; 70 71 bs = bdrv_find(str); 72 if (bs == NULL) { 73 return -ENOENT; 74 } 75 if (bdrv_attach_dev(bs, dev) < 0) { 76 return -EEXIST; 77 } 78 *ptr = bs; 79 return 0; 80 } 81 82 static void release_drive(Object *obj, const char *name, void *opaque) 83 { 84 DeviceState *dev = DEVICE(obj); 85 Property *prop = opaque; 86 BlockDriverState **ptr = qdev_get_prop_ptr(dev, prop); 87 88 if (*ptr) { 89 bdrv_detach_dev(*ptr, dev); 90 blockdev_auto_del(*ptr); 91 } 92 } 93 94 static const char *print_drive(void *ptr) 95 { 96 return bdrv_get_device_name(ptr); 97 } 98 99 static void get_drive(Object *obj, Visitor *v, void *opaque, 100 const char *name, Error **errp) 101 { 102 get_pointer(obj, v, opaque, print_drive, name, errp); 103 } 104 105 static void set_drive(Object *obj, Visitor *v, void *opaque, 106 const char *name, Error **errp) 107 { 108 set_pointer(obj, v, opaque, parse_drive, name, errp); 109 } 110 111 PropertyInfo qdev_prop_drive = { 112 .name = "drive", 113 .get = get_drive, 114 .set = set_drive, 115 .release = release_drive, 116 }; 117 118 /* --- character device --- */ 119 120 static int parse_chr(DeviceState *dev, const char *str, void **ptr) 121 { 122 CharDriverState *chr = qemu_chr_find(str); 123 if (chr == NULL) { 124 return -ENOENT; 125 } 126 if (qemu_chr_fe_claim(chr) != 0) { 127 return -EEXIST; 128 } 129 *ptr = chr; 130 return 0; 131 } 132 133 static void release_chr(Object *obj, const char *name, void *opaque) 134 { 135 DeviceState *dev = DEVICE(obj); 136 Property *prop = opaque; 137 CharDriverState **ptr = qdev_get_prop_ptr(dev, prop); 138 CharDriverState *chr = *ptr; 139 140 if (chr) { 141 qemu_chr_add_handlers(chr, NULL, NULL, NULL, NULL); 142 qemu_chr_fe_release(chr); 143 } 144 } 145 146 147 static const char *print_chr(void *ptr) 148 { 149 CharDriverState *chr = ptr; 150 151 return chr->label ? chr->label : ""; 152 } 153 154 static void get_chr(Object *obj, Visitor *v, void *opaque, 155 const char *name, Error **errp) 156 { 157 get_pointer(obj, v, opaque, print_chr, name, errp); 158 } 159 160 static void set_chr(Object *obj, Visitor *v, void *opaque, 161 const char *name, Error **errp) 162 { 163 set_pointer(obj, v, opaque, parse_chr, name, errp); 164 } 165 166 PropertyInfo qdev_prop_chr = { 167 .name = "chr", 168 .get = get_chr, 169 .set = set_chr, 170 .release = release_chr, 171 }; 172 173 /* --- netdev device --- */ 174 175 static int parse_netdev(DeviceState *dev, const char *str, void **ptr) 176 { 177 NICPeers *peers_ptr = (NICPeers *)ptr; 178 NICConf *conf = container_of(peers_ptr, NICConf, peers); 179 NetClientState **ncs = peers_ptr->ncs; 180 NetClientState *peers[MAX_QUEUE_NUM]; 181 int queues, i = 0; 182 int ret; 183 184 queues = qemu_find_net_clients_except(str, peers, 185 NET_CLIENT_OPTIONS_KIND_NIC, 186 MAX_QUEUE_NUM); 187 if (queues == 0) { 188 ret = -ENOENT; 189 goto err; 190 } 191 192 if (queues > MAX_QUEUE_NUM) { 193 ret = -E2BIG; 194 goto err; 195 } 196 197 for (i = 0; i < queues; i++) { 198 if (peers[i] == NULL) { 199 ret = -ENOENT; 200 goto err; 201 } 202 203 if (peers[i]->peer) { 204 ret = -EEXIST; 205 goto err; 206 } 207 208 ncs[i] = peers[i]; 209 ncs[i]->queue_index = i; 210 } 211 212 conf->queues = queues; 213 214 return 0; 215 216 err: 217 return ret; 218 } 219 220 static const char *print_netdev(void *ptr) 221 { 222 NetClientState *netdev = ptr; 223 224 return netdev->name ? netdev->name : ""; 225 } 226 227 static void get_netdev(Object *obj, Visitor *v, void *opaque, 228 const char *name, Error **errp) 229 { 230 get_pointer(obj, v, opaque, print_netdev, name, errp); 231 } 232 233 static void set_netdev(Object *obj, Visitor *v, void *opaque, 234 const char *name, Error **errp) 235 { 236 set_pointer(obj, v, opaque, parse_netdev, name, errp); 237 } 238 239 PropertyInfo qdev_prop_netdev = { 240 .name = "netdev", 241 .get = get_netdev, 242 .set = set_netdev, 243 }; 244 245 /* --- vlan --- */ 246 247 static int print_vlan(DeviceState *dev, Property *prop, char *dest, size_t len) 248 { 249 NetClientState **ptr = qdev_get_prop_ptr(dev, prop); 250 251 if (*ptr) { 252 int id; 253 if (!net_hub_id_for_client(*ptr, &id)) { 254 return snprintf(dest, len, "%d", id); 255 } 256 } 257 258 return snprintf(dest, len, "<null>"); 259 } 260 261 static void get_vlan(Object *obj, Visitor *v, void *opaque, 262 const char *name, Error **errp) 263 { 264 DeviceState *dev = DEVICE(obj); 265 Property *prop = opaque; 266 NetClientState **ptr = qdev_get_prop_ptr(dev, prop); 267 int32_t id = -1; 268 269 if (*ptr) { 270 int hub_id; 271 if (!net_hub_id_for_client(*ptr, &hub_id)) { 272 id = hub_id; 273 } 274 } 275 276 visit_type_int32(v, &id, name, errp); 277 } 278 279 static void set_vlan(Object *obj, Visitor *v, void *opaque, 280 const char *name, Error **errp) 281 { 282 DeviceState *dev = DEVICE(obj); 283 Property *prop = opaque; 284 NICPeers *peers_ptr = qdev_get_prop_ptr(dev, prop); 285 NetClientState **ptr = &peers_ptr->ncs[0]; 286 Error *local_err = NULL; 287 int32_t id; 288 NetClientState *hubport; 289 290 if (dev->realized) { 291 qdev_prop_set_after_realize(dev, name, errp); 292 return; 293 } 294 295 visit_type_int32(v, &id, name, &local_err); 296 if (local_err) { 297 error_propagate(errp, local_err); 298 return; 299 } 300 if (id == -1) { 301 *ptr = NULL; 302 return; 303 } 304 305 hubport = net_hub_port_find(id); 306 if (!hubport) { 307 error_set(errp, QERR_INVALID_PARAMETER_VALUE, 308 name, prop->info->name); 309 return; 310 } 311 *ptr = hubport; 312 } 313 314 PropertyInfo qdev_prop_vlan = { 315 .name = "vlan", 316 .print = print_vlan, 317 .get = get_vlan, 318 .set = set_vlan, 319 }; 320 321 int qdev_prop_set_drive(DeviceState *dev, const char *name, 322 BlockDriverState *value) 323 { 324 Error *errp = NULL; 325 const char *bdrv_name = value ? bdrv_get_device_name(value) : ""; 326 object_property_set_str(OBJECT(dev), bdrv_name, 327 name, &errp); 328 if (errp) { 329 qerror_report_err(errp); 330 error_free(errp); 331 return -1; 332 } 333 return 0; 334 } 335 336 void qdev_prop_set_drive_nofail(DeviceState *dev, const char *name, 337 BlockDriverState *value) 338 { 339 if (qdev_prop_set_drive(dev, name, value) < 0) { 340 exit(1); 341 } 342 } 343 void qdev_prop_set_chr(DeviceState *dev, const char *name, 344 CharDriverState *value) 345 { 346 Error *errp = NULL; 347 assert(!value || value->label); 348 object_property_set_str(OBJECT(dev), 349 value ? value->label : "", name, &errp); 350 assert_no_error(errp); 351 } 352 353 void qdev_prop_set_netdev(DeviceState *dev, const char *name, 354 NetClientState *value) 355 { 356 Error *errp = NULL; 357 assert(!value || value->name); 358 object_property_set_str(OBJECT(dev), 359 value ? value->name : "", name, &errp); 360 assert_no_error(errp); 361 } 362 363 void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd) 364 { 365 qdev_prop_set_macaddr(dev, "mac", nd->macaddr.a); 366 if (nd->netdev) { 367 qdev_prop_set_netdev(dev, "netdev", nd->netdev); 368 } 369 if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED && 370 object_property_find(OBJECT(dev), "vectors", NULL)) { 371 qdev_prop_set_uint32(dev, "vectors", nd->nvectors); 372 } 373 nd->instantiated = 1; 374 } 375 376 static int qdev_add_one_global(QemuOpts *opts, void *opaque) 377 { 378 GlobalProperty *g; 379 380 g = g_malloc0(sizeof(*g)); 381 g->driver = qemu_opt_get(opts, "driver"); 382 g->property = qemu_opt_get(opts, "property"); 383 g->value = qemu_opt_get(opts, "value"); 384 qdev_prop_register_global(g); 385 return 0; 386 } 387 388 void qemu_add_globals(void) 389 { 390 qemu_opts_foreach(qemu_find_opts("global"), qdev_add_one_global, NULL, 0); 391 } 392