qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Socratic RFC PATCH] include: attempt to document device_class_set_p


From: Alex Bennée
Subject: Re: [Socratic RFC PATCH] include: attempt to document device_class_set_props
Date: Tue, 28 Mar 2023 12:05:28 +0100
User-agent: mu4e 1.10.0; emacs 29.0.60

Peter Maydell <peter.maydell@linaro.org> writes:

> On Mon, 27 Mar 2023 at 23:10, Alex Bennée <alex.bennee@linaro.org> wrote:
>>
>>
>> Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk> writes:
>>
>> > On 27/03/2023 17:12, Alex Bennée wrote:
>> >
>> >> Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk> writes:
>> >>
>> >>> On 27/03/2023 14:15, Alex Bennée wrote:
>> >>>
>> >>>> I'm still not sure how I achieve by use case of the parent class
>> >>>> defining the following properties:
>> >>>>     static Property vud_properties[] = {
>> >>>>         DEFINE_PROP_CHR("chardev", VHostUserDevice, chardev),
>> >>>>         DEFINE_PROP_UINT16("id", VHostUserDevice, id, 0),
>> >>>>         DEFINE_PROP_UINT32("num_vqs", VHostUserDevice, num_vqs, 1),
>> >>>>         DEFINE_PROP_END_OF_LIST(),
>> >>>>     };
>> >>>> But for the specialisation of the class I want the id to default to
>> >>>> the actual device id, e.g.:
>> >>>>     static Property vu_rng_properties[] = {
>> >>>>         DEFINE_PROP_UINT16("id", VHostUserDevice, id, VIRTIO_ID_RNG),
>> >>>>         DEFINE_PROP_UINT32("num_vqs", VHostUserDevice, num_vqs, 1),
>> >>>>         DEFINE_PROP_END_OF_LIST(),
>> >>>>     };
>> >>>> And so far the API for doing that isn't super clear.
>> >>>> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
>> >>>> ---
>> >>>>    include/hw/qdev-core.h | 9 +++++++++
>> >>>>    1 file changed, 9 insertions(+)
>> >>>> diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h
>> >>>> index bd50ad5ee1..d4bbc30c92 100644
>> >>>> --- a/include/hw/qdev-core.h
>> >>>> +++ b/include/hw/qdev-core.h
>> >>>> @@ -776,6 +776,15 @@ BusState *sysbus_get_default(void);
>> >>>>    char *qdev_get_fw_dev_path(DeviceState *dev);
>> >>>>    char *qdev_get_own_fw_dev_path_from_handler(BusState *bus, 
>> >>>> DeviceState *dev);
>> >>>>    +/**
>> >>>> + * device_class_set_props(): add a set of properties to an device
>> >>>> + * @dc: the parent DeviceClass all devices inherit
>> >>>> + * @props: an array of properties, terminate by 
>> >>>> DEFINE_PROP_END_OF_LIST()
>> >>>> + *
>> >>>> + * This will add a set of properties to the object. It will fault if
>> >>>> + * you attempt to add an existing property defined by a parent class.
>> >>>> + * To modify an inherited property you need to use????
>> >>>> + */
>> >>>>    void device_class_set_props(DeviceClass *dc, Property *props);
>> >>>>      /**
>> >>>
>> >>> Hmmm that's an interesting one. Looking at the source in
>> >>> hw/core/qdev-properties.c you could possibly get away with something
>> >>> like this in vu_rng_class_init():
>> >>>
>> >>>      ObjectProperty *op = object_class_property_find(klass, "id");
>> >>>      object_property_set_default_uint(op, VIRTIO_ID_RNG);
>> >>>
>> >>> Of course this is all completely untested :)
>> >> Sadly we assert on the existing prop->defval:
>> >>    static void object_property_set_default(ObjectProperty *prop,
>> >> QObject *defval)
>> >>    {
>> >>        assert(!prop->defval);
>> >>        assert(!prop->init);
>> >>        prop->defval = defval;
>> >>        prop->init = object_property_init_defval;
>> >>    }
>> >> Maybe the assert is too aggressive or we need a different helper,
>> >> maybe
>> >> a:
>> >>    void object_property_update_default_uint(ObjectProperty *prop,
>> >> uint64_t value)
>> >> ?
>> >
>> > It seems in that case once the default has been set, it is impossible
>> > to change. The only other immediate option I can think of is to define
>> > a specific DEFINE_VHOST_PROPERTIES macro in a similar way to
>> > DEFINE_AUDIO_PROPERTIES which you can use to set the common properties
>> > for all VHostUserDevice devices, including providing the default ID.
>>
>> I tried this: allow the default to change
>>
>> modified   qom/object.c
>> @@ -1557,11 +1557,16 @@ static void object_property_init_defval(Object *obj, 
>> ObjectProperty *prop)
>>
>>  static void object_property_set_default(ObjectProperty *prop, QObject 
>> *defval)
>>  {
>> -    assert(!prop->defval);
>> -    assert(!prop->init);
>> +    if (prop->init == object_property_init_defval) {
>> +        fprintf(stderr, "%s: updating existing defval\n", __func__);
>> +        prop->defval = defval;
>> +    } else {
>> +        assert(!prop->defval);
>> +        assert(!prop->init);
>>
>> -    prop->defval = defval;
>> -    prop->init = object_property_init_defval;
>> +        prop->defval = defval;
>> +        prop->init = object_property_init_defval;
>> +    }
>>  }
>
> I think this leaves the door open to bugs where you create
> the property, somebody looks at it, and then you update
> the default value afterwards...

Really the pattern I have is:

  vhost-user-device has the property and is configurable
  vhost-user-rng-device specialises vhost-user-device and fixes the value

I'm not sure how best to represent that. This should all be happening at
class_init time.

-- 
Alex Bennée
Virtualisation Tech Lead @ Linaro



reply via email to

[Prev in Thread] Current Thread [Next in Thread]