block: Try to create well typed json:{} filenames
v2 cover block: Try to create well typed json:{} filenames
See cover letter of v1 here:
http://lists.nongnu.org/archive/html/qemu-block/2018-05/msg00290.html
This series depends on Markus's series "block: Configuration fixes and rbd authentication":
Based-on: <[email protected]>
Changes in v2:
- Rebased on top of Markus's series, which allowed my to drop qdict_stringify_for_keyval(), and the patch moving block-specific QDict functions into an own header
- %s/2.13/3.0/g
- Patch 1: [Eric]
- s/must/will/
- Set the discriminator field to its default value when not given, which simplifies the code both here and hopefully in the users, too
- Patch 3: [Eric]
- Rename existing optional discriminator test and keep it as an error test, instead of making it pass
- Add passing test to qapi-schema-test
- Patch 4: [Daniel]
- %s/from-image/auto/g
- Actually handle that format when specified explicitly
- Patch 5: [Eric/Daniel]
- Reduced complexity by just defaulting to 'aes'
- Patch 6: Rewritten because of the rebase
- Patch 7: Rebase conflicts
- Patch 8:
- Use qobject_input_visitor_new_flat_confused() from Markus's series
- Some rebase conflicts in test outputs
- Also, 198 now explicitly emits '"format": "auto"' due to visitors now setting the discriminator field to the default value, thanks to the change to patch 1 (working as intended, I suppose)
- Patch 10: Added a couple of qcow2+LUKS blockdev-add test cases for various values of encrypt.format
git-backport-diff against v1:
Key:
[----] : patches are identical
[####] : number of functional differences between upstream/downstream patch [down] : patch is downstream-only
The flags [FC] indicate (F)unctional and (C)ontextual differences, respectively
001/10:[0032] [FC] 'qapi: Add default-variant for flat unions' 002/10:[----] [--] 'docs/qapi: Document optional discriminators' 003/10:[0048] [FC] 'tests: Add QAPI optional discriminator tests' 004/10:[0025] [FC] 'qapi: Formalize qcow2 encryption probing' 005/10:[0027] [FC] 'qapi: Formalize qcow encryption probing' 006/10:[0042] [FC] 'qdict: Make qdict_flatten() shallow-clone-friendly' 007/10:[0002] [FC] 'tests: Add QDict clone-flatten test' 008/10:[0035] [FC] 'block: Try to create well typed json:{} filenames' 009/10:[----] [--] 'iotests: Test internal option typing' 010/10:[0089] [FC] 'iotests: qcow2's encrypt.format is now optional'
Max Reitz (10):
qapi: Add default-variant for flat unions docs/qapi: Document optional discriminators
tests: Add QAPI optional discriminator tests
qapi: Formalize qcow2 encryption probing
qapi: Formalize qcow encryption probing
qdict: Make qdict_flatten() shallow-clone-friendly
tests: Add QDict clone-flatten test
block: Try to create well typed json:{} filenames
iotests: Test internal option typing
iotests: qcow2's encrypt.format is now optional
docs/devel/qapi-code-gen.txt | 21 +++++-
tests/Makefile.include | 5 +-
qapi/block-core.json | 47 +++++++++++--
qapi/introspect.json | 8 +++
...ptional-discriminator-invalid-default.json | 12 ++++
...l-discriminator-invalid-specification.json | 12 ++++
...on-optional-discriminator-no-default.json} | 5 +-
...lat-union-superfluous-default-variant.json | 11 ++++
tests/qapi-schema/qapi-schema-test.json | 9 +++
block.c | 66 ++++++++++++++++++-
block/qcow2.c | 3 +
qobject/block-qdict.c | 19 ++++--
tests/check-block-qdict.c | 33 ++++++++++
scripts/qapi/common.py | 57 +++++++++++++---
scripts/qapi/doc.py | 8 ++-
scripts/qapi/introspect.py | 10 ++-
scripts/qapi/visit.py | 13 ++++
...optional-discriminator-invalid-default.err | 1 +
...tional-discriminator-invalid-default.exit} | 0
...ptional-discriminator-invalid-default.out} | 0
...al-discriminator-invalid-specification.err | 1 +
...l-discriminator-invalid-specification.exit | 1 +
...al-discriminator-invalid-specification.out | 0
...nion-optional-discriminator-no-default.err | 1 +
...ion-optional-discriminator-no-default.exit | 1 +
...nion-optional-discriminator-no-default.out | 0
.../flat-union-optional-discriminator.err | 1 -
...flat-union-superfluous-default-variant.err | 1 +
...lat-union-superfluous-default-variant.exit | 1 +
...flat-union-superfluous-default-variant.out | 0
tests/qapi-schema/qapi-schema-test.out | 9 +++
tests/qapi-schema/test-qapi.py | 2 +
tests/qemu-iotests/059.out | 2 +-
tests/qemu-iotests/087 | 65 ++++++++++--------
tests/qemu-iotests/087.out | 26 +++++++-
tests/qemu-iotests/089 | 25 +++++++
tests/qemu-iotests/089.out | 9 +++
tests/qemu-iotests/099.out | 4 +-
tests/qemu-iotests/110.out | 2 +-
tests/qemu-iotests/198.out | 4 +-
tests/qemu-iotests/207.out | 10 +--
41 files changed, 436 insertions(+), 69 deletions(-)
create mode 100644 tests/qapi-schema/flat-union-optional-discriminator-invalid-default.json
create mode 100644 tests/qapi-schema/flat-union-optional-discriminator-invalid-specification.json
rename tests/qapi-schema/{flat-union-optional-discriminator.json => flat-union-optional-discriminator-no-default.json} (70%)
create mode 100644 tests/qapi-schema/flat-union-superfluous-default-variant.json
create mode 100644 tests/qapi-schema/flat-union-optional-discriminator-invalid-default.err
rename tests/qapi-schema/{flat-union-optional-discriminator.exit => flat-union-optional-discriminator-invalid-default.exit} (100%)
rename tests/qapi-schema/{flat-union-optional-discriminator.out => flat-union-optional-discriminator-invalid-default.out} (100%)
create mode 100644 tests/qapi-schema/flat-union-optional-discriminator-invalid-specification.err
create mode 100644 tests/qapi-schema/flat-union-optional-discriminator-invalid-specification.exit
create mode 100644 tests/qapi-schema/flat-union-optional-discriminator-invalid-specification.out
create mode 100644 tests/qapi-schema/flat-union-optional-discriminator-no-default.err
create mode 100644 tests/qapi-schema/flat-union-optional-discriminator-no-default.exit
create mode 100644 tests/qapi-schema/flat-union-optional-discriminator-no-default.out
delete mode 100644 tests/qapi-schema/flat-union-optional-discriminator.err
create mode 100644 tests/qapi-schema/flat-union-superfluous-default-variant.err
create mode 100644 tests/qapi-schema/flat-union-superfluous-default-variant.exit
create mode 100644 tests/qapi-schema/flat-union-superfluous-default-variant.outSignature · 1 line
2.17.1
PATCH v2 1/10 qapi: Add default-variant for flat unions
This patch allows specifying a discriminator that is an optional member of the base struct. In such a case, a default value must be provided that is used when no value is given.
--- qapi/introspect.json | 8 +++++ scripts/qapi/common.py | 57 ++++++++++++++++++++++++++++------ scripts/qapi/doc.py | 8 +++-- scripts/qapi/introspect.py | 10 ++++-- scripts/qapi/visit.py | 13 ++++++++ tests/qapi-schema/test-qapi.py | 2 ++ 6 files changed, 83 insertions(+), 15 deletions(-)
qapi/introspect.json +8 -0
@@ -168,6 +168,13 @@ # @tag: the name of the member serving as type tag. # An element of @members with this name must exist. # +# @default-variant: if the @tag element of @members is optional, this +# is the default value for choosing a variant. Its +# value will be a valid value for @tag. +# Present exactly when @tag is present and the +# associated element of @members is optional. +# (Since: 3.0) +# # @variants: variant members, i.e. additional members that # depend on the type tag's value. Present exactly when # @tag is present. The variants are in no particular order,
@@ -181,6 +188,7 @@ { 'struct': 'SchemaInfoObject', 'data': { 'members': [ 'SchemaInfoObjectMember' ], '*tag': 'str', + '*default-variant': 'str', '*variants': [ 'SchemaInfoObjectVariant' ] } } ##
scripts/qapi/common.py +47 -10
118 lines not on this page · raw message
scripts/qapi/doc.py +6 -2
@@ -160,8 +160,12 @@ def texi_members(doc, what, base, variants, member_func): items += '@item The members of @code{%s}\n' % base.doc_type() if variants: for v in variants.variants: - when = ' when @code{%s} is @t{"%s"}' % ( - variants.tag_member.name, v.name) + if v.name == variants.default_tag_value: + when = ' when @code{%s} is @t{"%s"} or not given' % ( + variants.tag_member.name, v.name) + else: + when = ' when @code{%s} is @t{"%s"}' % ( + variants.tag_member.name, v.name) if v.type.is_implicit(): assert not v.type.base and not v.type.variants for m in v.type.local_members:
scripts/qapi/introspect.py +7 -3
@@ -142,9 +142,12 @@ const QLitObject %(c_name)s = %(c_string)s; ret['default'] = None return ret - def _gen_variants(self, tag_name, variants): - return {'tag': tag_name, - 'variants': [self._gen_variant(v) for v in variants]} + def _gen_variants(self, tag_name, default_variant, variants): + ret = {'tag': tag_name, + 'variants': [self._gen_variant(v) for v in variants]} + if default_variant: + ret['default-variant'] = default_variant + return ret def _gen_variant(self, variant): return {'case': variant.name, 'type': self._use_type(variant.type)}
@@ -163,6 +166,7 @@ const QLitObject %(c_name)s = %(c_string)s; obj = {'members': [self._gen_member(m) for m in members]} if variants: obj.update(self._gen_variants(variants.tag_member.name, + variants.default_tag_value, variants.variants)) self._gen_qlit(name, 'object', obj)
scripts/qapi/visit.py +13 -0
@@ -75,6 +75,19 @@ void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp) ''') if variants: + if variants.default_tag_value is not None: + ret += mcgen(''' + if (!obj->has_%(c_name)s) { + obj->has_%(c_name)s = true; + obj->%(c_name)s = %(enum_const)s; + } +''', + c_name=c_name(variants.tag_member.name), + enum_const=c_enum_const( + variants.tag_member.type.name, + variants.default_tag_value, + variants.tag_member.type.prefix)) + ret += mcgen(''' switch (obj->%(c_name)s) { ''',
tests/qapi-schema/test-qapi.py +2 -0
@@ -56,6 +56,8 @@ class QAPISchemaTestVisitor(QAPISchemaVisitor): def _print_variants(variants): if variants: print(' tag %s' % variants.tag_member.name) + if variants.default_tag_value: + print(' default variant: %s' % variants.default_tag_value) for v in variants.variants: print(' case %s: %s' % (v.name, v.type.name))
Signature · 1 line
2.17.1
Re: [Qemu-devel] [PATCH v2 01/10] qapi: Add default-variant for flat unions
Quoted 3 lines · Max Reitz
This patch allows specifying a discriminator that is an optional member of the base struct. In such a case, a default value must be provided that is used when no value is given.
Hmm. Can you explain why you need this feature?
Quoted 9 lines
Signed-off-by Max Reitz <[email protected]>--- qapi/introspect.json | 8 +++++ scripts/qapi/common.py | 57 ++++++++++++++++++++++++++++------ scripts/qapi/doc.py | 8 +++-- scripts/qapi/introspect.py | 10 ++++-- scripts/qapi/visit.py | 13 ++++++++ tests/qapi-schema/test-qapi.py | 2 ++ 6 files changed, 83 insertions(+), 15 deletions(-)
The documentation update is in the next patch, and tests are in the patch after next. I'd squash all three.
Quoted 20 lines
qapi/introspect.json +7
-0@@ -168,6 +168,13 @@ # @tag: the name of the member serving as type tag. # An element of @members with this name must exist. # +# @default-variant: if the @tag element of @members is optional, this +# is the default value for choosing a variant. Its +# value will be a valid value for @tag. +# Present exactly when @tag is present and the +# associated element of @members is optional. +# (Since: 3.0) +# # @variants: variant members, i.e. additional members that # depend on the type tag's value. Present exactly when # @tag is present. The variants are in no particular order,
qapi/introspect.json line 194
Quoted 7 lines
qapi/introspect.json +1
-0@@ -181,6 +188,7 @@ { 'struct': 'SchemaInfoObject', 'data': { 'members': [ 'SchemaInfoObjectMember' ], '*tag': 'str', + '*default-variant': 'str', '*variants': [ 'SchemaInfoObjectVariant' ] } } ##
Until now, the QAPI schema doesn't let you specify default values. Instead, code sees "absent", and does whatever needs to be done. Sometimes, it supplies a default value. "Absent" is shorthand for this value then. Sometimes, it behaves differently than for any value. "Absent" is a distinct additional case then. I'm not too fond of the latter case, but it's what we have, and it's not going away.
We've discussed extending the QAPI schema to let you specify default values. Two advantages: the default value is *specified* rather than just documented (or not, when documentation sucks), and we can supply the default value in generated code. The introspection schema is even prepared for this:
##
# @SchemaInfoObjectMember:
#
# An object member.
#
# @name: the member's name, as defined in the QAPI schema.
#
# @type: the name of the member's type.
#
--> # @default: default when used as command parameter.
# If absent, the parameter is mandatory.
# If present, the value must be null. The parameter is
# optional, and behavior when it's missing is not specified
# here.
# Future extension: if present and non-null, the parameter
# is optional, and defaults to this value.
#
# Since: 2.5
##The idea was to turn
'*name': 'type'
into sugar for
'name': { 'type': 'type', 'optional': true }then extend it to
'name': { 'type': 'type', 'optional': true, 'default': JSON-VALUE }where JSON-VALUE null means behavior for "absent" is not specified.
Your patch adds default values in a different way, and for just for union tags. For the QAPI language, that's not necessarily a deal breaker, as we don't need to maintain backward compatibility there. But for the introspection schema, we do. Once we add @default-variant, we're stuck with it. Even though it's redundant with SchemaInfoObjectMember's @default.
I'm skipping review of the implementation for now.
Re: [Qemu-devel] [PATCH v2 01/10] qapi: Add default-variant for flat unions
Quoted 7 lines · Markus Armbruster
Quoted 3 lines · Max Reitz
This patch allows specifying a discriminator that is an optional member of the base struct. In such a case, a default value must be provided that is used when no value is given.
Hmm. Can you explain why you need this feature?
Define "need".
Most block drivers support exactly the same syntax with -drive and with -blockdev. Type confusion occurs, but at least the options are the same.
One notable example is rbd's =keyvalue-pairs, but we all probably agree on the fact that it's just broken and we don't have to care too much.
The other case I noticed (and so far the only other case I know of besides =keyvalue-pairs) is qcow(2) encryption. The qcow(2) driver allows you to specify different parameters under @encrypt depending on the encryption format, so that naturally becomes a flat union with encrypt.format being the discriminator.
Now when opening a qcow(2) file, the header already gives you the encryption format (always qcow AES for qcow v1, qcow AES or LUKS for qcow2), so the user technically doesn't need to specify it. And that is what the qcow(2) driver allows on the command line: The user does not need to specify the format as it can be inferred from the header, and the union is interpreted accordingly. Of course, that doesn't fly with -blockdev.
Now luckily both AES and LUKS only allow you to specify a key-secret option. Therefore, with an optional discriminator, we can define a default variant that just has this key-secret option. This is what this patch is needed for. It allows full compatibility to the current interface, although it means we'll have to watch out when adding encryption options in the future.
The alternative I saw was to decide that auto-detection was a bad idea and to make encrypt.format mandatory for -drive. Maybe I would have taken that approach had I known how little I know about the QAPI generator at this point. But after I'd started, it was a challenge, so I continued down that other path. Anyway, the problem with this approach would be obviously compatibility breakage, and also being a bit cumbersome. ("Why would you need to specify encrypt.format when it's all in the image header anyway?")
Short answer: The current QAPI schema for qcow2 is incompatible to what it allows with -drive, this patch allows making it compatible -- and we need both to be compatible if we ever want BlockdevOptions throughout the block layer.
Quoted 10 lines
Quoted 9 lines
Signed-off-by Max Reitz <[email protected]>--- qapi/introspect.json | 8 +++++ scripts/qapi/common.py | 57 ++++++++++++++++++++++++++++------ scripts/qapi/doc.py | 8 +++-- scripts/qapi/introspect.py | 10 ++++-- scripts/qapi/visit.py | 13 ++++++++ tests/qapi-schema/test-qapi.py | 2 ++ 6 files changed, 83 insertions(+), 15 deletions(-)
Quoted 2 lines
The documentation update is in the next patch, and tests are in the patch after next. I'd squash all three.
I don't mind.
(I just like to split patches in advance, because squashing is always easy.)
Quoted 25 lines
Quoted 25 lines
qapi/introspect.json +8
-0@@ -168,6 +168,13 @@ # @tag: the name of the member serving as type tag. # An element of @members with this name must exist. # +# @default-variant: if the @tag element of @members is optional, this +# is the default value for choosing a variant. Its +# value will be a valid value for @tag. +# Present exactly when @tag is present and the +# associated element of @members is optional. +# (Since: 3.0) +# # @variants: variant members, i.e. additional members that # depend on the type tag's value. Present exactly when # @tag is present. The variants are in no particular order,@@ -181,6 +188,7 @@ { 'struct': 'SchemaInfoObject', 'data': { 'members': [ 'SchemaInfoObjectMember' ], '*tag': 'str', + '*default-variant': 'str', '*variants': [ 'SchemaInfoObjectVariant' ] } }
Quoted 8 lines
qapi/introspect.json line 194
Quoted 1 line
qapi/introspect.json +0
-0##Until now, the QAPI schema doesn't let you specify default values. Instead, code sees "absent", and does whatever needs to be done. Sometimes, it supplies a default value. "Absent" is shorthand for this value then. Sometimes, it behaves differently than for any value. "Absent" is a distinct additional case then. I'm not too fond of the latter case, but it's what we have, and it's not going away.
Just a note: In v1 of this series, it was possible to detect when the variant was indeed absent. Eric proposed just setting the member to the default value when absent (because he too is not too fond of that case), so here in v2 there is no difference between setting the value to its default value or not specifying it.
Quoted 32 lines
We've discussed extending the QAPI schema to let you specify default values. Two advantages: the default value is *specified* rather than just documented (or not, when documentation sucks), and we can supply the default value in generated code. The introspection schema is even prepared for this:
## # @SchemaInfoObjectMember: # # An object member. # # @name: the member's name, as defined in the QAPI schema. # # @type: the name of the member's type. # --> # @default: default when used as command parameter. # If absent, the parameter is mandatory. # If present, the value must be null. The parameter is # optional, and behavior when it's missing is not specified # here. # Future extension: if present and non-null, the parameter # is optional, and defaults to this value. # # Since: 2.5 ##The idea was to turn
'*name': 'type'into sugar for
Quoted 7 lines
'name': { 'type': 'type', 'optional': true }then extend it to
'name': { 'type': 'type', 'optional': true, 'default': JSON-VALUE }where JSON-VALUE null means behavior for "absent" is not specified.
Seems reasonable.
Quoted 6 lines
Your patch adds default values in a different way, and for just for union tags. For the QAPI language, that's not necessarily a deal breaker, as we don't need to maintain backward compatibility there. But for the introspection schema, we do. Once we add @default-variant, we're stuck with it. Even though it's redundant with SchemaInfoObjectMember's @default.
I too would like a general solution better. Of course the question is how far off such a general solution is. It's not like I consider this series extremely urgent, but I do have a BZ assigned for it, so take that as you will. O:-)
So between the lines I'd like to think I can read that it would be "fine" to add default-variant for now, but to omit it from introspection?
Max
Quoted 1 line
I'm skipping review of the implementation for now
PATCH v2 2/10 docs/qapi: Document optional discriminators
In reply to Max Reitz--- docs/devel/qapi-code-gen.txt | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-)
docs/devel/qapi-code-gen.txt +19 -2
@@ -472,8 +472,8 @@ All branches of the union must be complex types, and the top-level members of the union dictionary on the wire will be combination of members from both the base type and the appropriate branch type (when merging two dictionaries, there must be no keys in common). The -'discriminator' member must be the name of a non-optional enum-typed -member of the base struct. +'discriminator' member must be the name of an enum-typed member of the +base struct. The following example enhances the above simple union example by adding an optional common member 'read-only', renaming the
@@ -502,6 +502,23 @@ the enum). In the resulting generated C data types, a flat union is represented as a struct with the base members included directly, and then a union of structures for each branch of the struct. +If the discriminator points to an optional member of the base struct, +its default value must be specified as a 'default-variant'. In the +following example, the above BlockDriver struct is changed so it +defaults to the 'file' driver if that field is omitted on the wire: + + { 'union': 'BlockdevOptions', + 'base': { '*driver': 'BlockdevDriver', '*read-only': 'bool' }, + 'discriminator': 'driver', + 'default-variant': 'file', + 'data': { 'file': 'BlockdevOptionsFile', + 'qcow2': 'BlockdevOptionsQcow2' } } + +Now the 'file' JSON object can be abbreviated to: + + { "read-only": "true", + "filename": "/some/place/my-image" } + A simple union can always be re-written as a flat union where the base class has a single member named 'type', and where each branch of the union has a struct with a single member named 'data'. That is,
Signature · 1 line
2.17.1
PATCH v2 3/10 tests: Add QAPI optional discriminator tests
In reply to Max ReitzThere already is an optional discriminator test, although it also noted the discriminator name itself as optional. This already gives us one error test case, to which this patch adds various others.
Furthermore, a passing test case is added to qapi-schema-test.
---
tests/Makefile.include | 5 ++++-
...union-optional-discriminator-invalid-default.json | 12 ++++++++++++
...optional-discriminator-invalid-specification.json | 12 ++++++++++++
...lat-union-optional-discriminator-no-default.json} | 5 +++--
.../flat-union-superfluous-default-variant.json | 11 +++++++++++
tests/qapi-schema/qapi-schema-test.json | 9 +++++++++
...-union-optional-discriminator-invalid-default.err | 1 +
...nion-optional-discriminator-invalid-default.exit} | 0
...union-optional-discriminator-invalid-default.out} | 0
...-optional-discriminator-invalid-specification.err | 1 +
...optional-discriminator-invalid-specification.exit | 1 +
...-optional-discriminator-invalid-specification.out | 0
.../flat-union-optional-discriminator-no-default.err | 1 +
...flat-union-optional-discriminator-no-default.exit | 1 +
.../flat-union-optional-discriminator-no-default.out | 0
.../flat-union-optional-discriminator.err | 1 -
.../flat-union-superfluous-default-variant.err | 1 +
.../flat-union-superfluous-default-variant.exit | 1 +
.../flat-union-superfluous-default-variant.out | 0
tests/qapi-schema/qapi-schema-test.out | 9 +++++++++
20 files changed, 67 insertions(+), 4 deletions(-)
create mode 100644 tests/qapi-schema/flat-union-optional-discriminator-invalid-default.json
create mode 100644 tests/qapi-schema/flat-union-optional-discriminator-invalid-specification.json
rename tests/qapi-schema/{flat-union-optional-discriminator.json => flat-union-optional-discriminator-no-default.json} (70%)
create mode 100644 tests/qapi-schema/flat-union-superfluous-default-variant.json
create mode 100644 tests/qapi-schema/flat-union-optional-discriminator-invalid-default.err
rename tests/qapi-schema/{flat-union-optional-discriminator.exit => flat-union-optional-discriminator-invalid-default.exit} (100%)
rename tests/qapi-schema/{flat-union-optional-discriminator.out => flat-union-optional-discriminator-invalid-default.out} (100%)
create mode 100644 tests/qapi-schema/flat-union-optional-discriminator-invalid-specification.err
create mode 100644 tests/qapi-schema/flat-union-optional-discriminator-invalid-specification.exit
create mode 100644 tests/qapi-schema/flat-union-optional-discriminator-invalid-specification.out
create mode 100644 tests/qapi-schema/flat-union-optional-discriminator-no-default.err
create mode 100644 tests/qapi-schema/flat-union-optional-discriminator-no-default.exit
create mode 100644 tests/qapi-schema/flat-union-optional-discriminator-no-default.out
delete mode 100644 tests/qapi-schema/flat-union-optional-discriminator.err
create mode 100644 tests/qapi-schema/flat-union-superfluous-default-variant.err
create mode 100644 tests/qapi-schema/flat-union-superfluous-default-variant.exit
create mode 100644 tests/qapi-schema/flat-union-superfluous-default-variant.outtests/Makefile.include +4 -1
@@ -505,8 +505,11 @@ qapi-schema += flat-union-int-branch.json qapi-schema += flat-union-invalid-branch-key.json qapi-schema += flat-union-invalid-discriminator.json qapi-schema += flat-union-no-base.json -qapi-schema += flat-union-optional-discriminator.json +qapi-schema += flat-union-optional-discriminator-invalid-specification.json +qapi-schema += flat-union-optional-discriminator-no-default.json +qapi-schema += flat-union-optional-discriminator-invalid-default.json qapi-schema += flat-union-string-discriminator.json +qapi-schema += flat-union-superfluous-default-variant.json qapi-schema += funny-char.json qapi-schema += ident-with-escape.json qapi-schema += include-before-err.json
tests/qapi-schema/flat-union-optional-discriminator-invalid-default.json new file +12 -0
@@ -0,0 +1,12 @@ +# default-variant must refer to an actual value of the discriminator's +# enum +{ 'enum': 'Enum', 'data': [ 'one', 'two' ] } +{ 'struct': 'Base', + 'data': { '*switch': 'Enum' } } +{ 'struct': 'Branch', 'data': { 'name': 'str' } } +{ 'union': 'MyUnion', + 'base': 'Base', + 'discriminator': 'switch', + 'default-variant': 'three', + 'data': { 'one': 'Branch', + 'two': 'Branch' } }
tests/qapi-schema/flat-union-optional-discriminator-invalid-specification.json new file +12 -0
@@ -0,0 +1,12 @@ +# For using optional discriminators, only the field in the base struct +# must be marked optional, not the discriminator name itself +{ 'enum': 'Enum', 'data': [ 'one', 'two' ] } +{ 'struct': 'Base', + 'data': { '*switch': 'Enum' } } +{ 'struct': 'Branch', 'data': { 'name': 'str' } } +{ 'union': 'MyUnion', + 'base': 'Base', + 'discriminator': '*switch', + 'default-variant': 'one', + 'data': { 'one': 'Branch', + 'two': 'Branch' } }
tests/qapi-schema/flat-union-optional-discriminator-no-default.json renamed from tests/qapi-schema/flat-union-optional-discriminator.json +3 -2
@@ -1,10 +1,11 @@ -# we require the discriminator to be non-optional +# Using an optional discriminator requires specifying a default +# variant { 'enum': 'Enum', 'data': [ 'one', 'two' ] } { 'struct': 'Base', 'data': { '*switch': 'Enum' } } { 'struct': 'Branch', 'data': { 'name': 'str' } } { 'union': 'MyUnion', 'base': 'Base', - 'discriminator': '*switch', + 'discriminator': 'switch', 'data': { 'one': 'Branch', 'two': 'Branch' } }
tests/qapi-schema/flat-union-superfluous-default-variant.json new file +11 -0
@@ -0,0 +1,11 @@ +# default-variant only makes sense with an optional discriminator +{ 'enum': 'Enum', 'data': [ 'one', 'two' ] } +{ 'struct': 'Base', + 'data': { 'switch': 'Enum' } } +{ 'struct': 'Branch', 'data': { 'name': 'str' } } +{ 'union': 'MyUnion', + 'base': 'Base', + 'discriminator': 'switch', + 'default-variant': 'one', + 'data': { 'one': 'Branch', + 'two': 'Branch' } }
tests/qapi-schema/qapi-schema-test.json +9 -0
@@ -99,6 +99,15 @@ { 'struct': 'UserDefC', 'data': { 'string1': 'str', 'string2': 'str' } } +# for testing unions with an optional discriminator +{ 'union': 'UserDefFlatUnion3', + 'base': { '*enum1': 'EnumOne' }, + 'discriminator': 'enum1', + 'default-variant': 'value1', + 'data': { 'value1' : 'UserDefA', + 'value2' : 'UserDefB', + 'value3' : 'UserDefB' } } + # for testing use of 'number' within alternates { 'alternate': 'AltEnumBool', 'data': { 'e': 'EnumOne', 'b': 'bool' } } { 'alternate': 'AltEnumNum', 'data': { 'e': 'EnumOne', 'n': 'number' } }
tests/qapi-schema/flat-union-optional-discriminator-invalid-default.err new file +1 -0
@@ -0,0 +1 @@ +tests/qapi-schema/flat-union-optional-discriminator-invalid-default.json:7: Default variant 'three' of flat union 'MyUnion' is not part of 'Enum'
tests/qapi-schema/flat-union-optional-discriminator-invalid-default.exit renamed from tests/qapi-schema/flat-union-optional-discriminator.exit +0 -0
tests/qapi-schema/flat-union-optional-discriminator-invalid-default.out renamed from tests/qapi-schema/flat-union-optional-discriminator.out +0 -0
tests/qapi-schema/flat-union-optional-discriminator-invalid-specification.err new file +1 -0
@@ -0,0 +1 @@ +tests/qapi-schema/flat-union-optional-discriminator-invalid-specification.json:7: Discriminator of flat union 'MyUnion' does not allow optional name '*switch'
tests/qapi-schema/flat-union-optional-discriminator-invalid-specification.exit new file +1 -0
@@ -0,0 +1 @@ +1
tests/qapi-schema/flat-union-optional-discriminator-invalid-specification.out new file +0 -0
tests/qapi-schema/flat-union-optional-discriminator-no-default.err new file +1 -0
@@ -0,0 +1 @@ +tests/qapi-schema/flat-union-optional-discriminator-no-default.json:7: Default variant must be specified for optional discriminator 'switch'
tests/qapi-schema/flat-union-optional-discriminator-no-default.exit new file +1 -0
@@ -0,0 +1 @@ +1
tests/qapi-schema/flat-union-optional-discriminator-no-default.out new file +0 -0
tests/qapi-schema/flat-union-optional-discriminator.err deleted +0 -1
@@ -1 +0,0 @@ -tests/qapi-schema/flat-union-optional-discriminator.json:6: Discriminator of flat union 'MyUnion' does not allow optional name '*switch'
tests/qapi-schema/flat-union-superfluous-default-variant.err new file +1 -0
@@ -0,0 +1 @@ +tests/qapi-schema/flat-union-superfluous-default-variant.json:6: Must not specify a default variant for non-optional discriminator 'switch'
tests/qapi-schema/flat-union-superfluous-default-variant.exit new file +1 -0
@@ -0,0 +1 @@ +1
tests/qapi-schema/flat-union-superfluous-default-variant.out new file +0 -0
tests/qapi-schema/qapi-schema-test.out +9 -0
@@ -76,6 +76,15 @@ alternate UserDefAlternate object UserDefC member string1: str optional=False member string2: str optional=False +object q_obj_UserDefFlatUnion3-base + member enum1: EnumOne optional=True +object UserDefFlatUnion3 + base q_obj_UserDefFlatUnion3-base + tag enum1 + default variant: value1 + case value1: UserDefA + case value2: UserDefB + case value3: UserDefB alternate AltEnumBool tag type case e: EnumOne
Signature · 1 line
2.17.1
PATCH v2 4/10 qapi: Formalize qcow2 encryption probing
In reply to Max ReitzCurrently, you can give no encryption format for a qcow2 file while still passing a key-secret. That does not conform to the schema, so this patch changes the schema to allow it.
--- qapi/block-core.json | 44 ++++++++++++++++++++++++++++++++++++++++---- block/qcow2.c | 3 +++ 2 files changed, 43 insertions(+), 4 deletions(-)
qapi/block-core.json +40 -4
@@ -44,6 +44,19 @@ { 'struct': 'ImageInfoSpecificQCow2EncryptionBase', 'data': { 'format': 'BlockdevQcow2EncryptionFormat'}} +## +# @ImageInfoSpecificQCow2EncryptionNoInfo: +# +# Only used for the qcow2 encryption format "auto" in which the actual +# encryption format is determined from the image header. Therefore, +# this encryption format will never be reported in +# ImageInfoSpecificQCow2Encryption. +# +# Since: 3.0 +## +{ 'struct': 'ImageInfoSpecificQCow2EncryptionNoInfo', + 'data': { } } + ## # @ImageInfoSpecificQCow2Encryption: #
@@ -53,7 +66,8 @@ 'base': 'ImageInfoSpecificQCow2EncryptionBase', 'discriminator': 'format', 'data': { 'aes': 'QCryptoBlockInfoQCow', - 'luks': 'QCryptoBlockInfoLUKS' } } + 'luks': 'QCryptoBlockInfoLUKS', + 'auto': 'ImageInfoSpecificQCow2EncryptionNoInfo' } } ## # @ImageInfoSpecificQCow2:
@@ -2658,10 +2672,30 @@ # @BlockdevQcow2EncryptionFormat: # @aes: AES-CBC with plain64 initialization venctors # +# @auto: Determine the encryption format from the image +# header. This only allows the use of the +# key-secret option. (Since: 3.0) +# # Since: 2.10 ## { 'enum': 'BlockdevQcow2EncryptionFormat', - 'data': [ 'aes', 'luks' ] } + 'data': [ 'aes', 'luks', 'auto' ] } + +## +# @BlockdevQcow2EncryptionSecret: +# +# Allows specifying a key-secret without specifying the exact +# encryption format, which is determined automatically from the image +# header. +# +# @key-secret: The ID of a QCryptoSecret object providing the +# decryption key. Mandatory except when probing +# image for metadata only. +# +# Since: 3.0 +## +{ 'struct': 'BlockdevQcow2EncryptionSecret', + 'data': { '*key-secret': 'str' } } ## # @BlockdevQcow2Encryption:
@@ -2669,10 +2703,12 @@ # Since: 2.10 ## { 'union': 'BlockdevQcow2Encryption', - 'base': { 'format': 'BlockdevQcow2EncryptionFormat' }, + 'base': { '*format': 'BlockdevQcow2EncryptionFormat' }, 'discriminator': 'format', + 'default-variant': 'auto', 'data': { 'aes': 'QCryptoBlockOptionsQCow', - 'luks': 'QCryptoBlockOptionsLUKS'} } + 'luks': 'QCryptoBlockOptionsLUKS', + 'auto': 'BlockdevQcow2EncryptionSecret' } } ## # @BlockdevOptionsQcow2:
block/qcow2.c +3 -0
@@ -868,6 +868,9 @@ static int qcow2_update_options_prepare(BlockDriverState *bs,qdict_extract_subqdict (options, &encryptopts,"encrypt." ); encryptfmt =qdict_get_try_str (encryptopts,"format" ); +if (!g_strcmp0 (encryptfmt,"auto" )) { + encryptfmt =NULL ; + } opts =qemu_opts_create (&qcow2_runtime_opts,NULL ,0 , &error_abort);qemu_opts_absorb_qdict (opts, options, &local_err);
Signature · 1 line
2.17.1
Re: [Qemu-devel] [PATCH v2 04/10] qapi: Formalize qcow2 encryption probing
Quoted 12 lines · Max Reitz
Currently, you can give no encryption format for a qcow2 file while still passing a key-secret. That does not conform to the schema, so this patch changes the schema to allow it.
Signed-off-by Max Reitz <[email protected]>--- qapi/block-core.json | 44 ++++++++++++++++++++++++++++++++++++++++---- block/qcow2.c | 3 +++ 2 files changed, 43 insertions(+), 4 deletions(-)qapi/block-core.json +2
-0+## +# @ImageInfoSpecificQCow2EncryptionNoInfo:
qapi/block-core.json line 58
Quoted 10 lines
qapi/block-core.json +10
-0+# +# Only used for the qcow2 encryption format "auto" in which the actual +# encryption format is determined from the image header. Therefore, +# this encryption format will never be reported in +# ImageInfoSpecificQCow2Encryption. +# +# Since: 3.0 +## +{ 'struct': 'ImageInfoSpecificQCow2EncryptionNoInfo', + 'data': { } }
Do we actually need this type, given Anton's work on making omitted branches automatically use an empty struct?
https://lists.gnu.org/archive/html/qemu-devel/2018-05/msg06836.html
qapi/block-core.json line 70
Quoted 11 lines
qapi/block-core.json +3
-1+ ## # @ImageInfoSpecificQCow2Encryption: #@@ -53,7 +66,8 @@ 'base': 'ImageInfoSpecificQCow2EncryptionBase', 'discriminator': 'format', 'data': { 'aes': 'QCryptoBlockInfoQCow', - 'luks': 'QCryptoBlockInfoLUKS' } } + 'luks': 'QCryptoBlockInfoLUKS', + 'auto': 'ImageInfoSpecificQCow2EncryptionNoInfo' } }
If Anton's patches go in first, you don't even have to change this type;
Quoted 6 lines
qapi/block-core.json +0
-0## # @ImageInfoSpecificQCow2:@@ -2658,10 +2672,30 @@ # @BlockdevQcow2EncryptionFormat: # @aes: AES-CBC with plain64 initialization venctors
qapi/block-core.json line 2682
Quoted 10 lines
qapi/block-core.json +5
-1# +# @auto: Determine the encryption format from the image +# header. This only allows the use of the +# key-secret option. (Since: 3.0) +# # Since: 2.10 ## { 'enum': 'BlockdevQcow2EncryptionFormat', - 'data': [ 'aes', 'luks' ] } + 'data': [ 'aes', 'luks', 'auto' ] }
the changed enum would be sufficient.
Quoted 21 lines
qapi/block-core.json +16
-0+ +## +# @BlockdevQcow2EncryptionSecret: +# +# Allows specifying a key-secret without specifying the exact +# encryption format, which is determined automatically from the image +# header. +# +# @key-secret: The ID of a QCryptoSecret object providing the +# decryption key. Mandatory except when probing +# image for metadata only. +# +# Since: 3.0 +## +{ 'struct': 'BlockdevQcow2EncryptionSecret', + 'data': { '*key-secret': 'str' } } ## # @BlockdevQcow2Encryption:@@ -2669,10 +2703,12 @@ # Since: 2.10
qapi/block-core.json line 2711
Quoted 10 lines
qapi/block-core.json +4
-2## { 'union': 'BlockdevQcow2Encryption', - 'base': { 'format': 'BlockdevQcow2EncryptionFormat' }, + 'base': { '*format': 'BlockdevQcow2EncryptionFormat' }, 'discriminator': 'format', + 'default-variant': 'auto', 'data': { 'aes': 'QCryptoBlockOptionsQCow', - 'luks': 'QCryptoBlockOptionsLUKS'} } + 'luks': 'QCryptoBlockOptionsLUKS', + 'auto': 'BlockdevQcow2EncryptionSecret' } }
This part is necessary, though, and looks correct.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [Qemu-devel] [PATCH v2 04/10] qapi: Formalize qcow2 encryption probing
Quoted 27 lines · Eric Blake
Quoted 12 lines · Max Reitz
Currently, you can give no encryption format for a qcow2 file while still passing a key-secret. That does not conform to the schema, so this patch changes the schema to allow it.
Signed-off-by Max Reitz <[email protected]>---
qapi/block-core.json| 44 ++++++++++++++++++++++++++++++++++++++++---- block/qcow2.c | 3 +++2 files changed, 43 insertions(+), 4 deletions(-)qapi/block-core.json +2
-0+## +# @ImageInfoSpecificQCow2EncryptionNoInfo:qapi/block-core.json line 58
Quoted 10 lines
qapi/block-core.json +10
-0+# +# Only used for the qcow2 encryption format "auto" in which the actual +# encryption format is determined from the image header. Therefore, +# this encryption format will never be reported in +# ImageInfoSpecificQCow2Encryption. +# +# Since: 3.0 +## +{ 'struct': 'ImageInfoSpecificQCow2EncryptionNoInfo', + 'data': { } }Do we actually need this type, given Anton's work on making omitted branches automatically use an empty struct?
Looks like no, we don't. Great! :-)
I think I'll still keep part of the comment and move it down into the description of ImageInfoSpecificQCow2Encryption so that anyone who's wondering knows that this value won't appear.
Thanks for reviewing and pointing me at it,
Max
Re: [Qemu-devel] [PATCH v2 04/10] qapi: Formalize qcow2 encryption probing
In reply to Max ReitzQuoted 9 lines · Max Reitz
Currently, you can give no encryption format for a qcow2 file while still passing a key-secret. That does not conform to the schema, so this patch changes the schema to allow it.
Signed-off-by Max Reitz <[email protected]>--- qapi/block-core.json | 44 ++++++++++++++++++++++++++++++++++++++++---- block/qcow2.c | 3 +++ 2 files changed, 43 insertions(+), 4 deletions(-)
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
PATCH v2 5/10 qapi: Formalize qcow encryption probing
In reply to Max Reitzqcow only supports a single encryption (and there is no reason why that would change in the future), so we can make it the default.
--- qapi/block-core.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
qapi/block-core.json +2 -1
@@ -2647,8 +2647,9 @@ # Since: 2.10 ## { 'union': 'BlockdevQcowEncryption', - 'base': { 'format': 'BlockdevQcowEncryptionFormat' }, + 'base': { '*format': 'BlockdevQcowEncryptionFormat' }, 'discriminator': 'format', + 'default-variant': 'aes', 'data': { 'aes': 'QCryptoBlockOptionsQCow' } } ##
Signature · 1 line
2.17.1
Re: [Qemu-devel] [PATCH v2 05/10] qapi: Formalize qcow encryption probing
Quoted 7 lines · Max Reitz
qcow only supports a single encryption (and there is no reason why that would change in the future), so we can make it the default.
Signed-off-by Max Reitz <[email protected]>--- qapi/block-core.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 17 lines
qapi/block-core.json +2
-1@@ -2647,8 +2647,9 @@ # Since: 2.10 ## { 'union': 'BlockdevQcowEncryption', - 'base': { 'format': 'BlockdevQcowEncryptionFormat' }, + 'base': { '*format': 'BlockdevQcowEncryptionFormat' }, 'discriminator': 'format', + 'default-variant': 'aes', 'data': { 'aes': 'QCryptoBlockOptionsQCow' } } ##
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [Qemu-devel] [PATCH v2 05/10] qapi: Formalize qcow encryption probing
In reply to Max ReitzQuoted 7 lines · Max Reitz
qcow only supports a single encryption (and there is no reason why that would change in the future), so we can make it the default.
Signed-off-by Max Reitz <[email protected]>--- qapi/block-core.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
PATCH v2 6/10 qdict: Make qdict_flatten() shallow-clone-friendly
In reply to Max ReitzIn its current form, qdict_flatten() removes all entries from nested QDicts that are moved to the root QDict. It is completely sufficient to remove all old entries from the root QDict, however. If the nested dicts have a refcount of 1, this will automatically delete them, too. And if they have a greater refcount, we probably do not want to modify them in the first place.
The latter observation means that it was currently (in general) impossible to qdict_flatten() a shallowly cloned dict because that would empty nested QDicts in the original dict as well. This patch changes this, so you can now use qdict_flatten(qdict_shallow_clone(dict)) to get a flattened copy without disturbing the original.
--- qobject/block-qdict.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)
qobject/block-qdict.c +15 -4
@@ -114,19 +114,30 @@ static void qdict_flatten_qdict(QDict *qdict, QDict *target, const char *prefix)/* * Flatten non-empty QDict and QList recursively into @target, -* copy other objects to @target +* copy other objects to @target. +* On the root level (if @qdict == @target), remove flattened +* nested QDicts and QLists from @qdict. +* +* (Note that we do not need to remove entries from nested +* dicts or lists. Their reference count is decremented on +* the root level, so there are no leaks. In fact, if they +* have a reference count greater than one, we are probably +* well advised not to modify them altogether.) */ if (dict_val &&qdict_size (dict_val)) {qdict_flatten_qdict (dict_val, target, new_key ? new_key : entry->key); -qdict_del (qdict, entry->key); +if (target == qdict) { +qdict_del (qdict, entry->key); + } }else if (list_val && !qlist_empty (list_val)) {qdict_flatten_qlist (list_val, target, new_key ? new_key : entry->key); -qdict_del (qdict, entry->key); +if (target == qdict) { +qdict_del (qdict, entry->key); + } }else if (target != qdict) {qdict_put_obj (target, new_key,qobject_ref (value)); -qdict_del (qdict, entry->key); }g_free (new_key);
Signature · 1 line
2.17.1
Re: [Qemu-devel] [PATCH v2 06/10] qdict: Make qdict_flatten() shallow-clone-friendly
Quoted 47 lines · Max Reitz
In its current form,
qdict_flatten()removes all entries from nested QDicts that are moved to the root QDict. It is completely sufficient to remove all old entries from the root QDict, however. If the nested dicts have a refcount of 1, this will automatically delete them, too. And if they have a greater refcount, we probably do not want to modify them in the first place.The latter observation means that it was currently (in general) impossible to
qdict_flatten()a shallowly cloned dict because that would empty nested QDicts in the original dict as well. This patch changes this, so you can now use qdict_flatten(qdict_shallow_clone(dict)) to get a flattened copy without disturbing the original.Signed-off-by Max Reitz <[email protected]>--- qobject/block-qdict.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)qobject/block-qdict.c +12
-2@@ -114,19 +114,30 @@ static void qdict_flatten_qdict(QDict *qdict, QDict *target, const char *prefix)/* * Flatten non-empty QDict and QList recursively into @target, -* copy other objects to @target +* copy other objects to @target. +* On the root level (if @qdict == @target), remove flattened +* nested QDicts and QLists from @qdict. +* +* (Note that we do not need to remove entries from nested +* dicts or lists. Their reference count is decremented on +* the root level, so there are no leaks. In fact, if they +* have a reference count greater than one, we are probably +* well advised not to modify them altogether.) */ if (dict_val &&qdict_size (dict_val)) {qdict_flatten_qdict (dict_val, target, new_key ? new_key : entry->key); -qdict_del (qdict, entry->key); +if (target == qdict) { +qdict_del (qdict, entry->key); + } }else if (list_val && !qlist_empty (list_val)) {qdict_flatten_qlist (list_val, target, new_key ? new_key : entry->key);
qobject/block-qdict.c static void qdict_flatten_qdict(QDict *qdict, QDict *target, const char *prefix) · line 143
Quoted 10 lines
qobject/block-qdict.c +3
-2-qdict_del (qdict, entry->key); +if (target == qdict) { +qdict_del (qdict, entry->key); + } }else if (target != qdict) {qdict_put_obj (target, new_key,qobject_ref (value)); -qdict_del (qdict, entry->key); }g_free (new_key);
This simply makes sense, even without the later patch(es) that rely on it.
PATCH v2 7/10 tests: Add QDict clone-flatten test
In reply to Max ReitzThis new test verifies that qdict_flatten() does not modify a shallow clone of the given QDict.
--- tests/check-block-qdict.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)
tests/check-block-qdict.c +33 -0
@@ -125,6 +125,38 @@ static void qdict_flatten_test(void)qobject_unref (root); } +static void qdict_clone_flatten_test (void ) +{ +QDict *dict1 =qdict_new (); +QDict *dict2 =qdict_new (); +QDict *cloned_dict1; + +/* +* Test that we can clone and flatten +* { "a": { "b": 42 } } +* without modifying the clone. +*/ + +qdict_put_int (dict2,"b" ,42 ); +qdict_put (dict1,"a" , dict2); + + cloned_dict1 =qdict_clone_shallow (dict1); + +qdict_flatten (dict1); + +g_assert (qdict_size (dict1) ==1 ); +g_assert (qdict_get_int (dict1,"a.b" ) ==42 ); + +g_assert (qdict_size (cloned_dict1) ==1 ); +g_assert (qdict_get_qdict (cloned_dict1,"a" ) == dict2); + +g_assert (qdict_size (dict2) ==1 ); +g_assert (qdict_get_int (dict2,"b" ) ==42 ); + +qobject_unref (dict1); +qobject_unref (cloned_dict1); +} +static void qdict_array_split_test (void ) {QDict *test_dict =qdict_new ();
@@ -674,6 +706,7 @@ int main(int argc, char **argv)g_test_add_func ("/public/defaults" , qdict_defaults_test);g_test_add_func ("/public/flatten" , qdict_flatten_test); +g_test_add_func ("/public/clone_flatten" , qdict_clone_flatten_test);g_test_add_func ("/public/array_split" , qdict_array_split_test);g_test_add_func ("/public/array_entries" , qdict_array_entries_test);g_test_add_func ("/public/join" , qdict_join_test);
Signature · 1 line
2.17.1
Re: [Qemu-devel] [PATCH v2 07/10] tests: Add QDict clone-flatten test
Quoted 5 lines · Max Reitz
This new test verifies that
qdict_flatten()does not modify a shallow clone of the given QDict.Reviewed-by Eric Blake <[email protected]>Signed-off-by Max Reitz <[email protected]>
PATCH v2 8/10 block: Try to create well typed json:{} filenames
In reply to Max ReitzBy applying a health mix of qdict_flatten(), qdict_crumple(), qdict_stringify_for_keyval(), the keyval input visitor, and the QObject output visitor (not necessarily in that order), we can at least try to bring bs->full_open_options into accordance with the QAPI schema. This may not always work (there are some options left that have not been QAPI-fied yet), but in practice it usually will.
In any case, sometimes emitting wrongly typed json:{} filenames is better than doing it effectively half the time.
This affects some iotests because json:{} filenames are now usually crumpled. In 198, "format": "auto" now appears in the qcow2 encryption options because going through a visitor makes optional discriminators' default values explicit.
--- block.c | 66 +++++++++++++++++++++++++++++++++++++- tests/qemu-iotests/059.out | 2 +- tests/qemu-iotests/099.out | 4 +-- tests/qemu-iotests/110.out | 2 +- tests/qemu-iotests/198.out | 4 +-- tests/qemu-iotests/207.out | 10 +++--- 6 files changed, 76 insertions(+), 12 deletions(-)
block.c +65 -1
@@ -36,6 +36,7 @@#include "qapi/qmp/qjson.h" #include "qapi/qmp/qnull.h" #include "qapi/qmp/qstring.h" +#include "qapi/qobject-input-visitor.h" #include "qapi/qobject-output-visitor.h" #include "qapi/qapi-visit-block-core.h" #include "sysemu/block-backend.h"
@@ -5158,6 +5159,56 @@ static bool append_open_options(QDict *d, BlockDriverState *bs)return found_any; } +/** +* Take a blockdev @options QDict and convert its values to the +* correct type. +* +* Fail if @options does not match the QAPI schema of BlockdevOptions. +* +* In case of failure, return NULL and set @errp. +* +* In case of success, return a correctly typed new QDict. +*/ +static QDict *bdrv_type_blockdev_opts (const QDict *options,Error **errp) +{ +Visitor *v; +BlockdevOptions *blockdev_options; +QObject *typed_opts; +QDict *string_options; +Error *local_err =NULL ; + + string_options =qdict_clone_shallow (options); + +qdict_flatten (string_options); + v =qobject_input_visitor_new_flat_confused (string_options, errp); +if (!v) { +error_prepend (errp,"Failed to prepare options: " ); +return NULL ; + } + +visit_type_BlockdevOptions (v,NULL , &blockdev_options, &local_err); +visit_free (v); +if (local_err) { +error_propagate (errp, local_err); +error_prepend (errp,"Not a valid BlockdevOptions object: " ); +return NULL ; + } + + v =qobject_output_visitor_new (&typed_opts); +visit_type_BlockdevOptions (v,NULL , &blockdev_options, &local_err); +if (!local_err) { +visit_complete (v, &typed_opts); + } +visit_free (v); +qapi_free_BlockdevOptions (blockdev_options); +if (local_err) { +error_propagate (errp, local_err); +return NULL ; + } + +return qobject_to (QDict, typed_opts); +} +/* Updates the following BDS fields: * - exact_filename: A filename which may be used for opening a block device * which (mostly) equals the given BDS (even without any
@@ -5259,10 +5310,23 @@ void bdrv_refresh_filename(BlockDriverState *bs)if (bs->exact_filename[0 ]) {pstrcpy (bs->filename,sizeof (bs->filename), bs->exact_filename); }else if (bs->full_open_options) { -QString *json =qobject_to_json (QOBJECT (bs->full_open_options)); +QString *json; +QDict *typed_opts, *json_opts; + + typed_opts =bdrv_type_blockdev_opts (bs->full_open_options,NULL ); + +/* We cannot be certain that bs->full_open_options matches +* BlockdevOptions, so bdrv_type_blockdev_opts() may fail. +* That is not fatal, we can just emit bs->full_open_options +* directly -- qemu will accept that, even if it does not +* match the schema. */ + json_opts = typed_opts ?: bs->full_open_options; + + json =qobject_to_json (QOBJECT (json_opts));snprintf (bs->filename,sizeof (bs->filename),"json:%s" ,qstring_get_str (json));qobject_unref (json); +qobject_unref (typed_opts); } }
tests/qemu-iotests/059.out +1 -1
@@ -2050,7 +2050,7 @@ wrote 512/512 bytes at offset 10240 === Testing monolithicFlat with internally generated JSON file name === Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864 subformat=monolithicFlat -can't open: Cannot use relative extent paths with VMDK descriptor file 'json:{"image": {"driver": "file", "filename": "TEST_DIR/t.IMGFMT"}, "driver": "blkdebug", "inject-error.0.event": "read_aio"}' +can't open: Cannot use relative extent paths with VMDK descriptor file 'json:{"inject-error": [{"event": "read_aio"}], "image": {"driver": "file", "filename": "TEST_DIR/t.IMGFMT"}, "driver": "blkdebug"}' === Testing version 3 === image: TEST_DIR/iotest-version3.IMGFMT
tests/qemu-iotests/099.out +2 -2
@@ -12,11 +12,11 @@ blkverify:TEST_DIR/t.IMGFMT.compare:TEST_DIR/t.IMGFMT === Testing JSON filename for blkdebug === -json:{"driver": "IMGFMT", "file": {"image": {"driver": "file", "filename": "TEST_DIR/t.IMGFMT"}, "driver": "blkdebug", "inject-error.0.event": "l1_update"}} +json:{"driver": "IMGFMT", "file": {"inject-error": [{"event": "l1_update"}], "image": {"driver": "file", "filename": "TEST_DIR/t.IMGFMT"}, "driver": "blkdebug"}} === Testing indirectly enforced JSON filename === -json:{"driver": "raw", "file": {"test": {"driver": "IMGFMT", "file": {"image": {"driver": "file", "filename": "TEST_DIR/t.IMGFMT"}, "driver": "blkdebug", "inject-error.0.event": "l1_update"}}, "driver": "blkverify", "raw": {"driver": "file", "filename": "TEST_DIR/t.IMGFMT.compare"}}} +json:{"driver": "raw", "file": {"test": {"driver": "IMGFMT", "file": {"inject-error": [{"event": "l1_update"}], "image": {"driver": "file", "filename": "TEST_DIR/t.IMGFMT"}, "driver": "blkdebug"}}, "driver": "blkverify", "raw": {"driver": "file", "filename": "TEST_DIR/t.IMGFMT.compare"}}} === Testing plain filename for blkdebug ===
tests/qemu-iotests/110.out +1 -1
@@ -11,7 +11,7 @@ backing file: t.IMGFMT.base (actual path: TEST_DIR/t.IMGFMT.base) === Non-reconstructable filename === -image: json:{"driver": "IMGFMT", "file": {"set-state.0.event": "read_aio", "image": {"driver": "file", "filename": "TEST_DIR/t.IMGFMT"}, "driver": "blkdebug", "set-state.0.new_state": 42}} +image: json:{"driver": "IMGFMT", "file": {"set-state": [{"new_state": 42, "event": "read_aio"}], "image": {"driver": "file", "filename": "TEST_DIR/t.IMGFMT"}, "driver": "blkdebug"}} file format: IMGFMT virtual size: 64M (67108864 bytes) backing file: t.IMGFMT.base (cannot determine actual path)
tests/qemu-iotests/198.out +2 -2
@@ -32,7 +32,7 @@ read 16777216/16777216 bytes at offset 0 16 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) == checking image base == -image: json:{"encrypt.key-secret": "sec0", "driver": "IMGFMT", "file": {"driver": "file", "filename": "TEST_DIR/t.IMGFMT.base"}} +image: json:{"driver": "IMGFMT", "encrypt": {"format": "auto", "key-secret": "sec0"}, "file": {"driver": "file", "filename": "TEST_DIR/t.IMGFMT.base"}} file format: IMGFMT virtual size: 16M (16777216 bytes) Format specific information:
@@ -74,7 +74,7 @@ Format specific information: master key iters: 1024 == checking image layer == -image: json:{"encrypt.key-secret": "sec1", "driver": "IMGFMT", "file": {"driver": "file", "filename": "TEST_DIR/t.IMGFMT"}} +image: json:{"driver": "IMGFMT", "encrypt": {"format": "auto", "key-secret": "sec1"}, "file": {"driver": "file", "filename": "TEST_DIR/t.IMGFMT"}} file format: IMGFMT virtual size: 16M (16777216 bytes) backing file: TEST_DIR/t.IMGFMT.base
tests/qemu-iotests/207.out +5 -5
@@ -5,7 +5,7 @@ {'execute': 'job-dismiss', 'arguments': {'id': 'job0'}} {u'return': {}} -image: json:{"driver": "IMGFMT", "file": {"server.host": "127.0.0.1", "server.port": "22", "driver": "ssh", "path": "TEST_IMG"}} +image: json:{"driver": "IMGFMT", "file": {"driver": "ssh", "path": "TEST_IMG", "server": {"port": "22", "host": "127.0.0.1"}}} file format: IMGFMT virtual size: 4.0M (4194304 bytes)
@@ -21,7 +21,7 @@ virtual size: 4.0M (4194304 bytes) {'execute': 'job-dismiss', 'arguments': {'id': 'job0'}} {u'return': {}} -image: json:{"driver": "IMGFMT", "file": {"server.host": "127.0.0.1", "server.port": "22", "driver": "ssh", "path": "TEST_IMG"}} +image: json:{"driver": "IMGFMT", "file": {"driver": "ssh", "path": "TEST_IMG", "server": {"port": "22", "host": "127.0.0.1"}}} file format: IMGFMT virtual size: 8.0M (8388608 bytes)
@@ -30,7 +30,7 @@ virtual size: 8.0M (8388608 bytes) {'execute': 'job-dismiss', 'arguments': {'id': 'job0'}} {u'return': {}} -image: json:{"driver": "IMGFMT", "file": {"server.host": "127.0.0.1", "server.port": "22", "driver": "ssh", "path": "TEST_IMG"}} +image: json:{"driver": "IMGFMT", "file": {"driver": "ssh", "path": "TEST_IMG", "server": {"port": "22", "host": "127.0.0.1"}}} file format: IMGFMT virtual size: 4.0M (4194304 bytes)
@@ -45,7 +45,7 @@ Job failed: remote host key does not match host_key_check 'wrong' {'execute': 'job-dismiss', 'arguments': {'id': 'job0'}} {u'return': {}} -image: json:{"driver": "IMGFMT", "file": {"server.host": "127.0.0.1", "server.port": "22", "driver": "ssh", "path": "TEST_IMG"}} +image: json:{"driver": "IMGFMT", "file": {"driver": "ssh", "path": "TEST_IMG", "server": {"port": "22", "host": "127.0.0.1"}}} file format: IMGFMT virtual size: 8.0M (8388608 bytes)
@@ -60,7 +60,7 @@ Job failed: remote host key does not match host_key_check 'wrong' {'execute': 'job-dismiss', 'arguments': {'id': 'job0'}} {u'return': {}} -image: json:{"driver": "IMGFMT", "file": {"server.host": "127.0.0.1", "server.port": "22", "driver": "ssh", "path": "TEST_IMG"}} +image: json:{"driver": "IMGFMT", "file": {"driver": "ssh", "path": "TEST_IMG", "server": {"port": "22", "host": "127.0.0.1"}}} file format: IMGFMT virtual size: 4.0M (4194304 bytes)
Signature · 1 line
2.17.1
PATCH v2 9/10 iotests: Test internal option typing
In reply to Max ReitzIt would be nice if qemu used the correct types for blockdev options internally, even if the user specified string values (either through -drive or by being not so nice and using json:{} with string values). This patch adds a test verifying that fact.
--- tests/qemu-iotests/089 | 25 +++++++++++++++++++++++++ tests/qemu-iotests/089.out | 9 +++++++++ 2 files changed, 34 insertions(+)
tests/qemu-iotests/089 +25 -0
@@ -36,6 +36,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 # get standard environment, filters and checks . ./common.rc . ./common.filter +. ./common.qemu _supported_fmt qcow2 _supported_proto file
@@ -145,6 +146,30 @@ $QEMU_IO -c "open -o driver=qcow2 json:{\"file.filename\":\"$TEST_IMG\"}" \ $QEMU_IO -c "open -o driver=qcow2 json:{\"driver\":\"raw\",\"file.filename\":\"$TEST_IMG\"}" \ -c "info" 2>&1 | _filter_img_info +echo +echo "=== Testing option typing ===" +echo + +# json:{} accepts both strings and correctly typed values (even mixed, +# although we probably do not want to support that...), but when +# creating a json:{} filename, it should be correctly typed. +# Therefore, both of these should make the "size" value an integer. + +TEST_IMG="json:{'driver': 'null-co', 'size': 42 }" _img_info | grep '^image' +TEST_IMG="json:{'driver': 'null-co', 'size': '42'}" _img_info | grep '^image' + +echo + +# This should even work when some driver abuses bs->options to store +# non-QAPI options (and the given -drive options are not complete) +# (Watch for whether file.align appears as an int or a string) +qemu_comm_method=monitor _launch_qemu \ + -drive if=none,id=drv0,node-name=node0,format=raw,file=blkdebug::null-co://,file.align=512 + +_send_qemu_cmd $QEMU_HANDLE 'info block' 'json:' + +_cleanup_qemu + # success, all done echo "*** done"
tests/qemu-iotests/089.out +9 -0
@@ -49,4 +49,13 @@ vm state offset: 512 MiB format name: IMGFMT cluster size: 64 KiB vm state offset: 512 MiB + +=== Testing option typing === + +image: json:{"driver": "null-co", "size": 42} +image: json:{"driver": "null-co", "size": 42} + +QEMU X.Y.Z monitor - type 'help' for more information +(qemu) info block +drv0 (node0): json:{"driver": "raw", "file": {"image": {"driver": "null-co"}, "driver": "blkdebug", "align": 512}} (raw) *** done
Signature · 1 line
2.17.1
PATCH v2 10/10 iotests: qcow2's encrypt.format is now optional
In reply to Max ReitzRemove the encrypt.format option from the two blockdev-add test cases for encrypted qcow2 images in 087 to explicitly test that this parameter is now optional.
Additionally, show that explicitly specifying encrypt.format=auto works just as well, the same for specifying the correct format (encrypt.format=luks here), and that specifying the wrong format (encrypt.format=aes) results in an error.
While touching this test case, reduce the LUKS iteration time to 10 so the test stays reasonably fast.
--- tests/qemu-iotests/087 | 65 +++++++++++++++++++++++--------------- tests/qemu-iotests/087.out | 26 ++++++++++++++- 2 files changed, 64 insertions(+), 27 deletions(-)
tests/qemu-iotests/087 +39 -26
@@ -150,7 +150,6 @@ run_qemu <<EOF "filename": "$TEST_IMG" }, "encrypt": { - "format": "aes", "key-secret": "sec0" } }
@@ -162,34 +161,48 @@ echo echo === Encrypted image LUKS === echo -_make_test_img --object secret,id=sec0,data=123456 -o encrypt.format=luks,encrypt.key-secret=sec0 $size -run_qemu <<EOF -{ "execute": "qmp_capabilities" } -{ "execute": "object-add", - "arguments": { - "qom-type": "secret", - "id": "sec0", - "props": { - "data": "123456" - } - } -} -{ "execute": "blockdev-add", - "arguments": { - "driver": "$IMGFMT", - "node-name": "disk", - "file": { - "driver": "file", - "filename": "$TEST_IMG" - }, - "encrypt": { - "format": "luks", - "key-secret": "sec0" +_make_test_img \ + --object secret,id=sec0,data=123456 \ + -o encrypt.format=luks,encrypt.key-secret=sec0,encrypt.iter-time=10 \ + $size + +# Adds the qcow2+LUKS image via blockdev-add. +# First parameter: Optional entry for the 'encrypt' option dict +function luks_test() +{ + run_qemu <<EOF + { "execute": "qmp_capabilities" } + { "execute": "object-add", + "arguments": { + "qom-type": "secret", + "id": "sec0", + "props": { + "data": "123456" + } } } - } -{ "execute": "quit" } + { "execute": "blockdev-add", + "arguments": { + "driver": "$IMGFMT", + "node-name": "disk", + "file": { + "driver": "file", + "filename": "$TEST_IMG" + }, + "encrypt": { + $1 + "key-secret": "sec0" + } + } + } + { "execute": "quit" } EOF +} + +luks_test '' # Implicit encrypt.format=auto +luks_test '"format": "auto",' # Explicit encrypt.format=auto +luks_test '"format": "luks",' # Explicit encrypt.format=luks +luks_test '"format": "aes",' # Explicit encrypt.format=aes (wrong) echo echo === Missing driver ===
tests/qemu-iotests/087.out +25 -1
@@ -46,7 +46,7 @@ QMP_VERSION === Encrypted image LUKS === -Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728 encrypt.format=luks encrypt.key-secret=sec0 +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728 encrypt.format=luks encrypt.key-secret=sec0 encrypt.iter-time=10 Testing: QMP_VERSION {"return": {}}
@@ -55,6 +55,30 @@ QMP_VERSION {"return": {}} {"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event": "SHUTDOWN", "data": {"guest": false}} +Testing: +QMP_VERSION +{"return": {}} +{"return": {}} +{"return": {}} +{"return": {}} +{"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event": "SHUTDOWN", "data": {"guest": false}} + +Testing: +QMP_VERSION +{"return": {}} +{"return": {}} +{"return": {}} +{"return": {}} +{"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event": "SHUTDOWN", "data": {"guest": false}} + +Testing: +QMP_VERSION +{"return": {}} +{"return": {}} +{"error": {"class": "GenericError", "desc": "Header reported 'luks' encryption format but options specify 'aes'"}} +{"return": {}} +{"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event": "SHUTDOWN", "data": {"guest": false}} + === Missing driver ===
Signature · 1 line
2.17.1
Re: [Qemu-devel] [PATCH v2 10/10] iotests: qcow2's encrypt.format is now optional
Quoted 9 lines · Max Reitz
Remove the encrypt.format option from the two blockdev-add test cases for encrypted qcow2 images in 087 to explicitly test that this parameter is now optional.
Additionally, show that explicitly specifying encrypt.format=auto works just as well, the same for specifying the correct format (encrypt.format=luks here), and that specifying the wrong format (encrypt.format=aes) results in an error.
Quoted 8 lines
While touching this test case, reduce the LUKS iteration time to 10 so the test stays reasonably fast.
Signed-off-by Max Reitz <[email protected]>--- tests/qemu-iotests/087 | 65 +++++++++++++++++++++++--------------- tests/qemu-iotests/087.out | 26 ++++++++++++++- 2 files changed, 64 insertions(+), 27 deletions(-)
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Quoted 8 lines · Max Reitz
See cover letter of v1 here:
http://lists.nongnu.org/archive/html/qemu-block/2018-05/msg00290.htmlThis series depends on Markus's series "block: Configuration fixes and rbd authentication":
Based-on: <[email protected]>
PATCH 06+07 applied to qapi-next as fix of latent bug. Thanks!