Marc-André Lureau <[email protected]> writes:

> Hi
>
> On Fri, Oct 23, 2026 at 2:04 PM Daniel P. Berrangé <[email protected]> 
> wrote:
>>
>> On Tue, Oct 20, 2026 at 02:46:11PM +0400, Marc-André Lureau wrote:
>> > PciDevfn is a bit special, since it accepts integer and string as
>> > setter, but only output integer as getter.

Yes.  See below.

>> > Unfortunately, we can't declare an alternate str|int QAPI type for it,
>> > since: branch 'string' can't be distinguished from 'integer'

Yes, except with trickery; more on this below.

>> >
>> > Rather than declaring it as "str", use the common type instead.
>> >
>> > Signed-off-by: Markus Armbruster <[email protected]>
>> > Signed-off-by: Marc-André Lureau <[email protected]>
>> > ---
>> >  hw/core/qdev-properties-system.c | 3 ++-
>> >  1 file changed, 2 insertions(+), 1 deletion(-)
>> >
>> > diff --git a/hw/core/qdev-properties-system.c 
>> > b/hw/core/qdev-properties-system.c
>> > index 5bfdfe1ab9d9..3381de1144d6 100644
>> > --- a/hw/core/qdev-properties-system.c
>> > +++ b/hw/core/qdev-properties-system.c
>> > @@ -904,7 +904,8 @@ static char *print_pci_devfn(Object *obj, const 
>> > Property *prop)
>> >  }
>> >
>> >  const PropertyInfo qdev_prop_pci_devfn = {
>> > -    .type  = "str",
>> > +    /* The formatted string accepted by the setter is a CLI convenience. 
>> > */
>>
>> IIUC, this is not limited to the CLI, it will work anywhere the JSON
>> syntax is permitted which is both CLI and QMP.
>>
>> libvirt uses this string syntax:
>>
>>   -device 
>> '{"driver":"pcie-root-port","port":9,"chassis":2,"id":"pci.2","bus":"pcie.0","multifunction":true,"addr":"0x1.0x1"}'
>>  \
>>
>> > +    .type  = "int32",
>> >      .description = "Slot and optional function number, example: 06.0 or 
>> > 06",
          .print = print_pci_devfn,
          .get   = qdev_propinfo_get_int32,
          .set   = set_pci_devfn,
          .set_default_value = qdev_propinfo_set_default_value_int,
      };

The property is backed by an int32_t.  In
include/hw/core/qdev-properties-system.h:

    #define DEFINE_PROP_PCI_DEVFN(_n, _s, _f, _d)                   \
        DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_pci_devfn, int32_t)

It's only ever used for PCI device property "addr".

Its .get() simply visits it.  This is why qom-get returns an integer:

    --> 
{"execute":"qom-get","arguments":{"path":"/machine/unattached/device[4]/pm", 
"property": "type"}}
    <-- {"return": "PIIX4_PM"}
    --> 
{"execute":"qom-get","arguments":{"path":"/machine/unattached/device[4]/pm", 
"property": "addr"}}
    <-- {"return": 11}

Its .set() is a handwritten visit of an alternate of 'str' and 'int32',
where 'str' gets parsed as DEV.FN into int32.  This is why QMP
device_add accepts both JSON string and JSON number (HMP device_add and
-device only do string).  QAPI doesn't let us define such an alternate
type, as you wrote in your commit message, but handwritten visits fly
below that radar.  This is of course too clever by half.  Goes back to
commit 768a9ebe188 (qdev: accept both strings and integers for PCI
addresses) in v1.1.

Its .print() prints it like DEV.FN.  Its only user is "info qtree".

>> Changing the type to 'int32', where 99% of usage is input only
>> and string based, is pretty unpleasant. Even more so that the
>> description doesn't even mention encoding dev+function as an
>> integer.
>
> This is purely a metadata change, the setter still accepts both str &
> int,

Yes.

.type is only used as @type argument for object_class_property_add() and
object_property_add().  They store it in ObjectProperty member @type.
Visible externally in QOM introspection (ObjectPropertyInfo and
ObjectPropertyValue member type).  Also in help, but let's ignore that
for a minute.

Before the series, QOM instrospection shows type "str" for PCI device
property "addr".

For reading, that's a lie: qom-get and qom-list-get return an JSON
integer, not a JSON string.

For writing, it's merely incomplete: qom-set accepts a JSON string in
addition to a JSON integer.

Does it matter?  To make use of QOM introspection's type, you have to
hardcode so much silly stuff, what's one more case?

With this patch, it shows type "int32".  Less wrong.

Again, does it matter?  The change could conceivably break a client.  I
figure the risk is small.  Is "less wrong" worth even a small risk?

>      so I don't think this change blocks this series.

I figure it's no worse than the other type values the series changes.

Let's discuss them under my

    Subject: Property types changed in this series (was: [PATCH v7 00/65] Hi)
    Date: Wed, 21 Oct 2026 18:29:40 +0200
    Message-ID: <[email protected]>

>                                                       But I agree we
> could do better.
>
> There are multiple options. We could keep declaring the property as
> str instead. In this case, the getter should probably return a string
> for consistency and correctness. This might surprise some existing
> users though.

I.e. ditch numeric suppport for numeric addresses, right?

Incompatible change.  Do we care?

If we don't, we should be honest about it and mark qom-get unstable.

> We could introduce new properties like `devfn-str` and `devfn-int`
> that alias the same value.

Compatibility break.  If breaking compatibility is okay, I'd rather use
the opportunity to get rid of cleverness, and go with your first option.

> We could also introduce a new structured QAPI type.

What kind of type do you have in mind?

> Markus, what's your take?

>From a QAPI/QMP point of view, the clean way to support "either string
or numeric address" is an alternate type.

Trouble is the generator rejects such an alternate, as you pointed out
in your commit message.  Why is that?

docs/devel/qapi-code-gen.rst:

    An alternate type is like a union type, except there is no
    discriminator on the wire.  Instead, the branch to use is inferred
    from the value.  An alternate can only express a choice between types
    represented differently on the wire.

So, we can't have two 'str' branches (both JSON string), or a 'str' and
an 'enum' branch (same).

But 'int32' and 'str' are different on the wire (JSON number and JSON
string), so what's going on?  This:

commit c0644771ebedbd8f47f3c24816445e30111d226b (tag: pull-qapi-2017-05-31)
Author: Markus Armbruster <[email protected]>
Date:   Mon May 22 18:42:15 2017 +0200

    qapi: Reject alternates that can't work with keyval_parse()
    
    Alternates are sum types like unions, but use the JSON type on the
    wire / QType in QObject instead of an explicit tag.  That's why we
    require alternate members to have distinct QTypes.
    
    The recently introduced keyval_parse() (commit d454dbe) can only
    produce string scalars.  The qobject_input_visitor_new_keyval() input
    visitor mostly hides the difference, so code using a QObject input
    visitor doesn't have to care whether its input was parsed from JSON or
    KEY=VALUE,...  The difference leaks for alternates, as noted in commit
    0ee9ae7: a non-string, non-enum scalar alternate value can't currently
    be expressed.
    
    In part, this is just our insufficiently sophisticated implementation.
    Consider alternate type 'GuestFileWhence'.  It has an integer member
    and a 'QGASeek' member.  The latter is an enumeration with values
    'set', 'cur', 'end'.  The meaning of b=set, b=cur, b=end, b=0, b=1 and
    so forth is perfectly obvious.  However, our current implementation
    falls apart at run time for b=0, b=1, and so forth.  Fixable, but not
    today; add a test case and a TODO comment.
    
    Now consider an alternate type with a string and an integer member.
    What's the meaning of a=42?  Is it the string "42" or the integer 42?
    Whichever meaning you pick makes the other inexpressible.  This isn't
    just an implementation problem, it's fundamental.  Our current
    implementation will pick string.
    
    So far, we haven't needed such alternates.  To make sure we stop and
    think before we add one that cannot sanely work with keyval_parse(),
    let's require alternate members to have sufficiently distinct
    representation in KEY=VALUE,... syntax:
    
    * A string member clashes with any other scalar member
    
    * An enumeration member clashes with bool members when it has value
      'on' or 'off'.
    
    * An enumeration member clashes with numeric members when it has a
      value that starts with '-', '+', or a decimal digit.  This is a
      rather lazy approximation of the actual number syntax accepted by
      the visitor.
    
      Note that enumeration values starting with '-' and '+' are rejected
      elsewhere already, but better safe than sorry.
    
    Signed-off-by: Markus Armbruster <[email protected]>
    Message-Id: <[email protected]>
    Reviewed-by: Eric Blake <[email protected]>
    Reviewed-by: Marc-André Lureau <[email protected]>

However, keyval_parse() + its input visitor can't express everything
QAPI even with such alternates outlawed.  From util/keyval.c:

 * Design flaw: there is no way to denote an empty array or non-root
 * object.  While interpreting "key absent" as empty seems natural
 * (removing a key-val from the input string removes the member when
 * there are more, so why not when it's the last), it doesn't work:
 * "key absent" already means "optional object/array absent", which
 * isn't the same as "empty object/array present".
 *
 * Design flaw: scalar values can only be strings; there is no way to
 * denote numbers, true, false or null.  The special QObject input
 * visitor returned by qobject_input_visitor_new_keyval() mostly hides
 * this by automatically converting strings to the type the visitor
 * expects.  Breaks down for type 'any', where the visitor's
 * expectation isn't clear.  Code visiting 'any' needs to do the
 * conversion itself, but only when using this keyval visitor.
 * Awkward.  Note that we carefully restrict alternate types to avoid
 * similar ambiguity.

We commonly use keyval syntax together with JSON: if the input starts
with '{', it's assumed to be JSON, else keyval.  See
qobject_input_visitor_new_str().  This lets you fall back to JSON when
keyval can't express what you want.

Beware, there *are* uses of keyval without a JSON fallback.

As the commit message I quoted says, we could permit alternates that are
ambiguous with keyval.  The keyval visitor could then pick a branch (you
can't express the others), or it could refuse to pick (you can't express
any of them).

In the particular case of PCI device property "addr", picking str would
be fine: same behavior as the handwritten visit in in set_pci_devfn().

But once we permit such alternates, nothing but conscientious testing
with keyval syntax will stop us from adding interfaces that don't work
or work the wrong way.  Hmm.

I need to think about this some more if we want to explore this
direction.

[...]


Reply via email to