2012-12-05 20:49:11 +04:00
|
|
|
/*
|
|
|
|
* qdev property parsing and global properties
|
|
|
|
* (parts specific for qemu-system-*)
|
|
|
|
*
|
|
|
|
* This file is based on code from hw/qdev-properties.c from
|
|
|
|
* commit 074a86fccd185616469dfcdc0e157f438aebba18,
|
|
|
|
* Copyright (c) Gerd Hoffmann <kraxel@redhat.com> and other contributors.
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
2012-12-23 03:39:34 +04:00
|
|
|
#include "net/net.h"
|
2013-02-04 18:40:22 +04:00
|
|
|
#include "hw/qdev.h"
|
2012-12-23 03:39:34 +04:00
|
|
|
#include "qapi/qmp/qerror.h"
|
2014-10-07 15:59:18 +04:00
|
|
|
#include "sysemu/block-backend.h"
|
2012-12-23 03:39:34 +04:00
|
|
|
#include "sysemu/blockdev.h"
|
2013-02-05 20:06:20 +04:00
|
|
|
#include "hw/block/block.h"
|
2012-12-05 20:49:11 +04:00
|
|
|
#include "net/hub.h"
|
2012-12-23 03:39:34 +04:00
|
|
|
#include "qapi/visitor.h"
|
2013-04-08 18:55:25 +04:00
|
|
|
#include "sysemu/char.h"
|
2014-03-03 14:30:07 +04:00
|
|
|
#include "sysemu/iothread.h"
|
2012-12-05 20:49:11 +04:00
|
|
|
|
|
|
|
static void get_pointer(Object *obj, Visitor *v, Property *prop,
|
2014-03-03 14:30:06 +04:00
|
|
|
char *(*print)(void *ptr),
|
2012-12-05 20:49:11 +04:00
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
|
|
|
DeviceState *dev = DEVICE(obj);
|
|
|
|
void **ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
char *p;
|
|
|
|
|
2014-03-03 14:30:06 +04:00
|
|
|
p = *ptr ? print(*ptr) : g_strdup("");
|
2012-12-05 20:49:11 +04:00
|
|
|
visit_type_str(v, &p, name, errp);
|
2014-03-03 14:30:06 +04:00
|
|
|
g_free(p);
|
2012-12-05 20:49:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void set_pointer(Object *obj, Visitor *v, Property *prop,
|
|
|
|
int (*parse)(DeviceState *dev, const char *str,
|
|
|
|
void **ptr),
|
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
|
|
|
DeviceState *dev = DEVICE(obj);
|
|
|
|
Error *local_err = NULL;
|
|
|
|
void **ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
char *str;
|
|
|
|
int ret;
|
|
|
|
|
2013-01-09 06:58:10 +04:00
|
|
|
if (dev->realized) {
|
2013-03-25 17:40:44 +04:00
|
|
|
qdev_prop_set_after_realize(dev, name, errp);
|
2012-12-05 20:49:11 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
visit_type_str(v, &str, name, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!*str) {
|
|
|
|
g_free(str);
|
|
|
|
*ptr = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ret = parse(dev, str, ptr);
|
|
|
|
error_set_from_qdev_prop_error(errp, ret, dev, prop, str);
|
|
|
|
g_free(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --- drive --- */
|
|
|
|
|
|
|
|
static int parse_drive(DeviceState *dev, const char *str, void **ptr)
|
|
|
|
{
|
2014-10-07 15:59:18 +04:00
|
|
|
BlockBackend *blk;
|
2012-12-05 20:49:11 +04:00
|
|
|
|
2014-10-07 15:59:18 +04:00
|
|
|
blk = blk_by_name(str);
|
|
|
|
if (!blk) {
|
2012-12-05 20:49:11 +04:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
2014-10-07 15:59:18 +04:00
|
|
|
if (blk_attach_dev(blk, dev) < 0) {
|
2012-12-05 20:49:11 +04:00
|
|
|
return -EEXIST;
|
|
|
|
}
|
2014-10-07 15:59:18 +04:00
|
|
|
*ptr = blk;
|
2012-12-05 20:49:11 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void release_drive(Object *obj, const char *name, void *opaque)
|
|
|
|
{
|
|
|
|
DeviceState *dev = DEVICE(obj);
|
|
|
|
Property *prop = opaque;
|
2014-10-07 15:59:18 +04:00
|
|
|
BlockBackend **ptr = qdev_get_prop_ptr(dev, prop);
|
2012-12-05 20:49:11 +04:00
|
|
|
|
|
|
|
if (*ptr) {
|
2014-10-07 15:59:18 +04:00
|
|
|
blk_detach_dev(*ptr, dev);
|
2012-12-05 20:49:11 +04:00
|
|
|
blockdev_auto_del(*ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-03 14:30:06 +04:00
|
|
|
static char *print_drive(void *ptr)
|
2012-12-05 20:49:11 +04:00
|
|
|
{
|
2014-10-07 15:59:18 +04:00
|
|
|
return g_strdup(blk_name(ptr));
|
2012-12-05 20:49:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void get_drive(Object *obj, Visitor *v, void *opaque,
|
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
|
|
|
get_pointer(obj, v, opaque, print_drive, name, errp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_drive(Object *obj, Visitor *v, void *opaque,
|
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
|
|
|
set_pointer(obj, v, opaque, parse_drive, name, errp);
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyInfo qdev_prop_drive = {
|
2014-02-08 14:01:56 +04:00
|
|
|
.name = "str",
|
2014-10-07 10:33:20 +04:00
|
|
|
.description = "ID of a drive to use as a backend",
|
2012-12-05 20:49:11 +04:00
|
|
|
.get = get_drive,
|
|
|
|
.set = set_drive,
|
|
|
|
.release = release_drive,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* --- character device --- */
|
|
|
|
|
|
|
|
static int parse_chr(DeviceState *dev, const char *str, void **ptr)
|
|
|
|
{
|
|
|
|
CharDriverState *chr = qemu_chr_find(str);
|
|
|
|
if (chr == NULL) {
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
2013-03-27 23:29:39 +04:00
|
|
|
if (qemu_chr_fe_claim(chr) != 0) {
|
2012-12-05 20:49:11 +04:00
|
|
|
return -EEXIST;
|
|
|
|
}
|
|
|
|
*ptr = chr;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void release_chr(Object *obj, const char *name, void *opaque)
|
|
|
|
{
|
|
|
|
DeviceState *dev = DEVICE(obj);
|
|
|
|
Property *prop = opaque;
|
|
|
|
CharDriverState **ptr = qdev_get_prop_ptr(dev, prop);
|
2013-03-26 14:07:59 +04:00
|
|
|
CharDriverState *chr = *ptr;
|
2012-12-05 20:49:11 +04:00
|
|
|
|
2013-03-26 14:07:59 +04:00
|
|
|
if (chr) {
|
|
|
|
qemu_chr_add_handlers(chr, NULL, NULL, NULL, NULL);
|
2013-03-27 23:29:39 +04:00
|
|
|
qemu_chr_fe_release(chr);
|
2012-12-05 20:49:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-03 14:30:06 +04:00
|
|
|
static char *print_chr(void *ptr)
|
2012-12-05 20:49:11 +04:00
|
|
|
{
|
|
|
|
CharDriverState *chr = ptr;
|
2014-03-03 14:30:06 +04:00
|
|
|
const char *val = chr->label ? chr->label : "";
|
2012-12-05 20:49:11 +04:00
|
|
|
|
2014-03-03 14:30:06 +04:00
|
|
|
return g_strdup(val);
|
2012-12-05 20:49:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void get_chr(Object *obj, Visitor *v, void *opaque,
|
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
|
|
|
get_pointer(obj, v, opaque, print_chr, name, errp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_chr(Object *obj, Visitor *v, void *opaque,
|
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
|
|
|
set_pointer(obj, v, opaque, parse_chr, name, errp);
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyInfo qdev_prop_chr = {
|
2014-02-08 14:01:56 +04:00
|
|
|
.name = "str",
|
2014-10-07 10:33:20 +04:00
|
|
|
.description = "ID of a chardev to use as a backend",
|
2012-12-05 20:49:11 +04:00
|
|
|
.get = get_chr,
|
|
|
|
.set = set_chr,
|
|
|
|
.release = release_chr,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* --- netdev device --- */
|
2014-10-13 09:16:37 +04:00
|
|
|
static void get_netdev(Object *obj, Visitor *v, void *opaque,
|
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
|
|
|
DeviceState *dev = DEVICE(obj);
|
|
|
|
Property *prop = opaque;
|
|
|
|
NICPeers *peers_ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
char *p = g_strdup(peers_ptr->ncs[0] ? peers_ptr->ncs[0]->name : "");
|
|
|
|
|
|
|
|
visit_type_str(v, &p, name, errp);
|
|
|
|
g_free(p);
|
|
|
|
}
|
2012-12-05 20:49:11 +04:00
|
|
|
|
2014-10-13 09:16:37 +04:00
|
|
|
static void set_netdev(Object *obj, Visitor *v, void *opaque,
|
|
|
|
const char *name, Error **errp)
|
2012-12-05 20:49:11 +04:00
|
|
|
{
|
2014-10-13 09:16:37 +04:00
|
|
|
DeviceState *dev = DEVICE(obj);
|
|
|
|
Property *prop = opaque;
|
|
|
|
NICPeers *peers_ptr = qdev_get_prop_ptr(dev, prop);
|
2013-01-30 15:12:28 +04:00
|
|
|
NetClientState **ncs = peers_ptr->ncs;
|
|
|
|
NetClientState *peers[MAX_QUEUE_NUM];
|
2014-10-13 09:16:37 +04:00
|
|
|
Error *local_err = NULL;
|
|
|
|
int queues, err = 0, i = 0;
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
if (dev->realized) {
|
|
|
|
qdev_prop_set_after_realize(dev, name, errp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
visit_type_str(v, &str, name, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
|
|
|
}
|
2012-12-05 20:49:11 +04:00
|
|
|
|
2013-01-30 15:12:28 +04:00
|
|
|
queues = qemu_find_net_clients_except(str, peers,
|
|
|
|
NET_CLIENT_OPTIONS_KIND_NIC,
|
|
|
|
MAX_QUEUE_NUM);
|
|
|
|
if (queues == 0) {
|
2014-10-13 09:16:37 +04:00
|
|
|
err = -ENOENT;
|
|
|
|
goto out;
|
2012-12-05 20:49:11 +04:00
|
|
|
}
|
2013-01-30 15:12:28 +04:00
|
|
|
|
|
|
|
if (queues > MAX_QUEUE_NUM) {
|
2014-10-13 09:16:37 +04:00
|
|
|
error_setg(errp, "queues of backend '%s'(%d) exceeds QEMU limitation(%d)",
|
|
|
|
str, queues, MAX_QUEUE_NUM);
|
|
|
|
goto out;
|
2013-01-30 15:12:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < queues; i++) {
|
|
|
|
if (peers[i] == NULL) {
|
2014-10-13 09:16:37 +04:00
|
|
|
err = -ENOENT;
|
|
|
|
goto out;
|
2013-01-30 15:12:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (peers[i]->peer) {
|
2014-10-13 09:16:37 +04:00
|
|
|
err = -EEXIST;
|
|
|
|
goto out;
|
2013-01-30 15:12:28 +04:00
|
|
|
}
|
|
|
|
|
2013-11-08 06:13:09 +04:00
|
|
|
if (ncs[i]) {
|
2014-10-13 09:16:37 +04:00
|
|
|
err = -EINVAL;
|
|
|
|
goto out;
|
2013-11-08 06:13:09 +04:00
|
|
|
}
|
|
|
|
|
2013-01-30 15:12:28 +04:00
|
|
|
ncs[i] = peers[i];
|
|
|
|
ncs[i]->queue_index = i;
|
2012-12-05 20:49:11 +04:00
|
|
|
}
|
2013-01-30 15:12:28 +04:00
|
|
|
|
2014-05-26 14:04:08 +04:00
|
|
|
peers_ptr->queues = queues;
|
2013-01-30 15:12:28 +04:00
|
|
|
|
2014-10-13 09:16:37 +04:00
|
|
|
out:
|
|
|
|
error_set_from_qdev_prop_error(errp, err, dev, prop, str);
|
|
|
|
g_free(str);
|
2012-12-05 20:49:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PropertyInfo qdev_prop_netdev = {
|
2014-02-08 14:01:56 +04:00
|
|
|
.name = "str",
|
2014-10-07 10:33:20 +04:00
|
|
|
.description = "ID of a netdev to use as a backend",
|
2012-12-05 20:49:11 +04:00
|
|
|
.get = get_netdev,
|
|
|
|
.set = set_netdev,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* --- vlan --- */
|
|
|
|
|
|
|
|
static int print_vlan(DeviceState *dev, Property *prop, char *dest, size_t len)
|
|
|
|
{
|
|
|
|
NetClientState **ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
|
|
|
|
if (*ptr) {
|
|
|
|
int id;
|
|
|
|
if (!net_hub_id_for_client(*ptr, &id)) {
|
|
|
|
return snprintf(dest, len, "%d", id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return snprintf(dest, len, "<null>");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void get_vlan(Object *obj, Visitor *v, void *opaque,
|
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
|
|
|
DeviceState *dev = DEVICE(obj);
|
|
|
|
Property *prop = opaque;
|
|
|
|
NetClientState **ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
int32_t id = -1;
|
|
|
|
|
|
|
|
if (*ptr) {
|
|
|
|
int hub_id;
|
|
|
|
if (!net_hub_id_for_client(*ptr, &hub_id)) {
|
|
|
|
id = hub_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
visit_type_int32(v, &id, name, errp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_vlan(Object *obj, Visitor *v, void *opaque,
|
|
|
|
const char *name, Error **errp)
|
|
|
|
{
|
|
|
|
DeviceState *dev = DEVICE(obj);
|
|
|
|
Property *prop = opaque;
|
2013-01-30 15:12:28 +04:00
|
|
|
NICPeers *peers_ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
NetClientState **ptr = &peers_ptr->ncs[0];
|
2012-12-05 20:49:11 +04:00
|
|
|
Error *local_err = NULL;
|
|
|
|
int32_t id;
|
|
|
|
NetClientState *hubport;
|
|
|
|
|
2013-01-09 06:58:10 +04:00
|
|
|
if (dev->realized) {
|
2013-03-25 17:40:44 +04:00
|
|
|
qdev_prop_set_after_realize(dev, name, errp);
|
2012-12-05 20:49:11 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
visit_type_int32(v, &id, name, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (id == -1) {
|
|
|
|
*ptr = NULL;
|
|
|
|
return;
|
|
|
|
}
|
2013-11-08 06:13:09 +04:00
|
|
|
if (*ptr) {
|
|
|
|
error_set_from_qdev_prop_error(errp, -EINVAL, dev, prop, name);
|
|
|
|
return;
|
|
|
|
}
|
2012-12-05 20:49:11 +04:00
|
|
|
|
|
|
|
hubport = net_hub_port_find(id);
|
|
|
|
if (!hubport) {
|
|
|
|
error_set(errp, QERR_INVALID_PARAMETER_VALUE,
|
|
|
|
name, prop->info->name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*ptr = hubport;
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyInfo qdev_prop_vlan = {
|
2014-02-08 14:01:56 +04:00
|
|
|
.name = "int32",
|
2014-10-07 10:33:20 +04:00
|
|
|
.description = "Integer VLAN id to connect to",
|
2012-12-05 20:49:11 +04:00
|
|
|
.print = print_vlan,
|
|
|
|
.get = get_vlan,
|
|
|
|
.set = set_vlan,
|
|
|
|
};
|
|
|
|
|
|
|
|
int qdev_prop_set_drive(DeviceState *dev, const char *name,
|
2014-10-07 15:59:18 +04:00
|
|
|
BlockBackend *value)
|
2012-12-05 20:49:11 +04:00
|
|
|
{
|
2014-04-25 14:44:20 +04:00
|
|
|
Error *err = NULL;
|
2014-10-07 15:59:18 +04:00
|
|
|
object_property_set_str(OBJECT(dev),
|
|
|
|
value ? blk_name(value) : "", name, &err);
|
2014-04-25 14:44:20 +04:00
|
|
|
if (err) {
|
|
|
|
qerror_report_err(err);
|
|
|
|
error_free(err);
|
2012-12-05 20:49:11 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void qdev_prop_set_drive_nofail(DeviceState *dev, const char *name,
|
2014-10-07 15:59:18 +04:00
|
|
|
BlockBackend *value)
|
2012-12-05 20:49:11 +04:00
|
|
|
{
|
|
|
|
if (qdev_prop_set_drive(dev, name, value) < 0) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void qdev_prop_set_chr(DeviceState *dev, const char *name,
|
|
|
|
CharDriverState *value)
|
|
|
|
{
|
|
|
|
assert(!value || value->label);
|
|
|
|
object_property_set_str(OBJECT(dev),
|
2014-01-02 06:48:08 +04:00
|
|
|
value ? value->label : "", name, &error_abort);
|
2012-12-05 20:49:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void qdev_prop_set_netdev(DeviceState *dev, const char *name,
|
|
|
|
NetClientState *value)
|
|
|
|
{
|
|
|
|
assert(!value || value->name);
|
|
|
|
object_property_set_str(OBJECT(dev),
|
2014-01-02 06:48:08 +04:00
|
|
|
value ? value->name : "", name, &error_abort);
|
2012-12-05 20:49:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd)
|
|
|
|
{
|
|
|
|
qdev_prop_set_macaddr(dev, "mac", nd->macaddr.a);
|
|
|
|
if (nd->netdev) {
|
|
|
|
qdev_prop_set_netdev(dev, "netdev", nd->netdev);
|
|
|
|
}
|
|
|
|
if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
|
|
|
|
object_property_find(OBJECT(dev), "vectors", NULL)) {
|
|
|
|
qdev_prop_set_uint32(dev, "vectors", nd->nvectors);
|
|
|
|
}
|
|
|
|
nd->instantiated = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qdev_add_one_global(QemuOpts *opts, void *opaque)
|
|
|
|
{
|
|
|
|
GlobalProperty *g;
|
|
|
|
|
|
|
|
g = g_malloc0(sizeof(*g));
|
|
|
|
g->driver = qemu_opt_get(opts, "driver");
|
|
|
|
g->property = qemu_opt_get(opts, "property");
|
|
|
|
g->value = qemu_opt_get(opts, "value");
|
2014-08-08 23:03:31 +04:00
|
|
|
g->user_provided = true;
|
2012-12-05 20:49:11 +04:00
|
|
|
qdev_prop_register_global(g);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_add_globals(void)
|
|
|
|
{
|
|
|
|
qemu_opts_foreach(qemu_find_opts("global"), qdev_add_one_global, NULL, 0);
|
|
|
|
}
|